Making WordPress.org


Ignore:
Timestamp:
12/08/2016 01:58:37 PM (8 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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}
Note: See TracChangeset for help on using the changeset viewer.