| 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\Helpers; |
| 13 |
|
| 14 |
if (!defined('ABSPATH')) |
| 15 |
{ |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
use FPFramework\Libs\Registry; |
| 20 |
|
| 21 |
class BoxHelper |
| 22 |
{ |
| 23 |
/** |
| 24 |
* Get box meta |
| 25 |
* |
| 26 |
* @param int $id |
| 27 |
* |
| 28 |
* @return array |
| 29 |
*/ |
| 30 |
public static function getMeta($id) |
| 31 |
{ |
| 32 |
return get_post_meta($id, 'fpframework_meta_settings', true); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Gets all Boxes. |
| 37 |
* |
| 38 |
* @param array $status |
| 39 |
* @param int $limit |
| 40 |
* |
| 41 |
* @return array |
| 42 |
*/ |
| 43 |
public static function getAllBoxes($status = ['publish'], $limit = -1) |
| 44 |
{ |
| 45 |
// cache key |
| 46 |
$hash = md5('firebox_getAllBoxes_' . implode(',', $status)); |
| 47 |
|
| 48 |
// check cache |
| 49 |
if ($data = wp_cache_get($hash)) |
| 50 |
{ |
| 51 |
return $data; |
| 52 |
} |
| 53 |
|
| 54 |
$args = [ |
| 55 |
'post_status' => $status, |
| 56 |
'post_type' => 'firebox', |
| 57 |
'posts_per_page' => $limit |
| 58 |
]; |
| 59 |
|
| 60 |
// Get the query. |
| 61 |
$query = new \WP_Query($args); |
| 62 |
|
| 63 |
wp_reset_postdata(); |
| 64 |
|
| 65 |
// set cache |
| 66 |
wp_cache_set($hash, $query); |
| 67 |
|
| 68 |
return $query; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Retrieves all boxes in a key => value array of ID => title |
| 73 |
* |
| 74 |
* @return array |
| 75 |
*/ |
| 76 |
public static function getAllBoxesParsedByKeyValue() |
| 77 |
{ |
| 78 |
if (!$boxes = self::getAllBoxes()) |
| 79 |
{ |
| 80 |
return []; |
| 81 |
} |
| 82 |
|
| 83 |
return self::produceKeyValueBoxes($boxes->posts); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Produce a key,value pair of boxes containg their ID,title |
| 88 |
* |
| 89 |
* @return array |
| 90 |
*/ |
| 91 |
public static function produceKeyValueBoxes($boxes) |
| 92 |
{ |
| 93 |
if (!$boxes) |
| 94 |
{ |
| 95 |
return []; |
| 96 |
} |
| 97 |
|
| 98 |
$data = []; |
| 99 |
|
| 100 |
foreach ($boxes as $key => $box) |
| 101 |
{ |
| 102 |
$data[$box->ID] = $box->post_title; |
| 103 |
} |
| 104 |
|
| 105 |
return $data; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Gets all published Boxes except the given id. |
| 110 |
* The array structure is [ID, title] to properly appear in a Dropdown field. |
| 111 |
* |
| 112 |
* @param integer $id |
| 113 |
* |
| 114 |
* @return array |
| 115 |
*/ |
| 116 |
public static function getAllMirrorBoxesExceptID($id) |
| 117 |
{ |
| 118 |
if (!$id) |
| 119 |
{ |
| 120 |
return []; |
| 121 |
} |
| 122 |
|
| 123 |
$boxes = firebox()->tables->box->getResults([ |
| 124 |
'where' => [ |
| 125 |
'ID' => ' NOT IN (' . esc_sql($id) . ')', |
| 126 |
'post_status' => " = 'publish'", |
| 127 |
'post_type' => " = 'firebox'" |
| 128 |
] |
| 129 |
]); |
| 130 |
|
| 131 |
$boxes_parsed = []; |
| 132 |
|
| 133 |
foreach ($boxes as $key => $p) |
| 134 |
{ |
| 135 |
$boxes_parsed[$p->ID] = $p->post_title . ' (' . $p->ID . ')'; |
| 136 |
} |
| 137 |
|
| 138 |
return $boxes_parsed; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Get box data |
| 143 |
* |
| 144 |
* @param int $box |
| 145 |
* |
| 146 |
* @return array |
| 147 |
*/ |
| 148 |
public static function getBoxData($box) |
| 149 |
{ |
| 150 |
if (!$box) |
| 151 |
{ |
| 152 |
return false; |
| 153 |
} |
| 154 |
|
| 155 |
$box = (int) $box; |
| 156 |
|
| 157 |
$box = firebox()->tables->box->getResults([ |
| 158 |
'where' => [ |
| 159 |
'ID' => " = '" . esc_sql($box) . "'" |
| 160 |
] |
| 161 |
]); |
| 162 |
|
| 163 |
return isset($box[0]) ? $box[0] : []; |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Checks whether the box exist |
| 168 |
* |
| 169 |
* @param int $box |
| 170 |
* |
| 171 |
* @return boolean |
| 172 |
*/ |
| 173 |
public static function boxExist($box) |
| 174 |
{ |
| 175 |
if (!$box) |
| 176 |
{ |
| 177 |
return false; |
| 178 |
} |
| 179 |
|
| 180 |
$box = (int) $box; |
| 181 |
|
| 182 |
$box = firebox()->tables->box->getResults([ |
| 183 |
'where' => [ |
| 184 |
'ID' => " = '" . esc_sql($box) . "'" |
| 185 |
] |
| 186 |
]); |
| 187 |
|
| 188 |
if (!$box) |
| 189 |
{ |
| 190 |
return false; |
| 191 |
} |
| 192 |
|
| 193 |
return true; |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Gets boxes in a [id, title] pair from a list of Box IDs |
| 198 |
* |
| 199 |
* @param array $items |
| 200 |
* |
| 201 |
* @return array |
| 202 |
*/ |
| 203 |
public static function getSelectedSearchItems($items) |
| 204 |
{ |
| 205 |
$boxes = firebox()->tables->box->getResults([ |
| 206 |
'where' => [ |
| 207 |
'ID' => ' IN(' . implode(',', array_map('intval', $items)) . ')', |
| 208 |
'post_status' => " = 'publish'", |
| 209 |
'post_type' => " = 'firebox'" |
| 210 |
] |
| 211 |
]); |
| 212 |
|
| 213 |
$boxes_parsed = []; |
| 214 |
|
| 215 |
foreach ($boxes as $key => $p) |
| 216 |
{ |
| 217 |
$boxes_parsed[] = [ |
| 218 |
'id' => $p->ID, |
| 219 |
'title' => $p->post_title |
| 220 |
]; |
| 221 |
} |
| 222 |
|
| 223 |
return $boxes_parsed; |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Gets Settings Data |
| 228 |
* |
| 229 |
* @return array |
| 230 |
*/ |
| 231 |
public static function getParams() |
| 232 |
{ |
| 233 |
// cache key |
| 234 |
$cache_key = md5('fboxSettings'); |
| 235 |
|
| 236 |
// check cache |
| 237 |
if ($params = wp_cache_get($cache_key)) |
| 238 |
{ |
| 239 |
return $params; |
| 240 |
} |
| 241 |
|
| 242 |
// get params |
| 243 |
$params = get_option('firebox_settings'); |
| 244 |
|
| 245 |
// set cache |
| 246 |
wp_cache_set($cache_key, $params); |
| 247 |
|
| 248 |
return $params; |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* Returns the box template from the URL using the "template" query string. |
| 253 |
* |
| 254 |
* @return array |
| 255 |
*/ |
| 256 |
public static function getBoxFromTemplateURL() |
| 257 |
{ |
| 258 |
if (!isset($_GET['fpf_use_template'])) //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 259 |
{ |
| 260 |
return; |
| 261 |
} |
| 262 |
|
| 263 |
// $template must be an integer |
| 264 |
$template = isset($_GET['template']) ? intval($_GET['template']) : ''; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 265 |
if (empty($template) || $template === 'blank') |
| 266 |
{ |
| 267 |
return; |
| 268 |
} |
| 269 |
|
| 270 |
$local_template_path = \FPFramework\Helpers\WPHelper::getPluginUploadsDirectory('firebox', 'templates') . '/template.json'; |
| 271 |
if (!file_exists($local_template_path)) |
| 272 |
{ |
| 273 |
return; |
| 274 |
} |
| 275 |
|
| 276 |
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
| 277 |
$local_template = file_get_contents($local_template_path); |
| 278 |
if (!$local_template = json_decode($local_template, true)) |
| 279 |
{ |
| 280 |
return; |
| 281 |
} |
| 282 |
|
| 283 |
if (!isset($local_template['id'])) |
| 284 |
{ |
| 285 |
return; |
| 286 |
} |
| 287 |
|
| 288 |
if (!isset($local_template['template'])) |
| 289 |
{ |
| 290 |
return; |
| 291 |
} |
| 292 |
|
| 293 |
if ((int) $local_template['id'] !== (int) $template) |
| 294 |
{ |
| 295 |
return; |
| 296 |
} |
| 297 |
|
| 298 |
// Find images and save them locally. |
| 299 |
$local_template['template']['post_content'] = \FPFramework\Helpers\WPHelper::downloadAndReplaceImages($local_template['template']['post_content']); |
| 300 |
|
| 301 |
return $local_template; |
| 302 |
} |
| 303 |
|
| 304 |
/** |
| 305 |
* Duplicates a box |
| 306 |
* |
| 307 |
* @param integer $box_id |
| 308 |
* |
| 309 |
* @return bool |
| 310 |
*/ |
| 311 |
public static function duplicateBox($box_id) |
| 312 |
{ |
| 313 |
// get box |
| 314 |
$box = firebox()->tables->box->getResults([ |
| 315 |
'where' => [ |
| 316 |
'ID' => " = '" . esc_sql($box_id) . "'", |
| 317 |
'post_status' => " = '" . esc_sql(get_post_status($box_id)) . "'", |
| 318 |
'post_type' => " = 'firebox'" |
| 319 |
], |
| 320 |
'limit' => 1 |
| 321 |
]); |
| 322 |
|
| 323 |
if (empty($box)) |
| 324 |
{ |
| 325 |
return false; |
| 326 |
} |
| 327 |
|
| 328 |
// reset box ID and make it a draft |
| 329 |
$box = $box[0]; |
| 330 |
$box->ID = ''; |
| 331 |
$box->post_title = 'Copy of ' . $box->post_title; |
| 332 |
$box->post_status = 'draft'; |
| 333 |
|
| 334 |
$factory = new \FPFramework\Base\Factory(); |
| 335 |
|
| 336 |
$tz = wp_timezone(); |
| 337 |
$date_without_tz = $factory->getDate(); |
| 338 |
$date_with_tz = $factory->getDate()->setTimezone($tz); |
| 339 |
|
| 340 |
$box->post_date = $date_with_tz->format('Y-m-d H:i:s'); |
| 341 |
$box->post_date_gmt = $date_without_tz->format('Y-m-d H:i:s'); |
| 342 |
|
| 343 |
Form\Form::ensureUniqueFormIDs($box->post_content); |
| 344 |
|
| 345 |
// get meta options |
| 346 |
$meta = self::getMeta($box_id); |
| 347 |
|
| 348 |
// insert new box |
| 349 |
$new_box_id = firebox()->tables->box->insert($box); |
| 350 |
|
| 351 |
// add meta options for new box |
| 352 |
update_post_meta($new_box_id, 'fpframework_meta_settings', wp_slash($meta)); |
| 353 |
|
| 354 |
return true; |
| 355 |
} |
| 356 |
|
| 357 |
/** |
| 358 |
* Reset Box Stats |
| 359 |
* |
| 360 |
* @param array $box_ids |
| 361 |
* |
| 362 |
* @return void |
| 363 |
*/ |
| 364 |
public static function resetBoxStats($box_ids) |
| 365 |
{ |
| 366 |
$logs_table = firebox()->tables->boxlog->getFullTableName(); |
| 367 |
$logs_details_table = firebox()->tables->boxlogdetails->getFullTableName(); |
| 368 |
|
| 369 |
// delete box logs details |
| 370 |
firebox()->tables->boxlogdetails->executeRaw("DELETE FROM `$logs_details_table` WHERE log_id IN (SELECT id FROM `$logs_table` WHERE box IN (" . implode(",", $box_ids) . "))"); |
| 371 |
|
| 372 |
// delete box logs |
| 373 |
firebox()->tables->boxlog->deleteRaw('WHERE box IN (' . implode(',', $box_ids) . ')'); |
| 374 |
} |
| 375 |
|
| 376 |
/** |
| 377 |
* Exports boxes |
| 378 |
* |
| 379 |
* @param array $box_ids |
| 380 |
* |
| 381 |
* @return string |
| 382 |
*/ |
| 383 |
public static function exportBoxes($box_ids) |
| 384 |
{ |
| 385 |
// get boxes |
| 386 |
$boxes = firebox()->tables->box->getResults([ |
| 387 |
'where' => [ |
| 388 |
'ID' => ' IN (' . implode(',', esc_sql($box_ids)) . ')', |
| 389 |
'post_type' => " = 'firebox'" |
| 390 |
] |
| 391 |
]); |
| 392 |
|
| 393 |
$boxes = (array) $boxes; |
| 394 |
|
| 395 |
if (!count($boxes)) |
| 396 |
{ |
| 397 |
return; |
| 398 |
} |
| 399 |
|
| 400 |
$exported = []; |
| 401 |
|
| 402 |
$filename = firebox()->_('FB_PLUGIN_NAME') . ' Items'; |
| 403 |
|
| 404 |
// name for 1 box |
| 405 |
if (count($boxes) == 1) |
| 406 |
{ |
| 407 |
$name = mb_strtolower(html_entity_decode($boxes['0']->post_title)); |
| 408 |
$name = preg_replace('#[^a-z0-9_-]#', '_', $name); |
| 409 |
$name = trim(preg_replace('#__+#', '_', $name), '_-'); |
| 410 |
|
| 411 |
$filename = firebox()->_('FB_PLUGIN_NAME') . ' Item (' . $name . ')'; |
| 412 |
} |
| 413 |
|
| 414 |
foreach ($boxes as $box) |
| 415 |
{ |
| 416 |
$meta = self::getMeta($box->ID); |
| 417 |
|
| 418 |
$exported[] = [ |
| 419 |
'box' => $box, |
| 420 |
'meta' => $meta |
| 421 |
]; |
| 422 |
} |
| 423 |
|
| 424 |
// SET DOCUMENT HEADER |
| 425 |
if (preg_match('#Opera(/| )([0-9].[0-9]{1,2})#', $_SERVER['HTTP_USER_AGENT'])) |
| 426 |
{ |
| 427 |
$UserBrowser = "Opera"; |
| 428 |
} |
| 429 |
elseif (preg_match('#MSIE ([0-9].[0-9]{1,2})#', $_SERVER['HTTP_USER_AGENT'])) |
| 430 |
{ |
| 431 |
$UserBrowser = "IE"; |
| 432 |
} |
| 433 |
else |
| 434 |
{ |
| 435 |
$UserBrowser = ''; |
| 436 |
} |
| 437 |
$mime_type = ($UserBrowser == 'IE' || $UserBrowser == 'Opera') ? 'application/octetstream' : 'application/octet-stream'; |
| 438 |
@ob_end_clean(); |
| 439 |
ob_start(); |
| 440 |
|
| 441 |
header('Content-Type: ' . $mime_type); |
| 442 |
header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT'); |
| 443 |
|
| 444 |
if ($UserBrowser == 'IE') |
| 445 |
{ |
| 446 |
header('Content-Disposition: inline; filename="' . $filename . '.fbox"'); |
| 447 |
header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); |
| 448 |
header('Pragma: public'); |
| 449 |
} |
| 450 |
else |
| 451 |
{ |
| 452 |
header('Content-Disposition: attachment; filename="' . $filename . '.fbox"'); |
| 453 |
header('Pragma: no-cache'); |
| 454 |
} |
| 455 |
|
| 456 |
// PRINT STRING |
| 457 |
echo wp_json_encode($exported); |
| 458 |
die(); |
| 459 |
} |
| 460 |
|
| 461 |
/** |
| 462 |
* Returns the last date viewed of a campaign. |
| 463 |
* |
| 464 |
* @param int $id |
| 465 |
* |
| 466 |
* @return string |
| 467 |
*/ |
| 468 |
public static function getCampaignLastDateViewed($id = null) |
| 469 |
{ |
| 470 |
if (!$id) |
| 471 |
{ |
| 472 |
return; |
| 473 |
} |
| 474 |
|
| 475 |
$last_date_viewed = firebox()->tables->boxlog->getResults([ |
| 476 |
'select' => [ |
| 477 |
'date as last_date_viewed' |
| 478 |
], |
| 479 |
'where' => [ |
| 480 |
'box' => ' = ' . esc_sql($id), |
| 481 |
], |
| 482 |
'orderby' => ' date desc', |
| 483 |
'limit' => 1 |
| 484 |
]); |
| 485 |
|
| 486 |
return isset($last_date_viewed[0]->last_date_viewed) ? get_date_from_gmt($last_date_viewed[0]->last_date_viewed) : null; |
| 487 |
} |
| 488 |
} |