Making WordPress.org


Ignore:
Timestamp:
06/11/2016 10:07:10 PM (8 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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}
Note: See TracChangeset for help on using the changeset viewer.