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