/classes/Wordpress/Tools.php

https://github.com/Slaver/kohana-wordpress · PHP · 187 lines · 139 code · 28 blank · 20 comment · 26 complexity · 4055176ac3b3ebab2926abe474ebd5b1 MD5 · raw file

  1. <?php defined('SYSPATH') OR die('No direct access allowed.');
  2. class Wordpress_Tools {
  3. /**
  4. * Get or generate thumbnail for post
  5. *
  6. * @param mixed $post
  7. * @param mixed $size
  8. * @param mixed $alt
  9. * @param mixed $select
  10. * @param mixed $grayscale
  11. * @param mixed $local
  12. * @return string
  13. */
  14. public static function get_post_thumbnail($post = array(), $size = 'post-thumbnail', $html = TRUE, $alt = NULL, $select = 0, $grayscale = TRUE, $local = TRUE)
  15. {
  16. if ($size === 'post-thumbnail')
  17. {
  18. $size = 'thumbnail';
  19. }
  20. // Images sizes
  21. switch ($size)
  22. {
  23. case 'large':
  24. $sizes = array(Wordpress_Options::instance()->get_option('large_size_w'), Wordpress_Options::instance()->get_option('large_size_h'));
  25. break;
  26. case 'medium':
  27. $sizes = array(Wordpress_Options::instance()->get_option('medium_size_w'), Wordpress_Options::instance()->get_option('medium_size_h'));
  28. break;
  29. default:
  30. $sizes = array(Wordpress_Options::instance()->get_option('thumbnail_size_w'), Wordpress_Options::instance()->get_option('thumbnail_size_h'));
  31. break;
  32. }
  33. // Images path root
  34. $image_root = Wordpress_Options::instance()->get_option('upload_path');
  35. if ( ! empty($post['thumb']['attach']))
  36. {
  37. // Default image (full)
  38. if ($size === 'full')
  39. {
  40. $image = Arr::path($post, 'thumb.attach.file');
  41. if ($image !== NULL)
  42. {
  43. $image = Text::reduce_slashes($image_root.'/'.$image);
  44. return ($html) ? html::image($image, array(
  45. 'width' => Arr::path($post, 'thumb.attach.width'),
  46. 'height' => Arr::path($post, 'thumb.attach.height'),
  47. 'alt' => ( ! empty($alt)) ? $alt : Arr::path($post, 'thumb.content.post_title'),
  48. ), 'http') : URL::site($image, 'http');
  49. }
  50. }
  51. // Selected image
  52. $image_url = explode('/', $post['thumb']['attach']['file']);
  53. $file_path = $image_root.'/'.str_replace(end($image_url), '', $post['thumb']['attach']['file']);
  54. $image_by_size = Arr::path($post, 'thumb.attach.sizes.'.$size.'.file');
  55. if ($image_by_size !== NULL)
  56. {
  57. $image = Text::reduce_slashes($file_path.$image_by_size);
  58. return ($html) ? html::image($image, array(
  59. 'width' => Arr::path($post, 'thumb.attach.sizes.'.$size.'.width'),
  60. 'height' => Arr::path($post, 'thumb.attach.sizes.'.$size.'.height'),
  61. 'alt' => ( ! empty($alt)) ? $alt : Arr::path($post, 'thumb.content.post_title'),
  62. ), 'http') : URL::site($image, 'http');
  63. }
  64. }
  65. else if ( ! empty($post['post_content']))
  66. {
  67. // Grep text for images
  68. $pattern = "/\<\s*img.*src\s*=\s*[\"\']?([^\"\'\ >]*)[\"\']?.*\/\>/i";
  69. if ($local)
  70. {
  71. $home = addcslashes(Wordpress_Options::instance()->get_option('siteurl'), '.-/');
  72. //$home = 'http:\/\/ultra\-music\.com\/';
  73. $pattern = "/\<\s*img.*src\s*=\s*[\"\']?(?:$home|\/)([^\"\'\ >]*)[\"\']?.*\/\>/i";
  74. }
  75. // Count images in text
  76. preg_match_all($pattern, $post['post_content'], $images);
  77. $count = count($images[1]);
  78. if ($count > 0)
  79. {
  80. $select = (isset($select) && is_numeric($select)) ? $select : rand(0, $count);
  81. $image_url = ltrim($images[1][$select], '/');
  82. $image_path = realpath($image_url);
  83. if ( ! empty($image_url))
  84. {
  85. // Create thumbnail
  86. if ($size !== 'full')
  87. {
  88. $tmp_dir = $image_root.'/cache';
  89. $tmp_path = realpath($tmp_dir);
  90. $tmp_name = substr(md5($image_url.'-'.$sizes[0].'-'.$sizes[1]), 0, 7).'.jpg';
  91. $tmp_file = $tmp_path.'/'.$tmp_name;
  92. if ( ! is_file($tmp_file))
  93. {
  94. if ( ! $tmp_path)
  95. {
  96. mkdir($image_root.'/cache', 0755);
  97. }
  98. if ($local && $image_path)
  99. {
  100. $gen_image = Image::factory($image_path)->thumbnail($sizes[0], $sizes[1]);
  101. if ($grayscale)
  102. {
  103. $gen_image->grayscale();
  104. }
  105. $gen_image->save($tmp_file, 70);
  106. }
  107. }
  108. $image = $tmp_dir.'/'.$tmp_name;
  109. if (strpos($image, '://') === FALSE) {
  110. // Add the base URL
  111. $image = URL::site($image, 'http');
  112. }
  113. return ($html) ? html::image($image, array(
  114. 'width' => $sizes[0],
  115. 'height' => $sizes[1],
  116. 'alt' => ( ! empty($alt)) ? $alt : NULL,
  117. ), 'http') : URL::site($image, 'http');
  118. }
  119. }
  120. }
  121. }
  122. }
  123. public static function resize($file, $width, $height, $path = FALSE, $name = FALSE) {
  124. if ( ! empty($file)) {
  125. $home = Wordpress_Options::instance()->get_option('siteurl').'/';
  126. $file_path = realpath(str_replace($home, '', $file));
  127. if ( ! empty($file_path)) {
  128. $ext = strtolower(pathinfo($file_path, PATHINFO_EXTENSION));
  129. if ( ! $name) {
  130. $name = md5($file.'-'.$width.'-'.$height).'.'.$ext;
  131. } else {
  132. $name = $name.'.'.$ext;
  133. }
  134. if ( ! $path) {
  135. // Images path root
  136. $root = Wordpress_Options::instance()->get_option('upload_path');
  137. $tmp_dir = $root.'/cache/';
  138. $tmp_path = realpath($tmp_dir);
  139. $dir = $tmp_dir.'/'.substr($name, 0, 2).'/'.substr($name, 2, 4).'/';
  140. $path = $tmp_path.'/'.substr($name, 0, 2).'/'.substr($name, 2, 4).'/';
  141. } else {
  142. $tmp_path = realpath($path);
  143. $dir = $path.'/';
  144. $path = $tmp_path.'/';
  145. }
  146. if ( ! is_file($path.'/'.$name)) {
  147. if ( ! is_dir($path)) {
  148. mkdir($path, 0755, TRUE);
  149. }
  150. Image::factory($file_path)
  151. ->thumbnail($width, $height)
  152. ->save($path.'/'.$name, 80);
  153. }
  154. return Text::reduce_slashes($dir.$name);
  155. }
  156. return $file;
  157. }
  158. }
  159. }