Advertisement
Guest User

Untitled

a guest
Aug 1st, 2014
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.     /**
  25.      * Examine one of the posts in the query results to check if distance info
  26.      * has been assigned.
  27.      *
  28.      * @return bool
  29.      */
  30.     protected function are_distances_assigned() {
  31.         global $wp_query;
  32.         $posts_copy = $wp_query->posts;
  33.         $test_post = array_shift( $posts_copy );
  34.         return isset( $test_post->distance );
  35.     }
  36.  
  37.     /**
  38.      * Borrows quick sort implementation from previous incarnation
  39.      * of TribeEventsGeoLoc, in turn this borrows from the referenced
  40.      * answer on StackOverflow.
  41.      *
  42.      * @see http://stackoverflow.com/questions/1462503/sort-array-by-object-property-in-php
  43.      * @param $array
  44.      */
  45.     protected function quick_sort( &$array ) {
  46.         $cur           = 1;
  47.         $stack[1]['l'] = 0;
  48.         $stack[1]['r'] = count( $array ) - 1;
  49.  
  50.         do {
  51.             $l = $stack[$cur]['l'];
  52.             $r = $stack[$cur]['r'];
  53.             $cur --;
  54.  
  55.             do {
  56.                 $i   = $l;
  57.                 $j   = $r;
  58.                 $tmp = $array[(int) ( ( $l + $r ) / 2 )];
  59.  
  60.                 do {
  61.                     /* Divide... */
  62.                     while ( $array[$i]->distance < $tmp->distance ) {
  63.                         $i ++;
  64.                     }
  65.  
  66.                     while ( $tmp->distance < $array[$j]->distance ) {
  67.                         $j --;
  68.                     }
  69.  
  70.                     /* ...and conquer! */
  71.                     if ( $i <= $j ) {
  72.                         $w         = $array[$i];
  73.                         $array[$i] = $array[$j];
  74.                         $array[$j] = $w;
  75.  
  76.                         $i ++;
  77.                         $j --;
  78.                     }
  79.  
  80.                 } while ( $i <= $j );
  81.  
  82.                 if ( $i < $r ) {
  83.                     $cur ++;
  84.                     $stack[$cur]['l'] = $i;
  85.                     $stack[$cur]['r'] = $r;
  86.                 }
  87.                 $r = $j;
  88.  
  89.             } while ( $l < $r );
  90.  
  91.         } while ( $cur != 0 );
  92.     }
  93. }
  94.  
  95. new MapView_QuasiDistanceOrdering;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement