| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FireBox |
| 4 |
* @version 2.1.14 Free |
| 5 |
* |
| 6 |
* @author FirePlugins <info@fireplugins.com> |
| 7 |
* @link https://www.fireplugins.com |
| 8 |
* @copyright Copyright © 2024 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_FIREBOX_CAMPAIGN'); |
| 30 |
$this->plural = firebox()->_('FB_FIREBOX_CAMPAIGNS'); |
| 31 |
|
| 32 |
parent::__construct($this->getPayload()); |
| 33 |
|
| 34 |
$this->init(); |
| 35 |
} |
| 36 |
|
| 37 |
public function init() |
| 38 |
{ |
| 39 |
add_action('admin_init', [$this, 'custom_post_edit_redirect']); |
| 40 |
|
| 41 |
// load dependencies |
| 42 |
$this->initDependencies(); |
| 43 |
|
| 44 |
// set default post title |
| 45 |
add_filter('default_title', [$this, 'set_default_campaign_title'], 10, 2); |
| 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 |
// delete hook for FireBox |
| 54 |
add_action('delete_post', [$this, 'before_delete_firebox_callback']); |
| 55 |
|
| 56 |
add_action('load-firebox_page_firebox-campaigns', [$this, 'handle_bulk_actions']); |
| 57 |
|
| 58 |
add_filter('fpframework/metabox/after_filter', [$this, 'after_save'], 10, 3); |
| 59 |
|
| 60 |
add_filter('the_content', [$this, 'prepareContent'], 0); |
| 61 |
|
| 62 |
add_filter('wp_sitemaps_post_types', [$this, 'remove_post_type_from_wp_sitemap']); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Remove thw wptexturize filter as it replaces double dashes with em dash. |
| 67 |
* |
| 68 |
* @param string $content |
| 69 |
* |
| 70 |
* @return string |
| 71 |
*/ |
| 72 |
public function prepareContent($content) |
| 73 |
{ |
| 74 |
global $post; |
| 75 |
|
| 76 |
if (!$post instanceof \WP_Post) |
| 77 |
{ |
| 78 |
return $content; |
| 79 |
} |
| 80 |
|
| 81 |
// Check if the current post type is 'firebox' |
| 82 |
if ($post->post_type === 'firebox') |
| 83 |
{ |
| 84 |
remove_filter('the_content', 'wptexturize'); |
| 85 |
} |
| 86 |
|
| 87 |
return $content; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Remove the FireBox custom post type from the sitemap. |
| 92 |
* |
| 93 |
* This is not needed as published FireBox campaigns are not public. |
| 94 |
* |
| 95 |
* @param array $cpts |
| 96 |
* |
| 97 |
* @return array |
| 98 |
*/ |
| 99 |
public function remove_post_type_from_wp_sitemap($cpts) |
| 100 |
{ |
| 101 |
if (isset($cpts['firebox'])) |
| 102 |
{ |
| 103 |
unset($cpts['firebox']); |
| 104 |
} |
| 105 |
|
| 106 |
return $cpts; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* When going to edit.php?post_type=firebox, then |
| 111 |
* redirect to: admin.php?page=firebox-campaigns |
| 112 |
* |
| 113 |
* @return void |
| 114 |
*/ |
| 115 |
function custom_post_edit_redirect() |
| 116 |
{ |
| 117 |
global $pagenow; |
| 118 |
if ($pagenow !== 'post-new.php' && isset($_GET['post_type']) && $_GET['post_type'] === 'firebox') //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 119 |
{ |
| 120 |
wp_redirect(admin_url('admin.php?page=firebox-campaigns')); |
| 121 |
exit; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
public function handle_bulk_actions() |
| 126 |
{ |
| 127 |
if (empty($_GET)) |
| 128 |
{ |
| 129 |
return; |
| 130 |
} |
| 131 |
|
| 132 |
$action = isset($_GET['action']) ? sanitize_key($_GET['action']) : false; |
| 133 |
if (!$action) |
| 134 |
{ |
| 135 |
return; |
| 136 |
} |
| 137 |
|
| 138 |
if ($action !== 'fb_export') |
| 139 |
{ |
| 140 |
return; |
| 141 |
} |
| 142 |
|
| 143 |
// Ensure we have IDs |
| 144 |
$ids = isset($_GET['id']) ? array_map('intval', $_GET['id']) : []; |
| 145 |
if (!$ids) |
| 146 |
{ |
| 147 |
return; |
| 148 |
} |
| 149 |
|
| 150 |
// Get nonce |
| 151 |
$nonce = isset($_GET['_wpnonce']) ? sanitize_text_field($_GET['_wpnonce']) : ''; |
| 152 |
if (!$nonce) |
| 153 |
{ |
| 154 |
return; |
| 155 |
} |
| 156 |
|
| 157 |
// Verify nonce |
| 158 |
$nonce_action = 'bulk-fireboxes'; |
| 159 |
if (!wp_verify_nonce($nonce, $nonce_action)) |
| 160 |
{ |
| 161 |
return; |
| 162 |
} |
| 163 |
|
| 164 |
BoxHelper::exportBoxes($ids); |
| 165 |
\FPFramework\Libs\AdminNotice::displaySuccess(sprintf(firebox()->_('FB_X_CAMPAIGNS_HAVE_BEEN_EXPORTED'), count($ids))); |
| 166 |
} |
| 167 |
|
| 168 |
public function set_default_campaign_title($post_title, $post) |
| 169 |
{ |
| 170 |
if (!isset($post->post_type)) |
| 171 |
{ |
| 172 |
return $post_title; |
| 173 |
} |
| 174 |
|
| 175 |
if ($post->post_type !== 'firebox') |
| 176 |
{ |
| 177 |
return $post_title; |
| 178 |
} |
| 179 |
|
| 180 |
if (empty(trim($post_title))) |
| 181 |
{ |
| 182 |
$post_title = firebox()->_('FB_UNTITLED_CAMPAIGN'); |
| 183 |
} |
| 184 |
|
| 185 |
return $post_title; |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Prepare conditions after save. |
| 190 |
* |
| 191 |
* @param array $fields |
| 192 |
* @param int $post_id |
| 193 |
* @param string $cpt |
| 194 |
* |
| 195 |
* @return array |
| 196 |
*/ |
| 197 |
public function after_save($fields, $post_id, $cpt) |
| 198 |
{ |
| 199 |
if ($cpt !== 'firebox') |
| 200 |
{ |
| 201 |
return $fields; |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
\FPFramework\Libs\Cache::invalidate(); |
| 207 |
|
| 208 |
return $fields; |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Load dependencies |
| 213 |
* |
| 214 |
* @return void |
| 215 |
*/ |
| 216 |
private function initDependencies() |
| 217 |
{ |
| 218 |
new FireBox\AddFireBoxButtonAboveEditor(); |
| 219 |
new FireBox\TinyMCEButton(); |
| 220 |
new FireBox\SmartTagsCPTButton(); |
| 221 |
|
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Before deleting the FireBox, delete its box logs and box logs details data |
| 226 |
* |
| 227 |
* @param int $ID |
| 228 |
* |
| 229 |
* @return void |
| 230 |
*/ |
| 231 |
public function before_delete_firebox_callback($ID) |
| 232 |
{ |
| 233 |
$post = get_post($ID); |
| 234 |
|
| 235 |
if (!$post) |
| 236 |
{ |
| 237 |
return; |
| 238 |
} |
| 239 |
|
| 240 |
if ($post->post_type !== 'firebox') |
| 241 |
{ |
| 242 |
return; |
| 243 |
} |
| 244 |
|
| 245 |
$logs_table = firebox()->tables->boxlog->getFullTableName(); |
| 246 |
$logs_details_table = firebox()->tables->boxlogdetails->getFullTableName(); |
| 247 |
|
| 248 |
// Get all popup forms |
| 249 |
$forms = \FPFramework\Helpers\Plugins\FireBox\Form::getCampaignForms($ID); |
| 250 |
|
| 251 |
// delete logs details |
| 252 |
firebox()->tables->boxlogdetails->executeRaw("DELETE FROM `$logs_details_table` WHERE log_id IN (SELECT id FROM `$logs_table` WHERE box = %d)", [$ID]); |
| 253 |
|
| 254 |
// delete logs |
| 255 |
firebox()->tables->boxlog->delete([ |
| 256 |
'box' => $ID |
| 257 |
]); |
| 258 |
|
| 259 |
if ($forms) |
| 260 |
{ |
| 261 |
foreach ($forms as $form_id => $form_label) |
| 262 |
{ |
| 263 |
global $wpdb; |
| 264 |
|
| 265 |
// Delete submission meta records first |
| 266 |
$wpdb->query( |
| 267 |
$wpdb->prepare( |
| 268 |
"DELETE sm |
| 269 |
FROM {$wpdb->prefix}firebox_submission_meta AS sm |
| 270 |
INNER JOIN {$wpdb->prefix}firebox_submissions AS s ON sm.submission_id = s.id |
| 271 |
WHERE s.form_id = %s" |
| 272 |
, |
| 273 |
$form_id |
| 274 |
) |
| 275 |
); |
| 276 |
|
| 277 |
// Delete submissions |
| 278 |
$wpdb->delete("{$wpdb->prefix}firebox_submissions", ['form_id' => $form_id]); |
| 279 |
} |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
/** |
| 284 |
* Handles Box duplication |
| 285 |
* |
| 286 |
* @return void |
| 287 |
*/ |
| 288 |
public function handle_box_duplication() |
| 289 |
{ |
| 290 |
check_admin_referer('duplicate-firebox-campaign'); |
| 291 |
|
| 292 |
$post_id = isset($_GET['post']) ? intval($_GET['post']) : ''; |
| 293 |
|
| 294 |
$redirect_url = admin_url() . 'admin.php?page=firebox-campaigns'; |
| 295 |
|
| 296 |
if (empty($post_id)) |
| 297 |
{ |
| 298 |
wp_redirect($redirect_url); |
| 299 |
exit(); |
| 300 |
} |
| 301 |
|
| 302 |
BoxHelper::duplicateBox($post_id); |
| 303 |
|
| 304 |
\FPFramework\Libs\AdminNotice::displaySuccess(firebox()->_('FB_CAMPAIGN_DUPLICATED')); |
| 305 |
|
| 306 |
// redirect back to box list |
| 307 |
wp_redirect($redirect_url); |
| 308 |
} |
| 309 |
|
| 310 |
/** |
| 311 |
* Clears cookie from box |
| 312 |
* |
| 313 |
* @return void |
| 314 |
*/ |
| 315 |
public function handle_box_cookie_clear() |
| 316 |
{ |
| 317 |
check_admin_referer('clearcookie-firebox-campaign'); |
| 318 |
|
| 319 |
$post_id = isset($_GET['post']) ? intval($_GET['post']) : ''; |
| 320 |
|
| 321 |
$redirect_url = admin_url() . 'admin.php?page=firebox-campaigns'; |
| 322 |
|
| 323 |
if (empty($post_id)) |
| 324 |
{ |
| 325 |
wp_redirect($redirect_url); |
| 326 |
exit(); |
| 327 |
} |
| 328 |
|
| 329 |
$cookie = new \FireBox\Core\FB\Cookie(firebox()->box->get($post_id)); |
| 330 |
$cookie->remove(); |
| 331 |
|
| 332 |
// redirect back to box list |
| 333 |
wp_redirect($redirect_url); |
| 334 |
} |
| 335 |
|
| 336 |
/** |
| 337 |
* Returns CPT payload |
| 338 |
* |
| 339 |
* @return array |
| 340 |
*/ |
| 341 |
protected function getPayload() |
| 342 |
{ |
| 343 |
return [ |
| 344 |
'label_name' => firebox()->_('FB_PLUGIN_NAME'), |
| 345 |
'singular' => $this->singular, |
| 346 |
'label' => firebox()->_('FB_PLUGIN_NAME'), |
| 347 |
'plural' => $this->plural, |
| 348 |
'name' => 'firebox', |
| 349 |
'slug' => 'firebox', |
| 350 |
'has_archive' => false, |
| 351 |
'show_in_menu' => false, |
| 352 |
'is_public' => true, |
| 353 |
'exclude_from_search' => true, |
| 354 |
'supports' => ['title', 'editor'], |
| 355 |
'capability_type' => ['firebox', 'fireboxes'] |
| 356 |
]; |
| 357 |
} |
| 358 |
} |