multilanguage
1 year ago
recommendations
3 months ago
colibri.php
1 year ago
contact-form.php
1 year ago
entity.php
4 years ago
get-body-class.php
1 year ago
get-classic-page-template.php
1 year ago
get-page-query.php
1 year ago
get-page-title.php
1 year ago
get-post-content.php
1 year ago
get-post-styles.php
1 year ago
index.php
11 months ago
kubio-cloud.php
1 year ago
page-templates.php
4 years ago
save-template-parts-filter.php
1 year ago
subscribe-form.php
4 years ago
typekit.php
4 years ago
update-settings-flags.php
1 year ago
colibri.php
506 lines
| 1 | <?php |
| 2 | |
| 3 | use IlluminateAgnostic\Arr\Support\Arr; |
| 4 | use Kubio\Flags; |
| 5 | use Kubio\Core\Utils; |
| 6 | |
| 7 | add_action( |
| 8 | 'rest_api_init', |
| 9 | function () { |
| 10 | $namespace = 'kubio/v1'; |
| 11 | |
| 12 | register_rest_route( |
| 13 | $namespace, |
| 14 | '/enable-theme', |
| 15 | array( |
| 16 | 'methods' => 'GET', |
| 17 | 'callback' => 'kubio_enable_theme', |
| 18 | 'permission_callback' => function () { |
| 19 | return current_user_can( 'edit_theme_options' ); |
| 20 | }, |
| 21 | ) |
| 22 | ); |
| 23 | |
| 24 | register_rest_route( |
| 25 | $namespace, |
| 26 | '/colibri-data-export', |
| 27 | array( |
| 28 | 'methods' => 'GET', |
| 29 | 'callback' => 'kubio_colibri_data_export', |
| 30 | 'permission_callback' => function () { |
| 31 | return current_user_can( 'edit_theme_options' ); |
| 32 | }, |
| 33 | ) |
| 34 | ); |
| 35 | |
| 36 | register_rest_route( |
| 37 | $namespace, |
| 38 | '/colibri-data-export-done', |
| 39 | array( |
| 40 | 'methods' => 'GET', |
| 41 | 'callback' => 'kubio_colibri_data_export_done', |
| 42 | 'permission_callback' => function () { |
| 43 | return current_user_can( 'edit_theme_options' ); |
| 44 | }, |
| 45 | ) |
| 46 | ); |
| 47 | |
| 48 | register_rest_route( |
| 49 | $namespace, |
| 50 | '/snippet', |
| 51 | array( |
| 52 | 'methods' => 'POST', |
| 53 | 'callback' => 'kubio_save_block_snippet', |
| 54 | 'permission_callback' => function () { |
| 55 | return current_user_can( 'edit_theme_options' ); |
| 56 | }, |
| 57 | ) |
| 58 | ); |
| 59 | |
| 60 | register_rest_route( |
| 61 | $namespace, |
| 62 | '/snippet/update', |
| 63 | array( |
| 64 | 'methods' => 'POST', |
| 65 | 'callback' => 'kubio_update_block_snippet', |
| 66 | 'permission_callback' => function () { |
| 67 | return current_user_can( 'edit_theme_options' ); |
| 68 | }, |
| 69 | ) |
| 70 | ); |
| 71 | |
| 72 | register_rest_route( |
| 73 | $namespace, |
| 74 | '/update-generated-ai-site-content', |
| 75 | array( |
| 76 | 'methods' => 'POST', |
| 77 | 'callback' => 'kubio_update_generated_ai_site_content', |
| 78 | 'permission_callback' => function () { |
| 79 | return current_user_can( 'edit_theme_options' ); |
| 80 | }, |
| 81 | ) |
| 82 | ); |
| 83 | register_rest_route( |
| 84 | $namespace, |
| 85 | '/update-generated-ai-site-content-by-style', |
| 86 | array( |
| 87 | 'methods' => 'POST', |
| 88 | 'callback' => 'kubio_update_generated_ai_site_content_by_style', |
| 89 | 'permission_callback' => function () { |
| 90 | return current_user_can( 'edit_theme_options' ); |
| 91 | }, |
| 92 | ) |
| 93 | ); |
| 94 | } |
| 95 | ); |
| 96 | |
| 97 | |
| 98 | /** |
| 99 | * API callback for saving a snippet; It creates a zip archive with the html template, the screenshot and the assets. |
| 100 | * Also sends the zip archive to Kubio cloud. |
| 101 | * |
| 102 | * @param WP_REST_Request $request |
| 103 | * @return void |
| 104 | */ |
| 105 | function kubio_save_block_snippet( WP_REST_Request $request ) { |
| 106 | if ( ! class_exists( 'ZipArchive' ) ) { |
| 107 | wp_send_json_error( __( 'Zip Export not supported.', 'kubio' ) ); |
| 108 | } |
| 109 | |
| 110 | $name = $request->get_param( 'name' ); |
| 111 | $category = $request->get_param( 'category' ); |
| 112 | $snippet = $request->get_param( 'snippet' ); |
| 113 | $screenshot = $request->get_param( 'screenshot' ); |
| 114 | $global_data = $request->get_param( 'globalData' ); |
| 115 | $zip_path = get_temp_dir() . $name . '.zip'; |
| 116 | |
| 117 | $screenshot_ext = explode( ';base64,', $screenshot ); |
| 118 | $screenshot_ext = explode( 'data:image/', $screenshot_ext[0] ); |
| 119 | |
| 120 | if ( empty( $screenshot_ext[1] ) ) { |
| 121 | wp_send_json_error( __( 'Bad screenshot.', 'kubio' ) ); |
| 122 | } |
| 123 | |
| 124 | // allow only letters, numbers, underscore, dashe and space characters. |
| 125 | if ( preg_match( '/[^A-Za-z0-9_ \-]/', $name ) || strlen( $name ) > 100 ) { |
| 126 | wp_send_json_error( __( 'Bad name.', 'kubio' ) ); |
| 127 | } |
| 128 | |
| 129 | $zip = kubio_create_snippet_zip( $zip_path, $snippet, $screenshot, $global_data ); |
| 130 | |
| 131 | $apiKey = Flags::get( 'kubio_cloud_api_key' ); |
| 132 | |
| 133 | $request_url = add_query_arg( |
| 134 | array( |
| 135 | 'filename' => $name, |
| 136 | 'category_id' => $category, |
| 137 | 'block' => $snippet[0], |
| 138 | ), |
| 139 | Utils::getSnippetsURL( '/save' ) |
| 140 | ); |
| 141 | |
| 142 | $response = wp_remote_post( |
| 143 | $request_url, |
| 144 | array( |
| 145 | 'headers' => array( |
| 146 | 'X-Authorization' => $apiKey, |
| 147 | 'accept' => 'application/json', |
| 148 | 'content-type' => 'application/binary', |
| 149 | ), |
| 150 | 'body' => file_get_contents( $zip_path ), |
| 151 | ) |
| 152 | ); |
| 153 | |
| 154 | $response_code = wp_remote_retrieve_response_code( $response ); |
| 155 | |
| 156 | if ( 200 !== $response_code ) { |
| 157 | $errorMessage = wp_remote_retrieve_body( $response ); |
| 158 | wp_send_json_error( $errorMessage , $response_code); |
| 159 | } |
| 160 | |
| 161 | $body = wp_remote_retrieve_body( $response ); |
| 162 | |
| 163 | wp_send_json_success( json_decode( $body ) ); |
| 164 | } |
| 165 | |
| 166 | function kubio_update_block_snippet( WP_REST_Request $request ) { |
| 167 | if ( ! class_exists( 'ZipArchive' ) ) { |
| 168 | wp_send_json_error( __( 'Zip Export not supported.', 'kubio' ) ); |
| 169 | } |
| 170 | |
| 171 | $id = $request->get_param( 'id' ); |
| 172 | $name = $request->get_param( 'name' ); |
| 173 | $category = $request->get_param( 'category' ); |
| 174 | $snippet = $request->get_param( 'snippet' ); |
| 175 | $screenshot = $request->get_param( 'screenshot' ); |
| 176 | $global_data = $request->get_param( 'globalData' ); |
| 177 | |
| 178 | $zip_path = get_temp_dir() . $name . '.zip'; |
| 179 | |
| 180 | if ( empty( $id ) ) { |
| 181 | wp_send_json_error( __( 'No id.', 'kubio' ) ); |
| 182 | } |
| 183 | |
| 184 | // allow only letters, numbers, underscore, dashe and space characters. |
| 185 | if ( preg_match( '/[^A-Za-z0-9_ \-]/', $name ) || strlen( $name ) > 100 ) { |
| 186 | wp_send_json_error( __( 'Bad name.', 'kubio' ) ); |
| 187 | } |
| 188 | |
| 189 | $zip = kubio_create_snippet_zip( $zip_path, $snippet, $screenshot, $global_data ); |
| 190 | |
| 191 | $apiKey = Flags::get( 'kubio_cloud_api_key' ); |
| 192 | |
| 193 | $args = array( |
| 194 | 'filename' => $name, |
| 195 | 'category_id' => $category, |
| 196 | 'block' => $snippet[0], |
| 197 | |
| 198 | ); |
| 199 | |
| 200 | $request_url = add_query_arg( |
| 201 | $args, |
| 202 | Utils::getSnippetsURL( '/' . $id . '/update' ) |
| 203 | ); |
| 204 | |
| 205 | $response = wp_remote_post( |
| 206 | $request_url, |
| 207 | array( |
| 208 | 'headers' => array( |
| 209 | 'X-Authorization' => $apiKey, |
| 210 | 'accept' => 'application/json', |
| 211 | 'content-type' => 'application/binary', |
| 212 | ), |
| 213 | 'body' => file_get_contents( $zip_path ), |
| 214 | ) |
| 215 | ); |
| 216 | |
| 217 | $response_code = wp_remote_retrieve_response_code( $response ); |
| 218 | |
| 219 | if ( 200 !== $response_code ) { |
| 220 | $errorMessage = wp_remote_retrieve_body($response); |
| 221 | wp_send_json_error( $errorMessage, $response_code); |
| 222 | } |
| 223 | |
| 224 | $body = wp_remote_retrieve_body( $response ); |
| 225 | |
| 226 | wp_send_json_success( json_decode( $body ) ); |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * This function creates a temporary zip file for the given snippet. |
| 231 | * |
| 232 | * @param $path |
| 233 | * @param $snippet |
| 234 | * @param $screenshot_data |
| 235 | * @return false|ZipArchive |
| 236 | */ |
| 237 | function kubio_create_snippet_zip( $path, $snippet, $screenshot_data, $global_data ) { |
| 238 | // create the zip archive and start adding. |
| 239 | $zip = new ZipArchive(); |
| 240 | if ( true !== $zip->open( $path, ZipArchive::CREATE | ZipArchive::OVERWRITE ) ) { |
| 241 | wp_send_json_error( __( 'Unable to open export file (archive) for writing.', 'kubio' ) ); |
| 242 | } |
| 243 | |
| 244 | // handle the screenshot data and add it to the zip |
| 245 | if ( preg_match( '/^data:image\/(\w+);base64,/', $screenshot_data, $screenshot_type ) ) { |
| 246 | $screenshot_data = substr( $screenshot_data, strpos( $screenshot_data, ',' ) + 1 ); |
| 247 | $screenshot_type = strtolower( $screenshot_type[1] ); // jpg, png, gif |
| 248 | if ( ! in_array( $screenshot_type, array( 'jpg', 'jpeg', 'gif', 'png' ) ) ) { |
| 249 | return false; |
| 250 | } |
| 251 | |
| 252 | $screenshot_data = str_replace( ' ', '+', $screenshot_data ); |
| 253 | $screenshot_data = base64_decode( $screenshot_data ); |
| 254 | |
| 255 | $zip->addFromString( 'screenshot.' . $screenshot_type, $screenshot_data ); |
| 256 | } |
| 257 | |
| 258 | $zip->addFromString( 'global.json', json_encode( $global_data, JSON_PRETTY_PRINT ) ); |
| 259 | |
| 260 | // from now on we search assets from the template, and we save them in the archive. |
| 261 | //$zip->addEmptyDir( 'assets' ); |
| 262 | |
| 263 | // search for media files by urls. |
| 264 | $upload_settings = wp_get_upload_dir(); |
| 265 | $pattern = '#' . str_replace( '/', '\/', $upload_settings['baseurl'] ) . '(.*?)"#'; |
| 266 | $pattern = str_replace( ':', '\:', $pattern ); |
| 267 | preg_match_all( $pattern, json_encode( $snippet, JSON_UNESCAPED_SLASHES ), $matches ); |
| 268 | |
| 269 | $uploaded_files = array(); |
| 270 | |
| 271 | if ( ! empty( $matches ) ) { |
| 272 | $uploaded_files = $matches[1]; |
| 273 | } |
| 274 | |
| 275 | // search media files by id. |
| 276 | $uploaded_files = kubio_parse_blocks_for_media_id_atts_and_stack( $snippet, $uploaded_files ); |
| 277 | |
| 278 | foreach ( $uploaded_files as $uploaded_file ) { |
| 279 | $file_path = $upload_settings['basedir'] . $uploaded_file; |
| 280 | |
| 281 | if ( file_exists( $file_path ) ) { |
| 282 | $file = file_get_contents( $file_path ); |
| 283 | $new_path = wp_normalize_path( 'assets' . $uploaded_file ); |
| 284 | $zip->addFromString( $new_path, $file ); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | // at last, save the snippet template and close the zip archive. |
| 289 | $parsed = str_replace( $upload_settings['baseurl'], '{{snippet_url}}', json_encode( $snippet, JSON_UNESCAPED_SLASHES ) ); |
| 290 | $zip->addFromString( 'template.json', $parsed ); |
| 291 | |
| 292 | $zip->close(); |
| 293 | return $zip; |
| 294 | } |
| 295 | |
| 296 | |
| 297 | /** |
| 298 | * This function searches for attributes that hold media files and adds them to the $uploaded_files stack. |
| 299 | * |
| 300 | * @param $block |
| 301 | * @param $uploaded_files |
| 302 | * @return mixed |
| 303 | */ |
| 304 | function kubio_parse_blocks_for_media_id_atts_and_stack( $block, $uploaded_files = false ) { |
| 305 | // $block[0] is the block name. |
| 306 | $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block[0] ); |
| 307 | |
| 308 | if ( ! empty( $block_type->supports['kubio'] ) ) { |
| 309 | $assets_importer_map = Arr::get( $block_type->supports['kubio'], 'assetsURLImporterMap', array() ); |
| 310 | |
| 311 | if ( ! empty( $assets_importer_map ) ) { |
| 312 | $id_attr = ''; |
| 313 | foreach ( $assets_importer_map as $url_attr => $import_map ) { |
| 314 | $id_attr = Arr::get( $import_map, 'assetIdToAttr' ); |
| 315 | } |
| 316 | |
| 317 | // $block[1] holds the attributes |
| 318 | $id = $block[1][ $id_attr ]; |
| 319 | |
| 320 | if ( ! empty( $id ) ) { |
| 321 | $url = wp_get_attachment_url( $id ); |
| 322 | $parsed = wp_parse_url( $url ); |
| 323 | $new_path = str_replace( '/wp-content/uploads', '', $parsed['path'] ); |
| 324 | if ( ! in_array( $new_path, $uploaded_files ) ) { |
| 325 | $uploaded_files[] = $new_path; |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | // at $block['2'] there should be an array of blocks as innerBlocks. |
| 332 | if ( ! empty( $block['2'] ) ) { |
| 333 | $uploaded_files = kubio_parse_blocks_for_media_id_atts_and_stack( $block['2'][0], $uploaded_files ); |
| 334 | } |
| 335 | |
| 336 | return $uploaded_files; |
| 337 | } |
| 338 | function kubio_update_generated_ai_site_content( WP_REST_Request $request) { |
| 339 | |
| 340 | $params = $request->get_param( 'params' ); |
| 341 | |
| 342 | $apiKey = Flags::get( 'kubio_cloud_api_key' ); |
| 343 | |
| 344 | $request_url = Utils::getCloudURL( '/api/generated-ai-sites/update-content' ); |
| 345 | |
| 346 | |
| 347 | $response = wp_remote_post( |
| 348 | $request_url, |
| 349 | array( |
| 350 | 'headers' => array( |
| 351 | 'X-Authorization' => $apiKey, |
| 352 | 'accept' => 'application/json', |
| 353 | 'content-type' => 'application/json', |
| 354 | ), |
| 355 | 'body' => json_encode( |
| 356 | array( |
| 357 | 'params' => array_merge($params, |
| 358 | array( |
| 359 | 'testing' => true |
| 360 | ) |
| 361 | ), |
| 362 | ) |
| 363 | ), |
| 364 | ) |
| 365 | ); |
| 366 | |
| 367 | $response_code = wp_remote_retrieve_response_code( $response ); |
| 368 | |
| 369 | if ( 200 !== $response_code ) { |
| 370 | $errorMessage = wp_remote_retrieve_body($response); |
| 371 | wp_send_json_error( $errorMessage, $response_code ); |
| 372 | } |
| 373 | |
| 374 | $body = wp_remote_retrieve_body( $response ); |
| 375 | |
| 376 | wp_send_json_success( json_decode( $body ) ); |
| 377 | } |
| 378 | function kubio_update_generated_ai_site_content_by_style( WP_REST_Request $request) { |
| 379 | |
| 380 | $params = $request->get_param( 'params' ); |
| 381 | |
| 382 | $apiKey = Flags::get( 'kubio_cloud_api_key' ); |
| 383 | |
| 384 | $request_url = Utils::getCloudURL( '/api/generated-ai-sites-by-style/update-content' ); |
| 385 | |
| 386 | $response = wp_remote_post( |
| 387 | $request_url, |
| 388 | array( |
| 389 | 'headers' => array( |
| 390 | 'X-Authorization' => $apiKey, |
| 391 | 'accept' => 'application/json', |
| 392 | 'content-type' => 'application/json', |
| 393 | ), |
| 394 | 'body' => json_encode( |
| 395 | array( |
| 396 | 'params' => array_merge($params, |
| 397 | array( |
| 398 | 'testing' => true |
| 399 | ) |
| 400 | ), |
| 401 | ) |
| 402 | ), |
| 403 | ) |
| 404 | ); |
| 405 | |
| 406 | $response_code = wp_remote_retrieve_response_code( $response ); |
| 407 | |
| 408 | if ( 200 !== $response_code ) { |
| 409 | $errorMessage = wp_remote_retrieve_body($response); |
| 410 | wp_send_json_error( $errorMessage, $response_code ); |
| 411 | } |
| 412 | |
| 413 | $body = wp_remote_retrieve_body( $response ); |
| 414 | |
| 415 | wp_send_json_success( json_decode( $body ) ); |
| 416 | } |
| 417 | |
| 418 | function kubio_get_installed_theme() { |
| 419 | $themes = wp_get_themes(); |
| 420 | $kubio_themes = array( 'kubio', 'elevate-wp' ); |
| 421 | $intersect = array_values( array_intersect( array_keys( $themes ), $kubio_themes ) ); |
| 422 | $theme = Arr::get( $intersect, 0, null ); |
| 423 | return $theme; |
| 424 | } |
| 425 | |
| 426 | function kubio_enable_theme( WP_REST_Request $data ) { |
| 427 | switch_theme( $data['name'] ); |
| 428 | return wp_send_json( array( 'switched' => $data['name'] ) ); |
| 429 | } |
| 430 | |
| 431 | function kubio_colibri_data_export_done() { |
| 432 | |
| 433 | // copy menus location from colibri to kubio; |
| 434 | $colibri_options = get_option( 'theme_mods_colibri-wp', array() ); |
| 435 | $kubio_options = get_option( 'theme_mods_kubio', array() ); |
| 436 | |
| 437 | $colibri_locations = Arr::get( $colibri_options, 'nav_menu_locations', array() ); |
| 438 | $kubio_locations = Arr::get( $kubio_options, 'nav_menu_locations', array() ); |
| 439 | |
| 440 | $locations_map = array( |
| 441 | 'header-menu' => 'header-menu', |
| 442 | 'footer-menu' => 'footer-menu', |
| 443 | 'footer-menu-1' => 'footer-menu-secondary', |
| 444 | 'header-menu-1' => 'header-menu-secondary', |
| 445 | ); |
| 446 | |
| 447 | foreach ( $colibri_locations as $location => $menu ) { |
| 448 | $location = Arr::get( $locations_map, $location, $location ); |
| 449 | $kubio_locations[ $location ] = $menu; |
| 450 | } |
| 451 | |
| 452 | Arr::set( $kubio_options, 'nav_menu_locations', $kubio_locations ); |
| 453 | |
| 454 | if ( $theme = kubio_get_installed_theme() ) { |
| 455 | update_option( "theme_mods_{$theme}", $kubio_options ); |
| 456 | switch_theme( $theme ); |
| 457 | |
| 458 | flush_rewrite_rules(); |
| 459 | update_option( 'theme_switched', false ); |
| 460 | } |
| 461 | |
| 462 | wp_send_json_success(); |
| 463 | } |
| 464 | |
| 465 | function kubio_colibri_data_export() { |
| 466 | |
| 467 | $theme = kubio_get_installed_theme(); |
| 468 | |
| 469 | if ( $theme ) { |
| 470 | ob_clean(); |
| 471 | |
| 472 | // templates are created with colibri-wp theme on kubio activation // |
| 473 | foreach ( get_block_templates( array(), 'wp_template' ) as $template ) { |
| 474 | wp_set_post_terms( $template->wp_id, $theme, 'wp_theme' ); |
| 475 | } |
| 476 | |
| 477 | echo wp_json_encode( |
| 478 | \ExtendBuilder\export_colibri_data( |
| 479 | array( 'exclude_generated' => true ), |
| 480 | false |
| 481 | ) |
| 482 | ); |
| 483 | switch_theme( $theme ); |
| 484 | } else { |
| 485 | wp_send_json_error( |
| 486 | array( |
| 487 | 'error' => 'kubio-not-installed', |
| 488 | ) |
| 489 | ); |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | add_filter( |
| 494 | 'colibri_page_builder/get_partial_details', |
| 495 | function ( $data ) { |
| 496 | $post = get_post( $data['id'] ); |
| 497 | $new_data = array( |
| 498 | 'title' => $post->post_title, |
| 499 | 'page_template' => $post->page_template, |
| 500 | 'parent' => $post->post_parent, |
| 501 | ); |
| 502 | return array_merge( $data, $new_data ); |
| 503 | }, |
| 504 | 100 |
| 505 | ); |
| 506 |