| 1 |
<?php |
| 2 |
/** |
| 3 |
* Editor chrome gates for Profile Builder form CPT screens: |
| 4 |
* dequeue third-party editor JS; strip classic meta-boxes (bypassed in classic mode). |
| 5 |
*/ |
| 6 |
|
| 7 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 8 |
|
| 9 |
/** |
| 10 |
* Marker body class for editor-chrome CSS. Block `editor_style` loads on every |
| 11 |
* block-editor screen; SCSS scopes under `.wppb-fb-block-editor`. Canvas iframe |
| 12 |
* body gets the same class from EditorBodyClass.js. |
| 13 |
*/ |
| 14 |
add_filter( 'admin_body_class', 'wppb_fb_add_editor_body_class' ); |
| 15 |
function wppb_fb_add_editor_body_class( $classes ) { |
| 16 |
if ( ! function_exists( 'get_current_screen' ) ) return $classes; |
| 17 |
$screen = get_current_screen(); |
| 18 |
if ( ! $screen || ! $screen->is_block_editor() ) return $classes; |
| 19 |
if ( ! in_array( $screen->post_type, array( 'wppb-rf-cpt', 'wppb-epf-cpt' ), true ) ) return $classes; |
| 20 |
if ( ! wppb_fb_is_active_for( $screen->post_type ) ) return $classes; |
| 21 |
|
| 22 |
// admin_body_class is a space-separated string, not an array. |
| 23 |
return trim( $classes . ' wppb-fb-block-editor' ); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Dequeue non-allowlisted editor scripts on PB form CPT screens. |
| 28 |
* Head: priority 19 (before print_head_scripts at 20). |
| 29 |
* Footer: priority 9 (before _wp_footer_scripts at 10). |
| 30 |
*/ |
| 31 |
add_action( 'admin_print_scripts', 'wppb_fb_gate_editor_scripts', 19 ); |
| 32 |
add_action( 'admin_print_footer_scripts', 'wppb_fb_gate_editor_scripts', 9 ); |
| 33 |
function wppb_fb_gate_editor_scripts() { |
| 34 |
if ( ! function_exists( 'get_current_screen' ) ) return; |
| 35 |
$screen = get_current_screen(); |
| 36 |
if ( ! $screen || ! in_array( $screen->post_type, array( 'wppb-rf-cpt', 'wppb-epf-cpt' ), true ) ) { |
| 37 |
return; |
| 38 |
} |
| 39 |
if ( ! $screen->is_block_editor() ) { |
| 40 |
return; |
| 41 |
} |
| 42 |
|
| 43 |
$allowlist = apply_filters( 'wppb_fb_allowed_editor_scripts', array( |
| 44 |
'wppb-form-editor-bundle', |
| 45 |
) ); |
| 46 |
|
| 47 |
// Core handles suppressed despite the wp- prefix exemption. |
| 48 |
// wp-block-directory: inserter already locked to PB blocks. |
| 49 |
$denylist = apply_filters( 'wppb_fb_denied_editor_scripts', array( |
| 50 |
'wp-block-directory', |
| 51 |
) ); |
| 52 |
|
| 53 |
$core_prefixes = array( |
| 54 |
'wp-', 'react', 'react-dom', 'lodash', 'jquery', 'underscore', |
| 55 |
'moment', 'regenerator-runtime', 'backbone', 'imagesloaded', |
| 56 |
'masonry', 'media-', 'mce-', 'tinymce-', 'tinymce', |
| 57 |
); |
| 58 |
|
| 59 |
$core_path_fragments = array( '/wp-includes/', '/wp-admin/' ); |
| 60 |
|
| 61 |
$scripts = wp_scripts(); |
| 62 |
foreach ( (array) $scripts->queue as $handle ) { |
| 63 |
if ( in_array( $handle, $denylist, true ) ) { |
| 64 |
wp_dequeue_script( $handle ); |
| 65 |
continue; |
| 66 |
} |
| 67 |
|
| 68 |
if ( in_array( $handle, $allowlist, true ) ) { |
| 69 |
continue; |
| 70 |
} |
| 71 |
|
| 72 |
$is_core_handle = false; |
| 73 |
foreach ( $core_prefixes as $prefix ) { |
| 74 |
if ( $handle === rtrim( $prefix, '-' ) || strpos( $handle, $prefix ) === 0 ) { |
| 75 |
$is_core_handle = true; |
| 76 |
break; |
| 77 |
} |
| 78 |
} |
| 79 |
if ( $is_core_handle ) continue; |
| 80 |
|
| 81 |
$registered = isset( $scripts->registered[ $handle ] ) ? $scripts->registered[ $handle ] : null; |
| 82 |
$src = $registered ? (string) $registered->src : ''; |
| 83 |
|
| 84 |
$is_core_src = false; |
| 85 |
foreach ( $core_path_fragments as $fragment ) { |
| 86 |
if ( $src !== '' && strpos( $src, $fragment ) !== false ) { |
| 87 |
$is_core_src = true; |
| 88 |
break; |
| 89 |
} |
| 90 |
} |
| 91 |
if ( $is_core_src ) continue; |
| 92 |
|
| 93 |
wp_dequeue_script( $handle ); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Strip classic meta-boxes on modern-mode form CPT screens. Skip classic mode |
| 99 |
* (WCK metaboxes are the UI there). Allowlist via `wppb_fb_allowed_meta_boxes`. |
| 100 |
*/ |
| 101 |
add_action( 'add_meta_boxes', 'wppb_fb_gate_meta_boxes', 999 ); |
| 102 |
function wppb_fb_gate_meta_boxes() { |
| 103 |
if ( empty( $GLOBALS['wp_meta_boxes'] ) || ! is_array( $GLOBALS['wp_meta_boxes'] ) ) { |
| 104 |
return; |
| 105 |
} |
| 106 |
|
| 107 |
$allowed = apply_filters( 'wppb_fb_allowed_meta_boxes', array() ); |
| 108 |
|
| 109 |
foreach ( array( 'wppb-rf-cpt', 'wppb-epf-cpt' ) as $post_type ) { |
| 110 |
if ( ! wppb_fb_is_active_for( $post_type ) ) continue; |
| 111 |
|
| 112 |
if ( empty( $GLOBALS['wp_meta_boxes'][ $post_type ] ) ) continue; |
| 113 |
|
| 114 |
foreach ( $GLOBALS['wp_meta_boxes'][ $post_type ] as $context => $priorities ) { |
| 115 |
foreach ( $priorities as $priority => $boxes ) { |
| 116 |
foreach ( (array) $boxes as $id => $box ) { |
| 117 |
if ( in_array( $id, $allowed, true ) ) continue; |
| 118 |
unset( $GLOBALS['wp_meta_boxes'][ $post_type ][ $context ][ $priority ][ $id ] ); |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
|