Changeset 2022 for sites/trunk/wordpress.org/public_html/wp-content/plugins/trac-notifications/trac-notifications.php
- Timestamp:
- 11/01/2015 10:27:39 AM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordpress.org/public_html/wp-content/plugins/trac-notifications/trac-notifications.php
r1979 r2022 8 8 class wporg_trac_notifications { 9 9 10 protected $trac _subdomain;10 protected $trac; 11 11 protected $components; 12 12 … … 23 23 $trac = $_GET['trac']; 24 24 } 25 $this->set_trac( $trac ); 26 add_filter( 'allowed_http_origins', array( $this, 'filter_allowed_http_origins' ) ); 27 add_action( 'template_redirect', array( $this, 'action_template_redirect' ) ); 28 add_shortcode( 'trac-notifications', array( $this, 'notification_settings_page' ) ); 29 if ( 'core' === $trac ) { 30 require __DIR__ . '/trac-components.php'; 31 $this->components = new Make_Core_Trac_Components; 32 } 33 } 34 35 function set_trac( $trac ) { 36 $this->trac_subdomain = $trac; 25 26 require __DIR__ . '/trac-notifications-db.php'; 27 37 28 if ( function_exists( 'add_db_table' ) ) { 38 29 $tables = array( 'ticket', '_ticket_subs', '_notifications', 'ticket_change', 'component', 'milestone', 'ticket_custom' ); … … 40 31 add_db_table( 'trac_' . $trac, $table ); 41 32 } 42 $this->trac = $GLOBALS['wpdb']; 43 } 33 } 34 $this->api = new Trac_Notifications_DB( $GLOBALS['wpdb'] ); 35 36 if ( 'core' === $trac ) { 37 require __DIR__ . '/trac-components.php'; 38 $this->components = new Make_Core_Trac_Components( $this->api ); 39 } 40 41 $this->trac = $trac; 42 43 add_filter( 'allowed_http_origins', array( $this, 'filter_allowed_http_origins' ) ); 44 add_action( 'template_redirect', array( $this, 'action_template_redirect' ) ); 45 add_shortcode( 'trac-notifications', array( $this, 'notification_settings_page' ) ); 44 46 } 45 47 46 48 function trac_url() { 47 return 'https://' . $this->trac _subdomain. '.trac.wordpress.org';49 return 'https://' . $this->trac . '.trac.wordpress.org'; 48 50 } 49 51 … … 76 78 exit; 77 79 } 78 }79 80 function get_trac_ticket( $ticket_id ) {81 return $this->trac->get_row( $this->trac->prepare( "SELECT * FROM ticket WHERE id = %d", $ticket_id ) );82 }83 84 function get_trac_ticket_focuses( $ticket_id ) {85 return $this->trac->get_var( $this->trac->prepare( "SELECT value FROM ticket_custom WHERE ticket = %d AND name = 'focuses'", $ticket_id ) );86 }87 88 function get_trac_ticket_participants( $ticket_id ) {89 // Make sure we suppress CC-only comments that still exist in the database.90 // Do this by suppressing any 'cc' changes and also any empty comments (used by Trac for comment numbering).91 // Empty comments are also used for other property changes made without comment, but those changes will still be returned by this query.92 $ignore_cc = "field <> 'cc' AND NOT (field = 'comment' AND newvalue = '') AND";93 return $this->trac->get_col( $this->trac->prepare( "SELECT DISTINCT author FROM ticket_change WHERE $ignore_cc ticket = %d", $ticket_id ) );94 }95 96 function get_trac_ticket_subscriptions( $ticket_id ) {97 $by_status = array( 'blocked' => array(), 'starred' => array() );98 $subscriptions = $this->trac->get_results( $this->trac->prepare( "SELECT username, status FROM _ticket_subs WHERE ticket = %s", $ticket_id ) );99 foreach ( $subscriptions as $subscription ) {100 $by_status[ $subscription->status ? 'starred' : 'blocked' ][] = $subscription->username;101 }102 return $by_status;103 80 } 104 81 … … 135 112 } 136 113 137 function get_trac_components() {138 return $this->trac->get_col( "SELECT name FROM component WHERE name <> 'WordPress.org site' ORDER BY name ASC" );139 }140 141 function get_trac_milestones() {142 // Only show 3.8+, when this feature was launched.143 return $this->trac->get_results( "SELECT name, completed FROM milestone144 WHERE name NOT IN ('WordPress.org', '3.5.3', '3.6.2', '3.7.2') AND (completed = 0 OR completed >= 1386864000000000)145 ORDER BY (completed = 0) DESC, name DESC", OBJECT_K );146 }147 148 function get_trac_notifications_for_user( $username ) {149 $rows = $this->trac->get_results( $this->trac->prepare( "SELECT type, value FROM _notifications WHERE username = %s ORDER BY type ASC, value ASC", $username ) );150 $notifications = array( 'component' => array(), 'milestone' => array(), 'focus' => array(), 'newticket' => array() );151 152 foreach ( $rows as $row ) {153 $notifications[ $row->type ][ $row->value ] = true;154 }155 $notifications['newticket'] = ! empty( $notifications['newticket']['1'] );156 157 return $notifications;158 }159 160 function get_trac_ticket_subscription_status_for_user( $ticket_id, $username ) {161 $status = $this->trac->get_var( $this->trac->prepare( "SELECT status FROM _ticket_subs WHERE username = %s AND ticket = %s", $username, $ticket_id ) );162 if ( null !== $status ) {163 $status = (int) $status;164 }165 return $status;166 }167 168 function get_trac_ticket_subscriptions_for_user( $username ) {169 return $this->trac->get_col( $this->trac->prepare( "SELECT ticket FROM _ticket_subs WHERE username = %s AND status = 1", $username ) );170 }171 172 114 function trac_notifications_box_actions() { 173 115 send_origin_headers(); … … 179 121 $username = wp_get_current_user()->user_login; 180 122 181 $ticket _id= absint( $_POST['trac-ticket-sub'] );182 if ( ! $ticket _id) {123 $ticket = absint( $_POST['trac-ticket-sub'] ); 124 if ( ! $ticket ) { 183 125 wp_send_json_error(); 184 126 } 185 127 186 $ticket = $this->get_trac_ticket( $ticket_id ); 187 if ( ! $ticket ) { 128 if ( ! $this->api->get_trac_ticket( $ticket ) ) { 188 129 wp_send_json_error(); 189 130 } … … 198 139 case 'block' : 199 140 $status = $action === 'subscribe' ? 1 : 0; 200 $this->trac->delete( '_ticket_subs', array( 'username' => $username, 'ticket' => $ticket_id ) ); 201 $result = $this->trac->insert( '_ticket_subs', array( 'username' => $username, 'ticket' => $ticket_id, 'status' => $status ) ); 141 $result = $this->api->update_subscription( $username, $ticket, $status ); 202 142 break; 203 143 … … 205 145 case 'unblock' : 206 146 $status = $action === 'unsubscribe' ? 1 : 0; 207 $result = $this-> trac->delete( '_ticket_subs', array( 'username' => $username, 'ticket' => $ticket_id, 'status' => $status ));147 $result = $this->api->delete_subscription( $username, $ticket, $status ); 208 148 break; 209 149 } … … 229 169 } 230 170 231 $subscribed_tickets = $this-> get_trac_ticket_subscriptions_for_user( $username );171 $subscribed_tickets = $this->api->get_trac_ticket_subscriptions_for_user( $username ); 232 172 if ( ! is_array( $subscribed_tickets ) ) { 233 173 wp_send_json_error(); … … 250 190 exit; 251 191 } 252 $ticket = $this-> get_trac_ticket( $ticket_id );192 $ticket = $this->api->get_trac_ticket( $ticket_id ); 253 193 if ( ! $ticket ) { 254 194 exit; 255 195 } 256 196 257 $focuses = explode( ', ', $this-> get_trac_ticket_focuses( $ticket_id ) );258 259 $notifications = $this-> get_trac_notifications_for_user( $username );260 261 $ticket_sub = $this-> get_trac_ticket_subscription_status_for_user( $ticket_id, $username );262 263 $ticket_subscriptions = $this-> get_trac_ticket_subscriptions( $ticket_id );197 $focuses = explode( ', ', $this->api->get_trac_ticket_focuses( $ticket_id ) ); 198 199 $notifications = $this->api->get_trac_notifications_for_user( $username ); 200 201 $ticket_sub = $this->api->get_trac_ticket_subscription_status_for_user( $ticket_id, $username ); 202 203 $ticket_subscriptions = $this->api->get_trac_ticket_subscriptions( $ticket_id ); 264 204 $stars = $ticket_subscriptions['starred']; 265 205 $star_count = count( $stars ); 266 206 267 $participants = $this-> get_trac_ticket_participants( $ticket_id );207 $participants = $this->api->get_trac_ticket_participants( $ticket_id ); 268 208 269 209 $unblocked_participants = array_diff( $participants, $ticket_subscriptions['blocked'] ); … … 368 308 $send = array( 'notifications-box' => ob_get_clean() ); 369 309 if ( isset( $this->components ) ) { 370 $send['maintainers'] = $this->components->get_ maintainers_by_component( $ticket->component );310 $send['maintainers'] = $this->components->get_component_maintainers( $ticket->component ); 371 311 } 372 312 wp_send_json_success( $send ); … … 379 319 } 380 320 381 $previous_tickets = $this->trac->get_results( $this->trac->prepare( "SELECT id, summary, type, status, resolution 382 FROM ticket WHERE reporter = %s AND id <= %d LIMIT 5", $ticket->reporter, $ticket->id ) ); 383 384 if ( count( $previous_tickets ) >= 5 ) { 321 $activity = $this->api->get_reporter_past_activity( $ticket->reporter, $ticket->id ); 322 323 if ( count( $activity['tickets'] ) >= 5 ) { 385 324 return; 386 325 } 387 326 388 if ( 1 == count( $previous_tickets ) ) { 389 $previous_comments = $this->trac->get_var( $this->trac->prepare( "SELECT ticket FROM ticket_change 390 WHERE field = 'comment' AND author = %s AND ticket <> %d LIMIT 1", $ticket->reporter, $ticket->id ) ); 391 327 if ( 1 == count( $activity['tickets'] ) ) { 392 328 $output = '<strong>Make sure ' . $ticket->reporter . ' receives a warm welcome.</strong><br/>'; 393 329 394 if ( $previous_comments) {330 if ( ! empty( $activity['comments'] ) ) { 395 331 $output .= 'They’ve commented before, but it᾿s their first ticket!'; 396 332 } else { … … 400 336 $mapping = array( 2 => 'second', 3 => 'third', 4 => 'fourth' ); 401 337 402 $output = '<strong>This is only ' . $ticket->reporter . '’s ' . $mapping[ count( $ previous_tickets) ] . ' ticket!</strong><br/>Previously:';403 404 foreach ( $ previous_ticketsas $t ) {338 $output = '<strong>This is only ' . $ticket->reporter . '’s ' . $mapping[ count( $activity['tickets'] ) ] . ' ticket!</strong><br/>Previously:'; 339 340 foreach ( $activity['tickets'] as $t ) { 405 341 if ( $t->id != $ticket->id ) { 406 342 $output .= ' ' . $this->ticket_link( $t ); … … 424 360 425 361 ob_start(); 426 $components = $this-> get_trac_components();427 $milestones = $this-> get_trac_milestones();362 $components = $this->api->get_components(); 363 $milestones = $this->api->get_milestones(); 428 364 $focuses = $this->get_trac_focuses(); 429 365 430 366 $username = wp_get_current_user()->user_login; 431 $notifications = $this-> get_trac_notifications_for_user( $username );367 $notifications = $this->api->get_trac_notifications_for_user( $username ); 432 368 433 369 if ( $_POST && isset( $_POST['trac-nonce'] ) ) { 434 370 check_admin_referer( 'save-trac-notifications', 'trac-nonce' ); 371 372 $changes = array(); 435 373 436 374 foreach ( array( 'milestone', 'component', 'focus' ) as $type ) { … … 438 376 foreach ( $_POST['notifications'][ $type ] as $value => $on ) { 439 377 if ( empty( $notifications[ $type ][ $value ] ) ) { 440 $ this->trac->insert( '_notifications', compact( 'username', 'type', 'value' ));378 $changes['insert'][] = compact( 'username', 'type', 'value' ); 441 379 $notifications[ $type ][ $value ] = true; 442 380 } … … 446 384 foreach ( $notifications[ $type ] as $value => $on ) { 447 385 if ( empty( $_POST['notifications'][ $type ][ $value ] ) ) { 448 $ this->trac->delete( '_notifications', compact( 'username', 'type', 'value' ));386 $changes['delete'][] = compact( 'username', 'type', 'value' ); 449 387 unset( $notifications[ $type ][ $value ] ); 450 388 } … … 452 390 } 453 391 if ( empty( $_POST['notifications']['newticket'] ) && ! empty( $notifications['newticket'] ) ) { 454 $ this->trac->delete( '_notifications', array( 'username' => $username, 'type' => 'newticket' ));392 $changes['delete'][] = array( 'username' => $username, 'type' => 'newticket' ); 455 393 $notifications[ 'newticket' ] = false; 456 394 } elseif ( ! empty( $_POST['notifications']['newticket'] ) && empty( $notifications['newticket'] ) ) { 457 $ this->trac->insert( '_notifications', array( 'username' => $username, 'type' => 'newticket', 'value' => '1' ));395 $changes['insert'][] = array( 'username' => $username, 'type' => 'newticket', 'value' => '1' ); 458 396 $notifications[ 'newticket' ] = true; 459 397 } 460 } 398 $this->api->update_notifications( $changes ); 399 } 400 461 401 ?> 462 402
Note:
See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)