PluginProbe
Boxzilla – WordPress Popup Builder / 3.4.0
Boxzilla – WordPress Popup Builder v3.4.0
3.4.11 3.4.10 3.4.9 3.4.3 3.4.4 3.4.5 3.4.6 3.4.7 3.4.8 trunk 3.0 3.0.1 3.0.2 3.0.3 3.1 3.1.1 3.1.10 3.1.11 3.1.12 3.1.13 3.1.14 3.1.15 3.1.16 3.1.17 3.1.18 All 72 releases
← All changes | src/admin/class-admin.php +680 -728 trunk3.4.0 View file →
@@ -7,853 +7,805 @@
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'] = esc_attr__( '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 + ?>
132 + <div class="notice notice-info">
133 + <p><?php esc_html_e( 'Awesome, you are using Boxzilla! You can now safely deactivate the Scroll Triggered Boxes plugin.', 'boxzilla' ); ?></p>
134 + </div>
135 + <?php
136 + }
137 + }
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 - public function register()
203 - {
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 + }
204 174
205 - 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 + }
206 181
207 - wp_register_script(
208 - 'boxzilla-admin',
209 - $this->plugin->url('/assets/js/admin-script.js'),
210 - [
211 - 'jquery',
212 - 'wp-util',
213 - 'wp-color-picker',
214 - 'suggest',
215 - ],
216 - $this->plugin->version(),
217 - true
218 - );
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 + }
219 192
220 - wp_register_style('boxzilla-admin', $this->plugin->url('/assets/css/admin-styles.css'), [], $this->plugin->version());
221 - }
193 + /**
194 + * @param $columns
195 + *
196 + * @return mixed
197 + */
198 + public function post_type_column_titles( $columns ) {
199 + $columns = self::array_insert(
200 + $columns,
201 + array(
202 + 'box_id' => esc_html__( 'Box ID', 'boxzilla' ),
203 + ),
204 + 1
205 + );
222 206
223 - /**
224 - * Renders the Boxzilla admin menu items
225 - */
226 - public function menu()
227 - {
228 - $menu_items = [
229 - [
230 - esc_html__('Settings', 'boxzilla'),
231 - esc_html__('Settings', 'boxzilla'),
232 - 'boxzilla-settings',
233 - [ $this, 'show_settings_page' ],
234 - ],
235 - [
236 - esc_html__('Extensions', 'boxzilla'),
237 - '<span style="color: orange">' . esc_html__('Extensions', 'boxzilla') . '</span>',
238 - 'boxzilla-extensions',
239 - [ $this, 'show_extensions_page' ],
240 - ],
241 - ];
207 + $columns['title'] = esc_html__( 'Box Title', 'boxzilla' );
242 208
243 - $menu_items = apply_filters('boxzilla_admin_menu_items', $menu_items);
209 + return $columns;
210 + }
244 211
245 - foreach ($menu_items as $item) {
246 - add_submenu_page('edit.php?post_type=boxzilla-box', $item[0] . '- Boxzilla', $item[1], 'manage_options', $item[2], $item[3]);
247 - }
248 - }
212 + /**
213 + * Register stuffs
214 + */
215 + public function register() {
249 216
250 - /**
251 - * Shows the settings page
252 - */
253 - public function show_settings_page()
254 - {
255 - $opts = $this->boxzilla->options;
256 - require __DIR__ . '/views/settings.php';
257 - }
217 + register_setting( 'boxzilla_settings', 'boxzilla_settings', array( $this, 'sanitize_settings' ) );
258 218
259 - /**
260 - * Shows the extensions page
261 - */
262 - public function show_extensions_page()
263 - {
264 - $extensions = $this->fetch_extensions();
265 - require __DIR__ . '/views/extensions.php';
266 - }
219 + wp_register_script(
220 + 'boxzilla-admin',
221 + $this->plugin->url( '/assets/js/admin-script.js' ),
222 + array(
223 + 'jquery',
224 + 'wp-util',
225 + 'wp-color-picker',
226 + 'suggest',
227 + ),
228 + $this->plugin->version(),
229 + true
230 + );
267 231
268 - /**
269 - * Are we currently editing a box?
270 - *
271 - * @return bool
272 - */
273 - protected function on_edit_box_page()
274 - {
275 - global $pagenow;
232 + wp_register_style( 'boxzilla-admin', $this->plugin->url( '/assets/css/admin-styles.css' ), array(), $this->plugin->version() );
233 + }
276 234
277 - if (! in_array($pagenow, [ 'post-new.php', 'post.php' ], true)) {
278 - return false;
279 - }
235 + /**
236 + * Renders the Boxzilla admin menu items
237 + */
238 + public function menu() {
239 + $menu_items = array(
240 + array(
241 + esc_html__( 'Settings', 'boxzilla' ),
242 + esc_html__( 'Settings', 'boxzilla' ),
243 + 'boxzilla-settings',
244 + array( $this, 'show_settings_page' ),
245 + ),
246 + array(
247 + esc_html__( 'Extensions', 'boxzilla' ),
248 + '<span style="color: orange">' . esc_html__( 'Extensions', 'boxzilla' ) . '</span>',
249 + 'boxzilla-extensions',
250 + array( $this, 'show_extensions_page' ),
251 + ),
252 + );
280 253
281 - if (isset($_GET['post_type']) && $_GET['post_type'] === 'boxzilla-box') {
282 - return true;
283 - }
254 + $menu_items = apply_filters( 'boxzilla_admin_menu_items', $menu_items );
284 255
285 - if (get_post_type() === 'boxzilla-box') {
286 - return true;
287 - }
256 + foreach ( $menu_items as $item ) {
257 + add_submenu_page( 'edit.php?post_type=boxzilla-box', $item[0] . '- Boxzilla', $item[1], 'manage_options', $item[2], $item[3] );
258 + }
259 + }
288 260
289 - return false;
290 - }
261 + /**
262 + * Shows the settings page
263 + */
264 + public function show_settings_page() {
265 + $opts = $this->boxzilla->options;
266 + require __DIR__ . '/views/settings.php';
267 + }
291 268
292 - /**
293 - * @param array $args
294 - *
295 - * @return mixed
296 - */
297 - public function tinymce_init($args)
298 - {
269 + /**
270 + * Shows the extensions page
271 + */
272 + public function show_extensions_page() {
273 + $extensions = $this->fetch_extensions();
274 + require __DIR__ . '/views/extensions.php';
275 + }
299 276
300 - // only act on our post type
301 - if (get_post_type() !== 'boxzilla-box') {
302 - return $args;
303 - }
277 + /**
278 + * Are we currently editing a box?
279 + *
280 + * @return bool
281 + */
282 + protected function on_edit_box_page() {
283 + global $pagenow;
304 284
305 - $args['setup'] = 'function( editor ) { if(typeof(window.Boxzilla_Admin) === \'undefined\') { return; } editor.on("PreInit", window.Boxzilla_Admin.Designer.init ); }';
285 + if ( ! in_array( $pagenow, array( 'post-new.php', 'post.php' ), true ) ) {
286 + return false;
287 + }
306 288
307 - return $args;
308 - }
289 + if ( isset( $_GET['post_type'] ) && $_GET['post_type'] === 'boxzilla-box' ) {
290 + return true;
291 + }
309 292
310 - /**
311 - * Load plugin assets
312 - */
313 - public function load_assets()
314 - {
315 - $screen = get_current_screen();
293 + if ( get_post_type() === 'boxzilla-box' ) {
294 + return true;
295 + }
316 296
317 - if (! $screen instanceof WP_Screen) {
318 - return false;
319 - }
297 + return false;
298 + }
320 299
321 - if ($screen->base === 'edit' && $screen->post_type === 'boxzilla-box') {
322 - wp_enqueue_style('boxzilla-admin');
323 - }
300 + /**
301 + * @param $args
302 + *
303 + * @return mixed
304 + */
305 + public function tinymce_init( $args ) {
324 306
325 - if ($screen->base === 'post' && $screen->post_type === 'boxzilla-box') {
326 - // color picker
327 - wp_enqueue_style('wp-color-picker');
307 + // only act on our post type
308 + if ( get_post_type() !== 'boxzilla-box' ) {
309 + return $args;
310 + }
328 311
329 - // load scripts
330 - wp_enqueue_script('boxzilla-admin');
312 + $args['setup'] = 'function( editor ) { if(typeof(window.Boxzilla_Admin) === \'undefined\') { return; } editor.on("PreInit", window.Boxzilla_Admin.Designer.init ); }';
331 313
332 - $data = [
333 - 'and' => esc_html__('and', 'boxzilla'),
334 - 'or' => esc_html__('or', 'boxzilla'),
335 - 'enterCommaSeparatedValues' => esc_html__('Enter a comma-separated list of values.', 'boxzilla'),
336 - 'enterCommaSeparatedPosts' => esc_html__("Enter a comma-separated list of post slugs or post ID's..", 'boxzilla'),
337 - 'enterCommaSeparatedPages' => esc_html__("Enter a comma-separated list of page slugs or page ID's..", 'boxzilla'),
338 - 'enterCommaSeparatedPostTypes' => esc_html__('Enter a comma-separated list of post types..', 'boxzilla'),
339 - 'enterCommaSeparatedRelativeUrls' => esc_html__("Enter a comma-separated list of relative URL's, eg /contact/", 'boxzilla'),
340 - ];
341 - wp_localize_script('boxzilla-admin', 'boxzilla_i18n', $data);
314 + return $args;
315 + }
342 316
343 - // load stylesheets
344 - wp_enqueue_style('boxzilla-admin');
317 + /**
318 + * Load plugin assets
319 + */
320 + public function load_assets() {
321 + $screen = get_current_screen();
345 322
346 - // allow add-ons to easily load their own scripts or stylesheets
347 - do_action('boxzilla_load_admin_assets');
348 - }
323 + if ( ! $screen instanceof WP_Screen ) {
324 + return false;
325 + }
349 326
350 - if (isset($_GET['page']) && $_GET['page'] === 'boxzilla-settings') {
351 - wp_enqueue_style('boxzilla-admin');
352 - }
353 - }
327 + if ( $screen->base === 'edit' && $screen->post_type === 'boxzilla-box' ) {
328 + wp_enqueue_style( 'boxzilla-admin' );
329 + }
354 330
355 - /**
356 - * Register meta boxes
357 - *
358 - * @param string $post_type
359 - */
360 - public function add_meta_boxes($post_type)
361 - {
362 - if ($post_type !== 'boxzilla-box') {
363 - return;
364 - }
331 + if ( $screen->base === 'post' && $screen->post_type === 'boxzilla-box' ) {
332 + // color picker
333 + wp_enqueue_style( 'wp-color-picker' );
365 334
366 - add_meta_box(
367 - 'boxzilla-box-appearance-controls',
368 - esc_html__('Box Appearance', 'boxzilla'),
369 - [ $this, 'metabox_box_appearance_controls' ],
370 - 'boxzilla-box',
371 - 'normal',
372 - 'core'
373 - );
335 + // load scripts
336 + wp_enqueue_script( 'boxzilla-admin' );
374 337
375 - add_meta_box(
376 - 'boxzilla-box-options-controls',
377 - esc_html__('Box Options', 'boxzilla'),
378 - [ $this, 'metabox_box_option_controls' ],
379 - 'boxzilla-box',
380 - 'normal',
381 - 'core'
382 - );
338 + $data = array(
339 + 'and' => esc_html__( 'and', 'boxzilla' ),
340 + 'or' => esc_html__( 'or', 'boxzilla' ),
341 + 'enterCommaSeparatedValues' => esc_html__( 'Enter a comma-separated list of values.', 'boxzilla' ),
342 + 'enterCommaSeparatedPosts' => esc_html__( "Enter a comma-separated list of post slugs or post ID's..", 'boxzilla' ),
343 + 'enterCommaSeparatedPages' => esc_html__( "Enter a comma-separated list of page slugs or page ID's..", 'boxzilla' ),
344 + 'enterCommaSeparatedPostTypes' => esc_html__( 'Enter a comma-separated list of post types..', 'boxzilla' ),
345 + 'enterCommaSeparatedRelativeUrls' => esc_html__( "Enter a comma-separated list of relative URL's, eg /contact/", 'boxzilla' ),
346 + );
347 + wp_localize_script( 'boxzilla-admin', 'boxzilla_i18n', $data );
383 348
384 - add_meta_box(
385 - 'boxzilla-support',
386 - esc_html__('Looking for help?', 'boxzilla'),
387 - [ $this, 'metabox_support' ],
388 - 'boxzilla-box',
389 - 'side'
390 - );
349 + // load stylesheets
350 + wp_enqueue_style( 'boxzilla-admin' );
391 351
392 - add_meta_box(
393 - 'boxzilla-our-other-plugins',
394 - esc_html__('Our other plugins', 'boxzilla'),
395 - [ $this, 'metabox_our_other_plugins' ],
396 - 'boxzilla-box',
397 - 'side'
398 - );
399 - }
352 + // allow add-ons to easily load their own scripts or stylesheets
353 + do_action( 'boxzilla_load_admin_assets' );
354 + }
400 355
401 - /**
402 - * @param \WP_Post $post
403 - * @param $metabox
404 - */
405 - public function metabox_box_appearance_controls(\WP_Post $post, $metabox)
406 - {
356 + if ( isset( $_GET['page'] ) && $_GET['page'] === 'boxzilla-settings' ) {
357 + wp_enqueue_style( 'boxzilla-admin' );
358 + }
359 + }
407 360
408 - // get box options
409 - $box = new Box($post);
410 - $opts = $box->get_options();
361 + /**
362 + * Register meta boxes
363 + *
364 + * @param string $post_type
365 + *
366 + * @return bool
367 + */
368 + public function add_meta_boxes( $post_type ) {
369 + if ( $post_type !== 'boxzilla-box' ) {
370 + return false;
371 + }
411 372
412 - // include view
413 - include __DIR__ . '/views/metaboxes/box-appearance-controls.php';
414 - }
373 + add_meta_box(
374 + 'boxzilla-box-appearance-controls',
375 + esc_html__( 'Box Appearance', 'boxzilla' ),
376 + array( $this, 'metabox_box_appearance_controls' ),
377 + 'boxzilla-box',
378 + 'normal',
379 + 'core'
380 + );
415 381
416 - /**
417 - * @param \WP_Post $post
418 - * @param $metabox
419 - */
420 - public function metabox_box_option_controls(\WP_Post $post, $metabox)
421 - {
382 + add_meta_box(
383 + 'boxzilla-box-options-controls',
384 + esc_html__( 'Box Options', 'boxzilla' ),
385 + array( $this, 'metabox_box_option_controls' ),
386 + 'boxzilla-box',
387 + 'normal',
388 + 'core'
389 + );
422 390
423 - // get box options
424 - $box = new Box($post);
425 - $opts = $box->get_options();
426 - $global_opts = $this->boxzilla->options;
391 + add_meta_box(
392 + 'boxzilla-support',
393 + esc_html__( 'Looking for help?', 'boxzilla' ),
394 + array( $this, 'metabox_support' ),
395 + 'boxzilla-box',
396 + 'side'
397 + );
427 398
428 - if (empty($opts['rules'])) {
429 - $opts['rules'][] = [
430 - 'condition' => '',
431 - 'qualifier' => 1,
432 - 'value' => '',
433 - ];
434 - }
399 + add_meta_box(
400 + 'boxzilla-our-other-plugins',
401 + esc_html__( 'Our other plugins', 'boxzilla' ),
402 + array( $this, 'metabox_our_other_plugins' ),
403 + 'boxzilla-box',
404 + 'side'
405 + );
435 406
436 - // include view
437 - include __DIR__ . '/views/metaboxes/box-option-controls.php';
438 - }
407 + add_meta_box(
408 + 'boxzilla-email-optin',
409 + esc_html__( 'Subscribe to our newsletter', 'boxzilla' ),
410 + array( $this, 'metabox_email_optin' ),
411 + 'boxzilla-box',
412 + 'side'
413 + );
439 414
440 - /**
441 - * @param \WP_Post $post
442 - * @param $metabox
443 - */
444 - public function metabox_our_other_plugins(\WP_Post $post, $metabox)
445 - {
446 - include __DIR__ . '/views/metaboxes/our-other-plugins.php';
447 - }
415 + return true;
416 + }
448 417
449 - /**
450 - * @param \WP_Post $post
451 - * @param $metabox
452 - */
453 - public function metabox_support(\WP_Post $post, $metabox)
454 - {
455 - include __DIR__ . '/views/metaboxes/need-help.php';
456 - }
418 + /**
419 + * @param \WP_Post $post
420 + * @param $metabox
421 + */
422 + public function metabox_box_appearance_controls( \WP_Post $post, $metabox ) {
457 423
424 + // get box options
425 + $box = new Box( $post );
426 + $opts = $box->get_options();
458 427
459 - /**
460 - * Saves box options and rules
461 - *
462 - * @param int $box_id
463 - * @param \WP_Post $post
464 - */
465 - public function save_box_options($box_id, $post)
466 - {
467 - // Only act on our own post type
468 - if ($post->post_type !== 'boxzilla-box') {
469 - return;
470 - }
428 + // include view
429 + include __DIR__ . '/views/metaboxes/box-appearance-controls.php';
430 + }
471 431
472 - // is this a revision save?
473 - if (wp_is_post_revision($box_id) || ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )) {
474 - return;
475 - }
432 + /**
433 + * @param \WP_Post $post
434 + * @param $metabox
435 + */
436 + public function metabox_box_option_controls( \WP_Post $post, $metabox ) {
476 437
477 - // can user edit this post?
478 - if (! current_user_can('edit_post', $box_id)) {
479 - return;
480 - }
438 + // get box options
439 + $box = new Box( $post );
440 + $opts = $box->get_options();
441 + $global_opts = $this->boxzilla->options;
481 442
482 - // phpcs:disable WordPress.Security.NonceVerification
483 - // make sure options array is set
484 - if (! isset($_POST['boxzilla_box']) || ! is_array($_POST['boxzilla_box'])) {
485 - return;
486 - }
443 + if ( empty( $opts['rules'] ) ) {
444 + $opts['rules'][] = array(
445 + 'condition' => '',
446 + 'qualifier' => 1,
447 + 'value' => '',
448 + );
449 + }
487 450
488 - // get new options from $_POST
489 - $opts = $this->sanitize_box_options(wp_unslash($_POST['boxzilla_box']));
451 + // include view
452 + include __DIR__ . '/views/metaboxes/box-option-controls.php';
453 + }
490 454
491 - // allow extensions to filter the saved options
492 - $opts = apply_filters('boxzilla_saved_options', $opts, $box_id);
455 + /**
456 + * @param \WP_Post $post
457 + * @param $metabox
458 + */
459 + public function metabox_email_optin( \WP_Post $post, $metabox ) {
460 + include __DIR__ . '/views/metaboxes/email-optin.php';
461 + }
493 462
494 - // save individual box settings
495 - update_post_meta($box_id, 'boxzilla_options', $opts);
463 + /**
464 + * @param \WP_Post $post
465 + * @param $metabox
466 + */
467 + public function metabox_our_other_plugins( \WP_Post $post, $metabox ) {
468 + include __DIR__ . '/views/metaboxes/our-other-plugins.php';
469 + }
496 470
497 - // update global settings if given
498 - if (! empty($_POST['boxzilla_global_settings'])) {
499 - $raw_global_settings = wp_unslash($_POST['boxzilla_global_settings']);
500 - $global_settings = get_option('boxzilla_settings', []);
501 - if (! is_array($global_settings)) {
502 - $global_settings = [];
503 - }
504 - if (isset($raw_global_settings['test_mode'])) {
505 - $global_settings['test_mode'] = absint($raw_global_settings['test_mode']);
506 - }
507 - update_option('boxzilla_settings', $global_settings);
508 - }
471 + /**
472 + * @param \WP_Post $post
473 + * @param $metabox
474 + */
475 + public function metabox_support( \WP_Post $post, $metabox ) {
476 + include __DIR__ . '/views/metaboxes/need-help.php';
477 + }
509 478
510 - $this->flush_rules($box_id);
511 479
512 - // phpcs:enable WordPress.Security.NonceVerification
513 - }
480 + /**
481 + * Saves box options and rules
482 + *
483 + * @param int $box_id
484 + *
485 + * @return bool
486 + */
487 + public function save_box_options( $box_id, $post ) {
514 488
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 - }
489 + // Only act on our own post type
490 + if ( $post->post_type !== 'boxzilla-box' ) {
491 + return false;
492 + }
527 493
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 - }
494 + // is this a revision save?
495 + if ( wp_is_post_revision( $box_id ) || ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) ) {
496 + return false;
497 + }
539 498
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);
499 + // can user edit this post?
500 + if ( ! current_user_can( 'edit_post', $box_id ) ) {
501 + return false;
502 + }
544 503
545 - // get just the path
546 - $url_string = parse_url($url_string, PHP_URL_PATH);
547 - }
504 + // make sure options array is set
505 + if ( ! isset( $_POST['boxzilla_box'] ) || ! is_array( $_POST['boxzilla_box'] ) ) {
506 + return false;
507 + }
548 508
549 - return $url_string;
550 - }
509 + // get new options from $_POST
510 + $opts = $this->sanitize_box_options( $_POST['boxzilla_box'] );
551 511
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']);
512 + // allow extensions to filter the saved options
513 + $opts = apply_filters( 'boxzilla_saved_options', $opts, $box_id );
559 514
560 - // convert to array
561 - $rule['value'] = explode(',', trim($rule['value'], ','));
515 + // save individual box settings
516 + update_post_meta( $box_id, 'boxzilla_options', $opts );
562 517
563 - // trim all whitespace in value field
564 - $rule['value'] = array_map('trim', $rule['value']);
518 + // update global settings if given
519 + if ( ! empty( $_POST['boxzilla_global_settings'] ) ) {
520 + $global_settings = get_option( 'boxzilla_settings', array() );
521 + if ( ! is_array( $global_settings ) ) {
522 + $global_settings = array();
523 + }
524 + $global_settings = array_merge( $global_settings, $_POST['boxzilla_global_settings'] );
525 + update_option( 'boxzilla_settings', $global_settings );
526 + }
565 527
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 - }
528 + $this->flush_rules( $box_id );
570 529
571 - // (re)set value to 0 when condition is everywhere
572 - if ($rule['condition'] === 'everywhere') {
573 - $rule['value'] = '';
574 - }
530 + return true;
531 + }
575 532
576 - // convert back to string before saving
577 - if (is_array($rule['value'])) {
578 - $rule['value'] = join(',', $rule['value']);
579 - }
533 + /**
534 + * @param array $opts
535 + *
536 + * @return array
537 + */
538 + public function sanitize_settings( $opts ) {
539 + return $opts;
540 + }
580 541
581 - return $rule;
582 - }
542 + /**
543 + * @param string $url_string
544 + *
545 + * @return string
546 + */
547 + public function sanitize_url( $url_string ) {
583 548
584 - /**
585 - * @param array $css
586 - * @return array
587 - */
588 - public function sanitize_box_css($css)
589 - {
549 + // if empty, just return a slash
550 + if ( empty( $url_string ) ) {
551 + return '/';
552 + }
590 553
591 - // sanitize settings
592 - if ('' !== $css['width']) {
593 - $css['width'] = absint($css['width']);
594 - }
554 + // if string looks like an absolute URL, extract just the path
555 + if ( preg_match( '/^((https|http)?\:\/\/)?(\w+\.)?\w+\.\w+\.*/i', $url_string ) ) {
595 556
596 - if ('' !== $css['border_width']) {
597 - $css['border_width'] = absint($css['border_width']);
598 - }
557 + // make sure URL has scheme prepended, to make parse_url() understand..
558 + $url_string = 'https://' . str_replace( array( 'http://', 'https://' ), '', $url_string );
599 559
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);
560 + // get just the path
561 + $url_string = parse_url( $url_string, PHP_URL_PATH );
562 + }
605 563
606 - // make sure color starts with `#`
607 - if ('' !== $color && $color[0] !== '#') {
608 - $color = '#' . $color;
609 - }
610 - $css[ $key ] = $color;
611 - }
564 + // leading slash it
565 + return $url_string;
566 + }
612 567
613 - return $css;
614 - }
568 + /**
569 + * @param array $rule
570 + * @return array The sanitized rule array
571 + */
572 + public function sanitize_box_rule( $rule ) {
573 + $rule['value'] = trim( $rule['value'] );
615 574
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 - ];
575 + // convert to array
576 + $rule['value'] = explode( ',', trim( $rule['value'], ',' ) );
629 577
630 - $opts = array_replace_recursive($defaults, $opts);
578 + // trim all whitespace in value field
579 + $rule['value'] = array_map( 'trim', $rule['value'] );
631 580
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'] = (int) ($opts['screen_size_condition']['value']);
581 + // Make sure "is_url" values have a leading slash
582 + if ( $rule['condition'] === 'is_url' ) {
583 + $rule['value'] = array_map( array( $this, 'sanitize_url' ), $rule['value'] );
584 + }
640 585
641 - return $opts;
642 - }
586 + // (re)set value to 0 when condition is everywhere
587 + if ( $rule['condition'] === 'everywhere' ) {
588 + $rule['value'] = '';
589 + }
643 590
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 - }
591 + // convert back to string before saving
592 + if ( is_array( $rule['value'] ) ) {
593 + $rule['value'] = join( ',', $rule['value'] );
594 + }
657 595
658 - $settings_link = '<a href="' . esc_url(admin_url('edit.php?post_type=boxzilla-box')) . '">' . esc_html__('Boxes', 'boxzilla') . '</a>';
659 - array_unshift($links, $settings_link);
660 - return $links;
661 - }
596 + return $rule;
597 + }
662 598
663 - /**
664 - * Adds meta links to the plugin in the WP Admin > Plugins screen
665 - *
666 - * @param array $links
667 - * @param string $slug
668 - *
669 - * @return array
670 - */
671 - public function add_plugin_meta_links($links, $slug)
672 - {
673 - if ($slug !== $this->plugin->slug()) {
674 - return $links;
675 - }
599 + /**
600 + * @param array $css
601 + * @return array
602 + */
603 + public function sanitize_box_css( $css ) {
676 604
677 - $links[] = '<a href="https://boxzillaplugin.com/kb/">Documentation</a>';
678 - $links[] = '<a href="https://boxzillaplugin.com/add-ons/">Add-ons</a>';
605 + // sanitize settings
606 + if ( '' !== $css['width'] ) {
607 + $css['width'] = absint( $css['width'] );
608 + }
679 609
680 - return $links;
681 - }
610 + if ( '' !== $css['border_width'] ) {
611 + $css['border_width'] = absint( $css['border_width'] );
612 + }
682 613
683 - /**
684 - * Flush all box rules
685 - *
686 - * Loops through all published boxes and fills the rules option
687 - *
688 - * @param int $post_id
689 - */
690 - public function flush_rules($post_id)
691 - {
614 + // make sure colors start with `#`
615 + $color_keys = array( 'color', 'background_color', 'border_color' );
616 + foreach ( $color_keys as $key ) {
617 + $value = $css[ $key ];
618 + $color = sanitize_text_field( $value );
692 619
693 - // only act on our own post type
694 - $post = get_post($post_id);
695 - if (! $post instanceof WP_Post || $post->post_type !== 'boxzilla-box') {
696 - return;
697 - }
620 + // make sure color starts with `#`
621 + if ( '' !== $color && $color[0] !== '#' ) {
622 + $color = '#' . $color;
623 + }
624 + $css[ $key ] = $color;
625 + }
698 626
699 - // get all published boxes
700 - $query = new \WP_Query();
701 - $boxes = $query->query(
702 - [
703 - 'post_type' => 'boxzilla-box',
704 - 'post_status' => 'publish',
705 - 'posts_per_page' => - 1,
706 - 'ignore_sticky_posts' => true,
707 - 'no_found_rows' => true,
708 - ]
709 - );
627 + return $css;
628 + }
710 629
711 - // setup empty array of rules
712 - $rules = [];
630 + /**
631 + * Sanitize the options for this box.
632 + *
633 + * @param array $opts
634 + *
635 + * @return array
636 + */
637 + protected function sanitize_box_options( $opts ) {
638 + $defaults = array(
639 + 'rules' => array(),
640 + 'css' => array(),
641 + );
713 642
714 - // fill rules array
715 - if (is_array($boxes)) {
716 - foreach ($boxes as $box) {
717 - // get box meta data
718 - $box_meta = get_post_meta($box->ID, 'boxzilla_options', true);
719 - if (! $box_meta || ! is_array($box_meta) || empty($box_meta['rules'])) {
720 - continue;
721 - }
643 + $opts = array_replace_recursive( $defaults, $opts );
722 644
723 - // add box rules to all rules
724 - $rules[ $box->ID ] = (array) $box_meta['rules'];
725 - $rules[ $box->ID ]['comparision'] = isset($box_meta['rules_comparision']) ? $box_meta['rules_comparision'] : 'any';
726 - }
727 - }
645 + $opts['rules'] = array_map( array( $this, 'sanitize_box_rule' ), $opts['rules'] );
646 + $opts['css'] = $this->sanitize_box_css( $opts['css'] );
647 + $opts['cookie']['triggered'] = absint( $opts['cookie']['triggered'] );
648 + $opts['cookie']['dismissed'] = absint( $opts['cookie']['dismissed'] );
649 + $opts['trigger'] = sanitize_text_field( $opts['trigger'] );
650 + $opts['trigger_percentage'] = absint( $opts['trigger_percentage'] );
651 + $opts['trigger_element'] = sanitize_text_field( $opts['trigger_element'] );
652 + $opts['screen_size_condition']['value'] = intval( $opts['screen_size_condition']['value'] );
728 653
729 - update_option('boxzilla_rules', $rules);
730 - }
654 + return $opts;
655 + }
731 656
732 - /**
733 - * Fetches a list of available add-on plugins
734 - *
735 - * @return array
736 - */
737 - protected function fetch_extensions()
738 - {
739 - return [
740 - 0 =>
741 - (object) [
742 - 'name' => 'Theme Pack',
743 - 'description' => 'A beautiful set of eye-catching themes for your boxes',
744 - 'url' => 'https://boxzillaplugin.com/add-ons/theme-pack',
745 - 'image_url' => plugins_url("assets/img/theme-pack.png", BOXZILLA_FILE),
746 - 'page_url' => 'https://boxzillaplugin.com/add-ons/theme-pack',
747 - ],
748 - 1 =>
749 - (object) [
750 - 'name' => 'MailChimp',
751 - 'description' => 'Hide boxes for MailChimp subscribers.',
752 - 'url' => 'https://boxzillaplugin.com/add-ons/mailchimp',
753 - 'image_url' => plugins_url("assets/img/mailchimp.png", BOXZILLA_FILE),
754 - 'page_url' => 'https://boxzillaplugin.com/add-ons/mailchimp',
755 - ],
756 - 2 =>
757 - (object) [
758 - 'name' => 'Google Analytics',
759 - 'description' => 'Track box events in Google Analytics.',
760 - 'url' => 'https://boxzillaplugin.com/add-ons/google-analytics',
761 - 'image_url' => plugins_url("assets/img/google-analytics.png", BOXZILLA_FILE),
762 - 'page_url' => 'https://boxzillaplugin.com/add-ons/google-analytics',
763 - ],
764 - 3 =>
765 - (object) [
766 - 'name' => 'Exit Intent',
767 - 'description' => 'Trigger a box when a visitor intents to leave your webpage.',
768 - 'url' => 'https://boxzillaplugin.com/add-ons/exit-intent',
769 - 'image_url' => plugins_url("assets/img/exit-intent.png", BOXZILLA_FILE),
770 - 'page_url' => 'https://boxzillaplugin.com/add-ons/exit-intent',
771 - ],
772 - 4 =>
773 - (object) [
774 - 'name' => 'Time on Site',
775 - 'description' => 'Trigger a box after a visitor spent an amount of time on your site.',
776 - 'url' => 'https://boxzillaplugin.com/add-ons/time-on-site',
777 - 'image_url' => plugins_url("assets/img/time-on-site.png", BOXZILLA_FILE),
778 - 'page_url' => 'https://boxzillaplugin.com/add-ons/time-on-site',
779 - ],
780 - 5 =>
781 - (object) [
782 - 'name' => 'Pageviews',
783 - 'description' => 'Allows you to trigger a box after a certain number of pageviews.',
784 - 'url' => 'https://boxzillaplugin.com/add-ons/pageviews',
785 - 'image_url' => plugins_url("assets/img/pageviews.png", BOXZILLA_FILE),
786 - 'page_url' => 'https://boxzillaplugin.com/add-ons/pageviews',
787 - ],
788 - 6 =>
789 - (object) [
790 - 'name' => 'WooCommerce',
791 - 'description' => 'Offers advanced integration with WooCommerce.',
792 - 'url' => 'https://boxzillaplugin.com/add-ons/woocommerce',
793 - 'image_url' => plugins_url("assets/img/woocommerce.png", BOXZILLA_FILE),
794 - 'page_url' => 'https://boxzillaplugin.com/add-ons/woocommerce',
795 - ],
796 - 7 =>
797 - (object) [
798 - 'name' => 'Stats',
799 - 'description' => 'Measure how well your boxes are performing with simple statistics.',
800 - 'url' => 'https://boxzillaplugin.com/add-ons/stats',
801 - 'image_url' => plugins_url("assets/img/stats.png", BOXZILLA_FILE),
802 - 'page_url' => 'https://boxzillaplugin.com/add-ons/stats',
803 - ],
804 - 8 =>
805 - (object) [
806 - 'name' => 'Date Range',
807 - 'description' => 'Only load a box when date is within a certain range of one or two dates.',
808 - 'url' => 'https://boxzillaplugin.com/add-ons/date-range/',
809 - 'image_url' => plugins_url("assets/img/date-range.png", BOXZILLA_FILE),
810 - 'page_url' => 'https://boxzillaplugin.com/add-ons/date-range/',
811 - ],
812 - ];
813 - }
657 + /**
658 + * Add the settings link to the Plugins overview
659 + *
660 + * @param array $links
661 + * @param string $slug
662 + *
663 + * @return array
664 + */
665 + public function add_plugin_settings_link( $links, $slug ) {
666 + if ( $slug !== $this->plugin->slug() || ! is_array( $links ) ) {
667 + return $links;
668 + }
814 669
815 - /**
816 - * @param $arr
817 - * @param $insert
818 - * @param $position
819 - *
820 - * @return array
821 - */
822 - public static function array_insert($arr, $insert, $position)
823 - {
824 - $i = 0;
825 - $ret = [];
826 - foreach ($arr as $key => $value) {
827 - if ($i == $position) {
828 - foreach ($insert as $ikey => $ivalue) {
829 - $ret[ $ikey ] = $ivalue;
830 - }
831 - }
832 - $ret[ $key ] = $value;
833 - $i++;
834 - }
670 + $href = admin_url( 'edit.php?post_type=boxzilla-box' );
671 + $label = esc_html__( 'Boxes', 'boxzilla' );
672 + $settings_link = "<a href=\"{$href}\">{$label}</a>";
673 + array_unshift( $links, $settings_link );
674 + return $links;
675 + }
835 676
836 - return $ret;
837 - }
677 + /**
678 + * Adds meta links to the plugin in the WP Admin > Plugins screen
679 + *
680 + * @param array $links
681 + * @param string $slug
682 + *
683 + * @return array
684 + */
685 + public function add_plugin_meta_links( $links, $slug ) {
686 + if ( $slug !== $this->plugin->slug() ) {
687 + return $links;
688 + }
838 689
839 - /**
840 - * @param string $text
841 - *
842 - * @return string
843 - */
844 - public function admin_footer_text($text)
845 - {
846 - $screen = get_current_screen();
690 + $links[] = '<a href="https://kb.boxzillaplugin.com/">Documentation</a>';
691 + $links[] = '<a href="https://boxzillaplugin.com/add-ons/">Add-ons</a>';
847 692
848 - if (! $screen instanceof WP_Screen) {
849 - return $text;
850 - }
693 + return $links;
694 + }
851 695
852 - $on_edit_page = $screen->parent_base === 'edit' && $screen->post_type === 'boxzilla-box';
853 - if ($on_edit_page) {
854 - 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');
855 - }
696 + /**
697 + * Flush all box rules
698 + *
699 + * Loops through all published boxes and fills the rules option
700 + *
701 + * @param int $post_id
702 + */
703 + public function flush_rules( $post_id ) {
856 704
857 - return $text;
858 - }
705 + // only act on our own post type
706 + $post = get_post( $post_id );
707 + if ( ! $post instanceof WP_Post || $post->post_type !== 'boxzilla-box' ) {
708 + return;
709 + }
710 +
711 + // get all published boxes
712 + $query = new \WP_Query;
713 + $boxes = $query->query(
714 + array(
715 + 'post_type' => 'boxzilla-box',
716 + 'post_status' => 'publish',
717 + 'posts_per_page' => - 1,
718 + 'ignore_sticky_posts' => true,
719 + 'no_found_rows' => true,
720 + )
721 + );
722 +
723 + // setup empty array of rules
724 + $rules = array();
725 +
726 + // fill rules array
727 + if ( is_array( $boxes ) ) {
728 + foreach ( $boxes as $box ) {
729 + // get box meta data
730 + $box_meta = get_post_meta( $box->ID, 'boxzilla_options', true );
731 +
732 + // add box rules to all rules
733 + $rules[ $box->ID ] = (array) $box_meta['rules'];
734 + $rules[ $box->ID ]['comparision'] = isset( $box_meta['rules_comparision'] ) ? $box_meta['rules_comparision'] : 'any';
735 + }
736 + }
737 +
738 + update_option( 'boxzilla_rules', $rules );
739 + }
740 +
741 + /**
742 + * Fetches a list of available add-on plugins
743 + *
744 + * @return array
745 + */
746 + protected function fetch_extensions() {
747 + $extensions = get_transient( 'boxzilla_remote_extensions' );
748 + if ( $extensions ) {
749 + return $extensions;
750 + }
751 +
752 + $response = wp_remote_get( 'https://my.boxzillaplugin.com/api/v2/plugins' );
753 + if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) >= 400 ) {
754 + return array();
755 + }
756 +
757 + $body = wp_remote_retrieve_body( $response );
758 + $data = json_decode( $body );
759 + if ( is_array( $data ) ) {
760 + set_transient( 'boxzilla_remote_extensions', $data, 24 * HOUR_IN_SECONDS );
761 + return $data;
762 + }
763 +
764 + return array();
765 + }
766 +
767 + /**
768 + * @param $arr
769 + * @param $insert
770 + * @param $position
771 + *
772 + * @return array
773 + */
774 + public static function array_insert( $arr, $insert, $position ) {
775 + $i = 0;
776 + $ret = array();
777 + foreach ( $arr as $key => $value ) {
778 + if ( $i == $position ) {
779 + foreach ( $insert as $ikey => $ivalue ) {
780 + $ret[ $ikey ] = $ivalue;
781 + }
782 + }
783 + $ret[ $key ] = $value;
784 + $i ++;
785 + }
786 +
787 + return $ret;
788 + }
789 +
790 + /**
791 + * @param string $text
792 + *
793 + * @return string
794 + */
795 + public function admin_footer_text( $text ) {
796 + $screen = get_current_screen();
797 +
798 + if ( ! $screen instanceof WP_Screen ) {
799 + return $text;
800 + }
801 +
802 + $on_edit_page = $screen->parent_base === 'edit' && $screen->post_type === 'boxzilla-box';
803 + if ( $on_edit_page ) {
804 + return sprintf( 'If you enjoy using <strong>Boxzilla</strong>, please <a href="%s" target="_blank">leave us a ★★★★★ rating</a>. A <strong style="text-decoration: underline;">huge</strong> thank you in advance!', 'https://wordpress.org/support/view/plugin-reviews/boxzilla?rate=5#postform' );
805 + }
806 +
807 + return $text;
808 + }
809 +
810 +
859 811 }