Making WordPress.org


Ignore:
Timestamp:
01/16/2019 03:36:24 PM (6 years ago)
Author:
vedjain
Message:

WCPT: Applies code standard changes to wcpt plugin.

Most of the changes are small, but these are some important ones:

  1. Added nonce check in multiple places. This will ensure that request is always coming from the intended page.
  1. Escaped output HTML in many places. These are not necessarily XSS vulnerabilities, and in most places they were hardcoded. But its a good practice to always escape regardless of source.

Summary:

  • wcpt-event/class-event-admin.php
    • Added nonce check in metabox_save.
    • Escaped output in dislpay_meta_boxes
  • wcpt-event/class-event-application.php
    • Change definition of submit_application to pass $POST arguments
  • wcpt-loader.php
    • Indent whole file by 1 indent.
  • wcpt-meetup/class-meetup-admin.php
    • Added nonce check in maybe_update_meetup_data
  • wcpt-wordcamp/wordcamp-admin.php
    • Escaping in user_profile_wordcamp, column_data
    • Escaping using kses in post_row_actions
    • Use post_data_raw instead of $_POST in enforce_post_status
File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wcpt/wcpt-wordcamp/wordcamp-admin.php

    r8084 r8085  
    2323         * Initialize WCPT Admin
    2424         */
    25         function __construct() {
     25        public function __construct() {
    2626
    2727            parent::__construct();
    2828
    29             // Add some general styling to the admin area
     29            // Add some general styling to the admin area.
    3030            add_action( 'wcpt_admin_head', array( $this, 'admin_head' ) );
    3131
    32             // Scripts and CSS
     32            // Scripts and CSS.
    3333            add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ) );
    3434
    35             // Post status transitions
     35            // Post status transitions.
    3636            add_action( 'transition_post_status', array( $this, 'trigger_schedule_actions' ), 10, 3 );
    3737            add_action( 'wcpt_approved_for_pre_planning', array( $this, 'add_organizer_to_central' ), 10 );
     
    4141
    4242            add_filter(
    43                 'wp_insert_post_data', array(
     43                'wp_insert_post_data',
     44                array(
    4445                    $this,
    4546                    'require_complete_meta_to_publish_wordcamp',
    46                 ), 11, 2
    47             ); // after enforce_post_status
    48 
    49             // Cron jobs
     47                ),
     48                11,
     49                2
     50            ); // after enforce_post_status.
     51
     52            // Cron jobs.
    5053            add_action( 'plugins_loaded', array( $this, 'schedule_cron_jobs' ), 11 );
    5154            add_action( 'wcpt_close_wordcamps_after_event', array( $this, 'close_wordcamps_after_event' ) );
     
    5558
    5659        /**
    57          * metabox ()
    58          *
    5960         * Add the metabox
    6061         *
    6162         * @uses add_meta_box
    6263         */
    63         function metabox() {
     64        public function metabox() {
    6465            add_meta_box(
    6566                'wcpt_information',
     
    104105         * @return string
    105106         */
    106         static function get_event_label() {
     107        public static function get_event_label() {
    107108            return WordCamp_Application::get_event_label();
    108109        }
     
    113114         * @return string
    114115         */
    115         static function get_event_type() {
     116        public static function get_event_type() {
    116117            return WordCamp_Application::get_event_type();
    117118        }
     
    120121         * Check if a field is readonly.
    121122         *
    122          * @param $key
     123         * @param string $key
    123124         *
    124125         * @return bool
    125126         */
    126         function _is_protected_field( $key ) {
     127        public function _is_protected_field( $key ) {
    127128            return self::is_protected_field( $key );
    128129        }
    129130
     131        /**
     132         * Update mentor username.
     133         *
     134         * @param int $post_id
     135         */
    130136        public function update_mentor( $post_id ) {
    131137            if ( $this->get_event_type() !== get_post_type() ) {
     
    133139            }
    134140
    135             // If the Mentor username changed, update the site
     141            // If the Mentor username changed, update the site.
     142            //phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce verified in `metabox_save` in class-event-admin.php.
    136143            $mentor_username = $_POST[ wcpt_key_to_str( 'Mentor WordPress.org User Name', 'wcpt_' ) ];
    137             if ( $mentor_username !== get_post_meta( $post_id, 'Mentor WordPress.org User Name', true ) ) {
     144            if ( get_post_meta( $post_id, 'Mentor WordPress.org User Name', true ) !== $mentor_username ) {
    138145                $this->add_mentor( get_post( $post_id ), $mentor_username );
    139146            }
     
    146153         * These are used for the maps on Central, stats, etc.
    147154         *
    148          * @param int   $post_id              Post id
    149          * @param array $original_meta_values Original meta values before save
     155         * @param int   $post_id              Post id.
     156         * @param array $original_meta_values Original meta values before save.
    150157         */
    151158        public function update_venue_address( $post_id, $original_meta_values ) {
     
    154161            }
    155162
    156             // If the venue address was changed, update its coordinates
     163            // If the venue address was changed, update its coordinates.
     164            //phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce verified in `metabox_save` in class-event-admin.php.
    157165            $new_address = $_POST[ wcpt_key_to_str( 'Physical Address', 'wcpt_' ) ];
    158166            if ( $new_address === $original_meta_values['Physical Address'][0] ) {
     
    160168            }
    161169
    162             $request_url = add_query_arg( array(
    163                 'address' => rawurlencode( $new_address ),
    164             ), 'https://maps.googleapis.com/maps/api/geocode/json' );
     170            $request_url = add_query_arg(
     171                array(
     172                    'address' => rawurlencode( $new_address ),
     173                ),
     174                'https://maps.googleapis.com/maps/api/geocode/json'
     175            );
    165176
    166177            $key = apply_filters( 'wordcamp_google_maps_api_key', '', 'server' );
    167178
    168179            if ( $key ) {
    169                 $request_url = add_query_arg( array(
    170                     'key' => $key,
    171                 ), $request_url );
     180                $request_url = add_query_arg(
     181                    array( 'key' => $key ),
     182                    $request_url
     183                );
    172184            }
    173185
     
    175187            $body     = json_decode( wp_remote_retrieve_body( $response ) );
    176188
    177             // Don't delete the existing (and probably good) values if the request failed
     189            // Don't delete the existing (and probably good) values if the request failed.
    178190            if ( is_wp_error( $response ) || empty( $body->results[0]->address_components ) ) {
    179191                Logger\log( 'geocoding_failure', compact( 'request_url', 'response' ) );
     
    198210         * @see https://developers.google.com/maps/documentation/geocoding/intro#Types API response schema
    199211         *
    200          * @param $response
     212         * @param array $response
    201213         *
    202214         * @return array
     
    225237
    226238                            case 'country':
    227                                 $country_code = $component->short_name; // This is not guaranteed to be ISO 3166-1 alpha-2, but should match in most cases
     239                                $country_code = $component->short_name; // This is not guaranteed to be ISO 3166-1 alpha-2, but should match in most cases.
    228240                                $country_name = $component->long_name;
    229241                                break;
     
    272284         * @return array
    273285         */
    274         static function meta_keys( $meta_group = '' ) {
     286        public static function meta_keys( $meta_group = '' ) {
    275287            /*
    276288             * Warning: These keys are used for both the input field label and the postmeta key, so if you want to
     
    289301                        'Organizer Name'                   => 'text',
    290302                        'WordPress.org Username'           => 'text',
    291                         'Email Address'                    => 'text', // Note: This is the lead organizer's e-mail address, which is different than the "E-mail Address" field
     303                        'Email Address'                    => 'text', // Note: This is the lead organizer's e-mail address, which is different than the "E-mail Address" field.
    292304                        'Telephone'                        => 'text',
    293305                        'Mailing Address'                  => 'textarea',
     
    342354
    343355                case 'contributor':
    344                     // These fields names need to be unique, hence the 'Contributor' prefix on each one
     356                    // These fields names need to be unique, hence the 'Contributor' prefix on each one.
    345357                    $retval = array(
    346358                        'Contributor Day'                => 'checkbox',
     
    361373                        'URL'                             => 'wc-url',
    362374                        'E-mail Address'                  => 'text',
    363                         // Note: This is the address for the entire organizing team, which is different than the "Email Address" field
     375                        // Note: This is the address for the entire organizing team, which is different than the "Email Address" field.
    364376                        'Twitter'                         => 'text',
    365377                        'WordCamp Hashtag'                => 'text',
     
    457469         * @return array
    458470         */
    459         static function get_venue_address_meta_keys() {
     471        public static function get_venue_address_meta_keys() {
    460472            return array(
    461473                '_venue_coordinates',
     
    472484         * Adds jQuery UI
    473485         */
    474         function admin_scripts() {
    475 
    476             // Edit WordCamp screen
     486        public function admin_scripts() {
     487
     488            // Edit WordCamp screen.
    477489            if ( WCPT_POST_TYPE_ID === get_post_type() ) {
    478490
    479                 // Default data
     491                // Default data.
    480492                $data = array(
    481493                    'Mentors' => array(
     
    487499                );
    488500
    489                 // Only include mentor data if the Mentor username field is editable
     501                // Only include mentor data if the Mentor username field is editable.
    490502                if ( current_user_can( 'wordcamp_manage_mentors' ) ) {
    491503                    $data['Mentors']['data'] = Mentors_Dashboard\get_all_mentor_data();
     
    501513
    502514        /**
    503          * admin_head ()
    504          *
    505515         * Add some general styling to the admin area
    506516         */
    507         function admin_head() {
    508             if ( ! empty( $_GET['post_type'] ) && $_GET['post_type'] == WCPT_POST_TYPE_ID ) : ?>
     517        public function admin_head() {
     518            if ( ! empty( $_GET['post_type'] ) && WCPT_POST_TYPE_ID == $_GET['post_type'] ) : ?>
    509519
    510520            .column-title { width: 40%; }
     
    516526
    517527        /**
    518          * user_profile_update ()
    519          *
    520528         * Responsible for showing additional profile options and settings
    521529         *
    522530         * @todo Everything
    523531         */
    524         function user_profile_update( $user_id ) {
     532        public function user_profile_update( $user_id ) {
    525533            if ( ! wcpt_has_access() ) {
    526534                return false;
     
    529537
    530538        /**
    531          * user_profile_wordcamp ()
    532          *
    533539         * Responsible for saving additional profile options and settings
    534540         *
    535541         * @todo Everything
    536542         */
    537         function user_profile_wordcamp( $profileuser ) {
     543        public function user_profile_wordcamp( $profileuser ) {
    538544            if ( ! wcpt_has_access() ) {
    539545                return false;
     
    541547            ?>
    542548
    543         <h3><?php _e( 'WordCamps', 'wcpt' ); ?></h3>
     549        <h3><?php esc_html_e( 'WordCamps', 'wcpt' ); ?></h3>
    544550
    545551        <table class="form-table">
    546552            <tr valign="top">
    547                 <th scope="row"><?php _e( 'WordCamps', 'wcpt' ); ?></th>
     553                <th scope="row"><?php esc_html_e( 'WordCamps', 'wcpt' ); ?></th>
    548554
    549555                <td>
     
    556562
    557563        /**
    558          * column_headers ()
    559          *
    560564         * Manage the column headers
    561565         *
    562566         * @param array $columns
     567         *
    563568         * @return array $columns
    564569         */
    565         function column_headers( $columns ) {
     570        public function column_headers( $columns ) {
    566571            $columns = array(
    567572                'cb'             => '<input type="checkbox" />',
     
    577582
    578583        /**
    579          * column_data ( $column, $post_id )
    580          *
    581584         * Print extra columns
    582585         *
     
    584587         * @param int    $post_id
    585588         */
    586         function column_data( $column, $post_id ) {
    587             if ( $_GET['post_type'] !== WCPT_POST_TYPE_ID ) {
     589        public function column_data( $column, $post_id ) {
     590            if ( WCPT_POST_TYPE_ID !== $_GET['post_type'] ) {
    588591                return $column;
    589592            }
     
    591594            switch ( $column ) {
    592595                case 'wcpt_location':
    593                     echo wcpt_get_wordcamp_location() ? wcpt_get_wordcamp_location() : __( 'No Location', 'wcpt' );
     596                    echo esc_html( wcpt_get_wordcamp_location() ? wcpt_get_wordcamp_location() : __( 'No Location', 'wcpt' ) );
    594597                    break;
    595598
    596599                case 'wcpt_date':
    597                     // Has a start date
    598                     if ( $start = wcpt_get_wordcamp_start_date() ) {
    599 
    600                         // Has an end date
    601                         if ( $end = wcpt_get_wordcamp_end_date() ) {
     600                    // Has a start date.
     601                    $start = wcpt_get_wordcamp_start_date();
     602                    if ( $start ) {
     603
     604                        // Has an end date.
     605                        $end = wcpt_get_wordcamp_end_date();
     606                        if ( $end ) {
    602607                            $string_date = sprintf( __( 'Start: %1$s<br />End: %2$s', 'wcpt' ), $start, $end );
    603608
    604                             // No end date
     609                            // No end date.
    605610                        } else {
    606611                            $string_date = sprintf( __( 'Start: %1$s', 'wcpt' ), $start );
    607612                        }
    608613
    609                         // No date
     614                        // No date.
    610615                    } else {
    611616                        $string_date = __( 'No Date', 'wcpt' );
    612617                    }
    613618
    614                     echo $string_date;
     619                    echo wp_kses( $string_date, array( 'br' => array() ) );
    615620                    break;
    616621
    617622                case 'wcpt_organizer':
    618                     echo wcpt_get_wordcamp_organizer_name() ? wcpt_get_wordcamp_organizer_name() : __( 'No Organizer', 'wcpt' );
     623                    echo esc_html( wcpt_get_wordcamp_organizer_name() ? wcpt_get_wordcamp_organizer_name() : __( 'No Organizer', 'wcpt' ) );
    619624                    break;
    620625
    621626                case 'wcpt_venue':
    622                     echo wcpt_get_wordcamp_venue_name() ? wcpt_get_wordcamp_venue_name() : __( 'No Venue', 'wcpt' );
     627                    echo esc_html( wcpt_get_wordcamp_venue_name() ? wcpt_get_wordcamp_venue_name() : __( 'No Venue', 'wcpt' ) );
    623628                    break;
    624629            }
     
    626631
    627632        /**
    628          * post_row_actions ( $actions, $post )
    629          *
    630633         * Remove the quick-edit action link and display the description under
    631634         *
     
    634637         * @return array $actions
    635638         */
    636         function post_row_actions( $actions, $post ) {
     639        public function post_row_actions( $actions, $post ) {
    637640            if ( WCPT_POST_TYPE_ID == $post->post_type ) {
    638641                unset( $actions['inline hide-if-no-js'] );
     
    640643                $wc = array();
    641644
    642                 if ( $wc_location = wcpt_get_wordcamp_location() ) {
     645                $wc_location = wcpt_get_wordcamp_location();
     646                if ( $wc_location ) {
    643647                    $wc['location'] = $wc_location;
    644648                }
    645649
    646                 if ( $wc_url = make_clickable( wcpt_get_wordcamp_url() ) ) {
     650                $wc_url = make_clickable( wcpt_get_wordcamp_url() );
     651                if ( $wc_url ) {
    647652                    $wc['url'] = $wc_url;
    648653                }
    649654
    650                 echo implode( ' - ', (array) $wc );
     655                echo wp_kses( implode( ' - ', (array) $wc ), wp_kses_allowed_html() );
    651656            }
    652657
     
    672677            if ( 'wcpt-pre-planning' == $new_status ) {
    673678                do_action( 'wcpt_approved_for_pre_planning', $post );
    674             } elseif ( $old_status == 'wcpt-needs-schedule' && $new_status == 'wcpt-scheduled' ) {
     679            } elseif ( 'wcpt-needs-schedule' == $old_status && 'wcpt-scheduled' == $new_status ) {
    675680                do_action( 'wcpt_added_to_final_schedule', $post );
    676681            }
     
    689694         */
    690695        public function add_organizer_to_central( $post ) {
     696
     697            // phpcs:ignore WordPress.Security.NonceVerification.Missing -- WordCamp status can be moved to pre-planning status only from the admin edit screen where nonce is already verified.
    691698            $lead_organizer = get_user_by( 'login', $_POST['wcpt_wordpress_org_username'] );
    692699
     
    767774         */
    768775        public function enforce_post_status( $post_data, $post_data_raw ) {
    769             if ( $post_data['post_type'] != WCPT_POST_TYPE_ID || empty( $_POST['post_ID'] ) ) {
     776            if ( WCPT_POST_TYPE_ID != $post_data['post_type'] || empty( $post_data_raw['ID'] ) ) {
    770777                return $post_data;
    771778            }
    772779
    773             $post = get_post( $_POST['post_ID'] );
     780            $post = get_post( $post_data_raw['post_ID'] );
    774781            if ( ! $post ) {
    775782                return $post_data;
     
    814821            $required_scheduled_fields  = $this->get_required_fields( 'scheduled' );
    815822
    816             // Check pending posts
    817             if ( 'wcpt-needs-site' == $post_data['post_status'] && absint( $_POST['post_ID'] ) > $min_site_id ) {
     823            // Check pending posts.
     824            if ( 'wcpt-needs-site' == $post_data['post_status'] && absint( $post_data_raw['ID'] ) > $min_site_id ) {
    818825                foreach ( $required_needs_site_fields as $field ) {
     826
     827                    // phpcs:ignore WordPress.Security.NonceVerification.Missing -- nonce check would have done in `metabox_save`.
    819828                    $value = $_POST[ wcpt_key_to_str( $field, 'wcpt_' ) ];
    820829
     
    827836            }
    828837
    829             // Check published posts
    830             if ( 'wcpt-scheduled' == $post_data['post_status'] && isset( $_POST['post_ID'] ) && absint( $_POST['post_ID'] ) > $min_site_id ) {
     838            // Check published posts.
     839            if ( 'wcpt-scheduled' == $post_data['post_status'] && isset( $post_data_raw['ID'] ) && absint( $post_data_raw['ID'] ) > $min_site_id ) {
    831840                foreach ( $required_scheduled_fields as $field ) {
     841                    // phpcs:ignore WordPress.Security.NonceVerification.Missing -- nonce check would have done in `metabox_save`.
    832842                    $value = $_POST[ wcpt_key_to_str( $field, 'wcpt_' ) ];
    833843
     
    846856         * Get a list of fields required to move to a certain post status
    847857         *
    848          * @param string $status 'needs-site' | 'scheduled' | 'any'
     858         * @param string $status 'needs-site' | 'scheduled' | 'any'.
    849859         *
    850860         * @return array
     
    854864
    855865            $scheduled = array(
    856                 // WordCamp
     866                // WordCamp.
    857867                'Start Date (YYYY-mm-dd)',
    858868                'Location',
     
    862872                'Multi-Event Sponsor Region',
    863873
    864                 // Organizing Team
     874                // Organizing Team.
    865875                'Organizer Name',
    866876                'WordPress.org Username',
     
    873883                'Budget Wrangler E-mail Address',
    874884
    875                 // Venue
    876                 'Physical Address', // used to build stats
     885                // Venue.
     886                'Physical Address', // used to build stats.
    877887            );
    878888
     
    895905        }
    896906
     907        /**
     908         * TODO: Add description.
     909         *
     910         * @return array
     911         */
    897912        public static function get_protected_fields() {
    898913            $protected_fields = array();
     
    900915            if ( ! current_user_can( 'wordcamp_manage_mentors' ) ) {
    901916                $protected_fields = array_merge(
    902                     $protected_fields, array(
     917                    $protected_fields,
     918                    array(
    903919                        'Mentor WordPress.org User Name',
    904920                        'Mentor Name',
     
    910926            if ( ! current_user_can( 'wordcamp_wrangle_wordcamps' ) ) {
    911927                $protected_fields = array_merge(
    912                     $protected_fields, array(
     928                    $protected_fields,
     929                    array(
    913930                        'Multi-Event Sponsor Region',
    914931                    )
     
    941958
    942959            $screen = get_current_screen();
    943 
    944960
    945961            if ( empty( $post->post_type ) || $this->get_event_type() != $post->post_type || 'post' !== $screen->base ) {
     
    10561072        }
    10571073    }
    1058 endif; // class_exists check
     1074endif; // class_exists check.
    10591075
    10601076/**
     
    10661082}
    10671083
     1084/**
     1085 * Displays organizer metabox
     1086 */
    10681087function wcpt_organizer_metabox() {
    10691088    $meta_keys = $GLOBALS['wordcamp_admin']->meta_keys( 'organizer' );
     
    10711090}
    10721091
     1092/**
     1093 * Displays venue metabox
     1094 */
    10731095function wcpt_venue_metabox() {
    10741096    $meta_keys = $GLOBALS['wordcamp_admin']->meta_keys( 'venue' );
     
    10761098}
    10771099
     1100/**
     1101 * Displays contributor metabox
     1102 */
    10781103function wcpt_contributor_metabox() {
    10791104    $meta_keys = $GLOBALS['wordcamp_admin']->meta_keys( 'contributor' );
     
    10821107
    10831108/**
    1084  * wcpt_metabox ()
    1085  *
    10861109 * The metabox that holds all of the additional information
    10871110 *
     
    10951118    $required_fields = WordCamp_Admin::get_required_fields( 'any' );
    10961119
    1097     // @todo When you refactor meta_keys() to support changing labels -- see note in meta_keys() -- also make it support these notes
     1120    // @todo When you refactor meta_keys() to support changing labels -- see note in meta_keys() -- also make it support these notes.
    10981121    $messages = array(
    10991122        'Telephone'                       => 'Required for shipping.',
    11001123        'Mailing Address'                 => 'Shipping address.',
    1101         'Physical Address'                => 'Please include the city, state/province and country.', // So it can be geocoded correctly for the map
     1124        'Physical Address'                => 'Please include the city, state/province and country.', // So it can be geocoded correctly for the map.
    11021125        'Global Sponsorship Grant Amount' => 'No commas, thousands separators or currency symbols. Ex. 1234.56',
    11031126        'Global Sponsorship Grant'        => 'Deprecated.',
Note: See TracChangeset for help on using the changeset viewer.