Making WordPress.org

Changeset 3545


Ignore:
Timestamp:
06/22/2016 01:33:53 PM (8 years ago)
Author:
dd32
Message:

Plugin Directory: Add plugin favoriting to the user interface.
Someone will need to style this properly.

Fixes #1577

Location:
sites/trunk/wordpress.org/public_html/wp-content
Files:
7 edited
1 copied

Legend:

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

    r3510 r3545  
    1212     */
    1313    static function load_routes() {
    14         new Routes\Commit_Subscriptions();
    1514        new Routes\Internal_Stats();
    1615        new Routes\Plugin();
    1716        new Routes\Plugin_Translations();
     17        new Routes\Plugin_Favorites();
     18        new Routes\Commit_Subscriptions();
    1819        new Routes\Popular_Categories();
    1920        new Routes\Query_Plugins();
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/api/routes/class-plugin-favorites.php

    r3523 r3545  
    66
    77/**
    8  * An API endpoint for subscribing to commits for a particular plugin.
     8 * An API endpoint for favoriting a particular plugin.
    99 *
    1010 * @package WordPressdotorg_Plugin_Directory
    1111 */
    12 class Commit_Subscriptions extends Base {
     12class Plugin_Favorites extends Base {
    1313
    1414    public function __construct() {
    15         register_rest_route( 'plugins/v1', '/plugin/(?P<plugin_slug>[^/]+)/commit-subscription', array(
    16             'methods'  => \WP_REST_Server::READABLE,
    17             'callback' => array( $this, 'subscribe' ),
     15        register_rest_route( 'plugins/v1', '/plugin/(?P<plugin_slug>[^/]+)/favorite', array(
     16            'methods'  => array( \WP_REST_Server::READABLE, \WP_REST_Server::CREATABLE ),
     17            'callback' => array( $this, 'favorite' ),
    1818            'args' => array(
    1919                'plugin_slug' => array(
    2020                    'validate_callback' => array( $this, 'validate_plugin_slug_callback' ),
    2121                ),
    22                 'subscribe' => array(
     22                'favorite' => array(
    2323                    'validate_callback' => function( $bool ) { return is_numeric( $bool ); },
    2424                ),
    25                 'unsubscribe' => array(
     25                'unfavorite' => array(
    2626                    'validate_callback' => function( $bool ) { return is_numeric( $bool ); },
    2727                ),
     
    3232
    3333    /**
    34      * Endpoint to subscribe to a plugin's commits.
     34     * Endpoint to favorite a plugin.
    3535     *
    3636     * @param \WP_REST_Request $request The Rest API Request.
    37      * @return bool True if the subscription was successful.
     37     * @return bool True if the favoriting was successful.
    3838     */
    39     public function subscribe( $request ) {
    40         $location = get_permalink( Plugin_Directory::get_plugin_post( $request['plugin_slug'] ) ) . '#developers';
     39    public function favorite( $request ) {
     40        $location = get_permalink( Plugin_Directory::get_plugin_post( $request['plugin_slug'] ) );
    4141        header( "Location: $location" );
    4242
     
    4545        );
    4646
    47         if ( ! isset( $request['subscribe'] ) && ! isset( $request['unsubscribe'] ) ) {
     47        if ( ! isset( $request['favorite'] ) && ! isset( $request['unfavorite'] ) ) {
    4848            $result['error'] = 'Unknown Action';
    4949        }
    5050
    51         $result['subscribed'] = Tools::subscribe_to_plugin_commits( $request['plugin_slug'], get_current_user_id(), isset( $request['subscribe'] ) );
     51        $result['favorite'] = Tools::favorite_plugin( $request['plugin_slug'], get_current_user_id(), isset( $request['favorite'] ) );
    5252
    5353        return (object) $result;
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-template.php

    r3511 r3545  
    462462        return ent2ncr( htmlspecialchars_decode( htmlentities( $string, ENT_NOQUOTES, 'UTF-8' ), ENT_NOQUOTES ) );
    463463    }
     464
     465    /**
     466     * Generates a link to toggle a plugin favorites state.
     467     *
     468     * @param string $plugin_slug The plugin slug.
     469     * @param mixed  $user        The user to alter the favorite status for.
     470     * @return string URL to toggle status.
     471     */
     472    public static function get_favourite_link( $plugin_slug, $user = 0 ) {
     473        $post = Plugin_Directory::get_plugin_post( $plugin_slug );
     474        if ( ! $post ) {
     475            return false;
     476        }
     477
     478        $favorited = Tools::favorited_plugin( $post, $user );
     479
     480        return add_query_arg(
     481            array(
     482                '_wpnonce' => wp_create_nonce( 'wp_rest' ),
     483                ( $favorited ? 'unfavorite' : 'favorite' ) => '1'
     484            ),
     485            home_url( 'wp-json/plugins/v1/plugin/' . $post->post_name . '/favorite' )
     486        );
     487    }
    464488}
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-tools.php

    r3530 r3545  
    257257
    258258    /**
     259     * Determine if a plugin has been favorited by a user.
     260     *
     261     * @param string $plugin_slug The plugin to check.
     262     * @param mixed  $user        The user to check.
     263     * @return bool
     264     */
     265    public static function favorited_plugin( $plugin_slug, $user = 0 ) {
     266        $post = Plugin_Directory::get_plugin_post( $plugin_slug );
     267        if ( ! $post ) {
     268            return false;
     269        }
     270
     271        $user = new WP_User( $user ?: get_current_user_id() );
     272        if ( ! $user->exists() ) {
     273            return false;
     274        }
     275
     276        $users_favorites = get_user_meta( $user->ID, 'plugin_favorites', true ) ?: array();
     277
     278        return in_array( $post->post_name, $users_favorites, true );
     279    }
     280
     281    /**
     282     * Favorite a plugin
     283     *
     284     * @param string $plugin_slug The plugin to favorite
     285     * @param mixed  $user        The user favorite
     286     * @param bool   $favorite    Whether it's a favorite, or unfavorite.
     287     * @return bool
     288     */
     289    public static function favorite_plugin( $plugin_slug, $user = 0, $favorite = true ) {
     290        $post = Plugin_Directory::get_plugin_post( $plugin_slug );
     291        if ( ! $post ) {
     292            return false;
     293        }
     294
     295        $user = new WP_User( $user ?: get_current_user_id() );
     296        if ( ! $user->exists() ) {
     297            return false;
     298        }
     299
     300        $users_favorites = get_user_meta( $user->ID, 'plugin_favorites', true ) ?: array();
     301
     302        $already_favorited = in_array( $post->post_name, $users_favorites, true );
     303
     304        if ( $favorite && $already_favorited ) {
     305            return true;
     306        } elseif ( $favorite ) {
     307            // Add it
     308            $users_favorites[] = $post->post_name;
     309        } elseif ( ! $favorite && $already_favorited ) {
     310            // Remove it
     311            unset( $users_favorites[ array_search( $post->post_name, $users_favorites, true ) ] );
     312        } else {
     313            return true;
     314        }
     315
     316        return update_user_meta( $user->ID, 'plugin_favorites', wp_slash( array_values( $users_favorites ) ) );
     317
     318    }
     319
     320    /**
    259321     * Retrieve a list of users who are subscribed to plugin commits.
    260322     *
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-plugins/css/style-rtl.css

    r3539 r3545  
    21592159}
    21602160
    2161 .single .type-plugin .plugin-header .plugin-download {
     2161.single .type-plugin .plugin-header .plugin-actions {
    21622162  float: left;
     2163}
     2164
     2165.single .type-plugin .plugin-header .plugin-favorite-heart {
     2166  color: gray;
     2167  line-heght: 3em;
     2168  font-size: 3em;
     2169}
     2170
     2171.single .type-plugin .plugin-header .plugin-favorite-heart.favorited, .single .type-plugin .plugin-header .plugin-favorite-heart:hover, .single .type-plugin .plugin-header .plugin-favorite-heart:focus {
     2172  color: red;
     2173}
     2174
     2175.single .type-plugin .plugin-header .plugin-favorite-heart:hover, .single .type-plugin .plugin-header .plugin-favorite-heart:focus {
     2176  text-decoration: none;
     2177}
     2178
     2179.single .type-plugin .plugin-header .plugin-favorite-heart:after {
     2180  content: "\f487";
     2181  font-family: dashicons;
     2182  vertical-align: top;
    21632183}
    21642184
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-plugins/css/style.css

    r3539 r3545  
    21592159}
    21602160
    2161 .single .type-plugin .plugin-header .plugin-download {
     2161.single .type-plugin .plugin-header .plugin-actions {
    21622162  float: right;
     2163}
     2164
     2165.single .type-plugin .plugin-header .plugin-favorite-heart {
     2166  /* FIX ME */
     2167  color: gray;
     2168  line-heght: 3em;
     2169  font-size: 3em;
     2170}
     2171
     2172.single .type-plugin .plugin-header .plugin-favorite-heart.favorited, .single .type-plugin .plugin-header .plugin-favorite-heart:hover, .single .type-plugin .plugin-header .plugin-favorite-heart:focus {
     2173  color: red;
     2174}
     2175
     2176.single .type-plugin .plugin-header .plugin-favorite-heart:hover, .single .type-plugin .plugin-header .plugin-favorite-heart:focus {
     2177  text-decoration: none;
     2178}
     2179
     2180.single .type-plugin .plugin-header .plugin-favorite-heart:after {
     2181  content: "\f487";
     2182  font-family: dashicons;
     2183  vertical-align: top;
    21632184}
    21642185
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-plugins/sass/site/primary/_plugin-single.scss

    r3459 r3545  
    4747        }
    4848
    49         .plugin-download {
     49        .plugin-actions {
    5050            float: right;
     51        }
     52
     53        .plugin-favorite-heart {
     54            /* FIX ME */
     55            color: gray;
     56            line-heght: 3em;
     57            font-size: 3em;
     58
     59            &.favorited,
     60            &:hover,
     61            &:focus {
     62                color: red;
     63            }
     64
     65            &:hover,
     66            &:focus {
     67                text-decoration: none;
     68            }
     69
     70            &:after {
     71                content: "\f487";
     72                font-family: dashicons;
     73                vertical-align: top;
     74            }
     75           
    5176        }
    5277
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-plugins/template-parts/plugin-single.php

    r3466 r3545  
    1111use WordPressdotorg\Plugin_Directory\Plugin_Directory;
    1212use WordPressdotorg\Plugin_Directory\Template;
     13use WordPressdotorg\Plugin_Directory\Tools;
    1314
    1415$content = Plugin_Directory::instance()->split_post_content_into_pages( get_the_content() );
     
    3334        </div>
    3435
    35         <a class="plugin-download button download-button button-large" href="<?php echo esc_url( Template::download_link() ); ?>" itemprop="downloadUrl"><?php _e( 'Download', 'wporg-plugins' ); ?></a>
    36         <meta itemprop="softwareVersion" content="<?php echo esc_attr( get_post_meta( get_the_ID(), 'version', true ) ); ?>">
    37         <meta itemprop="fileFormat" content="application/zip">
     36        <div class="plugin-actions">
     37
     38            <?php
     39            if ( is_user_logged_in() ) {
     40                $url = Template::get_favourite_link( $post );
     41                echo '<a href="' . esc_url( $url ) . '" class="plugin-favorite-heart' . (Tools::favorited_plugin( $post ) ? ' favorited' : '' ) . '" onclick="jQuery(this).toggleClass(\'favorited\')"></a>';
     42            }
     43            ?>
     44
     45            <a class="plugin-download button download-button button-large" href="<?php echo esc_url( Template::download_link() ); ?>" itemprop="downloadUrl"><?php _e( 'Download', 'wporg-plugins' ); ?></a>
     46            <meta itemprop="softwareVersion" content="<?php echo esc_attr( get_post_meta( get_the_ID(), 'version', true ) ); ?>">
     47            <meta itemprop="fileFormat" content="application/zip">
     48        </div>
    3849
    3950        <?php the_title( '<h1 class="plugin-title">', '</h1>' ); ?>
Note: See TracChangeset for help on using the changeset viewer.