| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FireBox |
| 4 |
* @version 1.0.8 Free |
| 5 |
* |
| 6 |
* @author FirePlugins <info@fireplugins.com> |
| 7 |
* @link https://www.fireplugins.com |
| 8 |
* @copyright Copyright © 2021 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 |
$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>'; |
| 228 |
|
| 229 |
/** |
| 230 |
* Check if cookie has been set |
| 231 |
*/ |
| 232 |
if ((new \FireBox\Core\FB\Cookie($post->ID))->exist()) |
| 233 |
{ |
| 234 |
$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>'; |
| 235 |
} |
| 236 |
return $actions; |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Handles Box duplication |
| 241 |
* |
| 242 |
* @return void |
| 243 |
*/ |
| 244 |
public function handle_box_duplication() |
| 245 |
{ |
| 246 |
$post_id = isset($_GET['post']) ? intval($_GET['post']) : ''; |
| 247 |
|
| 248 |
$redirect_url = admin_url() . 'edit.php?post_type=firebox'; |
| 249 |
|
| 250 |
if (empty($post_id)) |
| 251 |
{ |
| 252 |
wp_redirect($redirect_url); |
| 253 |
exit(); |
| 254 |
} |
| 255 |
|
| 256 |
BoxHelper::duplicateBox($post_id); |
| 257 |
|
| 258 |
// redirect back to box list |
| 259 |
wp_redirect($redirect_url); |
| 260 |
} |
| 261 |
|
| 262 |
/** |
| 263 |
* Clears cookie from box |
| 264 |
* |
| 265 |
* @return void |
| 266 |
*/ |
| 267 |
public function handle_box_cookie_clear() |
| 268 |
{ |
| 269 |
$post_id = isset($_GET['post']) ? intval($_GET['post']) : ''; |
| 270 |
|
| 271 |
$redirect_url = admin_url() . 'edit.php?post_type=firebox'; |
| 272 |
|
| 273 |
if (empty($post_id)) |
| 274 |
{ |
| 275 |
wp_redirect($redirect_url); |
| 276 |
exit(); |
| 277 |
} |
| 278 |
|
| 279 |
$cookie = new \FireBox\Core\FB\Cookie($post_id); |
| 280 |
$cookie->remove(); |
| 281 |
|
| 282 |
// redirect back to box list |
| 283 |
wp_redirect($redirect_url); |
| 284 |
} |
| 285 |
|
| 286 |
/** |
| 287 |
* Returns CPT payload |
| 288 |
* |
| 289 |
* @return array |
| 290 |
*/ |
| 291 |
protected function getPayload() |
| 292 |
{ |
| 293 |
return [ |
| 294 |
'label_name' => firebox()->_('FB_PLUGIN_NAME'), |
| 295 |
'singular' => $this->singular, |
| 296 |
'label' => firebox()->_('FB_PLUGIN_NAME'), |
| 297 |
'plural' => $this->plural, |
| 298 |
'name' => 'firebox', |
| 299 |
'slug' => 'firebox', |
| 300 |
'has_archive' => false, |
| 301 |
'show_in_menu' => false, |
| 302 |
'is_public' => true, |
| 303 |
'supports' => ['title', 'editor'], |
| 304 |
'extra_columns' => [ |
| 305 |
'status' => [ |
| 306 |
'index' => 0, |
| 307 |
'label' => fpframework()->_('FPF_STATUS') |
| 308 |
], |
| 309 |
'mode' => fpframework()->_('FPF_MODE'), |
| 310 |
'trigger_point' => firebox()->_('FB_METABOX_TRIGGER_METHOD'), |
| 311 |
'impressions' => fpframework()->_('FPF_IMPRESSIONS'), |
| 312 |
'last_date_viewed' => firebox()->_('FB_LAST_DATE_VIEWED'), |
| 313 |
'id' => fpframework()->_('FPF_ID') |
| 314 |
], |
| 315 |
'capability_type' => ['firebox', 'fireboxes'] |
| 316 |
]; |
| 317 |
} |
| 318 |
|
| 319 |
/** |
| 320 |
* Adds the values for the exta columns |
| 321 |
* |
| 322 |
* @param string $column |
| 323 |
* @param int $post_id |
| 324 |
* |
| 325 |
* @return void |
| 326 |
*/ |
| 327 |
public function addExtraColumnsValues($column, $post_id) |
| 328 |
{ |
| 329 |
switch ($column) |
| 330 |
{ |
| 331 |
case 'status': |
| 332 |
echo \FPFramework\Helpers\HTML::renderFPToggle([ |
| 333 |
'input_class' => ['fpf-toggle-post-status'], |
| 334 |
'name' => 'fb_toggle_post_' . $post_id, |
| 335 |
'extra_atts' => [ |
| 336 |
'data-post-id' => $post_id |
| 337 |
], |
| 338 |
'value' => get_post_status($post_id) == 'publish' ? 1 : 0 |
| 339 |
]); |
| 340 |
break; |
| 341 |
case 'mode': |
| 342 |
$meta = get_post_meta( $post_id, 'fpframework_meta_settings', true ); |
| 343 |
echo esc_html($this->getMode($meta)); |
| 344 |
break; |
| 345 |
case 'trigger_point': |
| 346 |
$meta = get_post_meta( $post_id, 'fpframework_meta_settings', true ); |
| 347 |
echo esc_html($this->getTriggerPoint($meta)) . esc_html($this->getPosition($meta)); |
| 348 |
break; |
| 349 |
case 'impressions': |
| 350 |
|
| 351 |
$impressions = (int) firebox()->tables->boxlog->getResults([ |
| 352 |
'where' => [ |
| 353 |
'box' => ' = ' . esc_sql($post_id) |
| 354 |
] |
| 355 |
], false, true); |
| 356 |
|
| 357 |
|
| 358 |
$link_start = $link_end = ''; |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
?> |
| 364 |
<span class="fpf-badge info" title="<?php echo esc_attr(sprintf(firebox()->_('FB_POPUP_TOTAL_IMPRESSIONS'), $impressions)); ?>"> |
| 365 |
<?php echo wp_kses($link_start, ['a' => [ 'href' => true]]) . esc_html($impressions) . wp_kses($link_end, ['a' => true]); ?> |
| 366 |
</span> |
| 367 |
<?php |
| 368 |
break; |
| 369 |
case 'last_date_viewed': |
| 370 |
$last_date_viewed = firebox()->tables->boxlog->getResults([ |
| 371 |
'select' => [ |
| 372 |
'date as last_date_viewed' |
| 373 |
], |
| 374 |
'where' => [ |
| 375 |
'box' => ' = ' . esc_sql($post_id), |
| 376 |
], |
| 377 |
'orderby' => ' date desc', |
| 378 |
'limit' => 1 |
| 379 |
]); |
| 380 |
|
| 381 |
$last_date_viewed = isset($last_date_viewed[0]->last_date_viewed) ? $last_date_viewed[0]->last_date_viewed : '-'; |
| 382 |
|
| 383 |
echo '<span title="' . esc_attr(firebox()->_('FB_LAST_DAY_OPENED_DESC')) . '">' . esc_html($last_date_viewed) . '</span>'; |
| 384 |
break; |
| 385 |
case 'id': |
| 386 |
echo esc_html($post_id); |
| 387 |
break; |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
/** |
| 392 |
* Set sortable columns |
| 393 |
* |
| 394 |
* @param array $columns |
| 395 |
* |
| 396 |
* @return array |
| 397 |
*/ |
| 398 |
function setSortableColumns($columns) |
| 399 |
{ |
| 400 |
$columns['status'] = fpframework()->_('FPF_STATUS'); |
| 401 |
$columns['mode'] = fpframework()->_('FPF_MODE'); |
| 402 |
$columns['id'] = fpframework()->_('FPF_ID'); |
| 403 |
return $columns; |
| 404 |
} |
| 405 |
|
| 406 |
/** |
| 407 |
* Gets the mode |
| 408 |
* |
| 409 |
* @param array $meta |
| 410 |
* |
| 411 |
* @return string |
| 412 |
*/ |
| 413 |
private function getMode($meta) |
| 414 |
{ |
| 415 |
$mode = isset($meta['mode']) ? $meta['mode'] : ''; |
| 416 |
|
| 417 |
if (empty($mode)) |
| 418 |
{ |
| 419 |
return ''; |
| 420 |
} |
| 421 |
|
| 422 |
return firebox()->_('FB_METABOX_MODE_' . strtoupper($mode)); |
| 423 |
} |
| 424 |
|
| 425 |
/** |
| 426 |
* Gets the Trigger Point |
| 427 |
* |
| 428 |
* @param array $meta |
| 429 |
* |
| 430 |
* @return string |
| 431 |
*/ |
| 432 |
private function getTriggerPoint($meta) |
| 433 |
{ |
| 434 |
$point = isset($meta['triggermethod']) ? $meta['triggermethod'] : ''; |
| 435 |
|
| 436 |
if (empty($point)) |
| 437 |
{ |
| 438 |
return ''; |
| 439 |
} |
| 440 |
|
| 441 |
switch ($point) |
| 442 |
{ |
| 443 |
case 'pageheight': |
| 444 |
$point = 'PH'; |
| 445 |
break; |
| 446 |
case 'element': |
| 447 |
$point = 'EL'; |
| 448 |
break; |
| 449 |
case 'pageready': |
| 450 |
$point = 'PR'; |
| 451 |
break; |
| 452 |
case 'pageload': |
| 453 |
$point = 'PL'; |
| 454 |
break; |
| 455 |
case 'userleave': |
| 456 |
$point = 'UL'; |
| 457 |
break; |
| 458 |
case 'onclick': |
| 459 |
$point = 'OC'; |
| 460 |
break; |
| 461 |
case 'elementHover': |
| 462 |
$point = 'EH'; |
| 463 |
break; |
| 464 |
case 'ondemand': |
| 465 |
$point = 'OD'; |
| 466 |
break; |
| 467 |
} |
| 468 |
|
| 469 |
return firebox()->_('FB_METABOX_TRIGGER_METHOD_' . $point) . ' / '; |
| 470 |
} |
| 471 |
|
| 472 |
/** |
| 473 |
* Gets the position of the box |
| 474 |
* |
| 475 |
* @param array $meta |
| 476 |
* |
| 477 |
* @return array |
| 478 |
*/ |
| 479 |
private function getPosition($meta) |
| 480 |
{ |
| 481 |
$position = isset($meta['position']) ? $meta['position'] : ''; |
| 482 |
|
| 483 |
if (empty($position)) |
| 484 |
{ |
| 485 |
return ''; |
| 486 |
} |
| 487 |
|
| 488 |
switch ($position) |
| 489 |
{ |
| 490 |
case 'top-left': |
| 491 |
$position = 'TL'; |
| 492 |
break; |
| 493 |
case 'top-center': |
| 494 |
$position = 'TC'; |
| 495 |
break; |
| 496 |
case 'top-right': |
| 497 |
$position = 'TR'; |
| 498 |
break; |
| 499 |
case 'bottom-left': |
| 500 |
$position = 'BL'; |
| 501 |
break; |
| 502 |
case 'bottom-center': |
| 503 |
$position = 'BC'; |
| 504 |
break; |
| 505 |
case 'bottom-right': |
| 506 |
$position = 'BR'; |
| 507 |
break; |
| 508 |
case 'middle-left': |
| 509 |
$position = 'ML'; |
| 510 |
break; |
| 511 |
case 'middle-right': |
| 512 |
$position = 'MR'; |
| 513 |
break; |
| 514 |
case 'center': |
| 515 |
$position = 'C'; |
| 516 |
break; |
| 517 |
} |
| 518 |
|
| 519 |
return firebox()->_('FB_METABOX_POSITION_' . $position); |
| 520 |
} |
| 521 |
} |