DB_Twitter_Feed_Main_Options

The DevBuddy Twitter Feed Plugin DB Twitter Feed Main Options class.

Defined (1)

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

/admin/class.main-options.php  
  1. class DB_Twitter_Feed_Main_Options extends DB_Plugin_WP_Admin_Helper { 
  2.  
  3. /** 
  4. * @var array Holds important information about sections on the settings page 
  5. * @since 1.0.0 
  6. */ 
  7. private $sections = array(); 
  8.  
  9. /** 
  10. * @var array Holds important information about individual settings on the settings page 
  11. * @since 1.0.0 
  12. */ 
  13. private $settings = array(); 
  14.  
  15. /** 
  16. * @var string The prefix used to ensure that the IDs of HTML items are unique to the plugin 
  17. * @since 2.0.0 
  18. */ 
  19. protected $html_item_id_prefix; 
  20.  
  21.  
  22. /** 
  23. * Sets up the settings and initialises them within WordPress 
  24. * @access public 
  25. * @return void 
  26. * @since 1.0.0 
  27. */ 
  28. public function __construct() { 
  29. $this->set_main_admin_vars(); 
  30.  
  31. $this->html_item_id_prefix = $this->plugin_short_name.'_'; 
  32.  
  33. $this->set_sections(); 
  34. $this->set_settings(); 
  35.  
  36. add_action( 'admin_menu', array( $this, 'add_menu_item' ) ); 
  37. add_action( 'admin_init', array( $this, 'init_options' ) ); 
  38. add_action( 'admin_head', array( $this, 'set_admin_vars_js' ) ); 
  39. add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts_styles' ) ); 
  40.  
  41.  
  42. /** 
  43. * Establish the details of the sections to be rendered by WP on this settings page 
  44. * @access private 
  45. * @return void 
  46. * @since 1.0.0 
  47. */ 
  48. private function set_sections() { 
  49. $this->sections = 
  50. array( 
  51. 'cache' => array( 
  52. 'id' => 'cache_sec',  
  53. 'title' => 'Cache Management',  
  54. 'callback' => array( $this, 'write_cache_sec' ),  
  55. 'page' => $this->page_uri_main 
  56. ),  
  57. 'config' => array( 
  58. 'id' => 'configuration_sec',  
  59. 'title' => 'Configuration',  
  60. 'callback' => array( $this, 'write_configuration_sec' ),  
  61. 'page' => $this->page_uri_main 
  62. ),  
  63. 'feed' => array( 
  64. 'id' => 'feed_sec',  
  65. 'title' => 'Feed Settings',  
  66. 'callback' => array( $this, 'write_feed_sec' ),  
  67. 'page' => $this->page_uri_main 
  68. ),  
  69. 'settings' => array( 
  70. 'id' => 'settings_sec',  
  71. 'title' => 'General Settings',  
  72. 'callback' => array( $this, 'write_settings_sec' ),  
  73. 'page' => $this->page_uri_main 
  74. ); 
  75.  
  76.  
  77. /** 
  78. * Establish the details of the settings to be rendered by WP on this settings page 
  79. * @access private 
  80. * @return void 
  81. * @since 1.0.0 
  82. */ 
  83. private function set_settings() { 
  84. $this->settings = 
  85. array( 
  86. 'consumer_key' => array( 
  87. 'id' => 'consumer_key',  
  88. 'title' => 'Consumer Key',  
  89. 'callback' => array( $this, 'write_consumer_key_field' ),  
  90. 'page' => $this->page_uri_main,  
  91. 'section' => 'configuration_sec',  
  92. 'args' => '' 
  93. ),  
  94. 'consumer_secret' => array( 
  95. 'id' => 'consumer_secret',  
  96. 'title' => 'Consumer Secret',  
  97. 'callback' => array( $this, 'write_consumer_secret_field' ),  
  98. 'page' => $this->page_uri_main,  
  99. 'section' => 'configuration_sec',  
  100. 'args' => '' 
  101. ),  
  102. 'oauth_access_token' => array( 
  103. 'id' => 'oauth_access_token',  
  104. 'title' => 'OAuth Access Token',  
  105. 'callback' => array( $this, 'write_oauth_access_token_field' ),  
  106. 'page' => $this->page_uri_main,  
  107. 'section' => 'configuration_sec',  
  108. 'args' => '' 
  109. ),  
  110. 'oauth_access_token_secret' => array( 
  111. 'id' => 'oauth_access_token_secret',  
  112. 'title' => 'OAuth Access Token Secret',  
  113. 'callback' => array( $this, 'write_oauth_access_token_secret_field' ),  
  114. 'page' => $this->page_uri_main,  
  115. 'section' => 'configuration_sec',  
  116. 'args' => '' 
  117. ),  
  118. 'feed_type' => array( 
  119. 'id' => 'feed_type',  
  120. 'title' => 'Feed Type',  
  121. 'callback' => array( $this, 'write_radio_fields' ),  
  122. 'page' => $this->page_uri_main,  
  123. 'section' => 'feed_sec',  
  124. 'args' => array( 
  125. 'no_label' => TRUE,  
  126. 'options' => array( 
  127. 'Timeline' => 'user_timeline',  
  128. 'Search' => 'search' 
  129. ),  
  130. 'user' => array( 
  131. 'id' => 'twitter_username',  
  132. 'title' => 'Twitter Username',  
  133. 'callback' => array( $this, 'write_twitter_username_field' ),  
  134. 'page' => $this->page_uri_main,  
  135. 'section' => 'feed_sec',  
  136. 'args' => array( 
  137. 'attr' => array( 
  138. 'class' => 'input_feed_type' 
  139. ),  
  140. 'search_term' => array( 
  141. 'id' => 'search_term',  
  142. 'title' => 'Search Term',  
  143. 'callback' => array( $this, 'write_search_term_field' ),  
  144. 'page' => $this->page_uri_main,  
  145. 'section' => 'feed_sec',  
  146. 'args' => array( 
  147. 'desc' => 'Searches with or without a hashtag are acceptable',  
  148. 'attr' => array( 
  149. 'class' => 'input_feed_type' 
  150. ),  
  151. 'result_count' => array( 
  152. 'id' => 'result_count',  
  153. 'title' => 'Number of tweets to show',  
  154. 'callback' => array( $this, 'write_numeric_dropdown_field' ),  
  155. 'page' => $this->page_uri_main,  
  156. 'section' => 'settings_sec',  
  157. 'args' => array( 
  158. 'min' => 1,  
  159. 'max' => 30 
  160. ),  
  161. 'cache_hours' => array( 
  162. 'id' => 'cache_hours',  
  163. 'title' => 'Cache the feed for how many hours?',  
  164. 'callback' => array( $this, 'write_numeric_dropdown_field' ),  
  165. 'page' => $this->page_uri_main,  
  166. 'section' => 'settings_sec',  
  167. 'args' => array( 
  168. 'min' => 0,  
  169. 'max' => 24,  
  170. 'desc' => '<p class="description">Select 0 if you don’t wish to cache the feed</p>' ) 
  171. ),  
  172. 'exclude_replies' => array( 
  173. 'id' => 'exclude_replies',  
  174. 'title' => 'Exclude replies?',  
  175. 'callback' => array( $this, 'write_checkbox_field' ),  
  176. 'page' => $this->page_uri_main,  
  177. 'section' => 'settings_sec',  
  178. 'args' => array( 
  179. 'desc' => '<p class="description">Twitter removes replies only after it retrieves the number of tweets you request.<br />Thus if you choose 10, and out of that 10 6 are replies, only 4 tweets will be displayed.</p>' 
  180. ),  
  181. 'default_styling' => array( 
  182. 'id' => 'default_styling',  
  183. 'title' => 'Load default stylesheet?',  
  184. 'callback' => array( $this, 'write_checkbox_field' ),  
  185. 'page' => $this->page_uri_main,  
  186. 'section' => 'settings_sec',  
  187. 'args' => '' 
  188. )/**,  
  189. '' => array( 
  190. 'id' => '',  
  191. 'title' => '',  
  192. 'callback' => array( $this, 'write__field' ),  
  193. 'page' => $this->page_uri_main,  
  194. 'section' => '' 
  195. 'args' => '' 
  196. )*/ 
  197. ); 
  198.  
  199. foreach ( $this->settings as $name => $setting ) { 
  200. $id = $setting['id']; 
  201. $title = $setting['title']; 
  202. $html_item_id = $this->html_item_id_prefix.$id; 
  203.  
  204. // Wrap title in label, add it to appropriate $settings property 
  205. $no_label = ( isset( $setting['args']['no_label'] ) ) ? $setting['args']['no_label'] : FALSE; 
  206. if ( $no_label !== TRUE ) { 
  207. $this->settings[ $name ]['title'] = '<label for="'.$html_item_id.'">'.$title.'</label>'; 
  208.  
  209.  
  210. // Add standard data to the arguments of the setting 
  211. if ( is_array( $setting['args'] ) ) { 
  212. $this->settings[ $name ]['args']['option'] = $id; 
  213. } else { 
  214. $this->settings[ $name ]['args'] = array( 'option' => $id ); 
  215.  
  216. $this->settings[ $name ]['args']['html_item_id'] = $html_item_id; 
  217.  
  218.  
  219.  
  220. /** 
  221. * Load JavaScripts and styles necessary for the page 
  222. * @access public 
  223. * @return void 
  224. * @since 2.0.0 
  225. */ 
  226. public function enqueue_scripts_styles( $hook ) { 
  227. if ( $hook != 'settings_page_db-twitter-feed-settings' ) { 
  228. return; 
  229. wp_enqueue_style( $this->plugin_name.'_admin_styles', DBTF_URL.'/assets/main-admin.css', NULL, '1.0.1', 'all' ); 
  230. wp_enqueue_script( $this->plugin_name.'_admin_functions', DBTF_URL.'/assets/main-admin.js', array( 'jquery-core' ), '1.0.0', true ); 
  231.  
  232.  
  233. /** 
  234. * Create global JavaScript object that will hold certain plugin information 
  235. * @access public 
  236. * @return void 
  237. * @since 2.0.0 
  238. */ 
  239. public function set_admin_vars_js() { 
  240. $br = "\n"; 
  241. $tab = ' '; 
  242.  
  243. $class_name = strtoupper($this->plugin_short_name); 
  244.  
  245. $output = $br.'<script type="text/javascript">'.$br; 
  246. $output .= $tab.'var '.$class_name.' = '.$class_name.' || {};'.$br; 
  247.  
  248. $output .= $tab.$class_name.'.pluginName = \''.$this->plugin_name.'\';'.$br; 
  249. $output .= $tab.$class_name.'.pluginShortName = \''.$this->plugin_short_name.'\';'.$br; 
  250.  
  251. $output .= $tab.$class_name.'.optionsNameMain = \''.$this->options_name_main.'\';'.$br; 
  252. $output .= $tab.$class_name.'.optionsGroup = \''.$this->options_group_main.'\';'.$br; 
  253.  
  254. $output .= '</script>'.$br.$br; 
  255.  
  256. echo $output; 
  257.  
  258.  
  259. /** 
  260. * Add the item to the WordPress admin menu and call the function that renders the markup 
  261. * @access public 
  262. * @return void 
  263. * @since 1.0.0 
  264. */ 
  265. public function add_menu_item() { 
  266. add_submenu_page( 
  267. 'options-general.php',  
  268. 'Configure your Twitter feed set up',  
  269. 'Twitter Feed Settings',  
  270. 'manage_options',  
  271. $this->page_uri_main,  
  272. array( $this, 'settings_page_markup' ) 
  273. ); 
  274.  
  275.  
  276. /** 
  277. * Officially register the sections/settings with WordPress 
  278. * @access public 
  279. * @return void 
  280. * @since 1.0.0 
  281. */ 
  282. public function init_options() { 
  283. register_setting( $this->options_group_main, $this->options_name_main, array( $this, 'sanitize_settings_submission' ) ); 
  284.  
  285. // Loop through the Sections/Settings arrays and add them to WordPress 
  286. foreach ( $this->sections as $section ) { 
  287. add_settings_section( 
  288. $section['id'],  
  289. $section['title'],  
  290. $section['callback'],  
  291. $section['page'] 
  292. ); 
  293. foreach ( $this->settings as $setting ) { 
  294. add_settings_field( 
  295. $setting['id'],  
  296. $setting['title'],  
  297. $setting['callback'],  
  298. $setting['page'],  
  299. $setting['section'],  
  300. $setting['args'] 
  301. ); 
  302.  
  303.  
  304. /************************************************************************************************************** 
  305. Callbacks for writing the option fields themselves to the options page 
  306. **************************************************************************************************************/ 
  307. /** 
  308. * Write the markup for the settings page 
  309. * This method also checks to see if settings have been updated. If they have 
  310. * the method will clear the cache of the ID currently in the twitter_username 
  311. * field. 
  312. * @access public 
  313. * @return void 
  314. * @since 1.0.0 
  315. */ 
  316. public function settings_page_markup() { 
  317. if ( ! current_user_can( 'manage_options' ) ) { 
  318. wp_die( __( 'You do not have sufficient permissions to access this page.' ) ); 
  319. } ?> 
  320.  
  321. <div id="<?php echo $this->plugin_short_name ?>" class="wrap"> 
  322.  
  323. <?php screen_icon() ?> 
  324. <h2>Twitter Feed Settings</h2> 
  325.  
  326. <form id="<?php echo $this->plugin_name ?>_settings" action="options.php" method="post"> 
  327. <?php 
  328. settings_fields( $this->options_group_main ); 
  329. do_settings_sections( $this->page_uri_main ); 
  330.  
  331. submit_button( 'Save Changes' ) 
  332. ?> 
  333. </form> 
  334.  
  335. </div><!--END-<?php echo $this->plugin_short_name ?>--> 
  336. <?php } 
  337.  
  338.  
  339. /** 
  340. * Takes a string and returns it with the plugin's shortname prefixed to it 
  341. * This method should only be used to prefix HTML 
  342. * id attributes 
  343. * @access protected 
  344. * @return string 
  345. * @since 2.0.0 
  346. * @param string $item_id The ID of the item to be prefixed 
  347. */ 
  348. protected function _html_item_id_attr( $item_id ) { 
  349. return $this->html_item_id_prefix.$item_id; 
  350.  
  351.  
  352. /** Write Cache Management section 
  353. *******************************************/ 
  354. /** 
  355. * Output the Cache management section and its fields 
  356. * @access public 
  357. * @return void 
  358. * @since 2.0.0 
  359. */ 
  360. public function write_cache_sec() { 
  361. echo 'Select a cache segment to clear.'; 
  362. echo '<div class="' . $this->plugin_short_name . '_cache_management_section settings_item">'; 
  363.  
  364. echo '<select name="' . $this->options_name_main . '[cache_segment]"> 
  365. <option value="0">--</option> 
  366. <option value="user_timeline">User timelines</option> 
  367. <option value="search">Searches</option> 
  368. <option value="all">All</option> 
  369. </select>'; 
  370.  
  371. echo '<input type="hidden" id="' . $this->plugin_short_name . '_cache_clear_flag" name="' . $this->options_name_main . '[cache_clear_flag]" value="0" />'; 
  372.  
  373. echo get_submit_button( 'Clear Cache', 'secondary', $this->plugin_short_name . '_batch_clear_cache' ); 
  374. echo '</div>'; 
  375.  
  376.  
  377. /** 
  378. * Output batch clear cache field 
  379. * @access public 
  380. * @return void 
  381. * @since 2.0.0 
  382. */ 
  383. public function write_cache_segment_field() { 
  384. echo ''; 
  385.  
  386.  
  387. /** 
  388. * Output batch clear clear flag field 
  389. * @access public 
  390. * @return void 
  391. * @since 2.0.0 
  392. */ 
  393. public function write_cache_clear_flag() { 
  394. echo ''; 
  395.  
  396.  
  397. /** Write Configuration section 
  398. *******************************************/ 
  399. /** 
  400. * Output the section as set in the set_sections() method along with a little bit of guidance 
  401. * @access public 
  402. * @return void 
  403. * @since 1.0.0 
  404. */ 
  405. public function write_configuration_sec() { 
  406. echo 'You\'ll need to log into the Twitter Developers site and set up an app. Once you\'ve set one up you will get the data necessary for below. For a step by step, see the <a href="http://wordpress.org/plugins/devbuddy-twitter-feed/installation/" target="_blank">walkthrough</a>.'; 
  407.  
  408.  
  409. /** 
  410. * Output the Consumer Key setting's field 
  411. * @access public 
  412. * @return void 
  413. * @since 1.0.0 
  414. */ 
  415. public function write_consumer_key_field( $args ) { 
  416. $consumer_key = $this->get_db_plugin_option( $this->options_name_main, 'consumer_key' ); 
  417. $consumer_key = $this->mask_data( $consumer_key ); 
  418.  
  419. echo '<input type="text" id="'.$this->_html_item_id_attr( $args['option'] ).'" name="'.$this->options_name_main.'[consumer_key]" value="'.$consumer_key.'" style="width:450px;" />'; 
  420.  
  421.  
  422. /** 
  423. * Output the Consumer Secret setting's field 
  424. * @access public 
  425. * @return void 
  426. * @since 1.0.0 
  427. */ 
  428. public function write_consumer_secret_field( $args ) { 
  429. $consumer_secret = $this->get_db_plugin_option( $this->options_name_main, 'consumer_secret' ); 
  430. $consumer_secret = $this->mask_data( $consumer_secret ); 
  431.  
  432. echo '<input type="text" id="'.$this->_html_item_id_attr( $args['option'] ).'" name="'.$this->options_name_main.'[consumer_secret]" value="'.$consumer_secret.'" style="width:450px;" />'; 
  433.  
  434.  
  435. /** 
  436. * Output the OAuth Access Token setting's field 
  437. * @access public 
  438. * @return void 
  439. * @since 1.0.0 
  440. */ 
  441. public function write_oauth_access_token_field( $args ) { 
  442. $oauth_access_token = $this->get_db_plugin_option( $this->options_name_main, 'oauth_access_token' ); 
  443.  
  444. $oat_arr = explode( '-', $oauth_access_token ); 
  445. $start = strlen( $oat_arr[0] ); 
  446.  
  447. $oauth_access_token = $this->mask_data( $oauth_access_token, $start ); 
  448.  
  449. echo '<input type="text" id="'.$this->_html_item_id_attr( $args['option'] ).'" name="'.$this->options_name_main.'[oauth_access_token]" value="'.$oauth_access_token.'" style="width:450px;" />'; 
  450.  
  451.  
  452. /** 
  453. * Output the OAuth Access Token Secret setting's field 
  454. * @access public 
  455. * @return void 
  456. * @since 1.0.0 
  457. */ 
  458. public function write_oauth_access_token_secret_field( $args ) { 
  459. $oauth_access_token_secret = $this->get_db_plugin_option( $this->options_name_main, 'oauth_access_token_secret' ); 
  460. $oauth_access_token_secret = $this->mask_data( $oauth_access_token_secret ); 
  461.  
  462. echo '<input type="text" id="'.$this->_html_item_id_attr( $args['option'] ).'" name="'.$this->options_name_main.'[oauth_access_token_secret]" value="'.$oauth_access_token_secret.'" style="width:450px;" />'; 
  463.  
  464.  
  465. /** Write Feed Settings section 
  466. *******************************************/ 
  467. /** 
  468. * Output the section as set in the set_sections() method 
  469. * @access public 
  470. * @return void 
  471. * @since 1.0.0 
  472. */ 
  473. public function write_feed_sec() { 
  474. echo ''; 
  475.  
  476.  
  477. /** 
  478. * Output the Twitter username setting's field 
  479. * @access public 
  480. * @return void 
  481. * @since 1.0.0 
  482. */ 
  483. public function write_twitter_username_field( $args ) { 
  484. $twitter_username = $this->get_db_plugin_option( $this->options_name_main, 'twitter_username' ); 
  485.  
  486. echo '<strong>twitter.com/<input type="text" id="'.$this->_html_item_id_attr( $args['option'] ).'" name="'.$this->options_name_main.'[twitter_username]"'; 
  487.  
  488. if ( $twitter_username ) { 
  489. echo ' value="'.$twitter_username.'"'; 
  490.  
  491. echo ( isset( $args['attr'] ) ) ? $this->write_attr( $args['attr'] ) : ''; 
  492.  
  493. echo ' /></strong>'; 
  494.  
  495. echo '<input type="hidden" name="'.$this->options_name_main.'[twitter_username_hid]"'; 
  496.  
  497. if ( $twitter_username ) { 
  498. echo ' value="'.$twitter_username.'"'; 
  499.  
  500. echo ' />'; 
  501.  
  502. echo ( isset( $args['desc'] ) ) ? $this->write_desc( $args['desc'] ) : ''; 
  503.  
  504.  
  505. /** 
  506. * Output the Twitter username setting's field 
  507. * @access public 
  508. * @return void 
  509. * @since 1.0.0 
  510. */ 
  511. public function write_search_term_field( $args ) { 
  512. $search_term = $this->get_db_plugin_option( $this->options_name_main, 'search_term' ); 
  513.  
  514. echo '<input type="text" id="'.$this->_html_item_id_attr( $args['option'] ).'" name="'.$this->options_name_main.'[search_term]"'; 
  515.  
  516. if ( $search_term ) { 
  517. echo ' value="'.$search_term.'"'; 
  518.  
  519. echo ( isset( $args['attr'] ) ) ? $this->write_attr( $args['attr'] ) : ''; 
  520.  
  521. echo ' />'; 
  522.  
  523. echo '<input type="hidden" name="'.$this->options_name_main.'[search_term_hid]"'; 
  524.  
  525. if ( $search_term ) { 
  526. echo ' value="'.$search_term.'"'; 
  527.  
  528. echo ' />'; 
  529.  
  530. echo ( isset( $args['desc'] ) ) ? $this->write_desc( $args['desc'] ) : ''; 
  531.  
  532.  
  533. /** Write General Settings section 
  534. *******************************************/ 
  535. /** 
  536. * Output the section as set in the set_sections() method 
  537. * @access public 
  538. * @return void 
  539. * @since 1.0.0 
  540. */ 
  541. public function write_settings_sec() { 
  542. echo ''; 
  543.  
  544. }// END class