| 1 |
<?php |
| 2 |
|
| 3 |
namespace Getwid\Blocks; |
| 4 |
|
| 5 |
class AcfImage extends \Getwid\Blocks\AbstractBlock { |
| 6 |
|
| 7 |
protected static $blockName = 'getwid/template-acf-image'; |
| 8 |
protected static $assetsHandle = 'getwid/template-parts/acf'; |
| 9 |
|
| 10 |
public function __construct() { |
| 11 |
|
| 12 |
parent::__construct( self::$blockName ); |
| 13 |
|
| 14 |
register_block_type( |
| 15 |
self::$blockName, |
| 16 |
array( |
| 17 |
'attributes' => array( |
| 18 |
'align' => array( |
| 19 |
'type' => 'string' |
| 20 |
), |
| 21 |
'linkTo' => array( |
| 22 |
'type' => 'string', |
| 23 |
'default' => 'none' |
| 24 |
), |
| 25 |
'customField' => array( |
| 26 |
'type' => 'string' |
| 27 |
), |
| 28 |
'imageSize' => array( |
| 29 |
'type' => 'string', |
| 30 |
'default' => 'large' |
| 31 |
), |
| 32 |
|
| 33 |
'className' => array( |
| 34 |
'type' => 'string' |
| 35 |
), |
| 36 |
), |
| 37 |
'render_callback' => [$this, 'render_callback'] |
| 38 |
) |
| 39 |
); |
| 40 |
} |
| 41 |
|
| 42 |
public function block_frontend_assets() { |
| 43 |
|
| 44 |
if ( is_admin() ) { |
| 45 |
return; |
| 46 |
} |
| 47 |
|
| 48 |
if ( FALSE == getwid()->assetsOptimization()->load_assets_on_demand() ) { |
| 49 |
return; |
| 50 |
} |
| 51 |
|
| 52 |
add_filter( 'getwid/optimize/assets', |
| 53 |
function ( $assets ) { |
| 54 |
$assets[] = self::$assetsHandle; |
| 55 |
|
| 56 |
return $assets; |
| 57 |
} |
| 58 |
); |
| 59 |
|
| 60 |
$rtl = is_rtl() ? '.rtl' : ''; |
| 61 |
|
| 62 |
wp_enqueue_style( |
| 63 |
self::$assetsHandle, |
| 64 |
getwid_get_plugin_url( 'assets/blocks/template-parts/acf/style' . $rtl . '.css' ), |
| 65 |
[], |
| 66 |
getwid()->settings()->getVersion() |
| 67 |
); |
| 68 |
} |
| 69 |
|
| 70 |
public function render_callback( $attributes, $content ) { |
| 71 |
|
| 72 |
//Not BackEnd render if we view from template page |
| 73 |
if ( (get_post_type() == getwid()->postTemplatePart()->postType) || (get_post_type() == 'revision') ) { |
| 74 |
return $content; |
| 75 |
} |
| 76 |
|
| 77 |
$block_name = 'wp-block-getwid-template-acf-image'; |
| 78 |
|
| 79 |
$wrapper_class = $block_name; |
| 80 |
|
| 81 |
if ( isset( $attributes['className'] ) ) { |
| 82 |
$wrapper_class .= ' ' . esc_attr( $attributes['className'] ); |
| 83 |
} |
| 84 |
|
| 85 |
if ( isset( $attributes['customField'] ) ) { |
| 86 |
$wrapper_class .= ' ' . 'custom-field-' . esc_attr( $attributes['customField'] ); |
| 87 |
} |
| 88 |
|
| 89 |
if ( isset( $attributes['align'] ) ) { |
| 90 |
$wrapper_class .= ' align' . esc_attr( $attributes['align'] ); |
| 91 |
} |
| 92 |
|
| 93 |
$imageSize = ((isset( $attributes['imageSize'] ) && $attributes['imageSize']) ? $attributes['imageSize'] : 'post-thumbnail'); |
| 94 |
|
| 95 |
$result = ''; |
| 96 |
|
| 97 |
$extra_attr = array( |
| 98 |
'wrapper_class' => $wrapper_class, |
| 99 |
'imageSize' => $imageSize |
| 100 |
); |
| 101 |
|
| 102 |
if ( getwid_acf_is_active() && isset( $attributes['customField'] ) ) { |
| 103 |
ob_start(); |
| 104 |
|
| 105 |
getwid_get_template_part( 'template-parts/acf/image', $attributes, false, $extra_attr ); |
| 106 |
|
| 107 |
$result = ob_get_clean(); |
| 108 |
} |
| 109 |
|
| 110 |
$this->block_frontend_assets(); |
| 111 |
|
| 112 |
return $result; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
new \Getwid\Blocks\AcfImage(); |
| 117 |
|