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