DevBuddy_Feed_Plugin

The DevBuddy Twitter Feed Plugin DevBuddy Feed Plugin class.

Defined (1)

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

/lib/class.plugin-base.php  
  1. class DevBuddy_Feed_Plugin { 
  2.  
  3. /** 
  4. * @var string The name of the plugin to be used within the code 
  5. */ 
  6. public $plugin_name; 
  7.  
  8. /** 
  9. * @var mixed Holds raw feed data returned from API after main request is made 
  10. */ 
  11. public $feed_data; 
  12.  
  13. /** 
  14. * @var array Holds the configuration options once the feed class has been instantiated 
  15. */ 
  16. public $options; 
  17.  
  18. /** 
  19. * @var string The output of the entire feed will be stored here 
  20. */ 
  21. public $output = ''; 
  22.  
  23. /** 
  24. * @var int The number of feed items that have been rendered 
  25. */ 
  26. private $item_count = 0; 
  27.  
  28. /** 
  29. * @var bool A boolean indication of whether or not a cached version of the output is available 
  30. */ 
  31. public $is_cached; 
  32.  
  33. /** 
  34. * @var bool A boolean indication of whether or not the feed has been called via shortcode 
  35. */ 
  36. public $is_shortcode_called = FALSE; 
  37.  
  38. /** 
  39. * @var string The width of the display picture when set by the user 
  40. */ 
  41. private $dp_width; 
  42.  
  43. /** 
  44. * @var string The height of the display picture when set by the user 
  45. */ 
  46. private $dp_height; 
  47.  
  48.  
  49. /** 
  50. * Used to get the value of an option stored in the database 
  51. *  
  52. * Option data is typically stored within an array of values 
  53. * under one option entry within the WordPress database. So 
  54. * to get an option's value you need to provide the option 
  55. * entry along with the specific option you want the value 
  56. * of. 
  57. * @access public 
  58. * @return mixed The value of the option you're looking for or FALSE if no value exists 
  59. * @param string $option_entry The option name that WP recognises as an entry. Passing only this will return all option data for that entry 
  60. * @param string $option_name The name of the specific plugin option you want the value of 
  61. * @since 1.0.1 
  62. */ 
  63. public function get_option( $option_entry, $option_name = NULL ) { 
  64. $options = get_option( $option_entry ); 
  65.  
  66. if ( ! $options ) { 
  67. return FALSE; 
  68.  
  69. if ( $option_name === NULL ) { 
  70. return $options; 
  71.  
  72. } else { 
  73. if ( isset( $options[ $option_name ] ) && $options[ $option_name ] != '' ) { 
  74. return $options[ $option_name ]; 
  75. } else { 
  76. return FALSE; 
  77.  
  78.  
  79. /** 
  80. * An alias of DevBuddy_Feed_Plugin::get_option() 
  81. * @access public 
  82. * @return mixed The value of the option you're looking for or FALSE if no value exists 
  83. * @param string $option_entry The option name that WP recognises as an entry 
  84. * @param string $option_name The name of the specific plugin option you want the value of 
  85. * @since 1.0.0 
  86. */ 
  87. public function get_db_plugin_option( $option_entry, $option_name = NULL ) { 
  88. return $this->get_option( $option_entry, $option_name ); 
  89.  
  90.  
  91. /** 
  92. * Update a specific plugin option 
  93. * @access protected 
  94. * @return bool An indication of whether or not the update was successful 
  95. * @param string $option_entry The option name that WP recognises as an entry 
  96. * @param string $option_name The name of the specific plugin option you want to update 
  97. * @param mixed $new_value The value with which to update the option 
  98. * @since 1.1.0 
  99. */ 
  100. protected function update_option( $option_entry, $option_name, $new_value ) { 
  101. $options = get_option( $option_entry ); 
  102. $options[ $option_name ] = $new_value; 
  103.  
  104. if ( update_option( $option_entry, $options ) ) { 
  105. return TRUE; 
  106. } else { 
  107. return FALSE; 
  108.  
  109.  
  110. /** 
  111. * Update a specific plugin option 
  112. * @access protected 
  113. * @return bool An indication of whether or not the update was successful 
  114. * @param string $option_entry The option name that WP recognises as an entry 
  115. * @param string $option_name The name of the specific plugin option you want to update 
  116. * @param mixed $new_value The value with which to update the option 
  117. * @since 1.1.0 
  118. */ 
  119. protected function update_db_plugin_option( $option_entry, $option_name, $new_value ) { 
  120. return $this->update_option( $option_entry, $option_name, $new_value ); 
  121.  
  122.  
  123. /** 
  124. * Increase the feed item count by one 
  125. * @access public 
  126. * @return void 
  127. * @since 1.0.1 
  128. */ 
  129. public function increase_feed_item_count() { 
  130. $this->item_count++; 
  131.  
  132.  
  133. /** 
  134. * Return the current item count of the current feed 
  135. * @access public 
  136. * @return int 
  137. * @since 1.0.1 
  138. */ 
  139. public function get_item_count() { 
  140. return $this->item_count; 
  141.  
  142.  
  143. /** 
  144. * Cache whatever is in the DevBuddy_Feed_Plugin::$output property 
  145. * This method also sets the DevBuddy_Feed_Plugin::$is_cached property 
  146. * to TRUE once the cache is set. 
  147. * @access public 
  148. * @return void 
  149. * @since 1.0.0 
  150. * @param int $hours The number of hours the output should be cached for 
  151. */ 
  152. public function cache_output( $hours = 0 ) { 
  153. if ( (int) $hours !== 0 ) { 
  154. set_transient( $this->plugin_name . '_output_' . $this->options['user'], $this->output, 3600 * $hours ); 
  155.  
  156. $cache_successful = get_transient( $this->plugin_name . '_output_' . $this->options['user'] ); 
  157.  
  158. if ( $cache_successful ) { 
  159. $this->is_cached = TRUE; 
  160.  
  161.  
  162. /** 
  163. * Clear the cached output of a specific user 
  164. * This method also sets the DevBuddy_Feed_Plugin::$is_cached 
  165. * property to FALSE once the cache is deleted and is called 
  166. * when changes have been saved on the settings page in 
  167. * WordPress. 
  168. * @access public 
  169. * @return void 
  170. * @since 1.0.0 
  171. * @param string $user The username/ID of the feed owner 
  172. */ 
  173. public function clear_cache_output( $user ) { 
  174. delete_transient( $this->plugin_name . '_output_' . $user ); 
  175.  
  176. $clear_cache_successful = ( get_transient( $this->plugin_name . '_output_' . $user ) ) ? FALSE : TRUE; 
  177.  
  178. if ( $clear_cache_successful ) { 
  179. $this->is_cached = FALSE; 
  180.  
  181.  
  182. /** 
  183. * Format the date based on what the time that the data given represents 
  184. * An option for relative datetimes has been included,  
  185. * which will be useful in cases where the output is 
  186. * to be cached and the relative times would thus be 
  187. * inaccurate. 
  188. * @access protected 
  189. * @return string Some human readable representation of the date the post was published 
  190. * @since 1.0.0 
  191. * @param mixed $datetime The datetime that the post was published in any format that PHP's strtotime() can parse 
  192. * @param bool $relative_datetime Whether or not to return relative datetimes, e.g. "2 hours ago" 
  193. */ 
  194. protected function formatify_date( $datetime, $relative_datetime = TRUE ) { 
  195. $an_hour = 3600; 
  196. $a_day = $an_hour*24; 
  197. $a_week = $a_day*7; 
  198.  
  199. $now = time(); 
  200. $then = strtotime( $datetime ); 
  201. $diff = $now - $then; 
  202.  
  203. $mins = $diff / 60 % 60; 
  204. $the_mins_ago = $mins; 
  205. $the_mins_ago .= ( $mins == '1' ) ? ' minute ago' : ' minutes ago'; 
  206.  
  207. $hours = $diff / 3600 % 24; 
  208. $the_hours_ago = 'About '; 
  209. $the_hours_ago .= $hours; 
  210. $the_hours_ago .= ( $hours == '1' ) ? ' hour ago' : ' hours ago'; 
  211.  
  212. $the_time = date( 'H:i', $then ); 
  213. $the_day = date( 'D', $then ); 
  214. $the_date = date( 'j M', $then ); 
  215.  
  216.  
  217. if ( $relative_datetime && $diff <= $an_hour ) { 
  218. return $the_mins_ago; 
  219.  
  220. } elseif ( $diff <= $an_hour ) { 
  221. return $the_time.', '.$the_day; 
  222.  
  223. } elseif ( $relative_datetime && $diff > $an_hour && $diff <= $a_day ) { 
  224. return $the_hours_ago; 
  225.  
  226. } elseif ( $diff > $an_hour && $diff <= $a_day ) { 
  227. return $the_time.', '.$the_day; 
  228.  
  229. } elseif ( $diff > $a_day && $diff <= $a_week ) { 
  230. return $the_time.', '.$the_day; 
  231.  
  232. } else { 
  233. return $the_date; 
  234.  
  235.  
  236. /** 
  237. * Turn plain text links within text into hyperlinks and return the full text 
  238. * @access public 
  239. * @return string The original text with plain text links converted into hyperlinks 
  240. * @since 1.0.0 
  241. * @param string $text The text to parse for plain text links 
  242. */ 
  243. public function hyperlinkify_text( $text ) { 
  244. $new_text = preg_replace('@(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@', '<a href="$1" target="_blank">$1</a>', $text); 
  245. return $new_text; 
  246.  
  247.  
  248. /** 
  249. * Set the width and the height for the user display picture 
  250. * This does not manipulate any image, this method 
  251. * only sets the values that other methods/function 
  252. * can take advantage of. 
  253. * This method can accept an array with the width and 
  254. * height in seperate indexes, a string with just one 
  255. * number which will be used for both width and height,  
  256. * or a string with the width and height seperated with 
  257. * an "x". 
  258. * @access public 
  259. * @return void 
  260. * @since 1.0.0 
  261. * @param mixed $width_height The desired width/height of the display picture; either a string or an array is accepted 
  262. */ 
  263. /************************************************/ 
  264. public function set_dp_size( $width_height ) { 
  265. $min_size = 10; 
  266. $max_size = 200; 
  267.  
  268. if ( ! is_array( $width_height ) ) { 
  269. $width_height_arr = explode( 'x', $width_height ); 
  270.  
  271. // No "x" was present 
  272. if ( is_array( $width_height_arr ) && count( $width_height_arr ) === 1 ) { 
  273. $width_height_arr[0] = $width_height_arr[0]; 
  274. $width_height_arr[1] = $width_height_arr[0]; 
  275.  
  276. // "x" was present 
  277. } elseif ( is_array( $width_height_arr ) && count( $width_height_arr ) === 2 ) { 
  278. /** Don't actually need to do anything here,  
  279. but we don't want this condition getting 
  280. caught in the "else" either */ 
  281.  
  282. // Empty string 
  283. } else { 
  284. $width_height_arr[0] = $this->defaults['dp_size']; 
  285. $width_height_arr[1] = $this->defaults['dp_size']; 
  286.  
  287. // An array of two items, both numeric 
  288. } elseif( is_array( $width_height ) && count( $width_height ) === 2 ) { 
  289. $width_height_arr[0] = $width_height[0]; 
  290. $width_height_arr[1] = $width_height[1]; 
  291.  
  292. // Check for minimums and maximums 
  293. $i = 0; 
  294. foreach ( $width_height_arr as $dimension ) { 
  295. if ( $dimension < $min_size ) { 
  296. $width_height_arr[ $i ] = $min_size; 
  297.  
  298. } elseif( $dimension > $max_size ) { 
  299. $width_height_arr[ $i ] = $max_size; 
  300.  
  301. $i++; 
  302. unset( $i ); 
  303.  
  304. $this->dp_width = $width_height_arr[0]; 
  305. $this->dp_height = $width_height_arr[1]; 
  306.  
  307.  
  308. /** 
  309. * Return the values of the display picture size 
  310. * This value is set via DevBuddy_Feed_Plugin::set_dp_size() 
  311. * @access public 
  312. * @return array 
  313. * @since 1.0.1 
  314. */ 
  315. public function get_dp_size() { 
  316. $dp = array( 'width' => $this->dp_width, 'height' => $this->dp_height ); 
  317. return $dp; 
  318.  
  319.  
  320. /** 
  321. * Converts comma-separated values in a string to an array 
  322. * Sometimes a value may be either an array or a string 
  323. * so this is a way to ensure that we always get a the 
  324. * format we want 
  325. * @access public 
  326. * @return mixed 
  327. * @since 1.0.2 
  328. */ 
  329. public function list_convert( $list ) { 
  330. if ( ! is_array( $list ) ) { 
  331. $list = explode( ', ', $list ); 
  332.  
  333. return $list; 
  334.  
  335.  
  336. /** 
  337. * Mask sensitive information 
  338. * Takes a string and replaces certain values with 
  339. * an "x" to retain the privacy of sensitive data. 
  340. * @access protected 
  341. * @return string 
  342. * @since 1.0.0 
  343. * @param string $string The string you wish to have masked 
  344. * @param int $start The point at which you wish masking to begin 
  345. * @param int $end_offset The point at which you wish masking to end; 0 will result in masking until the end of the $string 
  346. */ 
  347. protected function mask_data( $string, $start = 3, $end_offset = 3 ) { 
  348. $char_data = str_split( $string ); 
  349. $length = count( $char_data ) - 1; 
  350.  
  351. for ( $i = $start; $i <= $length - $end_offset; $i++ ) { 
  352. if ( $char_data[ $i ] != '-' ) { 
  353. $char_data[ $i ] = 'x'; 
  354.  
  355. $string = ''; 
  356. foreach ( $char_data as $char ) { 
  357. $string .= $char; 
  358.  
  359. return $string; 
  360.  
  361.  
  362. /** 
  363. * A request to hide the plugin's WordPress admin menu item 
  364. * This method is used to register the hiding of 
  365. * this plugin's menu item from the WordPress admin 
  366. * menu but it does not execute it. 
  367. * @access public 
  368. * @return void 
  369. * @used_by DevBuddy_Feed_Plugin::hide_wp_admin_menu_item() Executes this request 
  370. * @since 1.0.0 
  371. */ 
  372. public function hide_admin_page() { 
  373. remove_submenu_page( 'options-general.php', $this->page_uri_main ); 
  374.  
  375.  
  376. /** 
  377. * A request to hide the plugin's WordPress admin menu item 
  378. * This method is used to execute the hiding of 
  379. * this plugin's menu item from the WordPress admin. 
  380. * @access public 
  381. * @return void 
  382. * @uses DevBuddy_Feed_Plugin::hide_admin_page() Registers this request 
  383. * @since 1.0.0 
  384. */ 
  385. public function hide_wp_admin_menu_item() { 
  386. add_action( 'admin_menu', array( $this, 'hide_admin_page' ), 999 ); 
  387.  
  388.  
  389. /** 
  390. * Write a message to the debug log, usually found 
  391. * in wp-content/debug.log. In some cases, such as 
  392. * the value of $msg being an array or object, the 
  393. * log will not be written to. 
  394. * @access public 
  395. * @since 1.1.1 
  396. * @return void 
  397. * @param string $msg The message to be written to the log 
  398. * @param boolean $override Write to the log regardless of whether or not WP_DEBUG_LOG is set to TRUE 
  399. */ 
  400. public function log( $msg, $override = FALSE ) { 
  401. if ( WP_DEBUG_LOG === TRUE || $override === TRUE ) { 
  402. @error_log( $msg ); 
  403.  
  404.  
  405. /** 
  406. * Output the $output to the screen as and when this method 
  407. * is called. Use DevBuddy_Feed_Plugin::kill() instead of 
  408. * this if you want to end script execution immediately. 
  409. * @access public 
  410. * @since 1.1.0 
  411. * @return void 
  412. *  
  413. * @param mixed $output The data you wish to output to screen 
  414. * @param bool $log Send the $output to the configured error log 
  415. */ 
  416. public function debug( $output, $log = FALSE ) { 
  417. // If $output is an array or object, convert it to a string 
  418. if ( is_array( $output ) || is_object( $output ) ) { 
  419. $output = print_r( $output, TRUE ); 
  420.  
  421. // If $output is boolean, convert it to a string 
  422. if ( is_bool( $output ) ) { 
  423. $output = '(bool) '; 
  424. $output .= ( $output ) ? 'true' : 'false'; 
  425.  
  426. // If $output is NULL, convert it to a string 
  427. if ( $output === NULL ) { 
  428. $output = 'NULL'; 
  429.  
  430. echo '<pre>' . $output . '</pre>' . "\n\n"; 
  431.  
  432. if ( $log ) { 
  433. $this->log( $output ); 
  434.  
  435.  
  436. /** 
  437. * Kills all script execution at the point that this method 
  438. * is called and sends $output to the error log. By setting 
  439. * the $print parameter to TRUE, the output can be sent to 
  440. * the screen too. $output will also be printed to screen if 
  441. * WP_DEBUG is set to TRUE. 
  442. * @access public 
  443. * @since 1.1.0 
  444. * @return void 
  445. *  
  446. * @param mixed $output The text that will be output in the error log, and on screen is $print is TRUE 
  447. * @param boolean $print Set to TRUE to print the output to screen 
  448. */ 
  449. public function kill( $output, $print = FALSE ) { 
  450. // $print param should be a boolean value 
  451. if ( ! is_bool( $print ) ) { 
  452. $this->debug( __METHOD__ . ' expects second parameter to be boolean, ' . gettype( $print ) . ' given', TRUE ); 
  453.  
  454. // Print error to page only if explicitly declared to or if in debug environment 
  455. if ( $print === TRUE || WP_DEBUG === TRUE ) { 
  456. $this->debug( $output ); 
  457.  
  458. // Log the output 
  459. $this->log( $output ); 
  460.  
  461. // Kill 
  462. die; 
  463.  
  464.  
  465. /** 
  466. * Declare an error 
  467. * @access protected 
  468. * @since 1.1.1 
  469. * @return void 
  470. * @param int $level The error level 
  471. * @param string $msg The error message/details to be written to the page and the error log 
  472. */ 
  473. protected function error( $level, $msg ) { 
  474. // Set error levels 
  475. $levels = array( 
  476. 1 => 'fatal',  
  477. 2 => 'warning',  
  478. 3 => 'notice' 
  479. ); 
  480.  
  481. $msg = '<b>' . $levels[$level] . '</b>: ' . $msg; 
  482.  
  483. // Kill script if fatal error 
  484. if ( $level === 1 ) { 
  485. $this->kill( $msg ); 
  486.  
  487. // Print error message to page for any other error 
  488. if ( WP_DEBUG === TRUE ) { 
  489. $this->debug( $msg ); 
  490.  
  491. // Log error if environment permits 
  492. if ( WP_DEBUG_LOG === TRUE ) { 
  493. $this->log( strip_tags($msg) ); 
  494.  
  495. } // END class