Importers
2 months ago
Admin.php
1 week ago
Editor.php
1 year ago
Elementor.php
1 year ago
License.php
1 year ago
Logger.php
2 years ago
Main.php
1 week ago
Rest_Server.php
2 weeks ago
Sites_Listing.php
2 weeks ago
Starter_Ranking.php
1 week ago
TI_Beaver.php
1 year ago
WP_Cli.php
3 months ago
White_Label_Config.php
3 years ago
Rest_Server.php
524 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Rest Endpoints Handler. |
| 4 | * |
| 5 | * @package templates-patterns-collection |
| 6 | */ |
| 7 | |
| 8 | namespace TIOB; |
| 9 | |
| 10 | use TIOB\Importers\Cleanup\Manager; |
| 11 | use TIOB\TI_Beaver; |
| 12 | use TIOB\Importers\Content_Importer; |
| 13 | use TIOB\Importers\Plugin_Importer; |
| 14 | use TIOB\Importers\Theme_Mods_Importer; |
| 15 | use TIOB\Importers\Widgets_Importer; |
| 16 | use TIOB\Importers\Zelle_Importer; |
| 17 | use WP_REST_Request; |
| 18 | use WP_REST_Response; |
| 19 | use WP_REST_Server; |
| 20 | use FLBuilderModel; |
| 21 | |
| 22 | /** |
| 23 | * Class Rest_Server |
| 24 | */ |
| 25 | class Rest_Server { |
| 26 | /** |
| 27 | * Initialize the rest functionality. |
| 28 | * |
| 29 | * @return void |
| 30 | */ |
| 31 | public function init() { |
| 32 | add_action( 'rest_api_init', array( $this, 'register_endpoints' ) ); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Register endpoints. |
| 37 | */ |
| 38 | public function register_endpoints() { |
| 39 | register_rest_route( |
| 40 | Main::API_ROOT, |
| 41 | '/refresh_sites_data', |
| 42 | array( |
| 43 | 'methods' => WP_REST_Server::READABLE, |
| 44 | 'callback' => array( $this, 'get_sites_data' ), |
| 45 | 'permission_callback' => function () { |
| 46 | return current_user_can( 'manage_options' ); |
| 47 | }, |
| 48 | ) |
| 49 | ); |
| 50 | |
| 51 | register_rest_route( |
| 52 | Main::API_ROOT, |
| 53 | '/install_plugins', |
| 54 | array( |
| 55 | 'methods' => WP_REST_Server::EDITABLE, |
| 56 | 'callback' => array( $this, 'run_plugin_importer' ), |
| 57 | 'permission_callback' => function () { |
| 58 | return current_user_can( 'manage_options' ); |
| 59 | }, |
| 60 | ) |
| 61 | ); |
| 62 | |
| 63 | register_rest_route( |
| 64 | Main::API_ROOT, |
| 65 | '/import_content', |
| 66 | array( |
| 67 | 'methods' => WP_REST_Server::EDITABLE, |
| 68 | 'callback' => array( $this, 'run_xml_importer' ), |
| 69 | 'permission_callback' => function () { |
| 70 | return current_user_can( 'manage_options' ); |
| 71 | }, |
| 72 | ) |
| 73 | ); |
| 74 | register_rest_route( |
| 75 | Main::API_ROOT, |
| 76 | '/import_theme_mods', |
| 77 | array( |
| 78 | 'methods' => WP_REST_Server::EDITABLE, |
| 79 | 'callback' => array( $this, 'run_theme_mods_importer' ), |
| 80 | 'permission_callback' => function () { |
| 81 | return current_user_can( 'manage_options' ); |
| 82 | }, |
| 83 | ) |
| 84 | ); |
| 85 | register_rest_route( |
| 86 | Main::API_ROOT, |
| 87 | '/import_widgets', |
| 88 | array( |
| 89 | 'methods' => WP_REST_Server::EDITABLE, |
| 90 | 'callback' => array( $this, 'run_widgets_importer' ), |
| 91 | 'permission_callback' => function () { |
| 92 | return current_user_can( 'manage_options' ); |
| 93 | }, |
| 94 | ) |
| 95 | ); |
| 96 | register_rest_route( |
| 97 | Main::API_ROOT, |
| 98 | '/migrate_frontpage', |
| 99 | array( |
| 100 | 'methods' => WP_REST_Server::EDITABLE, |
| 101 | 'callback' => array( $this, 'run_front_page_migration' ), |
| 102 | 'permission_callback' => function () { |
| 103 | return current_user_can( 'manage_options' ); |
| 104 | }, |
| 105 | ) |
| 106 | ); |
| 107 | register_rest_route( |
| 108 | Main::API_ROOT, |
| 109 | '/dismiss_migration', |
| 110 | array( |
| 111 | 'methods' => WP_REST_Server::EDITABLE, |
| 112 | 'callback' => array( $this, 'dismiss_migration' ), |
| 113 | 'permission_callback' => function () { |
| 114 | return current_user_can( 'manage_options' ); |
| 115 | }, |
| 116 | ) |
| 117 | ); |
| 118 | register_rest_route( |
| 119 | Main::API_ROOT, |
| 120 | '/import_single_templates', |
| 121 | array( |
| 122 | 'methods' => WP_REST_Server::EDITABLE, |
| 123 | 'callback' => array( $this, 'import_templates' ), |
| 124 | 'permission_callback' => function () { |
| 125 | return current_user_can( 'manage_options' ); |
| 126 | }, |
| 127 | ) |
| 128 | ); |
| 129 | |
| 130 | register_rest_route( |
| 131 | Main::API_ROOT, |
| 132 | '/cleanup', |
| 133 | array( |
| 134 | 'methods' => WP_REST_Server::EDITABLE, |
| 135 | 'callback' => array( $this, 'run_cleanup' ), |
| 136 | 'permission_callback' => function () { |
| 137 | return current_user_can( 'manage_options' ); |
| 138 | }, |
| 139 | ) |
| 140 | ); |
| 141 | |
| 142 | register_rest_route( |
| 143 | Main::API_ROOT, |
| 144 | '/starter_order', |
| 145 | array( |
| 146 | 'methods' => WP_REST_Server::READABLE, |
| 147 | 'callback' => array( $this, 'get_starter_order' ), |
| 148 | 'args' => array( |
| 149 | 'builder' => array( |
| 150 | 'type' => 'string', |
| 151 | 'required' => false, |
| 152 | ), |
| 153 | ), |
| 154 | 'permission_callback' => function () { |
| 155 | return current_user_can( 'manage_options' ); |
| 156 | }, |
| 157 | ) |
| 158 | ); |
| 159 | |
| 160 | register_rest_route( |
| 161 | Main::API_ROOT, |
| 162 | '/starter_search', |
| 163 | array( |
| 164 | 'methods' => WP_REST_Server::READABLE, |
| 165 | 'callback' => array( $this, 'get_starter_search' ), |
| 166 | 'args' => array( |
| 167 | 'q' => array( |
| 168 | 'type' => 'string', |
| 169 | 'required' => true, |
| 170 | ), |
| 171 | 'builder' => array( |
| 172 | 'type' => 'string', |
| 173 | 'required' => false, |
| 174 | ), |
| 175 | ), |
| 176 | 'permission_callback' => function () { |
| 177 | return current_user_can( 'manage_options' ); |
| 178 | }, |
| 179 | ) |
| 180 | ); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * AI semantic search over the starter-site catalog. |
| 185 | * |
| 186 | * @param WP_REST_Request $request Request. |
| 187 | * |
| 188 | * @return WP_REST_Response |
| 189 | */ |
| 190 | public function get_starter_search( WP_REST_Request $request ) { |
| 191 | $builder = $request->get_param( 'builder' ); |
| 192 | $builder = ( 'elementor' === $builder ) ? 'elementor' : 'gutenberg'; |
| 193 | $query = (string) $request->get_param( 'q' ); |
| 194 | |
| 195 | return new WP_REST_Response( |
| 196 | array( |
| 197 | 'success' => true, |
| 198 | 'builder' => $builder, |
| 199 | 'order' => Starter_Ranking::search( $query, $builder ), |
| 200 | ) |
| 201 | ); |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Return the personalized starter-site order for a builder. |
| 206 | * |
| 207 | * @param WP_REST_Request $request Request. |
| 208 | * |
| 209 | * @return WP_REST_Response |
| 210 | */ |
| 211 | public function get_starter_order( WP_REST_Request $request ) { |
| 212 | $builder = $request->get_param( 'builder' ); |
| 213 | $builder = ( 'elementor' === $builder ) ? 'elementor' : 'gutenberg'; |
| 214 | |
| 215 | return new WP_REST_Response( |
| 216 | array( |
| 217 | 'success' => true, |
| 218 | 'builder' => $builder, |
| 219 | 'order' => Starter_Ranking::get_order( $builder ), |
| 220 | ) |
| 221 | ); |
| 222 | } |
| 223 | |
| 224 | public function run_cleanup() { |
| 225 | |
| 226 | return new WP_REST_Response( |
| 227 | array( |
| 228 | 'success' => true, |
| 229 | 'data' => Manager::instance()->do_cleanup(), |
| 230 | ) |
| 231 | ); |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Refreshes the sites data. |
| 236 | */ |
| 237 | public function get_sites_data() { |
| 238 | return new WP_REST_Response( |
| 239 | array( |
| 240 | 'success' => true, |
| 241 | 'data' => Main::instance()->admin->get_sites_data(), |
| 242 | ) |
| 243 | ); |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Run the plugin importer. |
| 248 | * |
| 249 | * @param WP_REST_Request $request the async request. |
| 250 | * |
| 251 | * @return WP_REST_Response |
| 252 | */ |
| 253 | public function run_plugin_importer( WP_REST_Request $request ) { |
| 254 | $plugin_importer = new Plugin_Importer(); |
| 255 | return $plugin_importer->install_plugins( $request ); |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Run the XML importer. |
| 260 | * |
| 261 | * @param WP_REST_Request $request the async request. |
| 262 | * |
| 263 | * @return WP_REST_Response |
| 264 | */ |
| 265 | public function run_xml_importer( WP_REST_Request $request ) { |
| 266 | $content_importer = new Content_Importer(); |
| 267 | $import = $content_importer->import_remote_xml( $request ); |
| 268 | set_transient( 'ti_tpc_should_flush_permalinks', 'yes', 12 * HOUR_IN_SECONDS ); |
| 269 | if ( get_option( 'neve_notice_dismissed', 'no' ) !== 'yes' ) { |
| 270 | update_option( 'neve_notice_dismissed', 'yes' ); |
| 271 | } |
| 272 | |
| 273 | return $import; |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Run the theme mods importer. |
| 278 | * |
| 279 | * @param WP_REST_Request $request the async request. |
| 280 | * |
| 281 | * @return WP_REST_Response |
| 282 | */ |
| 283 | public function run_theme_mods_importer( WP_REST_Request $request ) { |
| 284 | $customizer_importer = new Theme_Mods_Importer(); |
| 285 | return $customizer_importer->import_theme_mods( $request ); |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Run the widgets importer. |
| 290 | * |
| 291 | * @param WP_REST_Request $request the async request. |
| 292 | * |
| 293 | * @return WP_REST_Response |
| 294 | */ |
| 295 | public function run_widgets_importer( WP_REST_Request $request ) { |
| 296 | $widgets_importer = new Widgets_Importer(); |
| 297 | $import = $widgets_importer->import_widgets( $request ); |
| 298 | |
| 299 | set_theme_mod( 'ti_content_imported', 'yes' ); |
| 300 | |
| 301 | return $import; |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * Run front page migration. |
| 306 | * |
| 307 | * @param WP_REST_Request $request |
| 308 | * |
| 309 | * @return WP_REST_Response |
| 310 | */ |
| 311 | public function run_front_page_migration( WP_REST_Request $request ) { |
| 312 | $params = $request->get_json_params(); |
| 313 | |
| 314 | if ( ! isset( $params['template'] ) || ! isset( $params['template_name'] ) ) { |
| 315 | return new WP_REST_Response( |
| 316 | array( |
| 317 | 'data' => 'ti__ob_rest_err_5', |
| 318 | 'success' => false, |
| 319 | ) |
| 320 | ); |
| 321 | } |
| 322 | |
| 323 | $migrator = new Zelle_Importer(); |
| 324 | $old_theme = get_theme_mod( 'ti_prev_theme', 'ti_onboarding_undefined' ); |
| 325 | $import = $migrator->import_zelle_frontpage( $params['template'], $old_theme ); |
| 326 | |
| 327 | if ( is_wp_error( $import ) ) { |
| 328 | return new WP_REST_Response( |
| 329 | array( |
| 330 | 'data' => $import->get_error_code(), |
| 331 | 'success' => false, |
| 332 | ) |
| 333 | ); |
| 334 | } |
| 335 | |
| 336 | return new WP_REST_Response( |
| 337 | array( |
| 338 | 'success' => true, |
| 339 | 'data' => $import, |
| 340 | ) |
| 341 | ); |
| 342 | } |
| 343 | |
| 344 | /** |
| 345 | * Dismiss the front page migration notice. |
| 346 | * |
| 347 | * @param WP_REST_Request $request |
| 348 | * |
| 349 | * @return WP_REST_Response |
| 350 | */ |
| 351 | public function dismiss_migration( WP_REST_Request $request ) { |
| 352 | $params = $request->get_json_params(); |
| 353 | if ( ! isset( $params['theme_mod'] ) ) { |
| 354 | return new WP_REST_Response( |
| 355 | array( |
| 356 | 'data' => 'ti__ob_rest_err_8', |
| 357 | 'success' => false, |
| 358 | ) |
| 359 | ); |
| 360 | } |
| 361 | set_theme_mod( $params['theme_mod'], 'yes' ); |
| 362 | |
| 363 | return new WP_REST_Response( array( 'success' => true ) ); |
| 364 | } |
| 365 | |
| 366 | /** |
| 367 | * Import Templates. |
| 368 | * |
| 369 | * @param WP_REST_Request $request |
| 370 | * @return WP_REST_Response |
| 371 | */ |
| 372 | public function import_templates( WP_REST_Request $request ) { |
| 373 | $params = $request->get_json_params(); |
| 374 | $imported = array(); |
| 375 | foreach ( $params as $template ) { |
| 376 | $id = $this->insert_single_template( $template ); |
| 377 | |
| 378 | // @phpstan-ignore-next-line |
| 379 | if ( $id instanceof \WP_Error ) { |
| 380 | return new WP_REST_Response( |
| 381 | array( |
| 382 | 'success' => false, |
| 383 | 'message' => $id->get_error_message(), |
| 384 | ) |
| 385 | ); |
| 386 | } |
| 387 | |
| 388 | $imported[] = array( |
| 389 | 'title' => get_the_title( $id ), |
| 390 | 'url' => get_post_permalink( $id ), |
| 391 | 'edit' => add_query_arg( |
| 392 | array( |
| 393 | 'post' => $id, |
| 394 | 'action' => 'edit', |
| 395 | ), |
| 396 | admin_url( 'post.php' ) |
| 397 | ), |
| 398 | ); |
| 399 | } |
| 400 | |
| 401 | $count = get_option( Admin::IMPORTED_TEMPLATES_COUNT_OPT, 0 ); |
| 402 | update_option( Admin::IMPORTED_TEMPLATES_COUNT_OPT, intval( $count ) + sizeof( $imported ) ); |
| 403 | |
| 404 | error_log( var_export( get_option( Admin::IMPORTED_TEMPLATES_COUNT_OPT, 0 ), true ) ); |
| 405 | |
| 406 | return new WP_REST_Response( |
| 407 | array( |
| 408 | 'success' => true, |
| 409 | 'pages' => $imported, |
| 410 | ) |
| 411 | ); |
| 412 | } |
| 413 | |
| 414 | /** |
| 415 | * Insert Single Template |
| 416 | * |
| 417 | * @param $template |
| 418 | * @return int |
| 419 | */ |
| 420 | private function insert_single_template( $template ) { |
| 421 | add_filter( |
| 422 | 'wp_insert_post_data', |
| 423 | array( $this, 'parse_global_colors_unicode' ), |
| 424 | 10, |
| 425 | 2 |
| 426 | ); |
| 427 | |
| 428 | $page_template = ''; |
| 429 | $post_type = 'page'; |
| 430 | |
| 431 | if ( 'gutenberg' !== $template['template_type'] ) { |
| 432 | $page_template = 'page-templates/template-pagebuilder-full-width.php'; |
| 433 | } |
| 434 | |
| 435 | if ( isset( $template['meta'] ) ) { |
| 436 | $meta = json_decode( $template['meta'], true ); |
| 437 | if ( isset( $meta['_wp_page_template'] ) ) { |
| 438 | $page_template = $meta['_wp_page_template']; |
| 439 | } |
| 440 | |
| 441 | if ( |
| 442 | isset( $meta['postType'] ) && |
| 443 | in_array( $meta['postType'], Editor::get_allowed_post_types(), true ) && |
| 444 | post_type_exists( $meta['postType'] ) |
| 445 | ) { |
| 446 | $post_type = $meta['postType']; |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | if ( 'beaver' === $template['template_type'] ) { |
| 451 | if ( class_exists( 'FLBuilderModel' ) ) { |
| 452 | $response = TI_Beaver::get_template_content( $template['template_id'] ); |
| 453 | } |
| 454 | |
| 455 | $post_id = wp_insert_post( |
| 456 | array( |
| 457 | 'post_title' => wp_strip_all_tags( $template['template_name'] ), |
| 458 | 'post_status' => 'publish', |
| 459 | 'post_type' => 'page', |
| 460 | 'page_template' => $page_template, |
| 461 | 'meta_input' => isset( $template['meta'] ) ? json_decode( $template['meta'], true ) : array(), |
| 462 | ) |
| 463 | ); |
| 464 | |
| 465 | if ( class_exists( 'FLBuilderModel' ) ) { |
| 466 | if ( isset( $response->nodes ) ) { |
| 467 | FLBuilderModel::update_layout_data( $response->nodes, 'published', $post_id ); |
| 468 | } |
| 469 | |
| 470 | if ( isset( $response->settings ) ) { |
| 471 | FLBuilderModel::update_layout_settings( $response->settings, 'published', $post_id ); |
| 472 | } |
| 473 | update_post_meta( $post_id, '_fl_builder_enabled', true ); |
| 474 | } |
| 475 | |
| 476 | return $post_id; |
| 477 | } |
| 478 | |
| 479 | if ( 'elementor' === $template['template_type'] ) { |
| 480 | return wp_insert_post( |
| 481 | array( |
| 482 | 'post_title' => wp_strip_all_tags( $template['template_name'] ), |
| 483 | 'post_status' => 'publish', |
| 484 | 'post_type' => 'page', |
| 485 | 'page_template' => $page_template, |
| 486 | 'meta_input' => array_merge( |
| 487 | array( |
| 488 | '_elementor_data' => $template['content'], |
| 489 | '_elementor_template_type' => 'wp-page', |
| 490 | '_elementor_edit_mode' => 'builder', |
| 491 | ), |
| 492 | isset( $template['meta'] ) ? json_decode( $template['meta'], true ) : array() |
| 493 | ), |
| 494 | ) |
| 495 | ); |
| 496 | } |
| 497 | |
| 498 | return wp_insert_post( |
| 499 | array( |
| 500 | 'post_title' => wp_strip_all_tags( $template['template_name'] ), |
| 501 | 'post_content' => wp_kses_post( $template['content'] ), |
| 502 | 'post_status' => 'publish', |
| 503 | 'post_type' => $post_type, |
| 504 | 'page_template' => $page_template, |
| 505 | 'meta_input' => isset( $template['meta'] ) ? json_decode( $template['meta'], true ) : array(), |
| 506 | ) |
| 507 | ); |
| 508 | } |
| 509 | |
| 510 | /** |
| 511 | * Parse global colors unicode. |
| 512 | * |
| 513 | * @param array $data post data |
| 514 | * @param array $post_arr post array. |
| 515 | * |
| 516 | * @return array |
| 517 | */ |
| 518 | public function parse_global_colors_unicode( $data, $post_arr ) { |
| 519 | $data['post_content'] = str_replace( 'var(\\u002d\\u002dnv', 'var(--nv', $data['post_content'] ); |
| 520 | |
| 521 | return $data; |
| 522 | } |
| 523 | } |
| 524 |