Ticket #187: 187-p2-new-post-categories.4.diff
File 187-p2-new-post-categories.4.diff, 5.3 KB (added by , 11 years ago) |
---|
-
sites/trunk/wordpress.org/public_html/wp-content/plugins/p2-new-post-categories/p2-new-post-categories.php
2 2 /* 3 3 Plugin Name: P2 New Post Categories 4 4 Description: Adds a category dropdown to P2s new post form. 5 Version: 0. 15 Version: 0.2 6 6 License: GPLv2 or Later 7 7 */ 8 8 … … 12 12 13 13 14 14 class P2NewPostCategories { 15 protected $new_category; 16 const VERSION = '0.1'; 17 const REGEX_CATEGORY_IN_TAG_LIST = '/\[category=(.*)\]/i'; 15 const VERSION = '0.2'; 18 16 19 17 /* 20 18 * Register hook callbacks 21 19 */ 22 20 public function __construct() { 23 add_action( 'wp_head', array( $this, 'print_css' ) );24 add_action( 'wp_footer', array( $this, 'print_javascript' ) );21 add_action( 'wp_head', array( $this, 'print_css' ) ); 22 add_action( 'wp_footer', array( $this, 'print_javascript' ) ); 25 23 26 add_action( 'p2_post_form', array( $this, 'add_new_post_category_dropdown' ) );27 add_action( ' p2_ajax', array( $this, 'parse_new_post_category' ) );28 add_action( 'wp_ insert_post', array( $this, 'assign_new_post_to_category' ) );24 add_action( 'p2_post_form', array( $this, 'add_new_post_category_dropdown' ) ); 25 add_action( 'wp_ajax_p2npc_assign_category', array( $this, 'assign_category_to_post' ) ); 26 add_action( 'wp_ajax_nopriv_p2npc_assign_category', array( $this, 'assign_category_to_post' ) ); 29 27 } 30 28 31 29 /* … … 68 66 * Initialization 69 67 */ 70 68 construct : function() { 71 $( '#p2-new-post-category' ).change( this.appendCategoryToTagList ); 69 P2NewPostCategories.dropdown = $( '#p2-new-post-category' ); 70 P2NewPostCategories.dropdown_default = P2NewPostCategories.dropdown.val(); 71 72 $( document ).on( 'p2_new_post_submit_success', P2NewPostCategories.new_post ); 72 73 }, 73 74 74 /* 75 * Adds the name of the selected category to the #tags input field 76 * See the comments in parse_new_post_category() for details. 75 /** 76 * Assign the selected category to the new post 77 * @param object event 78 * @param object data 77 79 */ 78 appendCategoryToTagList : function( event ) { 79 var optionalComma = '', 80 tags = $( '#tags' ).val(), 81 category = $( this ).children( 'option:selected' ).text(); 80 new_post : function( event, data ) { 81 $.post( 82 ajaxUrl.replace( '?p2ajax=true', '' ), { 83 'action' : 'p2npc_assign_category', 84 'post_id' : parseInt( data.post_id ), 85 'category_id' : parseInt( P2NewPostCategories.dropdown.val() ), 86 'p2npc_assign_category_nonce' : $( '#p2npc_assign_category_nonce' ).val() 87 } 88 ); 82 89 83 tags = tags.replace( /Tag it,?/, '' ).replace( /,?\[category=(.*)\]/, '' ); 84 if ( tags.length > 0 && ',' != tags.slice( -1 ) ) { 85 optionalComma = ','; 86 } 87 88 if ( 'Uncategorized' != category ) { 89 tags += optionalComma + '[category=' + category + ']'; 90 } 91 92 if ( ',' == tags.substring( 0, 1 ) ) { 93 tags = tags.substring( 1, tags.length ); 94 } 95 96 $( '#tags' ).val( tags ); 90 P2NewPostCategories.dropdown.val( parseInt( P2NewPostCategories.dropdown_default ) ).change(); 97 91 } 98 } 92 }; 99 93 100 94 P2NewPostCategories.construct(); 101 95 … … 115 109 ) ); 116 110 117 111 wp_dropdown_categories( $params ); 112 wp_nonce_field( 'p2npc_assign_category', 'p2npc_assign_category_nonce' ); 118 113 } 119 114 120 115 /* 121 * Since we can't hook into p2.posts.submit() and add a category parameter to the $.ajax() call,122 * we append the category to the tags list and then parse it out before P2 processes it.116 * Assign a category to a post 117 * This is an AJAX handler. 123 118 */ 124 public function parse_new_post_category( $action ) { 125 if ( 'new_post' == $action ) { 126 if ( preg_match( self::REGEX_CATEGORY_IN_TAG_LIST, $_POST['tags'], $matches ) ) { 127 $this->new_category = get_term_by( 'name', $matches[1], 'category' ); 128 $_POST['tags'] = preg_replace( self::REGEX_CATEGORY_IN_TAG_LIST, '', $_POST['tags'] ); 129 } 119 public function assign_category_to_post() { 120 $assigned = false; 121 $post_id = absint( $_REQUEST['post_id'] ); 122 $category_id = absint( $_REQUEST['category_id'] ); 123 124 check_ajax_referer( 'p2npc_assign_category', 'p2npc_assign_category_nonce' ); 125 126 if ( current_user_can( 'edit_post', $post_id ) ) { 127 $assigned = wp_set_object_terms( $post_id, $category_id, 'category' ); 128 $assigned = is_array( $assigned ) && ! empty( $assigned ); 130 129 } 130 131 wp_die( $assigned ); 131 132 } 132 133 133 /*134 * When the new post is being saved, we assign the category that we parsed out in parse_new_post_category()135 */136 public function assign_new_post_to_category( $post_id ) {137 if ( isset( $this->new_category->term_id ) && $this->new_category->term_id ) {138 wp_set_object_terms( $post_id, $this->new_category->slug, 'category' );139 }140 }141 142 134 } // end P2NewPostCategories 143 135 144 136 $GLOBALS['P2NewPostCategories'] = new P2NewPostCategories();