Making WordPress.org


Ignore:
Timestamp:
03/11/2016 06:04:19 PM (10 years ago)
Author:
obenland
Message:

Plugin Directory: Use a custom comment type for internal notes.

See #1570, #1603.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/admin/class-customizations.php

    r2659 r2735  
    1111
    1212    /**
    13      * Fetch the instance of the Admin Customizations class.
     13     * Fetch the instance of the Customizations class.
    1414     */
    1515    public static function instance() {
     
    1919    }
    2020
     21    /**
     22     * Constructor.
     23     */
    2124    private function __construct() {
    2225        // Admin Metaboxes
     
    2730
    2831        add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
    29         add_action( 'wp_ajax_save-note', array( $this, 'save_note' ) );
     32        add_action( 'wp_ajax_replyto-comment', array( $this, 'save_custom_comment' ), 0 );
     33
     34        add_filter( 'postbox_classes_plugin_internal-notes', array( $this, 'postbox_classes' ) );
    3035    }
    3136
     
    3338     * Adds the plugin name into the post editing title.
    3439     *
    35      * @global $title The wp-admin title variable.
     40     * @global string $title The wp-admin title variable.
    3641     *
    3742     * @param string $post_type The post type of the current page
     
    4045    public function replace_title_global( $post_type ) {
    4146        global $title;
     47
    4248        if ( 'plugin' === $post_type ) {
    4349            $title = sprintf( $title, get_the_title() ); // esc_html() on output
     
    7985
    8086        add_meta_box(
    81             'plugin-notes',
     87            'internal-notes',
    8288            __( 'Internal Notes', 'wporg-plugins' ),
    83             array( __NAMESPACE__ . '\Metabox\Notes', 'display' ),
     89            array( __NAMESPACE__ . '\Metabox\Internal_Notes', 'display' ),
    8490            'plugin', 'normal', 'high'
    8591        );
     
    127133
    128134    /**
    129      * Saves a plugin note.
    130      */
    131     public function save_note() {
    132         check_admin_referer( 'save-note', 'notce' );
    133 
    134         if ( empty( $_POST['id'] ) ) {
    135             wp_send_json_error( array( 'errorCode' => 'no_post_specified' ) );
    136         }
    137 
    138         if ( ! current_user_can( 'review_plugin', absint( $_POST['id'] ) ) ) {
    139             wp_send_json_error( array(
    140                 'error' => __( 'You do not have sufficient permissions to edit notes on this site.' ),
    141             ) );
    142         }
    143 
    144         update_post_meta( absint( $_POST['id'] ), 'note', wp_kses_post( $_POST['note'] ) );
    145 
    146         wp_send_json_success( array(
    147             'note' => wpautop( wp_kses_post( $_POST['note'] ) ),
    148         ) );
     135     * Filters the postbox classes for custom comment meta boxes.
     136     *
     137     * @param array $classes An array of postbox classes.
     138     * @return array
     139     */
     140    public function postbox_classes( $classes ) {
     141        $classes[] = 'comments-meta-box';
     142
     143        return array_filter( $classes );
     144    }
     145
     146    /**
     147     * Saves a comment that is not built-in.
     148     *
     149     * We pretty much have to replicate all of `wp_ajax_replyto_comment()` to be able to comment on draft posts.
     150     */
     151    public function save_custom_comment() {
     152        $comment_post_ID = (int) $_POST['comment_post_ID'];
     153        $post            = get_post( $comment_post_ID );
     154
     155        if ( 'plugin' !== $post->post_type ) {
     156            return;
     157        }
     158        remove_action( 'wp_ajax_replyto-comment', 'wp_ajax_replyto_comment', 1 );
     159
     160        global $wp_list_table;
     161        if ( empty( $action ) ) {
     162            $action = 'replyto-comment';
     163        }
     164
     165        check_ajax_referer( $action, '_ajax_nonce-replyto-comment' );
     166
     167        if ( ! $post ) {
     168            wp_die( - 1 );
     169        }
     170
     171        if ( ! current_user_can( 'edit_post', $comment_post_ID ) ) {
     172            wp_die( - 1 );
     173        }
     174
     175        if ( empty( $post->post_status ) ) {
     176            wp_die( 1 );
     177        }
     178
     179        $user = wp_get_current_user();
     180        if ( ! $user->exists() ) {
     181            wp_die( __( 'Sorry, you must be logged in to reply to a comment.' ) );
     182        }
     183
     184        $user_ID              = $user->ID;
     185        $comment_author       = wp_slash( $user->display_name );
     186        $comment_author_email = wp_slash( $user->user_email );
     187        $comment_author_url   = wp_slash( $user->user_url );
     188        $comment_content      = trim( $_POST['content'] );
     189        $comment_type         = isset( $_POST['comment_type'] ) ? trim( $_POST['comment_type'] ) : '';
     190
     191        if ( current_user_can( 'unfiltered_html' ) ) {
     192            if ( ! isset( $_POST['_wp_unfiltered_html_comment'] ) ) {
     193                $_POST['_wp_unfiltered_html_comment'] = '';
     194            }
     195
     196            if ( wp_create_nonce( 'unfiltered-html-comment' ) != $_POST['_wp_unfiltered_html_comment'] ) {
     197                kses_remove_filters(); // start with a clean slate
     198                kses_init_filters(); // set up the filters
     199            }
     200        }
     201
     202        if ( '' == $comment_content ) {
     203            wp_die( __( 'ERROR: please type a comment.' ) );
     204        }
     205
     206        $comment_parent = 0;
     207        if ( isset( $_POST['comment_ID'] ) ) {
     208            $comment_parent = absint( $_POST['comment_ID'] );
     209        }
     210        $comment_auto_approved = false;
     211        $comment_data          = compact( 'comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content', 'comment_type', 'comment_parent', 'user_ID' );
     212
     213        // Automatically approve parent comment.
     214        if ( ! empty( $_POST['approve_parent'] ) ) {
     215            $parent = get_comment( $comment_parent );
     216
     217            if ( $parent && $parent->comment_approved === '0' && $parent->comment_post_ID == $comment_post_ID ) {
     218                if ( ! current_user_can( 'edit_comment', $parent->comment_ID ) ) {
     219                    wp_die( - 1 );
     220                }
     221
     222                if ( wp_set_comment_status( $parent, 'approve' ) ) {
     223                    $comment_auto_approved = true;
     224                }
     225            }
     226        }
     227
     228        $comment_id = wp_new_comment( $comment_data );
     229        $comment    = get_comment( $comment_id );
     230        if ( ! $comment ) {
     231            wp_die( 1 );
     232        }
     233
     234        $position = ( isset( $_POST['position'] ) && (int) $_POST['position'] ) ? (int) $_POST['position'] : '-1';
     235
     236        ob_start();
     237        if ( isset( $_REQUEST['mode'] ) && 'dashboard' == $_REQUEST['mode'] ) {
     238            require_once( ABSPATH . 'wp-admin/includes/dashboard.php' );
     239            _wp_dashboard_recent_comments_row( $comment );
     240        } else {
     241            if ( isset( $_REQUEST['mode'] ) && 'single' == $_REQUEST['mode'] ) {
     242                $wp_list_table = _get_list_table( 'WP_Post_Comments_List_Table', array( 'screen' => 'edit-comments' ) );
     243            } else {
     244                $wp_list_table = _get_list_table( 'WP_Comments_List_Table', array( 'screen' => 'edit-comments' ) );
     245            }
     246            $wp_list_table->single_row( $comment );
     247        }
     248        $comment_list_item = ob_get_clean();
     249
     250        $response = array(
     251            'what'     => 'comment',
     252            'id'       => $comment->comment_ID,
     253            'data'     => $comment_list_item,
     254            'position' => $position
     255        );
     256
     257        $counts                   = wp_count_comments();
     258        $response['supplemental'] = array(
     259            'in_moderation'        => $counts->moderated,
     260            'i18n_comments_text'   => sprintf(
     261                _n( '%s Comment', '%s Comments', $counts->approved ),
     262                number_format_i18n( $counts->approved )
     263            ),
     264            'i18n_moderation_text' => sprintf(
     265                _nx( '%s in moderation', '%s in moderation', $counts->moderated, 'comments' ),
     266                number_format_i18n( $counts->moderated )
     267            )
     268        );
     269
     270        if ( $comment_auto_approved ) {
     271            $response['supplemental']['parent_approved'] = $parent->comment_ID;
     272            $response['supplemental']['parent_post_id']  = $parent->comment_post_ID;
     273        }
     274
     275        $x = new \WP_Ajax_Response();
     276        $x->add( $response );
     277        $x->send();
    149278    }
    150279}
Note: See TracChangeset for help on using the changeset viewer.