-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremove-eb-migration-transient.php
210 lines (176 loc) · 6.02 KB
/
remove-eb-migration-transient.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
/**
* Plugin Name: Eventbrite Tickets Extension: Clean Up Migration Data
* Description: Older versions of Eventbrite Tickets would sometimes add a transient with no expiration date. This extension removes that transient if it exists.
* Version: 1.0.0
* Author: Modern Tribe, Inc.
* Author URI: http://m.tri.be/1971
* License: GPLv2 or later
*/
defined( 'WPINC' ) or die;
class Tribe__Extension__Remove_Eventbrite_Migration_Transient {
/**
* The name of the migration transient that we may be deleting.
*
* @since 1.0.0
*
* @var string
*/
public static $transient_key = '_tribe_eventbrite_is_migrating';
/**
* The name of the option where we store a simple boolean if the old transient was deleted.
*
* @since 1.0.0
*
* @var string
*/
public static $deleted_key = '_tribe_deleted_old_migration_transient';
/**
* The path to this extension's main plugin file.
*
* @since 1.0.0
*
* @var string
*/
public $extension_path = '';
/**
* Extension version.
*
* @var string
*/
const VERSION = '1.0.0';
/**
* Required plugins as defined by their Main classes.
*
* @since 1.0.0
*
* @var string
*/
public $plugins_required = array(
'Tribe__Events__Main' => '4.7',
'Tribe__Events__Tickets__Eventbrite__Main' => '4.5'
);
/**
* Ensures extension hooks run well after main plugin hooks.
*
* @since 1.0.0
*/
public function __construct() {
$this->extension_path = plugin_basename( __FILE__ );
add_action( 'plugins_loaded', array( $this, 'init' ), 100 );
}
/**
* Simple check for whether the extension's okay to run.
*
* @since 1.0.0
*
* @return boolean
*/
public function extension_should_run() {
return function_exists( 'tribe_register_plugin' ) && tribe_register_plugin( __FILE__, __CLASS__, self::VERSION, $this->plugins_required );
}
/**
* Extension hooks.
*
* @since 1.0.0
*/
public function init() {
if ( ! $this->extension_should_run() ) {
return false;
}
add_action( 'init', array( $this, 'maybe_delete_old_transient' ) );
add_action( 'after_plugin_row_' . $this->extension_path, array( $this, 'maybe_add_deactivation_notice' ), 10, 3 );
add_action( 'admin_head', array( $this, 'admin_head_css' ) );
}
/**
* If the migration transient exists already, *and* doesn't have an expiration.
*
* @since 1.0.0
*
* @return boolean
*/
public function old_transient_exists() {
$transient = get_transient( self::$transient_key );
$transient_timeout = (bool) get_option( '_transient_timeout_' . self::$transient_key );
return ! empty( $transient ) && false === $transient_timeout;
}
/**
* Look for the old transient and check if it's got no expiration date; delete it if so.
*
* @since 1.0.0
*
* @return bool true if successful, false otherwise
*/
public function maybe_delete_old_transient() {
if ( ! $this->old_transient_exists() ) {
add_option( self::$deleted_key, 'false' );
return false;
}
$deleted = delete_transient( self::$transient_key );
if ( $deleted ) {
add_option( self::$deleted_key, 'true' );
}
return $deleted;
}
/**
* A deactivation notice that shows under the extension's listing in the Plugins list table.
*
* @since 1.0.0
*/
public function maybe_add_deactivation_notice( $plugin_file, $plugin_data, $status ) {
if ( false === get_option( self::$deleted_key ) ) {
return false;
}
$message = esc_html__( 'No Eventbrite Tickets cleanup task was necessary.', 'tribe-extension' );
$dashicon = 'dashicons-flag';
if ( 'true' === get_option( self::$deleted_key ) ) {
$message = esc_html__( 'Success! Eventbrite\'s migration data was cleaned up.', 'tribe-extension' );
$dashicon = 'dashicons-yes';
}
printf(
'<tr class="active tribe--plugin-row-notice"><th class="check-column" scope="row"><span class="dashicons %1$s"></span></th><td><strong>%2$s</strong></td><td>%3$s</td></tr>',
$dashicon,
$message,
esc_html__( 'You can now deactivate this extension, then delete it.', 'tribe-extension' )
);
}
/**
* Some styles for the plugin row notice.
*
* @since 1.0.0
*/
public function admin_head_css() {
?><style>
tr[data-plugin="remove-eb-migration-transient.php"] td,
tr[data-plugin="remove-eb-migration-transient.php"] th {
box-shadow: none !important;
}
tr.tribe--plugin-row-notice .check-column {
text-align: center;
}
tr.tribe--plugin-row-notice .dashicons {
margin-left: 5px;
line-height: 1.25;
}
/* Convoluted selectors, but adds a mouseover effect that helps the two separate table rows appear as one unit. */
tr.active[data-plugin="remove-eb-migration-transient.php"]:hover td,
tr.active[data-plugin="remove-eb-migration-transient.php"]:hover th,
tr.active[data-plugin="remove-eb-migration-transient.php"]:hover + tr td,
tr.active[data-plugin="remove-eb-migration-transient.php"]:hover + tr th {
background: #fdfff2 !important;
}
</style><?php
}
/**
* Remove deletion flag from options upon deactivation.
*
* @since 1.0.0
*
* @return bool true if successful, false otherwise
*/
public static function cleanup_on_deactivation() {
return delete_option( self::$deleted_key );
}
}
register_deactivation_hook( __FILE__, array( 'Tribe__Extension__Remove_Eventbrite_Migration_Transient', 'cleanup_on_deactivation' ) );
new Tribe__Extension__Remove_Eventbrite_Migration_Transient();