Making WordPress.org


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

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

Fixes #1577

File:
1 edited

Legend:

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