-
Notifications
You must be signed in to change notification settings - Fork 0
/
astro-gutenberg-block.php
198 lines (181 loc) · 5.81 KB
/
astro-gutenberg-block.php
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
/**
* Plugin Name: Astro Gutenberg Block
* Description: Example static block scaffolded with Create Block tool.
* Requires at least: 5.9
* Requires PHP: 7.0
* Version: 0.1.0
* Author: The WordPress Contributors
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: astro-gutenberg-block
*
* @package create-block
*/
define( 'GUTENBERG_LEARNING_PATH', untrailingslashit( plugin_dir_path( __FILE__ ) ) );
define( 'GUTENBERG_LEARNING_URL', untrailingslashit( plugin_dir_url( __FILE__ ) ) );
/**
* Registers the block using the metadata loaded from the `block.json` file.
* Behind the scenes, it registers also all assets so they can be enqueued
* through the block editor in the corresponding context.
*
* @see https://developer.wordpress.org/reference/functions/register_block_type/
*/
function create_block_astro_gutenberg_block_block_init() {
register_block_type( __DIR__ . '/build/astro-gutenberg-block' );
register_block_type(
'create-block/single-post-block',
[
'render_callback' => 'render_single_post_block',
'attributes' => [
'postId' => [
'type' => 'integer',
'default' => 0,
],
'ImageOnLeft' => [
'type' => 'boolean',
'default' => false,
],
'DisableDate' => [
'type' => 'boolean',
'default' => false,
],
'DisableExcerpt' => [
'type' => 'boolean',
'default' => false,
],
],
]
);
register_block_type( __DIR__ . '/build/gutenberg-innerblocks' );
}
add_action( 'init', 'create_block_astro_gutenberg_block_block_init' );
/**
* Function to render the server side for editor as well as saveed content markup.
*
* @param array $attributes Block Attributes.
*
* @return void
*/
function render_single_post_block( $attributes ) {
$attributes = wp_parse_args( $attributes, [] );
$border_side = 'div-border-left';
if ( array_key_exists( 'ImageOnLeft', $attributes ) && 1 === (int) $attributes['ImageOnLeft'] ) {
$image_pos = 'left-image';
$border_side = 'div-border-right';
}
if ( array_key_exists( 'DisableExcerpt', $attributes ) && 1 === (int) $attributes['DisableExcerpt'] ) {
$excerpt = 'do-not-show';
}
if ( array_key_exists( 'DisableDate', $attributes ) && 1 === (int) $attributes['DisableDate'] ) {
$date = 'do-not-show';
}
if ( ! array_key_exists( 'postId', $attributes ) || 0 === (int) $attributes['postId'] ) {
return '';
}
$post = get_post( (int) $attributes['postId'] );
$src = '';
$thumbnail_id = get_post_thumbnail_id( $post );
if ( false !== $thumbnail_id && 0 !== $thumbnail_id ) {
$src = wp_get_attachment_image_src( $thumbnail_id, 'thumbnail' );
}
ob_start();
?>
<div class="single-block-container <?php echo sanitize_html_class( $image_pos ); ?>">
<div class="column single-block-data">
<div class="single-post-title">
<h2 class="post-title"><?php echo esc_html( $post->post_title ); ?></h2>
</div>
<div class="single-post-excerpt <?php echo sanitize_html_class( $excerpt ); ?>">
<p><b><?php esc_html_e( 'Excerpt : ', 'astro-gutenberg-block' ); ?></b><?php echo esc_html( $post->post_excerpt ); ?></p>
</div>
<div class="single-post-date <?php echo sanitize_html_class( $date ); ?>">
<p><b><?php esc_html_e( 'Date - ', 'astro-gutenberg-block' ); ?></b><?php echo esc_html( human_time_diff( strtotime( $post->post_date ) ) ); ?></p>
</div>
</div>
<div class="column single-block-image <?php echo sanitize_html_class( $border_side ); ?> ">
<?php
if ( '' !== $src ) {
?>
<img class="single-block-image-img" src="<?php echo esc_url( $src[0] ); ?>" height = "150" width="300" />
<?php
} else {
?>
<div><?php esc_html_e( 'No Featured Image', 'astro-gutenberg-block' ); ?></div>
<?php
}
?>
</div>
</div>
<?php
$markup = ob_get_clean();
return $markup;
}
/**
* Function to enqueue block and editor style.
*
* @return void
*/
function enqueue_single_post_block() {
wp_enqueue_script(
'single-post-block',
GUTENBERG_LEARNING_URL . '/build/single-post-block/index.js',
array(),
filemtime( GUTENBERG_LEARNING_PATH . '/build/single-post-block/index.js' ),
true,
);
wp_enqueue_style(
'single-post-block-editor-style',
GUTENBERG_LEARNING_URL . '/build/single-post-block/index.css',
array(),
filemtime( GUTENBERG_LEARNING_PATH . '/build/single-post-block/index.css' ),
);
}
add_action( 'enqueue_block_editor_assets', 'enqueue_single_post_block' );
/**
* Function to enqueue style for the frontend.
*
* @return void
*/
function add_style_for_gutenberg_block() {
wp_enqueue_style(
'single-post-block-saved-content-style',
GUTENBERG_LEARNING_URL . '/build/single-post-block/style-index.css',
array(),
filemtime( GUTENBERG_LEARNING_PATH . '/build/single-post-block/style-index.css' ),
);
}
add_action( 'wp_enqueue_scripts', 'add_style_for_gutenberg_block' );
/**
* Registering the custom metas.
*
* @return void
*/
function register_custom_meta() {
register_post_meta( 'post', 'astro_sign', array(
'show_in_rest' => true,
'type' => 'string',
'single' => true,
'auth_callback' => function() {
return current_user_can( 'edit_posts' );
}
) );
register_post_meta( 'post', 'astro_sign_image', array(
'show_in_rest' => true,
'type' => 'number',
'single' => true,
'auth_callback' => function() {
return current_user_can( 'edit_posts' );
}
) );
register_post_meta( 'post', 'astro_category_id', array(
'show_in_rest' => true,
'type' => 'number',
'single' => true,
'default' => 0,
'auth_callback' => function() {
return current_user_can( 'edit_posts' );
}
) );
}
add_action( 'init', 'register_custom_meta' , 10 );