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