Making WordPress.org


Ignore:
Timestamp:
03/22/2016 06:21:26 AM (9 years ago)
Author:
dd32
Message:

Plugin Directory: Introduce the Plugin Committer/Review & Admin roles. Use the capabilities throughout the core flows of the admin.
This also has a few hacks to make plugin committers/authors only see plugins which they can manage, although a few core bugs remain which cause non-owner committers not to be able to edit plugins properly.

See #1571

File:
1 edited

Legend:

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

    r2776 r2777  
    22namespace WordPressdotorg\Plugin_Directory\Admin;
    33use \WordPressdotorg\Plugin_Directory;
     4use \WordPressdotorg\Plugin_Directory\Tools;
    45use \WordPressdotorg\Plugin_Directory\Admin\List_Table\Plugin_Posts;
    56
     
    2829        add_action( 'do_meta_boxes', array( $this, 'replace_title_global' ) );
    2930
     31        add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ) );
    3032        add_action( 'save_post_plugin', array( $this, 'save_plugin_post' ), 10, 2 );
     33        add_filter( 'views_edit-plugin', array( $this, 'list_table_views' ) );
    3134
    3235        add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
     
    4043        add_filter( 'wp_ajax_add-committer',    array( __NAMESPACE__ . '\Metabox\Committers', 'add_committer'    ) );
    4144        add_filter( 'wp_ajax_delete-committer', array( __NAMESPACE__ . '\Metabox\Committers', 'remove_committer' ) );
     45        add_action( 'admin_menu', array( $this, 'admin_menu' ) );
     46
    4247    }
    4348
     
    8287            }
    8388        }
     89    }
     90
     91    public function admin_menu() {
     92        // WordPress requires that the plugin post_type have at least one submenu accessible *other* than itself.
     93        // If it doesn't have at least one submenu then users who cannot also publish posts will not be able to access the post type.
     94        add_submenu_page( 'edit.php?post_type=plugin', 'Plugin Handbook', 'Plugin Handbook', 'read', 'handbook', function() {} );
     95        add_submenu_page( 'edit.php?post_type=plugin', 'Readme Validator', 'Readme Validator', 'read', 'readme_validator', function() {} );
     96
     97        remove_menu_page( 'index.php' );
     98        remove_menu_page( 'profile.php' );
     99    }
     100
     101    /**
     102     * Filter the query in wp-admin to list only
     103     */
     104    public function pre_get_posts( $query ) {
     105        global $wpdb;
     106        if ( ! $query->is_main_query() ) {
     107            return;
     108        }
     109
     110        if ( ! current_user_can( 'plugin_edit_others' ) || ( isset( $query->query['author'] ) && $query->query['author'] == get_current_user_id() ) ) {
     111            $query->query_vars['author'] = get_current_user_id();
     112            $plugins = Tools::get_users_write_access_plugins( get_current_user_id() );
     113            if ( $plugins ) {
     114                $query->query_vars['post_name__in'] = $plugins;
     115                add_filter( 'posts_where', array( $this, 'pre_get_posts_sql_name_or_user' ) );
     116            }
     117        }
     118    }
     119
     120    /**
     121     * Custom callback for pre_get_posts to use an OR query between post_name & post_author
     122     *
     123     * @ignore
     124     */
     125    public function pre_get_posts_sql_name_or_user( $where ) {
     126        remove_filter( 'posts_where', array( $this, 'pre_get_posts_sql_name_or_user' ) );
     127
     128        // Replace `post_name IN(..) AND post_author IN (..)`
     129        // With `( post_name IN() OR post_author IN() )`
     130
     131        $where = preg_replace( "!\s(\S+\.post_name IN .+?)\s*AND\s*(\s\S+\.post_author.+?)AND!i", ' ( $1 OR $2 ) AND', $where );
     132        return $where;
     133    }
     134
     135    public function list_table_views( $views ) {
     136        global $wp_query;
     137        if ( current_user_can( 'plugin_edit_others' ) ) {
     138            return $views;
     139        }
     140        // The only view the user needs, is their own.
     141        return array(
     142            sprintf(
     143                '<a href="#" class="current">%s</a>',
     144                sprintf(
     145                    _nx(
     146                        'Mine <span class="count">(%s)</span>',
     147                        'Mine <span class="count">(%s)</span>',
     148                        $wp_query->found_posts,
     149                        'posts',
     150                        'wporg-posts'
     151                    ),
     152                    number_format_i18n( $wp_query->found_posts )
     153                )
     154            )
     155        );
    84156    }
    85157
Note: See TracChangeset for help on using the changeset viewer.