BuddyBoss_Global_Search_Plugin

BuddyPress Global Search Plugin Main Controller **************************************.

Defined (1)

The class is defined in the following location(s).

/includes/main-class.php  
  1. class BuddyBoss_Global_Search_Plugin { 
  2. /** 
  3. * Default options for the plugin, the strings are 
  4. * run through localization functions during instantiation,  
  5. * and after the user saves options the first time they 
  6. * are loaded from the DB. 
  7. * @var array 
  8. */ 
  9. private $default_options = array( 
  10. 'enable-ajax-search' => 'yes',  
  11. 'items-per-page' => '5',  
  12. ); 
  13.  
  14. /** Includes 
  15. * =================================================================== 
  16. */ 
  17.  
  18. /** 
  19. * Most WordPress/BuddyPress plugin have the includes in the function 
  20. * method that loads them, we like to keep them up here for easier 
  21. * access. 
  22. * @var array 
  23. */ 
  24. private $main_includes = array( 
  25. // Core 
  26. 'functions',  
  27. 'template',  
  28. 'filters',  
  29. 'class.BBoss_Global_Search_Helper',  
  30.  
  31. //extensions 
  32. 'plugins/search-cpt/index',  
  33. 'plugins/awpcp/index',  
  34. 'plugins/search-acf/index',  
  35. 'plugins/invite-anyone/index',  
  36. ); 
  37.  
  38. /** 
  39. * Admin includes 
  40. * @var array 
  41. */ 
  42. private $admin_includes = array( 
  43. 'admin' 
  44. ); 
  45.  
  46. /** Plugin Options 
  47. * =================================================================== 
  48. */ 
  49.  
  50. /** 
  51. * This options array is setup during class instantiation, holds 
  52. * default and saved options for the plugin. 
  53. * @var array 
  54. */ 
  55. public $options = array(); 
  56.  
  57. /** 
  58. * Whether the plugin is activated network wide. 
  59. *  
  60. * @var boolean  
  61. */ 
  62. public $network_activated = false; 
  63.  
  64. /** 
  65. * Is BuddyPress installed and activated? 
  66. * @var boolean 
  67. */ 
  68. public $bp_enabled = false; 
  69.  
  70. /** Version 
  71. * =================================================================== 
  72. */ 
  73.  
  74. /** 
  75. * Plugin codebase version 
  76. * @var string 
  77. */ 
  78. public $version = '1.0.0'; 
  79.  
  80. /** Paths 
  81. * =================================================================== 
  82. */ 
  83. public $file = ''; 
  84. public $basename = ''; 
  85. public $plugin_dir = ''; 
  86. public $plugin_url = ''; 
  87. public $lang_dir = ''; 
  88. public $assets_dir = ''; 
  89. public $assets_url = ''; 
  90.  
  91. /** Magic 
  92. * =================================================================== 
  93. */ 
  94.  
  95. /** 
  96. * BuddyPress Global Search uses many variables, most of which can be filtered to 
  97. * customize the way that it works. To prevent unauthorized access,  
  98. * these variables are stored in a private array that is magically 
  99. * updated using PHP 5.2+ methods. This is to prevent third party 
  100. * plugins from tampering with essential information indirectly, which 
  101. * would cause issues later. 
  102. * @see BuddyBoss_Global_Search_Plugin::setup_globals() 
  103. * @var array 
  104. */ 
  105. private $data; 
  106.  
  107. /** Singleton 
  108. * =================================================================== 
  109. */ 
  110.  
  111. /** 
  112. * Main BuddyPress Global Search Instance. 
  113. * Insures that only one instance of BuddyPress Global Search exists in memory at any 
  114. * one time. Also prevents needing to define globals all over the place. 
  115. * @since 1.0.0 
  116. * @static object $instance 
  117. * @uses BuddyBoss_Global_Search_Plugin::setup_globals() Setup the globals needed. 
  118. * @uses BuddyBoss_Global_Search_Plugin::setup_actions() Setup the hooks and actions. 
  119. * @uses BuddyBoss_Global_Search_Plugin::setup_textdomain() Setup the plugin's language file. 
  120. * @see buddyboss_global_search() 
  121. * @return object BuddyBoss_Global_Search_Plugin 
  122. */ 
  123. public static function instance() { 
  124. // Store the instance locally to avoid private static replication 
  125. static $instance = null; 
  126.  
  127. // Only run these methods if they haven't been run previously 
  128. if (null === $instance) { 
  129. $instance = new BuddyBoss_Global_Search_Plugin(); 
  130. $instance->setup_globals(); 
  131. $instance->setup_actions(); 
  132. $instance->setup_textdomain(); 
  133.  
  134. // Always return the instance 
  135. return $instance; 
  136.  
  137. /** Magic Methods 
  138. * =================================================================== 
  139. */ 
  140.  
  141. /** 
  142. * A dummy constructor to prevent BuddyBoss Global Search from being loaded more than once. 
  143. * @since BuddyBoss Global Search (1.0.0) 
  144. * @see BuddyBoss_Global_Search_Plugin::instance() 
  145. * @see buddypress() 
  146. */ 
  147. private function __construct() { /** Do nothing here */ 
  148.  
  149. /** 
  150. * A dummy magic method to prevent BuddyPress Global Search from being cloned. 
  151. * @since 1.0.0 
  152. */ 
  153. public function __clone() { 
  154. _doing_it_wrong(__FUNCTION__, __('Cheatin’ huh?', 'buddypress-global-search'), '1.7'); 
  155.  
  156. /** 
  157. * A dummy magic method to prevent BuddyPress Global Search from being unserialized. 
  158. * @since 1.0.0 
  159. */ 
  160. public function __wakeup() { 
  161. _doing_it_wrong(__FUNCTION__, __('Cheatin’ huh?', 'buddypress-global-search'), '1.7'); 
  162.  
  163. /** 
  164. * Magic method for checking the existence of a certain custom field. 
  165. * @since 1.0.0 
  166. */ 
  167. public function __isset($key) { 
  168. return isset($this->data[$key]); 
  169.  
  170. /** 
  171. * Magic method for getting BuddyPress Global Search varibles. 
  172. * @since 1.0.0 
  173. */ 
  174. public function __get($key) { 
  175. return isset($this->data[$key]) ? $this->data[$key] : null; 
  176.  
  177. /** 
  178. * Magic method for setting BuddyPress Global Search varibles. 
  179. * @since 1.0.0 
  180. */ 
  181. public function __set($key, $value) { 
  182. $this->data[$key] = $value; 
  183.  
  184. /** 
  185. * Magic method for unsetting BuddyPress Global Search variables. 
  186. * @since 1.0.0 
  187. */ 
  188. public function __unset($key) { 
  189. if (isset($this->data[$key])) 
  190. unset($this->data[$key]); 
  191.  
  192. /** 
  193. * Magic method to prevent notices and errors from invalid method calls. 
  194. * @since 1.0.0 
  195. */ 
  196. public function __call($name = '', $args = array()) { 
  197. unset($name, $args); 
  198. return null; 
  199.  
  200. /** Plugin Specific, Setup Globals, Actions, Includes 
  201. * =================================================================== 
  202. */ 
  203.  
  204.  
  205. /** 
  206. * Setup BuddyPress Global Search plugin global variables. 
  207. * @since 1.0.0 
  208. * @access private 
  209. * @uses plugin_dir_path() To generate BuddyPress Global Search plugin path. 
  210. * @uses plugin_dir_url() To generate BuddyPress Global Search plugin url. 
  211. * @uses apply_filters() Calls various filters. 
  212. */ 
  213. private function setup_globals() { 
  214. $this->network_activated = $this->is_network_activated(); 
  215.  
  216. // DEFAULT CONFIGURATION OPTIONS 
  217. $default_options = $this->default_options; 
  218.  
  219. $saved_options = $this->network_activated ? get_site_option( 'buddyboss_global_search_plugin_options' ) : get_option( 'buddyboss_global_search_plugin_options' ); 
  220. $saved_options = maybe_unserialize( $saved_options ); 
  221.  
  222. $this->options = wp_parse_args( $saved_options, $default_options ); 
  223.  
  224. /** Versions ************************************************* */ 
  225. $this->version = BUDDYBOSS_GLOBAL_SEARCH_PLUGIN_VERSION; 
  226.  
  227. /** Paths***************************************************** */ 
  228. // BuddyBoss Global Search root directory 
  229. $this->file = BUDDYBOSS_GLOBAL_SEARCH_PLUGIN_FILE; 
  230. $this->basename = plugin_basename($this->file); 
  231. $this->plugin_dir = BUDDYBOSS_GLOBAL_SEARCH_PLUGIN_DIR; 
  232. $this->plugin_url = BUDDYBOSS_GLOBAL_SEARCH_PLUGIN_URL; 
  233.  
  234. // Languages 
  235. $this->lang_dir = dirname($this->basename) . '/languages/'; 
  236.  
  237. // Includes 
  238. $this->includes_dir = $this->plugin_dir . 'includes'; 
  239. $this->includes_url = $this->plugin_url . 'includes'; 
  240.  
  241. // Templates 
  242. $this->templates_dir = $this->plugin_dir . 'templates'; 
  243. $this->templates_url = $this->plugin_url . 'templates'; 
  244.  
  245. // Assets 
  246. $this->assets_dir = $this->plugin_dir . 'assets'; 
  247. $this->assets_url = $this->plugin_url . 'assets'; 
  248.  
  249. /** 
  250. * Check if the plugin is activated network wide(in multisite) 
  251. *  
  252. * @since 1.1.0 
  253. * @access private 
  254. *  
  255. * @return boolean 
  256. */ 
  257. private function is_network_activated() { 
  258. $network_activated = false; 
  259. if ( is_multisite() ) { 
  260. if ( ! function_exists( 'is_plugin_active_for_network' ) ) 
  261. require_once( ABSPATH . '/wp-admin/includes/plugin.php' ); 
  262.  
  263. if( is_plugin_active_for_network( 'buddypress-global-search/buddypress-global-search.php' ) ) { 
  264. $network_activated = true; 
  265. return $network_activated; 
  266.  
  267. /** 
  268. * Set up the default hooks and actions. 
  269. * @since 1.0.0 
  270. * @access private 
  271. * @uses add_action() To add various actions. 
  272. */ 
  273. private function setup_actions() { 
  274. // Admin 
  275. add_action( 'init', array( $this, 'setup_admin_settings' ) ); 
  276.  
  277. // Hook into BuddyPress init 
  278. add_action( 'init', array( $this, 'init_load' ) ); 
  279.  
  280. /** 
  281. * Load plugin text domain 
  282. * @since 1.0.0 
  283. * @uses sprintf() Format .mo file 
  284. * @uses get_locale() Get language 
  285. * @uses file_exists() Check for language file 
  286. * @uses load_textdomain() Load language file 
  287. */ 
  288. public function setup_textdomain() { 
  289. $domain = 'buddypress-global-search'; 
  290. $locale = apply_filters('plugin_locale', get_locale(), $domain); 
  291.  
  292. //first try to load from wp-contents/languages/plugins/ directory 
  293. load_textdomain($domain, WP_LANG_DIR . '/plugins/' . $domain . '-' . $locale . '.mo'); 
  294.  
  295. //if not found, then load from buddyboss-global-search/languages/ directory 
  296. load_plugin_textdomain( 'buddypress-global-search', false, $this->lang_dir ); 
  297.  
  298. /** 
  299. * Setup plugin options settings admin page 
  300. */ 
  301. public function setup_admin_settings() { 
  302.  
  303. if ( ( is_admin() || is_network_admin() ) && current_user_can( 'manage_options' ) ) { 
  304. $this->load_admin(); 
  305.  
  306.  
  307. /** 
  308. * We require BuddyPress to run the main components, so we attach 
  309. * to the 'bp_init' action which BuddyPress calls after it's started 
  310. * up. This ensures any BuddyPress related code is only loaded 
  311. * when BuddyPress is active. 
  312. * @since 1.0.0 
  313. * @return void 
  314. */ 
  315. public function init_load() { 
  316. global $bp; 
  317.  
  318. $this->bp_enabled = true; 
  319.  
  320. $this->load_main(); 
  321.  
  322. /** Load 
  323. * =================================================================== 
  324. */ 
  325.  
  326. /** 
  327. * Include required admin files. 
  328. * @since 1.0.0 
  329. * @access private 
  330. * @uses $this->do_includes() Loads array of files in the include folder 
  331. */ 
  332. private function load_admin() { 
  333. $this->do_includes($this->admin_includes); 
  334.  
  335. $this->admin = BuddyBoss_Global_Search_Admin::instance(); 
  336.  
  337. /** 
  338. * Include required files. 
  339. * @since 1.0.0 
  340. * @access private 
  341. * @uses BuddyBoss_Global_Search_Plugin::do_includes() Loads array of files in the include folder 
  342. */ 
  343. private function load_main() {  
  344. $this->do_includes($this->main_includes); 
  345.  
  346. $this->search = BBoss_Global_Search_Helper::instance(); 
  347.  
  348. // Front End Assets 
  349. if ( ! is_admin() && ! is_network_admin() ) { 
  350. add_action( 'wp_enqueue_scripts', array( $this, 'assets' ) ); 
  351.  
  352. // Remove bp compose message deprecated autocomplete 
  353. remove_action( "bp_enqueue_scripts", "messages_add_autocomplete_js" ); 
  354. // remove_action("wp_head", "messages_add_autocomplete_css"); 
  355.  
  356. /** 
  357. * Load css/js files 
  358. *  
  359. * @since 1.0.0 
  360. * @return void 
  361. */ 
  362. public function assets() { 
  363. $min = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min'; 
  364. wp_enqueue_style( 'jquery-ui-search', $this->assets_url . '/css/jquery-ui.min.css', array(), '1.11.2' ); 
  365. // wp_enqueue_style( 'buddypress-global-search', $this->assets_url . '/css/buddypress-global-search.css', array(), '1.1.2' ); 
  366. wp_enqueue_style( 'buddypress-global-search', $this->assets_url . '/css/buddypress-global-search' . $min . '.css', array(), BUDDYBOSS_GLOBAL_SEARCH_PLUGIN_VERSION ); 
  367.  
  368.  
  369. wp_enqueue_script( 'jquery-ui-autocomplete' ); 
  370. //wp_enqueue_script( 'buddypress-global-search', $this->assets_url . '/js/buddypress-global-search.js', array( 'jquery', 'jquery-ui-autocomplete' ), '1.0.4', true ); 
  371. wp_enqueue_script( 'buddypress-global-search', $this->assets_url . '/js/buddypress-global-search' . $min . '.js', array( 'jquery', 'jquery-ui-autocomplete' ), BUDDYBOSS_GLOBAL_SEARCH_PLUGIN_VERSION, true ); 
  372.  
  373. if(function_exists("bp_is_messages_component")) { 
  374. // Include the autocomplete JS for composing a message. 
  375. if ( bp_is_messages_component() && bp_is_current_action( 'compose' ) ) { 
  376. add_action( 'wp_head', array($this, 'messages_autocomplete_init_jsblock') ); 
  377.  
  378. $data = array( 
  379. 'nonce' => wp_create_nonce( 'bboss_global_search_ajax' ),  
  380. 'action' => 'bboss_global_search_ajax',  
  381. 'debug' => true, //set it to false on production 
  382. 'ajaxurl' => admin_url( 'admin-ajax.php', is_ssl() ? 'admin' : 'http' ),  
  383. //'search_url' => home_url( '/' ), Now we are using form[role='search'] selector 
  384. 'loading_msg' => __("Loading Suggestions", "buddypress-global-search"),  
  385. 'enable_ajax_search' => $this->option( 'enable-ajax-search' ),  
  386. 'per_page' => $this->option( 'items-per-page' ) 
  387. ); 
  388.  
  389. if(isset($_GET["s"])) { 
  390. $data["search_term"] = $_GET["s"]; 
  391.  
  392. wp_localize_script( 'buddypress-global-search', 'BBOSS_GLOBAL_SEARCH', $data ); 
  393.  
  394. /** Print inline JS for initializing the bp messages autocomplete. 
  395. * Proper updated auto complete code for buddypress message compose (replacing autocompletefb script). 
  396. * @todo : Why this inline code is not at proper file. 
  397. * @clean: This is not working. 
  398. */ 
  399. public function messages_autocomplete_init_jsblock() { 
  400. ?> 
  401.  
  402. <script type="text/javascript"> 
  403. window.user_profiles = Array(); 
  404. jQuery(document).ready(function() { 
  405. var obj = jQuery(".send-to-input").autocomplete({ 
  406. source: function(request, response) { 
  407. jQuery("body").data("ac-item-p", "even"); 
  408. var term = request.term; 
  409. if (term in window.user_profiles) { 
  410. response(window.user_profiles[term]); 
  411. return; 
  412. var data = { 
  413. 'action': 'messages_autocomplete_results',  
  414. 'search_term': request.term 
  415. }; 
  416. jQuery.ajax({ 
  417. url: ajaxurl + '?q=' + request.term + '&limit=10',  
  418. data: data,  
  419. success: function(data) { 
  420. var new_data = Array(); 
  421. d = data.split("\n"); 
  422. jQuery.each(d, function(i, item) { 
  423. new_data[new_data.length] = item; 
  424. }); 
  425. if (data != "") {  
  426. response(new_data); 
  427. }); 
  428. },  
  429. minLength: 1,  
  430. select: function(event, ui) { 
  431. sel_item = ui.item; 
  432. var d = String(sel_item.label).split(' ('); 
  433. var un = d[1].substr(0, d[1].length - 1); 
  434. //check if it already exists; 
  435. if (0 === jQuery('.acfb-holder').find('#un-' + un).length) { 
  436. var ln = '#link-' + un; 
  437. var l = jQuery(ln).attr('href'); 
  438. var v = '<li class="selected-user friend-tab" id="un-' + un + '"><span><a href="' + l + '">' + d[0] + '</a></span> <span class="p">X</span></li>'; 
  439. if (jQuery(".acfb-holder").find(".friend-tab").length == 0) { 
  440. var x = jQuery('.acfb-holder').prepend(v); 
  441. } else { 
  442. var x = jQuery('.acfb-holder').find(".friend-tab").last().after(v); 
  443. jQuery('#send-to-usernames').addClass(un); 
  444. return false; 
  445. },  
  446. focus: function(event, ui) { 
  447. jQuery(".ui-autocomplete li").removeClass("ui-state-hover"); 
  448. jQuery(".ui-autocomplete").find("li:has(a.ui-state-focus)").addClass("ui-state-hover"); 
  449. return false; 
  450. }); 
  451.  
  452. obj.data("ui-autocomplete")._renderItem = function(ul, item) { 
  453. ul.addClass("ac_results"); 
  454. if (jQuery("body").data("ac-item-p") == "even") { 
  455. c = "ac_event"; 
  456. jQuery("body").data("ac-item-p", "odd"); 
  457. } else { 
  458. c = "ac_odd"; 
  459. jQuery("body").data("ac-item-p", "even"); 
  460. return jQuery("<li class='"+c+"'>").append("<a>" + item.label + "</a>").appendTo(ul); 
  461. }; 
  462.  
  463. obj.data("ui-autocomplete")._resizeMenu = function () { 
  464. var ul = this.menu.element; 
  465. ul.outerWidth(this.element.outerWidth()); 
  466. }; 
  467.  
  468. jQuery(document).on("click", ".selected-user", function() { 
  469. jQuery(this).remove(); 
  470. }); 
  471. jQuery('#send_message_form').submit(function() { 
  472. tosend = Array(); 
  473. jQuery(".acfb-holder").find(".friend-tab").each(function(i, item) { 
  474. un = jQuery(this).attr("id"); 
  475. un = un.replace('un-', ''); 
  476. tosend[tosend.length] = un; 
  477. }); 
  478. document.getElementById('send-to-usernames').value = tosend.join(" "); 
  479. }); 
  480. }); 
  481. </script> 
  482.  
  483. <?php 
  484.  
  485. /** Utility functions 
  486. * =================================================================== 
  487. */ 
  488.  
  489. /** 
  490. * Include required array of files in the includes directory 
  491. * @since 1.0.0 
  492. * @uses require_once() Loads include file 
  493. */ 
  494. public function do_includes($includes = array()) { 
  495. foreach ((array) $includes as $include) { 
  496. require_once( $this->includes_dir . '/' . $include . '.php' ); 
  497.  
  498. /** 
  499. * Convenience function to access plugin options, returns false by default 
  500. * @since 1.0.0 
  501. * @param string $key Option key 
  502.   
  503. * @uses apply_filters() Filters option values with 'buddyboss_global_search_option' & 
  504. * 'buddyboss_global_search_option_{$option_name}' 
  505. * @uses sprintf() Sanitizes option specific filter 
  506. * @return mixed Option value (false if none/default) 
  507. */ 
  508. public function option($key) { 
  509. $key = strtolower($key); 
  510. $option = isset($this->options[$key]) ? $this->options[$key] : null; 
  511.  
  512. // Apply filters on options as they're called for maximum 
  513. // flexibility. Options are are also run through a filter on 
  514. // class instatiation/load. 
  515. // ------------------------ 
  516. // This filter is run for every option 
  517. $option = apply_filters('buddyboss_global_search_option', $option); 
  518.  
  519. // Option specific filter name is converted to lowercase 
  520. $filter_name = sprintf('buddyboss_global_search_option_%s', strtolower($key)); 
  521. $option = apply_filters($filter_name, $option); 
  522.  
  523. return $option; 
  524.