Making WordPress.org


Ignore:
Timestamp:
06/30/2016 09:06:52 PM (10 years ago)
Author:
coffee2code
Message:

developer.wordpress.org: Add explanation editor role.

Props DrewAPicture.
See #1781.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/inc/explanations.php

    r2372 r3607  
    3737
    3838                // Setup.
    39                 add_action( 'init',                    array( $this, 'register_post_type'     ), 0   );
    40                 add_action( 'init',                    array( $this, 'remove_editor_support'  ), 100 );
    41                 add_action( 'edit_form_after_title',   array( $this, 'post_to_expl_controls'  )      );
    42                 add_action( 'edit_form_top',           array( $this, 'expl_to_post_controls'  )      );
    43                 add_action( 'admin_bar_menu',          array( $this, 'toolbar_edit_link'      ), 100 );
     39                add_action( 'init',                    array( $this, 'register_post_type'     ), 0     );
     40                add_action( 'init',                    array( $this, 'remove_editor_support'  ), 100   );
     41                add_action( 'edit_form_after_title',   array( $this, 'post_to_expl_controls'  )        );
     42                add_action( 'edit_form_top',           array( $this, 'expl_to_post_controls'  )        );
     43                add_action( 'admin_bar_menu',          array( $this, 'toolbar_edit_link'      ), 100   );
     44
     45                // Permissions.
     46                add_action( 'after_switch_theme',      array( $this, 'add_roles'              )        );
     47                add_filter( 'user_has_cap',            array( $this, 'grant_caps'             )        );
     48                add_filter( 'post_row_actions',        array( $this, 'expl_row_action'        ), 10, 2 );
    4449
    4550                // Script and styles.
    46                 add_action( 'admin_enqueue_scripts',   array( $this, 'admin_enqueue_scripts'  )      );
     51                add_action( 'admin_enqueue_scripts',   array( $this, 'admin_enqueue_scripts'  )        );
    4752
    4853                // AJAX.
    49                 add_action( 'wp_ajax_new_explanation', array( $this, 'new_explanation'        )      );
    50                 add_action( 'wp_ajax_un_publish',      array( $this, 'un_publish_explanation' )      );
     54                add_action( 'wp_ajax_new_explanation', array( $this, 'new_explanation'        )        );
     55                add_action( 'wp_ajax_un_publish',      array( $this, 'un_publish_explanation' )        );
    5156
    5257                // Content tweaks.
     
    7681                        'show_in_menu'      => false,
    7782                        'show_in_nav_menus' => false,
     83                        'capability_type'   => 'explanation',
     84                        'map_meta_cap'      => true,
    7885                        'supports'          => array( 'editor', 'revisions' ),
    7986                        'rewrite'           => false,
     
    154161                        return;
    155162                }
    156 
    157                 $parent = is_a( $post, 'WP_Post' ) ? $post->post_parent : get_post( $post )->post_parent;
    158 
    159                 if ( 0 !== $parent ) :
    160                         $prefix = '<strong>' . __( 'Associated with: ', 'wporg' ) . '</strong>';
    161                         ?>
    162                         <div class="postbox-container" style="margin-top:20px;width:100%;">
    163                                 <div class="postbox">
    164                                         <div class="inside" style="padding-bottom:0;">
    165                                                 <?php edit_post_link( get_the_title( $parent ), $prefix, '', $parent ); ?>
    166                                         </div>
     163                ?>
     164                <div class="postbox-container" style="margin-top:20px;width:100%;">
     165                        <div class="postbox">
     166                                <div class="inside" style="padding-bottom:0;">
     167                                        <strong><?php _e( 'Associated with: ', 'wporg' ); ?></strong>
     168                                        <?php
     169                                        // Edit link if the current user can edit, otherwise view link.
     170                                        if ( current_user_can( 'edit_post', $post->post_parent ) ) :
     171                                                edit_post_link( get_the_title( $post->post_parent ), '', '', $post->post_parent );
     172                                        else :
     173                                                printf( '<a href="%1$s">%2$s</a>',
     174                                                        esc_url( get_permalink( $post->post_parent ) ),
     175                                                        str_replace( 'Explanation: ', '', get_the_title( $post->post_parent ) )
     176                                                );
     177                                        endif;
     178                                        ?>
    167179                                </div>
    168180                        </div>
     181                </div>
    169182                <?php
    170                 endif;
    171183        }
    172184
     
    202214
    203215        /**
     216         * Adds the 'Explanation Editor' role.
     217         *
     218         * @access public
     219         */
     220        public function add_roles() {
     221                add_role(
     222                        'expl_editor',
     223                        __( 'Explanation Editor', 'wporg' ),
     224                        array(
     225                                'unfiltered_html'             => true,
     226                                'read'                        => true,
     227                                'edit_explanations'           => true,
     228                                'edit_others_explanations'    => true,
     229                                'edit_published_explanations' => true,
     230                                'edit_private_explanations'   => true,
     231                                'read_private_explanations'   => true,
     232                        )
     233                );
     234        }
     235
     236        /**
     237         * Grants explanation capabilities to users.
     238         *
     239         * @access public
     240         *
     241         * @param array $caps Capabilities.
     242         * @return array Modified capabilities array.
     243         */
     244        public function grant_caps( $caps ) {
     245
     246                if ( ! is_user_member_of_blog() ) {
     247                        return $caps;
     248                }
     249
     250                $role = wp_get_current_user()->roles[0];
     251
     252                // Only grant explanation post type caps for admins, editors, and explanation editors.
     253                if ( in_array( $role, array( 'administrator', 'editor', 'expl_editor' ) ) ) {
     254                        $base_caps = array(
     255                                'edit_explanations', 'edit_others_explanations',
     256                                'edit_published_explanations', 'edit_posts'
     257                        );
     258
     259                        foreach ( $base_caps as $cap ) {
     260                                $caps[ $cap ] = true;
     261                        }
     262
     263                        $editor_caps = array(
     264                                'publish_explanations',
     265                                'delete_explanations', 'delete_others_explanations',
     266                                'delete_published_explanations', 'delete_private_explanations',
     267                                'edit_private_explanations', 'read_private_explanations'
     268                        );
     269
     270                        if ( ! empty( $caps['edit_pages'] ) ) {
     271                                foreach ( $editor_caps as $cap ) {
     272                                        $caps[ $cap ] = true;
     273                                }
     274                        }
     275                }
     276
     277                return $caps;
     278        }
     279
     280        /**
     281         * Adds the 'Add/Edit Explanation' row actions to the parsed post type list tables.
     282         *
     283         * @access public
     284         *
     285         * @param array    $actions Row actions.
     286         * @param \WP_Post $post    Parsed post object.
     287         * @return array (Maybe) filtered row actions.
     288         */
     289        public function expl_row_action( $actions, $post ) {
     290                if ( ! in_array( $post->post_type, \DevHub\get_parsed_post_types() ) ) {
     291                        return $actions;
     292                }
     293
     294                $expl = \DevHub\get_explanation( $post );
     295
     296                $expl_action = array();
     297
     298                if ( $expl ) {
     299                        if ( ! current_user_can( 'edit_posts', $expl->ID ) ) {
     300                                return $actions;
     301                        }
     302
     303                        $expl_action['edit-expl'] = sprintf( '<a href="%1$s" alt="%2$s">%3$s</a>',
     304                                esc_url( get_edit_post_link( $expl->ID ) ),
     305                                esc_attr__( 'Edit Explanation', 'wporg' ),
     306                                __( 'Edit Explanation', 'wporg' )
     307                        );
     308                } else {
     309                        $expl_action['add-expl'] = sprintf( '<a href="" class="create-expl" data-nonce="%1$s" data-id="%2$s">%3$s</a>',
     310                                esc_attr( wp_create_nonce( 'create-expl' ) ),
     311                                esc_attr( $post->ID ),
     312                                __( 'Add Explanation', 'wporg' )
     313                        );
     314                }
     315
     316                return array_merge( $expl_action, $actions );
     317        }
     318
     319        /**
    204320         * Output the Explanation status controls.
    205321         *
     
    216332                        <span id="expl-row-actions" class="expl-row-actions">
    217333                                <a id="edit-expl" href="<?php echo get_edit_post_link( $explanation->ID ); ?>">
    218                                         <?php _e( 'Edit Content', 'wporg' ); ?>
     334                                        <?php _e( 'Edit Explanation', 'wporg' ); ?>
    219335                                </a>
    220336                                <?php if ( 'publish' == get_post_status( $explanation ) ) : ?>
    221337                                        <a href="#unpublish" id="unpublish-expl" data-nonce="<?php echo wp_create_nonce( 'unpublish-expl' ); ?>" data-id="<?php the_ID(); ?>">
    222                                                 <?php _e( 'Un-publish', 'wporg' ); ?>
     338                                                <?php _e( 'Unpublish', 'wporg' ); ?>
    223339                                        </a>
    224340                                <?php endif; ?>
     
    250366                switch( $status = $post->post_status ) {
    251367                        case 'draft' :
    252                                 $label = __( 'Drafted', 'wporg' );
     368                                $label = __( 'Draft', 'wporg' );
    253369                                break;
    254370                        case 'pending' :
     
    274390        public function admin_enqueue_scripts() {
    275391
     392                $parsed_post_types = array();
     393
     394                foreach ( \DevHub\get_parsed_post_types() as $post_type ) {
     395                        $parsed_post_types[] = $post_type;
     396                        $parsed_post_types[] = "edit-{$post_type}";
     397                }
     398
    276399                if ( in_array( get_current_screen()->id, array_merge(
    277                                 DevHub\get_parsed_post_types(),
     400                                $parsed_post_types,
    278401                                array( 'wporg_explanations', 'edit-wporg_explanations' )
    279402                ) ) ) {
     
    282405
    283406                        wp_localize_script( 'wporg-explanations', 'wporg', array(
    284                                 'editContentLabel' => __( 'Edit Content', 'wporg' ),
     407                                'editContentLabel' => __( 'Edit Explanation', 'wporg' ),
    285408                                'statusLabel'      => array(
    286                                         'draft'        => __( 'Drafted', 'wporg' ),
     409                                        'draft'        => __( 'Draft', 'wporg' ),
    287410                                        'pending'      => __( 'Pending Review', 'wporg' ),
    288411                                        'publish'      => __( 'Published', 'wporg' ),
     
    301424
    302425                $post_id = empty( $_REQUEST['post_id'] ) ? 0 : absint( $_REQUEST['post_id'] );
     426                $context = empty( $_REQUEST['context'] ) ? '' : sanitize_text_field( $_REQUEST['context'] );
    303427
    304428                if ( DevHub\get_explanation( $post_id ) ) {
     
    316440                        if ( ! is_wp_error( $explanation ) && 0 !== $explanation ) {
    317441                                wp_send_json_success( array(
    318                                         'post_id' => $explanation
     442                                        'post_id'   => $explanation,
     443                                        'parent_id' => $post_id,
     444                                        'context'   => $context
    319445                                ) );
    320446                        } else {
Note: See TracChangeset for help on using the changeset viewer.