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