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

/wp-content/themes/nonus/framework/shortcodes/socials/ctTwitterShortcodeBase.class.php

https://github.com/alniko009/magic
PHP | 229 lines | 128 code | 32 blank | 69 comment | 33 complexity | ec56174ea5712aeaecc323fac831c097 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Twitter shortcode
  4. */
  5. abstract class ctTwitterShortcodeBase extends ctShortcode {
  6. /**
  7. * Returns name
  8. * @return string|void
  9. */
  10. public function getName() {
  11. return 'Twitter';
  12. }
  13. /**
  14. * Shortcode name
  15. * @return string
  16. */
  17. public function getShortcodeName() {
  18. return 'twitter';
  19. }
  20. /**
  21. * returns the follow link
  22. * @param $user
  23. * @return string
  24. */
  25. protected function getFollowLink($user){
  26. return "http://twitter.com/" . $user;
  27. }
  28. /**
  29. * gets twitter news
  30. * @param $user
  31. * @param $limit
  32. * @return stdClass[]
  33. */
  34. protected function getTweets($attributes) {
  35. extract($attributes);
  36. $tweets = array();
  37. $user = str_replace(' OR ', '%20OR%20', $user);
  38. //get json - search.json
  39. /*
  40. $feed = wp_remote_get('http://search.twitter.com/search.json?q=' . $user . '&rpp=' . $limit . '&include_entities=true', array(CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => 0));
  41. $xml = wp_remote_retrieve_body($feed);
  42. $json = json_decode($xml, true);
  43. $json = isset($json['results']) ? $json['results'] : array();
  44. */
  45. //get json - user_timeline.json
  46. /*
  47. $feed = wp_remote_get('http://api.twitter.com/1/statuses/user_timeline.json?screen_name=' . $user . '&count=' . $limit . '&include_entities=true&include_rts=true', array(CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => 0));
  48. $xml = wp_remote_retrieve_body($feed);
  49. $json = json_decode($xml, true);
  50. */
  51. $token = $token ? $token : ct_get_option('general_twit_token', '');
  52. $token_secret = $token_secret ? $token_secret : ct_get_option('general_twit_token_secret', '');
  53. $key = $key ? $key : ct_get_option('general_twit_customer_key', '');
  54. $secret = $secret ? $secret : ct_get_option('general_twit_customer_secret', '');
  55. $access = '&at=' . $token . '&ats=' . $token_secret . '&ck=' . $key . '&cs=' . $secret;
  56. $feed = wp_remote_get(CT_THEME_LIB_DIR_URI . '/shortcodes/socials/ctTwitterProxy.php?url=' . urlencode('statuses/user_timeline.json?screen_name=' . $user . '&count=' . $limit . '&include_entities=true&include_rts=true') . $access, array(CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => 0));
  57. $xml = wp_remote_retrieve_body($feed);
  58. $json = json_decode($xml, true);
  59. if ($json) {
  60. foreach ($json as $tweetInfo) {
  61. $content = $tweetInfo['text'];
  62. // parse URLs
  63. if ($parseurl != 'plain' && isset($tweetInfo['entities']['urls'])) {
  64. foreach ($tweetInfo['entities']['urls'] as $url) {
  65. $orgLink = $url['url'];
  66. $displayLink = $parseurl == 'display' ? $url['display_url'] : $orgLink;
  67. $content = str_replace($orgLink, '<a target="_blank" href="' . $orgLink . '">' . $displayLink . '</a>', $content);
  68. }
  69. }
  70. //parse media
  71. if (isset($tweetInfo['entities']['media'])) {
  72. foreach ($tweetInfo['entities']['media'] as $url) {
  73. $orgLink = $url['url'];
  74. $displayLink = $parsemedia == 'expanded' ? $url['expanded_url'] : ($parsemedia == 'display' ? $url['display_url'] : $orgLink);
  75. if($parsemedia != 'plain'){
  76. $content = str_replace($orgLink, '<a target="_blank" href="' . $orgLink . '">' . $displayLink . '</a>', $content);
  77. }
  78. //embed images
  79. if(isset($img) && isset($imgsize) && $img == 'yes' && $url['type'] == 'photo'){
  80. $content .= '<br><a target="_blank" href="' . $orgLink . '"><img src="' . $url['media_url'] . ':' . $imgsize . '"></img></a>';
  81. }
  82. }
  83. }
  84. // parse @id
  85. if($parseid == 'yes'){
  86. $content = preg_replace('/@(\w+)/', '@<a target="_blank" href="http://twitter.com/$1" class="at">$1</a>', $content);
  87. }
  88. // parse #hashtag
  89. if($parsehashtag == 'yes'){
  90. $content = preg_replace('/\s#(\w+)/', ' <a target="_blank" href="http://twitter.com/#!/search?q=%23$1" class="hashtag">#$1</a>', $content);
  91. }
  92. //max length of the content
  93. $content = (string)$content;
  94. if(is_numeric($maxlength) && strlen($content) > $maxlength){
  95. $content = $this->truncate($content, $maxlength, '...');
  96. }
  97. $tweet = new stdClass();
  98. $tweet->content = (string)$content;
  99. $tweet->updated = (int)strtotime($tweetInfo['created_at']);
  100. array_push($tweets, $tweet);
  101. unset($feed, $xml, $result, $tweet);
  102. }
  103. }
  104. return $tweets;
  105. }
  106. /**
  107. * counts time ago
  108. * @param $time
  109. * @return string
  110. */
  111. protected function ago($time) {
  112. $periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
  113. $lengths = array("60", "60", "24", "7", "4.35", "12", "10");
  114. $now = time();
  115. $difference = $now - $time;
  116. for ($j = 0; $difference >= $lengths[$j] && $j < count($lengths) - 1; $j++) {
  117. $difference /= $lengths[$j];
  118. }
  119. $difference = round($difference);
  120. if ($difference != 1) {
  121. $periods[$j] .= "s";
  122. }
  123. $difference = $difference < 0 ? 0 : $difference;
  124. return $difference . " " . $periods[$j] . ' ' . __('ago', 'ct_theme');
  125. }
  126. /**
  127. * cuts the content
  128. * @param $text
  129. * @param $length
  130. * @param string $suffix
  131. * @param bool $isHTML
  132. * @return mixed
  133. */
  134. protected function truncate($text, $length, $suffix = '&hellip;', $isHTML = true) {
  135. $i = 0;
  136. $simpleTags=array('br'=>true,'hr'=>true,'input'=>true,'image'=>true,'link'=>true,'meta'=>true);
  137. $tags = array();
  138. if($isHTML){
  139. preg_match_all('/<[^>]+>([^<]*)/', $text, $m, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
  140. foreach($m as $o){
  141. if($o[0][1] - $i >= $length)
  142. break;
  143. $t = substr(strtok($o[0][0], " \t\n\r\0\x0B>"), 1);
  144. // test if the tag is unpaired, then we mustn't save them
  145. if($t[0] != '/' && (!isset($simpleTags[$t])))
  146. $tags[] = $t;
  147. elseif(end($tags) == substr($t, 1))
  148. array_pop($tags);
  149. $i += $o[1][1] - $o[0][1];
  150. }
  151. }
  152. // output without closing tags
  153. $output = substr($text, 0, $length = min(strlen($text), $length + $i));
  154. // closing tags
  155. $output2 = (count($tags = array_reverse($tags)) ? '</' . implode('></', $tags) . '>' : '');
  156. // Find last space or HTML tag (solving problem with last space in HTML tag eg. <span class="new">)
  157. $pos = (int)end(end(preg_split('/<.*>| /', $output, -1, PREG_SPLIT_OFFSET_CAPTURE)));
  158. // Append closing tags to output
  159. $output.=$output2;
  160. // Get everything until last space
  161. $one = substr($output, 0, $pos);
  162. // Get the rest
  163. $two = substr($output, $pos, (strlen($output) - $pos));
  164. // Extract all tags from the last bit
  165. preg_match_all('/<(.*?)>/s', $two, $tags);
  166. // Add suffix if needed
  167. if (strlen($text) > $length) { $one .= $suffix; }
  168. // Re-attach tags
  169. $output = $one . implode($tags[0]);
  170. //added to remove unnecessary closure
  171. $output = str_replace('</!-->','',$output);
  172. return $output;
  173. }
  174. /**
  175. * Returns config
  176. * @return null
  177. */
  178. public function getAttributes() {
  179. return array(
  180. 'user' => array('label' => __('username', 'ct_theme'), 'default' => '', 'type' => 'input', 'help' => __("Twitter username", 'ct_theme')),
  181. 'key' => array('label' => __('customer key', 'ct_theme'), 'default' => ct_get_option('general_twit_customer_key', ''), 'type' => 'input', 'help' => __("Customer key", 'ct_theme')),
  182. 'secret' => array('label' => __('customer secret', 'ct_theme'), 'default' => ct_get_option('general_twit_customer_secret', ''), 'type' => 'input', 'help' => __("Customer secret", 'ct_theme')),
  183. 'token' => array('label' => __('token', 'ct_theme'), 'default' => ct_get_option('general_twit_token', ''), 'type' => 'input', 'help' => __("Access token", 'ct_theme')),
  184. 'token_secret' => array('label' => __('token secret', 'ct_theme'), 'default' => ct_get_option('general_twit_token_secret', ''), 'type' => 'input', 'help' => __("Access token secret", 'ct_theme')),
  185. 'limit' => array('label' => __('limit', 'ct_theme'), 'default' => '2', 'type' => 'input', 'help' => __("Limit news", 'ct_theme')),
  186. 'button' => array('label' => __("follow us button", 'ct_theme'), 'default' => __('Follow us', 'ct_theme'), 'type' => 'input', 'help' => "Follow us button label. Leave blank to hide it", 'ct_theme'),
  187. 'newwindow' => array('label' => __("new window?", 'ct_theme'), 'default' => 'false', 'type' => 'checkbox', 'help' => "Open in new window follow us button?", 'ct_theme'),
  188. 'parseurl' => array('label' => __('parse url', 'ct_theme'), 'default' => 'short', 'type' => 'select', 'choices' => array('plain' => __('plain text', 'ct_theme'), 'short' => __('short link', 'ct_theme'), 'display' => __('display link', 'ct_theme')), 'help' => __("You can display links from the content as plain text, short html links or full html links", 'ct_theme')),
  189. 'parsemedia' => array('label' => __('parse media', 'ct_theme'), 'default' => 'short', 'type' => 'select', 'choices' => array('plain' => __('plain text', 'ct_theme'), 'short' => __('short link', 'ct_theme'), 'display' => __('display link', 'ct_theme'), 'expanded' => __('expanded link', 'ct_theme')), 'help' => __("You can display media links from the content as plain text or 3 types of html links", 'ct_theme')),
  190. 'parseid' => array('label' => __('parse user id?', 'ct_theme'), 'default' => 'yes', 'type' => 'select', 'choices' => array('yes' => __('yes', 'ct_theme'), 'no' => __('no', 'ct_theme')), 'help' => __("Display user @ids as plain text or links", 'ct_theme')),
  191. 'parsehashtag' => array('label' => __('parse hashtag?', 'ct_theme'), 'default' => 'yes', 'type' => 'select', 'choices' => array('yes' => __('yes', 'ct_theme'), 'no' => __('no', 'ct_theme')), 'help' => __("Display #hashtags as plain text or links", 'ct_theme')),
  192. 'img' => array('label' => __('embed images?', 'ct_theme'), 'default' => 'no', 'type' => 'select', 'choices' => array('yes' => __('yes', 'ct_theme'), 'no' => __('no', 'ct_theme')), 'help' => __("Embed images into posts content?", 'ct_theme')),
  193. 'imgsize' => array('label' => __('size of embeded images?', 'ct_theme'), 'default' => 'thumb', 'type' => 'select', 'choices' => array('thumb' => __('thumb', 'ct_theme'), 'small' => __('small', 'ct_theme'), 'medium' => __('medium', 'ct_theme'), 'large' => __('large', 'ct_theme')), 'help' => __("Embedded image size", 'ct_theme')),
  194. 'maxlength' => array('label' => __('tweet length limit', 'ct_theme'), 'default' => '', 'type' => 'input', 'help' => __("Max length of the tweet", 'ct_theme')),
  195. );
  196. }
  197. }