/admin/class.main-options.php

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