PluginProbe
Boxzilla – WordPress Popup Builder / 3.2.21
Boxzilla – WordPress Popup Builder v3.2.21
3.4.11 3.4.10 3.4.9 3.4.3 3.4.4 3.4.5 3.4.6 3.4.7 3.4.8 trunk 3.0 3.0.1 3.0.2 3.0.3 3.1 3.1.1 3.1.10 3.1.11 3.1.12 3.1.13 3.1.14 3.1.15 3.1.16 3.1.17 3.1.18 All 72 releases
← All changes | src/admin/class-admin.php +815 -736 3.13.2.21 View file →
@@ -1,736 +1,815 @@
1 -<?php
2 -
3 -namespace Boxzilla\Admin;
4 -
5 -use Boxzilla\Plugin,
6 - Boxzilla\Box,
7 - Boxzilla\Boxzilla;
8 -use WP_Post;
9 -use WP_Screen;
10 -
11 -class Admin {
12 -
13 - /**
14 - * @var Plugin $plugin
15 - */
16 - private $plugin;
17 -
18 - /**
19 - * @var Boxzilla
20 - */
21 - protected $boxzilla;
22 -
23 - /**
24 - * @param Plugin $plugin
25 - * @param Boxzilla $boxzilla
26 - */
27 - public function __construct( Plugin $plugin, Boxzilla $boxzilla ) {
28 - $this->plugin = $plugin;
29 - $this->boxzilla = $boxzilla;
30 - }
31 -
32 - /**
33 - * Initialise the all admin related stuff
34 - */
35 - public function init() {
36 -
37 - // Load the plugin textdomain
38 - load_plugin_textdomain( 'boxzilla', null, basename( $this->plugin->dir() ) . '/languages' );
39 -
40 - // action hooks
41 - $this->add_hooks();
42 - $this->run_migrations();
43 - }
44 -
45 - /**
46 - * Add necessary hooks
47 - */
48 - protected function add_hooks() {
49 - add_action( 'admin_init', array( $this, 'lazy_add_hooks' ) );
50 - add_action( 'admin_init', array( $this, 'register' ) );
51 - add_action( 'admin_menu', array( $this, 'menu' ) );
52 - add_action( 'admin_notices', array( $this, 'notices' ) );
53 - add_action( 'save_post_boxzilla-box', array( $this, 'save_box_options' ), 20, 2 );
54 - add_action( 'trashed_post', array( $this, 'flush_rules' ) );
55 - add_action( 'untrashed_post', array( $this, 'flush_rules' ) );
56 - }
57 -
58 - /**
59 - * All logic for admin notices lives here.
60 - */
61 - public function notices() {
62 - global $pagenow,
63 - $current_screen;
64 -
65 - if( ( $pagenow === 'plugins.php' || ( $current_screen && $current_screen->post_type === 'boxzilla-box' ) )
66 - && current_user_can( 'install_plugins' )
67 - && is_plugin_active( 'scroll-triggered-boxes/index.php' ) ) {
68 -
69 - $url = wp_nonce_url( 'plugins.php?action=deactivate&plugin=scroll-triggered-boxes/index.php', 'deactivate-plugin_' . 'scroll-triggered-boxes/index.php' );
70 - ?>
71 - <div class="notice notice-info">
72 - <p><?php printf( __( 'Awesome, you are using Boxzilla! You can now safely <a href="%s">deactivate the Scroll Triggered Boxes plugin</a>.', 'boxzilla' ), $url ); ?></p>
73 - </div>
74 - <?php
75 - }
76 -
77 - }
78 -
79 - /**
80 - * Checks current version against stored version & runs necessary update routines.
81 - *
82 - * @return bool
83 - */
84 - protected function run_migrations() {
85 -
86 - // Only run if db option is at older version than code constant
87 - $previous_version = get_option( 'boxzilla_version', '0' );
88 - $current_version = $this->plugin->version();
89 -
90 - if( version_compare( $current_version, $previous_version, '<=' ) ) {
91 - return false;
92 - }
93 -
94 - $upgrade_routines = new Migrations( $previous_version, $current_version, __DIR__ . '/migrations' );
95 - $upgrade_routines->run();
96 - update_option( 'boxzilla_version', $current_version );
97 - }
98 -
99 - public function lazy_add_hooks() {
100 - global $pagenow;
101 -
102 - add_action( 'admin_enqueue_scripts', array( $this, 'load_assets' ) );
103 - add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
104 - add_filter( 'tiny_mce_before_init', array( $this, 'tinymce_init' ) );
105 - add_filter( 'manage_edit-boxzilla-box_columns', array( $this, 'post_type_column_titles' ) );
106 - add_action( 'manage_boxzilla-box_posts_custom_column', array(
107 - $this,
108 - 'post_type_column_content'
109 - ), 10, 2 );
110 - add_filter( 'admin_footer_text', array( $this, 'admin_footer_text' ) );
111 -
112 -
113 - if ( $pagenow === 'plugins.php' ) {
114 - add_filter( 'plugin_action_links', array( $this, 'add_plugin_settings_link' ), 10, 2 );
115 - add_filter( 'plugin_row_meta', array( $this, 'add_plugin_meta_links' ), 10, 2 );
116 - }
117 - }
118 -
119 - /**
120 - * @param $post_id
121 - */
122 - public function post_type_column_box_id_content( $post_id ) {
123 - echo $post_id;
124 - }
125 -
126 - /**
127 - * @param $column
128 - * @param $post_id
129 - */
130 - public function post_type_column_content( $column, $post_id ) {
131 - if ( method_exists( $this, 'post_type_column_' . $column . '_content' ) ) {
132 - call_user_func( array( $this, 'post_type_column_' . $column . '_content' ), $post_id );
133 - }
134 - }
135 -
136 - /**
137 - * @param $columns
138 - *
139 - * @return mixed
140 - */
141 - public function post_type_column_titles( $columns ) {
142 - $columns = self::array_insert( $columns, array(
143 - 'box_id' => __( 'Box ID', 'boxzilla' )
144 - ), 1 );
145 -
146 - $columns['title'] = __( 'Box Title', 'boxzilla' );
147 -
148 - return $columns;
149 - }
150 -
151 - /**
152 - * Register stuffs
153 - */
154 - public function register() {
155 -
156 - // register settings
157 - register_setting( 'boxzilla_settings', 'boxzilla_settings', array( $this, 'sanitize_settings' ) );
158 -
159 - // register scripts
160 - $pre_suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
161 -
162 - wp_register_script( 'boxzilla-admin', $this->plugin->url( '/assets/js/admin-script' . $pre_suffix . '.js' ), array(
163 - 'jquery',
164 - 'wp-util',
165 - 'wp-color-picker',
166 - 'suggest'
167 - ), $this->plugin->version(), true );
168 -
169 - // load stylesheets
170 - wp_register_style( 'boxzilla-admin', $this->plugin->url( '/assets/css/admin-styles' . $pre_suffix . '.css' ), array(), $this->plugin->version() );
171 - }
172 -
173 - /**
174 - * Renders the STB Menu items
175 - */
176 - public function menu() {
177 -
178 - $menu_items = array(
179 - array(
180 - __( 'Settings', 'boxzilla' ),
181 - __( 'Settings', 'boxzilla' ),
182 - 'boxzilla-settings',
183 - array( $this, 'show_settings_page' )
184 - ),
185 - array(
186 - __( 'Extensions', 'boxzilla' ),
187 - '<span style="color: orange">' . __( 'Extensions', 'boxzilla' ) . '</span>',
188 - 'boxzilla-extensions',
189 - array( $this, 'show_extensions_page' )
190 - )
191 - );
192 -
193 - $menu_items = apply_filters( 'boxzilla_admin_menu_items', $menu_items );
194 -
195 - foreach ( $menu_items as $item ) {
196 - add_submenu_page( 'edit.php?post_type=boxzilla-box', $item[0] . '- Boxzilla', $item[1], 'manage_options', $item[2], $item[3] );
197 - }
198 - }
199 -
200 - /**
201 - * Shows the settings page
202 - */
203 - public function show_settings_page() {
204 - $opts = $this->boxzilla->options;
205 - require __DIR__ . '/views/settings.php';
206 - }
207 -
208 - /**
209 - * Shows the extensions page
210 - */
211 - public function show_extensions_page() {
212 - $extensions = $this->fetch_extensions();
213 - require __DIR__ . '/views/extensions.php';
214 - }
215 -
216 - /**
217 - * Are we currently editing a box?
218 - *
219 - * @return bool
220 - */
221 - protected function on_edit_box_page() {
222 - global $pagenow;
223 -
224 - if ( ! in_array( $pagenow, array( 'post-new.php', 'post.php' ) ) ) {
225 - return false;
226 - }
227 -
228 - if ( isset( $_GET['post_type'] ) && $_GET['post_type'] === 'boxzilla-box' ) {
229 - return true;
230 - }
231 -
232 - if ( get_post_type() === 'boxzilla-box' ) {
233 - return true;
234 - }
235 -
236 - return false;
237 - }
238 -
239 - /**
240 - * @param $args
241 - *
242 - * @return mixed
243 - */
244 - public function tinymce_init( $args ) {
245 -
246 - // only act on our post type
247 - if ( get_post_type() !== 'boxzilla-box' ) {
248 - return $args;
249 - }
250 -
251 - $args['setup'] = 'function( editor ) { if(typeof(window.Boxzilla_Admin) === \'undefined\') { return; } editor.on("PreInit", window.Boxzilla_Admin.Designer.init ); }';
252 -
253 - return $args;
254 - }
255 -
256 - /**
257 - * Load plugin assets
258 - */
259 - public function load_assets() {
260 -
261 - $screen = get_current_screen();
262 -
263 - if ( ! $screen instanceof WP_Screen ) {
264 - return false;
265 - }
266 -
267 - if ( $screen->base === 'edit' && $screen->post_type === 'boxzilla-box' ) {
268 - // load stylesheets
269 - wp_enqueue_style( 'boxzilla-admin' );
270 - }
271 -
272 - if ( $screen->base === 'post' && $screen->post_type === 'boxzilla-box' ) {
273 - // color picker
274 - wp_enqueue_style( 'wp-color-picker' );
275 -
276 - // load scripts
277 - wp_enqueue_script( 'boxzilla-admin' );
278 -
279 - wp_localize_script( 'boxzilla-admin' ,'boxzilla_i18n', array(
280 - 'enterCommaSeparatedValues' => __( 'Enter a comma-separated list of values.', 'boxzilla' ),
281 - 'enterCommaSeparatedPosts' => __( "Enter a comma-separated list of post slugs or post ID's..", 'boxzilla' ),
282 - 'enterCommaSeparatedPages' => __( "Enter a comma-separated list of page slugs or page ID's..", 'boxzilla' ),
283 - 'enterCommaSeparatedPostTypes' => __( "Enter a comma-separated list of post types..", 'boxzilla' ),
284 - 'enterCommaSeparatedRelativeUrls' => __( "Enter a comma-separated list of relative URL's, eg /contact/", 'boxzilla' ),
285 - )
286 - );
287 -
288 - // load stylesheets
289 - wp_enqueue_style( 'boxzilla-admin' );
290 -
291 - // allow add-ons to easily load their own scripts or stylesheets
292 - do_action( 'boxzilla_load_admin_assets' );
293 - }
294 -
295 - if ( isset( $_GET['page'] ) && $_GET['page'] === 'boxzilla-settings' ) {
296 - // load stylesheets
297 - wp_enqueue_style( 'boxzilla-admin' );
298 - }
299 -
300 - }
301 -
302 - /**
303 - * Register meta boxes
304 - *
305 - * @param string $post_type
306 - *
307 - * @return bool
308 - */
309 - public function add_meta_boxes( $post_type ) {
310 -
311 - if ( $post_type !== 'boxzilla-box' ) {
312 - return false;
313 - }
314 -
315 - add_meta_box(
316 - 'boxzilla-box-appearance-controls',
317 - __( 'Box Appearance', 'boxzilla' ),
318 - array( $this, 'metabox_box_appearance_controls' ),
319 - 'boxzilla-box',
320 - 'normal',
321 - 'core'
322 - );
323 -
324 - add_meta_box(
325 - 'boxzilla-box-options-controls',
326 - __( 'Box Options', 'boxzilla' ),
327 - array( $this, 'metabox_box_option_controls' ),
328 - 'boxzilla-box',
329 - 'normal',
330 - 'core'
331 - );
332 -
333 - add_meta_box(
334 - 'boxzilla-support',
335 - __( 'Looking for help?', 'boxzilla' ),
336 - array( $this, 'metabox_support' ),
337 - 'boxzilla-box',
338 - 'side'
339 - );
340 -
341 - add_meta_box(
342 - 'boxzilla-email-optin',
343 - __( 'Subscribe to our newsletter', 'boxzilla' ),
344 - array( $this, 'metabox_email_optin' ),
345 - 'boxzilla-box',
346 - 'side'
347 - );
348 -
349 - return true;
350 - }
351 -
352 - /**
353 - * @param \WP_Post $post
354 - * @param $metabox
355 - */
356 - public function metabox_box_appearance_controls( \WP_Post $post, $metabox ) {
357 -
358 - // get box options
359 - $box = new Box( $post );
360 - $opts = $box->get_options();
361 -
362 - // include view
363 - include __DIR__ . '/views/metaboxes/box-appearance-controls.php';
364 - }
365 -
366 - /**
367 - * @param \WP_Post $post
368 - * @param $metabox
369 - */
370 - public function metabox_box_option_controls( \WP_Post $post, $metabox ) {
371 -
372 - // get box options
373 - $box = new Box( $post );
374 - $opts = $box->get_options();
375 - $global_opts = $this->boxzilla->options;
376 -
377 - if ( empty( $opts['rules'] ) ) {
378 - $opts['rules'][] = array( 'condition' => '', 'qualifier' => 1, 'value' => '' );
379 - }
380 -
381 - // include view
382 - include __DIR__ . '/views/metaboxes/box-option-controls.php';
383 - }
384 -
385 - /**
386 - * @param \WP_Post $post
387 - * @param $metabox
388 - */
389 - public function metabox_email_optin( \WP_Post $post, $metabox ) {
390 - include __DIR__ . '/views/metaboxes/email-optin.php';
391 - }
392 -
393 - /**
394 - * @param \WP_Post $post
395 - * @param $metabox
396 - */
397 - public function metabox_support( \WP_Post $post, $metabox ) {
398 - include __DIR__ . '/views/metaboxes/need-help.php';
399 - }
400 -
401 -
402 - /**
403 - * Saves box options and rules
404 - *
405 - * @param int $box_id
406 - *
407 - * @return bool
408 - */
409 - public function save_box_options( $box_id, $post ) {
410 -
411 - // Only act on our own post type
412 - if ( $post->post_type !== 'boxzilla-box' ) {
413 - return false;
414 - }
415 -
416 - // is this a revision save?
417 - if ( wp_is_post_revision( $box_id ) || ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) ) {
418 - return false;
419 - }
420 -
421 - // can user edit this post?
422 - if ( ! current_user_can( 'edit_post', $box_id ) ) {
423 - return false;
424 - }
425 -
426 - // make sure options array is set
427 - if ( ! isset( $_POST['boxzilla_box'] ) || ! is_array( $_POST['boxzilla_box'] ) ) {
428 - return false;
429 - }
430 -
431 - // get new options from $_POST
432 - $opts = $this->sanitize_box_options( $_POST['boxzilla_box'] );
433 -
434 - // allow extensions to filter the saved options
435 - $opts = apply_filters( 'boxzilla_saved_options', $opts, $box_id );
436 -
437 - // save individual box settings
438 - update_post_meta( $box_id, 'boxzilla_options', $opts );
439 -
440 - // update global settings if given
441 - if( ! empty( $_POST['boxzilla_global_settings'] ) ) {
442 - $global_settings = get_option( 'boxzilla_settings', array() );
443 - if( ! is_array( $global_settings ) ) { $global_settings = array(); }
444 - $global_settings = array_merge( $global_settings, $_POST['boxzilla_global_settings'] );
445 - update_option( 'boxzilla_settings', $global_settings );
446 - }
447 -
448 - $this->flush_rules( $box_id );
449 -
450 - return true;
451 - }
452 -
453 - /**
454 - * @param array $opts
455 - *
456 - * @return array
457 - */
458 - public function sanitize_settings( $opts ) {
459 - return $opts;
460 - }
461 -
462 - /**
463 - * @param string $url_string
464 - *
465 - * @return string
466 - */
467 - public function sanitize_url( $url_string ) {
468 -
469 - // if empty, just return a slash
470 - if( empty( $url_string ) ) {
471 - return '/';
472 - }
473 -
474 - // if string looks like an absolute URL, extract just the path
475 - if( preg_match( '/^((https|http)?\:\/\/)?(\w+\.)?\w+\.\w+\.*/i', $url_string ) ) {
476 -
477 - // make sure URL has scheme prepended, to make parse_url() understand..
478 - $url_string = 'https://' . str_replace( array( 'http://', 'https://' ), '', $url_string );
479 -
480 - // get just the path
481 - $url_string = parse_url( $url_string, PHP_URL_PATH );
482 - }
483 -
484 - // leading slash it
485 - return '/' . ltrim( $url_string, '/' );
486 - }
487 -
488 - /**
489 - * @param array $rule
490 - * @return array The sanitized rule array
491 - */
492 - public function sanitize_box_rule( $rule) {
493 -
494 - $rule['value'] = trim( $rule['value'] );
495 -
496 - // convert to array
497 - $rule['value'] = explode( ',', trim( $rule['value'], ',' ) );
498 -
499 - // trim all whitespace in value field
500 - $rule['value'] = array_map( 'trim', $rule['value'] );
501 -
502 - // Make sure "is_url" values have a leading slash
503 - if ( $rule['condition'] === 'is_url' ) {
504 - $rule['value'] = array_map( array( $this, 'sanitize_url' ), $rule['value'] );
505 - }
506 -
507 - // (re)set value to 0 when condition is everywhere
508 - if ( $rule['condition'] === 'everywhere' ) {
509 - $rule['value'] = '';
510 - }
511 -
512 - // convert back to string before saving
513 - if ( is_array( $rule['value'] ) ) {
514 - $rule['value'] = join( ',', $rule['value'] );
515 - }
516 -
517 - $rule['qualifier'] = isset( $rule['qualifier'] ) && ! $rule['qualifier'] ? 0 : 1;
518 -
519 - return $rule;
520 - }
521 -
522 - /**
523 - * @param array $css
524 - * @return array
525 - */
526 - public function sanitize_box_css( $css ) {
527 -
528 - // sanitize settings
529 - if ( '' !== $css['width'] ) {
530 - $css['width'] = absint( $css['width'] );
531 - }
532 -
533 - if ( '' !== $css['border_width'] ) {
534 - $css['border_width'] = absint( $css['border_width'] );
535 - }
536 -
537 - // make sure colors start with `#`
538 - $color_keys = array( 'color', 'background_color', 'border_color' );
539 - foreach ( $color_keys as $key ) {
540 - $value = $css[ $key ];
541 - $color = sanitize_text_field( $value );
542 -
543 - // make sure color starts with `#`
544 - if ( '' !== $color && $color[0] !== '#' ) {
545 - $color = '#' . $color;
546 - }
547 - $css[ $key ] = $color;
548 - }
549 -
550 - return $css;
551 - }
552 -
553 - /**
554 - * Sanitize the options for this box.
555 - *
556 - * @param array $opts
557 - *
558 - * @return array
559 - */
560 - protected function sanitize_box_options( $opts ) {
561 -
562 - static $defaults = array(
563 - 'rules' => array(),
564 - 'css' => array()
565 - );
566 -
567 - $opts = array_replace_recursive( $defaults, $opts );
568 -
569 - $opts['rules'] = array_map( array( $this, 'sanitize_box_rule' ), $opts['rules'] );
570 - $opts['css'] = $this->sanitize_box_css( $opts['css'] );
571 - $opts['cookie']['triggered'] = absint( $opts['cookie']['triggered'] );
572 - $opts['cookie']['dismissed'] = absint( $opts['cookie']['dismissed'] );
573 - $opts['trigger'] = sanitize_text_field( $opts['trigger'] );
574 - $opts['trigger_percentage'] = absint( $opts['trigger_percentage'] );
575 - $opts['trigger_element'] = sanitize_text_field( $opts['trigger_element'] );
576 -
577 - return $opts;
578 - }
579 -
580 - /**
581 - * Add the settings link to the Plugins overview
582 - *
583 - * @param array $links
584 - * @param string $slug
585 - *
586 - * @return array
587 - */
588 - public function add_plugin_settings_link( $links, $slug ) {
589 - if ( $slug !== $this->plugin->slug() ) {
590 - return $links;
591 - }
592 -
593 - $settings_link = '<a href="' . admin_url( 'edit.php?post_type=boxzilla-box' ) . '">' . __( 'Boxes' ) . '</a>';
594 - array_unshift( $links, $settings_link );
595 -
596 - return $links;
597 - }
598 -
599 - /**
600 - * Adds meta links to the plugin in the WP Admin > Plugins screen
601 - *
602 - * @param array $links
603 - * @param string $slug
604 - *
605 - * @return array
606 - */
607 - public function add_plugin_meta_links( $links, $slug ) {
608 - if ( $slug !== $this->plugin->slug() ) {
609 - return $links;
610 - }
611 -
612 - $links[] = '<a href="https://kb.boxzillaplugin.com/#utm_source=wp-plugin&utm_medium=boxzilla&utm_campaign=plugins-page">Documentation</a>';
613 - $links[] = '<a href="https://boxzillaplugin.com/add-ons/#utm_source=wp-plugin&utm_medium=boxzilla&utm_campaign=plugins-page">Add-ons</a>';
614 -
615 - return $links;
616 - }
617 -
618 - /**
619 - * Flush all box rules
620 - *
621 - * Loops through all published boxes and fills the rules option
622 - *
623 - * @param int $post_id
624 - */
625 - public function flush_rules( $post_id ) {
626 -
627 - // only act on our own post type
628 - $post = get_post( $post_id );
629 - if ( $post instanceof WP_Post && $post->post_type !== 'boxzilla-box' ) {
630 - return;
631 - }
632 -
633 - // get all published boxes
634 - $boxes = get_posts(
635 - array(
636 - 'post_type' => 'boxzilla-box',
637 - 'post_status' => 'publish',
638 - 'numberposts' => - 1
639 - )
640 - );
641 -
642 - // setup empty array of rules
643 - $rules = array();
644 -
645 - // fill rules array
646 - if ( is_array( $boxes ) ) {
647 -
648 - foreach ( $boxes as $box ) {
649 - // get box meta data
650 - $box_meta = get_post_meta( $box->ID, 'boxzilla_options', true );
651 -
652 - // add box rules to all rules
653 - $rules[ $box->ID ] = $box_meta['rules'];
654 - $rules[ $box->ID ]['comparision'] = isset( $box_meta['rules_comparision'] ) ? $box_meta['rules_comparision'] : 'any';
655 -
656 - }
657 -
658 - }
659 -
660 - update_option( 'boxzilla_rules', $rules );
661 - }
662 -
663 - /**
664 - * Fetches a list of available add-on plugins
665 - *
666 - * @return array
667 - */
668 - protected function fetch_extensions() {
669 -
670 - $extensions = get_transient( 'boxzilla_remote_extensions' );
671 - if ( $extensions ) {
672 - return $extensions;
673 - }
674 -
675 - $request = wp_remote_get( 'https://api.boxzillaplugin.com/v1/plugins' );
676 -
677 - if ( is_wp_error( $request ) ) {
678 - return array();
679 - }
680 -
681 - $response = wp_remote_retrieve_body( $request );
682 - $response = json_decode( $response );
683 -
684 - if ( is_array( $response->data ) ) {
685 - set_transient( 'boxzilla_remote_extensions', $response->data, HOUR_IN_SECONDS );
686 -
687 - return $response->data;
688 - }
689 -
690 - return array();
691 - }
692 -
693 - /**
694 - * @param $arr
695 - * @param $insert
696 - * @param $position
697 - *
698 - * @return array
699 - */
700 - public static function array_insert( $arr, $insert, $position ) {
701 - $i = 0;
702 - $ret = array();
703 - foreach ( $arr as $key => $value ) {
704 - if ( $i == $position ) {
705 - foreach ( $insert as $ikey => $ivalue ) {
706 - $ret[ $ikey ] = $ivalue;
707 - }
708 - }
709 - $ret[ $key ] = $value;
710 - $i ++;
711 - }
712 -
713 - return $ret;
714 - }
715 -
716 - /**
717 - * @param string $text
718 - *
719 - * @return string
720 - */
721 - public function admin_footer_text( $text ) {
722 - $screen = get_current_screen();
723 -
724 - if ( ! $screen instanceof WP_Screen ) {
725 - return $text;
726 - }
727 -
728 - $on_edit_page = $screen->parent_base === 'edit' && $screen->post_type === 'boxzilla-box';
729 - if ( $on_edit_page ) {
730 - return sprintf( 'If you enjoy using <strong>Boxzilla</strong>, please <a href="%s" target="_blank">leave us a ★★★★★ rating</a>. A <strong style="text-decoration: underline;">huge</strong> thank you in advance!', 'https://wordpress.org/support/view/plugin-reviews/boxzilla?rate=5#postform' );
731 - }
732 -
733 - return $text;
734 - }
735 -
736 -}
1 +<?php
2 +
3 +namespace Boxzilla\Admin;
4 +
5 +use Boxzilla\Plugin;
6 +use Boxzilla\Box;
7 +use Boxzilla\Boxzilla;
8 +use WP_Post;
9 +use WP_Screen;
10 +
11 +class Admin {
12 +
13 +
14 + /**
15 + * @var Plugin $plugin
16 + */
17 + private $plugin;
18 +
19 + /**
20 + * @var Boxzilla
21 + */
22 + protected $boxzilla;
23 +
24 + /**
25 + * @var ReviewNotice
26 + */
27 + protected $review_notice;
28 +
29 + /**
30 + * @param Plugin $plugin
31 + * @param Boxzilla $boxzilla
32 + */
33 + public function __construct( Plugin $plugin, Boxzilla $boxzilla ) {
34 + $this->plugin = $plugin;
35 + $this->boxzilla = $boxzilla;
36 + $this->review_notice = new ReviewNotice();
37 + }
38 +
39 + /**
40 + * Initialise the all admin related stuff
41 + */
42 + public function init() {
43 + $this->add_hooks();
44 + $this->run_migrations();
45 + $this->review_notice->init();
46 + }
47 +
48 + /**
49 + * Add necessary hooks
50 + */
51 + protected function add_hooks() {
52 + add_action( 'admin_init', array( $this, 'lazy_add_hooks' ) );
53 + add_action( 'admin_init', array( $this, 'register' ) );
54 + add_action( 'init', array( $this, 'listen_for_actions' ) );
55 + add_action( 'admin_menu', array( $this, 'menu' ) );
56 + add_action( 'admin_notices', array( $this, 'notices' ) );
57 + add_action( 'save_post_boxzilla-box', array( $this, 'save_box_options' ), 20, 2 );
58 + add_action( 'trashed_post', array( $this, 'flush_rules' ) );
59 + add_action( 'untrashed_post', array( $this, 'flush_rules' ) );
60 + add_filter( 'bulk_actions-edit-boxzilla-box', array( $this, 'bulk_action_add' ) );
61 + add_filter( 'handle_bulk_actions-edit-boxzilla-box', array( $this, 'bulk_action_handle' ), 10, 3 );
62 +
63 + }
64 +
65 + /**
66 + * Listen for admin actions.
67 + */
68 + public function listen_for_actions() {
69 + // triggered?
70 + $vars = array_merge( $_POST, $_GET );
71 + if ( empty( $vars['_boxzilla_admin_action'] ) ) {
72 + return false;
73 + }
74 +
75 + // authorized?
76 + if ( ! current_user_can( 'edit_posts' ) ) {
77 + return false;
78 + }
79 +
80 + // fire action
81 + $action = $vars['_boxzilla_admin_action'];
82 + do_action( 'boxzilla_admin_' . $action );
83 +
84 + return true;
85 + }
86 +
87 + public function bulk_action_add( $bulk_actions ) {
88 + $bulk_actions['boxzilla_duplicate_box'] = __( 'Duplicate box', 'boxzilla' );
89 + return $bulk_actions;
90 + }
91 +
92 + public function bulk_action_handle( $redirect_to, $doaction, $post_ids ) {
93 + if ( $doaction !== 'boxzilla_duplicate_box' || empty( $post_ids ) ) {
94 + return $redirect_to;
95 + }
96 +
97 + foreach ( $post_ids as $post_id ) {
98 + $post = get_post( $post_id );
99 + if ( $post->post_type !== 'boxzilla-box' ) {
100 + continue;
101 + }
102 +
103 + $new_post_id = wp_insert_post(
104 + array(
105 + 'post_type' => $post->post_type,
106 + 'post_title' => $post->post_title,
107 + 'post_content' => $post->post_content,
108 + 'post_status' => 'draft',
109 + )
110 + );
111 +
112 + $options = get_post_meta( $post_id, 'boxzilla_options', true );
113 + add_post_meta( $new_post_id, 'boxzilla_options', $options );
114 +
115 + $this->flush_rules( $new_post_id );
116 + }
117 +
118 + return $redirect_to;
119 + }
120 +
121 + /**
122 + * All logic for admin notices lives here.
123 + */
124 + public function notices() {
125 + global $pagenow,
126 + $current_screen;
127 +
128 + if ( ( $pagenow === 'plugins.php' || ( $current_screen && $current_screen->post_type === 'boxzilla-box' ) )
129 + && current_user_can( 'install_plugins' )
130 + && is_plugin_active( 'scroll-triggered-boxes/index.php' ) ) {
131 + $url = wp_nonce_url( 'plugins.php?action=deactivate&plugin=scroll-triggered-boxes/index.php', 'deactivate-plugin_' . 'scroll-triggered-boxes/index.php' ); ?>
132 + <div class="notice notice-info">
133 + <p><?php printf( __( 'Awesome, you are using Boxzilla! You can now safely <a href="%s">deactivate the Scroll Triggered Boxes plugin</a>.', 'boxzilla' ), $url ); ?></p>
134 + </div>
135 + <?php
136 + }
137 + }
138 +
139 + /**
140 + * Checks current version against stored version & runs necessary update routines.
141 + *
142 + * @return bool
143 + */
144 + protected function run_migrations() {
145 +
146 + // Only run if db option is at older version than code constant
147 + $previous_version = get_option( 'boxzilla_version', '0' );
148 + $current_version = $this->plugin->version();
149 +
150 + if ( version_compare( $current_version, $previous_version, '<=' ) ) {
151 + return false;
152 + }
153 +
154 + $upgrade_routines = new Migrations( $previous_version, $current_version, __DIR__ . '/migrations' );
155 + $upgrade_routines->run();
156 + update_option( 'boxzilla_version', $current_version );
157 + }
158 +
159 + public function lazy_add_hooks() {
160 + global $pagenow;
161 +
162 + add_action( 'admin_enqueue_scripts', array( $this, 'load_assets' ) );
163 + add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
164 + add_filter( 'tiny_mce_before_init', array( $this, 'tinymce_init' ) );
165 + add_filter( 'manage_edit-boxzilla-box_columns', array( $this, 'post_type_column_titles' ) );
166 + add_action( 'manage_boxzilla-box_posts_custom_column', array( $this, 'post_type_column_content' ), 10, 2 );
167 + add_filter( 'admin_footer_text', array( $this, 'admin_footer_text' ) );
168 +
169 + if ( $pagenow === 'plugins.php' ) {
170 + add_filter( 'plugin_action_links', array( $this, 'add_plugin_settings_link' ), 10, 2 );
171 + add_filter( 'plugin_row_meta', array( $this, 'add_plugin_meta_links' ), 10, 2 );
172 + }
173 + }
174 +
175 + /**
176 + * @param $post_id
177 + */
178 + public function post_type_column_box_id_content( $post_id ) {
179 + echo $post_id;
180 + }
181 +
182 + /**
183 + * @param $column
184 + * @param $post_id
185 + */
186 + public function post_type_column_content( $column, $post_id ) {
187 + $method_name = 'post_type_column_' . $column . '_content';
188 + if ( method_exists( $this, $method_name ) ) {
189 + call_user_func( array( $this, $method_name ), $post_id );
190 + }
191 + }
192 +
193 + /**
194 + * @param $columns
195 + *
196 + * @return mixed
197 + */
198 + public function post_type_column_titles( $columns ) {
199 + $columns = self::array_insert(
200 + $columns,
201 + array(
202 + 'box_id' => __( 'Box ID', 'boxzilla' ),
203 + ),
204 + 1
205 + );
206 +
207 + $columns['title'] = __( 'Box Title', 'boxzilla' );
208 +
209 + return $columns;
210 + }
211 +
212 + /**
213 + * Register stuffs
214 + */
215 + public function register() {
216 +
217 + // register settings
218 + register_setting( 'boxzilla_settings', 'boxzilla_settings', array( $this, 'sanitize_settings' ) );
219 +
220 + // register scripts
221 + $pre_suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
222 +
223 + wp_register_script(
224 + 'boxzilla-admin',
225 + $this->plugin->url( '/assets/js/admin-script' . $pre_suffix . '.js' ),
226 + array(
227 + 'jquery',
228 + 'wp-util',
229 + 'wp-color-picker',
230 + 'suggest',
231 + ),
232 + $this->plugin->version(),
233 + true
234 + );
235 +
236 + // load stylesheets
237 + wp_register_style( 'boxzilla-admin', $this->plugin->url( '/assets/css/admin-styles' . $pre_suffix . '.css' ), array(), $this->plugin->version() );
238 + }
239 +
240 + /**
241 + * Renders the STB Menu items
242 + */
243 + public function menu() {
244 + $menu_items = array(
245 + array(
246 + __( 'Settings', 'boxzilla' ),
247 + __( 'Settings', 'boxzilla' ),
248 + 'boxzilla-settings',
249 + array( $this, 'show_settings_page' ),
250 + ),
251 + array(
252 + __( 'Extensions', 'boxzilla' ),
253 + '<span style="color: orange">' . __( 'Extensions', 'boxzilla' ) . '</span>',
254 + 'boxzilla-extensions',
255 + array( $this, 'show_extensions_page' ),
256 + ),
257 + );
258 +
259 + $menu_items = apply_filters( 'boxzilla_admin_menu_items', $menu_items );
260 +
261 + foreach ( $menu_items as $item ) {
262 + add_submenu_page( 'edit.php?post_type=boxzilla-box', $item[0] . '- Boxzilla', $item[1], 'manage_options', $item[2], $item[3] );
263 + }
264 + }
265 +
266 + /**
267 + * Shows the settings page
268 + */
269 + public function show_settings_page() {
270 + $opts = $this->boxzilla->options;
271 + require __DIR__ . '/views/settings.php';
272 + }
273 +
274 + /**
275 + * Shows the extensions page
276 + */
277 + public function show_extensions_page() {
278 + $extensions = $this->fetch_extensions();
279 + require __DIR__ . '/views/extensions.php';
280 + }
281 +
282 + /**
283 + * Are we currently editing a box?
284 + *
285 + * @return bool
286 + */
287 + protected function on_edit_box_page() {
288 + global $pagenow;
289 +
290 + if ( ! in_array( $pagenow, array( 'post-new.php', 'post.php' ), true ) ) {
291 + return false;
292 + }
293 +
294 + if ( isset( $_GET['post_type'] ) && $_GET['post_type'] === 'boxzilla-box' ) {
295 + return true;
296 + }
297 +
298 + if ( get_post_type() === 'boxzilla-box' ) {
299 + return true;
300 + }
301 +
302 + return false;
303 + }
304 +
305 + /**
306 + * @param $args
307 + *
308 + * @return mixed
309 + */
310 + public function tinymce_init( $args ) {
311 +
312 + // only act on our post type
313 + if ( get_post_type() !== 'boxzilla-box' ) {
314 + return $args;
315 + }
316 +
317 + $args['setup'] = 'function( editor ) { if(typeof(window.Boxzilla_Admin) === \'undefined\') { return; } editor.on("PreInit", window.Boxzilla_Admin.Designer.init ); }';
318 +
319 + return $args;
320 + }
321 +
322 + /**
323 + * Load plugin assets
324 + */
325 + public function load_assets() {
326 + $screen = get_current_screen();
327 +
328 + if ( ! $screen instanceof WP_Screen ) {
329 + return false;
330 + }
331 +
332 + if ( $screen->base === 'edit' && $screen->post_type === 'boxzilla-box' ) {
333 + wp_enqueue_style( 'boxzilla-admin' );
334 + }
335 +
336 + if ( $screen->base === 'post' && $screen->post_type === 'boxzilla-box' ) {
337 + // color picker
338 + wp_enqueue_style( 'wp-color-picker' );
339 +
340 + // load scripts
341 + wp_enqueue_script( 'boxzilla-admin' );
342 +
343 + $data = array(
344 + 'and' => __( 'and', 'boxzilla' ),
345 + 'or' => __( 'or', 'boxzilla' ),
346 + 'enterCommaSeparatedValues' => __( 'Enter a comma-separated list of values.', 'boxzilla' ),
347 + 'enterCommaSeparatedPosts' => __( "Enter a comma-separated list of post slugs or post ID's..", 'boxzilla' ),
348 + 'enterCommaSeparatedPages' => __( "Enter a comma-separated list of page slugs or page ID's..", 'boxzilla' ),
349 + 'enterCommaSeparatedPostTypes' => __( 'Enter a comma-separated list of post types..', 'boxzilla' ),
350 + 'enterCommaSeparatedRelativeUrls' => __( "Enter a comma-separated list of relative URL's, eg /contact/", 'boxzilla' ),
351 + );
352 + wp_localize_script( 'boxzilla-admin', 'boxzilla_i18n', $data );
353 +
354 + // load stylesheets
355 + wp_enqueue_style( 'boxzilla-admin' );
356 +
357 + // allow add-ons to easily load their own scripts or stylesheets
358 + do_action( 'boxzilla_load_admin_assets' );
359 + }
360 +
361 + if ( isset( $_GET['page'] ) && $_GET['page'] === 'boxzilla-settings' ) {
362 + wp_enqueue_style( 'boxzilla-admin' );
363 + }
364 + }
365 +
366 + /**
367 + * Register meta boxes
368 + *
369 + * @param string $post_type
370 + *
371 + * @return bool
372 + */
373 + public function add_meta_boxes( $post_type ) {
374 + if ( $post_type !== 'boxzilla-box' ) {
375 + return false;
376 + }
377 +
378 + add_meta_box(
379 + 'boxzilla-box-appearance-controls',
380 + __( 'Box Appearance', 'boxzilla' ),
381 + array( $this, 'metabox_box_appearance_controls' ),
382 + 'boxzilla-box',
383 + 'normal',
384 + 'core'
385 + );
386 +
387 + add_meta_box(
388 + 'boxzilla-box-options-controls',
389 + __( 'Box Options', 'boxzilla' ),
390 + array( $this, 'metabox_box_option_controls' ),
391 + 'boxzilla-box',
392 + 'normal',
393 + 'core'
394 + );
395 +
396 + add_meta_box(
397 + 'boxzilla-support',
398 + __( 'Looking for help?', 'boxzilla' ),
399 + array( $this, 'metabox_support' ),
400 + 'boxzilla-box',
401 + 'side'
402 + );
403 +
404 + add_meta_box(
405 + 'boxzilla-our-other-plugins',
406 + __( 'Our other plugins', 'boxzilla' ),
407 + array( $this, 'metabox_our_other_plugins' ),
408 + 'boxzilla-box',
409 + 'side'
410 + );
411 +
412 + add_meta_box(
413 + 'boxzilla-email-optin',
414 + __( 'Subscribe to our newsletter', 'boxzilla' ),
415 + array( $this, 'metabox_email_optin' ),
416 + 'boxzilla-box',
417 + 'side'
418 + );
419 +
420 + return true;
421 + }
422 +
423 + /**
424 + * @param \WP_Post $post
425 + * @param $metabox
426 + */
427 + public function metabox_box_appearance_controls( \WP_Post $post, $metabox ) {
428 +
429 + // get box options
430 + $box = new Box( $post );
431 + $opts = $box->get_options();
432 +
433 + // include view
434 + include __DIR__ . '/views/metaboxes/box-appearance-controls.php';
435 + }
436 +
437 + /**
438 + * @param \WP_Post $post
439 + * @param $metabox
440 + */
441 + public function metabox_box_option_controls( \WP_Post $post, $metabox ) {
442 +
443 + // get box options
444 + $box = new Box( $post );
445 + $opts = $box->get_options();
446 + $global_opts = $this->boxzilla->options;
447 +
448 + if ( empty( $opts['rules'] ) ) {
449 + $opts['rules'][] = array(
450 + 'condition' => '',
451 + 'qualifier' => 1,
452 + 'value' => '',
453 + );
454 + }
455 +
456 + // include view
457 + include __DIR__ . '/views/metaboxes/box-option-controls.php';
458 + }
459 +
460 + /**
461 + * @param \WP_Post $post
462 + * @param $metabox
463 + */
464 + public function metabox_email_optin( \WP_Post $post, $metabox ) {
465 + include __DIR__ . '/views/metaboxes/email-optin.php';
466 + }
467 +
468 + /**
469 + * @param \WP_Post $post
470 + * @param $metabox
471 + */
472 + public function metabox_our_other_plugins( \WP_Post $post, $metabox ) {
473 + include __DIR__ . '/views/metaboxes/our-other-plugins.php';
474 + }
475 +
476 + /**
477 + * @param \WP_Post $post
478 + * @param $metabox
479 + */
480 + public function metabox_support( \WP_Post $post, $metabox ) {
481 + include __DIR__ . '/views/metaboxes/need-help.php';
482 + }
483 +
484 +
485 + /**
486 + * Saves box options and rules
487 + *
488 + * @param int $box_id
489 + *
490 + * @return bool
491 + */
492 + public function save_box_options( $box_id, $post ) {
493 +
494 + // Only act on our own post type
495 + if ( $post->post_type !== 'boxzilla-box' ) {
496 + return false;
497 + }
498 +
499 + // is this a revision save?
500 + if ( wp_is_post_revision( $box_id ) || ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) ) {
501 + return false;
502 + }
503 +
504 + // can user edit this post?
505 + if ( ! current_user_can( 'edit_post', $box_id ) ) {
506 + return false;
507 + }
508 +
509 + // make sure options array is set
510 + if ( ! isset( $_POST['boxzilla_box'] ) || ! is_array( $_POST['boxzilla_box'] ) ) {
511 + return false;
512 + }
513 +
514 + // get new options from $_POST
515 + $opts = $this->sanitize_box_options( $_POST['boxzilla_box'] );
516 +
517 + // allow extensions to filter the saved options
518 + $opts = apply_filters( 'boxzilla_saved_options', $opts, $box_id );
519 +
520 + // save individual box settings
521 + update_post_meta( $box_id, 'boxzilla_options', $opts );
522 +
523 + // update global settings if given
524 + if ( ! empty( $_POST['boxzilla_global_settings'] ) ) {
525 + $global_settings = get_option( 'boxzilla_settings', array() );
526 + if ( ! is_array( $global_settings ) ) {
527 + $global_settings = array();
528 + }
529 + $global_settings = array_merge( $global_settings, $_POST['boxzilla_global_settings'] );
530 + update_option( 'boxzilla_settings', $global_settings );
531 + }
532 +
533 + $this->flush_rules( $box_id );
534 +
535 + return true;
536 + }
537 +
538 + /**
539 + * @param array $opts
540 + *
541 + * @return array
542 + */
543 + public function sanitize_settings( $opts ) {
544 + return $opts;
545 + }
546 +
547 + /**
548 + * @param string $url_string
549 + *
550 + * @return string
551 + */
552 + public function sanitize_url( $url_string ) {
553 +
554 + // if empty, just return a slash
555 + if ( empty( $url_string ) ) {
556 + return '/';
557 + }
558 +
559 + // if string looks like an absolute URL, extract just the path
560 + if ( preg_match( '/^((https|http)?\:\/\/)?(\w+\.)?\w+\.\w+\.*/i', $url_string ) ) {
561 +
562 + // make sure URL has scheme prepended, to make parse_url() understand..
563 + $url_string = 'https://' . str_replace( array( 'http://', 'https://' ), '', $url_string );
564 +
565 + // get just the path
566 + $url_string = parse_url( $url_string, PHP_URL_PATH );
567 + }
568 +
569 + // leading slash it
570 + return $url_string;
571 + }
572 +
573 + /**
574 + * @param array $rule
575 + * @return array The sanitized rule array
576 + */
577 + public function sanitize_box_rule( $rule ) {
578 + $rule['value'] = trim( $rule['value'] );
579 +
580 + // convert to array
581 + $rule['value'] = explode( ',', trim( $rule['value'], ',' ) );
582 +
583 + // trim all whitespace in value field
584 + $rule['value'] = array_map( 'trim', $rule['value'] );
585 +
586 + // Make sure "is_url" values have a leading slash
587 + if ( $rule['condition'] === 'is_url' ) {
588 + $rule['value'] = array_map( array( $this, 'sanitize_url' ), $rule['value'] );
589 + }
590 +
591 + // (re)set value to 0 when condition is everywhere
592 + if ( $rule['condition'] === 'everywhere' ) {
593 + $rule['value'] = '';
594 + }
595 +
596 + // convert back to string before saving
597 + if ( is_array( $rule['value'] ) ) {
598 + $rule['value'] = join( ',', $rule['value'] );
599 + }
600 +
601 + return $rule;
602 + }
603 +
604 + /**
605 + * @param array $css
606 + * @return array
607 + */
608 + public function sanitize_box_css( $css ) {
609 +
610 + // sanitize settings
611 + if ( '' !== $css['width'] ) {
612 + $css['width'] = absint( $css['width'] );
613 + }
614 +
615 + if ( '' !== $css['border_width'] ) {
616 + $css['border_width'] = absint( $css['border_width'] );
617 + }
618 +
619 + // make sure colors start with `#`
620 + $color_keys = array( 'color', 'background_color', 'border_color' );
621 + foreach ( $color_keys as $key ) {
622 + $value = $css[ $key ];
623 + $color = sanitize_text_field( $value );
624 +
625 + // make sure color starts with `#`
626 + if ( '' !== $color && $color[0] !== '#' ) {
627 + $color = '#' . $color;
628 + }
629 + $css[ $key ] = $color;
630 + }
631 +
632 + return $css;
633 + }
634 +
635 + /**
636 + * Sanitize the options for this box.
637 + *
638 + * @param array $opts
639 + *
640 + * @return array
641 + */
642 + protected function sanitize_box_options( $opts ) {
643 + $defaults = array(
644 + 'rules' => array(),
645 + 'css' => array(),
646 + );
647 +
648 + $opts = array_replace_recursive( $defaults, $opts );
649 +
650 + $opts['rules'] = array_map( array( $this, 'sanitize_box_rule' ), $opts['rules'] );
651 + $opts['css'] = $this->sanitize_box_css( $opts['css'] );
652 + $opts['cookie']['triggered'] = absint( $opts['cookie']['triggered'] );
653 + $opts['cookie']['dismissed'] = absint( $opts['cookie']['dismissed'] );
654 + $opts['trigger'] = sanitize_text_field( $opts['trigger'] );
655 + $opts['trigger_percentage'] = absint( $opts['trigger_percentage'] );
656 + $opts['trigger_element'] = sanitize_text_field( $opts['trigger_element'] );
657 + $opts['screen_size_condition']['value'] = intval( $opts['screen_size_condition']['value'] );
658 +
659 + return $opts;
660 + }
661 +
662 + /**
663 + * Add the settings link to the Plugins overview
664 + *
665 + * @param array $links
666 + * @param string $slug
667 + *
668 + * @return array
669 + */
670 + public function add_plugin_settings_link( $links, $slug ) {
671 + if ( $slug !== $this->plugin->slug() || ! is_array( $links ) ) {
672 + return $links;
673 + }
674 +
675 + $settings_link = '<a href="' . admin_url( 'edit.php?post_type=boxzilla-box' ) . '">' . __( 'Boxes', 'boxzilla' ) . '</a>';
676 + array_unshift( $links, $settings_link );
677 +
678 + return $links;
679 + }
680 +
681 + /**
682 + * Adds meta links to the plugin in the WP Admin > Plugins screen
683 + *
684 + * @param array $links
685 + * @param string $slug
686 + *
687 + * @return array
688 + */
689 + public function add_plugin_meta_links( $links, $slug ) {
690 + if ( $slug !== $this->plugin->slug() ) {
691 + return $links;
692 + }
693 +
694 + $links[] = '<a href="https://kb.boxzillaplugin.com/#utm_source=wp-plugin&utm_medium=boxzilla&utm_campaign=plugins-page">Documentation</a>';
695 + $links[] = '<a href="https://boxzillaplugin.com/add-ons/#utm_source=wp-plugin&utm_medium=boxzilla&utm_campaign=plugins-page">Add-ons</a>';
696 +
697 + return $links;
698 + }
699 +
700 + /**
701 + * Flush all box rules
702 + *
703 + * Loops through all published boxes and fills the rules option
704 + *
705 + * @param int $post_id
706 + */
707 + public function flush_rules( $post_id ) {
708 +
709 + // only act on our own post type
710 + $post = get_post( $post_id );
711 + if ( ! $post instanceof WP_Post || $post->post_type !== 'boxzilla-box' ) {
712 + return;
713 + }
714 +
715 + // get all published boxes
716 + $query = new \WP_Query;
717 + $boxes = $query->query(
718 + array(
719 + 'post_type' => 'boxzilla-box',
720 + 'post_status' => 'publish',
721 + 'posts_per_page' => - 1,
722 + 'ignore_sticky_posts' => true,
723 + 'no_found_rows' => true,
724 + )
725 + );
726 +
727 + // setup empty array of rules
728 + $rules = array();
729 +
730 + // fill rules array
731 + if ( is_array( $boxes ) ) {
732 + foreach ( $boxes as $box ) {
733 + // get box meta data
734 + $box_meta = get_post_meta( $box->ID, 'boxzilla_options', true );
735 +
736 + // add box rules to all rules
737 + $rules[ $box->ID ] = (array) $box_meta['rules'];
738 + $rules[ $box->ID ]['comparision'] = isset( $box_meta['rules_comparision'] ) ? $box_meta['rules_comparision'] : 'any';
739 + }
740 + }
741 +
742 + update_option( 'boxzilla_rules', $rules );
743 + }
744 +
745 + /**
746 + * Fetches a list of available add-on plugins
747 + *
748 + * @return array
749 + */
750 + protected function fetch_extensions() {
751 + $extensions = get_transient( 'boxzilla_remote_extensions' );
752 + if ( $extensions ) {
753 + return $extensions;
754 + }
755 +
756 + $response = wp_remote_get( 'https://my.boxzillaplugin.com/api/v2/plugins' );
757 + if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) >= 400 ) {
758 + return array();
759 + }
760 +
761 + $body = wp_remote_retrieve_body( $response );
762 + $data = json_decode( $body );
763 + if ( is_array( $data ) ) {
764 + set_transient( 'boxzilla_remote_extensions', $data, 24 * HOUR_IN_SECONDS );
765 + return $data;
766 + }
767 +
768 + return array();
769 + }
770 +
771 + /**
772 + * @param $arr
773 + * @param $insert
774 + * @param $position
775 + *
776 + * @return array
777 + */
778 + public static function array_insert( $arr, $insert, $position ) {
779 + $i = 0;
780 + $ret = array();
781 + foreach ( $arr as $key => $value ) {
782 + if ( $i == $position ) {
783 + foreach ( $insert as $ikey => $ivalue ) {
784 + $ret[ $ikey ] = $ivalue;
785 + }
786 + }
787 + $ret[ $key ] = $value;
788 + $i ++;
789 + }
790 +
791 + return $ret;
792 + }
793 +
794 + /**
795 + * @param string $text
796 + *
797 + * @return string
798 + */
799 + public function admin_footer_text( $text ) {
800 + $screen = get_current_screen();
801 +
802 + if ( ! $screen instanceof WP_Screen ) {
803 + return $text;
804 + }
805 +
806 + $on_edit_page = $screen->parent_base === 'edit' && $screen->post_type === 'boxzilla-box';
807 + if ( $on_edit_page ) {
808 + return sprintf( 'If you enjoy using <strong>Boxzilla</strong>, please <a href="%s" target="_blank">leave us a ★★★★★ rating</a>. A <strong style="text-decoration: underline;">huge</strong> thank you in advance!', 'https://wordpress.org/support/view/plugin-reviews/boxzilla?rate=5#postform' );
809 + }
810 +
811 + return $text;
812 + }
813 +
814 +
815 +}