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