Making WordPress.org


Ignore:
Timestamp:
12/03/2017 07:32:59 PM (9 years 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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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}
Note: See TracChangeset for help on using the changeset viewer.