PluginProbe
Bulk Delete / trunk
Bulk Delete vtrunk
6.12 trunk 0.2 0.3 0.4 0.6 0.7 0.8 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 2.1 2.2 2.2.1 2.2.2 3.0 3.1 All 64 releases
bulk-delete / include / Core / Base / BasePage.php

BasePage.php in Bulk Delete trunk, at include/Core/Base/BasePage.php

577 lines 15.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace BulkWP\BulkDelete\Core\Base;
4
5 defined('ABSPATH') || exit; // Exit if accessed directly.
6
7 /**
8 * Base class for all Admin Page including Bulk Delete pages and setting pages.
9 *
10 * All concrete implementation of a Bulk Delete Admin page will extend this class.
11 *
12 * @since 6.0.0
13 */
14 abstract class BasePage
15 {
16 /**
17 * Slug of Bulk Delete Menu.
18 */
19 const BULK_WP_MENU_SLUG = 'bulk-delete-posts';
20
21 /**
22 * Path to main plugin file.
23 *
24 * @var string
25 */
26 protected $plugin_file;
27
28 /**
29 * Page Slug.
30 *
31 * @var string
32 */
33 protected $page_slug;
34
35 /**
36 * Hook Suffix of the current page.
37 *
38 * @var string
39 */
40 protected $hook_suffix;
41
42 /**
43 * Current screen.
44 *
45 * @var \WP_Screen
46 */
47 protected $screen;
48
49 /**
50 * Minimum capability needed for viewing this page.
51 *
52 * @var string
53 */
54 protected $capability = 'manage_options';
55
56 /**
57 * Labels used in this page.
58 *
59 * @var array
60 */
61 protected $label = array(
62 'page_title' => '',
63 'menu_title' => '',
64 );
65
66 /**
67 * Messages shown to the user.
68 *
69 * @var array
70 */
71 protected $messages = array(
72 'warning_message' => '',
73 );
74
75 /**
76 * Actions used in this page.
77 *
78 * @var array
79 */
80 protected $actions = array();
81
82 /**
83 * Should the link to this page be displayed in the plugin list. Default false.
84 *
85 * @var bool
86 */
87 protected $show_link_in_plugin_list = 0;
88
89 /**
90 * Initialize and setup variables and attributes of the page.
91 *
92 * @return void
93 */
94 abstract protected function initialize();
95
96 /**
97 * Render body content.
98 *
99 * @return void
100 */
101 abstract protected function render_body();
102
103 /**
104 * BasePage constructor.
105 *
106 * @param string $plugin_file Path to the main plugin file.
107 */
108 public function __construct($plugin_file)
109 {
110 $this->plugin_file = $plugin_file;
111 $this->initialize();
112 }
113
114 /**
115 * Register the page.
116 *
117 * This function will be called in the `admin_menu` hook.
118 */
119 public function register()
120 {
121 $this->register_page();
122 $this->register_hooks();
123 }
124
125 /**
126 * Register page as a submenu to the Bulk Delete Menu.
127 */
128 protected function register_page()
129 {
130 $hook_suffix = add_submenu_page(
131 self::BULK_WP_MENU_SLUG,
132 $this->label['page_title'],
133 $this->label['menu_title'],
134 $this->capability,
135 $this->page_slug,
136 array($this, 'render_page')
137 );
138
139 if (false !== $hook_suffix) {
140 $this->hook_suffix = $hook_suffix;
141 }
142 }
143
144 /**
145 * Register hooks.
146 */
147 protected function register_hooks()
148 {
149 add_filter('bd_action_nonce_check', array($this, 'verify_nonce'), 10, 2);
150
151 add_action("load-{$this->hook_suffix}", array($this, 'setup_contextual_help'));
152 add_filter('bd_admin_help_tabs', array($this, 'render_help_tab'), 10, 2);
153
154 add_action("bd_admin_footer_for_{$this->page_slug}", array($this, 'modify_admin_footer'));
155
156 if ($this->show_link_in_plugin_list) {
157 add_filter('bd_plugin_action_links', array($this, 'append_to_plugin_action_links'));
158 }
159
160 add_action( 'admin_print_scripts-' . $this->hook_suffix, array( $this, 'enqueue_assets' ) );
161
162 add_action('admin_action_bulkwp_install_wp301', array($this, 'install_wp301'));
163 }
164
165 /**
166 * Enqueue Scripts and Styles.
167 */
168 public function enqueue_assets() {
169 /**
170 * Runs just before enqueuing scripts and styles in all Bulk Delete admin pages.
171 *
172 * This action is primarily for registering or deregistering additional scripts or styles.
173 *
174 * @param \BulkWP\BulkDelete\Core\Base\BaseDeletePage The current page.
175 *
176 * @since 5.5.1
177 * @since 6.0.0 Added $page parameter.
178 */
179 do_action( 'bd_before_admin_enqueue_scripts', $this ); //phpcs:ignore
180
181 /**
182 * Runs just before enqueuing scripts and styles in a Bulk Delete admin pages.
183 *
184 * This action is primarily for registering or deregistering additional scripts or styles.
185 *
186 * @param \BulkWP\BulkDelete\Core\Base\BaseDeletePage The current page.
187 *
188 * @since 6.0.1
189 */
190 do_action( "bd_before_enqueue_page_assets_for_{$this->get_page_slug()}", $this ); //phpcs:ignore
191 wp_enqueue_style('wp-jquery-ui-dialog');
192 wp_enqueue_script('jquery-ui-dialog');
193
194 /*
195 wp_enqueue_style( 'jquery-ui-smoothness', $this->get_plugin_dir_url() . 'assets/css/jquery-ui-smoothness.min.css', array(), '1.12.1' );
196 wp_enqueue_script(
197 'jquery-ui-timepicker-addon',
198 $this->get_plugin_dir_url() . 'assets/js/jquery-ui-timepicker-addon.min.js',
199 array( 'jquery-ui-slider', 'jquery-ui-datepicker' ),
200 '1.6.3',
201 true
202 );
203 */
204
205 //wp_enqueue_style( 'jquery-ui-timepicker', $this->get_plugin_dir_url() . 'assets/css/jquery-ui-timepicker-addon.min.css', array(), '1.6.3' );
206
207 wp_enqueue_script( 'postbox');
208 wp_enqueue_script( 'select2', $this->get_plugin_dir_url() . 'assets/js/select2.min.js', array( 'jquery' ), '4.0.5', true );
209 wp_enqueue_style( 'select2', $this->get_plugin_dir_url() . 'assets/css/select2.min.css', array(), '4.0.5' );
210
211 $js_localize = array(
212 'bl_nonce' => wp_create_nonce('bl-nonce'),
213 'wp301_install_url' => add_query_arg(array('action' => 'bulkwp_install_wp301', '_wpnonce' => wp_create_nonce('install_wp301'), 'rnd' => wp_rand()), admin_url('admin.php')),
214 );
215
216 wp_enqueue_script(
217 'bulk-delete',
218 $this->get_plugin_dir_url() . 'assets/js/bulk-delete.js',
219 array(),
220 self::plugin_version(),
221 true
222 );
223
224 wp_localize_script('bulk-delete', 'bulk_delete', $js_localize);
225
226 wp_enqueue_style(
227 'bulk-delete',
228 $this->get_plugin_dir_url() . 'assets/css/bulk-delete.css',
229 array(),
230 self::plugin_version()
231 );
232
233 /**
234 * Filter JavaScript array.
235 *
236 * This filter can be used to extend the array that is passed to JavaScript
237 *
238 * @since 5.4
239 */
240 $translation_array = apply_filters( 'bd_javascript_array', //phpcs:ignore
241 array(
242 'msg' => array(),
243 'validators' => array(),
244 'dt_iterators' => array(),
245 'pre_action_msg' => array(), // deprecated since 6.0.1.
246 'pre_delete_msg' => array(),
247 'pre_schedule_msg' => array(),
248 'error_msg' => array(),
249 'pro_iterators' => array(),
250 )
251 );
252 wp_localize_script( 'bulk-delete', 'BulkWP', $translation_array ); // TODO: Change JavaScript variable to BulkWP.BulkDelete.
253
254 /**
255 * Runs just after enqueuing scripts and styles in all Bulk Delete admin pages.
256 *
257 * This action is primarily for registering additional scripts or styles.
258 *
259 * @param \BulkWP\BulkDelete\Core\Base\BaseDeletePage The current page.
260 *
261 * @since 5.5.1
262 * @since 6.0.0 Added $page parameter.
263 */
264 do_action( 'bd_after_admin_enqueue_scripts', $this ); //phpcs:ignore
265
266 /**
267 * Runs just after enqueuing scripts and styles in a Bulk Delete admin pages.
268 *
269 * This action is primarily for registering or deregistering additional scripts or styles.
270 *
271 * @param \BulkWP\BulkDelete\Core\Base\BaseDeletePage The current page.
272 *
273 * @since 6.0.1
274 */
275 do_action( "bd_after_enqueue_page_assets_for_{$this->get_page_slug()}", $this ); //phpcs:ignore
276 }
277
278 // auto download / install / activate WP 301 Redirects plugin
279 public function install_wp301()
280 {
281 check_ajax_referer('install_wp301');
282
283 if (false === current_user_can('manage_options')) {
284 wp_die('Sorry, you have to be an admin to run this action.');
285 }
286
287 $plugin_slug = 'eps-301-redirects/eps-301-redirects.php';
288 $plugin_zip = 'https://downloads.wordpress.org/plugin/eps-301-redirects.latest-stable.zip';
289
290 @include_once ABSPATH . 'wp-admin/includes/plugin.php';
291 @include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
292 @include_once ABSPATH . 'wp-admin/includes/plugin-install.php';
293 @include_once ABSPATH . 'wp-admin/includes/file.php';
294 @include_once ABSPATH . 'wp-admin/includes/misc.php';
295 echo '<style>
296 body{
297 font-family: sans-serif;
298 font-size: 14px;
299 line-height: 1.5;
300 color: #444;
301 }
302 </style>';
303
304 echo '<div style="margin: 20px; color:#444;">';
305 echo 'If things are not done in a minute <a target="_parent" href="' . esc_url(admin_url('plugin-install.php?s=301%20redirects%20webfactory&tab=search&type=term')) . '">install the plugin manually via Plugins page</a><br><br>';
306 echo 'Starting ...<br><br>';
307
308 wp_cache_flush();
309 $upgrader = new \Plugin_Upgrader();
310 echo 'Check if WP 301 Redirects is already installed ... <br />';
311 if ($this->is_plugin_installed($plugin_slug)) {
312 echo 'WP 301 Redirects is already installed! <br /><br />Making sure it\'s the latest version.<br />';
313 $upgrader->upgrade($plugin_slug);
314 $installed = true;
315 } else {
316 echo 'Installing WP 301 Redirects.<br />';
317 $installed = $upgrader->install($plugin_zip);
318 }
319 wp_cache_flush();
320
321 if (!is_wp_error($installed) && $installed) {
322 echo 'Activating WP 301 Redirects.<br />';
323 $activate = activate_plugin($plugin_slug);
324
325 if (is_null($activate)) {
326 echo 'WP 301 Redirects Activated.<br />';
327
328 echo '<script>setTimeout(function() { top.location = "' . esc_url(admin_url('options-general.php?page=eps_redirects')) . '"; }, 1000);</script>';
329 echo '<br>If you are not redirected in a few seconds - <a href="' . esc_url(admin_url('options-general.php?page=eps_redirects')) . '" target="_parent">click here</a>.';
330 }
331 } else {
332 echo 'Could not install WP 301 Redirects. You\'ll have to <a target="_parent" href="' . esc_url(admin_url('plugin-install.php?s=301%20redirects%20webfactory&tab=search&type=term')) . '">download and install manually</a>.';
333 }
334
335 echo '</div>';
336 } // install_wp301
337
338 /**
339 * Check if given plugin is installed
340 *
341 * @param [string] $slug Plugin slug
342 * @return boolean
343 */
344 static function is_plugin_installed($slug)
345 {
346 if (!function_exists('get_plugins')) {
347 require_once ABSPATH . 'wp-admin/includes/plugin.php';
348 }
349 $all_plugins = get_plugins();
350
351 if (!empty($all_plugins[$slug])) {
352 return true;
353 } else {
354 return false;
355 }
356 } // is_plugin_installed
357
358 static function plugin_version()
359 {
360 $plugin_data = get_file_data(BULK_DELETE_FILE, array('version' => 'Version'), 'plugin');
361
362 return $plugin_data['version'];
363 } // get_plugin_version
364
365 /**
366 * Check for nonce before executing the action.
367 *
368 * @param bool $result The current result.
369 * @param string $action Action name.
370 *
371 * @return bool True if nonce is verified, False otherwise.
372 */
373 public function verify_nonce($result, $action)
374 {
375 /**
376 * List of actions for page.
377 *
378 * @param array $actions Actions.
379 * @param BasePage $page Page objects.
380 *
381 * @since 6.0.0
382 */
383 $page_actions = apply_filters('bd_page_actions', $this->actions, $this); //phpcs:ignore
384
385 if (in_array($action, $page_actions, true)) {
386 if (check_admin_referer("bd-{$this->page_slug}", "bd-{$this->page_slug}-nonce")) {
387 return true;
388 }
389 }
390
391 return $result;
392 }
393
394 /**
395 * Setup hooks for rendering contextual help.
396 */
397 public function setup_contextual_help()
398 {
399 /**
400 * Add contextual help for admin screens.
401 *
402 * @since 5.1
403 *
404 * @param string Hook suffix of the current page.
405 */
406 }
407
408 /**
409 * Modify help tabs for the current page.
410 *
411 * @param array $help_tabs Current list of help tabs.
412 * @param string $hook_suffix Hook Suffix of the page.
413 *
414 * @return array Modified list of help tabs.
415 */
416 public function render_help_tab($help_tabs, $hook_suffix)
417 {
418 if ($this->hook_suffix === $hook_suffix) {
419 $help_tabs = $this->add_help_tab($help_tabs);
420 }
421
422 return $help_tabs;
423 }
424
425 /**
426 * Add help tabs.
427 *
428 * Help tabs can be added by overriding this function in the child class.
429 *
430 * @param array $help_tabs Current list of help tabs.
431 *
432 * @return array List of help tabs.
433 */
434 protected function add_help_tab($help_tabs)
435 {
436 return $help_tabs;
437 }
438
439 /**
440 * Render the page.
441 */
442 public function render_page()
443 {
444 ?>
445 <div class="wrap">
446 <h2><?php echo esc_html($this->label['page_title']); ?></h2>
447 <?php settings_errors(); ?>
448
449 <form method="post">
450 <?php $this->render_nonce_fields(); ?>
451 <div class="bulkwp-body-wrapper">
452 <div id="poststuff">
453 <div id="post-body" class="metabox-holder columns-1">
454
455 <?php $this->render_header(); ?>
456
457 <div id="postbox-container-2" class="postbox-container">
458 <?php $this->render_body(); ?>
459 </div> <!-- #postbox-container-2 -->
460
461 </div> <!-- #post-body -->
462 </div><!-- #poststuff -->
463 </div>
464 <div class="bulkwp-sidebar-wrapper">
465 <?php bd_wp_kses_wf(\Bulk_Delete::sidebar()); ?>
466 </div>
467 </form>
468 </div><!-- .wrap -->
469 <?php
470 $this->render_footer();
471 }
472
473 /**
474 * Print nonce fields.
475 */
476 protected function render_nonce_fields()
477 {
478 wp_nonce_field("bd-{$this->page_slug}", "bd-{$this->page_slug}-nonce");
479 }
480
481 /**
482 * Render page header.
483 */
484 protected function render_header()
485 {
486 if (empty($this->messages['warning_message'])) {
487 return;
488 }
489 ?>
490 <div class="notice notice-warning">
491 <p>
492 <strong>
493 <?php echo esc_html($this->messages['warning_message']); ?>
494 </strong>
495 </p>
496 </div>
497 <?php
498 }
499
500 /**
501 * Render page footer.
502 */
503 protected function render_footer()
504 {
505 /**
506 * Runs just before displaying the footer text in the admin page.
507 *
508 * This action is primarily for adding extra content in the footer of admin page.
509 *
510 * @since 5.5.4
511 */
512 do_action("bd_admin_footer_for_{$this->page_slug}"); //phpcs:ignore
513 }
514
515 /**
516 * Modify admin footer in Bulk Delete plugin pages.
517 */
518 public function modify_admin_footer()
519 {
520 add_filter('admin_footer_text', 'bd_add_rating_link');
521 }
522
523 /**
524 * Append link to the current page in plugin list.
525 *
526 * @param array $links Array of links.
527 *
528 * @return array Modified list of links.
529 */
530 public function append_to_plugin_action_links($links)
531 {
532 $links[$this->get_page_slug()] = '<a href="admin.php?page=' . $this->get_page_slug() . '">' . $this->label['page_title'] . '</a>';
533
534 return $links;
535 }
536
537 /**
538 * Getter for screen.
539 *
540 * @return \WP_Screen Current screen.
541 */
542 public function get_screen()
543 {
544 return $this->screen;
545 }
546
547 /**
548 * Getter for page_slug.
549 *
550 * @return string Slug of the page.
551 */
552 public function get_page_slug()
553 {
554 return $this->page_slug;
555 }
556
557 /**
558 * Getter for Hook Suffix.
559 *
560 * @return string Hook Suffix of the page.
561 */
562 public function get_hook_suffix()
563 {
564 return $this->hook_suffix;
565 }
566
567 /**
568 * Get the url to the plugin directory.
569 *
570 * @return string Url to plugin directory.
571 */
572 protected function get_plugin_dir_url()
573 {
574 return plugin_dir_url($this->plugin_file);
575 }
576 }
577