Making WordPress.org

Changeset 4260


Ignore:
Timestamp:
10/19/2016 02:30:54 AM (10 years ago)
Author:
dd32
Message:

Plugin Directory: Move the Stats from wp-admin to the front-end /$plugin/admin/ screen.
This also implements the capabilities required for access to the plugin admin screen for each plugin.

See #2111

Location:
sites/trunk/wordpress.org/public_html/wp-content
Files:
1 deleted
6 edited
1 moved

Legend:

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

    r4212 r4260  
    376376                        );
    377377
    378                         add_meta_box(
    379                                 'plugin-stats',
    380                                 __( 'Plugin Stats', 'wporg-plugins' ),
    381                                 array( __NAMESPACE__ . '\Metabox\Stats', 'display' ),
    382                                 'plugin', 'normal'
    383                         );
    384378                }
    385379
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-capabilities.php

    r4212 r4260  
    2222         */
    2323        public static function map_meta_cap( $required_caps, $cap, $user_id, $context ) {
     24                $plugin_edit_cap = false;
    2425                switch( $cap ) {
    25 
    26                         // TODO: Map these for the users
    27                         case 'plugin_edit':
     26                        case 'plugin_admin_edit':
    2827                        case 'plugin_add_committer':
    2928                        case 'plugin_remove_committer':
     29                                $plugin_edit_cap = true;
     30                                // Fall through
     31
     32                        case 'plugin_admin_view':
     33                                // Committers + Contributors.
     34                                // If no committers, post_author.
    3035                                $required_caps = array();
    3136                                $post = get_post( $context[0] );
     37
    3238                                if ( ! $post ) {
    3339                                        $required_caps[] = 'do_not_allow';
    … …  
    3541                                }
    3642
    37                                 $user       = new \WP_User( $user_id );
     43                                $user = new \WP_User( $user_id );
     44                                if ( $user->has_cap( 'plugin_review' ) ) {
     45                                        $required_caps[] = 'plugin_review';
     46                                        break;
     47                                }
     48
     49                                // Committers
    3850                                $committers = Tools::get_plugin_committers( $post->post_name );
     51                                if ( ! $committers && 'publish' === $post->post_status ) {
     52                                        // post_author in the event no committers exist (yet?)
     53                                        $committers = array( get_user_by( 'ID', $post->post_author )->user_login );
     54                                }
    3955
    40                                 if ( (int) $post->post_author === $user_id || in_array( $user->user_login, $committers, true ) ) {
    41                                         $required_caps[] = 'plugin_edit_own';
     56                                if ( in_array( $user->user_login, $committers ) ) {
     57                                        $required_caps[] = 'exist'; // All users are allowed to exist, even when they have no role.
     58                                        break;
     59                                }
    4260
    43                                 } else {
    44 
    45                                         if ( in_array( $post->post_status, array( 'draft', 'pending' ) ) ) {
    46                                                 $required_caps[] = 'plugin_review';
    47 
    48                                         } else {
    49                                                 $required_caps[] = 'plugin_approve';
     61                                if ( ! $plugin_edit_cap ) {
     62                                        // Contributors can view, but not edit.
     63                                        $contributors = (array) wp_list_pluck( get_the_terms( $post, 'plugin_contributors' ), 'name' );
     64                                        if ( in_array( $user->user_nicename, $contributors, true ) ) {
     65                                                $required_caps[] = 'exist'; // All users are allowed to exist, even when they have no role.
     66                                                break;
    5067                                        }
    5168                                }
    52                                 break;
    5369
    54                         // Don't allow any users to alter the post meta for plugins.
    55                         case 'add_post_meta':
    56                         case 'edit_post_meta':
    57                         case 'delete_post_meta':
    58                                 $post = get_post( $context );
    59                                 if ( $post && 'plugin' == $post->post_type ) {
    60                                         $required_caps[] = 'do_not_allow';
    61                                 }
     70                                // Else;
     71                                $required_caps[] = 'do_not_allow';
    6272                                break;
    6373
    … …  
    8595                $reviewer = array(
    8696                        'read'                 => true,
    87                         'plugin_edit_own'      => true,
    8897                        'plugin_set_category'  => true,
    89                         'plugin_add_committer' => true,
    90                         'plugin_edit_others'   => true,
    9198                        'moderate_comments'    => true,
    9299                        'plugin_edit_pending'  => true,
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-plugin-directory.php

    r4259 r4260  
    244244                        ),
    245245                        'public'            => true,
    246                         'show_ui'           => is_super_admin(),
     246                        'show_ui'           => true,
    247247                        'show_admin_column' => false,
    248248                        'meta_box_cb'       => false,
    … …  
    286286                add_rewrite_tag( '%browse%', '(featured|popular|beta|new|favorites)' );
    287287                add_permastruct( 'browse', 'browse/%browse%' );
    288 
    289                 // /browse/ should be the popular archive view.
    290                 add_rewrite_rule( '^browse$', 'index.php?browse=popular', 'top' );
    291288
    292289                // Create an archive for a users favorites too.
    … …  
    570567                        }
    571568
    572                         // TODO: Make plugins owned by `post_author = $current_user_id` show up here when they're not-publish?
    573 
    574569                        $wp_query->query_vars['orderby'] = 'post_title';
    575570                        $wp_query->query_vars['order'] = 'ASC';
    … …  
    592587                                }
    593588
    594                                 // TODO: Switch this to the capabilities systems
     589                                // Only the 'publish' cap shouldn't be affected by cap checks.
    595590                                $restricted_access_statii = array_diff( $wp_query->query_vars['post_status'], array( 'publish' ) );
    596591                                foreach ( $posts as $i => $post ) {
    … …  
    600595                                        }
    601596
    602                                         // Plugin Reviewers can see all sorts of plugins
    603                                         if ( current_user_can( 'plugin_review' ) ) {
     597                                        // If the current user can view the plugin admin, show it
     598                                        if ( current_user_can( 'plugin_admin_view', $post ) ) {
    604599                                                continue;
    605600                                        }
    606601
    607                                         // Original submitter can always see
    608                                         if ( $post->post_author == get_current_user_id() ) {
    609                                                 continue;
    610                                         }
    611 
    612                                         // Committers (user_login) can always see
    613                                         if ( in_array( wp_get_current_user()->user_login, (array) Tools::get_plugin_committers( $post->post_name ), true ) ) {
    614                                                 continue;
    615                                         }
    616 
    617                                         // Contributors (user_nicename) can always see
    618                                         if ( in_array( wp_get_current_user()->user_nicename, (array) wp_list_pluck( get_the_terms( $post, 'plugin_contributors' ), 'slug' ), true ) ) {
    619                                                 continue;
    620                                         }
    621 
    622                                         // everyone else can't.
     602                                        // Else hide it.
    623603                                        unset( $posts[ $i ] );
    624604                                }
    … …  
    802782                        case 'plugin_contributors':
    803783                        case 'plugin_committers':
    804                                 $user = get_user_by( 'slug', $term->slug );
     784                                $user = get_user_by( 'slug', $term->name );
    805785                                $name = $user->display_name;
    806786                                break;
    … …  
    888868                }
    889869
    890                 // TODO: Switch this to the capabilities systems, check if post_author should access
    891870                // Filter access to the plugin administration area. Only certain users are allowed access.
    892                 if ( get_query_var( 'plugin_admin' ) && ! current_user_can( 'plugin_review' ) ) {
    893                         $post = Plugin_Directory::get_plugin_post( get_query_var( 'name' ) );
    894                         if (
    895                                 // Logged out users can't access plugin admin
    896                                 ! is_user_logged_in() ||
    897                                 // Allow access to Committers OR Contributors.
    898                                 ! (
    899                                         // Committers can access plugin admin
    900                                         in_array( wp_get_current_user()->user_login, (array) Tools::get_plugin_committers( $post->post_name ), true ) ||
    901                                         // Contributors can access plugin admin (but will have a more limited access)
    902                                         in_array( wp_get_current_user()->user_nicename, (array) wp_list_pluck( get_the_terms( $post, 'plugin_contributors' ), 'slug' ), true )
    903                                 )
    904                         ) {
    905                                 wp_safe_redirect( get_permalink( $post ) );
    906                                 die();
    907                         }
     871                if ( get_query_var( 'plugin_admin' )  &&
     872                        ! current_user_can(
     873                                'plugin_admin_view',
     874                                $post = Plugin_Directory::get_plugin_post( get_query_var( 'name' ) )
     875                        )
     876                ) {
     877                        wp_safe_redirect( get_permalink( $post ) );
     878                        die();
    908879                }
    909880        }
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-plugins/functions.php

    r3834 r4260  
    8383        ) );
    8484
     85        if ( get_query_var( 'plugin_admin' ) ) {
     86                wp_enqueue_script( 'google-jsapi', 'https://www.google.com/jsapi', array(), false, true );
     87                wp_enqueue_script( 'wporg-plugins-stats', get_template_directory_uri() . '/js/stats.js', array( 'jquery', 'google-jsapi' ), '20161019', true );
     88
     89
     90                wp_localize_script( 'wporg-plugins-stats', 'pluginStats', array(
     91                        'slug' => is_singular( 'plugin' ) ? get_queried_object()->post_name : '',
     92                        'l10n' => array(
     93                                'date'      => __( 'Date', 'wporg-plugins' ),
     94                                'downloads' => __( 'Downloads', 'wporg-plugins' ),
     95                                'noData'    => __( 'No data yet', 'wporg-plugins' ),
     96                                'today'     => __( 'Today', 'wporg-plugins' ),
     97                                'yesterday' => __( 'Yesterday', 'wporg-plugins' ),
     98                                'last_week' => __( 'Last Week', 'wporg-plugins' ),
     99                                'all_time'  => __( 'All Time', 'wporg-plugins' ),
     100                        ),
     101                ) );
     102
     103        }
     104
    85105        if ( isset( $_REQUEST['react'] ) ) {
    86106                wp_enqueue_script( 'wporg-plugins-client', get_template_directory_uri() . '/js/theme.js', array(), false, true );
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-plugins/js/stats.js

    r4258 r4260  
    33( function( $, settings ) {
    44        $( function () {
    5                 jQuery.getJSON('https://api.wordpress.org/stats/plugin/1.0/downloads.php?slug=' + settings.slug + '&limit=267&callback=?', function( downloads ) {
     5                $.getJSON('https://api.wordpress.org/stats/plugin/1.0/downloads.php?slug=' + settings.slug + '&limit=267&callback=?', function( downloads ) {
    66                        var data = new google.visualization.DataTable(),
    77                                count = 0,
    … …  
    4545                                curveType: 'function'
    4646                        } );
     47                } );
     48
     49                $.getJSON('https://api.wordpress.org/stats/plugin/1.0/downloads.php?slug=' + settings.slug + '&historical_summary=1&callback=?', function( summary ) {
     50                        var $tbody = $('table#plugin-download-history-stats tbody:last-child');
     51
     52                        for ( var summary_field in summary ) {
     53                                if ( ! summary.hasOwnProperty( summary_field ) ) {
     54                                        continue;
     55                                }
     56
     57                                var text = settings.l10n[summary_field];
     58                                var count = parseInt(summary[ summary_field ]).toLocaleString();
     59
     60                                $newrow = $("<tr><th scope='row'></th><td>0</td></tr>");
     61                                $newrow.find( 'th' ).text( text );
     62                                $newrow.find( 'td' ).text( count );
     63                                $tbody.append( $newrow );
     64                        }
    4765                } );
    4866
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-plugins/template-parts/plugin-single.php

    r4224 r4260  
    8989                        <div>
    9090                                <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>
    91                                 <?php if ( true /* TODO: Logic on when to show the edit link */ ) : ?>
     91                                <?php if ( current_user_can( 'plugin_admin_view', get_post() ) ) : ?>
    9292                                        <br>
    93                                         <a class="plugin-edit" href="<?php echo esc_url( get_permalink() . 'admin/' ); ?>"><?php _e( 'Edit Plugin', 'wporg-plugins' ); ?></a>   
     93                                        <a class="plugin-edit" href="<?php echo esc_url( get_permalink() . 'admin/' ); ?>"><?php _e( 'Edit Plugin', 'wporg-plugins' ); ?></a>
    9494                                <?php endif; ?>
    9595                        </div>
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-plugins/template-parts/section-admin.php

    r4224 r4260  
     1<?php
     2/**
     3 * Template part for displaying the plugin administration sections.
     4 *
     5 * @link https://codex.wordpress.org/Template_Hierarchy
     6 *
     7 * @package WordPressdotorg\Plugin_Directory\Theme
     8 */
     9
     10namespace WordPressdotorg\Plugin_Directory\Theme;
     11use WordPressdotorg\Plugin_Directory\Plugin_Directory;
     12use WordPressdotorg\Plugin_Directory\Template;
     13?>
    114<div id="admin">
    2         <h2><?php _e( 'Plugin Admin', 'wporg-plugins' ); ?></h2>
     15        <h2><?php _e( 'Plugin Stats', 'wporg-plugins' ); ?></h2>
     16
     17        <h3><?php _e( 'Active versions', 'wporg-plugins' ); ?></h3>
     18        <div id="plugin-version-stats" class="chart"></div>
     19
     20        <h3><?php _e( 'Downloads Per Day', 'wporg-plugins' ); ?></h3>
     21        <div id="plugin-download-stats" class="chart"></div>
     22
     23        <h4><?php _e( 'Downloads history', 'wporg-plugins' ); ?></h4>
     24        <table id="plugin-download-history-stats"><tbody></tbody></table>
     25
     26
    327</div>
Note: See TracChangeset for help on using the changeset viewer.