Making WordPress.org


Ignore:
Timestamp:
12/08/2017 05:37:23 PM (7 years ago)
Author:
obenland
Message:

Plugins: Simplify template logic.

Moves most of the logic into template tags to make the single-plugin template it bit easier to understand.

Props joostdevalk.
Fixes #3310.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-plugins/inc/template-tags.php

    r4223 r6251  
    1010namespace WordPressdotorg\Plugin_Directory\Theme;
    1111use WordPressdotorg\Plugin_Directory\Template;
     12use WordPressdotorg\Plugin_Directory\Tools;
    1213
    1314// Returns an absolute url to the current url, no matter what that actually is.
     
    7273    }
    7374}
     75
     76
     77/**
     78 * Displays a plugin banner.
     79 *
     80 * @param int|\WP_Post|null $post Optional. Post ID or post object. Defaults to global $post.
     81 */
     82function the_plugin_banner( $post = null ) {
     83    echo Template::get_plugin_banner( $post, 'html' );
     84}
     85
     86/**
     87 * Displays a button to favorite or unfavorite a plugin.
     88 *
     89 * @param int|\WP_Post|null $post Optional. Post ID or post object. Defaults to global $post.
     90 */
     91function the_plugin_favorite_button( $post = null ) {
     92    if ( ! is_user_logged_in() ) {
     93        return;
     94    }
     95
     96    $is_favorited = Tools::favorited_plugin( get_post( $post ) );
     97?>
     98<div class="plugin-favorite">
     99    <a href="<?php echo esc_url( Template::get_favorite_link() ); ?>" class="plugin-favorite-heart<?php echo $is_favorited ? ' favorited' : ''; ?>">
     100        <span class="screen-reader-text">
     101            <?php
     102            if ( $is_favorited ) {
     103                /* translators: %s: plugin name */
     104                printf( __( 'Unfavorite %s', 'wporg-plugins' ), get_the_title() );
     105            } else {
     106                /* translators: %s: plugin name */
     107                printf( __( 'Favorite %s', 'wporg-plugins' ), get_the_title() );
     108            }
     109            ?>
     110        </span>
     111    </a>
     112    <script>
     113        jQuery( '.plugin-favorite-heart' )
     114            .on( 'click touchstart animationend', function() {
     115                jQuery( this ).toggleClass( 'is-animating' );
     116            } )
     117            .on( 'click', function() {
     118                jQuery( this ).toggleClass( 'favorited' );
     119            } );
     120    </script>
     121</div>
     122<?php
     123}
     124
     125/**
     126 * Displays the byline for a plugin author.
     127 *
     128 * @param int|\WP_Post|null $post Optional. Post ID or post object. Defaults to global $post.
     129 */
     130function the_author_byline( $post = null ) {
     131    $post = get_post( $post );
     132
     133    $url    = get_post_meta( $post->ID, 'header_author_uri', true );
     134    $author = strip_tags( get_post_meta( $post->ID, 'header_author', true ) ) ?: get_the_author();
     135    $author = esc_html( Template::encode( $author ) );
     136    $author = $url ? '<a class="url fn n" rel="nofollow" href="' . esc_url( $url ) . '">' . $author . '</a>' : $author;
     137
     138    printf( _x( 'By %s', 'post author', 'wporg-plugins' ), '<span class="author vcard">' . $author . '</span>' );
     139}
     140
     141/**
     142 * Displays a descriptive status notice for active plugins.
     143 *
     144 * @param int|\WP_Post|null $post Optional. Post ID or post object. Defaults to global $post.
     145 */
     146function the_active_plugin_notice( $post = null ) {
     147    if ( ! in_array( get_post_status( $post ), ['rejected', 'closed'], true ) ) {
     148        echo get_plugin_status_notice( $post );
     149    };
     150}
     151
     152/**
     153 * Displays a descriptive status notice for inactive plugins.
     154 *
     155 * @param int|\WP_Post|null $post Optional. Post ID or post object. Defaults to global $post.
     156 */
     157function the_closed_plugin_notice( $post = null ) {
     158    echo get_closed_plugin_notice( $post );
     159}
     160
     161/**
     162 * Returns a descriptive status notice for inactive plugins.
     163 *
     164 * @param int|\WP_Post|null $post Optional. Post ID or post object. Defaults to global $post.
     165 * @return string Message markup.
     166 */
     167function get_closed_plugin_notice( $post = null ) {
     168    $post   = get_post( $post );
     169    $notice = '';
     170
     171    if ( in_array( get_post_status( $post ), ['rejected', 'closed'], true ) ) {
     172        $notice = get_plugin_status_notice( $post );
     173
     174        if ( get_current_user_id() == $post->post_author ) {
     175            $info_notice = '<div class="plugin-notice notice notice-info notice-alt"><p>%s</p></div><!-- .plugin-notice -->';
     176            $message     = sprintf(
     177            /* translators: 1: plugins@wordpress.org */
     178                __( 'If you did not request this change, please contact <a href="mailto:%1$s">%1$s</a> for a status. All developers with commit access are contacted when a plugin is closed, with the reasons why, so check your spam email too.', 'wporgplugins' ),
     179                'plugins@wordpress.org'
     180            );
     181
     182            $notice .= sprintf( $info_notice, $message );
     183        }
     184    };
     185
     186    return $notice;
     187}
     188
     189/**
     190 * Return a descriptive status notice based on the plugin's current post_status.
     191 *
     192 * @param int|\WP_Post|null $post Optional. Post ID or post object. Defaults to global $post.
     193 * @return string Message markup.
     194 */
     195function get_plugin_status_notice( $post = null ) {
     196    $post_status    = get_post_status( $post );
     197    $info_notice    = '<div class="plugin-notice notice notice-info notice-alt"><p>%s</p></div><!-- .plugin-notice -->';
     198    $error_notice   = '<div class="plugin-notice notice notice-error notice-alt"><p>%s</p></div><!-- .plugin-notice -->';
     199    $warning_notice = '<div class="plugin-notice notice notice-warning notice-alt"><p>%s</p></div><!-- .plugin-notice -->';
     200
     201    $message = '';
     202
     203    switch ( $post_status ) {
     204        case 'publish':
     205            if ( time() - get_post_modified_time() > 2 * YEAR_IN_SECONDS ) {
     206                $message = sprintf(
     207                    $warning_notice,
     208                    __( 'This plugin <strong>hasn&#146;t been updated in over 2 years</strong>. It may no longer be maintained or supported and may have compatibility issues when used with more recent versions of WordPress.', 'wporg-plugins' )
     209                );
     210            }
     211            break;
     212
     213        case 'draft':
     214        case 'pending':
     215            $message = sprintf(
     216                $info_notice,
     217                __( 'This plugin is requested and not visible to the public yet. Please be patient as your plugin gets reviewed.', 'wporg-plugins' )
     218            );
     219            break;
     220
     221        case 'approved':
     222            $message = sprintf(
     223                $info_notice,
     224                __( 'This plugin is approved and awaiting data upload but not visible to the public yet. Once you make your first commit, the plugin will become public.', 'wporg-plugins' )
     225            );
     226            break;
     227
     228        case 'rejected':
     229            $message = sprintf(
     230                $error_notice,
     231                __( 'This plugin has been rejected and is not visible to the public.', 'wporg-plugins' )
     232            );
     233            break;
     234
     235        case 'disabled':
     236            $message = current_user_can( 'plugin_approve' )
     237                ? __( 'This plugin is disabled (closed, but actively serving updates).', 'wporg-plugins' )
     238                : __( 'This plugin has been closed for new installs.', 'wporg-plugins' );
     239
     240            $message = sprintf( $error_notice, $message );
     241            break;
     242
     243        case 'closed':
     244            $closed_date = get_post_meta( get_the_ID(), 'plugin_closed_date', true );
     245            if ( ! empty( $closed_date ) ) {
     246                $message = sprintf( __( 'This plugin was closed on %s and is no longer available for download.', 'wporg-plugins' ), mysql2date( get_option( 'date_format' ), $closed_date ) );
     247            } else {
     248                $message = __( 'This plugin has been closed and is no longer available for download.', 'wporg-plugins' );
     249            }
     250
     251            $message = sprintf( $error_notice, $message );
     252            break;
     253
     254        // Fall through.
     255        default:
     256            $message = sprintf(
     257                $error_notice,
     258                __( 'This plugin has been closed and is no longer available for download.', 'wporg-plugins' )
     259            );
     260            break;
     261    }
     262
     263    return $message;
     264}
Note: See TracChangeset for help on using the changeset viewer.