-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbook-review-block.php
285 lines (254 loc) · 8.13 KB
/
book-review-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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?php
/**
* Plugin Name: Book Review Block
* Plugin URI: https://github.com/donnapep/book-review-block
* Description: A Gutenberg block to add book details and a star rating to a book review post.
* Author: Donna Peplinskie
* Author URI: https://donnapeplinskie.com
* Version: 2.2.1
* Text Domain: book-review-block
* License: GPL2+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Requires at least: 6.0
* Tested up to: 6.6
* Requires PHP: 7.4
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class Book_Review_Block {
/**
* Instance of this class.
*
* @since 1.0.0
* @access private
* @var object $instance A single instance of this class.
*/
private static $instance;
/**
* The slug of this plugin.
*
* @since 1.0.0
* @access private
* @var string $slug The plugin slug.
*/
private $slug;
/**
* The version of this plugin.
*
* @since 1.0.0
* @access private
* @var string $version The current version of this plugin.
*/
private $version;
/**
* Controller to communicate with the REST API.
*
* @since 2.2.1
* @access private
* @var string $controller Property to access the REST API.
*/
private $controller;
/**
* Registers the plugin.
*
* @since 1.0.0
* @access public
* @return object A single instance of this class.
*/
public static function register() {
if ( null === self::$instance ) {
self::$instance = new Book_Review_Block();
}
}
/**
* Defines the core functionality of the plugin.
*
* @since 1.0.0
* @access private
*/
private function __construct() {
$this->version = '2.2.1';
$this->slug = 'book-review-block';
require_once plugin_dir_path( __FILE__ ) . 'includes/book-review-block-settings-controller.php';
$this->controller = new Book_Review_Block_REST_Controller();
$this->controller->init();
add_action( 'init', array( $this, 'init_block' ) );
}
/**
* Initializes the block.
*
* @since 1.1.2
* @access public
*/
public function init_block() {
// Automatically load dependencies and version.
$asset_file = include( plugin_dir_path( __FILE__ ) . 'build/index.asset.php');
wp_register_script(
$this->slug,
plugins_url( 'build/index.js', __FILE__ ),
$asset_file['dependencies'],
$asset_file['version']
);
wp_set_script_translations( $this->slug, 'book-review-block' );
wp_register_style(
$this->slug . '-editor',
plugins_url( 'build/index.css', __FILE__ ),
array( 'wp-edit-blocks' ),
$asset_file['version']
);
wp_register_style(
$this->slug,
plugins_url( 'build/style-index.css', __FILE__ ),
array(),
$asset_file['version']
);
// Register block.
register_block_type( 'book-review-block/book-review', array(
'editor_script' => $this->slug,
'editor_style' => $this->slug . '-editor',
'style' => $this->slug,
'render_callback' => array( $this, 'render_book_review' ),
) );
$this->register_meta_field( 'book_review_cover_url' );
$this->register_meta_field( 'book_review_title' );
$this->register_meta_field( 'book_review_series' );
$this->register_meta_field( 'book_review_author' );
$this->register_meta_field( 'book_review_genre' );
$this->register_meta_field( 'book_review_isbn' );
$this->register_meta_field( 'book_review_publisher' );
$this->register_meta_field( 'book_review_release_date' );
$this->register_meta_field( 'book_review_format' );
$this->register_meta_field( 'book_review_pages' );
$this->register_meta_field( 'book_review_source' );
$this->register_meta_field( 'book_review_rating' );
$this->register_meta_field( 'book_review_summary' );
}
/**
* Renders the block where the markup is stored in post content (i.e. deprecated v1 and current version).
*
* @since 1.1.2
* @param array $attributes Block attributes.
* @param string $content Block inner content.
* @return string Markup.
* @access public
*/
public function render_book_review( $attributes, $content ) {
if ( ! empty( $content ) ) {
$img_element = '<img ';
$img_position = strpos( $content, $img_element );
// Add schema.org markup to first image element.
if ( $img_position ) {
$content = substr_replace(
$content,
'itemprop="image" ',
$img_position + strlen( $img_element ),
0
);
}
$review_rating_element = '<div itemscope itemtype="https://schema.org/Rating" itemprop="reviewRating"';
$review_rating_element_position = strpos( $content, $review_rating_element );
// Add schema.org markup for the author of the review.
if ( $review_rating_element_position ) {
$content = substr_replace(
$content,
'<span itemscope itemtype="https://schema.org/Person" itemprop="author">' .
'<meta itemprop="name" content="' . esc_attr( get_the_author() ) . '"/>' .
'</span>',
$review_rating_element_position,
0
);
}
return $content;
}
if ( in_the_loop() ) {
return $this->render_book_review_deprecated_v2( $attributes, $content );
}
}
/**
* Renders the block where the markup is stored in post meta (deprecrated v2).
*
* @since 1.1.2
* @param array $attributes Block attributes.
* @param string $content Block inner content.
* @return string Markup.
* @access public
*/
private function render_book_review_deprecated_v2( $attributes, $content ) {
$title = $this->get_post_meta( 'book_review_title' );
$cover_url = $this->get_post_meta( 'book_review_cover_url' );
$series = $this->get_post_meta( 'book_review_series' );
$author = $this->get_post_meta( 'book_review_author' );
$genre = $this->get_post_meta( 'book_review_genre' );
$publisher = $this->get_post_meta( 'book_review_publisher' );
$release_date = $this->get_post_meta( 'book_review_release_date' );
$format = $this->get_post_meta( 'book_review_format' );
$pages = $this->get_post_meta( 'book_review_pages' );
$source = $this->get_post_meta( 'book_review_source' );
$summary = $this->get_post_meta( 'book_review_summary' );
$rating_html = array_map( array( $this, 'get_rating_html' ), array( 1, 2, 3, 4, 5 ) );
if ( isset( $attributes ) && isset( $attributes['backgroundColor'] ) ) {
$background_color = $attributes['backgroundColor'];
} else {
$background_color = '';
}
ob_start();
include( 'src/blocks/deprecated/book-review.php' );
return ob_get_clean();
}
/**
* Registers a meta field.
*
* @since 1.1.2
* @param string $meta_key Meta field key.
* @param bool $is_integer true if the value of the meta field is an integer, false otherwise.
* @access private
*/
private function register_meta_field( $meta_key ) {
register_post_meta( 'post', $meta_key, array(
'show_in_rest' => true,
'single' => true,
) );
}
/**
* Retrieves post meta field.
*
* @since 1.1.2
* @param string $meta_key Meta field key.
* @access private
*/
private function get_post_meta( $meta_key ) {
return apply_filters(
$meta_key,
get_post_meta( get_the_ID(), $meta_key, true ),
get_the_ID()
);
}
/**
* Gets the HTML for the rating.
*
* @since 1.1.2
* @param integer $rating Rating to compare to.
* @access private
*/
private function get_rating_html( $rating ) {
$current_rating = floatval( $this->get_post_meta( 'book_review_rating' ) );
$classname_whole = ( $current_rating >= ( $rating - 0.5 ) ) ? '' : 'is-rating-unfilled';
$classname_half = ( $current_rating >= $rating ) ? '' : 'is-rating-unfilled';
$html = "<span class='book-review-block__rating-button' role='button'>" .
"<span>" .
"<svg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 24 24'>" .
"<path class='{$classname_whole}' fill='#eba845' stroke='#eba845' d='M12,17.3l6.2,3.7l-1.6-7L22,9.2l-7.2-0.6L12,2L9.2,8.6L2,9.2L7.5,14l-1.6,7L12,17.3z' />" .
"</svg>" .
"</span>" .
"<span>" .
"<svg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 24 24'>" .
"<path class='{$classname_half}' fill='#eba845' stroke='#eba845' d='M12,17.3l6.2,3.7l-1.6-7L22,9.2l-7.2-0.6L12,2L9.2,8.6L2,9.2L7.5,14l-1.6,7L12,17.3z' />" .
"</svg>" .
"</span>" .
"</span>";
return $html;
}
}
Book_Review_Block::register();