| 1 |
<?php |
| 2 |
|
| 3 |
namespace Getwid\Blocks; |
| 4 |
|
| 5 |
class PostLink extends \Getwid\Blocks\AbstractBlock { |
| 6 |
|
| 7 |
protected static $blockName = 'getwid/template-post-link'; |
| 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 |
'buttonText' => array( |
| 28 |
'type' => 'string', |
| 29 |
'default' => __( 'Read More', 'getwid' ) |
| 30 |
), |
| 31 |
'textAlignment' => array( |
| 32 |
'type' => 'string' |
| 33 |
), |
| 34 |
|
| 35 |
'className' => array( |
| 36 |
'type' => 'string' |
| 37 |
) |
| 38 |
), |
| 39 |
'render_callback' => [ $this, 'render_callback' ] |
| 40 |
) |
| 41 |
); |
| 42 |
} |
| 43 |
|
| 44 |
public function block_frontend_assets() { |
| 45 |
|
| 46 |
if ( is_admin() ) { |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
if ( FALSE == getwid()->assetsOptimization()->load_assets_on_demand() ) { |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
add_filter( 'getwid/optimize/assets', |
| 55 |
function ( $assets ) { |
| 56 |
$assets[] = self::$assetsHandle; |
| 57 |
|
| 58 |
return $assets; |
| 59 |
} |
| 60 |
); |
| 61 |
|
| 62 |
$rtl = is_rtl() ? '.rtl' : ''; |
| 63 |
|
| 64 |
wp_enqueue_style( |
| 65 |
self::$assetsHandle, |
| 66 |
getwid_get_plugin_url( 'assets/blocks/template-parts/style' . $rtl . '.css' ), |
| 67 |
[], |
| 68 |
getwid()->settings()->getVersion() |
| 69 |
); |
| 70 |
} |
| 71 |
|
| 72 |
public function render_callback( $attributes, $content ) { |
| 73 |
|
| 74 |
//Not BackEnd render if we view from template page |
| 75 |
if ( ( get_post_type() == getwid()->postTemplatePart()->postType ) || ( get_post_type() == 'revision' ) ) { |
| 76 |
return $content; |
| 77 |
} |
| 78 |
|
| 79 |
$block_name = 'wp-block-getwid-template-post-link'; |
| 80 |
$wrapper_class = $block_name; |
| 81 |
|
| 82 |
$wrapper_style = ''; |
| 83 |
//Classes |
| 84 |
if ( isset( $attributes[ 'className' ] ) ) { |
| 85 |
$wrapper_class .= ' '.esc_attr( $attributes[ 'className' ] ); |
| 86 |
} |
| 87 |
|
| 88 |
if ( isset( $attributes[ 'textAlignment' ] ) ) { |
| 89 |
$wrapper_style .= 'text-align: ' . esc_attr( $attributes[ 'textAlignment' ] ) . ';'; |
| 90 |
} |
| 91 |
|
| 92 |
$is_back_end = getwid_is_block_editor(); |
| 93 |
|
| 94 |
//Link style & class |
| 95 |
getwid_custom_color_style_and_class( $wrapper_style, $wrapper_class, $attributes, 'color', $is_back_end ); |
| 96 |
|
| 97 |
$extra_attr = array( |
| 98 |
'wrapper_class' => $wrapper_class, |
| 99 |
'wrapper_style' => $wrapper_style |
| 100 |
); |
| 101 |
|
| 102 |
ob_start(); |
| 103 |
|
| 104 |
getwid_get_template_part( 'template-parts/post-link', $attributes, false, $extra_attr ); |
| 105 |
|
| 106 |
$result = ob_get_clean(); |
| 107 |
|
| 108 |
$this->block_frontend_assets(); |
| 109 |
|
| 110 |
return $result; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
new \Getwid\Blocks\PostLink(); |
| 115 |
|