PluginProbe
WP Ultimate Post Grid / 4.1.1
WP Ultimate Post Grid v4.1.1
4.1.1 trunk 1.5 1.6 1.7 1.7.1 1.7.2 1.8 1.9 2.0 2.1 2.2 2.3 2.4.0 2.5.0 2.6.0 2.7.0 2.8.0 2.8.2 3.0.0 3.3.0 3.4.0 3.5.0 3.6.0 3.7.0 All 32 releases
wp-ultimate-post-grid / includes / public / api / class-wpupg-api-preview-grid.php

class-wpupg-api-preview-grid.php in WP Ultimate Post Grid 4.1.1, at includes/public/api/class-wpupg-api-preview-grid.php

81 lines 1.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * API for previewing a grid.
4 *
5 * @link https://bootstrapped.ventures
6 * @since 3.0.0
7 *
8 * @package WP_Ultimate_Post_Grid
9 * @subpackage WP_Ultimate_Post_Grid/includes/public/api
10 */
11
12 /**
13 * API for previewing a grid.
14 *
15 * @since 3.0.0
16 * @package WP_Ultimate_Post_Grid
17 * @subpackage WP_Ultimate_Post_Grid/includes/public/api
18 * @author Brecht Vandersmissen <brecht@bootstrapped.ventures>
19 */
20 class WPUPG_API_Preview_Grid {
21
22 /**
23 * Register actions and filters.
24 *
25 * @since 3.0.0
26 */
27 public static function init() {
28 add_action( 'rest_api_init', array( __CLASS__, 'api_register_data' ) );
29 }
30
31 /**
32 * Register data for the REST API.
33 *
34 * @since 3.0.0
35 */
36 public static function api_register_data() {
37 if ( function_exists( 'register_rest_field' ) ) {
38 register_rest_route( 'wp-ultimate-post-grid/v1', '/preview', array(
39 'callback' => array( __CLASS__, 'api_preview_grid' ),
40 'methods' => 'POST',
41 'permission_callback' => array( __CLASS__, 'api_required_permissions' ),
42 ) );
43 }
44 }
45
46 /**
47 * Required permissions for the API.
48 *
49 * @since 3.0.0
50 */
51 public static function api_required_permissions() {
52 return current_user_can( WPUPG_Settings::get( 'features_manage_access' ) );
53 }
54
55 /**
56 * Handle preview grid call to the REST API.
57 *
58 * @since 3.0.0
59 * @param WP_REST_Request $request Current request.
60 */
61 public static function api_preview_grid( $request ) {
62 // Parameters.
63 $params = $request->get_params();
64 $grid_meta = isset( $params['grid'] ) ? $params['grid'] : array();
65
66 // When previewing, grid is using the latest arguments.
67 $grid_meta['version'] = WPUPG_VERSION;
68 $grid = new WPUPG_Grid( $grid_meta );
69
70 $grid_args = $grid->get_javascript_args();
71 $grid_args['is_preview'] = true;
72
73 return array(
74 'html' => WPUPG_Grid_Output::entire( $grid, array( 'preview' => true ) ),
75 'args' => $grid_args,
76 );
77 }
78 }
79
80 WPUPG_API_Preview_Grid::init();
81