PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/plugins/wd-facebook-feed/frontend/models/FFWDModelPopupBox.php

https://bitbucket.org/lovetheidea/hop-website
PHP | 275 lines | 219 code | 22 blank | 34 comment | 43 complexity | 782c0b3cb0ecda7697c5c361aeb86052 MD5 | raw file
Possible License(s): GPL-2.0, MIT, Apache-2.0, BSD-3-Clause
  1. <?php
  2. class FFWDModelPopupBox {
  3. ////////////////////////////////////////////////////////////////////////////////////////
  4. // Events //
  5. ////////////////////////////////////////////////////////////////////////////////////////
  6. ////////////////////////////////////////////////////////////////////////////////////////
  7. // Constants //
  8. ////////////////////////////////////////////////////////////////////////////////////////
  9. ////////////////////////////////////////////////////////////////////////////////////////
  10. // Variables //
  11. ////////////////////////////////////////////////////////////////////////////////////////
  12. public $access_token;
  13. public $graph_url = 'https://graph.facebook.com/{FB_ID}/{EDGE}?{ACCESS_TOKEN}{FIELDS}{LIMIT}{OTHER}';
  14. public $page_user_group;
  15. public $options;
  16. public $date_offset;
  17. ////////////////////////////////////////////////////////////////////////////////////////
  18. // Constructor & Destructor //
  19. ////////////////////////////////////////////////////////////////////////////////////////
  20. public function __construct() {
  21. }
  22. ////////////////////////////////////////////////////////////////////////////////////////
  23. // Public Methods //
  24. ////////////////////////////////////////////////////////////////////////////////////////
  25. public function get_theme_row_data($id) {
  26. global $wpdb;
  27. if ($id) {
  28. $row = $wpdb->get_row($wpdb->prepare('SELECT * FROM ' . $wpdb->prefix . 'wd_fb_theme WHERE id="%d"', $id));
  29. }
  30. else {
  31. $row = $wpdb->get_row($wpdb->prepare('SELECT * FROM ' . $wpdb->prefix . 'wd_fb_theme WHERE default_theme="%d"', 1));
  32. }
  33. $row = (object) array_merge((array)$row, (array)json_decode( $row->params));
  34. unset($row->params);
  35. return $row;
  36. }
  37. public function get_option_row_data() {
  38. global $wpdb;
  39. $row = $wpdb->get_row($wpdb->prepare('SELECT * FROM ' . $wpdb->prefix . 'wd_fb_option WHERE id="%d"', 1));
  40. $this->options = $row;
  41. return $row;
  42. }
  43. public function get_image_rows_data($fb_id, $sort_by, $order_by = 'asc') {
  44. global $wpdb;
  45. if ($sort_by == 'size' || $sort_by == 'resolution') {
  46. $sort_by = ' CAST(' . $sort_by . ' AS SIGNED) ';
  47. }
  48. elseif (($sort_by != 'alt') && ($sort_by != 'date') && ($sort_by != 'filetype') && ($sort_by != 'filename')) {
  49. //$sort_by = '`order`';
  50. $sort_by = '`id`';
  51. }
  52. if (preg_replace('/\s+/', '', $order_by) != 'asc') {
  53. $order_by = 'desc';
  54. }
  55. $sort_by = '`created_time_number`';
  56. $order_by = 'DESC';
  57. $row = $wpdb->get_results($wpdb->prepare('SELECT * FROM ' . $wpdb->prefix . 'wd_fb_data WHERE fb_id="%d" ORDER BY ' . $sort_by . ' ' . $order_by, $fb_id));
  58. return $row;
  59. }
  60. public function get_ffwd_info_data($id) {
  61. global $wpdb;
  62. $row = $wpdb->get_row($wpdb->prepare('SELECT * FROM ' . $wpdb->prefix . 'wd_fb_info WHERE published=1 AND id="%d"', $id));
  63. // set access token
  64. $this->access_token = $row->access_token;
  65. $graph_url_for_page_info = str_replace (
  66. array('{FB_ID}', '{EDGE}', '{ACCESS_TOKEN}', '{FIELDS}', '{LIMIT}', '{OTHER}'),
  67. array($row->from, '', 'access_token=' . $this->access_token . '&', 'fields=picture,name,link&', '', ''),
  68. $this->graph_url
  69. );
  70. $this->graph_url = str_replace (
  71. array('{ACCESS_TOKEN}', '{LIMIT}'),
  72. array('access_token=' . $this->access_token . '&', ''),
  73. $this->graph_url
  74. );
  75. $this->page_user_group = self::decap_do_curl($graph_url_for_page_info);
  76. $this->page_user_group = json_encode($this->page_user_group);
  77. return $row;
  78. }
  79. public static function decap_do_curl($uri) {
  80. $facebook_graph_results = null;
  81. $facebook_graph_url = $uri; //TODO: Add URL checking here, else error out
  82. //Attempt CURL
  83. if (extension_loaded('curl')){
  84. $ch = curl_init();
  85. curl_setopt($ch,CURLOPT_URL, $facebook_graph_url);
  86. curl_setopt($ch, CURLOPT_HEADER, 0);
  87. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  88. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  89. if(!$facebook_graph_results = curl_exec($ch)) {
  90. printf('<p>cURL Error: %1$s %2$s</p>', curl_errno($ch), curl_error($ch));
  91. printf('<p>Please try entering <strong>%s</strong> into your URL bar and seeing if the page loads.', $facebook_graph_url);
  92. }
  93. if(curl_errno($ch) == 7) {
  94. print '<p><strong>Your server cannot communicate with Facebook\'s servers. This means your server does not support IPv6 or is having issues resolving facebook.com. Please contact your hosting provider.';
  95. }
  96. curl_close($ch);
  97. } else {
  98. print ('Sorry, your server does not allow remote fopen or have CURL');
  99. }
  100. $facebook_graph_results = json_decode($facebook_graph_results, true);
  101. return $facebook_graph_results;
  102. }
  103. public function ffwd_set_date_timezone_offset(){
  104. $date_timezone = isset($this->options->date_timezone) && $this->options->date_timezone!='' ? $this->options->date_timezone : date_default_timezone_get() ;
  105. $date_for_offset = new DateTimeZone($date_timezone);
  106. $date_offset = new DateTime("now", $date_for_offset);
  107. $offset = $date_offset->getOffset();
  108. $offset /= 3600;
  109. $this->date_offset = $offset;
  110. }
  111. public function ffwd_time($created_time, $updated_time, $type) {
  112. $event_date_format = $this->options->event_date_format;
  113. $post_date_format = $this->options->post_date_format;
  114. $date_timezone = isset($this->options->date_timezone) && $this->options->date_timezone!='' ? $this->options->date_timezone : date_default_timezone_get() ;
  115. $date_create = new DateTime($created_time);
  116. $date_create->setTimezone(new DateTimeZone($date_timezone));
  117. $date_update = new DateTime($updated_time);
  118. $date_update->setTimezone(new DateTimeZone($date_timezone));
  119. if($type == "events") {
  120. if($this->options->event_date) {
  121. ?>
  122. <div class="ffwd_popup_from_time_event" style="">
  123. <?php
  124. echo $date_create->format($event_date_format) . '<br>';
  125. //echo 'Start - ' . $date_create->format($event_date_format) . '<br>' .
  126. //'End - ' . $date_update->format($event_date_format);
  127. ?>
  128. </div>
  129. <?php
  130. }
  131. }
  132. else {
  133. ?>
  134. <div class="ffwd_popup_from_time_post" style="">
  135. <?php
  136. if($post_date_format == 'ago') {
  137. $time = strtotime($created_time);
  138. echo $this->humanTiming($time);
  139. }
  140. else
  141. echo $date_create->format($post_date_format);
  142. ?>
  143. </div>
  144. <?php
  145. }
  146. }
  147. public function humanTiming ($time) {
  148. $time = time() - $time;
  149. $tokens = array (
  150. 31536000 => __('year','ffwd'),
  151. 2592000 => __('month','ffwd'),
  152. 604800 => __('week','ffwd'),
  153. 86400 => __('day','ffwd'),
  154. 3600 => __('hour','ffwd'),
  155. 60 => __('minute','ffwd'),
  156. 1 => __('second','ffwd')
  157. );
  158. $tokens_s = array (
  159. 31536000 => __('years','ffwd'),
  160. 2592000 => __('months','ffwd'),
  161. 604800 => __('weeks','ffwd'),
  162. 86400 => __('days','ffwd'),
  163. 3600 => __('hours','ffwd'),
  164. 60 => __('minutes','ffwd'),
  165. 1 => __('seconds','ffwd')
  166. );
  167. foreach ($tokens as $unit => $text) {
  168. if ($time < $unit) continue;
  169. $numberOfUnits = floor($time / $unit);
  170. if($numberOfUnits>1)
  171. return $numberOfUnits.' '.$tokens_s[$unit] . __(' ago','ffwd');
  172. return $numberOfUnits.' '.$text. __(' ago','ffwd');
  173. }
  174. }
  175. public function fill_hashtags ($str) {
  176. $str = preg_replace("/\n/", " \n", $str);
  177. $str = explode(' ', $str);
  178. for($i=0; $i<count($str); $i++) {
  179. if(strpos($str[$i], '#') !== false) {
  180. $hashtag = str_replace('#', '<a class="ffwd_hashtag" target="_blank" href="https://www.facebook.com/hashtag/', $str[$i]);
  181. $word = explode('#', $str[$i]);
  182. $word = '#' . $word[1];
  183. $hashtag .= '">' . $word . '</a>';
  184. $str[$i] = $hashtag;
  185. }
  186. }
  187. $str = implode(' ', $str);
  188. return $str;
  189. }
  190. public function see_less_more($string, $type, $row_type) {
  191. $string = strip_tags($string);
  192. $new_string = $string;
  193. $hide_text_paragraph = '';
  194. $length = strlen($string);
  195. /*var_dump($length);*/
  196. if($row_type == 'events')
  197. $text_length = isset($this->options->event_desp_length) ? $this->options->event_desp_length : 200;
  198. else
  199. $text_length = isset($this->options->post_text_length) ? $this->options->post_text_length : 200;
  200. if ($length > $text_length) {
  201. // Truncate string
  202. $stringCut = substr($string, 0, $text_length);//var_dump($stringCut);
  203. // Make sure it ends in a word so football doesn't footba ass...
  204. $last_whitespace_in_string_cut = strrpos($stringCut, ' ');
  205. $last_whitespace_in_string_cut = ($last_whitespace_in_string_cut === false) ? 0 : $last_whitespace_in_string_cut;
  206. // Get hide text
  207. $hide_text_length = $length - $last_whitespace_in_string_cut;
  208. $hide_text = substr($string, $last_whitespace_in_string_cut, $hide_text_length);
  209. $hide_text_paragraph = ' <span style="display:none" class="ffwd_object_'.$type.'_hide" >' . $hide_text . ' </span>';
  210. $new_string = substr($stringCut, 0, $last_whitespace_in_string_cut) . $hide_text_paragraph . ' <span class="ffwd_more_dotes" > ... </span> <a href="" class="ffwd_see_more ffwd_see_more_'.$type.'">See more</a>';
  211. }
  212. return $new_string;
  213. }
  214. public function fill_tags($string, $message_tags) {
  215. $message_tags = json_decode(str_replace("'", esc_html("'"), $message_tags));
  216. if($message_tags)
  217. foreach($message_tags as $message_tag) {
  218. $type = gettype ( $message_tag );
  219. $tag = ($type == "object") ? $message_tag : $message_tag["0"];
  220. if(strpos($string, $tag->name) !== false) {
  221. $string = str_replace($tag->name, '<a class="ffwd_message_tag" target="_blank" href="https://www.facebook.com/' . $tag->id . '" >'.$tag->name.'</a>', $string);
  222. }
  223. }
  224. return $string;
  225. }
  226. public function get_option_json_data() {
  227. if(isset($this->options) && $this->options != NULL)
  228. return stripslashes(json_encode($this->options));
  229. }
  230. public function ffwd_story($story, $place) {
  231. $enable_place_name = (isset($_GET['enable_place_name']) ? esc_html($_GET['enable_place_name']) : 0);
  232. //$enable_author = (isset($_GET['enable_author']) ? esc_html($_GET['enable_author']) : 0);
  233. $story = str_replace($this->page_user_group['name'], "", $story);
  234. $place = json_decode($place);
  235. if($place != null) {
  236. $place_name = $place->name;
  237. $story = str_replace(
  238. $place_name,
  239. '<a class="ffwd_place_name" href="https://www.facebook.com/' . $place->id . '" target="_blank">' . $place_name . '</a>',
  240. $story );
  241. }
  242. return $story;
  243. }
  244. ////////////////////////////////////////////////////////////////////////////////////////
  245. // Getters & Setters //
  246. ////////////////////////////////////////////////////////////////////////////////////////
  247. ////////////////////////////////////////////////////////////////////////////////////////
  248. // Private Methods //
  249. ////////////////////////////////////////////////////////////////////////////////////////
  250. ////////////////////////////////////////////////////////////////////////////////////////
  251. // Listeners //
  252. ////////////////////////////////////////////////////////////////////////////////////////
  253. }