| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class LP_Thumbnail_Helper |
| 5 |
* |
| 6 |
* @since 3.0.11 |
| 7 |
*/ |
| 8 |
class LP_Thumbnail_Helper { |
| 9 |
|
| 10 |
/** |
| 11 |
* @var LP_Thumbnail_Helper |
| 12 |
*/ |
| 13 |
protected static $instance = null; |
| 14 |
|
| 15 |
/** |
| 16 |
* LP_Thumbnail_Helper constructor. |
| 17 |
*/ |
| 18 |
protected function __construct() { |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* @param int $course_id |
| 23 |
* @param string $size |
| 24 |
* @param array $attr |
| 25 |
* |
| 26 |
* @return string |
| 27 |
*/ |
| 28 |
public function get_course_image( $course_id, $size = 'course_thumbnail', $attr = array() ) { |
| 29 |
$course = learn_press_get_course( $course_id ); |
| 30 |
|
| 31 |
if ( ! $course ) { |
| 32 |
return ''; |
| 33 |
} |
| 34 |
|
| 35 |
$attr = wp_parse_args( |
| 36 |
$attr, |
| 37 |
array( |
| 38 |
'alt' => $course->get_title(), |
| 39 |
'title' => $course->get_title(), |
| 40 |
) |
| 41 |
); |
| 42 |
$image = ''; |
| 43 |
|
| 44 |
$thumbnail = learn_press_get_course_thumbnail_dimensions(); |
| 45 |
$size = array( $thumbnail['width'], $thumbnail['height'] ); |
| 46 |
|
| 47 |
$parent_id = wp_get_post_parent_id( $course_id ); |
| 48 |
|
| 49 |
if ( has_post_thumbnail( $course_id ) ) { |
| 50 |
$image = get_the_post_thumbnail( $course_id, $size, $attr ); |
| 51 |
} elseif ( $parent_id && has_post_thumbnail( $parent_id ) ) { |
| 52 |
$image = get_the_post_thumbnail( $parent_id, $size, $attr ); |
| 53 |
} |
| 54 |
|
| 55 |
if ( ! $image ) { |
| 56 |
$image = LearnPress::instance()->image( 'no-image.png' ); |
| 57 |
$image = sprintf( |
| 58 |
'<img src="%s" alt="%s">', |
| 59 |
esc_url_raw( $image ), |
| 60 |
_x( 'course thumbnail', 'no course thumbnail', 'learnpress' ) |
| 61 |
); |
| 62 |
} |
| 63 |
|
| 64 |
// @deprecated |
| 65 |
// $image = apply_filters( 'learn_press_course_image', $image, $course_id, $size, $attr ); |
| 66 |
|
| 67 |
return $image; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* @return LP_Thumbnail_Helper |
| 72 |
*/ |
| 73 |
public static function instance() { |
| 74 |
if ( ! self::$instance ) { |
| 75 |
self::$instance = new self(); |
| 76 |
} |
| 77 |
|
| 78 |
return self::$instance; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
return LP_Thumbnail_Helper::instance(); |
| 83 |
|