| @@ -1,22 +1,29 @@ | ||
| 1 | 1 | <?php |
| 2 | + | |
| 2 | 3 | namespace UiCoreElements; |
| 4 | + | |
| 3 | 5 | use \Elementor\Plugin; |
| 6 | +use UiCoreElements\Helper; | |
| 7 | +use UiCoreElements\Utils\Contact_Form_Service; | |
| 4 | 8 | |
| 5 | 9 | /** |
| 6 | 10 | * REST_API Handler |
| 7 | 11 | */ |
| 8 | -class REST_API { | |
| 12 | +class REST_API | |
| 13 | +{ | |
| 9 | 14 | |
| 10 | - public function __construct() { | |
| 15 | + public function __construct() | |
| 16 | + { | |
| 11 | 17 | add_action('rest_api_init', [$this, 'register_routes']); |
| 12 | 18 | } |
| 13 | 19 | |
| 14 | - public function register_routes(){ | |
| 20 | + public function register_routes() | |
| 21 | + { | |
| 15 | 22 | register_rest_route('uielem/v1', '/load_more/', [ |
| 16 | 23 | 'methods' => 'GET', |
| 17 | 24 | 'show_in_index' => true, |
| 18 | - 'callback' => [$this, 'settings_update'], | |
| 25 | + 'callback' => [$this, 'apg_load_more'], | |
| 19 | 26 | 'permission_callback' => '__return_true', |
| 20 | 27 | 'args' => [ |
| 21 | 28 | 'widget_id' => [ |
| 22 | 29 | 'required' => true, |
| @@ -37,8 +44,24 @@ | ||
| 37 | 44 | 'default' => '', |
| 38 | 45 | ], |
| 39 | 46 | ], |
| 40 | 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 | + ]); | |
| 41 | 64 | } |
| 42 | 65 | |
| 43 | 66 | public function check_for_permission() |
| 44 | 67 | { |
| @@ -44,25 +67,46 @@ | ||
| 44 | 67 | { |
| 45 | 68 | return current_user_can('manage_options'); |
| 46 | 69 | } |
| 47 | 70 | |
| 48 | - public function settings_update(\WP_REST_Request $request) { | |
| 71 | + public function apg_load_more(\WP_REST_Request $request) | |
| 72 | + { | |
| 73 | + $current_query = false; | |
| 74 | + | |
| 49 | 75 | // Identify the widget |
| 50 | 76 | $widget_id = $request->get_param('widget_id'); |
| 51 | 77 | $widget_type = $request->get_param('widget_type'); |
| 78 | + $is_product = strpos($widget_type, 'product') !== false; | |
| 52 | 79 | |
| 53 | 80 | // Get widget settings |
| 54 | - $settings = get_transient('ui_elements_widgetdata_'.$widget_id); | |
| 81 | + $settings = get_transient('ui_elements_widgetdata_' . $widget_id); | |
| 55 | 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'); | |
| 56 | 89 | $settings['__current_page'] = $request->get_param('page'); |
| 90 | + $settings['__current_post_id'] = $request->get_param('current_post_id'); | |
| 57 | 91 | |
| 58 | - $tax = 'posts-filter_' . $request->get_param('type') . '_ids'; // todo: remove hard coded posts-filter_ and get the value set on widget | |
| 59 | - $settings[$tax] = $request->get_param('term'); | |
| 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 | + } | |
| 60 | 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 | + | |
| 61 | 104 | // Create the element data |
| 62 | 105 | $widget = [ |
| 63 | 106 | 'elType' => 'widget', |
| 64 | 107 | 'widgetType' => $widget_type, |
| 108 | + 'id' => $widget_id, | |
| 65 | 109 | ]; |
| 66 | 110 | |
| 67 | 111 | // Generate a new instance of the widget with those settings and return the markup |
| 68 | 112 | //$widget = new AdvancedPostGrid($widget, $settings); todo: discover why this method don't work |
| @@ -68,12 +112,53 @@ | ||
| 68 | 112 | //$widget = new AdvancedPostGrid($widget, $settings); todo: discover why this method don't work |
| 69 | 113 | $widget = Plugin::instance()->elements_manager->create_element_instance($widget, $settings); |
| 70 | 114 | $widget->set_settings($settings); |
| 71 | 115 | |
| 72 | - $markup = $widget->render_ajax(); | |
| 116 | + $data = $widget->render_ajax($current_query); | |
| 73 | 117 | |
| 74 | 118 | return [ |
| 75 | - 'markup' => $markup, | |
| 76 | - 'total_pages' => $widget->get_query()->max_num_pages, | |
| 119 | + 'markup' => $data['markup'], | |
| 120 | + 'total_pages' => $is_product ? $data['total_pages'] : $widget->get_query()->max_num_pages, | |
| 77 | 121 | ]; |
| 78 | 122 | } |
| 79 | -} | |
| 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 | +} | |