1 | <?php |
||
20 | abstract class BD_Meta_Box_Module { |
||
21 | /** |
||
22 | * @var string Item Type. Possible values 'posts', 'pages', 'users' etc. |
||
23 | */ |
||
24 | protected $item_type = 'posts'; |
||
25 | |||
26 | /** |
||
27 | * @var string The screen where this meta box would be shown. |
||
28 | */ |
||
29 | protected $screen; |
||
30 | |||
31 | /** |
||
32 | * @var string Slug of the page where this module will be shown. |
||
33 | */ |
||
34 | protected $page_slug; |
||
35 | |||
36 | /** |
||
37 | * @var string Slug for the form fields. |
||
38 | */ |
||
39 | protected $field_slug; |
||
40 | |||
41 | /** |
||
42 | * @var string Slug of the meta box. |
||
43 | */ |
||
44 | protected $meta_box_slug; |
||
45 | |||
46 | /** |
||
47 | * @var string Hook in which this meta box should be shown. |
||
48 | */ |
||
49 | protected $meta_box_hook; |
||
50 | |||
51 | /** |
||
52 | * @var string Action in which the delete operation should be performed. |
||
53 | */ |
||
54 | protected $delete_action; |
||
55 | |||
56 | /** |
||
57 | * @var string Hook for scheduler. |
||
58 | */ |
||
59 | protected $cron_hook; |
||
60 | |||
61 | /** |
||
62 | * @var string Url of the scheduler addon. |
||
63 | */ |
||
64 | protected $scheduler_url; |
||
65 | |||
66 | /** |
||
67 | * @var array Messages shown to the user. |
||
68 | */ |
||
69 | protected $messages = array(); |
||
70 | |||
71 | /** |
||
72 | * Initialize and setup variables. |
||
73 | * |
||
74 | * @since 5.5 |
||
75 | * @abstract |
||
76 | * |
||
77 | * @return void |
||
78 | */ |
||
79 | abstract protected function initialize(); |
||
80 | |||
81 | /** |
||
82 | * Initialize and setup variables. |
||
83 | * |
||
84 | * @since 5.5 |
||
85 | * @abstract |
||
86 | * |
||
87 | * @return void |
||
88 | */ |
||
89 | abstract public function render(); |
||
90 | |||
91 | /** |
||
92 | * Process the deletion. |
||
93 | * |
||
94 | * @since 5.5 |
||
95 | * @abstract |
||
96 | * |
||
97 | * @return void |
||
98 | */ |
||
99 | abstract public function process(); |
||
100 | |||
101 | /** |
||
102 | * Perform the deletion. |
||
103 | * |
||
104 | * @since 5.5 |
||
105 | * @abstract |
||
106 | * |
||
107 | * @param mixed $delete_options |
||
108 | * |
||
109 | * @return int Number of users deleted |
||
110 | */ |
||
111 | abstract public function delete( $delete_options ); |
||
112 | |||
113 | /** |
||
114 | * Use `factory()` method to create instance of this class. |
||
115 | * Don't create instances directly. |
||
116 | * |
||
117 | * @since 5.5 |
||
118 | * @see factory() |
||
119 | */ |
||
120 | public function __construct() { |
||
121 | $this->setup(); |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Setup the module. |
||
126 | * |
||
127 | * @since 5.5 |
||
128 | */ |
||
129 | protected function setup() { |
||
130 | $this->initialize(); |
||
131 | $this->setup_hooks(); |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Setup hooks. |
||
136 | * |
||
137 | * @since 5.5 |
||
138 | */ |
||
139 | protected function setup_hooks() { |
||
140 | add_action( $this->meta_box_hook, array( $this, 'setup_metabox' ), 10, 2 ); |
||
141 | add_action( 'bd_' . $this->delete_action, array( $this, 'process' ) ); |
||
142 | add_filter( 'bd_javascript_array', array( $this, 'filter_js_array' ) ); |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * Setup the meta box. |
||
147 | * |
||
148 | * @since 5.5 |
||
149 | * |
||
150 | * @param mixed $screen |
||
151 | * @param mixed $page_slug |
||
152 | */ |
||
153 | public function setup_metabox( $screen, $page_slug ) { |
||
154 | $this->screen = $screen; |
||
155 | $this->page_slug = $page_slug; |
||
156 | |||
157 | add_meta_box( $this->meta_box_slug, $this->messages['box_label'], array( $this, 'render_box' ), $this->screen, 'advanced' ); |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * Render the meta box. |
||
162 | * |
||
163 | * @since 5.5 |
||
164 | */ |
||
165 | public function render_box() { |
||
166 | if ( $this->is_hidden() ) { |
||
167 | printf( __( 'This section just got enabled. Kindly <a href = "%1$s">refresh</a> the page to fully enable it.', 'bulk-delete' ), 'admin.php?page=' . $this->page_slug ); |
||
168 | |||
169 | return; |
||
170 | } |
||
171 | |||
172 | $this->render(); |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * Is the current meta box hidden by user. |
||
177 | * |
||
178 | * @since 5.5 |
||
179 | * |
||
180 | * @return bool True, if hidden. False, otherwise. |
||
181 | */ |
||
182 | protected function is_hidden() { |
||
183 | $current_user = wp_get_current_user(); |
||
184 | $user_meta_field = $this->get_hidden_box_user_meta_field(); |
||
185 | $hidden_boxes = get_user_meta( $current_user->ID, $user_meta_field, true ); |
||
186 | |||
187 | return is_array( $hidden_boxes ) && in_array( $this->meta_box_slug, $hidden_boxes ); |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * Get the user meta field that stores the status of the hidden meta boxes. |
||
192 | * |
||
193 | * @since 5.5 |
||
194 | * |
||
195 | * @return string Name of the User Meta field. |
||
196 | */ |
||
197 | protected function get_hidden_box_user_meta_field() { |
||
202 | } |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * Filter the js array. |
||
207 | * This function will be overridden by the child classes. |
||
208 | * |
||
209 | * @since 5.5 |
||
210 | * |
||
211 | * @param array $js_array JavaScript Array |
||
212 | * |
||
213 | * @return array Modified JavaScript Array |
||
214 | */ |
||
215 | public function filter_js_array( $js_array) { |
||
216 | return $js_array; |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * Render filtering table header. |
||
221 | * |
||
222 | * @since 5.5 |
||
223 | */ |
||
224 | protected function render_filtering_table_header() { |
||
225 | bd_render_filtering_table_header(); |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * Render restrict settings. |
||
230 | * |
||
231 | * @since 5.5 |
||
232 | */ |
||
233 | protected function render_restrict_settings() { |
||
234 | bd_render_restrict_settings( $this->field_slug, $this->item_type ); |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * Render delete settings. |
||
239 | * |
||
240 | * @since 5.5 |
||
241 | */ |
||
242 | protected function render_delete_settings() { |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * Render limit settings. |
||
248 | * |
||
249 | * @since 5.5 |
||
250 | */ |
||
251 | protected function render_limit_settings() { |
||
252 | bd_render_limit_settings( $this->field_slug, $this->item_type ); |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * Render cron settings. |
||
257 | * |
||
258 | * @since 5.5 |
||
259 | */ |
||
260 | protected function render_cron_settings() { |
||
261 | bd_render_cron_settings( $this->field_slug, $this->scheduler_url ); |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * Render submit button. |
||
266 | * |
||
267 | * @since 5.5 |
||
268 | */ |
||
269 | protected function render_submit_button() { |
||
270 | bd_render_submit_button( $this->delete_action ); |
||
271 | } |
||
272 | |||
273 | /** |
||
274 | * Helper function for processing deletion. |
||
275 | * Setups up cron and invokes the actual delete method. |
||
276 | * |
||
277 | * @since 5.5 |
||
278 | * |
||
279 | * @param mixed $delete_options |
||
280 | */ |
||
281 | protected function process_delete( $delete_options ) { |
||
282 | if ( bd_array_get_bool( $_POST, 'smbd_' . $this->field_slug . '_cron', false ) ) { |
||
283 | $freq = $_POST[ 'smbd_' . $this->field_slug . '_cron_freq' ]; |
||
284 | $time = strtotime( $_POST[ 'smbd_' . $this->field_slug . '_cron_start' ] ) - ( get_option( 'gmt_offset' ) * 60 * 60 ); |
||
285 | |||
286 | if ( -1 == $freq ) { |
||
287 | wp_schedule_single_event( $time, $this->cron_hook, array( $delete_options ) ); |
||
288 | } else { |
||
289 | wp_schedule_event( $time, $freq , $this->cron_hook, array( $delete_options ) ); |
||
290 | } |
||
291 | |||
292 | $msg = $this->messages['scheduled'] . ' ' . |
||
293 | sprintf( __( 'See the full list of <a href = "%s">scheduled tasks</a>' , 'bulk-delete' ), get_bloginfo( 'wpurl' ) . '/wp-admin/admin.php?page=' . Bulk_Delete::CRON_PAGE_SLUG ); |
||
294 | } else { |
||
295 | $deleted_count = $this->delete( $delete_options ); |
||
296 | $msg = sprintf( _n( $this->messages['deleted_single'], $this->messages['deleted_plural'] , $deleted_count, 'bulk-delete' ), $deleted_count ); |
||
297 | } |
||
298 | |||
299 | add_settings_error( |
||
300 | $this->page_slug, |
||
301 | 'deleted-' . $this->item_type, |
||
302 | $msg, |
||
303 | 'updated' |
||
304 | ); |
||
305 | } |
||
306 | |||
307 | /** |
||
308 | * Getter for cron_hook. |
||
309 | * |
||
310 | * @since 5.5 |
||
311 | * |
||
312 | * @return string Cron Hook name. |
||
313 | */ |
||
314 | public function get_cron_hook() { |
||
316 | } |
||
317 | |||
318 | /** |
||
319 | * Getter for field slug. |
||
320 | * |
||
321 | * @since 5.5 |
||
322 | * |
||
323 | * @return string Field Slug. |
||
324 | */ |
||
325 | public function get_field_slug() { |
||
327 | } |
||
328 | } |
||
329 |