WordPress.org

Making WordPress.org


Ignore:
Timestamp:
07/22/2015 04:00:32 AM (6 years ago)
Author:
dd32
Message:

WordPress.org Themes: Add the ability to favorite themes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/theme-directory/theme-directory.php

    r1765 r1770  
    148148
    149149    // Add the browse/* views
    150     add_rewrite_tag( '%browse%', '(featured|popular|new)' );
     150    add_rewrite_tag( '%browse%', '(featured|popular|new|favorites)' );
    151151    add_permastruct( 'browse', 'browse/%browse%' );
    152152
     
    611611    if ( get_query_var( 'browse' ) ) {
    612612        $request['browse'] = get_query_var( 'browse' );
     613
     614        if ( 'favorites' === $request['browse'] ) {
     615            $request['user'] = wp_get_current_user()->user_login;
     616        }
    613617
    614618    } else if ( get_query_var( 'tag' ) ) {
     
    682686    return $api->response;
    683687}
     688
     689
     690/**
     691 * Returns if the current theme is favourited by the current user.
     692 */
     693function wporg_themes_is_favourited( $theme_slug ) {
     694    return in_array( $theme_slug, wporg_themes_get_user_favorites() );
     695}
     696
     697/**
     698 * Returns the current themes favorited by the user.
     699 *
     700 * @param int $user_id The user to get the favorites of. Default: current user
     701 *
     702 * @return array
     703 */
     704function wporg_themes_get_user_favorites( $user_id = 0 ) {
     705    if ( ! $user_id ) {
     706        $user_id = get_current_user_id();
     707    }
     708    if ( ! $user_id ) {
     709        return array();
     710    }
     711
     712    $favorites = get_user_meta( $user_id, 'theme_favorites', true );
     713
     714    return $favorites ? array_values( $favorites ) : array();
     715}
     716
     717/**
     718 * Sets current themes favorited by the user.
     719 *
     720 * @param array $favorites An array of theme slugs to mark as favorited.
     721 * @param int   $user_id   The user to get the favorites of. Default: current user
     722 *
     723 * @return bool
     724 */
     725function wporg_themes_set_user_favorites( $favorites, $user_id = 0 ) {
     726    if ( ! $user_id ) {
     727        $user_id = get_current_user_id();
     728    }
     729    if ( ! $user_id ) {
     730        return false;
     731    }
     732
     733    return update_user_meta( $user_id, 'theme_favorites', (array) $favorites );
     734}
     735
     736/**
     737 * Favorite a theme for the current user
     738 *
     739 * @param int $theme_slug The theme to favorite.
     740 *
     741 * @return bool|WP_Error
     742 */
     743function wporg_themes_add_favorite( $theme_slug ) {
     744    if ( ! is_user_logged_in() ) {
     745        return new WP_Error( 'not_logged_in' );
     746    }
     747
     748    $favorites = wporg_themes_get_user_favorites();
     749    if ( ! in_array( $theme_slug, $favorites ) ) {
     750        $favorites[] = $theme_slug;
     751        return wporg_themes_set_user_favorites( $favorites );
     752    }
     753    return true;
     754}
     755
     756/**
     757 * Remove a favorited theme for the current user
     758 *
     759 * @param int $theme_slug The theme to favorite.
     760 *
     761 * @return bool|WP_Error
     762 */
     763function wporg_themes_remove_favorite( $theme_slug ) {
     764    if ( ! is_user_logged_in() ) {
     765        return new WP_Error( 'not_logged_in' );
     766    }
     767
     768    $favorites = wporg_themes_get_user_favorites();
     769    if ( in_array( $theme_slug, $favorites ) ) {
     770        unset( $favorites[ array_search( $theme_slug, $favorites, true ) ] );
     771        return wporg_themes_set_user_favorites( $favorites );
     772    }
     773    return true;
     774}
Note: See TracChangeset for help on using the changeset viewer.