BuddyBoss_Global_Search_Admin

BuddyPress Global Search Admin ********************.

Defined (1)

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

/includes/admin.php  
  1. class BuddyBoss_Global_Search_Admin { 
  2. /** Options/Load 
  3. * =================================================================== 
  4. */ 
  5.  
  6. /** 
  7. * Plugin options 
  8. * @var array 
  9. */ 
  10. public $options = array(); 
  11. private $plugin_settings_tabs = array(); 
  12.  
  13. private $network_activated = false,  
  14. $plugin_slug = 'buddyboss-globalsearch',  
  15. $menu_hook = 'admin_menu',  
  16. $settings_page = 'buddyboss-settings',  
  17. $capability = 'manage_options',  
  18. $form_action = 'options.php',  
  19. $plugin_settings_url; 
  20.  
  21. /** 
  22. * Empty constructor function to ensure a single instance 
  23. */ 
  24. public function __construct() { 
  25. // ... leave empty, see Singleton below 
  26.  
  27. /** Singleton 
  28. * =================================================================== 
  29. */ 
  30.  
  31. /** 
  32. * Admin singleton 
  33. * @since 1.0.0 
  34. * @param array $options [description] 
  35. * @uses BuddyBoss_Global_Search_Admin::setup() Init admin class 
  36. * @return object BuddyBoss_Global_Search_Admin 
  37. */ 
  38. public static function instance() { 
  39. static $instance = null; 
  40.  
  41. if (null === $instance) { 
  42. $instance = new BuddyBoss_Global_Search_Admin(); 
  43. $instance->setup(); 
  44.  
  45. return $instance; 
  46.  
  47. /** Utility functions 
  48. * =================================================================== 
  49. */ 
  50.  
  51. /** 
  52. * Get option 
  53. * @since BuddyPress Global Search (1.0.0) 
  54. * @param string $key Option key 
  55. * @uses BuddyBoss_Global_Search_Plugin::option() Get option 
  56. * @return mixed Option value 
  57. */ 
  58. public function option($key) { 
  59. $value = buddyboss_global_search()->option($key); 
  60. return $value; 
  61.  
  62. /** Actions/Init 
  63. * =================================================================== 
  64. */ 
  65.  
  66. /** 
  67. * Setup admin class 
  68. * @since BuddyPress Global Search (1.0.0) 
  69. * @uses buddyboss_global_search() Get options from main BuddyBoss_Global_Search_Plugin class 
  70. * @uses is_admin() Ensures we're in the admin area 
  71. * @uses curent_user_can() Checks for permissions 
  72. * @uses add_action() Add hooks 
  73. */ 
  74. public function setup() { 
  75. if ((!is_admin() && !is_network_admin() ) || !current_user_can('manage_options')) { 
  76. return; 
  77.  
  78. $this->plugin_settings_url = admin_url( 'admin.php?page=' . $this->plugin_slug ); 
  79.  
  80. $this->network_activated = $this->is_network_activated(); 
  81.  
  82. //if the plugin is activated network wide in multisite, we need to override few variables 
  83. if ( $this->network_activated ) { 
  84. // Main settings page - menu hook 
  85. $this->menu_hook = 'network_admin_menu'; 
  86.  
  87. // Main settings page - parent page 
  88. $this->settings_page = 'settings.php'; 
  89.  
  90. // Main settings page - Capability 
  91. $this->capability = 'manage_network_options'; 
  92.  
  93. // Settins page - form's action attribute 
  94. $this->form_action = 'edit.php?action=' . $this->plugin_slug; 
  95.  
  96. // Plugin settings page url 
  97. $this->plugin_settings_url = network_admin_url('settings.php?page=' . $this->plugin_slug); 
  98.  
  99. //if the plugin is activated network wide in multisite, we need to process settings form submit ourselves 
  100. if ( $this->network_activated ) { 
  101. add_action('network_admin_edit_' . $this->plugin_slug, array( $this, 'save_network_settings_page' )); 
  102.  
  103. /** 
  104. * Previously, settings were saved in options table to main site in network. 
  105. * Now, since network settings are saved and retrieved using update_site_option/get_site_option,  
  106. * all the sites who had the plugin activated netowrk wide, will loose their settings. 
  107. * Let's display a message. 
  108. */ 
  109. if ( $this->network_activated && current_user_can( 'manage_network_options' ) ) { 
  110. add_action( 'network_admin_notices', array( $this, 'admin_notice_update_settings' ) ); 
  111.  
  112. add_action( 'admin_init', array( $this, 'admin_init' ) ); 
  113. add_action( 'admin_init', array($this, 'register_support_settings' ) ); 
  114. add_action( $this->menu_hook, array( $this, 'admin_menu' ) ); 
  115.  
  116. add_filter( 'plugin_action_links', array( $this, 'add_action_links' ), 10, 2 ); 
  117. add_filter( 'network_admin_plugin_action_links', array( $this, 'add_action_links' ), 10, 2 ); 
  118.  
  119. /** 
  120. * Check if the plugin is activated network wide(in multisite). 
  121. *  
  122. * @return boolean 
  123. */ 
  124. private function is_network_activated() { 
  125. $network_activated = false; 
  126. if ( is_multisite() ) { 
  127. if ( !function_exists('is_plugin_active_for_network') ) 
  128. require_once( ABSPATH . '/wp-admin/includes/plugin.php' ); 
  129.  
  130. if ( is_plugin_active_for_network('buddypress-global-search/buddypress-global-search.php') ) { 
  131. $network_activated = true; 
  132. return $network_activated; 
  133.  
  134. /** 
  135. * Register admin settings 
  136. * @since 1.0.0 
  137. * @uses register_setting() Register plugin options 
  138. * @uses add_settings_section() Add settings page option sections 
  139. * @uses add_settings_field() Add settings page option 
  140. */ 
  141. public function admin_init() { 
  142.  
  143. $this->plugin_settings_tabs['buddyboss_global_search_plugin_options'] = 'General'; 
  144.  
  145. register_setting( 'buddyboss_global_search_plugin_options', 'buddyboss_global_search_plugin_options', array($this, 'plugin_options_validate')); 
  146. add_settings_section( 'general_section', __( 'General Settings', 'buddypress-global-search' ), array($this, 'section_general'), __FILE__); 
  147. //add_settings_section( 'style_section', 'Style Settings', array( $this, 'section_style' ), __FILE__ ); 
  148. //general options 
  149. add_settings_field('items-to-search', __( 'Items To Search', 'buddypress-global-search' ), array($this, 'setting_items_to_search'), __FILE__, 'general_section'); 
  150. add_settings_field('enable-ajax-search', __( 'AutoSuggest', 'buddypress-global-search' ), array($this, 'setting_enable_ajax_search'), __FILE__, 'general_section'); 
  151. add_settings_field('items-per-page', __( 'AutoSuggest number of items', 'buddypress-global-search' ), array($this, 'setting_items_per_page'), __FILE__, 'general_section'); 
  152.  
  153. function register_support_settings() { 
  154. $this->plugin_settings_tabs[ 'buddyboss_global_search_support_options' ] = __('Support', 'buddypress-global-search'); 
  155.  
  156. register_setting( 'buddyboss_global_search_support_options', 'buddyboss_global_search_support_options' ); 
  157. add_settings_section( 'section_support', ' ', array( &$this, 'section_support_desc' ), 'buddyboss_global_search_support_options' ); 
  158.  
  159. function section_support_desc() { 
  160. if ( file_exists( dirname( __FILE__ ) . '/help-support.php' ) ) { 
  161. require_once( dirname( __FILE__ ) . '/help-support.php' ); 
  162.  
  163. /** 
  164. * Add plugin settings page 
  165. * @since BuddyPress Global Search (1.0.0) 
  166. * @uses add_options_page() Add plugin settings page 
  167. */ 
  168. public function admin_menu() { 
  169. //add_options_page('BP Global Search', 'BP Global Search', 'manage_options', __FILE__, array($this, 'options_page')); 
  170. add_submenu_page( 
  171. $this->settings_page, 'BP Global Search', 'Global Search', $this->capability, $this->plugin_slug, array( $this, 'options_page' ) 
  172. ); 
  173.  
  174. /** Settings Page + Sections 
  175. * =================================================================== 
  176. */ 
  177.  
  178. /** 
  179. * Render settings page 
  180. * @since 1.0.0 
  181. * @uses do_settings_sections() Render settings sections 
  182. * @uses settings_fields() Render settings fields 
  183. * @uses esc_attr_e() Escape and localize text 
  184. */ 
  185. public function options_page() { 
  186. $tab = isset( $_GET['tab'] ) ? $_GET['tab'] : __FILE__; 
  187. ?> 
  188. <div class="wrap"> 
  189. <h2><?php _e( 'BuddyPress Global Search', 'buddypress-global-search' ); ?></h2> 
  190. <?php $this->plugin_options_tabs(); ?> 
  191. <div class="content-wrapper clearfix"> 
  192. <div class="settings"> 
  193. <div class="padder"> 
  194. <form method="post" action="<?php echo $this->form_action; ?>"> 
  195. <?php 
  196. if ( $this->network_activated && isset($_GET['updated']) ) { 
  197. echo "<div class='updated'><p>" . __('Settings updated.', 'buddypress-global-search') . "</p></div>"; 
  198.  
  199. if ( 'buddyboss_global_search_plugin_options' == $tab || empty($_GET['tab']) ) { 
  200. settings_fields( 'buddyboss_global_search_plugin_options' ); 
  201. do_settings_sections( __FILE__ ); ?> 
  202. <p class="submit"> 
  203. <input name="bboss_g_s_settings_submit" type="submit" class="button-primary" value="<?php esc_attr_e( 'Save Changes', 'buddypress-global-search' ); ?>" /> 
  204. </p><?php 
  205. } else { 
  206. settings_fields( $tab ); 
  207. do_settings_sections( $tab ); 
  208. } ?> 
  209.  
  210. </form> 
  211. </div> 
  212. </div> 
  213. <div style="clear: both"></div> 
  214. </div> 
  215. </div> 
  216. <?php 
  217.  
  218. function plugin_options_tabs() { 
  219. $current_tab = isset( $_GET[ 'tab' ] ) ? $_GET[ 'tab' ] : 'buddyboss_global_search_plugin_options'; 
  220.  
  221. echo '<h2 class="nav-tab-wrapper">'; 
  222. foreach ( $this->plugin_settings_tabs as $tab_key => $tab_caption ) { 
  223. $active = $current_tab == $tab_key ? 'nav-tab-active' : ''; 
  224. echo '<a class="nav-tab ' . $active . '" href="?page=' . 'buddyboss-globalsearch' . '&tab=' . $tab_key . '">' . $tab_caption . '</a>'; 
  225. echo '</h2>'; 
  226.  
  227. /** 
  228. * General settings section 
  229. * @since BuddyPress Global Search (1.0.0) 
  230. */ 
  231. public function section_general() { 
  232.  
  233.  
  234. /** 
  235. * Style settings section 
  236. * @since BuddyPress Global Search (1.0.0) 
  237. */ 
  238. public function section_style() { 
  239.  
  240.  
  241. /** 
  242. * Validate plugin option 
  243. * @since 1.0.0 
  244. */ 
  245. public function plugin_options_validate($input) { 
  246. if( !isset( $input['enable-ajax-search'] ) || !$input['enable-ajax-search'] ) 
  247. $input['enable-ajax-search'] = 'no'; 
  248.  
  249. return $input; // return validated input 
  250.  
  251. /** Settings Page Options 
  252. * =================================================================== 
  253. */ 
  254.  
  255. /** 
  256. * Setting > Whether to have autosuggest search dropdown 
  257. * @since 1.0.3 
  258. * @uses BuddyBoss_Global_Search_Admin::option() Get plugin option 
  259. */ 
  260. public function setting_enable_ajax_search() { 
  261. $enabled = $this->option('enable-ajax-search'); 
  262. $checked = $enabled=='yes' ? ' checked' : ''; 
  263. echo '<label><input type="checkbox" name="buddyboss_global_search_plugin_options[enable-ajax-search]" value="yes" '. $checked . '>' . __( 'Enable AutoSuggest dropdown in search inputs.', 'buddypress-global-search' ) . '</label>'; 
  264.  
  265. /** 
  266. * Setting > what to search? 
  267. * @since 1.0.0 
  268. * @uses BuddyBoss_Global_Search_Admin::option() Get plugin option 
  269. */ 
  270. public function setting_items_to_search() { 
  271. $items_to_search = $this->option('items-to-search'); 
  272.  
  273. echo '<p class="description">' . __('Search the following components:', 'buddypress-global-search') . '</p><br />'; 
  274.  
  275. $items = buddyboos_global_search_items(); 
  276.  
  277. //now print those items 
  278. foreach( $items as $item=>$label ) { 
  279. $checked = !empty( $items_to_search ) && in_array( $item, $items_to_search ) ? ' checked' : ''; 
  280. echo "<label><input type='checkbox' value='{$item}' name='buddyboss_global_search_plugin_options[items-to-search][]' {$checked}>{$label}</label><br>"; 
  281. do_action( 'bboss_global_search_settings_item_'.$item, $items_to_search ); 
  282.  
  283. /** 
  284. * Use the action below to add more things in the list of searchable items. 
  285. * This will just print those new items in admin section. You'll have hook into other actions/filters to actually perform the search. 
  286. */ 
  287. do_action( 'bboss_global_search_settings_items_to_search', $items_to_search ); 
  288.  
  289.  
  290. /** 
  291. * Setting > Number of items in autosuggest search dropdown 
  292. * @since 1.0.3 
  293. * @uses BuddyBoss_Global_Search_Admin::option() Get plugin option 
  294. */ 
  295. public function setting_items_per_page() { 
  296. $value = $this->option('items-per-page'); 
  297.  
  298. echo '<input type="number" name="buddyboss_global_search_plugin_options[items-per-page]" value="'. $value .'"/>'; 
  299.  
  300. public function add_action_links( $links, $file ) { 
  301. // Return normal links if not this plugin 
  302. if ( plugin_basename(basename(constant('BUDDYBOSS_GLOBAL_SEARCH_PLUGIN_DIR')) . '/buddypress-global-search.php') != $file ) { 
  303. return $links; 
  304.  
  305. $mylinks = array( 
  306. '<a href="' . esc_url($this->plugin_settings_url) . '">' . __("Settings", "buddypress-global-search") . '</a>',  
  307. ); 
  308. return array_merge($links, $mylinks); 
  309.  
  310. public function save_network_settings_page() { 
  311. if ( !check_admin_referer('buddyboss_global_search_plugin_options-options') ) 
  312. return; 
  313.  
  314. if ( !current_user_can($this->capability) ) 
  315. die('Access denied!'); 
  316.  
  317. if ( isset( $_POST['bboss_g_s_settings_submit'] ) ) { 
  318. $submitted = stripslashes_deep($_POST['buddyboss_global_search_plugin_options']); 
  319. $submitted = $this->plugin_options_validate($submitted); 
  320.  
  321. update_site_option('buddyboss_global_search_plugin_options', $submitted); 
  322.  
  323. // Where are we redirecting to? 
  324. $base_url = trailingslashit(network_admin_url()) . 'settings.php'; 
  325. $redirect_url = esc_url_raw(add_query_arg(array( 'page' => $this->plugin_slug, 'updated' => 'true' ), $base_url)); 
  326.  
  327. // Redirect 
  328. wp_redirect($redirect_url); 
  329. die(); 
  330.  
  331. public function admin_notice_update_settings() { 
  332. //hide notice if user has selected to do so 
  333. if( isset( $_GET['page'] ) && $_GET['page']==$this->plugin_slug && isset( $_GET['hidenotice'] ) ) { 
  334. update_site_option( 'bboss_g_s_upgrade_from_1_0_7', 'yes' ); 
  335.  
  336. //dont display it if user had hidden this message 
  337. if( get_site_option( 'bboss_g_s_upgrade_from_1_0_7', 'no' ) == 'yes' ) 
  338. return; 
  339.  
  340. // Where are we redirecting to? 
  341. $base_url = trailingslashit( network_admin_url() ) . 'settings.php'; 
  342. $settings_url = esc_url(add_query_arg( array( 'page' => $this->plugin_slug ), $base_url )); 
  343. $settings_link = "<a href='" . esc_url( $settings_url ) . "'>" . __( 'Settings', 'buddypress-global-search' ) . "</a>"; 
  344.  
  345. $notice = sprintf( __( "Hey! BuddyPress Global Search has better integration with multisite now. Your settings might have been reset to defaults after update. Please check your %s.", 'buddypress-global-search' ), $settings_link ); 
  346.  
  347. $hide_notice_url = esc_url(add_query_arg( array( 'hidenotice' => true ), $settings_url )); 
  348.  
  349. echo "<div class='update-nag'><p>{$notice}</p><p><a href='{$hide_notice_url}' class='button'>". __( 'Hide this notice', 'buddypress-global-search' ) ."</a></div>";