Making WordPress.org


Ignore:
Timestamp:
10/16/2018 05:30:06 PM (8 years ago)
Author:
vedjain
Message:

WordCamp Report: Add reporting for meetup applications.

This commit adds functionality for downloading meetup application reports from admin dashboard. Also refactors WordCamp report classes
to make code DRY.

File:
1 edited

Legend:

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

    r7588 r7734  
    1010use DateTime;
    1111use WP_Error, WP_Post;
    12 use function WordCamp\Reports\{get_assets_url, get_assets_dir_path, get_views_dir_path};
     12use function WordCamp\Reports\{get_views_dir_path};
    1313use WordCamp\Reports\Utility\Date_Range;
    1414use function WordCamp\Reports\Validation\{validate_date_range, validate_wordcamp_status};
    15 use function WordCamp\Reports\Time\{year_array, quarter_array, month_array, convert_time_period_to_date_range};
     15use function WordCamp\Reports\Time\{year_array, quarter_array, month_array};
    1616use WordCamp_Loader;
    1717use WordCamp\Utilities\Export_CSV;
     
    2424 * @package WordCamp\Reports\Report
    2525 */
    26 class WordCamp_Status extends Base {
     26class WordCamp_Status extends Base_Status {
    2727        /**
    2828         * Report name.
     
    223223
    224224                        $latest_log    = end( $logs );
    225                         $latest_status = $this->get_log_status_result( $latest_log );
     225                        $latest_status = $this->get_log_status_result( $latest_log, WordCamp_Loader::get_post_statuses() );
    226226                        reset( $logs );
    227227
     
    349349         * @return array
    350350         */
    351         protected function get_wordcamp_status_logs( WP_Post $wordcamp ) {
    352                 $log_entries = get_post_meta( $wordcamp->ID, '_status_change' );
    353 
    354                 if ( ! empty( $log_entries ) ) {
    355                         // Sort log entries in chronological order.
    356                         usort( $log_entries, function( $a, $b ) {
    357                                 if ( $a['timestamp'] === $b['timestamp'] ) {
    358                                         return 0;
    359                                 }
    360 
    361                                 return ( $a['timestamp'] > $b['timestamp'] ) ? 1 : -1;
    362                         } );
    363 
    364                         return $log_entries;
    365                 }
    366 
    367                 return array();
    368         }
    369 
    370         /**
    371          * Determine the ending status of a particular status change event.
    372          *
    373          * E.g. for this event:
    374          *
    375          *     Needs Vetting → More Info Requested
    376          *
    377          * The ending status would be "More Info Requested".
    378          *
    379          * @param array $log_entry A status change log entry.
    380          *
    381          * @return string
    382          */
    383         protected function get_log_status_result( $log_entry ) {
    384                 if ( isset( $log_entry['message'] ) ) {
    385                         $pieces = explode( ' → ', $log_entry['message'] );
    386 
    387                         if ( isset( $pieces[1] ) ) {
    388                                 return $this->get_status_id_from_name( $pieces[1] );
    389                         }
    390                 }
    391 
    392                 return '';
    393         }
    394 
    395         /**
    396          * Given the ID of a WordCamp status, determine the ID string.
    397          *
    398          * @param string $status_name A WordCamp status name.
    399          *
    400          * @return string
    401          */
    402         protected function get_status_id_from_name( $status_name ) {
    403                 $statuses = array_flip( WordCamp_Loader::get_post_statuses() );
    404 
    405                 if ( isset( $statuses[ $status_name ] ) ) {
    406                         return $statuses[ $status_name ];
    407                 }
    408 
    409                 return '';
     351        protected function get_wordcamp_status_logs( \WP_Post $wordcamp ) {
     352                return $this->sort_logs( get_post_meta( $wordcamp->ID, '_status_change' ) );
    410353        }
    411354
     
    447390
    448391        /**
    449          * Register all assets used by this report.
     392         * Determine whether to render the public report form.
     393         *
     394         * This shortcode is limited to use on pages.
     395         *
     396         * @return string HTML content to display shortcode.
     397         */
     398        public static function handle_shortcode() {
     399                $html = '';
     400
     401                if ( 'page' === get_post_type() ) {
     402                        self::register_assets();
     403
     404                        wp_enqueue_style( 'select2' );
     405                        wp_enqueue_script( self::$slug );
     406
     407                        ob_start();
     408                        self::render_public_page();
     409                        $html = ob_get_clean();
     410                }
     411
     412                return $html;
     413        }
     414
     415        /**
     416         * Render the page for this report on the front end.
    450417         *
    451418         * @return void
    452419         */
    453         protected static function register_assets() {
    454                 wp_register_script(
    455                         self::$slug,
    456                         get_assets_url() . 'js/' . self::$slug . '.js',
    457                         array( 'jquery', 'select2' ),
    458                         filemtime( get_assets_dir_path() . 'js/' . self::$slug . '.js' ),
    459                         true
    460                 );
    461 
    462                 wp_register_style(
    463                         self::$slug,
    464                         get_assets_url() . 'css/' . self::$slug . '.css',
    465                         array( 'select2' ),
    466                         filemtime( get_assets_dir_path() . 'css/' . self::$slug . '.css' ),
    467                         'screen'
    468                 );
    469         }
    470 
    471         /**
    472          * Enqueue JS and CSS assets for this report's admin interface.
    473          *
    474          * @return void
    475          */
    476         public static function enqueue_admin_assets() {
    477                 self::register_assets();
    478                 WordCamp_Details::register_assets();
    479 
    480                 wp_enqueue_style( WordCamp_Details::$slug );
    481                 wp_enqueue_script( self::$slug );
    482                 wp_enqueue_style( self::$slug );
     420        public static function render_public_page() {
     421                $params = self::parse_public_report_input();
     422                $years    = year_array( absint( date( 'Y' ) ), 2015 );
     423                $quarters = quarter_array();
     424                $months   = month_array();
     425                $statuses = WordCamp_Loader::get_post_statuses();
     426
     427                $error = $params['error'];
     428                $report = null;
     429                $period = $params['period'];
     430                $year = $params['year'];
     431                $status = $params['status'];
     432                if ( ! empty( $params ) && isset( $params['range'] ) ) {
     433                        $report = new self( $params['range']->start, $params['range']->end, $params['status'], $params['options'] );
     434                }
     435
     436                include get_views_dir_path() . 'public/wordcamp-status.php';
    483437        }
    484438
     
    619573        }
    620574
    621         /**
    622          * Determine whether to render the public report form.
    623          *
    624          * This shortcode is limited to use on pages.
    625          *
    626          * @return string HTML content to display shortcode.
    627          */
    628         public static function handle_shortcode() {
    629                 $html = '';
    630 
    631                 if ( 'page' === get_post_type() ) {
    632                         self::register_assets();
    633 
    634                         wp_enqueue_style( 'select2' );
    635                         wp_enqueue_script( self::$slug );
    636 
    637                         ob_start();
    638                         self::render_public_page();
    639                         $html = ob_get_clean();
    640                 }
    641 
    642                 return $html;
    643         }
    644 
    645         /**
    646          * Render the page for this report on the front end.
    647          *
    648          * @return void
    649          */
    650         public static function render_public_page() {
    651                 // Apparently 'year' is a reserved URL parameter on the front end, so we prepend 'report-'.
    652                 $year   = filter_input( INPUT_GET, 'report-year', FILTER_VALIDATE_INT );
    653                 $period = filter_input( INPUT_GET, 'period' );
    654                 $status = filter_input( INPUT_GET, 'status' );
    655                 $action = filter_input( INPUT_GET, 'action' );
    656 
    657                 $years    = year_array( absint( date( 'Y' ) ), 2015 );
    658                 $quarters = quarter_array();
    659                 $months   = month_array();
    660                 $statuses = WordCamp_Loader::get_post_statuses();
    661 
    662                 if ( ! $year ) {
    663                         $year = absint( date( 'Y' ) );
    664                 }
    665 
    666                 if ( ! $period ) {
    667                         $period = absint( date( 'm' ) );
    668                 }
    669 
    670                 $report = null;
    671 
    672                 if ( 'Show results' === $action ) {
    673                         try {
    674                                 $range = convert_time_period_to_date_range( $year, $period );
    675                         } catch ( Exception $e ) {
    676                                 $error = new WP_Error(
    677                                         self::$slug . '-time-period-error',
    678                                         $e->getMessage()
    679                                 );
    680                         }
    681 
    682                         $options = array(
    683                                 'earliest_start' => new DateTime( '2015-01-01' ), // No status log data before 2015.
    684                         );
    685 
    686                         $report = new self( $range->start, $range->end, $status, $options );
    687                 }
    688 
    689                 include get_views_dir_path() . 'public/wordcamp-status.php';
    690         }
     575        static public function get_report_object( $date_range, $status, $options ) {
     576                return new self( $date_range->start, $date_range->end, $status, $options );
     577        }
     578
    691579}
Note: See TracChangeset for help on using the changeset viewer.