PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://github.com/alniko009/magic
PHP | 119 lines | 69 code | 18 blank | 32 comment | 13 complexity | 1b62896dc781c62eeb5cebd8dcffe6e6 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Facebook feed shortcode
  4. */
  5. abstract class ctFacebookFeedShortcodeBase extends ctShortcode {
  6. /**
  7. * Returns name
  8. * @return string|void
  9. */
  10. public function getName() {
  11. return 'fb';
  12. }
  13. /**
  14. * Shortcode name
  15. * @return string
  16. */
  17. public function getShortcodeName() {
  18. return 'fb';
  19. }
  20. /**
  21. * returns the follow link
  22. * @param $pageid
  23. * @return string
  24. */
  25. protected function getFollowLink($pageid){
  26. return "http://www.facebook.com/" . $pageid;
  27. }
  28. /**
  29. * gets twitter news
  30. * @param $user
  31. * @param $limit
  32. * @return stdClass[]
  33. */
  34. protected function getPosts($attributes) {
  35. extract($attributes);
  36. $feed = wp_remote_get('https://graph.facebook.com/' . $pageid . '/feed?access_token=' . $token);
  37. $xml = wp_remote_retrieve_body($feed);
  38. $json = json_decode($xml, true);
  39. $data = array();
  40. if(isset($json['data'])){
  41. foreach($json['data'] as $p) {
  42. if(!in_array($p['type'], array('status', 'photo', 'video')) || $limit<=0) { continue; }
  43. //split user and post ID (userID_postID)
  44. $idArray = explode("_", $p['id']);
  45. $post = array();
  46. $post['author'] = $p['from'];
  47. $post['content'] = isset($p['message']) ? $p['message'] : '';
  48. if($p['type'] == 'photo') {
  49. $post['image'] = $p['picture'];
  50. } elseif($p['type'] == 'video') {
  51. $post['image'] = $p['picture'];
  52. $post['content'] .= "\n\n {$p['link']}";
  53. } else {
  54. $post['image'] = null;
  55. }
  56. $post['timestamp'] = strtotime($p['created_time']);
  57. $post['like_count'] = (isset($p['likes'])) ? $p['likes']['count'] : 0;
  58. $post['comment_count'] = (isset($p['comments'])) ? $p['comments']['count'] : 0;
  59. $post['link'] = "http://www.facebook.com/".$pageid."/posts/".$idArray[1];
  60. if($post['content'] || $post['image']){
  61. $data[] = $post;
  62. $limit--;
  63. }
  64. }
  65. }
  66. return $data;
  67. }
  68. /**
  69. * counts time ago
  70. * @param $time
  71. * @return string
  72. */
  73. protected function ago($time) {
  74. $periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
  75. $lengths = array("60", "60", "24", "7", "4.35", "12", "10");
  76. $now = time();
  77. $difference = $now - $time;
  78. for ($j = 0; $difference >= $lengths[$j] && $j < count($lengths) - 1; $j++) {
  79. $difference /= $lengths[$j];
  80. }
  81. $difference = round($difference);
  82. if ($difference != 1) {
  83. $periods[$j] .= "s";
  84. }
  85. return $difference . " " . $periods[$j] . ' ' . __('ago', 'ct_theme');
  86. }
  87. /**
  88. * Returns config
  89. * @return null
  90. */
  91. public function getAttributes() {
  92. return array(
  93. 'pageid' => array('label' => __('page id', 'ct_theme'), 'default' => '', 'type' => 'input', 'help' => __("Page/user id", 'ct_theme')),
  94. 'token' => array('label' => __('token', 'ct_theme'), 'default' => '', 'type' => 'input', 'help' => __("Access token", 'ct_theme')),
  95. 'limit' => array('label' => __('limit', 'ct_theme'), 'default' => '2', 'type' => 'input', 'help' => __("Limit news", 'ct_theme')),
  96. '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'),
  97. 'newwindow' => array('label' => __("new window?", 'ct_theme'), 'default' => 'false', 'type' => 'checkbox', 'help' => "Open in new window follow us button?", 'ct_theme'),
  98. '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')),
  99. );
  100. }
  101. }