| @@ -1,12 +1,12 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | 3 | * @package FireBox |
| 4 | - * @version 1.0.11 Free | |
| 4 | + * @version 2.0.12 Free | |
| 5 | 5 | * |
| 6 | 6 | * @author FirePlugins <info@fireplugins.com> |
| 7 | 7 | * @link https://www.fireplugins.com |
| 8 | - * @copyright Copyright © 2022 FirePlugins All Rights Reserved | |
| 8 | + * @copyright Copyright © 2023 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,24 +20,52 @@ | ||
| 20 | 20 | |
| 21 | 21 | class BoxHelper |
| 22 | 22 | { |
| 23 | 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 | + /** | |
| 24 | 36 | * Gets all Boxes. |
| 25 | - * We use get_posts instead of firebox()->tables->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 | |
| 28 | 37 | * |
| 38 | + * @param array $status | |
| 39 | + * | |
| 29 | 40 | * @return array |
| 30 | 41 | */ |
| 31 | - public static function getAllBoxes($status = 'publish') | |
| 42 | + public static function getAllBoxes($status = ['publish']) | |
| 32 | 43 | { |
| 33 | - $payload = [ | |
| 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 = [ | |
| 34 | 54 | 'post_status' => $status, |
| 35 | 55 | 'post_type' => 'firebox', |
| 36 | - 'numberposts' => -1 | |
| 56 | + 'posts_per_page' => -1 | |
| 37 | 57 | ]; |
| 58 | + | |
| 59 | + // Get the query. | |
| 60 | + $query = new \WP_Query($args); | |
| 38 | 61 | |
| 39 | - return get_posts($payload); | |
| 62 | + wp_reset_postdata(); | |
| 63 | + | |
| 64 | + // set cache | |
| 65 | + wp_cache_set($hash, $query); | |
| 66 | + | |
| 67 | + return $query; | |
| 40 | 68 | } |
| 41 | 69 | |
| 42 | 70 | /** |
| 43 | 71 | * Retrieves all boxes in a key => value array of ID => title |
| @@ -50,9 +78,9 @@ | ||
| 50 | 78 | { |
| 51 | 79 | return []; |
| 52 | 80 | } |
| 53 | 81 | |
| 54 | - return self::produceKeyValueBoxes($boxes); | |
| 82 | + return self::produceKeyValueBoxes($boxes->posts); | |
| 55 | 83 | } |
| 56 | 84 | |
| 57 | 85 | /** |
| 58 | 86 | * Produce a key,value pair of boxes containg their ID,title |
| @@ -219,48 +247,57 @@ | ||
| 219 | 247 | return $params; |
| 220 | 248 | } |
| 221 | 249 | |
| 222 | 250 | /** |
| 223 | - * Finds and returns the box from $_GET['template'] | |
| 251 | + * Returns the box template from the URL using the "template" query string. | |
| 224 | 252 | * |
| 225 | - * @return mixed | |
| 253 | + * @return array | |
| 226 | 254 | */ |
| 227 | - public static function getBoxFromTemplate() | |
| 255 | + public static function getBoxFromTemplateURL() | |
| 228 | 256 | { |
| 229 | - $template = isset($_GET['template']) ? sanitize_text_field($_GET['template']) : ''; | |
| 230 | - | |
| 231 | - if (empty($template)) | |
| 257 | + if (!isset($_GET['fpf_use_template'])) | |
| 232 | 258 | { |
| 233 | - return ''; | |
| 259 | + return; | |
| 234 | 260 | } |
| 235 | 261 | |
| 236 | - return firebox()->library->find($template); | |
| 237 | - } | |
| 238 | - | |
| 239 | - /** | |
| 240 | - * Transforms an array of key,value to inline CSS | |
| 241 | - * | |
| 242 | - * @param array $array | |
| 243 | - * | |
| 244 | - * @return string | |
| 245 | - */ | |
| 246 | - public static function arrayToCSSS($array) | |
| 247 | - { | |
| 248 | - $array = array_filter($array); | |
| 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 | + } | |
| 249 | 280 | |
| 250 | - if (empty($array)) | |
| 251 | - { | |
| 252 | - return ''; | |
| 253 | - } | |
| 281 | + if (!isset($local_template['id'])) | |
| 282 | + { | |
| 283 | + return; | |
| 284 | + } | |
| 254 | 285 | |
| 255 | - $styles = ''; | |
| 286 | + if (!isset($local_template['template'])) | |
| 287 | + { | |
| 288 | + return; | |
| 289 | + } | |
| 256 | 290 | |
| 257 | - foreach ($array as $key => $value) | |
| 258 | - { | |
| 259 | - $styles .= $key . ':' . $value . ';'; | |
| 260 | - } | |
| 291 | + if ((int) $local_template['id'] !== (int) $template) | |
| 292 | + { | |
| 293 | + return; | |
| 294 | + } | |
| 261 | 295 | |
| 262 | - return $styles; | |
| 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; | |
| 263 | 300 | } |
| 264 | 301 | |
| 265 | 302 | /** |
| 266 | 303 | * Duplicates a box |
| @@ -290,17 +327,19 @@ | ||
| 290 | 327 | $box = $box[0]; |
| 291 | 328 | $box->ID = ''; |
| 292 | 329 | $box->post_title = 'Copy of ' . $box->post_title; |
| 293 | 330 | $box->post_status = 'draft'; |
| 294 | - | |
| 331 | + | |
| 332 | + Form\Form::ensureUniqueFormIDs($box->post_content); | |
| 333 | + | |
| 334 | + // get meta options | |
| 335 | + $meta = self::getMeta($box_id); | |
| 336 | + | |
| 295 | 337 | // insert new box |
| 296 | 338 | $new_box_id = firebox()->tables->box->insert($box); |
| 297 | 339 | |
| 298 | - // get meta options | |
| 299 | - $meta = get_post_meta($box_id, 'fpframework_meta_settings', true); | |
| 300 | - | |
| 301 | 340 | // add meta options for new box |
| 302 | - update_post_meta($new_box_id, 'fpframework_meta_settings', $meta); | |
| 341 | + update_post_meta($new_box_id, 'fpframework_meta_settings', wp_slash($meta)); | |
| 303 | 342 | } |
| 304 | 343 | |
| 305 | 344 | /** |
| 306 | 345 | * Reset Box Stats |
| @@ -360,9 +399,9 @@ | ||
| 360 | 399 | } |
| 361 | 400 | |
| 362 | 401 | foreach ($boxes as $box) |
| 363 | 402 | { |
| 364 | - $meta = get_post_meta($box->ID, 'fpframework_meta_settings', true); | |
| 403 | + $meta = self::getMeta($box->ID); | |
| 365 | 404 | |
| 366 | 405 | $exported[] = [ |
| 367 | 406 | 'box' => $box, |
| 368 | 407 | 'meta' => $meta |
| @@ -368,10 +407,8 @@ | ||
| 368 | 407 | 'meta' => $meta |
| 369 | 408 | ]; |
| 370 | 409 | } |
| 371 | 410 | |
| 372 | - $string = json_encode($exported); | |
| 373 | - | |
| 374 | 411 | // SET DOCUMENT HEADER |
| 375 | 412 | if (preg_match('#Opera(/| )([0-9].[0-9]{1,2})#', $_SERVER['HTTP_USER_AGENT'])) |
| 376 | 413 | { |
| 377 | 414 | $UserBrowser = "Opera"; |
| @@ -403,8 +440,8 @@ | ||
| 403 | 440 | header('Pragma: no-cache'); |
| 404 | 441 | } |
| 405 | 442 | |
| 406 | 443 | // PRINT STRING |
| 407 | - echo $string; | |
| 408 | - die; | |
| 444 | + echo wp_json_encode($exported); | |
| 445 | + die(); | |
| 409 | 446 | } |
| 410 | 447 | } |