| @@ -1,12 +1,12 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | 3 | * @package FireBox |
| 4 | - * @version 1.0.0 Free | |
| 4 | + * @version 2.1.14 Free | |
| 5 | 5 | * |
| 6 | 6 | * @author FirePlugins <info@fireplugins.com> |
| 7 | 7 | * @link https://www.fireplugins.com |
| 8 | - * @copyright Copyright © 2021 FirePlugins All Rights Reserved | |
| 8 | + * @copyright Copyright © 2024 FirePlugins All Rights Reserved | |
| 9 | 9 | * @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later |
| 10 | 10 | */ |
| 11 | 11 | |
| 12 | 12 | namespace FireBox\Core\Helpers; |
| @@ -20,45 +20,51 @@ | ||
| 20 | 20 | |
| 21 | 21 | class BoxHelper |
| 22 | 22 | { |
| 23 | 23 | /** |
| 24 | - * Gets all Boxes. | |
| 25 | - * We use get_posts instead of firebox()->models->box->getResults | |
| 26 | - * as we need it on Widgets initialization where we don't have the framework | |
| 27 | - * available yet to query the database with our wrapper | |
| 24 | + * Get box meta | |
| 28 | 25 | * |
| 26 | + * @param int $id | |
| 27 | + * | |
| 29 | 28 | * @return array |
| 30 | 29 | */ |
| 31 | - public static function getAllBoxes($status = 'publish') | |
| 30 | + public static function getMeta($id) | |
| 32 | 31 | { |
| 33 | - $payload = [ | |
| 34 | - 'post_status' => $status, | |
| 35 | - 'post_type' => 'firebox', | |
| 36 | - 'numberposts' => -1 | |
| 37 | - ]; | |
| 38 | - | |
| 39 | - return get_posts($payload); | |
| 32 | + return get_post_meta($id, 'fpframework_meta_settings', true); | |
| 40 | 33 | } |
| 41 | 34 | |
| 42 | 35 | /** |
| 43 | - * Gets all Boxes WP Query. | |
| 36 | + * Gets all Boxes. | |
| 44 | 37 | * |
| 38 | + * @param array $status | |
| 39 | + * @param int $limit | |
| 40 | + * | |
| 45 | 41 | * @return array |
| 46 | 42 | */ |
| 47 | - public static function getAllBoxesWPQuery($status = 'publish') | |
| 43 | + public static function getAllBoxes($status = ['publish'], $limit = -1) | |
| 48 | 44 | { |
| 49 | - $payload = array( | |
| 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 = [ | |
| 50 | 55 | 'post_status' => $status, |
| 51 | 56 | 'post_type' => 'firebox', |
| 52 | - 'numberposts' => -1 | |
| 53 | - ); | |
| 54 | - $query = new \WP_Query($payload); | |
| 57 | + 'posts_per_page' => $limit | |
| 58 | + ]; | |
| 59 | + | |
| 60 | + // Get the query. | |
| 61 | + $query = new \WP_Query($args); | |
| 62 | + | |
| 55 | 63 | wp_reset_postdata(); |
| 56 | 64 | |
| 57 | - if (!$query->have_posts()) | |
| 58 | - { | |
| 59 | - return []; | |
| 60 | - } | |
| 65 | + // set cache | |
| 66 | + wp_cache_set($hash, $query); | |
| 61 | 67 | |
| 62 | 68 | return $query; |
| 63 | 69 | } |
| 64 | 70 | |
| @@ -73,9 +79,9 @@ | ||
| 73 | 79 | { |
| 74 | 80 | return []; |
| 75 | 81 | } |
| 76 | 82 | |
| 77 | - return self::produceKeyValueBoxes($boxes); | |
| 83 | + return self::produceKeyValueBoxes($boxes->posts); | |
| 78 | 84 | } |
| 79 | 85 | |
| 80 | 86 | /** |
| 81 | 87 | * Produce a key,value pair of boxes containg their ID,title |
| @@ -113,9 +119,9 @@ | ||
| 113 | 119 | { |
| 114 | 120 | return []; |
| 115 | 121 | } |
| 116 | 122 | |
| 117 | - $boxes = firebox()->models->box->getResults([ | |
| 123 | + $boxes = firebox()->tables->box->getResults([ | |
| 118 | 124 | 'where' => [ |
| 119 | 125 | 'ID' => ' NOT IN (' . esc_sql($id) . ')', |
| 120 | 126 | 'post_status' => " = 'publish'", |
| 121 | 127 | 'post_type' => " = 'firebox'" |
| @@ -147,9 +153,9 @@ | ||
| 147 | 153 | } |
| 148 | 154 | |
| 149 | 155 | $box = (int) $box; |
| 150 | 156 | |
| 151 | - $box = firebox()->models->box->getResults([ | |
| 157 | + $box = firebox()->tables->box->getResults([ | |
| 152 | 158 | 'where' => [ |
| 153 | 159 | 'ID' => " = '" . esc_sql($box) . "'" |
| 154 | 160 | ] |
| 155 | 161 | ]); |
| @@ -172,9 +178,9 @@ | ||
| 172 | 178 | } |
| 173 | 179 | |
| 174 | 180 | $box = (int) $box; |
| 175 | 181 | |
| 176 | - $box = firebox()->models->box->getResults([ | |
| 182 | + $box = firebox()->tables->box->getResults([ | |
| 177 | 183 | 'where' => [ |
| 178 | 184 | 'ID' => " = '" . esc_sql($box) . "'" |
| 179 | 185 | ] |
| 180 | 186 | ]); |
| @@ -195,11 +201,11 @@ | ||
| 195 | 201 | * @return array |
| 196 | 202 | */ |
| 197 | 203 | public static function getSelectedSearchItems($items) |
| 198 | 204 | { |
| 199 | - $boxes = firebox()->models->box->getResults([ | |
| 205 | + $boxes = firebox()->tables->box->getResults([ | |
| 200 | 206 | 'where' => [ |
| 201 | - 'ID' => ' IN(' . implode(',', esc_sql($items)) . ')', | |
| 207 | + 'ID' => ' IN(' . implode(',', array_map('intval', $items)) . ')', | |
| 202 | 208 | 'post_status' => " = 'publish'", |
| 203 | 209 | 'post_type' => " = 'firebox'" |
| 204 | 210 | ] |
| 205 | 211 | ]); |
| @@ -242,48 +248,58 @@ | ||
| 242 | 248 | return $params; |
| 243 | 249 | } |
| 244 | 250 | |
| 245 | 251 | /** |
| 246 | - * Finds and returns the box from $_GET['template'] | |
| 252 | + * Returns the box template from the URL using the "template" query string. | |
| 247 | 253 | * |
| 248 | - * @return mixed | |
| 254 | + * @return array | |
| 249 | 255 | */ |
| 250 | - public static function getBoxFromTemplate() | |
| 256 | + public static function getBoxFromTemplateURL() | |
| 251 | 257 | { |
| 252 | - $template = isset($_GET['template']) ? sanitize_text_field($_GET['template']) : ''; | |
| 253 | - | |
| 254 | - if (empty($template)) | |
| 258 | + if (!isset($_GET['fpf_use_template'])) //phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 255 | 259 | { |
| 256 | - return ''; | |
| 260 | + return; | |
| 257 | 261 | } |
| 258 | 262 | |
| 259 | - return firebox()->library->find($template); | |
| 260 | - } | |
| 261 | - | |
| 262 | - /** | |
| 263 | - * Transforms an array of key,value to inline CSS | |
| 264 | - * | |
| 265 | - * @param array $array | |
| 266 | - * | |
| 267 | - * @return string | |
| 268 | - */ | |
| 269 | - public static function arrayToCSSS($array) | |
| 270 | - { | |
| 271 | - $array = array_filter($array); | |
| 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 | + } | |
| 272 | 282 | |
| 273 | - if (empty($array)) | |
| 274 | - { | |
| 275 | - return ''; | |
| 276 | - } | |
| 283 | + if (!isset($local_template['id'])) | |
| 284 | + { | |
| 285 | + return; | |
| 286 | + } | |
| 277 | 287 | |
| 278 | - $styles = ''; | |
| 288 | + if (!isset($local_template['template'])) | |
| 289 | + { | |
| 290 | + return; | |
| 291 | + } | |
| 279 | 292 | |
| 280 | - foreach ($array as $key => $value) | |
| 281 | - { | |
| 282 | - $styles .= $key . ':' . $value . ';'; | |
| 283 | - } | |
| 293 | + if ((int) $local_template['id'] !== (int) $template) | |
| 294 | + { | |
| 295 | + return; | |
| 296 | + } | |
| 284 | 297 | |
| 285 | - return $styles; | |
| 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; | |
| 286 | 302 | } |
| 287 | 303 | |
| 288 | 304 | /** |
| 289 | 305 | * Duplicates a box |
| @@ -294,9 +310,9 @@ | ||
| 294 | 310 | */ |
| 295 | 311 | public static function duplicateBox($box_id) |
| 296 | 312 | { |
| 297 | 313 | // get box |
| 298 | - $box = firebox()->models->box->getResults([ | |
| 314 | + $box = firebox()->tables->box->getResults([ | |
| 299 | 315 | 'where' => [ |
| 300 | 316 | 'ID' => " = '" . esc_sql($box_id) . "'", |
| 301 | 317 | 'post_status' => " = '" . esc_sql(get_post_status($box_id)) . "'", |
| 302 | 318 | 'post_type' => " = 'firebox'" |
| @@ -313,17 +329,30 @@ | ||
| 313 | 329 | $box = $box[0]; |
| 314 | 330 | $box->ID = ''; |
| 315 | 331 | $box->post_title = 'Copy of ' . $box->post_title; |
| 316 | 332 | $box->post_status = 'draft'; |
| 317 | - | |
| 318 | - // insert new box | |
| 319 | - $new_box_id = firebox()->models->box->insert($box); | |
| 320 | 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 | + | |
| 321 | 345 | // get meta options |
| 322 | - $meta = get_post_meta($box_id, 'fpframework_meta_settings', true); | |
| 346 | + $meta = self::getMeta($box_id); | |
| 323 | 347 | |
| 348 | + // insert new box | |
| 349 | + $new_box_id = firebox()->tables->box->insert($box); | |
| 350 | + | |
| 324 | 351 | // add meta options for new box |
| 325 | - update_post_meta($new_box_id, 'fpframework_meta_settings', $meta); | |
| 352 | + update_post_meta($new_box_id, 'fpframework_meta_settings', wp_slash($meta)); | |
| 353 | + | |
| 354 | + return true; | |
| 326 | 355 | } |
| 327 | 356 | |
| 328 | 357 | /** |
| 329 | 358 | * Reset Box Stats |
| @@ -333,16 +362,16 @@ | ||
| 333 | 362 | * @return void |
| 334 | 363 | */ |
| 335 | 364 | public static function resetBoxStats($box_ids) |
| 336 | 365 | { |
| 337 | - $logs_table = firebox()->models->boxlog->getFullTableName(); | |
| 338 | - $logs_details_table = firebox()->models->boxlogdetails->getFullTableName(); | |
| 366 | + $logs_table = firebox()->tables->boxlog->getFullTableName(); | |
| 367 | + $logs_details_table = firebox()->tables->boxlogdetails->getFullTableName(); | |
| 339 | 368 | |
| 340 | 369 | // delete box logs details |
| 341 | - firebox()->models->boxlogdetails->executeRaw("DELETE FROM `$logs_details_table` WHERE log_id IN (SELECT id FROM `$logs_table` WHERE box IN (" . implode(",", $box_ids) . "))"); | |
| 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) . "))"); | |
| 342 | 371 | |
| 343 | 372 | // delete box logs |
| 344 | - firebox()->models->boxlog->deleteRaw('WHERE box IN (' . implode(',', $box_ids) . ')'); | |
| 373 | + firebox()->tables->boxlog->deleteRaw('WHERE box IN (' . implode(',', $box_ids) . ')'); | |
| 345 | 374 | } |
| 346 | 375 | |
| 347 | 376 | /** |
| 348 | 377 | * Exports boxes |
| @@ -353,9 +382,9 @@ | ||
| 353 | 382 | */ |
| 354 | 383 | public static function exportBoxes($box_ids) |
| 355 | 384 | { |
| 356 | 385 | // get boxes |
| 357 | - $boxes = firebox()->models->box->getResults([ | |
| 386 | + $boxes = firebox()->tables->box->getResults([ | |
| 358 | 387 | 'where' => [ |
| 359 | 388 | 'ID' => ' IN (' . implode(',', esc_sql($box_ids)) . ')', |
| 360 | 389 | 'post_type' => " = 'firebox'" |
| 361 | 390 | ] |
| @@ -383,9 +412,9 @@ | ||
| 383 | 412 | } |
| 384 | 413 | |
| 385 | 414 | foreach ($boxes as $box) |
| 386 | 415 | { |
| 387 | - $meta = get_post_meta($box->ID, 'fpframework_meta_settings', true); | |
| 416 | + $meta = self::getMeta($box->ID); | |
| 388 | 417 | |
| 389 | 418 | $exported[] = [ |
| 390 | 419 | 'box' => $box, |
| 391 | 420 | 'meta' => $meta |
| @@ -391,10 +420,8 @@ | ||
| 391 | 420 | 'meta' => $meta |
| 392 | 421 | ]; |
| 393 | 422 | } |
| 394 | 423 | |
| 395 | - $string = json_encode($exported); | |
| 396 | - | |
| 397 | 424 | // SET DOCUMENT HEADER |
| 398 | 425 | if (preg_match('#Opera(/| )([0-9].[0-9]{1,2})#', $_SERVER['HTTP_USER_AGENT'])) |
| 399 | 426 | { |
| 400 | 427 | $UserBrowser = "Opera"; |
| @@ -426,8 +453,36 @@ | ||
| 426 | 453 | header('Pragma: no-cache'); |
| 427 | 454 | } |
| 428 | 455 | |
| 429 | 456 | // PRINT STRING |
| 430 | - echo $string; | |
| 431 | - die; | |
| 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; | |
| 432 | 487 | } |
| 433 | 488 | } |