PluginProbe
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor / 4.0.3
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor v4.0.3
4.0.3 4.0.2 4.0.1 4.0.0 3.16.6 3.16.5 3.16.4 3.16.3 3.16.2 3.16.1 3.16.0 3.15.9 3.9.9 3.9.5 3.9.6 3.9.7 3.9.8 1.1.7 1.1.8 1.1.9 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 341 releases
profile-builder / form-builder / form-builder-editor-mode.php

form-builder-editor-mode.php in User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor 4.0.3, at form-builder/form-builder-editor-mode.php

195 lines 6.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * STORED vs EFFECTIVE mode: `wppb_fb_stored_forms_editor_mode()` is the user's
9 * saved preference; `wppb_fb_forms_editor_mode()` is what the request actually
10 * runs on.
11 */
12
13 if ( ! defined( 'ABSPATH' ) ) exit;
14
15 /**
16 * The user's saved preference, ignoring any runtime override.
17 *
18 * Read this only for the settings UI (and the sanitizer's round-trip). Every
19 * behavioral gate must go through wppb_fb_forms_editor_mode() /
20 * wppb_fb_is_active_for() so it sees the effective mode.
21 *
22 * @param string $post_type One of wppb-rf-cpt, wppb-epf-cpt.
23 * @return string 'modern' | 'classic'
24 */
25 function wppb_fb_stored_forms_editor_mode( $post_type ) {
26 $modes = get_option( 'wppb_forms_editor_mode', array() );
27 if ( isset( $modes[ $post_type ] ) && $modes[ $post_type ] === 'classic' ) {
28 return 'classic';
29 }
30 return 'modern';
31 }
32
33 /**
34 * The mode this request runs on: the stored preference, unless the Classic
35 * Editor plugin forces the classic fallback.
36 *
37 * @param string $post_type One of wppb-rf-cpt, wppb-epf-cpt.
38 * @return string 'modern' | 'classic'
39 */
40 function wppb_fb_forms_editor_mode( $post_type ) {
41 if ( wppb_fb_classic_editor_plugin_forces_classic() ) {
42 return 'classic';
43 }
44 return wppb_fb_stored_forms_editor_mode( $post_type );
45 }
46
47 /**
48 * Is the WordPress.org Classic Editor plugin loaded?
49 *
50 * @return bool
51 */
52 function wppb_fb_classic_editor_plugin_active() {
53 return (bool) apply_filters( 'wppb_fb_classic_editor_plugin_active', class_exists( 'Classic_Editor', false ) );
54 }
55
56 /**
57 * Resolve the Classic Editor plugin's effective settings.
58 *
59 * @return array{editor:string,allow-users:bool}|null Null when the plugin is absent.
60 */
61 function wppb_fb_classic_editor_plugin_settings() {
62 if ( ! wppb_fb_classic_editor_plugin_active() ) {
63 return null;
64 }
65
66 $override = apply_filters( 'classic_editor_plugin_settings', false );
67 if ( is_array( $override ) ) {
68 return array(
69 'editor' => ( isset( $override['editor'] ) && $override['editor'] === 'block' ) ? 'block' : 'classic',
70 'allow-users' => ! empty( $override['allow-users'] ),
71 );
72 }
73
74 if ( is_multisite() ) {
75 $defaults = apply_filters(
76 'classic_editor_network_default_settings',
77 array(
78 'editor' => get_network_option( null, 'classic-editor-replace' ) === 'block' ? 'block' : 'classic',
79 'allow-users' => false,
80 )
81 );
82
83 if ( get_network_option( null, 'classic-editor-allow-sites' ) !== 'allow' ) {
84 return array(
85 'editor' => ( isset( $defaults['editor'] ) && $defaults['editor'] === 'block' ) ? 'block' : 'classic',
86 'allow-users' => ! empty( $defaults['allow-users'] ),
87 );
88 }
89
90 $editor_option = get_option( 'classic-editor-replace' );
91 $allow_users_option = get_option( 'classic-editor-allow-users' );
92 if ( $editor_option ) {
93 $defaults['editor'] = $editor_option;
94 }
95 if ( $allow_users_option ) {
96 $defaults['allow-users'] = ( $allow_users_option === 'allow' );
97 }
98
99 return array(
100 'editor' => ( isset( $defaults['editor'] ) && $defaults['editor'] === 'block' ) ? 'block' : 'classic',
101 'allow-users' => ! empty( $defaults['allow-users'] ),
102 );
103 }
104
105 $option = get_option( 'classic-editor-replace' );
106
107 return array(
108 // empty( $option ) || 'classic' || legacy 'replace' => classic.
109 'editor' => ( $option === 'block' || $option === 'no-replace' ) ? 'block' : 'classic',
110 'allow-users' => ( get_option( 'classic-editor-allow-users' ) === 'allow' ),
111 );
112 }
113
114 /**
115 * Should the modern form editor fall back to the Classic Form Design because of
116 * the Classic Editor plugin?
117 *
118 * @return bool
119 */
120 function wppb_fb_classic_editor_plugin_forces_classic() {
121 $settings = wppb_fb_classic_editor_plugin_settings();
122 $forced = ( null !== $settings ) && ( $settings['editor'] !== 'block' || $settings['allow-users'] );
123
124 return (bool) apply_filters( 'wppb_fb_force_classic_for_classic_editor_plugin', $forced, $settings );
125 }
126
127 /**
128 * @param string $post_type One of wppb-rf-cpt, wppb-epf-cpt.
129 * @return bool
130 */
131 function wppb_fb_is_active_for( $post_type ) {
132 return wppb_fb_forms_editor_mode( $post_type ) === 'modern';
133 }
134
135 add_filter( 'use_block_editor_for_post', 'wppb_fb_use_block_editor_for_post', 10, 2 );
136
137 /**
138 * Force classic editor when mode is classic (covers third-party CPT overrides).
139 *
140 * @param bool $use Whether the post can be edited in the block editor.
141 * @param WP_Post $post The post being checked.
142 * @return bool
143 */
144 function wppb_fb_use_block_editor_for_post( $use, $post ) {
145 if ( ! $post instanceof WP_Post ) return $use;
146 if ( ! in_array( $post->post_type, array( 'wppb-rf-cpt', 'wppb-epf-cpt' ), true ) ) return $use;
147 return wppb_fb_is_active_for( $post->post_type ) ? $use : false;
148 }
149
150 /**
151 * Register under `wppb_toolbox_forms_settings` so one Save Changes persists both.
152 */
153 add_action( 'admin_init', 'wppb_fb_register_editor_mode_setting' );
154 function wppb_fb_register_editor_mode_setting() {
155 register_setting(
156 'wppb_toolbox_forms_settings',
157 'wppb_forms_editor_mode',
158 array(
159 'type' => 'array',
160 'sanitize_callback' => 'wppb_fb_sanitize_editor_mode',
161 'default' => array(),
162 )
163 );
164 }
165
166 /**
167 * Only known CPT keys and modern|classic values; missing keys default to modern.
168 *
169 * This writes the STORED preference, never the effective mode -- a Classic
170 * Editor-forced fallback must not be persisted here, or deactivating that plugin
171 * would leave the user stuck in classic. Because a missing key defaults to
172 * 'modern', the settings view emits a hidden field per CPT carrying the stored
173 * value so a disabled radio can't silently wipe an explicit Classic preference.
174 *
175 * @param mixed $input Raw submitted value (expected: array<string, string>).
176 * @return array<string, string>
177 */
178 function wppb_fb_sanitize_editor_mode( $input ) {
179 $allowed = array( 'modern', 'classic' );
180 $out = array();
181 foreach ( array( 'wppb-rf-cpt', 'wppb-epf-cpt' ) as $post_type ) {
182 $submitted = ( is_array( $input ) && isset( $input[ $post_type ] ) )
183 ? sanitize_key( $input[ $post_type ] )
184 : 'modern';
185 $out[ $post_type ] = in_array( $submitted, $allowed, true ) ? $submitted : 'modern';
186 }
187 return $out;
188 }
189
190 /*
191 * No write-back to wppb_module_settings: the read filter already forces both
192 * gates to 'show'. Writing that filtered value would permanently rewrite a site
193 * option the user never set.
194 */
195