Make WordPress Core

Changeset 44241


Ignore:
Timestamp:
12/16/2018 10:51:01 PM (6 years ago)
Author:
SergeyBiryukov
Message:

Meta Boxes: Add the block_editor_meta_box_hidden_fields action.

Lacking an appropriate action in the classic editor, plugins that add meta boxes have historically hooked into various actions in order to add hidden input fields.

This change also adds backwards compatibility for two of the most common: edit_form_after_title, and edit_form_advanced.

Props pento, danielbachhuber.
Merges [43882] to trunk.
Fixes #45283.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/src/wp-admin/edit-form-blocks.php

    r44223 r44241  
    359359register_and_do_post_meta_boxes( $post );
    360360
    361 // Some meta boxes hook into the 'edit_form_advanced' filter.
    362 /** This action is documented in wp-admin/edit-form-advanced.php */
    363 do_action( 'edit_form_advanced', $post );
    364 
    365361require_once( ABSPATH . 'wp-admin/admin-header.php' );
    366362?>
  • trunk/src/wp-admin/includes/post.php

    r44240 r44241  
    23082308    $user_id      = $current_user->ID;
    23092309    wp_nonce_field( $nonce_action );
     2310
     2311    /*
     2312     * Some meta boxes hook into these actions to add hidden input fields in the classic post form. For backwards
     2313     * compatibility, we can capture the output from these actions, and extract the hidden input fields.
     2314     */
     2315    $actions = array(
     2316        'edit_form_after_title',
     2317        'edit_form_advanced',
     2318    );
     2319
     2320    foreach ( $actions as $action ) {
     2321        ob_start();
     2322        do_action_deprecated(
     2323            $action,
     2324            array( $post ),
     2325            '5.0.0',
     2326            'block_editor_meta_box_hidden_fields',
     2327            __( 'This action is still supported in the classic editor, but is deprecated in the block editor.' )
     2328        );
     2329        $classic_output = ob_get_clean();
     2330
     2331        if ( ! $classic_output ) {
     2332            continue;
     2333        }
     2334
     2335        $classic_elements = wp_html_split( $classic_output );
     2336        $hidden_inputs    = '';
     2337        foreach( $classic_elements as $element ) {
     2338            if ( 0 !== strpos( $element, '<input ') ) {
     2339                continue;
     2340            }
     2341
     2342            if ( preg_match( '/\stype=[\'"]hidden[\'"]\s/', $element ) ) {
     2343                echo $element;
     2344            }
     2345        }
     2346    }
    23102347    ?>
    23112348    <input type="hidden" id="user-id" name="user_ID" value="<?php echo (int) $user_id; ?>" />
     
    23252362    // Permalink title nonce.
    23262363    wp_nonce_field( 'samplepermalink', 'samplepermalinknonce', false );
    2327 }
     2364
     2365    /**
     2366     * Add hidden input fields to the meta box save form.
     2367     *
     2368     * Hook into this action to print `<input type="hidden" ... />` fields, which will be POSTed back to
     2369     * the server when meta boxes are saved.
     2370     *
     2371     * @since 5.0.0
     2372     *
     2373     * @params WP_Post $post The post that is being edited.
     2374     */
     2375    do_action( 'block_editor_meta_box_hidden_fields', $post );
     2376}
Note: See TracChangeset for help on using the changeset viewer.