| 1 |
<?php |
| 2 |
/** |
| 3 |
* Open up grid templates in the WordPress REST API. |
| 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 |
* Open up grid templates in the WordPress REST API. |
| 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_Templates { |
| 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' ) ) { // Prevent issue with Jetpack. |
| 38 |
register_rest_route( 'wp-ultimate-post-grid/v1', '/template', array( |
| 39 |
'callback' => array( __CLASS__, 'api_get_templates' ), |
| 40 |
'methods' => 'GET', |
| 41 |
'permission_callback' => array( __CLASS__, 'api_required_permissions' ), |
| 42 |
)); |
| 43 |
register_rest_route( 'wp-ultimate-post-grid/v1', '/template', array( |
| 44 |
'callback' => array( __CLASS__, 'api_update_template' ), |
| 45 |
'methods' => 'POST', |
| 46 |
'permission_callback' => array( __CLASS__, 'api_required_permissions' ), |
| 47 |
)); |
| 48 |
register_rest_route( 'wp-ultimate-post-grid/v1', '/template', array( |
| 49 |
'callback' => array( __CLASS__, 'api_delete_template' ), |
| 50 |
'methods' => 'DELETE', |
| 51 |
'permission_callback' => array( __CLASS__, 'api_required_permissions' ), |
| 52 |
)); |
| 53 |
register_rest_route( 'wp-ultimate-post-grid/v1', '/template/preview', array( |
| 54 |
'callback' => array( __CLASS__, 'api_preview_template' ), |
| 55 |
'methods' => 'POST', |
| 56 |
'permission_callback' => array( __CLASS__, 'api_required_permissions' ), |
| 57 |
)); |
| 58 |
register_rest_route( 'wp-ultimate-post-grid/v1', '/template/preview-item', array( |
| 59 |
'callback' => array( __CLASS__, 'api_preview_template_items' ), |
| 60 |
'methods' => 'POST', |
| 61 |
'permission_callback' => array( __CLASS__, 'api_required_permissions' ), |
| 62 |
)); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Required permissions for the API. |
| 68 |
* |
| 69 |
* @since 3.0.0 |
| 70 |
*/ |
| 71 |
public static function api_required_permissions() { |
| 72 |
return current_user_can( 'manage_options' ); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Handle get template call to the REST API. |
| 77 |
* |
| 78 |
* @since 3.0.0 |
| 79 |
* @param WP_REST_Request $request Current request. |
| 80 |
*/ |
| 81 |
public static function api_get_templates( $request ) { |
| 82 |
return WPUPG_Template_Manager::get_templates(); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Handle update template call to the REST API. |
| 87 |
* |
| 88 |
* @since 3.0.0 |
| 89 |
* @param WP_REST_Request $request Current request. |
| 90 |
*/ |
| 91 |
public static function api_update_template( $request ) { |
| 92 |
$params = $request->get_params(); |
| 93 |
$template = isset( $params['template'] ) ? $params['template'] : array(); |
| 94 |
return WPUPG_Template_Editor::prepare_template_for_editor( WPUPG_Template_Manager::save_template( $template ) ); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Handle delete template call to the REST API. |
| 99 |
* |
| 100 |
* @since 3.0.0 |
| 101 |
* @param WP_REST_Request $request Current request. |
| 102 |
*/ |
| 103 |
public static function api_delete_template( $request ) { |
| 104 |
$params = $request->get_params(); |
| 105 |
$slug = isset( $params['slug'] ) ? $params['slug'] : false; |
| 106 |
return WPUPG_Template_Manager::delete_template( $slug ); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Handle preview template call to the REST API. |
| 111 |
* |
| 112 |
* @since 4.0.3 |
| 113 |
* @param WP_REST_Request $request Current request. |
| 114 |
*/ |
| 115 |
public static function api_preview_template( $request ) { |
| 116 |
$params = $request->get_params(); |
| 117 |
$item_id = isset( $params['item'] ) ? $params['item'] : false; |
| 118 |
$shortcodes = isset( $params['shortcodes'] ) ? (array) $params['shortcodes'] : array(); |
| 119 |
|
| 120 |
$preview = array(); |
| 121 |
$item = WPUPG_Item_Manager::get_item( $item_id ); |
| 122 |
|
| 123 |
if ( $item ) { |
| 124 |
WPUPG_Template_Shortcodes::set_current_item( $item ); |
| 125 |
|
| 126 |
foreach ( $shortcodes as $uid => $shortcode ) { |
| 127 |
$preview[ $uid ] = do_shortcode( $shortcode ); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
return array( |
| 132 |
'preview' => (object) $preview, |
| 133 |
); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Handle preview template items call to the REST API. |
| 138 |
* |
| 139 |
* @since 4.0.3 |
| 140 |
* @param WP_REST_Request $request Current request. |
| 141 |
*/ |
| 142 |
public static function api_preview_template_items( $request ) { |
| 143 |
$params = $request->get_params(); |
| 144 |
$input = isset( $params['input'] ) ? $params['input'] : false; |
| 145 |
|
| 146 |
$items = array(); |
| 147 |
|
| 148 |
if ( $input ) { |
| 149 |
$post_types = get_post_types(); |
| 150 |
|
| 151 |
unset( $post_types[WPUPG_POST_TYPE] ); |
| 152 |
unset( $post_types['revision'] ); |
| 153 |
unset( $post_types['nav_menu_item'] ); |
| 154 |
|
| 155 |
$args = array( |
| 156 |
'post_type' => $post_types, |
| 157 |
'post_status' => 'any', |
| 158 |
'posts_per_page' => 20, |
| 159 |
'ignore_sticky_posts' => true, |
| 160 |
's' => $input, |
| 161 |
); |
| 162 |
|
| 163 |
$query = new WP_Query( $args ); |
| 164 |
$posts = $query->have_posts() ? $query->posts : array(); |
| 165 |
|
| 166 |
foreach ( $posts as $post ) { |
| 167 |
$item = WPUPG_Item_Manager::get_item( $post->ID ); |
| 168 |
$post_type = get_post_type_object( $post->post_type ); |
| 169 |
|
| 170 |
$items[] = array( |
| 171 |
'value' => $post->ID, |
| 172 |
'label' => $post_type->labels->singular_name . ' - ' . $post->ID . ' - ' . $post->post_title, |
| 173 |
'classes' => $item->classes(), |
| 174 |
); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
return $items; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
WPUPG_Api_Templates::init(); |
| 183 |
|