Advertisement
Guest User

Untitled

a guest
Feb 6th, 2012
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.59 KB | None | 0 0
  1. <?php
  2. if ( ! class_exists( 'oxoScriptsClass' ) )
  3. {
  4.     add_action( 'init', array( 'oxoScriptsClass', 'init' ) );
  5.  
  6. /**
  7.  * Loads all styles and scripts
  8.  */
  9. class oxoScriptsClass
  10. {
  11.     /**
  12.      * The Class Object
  13.      */
  14.     static private $class = NULL;
  15.     static $nearby_nonce_val    = 'neary-nonce-val';
  16.  
  17.  
  18.     /**
  19.      * Handler for the action 'init'. Instantiates this class.
  20.      *
  21.      * @return void
  22.      */
  23.     public static function init()
  24.     {
  25.         if ( null === self::$class )
  26.             self::$class = new self;
  27.  
  28.         return self::$class;
  29.     }
  30.  
  31.  
  32.     /**
  33.      * Construct
  34.      *
  35.      * @return void
  36.      */
  37.     public function __construct()
  38.     {
  39.         // Custom function
  40.         if ( is_login() )
  41.             return;
  42.  
  43.         add_action( 'wp_enqueue_scripts', array( &$this, 'switch_jquery_google_cdn' ), 20 );
  44.         add_action( 'wp_enqueue_scripts', array( &$this, 'load_jquery' ), 20 );
  45.  
  46.         # >>>> AJAX
  47.         add_action( 'wp_enqueue_scripts', array( &$this, 'ajax_search' ), 30 );
  48.  
  49.         // Ajax Callbacks
  50.         add_action( 'wp_ajax_nopriv_nearby_locations',  array( &$this, 'nearby_locations_cb' ) );
  51.         add_action( 'wp_ajax_nearby_locations', array( &$this, 'nearby_locations_cb' ) );
  52.         # <<<<
  53.  
  54.         // DEV & DEBUG
  55.         if ( $this->debug AND current_user_can( 'manage_options' ) )
  56.             add_action( 'shutdown', array( &$this, '_dev' ) );
  57.     }
  58.  
  59.  
  60.     /**
  61.      * Load jQuery from Google CDN
  62.      * Fallback is WP core
  63.      *
  64.      * @since 0.3
  65.      * @return void
  66.      */
  67.     public function switch_jquery_google_cdn()
  68.     {
  69.         global $wp_scripts;
  70.  
  71.         $http = is_ssl() ? 'https' : 'http';
  72.         $ver  = wp_script_is( 'jquery', 'registered' ) ? $wp_scripts->registered['jquery']->ver : '1';
  73.         $min  = defined( SCRIPT_DEBUG ) && SCRIPT_DEBUG ? '' : '.min';
  74.         $src  = "{$http}://ajax.googleapis.com/ajax/libs/jquery/{$ver}/jquery{$min}.js";
  75.  
  76.         wp_deregister_script( 'jquery' );
  77.         wp_register_script( 'jquery', $src, array(), null, true );
  78.     }
  79.  
  80.  
  81.     /**
  82.      * Enqueue jQuery
  83.      *
  84.      * @since 0.3
  85.      * @return void
  86.      */
  87.     public function load_jquery()
  88.     {
  89.         wp_enqueue_script( 'jquery' );
  90.     }
  91.  
  92.  
  93.     /**
  94.      * Registers & enqueues ajax script related stuff
  95.      * For public search
  96.      *
  97.      * @since 0.3
  98.      *
  99.      * @return void
  100.      */
  101.     public function ajax_search()
  102.     {
  103.         wp_register_script( 'json2', false, array(), false, true );
  104.  
  105.         $url    = get_stylesheet_directory_uri().'/lib/search/js';
  106.         $deps   = array( 'jquery', 'jquery-form', 'json2' );
  107.  
  108.         wp_register_script( 'nearby-locations-script',  "{$url}/nearby_location.js", $deps, false, true );
  109.  
  110.         wp_enqueue_script( 'json2' );
  111.         // Ajax: Location
  112.         wp_enqueue_script( 'nearby-locations-script' );
  113.         wp_localize_script(
  114.              'nearby-locations-script'
  115.             ,'nearby_locations_object'
  116.             ,array(
  117.                  'ajaxurl'      => admin_url( 'admin-ajax.php' )
  118.                 ,'nearby_nonce' => wp_create_nonce( self :: $nearby_nonce_val )
  119.                 ,'action'       => 'nearby_locations'
  120.                 ,'type'         => null
  121.                 ,'distance'     => null
  122.                 ,'entry'        => null
  123.              )
  124.         );
  125. # DEBUG:
  126. # echo '<pre>'; print_r( $GLOBALS['wp_filter'][ "wp_ajax_nopriv_nearby-parties" ] ); echo '</pre>';
  127.     }
  128.  
  129.  
  130.     /**
  131.      * Ajax cb fn for
  132.      *
  133.      * @since 0.4
  134.      *
  135.      * @return string json result
  136.      */
  137.     public function nearby_locations_cb()
  138.     {
  139.         # check_ajax_referer( 'neary-nonce-val', 'nearby_nonce' );
  140. echo '<h1>NEARBY LOCATIONS CALLBACK FUNCTION</h1>';
  141.         var_dump( $_REQUEST );
  142.         var_dump( $_POST );
  143.         var_dump( $_GET );
  144.  
  145.         $data = $_REQUEST;
  146.  
  147.         $package = json_encode( array(
  148.              'data' => 'FOO'
  149.              ,'bla' => $_POST
  150.              ,'data'=> $data
  151.         ) );
  152.         $response = "{$_GET['callback']} ( {$package} )";
  153.  
  154.         header( "Content-Type: application/json" );
  155.         echo $response;
  156.         exit;
  157.     }
  158. } // END Class oxoThemeStylesScripts
  159.  
  160. } // endif;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement