PluginProbe
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment / 2.0.10
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment v2.0.10
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 1.1.1 All 122 releases
firebox / Inc / Core / Admin / Includes / Cpts / Firebox.php

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

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