| 1 |
<?php |
| 2 |
namespace ULTP\blocks; |
| 3 |
|
| 4 |
defined('ABSPATH') || exit; |
| 5 |
|
| 6 |
class Post_Featured_Image { |
| 7 |
public function __construct() { |
| 8 |
add_action('init', array($this, 'register')); |
| 9 |
} |
| 10 |
public function get_attributes() { |
| 11 |
|
| 12 |
return array( |
| 13 |
'blockId' => '', |
| 14 |
/*============================ |
| 15 |
Post Featured Image Setting |
| 16 |
============================*/ |
| 17 |
'defImgShow' => false, |
| 18 |
'altText' => 'Image', |
| 19 |
'imgCrop' => 'full', |
| 20 |
'imgSrcset' => false, |
| 21 |
|
| 22 |
/*============================ |
| 23 |
Dynamic Caption |
| 24 |
============================*/ |
| 25 |
'enableCaption' => false, |
| 26 |
|
| 27 |
/*============================ |
| 28 |
Video Settings |
| 29 |
============================*/ |
| 30 |
'enableVideoCaption' => false, |
| 31 |
'videoWidth' => (object)['lg' =>'100'], |
| 32 |
'stickyEnable' => false, |
| 33 |
|
| 34 |
/*============================ |
| 35 |
Advanced Settings |
| 36 |
============================*/ |
| 37 |
'advanceId' => '', |
| 38 |
'advanceZindex' => '', |
| 39 |
'hideExtraLarge' => false, |
| 40 |
'hideDesktop' => false, |
| 41 |
'hideTablet' => false, |
| 42 |
'hideMobile' => false, |
| 43 |
'advanceCss' => '', |
| 44 |
); |
| 45 |
} |
| 46 |
|
| 47 |
public function register() { |
| 48 |
register_block_type( 'ultimate-post/post-featured-image', |
| 49 |
array( |
| 50 |
'editor_script' => 'ultp-blocks-editor-script', |
| 51 |
'editor_style' => 'ultp-blocks-editor-css', |
| 52 |
'render_callback' => array($this, 'content') |
| 53 |
) |
| 54 |
); |
| 55 |
} |
| 56 |
|
| 57 |
public function content($attr, $noAjax) { |
| 58 |
$attr = wp_parse_args($attr, $this->get_attributes()); |
| 59 |
$block_name = 'post-image'; |
| 60 |
$wrapper_before = $wrapper_after = $content = ''; |
| 61 |
|
| 62 |
$attr['className'] = isset($attr['className']) && $attr['className'] ? preg_replace('/[^A-Za-z0-9_ -]/', '', $attr['className']) : ''; |
| 63 |
$attr['align'] = isset($attr['align']) && $attr['align'] ? preg_replace('/[^A-Za-z0-9_ -]/', '', $attr['align']) : ''; |
| 64 |
$attr['advanceId'] = isset($attr['advanceId']) ? sanitize_html_class( $attr['advanceId'] ) : ''; |
| 65 |
$attr['blockId'] = isset($attr['blockId']) ? sanitize_html_class( $attr['blockId'] ) : ''; |
| 66 |
$attr['altText'] = wp_kses($attr['altText'], ultimate_post()->ultp_allowed_html_tags()); |
| 67 |
|
| 68 |
$post_video = get_post_meta(get_the_ID(), '__builder_feature_video', true); |
| 69 |
$caption = get_post_meta(get_the_ID(), '__builder_feature_caption', true); |
| 70 |
|
| 71 |
$embeded = $post_video ? ultimate_post()->get_embeded_video($post_video, false, true, false, true, true, false, true, array('width' => array('width' => $attr["videoWidth"])) ) : ''; |
| 72 |
$post_thumb_id = get_post_thumbnail_id(get_the_ID()); |
| 73 |
$img_content = ultimate_post()->get_image($post_thumb_id, $attr['imgCrop'], '', $attr['altText'], $attr['imgSrcset']); |
| 74 |
$img_caption = wp_get_attachment_caption($post_thumb_id); |
| 75 |
|
| 76 |
if ( $embeded || has_post_thumbnail() ) { |
| 77 |
$isvideo = $embeded ? ( isset($attr['defImgShow']) && $attr['defImgShow'] && $post_thumb_id ? false : true ) : false; |
| 78 |
$wrapper_before .= '<div '.( $attr['advanceId'] ? 'id="'.$attr['advanceId'].'" ':'' ).' class="wp-block-ultimate-post-'.$block_name.' ultp-block-'.$attr["blockId"].( $attr["className"] ?' '.$attr["className"]:'' ).''.( $attr["align"] ? ' align' .$attr["align"]:'' ).'">'; |
| 79 |
$wrapper_before .= '<div class="ultp-block-wrapper">'; |
| 80 |
$wrapper_before .= '<div class="ultp-image-wrapper">'; |
| 81 |
$wrapper_before .= '<div class="ultp-builder-'.($isvideo ? "video": "image").'">'; |
| 82 |
$content .= '<div class="ultp-'.($isvideo ? "video": "image").'-block'.($attr['stickyEnable'] ? " ultp-sticky-video": "").'">'; |
| 83 |
$content .= $isvideo ? $embeded : $img_content ; |
| 84 |
$wrapper_after .= '<span class="ultp-sticky-close"></span></div>'; |
| 85 |
$wrapper_after .= '</div>'; |
| 86 |
$wrapper_after .= '</div>'; |
| 87 |
|
| 88 |
if($attr['enableCaption'] && $img_caption || $caption && $attr['enableVideoCaption']){ |
| 89 |
$wrapper_after .= '<div class="ultp-featureImg-caption">'; |
| 90 |
$wrapper_after .= $isvideo ? $caption : $img_caption; |
| 91 |
$wrapper_after .= '</div>'; |
| 92 |
} |
| 93 |
$wrapper_after .= '</div>'; |
| 94 |
$wrapper_after .= '</div>'; |
| 95 |
} |
| 96 |
return $wrapper_before.$content.$wrapper_after; |
| 97 |
} |
| 98 |
} |