Making WordPress.org

Changeset 2561


Ignore:
Timestamp:
02/24/2016 06:44:33 AM (9 years ago)
Author:
dd32
Message:

Plugins Directory: Split get_or_create_plugin_post() into get_plugin_post() and create_plugin_post().
See #1584

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

Legend:

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

    r2556 r2561  
    150150    static function get_plugin_icon( $plugin, $output = 'raw' ) {
    151151        global $wporg_plugin_directory;
    152         $plugin = $wporg_plugin_directory->get_or_create_plugin_post( $plugin );
    153         if ( is_wp_error( $plugin ) ) {
     152        $plugin = $wporg_plugin_directory->get_plugin_post( $plugin );
     153        if ( ! $plugin ) {
    154154            return false;
    155155        }
     
    219219    static function get_plugin_banner( $plugin, $output = 'raw' ) {
    220220        global $wporg_plugin_directory;
    221         $plugin = $wporg_plugin_directory->get_or_create_plugin_post( $plugin );
    222         if ( is_wp_error( $plugin ) ) {
     221        $plugin = $wporg_plugin_directory->get_plugin_post( $plugin );
     222        if ( ! $plugin ) {
    223223            return false;
    224224        }
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-wporg-plugin-directory.php

    r2558 r2561  
    278278     */
    279279    public function split_post_content_into_pages( $content ) {
    280         $_pages        = preg_split( "#<!--section=(.+?)-->#", $content, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY );
     280        $_pages = preg_split( "#<!--section=(.+?)-->#", $content, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY );
    281281        $content_pages = array(
    282282            'screenshots' => '[wporg-plugins-screenshots]',
     
    302302     * @return WP_Post|WP_Error
    303303     */
    304     public function get_or_create_plugin_post( $plugin_slug ) {
     304    public function get_plugin_post( $plugin_slug ) {
    305305        if ( $plugin_slug instanceof WP_Post ) {
    306306            return $plugin_slug;
     
    308308
    309309        // get_post_by_slug();
    310         $posts = get_posts( array( 'post_type' => 'plugin', 'name' => $plugin_slug, 'post_status' => 'any', 'fields' => 'ids' ) );
    311 
    312         if ( $posts ) {
    313             $id = reset( $posts );
    314         } else {
    315             $id = wp_insert_post( array(
    316                 'post_type' => 'plugin',
    317                 'post_status' => 'pending',
    318                 'post_name' => $plugin_slug,
    319                 'post_title' => $plugin_slug,
    320                 'post_content' => '',
    321             ), true );
    322 
    323             if ( is_wp_error( $id ) ) {
    324                 return $id;
    325             }
     310        $posts = get_posts( array(
     311            'post_type'   => 'plugin',
     312            'name'        => $plugin_slug,
     313            'post_status' => 'any'
     314        ) );
     315        if ( ! $posts ) {
     316            return false;
     317        }
     318
     319        $plugin = reset( $posts );
     320        return $plugin;
     321    }
     322
     323    /**
     324     * Create a new post entry for a given plugin slug.
     325     *
     326     * @param array $plugin_info {
     327     *     Array of initial plugin post data, all fields are optional.
     328     *
     329     *     @type string $title       The title of the plugin.
     330     *     @type string $slug        The slug of the plugin.
     331     *     @type string $status      The status of the plugin ( 'publish', 'pending', 'disabled', 'closed' ).
     332     *     @type int    $author      The ID of the plugin author.
     333     *     @type string $description The short description of the plugin.
     334     * }
     335     * @return WP_Post|WP_Error
     336     */
     337    public function create_plugin_post( array $plugin_info ) {
     338        $title  = !empty( $plugin_info['title'] )       ? $plugin_info['title']       : '';
     339        $slug   = !empty( $plugin_info['slug'] )        ? $plugin_info['slug']        : sanitize_title( $title );
     340        $status = !empty( $plugin_info['status'] )      ? $plugin_info['status']      : 'pending';
     341        $author = !empty( $plugin_info['author'] )      ? $plugin_info['author']      : 0;
     342        $desc   = !empty( $plugin_info['description'] ) ? $plugin_info['description'] : '';
     343
     344        $id = wp_insert_post( array(
     345            'post_type'    => 'plugin',
     346            'post_status'  => $status,
     347            'post_name'    => $slug,
     348            'post_title'   => $title ?: $slug,
     349            'post_author'  => $author,
     350            'post_content' => '',
     351            'post_excerpt' => $desc,
     352        ), true );
     353
     354        if ( is_wp_error( $id ) ) {
     355            return $id;
    326356        }
    327357
Note: See TracChangeset for help on using the changeset viewer.