Making WordPress.org

Changeset 3510


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

Location:
sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory
Files:
1 added
3 edited

Legend:

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

    r3460 r3510  
    1212         */
    1313        static function load_routes() {
     14                new Routes\Commit_Subscriptions();
    1415                new Routes\Internal_Stats();
    1516                new Routes\Plugin();
  • 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}
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/shortcodes/class-developers.php

    r3435 r3510  
    3232                $output .= '</ul>';
    3333
     34                if ( is_user_logged_in() ) {
     35                        $subscribed = Tools::subscribed_to_plugin_commits( $post, get_current_user_id() );
     36
     37                        $subscribe_change_url = esc_url( add_query_arg(
     38                                array(
     39                                        '_wpnonce' => wp_create_nonce( 'wp_rest' ),
     40                                        ( $subscribed ? 'unsubscribe' : 'subscribe' ) => '1'
     41                                ),
     42                                home_url( 'wp-json/plugins/v1/plugin/' . $post->post_name . '/commit-subscription' )
     43                        ) );
     44                }
     45
    3446                return $output .
    3547                        '<h5>' . __( 'Browse the code', 'wporg-plugins' ) . '</h5>' .
     
    3749                                '<li>' .
    3850                                        '<a href="' . esc_url( "https://plugins.trac.wordpress.org/log/{$post->post_name}/" ) . '" rel="nofollow">' . __( 'Development Log', 'wporg-plugins' ) . '</a>' . "\n" .
    39                                         '<a href="' . esc_url( "https://plugins.trac.wordpress.org/log/{$post->post_name}/?limit=100&mode=stop_on_copy&format=rss" ) . '" rel="nofollow"><img src="//s.w.org/style/images/feedicon.png" /></a>' .
     51                                        '<a href="' . esc_url( "https://plugins.trac.wordpress.org/log/{$post->post_name}/?limit=100&mode=stop_on_copy&format=rss" ) . '" rel="nofollow"><img src="https://s.w.org/style/images/feedicon.png" /></a>' .
    4052                                '</li>' .
    4153                                '<li><a href="' . esc_url( "https://plugins.svn.wordpress.org/{$post->post_name}/" ) . '" rel="nofollow">' . __( 'Subversion Repository', 'wporg-plugins' ) . '</a></li>' .
    4254                                '<li><a href="' . esc_url( "https://plugins.trac.wordpress.org/browser/{$post->post_name}/" ) . '" rel="nofollow">' . __( 'Browse in Trac', 'wporg-plugins' ) . '</a></li>' .
    4355                                '<li><a href="' . esc_url( "https://translate.wordpress.org/projects/wp-plugins/{$post->post_name}/" ) . '" rel="nofollow">' . __( 'Translation Contributors', 'wporg-plugins' ) . '</a></li>' .
     56                                 ( !is_user_logged_in() ? '' : (
     57                                        $subscribed ?
     58                                        "<li><a href='$subscribe_change_url'>" . __( 'Unsubscribe from plugin commits', 'wporg-plugins' ) . '</a></li>' :
     59                                        "<li><a href='$subscribe_change_url'>" . __( 'Subscribe to plugin commits via email', 'wporg-plugins' ) . '</a></li>'
     60                                 ) ) .
    4461                        '</ul>';
    4562        }
Note: See TracChangeset for help on using the changeset viewer.