| @@ -1,53 +1,60 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -namespace UltimateStoreKit\Includes\Builder; | |
| 4 | - | |
| 5 | -use UltimateStoreKit\Base\Singleton; | |
| 6 | - | |
| 7 | -class Builder_Post_Singleton { | |
| 8 | - use Singleton; | |
| 9 | - | |
| 10 | - public static function set_sample_post($postId = null) { | |
| 11 | - global $post; | |
| 12 | - | |
| 13 | - if (!$postId) { | |
| 14 | - $postId = $post->ID; | |
| 15 | - } | |
| 16 | - | |
| 17 | - $meta = get_post_meta($postId); | |
| 18 | - | |
| 19 | - $templateMeta = optional($meta)[Meta::TEMPLATE_TYPE]; | |
| 20 | - | |
| 21 | - if (!isset($templateMeta[0])) { | |
| 22 | - return; | |
| 23 | - } | |
| 24 | - | |
| 25 | - $postMeta = $templateMeta[0]; | |
| 26 | - $postMeta = explode('|', $postMeta); | |
| 27 | - $postType = $postMeta[0]; | |
| 28 | - | |
| 29 | - $args = [ | |
| 30 | - 'post_type' => $postType, | |
| 31 | - 'post_status' => ['publish', 'pending', 'draft', 'future'], | |
| 32 | - 'posts_per_page' => 1, | |
| 33 | - ]; | |
| 34 | - | |
| 35 | - $page_settings_manager = \Elementor\Core\Settings\Manager::get_settings_managers('page'); | |
| 36 | - $page_settings_model = $page_settings_manager->get_model($postId); | |
| 37 | - $sample_product = $page_settings_model->get_settings('usk_builder_sample_post_id'); | |
| 38 | - | |
| 39 | - if ($sample_product) { | |
| 40 | - $args['p'] = $sample_product; | |
| 41 | - } | |
| 42 | - | |
| 43 | - $wp_query = new \WP_Query($args); | |
| 44 | - | |
| 45 | - if ($wp_query->have_posts()) { | |
| 46 | - $post = $wp_query->posts[0]; | |
| 47 | - set_transient('ultimate_store_template_id_' . get_current_user_id(), $postId); | |
| 48 | - set_transient('ultimate_store_template_sample_post_' . get_current_user_id(), $wp_query); | |
| 49 | - $GLOBALS['post'] = $post; | |
| 50 | - setup_postdata($post); | |
| 51 | - } | |
| 52 | - } | |
| 53 | -} | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace UltimateStoreKit\Includes\Builder; | |
| 4 | + | |
| 5 | +if (! defined('ABSPATH')) { | |
| 6 | + exit; // Exit if accessed directly | |
| 7 | +} | |
| 8 | + | |
| 9 | +use UltimateStoreKit\Base\Singleton; | |
| 10 | + | |
| 11 | +class Builder_Post_Singleton { | |
| 12 | + use Singleton; | |
| 13 | + | |
| 14 | + public static function set_sample_post($postId = null) { | |
| 15 | + global $post; | |
| 16 | + | |
| 17 | + if (!$postId) { | |
| 18 | + $postId = $post->ID; | |
| 19 | + } | |
| 20 | + | |
| 21 | + $meta = get_post_meta($postId); | |
| 22 | + | |
| 23 | + $templateMeta = ultimate_store_kit_optional($meta)[Meta::TEMPLATE_TYPE]; | |
| 24 | + | |
| 25 | + if (!isset($templateMeta[0])) { | |
| 26 | + return; | |
| 27 | + } | |
| 28 | + | |
| 29 | + $postMeta = $templateMeta[0]; | |
| 30 | + $postMeta = explode('|', $postMeta); | |
| 31 | + $postType = $postMeta[0]; | |
| 32 | + | |
| 33 | + $args = [ | |
| 34 | + 'post_type' => $postType, | |
| 35 | + 'post_status' => ['publish', 'pending', 'draft', 'future'], | |
| 36 | + 'posts_per_page' => 1, | |
| 37 | + ]; | |
| 38 | + | |
| 39 | + $page_settings_manager = \Elementor\Core\Settings\Manager::get_settings_managers('page'); | |
| 40 | + $page_settings_model = $page_settings_manager->get_model($postId); | |
| 41 | + $sample_product = $page_settings_model->get_settings('usk_builder_sample_post_id'); | |
| 42 | + | |
| 43 | + if ($sample_product) { | |
| 44 | + $args['p'] = $sample_product; | |
| 45 | + } | |
| 46 | + | |
| 47 | + $wp_query = new \WP_Query($args); | |
| 48 | + | |
| 49 | + if ($wp_query->have_posts()) { | |
| 50 | + $post = $wp_query->posts[0]; | |
| 51 | + // An explicit lifetime is required: a transient stored with no expiry is | |
| 52 | + // autoloaded on every request, so these editor-preview handoffs otherwise | |
| 53 | + // accumulate one permanent wp_options row per user who opens the builder. | |
| 54 | + set_transient('ultimate_store_kit_template_id_' . get_current_user_id(), $postId, HOUR_IN_SECONDS); | |
| 55 | + set_transient('ultimate_store_kit_template_sample_post_' . get_current_user_id(), $wp_query, HOUR_IN_SECONDS); | |
| 56 | + $GLOBALS['post'] = $post; | |
| 57 | + setup_postdata($post); | |
| 58 | + } | |
| 59 | + } | |
| 60 | +} | |