Making WordPress.org


Ignore:
Timestamp:
11/14/2013 07:44:26 PM (11 years ago)
Author:
Otto42
Message:

New version of p2 plugin for make/community. props iandunn. see #187

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/p2-new-post-categories/p2-new-post-categories.php

    r110 r118  
    33Plugin Name: P2 New Post Categories
    44Description: Adds a category dropdown to P2s new post form.
    5 Version:     0.1
     5Version:     0.2
    66License:     GPLv2 or Later
    77*/
     
    1313
    1414class P2NewPostCategories {
    15     protected $new_category;
    16     const VERSION = '0.1';
    17     const REGEX_CATEGORY_IN_TAG_LIST = '/\[category=(.*)\]/i';
     15    const VERSION = '0.2';
    1816
    1917    /*
     
    2119     */
    2220    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' ) );
    2523
    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' ) );
    2927    }
    3028
     
    6967                     */
    7068                    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 );
    7273                    },
    7374
    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
    7779                     */
    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                        );
    8289
    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();
    9791                    }
    98                 }
     92                };
    9993
    10094                P2NewPostCategories.construct();
     
    116110
    117111        wp_dropdown_categories( $params );
     112        wp_nonce_field( 'p2npc_assign_category', 'p2npc_assign_category_nonce' );
    118113    }
    119114
    120115    /*
    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.
    123118     */
    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 );
    130129        }
    131     }
    132 
    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         }
     130       
     131        wp_die( $assigned );
    140132    }
    141133
Note: See TracChangeset for help on using the changeset viewer.