| 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( |
| 111 | 'orderby' => 'name', |
| 112 | 'id' => 'p2-new-post-category', |
| 113 | 'hide_empty' => false, |
| 114 | ) ); |
| 115 | |
| 116 | wp_dropdown_categories( $params ); |
| 117 | } |
| 118 | |
| 119 | /* |
| 120 | * Since we can't hook into p2.posts.submit() and add a category parameter to the $.ajax() call, |
| 121 | * we append the category to the tags list and then parse it out before P2 processes it. |
| 122 | */ |
| 123 | public function parse_new_post_category( $action ) { |
| 124 | if ( 'new_post' == $action ) { |
| 125 | if ( preg_match( self::REGEX_CATEGORY_IN_TAG_LIST, $_POST['tags'], $matches ) ) { |
| 126 | $this->new_category = get_term_by( 'name', $matches[1], 'category' ); |
| 127 | $_POST['tags'] = preg_replace( self::REGEX_CATEGORY_IN_TAG_LIST, '', $_POST['tags'] ); |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /* |
| 133 | * When the new post is being saved, we assign the category that we parsed out in parse_new_post_category() |
| 134 | */ |
| 135 | public function assign_new_post_to_category( $post_id ) { |
| 136 | if ( isset( $this->new_category->term_id ) && $this->new_category->term_id ) { |
| 137 | wp_set_object_terms( $post_id, $this->new_category->slug, 'category' ); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | } // end P2NewPostCategories |
| 142 | |
| 143 | $GLOBALS['P2NewPostCategories'] = new P2NewPostCategories(); |