PageRenderTime 36ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/webapp/plugins/instagram/extlib/Instagram/Media.php

https://github.com/ShadMickelberry/ThinkUp
PHP | 263 lines | 92 code | 25 blank | 146 comment | 11 complexity | 7a62adbf0b6fccdfde48b309d116bb92 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1, Apache-2.0, GPL-2.0
  1. <?php
  2. /**
  3. * Instagram PHP
  4. * @author Galen Grover <galenjr@gmail.com>
  5. * @license http://opensource.org/licenses/mit-license.php The MIT License
  6. */
  7. namespace Instagram;
  8. use \Instagram\Comment;
  9. use \Instagram\User;
  10. use \Instagram\Location;
  11. use \Instagram\Collection\CommentCollection;
  12. use \Instagram\Collection\TagCollection;
  13. use \Instagram\Collection\UserCollection;
  14. /**
  15. * Media class
  16. *
  17. * @see \Instagram\Instagram->getLocation()
  18. * {@link https://github.com/galen/PHP-Instagram-API/blob/master/Examples/media.php}
  19. * {@link http://galengrover.com/projects/instagram/?example=media.php}
  20. */
  21. class Media extends \Instagram\Core\BaseObjectAbstract {
  22. /**
  23. * User cache
  24. *
  25. * @var \Instagram\User
  26. */
  27. protected $user = null;
  28. /**
  29. * Comments cache
  30. *
  31. * @var \Instagram\Collection\CommentCollection
  32. */
  33. protected $comments = null;
  34. /**
  35. * Location cache
  36. *
  37. * @var \Instagram\Location
  38. */
  39. protected $location = null;
  40. /**
  41. * Tags cache
  42. *
  43. * @var \Instagram\Collection\TagCollection
  44. */
  45. protected $tags = null;
  46. /**
  47. * Get the thumbnail
  48. *
  49. * @return string
  50. * @access public
  51. */
  52. public function getThumbnail() {
  53. return $this->data->images->thumbnail;
  54. }
  55. /**
  56. * Get the standard resolution image
  57. *
  58. * @return string
  59. * @access public
  60. */
  61. public function getStandardRes() {
  62. return $this->data->images->standard_resolution;
  63. }
  64. /**
  65. * Get the low resolution image
  66. *
  67. * @return string
  68. * @access public
  69. */
  70. public function getLowRes() {
  71. return $this->data->images->low_resolution;
  72. }
  73. /**
  74. * Get the media caption
  75. *
  76. * @return string
  77. * @access public
  78. */
  79. public function getCaption() {
  80. if ( $this->data->caption ) {
  81. return new Comment( $this->data->caption );
  82. }
  83. return null;
  84. }
  85. /**
  86. * Get the created time
  87. *
  88. * @param string $format {@link http://php.net/manual/en/function.date.php}
  89. * @return string
  90. * @access public
  91. */
  92. public function getCreatedTime( $format = null ) {
  93. if ( $format ) {
  94. $date = date( $format, $this->data->created_time );
  95. }
  96. else {
  97. $date = $this->data->created_time;
  98. }
  99. return $date;
  100. }
  101. /**
  102. * Get the user that posted the media
  103. *
  104. * @return \Instagram\User
  105. * @access public
  106. */
  107. public function getUser() {
  108. if ( !$this->user ) {
  109. $this->user = new User( $this->data->user, $this->proxy );
  110. }
  111. return $this->user;
  112. }
  113. /**
  114. * Get media comments
  115. *
  116. * Return all the comments associated with a media
  117. *
  118. * @return \Instagram\CommentCollection
  119. * @access public
  120. */
  121. public function getComments() {
  122. if ( !$this->comments ) {
  123. $this->comments = new CommentCollection( $this->proxy->getMediaComments( $this->getApiId() ), $this->proxy );
  124. }
  125. return $this->comments;
  126. }
  127. /**
  128. * Get the media filter
  129. *
  130. * @return string
  131. * @access public
  132. */
  133. public function getFilter() {
  134. return $this->data->filter;
  135. }
  136. /**
  137. * Get the media's tags
  138. *
  139. * @return \Instagram\Collection\TagCollection
  140. * @access public
  141. */
  142. public function getTags() {
  143. if ( !$this->tags ) {
  144. $this->tags = new TagCollection( $this->data->tags, $this->proxy );
  145. }
  146. return $this->tags;
  147. }
  148. /**
  149. * Get the media's link
  150. *
  151. * @return string
  152. * @access public
  153. */
  154. public function getLink() {
  155. return $this->data->link;
  156. }
  157. /**
  158. * Get the media's likes count
  159. *
  160. * @return int
  161. * @access public
  162. */
  163. public function getLikesCount() {
  164. return (int)$this->data->likes->count;
  165. }
  166. /**
  167. * Get media likes
  168. *
  169. * Media objects contain the first 10 likes. You can get these likes by passing `false`
  170. * to this method. Using the internal likes of a media object cause issues when liking/disliking media.
  171. *
  172. * @param bool $fetch_from_api Query the API or use internal
  173. * @return \Instagram\UserCollection
  174. * @access public
  175. */
  176. public function getLikes( $fetch_from_api = true ) {
  177. if ( !$fetch_from_api ) {
  178. return new UserCollection( $this->data->likes );
  179. }
  180. $user_collection = new UserCollection( $this->proxy->getMediaLikes( $this->getApiId() ), $this->proxy );
  181. $user_collection->setProxies( $this->proxy );
  182. $this->likes = $user_collection;
  183. return $this->likes;
  184. }
  185. /**
  186. * Get location status
  187. *
  188. * Will return true if any location data is associated with the media
  189. *
  190. * @return bool
  191. * @access public
  192. */
  193. public function hasLocation() {
  194. return isset( $this->data->location->latitude ) && isset( $this->data->location->longitude );
  195. }
  196. /**
  197. * Get location status
  198. *
  199. * Will return true if the media has a named location attached to it
  200. *
  201. * Some media only has lat/lng data
  202. *
  203. * @return bool
  204. * @access public
  205. */
  206. public function hasNamedLocation() {
  207. return isset( $this->data->location->id );
  208. }
  209. /**
  210. * Get the location
  211. *
  212. * Returns the location associated with the media or null if no location data is available
  213. *
  214. * @param bool $force_fetch Don't use the cache
  215. * @return \Instagram\Location|null
  216. * @access public
  217. */
  218. public function getLocation( $force_fetch = false ) {
  219. if ( !$this->hasLocation() ) {
  220. return null;
  221. }
  222. if ( !$this->location || (bool)$force_fetch ) {
  223. $this->location = new Location( $this->data->location, isset( $this->data->location->id ) ? $this->proxy : null );
  224. }
  225. return $this->location;
  226. }
  227. /**
  228. * Magic toString method
  229. *
  230. * Returns the media's thumbnail url
  231. *
  232. * @return string
  233. * @access public
  234. */
  235. public function __toString() {
  236. return $this->getThumbnail()->url;
  237. }
  238. }