Making WordPress.org


Ignore:
Timestamp:
02/25/2016 10:49:24 PM (10 years ago)
Author:
obenland
Message:

Plugins Directory: Formatting update.

  • Updates phpdocs throughout the plugin.
  • Breaks out classes into their own files.
  • Some minor namespace-related tweaks.

See #1584.

File:
1 edited

Legend:

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

    r2611 r2621  
    55 * The main Plugin Directory class, it handles most of the bootstrap and basic operations of the plugin.
    66 *
    7  * @package WordPressdotorg_Plugin_Directory
     7 * @package WordPressdotorg\Plugin_Directory
    88 */
    99class Plugin_Directory {
     
    1414    public static function instance( $plugin_file = null ) {
    1515        static $instance = null;
     16
    1617        return ! is_null( $instance ) ? $instance : $instance = new Plugin_Directory( $plugin_file );
    1718    }
    1819
     20    /**
     21     * @param string $plugin_file
     22     */
    1923    private function __construct( $plugin_file ) {
    2024        add_action( 'init', array( $this, 'init' ) );
     
    2529        add_filter( 'the_content', array( $this, 'filter_post_content_to_correct_page' ), 1 );
    2630
    27         // Load all Admin-specific items
    28         add_action( 'admin_init', array( __NAMESPACE__ . '\\Admin\\Admin_Customizations', 'instance' ) );
     31        // Load all Admin-specific items.
     32        add_action( 'admin_init', array( __NAMESPACE__ . '\\Admin\\Customizations', 'instance' ) );
    2933
    3034        register_activation_hook( $plugin_file, array( $this, 'activate' ) );
     
    9599        // When this plugin is used in the context of a Rosetta site, handle it gracefully
    96100        if ( 'wordpress.org' != $_SERVER['HTTP_HOST'] && defined( 'WPORG_PLUGIN_DIRECTORY_BLOGID' ) ) {
    97             $this->add_rosetta_network_filters();   
     101            add_filter( 'option_home',    array( $this, 'rosetta_network_localize_url' ) );
     102            add_filter( 'option_siteurl', array( $this, 'rosetta_network_localize_url' ) );
    98103        }
    99104    }
     
    109114
    110115    /**
    111      * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
     116     * @global \WP_Rewrite $wp_rewrite WordPress rewrite component.
    112117     */
    113118    public function activate() {
     
    151156
    152157    /**
     158     * Filter the URLs to use the current localized domain name, rather than WordPress.org.
     159     *
    153160     * The Plugin Directory is available at multiple URLs (internationalised domains), this method allows
    154161     * for the one blog (a single blog_id) to be presented at multiple URLs yet have correct localised links.
    155      */
    156     public function add_rosetta_network_filters() {
    157         // Filter the URLs to use the current localised domain name, rather than WordPress.org.
    158         foreach ( array( 'option_home', 'option_siteurl' ) as $filter ) {
    159             add_filter( $filter, function( $url ) {
    160                 static $localized_url = null;
    161                 if ( is_null( $localized_url ) ) {
    162                     $localized_url = 'https://' . preg_replace( '![^a-z.-]+!', '', $_SERVER['HTTP_HOST'] );
    163                 }
    164 
    165                 return preg_replace( '!^[https]+://wordpress\.org!i', $localized_url, $url );
    166             } );
    167         }
    168 
    169         /*
    170         // This method works in conjuction with a filter in sunrise.php, duplicated here for transparency:
    171 
    172         // Make the Plugin Directory available at /plugins/ on all rosetta sites.
    173         function wporg_plugins_on_rosetta_domains( $site, $domain, $path, $segments ) {
    174             // All non-rosetta networks define DOMAIN_CURRENT_SITE in wp-config.php
    175             if ( ! defined( 'DOMAIN_CURRENT_SITE' ) && 'wordpress.org' != $domain && '/plugins/' == substr( $path . '/', 0, 9 ) ) {
    176                 $site = get_blog_details( WPORG_PLUGIN_DIRECTORY_BLOGID );
    177                 if ( $site ) {
    178                     $site = clone $site;
    179                     // 6 = The Rosetta network, this causes the site to be loaded as part of the Rosetta network
    180                     $site->site_id = 6;
    181                     return $site;
    182                 }
    183             }
    184        
    185             return $site;
    186         }
    187         add_filter( 'pre_get_site_by_path', 'wporg_plugins_on_rosetta_domains', 10, 4 );
    188         */
     162     *
     163     * This method works in conjunction with a filter in sunrise.php, duplicated here for transparency:
     164     *
     165     * // Make the Plugin Directory available at /plugins/ on all rosetta sites.
     166     * function wporg_plugins_on_rosetta_domains( $site, $domain, $path, $segments ) {
     167     *     // All non-rosetta networks define DOMAIN_CURRENT_SITE in wp-config.php
     168     *     if ( ! defined( 'DOMAIN_CURRENT_SITE' ) && 'wordpress.org' != $domain && '/plugins/' == substr( $path . '/', 0, 9 ) ) {
     169     *          $site = get_blog_details( WPORG_PLUGIN_DIRECTORY_BLOGID );
     170     *          if ( $site ) {
     171     *              $site = clone $site;
     172     *              // 6 = The Rosetta network, this causes the site to be loaded as part of the Rosetta network
     173     *              $site->site_id = 6;
     174     *              return $site;
     175     *          }
     176     *     }
     177     *
     178     *     return $site;
     179     * }
     180     * add_filter( 'pre_get_site_by_path', 'wporg_plugins_on_rosetta_domains', 10, 4 );
     181     *
     182     * @param string $url The URL to be localized.
     183     * @return string
     184     */
     185    public function rosetta_network_localize_url( $url ) {
     186        static $localized_url = null;
     187
     188        if ( is_null( $localized_url ) ) {
     189            $localized_url = 'https://' . preg_replace( '![^a-z.-]+!', '', $_SERVER['HTTP_HOST'] );
     190        }
     191
     192        return preg_replace( '!^[https]+://wordpress\.org!i', $localized_url, $url );
    189193    }
    190194
     
    192196     * Filter the permalink for the Plugins to be /plugin-name/.
    193197     *
    194      * @param string  $link The generated permalink.
    195      * @param WP_Post $post The Plugin post object.
     198     * @param string   $link The generated permalink.
     199     * @param \WP_Post $post The Plugin post object.
    196200     * @return string
    197201     */
     
    208212     *
    209213     * @param string $term The term to add or update.
    210      * @return string|WP_Error The term to add or update or WP_Error on failure.
     214     * @return string|\WP_Error The term to add or update or WP_Error on failure.
    211215     */
    212216    public function pre_insert_term_prevent( $term ) {
    213217        if ( ! is_super_admin() ) {
    214             $term = new WP_Error( 'not-allowed', __( 'You are not allowed to add terms.', 'wporg-plugins' ) );
     218            $term = new \WP_Error( 'not-allowed', __( 'You are not allowed to add terms.', 'wporg-plugins' ) );
    215219        }
    216220
     
    219223
    220224    /**
    221      * @param WP_Query $wp_query The WordPress Query object.
     225     * @param \WP_Query $wp_query The WordPress Query object.
    222226     */
    223227    public function use_plugins_in_query( $wp_query ) {
     
    299303     */
    300304    public function split_post_content_into_pages( $content ) {
    301         $_pages = preg_split( "#<!--section=(.+?)-->#", $content, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY );
     305        $_pages        = preg_split( "#<!--section=(.+?)-->#", $content, - 1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY );
    302306        $content_pages = array(
    303307            'screenshots' => '[wporg-plugins-screenshots]',
     
    318322
    319323    /**
    320      * Retrieve the WP_Post object representing a given plugin.
    321      *
    322      * @param $plugin_slug string|WP_Post The slug of the plugin to retrieve.
    323      * @return WP_Post|WP_Error
    324      */
     324     * Retrieve the WP_Post object representing a given plugin.
     325     *
     326     * @param $plugin_slug string|\WP_Post The slug of the plugin to retrieve.
     327     * @return \WP_Post|\WP_Error
     328     */
    325329    public function get_plugin_post( $plugin_slug ) {
    326330        if ( $plugin_slug instanceof \WP_Post ) {
     
    332336            'post_type'   => 'plugin',
    333337            'name'        => $plugin_slug,
    334             'post_status' => 'any'
     338            'post_status' => 'any',
    335339        ) );
    336340        if ( ! $posts ) {
     
    338342        }
    339343
    340         $plugin = reset( $posts );
    341         return $plugin;
    342     }
    343 
    344     /**
    345      * Create a new post entry for a given plugin slug.
    346      *
     344        return reset( $posts );
     345    }
     346
     347    /**
     348     * Create a new post entry for a given plugin slug.
     349     *
    347350     * @param array $plugin_info {
    348351     *     Array of initial plugin post data, all fields are optional.
    349      *
     352     *
    350353     *     @type string $title       The title of the plugin.
    351354     *     @type string $slug        The slug of the plugin.
     
    354357     *     @type string $description The short description of the plugin.
    355358     * }
    356      * @return WP_Post|WP_Error
    357      */
     359     * @return \WP_Post|\WP_Error
     360     */
    358361    public function create_plugin_post( array $plugin_info ) {
    359362        $title  = !empty( $plugin_info['title'] )       ? $plugin_info['title']       : '';
     
    377380        }
    378381
    379         $plugin = get_post( $id );
    380         return $plugin;
     382        return get_post( $id );
    381383    }
    382384}
Note: See TracChangeset for help on using the changeset viewer.