| 1 |
<?php |
| 2 |
namespace UiCoreElements; |
| 3 |
use \Elementor\Plugin; |
| 4 |
|
| 5 |
/** |
| 6 |
* REST_API Handler |
| 7 |
*/ |
| 8 |
class REST_API { |
| 9 |
|
| 10 |
public function __construct() { |
| 11 |
add_action('rest_api_init', [$this, 'register_routes']); |
| 12 |
} |
| 13 |
|
| 14 |
public function register_routes(){ |
| 15 |
register_rest_route('uielem/v1', '/load_more/', [ |
| 16 |
'methods' => 'GET', |
| 17 |
'show_in_index' => true, |
| 18 |
'callback' => [$this, 'settings_update'], |
| 19 |
'permission_callback' => '__return_true', |
| 20 |
'args' => [ |
| 21 |
'widget_id' => [ |
| 22 |
'required' => true, |
| 23 |
], |
| 24 |
'widget_type' => [ |
| 25 |
'required' => true, |
| 26 |
], |
| 27 |
'page' => [ |
| 28 |
'required' => false, |
| 29 |
'default' => 1, |
| 30 |
], |
| 31 |
'type' => [ |
| 32 |
'required' => false, |
| 33 |
'default' => '', |
| 34 |
], |
| 35 |
'term' => [ |
| 36 |
'required' => false, |
| 37 |
'default' => '', |
| 38 |
], |
| 39 |
], |
| 40 |
]); |
| 41 |
} |
| 42 |
|
| 43 |
public function check_for_permission() |
| 44 |
{ |
| 45 |
return current_user_can('manage_options'); |
| 46 |
} |
| 47 |
|
| 48 |
public function settings_update(\WP_REST_Request $request) { |
| 49 |
// Identify the widget |
| 50 |
$widget_id = $request->get_param('widget_id'); |
| 51 |
$widget_type = $request->get_param('widget_type'); |
| 52 |
|
| 53 |
// Get widget settings |
| 54 |
$settings = get_transient('ui_elements_widgetdata_'.$widget_id); |
| 55 |
|
| 56 |
$settings['__current_page'] = $request->get_param('page'); |
| 57 |
|
| 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'); |
| 60 |
|
| 61 |
// Create the element data |
| 62 |
$widget = [ |
| 63 |
'elType' => 'widget', |
| 64 |
'widgetType' => $widget_type, |
| 65 |
]; |
| 66 |
|
| 67 |
// Generate a new instance of the widget with those settings and return the markup |
| 68 |
//$widget = new AdvancedPostGrid($widget, $settings); todo: discover why this method don't work |
| 69 |
$widget = Plugin::instance()->elements_manager->create_element_instance($widget, $settings); |
| 70 |
$widget->set_settings($settings); |
| 71 |
|
| 72 |
$markup = $widget->render_ajax(); |
| 73 |
|
| 74 |
return [ |
| 75 |
'markup' => $markup, |
| 76 |
'total_pages' => $widget->get_query()->max_num_pages, |
| 77 |
]; |
| 78 |
} |
| 79 |
} |