PageRenderTime 58ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/plugins/timber-library/lib/timber-image.php

https://gitlab.com/aristath/maera
PHP | 319 lines | 213 code | 42 blank | 64 comment | 48 complexity | 4a385b0f3f73106742b44bc577857dea MD5 | raw file
  1. <?php
  2. class TimberImage extends TimberPost implements TimberCoreInterface {
  3. public $_can_edit;
  4. public $_dimensions;
  5. public $abs_url;
  6. public $PostClass = 'TimberPost';
  7. public $object_type = 'image';
  8. public static $representation = 'image';
  9. public $file_loc;
  10. public $file;
  11. public $sizes = array();
  12. public $post_parent;
  13. public $caption;
  14. public $_wp_attached_file;
  15. /**
  16. * @param int $iid
  17. */
  18. function __construct($iid) {
  19. $this->init($iid);
  20. }
  21. /**
  22. * @return string|null
  23. */
  24. function __toString() {
  25. if ($this->get_src()) {
  26. return $this->get_src();
  27. }
  28. }
  29. /**
  30. * @return mixed
  31. */
  32. function get_pathinfo() {
  33. return pathinfo($this->file);
  34. }
  35. /**
  36. * @param string $dim
  37. * @return array|int
  38. */
  39. function get_dimensions($dim = null) {
  40. if (isset($this->_dimensions)) {
  41. return $this->get_dimensions_loaded($dim);
  42. }
  43. list($width, $height) = getimagesize($this->file_loc);
  44. $this->_dimensions = array();
  45. $this->_dimensions[0] = $width;
  46. $this->_dimensions[1] = $height;
  47. return $this->get_dimensions_loaded($dim);
  48. }
  49. /**
  50. * @param string|null $dim
  51. * @return array|int
  52. */
  53. protected function get_dimensions_loaded($dim) {
  54. if ($dim === null) {
  55. return $this->_dimensions;
  56. }
  57. if ($dim == 'w' || $dim == 'width') {
  58. return $this->_dimensions[0];
  59. }
  60. if ($dim == 'h' || $dim == 'height') {
  61. return $this->_dimensions[1];
  62. }
  63. return null;
  64. }
  65. /**
  66. * @return int
  67. */
  68. function get_width() {
  69. return $this->get_dimensions('width');
  70. }
  71. /**
  72. * @return int
  73. */
  74. function get_height() {
  75. return $this->get_dimensions('height');
  76. }
  77. /**
  78. * @param string $size
  79. * @return bool|string
  80. */
  81. function get_src( $size = '' ) {
  82. if (isset($this->abs_url)) {
  83. return $this->_maybe_secure_url($this->abs_url);
  84. }
  85. if ($size && is_string($size) && isset($this->sizes[$size])) {
  86. $image = image_downsize($this->ID, $size);
  87. return $this->_maybe_secure_url(reset($image));
  88. }
  89. if (!isset($this->file) && isset($this->_wp_attached_file)) {
  90. $this->file = $this->_wp_attached_file;
  91. }
  92. if (!isset($this->file)) {
  93. return false;
  94. }
  95. $dir = self::wp_upload_dir();
  96. $base = ($dir["baseurl"]);
  97. $src = trailingslashit($this->_maybe_secure_url($base)) . $this->file;
  98. return apply_filters('timber_image_src', $src);
  99. }
  100. private static function _maybe_secure_url($url) {
  101. if (is_ssl() && strpos($url, 'https') !== 0 && strpos($url, 'http') === 0) {
  102. $url = 'https' . substr($url, strlen('http'));
  103. }
  104. return $url;
  105. }
  106. public static function wp_upload_dir() {
  107. static $wp_upload_dir = false;
  108. if (!$wp_upload_dir) {
  109. $wp_upload_dir = wp_upload_dir();
  110. }
  111. return $wp_upload_dir;
  112. }
  113. /**
  114. * @return string
  115. */
  116. function get_path() {
  117. if (strlen($this->abs_url)) {
  118. return $this->abs_url;
  119. }
  120. return get_permalink($this->ID);
  121. }
  122. /**
  123. * @return bool|TimberImage
  124. */
  125. function get_parent() {
  126. if (!$this->post_parent) {
  127. return false;
  128. }
  129. return new $this->PostClass($this->post_parent);
  130. }
  131. function get_alt() {
  132. $alt = trim(strip_tags(get_post_meta($this->ID, '_wp_attachment_image_alt', true)));
  133. return $alt;
  134. }
  135. /**
  136. * @param int $iid
  137. */
  138. function init( $iid = false ) {
  139. if ( !is_numeric( $iid ) && is_string( $iid ) ) {
  140. if (strstr($iid, '://')) {
  141. $this->init_with_url($iid);
  142. return;
  143. }
  144. if ( strstr($iid, ABSPATH) ) {
  145. $this->init_with_file_path($iid);
  146. return;
  147. }
  148. if (strstr(strtolower($iid), '.jpg')) {
  149. $this->init_with_relative_path($iid);
  150. return;
  151. }
  152. }
  153. $image_info = $this->get_image_info($iid);
  154. $this->import($image_info);
  155. $basedir = self::wp_upload_dir();
  156. $basedir = $basedir['basedir'];
  157. if (isset($this->file)) {
  158. $this->file_loc = $basedir . DIRECTORY_SEPARATOR . $this->file;
  159. } else if (isset($this->_wp_attached_file)) {
  160. $this->file = reset($this->_wp_attached_file);
  161. $this->file_loc = $basedir . DIRECTORY_SEPARATOR . $this->file;
  162. }
  163. if (isset($image_info['id'])) {
  164. $this->ID = $image_info['id'];
  165. } else if (is_numeric($iid)) {
  166. $this->ID = $iid;
  167. }
  168. if (isset($this->ID)) {
  169. $custom = get_post_custom($this->ID);
  170. foreach ($custom as $key => $value) {
  171. $this->$key = $value[0];
  172. }
  173. } else {
  174. if (is_array($iid) || is_object($iid)) {
  175. TimberHelper::error_log('Not able to init in TimberImage with iid=');
  176. TimberHelper::error_log($iid);
  177. } else {
  178. TimberHelper::error_log('Not able to init in TimberImage with iid=' . $iid);
  179. }
  180. }
  181. }
  182. private function get_image_info( $iid ) {
  183. $image_info = $iid;
  184. if (is_numeric($iid)) {
  185. $image_info = wp_get_attachment_metadata($iid);
  186. if (!is_array($image_info)) {
  187. $image_info = array();
  188. }
  189. $image_custom = get_post_custom($iid);
  190. $basic = get_post($iid);
  191. if ($basic) {
  192. if (isset($basic->post_excerpt)) {
  193. $this->caption = $basic->post_excerpt;
  194. }
  195. $image_custom = array_merge($image_custom, get_object_vars($basic));
  196. }
  197. return array_merge($image_info, $image_custom);
  198. }
  199. if (is_array($image_info) && isset($image_info['image'])) {
  200. return $image_info['image'];
  201. }
  202. if (is_object($image_info)) {
  203. return get_object_vars($image_info);
  204. }
  205. return $iid;
  206. }
  207. private function init_with_relative_path( $relative_path ) {
  208. $this->abs_url = home_url( $relative_path );
  209. $file_path = TimberURLHelper::get_full_path( $relative_path );
  210. $this->file_loc = $file_path;
  211. $this->file = $file_path;
  212. }
  213. private function init_with_file_path( $file_path ) {
  214. $url = TimberURLHelper::file_system_to_url( $file_path );
  215. $this->abs_url = $url;
  216. $this->file_loc = $file_path;
  217. $this->file = $file_path;
  218. }
  219. /**
  220. * @param string $url
  221. */
  222. private function init_with_url($url) {
  223. $this->abs_url = $url;
  224. if (TimberURLHelper::is_local($url)) {
  225. $this->file = ABSPATH . TimberURLHelper::get_rel_url($url);
  226. $this->file_loc = ABSPATH . TimberURLHelper::get_rel_url($url);
  227. }
  228. }
  229. /**
  230. * @deprecated; use src() instead
  231. * @return string
  232. */
  233. function get_url() {
  234. return $this->get_src();
  235. }
  236. /**
  237. * @deprecated; use src() instead
  238. * @return string
  239. */
  240. function url() {
  241. return $this->get_src();
  242. }
  243. /* Alias */
  244. /**
  245. * @return float
  246. */
  247. public function aspect() {
  248. $w = intval($this->width());
  249. $h = intval($this->height());
  250. return $w / $h;
  251. }
  252. /**
  253. * @return int
  254. */
  255. public function height() {
  256. return $this->get_height();
  257. }
  258. /**
  259. * @param string $size
  260. * @return bool|string
  261. */
  262. public function src($size = '') {
  263. return $this->get_src($size);
  264. }
  265. /**
  266. * @return int
  267. */
  268. public function width() {
  269. return $this->get_width();
  270. }
  271. /**
  272. * @return string alt text stored in WordPress
  273. */
  274. public function alt() {
  275. return $this->get_alt();
  276. }
  277. }