| 1 |
<?php |
| 2 |
|
| 3 |
namespace Getwid; |
| 4 |
|
| 5 |
/** |
| 6 |
* Class REST API |
| 7 |
* @package Getwid |
| 8 |
*/ |
| 9 |
class RestAPI { |
| 10 |
|
| 11 |
protected $_namespace = 'getwid/v1'; |
| 12 |
|
| 13 |
protected $remote_template_library_url; |
| 14 |
|
| 15 |
/** |
| 16 |
* RestAPI constructor. |
| 17 |
*/ |
| 18 |
public function __construct( ) { |
| 19 |
|
| 20 |
$this->remote_template_library_url = defined( 'GETWID_REMOTE_TEMPLATE_LIBRARY_URL' ) ? |
| 21 |
GETWID_REMOTE_TEMPLATE_LIBRARY_URL : 'https://cgw.motopress.com'; |
| 22 |
|
| 23 |
add_action( 'rest_api_init', [ $this, 'register_rest_route' ] ); |
| 24 |
|
| 25 |
} |
| 26 |
|
| 27 |
public function register_rest_route(){ |
| 28 |
|
| 29 |
register_rest_route( $this->_namespace, '/get_remote_templates', array( |
| 30 |
array( |
| 31 |
'methods' => 'GET', |
| 32 |
'callback' => [ $this, 'get_remote_templates' ], |
| 33 |
'permission_callback' => [ $this, 'permissions_check' ], |
| 34 |
) |
| 35 |
) ); |
| 36 |
|
| 37 |
register_rest_route( $this->_namespace, '/get_remote_content', array( |
| 38 |
array( |
| 39 |
'methods' => 'GET', |
| 40 |
'callback' => [ $this, 'get_remote_content' ], |
| 41 |
'permission_callback' => [ $this, 'permissions_check' ], |
| 42 |
) |
| 43 |
) ); |
| 44 |
|
| 45 |
register_rest_route( $this->_namespace, '/taxonomies', array( |
| 46 |
array( |
| 47 |
'methods' => 'GET', |
| 48 |
'callback' => [ $this, 'get_taxonomies' ], |
| 49 |
'permission_callback' => [ $this, 'permissions_check' ], |
| 50 |
), |
| 51 |
'schema' => array( $this, 'taxonomy_schema' ) |
| 52 |
) ); |
| 53 |
|
| 54 |
register_rest_route( $this->_namespace, '/terms', array( |
| 55 |
array( |
| 56 |
'methods' => 'GET', |
| 57 |
'callback' => [ $this, 'get_terms' ], |
| 58 |
'permission_callback' => [ $this, 'permissions_check' ], |
| 59 |
), |
| 60 |
'schema' => array( $this, 'terms_schema' ) |
| 61 |
) ); |
| 62 |
|
| 63 |
register_rest_route( $this->_namespace, '/templates', array( |
| 64 |
array( |
| 65 |
'methods' => 'GET', |
| 66 |
'callback' => [ $this, 'get_templates' ], |
| 67 |
'permission_callback' => [ $this, 'permissions_check' ], |
| 68 |
), |
| 69 |
'schema' => array( $this, 'templates_schema' ) |
| 70 |
) ); |
| 71 |
} |
| 72 |
|
| 73 |
public function permissions_check( $request ) { |
| 74 |
if ( ! current_user_can( 'edit_posts' ) ) { |
| 75 |
return new \WP_Error( |
| 76 |
'rest_forbidden', |
| 77 |
esc_html__( 'Forbidden.' ), |
| 78 |
array( 'status' => $this->authorization_status_code() ) |
| 79 |
); |
| 80 |
} |
| 81 |
|
| 82 |
return true; |
| 83 |
} |
| 84 |
|
| 85 |
// Sets up the proper HTTP status code for authorization. |
| 86 |
public function authorization_status_code() { |
| 87 |
|
| 88 |
$status = 401; |
| 89 |
|
| 90 |
if ( is_user_logged_in() ) { |
| 91 |
$status = 403; |
| 92 |
} |
| 93 |
|
| 94 |
return $status; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Schema for a taxonomy. |
| 99 |
* |
| 100 |
* @param WP_REST_Request $request Current request. |
| 101 |
*/ |
| 102 |
public function taxonomy_schema() { |
| 103 |
$schema = array( |
| 104 |
'$schema' => 'http://json-schema.org/draft-04/schema#', |
| 105 |
'title' => 'taxonomy', |
| 106 |
'type' => 'object', |
| 107 |
'properties' => array( |
| 108 |
'post_type_name' => array( |
| 109 |
'description' => esc_html__( 'Post Type' ), |
| 110 |
'type' => 'string', |
| 111 |
'context' => array( 'view', 'edit', 'embed' ), |
| 112 |
'readonly' => true, |
| 113 |
) |
| 114 |
) |
| 115 |
); |
| 116 |
|
| 117 |
return $schema; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Schema for a terms. |
| 122 |
* |
| 123 |
* @param WP_REST_Request $request Current request. |
| 124 |
*/ |
| 125 |
public function terms_schema() { |
| 126 |
$schema = array( |
| 127 |
'$schema' => 'http://json-schema.org/draft-04/schema#', |
| 128 |
'title' => 'terms', |
| 129 |
'type' => 'object', |
| 130 |
'properties' => array( |
| 131 |
'taxonomy_name' => array( |
| 132 |
'description' => esc_html__( 'Taxonomy' ), |
| 133 |
'type' => 'string', |
| 134 |
'context' => array( 'view', 'edit', 'embed' ), |
| 135 |
'readonly' => true, |
| 136 |
) |
| 137 |
) |
| 138 |
); |
| 139 |
|
| 140 |
return $schema; |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Schema for a templates. |
| 145 |
* |
| 146 |
* @param WP_REST_Request $request Current request. |
| 147 |
*/ |
| 148 |
public function templates_schema() { |
| 149 |
$schema = array( |
| 150 |
'$schema' => 'http://json-schema.org/draft-04/schema#', |
| 151 |
'title' => 'templates', |
| 152 |
'type' => 'object', |
| 153 |
'properties' => array( |
| 154 |
'post_type_name' => array( |
| 155 |
'description' => esc_html__( 'Templates' ), |
| 156 |
'type' => 'string', |
| 157 |
'context' => array( 'view', 'edit', 'embed' ), |
| 158 |
'readonly' => true, |
| 159 |
) |
| 160 |
) |
| 161 |
); |
| 162 |
|
| 163 |
return $schema; |
| 164 |
} |
| 165 |
|
| 166 |
public function get_remote_templates() { |
| 167 |
|
| 168 |
$cache = isset( $_GET['cache'] ) ? sanitize_text_field( wp_unslash( $_GET['cache'] ) ) : 'refresh'; |
| 169 |
$templates_data = []; |
| 170 |
|
| 171 |
if ($cache == 'cache'){ |
| 172 |
$templates_data = get_transient( 'getwid_templates_response_data' ); //Get Cache response |
| 173 |
} elseif ($cache == 'refresh') { |
| 174 |
delete_transient( 'getwid_templates_response_data' ); //Delete cache data |
| 175 |
} |
| 176 |
|
| 177 |
if ( $templates_data == false || empty( $templates_data ) ) { |
| 178 |
|
| 179 |
//Get Templates from remote server |
| 180 |
$response = wp_remote_get( |
| 181 |
$this->remote_template_library_url . "/wp-json/getwid-templates-server/v1/get_templates", |
| 182 |
array( |
| 183 |
'timeout' => 15, |
| 184 |
) |
| 185 |
); |
| 186 |
// var_dump( $response ); exit(); |
| 187 |
|
| 188 |
if ( is_wp_error( $response ) ) { |
| 189 |
return '<p>' . $response->get_error_message() . '</p>'; |
| 190 |
} else { |
| 191 |
|
| 192 |
$templates_data = json_decode( wp_remote_retrieve_body( $response ), false ); |
| 193 |
|
| 194 |
//JSON valid |
| 195 |
if ( json_last_error() === JSON_ERROR_NONE && $templates_data ) { |
| 196 |
|
| 197 |
set_transient( 'getwid_templates_response_data', $templates_data, 24 * HOUR_IN_SECONDS ); //Cache response |
| 198 |
return $templates_data; |
| 199 |
|
| 200 |
} else { |
| 201 |
return __( 'Error in json_decode.', 'getwid' ); |
| 202 |
} |
| 203 |
} |
| 204 |
} else { |
| 205 |
return $templates_data; |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
public function get_remote_content() { |
| 210 |
|
| 211 |
$get_content_url = isset( $_GET['get_content_url'] ) ? esc_url_raw( $_GET['get_content_url'] ) : false; |
| 212 |
|
| 213 |
if ( ! $this->content_url_is_valid( $get_content_url ) ) { |
| 214 |
return __( 'Please provide a valid URL.', 'getwid' ); |
| 215 |
} |
| 216 |
|
| 217 |
//Get Templates from remote server |
| 218 |
$response = wp_remote_get( |
| 219 |
$get_content_url, |
| 220 |
array( |
| 221 |
'timeout' => 15, |
| 222 |
) |
| 223 |
); |
| 224 |
|
| 225 |
$templates_data = json_decode( wp_remote_retrieve_body( $response ) ); |
| 226 |
|
| 227 |
//JSON valid |
| 228 |
if ( json_last_error() === JSON_ERROR_NONE ) { |
| 229 |
return $templates_data; |
| 230 |
} else { |
| 231 |
return __( 'Error in json_decode.', 'getwid' ); |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
public function get_taxonomies($object) { |
| 236 |
$post_type_name = sanitize_text_field( wp_unslash( $_GET['post_type_name'] ) ); |
| 237 |
$taxonomies = get_object_taxonomies( $post_type_name, 'objects' ); |
| 238 |
|
| 239 |
$return = []; |
| 240 |
if (!empty($taxonomies)){ |
| 241 |
foreach ($taxonomies as $key => $taxonomy_name) { |
| 242 |
$return[] = array( |
| 243 |
'value' => $key, |
| 244 |
'label' => $taxonomy_name->labels->name.' ('.$key.')' |
| 245 |
); |
| 246 |
} |
| 247 |
} |
| 248 |
return $return; |
| 249 |
} |
| 250 |
|
| 251 |
public function get_terms($object) { |
| 252 |
$taxonomy_name = getwid_recursive_sanitize_array( wp_unslash( $_GET['taxonomy_name'] ) ); |
| 253 |
|
| 254 |
$return = []; |
| 255 |
$terms = get_terms(array( |
| 256 |
'taxonomy' => $taxonomy_name, |
| 257 |
'hide_empty' => false, |
| 258 |
)); |
| 259 |
|
| 260 |
if (!empty($terms)){ |
| 261 |
foreach ($terms as $key => $term_name) { |
| 262 |
$taxonomy_obj = get_taxonomy( $term_name->taxonomy ); |
| 263 |
|
| 264 |
$return[$term_name->taxonomy]['group_name'] = $taxonomy_obj->label; |
| 265 |
$return[$term_name->taxonomy]['group_value'][] = array( |
| 266 |
'value' => $term_name->taxonomy.'['.$term_name->term_id.']', |
| 267 |
'label' => $term_name->name, |
| 268 |
); |
| 269 |
} |
| 270 |
} |
| 271 |
return $return; |
| 272 |
} |
| 273 |
|
| 274 |
public function get_templates($object) { |
| 275 |
$template_name = sanitize_text_field( wp_unslash( $_GET['template_name'] ) ); |
| 276 |
|
| 277 |
$posts = get_posts( array( |
| 278 |
'numberposts' => -1, |
| 279 |
'orderby' => 'date', |
| 280 |
'order' => 'DESC', |
| 281 |
'post_type' => $template_name, |
| 282 |
) ); |
| 283 |
|
| 284 |
$return = []; |
| 285 |
if (!empty($posts)){ |
| 286 |
foreach ($posts as $key => $post) { |
| 287 |
$return[] = array( |
| 288 |
'value' => $post->ID, |
| 289 |
'label' => $post->post_title |
| 290 |
); |
| 291 |
} |
| 292 |
} |
| 293 |
return $return; |
| 294 |
} |
| 295 |
|
| 296 |
private function content_url_is_valid( $url ) { |
| 297 |
|
| 298 |
$parsed_url = wp_parse_url( $url ); |
| 299 |
|
| 300 |
if ( ! $parsed_url ) { |
| 301 |
return false; |
| 302 |
} |
| 303 |
|
| 304 |
$valid_hosts = array( 'elements.getwid.getmotopress.com' ); |
| 305 |
$valid_path = '/wp-json/getwid-templates-server/v1/get_content'; |
| 306 |
|
| 307 |
$remote_library_url = wp_parse_url( $this->remote_template_library_url ); |
| 308 |
|
| 309 |
if ( $remote_library_url && isset( $remote_library_url['host'] ) ) { |
| 310 |
$valid_hosts[] = $remote_library_url['host']; |
| 311 |
} |
| 312 |
|
| 313 |
$host_is_valid = false; |
| 314 |
$path_is_valid = false; |
| 315 |
|
| 316 |
if ( isset( $parsed_url['host'] ) && in_array( $parsed_url['host'], $valid_hosts ) ) { |
| 317 |
$host_is_valid = true; |
| 318 |
} |
| 319 |
|
| 320 |
if ( isset( $parsed_url['path'] ) && $valid_path == $parsed_url['path'] ) { |
| 321 |
$path_is_valid = true; |
| 322 |
} |
| 323 |
|
| 324 |
return $host_is_valid && $path_is_valid; |
| 325 |
} |
| 326 |
} |
| 327 |
|