| 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 |
$wraper_before = ''; |
| 66 |
$block_name = 'image'; |
| 67 |
$attr['headingShow'] = true; |
| 68 |
$darkImageArr = (array)$attr['darkImage']; |
| 69 |
$darkImg = [ |
| 70 |
'enable'=> $attr['darkImgEnable'], |
| 71 |
'url'=> isset($darkImageArr['url']) ? $darkImageArr['url'] : ULTP_URL.'assets/img/ultp-placeholder.jpg', |
| 72 |
'srcset' => $attr['imgSrcset'] ? ' srcset="'.esc_attr(wp_get_attachment_image_srcset($darkImageArr['id'])).'"' : '' |
| 73 |
]; |
| 74 |
|
| 75 |
$attr['className'] = isset($attr['className']) && $attr['className'] ? preg_replace('/[^A-Za-z0-9_ -]/', '', $attr['className']) : ''; |
| 76 |
$attr['align'] = isset($attr['align']) && $attr['align'] ? preg_replace('/[^A-Za-z0-9_ -]/', '', $attr['align']) : ''; |
| 77 |
$attr['advanceId'] = isset($attr['advanceId']) ? sanitize_html_class( $attr['advanceId'] ) : ''; |
| 78 |
$attr['blockId'] = isset($attr['blockId']) ? sanitize_html_class( $attr['blockId'] ) : ''; |
| 79 |
$attr['imgAnimation'] = sanitize_html_class( $attr['imgAnimation'] ); |
| 80 |
$attr['imgOverlayType'] = sanitize_html_class( $attr['imgOverlayType'] ); |
| 81 |
$allowed_html_tags = ultimate_post()->ultp_allowed_html_tags(); |
| 82 |
$attr['btnText'] = wp_kses($attr['btnText'], $allowed_html_tags); |
| 83 |
$attr['headingText'] = wp_kses($attr['headingText'], $allowed_html_tags); |
| 84 |
|
| 85 |
|
| 86 |
$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"]:'').'">'; |
| 87 |
$wraper_before .= '<div class="ultp-block-wrapper">'; |
| 88 |
$wraper_before .= '<figure class="ultp-image-block-wrapper">'; |
| 89 |
$wraper_before .= '<div class="ultp-image-block ultp-image-block-'.$attr['imgAnimation'].($attr["imgOverlay"] ? ' ultp-image-block-overlay ultp-image-block-'.$attr["imgOverlayType"] : '' ).'">'; |
| 90 |
// Single Image |
| 91 |
$img_arr = (array)$attr['imageUpload']; |
| 92 |
$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']; |
| 93 |
if ( ! empty( $img_arr ) ) { |
| 94 |
$srcset_data = $attr['imgSrcset'] ? ' srcset="'.esc_attr(wp_get_attachment_image_srcset($img_arr['id'])).'"' : ''; |
| 95 |
if ( $attr['linkType'] == 'link' && $attr['imgLink'] ) { |
| 96 |
$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>'; |
| 97 |
} else { |
| 98 |
// $wraper_before .= ultimate_post()->get_image_html($img_arr['url'], 'full', 'ultp-image', $attr['imgAlt'], $attr['imgLazy'], $darkImg); |
| 99 |
$wraper_before .= ultimate_post()->get_image_html( $img_url, 'full', 'ultp-image', $attr['imgAlt'], $attr['imgLazy'], $darkImg, $srcset_data ); |
| 100 |
} |
| 101 |
} |
| 102 |
if ( $attr['btnLink'] && $attr['linkType'] == 'button' ) { |
| 103 |
$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>'; |
| 104 |
} |
| 105 |
$wraper_before .= '</div>'; |
| 106 |
if ( $attr['headingEnable'] == 1 ) { |
| 107 |
$wraper_before .= '<figcaption class="ultp-image-caption">'.$attr['headingText'].'</figcaption>'; |
| 108 |
} |
| 109 |
$wraper_before .= '</figure>'; |
| 110 |
$wraper_before .= '</div>'; |
| 111 |
$wraper_before .= '</div>'; |
| 112 |
|
| 113 |
return $wraper_before; |
| 114 |
} |
| 115 |
} |