| @@ -1,113 +1,164 @@ | ||
| 1 | -<?php | |
| 2 | -namespace UiCoreElements; | |
| 3 | -use \Elementor\Plugin; | |
| 4 | -use UiCoreElements\Helper; | |
| 5 | -use UiCoreElements\Utils\Contact_Form_Service; | |
| 6 | - | |
| 7 | -/** | |
| 8 | - * REST_API Handler | |
| 9 | - */ | |
| 10 | -class REST_API { | |
| 11 | - | |
| 12 | - public function __construct() { | |
| 13 | - add_action('rest_api_init', [$this, 'register_routes']); | |
| 14 | - } | |
| 15 | - | |
| 16 | - public function register_routes(){ | |
| 17 | - register_rest_route('uielem/v1', '/load_more/', [ | |
| 18 | - 'methods' => 'GET', | |
| 19 | - 'show_in_index' => true, | |
| 20 | - 'callback' => [$this, 'apg_load_more'], | |
| 21 | - 'permission_callback' => '__return_true', | |
| 22 | - 'args' => [ | |
| 23 | - 'widget_id' => [ | |
| 24 | - 'required' => true, | |
| 25 | - ], | |
| 26 | - 'widget_type' => [ | |
| 27 | - 'required' => true, | |
| 28 | - ], | |
| 29 | - 'page' => [ | |
| 30 | - 'required' => false, | |
| 31 | - 'default' => 1, | |
| 32 | - ], | |
| 33 | - 'type' => [ | |
| 34 | - 'required' => false, | |
| 35 | - 'default' => '', | |
| 36 | - ], | |
| 37 | - 'term' => [ | |
| 38 | - 'required' => false, | |
| 39 | - 'default' => '', | |
| 40 | - ], | |
| 41 | - ], | |
| 42 | - ]); | |
| 43 | - register_rest_route('uielem/v1', '/submit_actions/', [ | |
| 44 | - 'methods' => 'POST', | |
| 45 | - 'callback' => [$this, 'process_submission'], | |
| 46 | - 'permission_callback' => '__return_true', | |
| 47 | - ]); | |
| 48 | - } | |
| 49 | - | |
| 50 | - public function check_for_permission() | |
| 51 | - { | |
| 52 | - return current_user_can('manage_options'); | |
| 53 | - } | |
| 54 | - | |
| 55 | - public function apg_load_more(\WP_REST_Request $request) { | |
| 56 | - $current_query = false; | |
| 57 | - | |
| 58 | - // Identify the widget | |
| 59 | - $widget_id = $request->get_param('widget_id'); | |
| 60 | - $widget_type = $request->get_param('widget_type'); | |
| 61 | - $is_product = strpos($widget_type, 'product') !== false; | |
| 62 | - | |
| 63 | - // Get widget settings | |
| 64 | - $settings = get_transient('ui_elements_widgetdata_'.$widget_id); | |
| 65 | - | |
| 66 | - // Check if is an "Advanced Product.." widget to determine the proper control slug | |
| 67 | - $slug = $is_product ? 'product-filter' : 'posts-filter'; | |
| 68 | - | |
| 69 | - // Set extra settings from request params | |
| 70 | - $tax = $slug . '_' . $request->get_param('type') . '_ids'; | |
| 71 | - $settings[$tax] = $request->get_param('term'); | |
| 72 | - $settings['__current_page'] = $request->get_param('page'); | |
| 73 | - | |
| 74 | - // Build current query if requested | |
| 75 | - if( $settings[$slug . '_post_type'] == 'current' ){ | |
| 76 | - $current_query = $request->get_param('current_query'); | |
| 77 | - $current_query = json_decode( urldecode($current_query), true ); //decode url and transform to array | |
| 78 | - $current_query['paged'] = $request->get_param('page'); | |
| 79 | - } | |
| 80 | - | |
| 81 | - // Create the element data | |
| 82 | - $widget = [ | |
| 83 | - 'elType' => 'widget', | |
| 84 | - 'widgetType' => $widget_type, | |
| 85 | - 'id' => $widget_id, | |
| 86 | - ]; | |
| 87 | - | |
| 88 | - // Generate a new instance of the widget with those settings and return the markup | |
| 89 | - //$widget = new AdvancedPostGrid($widget, $settings); todo: discover why this method don't work | |
| 90 | - $widget = Plugin::instance()->elements_manager->create_element_instance( $widget, $settings ); | |
| 91 | - $widget->set_settings($settings); | |
| 92 | - | |
| 93 | - $data = $widget->render_ajax( $current_query ); | |
| 94 | - | |
| 95 | - return [ | |
| 96 | - 'markup' => $data['markup'], | |
| 97 | - 'total_pages' => $is_product ? $data['total_pages'] : $widget->get_query()->max_num_pages, | |
| 98 | - ]; | |
| 99 | - } | |
| 100 | - | |
| 101 | - public function process_submission(\WP_REST_Request $request) { | |
| 102 | - | |
| 103 | - // Get referer origin, form and widget data | |
| 104 | - $form_data = $request->get_params(); | |
| 105 | - $files = $request->get_file_params(); | |
| 106 | - $settings = get_transient('ui_e_form_widget_settings_' . $form_data['widget_id']); | |
| 107 | - | |
| 108 | - // Request the contact form service and return the response | |
| 109 | - $service = new Contact_Form_Service($form_data, $settings, $files); | |
| 110 | - $response = $service->handle(); | |
| 111 | - return $response; | |
| 112 | - } | |
| 113 | -} | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace UiCoreElements; | |
| 4 | + | |
| 5 | +use \Elementor\Plugin; | |
| 6 | +use UiCoreElements\Helper; | |
| 7 | +use UiCoreElements\Utils\Contact_Form_Service; | |
| 8 | + | |
| 9 | +/** | |
| 10 | + * REST_API Handler | |
| 11 | + */ | |
| 12 | +class REST_API | |
| 13 | +{ | |
| 14 | + | |
| 15 | + public function __construct() | |
| 16 | + { | |
| 17 | + add_action('rest_api_init', [$this, 'register_routes']); | |
| 18 | + } | |
| 19 | + | |
| 20 | + public function register_routes() | |
| 21 | + { | |
| 22 | + register_rest_route('uielem/v1', '/load_more/', [ | |
| 23 | + 'methods' => 'GET', | |
| 24 | + 'show_in_index' => true, | |
| 25 | + 'callback' => [$this, 'apg_load_more'], | |
| 26 | + 'permission_callback' => '__return_true', | |
| 27 | + 'args' => [ | |
| 28 | + 'widget_id' => [ | |
| 29 | + 'required' => true, | |
| 30 | + ], | |
| 31 | + 'widget_type' => [ | |
| 32 | + 'required' => true, | |
| 33 | + ], | |
| 34 | + 'page' => [ | |
| 35 | + 'required' => false, | |
| 36 | + 'default' => 1, | |
| 37 | + ], | |
| 38 | + 'type' => [ | |
| 39 | + 'required' => false, | |
| 40 | + 'default' => '', | |
| 41 | + ], | |
| 42 | + 'term' => [ | |
| 43 | + 'required' => false, | |
| 44 | + 'default' => '', | |
| 45 | + ], | |
| 46 | + ], | |
| 47 | + ]); | |
| 48 | + register_rest_route('uielem/v1', '/submit_actions/', [ | |
| 49 | + 'methods' => 'POST', | |
| 50 | + 'callback' => [$this, 'process_submission'], | |
| 51 | + 'permission_callback' => '__return_true', | |
| 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 | + ]); | |
| 64 | + } | |
| 65 | + | |
| 66 | + public function check_for_permission() | |
| 67 | + { | |
| 68 | + return current_user_can('manage_options'); | |
| 69 | + } | |
| 70 | + | |
| 71 | + public function apg_load_more(\WP_REST_Request $request) | |
| 72 | + { | |
| 73 | + $current_query = false; | |
| 74 | + | |
| 75 | + // Identify the widget | |
| 76 | + $widget_id = $request->get_param('widget_id'); | |
| 77 | + $widget_type = $request->get_param('widget_type'); | |
| 78 | + $is_product = strpos($widget_type, 'product') !== false; | |
| 79 | + | |
| 80 | + // Get widget settings | |
| 81 | + $settings = get_transient('ui_elements_widgetdata_' . $widget_id); | |
| 82 | + | |
| 83 | + // Check if is an "Advanced Product.." widget to determine the proper control slug | |
| 84 | + $slug = $is_product ? 'product-filter' : 'posts-filter'; | |
| 85 | + | |
| 86 | + // Set extra settings from request params | |
| 87 | + $tax = $slug . '_' . $request->get_param('type') . '_ids'; | |
| 88 | + $settings[$tax] = $request->get_param('term'); | |
| 89 | + $settings['__current_page'] = $request->get_param('page'); | |
| 90 | + $settings['__current_post_id'] = $request->get_param('current_post_id'); | |
| 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 | + | |
| 97 | + // Build current query if requested | |
| 98 | + if ($settings[$slug . '_post_type'] == 'current') { | |
| 99 | + $current_query = $request->get_param('current_query'); | |
| 100 | + $current_query = json_decode(urldecode($current_query), true); //decode url and transform to array | |
| 101 | + $current_query['paged'] = $request->get_param('page'); | |
| 102 | + } | |
| 103 | + | |
| 104 | + // Create the element data | |
| 105 | + $widget = [ | |
| 106 | + 'elType' => 'widget', | |
| 107 | + 'widgetType' => $widget_type, | |
| 108 | + 'id' => $widget_id, | |
| 109 | + ]; | |
| 110 | + | |
| 111 | + // Generate a new instance of the widget with those settings and return the markup | |
| 112 | + //$widget = new AdvancedPostGrid($widget, $settings); todo: discover why this method don't work | |
| 113 | + $widget = Plugin::instance()->elements_manager->create_element_instance($widget, $settings); | |
| 114 | + $widget->set_settings($settings); | |
| 115 | + | |
| 116 | + $data = $widget->render_ajax($current_query); | |
| 117 | + | |
| 118 | + return [ | |
| 119 | + 'markup' => $data['markup'], | |
| 120 | + 'total_pages' => $is_product ? $data['total_pages'] : $widget->get_query()->max_num_pages, | |
| 121 | + ]; | |
| 122 | + } | |
| 123 | + | |
| 124 | + public function process_submission(\WP_REST_Request $request) | |
| 125 | + { | |
| 126 | + | |
| 127 | + // Get referer origin, form and widget data | |
| 128 | + $form_data = $request->get_params(); | |
| 129 | + $files = $request->get_file_params(); | |
| 130 | + $settings = get_transient('ui_e_form_widget_settings_' . $form_data['widget_id']); | |
| 131 | + | |
| 132 | + // Request the contact form service and return the response | |
| 133 | + $service = new Contact_Form_Service($form_data, $settings, $files); | |
| 134 | + $response = $service->handle(); | |
| 135 | + return $response; | |
| 136 | + } | |
| 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 | +} | |