| @@ -1,6 +1,8 @@ | ||
| 1 | 1 | <?php |
| 2 | + | |
| 2 | 3 | namespace UiCoreElements; |
| 4 | + | |
| 3 | 5 | use \Elementor\Plugin; |
| 4 | 6 | use UiCoreElements\Helper; |
| 5 | 7 | use UiCoreElements\Utils\Contact_Form_Service; |
| 6 | 8 | |
| @@ -6,15 +8,18 @@ | ||
| 6 | 8 | |
| 7 | 9 | /** |
| 8 | 10 | * REST_API Handler |
| 9 | 11 | */ |
| 10 | -class REST_API { | |
| 12 | +class REST_API | |
| 13 | +{ | |
| 11 | 14 | |
| 12 | - public function __construct() { | |
| 15 | + public function __construct() | |
| 16 | + { | |
| 13 | 17 | add_action('rest_api_init', [$this, 'register_routes']); |
| 14 | 18 | } |
| 15 | 19 | |
| 16 | - public function register_routes(){ | |
| 20 | + public function register_routes() | |
| 21 | + { | |
| 17 | 22 | register_rest_route('uielem/v1', '/load_more/', [ |
| 18 | 23 | 'methods' => 'GET', |
| 19 | 24 | 'show_in_index' => true, |
| 20 | 25 | 'callback' => [$this, 'apg_load_more'], |
| @@ -44,8 +49,19 @@ | ||
| 44 | 49 | 'methods' => 'POST', |
| 45 | 50 | 'callback' => [$this, 'process_submission'], |
| 46 | 51 | 'permission_callback' => '__return_true', |
| 47 | 52 | ]); |
| 53 | + register_rest_route('uielem/v1', '/prepare_template', [ | |
| 54 | + 'methods' => 'POST', | |
| 55 | + 'callback' => [$this, 'prepare_template'], | |
| 56 | + 'permission_callback' => [$this, 'check_for_permission'], | |
| 57 | + ]); | |
| 58 | + | |
| 59 | + register_rest_route('uielem/v1', '/check_connection', [ | |
| 60 | + 'methods' => 'GET', | |
| 61 | + 'callback' => [$this, 'check_connection'], | |
| 62 | + 'permission_callback' => [$this, 'check_for_permission'], | |
| 63 | + ]); | |
| 48 | 64 | } |
| 49 | 65 | |
| 50 | 66 | public function check_for_permission() |
| 51 | 67 | { |
| @@ -51,9 +67,10 @@ | ||
| 51 | 67 | { |
| 52 | 68 | return current_user_can('manage_options'); |
| 53 | 69 | } |
| 54 | 70 | |
| 55 | - public function apg_load_more(\WP_REST_Request $request) { | |
| 71 | + public function apg_load_more(\WP_REST_Request $request) | |
| 72 | + { | |
| 56 | 73 | $current_query = false; |
| 57 | 74 | |
| 58 | 75 | // Identify the widget |
| 59 | 76 | $widget_id = $request->get_param('widget_id'); |
| @@ -60,9 +77,9 @@ | ||
| 60 | 77 | $widget_type = $request->get_param('widget_type'); |
| 61 | 78 | $is_product = strpos($widget_type, 'product') !== false; |
| 62 | 79 | |
| 63 | 80 | // Get widget settings |
| 64 | - $settings = get_transient('ui_elements_widgetdata_'.$widget_id); | |
| 81 | + $settings = get_transient('ui_elements_widgetdata_' . $widget_id); | |
| 65 | 82 | |
| 66 | 83 | // Check if is an "Advanced Product.." widget to determine the proper control slug |
| 67 | 84 | $slug = $is_product ? 'product-filter' : 'posts-filter'; |
| 68 | 85 | |
| @@ -69,13 +86,19 @@ | ||
| 69 | 86 | // Set extra settings from request params |
| 70 | 87 | $tax = $slug . '_' . $request->get_param('type') . '_ids'; |
| 71 | 88 | $settings[$tax] = $request->get_param('term'); |
| 72 | 89 | $settings['__current_page'] = $request->get_param('page'); |
| 90 | + $settings['__current_post_id'] = $request->get_param('current_post_id'); | |
| 73 | 91 | |
| 92 | + // Use load more qty (new option) if available | |
| 93 | + if (isset($settings['load_more_qty']) && isset($settings['load_more_qty']['size']) && !empty($settings['load_more_qty']['size'])) { | |
| 94 | + $settings['item_limit'] = $settings['load_more_qty']; | |
| 95 | + } | |
| 96 | + | |
| 74 | 97 | // Build current query if requested |
| 75 | - if( $settings[$slug . '_post_type'] == 'current' ){ | |
| 98 | + if ($settings[$slug . '_post_type'] == 'current') { | |
| 76 | 99 | $current_query = $request->get_param('current_query'); |
| 77 | - $current_query = json_decode( urldecode($current_query), true ); //decode url and transform to array | |
| 100 | + $current_query = json_decode(urldecode($current_query), true); //decode url and transform to array | |
| 78 | 101 | $current_query['paged'] = $request->get_param('page'); |
| 79 | 102 | } |
| 80 | 103 | |
| 81 | 104 | // Create the element data |
| @@ -86,12 +109,12 @@ | ||
| 86 | 109 | ]; |
| 87 | 110 | |
| 88 | 111 | // Generate a new instance of the widget with those settings and return the markup |
| 89 | 112 | //$widget = new AdvancedPostGrid($widget, $settings); todo: discover why this method don't work |
| 90 | - $widget = Plugin::instance()->elements_manager->create_element_instance( $widget, $settings ); | |
| 113 | + $widget = Plugin::instance()->elements_manager->create_element_instance($widget, $settings); | |
| 91 | 114 | $widget->set_settings($settings); |
| 92 | 115 | |
| 93 | - $data = $widget->render_ajax( $current_query ); | |
| 116 | + $data = $widget->render_ajax($current_query); | |
| 94 | 117 | |
| 95 | 118 | return [ |
| 96 | 119 | 'markup' => $data['markup'], |
| 97 | 120 | 'total_pages' => $is_product ? $data['total_pages'] : $widget->get_query()->max_num_pages, |
| @@ -97,9 +120,10 @@ | ||
| 97 | 120 | 'total_pages' => $is_product ? $data['total_pages'] : $widget->get_query()->max_num_pages, |
| 98 | 121 | ]; |
| 99 | 122 | } |
| 100 | 123 | |
| 101 | - public function process_submission(\WP_REST_Request $request) { | |
| 124 | + public function process_submission(\WP_REST_Request $request) | |
| 125 | + { | |
| 102 | 126 | |
| 103 | 127 | // Get referer origin, form and widget data |
| 104 | 128 | $form_data = $request->get_params(); |
| 105 | 129 | $files = $request->get_file_params(); |
| @@ -109,5 +133,32 @@ | ||
| 109 | 133 | $service = new Contact_Form_Service($form_data, $settings, $files); |
| 110 | 134 | $response = $service->handle(); |
| 111 | 135 | return $response; |
| 112 | 136 | } |
| 113 | -} | |
| 137 | + | |
| 138 | + | |
| 139 | + public function prepare_template(\WP_REST_Request $request) | |
| 140 | + { | |
| 141 | + $template = $request->get_param('data'); | |
| 142 | + $template = json_decode($template, true); | |
| 143 | + $template = DesignCloud::import_content($template); | |
| 144 | + return [ | |
| 145 | + 'success' => true, | |
| 146 | + 'template' => $template, | |
| 147 | + ]; | |
| 148 | + } | |
| 149 | + | |
| 150 | + public function check_connection() | |
| 151 | + { | |
| 152 | + $local_data = get_option('uicore_connect', [ | |
| 153 | + 'url' => '', | |
| 154 | + 'token' => '', | |
| 155 | + ]); | |
| 156 | + return [ | |
| 157 | + 'success' => true, | |
| 158 | + 'license' => [ | |
| 159 | + 'key' => $local_data['token'], | |
| 160 | + 'url' => $local_data['url'], | |
| 161 | + ], | |
| 162 | + ]; | |
| 163 | + } | |
| 164 | +} | |