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