| 1 |
<?php |
| 2 |
namespace ULTP\blocks; |
| 3 |
|
| 4 |
defined( 'ABSPATH' ) || exit; |
| 5 |
|
| 6 |
class Image { |
| 7 |
|
| 8 |
public function __construct() { |
| 9 |
add_action( 'init', array( $this, 'register' ) ); |
| 10 |
} |
| 11 |
public function get_attributes() { |
| 12 |
return array( |
| 13 |
'blockId' => '', |
| 14 |
'previewImg' => '', |
| 15 |
// Image Settings |
| 16 |
'imageUpload' => (object)[ 'id'=>'999999', 'url' => ULTP_URL.'assets/img/ultp-placeholder.jpg' ], |
| 17 |
'darkImgEnable' => false, |
| 18 |
'darkImage' => (object)[ 'id'=>'999999', 'url' => ULTP_URL.'assets/img/ultp-placeholder.jpg' ], |
| 19 |
'linkType' => 'link', |
| 20 |
'imgLink' => '', |
| 21 |
'linkTarget' => '_blank', |
| 22 |
'imgAlt' => 'Image', |
| 23 |
'imgAlignment' => ['lg' => 'left'], |
| 24 |
'imgCrop' => 'full', |
| 25 |
'imgAnimation' => 'none', |
| 26 |
'imgMargin' => (object)['lg'=>''], |
| 27 |
'imgOverlay' => false, |
| 28 |
'imgOverlayType' => 'default', |
| 29 |
'imgSrcset' => false, |
| 30 |
'imgLazy' => false, |
| 31 |
// Heading Setting/Style |
| 32 |
'headingText' => 'This is a Image Example', |
| 33 |
'headingEnable' => false, |
| 34 |
'alignment' => ['lg' => 'left'], |
| 35 |
// Button Settings |
| 36 |
'buttonEnable' => false, |
| 37 |
'btnText' => 'Free Download', |
| 38 |
'btnLink' => '#', |
| 39 |
'btnTarget' => '_blank', |
| 40 |
'btnPosition' => 'centerCenter', |
| 41 |
// Style |
| 42 |
|
| 43 |
'advanceId' => '', |
| 44 |
'advanceZindex' => '', |
| 45 |
'hideExtraLarge' => false, |
| 46 |
'hideTablet' => false, |
| 47 |
'hideMobile' => false, |
| 48 |
'advanceCss' => '', |
| 49 |
); |
| 50 |
} |
| 51 |
|
| 52 |
public function register() { |
| 53 |
register_block_type( 'ultimate-post/image', |
| 54 |
array( |
| 55 |
'editor_script' => 'ultp-blocks-editor-script', |
| 56 |
'editor_style' => 'ultp-blocks-editor-css', |
| 57 |
'render_callback' => array( $this, 'content' ) |
| 58 |
) |
| 59 |
); |
| 60 |
} |
| 61 |
|
| 62 |
public function content( $attr, $noAjax ) { |
| 63 |
$attr = wp_parse_args( $attr, $this->get_attributes() ); |
| 64 |
|
| 65 |
// Dynamic Content |
| 66 |
if (ultimate_post()->is_dc_active($attr)) { |
| 67 |
|
| 68 |
// Dark Image |
| 69 |
$dark_image_content_src = $attr['dc']['darkImage']['contentSrc']; |
| 70 |
$dark_image_post_id = $attr['dc']['darkImage']['postId']; |
| 71 |
|
| 72 |
if (is_string($dark_image_content_src) && $attr['dcEnabled']['darkImage']) { |
| 73 |
$attr['darkImage']['url'] = \ULTP\DCService::get_dc_content_for_image($dark_image_post_id, $dark_image_content_src); |
| 74 |
} |
| 75 |
|
| 76 |
// Image |
| 77 |
$image_content_src = $attr['dc']['imageUpload']['contentSrc']; |
| 78 |
$image_post_id = $attr['dc']['imageUpload']['postId']; |
| 79 |
|
| 80 |
if (is_string($image_content_src) && $attr['dcEnabled']['imageUpload']) { |
| 81 |
$attr['imageUpload']['url'] = \ULTP\DCService::get_dc_content_for_image($image_post_id, $image_content_src); |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
// imgLink |
| 86 |
$img_link_link_src = $attr['dc']['imgLink']['linkSrc']; |
| 87 |
$img_link_post_id = $attr['dc']['imgLink']['postId']; |
| 88 |
if (is_string($img_link_link_src) && $attr['dcEnabled']['imgLink']) { |
| 89 |
if (str_starts_with($img_link_link_src, 'link_')) { |
| 90 |
$attr['imgLink'] = \ULTP\DCService::get_link($img_link_post_id, $img_link_link_src); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
// btnLink |
| 95 |
$btn_link_link_src = $attr['dc']['btnLink']['linkSrc']; |
| 96 |
$btn_link_post_id = $attr['dc']['btnLink']['postId']; |
| 97 |
if (is_string($btn_link_link_src) && $attr['dcEnabled']['btnLink']) { |
| 98 |
if (str_starts_with($btn_link_link_src, 'link_')) { |
| 99 |
$attr['btnLink'] = \ULTP\DCService::get_link($btn_link_post_id, $btn_link_link_src); |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
$wraper_before = ''; |
| 105 |
$block_name = 'image'; |
| 106 |
$attr['headingShow'] = true; |
| 107 |
$darkImageArr = (array)$attr['darkImage']; |
| 108 |
$darkImg = [ |
| 109 |
'enable'=> $attr['darkImgEnable'], |
| 110 |
'url'=> isset($darkImageArr['url']) ? $darkImageArr['url'] : ULTP_URL.'assets/img/ultp-placeholder.jpg', |
| 111 |
'srcset' => ( $attr['imgSrcset'] && isset($darkImageArr['id']) ) ? ' srcset="'.esc_attr(wp_get_attachment_image_srcset($darkImageArr['id'])).'"' : '' |
| 112 |
]; |
| 113 |
|
| 114 |
$attr['className'] = isset($attr['className']) && $attr['className'] ? preg_replace('/[^A-Za-z0-9_ -]/', '', $attr['className']) : ''; |
| 115 |
$attr['align'] = isset($attr['align']) && $attr['align'] ? preg_replace('/[^A-Za-z0-9_ -]/', '', $attr['align']) : ''; |
| 116 |
$attr['advanceId'] = isset($attr['advanceId']) ? sanitize_html_class( $attr['advanceId'] ) : ''; |
| 117 |
$attr['blockId'] = isset($attr['blockId']) ? sanitize_html_class( $attr['blockId'] ) : ''; |
| 118 |
$attr['imgAnimation'] = sanitize_html_class( $attr['imgAnimation'] ); |
| 119 |
$attr['imgOverlayType'] = sanitize_html_class( $attr['imgOverlayType'] ); |
| 120 |
$allowed_html_tags = ultimate_post()->ultp_allowed_html_tags(); |
| 121 |
$attr['btnText'] = wp_kses($attr['btnText'], $allowed_html_tags); |
| 122 |
$attr['headingText'] = wp_kses($attr['headingText'], $allowed_html_tags); |
| 123 |
|
| 124 |
|
| 125 |
$wraper_before .= '<div '.($attr['advanceId'] ? 'id="'.$attr['advanceId'].'" ':'').' class="wp-block-ultimate-post-'.$block_name.' ultp-block-'.$attr["blockId"].''.($attr["align"] ? ' align' .$attr["align"]:'').''.($attr["className"] ?' '.$attr["className"]:'').'">'; |
| 126 |
$wraper_before .= '<div class="ultp-block-wrapper">'; |
| 127 |
$wraper_before .= '<figure class="ultp-image-block-wrapper">'; |
| 128 |
$wraper_before .= '<div class="ultp-image-block ultp-image-block-'.$attr['imgAnimation'].($attr["imgOverlay"] ? ' ultp-image-block-overlay ultp-image-block-'.$attr["imgOverlayType"] : '' ).'">'; |
| 129 |
// Single Image |
| 130 |
$img_arr = (array)$attr['imageUpload']; |
| 131 |
$img_url = ( isset($img_arr['size']) && isset($img_arr['size'][$attr['imgCrop']]) ) ? ( isset($img_arr['size'][$attr['imgCrop']]['url']) ? $img_arr['size'][$attr['imgCrop']]['url'] : $img_arr['size'][$attr['imgCrop']] ) : $img_arr['url']; |
| 132 |
if ( ! empty( $img_arr ) ) { |
| 133 |
$srcset_data = $attr['imgSrcset'] ? ' srcset="'.esc_attr(wp_get_attachment_image_srcset($img_arr['id'])).'"' : ''; |
| 134 |
if ( $attr['linkType'] == 'link' && $attr['imgLink'] ) { |
| 135 |
$wraper_before .= '<a href="'.esc_url($attr['imgLink']).'" target="'.sanitize_html_class( $attr['linkTarget'] ).'">'.ultimate_post()->get_image_html($img_url, 'full', 'ultp-image', $attr['imgAlt'], $attr['imgLazy'], $darkImg, $srcset_data).'</a>'; |
| 136 |
} else { |
| 137 |
// $wraper_before .= ultimate_post()->get_image_html($img_arr['url'], 'full', 'ultp-image', $attr['imgAlt'], $attr['imgLazy'], $darkImg); |
| 138 |
$wraper_before .= ultimate_post()->get_image_html( $img_url, 'full', 'ultp-image', $attr['imgAlt'], $attr['imgLazy'], $darkImg, $srcset_data ); |
| 139 |
} |
| 140 |
} |
| 141 |
if ( $attr['btnLink'] && $attr['linkType'] == 'button' ) { |
| 142 |
$wraper_before .= '<div class="ultp-image-button ultp-image-button-'.sanitize_html_class( $attr['btnPosition']).'"><a href="'.esc_url($attr['btnLink']).'" target="'.sanitize_html_class($attr['btnTarget']).'">'.$attr['btnText'].'</a></div>'; |
| 143 |
} |
| 144 |
$wraper_before .= '</div>'; |
| 145 |
if ( $attr['headingEnable'] == 1 ) { |
| 146 |
$wraper_before .= '<figcaption class="ultp-image-caption">'.$attr['headingText'].'</figcaption>'; |
| 147 |
} |
| 148 |
$wraper_before .= '</figure>'; |
| 149 |
$wraper_before .= '</div>'; |
| 150 |
$wraper_before .= '</div>'; |
| 151 |
|
| 152 |
return $wraper_before; |
| 153 |
} |
| 154 |
} |