AI
9 months ago
admin-pages
1 year ago
api
3 months ago
blog
1 year ago
customizer
1 year ago
filters
1 month ago
full-site-editing
1 month ago
importer
1 year ago
integrations
1 month ago
menu
1 year ago
polyfills
1 year ago
preview
7 months ago
recommendations
1 month ago
shapes
2 years ago
shortcodes
1 year ago
src
1 month ago
add-edit-in-kubio.php
11 months ago
editor-assets.php
1 month ago
filters.php
1 year ago
frontend.php
1 year ago
global-data.php
1 year ago
init.php
1 year ago
kubio-block-library.php
1 month ago
kubio-editor.php
1 month ago
load.php
1 month ago
kubio-editor.php
1425 lines
| 1 | <?php |
| 2 | |
| 3 | use IlluminateAgnostic\Arr\Support\Arr; |
| 4 | use Kubio\AssetsDependencyInjector; |
| 5 | use Kubio\Core\LodashBasic; |
| 6 | use Kubio\Core\Utils; |
| 7 | use Kubio\Flags; |
| 8 | |
| 9 | function kubio_is_kubio_editor_page() { |
| 10 | global $pagenow; |
| 11 | |
| 12 | $is = false; |
| 13 | if ( substr( $pagenow, 0, -4 ) === 'admin' ) { |
| 14 | |
| 15 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 16 | $page = sanitize_text_field( Arr::get( $_REQUEST, 'page', false ) ); |
| 17 | |
| 18 | if ( $page === 'kubio' ) { |
| 19 | $is = true; |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | return apply_filters( 'kubio/is_kubio_editor_page', $is ); |
| 24 | } |
| 25 | |
| 26 | function kubio_edit_site_get_first_post_id( $loaded_id ) { |
| 27 | if ( ! $loaded_id ) { |
| 28 | $ids = get_posts( |
| 29 | array( |
| 30 | 'post_type' => 'page', |
| 31 | 'posts_per_page' => 1, |
| 32 | 'fields' => 'ids', |
| 33 | 'orderby' => 'date', |
| 34 | 'order' => 'ASC', |
| 35 | ) |
| 36 | ); |
| 37 | |
| 38 | if ( ! empty( $ids ) ) { |
| 39 | $loaded_id = $ids[0]; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | if ( ! $loaded_id ) { |
| 44 | $ids = get_posts( |
| 45 | array( |
| 46 | 'post_type' => 'post', |
| 47 | 'posts_per_page' => 1, |
| 48 | 'fields' => 'ids', |
| 49 | ) |
| 50 | ); |
| 51 | |
| 52 | if ( ! empty( $ids ) ) { |
| 53 | $loaded_id = $ids[0]; |
| 54 | } |
| 55 | } |
| 56 | return $loaded_id; |
| 57 | } |
| 58 | //this should also treat the template parts case. But at the moment you can't preview template parts but if will in the future |
| 59 | //the logic should work the same |
| 60 | function kubio_edit_site_get_template_id( $loaded_id, $postType ) { |
| 61 | |
| 62 | $parts = explode( '//', $loaded_id ); |
| 63 | if ( count( $parts ) !== 2 ) { |
| 64 | return null; |
| 65 | } |
| 66 | $templateName = $parts[1]; |
| 67 | $query = array( |
| 68 | 'name' => $templateName, |
| 69 | 'post_type' => $postType, |
| 70 | 'fields' => 'ids', |
| 71 | 'post_status' => 'publish', |
| 72 | 'posts_per_page' => 1, |
| 73 | ); |
| 74 | $ids = get_posts( $query ); |
| 75 | $resultPostId = null; |
| 76 | if ( ! empty( $ids ) ) { |
| 77 | $resultPostId = $ids[0]; |
| 78 | } |
| 79 | return $resultPostId; |
| 80 | } |
| 81 | function kubio_edit_site_get_edited_entity() { |
| 82 | $pag_on_front = intval( get_option( 'page_on_front' ) ); |
| 83 | |
| 84 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 85 | $loaded_id = isset( $_GET['postId'] ) ? intval( $_GET['postId'] ) : $pag_on_front; |
| 86 | |
| 87 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.MissingUnslash |
| 88 | $post_type = isset( $_GET['postType'] ) ? sanitize_text_field( $_GET['postType'] ) : null; |
| 89 | |
| 90 | if ( ! post_type_exists( $post_type ) || ! is_numeric( $loaded_id ) ) { |
| 91 | return array(); |
| 92 | } |
| 93 | |
| 94 | //when you have latest post set as your home page and you enter the builder without the post id the loaded_id will be 0 |
| 95 | //in this case we'll load the index template |
| 96 | if ( $loaded_id === 0 ) { |
| 97 | $themeName = get_stylesheet(); |
| 98 | $indexTemplateId = strtr( ':theme//index', array( ':theme' => $themeName ) ); |
| 99 | $loaded_id = $indexTemplateId; |
| 100 | $post_type = 'wp_template'; |
| 101 | } |
| 102 | |
| 103 | $templatePostId = null; |
| 104 | if ( ! ! $loaded_id && ! is_numeric( $loaded_id ) ) { |
| 105 | $templatePostId = $loaded_id; |
| 106 | $loaded_id = kubio_edit_site_get_template_id( $loaded_id, $post_type ); |
| 107 | } |
| 108 | if ( ! $loaded_id || ! is_numeric( $loaded_id ) ) { |
| 109 | $loaded_id = kubio_edit_site_get_first_post_id( $loaded_id ); |
| 110 | } |
| 111 | |
| 112 | if ( $loaded_id ) { |
| 113 | $entity = get_post( $loaded_id ); |
| 114 | |
| 115 | return array( |
| 116 | 'path' => get_permalink( $loaded_id ), |
| 117 | 'context' => array( |
| 118 | 'postType' => $entity ? $entity->post_type : '', |
| 119 | 'postId' => $templatePostId ? $templatePostId : $loaded_id, |
| 120 | 'query' => '', |
| 121 | 'postTitle' => $entity ? $entity->post_title : '', |
| 122 | 'title' => $entity ? $entity->post_title : '', |
| 123 | ), |
| 124 | 'slug' => $entity ? $entity->post_name : '', |
| 125 | 'label' => $entity ? $entity->post_title : '', |
| 126 | ); |
| 127 | } |
| 128 | |
| 129 | return array(); |
| 130 | } |
| 131 | |
| 132 | |
| 133 | function kubio_get_patterns_categories() { |
| 134 | return array( |
| 135 | array( |
| 136 | 'label' => __( 'Hero accent', 'kubio' ), |
| 137 | 'name' => 'kubio-content/hero accent', |
| 138 | ), |
| 139 | |
| 140 | array( |
| 141 | 'label' => __( 'About', 'kubio' ), |
| 142 | 'name' => 'kubio-content/about', |
| 143 | ), |
| 144 | |
| 145 | array( |
| 146 | 'label' => __( 'Features', 'kubio' ), |
| 147 | 'name' => 'kubio-content/features', |
| 148 | ), |
| 149 | |
| 150 | array( |
| 151 | 'label' => __( 'Content', 'kubio' ), |
| 152 | 'name' => 'kubio-content/content', |
| 153 | ), |
| 154 | |
| 155 | array( |
| 156 | 'label' => __( 'Call to action', 'kubio' ), |
| 157 | 'name' => 'kubio-content/cta', |
| 158 | ), |
| 159 | array( |
| 160 | 'label' => __( 'Blog', 'kubio' ), |
| 161 | 'name' => 'kubio-content/blog', |
| 162 | ), |
| 163 | |
| 164 | array( |
| 165 | 'label' => __( 'Counters', 'kubio' ), |
| 166 | 'name' => 'kubio-content/counters', |
| 167 | ), |
| 168 | |
| 169 | array( |
| 170 | 'label' => __( 'Portfolio', 'kubio' ), |
| 171 | 'name' => 'kubio-content/portfolio', |
| 172 | ), |
| 173 | |
| 174 | array( |
| 175 | 'label' => __( 'Photo gallery', 'kubio' ), |
| 176 | 'name' => 'kubio-content/photo gallery', |
| 177 | ), |
| 178 | |
| 179 | array( |
| 180 | 'label' => __( 'Testimonials', 'kubio' ), |
| 181 | 'name' => 'kubio-content/testimonials', |
| 182 | ), |
| 183 | |
| 184 | array( |
| 185 | 'label' => __( 'Clients', 'kubio' ), |
| 186 | 'name' => 'kubio-content/clients', |
| 187 | ), |
| 188 | |
| 189 | array( |
| 190 | 'label' => __( 'Team', 'kubio' ), |
| 191 | 'name' => 'kubio-content/team', |
| 192 | ), |
| 193 | |
| 194 | array( |
| 195 | 'label' => __( 'Contact', 'kubio' ), |
| 196 | 'name' => 'kubio-content/contact', |
| 197 | ), |
| 198 | |
| 199 | array( |
| 200 | 'label' => __( 'F.A.Q.', 'kubio' ), |
| 201 | 'name' => 'kubio-content/f.a.q.', |
| 202 | ), |
| 203 | |
| 204 | array( |
| 205 | 'label' => __( 'Pricing', 'kubio' ), |
| 206 | 'name' => 'kubio-content/pricing', |
| 207 | ), |
| 208 | array( |
| 209 | 'label' => __( 'Inner Headers', 'kubio' ), |
| 210 | 'name' => 'kubio-header/inner headers', |
| 211 | ), |
| 212 | |
| 213 | array( |
| 214 | 'label' => __( 'Headers', 'kubio' ), |
| 215 | 'name' => 'kubio-header/headers', |
| 216 | ), |
| 217 | |
| 218 | array( |
| 219 | 'label' => __( 'Footers', 'kubio' ), |
| 220 | 'name' => 'kubio-footer/footers', |
| 221 | ), |
| 222 | ); |
| 223 | } |
| 224 | |
| 225 | function kubio_get_editor_style( $get_css = false, $skiped_handlers = array() ) { |
| 226 | $style_handles = array( |
| 227 | 'wp-edit-blocks', |
| 228 | 'wp-block-editor', |
| 229 | 'wp-block-library', |
| 230 | 'wp-block-library-theme', |
| 231 | 'kubio-editor', |
| 232 | 'kubio-controls', |
| 233 | 'kubio-utils', |
| 234 | 'kubio-format-library', |
| 235 | 'kubio-pro', |
| 236 | 'kubio-block-library-editor', |
| 237 | 'kubio-third-party-blocks', |
| 238 | 'kubio-icons', |
| 239 | ); |
| 240 | |
| 241 | if ( kubio_wpml_is_active() ) { |
| 242 | $styles_to_copy = array( |
| 243 | 'legacy-dropdown', |
| 244 | 'legacy-dropdown-click', |
| 245 | 'legacy-list-horizontal', |
| 246 | 'legacy-list-vertical', |
| 247 | 'legacy-post-translations', |
| 248 | 'menu-item', |
| 249 | ); |
| 250 | |
| 251 | foreach ( $styles_to_copy as $style ) { |
| 252 | $style_handles[] = "kubio-copy-of-wpml-{$style}"; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | $style_handles = apply_filters( 'kubio/kubio_get_editor_style/style_handles', $style_handles ); |
| 257 | |
| 258 | $wp_styles = wp_styles(); |
| 259 | foreach ( array_keys( $wp_styles->registered ) as $handle ) { |
| 260 | if ( strpos( $handle, AssetsDependencyInjector::KUBIO_DEPENENCY_PREFIX ) === 0 ) { |
| 261 | $style_handles[] = $handle; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | foreach ( $skiped_handlers as $index => $handler ) { |
| 266 | $handler = preg_replace( '#(.*?)-inline-css$#', '$1', $handler ); |
| 267 | $handler = preg_replace( '#(.*?)-css$#', '$1', $handler ); |
| 268 | $skiped_handlers[ $index ] = $handler; |
| 269 | } |
| 270 | |
| 271 | $skiped_handlers = array_unique( $skiped_handlers ); |
| 272 | $style_handles = array_diff( $style_handles, $skiped_handlers ); |
| 273 | |
| 274 | $block_registry = WP_Block_Type_Registry::get_instance(); |
| 275 | |
| 276 | foreach ( $block_registry->get_all_registered() as $block_type ) { |
| 277 | if ( ! empty( $block_type->style ) ) { |
| 278 | $style_handles[] = $block_type->style; |
| 279 | } |
| 280 | |
| 281 | if ( ! empty( $block_type->editor_style ) ) { |
| 282 | $style_handles[] = $block_type->editor_style; |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | $style_handles = Arr::flatten( $style_handles ); |
| 287 | $style_handles = array_values( array_unique( $style_handles ) ); |
| 288 | $done = wp_styles()->done; |
| 289 | ob_start(); |
| 290 | |
| 291 | ?> |
| 292 | <style> |
| 293 | <?php |
| 294 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 295 | echo wp_get_global_stylesheet(); |
| 296 | ?> |
| 297 | </style> |
| 298 | <?php |
| 299 | wp_styles()->done = $skiped_handlers; |
| 300 | wp_styles()->do_items( $style_handles ); |
| 301 | wp_styles()->done = $done; |
| 302 | |
| 303 | $style = ob_get_clean(); |
| 304 | |
| 305 | if ( $get_css ) { |
| 306 | $style = preg_replace( "#<link(.*)href='(.*?)'(.*)/>#", '@import url($2);', $style ); |
| 307 | $style = preg_replace( '#<style(.*)>#', '', $style ); |
| 308 | $style = preg_replace( '#</style>#', '', $style ); |
| 309 | } |
| 310 | |
| 311 | return $style; |
| 312 | } |
| 313 | |
| 314 | function kubio_get_editor_scripts( $skiped_handlers = array() ) { |
| 315 | $script_handles = kubio_get_frontend_scripts(); |
| 316 | |
| 317 | $wp_scripts = wp_scripts(); |
| 318 | foreach ( array_keys( $wp_scripts->registered ) as $handle ) { |
| 319 | if ( strpos( $handle, AssetsDependencyInjector::KUBIO_DEPENENCY_PREFIX ) === 0 ) { |
| 320 | array_unshift( $script_handles, $handle ); |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | foreach ( $skiped_handlers as $index => $handler ) { |
| 325 | $handler = preg_replace( '#(.*?)-js-after#', '$1', $handler ); |
| 326 | $handler = preg_replace( '#(.*?)-js-before#', '$1', $handler ); |
| 327 | $handler = preg_replace( '#(.*?)-js-translations#', '$1', $handler ); |
| 328 | $handler = preg_replace( '#(.*?)-js-extra#', '$1', $handler ); |
| 329 | $skiped_handlers[ $index ] = $handler; |
| 330 | } |
| 331 | |
| 332 | $script = ''; |
| 333 | $done = wp_scripts()->done; |
| 334 | ob_start(); |
| 335 | wp_scripts()->done = $skiped_handlers; |
| 336 | wp_scripts()->do_items( $script_handles ); |
| 337 | wp_scripts()->done = $done; |
| 338 | $script = ob_get_clean(); |
| 339 | |
| 340 | return $script; |
| 341 | } |
| 342 | |
| 343 | function kubio_enqueue_editor_page_assets() { |
| 344 | // Editor default styles |
| 345 | wp_enqueue_media(); |
| 346 | wp_enqueue_style( 'wp-block-editor' ); |
| 347 | wp_enqueue_style( 'wp-format-library' ); |
| 348 | wp_enqueue_style( 'kubio-format-library' ); |
| 349 | wp_enqueue_script( 'kubio-editor' ); |
| 350 | wp_enqueue_script( 'kubio-block-library' ); |
| 351 | wp_enqueue_style( 'kubio-admin-panel' ); |
| 352 | wp_enqueue_style( 'kubio-ai' ); |
| 353 | |
| 354 | wp_enqueue_style( 'kubio-editor' ); |
| 355 | wp_enqueue_style( 'kubio-icons' ); |
| 356 | wp_enqueue_style( 'kubio-advanced-panel' ); |
| 357 | wp_enqueue_style( 'kubio-block-library-editor' ); |
| 358 | wp_enqueue_style( 'kubio-controls' ); |
| 359 | wp_enqueue_style( 'kubio-utils' ); |
| 360 | wp_enqueue_style( 'kubio-scripts' ); |
| 361 | wp_enqueue_style( 'kubio-wp-global-styles' ); |
| 362 | } |
| 363 | |
| 364 | function kubio_extend_block_editor_styles_html() { |
| 365 | $script = kubio_get_editor_scripts(); |
| 366 | $style = kubio_get_editor_style(); |
| 367 | $script = "<template id='kubio-scripts-template'>{$script}</template>"; |
| 368 | |
| 369 | printf( |
| 370 | '<script>window.__kubioEditorStyles = %s</script>', |
| 371 | wp_json_encode( array( 'html' => $style . $script ) ) |
| 372 | ); |
| 373 | } |
| 374 | |
| 375 | function kubio_edit_site_get_settings() { |
| 376 | $max_upload_size = wp_max_upload_size(); |
| 377 | if ( ! $max_upload_size ) { |
| 378 | $max_upload_size = 0; |
| 379 | } |
| 380 | |
| 381 | // This filter is documented in wp-admin/includes/media.php. |
| 382 | // This filter is documented in wp-admin/includes/media.php. |
| 383 | $image_size_names = apply_filters( |
| 384 | 'image_size_names_choose', |
| 385 | array( |
| 386 | 'thumbnail' => __( 'Thumbnail', 'kubio' ), |
| 387 | 'medium' => __( 'Medium', 'kubio' ), |
| 388 | 'large' => __( 'Large', 'kubio' ), |
| 389 | 'full' => __( 'Original Size', 'kubio' ), |
| 390 | ) |
| 391 | ); |
| 392 | $available_image_sizes = array(); |
| 393 | foreach ( $image_size_names as $image_size_slug => $image_size_name ) { |
| 394 | $available_image_sizes[] = array( |
| 395 | 'slug' => $image_size_slug, |
| 396 | 'name' => $image_size_name, |
| 397 | ); |
| 398 | } |
| 399 | |
| 400 | global $current_screen; |
| 401 | if ( function_exists( '_register_core_block_patterns_and_categories' ) ) { |
| 402 | _register_core_block_patterns_and_categories(); |
| 403 | } |
| 404 | |
| 405 | if ( function_exists( '_load_remote_block_patterns' ) ) { |
| 406 | _load_remote_block_patterns(); |
| 407 | } |
| 408 | |
| 409 | if ( function_exists( '_load_remote_featured_patterns' ) ) { |
| 410 | _load_remote_featured_patterns( $current_screen ); |
| 411 | } |
| 412 | |
| 413 | global $wp_version; |
| 414 | $block_editor_context = new WP_Block_Editor_Context( array( 'name' => 'core/edit-site' ) ); |
| 415 | $settings = array_merge( |
| 416 | get_default_block_editor_settings(), |
| 417 | array( |
| 418 | 'alignWide' => true, //get_theme_support( 'align-wide' ), |
| 419 | 'siteUrl' => site_url( '/' ), |
| 420 | 'postsPerPage' => get_option( 'posts_per_page' ), |
| 421 | 'defaultTemplateTypes' => kubio_get_indexed_default_template_types(), |
| 422 | '__experimentalBlockPatterns' => array(), |
| 423 | '__experimentalBlockPatternCategories' => array_merge( |
| 424 | WP_Block_Pattern_Categories_Registry::get_instance()->get_all_registered(), |
| 425 | kubio_get_patterns_categories() |
| 426 | ), |
| 427 | 'imageSizes' => $available_image_sizes, |
| 428 | 'isRTL' => is_rtl(), |
| 429 | 'maxUploadFileSize' => $max_upload_size, |
| 430 | |
| 431 | 'wpVersion' => preg_replace( '/([0-9]+).([0-9]+).*/', '$1.$2', $wp_version ), |
| 432 | ) |
| 433 | ); |
| 434 | |
| 435 | if ( function_exists( 'get_block_editor_settings' ) && class_exists( '\WP_Block_Editor_Context' ) ) { |
| 436 | $settings = get_block_editor_settings( $settings, $block_editor_context ); |
| 437 | } |
| 438 | |
| 439 | if ( function_exists( 'gutenberg_experimental_global_styles_settings' ) ) { |
| 440 | $settings = gutenberg_experimental_global_styles_settings( $settings ); |
| 441 | } |
| 442 | |
| 443 | $settings['state'] = array( |
| 444 | 'entity' => kubio_edit_site_get_edited_entity(), |
| 445 | ); |
| 446 | |
| 447 | $settings['kubioPrimaryMenuLocation'] = apply_filters( |
| 448 | 'kubio/primary_menu_location', |
| 449 | 'header-menu' |
| 450 | ); |
| 451 | |
| 452 | $settings['isKubioTheme'] = kubio_theme_has_kubio_block_support(); |
| 453 | |
| 454 | $settings['supportsTemplateMode'] = kubio_theme_has_block_templates_support(); |
| 455 | |
| 456 | global $post; |
| 457 | $settings = apply_filters( 'block_editor_settings_all', $settings, $post ); |
| 458 | $settings = apply_filters( 'kubio/block_editor_settings', $settings, $post ); |
| 459 | $settings['isWooCommerce'] = function_exists( 'WC' ); |
| 460 | |
| 461 | $settings['defaultTemplatePartAreas'] = get_allowed_block_template_part_areas(); |
| 462 | $settings['classicThemeTemplates'] = kubio_get_classic_theme_templates(); |
| 463 | $settings['kubioThemeAssetsUrlBase'] = untrailingslashit( apply_filters( 'kubio/importer/kubio-url-placeholder-replacement', '' ) ); |
| 464 | |
| 465 | // adding it here since `select('core').getCurrentTheme().theme_uri` returns an empty string. |
| 466 | $settings['themeUri'] = get_template_directory_uri(); |
| 467 | $settings['classicHasFrontPageTemplate'] = kubio_third_party_theme_has_front_page_template(); |
| 468 | $settings['classicThemePrimaryTemplates'] = array_keys( kubio_get_classic_theme_primary_templates() ); |
| 469 | |
| 470 | $scrollable_image_path = wp_get_theme()->get_file_path( 'resources/images/front-page-preview.jpg' ); |
| 471 | // Handle the Front page suggestion preview image |
| 472 | if ( file_exists( $scrollable_image_path ) ) { |
| 473 | $settings['kubioHasFpsScrollPreview'] = true; // 'resources/images/front-page-preview.jpg'; |
| 474 | } else { |
| 475 | $settings['kubioHasFpsScrollPreview'] = false; //'screenshot.jpg'; |
| 476 | } |
| 477 | |
| 478 | $settings['supportsLayout'] = isset( $settings['supportsLayout'] ) ? $settings['supportsLayout'] : false; |
| 479 | $layout = array_merge( array( 'contentSize' => '1172px' ), LodashBasic::get( $settings, '__experimentalFeatures.layout', array() ) ); |
| 480 | |
| 481 | LodashBasic::set( $settings, '__experimentalFeatures.layout', $layout ); |
| 482 | LodashBasic::set( $settings, '__experimentalFeatures.blocks.core/post-content.layout', $layout ); |
| 483 | |
| 484 | // use array_values to ensure that json_encode sees it as array not object |
| 485 | $styles = array_values( |
| 486 | array_merge( |
| 487 | function_exists( 'get_block_editor_theme_styles' ) ? get_block_editor_theme_styles() : array(), |
| 488 | array( |
| 489 | array( |
| 490 | 'css' => kubio_get_editor_style( true ), |
| 491 | '__unstableType' => 'theme', |
| 492 | ), |
| 493 | ) |
| 494 | ) |
| 495 | ); |
| 496 | |
| 497 | if ( function_exists( 'wp_get_global_stylesheet' ) ) { |
| 498 | $block_classes = array( |
| 499 | 'css' => 'base-layout-styles', |
| 500 | '__unstableType' => 'base-layout', |
| 501 | 'isGlobalStyles' => true, |
| 502 | ); |
| 503 | $actual_css = wp_get_global_stylesheet( array( $block_classes['css'] ) ); |
| 504 | |
| 505 | if ( $actual_css ) { |
| 506 | $block_classes['css'] = $actual_css; |
| 507 | $styles[] = $block_classes; |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | $settings['styles'] = array_merge( $settings['styles'], $styles ); |
| 512 | |
| 513 | return $settings; |
| 514 | } |
| 515 | |
| 516 | function kubio_block_editor_general_settings( $settings ) { |
| 517 | $settings['__unstableEnableFullSiteEditingBlocks'] = true; |
| 518 | $settings['enableFSEBlocks'] = true; |
| 519 | $settings['kubioGlobalSettings'] = (object) Flags::getSettings(); |
| 520 | |
| 521 | // settings for outside Kubio editor |
| 522 | // __unstableResolvedAssets was added in WP 6.0 |
| 523 | if ( isset( $settings['__unstableResolvedAssets'] ) && ! kubio_is_kubio_editor_page() ) { |
| 524 | $current_style = Arr::get( $settings, '__unstableResolvedAssets.styles', '' ); |
| 525 | $current_scripts = Arr::get( $settings, '__unstableResolvedAssets.scripts', '' ); |
| 526 | |
| 527 | preg_match_all( "#id='(.*?)'#m", $current_style, $matches ); |
| 528 | $style_ids = Arr::get( $matches, 1, array() ); |
| 529 | |
| 530 | preg_match_all( "#id='(.*?)'#m", $current_scripts, $matches ); |
| 531 | $script_ids = Arr::get( $matches, 1, array() ); |
| 532 | |
| 533 | $style = array( |
| 534 | sprintf( '<style>%s</style>', kubio_render_global_colors() ), |
| 535 | kubio_get_editor_style( false, $style_ids ), |
| 536 | ); |
| 537 | |
| 538 | $script = kubio_get_editor_scripts( $script_ids ); |
| 539 | |
| 540 | Arr::set( |
| 541 | $settings, |
| 542 | '__unstableResolvedAssets.styles', |
| 543 | $current_style . "\n\n" . implode( "\n", $style ) |
| 544 | ); |
| 545 | |
| 546 | Arr::set( |
| 547 | $settings, |
| 548 | '__unstableResolvedAssets.scripts', |
| 549 | $current_scripts . "\n\n$script" |
| 550 | ); |
| 551 | |
| 552 | // kubio_enqueue_frontend_scripts(); |
| 553 | |
| 554 | } |
| 555 | |
| 556 | $settings['kubioLoaded'] = true; |
| 557 | |
| 558 | $settings['kubioThemesBaseURL'] = get_theme_root_uri(); |
| 559 | return $settings; |
| 560 | } |
| 561 | |
| 562 | add_filter( 'block_editor_settings_all', 'kubio_block_editor_general_settings', 50 ); |
| 563 | |
| 564 | |
| 565 | function kubio_load_gutenberg_assets() { |
| 566 | |
| 567 | kubio_fix_template_and_parts_default_editor(); |
| 568 | |
| 569 | AssetsDependencyInjector::injectKubioScriptDependencies( 'fancybox' ); |
| 570 | AssetsDependencyInjector::injectKubioFrontendStyleDependencies( 'fancybox' ); |
| 571 | AssetsDependencyInjector::injectKubioScriptDependencies( 'jquery-masonry', false ); |
| 572 | |
| 573 | do_action( 'kubio/editor/load_gutenberg_assets' ); |
| 574 | wp_enqueue_style( 'kubio-pro' ); |
| 575 | wp_enqueue_script( 'kubio-block-patterns' ); |
| 576 | wp_enqueue_script( 'kubio-third-party-blocks' ); |
| 577 | wp_localize_script( |
| 578 | 'kubio-block-patterns', |
| 579 | 'kubioBlockPatterns', |
| 580 | array( |
| 581 | 'inGutenbergEditor' => ! kubio_is_kubio_editor_page(), |
| 582 | ) |
| 583 | ); |
| 584 | |
| 585 | wp_add_inline_style( 'kubio-block-library', kubio_get_shapes_css() ); |
| 586 | } |
| 587 | |
| 588 | //https://mantis.iconvert.pro/view.php?id=54771 |
| 589 | function kubio_fix_template_and_parts_default_editor() { |
| 590 | |
| 591 | global $post; |
| 592 | |
| 593 | if ( ! $post ) { |
| 594 | return; |
| 595 | } |
| 596 | if ( ! in_array( $post->post_type, array( 'wp_template', 'wp_template_part' ) ) ) { |
| 597 | return; |
| 598 | } |
| 599 | $preload_paths = array( |
| 600 | |
| 601 | '/wp/v2/themes?context=edit&status=active', |
| 602 | |
| 603 | ); |
| 604 | |
| 605 | $preload_data = array_reduce( |
| 606 | $preload_paths, |
| 607 | 'rest_preload_api_request', |
| 608 | array() |
| 609 | ); |
| 610 | $theme_data = Arr::get( $preload_data, $preload_paths[0] . '.body.0' ); |
| 611 | if ( ! $theme_data ) { |
| 612 | return; |
| 613 | } |
| 614 | //Very dirty method of fixing the bug(https://mantis.iconvert.pro/view.php?id=54771). But could not find any other method. The normal preload did not seem to fix the issue. |
| 615 | wp_add_inline_script( |
| 616 | 'wp-core-data', |
| 617 | sprintf( 'wp.data.select("core").getCurrentTheme = () => { return %s }', wp_json_encode( $theme_data ) ), |
| 618 | 'after' |
| 619 | ); |
| 620 | } |
| 621 | |
| 622 | function kubio_get_post_types() { |
| 623 | $extra_types = get_post_types( |
| 624 | array( |
| 625 | '_builtin' => false, |
| 626 | 'public' => true, |
| 627 | ) |
| 628 | ); |
| 629 | |
| 630 | $types = array(); |
| 631 | global $wp_post_types; |
| 632 | |
| 633 | foreach ( $extra_types as $type ) { |
| 634 | $types[] = array( |
| 635 | 'entity' => $type, |
| 636 | 'kind' => 'postType', |
| 637 | 'title' => $wp_post_types[ $type ]->label, |
| 638 | ); |
| 639 | } |
| 640 | |
| 641 | return $types; |
| 642 | } |
| 643 | |
| 644 | add_action( 'enqueue_block_editor_assets', 'kubio_load_gutenberg_assets' ); |
| 645 | |
| 646 | |
| 647 | function kubio_add_endpoint_details( |
| 648 | $url, |
| 649 | $results_per_page = null |
| 650 | ) { |
| 651 | $vars = array( 'context' => 'edit' ); |
| 652 | if ( $results_per_page !== null ) { |
| 653 | $vars['per_page'] = $results_per_page; |
| 654 | } |
| 655 | return $url . '?' . build_query( $vars ); |
| 656 | } |
| 657 | |
| 658 | function kubio_edit_site_init( $hook ) { |
| 659 | if ( ! kubio_is_kubio_editor_page() ) { |
| 660 | return; |
| 661 | } |
| 662 | add_filter( 'use_block_editor_for_post_type', '__return_true' ); |
| 663 | |
| 664 | global $current_screen, $post; |
| 665 | $current_screen->is_block_editor( true ); |
| 666 | $active_theme = wp_get_theme()->get_stylesheet(); |
| 667 | |
| 668 | // Inline the Editor Settings |
| 669 | $settings = kubio_edit_site_get_settings(); |
| 670 | // Preload block editor paths. |
| 671 | // most of these are copied from edit-forms-blocks.php. |
| 672 | $preload_paths = array( |
| 673 | array( '/wp/v2/media', 'OPTIONS' ), |
| 674 | '/wp/v2/types?context=view', |
| 675 | '/wp/v2/types/wp_template?context=edit', |
| 676 | '/wp/v2/types/wp_template-part?context=edit', |
| 677 | '/wp/v2/templates?context=edit&per_page=-1', |
| 678 | '/wp/v2/template-parts?context=edit&per_page=-1', |
| 679 | '/wp/v2/themes?context=edit&status=active', |
| 680 | '/wp/v2/users/me', |
| 681 | |
| 682 | // contact and subscribe |
| 683 | '/kubio/v1/contact-form/forms_by_type', |
| 684 | '/kubio/v1/subscribe-form/forms_by_type', |
| 685 | |
| 686 | // menu locations and menus |
| 687 | '/__experimental/menu-locations?context=edit', |
| 688 | '/wp/v2/menu-locations?context=edit', |
| 689 | '/wp/v2/users/me?context=edit', |
| 690 | |
| 691 | // taxonimies |
| 692 | '/wp/v2/taxonomies?context=view', |
| 693 | |
| 694 | // global data |
| 695 | '/wp/v2/kubio/global-data/' . kubio_global_data_post_id(), |
| 696 | array( '/wp/v2/kubio/global-data/' . kubio_global_data_post_id(), 'OPTIONS' ), |
| 697 | '/wp/v2/kubio/global-data/' . kubio_global_data_post_id() . '?context=edit', |
| 698 | |
| 699 | // general |
| 700 | '/wp/v2/settings', |
| 701 | array( '/wp/v2/settings', 'OPTIONS' ), |
| 702 | |
| 703 | kubio_add_endpoint_details( "/wp/v2/template-parts/{$active_theme}//footer" ), |
| 704 | kubio_add_endpoint_details( "/wp/v2/template-parts/{$active_theme}//front-header" ), |
| 705 | kubio_add_endpoint_details( "/wp/v2/templates/{$active_theme}//front-page" ), |
| 706 | kubio_add_endpoint_details( "/wp/v2/templates/{$active_theme}//full-width" ), |
| 707 | kubio_add_endpoint_details( "/wp/v2/templates/{$active_theme}//front-page" ), |
| 708 | |
| 709 | ); |
| 710 | |
| 711 | // try to preload current page |
| 712 | |
| 713 | $wp_global_style = 0; |
| 714 | if ( class_exists( '\WP_Theme_JSON_Resolver' ) && method_exists( WP_Theme_JSON_Resolver::class, 'get_user_global_styles_post_id' ) ) { |
| 715 | $wp_global_style = WP_Theme_JSON_Resolver::get_user_global_styles_post_id(); |
| 716 | } |
| 717 | if ( $wp_global_style ) { |
| 718 | $preload_paths = array_merge( |
| 719 | $preload_paths, |
| 720 | array( |
| 721 | '/wp/v2/global-styles/' . $wp_global_style . '?context=edit', |
| 722 | '/wp/v2/global-styles/' . $wp_global_style, |
| 723 | '/wp/v2/global-styles/themes/' . $active_theme, |
| 724 | ) |
| 725 | ); |
| 726 | } |
| 727 | |
| 728 | // menus |
| 729 | $menus = wp_get_nav_menus(); |
| 730 | foreach ( $menus as $menu_term ) { |
| 731 | $preload_paths[] = "/kubio/v1/menu/{$menu_term->term_id}"; |
| 732 | $preload_paths[] = "/kubio/v1/menu/{$menu_term->term_id}?context=edit"; |
| 733 | } |
| 734 | |
| 735 | $preload_data = array_reduce( |
| 736 | $preload_paths, |
| 737 | 'rest_preload_api_request', |
| 738 | array() |
| 739 | ); |
| 740 | |
| 741 | // prepare editor context |
| 742 | $editor_context_props = array( 'name' => 'core/edit-site' ); |
| 743 | |
| 744 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 745 | $query_post_id = Arr::get( $_REQUEST, 'postId', null ); |
| 746 | |
| 747 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 748 | $query_post_type = Arr::get( $_REQUEST, 'postType', null ); |
| 749 | $page_on_front = get_option( 'page_on_front', null ); |
| 750 | $post_to_load = null; |
| 751 | |
| 752 | if ( ! $query_post_id && ! $query_post_type ) { |
| 753 | if ( $page_on_front ) { |
| 754 | $post_to_load = get_post( $page_on_front ); |
| 755 | if ( is_wp_error( $post_to_load ) ) { |
| 756 | $post_to_load = null; |
| 757 | } |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | if ( $query_post_type && $query_post_id ) { |
| 762 | if ( is_numeric( $query_post_id ) ) { |
| 763 | $post_to_load = get_post( $query_post_id ); |
| 764 | if ( is_wp_error( $post_to_load ) ) { |
| 765 | $post_to_load = null; |
| 766 | } |
| 767 | } else { |
| 768 | $post_to_load = kubio_get_block_template( $query_post_id, $query_post_type ); |
| 769 | |
| 770 | if ( ! $post_to_load || is_wp_error( $post_to_load ) ) { |
| 771 | $post_to_load = null; |
| 772 | } |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | if ( $post_to_load ) { |
| 777 | $post_to_load_type = property_exists( $post_to_load, 'type' ) ? $post_to_load->type : $post_to_load->post_type; |
| 778 | $post_to_load_id = property_exists( $post_to_load, 'wp_id' ) ? $post_to_load->wp_id : $post_to_load->ID; |
| 779 | |
| 780 | $editor_context_props['post'] = get_post( $post_to_load_id ); |
| 781 | $rest_path = rest_get_route_for_post( $post_to_load_id ); |
| 782 | $preload_paths[] = $rest_path; |
| 783 | $preload_paths[] = add_query_arg( 'context', 'edit', $rest_path ); |
| 784 | $preload_paths[] = sprintf( '/wp/v2/types/%s?context=edit', $post_to_load_type ); |
| 785 | } |
| 786 | |
| 787 | $block_editor_context = new \WP_Block_Editor_Context( $editor_context_props ); |
| 788 | |
| 789 | if ( function_exists( 'block_editor_rest_api_preload' ) ) { |
| 790 | block_editor_rest_api_preload( $preload_paths, $block_editor_context ); |
| 791 | } else { |
| 792 | wp_add_inline_script( |
| 793 | 'wp-api-fetch', |
| 794 | sprintf( |
| 795 | 'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );', |
| 796 | wp_json_encode( $preload_data ) |
| 797 | ), |
| 798 | 'after' |
| 799 | ); |
| 800 | } |
| 801 | |
| 802 | $server_side_settings = get_block_editor_server_block_settings(); |
| 803 | foreach ( array_keys( $server_side_settings ) as $server_side_block_name ) { |
| 804 | if ( strpos( $server_side_block_name, 'kubio/' ) === 0 ) { |
| 805 | unset( $server_side_settings[ $server_side_block_name ] ); |
| 806 | } |
| 807 | } |
| 808 | wp_add_inline_script( |
| 809 | 'wp-blocks', |
| 810 | sprintf( |
| 811 | 'wp.blocks.unstable__bootstrapServerSideBlockDefinitions( %s );', |
| 812 | wp_json_encode( $server_side_settings ) |
| 813 | ), |
| 814 | 'after' |
| 815 | ); |
| 816 | |
| 817 | wp_add_inline_script( |
| 818 | 'wp-blocks', |
| 819 | sprintf( |
| 820 | 'wp.blocks.setCategories( %s );', |
| 821 | wp_json_encode( get_block_categories( $post ) ) |
| 822 | ), |
| 823 | 'after' |
| 824 | ); |
| 825 | |
| 826 | kubio_enqueue_editor_page_assets(); |
| 827 | $settings['postTypes'] = kubio_get_post_types(); |
| 828 | |
| 829 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 830 | $ai_hash = sanitize_text_field( Flags::get( 'start_with_ai_hash', null ) ); |
| 831 | |
| 832 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 833 | $ai_hash_param = sanitize_text_field( Arr::get( $_REQUEST, 'ai', null ) ); |
| 834 | |
| 835 | $settings['startWithAIFrontPage'] = ( $ai_hash && $ai_hash_param && $ai_hash_param === $ai_hash ); |
| 836 | |
| 837 | $black_wizard_onboarding_hash = Flags::get( 'black_wizard_onboarding_hash', null ); |
| 838 | |
| 839 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 840 | $black_wizard_onboarding_param = sanitize_text_field( Arr::get( $_REQUEST, 'black-wizard-onboarding', null ) ); |
| 841 | $settings['startWithBlackWizardOnboarding'] = ( kubio_is_black_wizard_onboarding_enabled() && |
| 842 | $black_wizard_onboarding_hash && |
| 843 | $black_wizard_onboarding_param && |
| 844 | $black_wizard_onboarding_hash === $black_wizard_onboarding_param ); |
| 845 | |
| 846 | if ( $settings['startWithBlackWizardOnboarding'] ) { |
| 847 | add_filter( |
| 848 | 'admin_body_class', |
| 849 | function ( $classes ) { |
| 850 | $classes .= ' kubio-will-have-black-wizard-onboarding'; |
| 851 | return $classes; |
| 852 | } |
| 853 | ); |
| 854 | } |
| 855 | |
| 856 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 857 | if ( isset( $_REQUEST['generate-ai-frontpage'] ) && ! Flags::get( 'generated_from_getting_started', false ) ) { |
| 858 | $settings['startGeneratingAIFrontPage'] = true; |
| 859 | Flags::set( 'generated_from_getting_started', true ); |
| 860 | } |
| 861 | |
| 862 | wp_add_inline_script( |
| 863 | 'kubio-editor', |
| 864 | sprintf( |
| 865 | "window.kubioEditSiteSettings = %s;\n" . |
| 866 | 'wp.domReady( function() { kubio.editor.initialize( "kubio-editor", window.kubioEditSiteSettings ); } );', |
| 867 | wp_json_encode( $settings ) |
| 868 | ), |
| 869 | 'after' |
| 870 | ); |
| 871 | |
| 872 | wp_add_inline_style( 'kubio-editor', kubio_get_shapes_css() ); |
| 873 | wp_add_inline_style( 'kubio-editor', kubio_render_global_colors() ); |
| 874 | |
| 875 | add_action( 'admin_footer', 'kubio_extend_block_editor_styles_html' ); |
| 876 | do_action( 'enqueue_block_editor_assets' ); |
| 877 | do_action( 'kubio/editor/enqueue_assets' ); |
| 878 | |
| 879 | $editor_styles = function_exists( 'get_editor_stylesheets' ) ? get_editor_stylesheets() : null; |
| 880 | if ( |
| 881 | current_theme_supports( 'wp-block-styles' ) || |
| 882 | ( ! is_array( $editor_styles ) || count( $editor_styles ) === 0 ) |
| 883 | ) { |
| 884 | wp_enqueue_style( 'wp-block-library-theme' ); |
| 885 | } |
| 886 | } |
| 887 | |
| 888 | add_action( 'admin_enqueue_scripts', 'kubio_edit_site_init' ); |
| 889 | |
| 890 | // Add tinymce scripts within the Kubio Editor and WordPress Editor since we need them in blocks like Subscribe form. |
| 891 | // we use the `enqueue_block_editor_assets` action since it is called in both editors. |
| 892 | add_action( |
| 893 | 'enqueue_block_editor_assets', |
| 894 | function () { |
| 895 | wp_enqueue_script( 'wp-tinymce' ); |
| 896 | wp_enqueue_editor(); |
| 897 | wp_enqueue_code_editor( array() ); |
| 898 | wp_tinymce_inline_scripts(); |
| 899 | } |
| 900 | ); |
| 901 | |
| 902 | /** |
| 903 | * Renders the Menu Page |
| 904 | * |
| 905 | * @return void |
| 906 | */ |
| 907 | function kubio_edit_site_render_block_editor() { |
| 908 | ?> |
| 909 | <style> |
| 910 | div#wpcontent, |
| 911 | #wpbody-content { |
| 912 | position: fixed; |
| 913 | z-index: 1000000; |
| 914 | width: 100vw; |
| 915 | height: 100vh; |
| 916 | background: #fff; |
| 917 | top: 0%; |
| 918 | left: 0%; |
| 919 | right: 0; |
| 920 | border: 0; |
| 921 | display: block; |
| 922 | box-sizing: border-box; |
| 923 | } |
| 924 | |
| 925 | #wp-link-backdrop { |
| 926 | z-index: 1000050; |
| 927 | } |
| 928 | |
| 929 | #wp-link-wrap { |
| 930 | z-index: 1000100; |
| 931 | } |
| 932 | |
| 933 | #kubio-editor { |
| 934 | position: absolute; |
| 935 | top: 0; |
| 936 | bottom: 0; |
| 937 | left: 0; |
| 938 | right: 0; |
| 939 | z-index: 1000; |
| 940 | width: 100%; |
| 941 | height: 100%; |
| 942 | display: block !important; |
| 943 | } |
| 944 | |
| 945 | .kubio-loading-editor { |
| 946 | position: absolute; |
| 947 | top: 50%; |
| 948 | left: 50%; |
| 949 | display: block; |
| 950 | font-size: 1.5em; |
| 951 | transform: translate(-50%, -50%); |
| 952 | } |
| 953 | |
| 954 | .kubio-loading-editor iframe { |
| 955 | margin: 0px auto 0px auto; |
| 956 | } |
| 957 | </style> |
| 958 | <div id="kubio-editor" class="kubio"> |
| 959 | <div class="kubio-loading-editor"> |
| 960 | <?php |
| 961 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 962 | echo kubio_get_iframe_loader( array( 'size' => '120px' ) ); |
| 963 | ?> |
| 964 | </div> |
| 965 | </div> |
| 966 | <?php |
| 967 | } |
| 968 | |
| 969 | /** |
| 970 | * Registers the new WP Admin Menu |
| 971 | * |
| 972 | * @return void |
| 973 | */ |
| 974 | function kubio_edit_site_add_menu_page() { |
| 975 | |
| 976 | if ( ! current_user_can( 'edit_theme_options' ) ) { |
| 977 | return; |
| 978 | } |
| 979 | |
| 980 | add_menu_page( |
| 981 | __( 'Kubio', 'kubio' ), |
| 982 | sprintf( |
| 983 | '<span class="kubio-menu-item"><span class="kubio-menu-item--icon">%s</span><span>%s</span></span>', |
| 984 | wp_kses_post( KUBIO_LOGO_SVG ), |
| 985 | __( 'Kubio', 'kubio' ) |
| 986 | ), |
| 987 | 'edit_posts', |
| 988 | 'kubio', |
| 989 | 'kubio_edit_site_render_block_editor', |
| 990 | 'data:image/svg+xml;base64,' . base64_encode( str_replace( '<svg', '<svg fill="#5246F1" ', KUBIO_LOGO_SVG ) ), |
| 991 | 20 |
| 992 | ); |
| 993 | |
| 994 | add_submenu_page( |
| 995 | 'themes.php', |
| 996 | __( 'Kubio', 'kubio' ), |
| 997 | __( 'Edit with Kubio', 'kubio' ), |
| 998 | 'edit_posts', |
| 999 | 'kubio_appearance_edit', |
| 1000 | 'kubio_edit_site_render_block_editor', |
| 1001 | 1 |
| 1002 | ); |
| 1003 | |
| 1004 | global $submenu; |
| 1005 | |
| 1006 | if ( isset( $submenu['themes.php'] ) ) { |
| 1007 | foreach ( $submenu['themes.php'] as $index => $submenu_item ) { |
| 1008 | if ( $submenu_item[2] === 'kubio_appearance_edit' ) { |
| 1009 | $submenu['themes.php'][ $index ][2] = Utils::kubioGetEditorURL(); |
| 1010 | } |
| 1011 | } |
| 1012 | } |
| 1013 | } |
| 1014 | |
| 1015 | function kubio_admin_page_submenu_hook() { |
| 1016 | global $submenu; |
| 1017 | |
| 1018 | $kubio_submenu = Arr::get( $submenu, 'kubio', null ); |
| 1019 | |
| 1020 | if ( is_array( $kubio_submenu ) ) { |
| 1021 | foreach ( $kubio_submenu as $index => $submenu_data ) { |
| 1022 | $menu_slug = Arr::get( $submenu_data, '2', null ); |
| 1023 | if ( $menu_slug === 'kubio' ) { |
| 1024 | $submenu['kubio'][ $index ][0] = __( 'Open Kubio Editor', 'kubio' ); |
| 1025 | } |
| 1026 | } |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | add_action( 'admin_menu', 'kubio_admin_page_submenu_hook', 200 ); |
| 1031 | |
| 1032 | add_action( 'admin_menu', 'kubio_edit_site_add_menu_page' ); |
| 1033 | |
| 1034 | function kubio_admin_menu_style() { |
| 1035 | ?> |
| 1036 | <style> |
| 1037 | #adminmenu .toplevel_page_kubio .wp-menu-image { |
| 1038 | display: none; |
| 1039 | } |
| 1040 | |
| 1041 | .wp-admin.folded #adminmenu .toplevel_page_kubio .wp-menu-image { |
| 1042 | display: block; |
| 1043 | background-size: 16px auto; |
| 1044 | } |
| 1045 | |
| 1046 | #adminmenu .kubio-menu-item { |
| 1047 | display: flex; |
| 1048 | align-items: center; |
| 1049 | } |
| 1050 | |
| 1051 | #adminmenu .kubio-menu-item>span { |
| 1052 | display: block; |
| 1053 | line-height: 1; |
| 1054 | } |
| 1055 | |
| 1056 | #adminmenu .kubio-menu-item--icon { |
| 1057 | color: rgba(240, 246, 252, .6); |
| 1058 | } |
| 1059 | body:not(.admin-color-fresh) #adminmenu .kubio-menu-item--icon { |
| 1060 | color: hsl(0, 7%, 95%); |
| 1061 | } |
| 1062 | |
| 1063 | #adminmenu .wp-menu-name:hover .kubio-menu-item--icon { |
| 1064 | color: inherit; |
| 1065 | } |
| 1066 | |
| 1067 | #adminmenu .kubio-menu-item--icon svg { |
| 1068 | fill: currentColor; |
| 1069 | height: 15px; |
| 1070 | margin-right: 11px; |
| 1071 | } |
| 1072 | |
| 1073 | #adminmenu .wp-menu-open .kubio-menu-item--icon svg { |
| 1074 | fill: #fff; |
| 1075 | } |
| 1076 | |
| 1077 | #adminmenu .toplevel_page_kubio div.wp-menu-name { |
| 1078 | padding: 8px 8px 8px 10px; |
| 1079 | } |
| 1080 | |
| 1081 | #adminmenu .wp-submenu .kubio-menu-item--icon { |
| 1082 | display: none; |
| 1083 | } |
| 1084 | </style> |
| 1085 | <?php |
| 1086 | } |
| 1087 | |
| 1088 | add_action( 'admin_head', 'kubio_admin_menu_style' ); |
| 1089 | |
| 1090 | /** |
| 1091 | * Register a core site setting for front page information. |
| 1092 | */ |
| 1093 | function kubio_register_site_editor_homepage_settings() { |
| 1094 | global $wp_registered_settings; |
| 1095 | |
| 1096 | if ( ! isset( $wp_registered_settings['show_on_front'] ) ) { |
| 1097 | |
| 1098 | // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic |
| 1099 | register_setting( |
| 1100 | 'reading', |
| 1101 | 'show_on_front', |
| 1102 | array( |
| 1103 | 'show_in_rest' => true, |
| 1104 | 'type' => 'string', |
| 1105 | 'description' => __( 'What to show on the front page', 'kubio' ), |
| 1106 | ) |
| 1107 | ); |
| 1108 | } |
| 1109 | |
| 1110 | if ( ! isset( $wp_registered_settings['page_on_front'] ) ) { |
| 1111 | // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic |
| 1112 | register_setting( |
| 1113 | 'reading', |
| 1114 | 'page_on_front', |
| 1115 | array( |
| 1116 | 'show_in_rest' => true, |
| 1117 | 'type' => 'number', |
| 1118 | 'description' => __( |
| 1119 | 'The ID of the page that should be displayed on the front page', |
| 1120 | 'kubio' |
| 1121 | ), |
| 1122 | ) |
| 1123 | ); |
| 1124 | } |
| 1125 | if ( ! isset( $wp_registered_settings['page_for_posts'] ) ) { |
| 1126 | // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic |
| 1127 | register_setting( |
| 1128 | 'reading', |
| 1129 | 'page_for_posts', |
| 1130 | array( |
| 1131 | 'show_in_rest' => true, |
| 1132 | 'type' => 'number', |
| 1133 | 'description' => __( |
| 1134 | 'The ID of the page that displays the posts', |
| 1135 | 'kubio' |
| 1136 | ), |
| 1137 | ) |
| 1138 | ); |
| 1139 | } |
| 1140 | |
| 1141 | if ( ! isset( $wp_registered_settings['site_icon'] ) ) { |
| 1142 | // phpcs:ignore PluginCheck.CodeAnalysis.SettingSanitization.register_settingDynamic |
| 1143 | register_setting( |
| 1144 | 'general', |
| 1145 | 'site_icon', |
| 1146 | array( |
| 1147 | 'show_in_rest' => array( |
| 1148 | 'name' => 'site_icon', |
| 1149 | ), |
| 1150 | 'type' => 'integer', |
| 1151 | 'description' => __( 'Site icon.', 'kubio' ), |
| 1152 | ) |
| 1153 | ); |
| 1154 | } |
| 1155 | |
| 1156 | if ( class_exists( 'Jetpack_Notifications' ) ) { |
| 1157 | remove_action( 'wp_head', array( Jetpack_Notifications::init(), 'styles_and_scripts' ), 120 ); |
| 1158 | remove_action( 'admin_head', array( Jetpack_Notifications::init(), 'styles_and_scripts' ) ); |
| 1159 | } |
| 1160 | } |
| 1161 | |
| 1162 | add_action( 'init', 'kubio_register_site_editor_homepage_settings', 100 ); |
| 1163 | |
| 1164 | function kubio_editor_add_default_template_types( $template_types ) { |
| 1165 | if ( kubio_is_kubio_editor_page() && is_user_logged_in() ) { |
| 1166 | $kubio_templates = array( |
| 1167 | 'full-width' => array( |
| 1168 | 'title' => _x( 'Full Width', 'Template name', 'kubio' ), |
| 1169 | 'description' => __( |
| 1170 | 'Recommended Kubio template to display individual pages. This template works best with the Kubio provided blocks and predesigned sections.', |
| 1171 | 'kubio' |
| 1172 | ), |
| 1173 | ), |
| 1174 | 'kubio-full-width' => array( |
| 1175 | 'title' => _x( 'Kubio Full Width', 'Template name', 'kubio' ), |
| 1176 | 'description' => __( |
| 1177 | 'Recommended Kubio template to display individual pages. This template works best with the Kubio provided blocks and predesigned sections.', |
| 1178 | 'kubio' |
| 1179 | ), |
| 1180 | ), |
| 1181 | ); |
| 1182 | |
| 1183 | foreach ( $kubio_templates as $slug => $settings ) { |
| 1184 | $template_types[ $slug ] = $settings; |
| 1185 | } |
| 1186 | } |
| 1187 | |
| 1188 | return $template_types; |
| 1189 | } |
| 1190 | |
| 1191 | add_filter( 'default_template_types', 'kubio_editor_add_default_template_types' ); |
| 1192 | |
| 1193 | add_action( |
| 1194 | 'init', |
| 1195 | function () { |
| 1196 | if ( kubio_is_kubio_editor_page() && is_user_logged_in() ) { |
| 1197 | $has_valid_req = Utils::validateRequirements(); |
| 1198 | |
| 1199 | if ( is_wp_error( $has_valid_req ) ) { |
| 1200 | ob_start(); |
| 1201 | ?> |
| 1202 | <style> |
| 1203 | body#error-page { |
| 1204 | margin: 0; |
| 1205 | position: absolute; |
| 1206 | top: 50%; |
| 1207 | left: 50%; |
| 1208 | transform: translate(-50%, -50%); |
| 1209 | border-left: 4px solid #fb3e41; |
| 1210 | padding-top: 0; |
| 1211 | padding-bottom: 0; |
| 1212 | } |
| 1213 | |
| 1214 | body#error-page .kubio-mrn { |
| 1215 | flex-direction: column; |
| 1216 | align-items: flex-start; |
| 1217 | } |
| 1218 | |
| 1219 | body#error-page .kubio-mrn-icon-holder { |
| 1220 | padding-bottom: 12px; |
| 1221 | } |
| 1222 | |
| 1223 | body#error-page br { |
| 1224 | display: none; |
| 1225 | } |
| 1226 | |
| 1227 | p { |
| 1228 | margin: 12px 0 0 0; |
| 1229 | } |
| 1230 | |
| 1231 | body#error-page a { |
| 1232 | margin-top: 16px; |
| 1233 | display: block; |
| 1234 | } |
| 1235 | |
| 1236 | #error-page p.kubio-mrn-rollback-message>a { |
| 1237 | display: inline; |
| 1238 | } |
| 1239 | </style> |
| 1240 | <?php |
| 1241 | _kubio_requirements_not_met_notice(); |
| 1242 | $content = ob_get_clean(); |
| 1243 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 1244 | wp_die( $content ); |
| 1245 | } |
| 1246 | |
| 1247 | if ( ! current_user_can( 'edit_theme_options' ) ) { |
| 1248 | wp_die( |
| 1249 | '<h1>' . esc_html__( 'You need a higher level of permission.', 'kubio' ) . '</h1>' . |
| 1250 | '<p>' . esc_html__( 'Sorry, you are not allowed to customize this site.', 'kubio' ) . '</p>', |
| 1251 | 403 |
| 1252 | ); |
| 1253 | } |
| 1254 | } |
| 1255 | }, |
| 1256 | 5 |
| 1257 | ); |
| 1258 | |
| 1259 | /** |
| 1260 | * Registers block editor 'kubio_section' post type. |
| 1261 | */ |
| 1262 | function kubio_register_kubio_section_post_type() { |
| 1263 | if ( post_type_exists( 'kubio_section' ) ) { |
| 1264 | return; |
| 1265 | } |
| 1266 | |
| 1267 | $args = array( |
| 1268 | 'label' => __( 'Kubio Section', 'kubio' ), |
| 1269 | 'public' => false, |
| 1270 | 'show_ui' => false, |
| 1271 | 'show_in_rest' => true, |
| 1272 | 'show_in_menu' => false, |
| 1273 | 'show_in_nav_menus' => false, |
| 1274 | 'rewrite' => false, |
| 1275 | 'exclude_from_search' => true, |
| 1276 | 'publicly_queryable' => false, |
| 1277 | 'rest_base' => 'kubio_section', |
| 1278 | 'capabilities' => array( |
| 1279 | 'read' => 'edit_theme_options', |
| 1280 | 'create_posts' => 'edit_theme_options', |
| 1281 | 'edit_posts' => 'edit_theme_options', |
| 1282 | 'edit_published_posts' => 'edit_theme_options', |
| 1283 | 'delete_published_posts' => 'edit_theme_options', |
| 1284 | 'edit_others_posts' => 'edit_theme_options', |
| 1285 | 'delete_others_posts' => 'edit_theme_options', |
| 1286 | ), |
| 1287 | 'map_meta_cap' => true, |
| 1288 | 'supports' => array( |
| 1289 | 'title', |
| 1290 | 'editor', |
| 1291 | 'revisions', |
| 1292 | ), |
| 1293 | ); |
| 1294 | |
| 1295 | register_post_type( 'kubio_section', $args ); |
| 1296 | } |
| 1297 | |
| 1298 | add_action( 'init', 'kubio_register_kubio_section_post_type', 9 ); |
| 1299 | |
| 1300 | |
| 1301 | /** |
| 1302 | * Registers block editor 'kubio_section' post type. |
| 1303 | */ |
| 1304 | function kubio_register_kubio_favorites_post_type() { |
| 1305 | if ( post_type_exists( 'kubio_favorites' ) ) { |
| 1306 | return; |
| 1307 | } |
| 1308 | |
| 1309 | $args = array( |
| 1310 | 'label' => __( 'Kubio Favorites', 'kubio' ), |
| 1311 | 'public' => false, |
| 1312 | 'show_ui' => false, |
| 1313 | 'show_in_rest' => true, |
| 1314 | 'show_in_menu' => false, |
| 1315 | 'show_in_nav_menus' => false, |
| 1316 | 'rewrite' => false, |
| 1317 | 'exclude_from_search' => true, |
| 1318 | 'publicly_queryable' => false, |
| 1319 | 'rest_base' => 'kubio_favorites', |
| 1320 | 'capabilities' => array( |
| 1321 | 'read' => 'edit_theme_options', |
| 1322 | 'create_posts' => 'edit_theme_options', |
| 1323 | 'edit_posts' => 'edit_theme_options', |
| 1324 | 'edit_published_posts' => 'edit_theme_options', |
| 1325 | 'delete_published_posts' => 'edit_theme_options', |
| 1326 | 'edit_others_posts' => 'edit_theme_options', |
| 1327 | 'delete_others_posts' => 'edit_theme_options', |
| 1328 | ), |
| 1329 | 'map_meta_cap' => true, |
| 1330 | 'supports' => array( |
| 1331 | 'title', |
| 1332 | 'editor', |
| 1333 | 'revisions', |
| 1334 | ), |
| 1335 | 'can_export' => false, |
| 1336 | ); |
| 1337 | |
| 1338 | register_post_type( 'kubio_favorites', $args ); |
| 1339 | } |
| 1340 | |
| 1341 | add_action( 'init', 'kubio_register_kubio_favorites_post_type', 9 ); |
| 1342 | |
| 1343 | //From gutenberg_bootstrap_server_block_bindings_sources |
| 1344 | function kubio_load_block_bindings_sources() { |
| 1345 | if ( ! kubio_is_kubio_editor_page() ) { |
| 1346 | return; |
| 1347 | } |
| 1348 | $registered_sources = get_all_registered_block_bindings_sources(); |
| 1349 | if ( ! empty( $registered_sources ) ) { |
| 1350 | $filtered_sources = array(); |
| 1351 | foreach ( $registered_sources as $source ) { |
| 1352 | $filtered_sources[] = array( |
| 1353 | 'name' => $source->name, |
| 1354 | 'label' => $source->label, |
| 1355 | 'usesContext' => $source->uses_context, |
| 1356 | ); |
| 1357 | } |
| 1358 | $encoded_data = wp_json_encode( $filtered_sources ); |
| 1359 | ob_start(); |
| 1360 | ?> |
| 1361 | <script> |
| 1362 | (function() { |
| 1363 | var kubioBindingSources = JSON.parse('<?php echo wp_kses_post( $encoded_data ); ?>'); |
| 1364 | |
| 1365 | let bindingSourcesAreSupported = wp.blocks && wp.blocks.getBlockBindingsSource && wp.blocks.registerBlockBindingsSource; |
| 1366 | if (!bindingSourcesAreSupported) { |
| 1367 | return |
| 1368 | } |
| 1369 | for (const source of kubioBindingSources) { |
| 1370 | !wp.blocks.getBlockBindingsSource(source.name) && wp.blocks.registerBlockBindingsSource(source); |
| 1371 | } |
| 1372 | })() |
| 1373 | </script> |
| 1374 | <?php |
| 1375 | |
| 1376 | // phpcs:disable WordPress.WP.AlternativeFunctions.strip_tags_strip_tags |
| 1377 | $script = strip_tags( ob_get_clean() ); |
| 1378 | wp_add_inline_script( |
| 1379 | 'wp-blocks', |
| 1380 | $script |
| 1381 | ); |
| 1382 | } |
| 1383 | } |
| 1384 | |
| 1385 | add_action( 'enqueue_block_editor_assets', 'kubio_load_block_bindings_sources', 5 ); |
| 1386 | |
| 1387 | //0057618: Button styles do not work in kubio editor |
| 1388 | //Make the classic theme as a depedency for the block style variations to load the styles in the correct order so the classic |
| 1389 | //theme css does not override the variation styles |
| 1390 | add_action( |
| 1391 | 'wp_enqueue_scripts', |
| 1392 | function () { |
| 1393 | $variation_handle = 'block-style-variation-styles'; |
| 1394 | $classic_handle = 'classic-theme-styles'; |
| 1395 | // Ensure both styles are registered before modifying them |
| 1396 | if ( wp_style_is( $classic_handle, 'registered' ) && wp_style_is( $variation_handle, 'registered' ) ) { |
| 1397 | |
| 1398 | $style = wp_styles()->registered[ $variation_handle ]; |
| 1399 | $handle = $style->handle; |
| 1400 | $src = $style->src; |
| 1401 | $deps = $style->deps; |
| 1402 | $ver = $style->ver; |
| 1403 | $media = $style->args; |
| 1404 | $extra = $style->extra; |
| 1405 | |
| 1406 | if ( ! in_array( $classic_handle, $deps ) ) { |
| 1407 | $deps[] = $classic_handle; |
| 1408 | } |
| 1409 | |
| 1410 | $inline_styles = LodashBasic::get( $extra, 'after', array() ); |
| 1411 | |
| 1412 | // Deregister and re-register the style with updated dependencies |
| 1413 | wp_deregister_style( $handle ); |
| 1414 | wp_register_style( $handle, $src, $deps, $ver, $media ); |
| 1415 | wp_enqueue_style( $handle ); |
| 1416 | |
| 1417 | // Reapply the captured inline styles |
| 1418 | foreach ( $inline_styles as $inline_style ) { |
| 1419 | wp_add_inline_style( $handle, $inline_style ); |
| 1420 | } |
| 1421 | } |
| 1422 | }, |
| 1423 | 100 |
| 1424 | ); |
| 1425 |