PluginProbe
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment / 1.1.0
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment v1.1.0
3.1.14 3.1.13 3.1.12 3.1.11 3.1.10 3.1.9 3.1.8 3.1.7 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 All 123 releases
firebox / Inc / Core / Admin / Includes / Cpts / Firebox.php

Firebox.php in FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment 1.1.0, at Inc/Core/Admin/Includes/Cpts/Firebox.php

528 lines 11.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package FireBox
4 * @version 1.1.0 Free
5 *
6 * @author FirePlugins <[email protected]>
7 * @link https://www.fireplugins.com
8 * @copyright Copyright © 2022 FirePlugins All Rights Reserved
9 * @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later
10 */
11
12 namespace FireBox\Core\Admin\Includes\Cpts;
13
14 if (!defined('ABSPATH'))
15 {
16 exit; // Exit if accessed directly.
17 }
18
19 use FPFramework\Libs\Cpt;
20 use FireBox\Core\Helpers\BoxHelper;
21
22 class Firebox extends Cpt
23 {
24 public $singular;
25 public $plural;
26
27 public function __construct()
28 {
29 $this->singular = firebox()->_('FB_PLUGIN_NAME');
30 $this->plural = firebox()->_('FB_PLUGIN_PLULAR_NAME');
31
32 parent::__construct($this->getPayload());
33
34 $this->init();
35 }
36
37 public function init()
38 {
39 // load dependencies
40 $this->initDependencies();
41
42 // adds extra actions per item on hover
43 add_filter('post_row_actions', [$this, 'filter_row_actions'], 10, 2);
44
45 add_action('pre_get_posts', [$this, 'custom_orderby']);
46
47 // handle box duplication
48 add_action('admin_action_fb_duplicate_post_as_draft', [$this, 'handle_box_duplication']);
49
50 // handle box cookie clear
51 add_action('admin_action_fb_clear_cookie', [$this, 'handle_box_cookie_clear']);
52
53 // bulk actions
54 add_action('bulk_actions-edit-firebox', [$this, 'filter_bulk_actions']);
55 add_action('handle_bulk_actions-edit-firebox', [$this, 'handle_bulk_actions'], 10, 3);
56
57 // add custom buttons at top of the CPT
58 add_filter('views_edit-firebox', [$this, 'filter_top_buttons']);
59
60 // delete hook for FireBox
61 add_action('delete_post', [$this, 'before_delete_firebox_callback']);
62 }
63
64 /**
65 * Load dependencies
66 *
67 * @return void
68 */
69 private function initDependencies()
70 {
71 new FireBox\AddFireBoxButtonAboveEditor();
72 new FireBox\MetaBoxLanguageIntegration();
73 new FireBox\TinyMCEButton();
74 new FireBox\SmartTagsCPTButton();
75 }
76
77 /**
78 * Before deleting the FireBox, delete its box logs and box logs details data
79 *
80 * @param int $ID
81 *
82 * @return void
83 */
84 public function before_delete_firebox_callback($ID)
85 {
86 global $post;
87
88 if (!$post)
89 {
90 return;
91 }
92
93 if ($post->post_type != 'firebox')
94 {
95 return;
96 }
97
98 $logs_table = firebox()->tables->boxlog->getFullTableName();
99 $logs_details_table = firebox()->tables->boxlogdetails->getFullTableName();
100
101 // delete logs details
102 firebox()->tables->boxlogdetails->executeRaw("DELETE FROM `$logs_details_table` WHERE log_id IN (SELECT id FROM `$logs_table` WHERE box = %d)", [$ID]);
103
104 // delete logs
105 firebox()->tables->boxlog->delete([
106 'box' => $ID
107 ]);
108 }
109
110 /**
111 * Custom Order BY
112 *
113 * @param array $query
114 *
115 * @return void
116 */
117 public function custom_orderby($query)
118 {
119 if (!is_admin())
120 {
121 return;
122 }
123
124 if (!is_string($query))
125 {
126 return;
127 }
128
129 $orderby = strtolower($query->get('orderby'));
130
131 switch ($orderby) {
132 case 'status':
133 $meta_query = [
134 'relation' => 'OR',
135 [
136 'key' => 'post_status',
137 'compare' => 'NOT EXISTS',
138 ],
139 [
140 'key' => 'post_status',
141 ]
142 ];
143
144 $order = $query->get('order');
145
146 $query->set('meta_query', $meta_query);
147 $query->set('orderby', ($order == 'asc' ? 'publish' : 'draft'));
148 break;
149 }
150 }
151
152 /**
153 * Adds Import to the top of the CPT page
154 *
155 * @param array $views
156 *
157 * @return array
158 */
159 public function filter_top_buttons($views)
160 {
161 $views['import'] = '<a href="admin.php?page=firebox-import">' . fpframework()->_('FPF_IMPORT') . '</a>';
162 return $views;
163 }
164
165 /**
166 * Handles custom bulk actions
167 *
168 * @param string $redirect_to
169 * @param string $action
170 * @param array $post_ids
171 *
172 * @return mixed
173 */
174 public function handle_bulk_actions($redirect_to, $action, $post_ids)
175 {
176 $redirect_url = admin_url() . 'edit.php?post_type=firebox';
177
178 if (empty($post_ids))
179 {
180 return $redirect_url;
181 }
182
183 // Check whether action that user wants to perform is Export
184 if ($action == 'export')
185 {
186 BoxHelper::exportBoxes($post_ids);
187 }
188 else if ($action == 'reset_stats')
189 {
190 BoxHelper::resetBoxStats($post_ids);
191
192 \FPFramework\Libs\AdminNotice::displaySuccess(firebox()->_('FB_POPUP_RESET_STATS'));
193 }
194
195 return $redirect_to;
196 }
197
198 /**
199 * Adds filter to the bulk actions
200 *
201 * @param array $actions
202 *
203 * @return array
204 */
205 public function filter_bulk_actions($actions)
206 {
207 $actions['export'] = fpframework()->_('FPF_EXPORT');
208 $actions['reset_stats'] = firebox()->_('FB_RESET_IMPRESSIONS');
209 return $actions;
210 }
211
212 /**
213 * Adds extra action in each row item
214 *
215 * @param array $actions
216 * @param object $post
217 *
218 * @return array
219 */
220 public function filter_row_actions($actions, $post)
221 {
222 if ($post->post_type != 'firebox')
223 {
224 return $actions;
225 }
226
227 // Remove "View" from published popups as we are not utilizing the front-end view of the custom post type.
228 if ($post->post_status === 'publish')
229 {
230 unset($actions['view']);
231 }
232
233 // Add 'Duplicate' action
234 $actions['duplicate'] = '<a href="admin.php?action=fb_duplicate_post_as_draft&post=' . $post->ID . '" title="' . firebox()->_('FB_DUPLICATE_BOX') . '" rel="permalink">' . fpframework()->_('FPF_DUPLICATE') . '</a>';
235
236 /**
237 * Check if cookie has been set
238 */
239 if ((new \FireBox\Core\FB\Cookie(firebox()->box->get($post->ID)))->exist())
240 {
241 $actions['clear_cookie'] = '<a class="fb_red_text_color" href="admin.php?action=fb_clear_cookie&post=' . $post->ID . '" title="' . firebox()->_('FB_CLEAR_COOKIE') . '" rel="permalink">' . firebox()->_('FB_HIDDEN_BY_COOKIE') . '</a>';
242 }
243 return $actions;
244 }
245
246 /**
247 * Handles Box duplication
248 *
249 * @return void
250 */
251 public function handle_box_duplication()
252 {
253 $post_id = isset($_GET['post']) ? intval($_GET['post']) : '';
254
255 $redirect_url = admin_url() . 'edit.php?post_type=firebox';
256
257 if (empty($post_id))
258 {
259 wp_redirect($redirect_url);
260 exit();
261 }
262
263 BoxHelper::duplicateBox($post_id);
264
265 // redirect back to box list
266 wp_redirect($redirect_url);
267 }
268
269 /**
270 * Clears cookie from box
271 *
272 * @return void
273 */
274 public function handle_box_cookie_clear()
275 {
276 $post_id = isset($_GET['post']) ? intval($_GET['post']) : '';
277
278 $redirect_url = admin_url() . 'edit.php?post_type=firebox';
279
280 if (empty($post_id))
281 {
282 wp_redirect($redirect_url);
283 exit();
284 }
285
286 $cookie = new \FireBox\Core\FB\Cookie(firebox()->box->get($post_id));
287 $cookie->remove();
288
289 // redirect back to box list
290 wp_redirect($redirect_url);
291 }
292
293 /**
294 * Returns CPT payload
295 *
296 * @return array
297 */
298 protected function getPayload()
299 {
300 return [
301 'label_name' => firebox()->_('FB_PLUGIN_NAME'),
302 'singular' => $this->singular,
303 'label' => firebox()->_('FB_PLUGIN_NAME'),
304 'plural' => $this->plural,
305 'name' => 'firebox',
306 'slug' => 'firebox',
307 'has_archive' => false,
308 'show_in_menu' => false,
309 'is_public' => true,
310 'supports' => ['title', 'editor'],
311 'extra_columns' => [
312 'status' => [
313 'index' => 0,
314 'label' => fpframework()->_('FPF_STATUS')
315 ],
316 'mode' => fpframework()->_('FPF_MODE'),
317 'trigger_point' => firebox()->_('FB_METABOX_TRIGGER_METHOD'),
318 'impressions' => fpframework()->_('FPF_IMPRESSIONS'),
319 'last_date_viewed' => firebox()->_('FB_LAST_DATE_VIEWED'),
320 'id' => fpframework()->_('FPF_ID')
321 ],
322 'capability_type' => ['firebox', 'fireboxes']
323 ];
324 }
325
326 /**
327 * Adds the values for the exta columns
328 *
329 * @param string $column
330 * @param int $post_id
331 *
332 * @return void
333 */
334 public function addExtraColumnsValues($column, $post_id)
335 {
336 switch ($column)
337 {
338 case 'status':
339 echo \FPFramework\Helpers\HTML::renderFPToggle([
340 'input_class' => ['fpf-toggle-post-status'],
341 'name' => 'fb_toggle_post_' . $post_id,
342 'extra_atts' => [
343 'data-post-id' => $post_id
344 ],
345 'value' => get_post_status($post_id) == 'publish' ? 1 : 0
346 ]);
347 break;
348 case 'mode':
349 $meta = get_post_meta( $post_id, 'fpframework_meta_settings', true );
350 echo esc_html($this->getMode($meta));
351 break;
352 case 'trigger_point':
353 $meta = get_post_meta( $post_id, 'fpframework_meta_settings', true );
354 echo esc_html($this->getTriggerPoint($meta)) . esc_html($this->getPosition($meta));
355 break;
356 case 'impressions':
357
358 $impressions = (int) firebox()->tables->boxlog->getResults([
359 'where' => [
360 'box' => ' = ' . esc_sql($post_id)
361 ]
362 ], false, true);
363
364
365 $link_start = $link_end = '';
366
367
368
369
370 ?>
371 <span class="fpf-badge info" title="<?php echo esc_attr(sprintf(firebox()->_('FB_POPUP_TOTAL_IMPRESSIONS'), $impressions)); ?>">
372 <?php echo wp_kses($link_start, ['a' => [ 'href' => true]]) . esc_html($impressions) . wp_kses($link_end, ['a' => true]); ?>
373 </span>
374 <?php
375 break;
376 case 'last_date_viewed':
377 $last_date_viewed = firebox()->tables->boxlog->getResults([
378 'select' => [
379 'date as last_date_viewed'
380 ],
381 'where' => [
382 'box' => ' = ' . esc_sql($post_id),
383 ],
384 'orderby' => ' date desc',
385 'limit' => 1
386 ]);
387
388 $last_date_viewed = isset($last_date_viewed[0]->last_date_viewed) ? $last_date_viewed[0]->last_date_viewed : '-';
389
390 echo '<span title="' . esc_attr(firebox()->_('FB_LAST_DAY_OPENED_DESC')) . '">' . esc_html($last_date_viewed) . '</span>';
391 break;
392 case 'id':
393 echo esc_html($post_id);
394 break;
395 }
396 }
397
398 /**
399 * Set sortable columns
400 *
401 * @param array $columns
402 *
403 * @return array
404 */
405 function setSortableColumns($columns)
406 {
407 $columns['status'] = fpframework()->_('FPF_STATUS');
408 $columns['mode'] = fpframework()->_('FPF_MODE');
409 $columns['id'] = fpframework()->_('FPF_ID');
410 return $columns;
411 }
412
413 /**
414 * Gets the mode
415 *
416 * @param array $meta
417 *
418 * @return string
419 */
420 private function getMode($meta)
421 {
422 $mode = isset($meta['mode']) ? $meta['mode'] : '';
423
424 if (empty($mode))
425 {
426 return '';
427 }
428
429 return firebox()->_('FB_METABOX_MODE_' . strtoupper($mode));
430 }
431
432 /**
433 * Gets the Trigger Point
434 *
435 * @param array $meta
436 *
437 * @return string
438 */
439 private function getTriggerPoint($meta)
440 {
441 $point = isset($meta['triggermethod']) ? $meta['triggermethod'] : '';
442
443 if (empty($point))
444 {
445 return '';
446 }
447
448 switch ($point)
449 {
450 case 'pageheight':
451 $point = 'PH';
452 break;
453 case 'element':
454 $point = 'EL';
455 break;
456 case 'pageready':
457 $point = 'PR';
458 break;
459 case 'pageload':
460 $point = 'PL';
461 break;
462 case 'userleave':
463 $point = 'UL';
464 break;
465 case 'onclick':
466 $point = 'OC';
467 break;
468 case 'elementHover':
469 $point = 'EH';
470 break;
471 case 'ondemand':
472 $point = 'OD';
473 break;
474 }
475
476 return firebox()->_('FB_METABOX_TRIGGER_METHOD_' . $point) . ' / ';
477 }
478
479 /**
480 * Gets the position of the box
481 *
482 * @param array $meta
483 *
484 * @return array
485 */
486 private function getPosition($meta)
487 {
488 $position = isset($meta['position']) ? $meta['position'] : '';
489
490 if (empty($position))
491 {
492 return '';
493 }
494
495 switch ($position)
496 {
497 case 'top-left':
498 $position = 'TL';
499 break;
500 case 'top-center':
501 $position = 'TC';
502 break;
503 case 'top-right':
504 $position = 'TR';
505 break;
506 case 'bottom-left':
507 $position = 'BL';
508 break;
509 case 'bottom-center':
510 $position = 'BC';
511 break;
512 case 'bottom-right':
513 $position = 'BR';
514 break;
515 case 'middle-left':
516 $position = 'ML';
517 break;
518 case 'middle-right':
519 $position = 'MR';
520 break;
521 case 'center':
522 $position = 'C';
523 break;
524 }
525
526 return firebox()->_('FB_METABOX_POSITION_' . $position);
527 }
528 }