Making WordPress.org


Ignore:
Timestamp:
06/20/2016 05:34:18 PM (10 years ago)
Author:
obenland
Message:

Plugin Directory: Documentation and formatting updates.

See #1584.

File:
1 edited

Legend:

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

    r3506 r3511  
    1212
    1313        /**
    14          * Local cache for translated content injected into meta
     14         * Local cache for translated content injected into meta.
     15         *
     16         * @access private
     17         *
     18         * @var array
    1519         */
    1620        private $i18n_meta = array();
    … …  
    1822        /**
    1923         * Fetch the instance of the Plugin_Directory class.
     24         *
     25         * @static
    2026         */
    2127        public static function instance() {
    … …  
    2531        }
    2632
     33        /**
     34         * Plugin_Directory constructor.
     35         *
     36         * @access private
     37         */
    2738        private function __construct() {
    2839                add_action( 'init', array( $this, 'init' ) );
    … …  
    4455                add_filter( 'oembed_providers', array( $this, 'oembed_whitelist' ) );
    4556
    46                 // Shim in postmeta support for data which doesn't yet live in postmeta
     57                // Shim in postmeta support for data which doesn't yet live in postmeta.
    4758                add_filter( 'get_post_metadata', array( $this, 'filter_shim_postmeta' ), 10, 3 );
    4859
    4960                add_filter( 'map_meta_cap', array( __NAMESPACE__ . '\Capabilities', 'map_meta_cap' ), 10, 4 );
    5061
    51                 // Load the API routes
     62                // Load the API routes.
    5263                add_action( 'rest_api_init', array( __NAMESPACE__ . '\API\Base', 'load_routes' ) );
    5364
    54                 // Load all Admin-specific items.
    55                 // Cannot be included on `admin_init` to allow access to menu hooks
     65                /*
     66                 * Load all Admin-specific items.
     67                 * Cannot be included on `admin_init` to allow access to menu hooks.
     68                 */
    5669                if ( defined( 'WP_ADMIN' ) && WP_ADMIN ) {
    5770                        Customizations::instance();
    5871
    59                         add_action( 'wp_insert_post_data', array( __NAMESPACE__ . '\Admin\Status_Transitions', 'can_change_post_status' ), 10, 2 );
     72                        add_action( 'wp_insert_post_data',    array( __NAMESPACE__ . '\Admin\Status_Transitions', 'can_change_post_status' ), 10, 2 );
    6073                        add_action( 'transition_post_status', array( __NAMESPACE__ . '\Admin\Status_Transitions', 'instance' ) );
    6174                }
    … …  
    7487
    7588                register_post_type( 'plugin', array(
    76                         'labels'          => array(
     89                        'labels'       => array(
    7790                                'name'               => __( 'Plugins',                   'wporg-plugins' ),
    7891                                'singular_name'      => __( 'Plugin',                    'wporg-plugins' ),
    … …  
    105118                                'delete_posts'       => 'do_not_allow',
    106119                                'create_posts'       => 'do_not_allow',
    107                         )
     120                        ),
    108121                ) );
    109122
    … …  
    148161                        'capabilities'      => array(
    149162                                'assign_terms' => 'plugin_set_category',
    150                         )
     163                        ),
    151164                ) );
    152165
    … …  
    156169                        'rewrite'           => false,
    157170                        'labels'            => array(
    158                                 'name'          => __( 'Built For', 'wporg-plugins' ),
     171                                'name' => __( 'Built For', 'wporg-plugins' ),
    159172                        ),
    160173                        'public'            => true,
    … …  
    164177                        'capabilities'      => array(
    165178                                'assign_terms' => 'plugin_set_category',
    166                         )
     179                        ),
    167180                ) );
    168181
    … …  
    172185                        'rewrite'           => false,
    173186                        'labels'            => array(
    174                                 'name'          => __( 'Business Model', 'wporg-plugins' ),
     187                                'name' => __( 'Business Model', 'wporg-plugins' ),
    175188                        ),
    176189                        'public'            => true,
    … …  
    180193                        'capabilities'      => array(
    181194                                'assign_terms' => 'plugin_set_category',
    182                         )
     195                        ),
    183196                ) );
    184197
    … …  
    220233                // /browse/ should be the popular archive view.
    221234                add_rewrite_rule( '^browse$', 'index.php?browse=popular', 'top' );
     235
    222236                // Create an archive for a users favorites too.
    223237                add_rewrite_rule( '^browse/favorites/([^/]+)$', 'index.php?browse=favorites&favorites_user=$matches[1]', 'top' );
    224238
    225                 // Handle the old plugin tabs URLs
     239                // Handle the old plugin tabs URLs.
    226240                add_rewrite_rule( '^([^/]+)/(installation|faq|screenshots|changelog|stats|developers|other_notes)$', 'index.php?redirect_plugin_tab=$matches[1]/#$matches[2]', 'top' );
    227241
    … …  
    229243                //Capabilities::add_roles();
    230244
    231                 // When this plugin is used in the context of a Rosetta site, handle it gracefully
     245                // When this plugin is used in the context of a Rosetta site, handle it gracefully.
    232246                if ( 'wordpress.org' != $_SERVER['HTTP_HOST'] && defined( 'WPORG_PLUGIN_DIRECTORY_BLOGID' ) ) {
    233247                        add_filter( 'option_home',    array( $this, 'rosetta_network_localize_url' ) );
    … …  
    236250
    237251                if ( 'en_US' != get_locale() ) {
    238                         add_filter( 'get_term', array( __NAMESPACE__ . '\i18n', 'translate_term' ) );
     252                        add_filter( 'get_term', array( __NAMESPACE__ . '\I18n', 'translate_term' ) );
    239253                        add_filter( 'the_content', array( $this, 'translate_post_content' ), 1, 2 );
    240254                        add_filter( 'the_title', array( $this, 'translate_post_title' ), 1, 2 );
    … …  
    250264                // When Jetpack syncs, we want to add filters to inject additional metadata for Jetpack, so it syncs for ElasticSearch indexing.
    251265                add_action( 'shutdown', array( $this, 'append_meta_for_jetpack' ), 8 );
    252 
    253266        }
    254267
    … …  
    263276        }
    264277
     278        /**
     279         *  Register the Widgets used plugin detail pages.
     280         */
    265281        public function register_widgets() {
    266                 register_widget( __NAMESPACE__ . '\Widgets\Donate' );
    267                 register_widget( __NAMESPACE__ . '\Widgets\Meta' );
     282                register_widget( __NAMESPACE__ . '\Widgets\Donate'  );
     283                register_widget( __NAMESPACE__ . '\Widgets\Meta'    );
    268284                register_widget( __NAMESPACE__ . '\Widgets\Ratings' );
    269285                register_widget( __NAMESPACE__ . '\Widgets\Support' );
    … …  
    391407                }
    392408                if ( 'plugin_built_for' == $term->taxonomy ) {
     409
    393410                        // Term slug = Post Slug = /%postname%/
    394411                        return trailingslashit( home_url( $term->slug ) );
    … …  
    429446                }
    430447
    431                 switch ( $wp_query->query_vars['browse'] ) {
     448                switch ( $wp_query->get( 'browse' ) ) {
    432449                        case 'favorites':
    433450                                $favorites_user = wp_get_current_user();
    434                                 if ( !empty( $wp_query->query_vars['favorites_user'] ) ) {
     451                                if ( ! empty( $wp_query->query_vars['favorites_user'] ) ) {
    435452                                        $favorites_user = $wp_query->query_vars['favorites_user'];
    436                                 } elseif ( !empty( $_GET['favorites_user'] ) ) {
     453                                } elseif ( ! empty( $_GET['favorites_user'] ) ) {
    437454                                        $favorites_user = $_GET['favorites_user'];
    438455                                }
    … …  
    450467                                        $wp_query->query_vars['p'] = -1;
    451468                                }
    452 
    453469                                break;
    454470
    455471                        case 'new':
    456                                 $wp_query->query_vars['orderby']  = 'post_modified';
     472                                $wp_query->query_vars['orderby'] = 'post_modified';
    457473                                break;
    458474                }
    459475
    460                 if ( isset( $wp_query->query['browse'] ) ) {
    461                         if ( 'beta' != $wp_query->query['browse'] && 'featured' != $wp_query->query['browse'] ) {
    462                                 unset( $wp_query->query_vars['browse'] );
    463 
    464                                 add_filter( 'the_posts', function( $posts, $wp_query ) {
    465                                         // Fix the queried object for the archive view
    466                                         if ( ! $wp_query->queried_object && isset( $wp_query->query['browse'] ) ) {
    467                                                 $wp_query->query_vars['browse'] = $wp_query->query['browse'];
    468                                                 $wp_query->queried_object = get_term_by( 'slug', $wp_query->query['browse'], 'plugin_section' );
    469                                         }
    470                                         return $posts;
    471                                 }, 10, 2 );
    472 
    473                         }
     476                if ( isset( $wp_query->query['browse'] ) && 'beta' != $wp_query->query['browse'] && 'featured' != $wp_query->query['browse'] ) {
     477                        unset( $wp_query->query_vars['browse'] );
     478
     479                        add_filter( 'the_posts', function( $posts, $wp_query ) {
     480
     481                                // Fix the queried object for the archive view.
     482                                if ( ! $wp_query->queried_object && isset( $wp_query->query['browse'] ) ) {
     483                                        $wp_query->query_vars['browse'] = $wp_query->query['browse'];
     484                                        $wp_query->queried_object       = get_term_by( 'slug', $wp_query->query['browse'], 'plugin_section' );
     485                                }
     486
     487                                return $posts;
     488                        }, 10, 2 );
    474489                }
    475490
    … …  
    485500         * @param string $content Post content.
    486501         * @param string $section Optional. Which readme section to translate.
     502         * @param int    $post_id Optional. Post ID. Default: 0.
    487503         * @return string
    488504         */
    489         public function translate_post_content( $content, $section = null, $post_id = null ) {
     505        public function translate_post_content( $content, $section = null, $post_id = 0 ) {
    490506                if ( is_null( $section ) ) {
    491507                        return $content;
    … …  
    514530         * Returns the requested page's excerpt, translated.
    515531         *
    516          * @param string $excerpt
    517          * @param \WP_Post $post
     532         * @param string       $excerpt
     533         * @param int|\WP_Post $post
    518534         * @return string
    519535         */
    … …  
    523539
    524540        /**
    525          * Shutdown action that will add a filter to inject additional postmeta containing translated content if Jetpack is syncing.
    526          *
     541         * Shutdown action that will add a filter to inject additional postmeta containing translated content if Jetpack
     542         * is syncing.
    527543         */
    528544        public function append_meta_for_jetpack() {
    529                 // Guess if a Jetpack sync is scheduled to run. It runs during shutdown at a lower priority than this action, so we can get in first.
    530                 // Fetching the extra meta to inject is expensive, so we only want to do this if a sync is likely.
    531                 if ( class_exists( 'Jetpack' ) && !empty(\Jetpack::$instance->sync->sync) ) {
     545
     546                /*
     547                 * Guess if a Jetpack sync is scheduled to run. It runs during shutdown at a lower priority than this action,
     548                 * so we can get in first.Fetching the extra meta to inject is expensive, so we only want to do this if a sync
     549                 * is likely.
     550                 */
     551                if ( class_exists( 'Jetpack' ) && ! empty( \Jetpack::$instance->sync->sync ) ) {
    532552                        add_filter( 'wporg_plugins_custom_meta_fields', array( $this, 'filter_post_meta_i18n' ), 10, 2 );
    533553                }
    534 
    535554        }
    536555
    … …  
    538557         * Filter for wporg_plugins_custom_meta_fields to inject translated content for ES.
    539558         *
     559         * @global string $locale Current locale.
     560         *
    540561         * @param array $meta
    541          * @param int $post_id
     562         * @param int   $post_id
    542563         * @return array
    543564         */
    544565        public function filter_post_meta_i18n( $meta, $post_id ) {
    545                 // Prevent recursion and repeat runs
     566
     567                // Prevent recursion and repeat runs.
    546568                remove_filter( 'wporg_plugins_custom_meta_fields', array( $this, 'filter_post_meta_i18n' ) );
    547569
    548570                if ( $post_id <= 200 ) {
    549                         $locales_to_sync = array( 'fr_FR', 'es_ES' ); // This should probably be a list of available translations for the plugin readme.
     571
     572                        // This should probably be a list of available translations for the plugin readme.
     573                        $locales_to_sync = array(
     574                                'fr_FR',
     575                                'es_ES',
     576                        );
    550577
    551578                        global $locale;
    552579                        $_locale = $locale;
     580
    553581                        foreach ( $locales_to_sync as $locale ) {
    554                                 $this->i18n_meta[$post_id]['title_'.$locale] = $this->translate_post_title( get_the_title( $post_id ), $post_id );
    555                                 $this->i18n_meta[$post_id]['excerpt_'.$locale] = $this->translate_post_excerpt( get_the_excerpt( $post_id ), $post_id );
    556 
    557                                 // Split up the content to translate it in sections
    558                                 $content = '';
     582                                $this->i18n_meta[ $post_id ][ 'title_' . $locale ]  = $this->translate_post_title( get_the_title( $post_id ), $post_id );
     583                                $this->i18n_meta[ $post_id ][ 'excerpt_' . $locale ] = $this->translate_post_excerpt( get_the_excerpt( $post_id ), $post_id );
     584
     585                                // Split up the content to translate it in sections.
     586                                $content  = '';
    559587                                $sections = $this->split_post_content_into_pages( get_the_content( $post_id ) );
    560588                                foreach ( $sections as $section => $section_content ) {
    561589                                        $content .= $this->translate_post_content( $section_content, $section, $post_id );
    562590                                }
    563                                 $this->i18n_meta[$post_id]['content_'.$locale] = $content;
    564 
     591                                $this->i18n_meta[ $post_id ][ 'content_' . $locale ] = $content;
    565592                        }
    566593
    567594                        $locale = $_locale;
    568595
    569                         $meta = array_merge( $meta, array_keys( $this->i18n_meta[$post_id] ) );
    570                 }
    571 
    572                 add_filter( 'wporg_plugins_custom_meta_fields', array( $this, 'filter_post_meta_i18n'), 10, 2 );
     596                        $meta = array_merge( $meta, array_keys( $this->i18n_meta[ $post_id ] ) );
     597                }
     598
     599                add_filter( 'wporg_plugins_custom_meta_fields', array( $this, 'filter_post_meta_i18n' ), 10, 2 );
     600
    573601                return $meta;
    574602        }
    575 
    576603
    577604        /**
    … …  
    583610        public function filter_allowed_post_types( $allowed_post_types ) {
    584611                $allowed_post_types[] = 'plugin';
     612
    585613                return $allowed_post_types;
    586614        }
    … …  
    588616        /**
    589617         * Filters the available public query vars to add our custom parameters.
     618         *
     619         * @param array $vars Public query vars.
     620         * @return array
    590621         */
    591622        public function filter_query_vars( $vars ) {
    592623                $vars[] = 'favorites_user';
    593624                $vars[] = 'redirect_plugin_tab';
     625
    594626                return $vars;
    595627        }
    … …  
    602634         */
    603635        public function filter_jetpack_options( $new_value ) {
    604                 if ( is_array( $new_value ) && array_key_exists( 'public', $new_value ) )
     636                if ( is_array( $new_value ) && array_key_exists( 'public', $new_value ) ) {
    605637                        $new_value['public'] = 1;
     638                }
    606639
    607640                return $new_value;
    … …  
    621654         */
    622655        function redirect_old_plugin_urls() {
    623                 // Handle a redirect for /$plugin/$tab_name/ to /$plugin/#$tab_name
     656
     657                // Handle a redirect for /$plugin/$tab_name/ to /$plugin/#$tab_name.
    624658                if ( get_query_var( 'redirect_plugin_tab' ) ) {
    625659                        wp_safe_redirect( site_url( get_query_var( 'redirect_plugin_tab' ) ) );
    … …  
    630664                if ( is_404() ) {
    631665
    632                         // [1] => plugins [2] => example-plugin-name [2..] => random()
     666                        // [1] => plugins [2] => example-plugin-name [2..] => random().
    633667                        $path = explode( '/', $_SERVER['REQUEST_URI'] );
    634668
    … …  
    646680                        }
    647681
    648                         // The about page is now over at /developers/
     682                        // The about page is now over at /developers/.
    649683                        if ( 'about' === $path[2] ) {
    650                                 wp_safe_redirect( home_url('/developers/' . ( ( isset( $path[3] ) && 'add' == $path[3] ) ? 'add/' : '' ) ) );
     684                                wp_safe_redirect( home_url( '/developers/' . ( ( isset( $path[3] ) && 'add' == $path[3] ) ? 'add/' : '' ) ) );
    651685                                die();
    652686                        }
    653687
    654                         // Otherwise, handle a plugin redirect
     688                        // Otherwise, handle a plugin redirect.
    655689                        if ( $plugin = self::get_plugin_post( $path[2] ) ) {
    656690                                $is_disabled = in_array( $plugin->post_status, array( 'disabled', 'closed' ), true );
    657691
    658692                                if ( $is_disabled && current_user_can( 'edit_post', $plugin ) ) {
    659                                         wp_safe_redirect( add_query_arg( array( 'post' => $plugin->ID, 'action' => 'edit' ), admin_url( 'post.php' ) ) );
     693                                        wp_safe_redirect( add_query_arg( array(
     694                                                'post'   => $plugin->ID,
     695                                                'action' => 'edit',
     696                                        ), admin_url( 'post.php' ) ) );
    660697                                        die();
    661698                                } else if ( ! $is_disabled ) {
    … …  
    672709                }
    673710
    674                 // new-style Search links
     711                // new-style Search links.
    675712                if ( get_query_var( 's' ) && isset( $_GET['s'] ) ) {
    676713                        wp_safe_redirect( site_url( '/search/' . urlencode( get_query_var( 's' ) ) . '/' ) );
    … …  
    706743                                }
    707744                        }
     745
    708746                        return false;
    709747                } );
    … …  
    722760                switch ( $meta_key ) {
    723761                        case 'downloads':
    724                                 $post = get_post( $object_id );
     762                                $post  = get_post( $object_id );
    725763                                $count = Template::get_downloads_count( $post );
    726 
    727                                 return array( $count );
     764                                $value = array( $count );
    728765                                break;
     766
    729767                        case 'rating':
    730768                                $post = get_post( $object_id );
    … …  
    733771                                        break;
    734772                                }
     773
    735774                                $rating = wporg_get_rating_avg( 'plugin', $post->post_name );
    736 
    737                                 return array( $rating );
     775                                $value  = array( $rating );
    738776                                break;
     777
    739778                        case 'ratings':
    740779                                $post = get_post( $object_id );
    … …  
    742781                                        break;
    743782                                }
     783
    744784                                $ratings = wporg_get_rating_counts( 'plugin', $post->post_name );
    745 
    746                                 return array( $ratings );
     785                                $value   = array( $ratings );
    747786                                break;
     787
    748788                        case false:
     789
    749790                                // In the event $meta_key is false, the caller wants all meta fields, so we'll append our custom ones here too.
    750791                                remove_filter( 'get_post_metadata', array( $this, 'filter_shim_postmeta' ) );
    … …  
    760801
    761802                                foreach ( $custom_meta_fields as $key ) {
    762                                         // When WordPress calls `get_post_meta( $post_id, false )` it expects an array of maybe_serialize()'d data
     803
     804                                        // When WordPress calls `get_post_meta( $post_id, false )` it expects an array of maybe_serialize()'d data.
    763805                                        $shimed_data = $this->filter_shim_postmeta( false, $object_id, $key );
    764806                                        if ( $shimed_data ) {
    … …  
    766808                                        }
    767809                                }
    768 
    769810                                break;
     811
    770812                        default:
    771813                                if ( isset( $this->i18n_meta[ $object_id ][ $meta_key ] ) ) {
    … …  
    774816                                break;
    775817                }
     818
    776819                return $value;
    777820        }
    … …  
    823866         * Retrieve the WP_Post object representing a given plugin.
    824867         *
    825          * @param $plugin_slug string|\WP_Post The slug of the plugin to retrieve.
     868         * @static
     869         * @global \WP_Post $post WordPress post object.
     870         *
     871         * @param string|\WP_Post $plugin_slug The slug of the plugin to retrieve.
    826872         * @return \WP_Post|bool
    827873         */
    828         static public function get_plugin_post( $plugin_slug ) {
     874        public static function get_plugin_post( $plugin_slug ) {
    829875                global $post;
     876
    830877                if ( $plugin_slug instanceof \WP_Post ) {
    831878                        return $plugin_slug;
    832879                }
     880
    833881                // Use the global $post object when it matches to avoid hitting the database.
    834                 if ( !empty( $post ) && 'plugin' == $post->post_type && $plugin_slug == $post->post_name ) {
     882                if ( ! empty( $post ) && 'plugin' == $post->post_type && $plugin_slug == $post->post_name ) {
    835883                        return $post;
    836884                }
    … …  
    841889                        // We have a $post.
    842890                } else {
     891
    843892                        // get_post_by_slug();
    844893                        $posts = get_posts( array(
    … …  
    847896                                'post_status' => array( 'publish', 'pending', 'disabled', 'closed', 'draft', 'approved' ),
    848897                        ) );
     898
    849899                        if ( ! $posts ) {
    850900                                $post = false;
    … …  
    861911        /**
    862912         * Create a new post entry for a given plugin slug.
     913         *
     914         * @static
    863915         *
    864916         * @param array $args {
Note: See TracChangeset for help on using the changeset viewer.