Advertisement
simonwheatley

updates

Sep 3rd, 2011
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1.  
  2. /**
  3. * Does updates, flushes the rewrite rules, etc, if we're in the
  4. * admin area, and if they need it.
  5. *
  6. * @return void
  7. * @author Simon Wheatley
  8. **/
  9. protected function do_update() {
  10. global $wpdb;
  11.  
  12. if ( ! is_admin() )
  13. return;
  14.  
  15. $version_option = $this->name . '_version';
  16. $version = get_option( $version_option, 0 );
  17.  
  18. if ( $version == $this->version )
  19. return;
  20.  
  21. if ( $version < 4 ) {
  22. wp_clear_scheduled_hook( 'sie_events_expire_events' );
  23. wp_schedule_event( gmmktime( 2 ), 'daily', 'sie_events_expire_events' );
  24. error_log( "SI Events Venue: Setup Cron" );
  25. }
  26.  
  27. if ( $version < 4 ) {
  28. flush_rewrite_rules();
  29. error_log( "SI Events Venue: Flush rewrite rules" );
  30. }
  31.  
  32. if ( $version < 6 ) {
  33. $venue_ids = $wpdb->get_col( " SELECT ID from $wpdb->posts WHERE post_type = 'venue' AND post_status = 'publish' " );
  34. $postgeo = $wpdb->prefix . 'postgeo';
  35. $postgeo_sql = " INSERT INTO $postgeo VALUES( %d, %s, %s ) ON DUPLICATE KEY UPDATE lng = %s, lat = %s ";
  36. foreach ( $venue_ids as $venue_id ) {
  37. $venue_term_id = (int) get_post_meta( $venue_id, '_venue_term_id', true );
  38. $venue_geo = $wpdb->get_row( $wpdb->prepare( " SELECT lng, lat FROM $postgeo WHERE post_id = %s ", $venue_id ) );
  39. $event_ids = get_objects_in_term( $venue_term_id, 'venue' );
  40. error_log( "Updating postgeo on EVENTS " . join( ',', $event_ids ) . " for VENUE $venue_id" );
  41. foreach ( $event_ids as $event_id ) {
  42. $prepared_sql = $wpdb->prepare( $postgeo_sql, $event_id, $venue_geo->lng, $venue_geo->lat, $venue_geo->lng, $venue_geo->lat );
  43. if ( false === $wpdb->query( $prepared_sql ) )
  44. error_log( "DB error on do_update creating postgeo $postgeo for event $event_id" );
  45. }
  46. }
  47. error_log( "SI Events Venue: reset all events coords from their venue" );
  48. }
  49.  
  50. error_log( "Updating version ($version_option) to $this->version (was $version)" );
  51. update_option( $version_option, $this->version );
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement