Making WordPress.org

Changeset 7573


Ignore:
Timestamp:
08/01/2018 12:17:32 AM (8 years ago)
Author:
coreymckrill
Message:

WordCamp Reports: Add report for meetup events

Specify a date range and get information on the number of events during the
time period, sorted by country and by meetup group.

Location:
sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-reports
Files:
3 added
4 edited

Legend:

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

    r6662 r7573  
    99defined( 'WPINC' ) || die();
    1010
     11use Exception;
     12use DateTimeImmutable, DateTime;
     13use WP_Error;
    1114use WordCamp\Reports;
    12 use WordCamp\Utilities;
     15use function WordCamp\Reports\get_views_dir_path;
     16use function WordCamp\Reports\Validation\validate_date_range;
     17use function WordCamp\Reports\Time\{year_array, quarter_array, month_array, convert_time_period_to_date_range};
     18use WordCamp\Utilities\{Meetup_Client, Export_CSV};
    1319
    1420/**
    … …  
    1723 * @package WordCamp\Reports\Report
    1824 */
    19 class Meetup_Groups extends Date_Range {
     25class Meetup_Groups extends Base {
    2026        /**
    2127         * Report name.
    … …  
    6167         */
    6268        public static $shortcode_tag = 'meetup_groups_report';
     69
     70        /**
     71         * The date range that defines the scope of the report data.
     72         *
     73         * @var null|Date_Range
     74         */
     75        public $range = null;
    6376
    6477        /**
    … …  
    8194
    8295        /**
     96         * Meetup_Groups constructor.
     97         *
     98         * @param string $start_date       The start of the date range for the report.
     99         * @param string $end_date         The end of the date range for the report.
     100         * @param array  $options          {
     101         *     Optional. Additional report parameters.
     102         *     See Base::__construct and the functions in WordCamp\Reports\Validation for additional parameters.
     103         * }
     104         */
     105        public function __construct( $start_date, $end_date, array $options = [] ) {
     106                parent::__construct( $options );
     107
     108                try {
     109                        $this->range = validate_date_range( $start_date, $end_date, $options );
     110                } catch ( Exception $e ) {
     111                        $this->error->add(
     112                                self::$slug . '-date-error',
     113                                $e->getMessage()
     114                        );
     115                }
     116        }
     117
     118        /**
     119         * Generate a cache key.
     120         *
     121         * @return string
     122         */
     123        protected function get_cache_key() {
     124                $cache_key_segments = [
     125                        parent::get_cache_key(),
     126                        $this->range->generate_cache_key_segment(),
     127                ];
     128
     129                return implode( '_', $cache_key_segments );
     130        }
     131
     132        /**
     133         * Generate a cache expiration interval.
     134         *
     135         * @return int A time interval in seconds.
     136         */
     137        protected function get_cache_expiration() {
     138                return $this->range->generate_cache_duration( parent::get_cache_expiration() );
     139        }
     140
     141        /**
    83142         * Query and parse the data for the report.
    84143         *
    … …  
    97156                }
    98157
    99                 $meetup = new Utilities\Meetup_Client();
     158                $meetup = new Meetup_Client();
    100159
    101160                $data = $meetup->get_groups( array(
    102                         'pro_join_date_max' => $this->end_date->getTimestamp() * 1000, // Meetup API uses milliseconds.
     161                        'pro_join_date_max' => $this->range->end->getTimestamp() * 1000, // Meetup API uses milliseconds.
    103162                ) );
    104163
    … …  
    124183        public function compile_report_data( array $data ) {
    125184                $joined_groups = array_filter( $data, function( $group ) {
    126                         $join_date = new \DateTime();
    127                         $join_date->setTimestamp( intval( $group['pro_join_date'] / 1000 ) ); // Meetup API uses milliseconds.
    128 
    129                         if ( $join_date >= $this->start_date && $join_date <= $this->end_date ) {
     185                        $join_date = new DateTimeImmutable();
     186                        $join_date = $join_date->setTimestamp( intval( $group['pro_join_date'] / 1000 ) ); // Meetup API uses milliseconds.
     187
     188                        if ( $join_date >= $this->range->start && $join_date <= $this->range->end ) {
    130189                                return true;
    131190                        }
    … …  
    220279        public function render_html() {
    221280                $data       = $this->compile_report_data( $this->get_data() );
    222                 $start_date = $this->start_date;
    223                 $end_date   = $this->end_date;
     281                $start_date = $this->range->start;
     282                $end_date   = $this->range->end;
    224283
    225284                if ( ! empty( $this->error->get_error_messages() ) ) {
    226285                        $this->render_error_html();
    227286                } else {
    228                         include Reports\get_views_dir_path() . 'html/meetup-groups.php';
     287                        include get_views_dir_path() . 'html/meetup-groups.php';
    229288                }
    230289        }
    … …  
    236295         */
    237296        public static function render_admin_page() {
    238                 $start_date  = filter_input( INPUT_POST, 'start-date' );
    239                 $end_date    = filter_input( INPUT_POST, 'end-date' );
    240                 $refresh     = filter_input( INPUT_POST, 'refresh', FILTER_VALIDATE_BOOLEAN );
    241                 $action      = filter_input( INPUT_POST, 'action' );
    242                 $nonce       = filter_input( INPUT_POST, self::$slug . '-nonce' );
     297                $start_date = filter_input( INPUT_POST, 'start-date' );
     298                $end_date   = filter_input( INPUT_POST, 'end-date' );
     299                $refresh    = filter_input( INPUT_POST, 'refresh', FILTER_VALIDATE_BOOLEAN );
     300                $action     = filter_input( INPUT_POST, 'action' );
     301                $nonce      = filter_input( INPUT_POST, self::$slug . '-nonce' );
    243302
    244303                $report = null;
    … …  
    249308                ) {
    250309                        $options = array(
    251                                 'earliest_start' => new \DateTime( '2015-01-01' ), // Chapter program started in 2015.
     310                                'earliest_start' => new DateTime( '2015-01-01' ), // Chapter program started in 2015.
    252311                        );
    253312
    … …  
    257316
    258317                        $report = new self( $start_date, $end_date, $options );
    259 
    260                         // The report adjusts the end date in some circumstances.
    261                         if ( empty( $report->error->get_error_messages() ) ) {
    262                                 $end_date = $report->end_date->format( 'Y-m-d' );
    263                         }
    264                 }
    265 
    266                 include Reports\get_views_dir_path() . 'report/meetup-groups.php';
     318                }
     319
     320                include get_views_dir_path() . 'report/meetup-groups.php';
    267321        }
    268322
    … …  
    273327         */
    274328        public static function export_to_file() {
    275                 $start_date  = filter_input( INPUT_POST, 'start-date' );
    276                 $end_date    = filter_input( INPUT_POST, 'end-date' );
    277                 $refresh     = filter_input( INPUT_POST, 'refresh', FILTER_VALIDATE_BOOLEAN );
    278                 $action      = filter_input( INPUT_POST, 'action' );
    279                 $nonce       = filter_input( INPUT_POST, self::$slug . '-nonce' );
     329                $start_date = filter_input( INPUT_POST, 'start-date' );
     330                $end_date   = filter_input( INPUT_POST, 'end-date' );
     331                $refresh    = filter_input( INPUT_POST, 'refresh', FILTER_VALIDATE_BOOLEAN );
     332                $action     = filter_input( INPUT_POST, 'action' );
     333                $nonce      = filter_input( INPUT_POST, self::$slug . '-nonce' );
    280334
    281335                $report = null;
    … …  
    287341                if ( wp_verify_nonce( $nonce, 'run-report' ) && current_user_can( 'manage_network' ) ) {
    288342                        $options = array(
    289                                 'earliest_start' => new \DateTime( '2015-01-01' ), // Chapter program started in 2015.
     343                                'earliest_start' => new DateTime( '2015-01-01' ), // Chapter program started in 2015.
    290344                        );
    291345
    … …  
    296350                        $report = new self( $start_date, $end_date, $options );
    297351
    298                         // The report adjusts the end date in some circumstances.
    299                         if ( empty( $report->error->get_error_messages() ) ) {
    300                                 $end_date = $report->end_date->format( 'Y-m-d' );
    301                         }
    302 
    303352                        $filename   = array( $report::$name );
    304                         $filename[] = $report->start_date->format( 'Y-m-d' );
    305                         $filename[] = $report->end_date->format( 'Y-m-d' );
     353                        $filename[] = $report->range->start->format( 'Y-m-d' );
     354                        $filename[] = $report->range->end->format( 'Y-m-d' );
    306355
    307356                        $headers = array( 'Name', 'URL', 'City', 'State', 'Country', 'Latitude', 'Longitude', 'Member Count', 'Date Founded', 'Date Joined' );
    … …  
    315364                        } );
    316365
    317                         $exporter = new Utilities\Export_CSV( array(
     366                        $exporter = new Export_CSV( array(
    318367                                'filename' => $filename,
    319368                                'headers'  => $headers,
    … …  
    359408                $action = filter_input( INPUT_GET, 'action' );
    360409
    361                 $years    = self::year_array( absint( date( 'Y' ) ), 2015 );
    362                 $quarters = self::quarter_array();
    363                 $months   = self::month_array();
     410                $years    = year_array( absint( date( 'Y' ) ), 2015 );
     411                $quarters = quarter_array();
     412                $months   = month_array();
    364413
    365414                if ( ! $year ) {
    … …  
    374423
    375424                if ( 'Show results' === $action ) {
    376                         $range = self::convert_time_period_to_date_range( $year, $period );
     425                        $error = null;
     426
     427                        try {
     428                                $range = convert_time_period_to_date_range( $year, $period );
     429                        } catch ( Exception $e ) {
     430                                $error = new WP_Error(
     431                                        self::$slug . '-time-period-error',
     432                                        $e->getMessage()
     433                                );
     434                        }
    377435
    378436                        $options = array(
    379                                 'earliest_start' => new \DateTime( '2015-01-01' ), // Chapter program started in 2015.
     437                                'earliest_start' => new DateTime( '2015-01-01' ), // Chapter program started in 2015.
    380438                        );
    381439
    382                         $report = new self( $range['start_date'], $range['end_date'], $options );
    383                 }
    384 
    385                 include Reports\get_views_dir_path() . 'public/meetup-groups.php';
     440                        $report = new self( $range->start, $range->end, $options );
     441
     442                        if ( ! is_null( $error ) ) {
     443                                $report->merge_errors( $error, $report->error );
     444                        }
     445                }
     446
     447                include get_views_dir_path() . 'public/meetup-groups.php';
    386448        }
    387449}
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-reports/includes/validation.php

    r7516 r7573  
    4343        }
    4444
    45         try {
    46                 $start_date = new DateTimeImmutable( $start_date ); // Immutable so methods don't modify the original object.
    47         } catch ( Exception $e ) {
    48                 throw new Exception( sprintf(
    49                         'Invalid start date: %s',
    50                         $e->getMessage()
    51                 ) );
     45        if ( $start_date instanceof DateTime ) {
     46                $start_date = DateTimeImmutable::createFromMutable( $start_date );
     47        }
     48
     49        if ( ! $start_date instanceof DateTimeImmutable ) {
     50                try {
     51                        $start_date = new DateTimeImmutable( $start_date ); // Immutable so methods don't modify the original object.
     52                } catch ( Exception $e ) {
     53                        throw new Exception( sprintf(
     54                                'Invalid start date: %s',
     55                                $e->getMessage()
     56                        ) );
     57                }
    5258        }
    5359
    … …  
    6571        }
    6672
    67         try {
    68                 $end_date = new DateTimeImmutable( $end_date ); // Immutable so methods don't modify the original object.
    69         } catch ( Exception $e ) {
    70                 throw new Exception( sprintf(
    71                         'Invalid end date: %s',
    72                         $e->getMessage()
    73                 ) );
     73        if ( $end_date instanceof DateTime ) {
     74                $end_date = DateTimeImmutable::createFromMutable( $end_date );
     75        }
     76
     77        if ( ! $end_date instanceof DateTimeImmutable ) {
     78                try {
     79                        $end_date = new DateTimeImmutable( $end_date ); // Immutable so methods don't modify the original object.
     80                } catch ( Exception $e ) {
     81                        throw new Exception( sprintf(
     82                                'Invalid end date: %s',
     83                                $e->getMessage()
     84                        ) );
     85                }
    7486        }
    7587
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-reports/index.php

    r7516 r7573  
    137137                __NAMESPACE__ . '\Report\WordCamp_Status',
    138138                __NAMESPACE__ . '\Report\Meetup_Groups',
     139                __NAMESPACE__ . '\Report\Meetup_Events',
    139140                __NAMESPACE__ . '\Report\WordCamp_Payment_Methods',
    140141        );
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-reports/views/html/meetup-groups.php

    r6662 r7573  
    44 */
    55
    6 namespace WordCamp\Reports\Views\HTML\Ticket_Revenue;
     6namespace WordCamp\Reports\Views\HTML\Meetup_Groups;
    77defined( 'WPINC' ) || die();
    88
    … …  
    1313
    1414<?php if ( $data['total_groups'] ) : ?>
    15         <h3>Meetup groups in the chapter program as of <?php echo esc_html( $end_date->format( 'M jS, Y' ) ); ?></h3>
    16 
    17         <h4>Total groups: <?php echo number_format_i18n( $data['total_groups'] ); ?></h4>
    18         <h4>Total groups by country:</h4>
     15        <h3>Total meetup groups in the chapter program as of <?php echo esc_html( $end_date->format( 'M jS, Y' ) ); ?></h3>
    1916
    2017        <table class="striped widefat but-not-too-wide">
    … …  
    2320                        <td>Country</td>
    2421                        <td># of Groups</td>
     22                        <td># of Members (non-unique)</td>
    2523                </tr>
    2624                </thead>
    … …  
    3028                                <td><?php echo esc_html( $country ); ?></td>
    3129                                <td class="number"><?php echo number_format_i18n( $data['total_groups_by_country'][ $country ] ); ?></td>
    32                         </tr>
    33                 <?php endforeach; ?>
    34                 </tbody>
    35         </table>
    36 
    37         <h4>Total group members (non-unique): <?php echo number_format_i18n( $data['total_members'] ); ?></h4>
    38         <h4>Total group members by country:</h4>
    39 
    40         <table class="striped widefat but-not-too-wide">
    41                 <thead>
    42                 <tr>
    43                         <td>Country</td>
    44                         <td># of Members</td>
    45                 </tr>
    46                 </thead>
    47                 <tbody>
    48                 <?php foreach ( array_keys( $data['total_members_by_country'] ) as $country ) : ?>
    49                         <tr>
    50                                 <td><?php echo esc_html( $country ); ?></td>
    5130                                <td class="number"><?php echo number_format_i18n( $data['total_members_by_country'][ $country ] ); ?></td>
    5231                        </tr>
    5332                <?php endforeach; ?>
     33                <tr>
     34                        <td class="total">Total</td>
     35                        <td class="number total"><?php echo number_format_i18n( $data['total_groups'] ); ?></td>
     36                        <td class="number total"><?php echo number_format_i18n( $data['total_members'] ); ?></td>
     37                </tr>
    5438                </tbody>
    5539        </table>
    5640
    5741        <?php if ( $data['joined_groups'] ) : ?>
    58                 <h3>Meetup groups that joined the chapter program between <?php echo esc_html( $start_date->format( 'M jS, Y' ) ); ?> and <?php echo esc_html( $end_date->format( 'M jS, Y' ) ); ?></h3>
    59 
    60                 <h4>Total groups that joined: <?php echo number_format_i18n( $data['joined_groups'] ); ?></h4>
    61                 <h4>Total groups that joined by country:</h4>
     42                <h3>New meetup groups that joined the chapter program between <?php echo esc_html( $start_date->format( 'M jS, Y' ) ); ?> and <?php echo esc_html( $end_date->format( 'M jS, Y' ) ); ?></h3>
    6243
    6344                <table class="striped widefat but-not-too-wide">
    … …  
    6647                                <td>Country</td>
    6748                                <td># of Groups</td>
     49                                <td># of Members (non-unique)</td>
    6850                        </tr>
    6951                        </thead>
    … …  
    7355                                        <td><?php echo esc_html( $country ); ?></td>
    7456                                        <td class="number"><?php echo number_format_i18n( $data['joined_groups_by_country'][ $country ] ); ?></td>
    75                                 </tr>
    76                         <?php endforeach; ?>
    77                         </tbody>
    78                 </table>
    79 
    80                 <h4>Total group members that joined (non-unique): <?php echo number_format_i18n( $data['joined_members'] ); ?></h4>
    81                 <h4>Total group members that joined by country:</h4>
    82 
    83                 <table class="striped widefat but-not-too-wide">
    84                         <thead>
    85                         <tr>
    86                                 <td>Country</td>
    87                                 <td># of Members</td>
    88                         </tr>
    89                         </thead>
    90                         <tbody>
    91                         <?php foreach ( array_keys( $data['joined_members_by_country'] ) as $country ) : ?>
    92                                 <tr>
    93                                         <td><?php echo esc_html( $country ); ?></td>
    9457                                        <td class="number"><?php echo number_format_i18n( $data['joined_members_by_country'][ $country ] ); ?></td>
    9558                                </tr>
    9659                        <?php endforeach; ?>
     60                        <tr>
     61                                <td class="total">Total</td>
     62                                <td class="number total"><?php echo number_format_i18n( $data['joined_groups'] ); ?></td>
     63                                <td class="number total"><?php echo number_format_i18n( $data['joined_members'] ); ?></td>
     64                        </tr>
    9765                        </tbody>
    9866                </table>
Note: See TracChangeset for help on using the changeset viewer.