helper.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. defined('ABSPATH') || exit;
  3. function tnp_post_thumbnail_src($post, $size = 'thumbnail', $alternative = '') {
  4. if (is_object($post)) {
  5. $post = $post->ID;
  6. }
  7. // Find a media id to be used as featured image
  8. $media_id = get_post_thumbnail_id($post);
  9. if (empty($media_id)) {
  10. $attachments = get_children(array('numberpost' => 1, 'post_parent' => $post, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order'));
  11. if (!empty($attachments)) {
  12. foreach ($attachments as $id => &$attachment) {
  13. $media_id = $id;
  14. break;
  15. }
  16. }
  17. }
  18. if (!$media_id) {
  19. return $alternative;
  20. }
  21. if (!defined('NEWSLETTER_MEDIA_RESIZE') || NEWSLETTER_MEDIA_RESIZE) {
  22. if (is_array($size)) {
  23. $src = tnp_media_resize($media_id, $size);
  24. if (is_wp_error($src)) {
  25. Newsletter::instance()->logger->error($src);
  26. return $alternative;
  27. } else {
  28. return $src;
  29. }
  30. }
  31. }
  32. $media = wp_get_attachment_image_src($media_id, $size);
  33. if (strpos($media[0], 'http') !== 0) {
  34. $media[0] = 'http:' . $media[0];
  35. }
  36. return $media[0];
  37. }
  38. function tnp_post_excerpt($post, $length = 30) {
  39. if (empty($post->post_excerpt)) {
  40. $excerpt = wp_strip_all_tags(strip_shortcodes($post->post_content));
  41. $excerpt = wp_trim_words($excerpt, $length);
  42. } else {
  43. $excerpt = wp_trim_words($post->post_excerpt, $length);
  44. }
  45. $excerpt = preg_replace("/\[vc_row.*?\]/", "", $excerpt);
  46. return $excerpt;
  47. }
  48. function tnp_post_permalink($post) {
  49. return get_permalink($post->ID);
  50. }
  51. function tnp_post_content($post) {
  52. return $post->post_content;
  53. }
  54. function tnp_post_title($post) {
  55. return $post->post_title;
  56. }
  57. function tnp_post_date($post, $format = null) {
  58. if (empty($format)) {
  59. $format = get_option('date_format');
  60. }
  61. return mysql2date($format, $post->post_date);
  62. }
  63. /**
  64. * Tries to create a resized version of a media uploaded to the media library.
  65. * Returns an empty string if the media does not exists or generally if the attached file
  66. * cannot be found. If the resize fails for whatever reason, fall backs to the
  67. * standard image source returned by WP which is usually not exactly the
  68. * requested size.
  69. *
  70. * @param int $media_id
  71. * @param array $size
  72. * @return string
  73. */
  74. function tnp_media_resize($media_id, $size) {
  75. if (empty($media_id))
  76. return '';
  77. $relative_file = get_post_meta($media_id, '_wp_attached_file', true);
  78. if (empty($relative_file))
  79. return '';
  80. $width = $size[0];
  81. $height = $size[1];
  82. $crop = false;
  83. if (isset($size[2])) {
  84. $crop = (boolean) $size[2];
  85. }
  86. $uploads = wp_upload_dir();
  87. $absolute_file = $uploads['basedir'] . '/' . $relative_file;
  88. // Relative and absolute name of the thumbnail.
  89. $pathinfo = pathinfo($relative_file);
  90. $relative_thumb = $pathinfo['dirname'] . '/' . $pathinfo['filename'] . '-' . $width . 'x' .
  91. $height . ($crop ? '-c' : '') . '.' . $pathinfo['extension'];
  92. $absolute_thumb = $uploads['basedir'] . '/newsletter/thumbnails/' . $relative_thumb;
  93. // Thumbnail generation if needed.
  94. if (!file_exists($absolute_thumb) || filemtime($absolute_thumb) < filemtime($absolute_file)) {
  95. $r = wp_mkdir_p($uploads['basedir'] . '/newsletter/thumbnails/' . $pathinfo['dirname']);
  96. if (!$r) {
  97. $src = wp_get_attachment_image_src($media_id, 'full');
  98. return $src[0];
  99. }
  100. $editor = wp_get_image_editor($absolute_file);
  101. if (is_wp_error($editor)) {
  102. $src = wp_get_attachment_image_src($media_id, 'full');
  103. return $src[0];
  104. //return $editor;
  105. //return $uploads['baseurl'] . '/' . $relative_file;
  106. }
  107. $original_size = $editor->get_size();
  108. if ($width > $original_size['width'] || $height > $original_size['height']) {
  109. $src = wp_get_attachment_image_src($media_id, 'full');
  110. return $src[0];
  111. }
  112. $editor->set_quality(80);
  113. $resized = $editor->resize($width, $height, $crop);
  114. if (is_wp_error($resized)) {
  115. $src = wp_get_attachment_image_src($media_id, 'full');
  116. return $src[0];
  117. }
  118. $saved = $editor->save($absolute_thumb);
  119. if (is_wp_error($saved)) {
  120. $src = wp_get_attachment_image_src($media_id, 'full');
  121. return $src[0];
  122. //return $saved;
  123. //return $uploads['baseurl'] . '/' . $relative_file;
  124. }
  125. }
  126. return $uploads['baseurl'] . '/newsletter/thumbnails/' . $relative_thumb;
  127. }