| 1 | <?php |
| 2 | /* |
| 3 | Plugin Name: P2 New Post Categories |
| 4 | Description: Adds a category dropdown to P2's new post form. |
| 5 | Version: 0.1 |
| 6 | License: GPLv2 or Later |
| 7 | */ |
| 8 | |
| 9 | /* |
| 10 | * Note: This has been forked from the original version to conform to WordPress.org conventions. |
| 11 | */ |
| 12 | |
| 13 | |
| 14 | class P2NewPostCategories { |
| 15 | protected $new_category; |
| 16 | const VERSION = '0.1'; |
| 17 | const REGEX_CATEGORY_IN_TAG_LIST = '/\[category=(.*)\]/i'; |
| 18 | |
| 19 | /* |
| 20 | * Register hook callbacks |
| 21 | */ |
| 22 | public function __construct() { |
| 23 | add_action( 'wp_head', array( $this, 'print_css' ) ); |
| 24 | add_action( 'wp_footer', array( $this, 'print_javascript' ) ); |
| 25 | |
| 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' ) ); |
| 29 | } |
| 30 | |
| 31 | /* |
| 32 | * Print our CSS |
| 33 | */ |
| 34 | public function print_css() { |
| 35 | ?> |
| 36 | |
| 37 | <!-- Begin P2 New Post Categories --> |
| 38 | <style type="text/css"> |
| 39 | #new_post { |
| 40 | position: relative; |
| 41 | padding-bottom: 2.25em; |
| 42 | } |
| 43 | |
| 44 | #p2-new-post-category { |
| 45 | position: absolute; |
| 46 | left: 0; |
| 47 | bottom: 0; |
| 48 | } |
| 49 | </style> |
| 50 | <!-- End P2 New Post Categories --> |
| 51 | |
| 52 | <?php |
| 53 | } |
| 54 | |
| 55 | /* |
| 56 | * Print our JavaScript |
| 57 | */ |
| 58 | public function print_javascript() { |
| 59 | ?> |
| 60 | |
| 61 | <!-- Begin P2 New Post Categories --> |
| 62 | <script type="text/javascript"> |
| 63 | ( function( $ ) { |
| 64 | |
| 65 | var P2NewPostCategories = { |
| 66 | |
| 67 | /* |
| 68 | * Initialization |
| 69 | */ |
| 70 | construct : function() { |
| 71 | $( '#p2-new-post-category' ).change( this.appendCategoryToTagList ); |
| 72 | }, |
| 73 | |
| 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. |
| 77 | */ |
| 78 | appendCategoryToTagList : function( event ) { |
| 79 | var optionalComma = '', |
| 80 | tags = $( '#tags' ).val(), |
| 81 | category = $( this ).children( 'option:selected' ).text(); |
| 82 | |
| 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 ); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | P2NewPostCategories.construct(); |
| 101 | |
| 102 | } )( jQuery ); |
| 103 | </script> |
| 104 | <!-- End P2 New Post Categories --> |
| 105 | |
| 106 | <?php |
| 107 | } |
| 108 | |
| 109 | public function add_new_post_category_dropdown() { |
| 110 | $params = apply_filters( 'p2npc_category_dropdown_params', array( 'id' => 'p2-new-post-category' ) ); |
| 111 | wp_dropdown_categories( $params ); |
| 112 | } |
| 113 | |
| 114 | /* |
| 115 | * Since we can't hook into p2.posts.submit() and add a category parameter to the $.ajax() call, |
| 116 | * we append the category to the tags list and then parse it out before P2 processes it. |
| 117 | */ |
| 118 | public function parse_new_post_category( $action ) { |
| 119 | if ( 'new_post' == $action ) { |
| 120 | if ( preg_match( self::REGEX_CATEGORY_IN_TAG_LIST, $_POST['tags'], $matches ) ) { |
| 121 | $this->new_category = get_term_by( 'name', $matches[1], 'category' ); |
| 122 | $_POST['tags'] = preg_replace( self::REGEX_CATEGORY_IN_TAG_LIST, '', $_POST['tags'] ); |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | * When the new post is being saved, we assign the category that we parsed out in parse_new_post_category() |
| 129 | */ |
| 130 | public function assign_new_post_to_category( $post_id ) { |
| 131 | if ( isset( $this->new_category->term_id ) && $this->new_category->term_id ) { |
| 132 | wp_set_object_terms( $post_id, $this->new_category->slug, 'category' ); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | } // end P2NewPostCategories |
| 137 | |
| 138 | $GLOBALS['P2NewPostCategories'] = new P2NewPostCategories(); |