Making WordPress.org

Changeset 4505


Ignore:
Timestamp:
12/08/2016 01:58:37 PM (10 years ago)
Author:
obenland
Message:

Plugin Directory: Use get_post() for template functions.

Template functions should fall back to the global post object.
Also fixes a bug where assets weren't loaded correctly while in the loop.

Props joostdevalk for initial patch.
Fixes #2303.

Location:
sites/trunk/wordpress.org/public_html/wp-content
Files:
6 edited

Legend:

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

    r4458 r4505  
    1414class Plugin extends Base {
    1515
     16        /**
     17         * Plugin constructor.
     18         */
    1619        function __construct() {
    1720                register_rest_route( 'plugins/v1', '/plugin/(?P<plugin_slug>[^/]+)/?', array(
     
    124127                foreach ( $screen_shots as $image ) {
    125128                        $result['screenshots'][ $image['resolution'] ] = array(
    126                                 'src'     => Template::get_asset_url( $post->post_name, $image ),
     129                                'src'     => Template::get_asset_url( $post, $image ),
    127130                                'caption' => array_key_exists( $image['resolution'], $descriptions ) ? $descriptions[ $image['resolution'] ] : ''
    128131                        );
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-template.php

    r4411 r4505  
    117117         */
    118118        public static function get_plugin_sections( $post = null ) {
    119                 $plugin      = get_post( $post );
    120                 $plugin_slug = $plugin->post_name;
     119                $plugin = get_post( $post );
    121120
    122121                $default_sections = array(
     
    174173                                case 'support':
    175174                                        $title = _x( 'Support', 'plugin tab title', 'wporg-plugins' );
    176                                         $url   = 'https://wordpress.org/support/plugin/' . $plugin_slug;
     175                                        $url   = 'https://wordpress.org/support/plugin/' . $plugin->post_name;
    177176                                        break;
    178177
    179178                                case 'reviews':
    180179                                        $title = _x( 'Reviews', 'plugin tab title', 'wporg-plugins' );
    181                                         $url   = 'https://wordpress.org/support/plugin/' . $plugin_slug . '/reviews/';
     180                                        $url   = 'https://wordpress.org/support/plugin/' . $plugin->post_name . '/reviews/';
    182181                                        break;
    183182
     
    211210         * @static
    212211         *
    213          * @param \WP_Post|string $plugin An instance of a Plugin post, or the plugin slug.
    214          * @param string          $output Optional. Output type. 'html' or 'raw'. Default: 'raw'.
     212         * @param int|\WP_Post|null $post   Optional. Post ID or post object. Defaults to global $post.
     213         * @param string            $output Optional. Output type. 'html' or 'raw'. Default: 'raw'.
    215214         * @return mixed
    216215         */
    217         public static function get_plugin_icon( $plugin, $output = 'raw' ) {
    218                 $plugin = Plugin_Directory::instance()->get_plugin_post( $plugin );
    219                 if ( ! $plugin ) {
    220                         return false;
    221                 }
    222                 $plugin_slug = $plugin->post_name;
    223 
     216        public static function get_plugin_icon( $post = null, $output = 'raw' ) {
     217                $plugin    = get_post( $post );
    224218                $raw_icons = get_post_meta( $plugin->ID, 'assets_icons', true );
    225219
     
    228222                        switch ( $info['resolution'] ) {
    229223                                case '256x256':
    230                                         $icon_2x = self::get_asset_url( $plugin_slug, $info );
     224                                        $icon_2x = self::get_asset_url( $plugin, $info );
    231225                                        break;
    232226
    233227                                case '128x128':
    234                                         $icon = self::get_asset_url( $plugin_slug, $info );
     228                                        $icon = self::get_asset_url( $plugin, $info );
    235229                                        break;
    236230
    237231                                /* false = the resolution of the icon, this is NOT disabled */
    238232                                case false && 'icon.svg' == $file:
    239                                         $svg = self::get_asset_url( $plugin_slug, $info );
     233                                        $svg = self::get_asset_url( $plugin, $info );
    240234                                        break;
    241235                        }
     
    270264                switch ( $output ) {
    271265                        case 'html':
    272                                 $id    = "plugin-icon-{$plugin_slug}";
     266                                $id    = "plugin-icon-{$plugin->post_name}";
    273267                                $html  = "<style type='text/css'>";
    274268                                $html .= "#{$id} { background-image: url('{$icon}'); } .plugin-icon { background-size: cover; height: 128px; width: 128px; }";
     
    293287         * @static
    294288         *
    295          * @param \WP_Post|string $plugin An instance of a Plugin post, or the plugin slug.
    296          * @param string          $output Optional. Output type. 'html' or 'raw'. Default: 'raw'.
     289         * @param int|\WP_Post|null $post   Optional. Post ID or post object. Defaults to global $post.
     290         * @param string            $output Optional. Output type. 'html' or 'raw'. Default: 'raw'.
    297291         * @return mixed
    298292         */
    299         public static function get_plugin_banner( $plugin, $output = 'raw' ) {
    300                 $plugin = Plugin_Directory::instance()->get_plugin_post( $plugin );
    301                 if ( ! $plugin ) {
    302                         return false;
    303                 }
     293        public static function get_plugin_banner( $post = null, $output = 'raw' ) {
     294                $plugin = get_post( $post );
    304295
    305296                $banner      = $banner_2x = false;
    306                 $plugin_slug = $plugin->post_name;
    307297                $raw_banners = get_post_meta( $plugin->ID, 'assets_banners', true );
    308298
     
    317307                        switch ( $info['resolution'] ) {
    318308                                case '1544x500':
    319                                         $banner_2x = self::get_asset_url( $plugin_slug, $info );
     309                                        $banner_2x = self::get_asset_url( $plugin, $info );
    320310                                        break;
    321311
    322312                                case '772x250':
    323                                         $banner = self::get_asset_url( $plugin_slug, $info );
     313                                        $banner = self::get_asset_url( $plugin, $info );
    324314                                        break;
    325315                        }
     
    330320                                switch ( $info['resolution'] ) {
    331321                                        case '1544x500':
    332                                                 $banner_2x = self::get_asset_url( $plugin_slug, $info );
     322                                                $banner_2x = self::get_asset_url( $plugin, $info );
    333323                                                break;
    334324
    335325                                        case '772x250':
    336                                                 $banner = self::get_asset_url( $plugin_slug, $info );
     326                                                $banner = self::get_asset_url( $plugin, $info );
    337327                                                break;
    338328                                }
     
    346336                switch ( $output ) {
    347337                        case 'html':
    348                                 $id    = "plugin-banner-{$plugin_slug}";
     338                                $id    = "plugin-banner-{$plugin->post_name}";
    349339                                $html  = "<style type='text/css'>";
    350340                                $html .= "#{$id} { background-image: url('{$banner}'); }";
     
    371361         * @static
    372362         *
    373          * @param string $plugin Plugin slug.
    374          * @param array  $asset Assets folder information.
     363         * @param int|\WP_Post|null $post  Optional. Post ID or post object. Defaults to global $post.
     364         * @param array             $asset Assets folder information.
    375365         * @return string
    376366         */
    377         public static function get_asset_url( $plugin, $asset ) {
     367        public static function get_asset_url( $post, $asset ) {
    378368                if ( ! empty( $asset['location'] ) && 'plugin' == $asset['location'] ) {
    379369
     
    394384                        $format,
    395385                        $asset['revision'],
    396                         $plugin,
     386                        get_post( $post )->post_name,
    397387                        $asset['filename']
    398388                ) );
     
    451441         * Generate a download link for a given plugin & version.
    452442         *
    453          * @param \WP_Post $post    The Plugin Post.
    454          * @param string   $version The version to link to. Optional. Default: latest.
     443         * @param int|\WP_Post|null $post    Optional. Post ID or post object. Defaults to global $post.
     444         * @param string            $version The version to link to. Optional. Default: latest.
    455445         * @return string The Download URL.
    456446         */
     
    486476         * Generates a link to toggle a plugin favorites state.
    487477         *
    488          * @param string $plugin_slug The plugin slug.
    489          * @param mixed  $user        The user to alter the favorite status for.
     478         * @param int|\WP_Post|null $post Optional. Post ID or post object. Defaults to global $post.
     479         * @param mixed             $user The user to alter the favorite status for.
    490480         * @return string URL to toggle status.
    491481         */
    492         public static function get_favourite_link( $plugin_slug, $user = 0 ) {
    493                 $post = Plugin_Directory::get_plugin_post( $plugin_slug );
    494                 if ( ! $post ) {
    495                         return false;
    496                 }
     482        public static function get_favorite_link( $post = null, $user = 0 ) {
     483                $post = get_post( $post );
    497484
    498485                $favorited = Tools::favorited_plugin( $post, $user );
    499486
    500                 return add_query_arg(
    501                         array(
    502                                 '_wpnonce' => wp_create_nonce( 'wp_rest' ),
    503                                 ( $favorited ? 'unfavorite' : 'favorite' ) => '1'
    504                         ),
    505                         home_url( 'wp-json/plugins/v1/plugin/' . $post->post_name . '/favorite' )
    506                 );
     487                return add_query_arg( array(
     488                        '_wpnonce' => wp_create_nonce( 'wp_rest' ),
     489                        ( $favorited ? 'unfavorite' : 'favorite' ) => '1'
     490                ), home_url( 'wp-json/plugins/v1/plugin/' . $post->post_name . '/favorite' ) );
    507491        }
    508492}
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/cli/class-import.php

    r4423 r4505  
    180180                if ( $first_banner = reset( $assets['banner'] ) ) {
    181181                        // The Banners are not stored locally, which is why a URL is used here
    182                         $banner_average_color = Tools::get_image_average_color( Template::get_asset_url( $plugin_slug, $first_banner ) );
     182                        $banner_average_color = Tools::get_image_average_color( Template::get_asset_url( $plugin, $first_banner ) );
    183183                }
    184184                update_post_meta( $plugin->ID, 'assets_banners_color', wp_slash( $banner_average_color ) );
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/shortcodes/class-screenshots.php

    r4471 r4505  
    3131                foreach ( $screen_shots as $image ) {
    3232                        $screen_shot = sprintf( '<a href="%1$s" rel="nofollow"><img class="screenshot" src="%1$s" alt="" /></a>',
    33                                 Template::get_asset_url( $plugin->post_name, $image )
     33                                Template::get_asset_url( $plugin, $image )
    3434                        );
    3535
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-plugins/functions.php

    r4504 r4505  
    164164        }
    165165
    166         $post_id = get_the_ID();
    167         $banner  = Template::get_plugin_banner( $post_id );
     166        $banner  = Template::get_plugin_banner();
    168167        $banner['banner_2x'] = $banner['banner_2x'] ? $banner['banner'] : false;
    169         $icon = Template::get_plugin_icon( $post_id );
    170 
    171         printf( '<meta property="og:title" content="%s" />' . "\n", the_title_attribute( array( 'echo' => false, 'post' => $post_id ) ) );
    172         printf( '<meta property="og:description" content="%s" />' . "\n", esc_attr( strip_tags( get_the_excerpt( $post_id ) ) ) );
     168        $icon = Template::get_plugin_icon();
     169
     170        printf( '<meta property="og:title" content="%s" />' . "\n", the_title_attribute( array( 'echo' => false ) ) );
     171        printf( '<meta property="og:description" content="%s" />' . "\n", esc_attr( strip_tags( get_the_excerpt() ) ) );
    173172        printf( '<meta property="og:site_name" content="WordPress.org" />' . "\n" );
    174173        printf( '<meta property="og:type" content="website" />' . "\n" );
    175         printf( '<meta property="og:url" content="%s" />' . "\n", esc_url( get_permalink( $post_id ) ) );
     174        printf( '<meta property="og:url" content="%s" />' . "\n", esc_url( get_permalink() ) );
    176175        printf( '<meta name="twitter:card" content="summary_large_image">' . "\n" );
    177176        printf( '<meta name="twitter:site" content="@WordPress">' . "\n" );
    178177
    179178        if ( $banner['banner_2x'] ) {
    180                 printf( '<meta name="twitter:image" content="%s" />' . "\n", $banner['banner_2x'] );
     179                printf( '<meta name="twitter:image" content="%s" />' . "\n", esc_url( $banner['banner_2x'] ) );
    181180        }
    182181        if ( isset( $banner['banner'] ) ) {
    183                 printf( '<meta property="og:image" content="%s" />' . "\n", $banner['banner'] );
     182                printf( '<meta property="og:image" content="%s" />' . "\n", esc_url( $banner['banner'] ) );
    184183        }
    185184        if ( ! $icon['generated'] && ( $icon['icon_2x'] || $icon['icon'] ) ) {
    186                 printf( '<meta name="thumbnail" content="%s" />', $icon['icon_2x'] ?: $icon['icon'] );
     185                printf( '<meta name="thumbnail" content="%s" />' . "\n", esc_url( $icon['icon_2x'] ?: $icon['icon'] ) );
    187186        }
    188187}
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-plugins/template-parts/plugin-single.php

    r4467 r4505  
    6767                        <?php
    6868                        if ( is_user_logged_in() ) :
    69                                 $url = Template::get_favourite_link( $post );
     69                                $url = Template::get_favorite_link();
    7070                                $is_favorited = Tools::favorited_plugin( $post );
    7171                                ?>
     
    141141                                        $section_no_read_mores[] = 'faq';
    142142                                }
    143                                
     143
    144144                                $section_read_more = ! in_array( $section_slug, $section_no_read_mores );
    145145
Note: See TracChangeset for help on using the changeset viewer.