| 1 |
<?php |
| 2 |
|
| 3 |
namespace Getwid\Blocks; |
| 4 |
|
| 5 |
class PostCustomField extends \Getwid\Blocks\AbstractBlock { |
| 6 |
|
| 7 |
protected static $blockName = 'getwid/template-post-custom-field'; |
| 8 |
protected static $assetsHandle = 'getwid/template-parts'; |
| 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 |
//Colors |
| 19 |
'textColor' => array( |
| 20 |
'type' => 'string' |
| 21 |
), |
| 22 |
'customTextColor' => array( |
| 23 |
'type' => 'string' |
| 24 |
), |
| 25 |
|
| 26 |
//Colors |
| 27 |
'fontSize' => array( |
| 28 |
'type' => 'string' |
| 29 |
), |
| 30 |
'customFontSize' => array( |
| 31 |
'type' => 'string' |
| 32 |
), |
| 33 |
'customField' => array( |
| 34 |
'type' => 'string' |
| 35 |
), |
| 36 |
'bold' => array( |
| 37 |
'type' => 'boolean', |
| 38 |
'default' => false |
| 39 |
), |
| 40 |
'italic' => array( |
| 41 |
'type' => 'boolean', |
| 42 |
'default' => false |
| 43 |
), |
| 44 |
'textAlignment' => array( |
| 45 |
'type' => 'string' |
| 46 |
), |
| 47 |
'className' => array( |
| 48 |
'type' => 'string' |
| 49 |
), |
| 50 |
), |
| 51 |
'render_callback' => [ $this, 'render_callback' ] |
| 52 |
) |
| 53 |
); |
| 54 |
} |
| 55 |
|
| 56 |
public function block_frontend_assets() { |
| 57 |
|
| 58 |
if ( is_admin() ) { |
| 59 |
return; |
| 60 |
} |
| 61 |
|
| 62 |
if ( FALSE == getwid()->assetsOptimization()->load_assets_on_demand() ) { |
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
add_filter( 'getwid/optimize/assets', |
| 67 |
function ( $assets ) { |
| 68 |
$assets[] = self::$assetsHandle; |
| 69 |
|
| 70 |
return $assets; |
| 71 |
} |
| 72 |
); |
| 73 |
|
| 74 |
$rtl = is_rtl() ? '.rtl' : ''; |
| 75 |
|
| 76 |
wp_enqueue_style( |
| 77 |
self::$assetsHandle, |
| 78 |
getwid_get_plugin_url( 'assets/blocks/template-parts/style' . $rtl . '.css' ), |
| 79 |
[], |
| 80 |
getwid()->settings()->getVersion() |
| 81 |
); |
| 82 |
} |
| 83 |
|
| 84 |
public function render_callback( $attributes, $content ) { |
| 85 |
|
| 86 |
//Not BackEnd render if we view from template page |
| 87 |
if ( (get_post_type() == getwid()->postTemplatePart()->postType) || (get_post_type() == 'revision') ){ |
| 88 |
return $content; |
| 89 |
} |
| 90 |
|
| 91 |
$block_name = 'wp-block-getwid-template-post-custom-field'; |
| 92 |
$wrapper_class = $block_name; |
| 93 |
|
| 94 |
if ( isset( $attributes[ 'className' ] ) ) { |
| 95 |
$wrapper_class .= ' ' . esc_attr( $attributes[ 'className' ] ); |
| 96 |
} |
| 97 |
|
| 98 |
if ( isset( $attributes[ 'customField' ] ) ) { |
| 99 |
$wrapper_class .= ' ' . 'custom-field-' . esc_attr( $attributes[ 'customField' ] ); |
| 100 |
} |
| 101 |
|
| 102 |
$wrapper_style = ''; |
| 103 |
//Classes |
| 104 |
if ( isset( $attributes[ 'textAlignment' ] ) ) { |
| 105 |
$wrapper_style .= 'text-align: '.esc_attr( $attributes[ 'textAlignment' ] ) . ';'; |
| 106 |
} |
| 107 |
if ( isset( $attributes[ 'bold' ] ) && $attributes[ 'bold' ] ) { |
| 108 |
$wrapper_style .= 'font-weight: bold;'; |
| 109 |
} |
| 110 |
if ( isset( $attributes[ 'italic' ] ) && $attributes[ 'italic' ] ) { |
| 111 |
$wrapper_style .= 'font-style: italic;'; |
| 112 |
} |
| 113 |
|
| 114 |
$is_back_end = getwid_is_block_editor(); |
| 115 |
|
| 116 |
//Link style & class |
| 117 |
if ( isset( $attributes[ 'customFontSize' ] ) ) { |
| 118 |
$font_size = is_numeric( $attributes['customFontSize'] ) ? $attributes['customFontSize'] . 'px' : $attributes['customFontSize']; |
| 119 |
$wrapper_style .= 'font-size: ' . esc_attr( $font_size ) . ';'; |
| 120 |
} |
| 121 |
|
| 122 |
if ( isset( $attributes[ 'fontSize' ] ) ) { |
| 123 |
$wrapper_class .= ' has-' . esc_attr( $attributes[ 'fontSize' ] ) . '-font-size'; |
| 124 |
} |
| 125 |
|
| 126 |
getwid_custom_color_style_and_class($wrapper_style, $wrapper_class, $attributes, 'color', $is_back_end); |
| 127 |
|
| 128 |
$result = ''; |
| 129 |
|
| 130 |
$extra_attr = array( |
| 131 |
'wrapper_class' => $wrapper_class, |
| 132 |
'wrapper_style' => $wrapper_style, |
| 133 |
); |
| 134 |
|
| 135 |
if ( isset( $attributes[ 'customField' ] ) ) { |
| 136 |
ob_start(); |
| 137 |
|
| 138 |
getwid_get_template_part( 'template-parts/post-custom-field', $attributes, false, $extra_attr ); |
| 139 |
|
| 140 |
$result = ob_get_clean(); |
| 141 |
} |
| 142 |
|
| 143 |
$this->block_frontend_assets(); |
| 144 |
|
| 145 |
return $result; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
new \Getwid\Blocks\PostCustomField(); |
| 150 |
|