Guest User

Restore distance-based ordering

a guest
Aug 1st, 2014
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.95 KB | None | 0 0
  1. class MapView_QuasiDistanceOrdering {
  2.     /**
  3.      * Set up a listener to potentially re-order location based search results.
  4.      */
  5.     public function __construct() {
  6.         add_action( 'tribe_events_before_view', array( $this, 'listen' ) );
  7.     }
  8.  
  9.     /**
  10.      * We can only reorder by distance if the query results contain distance/geodata
  11.      * for each result.
  12.      *
  13.      * @param $template
  14.      */
  15.     public function listen( $template ) {
  16.         global $wp_query;
  17.  
  18.         if ( empty( $wp_query->posts ) ) return;
  19.         if ( ! $this->are_distances_assigned() ) return;
  20.  
  21.         $this->quick_sort( $wp_query->posts );
  22.     }
  23.  
  24.     protected function are_distances_assigned() {
  25.         global $wp_query;
  26.         $posts_copy = $wp_query->posts;
  27.         $test_post = array_shift( $posts_copy );
  28.         return isset( $test_post->distance );
  29.     }
  30.  
  31.     /**
  32.      * Borrows quick sort implementation from previous incarnation
  33.      * of TribeEventsGeoLoc, in turn this borrows from the referenced
  34.      * answer on StackOverflow.
  35.      *
  36.      * @see http://stackoverflow.com/questions/1462503/sort-array-by-object-property-in-php
  37.      * @param $array
  38.      */
  39.     protected function quick_sort( &$array ) {
  40.         $cur           = 1;
  41.         $stack[1]['l'] = 0;
  42.         $stack[1]['r'] = count( $array ) - 1;
  43.  
  44.         do {
  45.             $l = $stack[$cur]['l'];
  46.             $r = $stack[$cur]['r'];
  47.             $cur --;
  48.  
  49.             do {
  50.                 $i   = $l;
  51.                 $j   = $r;
  52.                 $tmp = $array[(int) ( ( $l + $r ) / 2 )];
  53.  
  54.                 do {
  55.                     /* Divide... */
  56.                     while ( $array[$i]->distance < $tmp->distance ) {
  57.                         $i ++;
  58.                     }
  59.  
  60.                     while ( $tmp->distance < $array[$j]->distance ) {
  61.                         $j --;
  62.                     }
  63.  
  64.                     /* ...and conquer! */
  65.                     if ( $i <= $j ) {
  66.                         $w         = $array[$i];
  67.                         $array[$i] = $array[$j];
  68.                         $array[$j] = $w;
  69.  
  70.                         $i ++;
  71.                         $j --;
  72.                     }
  73.  
  74.                 } while ( $i <= $j );
  75.  
  76.                 if ( $i < $r ) {
  77.                     $cur ++;
  78.                     $stack[$cur]['l'] = $i;
  79.                     $stack[$cur]['r'] = $r;
  80.                 }
  81.                 $r = $j;
  82.  
  83.             } while ( $l < $r );
  84.  
  85.         } while ( $cur != 0 );
  86.     }
  87. }
  88.  
  89. new MapView_QuasiDistanceOrdering;
Advertisement
Add Comment
Please, Sign In to add comment