Changeset 7573
- Timestamp:
- 08/01/2018 12:17:32 AM (5 years ago)
- 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 9 9 defined( 'WPINC' ) || die(); 10 10 11 use Exception; 12 use DateTimeImmutable, DateTime; 13 use WP_Error; 11 14 use WordCamp\Reports; 12 use WordCamp\Utilities; 15 use function WordCamp\Reports\get_views_dir_path; 16 use function WordCamp\Reports\Validation\validate_date_range; 17 use function WordCamp\Reports\Time\{year_array, quarter_array, month_array, convert_time_period_to_date_range}; 18 use WordCamp\Utilities\{Meetup_Client, Export_CSV}; 13 19 14 20 /** … … 17 23 * @package WordCamp\Reports\Report 18 24 */ 19 class Meetup_Groups extends Date_Range {25 class Meetup_Groups extends Base { 20 26 /** 21 27 * Report name. … … 61 67 */ 62 68 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; 63 76 64 77 /** … … 81 94 82 95 /** 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 /** 83 142 * Query and parse the data for the report. 84 143 * … … 97 156 } 98 157 99 $meetup = new Utilities\Meetup_Client();158 $meetup = new Meetup_Client(); 100 159 101 160 $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. 103 162 ) ); 104 163 … … 124 183 public function compile_report_data( array $data ) { 125 184 $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 ) { 130 189 return true; 131 190 } … … 220 279 public function render_html() { 221 280 $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; 224 283 225 284 if ( ! empty( $this->error->get_error_messages() ) ) { 226 285 $this->render_error_html(); 227 286 } else { 228 include Reports\get_views_dir_path() . 'html/meetup-groups.php';287 include get_views_dir_path() . 'html/meetup-groups.php'; 229 288 } 230 289 } … … 236 295 */ 237 296 public static function render_admin_page() { 238 $start_date 239 $end_date 240 $refresh 241 $action 242 $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' ); 243 302 244 303 $report = null; … … 249 308 ) { 250 309 $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. 252 311 ); 253 312 … … 257 316 258 317 $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'; 267 321 } 268 322 … … 273 327 */ 274 328 public static function export_to_file() { 275 $start_date 276 $end_date 277 $refresh 278 $action 279 $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' ); 280 334 281 335 $report = null; … … 287 341 if ( wp_verify_nonce( $nonce, 'run-report' ) && current_user_can( 'manage_network' ) ) { 288 342 $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. 290 344 ); 291 345 … … 296 350 $report = new self( $start_date, $end_date, $options ); 297 351 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 303 352 $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' ); 306 355 307 356 $headers = array( 'Name', 'URL', 'City', 'State', 'Country', 'Latitude', 'Longitude', 'Member Count', 'Date Founded', 'Date Joined' ); … … 315 364 } ); 316 365 317 $exporter = new Utilities\Export_CSV( array(366 $exporter = new Export_CSV( array( 318 367 'filename' => $filename, 319 368 'headers' => $headers, … … 359 408 $action = filter_input( INPUT_GET, 'action' ); 360 409 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(); 364 413 365 414 if ( ! $year ) { … … 374 423 375 424 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 } 377 435 378 436 $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. 380 438 ); 381 439 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'; 386 448 } 387 449 } -
sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-reports/includes/validation.php
r7516 r7573 43 43 } 44 44 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 } 52 58 } 53 59 … … 65 71 } 66 72 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 } 74 86 } 75 87 -
sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-reports/index.php
r7516 r7573 137 137 __NAMESPACE__ . '\Report\WordCamp_Status', 138 138 __NAMESPACE__ . '\Report\Meetup_Groups', 139 __NAMESPACE__ . '\Report\Meetup_Events', 139 140 __NAMESPACE__ . '\Report\WordCamp_Payment_Methods', 140 141 ); -
sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-reports/views/html/meetup-groups.php
r6662 r7573 4 4 */ 5 5 6 namespace WordCamp\Reports\Views\HTML\ Ticket_Revenue;6 namespace WordCamp\Reports\Views\HTML\Meetup_Groups; 7 7 defined( 'WPINC' ) || die(); 8 8 … … 13 13 14 14 <?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> 19 16 20 17 <table class="striped widefat but-not-too-wide"> … … 23 20 <td>Country</td> 24 21 <td># of Groups</td> 22 <td># of Members (non-unique)</td> 25 23 </tr> 26 24 </thead> … … 30 28 <td><?php echo esc_html( $country ); ?></td> 31 29 <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>51 30 <td class="number"><?php echo number_format_i18n( $data['total_members_by_country'][ $country ] ); ?></td> 52 31 </tr> 53 32 <?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> 54 38 </tbody> 55 39 </table> 56 40 57 41 <?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> 62 43 63 44 <table class="striped widefat but-not-too-wide"> … … 66 47 <td>Country</td> 67 48 <td># of Groups</td> 49 <td># of Members (non-unique)</td> 68 50 </tr> 69 51 </thead> … … 73 55 <td><?php echo esc_html( $country ); ?></td> 74 56 <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>94 57 <td class="number"><?php echo number_format_i18n( $data['joined_members_by_country'][ $country ] ); ?></td> 95 58 </tr> 96 59 <?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> 97 65 </tbody> 98 66 </table>
Note: See TracChangeset
for help on using the changeset viewer.