Making WordPress.org


Ignore:
Timestamp:
06/20/2016 04:37:50 PM (10 years ago)
Author:
dd32
Message:

Plugin Directory: Allow a user to subscribe/unsubscribe from plugin commit emails.

Note: Plugin committers are automatically subscribed, and cannot unsubscribe.

See #1597

File:
1 edited

Legend:

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

    r3440 r3510  
    11<?php
    22namespace WordPressdotorg\Plugin_Directory;
     3use WP_User;
    34
    45/**
     
    169170                );
    170171        }
     172
     173        /**
     174         * Subscribe/Unsubscribe a user to a plugins commits.
     175         *
     176         * Plugin Committers are automatically subscribed to plugin commit
     177         * emails and cannot unsubscribe.
     178         *
     179         * @param string      $plugin_slug The plugin to subscribe to.
     180         * @param int|WP_User $user        The user to subscribe. Default current user.
     181         * @param bool        $subscribe   Whether to subscribe (true) or unsubscribe (false).
     182         *
     183         * @return bool Whether the user is subscribed.
     184         */
     185        public static function subscribe_to_plugin_commits( $plugin_slug, $user = 0, $subscribe = true ) {
     186                $post = Plugin_Directory::get_plugin_post( $plugin_slug );
     187                if ( ! $post ) {
     188                        return false;
     189                }
     190
     191                $user = new WP_User( $user ?: get_current_user_id() );
     192                if ( ! $user->exists() ) {
     193                        return false;
     194                }
     195
     196                $users = get_post_meta( $post->ID, '_commit_subscribed', true ) ?: array();
     197
     198                if ( $subscribe ) {
     199                        $users[] = $user->ID;
     200                        $users = array_unique( $users );
     201                } else {
     202                        if ( false !== ( $pos = array_search( $user->ID, $users, true ) ) ) {
     203                                unset( $users[ $pos ] );
     204                        }
     205                }
     206
     207                update_post_meta( $post->ID, '_commit_subscribed', $users );
     208
     209                return self::subscribed_to_plugin_commits( $plugin_slug, $user->ID );
     210        }
     211
     212        /**
     213         * Determine if a user is subscribed to a plugins commits.
     214         *
     215         * Plugin Committers are automatically subscribed to commits, and this
     216         * function does not respect that status.
     217         *
     218         * @param string      $plugin_slug The plugin to subscribe to.
     219         * @param int|WP_User $user        The user to check. Default current user.
     220         *
     221         * @return bool Whether the specified user is subscribed to commits.
     222         */
     223        public static function subscribed_to_plugin_commits( $plugin_slug, $user = 0 ) {
     224                $post = Plugin_Directory::get_plugin_post( $plugin_slug );
     225                if ( ! $post ) {
     226                        return false;
     227                }
     228
     229                $user = new WP_User( $user ?: get_current_user_id() );
     230                if ( ! $user->exists() ) {
     231                        return false;
     232                }
     233
     234                $users = get_post_meta( $post->ID, '_commit_subscribed', true ) ?: array();
     235
     236                return in_array( $user->ID, $users, true );
     237        }
    171238}
Note: See TracChangeset for help on using the changeset viewer.