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