Making WordPress.org

Changeset 3335


Ignore:
Timestamp:
06/11/2016 10:07:10 PM (9 years ago)
Author:
obenland
Message:

Plugin Directory: Let plugin authors select categories on upload.

Also refactors create_plugin_post() to me more of a direct wrapper of
wp_insert_post(). Front-end needs styles.

See #1573.

Location:
sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory
Files:
5 edited

Legend:

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

    r3331 r3335  
    2929        add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ) );
    3030        add_action( 'save_post_plugin', array( $this, 'save_plugin_post' ) );
    31         add_filter( 'tax_input_pre', array( $this, 'filter_tax_input' ) );
    3231
    3332        add_action( 'load-edit.php', array( $this, 'bulk_reject_plugins' ) );
     
    469468            update_post_meta( $post_id, 'tested', wp_slash( wp_unslash( $_POST['tested_with'] ) ) );
    470469        }
    471     }
    472 
    473     /**
    474      * Filters the value of tax_inputs before saving.
    475      *
    476      * @param array $tax_input Array of taxonomies with selected terms.
    477      * @return array
    478      */
    479     public function filter_tax_input( $tax_input ) {
    480 
    481         // Limit the amount of assignable categories to 3.
    482         if ( isset( $tax_input['plugin_category'] ) ) {
    483             $tax_input['plugin_category'] = array_slice( array_filter( $tax_input['plugin_category'] ), 0, 3 );
    484         }
    485 
    486         return $tax_input;
    487470    }
    488471
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-plugin-directory.php

    r3331 r3335  
    2626        add_filter( 'term_link', array( $this, 'term_link' ), 10, 2 );
    2727        add_filter( 'pre_insert_term', array( $this, 'pre_insert_term_prevent' ) );
     28        add_filter( 'tax_input_pre', array( $this, 'filter_tax_input' ) );
    2829        add_action( 'pre_get_posts', array( $this, 'use_plugins_in_query' ) );
    2930        add_filter( 'rest_api_allowed_post_types', array( $this, 'filter_allowed_post_types' ) );
     
    3334        // Shim in postmeta support for data which doesn't yet live in postmeta
    3435        add_filter( 'get_post_metadata', array( $this, 'filter_shim_postmeta' ), 10, 3 );
     36
    3537
    3638        add_filter( 'map_meta_cap', array( __NAMESPACE__ . '\Capabilities', 'map_meta_cap' ), 10, 4 );
     
    610612
    611613    /**
     614     * Filters the value of tax_inputs before saving.
     615     *
     616     * Used both in the admin and the uploader.
     617     *
     618     * @param array $tax_input Array of taxonomies with selected terms.
     619     * @return array
     620     */
     621    public function filter_tax_input( $tax_input ) {
     622
     623        // Limit the amount of assignable categories to 3.
     624        if ( isset( $tax_input['plugin_category'] ) ) {
     625            $tax_input['plugin_category'] = array_slice( array_filter( $tax_input['plugin_category'] ), 0, 3 );
     626        }
     627
     628        return $tax_input;
     629    }
     630
     631    /**
    612632     * Retrieve the WP_Post object representing a given plugin.
    613633     *
     
    651671     * Create a new post entry for a given plugin slug.
    652672     *
    653      * @param array $plugin_info {
    654      *     Array of initial plugin post data, all fields are optional.
    655      *
    656      *     @type string $title       The title of the plugin.
    657      *     @type string $slug        The slug of the plugin.
    658      *     @type string $status      The status of the plugin ( 'publish', 'pending', 'disabled', 'closed' ).
    659      *     @type int    $author      The ID of the plugin author.
    660      *     @type string $description The short description of the plugin.
    661      *     @type string $content     The long description of the plugin.
    662      *     @type array  $tags        The tags associated with the plugin.
    663      *     @type array  $meta        The meta information of the plugin.
     673     * @param array $args {
     674     *     An array of elements that make up a post to insert.
     675     *
     676     *     @type int    $ID                    The post ID. If equal to something other than 0,
     677     *                                         the post with that ID will be updated. Default 0.
     678     *     @type int    $post_author           The ID of the user who added the post. Default is
     679     *                                         the current user ID.
     680     *     @type string $post_date             The date of the post. Default is the current time.
     681     *     @type string $post_date_gmt         The date of the post in the GMT timezone. Default is
     682     *                                         the value of `$post_date`.
     683     *     @type mixed  $post_content          The post content. Default empty.
     684     *     @type string $post_content_filtered The filtered post content. Default empty.
     685     *     @type string $post_title            The post title. Default empty.
     686     *     @type string $post_excerpt          The post excerpt. Default empty.
     687     *     @type string $post_status           The post status. Default 'draft'.
     688     *     @type string $post_type             The post type. Default 'post'.
     689     *     @type string $comment_status        Whether the post can accept comments. Accepts 'open' or 'closed'.
     690     *                                         Default is the value of 'default_comment_status' option.
     691     *     @type string $ping_status           Whether the post can accept pings. Accepts 'open' or 'closed'.
     692     *                                         Default is the value of 'default_ping_status' option.
     693     *     @type string $post_password         The password to access the post. Default empty.
     694     *     @type string $post_name             The post name. Default is the sanitized post title
     695     *                                         when creating a new post.
     696     *     @type string $to_ping               Space or carriage return-separated list of URLs to ping.
     697     *                                         Default empty.
     698     *     @type string $pinged                Space or carriage return-separated list of URLs that have
     699     *                                         been pinged. Default empty.
     700     *     @type string $post_modified         The date when the post was last modified. Default is
     701     *                                         the current time.
     702     *     @type string $post_modified_gmt     The date when the post was last modified in the GMT
     703     *                                         timezone. Default is the current time.
     704     *     @type int    $post_parent           Set this for the post it belongs to, if any. Default 0.
     705     *     @type int    $menu_order            The order the post should be displayed in. Default 0.
     706     *     @type string $post_mime_type        The mime type of the post. Default empty.
     707     *     @type string $guid                  Global Unique ID for referencing the post. Default empty.
     708     *     @type array  $post_category         Array of category names, slugs, or IDs.
     709     *                                         Defaults to value of the 'default_category' option.
     710     *     @type array  $tax_input             Array of taxonomy terms keyed by their taxonomy name. Default empty.
     711     *     @type array  $meta_input            Array of post meta values keyed by their post meta key. Default empty.
    664712     * }
    665713     * @return \WP_Post|\WP_Error
    666714     */
    667     static public function create_plugin_post( array $plugin_info ) {
    668         $title   = ! empty( $plugin_info['title'] )       ? $plugin_info['title']       : '';
    669         $slug    = ! empty( $plugin_info['slug'] )        ? $plugin_info['slug']        : sanitize_title( $title );
    670         $status  = ! empty( $plugin_info['status'] )      ? $plugin_info['status']      : 'draft';
    671         $author  = ! empty( $plugin_info['author'] )      ? $plugin_info['author']      : 0;
    672         $desc    = ! empty( $plugin_info['description'] ) ? $plugin_info['description'] : '';
    673         $content = ! empty( $plugin_info['content'] )     ? $plugin_info['content']     : '';
    674         $meta    = ! empty( $plugin_info['meta'] )        ? $plugin_info['meta']        : array();
    675 
    676         $post_date         = ! empty( $plugin_info['post_date'] )         ? $plugin_info['post_date']         : '';
    677         $post_date_gmt     = ! empty( $plugin_info['post_date_gmt'] )     ? $plugin_info['post_date_gmt']     : '';
    678         $post_modified     = ! empty( $plugin_info['post_modified'] )     ? $plugin_info['post_modified']     : '';
    679         $post_modified_gmt = ! empty( $plugin_info['post_modified_gmt'] ) ? $plugin_info['post_modified_gmt'] : '';
    680 
    681         $id = wp_insert_post( array(
    682             'post_type'    => 'plugin',
    683             'post_status'  => $status,
    684             'post_name'    => $slug,
    685             'post_title'   => $title ?: $slug,
    686             'post_author'  => $author,
    687             'post_content' => $content,
    688             'post_excerpt' => $desc,
    689             'meta_input'   => $meta,
    690             'post_date'         => $post_date,
    691             'post_date_gmt'     => $post_date_gmt,
    692             'post_modified'     => $post_modified,
    693             'post_modified_gmt' => $post_modified_gmt,
    694         ), true );
    695 
    696         if ( is_wp_error( $id ) ) {
    697             return $id;
    698         }
    699 
    700         wp_cache_set( $id, $slug, 'plugin-slugs' );
    701 
    702         return get_post( $id );
     715    public static function create_plugin_post( array $args ) {
     716        $title = $args['post_title'] ?: $args['post_name'];
     717        $slug  = $args['post_name']  ?: sanitize_title( $title );
     718
     719        $args = wp_parse_args( $args, array(
     720            'post_title'        => $title,
     721            'post_name'         => $slug,
     722            'post_type'         => 'plugin',
     723            'post_date'         => '',
     724            'post_date_gmt'     => '',
     725            'post_modified'     => '',
     726            'post_modified_gmt' => '',
     727        ) );
     728
     729        $result = wp_insert_post( $args, true );
     730
     731        if ( ! is_wp_error( $result ) ) {
     732            wp_cache_set( $result, $slug, 'plugin-slugs' );
     733            $result = get_post( $result );
     734        }
     735
     736        return $result;
    703737    }
    704738}
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/cli/class-import.php

    r3331 r3335  
    8383
    8484            $plugin = Plugin_Directory::create_plugin_post( array(
    85                 'slug' => $plugin_slug,
    86                 'status' => $status,
    87                 'author' => $topic->topic_poster,
     85                'post_name' => $plugin_slug,
     86                'post_status' => $status,
     87                'post_author' => $topic->topic_poster,
    8888                'override_modified_date' => true,
    8989                'post_date_gmt' => $topic->topic_start_time,
     
    9191                'post_modified' => $topic->topic_time,
    9292                'post_modified_gmt' => $topic->topic_time,
    93                 'meta' => array(
     93                'meta_input' => array(
    9494                    '_author_ip' => $author_ip,
    9595                ),
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/shortcodes/class-upload-handler.php

    r3331 r3335  
    175175            // Add a Plugin Directory entry for this plugin.
    176176            $plugin_post = Plugin_Directory::create_plugin_post( array(
    177                 'title'       => $this->plugin['Name'],
    178                 'slug'        => $this->plugin_slug,
    179                 'status'      => 'draft',
    180                 'author'      => get_current_user_id(),
    181                 'content'     => $content,
    182                 'description' => $this->plugin['Description'],
    183                 'tags'        => $readme->tags,
    184                 'meta'        => array(
     177                'post_title'   => $this->plugin['Name'],
     178                'post_name'    => $this->plugin_slug,
     179                'post_content' => $content,
     180                'post_excerpt' => $this->plugin['Description'],
     181                'tax_input'    => wp_unslash( $_POST['tax_input'] ),
     182                'meta_input'   => array(
    185183                    'tested'                   => $readme->tested,
    186184                    'requires'                 => $readme->requires,
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/shortcodes/class-upload.php

    r3230 r3335  
    1111
    1212        if ( is_user_logged_in() ) :
     13            include_once ABSPATH . 'wp-admin/includes/template.php';
    1314
    1415            if ( ! empty( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'wporg-plugins-upload' ) && 'upload' === $_POST['action'] ) :
     
    4142            <?php endif; ?>
    4243
    43             <form enctype="multipart/form-data" id="upload_form" method="POST" action="">
     44            <form id="upload_form" enctype="multipart/form-data" method="POST" action="">
    4445                <?php wp_nonce_field( 'wporg-plugins-upload' ); ?>
    4546                <input type="hidden" name="action" value="upload"/>
     47                <fieldset>
     48                    <legend><?php _e( 'Plugin Categories (up to 3)', 'wporg-plugins' ); ?></legend>
     49                    <ul class="category-checklist form-no-clear">
     50                        <?php wp_terms_checklist( 0, array( 'taxonomy' => 'plugin_category' ) ); ?>
     51                    </ul>
     52                </fieldset>
    4653                <input type="file" id="zip_file" name="zip_file" size="25"/>
    4754                <input id="upload_button" class="button" type="submit" value="<?php esc_attr_e( 'Upload', 'wporg-plugins' ); ?>"/>
Note: See TracChangeset for help on using the changeset viewer.