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