Advertisement
Guest User

overwrite normal ajax request

a guest
Nov 27th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.27 KB | None | 0 0
  1. <?php
  2. /**
  3.  * @author : Jegtheme
  4.  */
  5. namespace JNews;
  6.  
  7. use Jeg\Template;
  8. use JNews\Ajax\AccountHandler;
  9. use JNews\Ajax\LiveSearch;
  10. use JNews\Ajax\WebsiteFragment;
  11. use JNews\Menu\Menu;
  12. use JNews\Module\ModuleManager;
  13. use JNews\Sidefeed\Sidefeed;
  14. use JNews\Dashboard\SystemDashboard;
  15.  
  16. /**
  17.  * Class JNews Frontend Ajax
  18.  */
  19. Class FrontendAjax
  20. {
  21.     /**
  22.      * @var FrontendAjax
  23.      */
  24.     private static $instance;
  25.  
  26.     private $endpoint = 'ajax-request';
  27.  
  28.     /**
  29.      * @return FrontendAjax
  30.      */
  31.     public static function getInstance()
  32.     {
  33.         if (null === static::$instance)
  34.         {
  35.             static::$instance = new static();
  36.         }
  37.         return static::$instance;
  38.     }
  39.  
  40.     /**
  41.      * FrontendAjax constructor.
  42.      */
  43.     private function __construct()
  44.     {
  45.         add_action( 'wp_head',              array( $this, 'frontend_ajax_script' ), 1, 9999 );
  46.  
  47.         add_action( 'parse_request',        array( $this, 'ajax_parse_request' ) );
  48.         add_filter( 'query_vars',           array( $this, 'ajax_query_vars' ) );
  49.     }
  50.  
  51.     public function ajax_query_vars( $vars )
  52.     {
  53.         $vars[] = $this->endpoint;
  54.         $vars[] = 'action';
  55.         return $vars;
  56.     }
  57.  
  58.     public function is_doing_ajax()
  59.     {
  60.         return true;
  61.     }
  62.  
  63.     public function ajax_parse_request( $wp )
  64.     {
  65.         if ( array_key_exists( $this->endpoint, $wp->query_vars ) )
  66.         {
  67.             // need to flag this request is ajax request
  68.             add_filter('wp_doing_ajax', array($this, 'is_doing_ajax'));
  69.  
  70.             $action = $wp->query_vars['action'];
  71.  
  72.             switch( $action )
  73.             {
  74.                 case 'jnews_website_fragment' :
  75.                     $fragment = new WebsiteFragment();
  76.                     $fragment->build_response();
  77.                     break;
  78.                 case 'jnews_newsfeed_load' :
  79.                     $sidefeed = new Sidefeed();
  80.                     $sidefeed->build_response();
  81.                     break;
  82.                 case 'jnews_ajax_live_search' :
  83.                     $search = new LiveSearch();
  84.                     $search->build_response();
  85.                     break;
  86.                 case 'jnews_mega_category_1' :
  87.                     $mega_menu = Menu::getInstance();
  88.                     $mega_menu->mega_menu_category_1_article();
  89.                     break;
  90.                 case 'jnews_mega_category_2' :
  91.                     $menu_menu = Menu::getInstance();
  92.                     $menu_menu->mega_menu_category_2_article();
  93.                     break;
  94.                 case 'jnews_refresh_nonce' :
  95.                     wp_send_json(array(
  96.                         'jnews_nonce' => wp_create_nonce('jnews-view-token')
  97.                     ));
  98.                     break;
  99.                 case 'jnews_system' :
  100.                     $template   = new Template( JNEWS_THEME_DIR . 'class/Dashboard/template/' );
  101.                     $system     = new SystemDashboard( $template );
  102.                     $system->backend_status();
  103.                     break;
  104.                 case 'login_handler':
  105.                 case 'register_handler':
  106.                 case 'forget_password_handler':
  107.                     $account = AccountHandler::getInstance();
  108.                     $account->$action();
  109.                     break;
  110.             }
  111.  
  112.             // Module Ajax
  113.             $module_prefix = ModuleManager::$module_ajax_prefix;
  114.             if(0 === strpos($action, $module_prefix))
  115.             {
  116.                 $module_name = str_replace($module_prefix, '', $action);
  117.                 ModuleManager::getInstance()->module_ajax($module_name);
  118.             }
  119.  
  120.             do_action( 'jnews_ajax_' . $action );
  121.  
  122.             // call other ajax request
  123.             do_action('wp_ajax_' . $_REQUEST['action']);
  124.             do_action('wp_ajax_nopriv_' . $_REQUEST['action']);
  125.  
  126.             exit;
  127.         }
  128.     }
  129.  
  130.     public function ajax_url()
  131.     {
  132.         return add_query_arg( array( $this->endpoint => 'jnews' ), home_url('/') );
  133.     }
  134.  
  135.     public function frontend_ajax_script()
  136.     {
  137.         if(!is_admin())
  138.         {
  139.             ?>
  140.             <script type="text/javascript"> var ajax_url = '<?php echo esc_url( $this->ajax_url() ); ?>'; </script>
  141.             <?php
  142.         }
  143.     }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement