WordPress.org

Making WordPress.org

Changeset 2611


Ignore:
Timestamp:
02/25/2016 05:03:19 AM (4 years ago)
Author:
dd32
Message:

Plugin Directory: Switch to using namespaces instead of implemeting them through classnames.
This change introduces an autoloader and relies upon it for most file inclusions, this should encourage us to keep a standard naming schema and writing more component classes going forward.
See #1584

Location:
sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory
Files:
3 added
1 copied
7 moved

Legend:

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

    r2562 r2611  
    11<?php
     2namespace WordPressdotorg\Plugin_Directory\Admin\Metabox;
     3use WordPressdotorg\Plugin_Directory\Tools;
    24
    3 class WPorg_Plugin_Directory_Admin_Metabox_Committers {
    4     function __construct() {
    5         add_action( 'add_meta_boxes', array( $this, 'register' ) );
    6     }
    7 
    8     function register() {
    9         add_meta_box( 'plugin-committers', __( 'Plugin Committers', 'wporg-plugins' ), array( $this, 'display' ), 'plugin' );
    10     }
    11 
    12     function display() {
     5/**
     6 * The Plugin Committers admin metabox.
     7 *
     8 * @package WordPressdotorg_Plugin_Directory
     9 */
     10class Committers {
     11    static function display() {
    1312        $plugin_slug = get_post()->post_name;
    14         $existing_committers = WPorg_Plugin_Directory_Tools::get_plugin_committers( $plugin_slug );
    15         $existing_committers = array_map( function( $user ) { return new WP_User( $user ); }, $existing_committers );
     13        $existing_committers = Tools::get_plugin_committers( $plugin_slug );
     14        $existing_committers = array_map( function( $user ) { return new \WP_User( $user ); }, $existing_committers );
    1615
    1716        $output = '';
     
    2625        echo '<ul class="committers">' . $output . '</ul>';
    2726    }
    28 
    2927}
    30 new WPorg_Plugin_Directory_Admin_Metabox_Committers();
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-plugin-directory.php

    r2561 r2611  
    11<?php
     2namespace WordPressdotorg\Plugin_Directory;
     3
    24/**
    3  * @package WPorg_Plugin_Directory
     5 * The main Plugin Directory class, it handles most of the bootstrap and basic operations of the plugin.
     6 *
     7 * @package WordPressdotorg_Plugin_Directory
    48 */
    5 
    6 /**
    7  * Class WPorg_Plugin_Directory
    8  */
    9 class WPorg_Plugin_Directory {
    10 
    11     /**
    12      * Constructor.
    13      */
    14     public function __construct() {
     9class Plugin_Directory {
     10
     11    /**
     12     * Fetch the instance of the Plugin_Directory class.
     13     */
     14    public static function instance( $plugin_file = null ) {
     15        static $instance = null;
     16        return ! is_null( $instance ) ? $instance : $instance = new Plugin_Directory( $plugin_file );
     17    }
     18
     19    private function __construct( $plugin_file ) {
    1520        add_action( 'init', array( $this, 'init' ) );
     21        add_action( 'init', array( $this, 'register_shortcodes' ) );
    1622        add_filter( 'post_type_link', array( $this, 'package_link' ), 10, 2 );
    1723        add_filter( 'pre_insert_term', array( $this, 'pre_insert_term_prevent' ) );
    1824        add_action( 'pre_get_posts', array( $this, 'use_plugins_in_query' ) );
    1925        add_filter( 'the_content', array( $this, 'filter_post_content_to_correct_page' ), 1 );
    20     }
    21 
    22     /**
    23      * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
    24      */
    25     public function activate() {
    26         global $wp_rewrite;
    27 
    28         // Setup the environment.
    29         $this->init();
    30 
    31         // %postname% is required.
    32         $wp_rewrite->set_permalink_structure( '/%postname%/' );
    33 
    34         // /tags/%slug% is required for tags.
    35         $wp_rewrite->set_tag_base( '/tags' );
    36 
    37         // We require the WordPress.org Ratings plugin also be active.
    38         if ( ! is_plugin_active( 'wporg-ratings/wporg-ratings.php' ) ) {
    39             activate_plugin( 'wporg-ratings/wporg-ratings.php' );
    40         }
    41 
    42         // Enable the WordPress.org Plugin Repo Theme.
    43         foreach ( wp_get_themes() as $theme ) {
    44             if ( $theme->get( 'Name' ) === 'WordPress.org Plugins' ) {
    45                 switch_theme( $theme->get_stylesheet() );
    46                 break;
    47             }
    48         }
    49 
    50         flush_rewrite_rules();
    51 
    52         do_action( 'wporg_plugins_activation' );
    53     }
    54 
    55     /**
    56      *
    57      */
    58     public function deactivate() {
    59         flush_rewrite_rules();
    60 
    61         do_action( 'wporg_plugins_deactivation' );
     26
     27        // Load all Admin-specific items
     28        add_action( 'admin_init', array( __NAMESPACE__ . '\\Admin\\Admin_Customizations', 'instance' ) );
     29
     30        register_activation_hook( $plugin_file, array( $this, 'activate' ) );
     31        register_deactivation_hook( $plugin_file, array( $this, 'deactivate' ) );
    6232    }
    6333
     
    130100
    131101    /**
     102     * Register the Shortcodes used within the content.
     103     */
     104    public function register_shortcodes() {
     105        add_shortcode( 'wporg-plugins-screenshots', array( __NAMESPACE__ . '\\Shortcodes\\Screenshots', 'display' ) );
     106    //  add_shortcode( 'wporg-plugins-stats',       array( __NAMESPACE__ . '\\Shortcodes\\Stats',       'display' ) );
     107    //  add_shortcode( 'wporg-plugins-developer',   array( __NAMESPACE__ . '\\Shortcodes\\Developer',   'display' ) );
     108    }
     109
     110    /**
     111     * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
     112     */
     113    public function activate() {
     114        global $wp_rewrite;
     115
     116        // Setup the environment.
     117        $this->init();
     118
     119        // %postname% is required.
     120        $wp_rewrite->set_permalink_structure( '/%postname%/' );
     121
     122        // /tags/%slug% is required for tags.
     123        $wp_rewrite->set_tag_base( '/tags' );
     124
     125        // We require the WordPress.org Ratings plugin also be active.
     126        if ( ! is_plugin_active( 'wporg-ratings/wporg-ratings.php' ) ) {
     127            activate_plugin( 'wporg-ratings/wporg-ratings.php' );
     128        }
     129
     130        // Enable the WordPress.org Plugin Repo Theme.
     131        foreach ( wp_get_themes() as $theme ) {
     132            if ( $theme->get( 'Name' ) === 'WordPress.org Plugins' ) {
     133                switch_theme( $theme->get_stylesheet() );
     134                break;
     135            }
     136        }
     137
     138        flush_rewrite_rules();
     139
     140        do_action( 'wporg_plugins_activation' );
     141    }
     142
     143    /**
     144     *
     145     */
     146    public function deactivate() {
     147        flush_rewrite_rules();
     148
     149        do_action( 'wporg_plugins_deactivation' );
     150    }
     151
     152    /**
    132153     * The Plugin Directory is available at multiple URLs (internationalised domains), this method allows
    133154     * for the one blog (a single blog_id) to be presented at multiple URLs yet have correct localised links.
     
    303324     */
    304325    public function get_plugin_post( $plugin_slug ) {
    305         if ( $plugin_slug instanceof WP_Post ) {
     326        if ( $plugin_slug instanceof \WP_Post ) {
    306327            return $plugin_slug;
    307328        }
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-plugin-geopattern.php

    r2560 r2611  
    11<?php
     2namespace WordPressdotorg\Plugin_Directory;
    23
    34require __DIR__ . '/libs/geopattern-1.1.0/geopattern_loader.php';
    45
    5 class WPorg_Plugin_Geopattern extends \RedeyeVentures\GeoPattern\GeoPattern {
     6/**
     7 * Generates Geopattern icons for Plugins.
     8 *
     9 * @package WordPressdotorg_Plugin_Directory
     10 */
     11class Plugin_Geopattern extends \RedeyeVentures\GeoPattern\GeoPattern {
    612
    713    var $slug; // Hashed to generate pattern
     
    1925
    2026        // Replace the base SVG object with our own, so the dimensions are gettable.
    21         $this->svg = new WPorg_Plugin_Geopattern_SVG();
     27        $this->svg = new Plugin_Geopattern_SVG();
    2228    }
    2329
     
    3844
    3945    public function toSVG( $width = 128, $height = 128 ) {
    40         $this->svg = new WPorg_Plugin_Geopattern_SVG();
     46        $this->svg = new Plugin_Geopattern_SVG();
    4147        $this->generateBackground();
    4248
     
    6975
    7076// The base SVG class doesn't provide functions for getting dimensions, so..
    71 class WPorg_Plugin_Geopattern_SVG extends \RedeyeVentures\GeoPattern\SVG {
     77class Plugin_Geopattern_SVG extends \RedeyeVentures\GeoPattern\SVG {
    7278
    7379    protected $viewbox;
     
    8995
    9096    public function addText( $text, $x, $y, $text_anchor, $style, $args=array() ) {
    91         $element = new WPorg_Plugin_Geopattern_SVGText($text, $x, $y, $text_anchor, $style, $args);
     97        $element = new Plugin_Geopattern_SVGText($text, $x, $y, $text_anchor, $style, $args);
    9298        $this->svgString .= $element;
    9399        return $this;
     
    106112
    107113// Nor does it support text.
    108 class WPorg_Plugin_Geopattern_SVGText extends \RedeyeVentures\GeoPattern\SVGElements\Base {
     114class Plugin_Geopattern_SVGText extends \RedeyeVentures\GeoPattern\SVGElements\Base {
    109115    protected $tag = 'text';
    110116    protected $text;
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-template.php

    r2561 r2611  
    11<?php
    2 /**
    3  * @package WPorg_Plugin_Directory
    4  */
     2namespace WordPressdotorg\Plugin_Directory;
    53
    64/**
    75 * Various helpers to retrieve data not stored within WordPress.
     6 *
     7 * @package WordPressdotorg_Plugin_Directory
    88 */
    9 class WPorg_Plugin_Directory_Template {
     9class Template {
    1010
    1111    /**
     
    149149     */
    150150    static function get_plugin_icon( $plugin, $output = 'raw' ) {
    151         global $wporg_plugin_directory;
    152         $plugin = $wporg_plugin_directory->get_plugin_post( $plugin );
     151        $plugin = Plugin_Directory::instance()->get_plugin_post( $plugin );
    153152        if ( ! $plugin ) {
    154153            return false;
     
    176175        if ( ! $icon ) {
    177176            $generated = true;
    178             if ( ! class_exists( 'WPorg_Plugin_Geopattern' ) ) {
    179                 include __DIR__ . '/class-wporg-plugin-geopattern.php';
    180             }
    181             $icon = new WPorg_Plugin_Geopattern;
     177
     178            $icon = new Plugin_Geopattern;
    182179            $icon->setString( $plugin->post_name );
    183180
     
    218215     */
    219216    static function get_plugin_banner( $plugin, $output = 'raw' ) {
    220         global $wporg_plugin_directory;
    221         $plugin = $wporg_plugin_directory->get_plugin_post( $plugin );
     217        $plugin = Plugin_Directory::instance()->get_plugin_post( $plugin );
    222218        if ( ! $plugin ) {
    223219            return false;
     
    251247
    252248    static function get_asset_url( $plugin, $asset ) {
     249        if ( ! empty( $asset['location'] ) && 'plugin' == $asset['location'] ) {
     250            // Screenshots in the plugin folder - /plugins/plugin-name/screenshot-1.png
     251            $format = 'https://s.w.org/plugins/%s/%s?rev=%s';
     252        } else {
     253            // Images in the assets folder - /plugin-name/assets/screenshot-1.png
     254            $format = 'https://ps.w.org/%s/assets/%s?rev=%s';
     255        }
     256
    253257        return esc_url( sprintf(
    254             'https://ps.w.org/%s/assets/%s?rev=%s',
     258            $format,
    255259            $plugin,
    256260            $asset['filename'],
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-tools.php

    r2562 r2611  
    11<?php
    2 /**
    3  * @package WPorg_Plugin_Directory
    4  */
     2namespace WordPressdotorg\Plugin_Directory;
    53
    64/**
    75 * Various functions used by other processes, will make sense to move to specific classes.
     6 *
     7 * @package WordPressdotorg_Plugin_Directory
    88 */
    9 class WPorg_Plugin_Directory_Tools {
     9class Tools {
    1010
    1111    /**
     
    1919        include_once __DIR__ . '/readme-parser/compat.php';
    2020
    21         $data = (object) WPorg_Readme::parse_readme( $readme );
     21        $data = (object) \WPorg_Readme::parse_readme( $readme );
    2222
    2323        unset( $data->sections['screenshots'] ); // Useless.
     
    5353        }
    5454
    55         $tonesque = new Tonesque( $file_location );
     55        $tonesque = new \Tonesque( $file_location );
    5656        return $tonesque->color();
    5757    }
     
    7676     */
    7777    public static function grant_plugin_committer( $plugin_slug, $user ) {
    78         global $wpdb, $wporg_plugin_directory;
    79         if ( ! $user instanceof WP_User ) {
    80             $user = new WP_User( $user );
     78        global $wpdb;
     79        if ( ! $user instanceof \WP_User ) {
     80            $user = new \WP_User( $user );
    8181        }
    8282
    83         if ( ! $user->exists() || ! $wporg_plugin_directory->get_plugin_post( $plugin_slug ) ) {
     83        if ( ! $user->exists() || ! Plugin_Directory::instance()->get_plugin_post( $plugin_slug ) ) {
    8484            return false;
    8585        }
     
    108108     */
    109109    public static function revoke_plugin_committer( $plugin_slug, $user ) {
    110         global $wpdb, $wporg_plugin_directory;
    111         if ( ! $user instanceof WP_User ) {
    112             $user = new WP_User( $user );
     110        global $wpdb;
     111        if ( ! $user instanceof \WP_User ) {
     112            $user = new \WP_User( $user );
    113113        }
    114114
    115         if ( ! $user->exists() || ! $wporg_plugin_directory->get_plugin_post( $plugin_slug ) ) {
     115        if ( ! $user->exists() || ! Plugin_Directory::instance()->get_plugin_post( $plugin_slug ) ) {
    116116            return false;
    117117        }
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/plugin-directory.php

    r2602 r2611  
    11<?php
     2namespace WordPressdotorg\Plugin_Directory;
    23/**
    34 * Plugin Name: Plugin Directory
     
    1112 * License URI: http://opensource.org/licenses/gpl-2.0.php
    1213 *
    13  * @package WPorg_Plugin_Directory
     14 * @package WordPressdotorg_Plugin_Directory
    1415 */
    1516
    16 include __DIR__ . '/class-wporg-plugin-directory.php';
    17 include __DIR__ . '/class-wporg-plugin-directory-template.php';
    18 include __DIR__ . '/class-wporg-plugin-directory-tools.php';
     17// Register an Autoloader for all files
     18include __DIR__ . '/class-autoloader.php';
     19Autoloader\register_class_path( __NAMESPACE__, __DIR__ );
    1920
    20 include __DIR__ . '/shortcodes/screenshots.php';
    21 
    22 if ( is_admin() ) {
    23     include __DIR__ . '/admin/class-wporg-plugin-directory-admin-metabox-committers.php';
    24 }
    25 
    26 $wporg_plugin_directory = new WPorg_Plugin_Directory();
    27 register_activation_hook( __FILE__, array( $wporg_plugin_directory, 'activate' ) );
    28 register_deactivation_hook( __FILE__, array( $wporg_plugin_directory, 'deactivate' ) );
     21// Create the instance of the plugin, passing __FILE__ for actions that need it.
     22include __DIR__ . '/class-plugin-directory.php';
     23Plugin_Directory::instance( __FILE__ );
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/shortcodes/class-screenshots.php

    r2560 r2611  
    11<?php
    2 add_shortcode( 'wporg-plugins-screenshots', 'wporg_plugins_screenshots' );
     2namespace WordPressdotorg\Plugin_Directory\Shortcodes;
     3use WordPressdotorg\Plugin_Directory\Template;
    34
    4 function wporg_plugins_screenshots() {
    5     $plugin = get_post();
     5/**
     6 * The [wporg-plugins-screenshots] shortcode handler to display a plugins screenshots.
     7 *
     8 * @package WordPressdotorg_Plugin_Directory
     9 */
     10class Screenshots {
     11    static function display() {
     12        $plugin = get_post();
    613
    7     // All indexed from 1
    8     $screenshot_descriptions = get_post_meta( $plugin->ID, 'screenshots', true );
    9     $assets_screenshots = get_post_meta( $plugin->ID, 'assets_screenshots', true );
     14        // All indexed from 1
     15        $screenshot_descriptions = get_post_meta( $plugin->ID, 'screenshots', true );
     16        $assets_screenshots = get_post_meta( $plugin->ID, 'assets_screenshots', true );
    1017
    11     $output = '';
    12     foreach ( $screenshot_descriptions as $index => $description ) {
    13         // Find the image that corresponds with the text.
    14         // The image numbers are stored within the 'resolution' key.
    15         $found = false;
    16         foreach ( $assets_screenshots as $image ) {
    17             if ( $index == $image['resolution'] ) {
    18                 $found = true;
    19                 break;
     18        $output = '';
     19        foreach ( $screenshot_descriptions as $index => $description ) {
     20            // Find the image that corresponds with the text.
     21            // The image numbers are stored within the 'resolution' key.
     22            $found = false;
     23            foreach ( $assets_screenshots as $image ) {
     24                if ( $index == $image['resolution'] ) {
     25                    $found = true;
     26                    break;
     27                }
    2028            }
    21         }
    22         if ( ! $found ) {
    23             continue;
    24         }
     29            if ( ! $found ) {
     30                continue;
     31            }
    2532
    26         if ( ! empty( $image['location'] ) && 'plugin' == $image['location'] ) {
    27             // Screenshot is within the plugin folder
    28             $url = sprintf(
    29                 'https://s.w.org/plugins/%s/%s?rev=%s',
    30                 $plugin->post_name,
    31                 $image['filename'],
    32                 $image['revision']
    33             );
    34         } else {
    35             // In the /assets/ folder
    36             $url = sprintf(
    37                 'https://ps.w.org/%s/assets/%s?rev=%s',
    38                 $plugin->post_name,
    39                 $image['filename'],
    40                 $image['revision']
     33            $url = Template::get_asset_url( $plugin->post_name, $image );
     34
     35            $output .= sprintf(
     36                '<li>
     37                    <a href="%1$s" rel="nofollow">
     38                        <img class="screenshot" src="%1$s">
     39                    </a>
     40                    <p>%2$s</p>
     41                </li>',
     42                $url,
     43                $description
    4144            );
    4245        }
    4346
    44         $output .= sprintf(
    45             '<li>
    46                 <a href="%1$s" rel="nofollow">
    47                     <img class="screenshot" src="%1$s">
    48                 </a>
    49                 <p>%2$s</p>
    50             </li>',
    51             $url,
    52             $description
    53         );
     47        return '<ol class="screenshots">' . $output . '</ol>';
     48
    5449    }
    55 
    56     return '<ol class="screenshots">' . $output . '</ol>';
    57 
    5850}
Note: See TracChangeset for help on using the changeset viewer.