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