Changeset 12330 for sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc/class-blocks.php
- Timestamp:
- 12/15/2022 06:08:56 AM (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc/class-blocks.php
r12326 r12330 9 9 */ 10 10 class Blocks { 11 12 public $forum_enabled_by_default = false; 13 public $user_enabled_by_default = true; 14 11 15 public function __construct() { 16 if ( null !== get_option( 'forum_block_editor_enabled', null ) ) { 17 $this->forum_enabled_by_default = get_option( 'forum_block_editor_enabled' ); 18 } 19 if ( null !== get_option( 'user_block_editor_enabled', null ) ) { 20 $this->user_enabled_by_default = get_option( 'user_block_editor_enabled' ); 21 } 22 12 23 // Enable bbPress support. 13 24 add_filter( 'blocks_everywhere_bbpress', '__return_true' ); … … 36 47 // Add block patterns. 37 48 add_filter( 'init', [ $this, 'register_predefs' ] ); 49 50 // Add user opt-in/out 51 add_action( 'bbp_user_edit_after', [ $this, 'bbp_user_edit_after' ], 11 ); 52 add_action( 'bbp_profile_update', [ $this, 'bbp_profile_update' ], 10, 1 ); 53 add_filter( 'blocks_everywhere_bbpress_editor', [ $this, 'blocks_everywhere_bbpress_editor' ] ); 54 55 // Add forum opt-in/out. 56 add_action( 'admin_init', [ $this, 'admin_init' ] ); 57 add_action( 'save_post', [ $this, 'metabox_forum_optin_save_handler' ] ); 38 58 } 39 59 … … 83 103 ) ); 84 104 85 // Add patterns 105 // Add patterns. 86 106 $settings['editor']['__experimentalBlockPatterns'] = WP_Block_Patterns_Registry::get_instance()->get_all_registered(); 87 107 $settings['editor']['__experimentalBlockPatternCategories'] = WP_Block_Pattern_Categories_Registry::get_instance()->get_all_registered(); … … 133 153 134 154 /** 155 * Add an option to the user profile to enable/disable it. 156 */ 157 public function bbp_user_edit_after() { 158 $user_id = bbp_get_displayed_user_id(); 159 $user_status = get_user_option( 'block_editor', $user_id ) ?: 'default'; 160 $default = $this->user_enabled_by_default ? 'enabled' : 'disabled'; 161 162 // TODO: Enable for all users 163 if ( ! current_user_can( 'moderate' ) && 'default' === $user_status ) { 164 return; 165 } 166 167 // TODO: Checkbox at public launch. A checkbox doesn't make sense until it's enabled-by-default. 168 printf( 169 '<p> 170 <select name="block_editor" id="block_editor"> 171 <option value="default" %s>Default (%s)</option> 172 <option value="enabled" %s>Enabled (yes)</option> 173 <option value="disabled" %s>Disabled (no)</option> 174 </select> 175 <label for="disable_block_editor">%s</label> 176 </p>', 177 selected( $user_status, 'default', false ), 178 esc_html( $default ), 179 selected( $user_status, 'enabled', false ), 180 selected( $user_status, 'disabled', false ), 181 'Use the Block Editor for new topics and replies.', 182 ); 183 } 184 185 /** 186 * Save the user option to enable/disable. 187 */ 188 public function bbp_profile_update( $user_id ) { 189 if ( empty( $_REQUEST['block_editor'] ) ) { 190 return; 191 } 192 193 $value = sanitize_key( wp_unslash( $_REQUEST['block_editor'] ) ); 194 if ( 'default' === $value ) { 195 delete_user_option( $user_id, 'block_editor' ); 196 } else { 197 update_user_option( $user_id, 'block_editor', $value, false ); 198 } 199 200 } 201 202 /** 203 * Add an admin interface to enable/disable the Block Editor for a forum. 204 */ 205 public function admin_init() { 206 add_meta_box( 'block_editor', 'Block Editor for Topics/Replies', [ $this, 'metabox_forum_optin' ], 'forum', 'side' ); 207 } 208 209 /** 210 * Display the forum opt-in for the Block Editor. 211 */ 212 function metabox_forum_optin() { 213 global $post; 214 215 $forum_status = $post->block_editor ?: 'default'; 216 $default = $this->forum_enabled_by_default ? 'enabled' : 'disabled'; 217 218 printf( 219 '<p> 220 <select name="block_editor" id="block_editor"> 221 <option value="default" %s>Default (%s)</option> 222 <option value="enabled" %s>Enabled</option> 223 <option value="disabled" %s>Disabled</option> 224 </select> 225 </p>', 226 selected( $forum_status, 'default', false ), 227 esc_html( $default ), 228 selected( $forum_status, 'enabled', false ), 229 selected( $forum_status, 'disabled', false ), 230 ); 231 } 232 /** 233 * Save the values for ::metabox_forum_optin(). 234 * 235 * @param WP_Post $post The post being edited. 236 */ 237 function metabox_forum_optin_save_handler( $post_id ) { 238 $post = get_post( $post_id ); 239 if ( 240 ! $post || 241 'forum' !== $post->post_type || 242 ! current_user_can( 'edit_post', $post->ID ) || 243 ! isset( $_REQUEST['block_editor'] ) 244 ) { 245 return; 246 } 247 248 $value = sanitize_key( wp_unslash( $_REQUEST['block_editor'] ) ); 249 if ( 'default' === $value ) { 250 delete_post_meta( $post->ID, 'block_editor' ); 251 } else { 252 update_post_meta( $post->ID, 'block_editor', $value ); 253 } 254 } 255 256 /** 257 * Conditionally disable the Block Editor under certain circumstances. 258 * 259 * Those circumstances are: 260 * - The user has disabled the editor. 261 * - The default is forum opt-in, and the forum has the block_editor not enabled. 262 * - The topic/reply being edited was not created in the Block Editor. 263 */ 264 public function blocks_everywhere_bbpress_editor( $use_it ) { 265 if ( ! $use_it ) { 266 return $use_it; 267 } 268 269 $user_id = get_current_user_id(); 270 271 // Respect the user option. 272 $user_option = get_user_option( 'block_editor', $user_id ); 273 if ( ! $user_option ) { 274 $user_option = $this->user_enabled_by_default ? 'enabled' : 'disabled'; 275 } 276 277 // Determine if the forum has the editor enabled. 278 $forum = bbp_get_forum( bbp_get_forum_id() ); 279 $enabled_for_forum = ( 'enabled' === $forum->block_editor || ( ! $forum->block_editor && $this->forum_enabled_by_default ) ); 280 $enabled_for_user = ( 'disabled' !== $user_option ); 281 $use_it = ( $enabled_for_user && $enabled_for_forum ); 282 283 // If we're editing a post made without the editor, let's respect that. 284 if ( $use_it && bbp_is_reply_edit() ) { 285 $reply = bbp_get_reply( bbp_get_reply_id() ); 286 287 if ( $reply && ! has_blocks( $reply->post_content ) ) { 288 $use_it = false; 289 } 290 } elseif ( $use_it && bbp_is_topic_edit() ) { 291 if ( ! has_blocks( get_post_field( 'post_content', bbp_get_topic_id() ) ) ) { 292 $use_it = false; 293 } 294 } 295 296 return $use_it; 297 } 298 299 /** 135 300 * Register pre-defs for the forums. 136 301 */
Note: See TracChangeset
for help on using the changeset viewer.