Making WordPress.org

Changeset 7872


Ignore:
Timestamp:
11/19/2018 08:54:21 PM (6 years ago)
Author:
coreymckrill
Message:

WordCamp Reports: Normalize validation of WordCamp IDs

This removes the WordCamp ID validation method in the Base class in favor of
the external validation function, and switches all of the reports over to use
the external function.

Location:
sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-reports/classes/report
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-reports/classes/report/class-base.php

    r7734 r7872  
    55
    66namespace WordCamp\Reports\Report;
    7 use WP_Post;
    8 
    97defined( 'WPINC' ) || die();
     8
     9use WP_Error, WP_Post, WP_REST_Response;
    1010
    1111/**
     
    115115        ) );
    116116
    117         $this->error = new \WP_Error();
     117        $this->error = new WP_Error();
    118118    }
    119119
     
    149149
    150150        return $data;
     151    }
     152
     153    /**
     154     * Determine the data fields safelist based on the context of the report.
     155     *
     156     * @return array The list of fields that are safe to include.
     157     */
     158    public function get_data_fields_safelist() {
     159        $safelist = $this->public_data_fields;
     160
     161        if ( false === $this->options['public'] ) {
     162            $safelist = array_merge( $safelist, $this->private_data_fields );
     163        }
     164
     165        return $safelist;
    151166    }
    152167
     
    218233     * Merge two error objects into one, new error object.
    219234     *
    220      * @param \WP_Error $error1 An error object.
    221      * @param \WP_Error $error2 An error object.
    222      *
    223      * @return \WP_Error The combined errors of the two parameters.
    224      */
    225     protected function merge_errors( \WP_Error $error1, \WP_Error $error2 ) {
     235     * @param WP_Error $error1 An error object.
     236     * @param WP_Error $error2 An error object.
     237     *
     238     * @return WP_Error The combined errors of the two parameters.
     239     */
     240    protected function merge_errors( WP_Error $error1, WP_Error $error2 ) {
    226241        $codes = $error2->get_error_codes();
    227242
     
    235250
    236251        return $error1;
    237     }
    238 
    239     /**
    240      * Validate a given WordCamp post ID.
    241      *
    242      * @param int $wordcamp_id The ID of a WordCamp post.
    243      *
    244      * @return bool True if the WordCamp ID is valid. Otherwise false.
    245      */
    246     protected function validate_wordcamp_id( $wordcamp_id ) {
    247         $wordcamp = get_post( $wordcamp_id );
    248 
    249         if ( ! $wordcamp instanceof WP_Post || WCPT_POST_TYPE_ID !== get_post_type( $wordcamp ) ) {
    250             $this->error->add( 'invalid_wordcamp_id', 'Please enter a valid WordCamp ID.' );
    251 
    252             return false;
    253         }
    254 
    255         $wordcamp_site_id = get_wordcamp_site_id( $wordcamp );
    256 
    257         if ( ! $wordcamp_site_id ) {
    258             $this->error->add( 'wordcamp_without_site', 'The specified WordCamp does not have a site yet.' );
    259 
    260             return false;
    261         }
    262 
    263         return true;
    264252    }
    265253
     
    288276     * @param array $additional_response_params Additional top-level parameters to add to the response.
    289277     *
    290      * @return \WP_REST_Response
     278     * @return WP_REST_Response
    291279     */
    292280    protected static function prepare_rest_response( $data, array $additional_response_params = array() ) {
     
    298286        $response_data['data'] = $data;
    299287
    300         return new \WP_REST_Response( $response_data );
    301     }
    302 
    303     /**
    304      * Determine the data fields safelist based on the context of the report.
    305      *
    306      * @return array The list of fields that are safe to include.
    307      */
    308     public function get_data_fields_safelist() {
    309         $safelist = $this->public_data_fields;
    310 
    311         if ( false === $this->options['public'] ) {
    312             $safelist = array_merge( $safelist, $this->private_data_fields );
    313         }
    314 
    315         return $safelist;
     288        return new WP_REST_Response( $response_data );
    316289    }
    317290}
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-reports/classes/report/class-payment-activity.php

    r7434 r7872  
    99defined( 'WPINC' ) || die();
    1010
     11use Exception;
    1112use WordCamp\Reports;
    1213use WordCamp\Utilities;
     14use function WordCamp\Reports\Validation\{validate_wordcamp_id};
    1315use WordCamp\Budgets_Dashboard\Reimbursement_Requests;
    1416
     
    122124        $this->xrt = new Utilities\Currency_XRT_Client();
    123125
    124         if ( $wordcamp_id && $this->validate_wordcamp_id( $wordcamp_id ) ) {
    125             $this->wordcamp_id      = $wordcamp_id;
    126             $this->wordcamp_site_id = get_wordcamp_site_id( get_post( $wordcamp_id ) );
     126        if ( $wordcamp_id ) {
     127            try {
     128                $valid = validate_wordcamp_id( $wordcamp_id );
     129
     130                $this->wordcamp_id      = $valid->post_id;
     131                $this->wordcamp_site_id = $valid->site_id;
     132            } catch( Exception $e ) {
     133                $this->error->add(
     134                    self::$slug . '-wordcamp-id-error',
     135                    $e->getMessage()
     136                );
     137            }
    127138        }
    128139    }
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-reports/classes/report/class-sponsor-invoices.php

    r7434 r7872  
    99defined( 'WPINC' ) || die();
    1010
     11use Exception;
    1112use WordCamp\Reports;
    1213use WordCamp\Utilities;
     14use function WordCamp\Reports\Validation\{validate_wordcamp_id};
    1315use WordCamp\Budgets_Dashboard\Sponsor_Invoices as WCBD_Sponsor_Invoices;
    1416
     
    121123        $this->xrt = new Utilities\Currency_XRT_Client();
    122124
    123         if ( $wordcamp_id && $this->validate_wordcamp_id( $wordcamp_id ) ) {
    124             $this->wordcamp_id      = $wordcamp_id;
    125             $this->wordcamp_site_id = get_wordcamp_site_id( get_post( $wordcamp_id ) );
     125        if ( $wordcamp_id ) {
     126            try {
     127                $valid = validate_wordcamp_id( $wordcamp_id );
     128
     129                $this->wordcamp_id      = $valid->post_id;
     130                $this->wordcamp_site_id = $valid->site_id;
     131            } catch( Exception $e ) {
     132                $this->error->add(
     133                    self::$slug . '-wordcamp-id-error',
     134                    $e->getMessage()
     135                );
     136            }
    126137        }
    127138    }
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-reports/classes/report/class-sponsorship-grants.php

    r7434 r7872  
    99defined( 'WPINC' ) || die();
    1010
     11use Exception;
    1112use WordCamp\Reports;
    1213use WordCamp\Reports\Report;
    1314use WordCamp\Utilities;
     15use function WordCamp\Reports\Validation\{validate_wordcamp_id};
    1416
    1517/**
     
    116118        $this->xrt = new Utilities\Currency_XRT_Client();
    117119
    118         if ( $wordcamp_id && $this->validate_wordcamp_id( $wordcamp_id ) ) {
    119             $this->wordcamp_id      = $wordcamp_id;
    120             $this->wordcamp_site_id = get_wordcamp_site_id( get_post( $wordcamp_id ) );
     120        if ( $wordcamp_id ) {
     121            try {
     122                $valid = validate_wordcamp_id( $wordcamp_id );
     123
     124                $this->wordcamp_id      = $valid->post_id;
     125                $this->wordcamp_site_id = $valid->site_id;
     126            } catch( Exception $e ) {
     127                $this->error->add(
     128                    self::$slug . '-wordcamp-id-error',
     129                    $e->getMessage()
     130                );
     131            }
    121132        }
    122133    }
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-reports/classes/report/class-ticket-revenue.php

    r7434 r7872  
    99defined( 'WPINC' ) || die();
    1010
     11use Exception;
    1112use WordCamp\Reports;
    1213use WordCamp\Utilities;
     14use function WordCamp\Reports\Validation\{validate_wordcamp_id};
    1315
    1416/**
     
    127129        $this->xrt = new Utilities\Currency_XRT_Client();
    128130
    129         if ( $wordcamp_id && $this->validate_wordcamp_id( $wordcamp_id ) ) {
    130             $this->wordcamp_id      = $wordcamp_id;
    131             $this->wordcamp_site_id = get_wordcamp_site_id( get_post( $wordcamp_id ) );
     131        if ( $wordcamp_id ) {
     132            try {
     133                $valid = validate_wordcamp_id( $wordcamp_id );
     134
     135                $this->wordcamp_id      = $valid->post_id;
     136                $this->wordcamp_site_id = $valid->site_id;
     137            } catch( Exception $e ) {
     138                $this->error->add(
     139                    self::$slug . '-wordcamp-id-error',
     140                    $e->getMessage()
     141                );
     142            }
    132143        }
    133144    }
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-reports/classes/report/class-wordcamp-payment-methods.php

    r7465 r7872  
    99defined( 'WPINC' ) || die();
    1010
     11use Exception;
    1112use WordCamp\Reports;
    1213use WordCamp\Utilities;
     14use function WordCamp\Reports\Validation\{validate_wordcamp_id};
    1315
    1416/**
     
    112114        $this->xrt = new Utilities\Currency_XRT_Client();
    113115
    114         if ( $wordcamp_id && $this->validate_wordcamp_id( $wordcamp_id ) ) {
    115             $this->wordcamp_id      = $wordcamp_id;
    116             $this->wordcamp_site_id = get_wordcamp_site_id( get_post( $wordcamp_id ) );
     116        if ( $wordcamp_id ) {
     117            try {
     118                $valid = validate_wordcamp_id( $wordcamp_id );
     119
     120                $this->wordcamp_id      = $valid->post_id;
     121                $this->wordcamp_site_id = $valid->site_id;
     122            } catch( Exception $e ) {
     123                $this->error->add(
     124                    self::$slug . '-wordcamp-id-error',
     125                    $e->getMessage()
     126                );
     127            }
    117128        }
    118129    }
Note: See TracChangeset for help on using the changeset viewer.