| @@ -1,374 +1,374 @@ | ||
| 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 | - register_rest_route( $this->_namespace, '/get_meta_keys', array( | |
| 73 | - array( | |
| 74 | - 'methods' => 'POST', | |
| 75 | - 'callback' => [ $this, 'get_meta_keys' ], | |
| 76 | - 'permission_callback' => [ $this, 'permissions_check' ], | |
| 77 | - ) | |
| 78 | - ) ); | |
| 79 | - } | |
| 80 | - | |
| 81 | - public function permissions_check( $request ) { | |
| 82 | - if ( ! current_user_can( 'edit_posts' ) ) { | |
| 83 | - return new \WP_Error( | |
| 84 | - 'rest_forbidden', | |
| 85 | - esc_html__( 'Forbidden.' ), | |
| 86 | - array( 'status' => $this->authorization_status_code() ) | |
| 87 | - ); | |
| 88 | - } | |
| 89 | - | |
| 90 | - return true; | |
| 91 | - } | |
| 92 | - | |
| 93 | - // Sets up the proper HTTP status code for authorization. | |
| 94 | - public function authorization_status_code() { | |
| 95 | - | |
| 96 | - $status = 401; | |
| 97 | - | |
| 98 | - if ( is_user_logged_in() ) { | |
| 99 | - $status = 403; | |
| 100 | - } | |
| 101 | - | |
| 102 | - return $status; | |
| 103 | - } | |
| 104 | - | |
| 105 | - /** | |
| 106 | - * Schema for a taxonomy. | |
| 107 | - * | |
| 108 | - * @param WP_REST_Request $request Current request. | |
| 109 | - */ | |
| 110 | - public function taxonomy_schema() { | |
| 111 | - $schema = array( | |
| 112 | - '$schema' => 'http://json-schema.org/draft-04/schema#', | |
| 113 | - 'title' => 'taxonomy', | |
| 114 | - 'type' => 'object', | |
| 115 | - 'properties' => array( | |
| 116 | - 'post_type_name' => array( | |
| 117 | - 'description' => esc_html__( 'Post Type' ), | |
| 118 | - 'type' => 'string', | |
| 119 | - 'context' => array( 'view', 'edit', 'embed' ), | |
| 120 | - 'readonly' => true, | |
| 121 | - ) | |
| 122 | - ) | |
| 123 | - ); | |
| 124 | - | |
| 125 | - return $schema; | |
| 126 | - } | |
| 127 | - | |
| 128 | - /** | |
| 129 | - * Schema for a terms. | |
| 130 | - * | |
| 131 | - * @param WP_REST_Request $request Current request. | |
| 132 | - */ | |
| 133 | - public function terms_schema() { | |
| 134 | - $schema = array( | |
| 135 | - '$schema' => 'http://json-schema.org/draft-04/schema#', | |
| 136 | - 'title' => 'terms', | |
| 137 | - 'type' => 'object', | |
| 138 | - 'properties' => array( | |
| 139 | - 'taxonomy_name' => array( | |
| 140 | - 'description' => esc_html__( 'Taxonomy' ), | |
| 141 | - 'type' => 'string', | |
| 142 | - 'context' => array( 'view', 'edit', 'embed' ), | |
| 143 | - 'readonly' => true, | |
| 144 | - ) | |
| 145 | - ) | |
| 146 | - ); | |
| 147 | - | |
| 148 | - return $schema; | |
| 149 | - } | |
| 150 | - | |
| 151 | - /** | |
| 152 | - * Schema for a templates. | |
| 153 | - * | |
| 154 | - * @param WP_REST_Request $request Current request. | |
| 155 | - */ | |
| 156 | - public function templates_schema() { | |
| 157 | - $schema = array( | |
| 158 | - '$schema' => 'http://json-schema.org/draft-04/schema#', | |
| 159 | - 'title' => 'templates', | |
| 160 | - 'type' => 'object', | |
| 161 | - 'properties' => array( | |
| 162 | - 'post_type_name' => array( | |
| 163 | - 'description' => esc_html__( 'Templates' ), | |
| 164 | - 'type' => 'string', | |
| 165 | - 'context' => array( 'view', 'edit', 'embed' ), | |
| 166 | - 'readonly' => true, | |
| 167 | - ) | |
| 168 | - ) | |
| 169 | - ); | |
| 170 | - | |
| 171 | - return $schema; | |
| 172 | - } | |
| 173 | - | |
| 174 | - public function get_remote_templates() { | |
| 175 | - | |
| 176 | - $cache = isset( $_GET['cache'] ) ? sanitize_text_field( wp_unslash( $_GET['cache'] ) ) : 'refresh'; | |
| 177 | - $templates_data = []; | |
| 178 | - | |
| 179 | - if ($cache == 'cache'){ | |
| 180 | - $templates_data = get_transient( 'getwid_templates_response_data' ); //Get Cache response | |
| 181 | - } elseif ($cache == 'refresh') { | |
| 182 | - delete_transient( 'getwid_templates_response_data' ); //Delete cache data | |
| 183 | - } | |
| 184 | - | |
| 185 | - if ( $templates_data == false || empty( $templates_data ) ) { | |
| 186 | - | |
| 187 | - //Get Templates from remote server | |
| 188 | - $response = wp_remote_get( | |
| 189 | - $this->remote_template_library_url . "/wp-json/getwid-templates-server/v1/get_templates", | |
| 190 | - array( | |
| 191 | - 'timeout' => 15, | |
| 192 | - ) | |
| 193 | - ); | |
| 194 | - // var_dump( $response ); exit(); | |
| 195 | - | |
| 196 | - if ( is_wp_error( $response ) ) { | |
| 197 | - return '<p>' . $response->get_error_message() . '</p>'; | |
| 198 | - } else { | |
| 199 | - | |
| 200 | - $templates_data = json_decode( wp_remote_retrieve_body( $response ), false ); | |
| 201 | - | |
| 202 | - //JSON valid | |
| 203 | - if ( json_last_error() === JSON_ERROR_NONE && $templates_data ) { | |
| 204 | - | |
| 205 | - set_transient( 'getwid_templates_response_data', $templates_data, 24 * HOUR_IN_SECONDS ); //Cache response | |
| 206 | - return $templates_data; | |
| 207 | - | |
| 208 | - } else { | |
| 209 | - return __( 'Error in json_decode.', 'getwid' ); | |
| 210 | - } | |
| 211 | - } | |
| 212 | - } else { | |
| 213 | - return $templates_data; | |
| 214 | - } | |
| 215 | - } | |
| 216 | - | |
| 217 | - public function get_remote_content() { | |
| 218 | - | |
| 219 | - $get_content_url = isset( $_GET['get_content_url'] ) ? esc_url_raw( $_GET['get_content_url'] ) : false; | |
| 220 | - | |
| 221 | - if ( ! $this->content_url_is_valid( $get_content_url ) ) { | |
| 222 | - return __( 'Please provide a valid URL.', 'getwid' ); | |
| 223 | - } | |
| 224 | - | |
| 225 | - //Get Templates from remote server | |
| 226 | - $response = wp_remote_get( | |
| 227 | - $get_content_url, | |
| 228 | - array( | |
| 229 | - 'timeout' => 15, | |
| 230 | - ) | |
| 231 | - ); | |
| 232 | - | |
| 233 | - $templates_data = json_decode( wp_remote_retrieve_body( $response ) ); | |
| 234 | - | |
| 235 | - //JSON valid | |
| 236 | - if ( json_last_error() === JSON_ERROR_NONE ) { | |
| 237 | - return $templates_data; | |
| 238 | - } else { | |
| 239 | - return __( 'Error in json_decode.', 'getwid' ); | |
| 240 | - } | |
| 241 | - } | |
| 242 | - | |
| 243 | - public function get_taxonomies($object) { | |
| 244 | - $post_type_name = sanitize_text_field( wp_unslash( $_GET['post_type_name'] ) ); | |
| 245 | - $taxonomies = get_object_taxonomies( $post_type_name, 'objects' ); | |
| 246 | - | |
| 247 | - $return = []; | |
| 248 | - if (!empty($taxonomies)){ | |
| 249 | - foreach ($taxonomies as $key => $taxonomy_name) { | |
| 250 | - $return[] = array( | |
| 251 | - 'value' => $key, | |
| 252 | - 'label' => $taxonomy_name->labels->name.' ('.$key.')' | |
| 253 | - ); | |
| 254 | - } | |
| 255 | - } | |
| 256 | - return $return; | |
| 257 | - } | |
| 258 | - | |
| 259 | - public function get_terms($object) { | |
| 260 | - $taxonomy_name = getwid_recursive_sanitize_array( wp_unslash( $_GET['taxonomy_name'] ) ); | |
| 261 | - | |
| 262 | - $return = []; | |
| 263 | - $terms = get_terms(array( | |
| 264 | - 'taxonomy' => $taxonomy_name, | |
| 265 | - 'hide_empty' => false, | |
| 266 | - )); | |
| 267 | - | |
| 268 | - if (!empty($terms)){ | |
| 269 | - foreach ($terms as $key => $term_name) { | |
| 270 | - $taxonomy_obj = get_taxonomy( $term_name->taxonomy ); | |
| 271 | - | |
| 272 | - $return[$term_name->taxonomy]['group_name'] = $taxonomy_obj->label; | |
| 273 | - $return[$term_name->taxonomy]['group_value'][] = array( | |
| 274 | - 'value' => $term_name->taxonomy.'['.$term_name->term_id.']', | |
| 275 | - 'label' => $term_name->name, | |
| 276 | - ); | |
| 277 | - } | |
| 278 | - } | |
| 279 | - return $return; | |
| 280 | - } | |
| 281 | - | |
| 282 | - public function get_templates($object) { | |
| 283 | - $template_name = sanitize_text_field( wp_unslash( $_GET['template_name'] ) ); | |
| 284 | - | |
| 285 | - $posts = get_posts( array( | |
| 286 | - 'numberposts' => -1, | |
| 287 | - 'orderby' => 'date', | |
| 288 | - 'order' => 'DESC', | |
| 289 | - 'post_type' => $template_name, | |
| 290 | - ) ); | |
| 291 | - | |
| 292 | - $return = []; | |
| 293 | - if (!empty($posts)){ | |
| 294 | - foreach ($posts as $key => $post) { | |
| 295 | - $return[] = array( | |
| 296 | - 'value' => $post->ID, | |
| 297 | - 'label' => $post->post_title | |
| 298 | - ); | |
| 299 | - } | |
| 300 | - } | |
| 301 | - return $return; | |
| 302 | - } | |
| 303 | - | |
| 304 | - public function get_meta_keys( $request ) { | |
| 305 | - | |
| 306 | - $params = $request->get_params(); | |
| 307 | - unset( $params['metaQuery'] ); | |
| 308 | - | |
| 309 | - $args = getwid_build_custom_post_type_query( $params ); | |
| 310 | - $args['fields'] = 'ids'; | |
| 311 | - | |
| 312 | - if ( $args['posts_per_page'] > 100 ) { | |
| 313 | - $args['posts_per_page'] = 100; | |
| 314 | - } | |
| 315 | - | |
| 316 | - $posts = get_posts( $args ); | |
| 317 | - | |
| 318 | - $meta_keys_to_skip = [ '_oembed', '_edit_lock', '_edit_last', '_wp_old_slug', '_wxr' ]; | |
| 319 | - $meta_keys = []; | |
| 320 | - foreach ( $posts as $postID ) { | |
| 321 | - foreach ( get_post_meta( $postID, '', true ) as $meta_key => $value ) { | |
| 322 | - | |
| 323 | - if ( in_array( $meta_key, $meta_keys ) ) { | |
| 324 | - continue; | |
| 325 | - } | |
| 326 | - | |
| 327 | - $should_skip = false; | |
| 328 | - foreach( $meta_keys_to_skip as $skip ) { | |
| 329 | - if ( 0 === strpos( $meta_key, $skip ) ) { | |
| 330 | - $should_skip = true; | |
| 331 | - break; | |
| 332 | - } | |
| 333 | - } | |
| 334 | - | |
| 335 | - if ( ! $should_skip ) { | |
| 336 | - $meta_keys[] = $meta_key; | |
| 337 | - } | |
| 338 | - } | |
| 339 | - } | |
| 340 | - | |
| 341 | - return array_values( $meta_keys ); | |
| 342 | - } | |
| 343 | - | |
| 344 | - private function content_url_is_valid( $url ) { | |
| 345 | - | |
| 346 | - $parsed_url = wp_parse_url( $url ); | |
| 347 | - | |
| 348 | - if ( ! $parsed_url ) { | |
| 349 | - return false; | |
| 350 | - } | |
| 351 | - | |
| 352 | - $valid_hosts = array( 'elements.getwid.getmotopress.com' ); | |
| 353 | - $valid_path = '/wp-json/getwid-templates-server/v1/get_content'; | |
| 354 | - | |
| 355 | - $remote_library_url = wp_parse_url( $this->remote_template_library_url ); | |
| 356 | - | |
| 357 | - if ( $remote_library_url && isset( $remote_library_url['host'] ) ) { | |
| 358 | - $valid_hosts[] = $remote_library_url['host']; | |
| 359 | - } | |
| 360 | - | |
| 361 | - $host_is_valid = false; | |
| 362 | - $path_is_valid = false; | |
| 363 | - | |
| 364 | - if ( isset( $parsed_url['host'] ) && in_array( $parsed_url['host'], $valid_hosts ) ) { | |
| 365 | - $host_is_valid = true; | |
| 366 | - } | |
| 367 | - | |
| 368 | - if ( isset( $parsed_url['path'] ) && $valid_path == $parsed_url['path'] ) { | |
| 369 | - $path_is_valid = true; | |
| 370 | - } | |
| 371 | - | |
| 372 | - return $host_is_valid && $path_is_valid; | |
| 373 | - } | |
| 374 | -} | |
| 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 | + register_rest_route( $this->_namespace, '/get_meta_keys', array( | |
| 73 | + array( | |
| 74 | + 'methods' => 'POST', | |
| 75 | + 'callback' => [ $this, 'get_meta_keys' ], | |
| 76 | + 'permission_callback' => [ $this, 'permissions_check' ], | |
| 77 | + ) | |
| 78 | + ) ); | |
| 79 | + } | |
| 80 | + | |
| 81 | + public function permissions_check( $request ) { | |
| 82 | + if ( ! current_user_can( 'edit_posts' ) ) { | |
| 83 | + return new \WP_Error( | |
| 84 | + 'rest_forbidden', | |
| 85 | + esc_html__( 'Forbidden.' ), | |
| 86 | + array( 'status' => $this->authorization_status_code() ) | |
| 87 | + ); | |
| 88 | + } | |
| 89 | + | |
| 90 | + return true; | |
| 91 | + } | |
| 92 | + | |
| 93 | + // Sets up the proper HTTP status code for authorization. | |
| 94 | + public function authorization_status_code() { | |
| 95 | + | |
| 96 | + $status = 401; | |
| 97 | + | |
| 98 | + if ( is_user_logged_in() ) { | |
| 99 | + $status = 403; | |
| 100 | + } | |
| 101 | + | |
| 102 | + return $status; | |
| 103 | + } | |
| 104 | + | |
| 105 | + /** | |
| 106 | + * Schema for a taxonomy. | |
| 107 | + * | |
| 108 | + * @param WP_REST_Request $request Current request. | |
| 109 | + */ | |
| 110 | + public function taxonomy_schema() { | |
| 111 | + $schema = array( | |
| 112 | + '$schema' => 'http://json-schema.org/draft-04/schema#', | |
| 113 | + 'title' => 'taxonomy', | |
| 114 | + 'type' => 'object', | |
| 115 | + 'properties' => array( | |
| 116 | + 'post_type_name' => array( | |
| 117 | + 'description' => esc_html__( 'Post Type' ), | |
| 118 | + 'type' => 'string', | |
| 119 | + 'context' => array( 'view', 'edit', 'embed' ), | |
| 120 | + 'readonly' => true, | |
| 121 | + ) | |
| 122 | + ) | |
| 123 | + ); | |
| 124 | + | |
| 125 | + return $schema; | |
| 126 | + } | |
| 127 | + | |
| 128 | + /** | |
| 129 | + * Schema for a terms. | |
| 130 | + * | |
| 131 | + * @param WP_REST_Request $request Current request. | |
| 132 | + */ | |
| 133 | + public function terms_schema() { | |
| 134 | + $schema = array( | |
| 135 | + '$schema' => 'http://json-schema.org/draft-04/schema#', | |
| 136 | + 'title' => 'terms', | |
| 137 | + 'type' => 'object', | |
| 138 | + 'properties' => array( | |
| 139 | + 'taxonomy_name' => array( | |
| 140 | + 'description' => esc_html__( 'Taxonomy' ), | |
| 141 | + 'type' => 'string', | |
| 142 | + 'context' => array( 'view', 'edit', 'embed' ), | |
| 143 | + 'readonly' => true, | |
| 144 | + ) | |
| 145 | + ) | |
| 146 | + ); | |
| 147 | + | |
| 148 | + return $schema; | |
| 149 | + } | |
| 150 | + | |
| 151 | + /** | |
| 152 | + * Schema for a templates. | |
| 153 | + * | |
| 154 | + * @param WP_REST_Request $request Current request. | |
| 155 | + */ | |
| 156 | + public function templates_schema() { | |
| 157 | + $schema = array( | |
| 158 | + '$schema' => 'http://json-schema.org/draft-04/schema#', | |
| 159 | + 'title' => 'templates', | |
| 160 | + 'type' => 'object', | |
| 161 | + 'properties' => array( | |
| 162 | + 'post_type_name' => array( | |
| 163 | + 'description' => esc_html__( 'Templates' ), | |
| 164 | + 'type' => 'string', | |
| 165 | + 'context' => array( 'view', 'edit', 'embed' ), | |
| 166 | + 'readonly' => true, | |
| 167 | + ) | |
| 168 | + ) | |
| 169 | + ); | |
| 170 | + | |
| 171 | + return $schema; | |
| 172 | + } | |
| 173 | + | |
| 174 | + public function get_remote_templates() { | |
| 175 | + | |
| 176 | + $cache = isset( $_GET['cache'] ) ? sanitize_text_field( wp_unslash( $_GET['cache'] ) ) : 'refresh'; | |
| 177 | + $templates_data = []; | |
| 178 | + | |
| 179 | + if ($cache == 'cache'){ | |
| 180 | + $templates_data = get_transient( 'getwid_templates_response_data' ); //Get Cache response | |
| 181 | + } elseif ($cache == 'refresh') { | |
| 182 | + delete_transient( 'getwid_templates_response_data' ); //Delete cache data | |
| 183 | + } | |
| 184 | + | |
| 185 | + if ( $templates_data == false || empty( $templates_data ) ) { | |
| 186 | + | |
| 187 | + //Get Templates from remote server | |
| 188 | + $response = wp_remote_get( | |
| 189 | + $this->remote_template_library_url . "/wp-json/getwid-templates-server/v1/get_templates", | |
| 190 | + array( | |
| 191 | + 'timeout' => 15, | |
| 192 | + ) | |
| 193 | + ); | |
| 194 | + // var_dump( $response ); exit(); | |
| 195 | + | |
| 196 | + if ( is_wp_error( $response ) ) { | |
| 197 | + return '<p>' . $response->get_error_message() . '</p>'; | |
| 198 | + } else { | |
| 199 | + | |
| 200 | + $templates_data = json_decode( wp_remote_retrieve_body( $response ), false ); | |
| 201 | + | |
| 202 | + //JSON valid | |
| 203 | + if ( json_last_error() === JSON_ERROR_NONE && $templates_data ) { | |
| 204 | + | |
| 205 | + set_transient( 'getwid_templates_response_data', $templates_data, 24 * HOUR_IN_SECONDS ); //Cache response | |
| 206 | + return $templates_data; | |
| 207 | + | |
| 208 | + } else { | |
| 209 | + return __( 'Error in json_decode.', 'getwid' ); | |
| 210 | + } | |
| 211 | + } | |
| 212 | + } else { | |
| 213 | + return $templates_data; | |
| 214 | + } | |
| 215 | + } | |
| 216 | + | |
| 217 | + public function get_remote_content() { | |
| 218 | + | |
| 219 | + $get_content_url = isset( $_GET['get_content_url'] ) ? esc_url_raw( $_GET['get_content_url'] ) : false; | |
| 220 | + | |
| 221 | + if ( ! $this->content_url_is_valid( $get_content_url ) ) { | |
| 222 | + return __( 'Please provide a valid URL.', 'getwid' ); | |
| 223 | + } | |
| 224 | + | |
| 225 | + //Get Templates from remote server | |
| 226 | + $response = wp_remote_get( | |
| 227 | + $get_content_url, | |
| 228 | + array( | |
| 229 | + 'timeout' => 15, | |
| 230 | + ) | |
| 231 | + ); | |
| 232 | + | |
| 233 | + $templates_data = json_decode( wp_remote_retrieve_body( $response ) ); | |
| 234 | + | |
| 235 | + //JSON valid | |
| 236 | + if ( json_last_error() === JSON_ERROR_NONE ) { | |
| 237 | + return $templates_data; | |
| 238 | + } else { | |
| 239 | + return __( 'Error in json_decode.', 'getwid' ); | |
| 240 | + } | |
| 241 | + } | |
| 242 | + | |
| 243 | + public function get_taxonomies($object) { | |
| 244 | + $post_type_name = sanitize_text_field( wp_unslash( $_GET['post_type_name'] ) ); | |
| 245 | + $taxonomies = get_object_taxonomies( $post_type_name, 'objects' ); | |
| 246 | + | |
| 247 | + $return = []; | |
| 248 | + if (!empty($taxonomies)){ | |
| 249 | + foreach ($taxonomies as $key => $taxonomy_name) { | |
| 250 | + $return[] = array( | |
| 251 | + 'value' => $key, | |
| 252 | + 'label' => $taxonomy_name->labels->name.' ('.$key.')' | |
| 253 | + ); | |
| 254 | + } | |
| 255 | + } | |
| 256 | + return $return; | |
| 257 | + } | |
| 258 | + | |
| 259 | + public function get_terms($object) { | |
| 260 | + $taxonomy_name = getwid_recursive_sanitize_array( wp_unslash( $_GET['taxonomy_name'] ) ); | |
| 261 | + | |
| 262 | + $return = []; | |
| 263 | + $terms = get_terms(array( | |
| 264 | + 'taxonomy' => $taxonomy_name, | |
| 265 | + 'hide_empty' => false, | |
| 266 | + )); | |
| 267 | + | |
| 268 | + if (!empty($terms)){ | |
| 269 | + foreach ($terms as $key => $term_name) { | |
| 270 | + $taxonomy_obj = get_taxonomy( $term_name->taxonomy ); | |
| 271 | + | |
| 272 | + $return[$term_name->taxonomy]['group_name'] = $taxonomy_obj->label; | |
| 273 | + $return[$term_name->taxonomy]['group_value'][] = array( | |
| 274 | + 'value' => $term_name->taxonomy.'['.$term_name->term_id.']', | |
| 275 | + 'label' => $term_name->name, | |
| 276 | + ); | |
| 277 | + } | |
| 278 | + } | |
| 279 | + return $return; | |
| 280 | + } | |
| 281 | + | |
| 282 | + public function get_templates($object) { | |
| 283 | + $template_name = sanitize_text_field( wp_unslash( $_GET['template_name'] ) ); | |
| 284 | + | |
| 285 | + $posts = get_posts( array( | |
| 286 | + 'numberposts' => -1, | |
| 287 | + 'orderby' => 'date', | |
| 288 | + 'order' => 'DESC', | |
| 289 | + 'post_type' => $template_name, | |
| 290 | + ) ); | |
| 291 | + | |
| 292 | + $return = []; | |
| 293 | + if (!empty($posts)){ | |
| 294 | + foreach ($posts as $key => $post) { | |
| 295 | + $return[] = array( | |
| 296 | + 'value' => $post->ID, | |
| 297 | + 'label' => $post->post_title | |
| 298 | + ); | |
| 299 | + } | |
| 300 | + } | |
| 301 | + return $return; | |
| 302 | + } | |
| 303 | + | |
| 304 | + public function get_meta_keys( $request ) { | |
| 305 | + | |
| 306 | + $params = $request->get_params(); | |
| 307 | + unset( $params['metaQuery'] ); | |
| 308 | + | |
| 309 | + $args = getwid_build_custom_post_type_query( $params ); | |
| 310 | + $args['fields'] = 'ids'; | |
| 311 | + | |
| 312 | + if ( $args['posts_per_page'] > 100 ) { | |
| 313 | + $args['posts_per_page'] = 100; | |
| 314 | + } | |
| 315 | + | |
| 316 | + $posts = get_posts( $args ); | |
| 317 | + | |
| 318 | + $meta_keys_to_skip = [ '_oembed', '_edit_lock', '_edit_last', '_wp_old_slug', '_wxr' ]; | |
| 319 | + $meta_keys = []; | |
| 320 | + foreach ( $posts as $postID ) { | |
| 321 | + foreach ( get_post_meta( $postID, '', true ) as $meta_key => $value ) { | |
| 322 | + | |
| 323 | + if ( in_array( $meta_key, $meta_keys ) ) { | |
| 324 | + continue; | |
| 325 | + } | |
| 326 | + | |
| 327 | + $should_skip = false; | |
| 328 | + foreach( $meta_keys_to_skip as $skip ) { | |
| 329 | + if ( 0 === strpos( $meta_key, $skip ) ) { | |
| 330 | + $should_skip = true; | |
| 331 | + break; | |
| 332 | + } | |
| 333 | + } | |
| 334 | + | |
| 335 | + if ( ! $should_skip ) { | |
| 336 | + $meta_keys[] = $meta_key; | |
| 337 | + } | |
| 338 | + } | |
| 339 | + } | |
| 340 | + | |
| 341 | + return array_values( $meta_keys ); | |
| 342 | + } | |
| 343 | + | |
| 344 | + private function content_url_is_valid( $url ) { | |
| 345 | + | |
| 346 | + $parsed_url = wp_parse_url( $url ); | |
| 347 | + | |
| 348 | + if ( ! $parsed_url ) { | |
| 349 | + return false; | |
| 350 | + } | |
| 351 | + | |
| 352 | + $valid_hosts = array( 'elements.getwid.getmotopress.com' ); | |
| 353 | + $valid_path = '/wp-json/getwid-templates-server/v1/get_content'; | |
| 354 | + | |
| 355 | + $remote_library_url = wp_parse_url( $this->remote_template_library_url ); | |
| 356 | + | |
| 357 | + if ( $remote_library_url && isset( $remote_library_url['host'] ) ) { | |
| 358 | + $valid_hosts[] = $remote_library_url['host']; | |
| 359 | + } | |
| 360 | + | |
| 361 | + $host_is_valid = false; | |
| 362 | + $path_is_valid = false; | |
| 363 | + | |
| 364 | + if ( isset( $parsed_url['host'] ) && in_array( $parsed_url['host'], $valid_hosts ) ) { | |
| 365 | + $host_is_valid = true; | |
| 366 | + } | |
| 367 | + | |
| 368 | + if ( isset( $parsed_url['path'] ) && $valid_path == $parsed_url['path'] ) { | |
| 369 | + $path_is_valid = true; | |
| 370 | + } | |
| 371 | + | |
| 372 | + return $host_is_valid && $path_is_valid; | |
| 373 | + } | |
| 374 | +} | |