Making WordPress.org


Ignore:
Timestamp:
02/13/2024 01:20:43 AM (11 months ago)
Author:
dd32
Message:

Plugin Directory: Allow pending plugins to have a slug when updated by a non-reviewer.

In [13198] we've allowed pending plugins to upload new ZIPs for review, but this triggers a wp_update_post() which clears the post_name field for users who can't publish a plugin.
This overrides that and prevents the slug overwrite.

See #7384.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-plugin-directory.php

    r13196 r13210  
    7979        add_action( 'rest_api_init', array( __NAMESPACE__ . '\API\Base', 'init' ) );
    8080
    81         // Allow post_modified not to be modified when we don't specifically bump it.
     81        // Allow post_modified not to be modified when we don't specifically bump it, and slugs for pending plugins.
    8282        add_filter( 'wp_insert_post_data', array( $this, 'filter_wp_insert_post_data' ), 10, 2 );
    8383
     
    119119    /**
    120120     * Filters `wp_insert_post()` to respect the presented data.
     121     *
    121122     * This function overrides `wp_insert_post()`s constant updating of
    122      * the post_modified fields.
     123     * the post_modified fields, and allows for pending posts to have a slug.
    123124     *
    124125     * @param array $data    The data to be inserted into the database.
     
    128129     */
    129130    public function filter_wp_insert_post_data( $data, $postarr ) {
    130         if ( 'plugin' === $postarr['post_type'] ) {
    131             $data['post_modified']     = $postarr['post_modified'];
    132             $data['post_modified_gmt'] = $postarr['post_modified_gmt'];
    133         }
     131        if ( 'plugin' !== $postarr['post_type'] ) {
     132            return $data;
     133        }
     134
     135        // Allow setting post_modified fields.
     136        $data['post_modified']     = $postarr['post_modified'];
     137        $data['post_modified_gmt'] = $postarr['post_modified_gmt'];
     138
     139        /*
     140         * wp_insert_post() does not allow `pending` posts to have a slug, unless the user can publish it.
     141         *
     142         * Inherit the previous slug, never allowing it to go to empty for this case.
     143         *
     144         * There's an edgecase here, where we might be inserting a post as pending for the first time,
     145         * in that case we just do our best to respect the data provided..
     146         */
     147        if (
     148            'pending' === $data['post_status'] &&
     149            empty( $data['post_name'] )
     150        ) {
     151            if ( ! empty( $postarr['ID'] ) ) {
     152                // Updating an existing post.
     153                $data['post_name'] = get_post_field( 'post_name', $postarr['ID'] );
     154            } else {
     155                // New insert, we'll just hope that it was specified.
     156                $data['post_name'] = $postarr['post_name'] ?? '';
     157            }
     158        }
     159
    134160        return $data;
    135161    }
Note: See TracChangeset for help on using the changeset viewer.