Making WordPress.org

Changeset 5727


Ignore:
Timestamp:
07/31/2017 08:51:48 PM (7 years ago)
Author:
Otto42
Message:

Plugin Directory: Prevent plugin slugs from clashing. Fixes #3011. props @SergeyBiryukov

File:
1 edited

Legend:

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

    r5714 r5727  
    3737        add_action( 'all_admin_notices', array( $this, 'admin_notices' ) );
    3838        add_filter( 'display_post_states', array( $this, 'post_states' ), 10, 2 );
     39
     40        add_filter( 'wp_insert_post_data', array( $this, 'check_existing_plugin_slug_on_post_update' ), 10, 2 );
     41        add_filter( 'wp_unique_post_slug', array( $this, 'check_existing_plugin_slug_on_inline_save' ), 10, 6 );
    3942
    4043        add_action( 'wp_ajax_replyto-comment', array( $this, 'save_custom_comment' ), 0 );
     
    313316
    314317    /**
     318     * Check if a plugin with the provided slug already exists.
     319     *
     320     * Runs on 'wp_insert_post_data' when editing a plugin on Edit Plugin screen.
     321     *
     322     * @param array $data    The data to be inserted into the database.
     323     * @param array $postarr The raw data passed to `wp_insert_post()`.
     324     * @return array The data to insert into the database.
     325     */
     326    function check_existing_plugin_slug_on_post_update( $data, $postarr ) {
     327        if ( 'plugin' !== $data['post_type'] || ! isset( $postarr['ID'] ) ) {
     328            return $data;
     329        }
     330       
     331        $existing_plugin = Plugin_Directory\Plugin_Directory::get_plugin_post( $data['post_name'] );
     332
     333        // Is there already a plugin with the same slug?
     334        if ( $existing_plugin && $existing_plugin->ID != $postarr['ID'] ) {
     335            wp_die( sprintf(
     336                /* translators: %s: plugin slug */
     337                __( 'Error: The plugin %s already exists.', 'wporg-plugins' ),
     338                $data['post_name']
     339            ) );
     340        }
     341
     342        return $data;
     343    }
     344
     345    /**
     346     * Check if a plugin with the provided slug already exists.
     347     *
     348     * Runs on 'wp_unique_post_slug' when editing a plugin via Quick Edit.
     349     *
     350     * @param string $slug          The unique post slug.
     351     * @param int    $post_ID       Post ID.
     352     * @param string $post_status   Post status.
     353     * @param string $post_type     Post type.
     354     * @param int    $post_parent   Post parent ID.
     355     * @param string $original_slug The original post slug.
     356     * @return string The unique post slug.
     357     */
     358    function check_existing_plugin_slug_on_inline_save( $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug ) {
     359        if ( 'plugin' !== $post_type ) {
     360            return $slug;
     361        }
     362
     363        // Did wp_unique_post_slug() change the slug to avoid a conflict?
     364        if ( $slug !== $original_slug ) {
     365            wp_die( sprintf(
     366                /* translators: %s: plugin slug */
     367                __( 'Error: The plugin %s already exists.', 'wporg-plugins' ),
     368                $original_slug
     369            ) );
     370        }
     371
     372        return $slug;
     373    }
     374
     375    /**
    315376     * Register the Admin metaboxes for the plugin management screens.
    316377     *
Note: See TracChangeset for help on using the changeset viewer.