Ticket #1438: co-author-plus.php.diff
| File co-author-plus.php.diff, 4.0 KB (added by kingkool68, 17 months ago) |
|---|
-
co-authors-plus/co-authors-plus.php
86 86 // Action to set users when a post is saved 87 87 add_action( 'save_post', array( $this, 'coauthors_update_post' ), 10, 2 ); 88 88 // Filter to set the post_author field when wp_insert_post is called 89 add_filter( 'wp_insert_post_data', array( $this, 'coauthors_set_post_author_field' ) ); 89 add_filter( 'wp_insert_post_data', array( $this, 'coauthors_set_post_author_field' ), 10, 2 ); 90 // Filter to ensure authors are retrieved basedo n their term order when a post is saved in bulk-edit mode. 91 add_filter( 'wp_get_object_terms', array( $this, 'coauthors_stop_bulk_edit' ), 10, 4 ); 90 92 91 93 // Action to reassign posts when a user is deleted 92 94 add_action( 'delete_user', array( $this, 'delete_user_action' ) ); … … 323 325 $count = 1; 324 326 foreach( $authors as $author ) : 325 327 ?> 326 <a href=" edit.php?author=<?php echo $author->ID; ?>"><?php echo $author->display_name?></a><?php echo ( $count < count( $authors ) ) ? ',' : ''; ?>328 <a href="<?php echo esc_url( get_admin_url( null, 'edit.php?author=' . $author->ID ) ); ?>"><?php echo esc_html( $author->display_name ); ?></a><?php echo ( $count < count( $authors ) ) ? ',' : ''; ?> 327 329 <?php 328 330 $count++; 329 331 endforeach; … … 385 387 /** 386 388 * Filters post data before saving to db to set post_author 387 389 */ 388 function coauthors_set_post_author_field( $data ) {390 function coauthors_set_post_author_field( $data, $postarr ) { 389 391 390 392 // Bail on autosave 391 393 if ( defined( 'DOING_AUTOSAVE' ) && !DOING_AUTOSAVE ) … … 396 398 return $data; 397 399 398 400 if( isset( $_REQUEST['coauthors-nonce'] ) && is_array( $_POST['coauthors'] ) ) { 399 $author = $_POST['coauthors'][0];401 $author = sanitize_key( $_POST['coauthors'][0] ); 400 402 if( $author ) { 401 403 $author_data = get_user_by( 'login', $author ); 402 404 $data['post_author'] = $author_data->ID; … … 409 411 } 410 412 } 411 413 414 global $postarr; 415 unset($postarr['tax_input']['author']); 416 412 417 return $data; 413 418 } 414 419 … … 420 425 function coauthors_update_post( $post_id, $post ) { 421 426 $post_type = $post->post_type; 422 427 423 if ( defined( 'DOING_AUTOSAVE' ) && !DOING_AUTOSAVE || $_REQUEST['bulk_edit'])428 if ( defined( 'DOING_AUTOSAVE' ) && !DOING_AUTOSAVE ) 424 429 return; 425 430 426 431 if( isset( $_POST['coauthors-nonce'] ) && isset( $_POST['coauthors'] ) ) { … … 428 433 429 434 if( $this->current_user_can_set_authors() ){ 430 435 $coauthors = (array) $_POST['coauthors']; 431 $coauthors = array_map( ' esc_html', $coauthors );436 $coauthors = array_map( 'sanitize_key', $coauthors ); 432 437 return $this->add_coauthors( $post_id, $coauthors ); 433 438 } 434 439 } 435 440 } 436 441 437 442 /** 443 * Saving a post in Bulk Edit mode flushes the order of the co-authors without this filter. 444 */ 445 function coauthors_stop_bulk_edit( $terms, $object_ids, $taxonomies, $args ) { 446 if( $_REQUEST['bulk_edit'] && $taxonomies == "'author'" ) { 447 global $wpdb; 448 $orderby = 'ORDER BY tr.term_order'; 449 $order = 'ASC'; 450 $query = "SELECT t.slug, t.term_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN $wpdb->term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND tr.object_id IN ($object_ids) $orderby $order"; 451 $raw_coauthors = $wpdb->get_results($query); 452 $terms = array(); 453 foreach( $raw_coauthors as $author ) { 454 $terms[] = (int) $author->term_id; 455 } 456 } 457 458 return $terms; 459 } 460 461 /** 438 462 * Add a user as coauthor for a post 439 463 */ 440 464 function add_coauthors( $post_id, $coauthors, $append = false ) { … … 574 598 if( ! $this->current_user_can_set_authors() ) 575 599 die(); 576 600 577 $search = esc_html( strtolower( $_REQUEST['q'] ) );601 $search = sanitize_text_field( strtolower( $_REQUEST['q'] ) ); 578 602 579 603 $authors = $this->search_authors( $search ); 580 604
