| 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 |
* True on a modern-mode PB form CPT block-editor screen. |
| 28 |
* |
| 29 |
* @return bool |
| 30 |
*/ |
| 31 |
function wppb_fb_is_form_block_editor_screen() { |
| 32 |
if ( ! function_exists( 'get_current_screen' ) ) return false; |
| 33 |
$screen = get_current_screen(); |
| 34 |
if ( ! $screen || ! in_array( $screen->post_type, array( 'wppb-rf-cpt', 'wppb-epf-cpt' ), true ) ) { |
| 35 |
return false; |
| 36 |
} |
| 37 |
return (bool) $screen->is_block_editor(); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Drop third-party TinyMCE external plugins on PB form CPT screens. |
| 42 |
* |
| 43 |
* `mce_external_plugins` entries are URLs TinyMCE fetches itself, so the script |
| 44 |
* gate below never sees them. The companion handles that carry their localized |
| 45 |
* data are plain script handles, so they *are* dequeued -- the external plugin |
| 46 |
* then initializes against a missing global and TinyMCE throws |
| 47 |
* "Failed to initialize plugin: <name>" (e.g. TablePress' `tablepress_tinymce`, |
| 48 |
* whose `init()` reads the `tablepress_editor_button` const). |
| 49 |
* |
| 50 |
* Nothing is lost: the inserter is locked to PB blocks, so no TinyMCE-authored |
| 51 |
* content exists on these screens. |
| 52 |
*/ |
| 53 |
add_filter( 'mce_external_plugins', 'wppb_fb_gate_mce_external_plugins', 999 ); |
| 54 |
function wppb_fb_gate_mce_external_plugins( $plugins ) { |
| 55 |
if ( ! wppb_fb_is_form_block_editor_screen() ) return $plugins; |
| 56 |
|
| 57 |
return apply_filters( 'wppb_fb_allowed_mce_external_plugins', array(), $plugins ); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Dequeue non-allowlisted editor scripts on PB form CPT screens. |
| 62 |
* Head: priority 19 (before print_head_scripts at 20). |
| 63 |
* Footer: priority 9 (before _wp_footer_scripts at 10). |
| 64 |
*/ |
| 65 |
add_action( 'admin_print_scripts', 'wppb_fb_gate_editor_scripts', 19 ); |
| 66 |
add_action( 'admin_print_footer_scripts', 'wppb_fb_gate_editor_scripts', 9 ); |
| 67 |
function wppb_fb_gate_editor_scripts() { |
| 68 |
if ( ! wppb_fb_is_form_block_editor_screen() ) { |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
$allowlist = apply_filters( 'wppb_fb_allowed_editor_scripts', array( |
| 73 |
'wppb-form-editor-bundle', |
| 74 |
) ); |
| 75 |
|
| 76 |
// Core handles suppressed despite the wp- prefix exemption. |
| 77 |
// wp-block-directory: inserter already locked to PB blocks. |
| 78 |
$denylist = apply_filters( 'wppb_fb_denied_editor_scripts', array( |
| 79 |
'wp-block-directory', |
| 80 |
) ); |
| 81 |
|
| 82 |
$core_prefixes = array( |
| 83 |
'wp-', 'react', 'react-dom', 'lodash', 'jquery', 'underscore', |
| 84 |
'moment', 'regenerator-runtime', 'backbone', 'imagesloaded', |
| 85 |
'masonry', 'media-', 'mce-', 'tinymce-', 'tinymce', |
| 86 |
); |
| 87 |
|
| 88 |
$core_path_fragments = array( '/wp-includes/', '/wp-admin/' ); |
| 89 |
|
| 90 |
$scripts = wp_scripts(); |
| 91 |
foreach ( (array) $scripts->queue as $handle ) { |
| 92 |
if ( in_array( $handle, $denylist, true ) ) { |
| 93 |
wp_dequeue_script( $handle ); |
| 94 |
continue; |
| 95 |
} |
| 96 |
|
| 97 |
if ( in_array( $handle, $allowlist, true ) ) { |
| 98 |
continue; |
| 99 |
} |
| 100 |
|
| 101 |
$is_core_handle = false; |
| 102 |
foreach ( $core_prefixes as $prefix ) { |
| 103 |
if ( $handle === rtrim( $prefix, '-' ) || strpos( $handle, $prefix ) === 0 ) { |
| 104 |
$is_core_handle = true; |
| 105 |
break; |
| 106 |
} |
| 107 |
} |
| 108 |
if ( $is_core_handle ) continue; |
| 109 |
|
| 110 |
$registered = isset( $scripts->registered[ $handle ] ) ? $scripts->registered[ $handle ] : null; |
| 111 |
$src = $registered ? (string) $registered->src : ''; |
| 112 |
|
| 113 |
$is_core_src = false; |
| 114 |
foreach ( $core_path_fragments as $fragment ) { |
| 115 |
if ( $src !== '' && strpos( $src, $fragment ) !== false ) { |
| 116 |
$is_core_src = true; |
| 117 |
break; |
| 118 |
} |
| 119 |
} |
| 120 |
if ( $is_core_src ) continue; |
| 121 |
|
| 122 |
wp_dequeue_script( $handle ); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Strip classic meta-boxes on modern-mode form CPT screens. Skip classic mode |
| 128 |
* (WCK metaboxes are the UI there). Allowlist via `wppb_fb_allowed_meta_boxes`. |
| 129 |
*/ |
| 130 |
add_action( 'add_meta_boxes', 'wppb_fb_gate_meta_boxes', 999 ); |
| 131 |
function wppb_fb_gate_meta_boxes() { |
| 132 |
if ( empty( $GLOBALS['wp_meta_boxes'] ) || ! is_array( $GLOBALS['wp_meta_boxes'] ) ) { |
| 133 |
return; |
| 134 |
} |
| 135 |
|
| 136 |
$allowed = apply_filters( 'wppb_fb_allowed_meta_boxes', array() ); |
| 137 |
|
| 138 |
foreach ( array( 'wppb-rf-cpt', 'wppb-epf-cpt' ) as $post_type ) { |
| 139 |
if ( ! wppb_fb_is_active_for( $post_type ) ) continue; |
| 140 |
|
| 141 |
if ( empty( $GLOBALS['wp_meta_boxes'][ $post_type ] ) ) continue; |
| 142 |
|
| 143 |
foreach ( $GLOBALS['wp_meta_boxes'][ $post_type ] as $context => $priorities ) { |
| 144 |
foreach ( $priorities as $priority => $boxes ) { |
| 145 |
foreach ( (array) $boxes as $id => $box ) { |
| 146 |
if ( in_array( $id, $allowed, true ) ) continue; |
| 147 |
unset( $GLOBALS['wp_meta_boxes'][ $post_type ][ $context ][ $priority ][ $id ] ); |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
|