PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/rss-feed-widget/functions.php

https://gitlab.com/eita/agencia-consumo-responsavel
PHP | 376 lines | 237 code | 119 blank | 20 comment | 19 complexity | 09f21f2da3ceec8f56b8b2d8927b3a9c MD5 | raw file
  1. <?php
  2. /*
  3. if(!function_exists('rfw_cache')){
  4. function rfw_cache(){
  5. }
  6. }*/
  7. class rfw_dock extends WP_Widget {
  8. public $rfw_cache_val;
  9. public function __construct() {
  10. // widget actual processes
  11. parent::__construct(
  12. 'rfw_dock', // Base ID
  13. __('RSS Feed Widget', 'text_domain'), // Name
  14. array( 'description' => __( 'A feed widget with sliding effect.', 'text_domain' ), ) // Args
  15. );
  16. }
  17. function clean_xhtml($string)
  18. {
  19. $string = ereg_replace("<[^>]*>", "", $string);
  20. $string = preg_replace("@<p[^>]*?>.*?
  21. @siu", '',$string);
  22. return $string;
  23. }
  24. public function rfw_cache(){
  25. return $this->rfw_cache_val;
  26. }
  27. public function is_url($url){
  28. $ret = true;
  29. if (filter_var($url, FILTER_VALIDATE_URL) === FALSE) {
  30. $ret = false;
  31. }
  32. return $ret;
  33. }
  34. public function widget( $args, $instance ) {
  35. // outputs the content of the widget
  36. global $wpdb;
  37. //pree($instance);
  38. $limit_feeds = ($instance['number']>0?$instance['number']:1);
  39. $title = $instance['title'];
  40. //pree($title);
  41. $rss_url = $instance['rss_url'];
  42. $fb_page_id = !$this->is_url($rss_url);
  43. $show_feed_title = $instance['show_feed_title'];
  44. $feed_words = ($instance['feed_words']>0?$instance['feed_words']:60);
  45. $display_type = $instance['content_display'];
  46. $content_order = $instance['content_order'];
  47. $content_height = $instance['content_height'];
  48. $this->rfw_cache_val = isset($instance['rfw_cache'])?$instance['rfw_cache']:'';
  49. //pree($title);
  50. $html = '<aside id="rfw-id" class="rfw-class">';
  51. $html.= ($title!='')?'<h3 class="widget-title">'.$title.'</h3>':'';
  52. $html.= '<nav class="add-nav widget_dock" id="rfw-authors">';
  53. //pree($title);
  54. //pree($rss_url);
  55. $html_arr = array();
  56. $html_ul = '<ul class="rfw_dock" style="'.($content_height!=''?'height:'.$content_height:'').'">';
  57. if($fb_page_id){
  58. $access_token_array = array(
  59. '1489500477999288|KFys5ppNi3sreihdreqPkU2ChIE',
  60. '859332767418162|BR-YU8zjzvonNrszlll_1a4y_xE',
  61. '360558880785446|4jyruti_VkxxK7gS7JeyX-EuSXs',
  62. '1487072591579718|0KQzP-O2E4mvFCPxTLWP1b87I4Q',
  63. '640861236031365|2rENQzxtWtG12DtlZwqfZ6Vu6BE',
  64. '334487440086538|hI_NNy1NvxQiQxm-TtXsrmoCVaE',
  65. '755471677869105|Jxv8xVDad7vUUTauk8K2o71wG2w',
  66. '518353204973067|dA7YTe-k8eSvgZ8lqa51xSm16DA',
  67. '444286039063163|5qkYu2qxpERWO3gcs2f3nxeqhpg',
  68. '944793728885704|XJ6QqKK8Ldsssr4n5Qrs2tVr7rs',
  69. '1444667452511509|wU7tzWiuj6NadfpHfgkIGLGO86o',
  70. '1574171666165548|ZL9tXNXxpnCdAvdUjCX5HtRnsR8'
  71. );
  72. $access_token = $access_token_array[rand(0, 11)];
  73. $json_resp = 'https://graph.facebook.com/'.$rss_url.'/posts?fields=object_id,message,story,link,type&access_token='.$access_token.'&limit='.$limit_feeds.'&locale=en_US';
  74. $response = wp_remote_get( $url );
  75. $response = wp_remote_get($json_resp);
  76. if( is_wp_error( $response ) ) {
  77. $error_message = $response->get_error_message();
  78. //echo "Something went wrong: $error_message";
  79. } else {
  80. if( is_array($response) ) {
  81. $header = $response['headers']; // array of http header lines
  82. $body = $response['body']; // use the content
  83. $body = json_decode($body);
  84. $body = (is_array($body)?$body:array());
  85. $body = current($body);
  86. if(!empty($body)){
  87. foreach($body as $items){
  88. $items->message = (isset($items->message)?$items->message:'');
  89. $items->story = (isset($items->story)?$items->story:$items->message);
  90. $unique_key = strtolower(substr($items->story, 0, 1));
  91. $content = '';
  92. $img = '';
  93. $link = '<a title="'.$items->story.'" href="'.esc_url( $items->link ).'" target="_blank" rel="nofollow">_LINK_TEXT_</a>';
  94. $content .= $show_feed_title?str_replace('_LINK_TEXT_', '<h3 class="entry-title">'.esc_html( $items->story ).'</h3>', $link):'';
  95. $description = $items->message;
  96. switch($display_type){
  97. case 'text_only':
  98. $content .= $this->string_limit_words($this->clean_xhtml($description), $feed_words).' ...';
  99. break;
  100. default:
  101. if($items->type=='photo')
  102. $img = 'https://graph.facebook.com/'.$items->object_id.'/picture';
  103. $description = (!$this->is_url($description)?$description:'');
  104. $content .= ($description!=''?'<div class="text_div">'.$this->string_limit_words($description, $feed_words).' ...</div>':'');
  105. break;
  106. case 'image_only':
  107. if($items->type=='photo')
  108. $img = 'https://graph.facebook.com/'.$items->object_id.'/picture';
  109. break;
  110. }
  111. if($img!=''){
  112. $content .= str_replace('_LINK_TEXT_', '<div class="image_only"><img alt="'.$items->story.'" src="'.$img.'" /></div>', $link);
  113. }
  114. $html_arr[$unique_key][] = ($content!=''?'<li>'.$content.'</li>':'');
  115. }
  116. }
  117. }
  118. }
  119. }else{
  120. add_filter( 'wp_feed_cache_transient_lifetime' , array($this, 'rfw_cache') );
  121. $rss = fetch_feed($rss_url);
  122. //pree($rss);
  123. remove_filter( 'wp_feed_cache_transient_lifetime' , array($this, 'rfw_cache') );
  124. if ( ! is_wp_error( $rss ) ){
  125. $maxitems = $rss->get_item_quantity( $limit_feeds );
  126. $rss_items = $rss->get_items( 0, $maxitems );
  127. if ( $maxitems > 0 ){
  128. foreach ( $rss_items as $item ){
  129. $unique_key = strtolower(substr($item->get_title(), 0, 1));
  130. $html_arr[$unique_key][]='<li>';
  131. $img = '';
  132. $link = '<a href="'.esc_url( $item->get_permalink() ).'" target="_blank" rel="nofollow">_LINK_TEXT_</a>';
  133. $html_arr[$unique_key][]= $show_feed_title?str_replace('_LINK_TEXT_', '<h3 class="entry-title">'.esc_html( $item->get_title() ).'</h3>', $link):'';
  134. $description = $item->get_description();
  135. switch($display_type){
  136. case 'text_only':
  137. $html_arr[$unique_key][]= $this->string_limit_words($this->clean_xhtml($description), $feed_words).' ...';
  138. break;
  139. default:
  140. $html_arr[$unique_key][]= $this->string_limit_words($description, $feed_words).' ...';
  141. break;
  142. case 'image_only':
  143. //preg_match_all('/<img[^>]+>/i', $description, $img);
  144. //
  145. //preg_match_all('/src="([^"]*)"/', $description, $img);
  146. preg_match_all('@<img.*src="([^"]*)"[^>/]*/?>@Ui', $description, $img);
  147. if(!empty($img)){
  148. $img = end($img);
  149. if(isset($img[0]) && $img[0]!=''){
  150. $img = $img[0];
  151. $img = current(explode('?', $img));
  152. }else{
  153. $img = '';
  154. }
  155. }
  156. break;
  157. }
  158. if($img!=''){
  159. $html_arr[$unique_key][]= str_replace('_LINK_TEXT_', '<div class="image_only"><img src="'.$img.'" /></div>', $link);
  160. }
  161. $html_arr[$unique_key][]= '</li>';
  162. }
  163. }
  164. }
  165. }
  166. if(is_array($html_arr) && !empty($html_arr)){
  167. switch($content_order){
  168. case 'aa':
  169. ksort($html_arr);
  170. break;
  171. case 'ad':
  172. krsort($html_arr);
  173. break;
  174. }
  175. foreach($html_arr as $alphabet=>$feeds){
  176. $html_ul .= implode(' ', $feeds);
  177. }
  178. }
  179. $html .= $html_ul.'</ul></nav></aside>';
  180. echo $html;
  181. }
  182. private function string_limit_words($string, $word_limit)
  183. {
  184. $words = explode(' ', $string, ($word_limit + 1));
  185. if(count($words) > $word_limit)
  186. array_pop($words);
  187. return implode(' ', $words);
  188. }
  189. public function form( $instance ) {
  190. // outputs the options form on admin
  191. $title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
  192. $rss_url = isset( $instance['rss_url'] ) ? esc_attr( $instance['rss_url'] ) : '';
  193. $number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 6;
  194. $show_feed_title = isset( $instance['show_feed_title'] ) ? esc_attr( $instance['show_feed_title'] ) : true;
  195. $feed_words = isset( $instance['feed_words'] ) ? absint( $instance['feed_words'] ) : 60;
  196. $content_display = isset( $instance['content_display'] ) ? $instance['content_display'] : 'default';
  197. $content_order = isset( $instance['content_order'] ) ? $instance['content_order'] : 'default';
  198. $content_height = isset( $instance['content_height'] ) ? $instance['content_height'] : '';
  199. $rfw_cache = isset( $instance['rfw_cache'] ) ? $instance['rfw_cache'] : '';
  200. ?>
  201. <p>
  202. <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title (optional):' ); ?></label>
  203. <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
  204. </p>
  205. <p class="rss_url_area"><label for="<?php echo $this->get_field_id( 'rss_url' ); ?>"><?php _e('Enter the RSS feed URL or Facebook Page ID here: <a title="Click here for examples" target="_blank" href="http://www.tutorsloop.net/app/live.php?id=4330772">(What\'s this?)</a>'); ?></label>
  206. <input class="widefat" id="<?php echo $this->get_field_id( 'rss_url' ); ?>" name="<?php echo $this->get_field_name( 'rss_url' ); ?>" type="text" value="<?php echo esc_attr( $rss_url ); ?>" /><a>Add More</a></p>
  207. <p><label for="<?php echo $this->get_field_id( 'content_display' ); ?>"><?php _e( 'Display type:' ); ?></label>
  208. <select id="<?php echo $this->get_field_id( 'content_display' ); ?>" name="<?php echo $this->get_field_name( 'content_display' ); ?>">
  209. <option value="default" <?php echo $content_display=='default'?'selected="selected"':''; ?>>Default</option>
  210. <option value="text_only" <?php echo $content_display=='text_only'?'selected="selected"':''; ?>>Text Only</option>
  211. <option value="image_only" <?php echo $content_display=='image_only'?'selected="selected"':''; ?>>Image Only</option>
  212. </select></p>
  213. <p><label for="<?php echo $this->get_field_id( 'content_order' ); ?>"><?php _e( 'Sort order:' ); ?></label>
  214. <select id="<?php echo $this->get_field_id( 'content_order' ); ?>" name="<?php echo $this->get_field_name( 'content_order' ); ?>">
  215. <option value="default" <?php echo $content_order=='default'?'selected="selected"':''; ?>>Default</option>
  216. <option value="aa" <?php echo $content_order=='aa'?'selected="selected"':''; ?>>Alphabetical Ascending</option>
  217. <option value="ad" <?php echo $content_order=='ad'?'selected="selected"':''; ?>>Alphabetical Descending</option>
  218. </select></p>
  219. <p><label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number of feeds to show:' ); ?></label>
  220. <input id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p>
  221. <p><label for="<?php echo $this->get_field_id( 'show_feed_title' ); ?>"><?php _e( 'Display feed title (Yes/No):' ); ?></label>
  222. <input id="<?php echo $this->get_field_id( 'show_feed_title' ); ?>" name="<?php echo $this->get_field_name( 'show_feed_title' ); ?>" type="checkbox" value="true" <?php echo $show_feed_title?'checked="checked"':''; ?> /></p>
  223. <p><label for="<?php echo $this->get_field_id( 'feed_words' ); ?>"><?php _e( 'No. of feed words to show:' ); ?></label>
  224. <input id="<?php echo $this->get_field_id( 'feed_words' ); ?>" name="<?php echo $this->get_field_name( 'feed_words' ); ?>" type="text" value="<?php echo $feed_words; ?>" size="3" /></p>
  225. <p><label for="<?php echo $this->get_field_id( 'content_height' ); ?>"><?php _e( 'Widget Height:' ); ?></label>
  226. <input id="<?php echo $this->get_field_id( 'content_height' ); ?>" name="<?php echo $this->get_field_name( 'content_height' ); ?>" type="text" value="<?php echo $content_height; ?>" size="6" /><small><br />
  227. e.g. 400px<br />
  228. <?php _e( 'Leave empty for auto.' ); ?></small>
  229. </p>
  230. <p><label for="<?php echo $this->get_field_id( 'rfw_cache' ); ?>"><?php _e( 'Cache Period:' ); ?></label>
  231. <input id="<?php echo $this->get_field_id( 'rfw_cache' ); ?>" name="<?php echo $this->get_field_name( 'rfw_cache' ); ?>" type="text" value="<?php echo $rfw_cache; ?>" placeholder="Enter in seconds" size="20" /><small><br />
  232. e.g. 7200 seconds = 2 hours<br />
  233. <?php _e( 'Leave empty or 0 for no cache.' ); ?></small>
  234. </p>
  235. <?php
  236. }
  237. public function update( $new_instance, $old_instance ) {
  238. // processes widget options to be saved
  239. $instance = array();
  240. $instance['title'] = strip_tags($new_instance['title']);
  241. $instance['rss_url'] = trim(strip_tags($new_instance['rss_url']));
  242. $instance['number'] = (int) $new_instance['number'];
  243. $instance['show_feed_title'] = ($new_instance['show_feed_title']==true);
  244. $instance['feed_words'] = (int) $new_instance['feed_words'];
  245. $instance['content_display'] = strip_tags($new_instance['content_display']);
  246. $instance['content_order'] = strip_tags($new_instance['content_order']);
  247. $instance['content_height'] = strip_tags($new_instance['content_height']);
  248. return $instance;
  249. }
  250. }
  251. if(!function_exists('rfw_init')){
  252. function rfw_init(){
  253. register_widget( 'rfw_dock' );
  254. }
  255. }
  256. ?>