| 1 |
<?php |
| 2 |
/** |
| 3 |
* Per-CPT editor mode: modern (Gutenberg form-builder) or classic (WCK). |
| 4 |
* Stored in `wppb_forms_editor_mode`; missing keys default to 'modern'. |
| 5 |
* Read at `init` priority < 9 so CPT registration stays stable for the request. |
| 6 |
* Toggle lives in Advanced Settings → Forms under the shared toolbox option group. |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 10 |
|
| 11 |
/** |
| 12 |
* @param string $post_type One of wppb-rf-cpt, wppb-epf-cpt. |
| 13 |
* @return string 'modern' | 'classic' |
| 14 |
*/ |
| 15 |
function wppb_fb_forms_editor_mode( $post_type ) { |
| 16 |
$modes = get_option( 'wppb_forms_editor_mode', array() ); |
| 17 |
if ( isset( $modes[ $post_type ] ) && $modes[ $post_type ] === 'classic' ) { |
| 18 |
return 'classic'; |
| 19 |
} |
| 20 |
return 'modern'; |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* @param string $post_type One of wppb-rf-cpt, wppb-epf-cpt. |
| 25 |
* @return bool |
| 26 |
*/ |
| 27 |
function wppb_fb_is_active_for( $post_type ) { |
| 28 |
return wppb_fb_forms_editor_mode( $post_type ) === 'modern'; |
| 29 |
} |
| 30 |
|
| 31 |
add_filter( 'use_block_editor_for_post', 'wppb_fb_use_block_editor_for_post', 10, 2 ); |
| 32 |
|
| 33 |
/** |
| 34 |
* Force classic editor when mode is classic (covers third-party CPT overrides). |
| 35 |
* |
| 36 |
* @param bool $use Whether the post can be edited in the block editor. |
| 37 |
* @param WP_Post $post The post being checked. |
| 38 |
* @return bool |
| 39 |
*/ |
| 40 |
function wppb_fb_use_block_editor_for_post( $use, $post ) { |
| 41 |
if ( ! $post instanceof WP_Post ) return $use; |
| 42 |
if ( ! in_array( $post->post_type, array( 'wppb-rf-cpt', 'wppb-epf-cpt' ), true ) ) return $use; |
| 43 |
return wppb_fb_is_active_for( $post->post_type ) ? $use : false; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Register under `wppb_toolbox_forms_settings` so one Save Changes persists both. |
| 48 |
*/ |
| 49 |
add_action( 'admin_init', 'wppb_fb_register_editor_mode_setting' ); |
| 50 |
function wppb_fb_register_editor_mode_setting() { |
| 51 |
register_setting( |
| 52 |
'wppb_toolbox_forms_settings', |
| 53 |
'wppb_forms_editor_mode', |
| 54 |
array( |
| 55 |
'type' => 'array', |
| 56 |
'sanitize_callback' => 'wppb_fb_sanitize_editor_mode', |
| 57 |
'default' => array(), |
| 58 |
) |
| 59 |
); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Only known CPT keys and modern|classic values; missing keys default to modern. |
| 64 |
* |
| 65 |
* @param mixed $input Raw submitted value (expected: array<string, string>). |
| 66 |
* @return array<string, string> |
| 67 |
*/ |
| 68 |
function wppb_fb_sanitize_editor_mode( $input ) { |
| 69 |
$allowed = array( 'modern', 'classic' ); |
| 70 |
$out = array(); |
| 71 |
foreach ( array( 'wppb-rf-cpt', 'wppb-epf-cpt' ) as $post_type ) { |
| 72 |
$submitted = ( is_array( $input ) && isset( $input[ $post_type ] ) ) |
| 73 |
? sanitize_key( $input[ $post_type ] ) |
| 74 |
: 'modern'; |
| 75 |
$out[ $post_type ] = in_array( $submitted, $allowed, true ) ? $submitted : 'modern'; |
| 76 |
} |
| 77 |
return $out; |
| 78 |
} |
| 79 |
|
| 80 |
/* |
| 81 |
* No write-back to wppb_module_settings: the read filter already forces both |
| 82 |
* gates to 'show'. Writing that filtered value would permanently rewrite a site |
| 83 |
* option the user never set. |
| 84 |
*/ |
| 85 |
|