| 1 |
<?php |
| 2 |
/** |
| 3 |
* Rest API functions. |
| 4 |
* |
| 5 |
* @package Gutenify |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
require GUTENIFY_BASE_DIR . 'core/' . 'inc/rest-api/class-template-kits.php'; |
| 13 |
|
| 14 |
/** |
| 15 |
* Class Gutenify_Rest |
| 16 |
*/ |
| 17 |
class Gutenify_Rest extends WP_REST_Controller { |
| 18 |
/** |
| 19 |
* Namespace. |
| 20 |
* |
| 21 |
* @var string |
| 22 |
*/ |
| 23 |
protected $namespace = 'gutenify/v'; |
| 24 |
|
| 25 |
/** |
| 26 |
* Version. |
| 27 |
* |
| 28 |
* @var string |
| 29 |
*/ |
| 30 |
protected $version = '1'; |
| 31 |
|
| 32 |
/** |
| 33 |
* Gutenify_Rest constructor. |
| 34 |
*/ |
| 35 |
public function __construct() { |
| 36 |
add_action( 'rest_api_init', array( $this, 'register_routes' ) ); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Register rest routes. |
| 41 |
*/ |
| 42 |
public function register_routes() { |
| 43 |
$namespace = $this->namespace . $this->version; |
| 44 |
|
| 45 |
// Get Templates. |
| 46 |
register_rest_route( |
| 47 |
$namespace, |
| 48 |
'/get_templates/', |
| 49 |
array( |
| 50 |
'methods' => WP_REST_Server::READABLE, |
| 51 |
'callback' => array( $this, 'get_templates' ), |
| 52 |
'permission_callback' => array( $this, 'update_settings_permission' ), |
| 53 |
) |
| 54 |
); |
| 55 |
|
| 56 |
// Get template data. |
| 57 |
register_rest_route( |
| 58 |
$namespace, |
| 59 |
'/get_template_data/', |
| 60 |
array( |
| 61 |
'methods' => WP_REST_Server::READABLE, |
| 62 |
'callback' => array( $this, 'get_template_data' ), |
| 63 |
'permission_callback' => array( $this, 'update_settings_permission' ), |
| 64 |
) |
| 65 |
); |
| 66 |
|
| 67 |
// Get settings data. |
| 68 |
register_rest_route( |
| 69 |
$namespace, |
| 70 |
'/get_settings/', |
| 71 |
array( |
| 72 |
'methods' => WP_REST_Server::READABLE, |
| 73 |
'callback' => array( $this, 'get_settings' ), |
| 74 |
'permission_callback' => array( $this, 'update_settings_permission' ), |
| 75 |
) |
| 76 |
); |
| 77 |
|
| 78 |
// Update Settings. |
| 79 |
register_rest_route( |
| 80 |
$namespace, |
| 81 |
'/update_settings/', |
| 82 |
array( |
| 83 |
'methods' => WP_REST_Server::EDITABLE, |
| 84 |
'callback' => array( $this, 'update_settings' ), |
| 85 |
'permission_callback' => array( $this, 'update_settings_permission' ), |
| 86 |
) |
| 87 |
); |
| 88 |
|
| 89 |
// Get Templates. |
| 90 |
register_rest_route( |
| 91 |
$namespace, |
| 92 |
'/get_template_kit/', |
| 93 |
array( |
| 94 |
'methods' => WP_REST_Server::READABLE, |
| 95 |
'callback' => array( $this, 'get_template_kit' ), |
| 96 |
'permission_callback' => array( $this, 'update_settings_permission' ), |
| 97 |
) |
| 98 |
); |
| 99 |
|
| 100 |
// Get Templates. |
| 101 |
register_rest_route( |
| 102 |
$namespace, |
| 103 |
'/get_template_kit_data/', |
| 104 |
array( |
| 105 |
'methods' => WP_REST_Server::READABLE, |
| 106 |
'callback' => array( $this, 'get_template_kit_data' ), |
| 107 |
'permission_callback' => array( $this, 'update_settings_permission' ), |
| 108 |
) |
| 109 |
); |
| 110 |
|
| 111 |
// Add Kit. |
| 112 |
register_rest_route( |
| 113 |
$namespace, |
| 114 |
'/create_kit/', |
| 115 |
array( |
| 116 |
'methods' => WP_REST_Server::EDITABLE, |
| 117 |
'callback' => array( $this, 'create_kit' ), |
| 118 |
'permission_callback' => array( $this, 'update_settings_permission' ), |
| 119 |
) |
| 120 |
); |
| 121 |
|
| 122 |
// Get Templates. |
| 123 |
register_rest_route( |
| 124 |
$namespace, |
| 125 |
'/get_site_options/', |
| 126 |
array( |
| 127 |
'methods' => WP_REST_Server::READABLE, |
| 128 |
'callback' => array( $this, 'get_site_options' ), |
| 129 |
'permission_callback' => array( $this, 'update_settings_permission' ), |
| 130 |
) |
| 131 |
); |
| 132 |
|
| 133 |
// Update Site Optoins |
| 134 |
register_rest_route( |
| 135 |
$namespace, |
| 136 |
'/update_site_options/', |
| 137 |
array( |
| 138 |
'methods' => WP_REST_Server::EDITABLE, |
| 139 |
'callback' => array( $this, 'update_site_options' ), |
| 140 |
'permission_callback' => array( $this, 'update_settings_permission' ), |
| 141 |
) |
| 142 |
); |
| 143 |
|
| 144 |
// Update Site Optoins |
| 145 |
register_rest_route( |
| 146 |
$namespace, |
| 147 |
'/update_global_styles/', |
| 148 |
array( |
| 149 |
'methods' => WP_REST_Server::EDITABLE, |
| 150 |
'callback' => array( $this, 'update_global_styles' ), |
| 151 |
'permission_callback' => array( $this, 'update_settings_permission' ), |
| 152 |
) |
| 153 |
); |
| 154 |
|
| 155 |
// Get get_demo_categories. |
| 156 |
register_rest_route( |
| 157 |
$namespace, |
| 158 |
'/get_demo_categories/', |
| 159 |
array( |
| 160 |
'methods' => WP_REST_Server::READABLE, |
| 161 |
'callback' => array( $this, 'get_demo_categories' ), |
| 162 |
'permission_callback' => array( $this, 'update_settings_permission' ), |
| 163 |
) |
| 164 |
); |
| 165 |
|
| 166 |
// Get Demos. |
| 167 |
register_rest_route( |
| 168 |
$namespace, |
| 169 |
'/get_theme_demo_list/', |
| 170 |
array( |
| 171 |
'methods' => WP_REST_Server::READABLE, |
| 172 |
'callback' => array( $this, 'get_theme_demo_list' ), |
| 173 |
'permission_callback' => array( $this, 'update_settings_permission' ), |
| 174 |
) |
| 175 |
); |
| 176 |
|
| 177 |
register_rest_route( |
| 178 |
$namespace, |
| 179 |
'/get_theme_import_demo_set_pages/', |
| 180 |
array( |
| 181 |
'methods' => WP_REST_Server::EDITABLE, |
| 182 |
'callback' => array( $this, 'get_theme_import_demo_set_pages' ), |
| 183 |
'permission_callback' => array( $this, 'update_settings_permission' ), |
| 184 |
) |
| 185 |
); |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Get templates. |
| 190 |
* |
| 191 |
* @return mixed |
| 192 |
*/ |
| 193 |
public function get_templates( WP_REST_Request $request ) { |
| 194 |
$constants = \gutenify\Helpers::plugin_constants(); |
| 195 |
$plugin_main_slug = $constants['plugin_main_slug']; |
| 196 |
$plugin_main_function_prefix = $constants['plugin_main_function_prefix']; |
| 197 |
$plugin_main_base_url = $constants['plugin_main_base_url']; |
| 198 |
$plugin_main_version = $constants['plugin_main_version']; |
| 199 |
$plugin_main_post_type_prefix = $constants['plugin_main_post_type_prefix']; |
| 200 |
|
| 201 |
$api_site = defined( 'GUTENIFY_API_URL' ) ? GUTENIFY_API_URL : 'https://api.gutenify.com/'; |
| 202 |
$url = $api_site . 'wp-json/gutenify-library/v1/get_library/'; |
| 203 |
$templates = get_transient( 'gutenify_remote_templates', false ); |
| 204 |
|
| 205 |
$data = $request->get_params(); |
| 206 |
|
| 207 |
if ( ! empty( $data['reset'] ) && 'true' === $data['reset'] ) { |
| 208 |
$templates = array(); |
| 209 |
} |
| 210 |
|
| 211 |
/* |
| 212 |
* Get remote templates. |
| 213 |
*/ |
| 214 |
if ( ! $templates ) { |
| 215 |
$requested_templates = wp_remote_get( |
| 216 |
add_query_arg( |
| 217 |
array( |
| 218 |
'gutenify_version' => '2.19.3', |
| 219 |
'gutenify_pro' => false, |
| 220 |
'gutenify_pro_version' => null, |
| 221 |
), |
| 222 |
$url |
| 223 |
) |
| 224 |
); |
| 225 |
|
| 226 |
if ( ! is_wp_error( $requested_templates ) ) { |
| 227 |
$new_templates = wp_remote_retrieve_body( $requested_templates ); |
| 228 |
$new_templates = json_decode( $new_templates, true ); |
| 229 |
|
| 230 |
if ( $new_templates && isset( $new_templates['response'] ) && is_array( $new_templates['response'] ) ) { |
| 231 |
$templates = $new_templates['response']; |
| 232 |
set_transient( 'gutenify_remote_templates', $templates, DAY_IN_SECONDS ); |
| 233 |
} |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
// Remove Pro templates from array, cause for now there is no way to check if pro addon is activated. |
| 238 |
if ( $templates ) { |
| 239 |
foreach ( $templates as $k => $template ) { |
| 240 |
$is_pro = false; |
| 241 |
|
| 242 |
if ( isset( $template['types'] ) && is_array( $template['types'] ) ) { |
| 243 |
foreach ( $template['types'] as $type ) { |
| 244 |
$is_pro = $is_pro || 'pro' === $type['slug']; |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
if ( $is_pro ) { |
| 249 |
unset( $templates[ $k ] ); |
| 250 |
} |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
/* |
| 255 |
* Get user templates from db. |
| 256 |
*/ |
| 257 |
|
| 258 |
// Stupid hack. |
| 259 |
// https://core.trac.wordpress.org/ticket/18408. |
| 260 |
global $post; |
| 261 |
$backup_global_post = $post; |
| 262 |
$local_templates = array(); |
| 263 |
|
| 264 |
$local_templates_query = new WP_Query( |
| 265 |
array( |
| 266 |
'post_type' => $plugin_main_post_type_prefix . '_template', |
| 267 |
'posts_per_page' => -1, |
| 268 |
'showposts' => -1, |
| 269 |
'paged' => -1, |
| 270 |
) |
| 271 |
); |
| 272 |
|
| 273 |
while ( $local_templates_query->have_posts() ) { |
| 274 |
$local_templates_query->the_post(); |
| 275 |
$db_template = get_post(); |
| 276 |
|
| 277 |
$categories = array(); |
| 278 |
$category_terms = get_the_terms( $db_template->ID, $plugin_main_post_type_prefix . '_template_category' ); |
| 279 |
|
| 280 |
if ( $category_terms ) { |
| 281 |
foreach ( $category_terms as $cat ) { |
| 282 |
$categories[] = array( |
| 283 |
'slug' => $cat->slug, |
| 284 |
'name' => $cat->name, |
| 285 |
); |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
$image_id = get_post_thumbnail_id( $db_template->ID ); |
| 290 |
$image_data = wp_get_attachment_image_src( $image_id, 'large' ); |
| 291 |
|
| 292 |
$local_templates[] = array( |
| 293 |
'id' => $db_template->ID, |
| 294 |
'title' => $db_template->post_title, |
| 295 |
'types' => array( |
| 296 |
array( |
| 297 |
'slug' => 'local', |
| 298 |
), |
| 299 |
), |
| 300 |
'categories' => empty( $categories ) ? false : $categories, |
| 301 |
'url' => get_post_permalink( $db_template->ID ), |
| 302 |
'thumbnail' => isset( $image_data[0] ) ? $image_data[0] : false, |
| 303 |
'thumbnail_width' => isset( $image_data[1] ) ? $image_data[1] : false, |
| 304 |
'thumbnail_height' => isset( $image_data[2] ) ? $image_data[2] : false, |
| 305 |
); |
| 306 |
} |
| 307 |
|
| 308 |
// Restore the global $post as it was before custom WP_Query. |
| 309 |
// phpcs:ignore |
| 310 |
$post = $backup_global_post; |
| 311 |
|
| 312 |
/* |
| 313 |
* Get theme templates. |
| 314 |
*/ |
| 315 |
$theme_templates = array(); |
| 316 |
$theme_templates_data = array(); |
| 317 |
|
| 318 |
foreach ( glob( get_template_directory() . '/gutenify/templates/*/content.php' ) as $template ) { |
| 319 |
$template_path = dirname( $template ); |
| 320 |
$template_url = get_template_directory_uri() . str_replace( get_template_directory(), '', $template_path ); |
| 321 |
$slug = basename( $template_path ); |
| 322 |
|
| 323 |
$theme_templates_data[ $slug ] = array( |
| 324 |
'slug' => $slug, |
| 325 |
'path' => $template_path, |
| 326 |
'url' => $template_url, |
| 327 |
); |
| 328 |
} |
| 329 |
|
| 330 |
// get child theme templates. |
| 331 |
if ( get_stylesheet_directory() !== get_template_directory() ) { |
| 332 |
foreach ( glob( get_stylesheet_directory() . '/gutenify/templates/*/content.php' ) as $template ) { |
| 333 |
$template_path = dirname( $template ); |
| 334 |
$template_url = get_stylesheet_directory_uri() . str_replace( get_stylesheet_directory(), '', $template_path ); |
| 335 |
$slug = basename( $template_path ); |
| 336 |
|
| 337 |
$theme_templates_data[ $slug ] = array( |
| 338 |
'slug' => $slug, |
| 339 |
'path' => $template_path, |
| 340 |
'url' => $template_url, |
| 341 |
); |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
// natural sort. |
| 346 |
array_multisort( array_keys( $theme_templates_data ), SORT_NATURAL, $theme_templates_data ); |
| 347 |
|
| 348 |
foreach ( $theme_templates_data as $template_data ) { |
| 349 |
$file_data = get_file_data( |
| 350 |
$template_data['path'] . '/content.php', |
| 351 |
array( |
| 352 |
'name' => 'Name', |
| 353 |
'category' => 'Category', |
| 354 |
'source' => 'Source', |
| 355 |
) |
| 356 |
); |
| 357 |
|
| 358 |
$thumbnail = false; |
| 359 |
$thumbnail_width = false; |
| 360 |
$thumbnail_height = false; |
| 361 |
|
| 362 |
if ( file_exists( $template_data['path'] . '/thumbnail.png' ) ) { |
| 363 |
$thumbnail = $template_data['url'] . '/thumbnail.png'; |
| 364 |
|
| 365 |
list($thumbnail_width, $thumbnail_height) = getimagesize( $thumbnail ); |
| 366 |
} |
| 367 |
|
| 368 |
if ( file_exists( $template_data['path'] . '/thumbnail.jpg' ) ) { |
| 369 |
$thumbnail = $template_data['url'] . '/thumbnail.jpg'; |
| 370 |
} |
| 371 |
|
| 372 |
$theme_templates[] = array( |
| 373 |
'id' => basename( $template_data['path'] ), |
| 374 |
'title' => $file_data['name'], |
| 375 |
'types' => array( |
| 376 |
array( |
| 377 |
'slug' => 'theme', |
| 378 |
), |
| 379 |
), |
| 380 |
'categories' => isset( $file_data['category'] ) && $file_data['category'] ? array( |
| 381 |
array( |
| 382 |
'slug' => $file_data['category'], |
| 383 |
'name' => $file_data['category'], |
| 384 |
), |
| 385 |
) : false, |
| 386 |
'url' => false, |
| 387 |
'thumbnail' => $thumbnail, |
| 388 |
'thumbnail_width' => $thumbnail_width, |
| 389 |
'thumbnail_height' => $thumbnail_height, |
| 390 |
); |
| 391 |
} |
| 392 |
|
| 393 |
// merge all available templates. |
| 394 |
$templates = ! empty( $templates ) ? $templates : array(); |
| 395 |
$templates = array_merge( $templates, $local_templates, $theme_templates ); |
| 396 |
|
| 397 |
if ( is_array( $templates ) ) { |
| 398 |
return $this->success( $templates ); |
| 399 |
} else { |
| 400 |
return $this->error( 'no_templates', __( 'Templates not found.', 'gutenify' ) ); |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
/** |
| 405 |
* Get templates. |
| 406 |
* |
| 407 |
* @param WP_REST_Request $request request object. |
| 408 |
* |
| 409 |
* @return mixed |
| 410 |
*/ |
| 411 |
public function get_template_data( WP_REST_Request $request ) { |
| 412 |
$constants = \gutenify\Helpers::plugin_constants(); |
| 413 |
$plugin_main_slug = $constants['plugin_main_slug']; |
| 414 |
$plugin_main_function_prefix = $constants['plugin_main_function_prefix']; |
| 415 |
$plugin_main_base_url = $constants['plugin_main_base_url']; |
| 416 |
$plugin_main_version = $constants['plugin_main_version']; |
| 417 |
$plugin_main_post_type_prefix = $constants['plugin_main_post_type_prefix']; |
| 418 |
|
| 419 |
$api_site = defined( 'GUTENIFY_API_URL' ) ? GUTENIFY_API_URL : 'https://api.gutenify.com/'; |
| 420 |
$url = $api_site . 'wp-json/gutenify-library/v1/get_library_data/'; |
| 421 |
$id = $request->get_param( 'id' ); |
| 422 |
$type = $request->get_param( 'type' ); |
| 423 |
$template_data = false; |
| 424 |
|
| 425 |
if ( 1 > absint( $id ) || empty( $type ) ) { |
| 426 |
return $this->error( 'no_template_data', __( 'Template data not found.', 'gutenify' ) ); |
| 427 |
} |
| 428 |
|
| 429 |
$id = absint( $id ); |
| 430 |
switch ( $type ) { |
| 431 |
case 'remote': |
| 432 |
$template_data = get_transient( 'gutenify_template_' . $type . '_' . $id, false ); |
| 433 |
|
| 434 |
if ( ! $template_data ) { |
| 435 |
$url = add_query_arg( |
| 436 |
apply_filters( |
| 437 |
'gutenify_rest_template_data_url_args', |
| 438 |
array( |
| 439 |
'id' => $id, |
| 440 |
'gutenify_version' => GUTENIFY_VERSION, |
| 441 |
'site_url' => site_url( '/' ), |
| 442 |
) |
| 443 |
), |
| 444 |
$url |
| 445 |
); |
| 446 |
|
| 447 |
$requested_template_data = wp_remote_get( $url ); |
| 448 |
|
| 449 |
if ( ! is_wp_error( $requested_template_data ) ) { |
| 450 |
$new_template_data = wp_remote_retrieve_body( $requested_template_data ); |
| 451 |
$new_template_data = json_decode( $new_template_data, true ); |
| 452 |
|
| 453 |
if ( $new_template_data && isset( $new_template_data['response'] ) && is_array( $new_template_data['response'] ) ) { |
| 454 |
$template_data = $new_template_data['response']; |
| 455 |
set_transient( 'gutenify_template_' . $type . '_' . $id, $template_data, DAY_IN_SECONDS ); |
| 456 |
} |
| 457 |
} |
| 458 |
} |
| 459 |
break; |
| 460 |
case 'local': |
| 461 |
$post = get_post( $id ); |
| 462 |
|
| 463 |
if ( $post && $plugin_main_post_type_prefix . '_template' === $post->post_type && 'publish' === $post->post_status ) { |
| 464 |
$template_data = array( |
| 465 |
'id' => $post->ID, |
| 466 |
'title' => $post->post_title, |
| 467 |
'content' => $post->post_content, |
| 468 |
); |
| 469 |
} |
| 470 |
|
| 471 |
break; |
| 472 |
} |
| 473 |
|
| 474 |
if ( is_array( $template_data ) ) { |
| 475 |
return $this->success( $template_data ); |
| 476 |
} else { |
| 477 |
return $this->error( 'no_template_data', __( 'Template data not found.', 'gutenify' ) ); |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
/** |
| 482 |
* Get settings. |
| 483 |
* |
| 484 |
* @return mixed |
| 485 |
*/ |
| 486 |
public function get_settings() { |
| 487 |
$settings = gutenify_settings(); |
| 488 |
|
| 489 |
if ( is_array( $settings ) ) { |
| 490 |
return $this->success( $settings ); |
| 491 |
} else { |
| 492 |
return $this->error( 'no_settings', __( 'Settings data not found.', 'gutenify' ) ); |
| 493 |
} |
| 494 |
} |
| 495 |
|
| 496 |
/** |
| 497 |
* Get edit options permissions. |
| 498 |
* |
| 499 |
* @return bool |
| 500 |
*/ |
| 501 |
public function update_settings_permission() { |
| 502 |
return current_user_can( 'manage_options' ); |
| 503 |
} |
| 504 |
|
| 505 |
/** |
| 506 |
* Update Settings. |
| 507 |
* |
| 508 |
* @param WP_REST_Request $request request object. |
| 509 |
* |
| 510 |
* @return mixed |
| 511 |
*/ |
| 512 |
public function update_settings( WP_REST_Request $request ) { |
| 513 |
$new_settings = $request->get_param( 'settings' ); |
| 514 |
|
| 515 |
if ( is_array( $new_settings ) ) { |
| 516 |
$current_settings = get_option( 'gutenify_settings', array() ); |
| 517 |
update_option( 'gutenify_settings', array_merge( $current_settings, $new_settings ) ); |
| 518 |
} |
| 519 |
|
| 520 |
return $this->success( true ); |
| 521 |
} |
| 522 |
|
| 523 |
/** |
| 524 |
* Get templates. |
| 525 |
* |
| 526 |
* @return mixed |
| 527 |
*/ |
| 528 |
public function get_template_kit( WP_REST_Request $request ) { |
| 529 |
$api_site = defined( 'GUTENIFY_API_URL' ) ? GUTENIFY_API_URL : 'https://api.gutenify.com/'; |
| 530 |
$url = $api_site . 'wp-json/gutenify-library/v1/get_kit_library/'; |
| 531 |
$templates = get_transient( 'gutenify_remote_template_kits', false ); |
| 532 |
$data = $request->get_params(); |
| 533 |
|
| 534 |
if ( ! empty( $data['reset'] ) && 'true' === $data['reset'] ) { |
| 535 |
$templates = array(); |
| 536 |
} |
| 537 |
|
| 538 |
/* |
| 539 |
* Get remote templates. |
| 540 |
*/ |
| 541 |
if ( ! $templates ) { |
| 542 |
$requested_templates = wp_remote_get( |
| 543 |
add_query_arg( |
| 544 |
array( |
| 545 |
'gutenify_version' => '2.19.3', |
| 546 |
'gutenify_pro' => false, |
| 547 |
'gutenify_pro_version' => null, |
| 548 |
), |
| 549 |
$url |
| 550 |
) |
| 551 |
); |
| 552 |
|
| 553 |
if ( ! is_wp_error( $requested_templates ) ) { |
| 554 |
$new_templates = wp_remote_retrieve_body( $requested_templates ); |
| 555 |
$new_templates = json_decode( $new_templates, true ); |
| 556 |
|
| 557 |
if ( $new_templates && isset( $new_templates['response'] ) && is_array( $new_templates['response'] ) ) { |
| 558 |
$templates = $new_templates['response']; |
| 559 |
set_transient( 'gutenify_remote_template_kits', $templates, DAY_IN_SECONDS ); |
| 560 |
} |
| 561 |
} |
| 562 |
} |
| 563 |
|
| 564 |
// Remove Pro templates from array, cause for now there is no way to check if pro addon is activated. |
| 565 |
if ( $templates ) { |
| 566 |
foreach ( $templates as $k => $template ) { |
| 567 |
$is_pro = false; |
| 568 |
|
| 569 |
if ( isset( $template['types'] ) && is_array( $template['types'] ) ) { |
| 570 |
foreach ( $template['types'] as $type ) { |
| 571 |
$is_pro = $is_pro || 'pro' === $type['slug']; |
| 572 |
} |
| 573 |
} |
| 574 |
|
| 575 |
if ( $is_pro ) { |
| 576 |
unset( $templates[ $k ] ); |
| 577 |
} |
| 578 |
} |
| 579 |
} |
| 580 |
|
| 581 |
if ( is_array( $templates ) ) { |
| 582 |
return $this->success( $templates ); |
| 583 |
} else { |
| 584 |
return $this->error( 'no_template_kits', __( 'Template kit not found.' . $url, 'gutenify' ) ); |
| 585 |
} |
| 586 |
} |
| 587 |
|
| 588 |
public function get_template_kit_data( WP_REST_Request $request ) { |
| 589 |
$api_site = defined( 'GUTENIFY_API_URL' ) ? GUTENIFY_API_URL : 'https://api.gutenify.com/'; |
| 590 |
$url = $api_site . 'wp-json/gutenify-library/v1/get_kit_library_data/'; |
| 591 |
$id = $request->get_param( 'id' ); |
| 592 |
$type = $request->get_param( 'type' ); |
| 593 |
$template_data = false; |
| 594 |
switch ( $type ) { |
| 595 |
case 'remote': |
| 596 |
$template_data = get_transient( 'gutenify_template_kit_' . $type . '_' . $id, false ); |
| 597 |
|
| 598 |
if ( ! $template_data ) { |
| 599 |
$requested_template_data = wp_remote_get( |
| 600 |
add_query_arg( |
| 601 |
apply_filters( |
| 602 |
'gutenify_rest_template_data_url_args', |
| 603 |
array( |
| 604 |
'id' => $id, |
| 605 |
'gutenify_version' => GUTENIFY_VERSION, |
| 606 |
'gutenify_site' => site_url( '/' ), |
| 607 |
) |
| 608 |
), |
| 609 |
$url |
| 610 |
) |
| 611 |
); |
| 612 |
|
| 613 |
if ( ! is_wp_error( $requested_template_data ) ) { |
| 614 |
$new_template_data = wp_remote_retrieve_body( $requested_template_data ); |
| 615 |
$new_template_data = json_decode( $new_template_data, true ); |
| 616 |
|
| 617 |
if ( $new_template_data && isset( $new_template_data['response'] ) && is_array( $new_template_data['response'] ) ) { |
| 618 |
$template_data = $new_template_data['response']; |
| 619 |
set_transient( 'gutenify_template_' . $type . '_' . $id, $template_data, DAY_IN_SECONDS ); |
| 620 |
} |
| 621 |
} |
| 622 |
} |
| 623 |
break; |
| 624 |
case 'local': |
| 625 |
$post = get_post( $id ); |
| 626 |
|
| 627 |
if ( $post && 'gutenify_template' === $post->post_type && 'publish' === $post->post_status ) { |
| 628 |
$template_data = array( |
| 629 |
'id' => $post->ID, |
| 630 |
'title' => $post->post_title, |
| 631 |
'content' => $post->post_content, |
| 632 |
); |
| 633 |
} |
| 634 |
|
| 635 |
break; |
| 636 |
} |
| 637 |
|
| 638 |
if ( is_array( $template_data ) ) { |
| 639 |
return $this->success( $template_data ); |
| 640 |
} else { |
| 641 |
return $this->error( 'no_template_data', __( 'Template data not found.', 'gutenify' ) ); |
| 642 |
} |
| 643 |
} |
| 644 |
|
| 645 |
public function create_kit( WP_REST_Request $request ) { |
| 646 |
$data = $request->get_params(); |
| 647 |
$gutenify_template_kits = new Gutenify_Template_Kits(); |
| 648 |
$response = $gutenify_template_kits->add_kit( $data ); |
| 649 |
|
| 650 |
if ( $response ) { |
| 651 |
return $this->success( $response ); |
| 652 |
} |
| 653 |
return $this->error( 'error_creating_kit', __( 'Error creating Kit', 'gutenify' ) ); |
| 654 |
} |
| 655 |
|
| 656 |
public function get_site_options() { |
| 657 |
$options = get_option( 'gutenify_site_options' ); |
| 658 |
return $this->success( $options ); |
| 659 |
} |
| 660 |
|
| 661 |
/** |
| 662 |
* Update site options. |
| 663 |
* |
| 664 |
* @param WP_REST_Request $request request object. |
| 665 |
* |
| 666 |
* @return mixed |
| 667 |
*/ |
| 668 |
public function update_site_options( WP_REST_Request $request ) { |
| 669 |
$options = $request->get_params(); |
| 670 |
$merged_options = array(); |
| 671 |
if ( is_array( $options ) ) { |
| 672 |
$current_options = get_option( 'gutenify_site_options', array() ); |
| 673 |
if ( empty( $current_options ) ) { |
| 674 |
$current_options = array(); |
| 675 |
} |
| 676 |
$merged_options = array_merge( $current_options, $options ); |
| 677 |
|
| 678 |
// $site_fonts = array(); |
| 679 |
// if ( ! empty( $merged_options['styles']['siteFonts'] ) ) { |
| 680 |
// $site_settings = wp_get_global_settings(); |
| 681 |
// $site_styles = wp_get_global_styles(); |
| 682 |
// $default_font_families = ! empty( $site_settings['typography']['fontFamilies']['theme'] )? $site_settings['typography']['fontFamilies']['theme'] : array(); |
| 683 |
// $default_font_families_slugs = wp_list_pluck( $default_font_families, 'slug' ); |
| 684 |
// error_log( print_r( $default_font_families_slugs, true )); |
| 685 |
// foreach( $merged_options['styles']['siteFonts'] as $font ) { |
| 686 |
// if ( ! in_array( $font['value'], $default_font_families_slugs, true ) ) { |
| 687 |
// $site_fonts[] = array( |
| 688 |
// 'fontFamily' => $font['label'], |
| 689 |
// 'name' => $font['label'], |
| 690 |
// 'slug' => $font['value'], |
| 691 |
// ); |
| 692 |
// } |
| 693 |
// } |
| 694 |
// $new_font_families = array_merge( $default_font_families, $site_fonts ); |
| 695 |
// error_log( print_r( $new_font_families, true )); |
| 696 |
// $site_settings['typography']['fontFamilies']['theme'] = $new_font_families; |
| 697 |
|
| 698 |
// gutenify_update_global_styles( $site_settings, $site_styles ); |
| 699 |
// } |
| 700 |
|
| 701 |
// Ideally the call to update_item would delete all of the appropriate transients and caches |
| 702 |
delete_transient( 'global_styles' ); |
| 703 |
delete_transient( 'global_styles_' . get_stylesheet() ); |
| 704 |
delete_transient( 'gutenberg_global_styles' ); |
| 705 |
delete_transient( 'gutenberg_global_styles_' . get_stylesheet() ); |
| 706 |
|
| 707 |
if ( class_exists( 'wp_theme_json_resolver' ) ) { |
| 708 |
wp_theme_json_resolver::clean_cached_data(); |
| 709 |
} |
| 710 |
update_option( 'gutenify_site_options', array_merge( $current_options, $options ) ); |
| 711 |
} |
| 712 |
|
| 713 |
return $this->success( $merged_options ); |
| 714 |
} |
| 715 |
|
| 716 |
/** |
| 717 |
* Update site options. |
| 718 |
* |
| 719 |
* @param WP_REST_Request $request request object. |
| 720 |
* |
| 721 |
* @return mixed |
| 722 |
*/ |
| 723 |
public function update_global_styles( WP_REST_Request $request ) { |
| 724 |
$options = (array) $request->get_params(); |
| 725 |
|
| 726 |
if ( isset( $options['css'] ) ) { |
| 727 |
// Sanitize CSS at save-time: strip any HTML tags before storing. |
| 728 |
update_option( 'gutenify_global_style', wp_strip_all_tags( $options['css'] ) ); |
| 729 |
} |
| 730 |
|
| 731 |
return $this->success( $options ); |
| 732 |
} |
| 733 |
|
| 734 |
public function get_demo_categories( WP_REST_Request $request ) { |
| 735 |
$options = (array) $request->get_params(); |
| 736 |
$demo_categories = false; |
| 737 |
if ( empty( $options['force'] ) || 'false' === $options['force'] ) { |
| 738 |
$demo_categories = get_transient( 'gutenify_demo_categories', false ); |
| 739 |
} |
| 740 |
if ( false === $demo_categories ) { |
| 741 |
$error = $this->error( 'error_demo_categories', __( 'Error listing demo', 'gutenify' ) ); |
| 742 |
|
| 743 |
try { |
| 744 |
$url = 'https://demo.gutenify.com/wp-json/demo-api/v1/get_demo_categories'; |
| 745 |
// 'https://api.github.com/repos/gutenify/demo-content/contents/themes' |
| 746 |
/** @var array|WP_Error $response */ |
| 747 |
$response = wp_remote_get( |
| 748 |
$url, |
| 749 |
array( |
| 750 |
'timeout' => 120, |
| 751 |
'httpversion' => '1.1', |
| 752 |
'headers' => array( |
| 753 |
'Accept' => 'application/json', |
| 754 |
), |
| 755 |
) |
| 756 |
); |
| 757 |
if ( ( ! is_wp_error( $response ) ) && ( 200 === wp_remote_retrieve_response_code( $response ) ) ) { |
| 758 |
if ( $body = wp_remote_retrieve_body( $response ) ) { |
| 759 |
$responseBody = json_decode( $body ); |
| 760 |
$categories = array(); |
| 761 |
if ( ! empty( $responseBody ) && ! empty( $responseBody->response ) ) { |
| 762 |
$categories = $responseBody->response; |
| 763 |
} |
| 764 |
set_transient( 'gutenify_demo_categories', json_encode( $categories ), DAY_IN_SECONDS ); |
| 765 |
return $this->success( $categories ); |
| 766 |
} |
| 767 |
return $error; |
| 768 |
} |
| 769 |
} catch ( Exception $ex ) { |
| 770 |
return $error; |
| 771 |
} |
| 772 |
} |
| 773 |
return $this->success( json_decode( $demo_categories ) ); |
| 774 |
} |
| 775 |
|
| 776 |
public function get_theme_demo_list( WP_REST_Request $request ) { |
| 777 |
$options = (array) $request->get_params(); |
| 778 |
$demo_list = false; |
| 779 |
if ( empty( $options['force'] ) || 'false' === $options['force'] ) { |
| 780 |
$demo_list = get_transient( 'gutenify_demo_import_list', false ); |
| 781 |
} |
| 782 |
if ( false === $demo_list ) { |
| 783 |
$error = $this->error( 'error_demo_list', __( 'Error listing demo', 'gutenify' ) ); |
| 784 |
|
| 785 |
try { |
| 786 |
$url = 'https://demo.gutenify.com/wp-json/demo-api/v1/get_demos'; |
| 787 |
/** @var array|WP_Error $response */ |
| 788 |
$response = wp_remote_get( |
| 789 |
$url, |
| 790 |
array( |
| 791 |
'timeout' => 120, |
| 792 |
'httpversion' => '1.1', |
| 793 |
'headers' => array( |
| 794 |
'Accept' => 'application/json', |
| 795 |
), |
| 796 |
) |
| 797 |
); |
| 798 |
if ( ( ! is_wp_error( $response ) ) && ( 200 === wp_remote_retrieve_response_code( $response ) ) ) { |
| 799 |
if ( $body = wp_remote_retrieve_body( $response ) ) { |
| 800 |
$responseBody = json_decode( $body ); |
| 801 |
$themes = array(); |
| 802 |
if ( ! empty( $responseBody ) && ! empty( $responseBody->response ) ) { |
| 803 |
$themes = (array) $responseBody->response; |
| 804 |
} |
| 805 |
set_transient( 'gutenify_demo_import_list', json_encode( $themes ), DAY_IN_SECONDS ); |
| 806 |
return $this->success( $themes ); |
| 807 |
} |
| 808 |
return $error; |
| 809 |
} |
| 810 |
} catch ( Exception $ex ) { |
| 811 |
return $error; |
| 812 |
} |
| 813 |
} |
| 814 |
return $this->success( json_decode( $demo_list ) ); |
| 815 |
} |
| 816 |
|
| 817 |
|
| 818 |
public function get_theme_import_demo_set_pages( WP_REST_Request $request ) { |
| 819 |
$options = (array) $request->get_params(); |
| 820 |
|
| 821 |
if ( ! empty( $options['name'] ) ) { |
| 822 |
$name = $options['name']; |
| 823 |
$theme = wp_get_theme(); |
| 824 |
|
| 825 |
// Set Header |
| 826 |
$args = array( |
| 827 |
'post_type' => 'wp_template_part', |
| 828 |
'post_name__in' => array( 'header' ), |
| 829 |
'posts_per_page' => 1, |
| 830 |
); |
| 831 |
$headers = get_posts( $args ); |
| 832 |
if ( ! empty( $headers ) ) { |
| 833 |
$post_data = $headers[0]; |
| 834 |
wp_set_post_terms( $post_data->ID, 'header', 'wp_template_part_area' ); |
| 835 |
wp_set_post_terms( $post_data->ID, $theme->template, 'wp_theme' ); |
| 836 |
} |
| 837 |
|
| 838 |
// Set Footer |
| 839 |
$args['post_name__in'] = array( 'footer' ); |
| 840 |
$footers = get_posts( $args ); |
| 841 |
if ( ! empty( $footers ) ) { |
| 842 |
$post_data = $footers[0]; |
| 843 |
wp_set_post_terms( $post_data->ID, 'footer', 'wp_template_part_area' ); |
| 844 |
wp_set_post_terms( $post_data->ID, $theme->template, 'wp_theme' ); |
| 845 |
} |
| 846 |
|
| 847 |
// Set Sidebar |
| 848 |
$args['post_name__in'] = array( 'sidebar' ); |
| 849 |
$sidebars = get_posts( $args ); |
| 850 |
if ( ! empty( $sidebars ) ) { |
| 851 |
$post_data = $sidebars[0]; |
| 852 |
// wp_set_post_terms( $post_data->ID, 'footer', 'wp_template_part_area' ); |
| 853 |
wp_set_post_terms( $post_data->ID, $theme->template, 'wp_theme' ); |
| 854 |
} |
| 855 |
|
| 856 |
$imported_posts = get_transient( '_transient_pt_importer_data' ); |
| 857 |
// $imported_posts = get_option( '_pt_importer_data' ); |
| 858 |
|
| 859 |
$args = array( |
| 860 |
'post_type' => 'wp_template', |
| 861 |
// 'post_name__in' => array( 'header' ), |
| 862 |
'posts_per_page' => -1, |
| 863 |
'tax_query' => array( |
| 864 |
array( |
| 865 |
'taxonomy' => 'wp_theme', |
| 866 |
'field' => 'slug', |
| 867 |
'terms' => $theme->template, |
| 868 |
'operator' => 'NOT IN', |
| 869 |
), |
| 870 |
), |
| 871 |
); |
| 872 |
|
| 873 |
if ( false !== $imported_posts && ! empty( $imported_posts['mapping']['post'] ) ) { |
| 874 |
$post_ids = $imported_posts['mapping']['post']; |
| 875 |
$args['post__in'] = $post_ids; |
| 876 |
} |
| 877 |
|
| 878 |
$templates = get_posts( $args ); |
| 879 |
|
| 880 |
if ( ! empty( $templates ) ) { |
| 881 |
$do_not_duplicate = array(); |
| 882 |
$patterns = array( '/(\"theme\"\:\".*\")/' ); |
| 883 |
$replace = array( '"theme":"' . $theme->template . '"' ); |
| 884 |
|
| 885 |
foreach ( $templates as $template ) { |
| 886 |
if ( ! in_array( $template->post_name, $do_not_duplicate ) ) { |
| 887 |
$count = 0; |
| 888 |
$content = preg_replace( $patterns, $replace, $template->post_content, -1, $count ); |
| 889 |
|
| 890 |
$data = array( |
| 891 |
'ID' => $template->ID, |
| 892 |
'post_content' => $content, |
| 893 |
); |
| 894 |
|
| 895 |
wp_update_post( $data ); |
| 896 |
wp_set_post_terms( $template->ID, $theme->template, 'wp_theme' ); |
| 897 |
$do_not_duplicate[] = $template->post_name; |
| 898 |
} |
| 899 |
} |
| 900 |
} |
| 901 |
|
| 902 |
return $this->success( |
| 903 |
array( |
| 904 |
'name' => $name, |
| 905 |
'headers' => $headers, |
| 906 |
'templates' => $templates, |
| 907 |
'args' => $args, |
| 908 |
'imported_posts' => $imported_posts, |
| 909 |
) |
| 910 |
); |
| 911 |
} |
| 912 |
$error = $this->error( 'error_demo_set_pages', __( 'Error demo set pages.', 'gutenify' ) ); |
| 913 |
} |
| 914 |
|
| 915 |
/** |
| 916 |
* Success rest. |
| 917 |
* |
| 918 |
* @param mixed $response response data. |
| 919 |
* @return mixed |
| 920 |
*/ |
| 921 |
public function success( $response ) { |
| 922 |
return new WP_REST_Response( |
| 923 |
array( |
| 924 |
'success' => true, |
| 925 |
'response' => $response, |
| 926 |
), |
| 927 |
200 |
| 928 |
); |
| 929 |
} |
| 930 |
|
| 931 |
/** |
| 932 |
* Error rest. |
| 933 |
* |
| 934 |
* @param mixed $code error code. |
| 935 |
* @param mixed $response response data. |
| 936 |
* @return mixed |
| 937 |
*/ |
| 938 |
public function error( $code, $response ) { |
| 939 |
return new WP_REST_Response( |
| 940 |
array( |
| 941 |
'error' => true, |
| 942 |
'success' => false, |
| 943 |
'error_code' => $code, |
| 944 |
'response' => $response, |
| 945 |
), |
| 946 |
401 |
| 947 |
); |
| 948 |
} |
| 949 |
} |
| 950 |
new Gutenify_Rest(); |
| 951 |
|