Ticket #1781: 1781.diff
File 1781.diff, 11.0 KB (added by , 9 years ago) |
---|
-
inc/explanations.php
36 36 $this->post_types = DevHub\get_parsed_post_types(); 37 37 38 38 // 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 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 ); 49 45 50 // 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' ) ); 47 52 48 53 // 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' ) ); 51 56 52 57 // Content tweaks. 53 58 add_filter( 'syntaxhighlighter_precode', 'html_entity_decode' ); … … 75 80 'show_ui' => true, 76 81 'show_in_menu' => false, 77 82 'show_in_nav_menus' => false, 83 'capability_type' => 'explanation', 84 'map_meta_cap' => true, 78 85 'supports' => array( 'editor', 'revisions' ), 79 86 'rewrite' => false, 80 87 'query_var' => false, … … 153 160 if ( $this->exp_post_type !== $post->post_type ) { 154 161 return; 155 162 } 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 ?> 167 179 </div> 168 180 </div> 181 </div> 169 182 <?php 170 endif;171 183 } 172 184 173 185 /** … … 201 213 } 202 214 203 215 /** 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 /** 204 320 * Output the Explanation status controls. 205 321 * 206 322 * @access public … … 215 331 ?> 216 332 <span id="expl-row-actions" class="expl-row-actions"> 217 333 <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' ); ?> 219 335 </a> 220 336 <?php if ( 'publish' == get_post_status( $explanation ) ) : ?> 221 337 <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' ); ?> 223 339 </a> 224 340 <?php endif; ?> 225 341 </span><!-- .expl-row-actions --> … … 249 365 250 366 switch( $status = $post->post_status ) { 251 367 case 'draft' : 252 $label = __( 'Draft ed', 'wporg' );368 $label = __( 'Draft', 'wporg' ); 253 369 break; 254 370 case 'pending' : 255 371 $label = __( 'Pending Review', 'wporg' ); … … 273 389 */ 274 390 public function admin_enqueue_scripts() { 275 391 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 276 399 if ( in_array( get_current_screen()->id, array_merge( 277 DevHub\get_parsed_post_types(),400 $parsed_post_types, 278 401 array( 'wporg_explanations', 'edit-wporg_explanations' ) 279 402 ) ) ) { 280 403 wp_enqueue_style( 'wporg-admin', get_template_directory_uri() . '/stylesheets/admin.css', array(), '20141218' ); … … 281 404 wp_enqueue_script( 'wporg-explanations', get_template_directory_uri() . '/js/explanations.js', array( 'jquery', 'wp-util' ), '20141218', true ); 282 405 283 406 wp_localize_script( 'wporg-explanations', 'wporg', array( 284 'editContentLabel' => __( 'Edit Content', 'wporg' ),407 'editContentLabel' => __( 'Edit Explanation', 'wporg' ), 285 408 'statusLabel' => array( 286 'draft' => __( 'Draft ed', 'wporg' ),409 'draft' => __( 'Draft', 'wporg' ), 287 410 'pending' => __( 'Pending Review', 'wporg' ), 288 411 'publish' => __( 'Published', 'wporg' ), 289 412 ), … … 300 423 check_ajax_referer( 'create-expl', 'nonce' ); 301 424 302 425 $post_id = empty( $_REQUEST['post_id'] ) ? 0 : absint( $_REQUEST['post_id'] ); 426 $context = empty( $_REQUEST['context'] ) ? '' : sanitize_text_field( $_REQUEST['context'] ); 303 427 304 428 if ( DevHub\get_explanation( $post_id ) ) { 305 429 wp_send_json_error( new WP_Error( 'post_exists', __( 'Explanation already exists.', 'wporg' ) ) ); … … 315 439 316 440 if ( ! is_wp_error( $explanation ) && 0 !== $explanation ) { 317 441 wp_send_json_success( array( 318 'post_id' => $explanation 442 'post_id' => $explanation, 443 'parent_id' => $post_id, 444 'context' => $context 319 445 ) ); 320 446 } else { 321 447 wp_send_json_error( -
js/explanations.js
13 13 unPublishLink = $( '#unpublish-expl' ), 14 14 rowActions = $( '#expl-row-actions' ); 15 15 16 var rowCreateLink = $( '.create-expl' ); 17 16 18 /** 17 19 * AJAX handler for creating and associating a new explanation post. 18 20 * … … 26 28 error: createExplError, 27 29 data: { 28 30 nonce: $( this ).data( 'nonce' ), 29 post_id: $( this ).data( 'id' ) 31 post_id: $( this ).data( 'id' ), 32 context: event.data.context 30 33 } 31 34 } ); 32 35 } … … 37 40 * @param {object} data Data response object. 38 41 */ 39 42 function createExplSuccess( data ) { 40 createLink.hide(); 41 rowActions.html( '<a href="post.php?post=' + data.post_id + '&action=edit">' + wporg.editContentLabel + '</a>' ); 42 statusLabel.text( wporg.statusLabel.draft ); 43 var editLink = '<a href="post.php?post=' + data.post_id + '&action=edit">' + wporg.editContentLabel + '</a>'; 44 45 if ( 'edit' == data.context ) { 46 // Action in the parsed post type edit screen. 47 createLink.hide(); 48 rowActions.html( editLink ); 49 statusLabel.text( wporg.statusLabel.draft ); 50 } else { 51 // Row link in the list table. 52 var rowLink = $( '#post-' + data.parent_id + ' .add-expl' ).html( editLink + ' | ' ); 53 } 43 54 } 44 55 45 56 /** … … 87 98 function unPublishError( data ) {} 88 99 89 100 // Events. 90 $( '#create-expl' ).on( 'click', createExplanation ); 91 $( '#unpublish-expl' ).on( 'click', unPublishExplantaion ); 101 createLink.on( 'click', { context: 'edit' }, createExplanation ); 102 rowCreateLink.on( 'click', { context: 'list' }, createExplanation ); 103 unPublishLink.on( 'click', unPublishExplantaion ); 92 104 93 105 } )( jQuery );