| 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 |
} |
| 165 |
|