WordPress.org

Making WordPress.org

Changeset 6217


Ignore:
Timestamp:
12/03/17 19:32:59 (12 days ago)
Author:
Otto42
Message:

Plugins: General code cleanup, migrate events that happen on transistions to single place, remove duplicated code, add auditing, simplify logic. props @joostdevalk See #2860

Location:
sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory
Files:
3 edited

Legend:

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

    r6129 r6217  
    4040        add_filter( 'wp_insert_post_data', array( $this, 'check_existing_plugin_slug_on_post_update' ), 10, 2 ); 
    4141        add_filter( 'wp_unique_post_slug', array( $this, 'check_existing_plugin_slug_on_inline_save' ), 10, 6 ); 
    42  
    43         add_action( 'save_post', array( $this, 'save_close_reason' ), 10, 2 ); 
    4442 
    4543        add_action( 'wp_ajax_replyto-comment', array( $this, 'save_custom_comment' ), 0 ); 
     
    379377 
    380378    /** 
    381      * Save the reason for closing or disabling a plugin. 
    382      * 
    383      * @param int      $post_id Post ID. 
    384      * @param \WP_Post $post    Post object. 
    385      */ 
    386     public function save_close_reason( $post_id, $post ) { 
    387         if ( 'plugin' !== $post->post_type || ! isset( $_POST['close_reason'] ) ) { 
    388             return; 
    389         } 
    390  
    391         if ( ! current_user_can( 'plugin_approve', $post_id ) ) { 
    392             return; 
    393         } 
    394  
    395         if ( ! in_array( $post->post_status, array( 'closed', 'disabled' ) ) ) { 
    396             return; 
    397         } 
    398  
    399         update_post_meta( $post_id, '_close_reason', sanitize_key( $_POST['close_reason'] ) ); 
    400         update_post_meta( $post_id, '_close_date',   current_time( 'mysql' ) ); 
    401     } 
    402  
    403     /** 
    404379     * Register the Admin metaboxes for the plugin management screens. 
    405380     * 
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/admin/class-status-transitions.php

    r6026 r6217  
    11<?php 
     2 
    23namespace WordPressdotorg\Plugin_Directory\Admin; 
     4 
    35use WordPressdotorg\Plugin_Directory\Tools; 
    46use WordPressdotorg\Plugin_Directory\Tools\SVN; 
     
    1113 */ 
    1214class Status_Transitions { 
    13  
    1415    /** 
    1516     * Fetch the instance of the Status_Transitions class. 
     
    3233     * 
    3334     * @param string $post_status Plugin post status. 
     35     * 
    3436     * @return array An array of allowed post status transitions. 
    3537     */ 
     
    7274     * @param array $data    An array of slashed post data. 
    7375     * @param array $postarr An array of sanitized, but otherwise unmodified post data. 
     76     * 
    7477     * @return array 
    7578     */ 
     
    9396 
    9497        // ...or it's a white-listed status for plugin reviewers. 
    95         if ( current_user_can( 'plugin_review', $postarr['ID'] ) && in_array( $postarr['post_status'], array( 'new', 'pending' ) ) ) { 
     98        if ( current_user_can( 'plugin_review', $postarr['ID'] ) && in_array( $postarr['post_status'], array( 
     99                'new', 
     100                'pending', 
     101            ) ) ) { 
    96102            return $data; 
    97103        } 
     
    128134                $this->rejected( $post->ID, $post ); 
    129135                break; 
     136            case 'publish': 
     137                $this->clean_closed_date( $post->ID ); 
     138                break; 
     139            case 'disabled': 
     140            case 'closed': 
     141                $this->save_close_reason( $post->ID ); 
     142                break; 
    130143        } 
    131144 
     
    141154     */ 
    142155    public function approved( $post_id, $post ) { 
    143         $attachments = get_attached_media( 'application/zip', $post_id ); 
     156        $attachments   = get_attached_media( 'application/zip', $post_id ); 
    144157        $plugin_author = get_user_by( 'id', $post->post_author ); 
    145158 
     
    178191 
    179192        /* translators: 1: plugin name, 2: plugin slug */ 
    180         $content  = sprintf( __( 'Congratulations, your plugin hosting request for %1$s has been approved. 
     193        $content = sprintf( __( 'Congratulations, your plugin hosting request for %1$s has been approved. 
    181194 
    182195Within one hour you will have access to your SVN repository with the WordPress.org username and password you used to log in and submit your request. Your username is case sensitive. 
     
    215228        ); 
    216229 
     230        $this->audit_log( 'Plugin approved.', $post_id ); 
    217231        wp_mail( $plugin_author->user_email, $subject, $content, 'From: plugins@wordpress.org' ); 
    218232    } 
     
    234248        wp_update_post( array( 
    235249            'ID'        => $post_id, 
    236             'post_name' => sprintf( 'rejected-%s-rejected', $post->post_name ) 
     250            'post_name' => sprintf( 'rejected-%s-rejected', $post->post_name ), 
    237251        ) ); 
    238252 
     
    242256 
    243257        /* translators: 1: plugin name, 2: plugins@wordpress.org */ 
    244         $content  = sprintf( __( 'Unfortunately your plugin submission for %1$s has been rejected from the WordPress Plugin Directory. 
     258        $content = sprintf( __( 'Unfortunately your plugin submission for %1$s has been rejected from the WordPress Plugin Directory. 
    245259 
    246260If you believe this to be in error, please email %2$s with your plugin attached as a zip and explain why you feel your plugin should be an exception. 
     
    253267        ); 
    254268 
     269        $this->audit_log( 'Plugin rejected.', $post_id ); 
    255270        wp_mail( $email, $subject, $content, 'From: plugins@wordpress.org' ); 
    256271    } 
     
    260275     * 
    261276     * @param string $dir Directory to search in. 
     277     * 
    262278     * @return string 
    263279     */ 
     
    279295        return $plugin_root; 
    280296    } 
     297 
     298    /** 
     299     * Deletes the plugin closed date meta field. 
     300     * 
     301     * @param integer $post_id Post ID. 
     302     */ 
     303    public function clean_closed_date( $post_id ) { 
     304        delete_post_meta( $post_id, 'plugin_closed_date' ); 
     305        delete_post_meta( $post_id, '_close_reason' ); 
     306    } 
     307 
     308    /** 
     309     * Save the reason for closing or disabling a plugin. 
     310     * 
     311     * @param int $post_id Post ID. 
     312     */ 
     313    public function save_close_reason( $post_id ) { 
     314        if ( ! isset( $_POST['close_reason'] ) ) { 
     315            return; 
     316        } 
     317 
     318        if ( ! current_user_can( 'plugin_approve', $post_id ) ) { 
     319            return; 
     320        } 
     321 
     322        $close_reason = sanitize_key( $_POST['close_reason'] ); 
     323 
     324        update_post_meta( $post_id, '_close_reason', $close_reason ); 
     325        update_post_meta( $post_id, 'plugin_closed_date', current_time( 'mysql' ) ); 
     326 
     327        $this->audit_log( sprintf( 'Plugin closed for: %s', $close_reason ), $post_id ); 
     328    } 
     329 
     330    /** 
     331     * Saves an audit_log comment for the plugin. 
     332     * 
     333     * @param string  $message The message for the audit log. 
     334     * @param integer $post_id The Post ID. 
     335     */ 
     336    private function audit_log( $message, $post_id ) { 
     337        $comment = array( 
     338            'comment_type'    => 'audit_log', 
     339            'comment_post_ID' => $post_id, 
     340            'comment_author'  => get_current_user_id(), 
     341            'comment_content' => $message, 
     342        ); 
     343        wp_insert_comment( $comment ); 
     344    } 
    281345} 
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-plugin-directory.php

    r6207 r6217  
    343343            'label_count'               => _n_noop( 'Rejected <span class="count">(%s)</span>', 'Rejected <span class="count">(%s)</span>', 'wporg-plugins' ), 
    344344        ) ); 
    345  
    346         add_action( 'transition_post_status', array( $this, 'plugin_close_date' ), 10, 3 ); 
    347345 
    348346        /** 
     
    13421340    } 
    13431341 
    1344     /** 
    1345      * Save the date a plugin was closed and delete that date when it's reopened. 
    1346      * 
    1347      * @param string  $new_status The new plugin status. 
    1348      * @param string  $old_status The old plugin status. 
    1349      * @param WP_Post $post Post data. 
    1350      */ 
    1351     public function plugin_close_date( $new_status, $old_status, $post ) { 
    1352         if ( in_array( $new_status, array( 'closed', 'disabled' ) ) ) { 
    1353             update_post_meta( $post->ID, 'plugin_closed_date', current_time( 'mysql' ) ); 
    1354         } 
    1355         if ( 'publish' === $new_status && in_array( $old_status, array( 'closed', 'disabled' ) ) ) { 
    1356             delete_post_meta( $post->ID, 'plugin_closed_date' ); 
    1357         } 
    1358     } 
    13591342} 
Note: See TracChangeset for help on using the changeset viewer.