-
Notifications
You must be signed in to change notification settings - Fork 1
/
shortcode-star-rating.php
372 lines (325 loc) · 11.7 KB
/
shortcode-star-rating.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
<?php
/*
Plugin Name: Shortcode Star Rating
Plugin URI: https://github.com/modshrink/shortcode-star-rating
Description: You can star rating in the article by using the shortcode. It uses the built-in icon fonts, WordPress 3.8 or higher is required.
Version: 0.3
Author: modshrink
Author URI: http://www.modshrink.com/
Text Domain: shortcode-star-rating
Domain Path: /languages
License: GPL2
Copyright 2016 modshrink (email : hello@modshrink.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
$ShortcodeStarRating = new ShortcodeStarRating();
class ShortcodeStarRating {
public $size;
public $icon_font_size;
public $int_font_size;
public $color;
public $checked;
/**
* Construnt
*/
public function __construct() {
// Text Domain
load_plugin_textdomain( 'shortcode-star-rating', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
// Plugin Activation
if ( function_exists( 'register_activation_hook' ) ) {
register_activation_hook( __FILE__, array( &$this, 'activationHook' ) );
}
// Plugin Uninstall
if ( function_exists( 'register_uninstall_hook' ) ) {
register_uninstall_hook( __FILE__, 'ShortcodeStarRating::uninstallHook' );
}
// Version Check
global $wp_version;
if( version_compare( $wp_version, '3.8', '<' ) ) {
add_action( 'admin_notices', array( &$this, 'ssr_notice' ) );
}
add_action( 'admin_init', array( &$this, 'pluginDeactivate' ) );
add_action( 'wp_enqueue_scripts', array( &$this, 'dashicons_css' ) );
add_action( 'wp_head', array( &$this, 'shortcode_star_rating_css' ) );
add_action( 'admin_print_footer_scripts', array( &$this, 'appthemes_add_quicktags' ) );
add_action( 'admin_menu', array( &$this, 'ssr_add_pages' ) );
add_shortcode( 'star', array( &$this, 'shortcode_star_rating_func' ) );
}
/**
* Display when it is activated in WordPress 3.8 or lower.
*/
public function ssr_notice() { ?>
<div class="updated">
<p><?php printf( __( '%s is not working.', 'shortcode-star-rating' ), '<strong>Shortcode Star Raging</strong>', 'shortcode-star-rating' ); ?></p>
<p><?php
_e( 'This plugin require the <strong>WordPress 3.8 or higher</strong>.', 'shortcode-star-rating' );
printf( __( ' Your WordPress version is %s.', 'shortcode-star-rating' ), get_bloginfo('version') );
echo ' <a href="?deactivatePluginKey=1">' . __( ' Deactivate Now', 'shortcode-star-rating' ) . '</a>';
?></p>
</div>
<?php
}
/**
* Plugin Deactivated from Update Message Box.
*/
public function pluginDeactivate() {
if( is_plugin_active( 'shortcode-star-rating/shortcode-star-rating.php' ) && isset( $_GET['deactivatePluginKey'] ) ) {
deactivate_plugins( 'shortcode-star-rating/shortcode-star-rating.php' );
remove_action( 'admin_notices', array( &$this, 'ssr_notice' ) );
add_action( 'admin_notices', array( &$this, 'pluginDeactivateMessage' ) );
}
}
/**
* Message after plugin deactivated.
*/
public function pluginDeactivateMessage() { ?>
<div id="message" class="updated"><p><?php _e( 'Plugin <strong>deactivated</strong>.' ) ?></p></div>
<?php }
/**
* Plugin Activation
*/
public function activationHook() {
// Installed Flag
if ( !get_option( 'shortcode_star_rating_installed' ) ) {
update_option( 'shortcode_star_rating_installed', 1 );
}
// Default Star Color
if ( !get_option( 'ssr_star_color' ) ) {
update_option( 'ssr_star_color', '#FCAE00' );
}
}
/**
* Plugin Uninstall
*/
static function uninstallHook() {
delete_option( 'shortcode_star_rating_installed' );
delete_option( 'ssr_star_color' );
delete_option( 'ssr_star_size' );
}
/**
* Load plugin textdomain.
*/
public function dashicons_css() {
wp_enqueue_style( 'dashicons', site_url( '/' )."/wp-includes/css/dashicons.min.css" );
}
/**
* Inline CSS for styling of star.
*/
public function shortcode_star_rating_css() {
if( get_option( 'ssr_star_size' ) == 'yes' ) {
$this->icon_font_size = '100%';
$this->int_font_size = '80%';
} else {
$this->icon_font_size = '20px';
$this->int_font_size = '13px';
}
?>
<style type="text/css" media="all">.shortcode-star-rating{padding:0 0.5em;}.dashicons{font-size:<?php echo $this->icon_font_size; ?>;width:auto;height:auto;line-height:normal;text-decoration:inherit;vertical-align:middle;}.shortcode-star-rating [class^="dashicons dashicons-star-"]:before{color:<?php echo get_option( 'ssr_star_color' ) ?>;}.ssr-int{margin-left:0.2em;font-size:<?php echo $this->int_font_size; ?>;vertical-align:middle;color:#333;}/*.shortcode-star-rating:before,.shortcode-star-rating:after{display: block;height:0;visibility:hidden;content:"\0020";}.shortcode-star-rating:after{clear:both;}*/</style>
<?php }
/**
* Replace shortcode to rating star.
*/
public function shortcode_star_rating_func( $atts ) {
global $post;
extract( shortcode_atts( array(
'rating' => '0',
'type' => 'rating',
'number' => '0',
'max' => '5',
'numeric' => 'no',
'schema' => 'false',
), $atts ) );
if( $max == NULL ) {
$max = 5;
}
/* Display tyle: rating */
if( $type == "rating" ) {
// 小数点以下の最後が0の場合は削除
if( is_float( $rating ) ) {
$rating = preg_replace( '/\.?0+$/', '', (int)$rating );
}
$empty_rating = $max - $rating;
if( is_float( $empty_rating ) ) {
$filled = floor( $rating );
$half = 1;
$empty = floor($empty_rating);
} else {
$filled = $rating;
$half = 0;
$empty = $empty_rating;
}
if( $max < $filled ) {
$filled = $max;
}
}
/* Display tyle: percent */
if( $type == "percent" ) {
$fill_percentage = $max * ( $rating * 0.01 );
$empty_percentage = $max - $fill_percentage;
if( preg_match( '/^\d+\.\d+$/', $fill_percentage ) ) {
$filled = floor( $fill_percentage );
$half = 1;
$empty = floor( $empty_percentage );
} else {
$filled = $fill_percentage;
$half = 0;
$empty = $empty_percentage;
}
}
// ratingがmaxより高くmaxがマイナス値を取る場合はsrt_repeatでエラーが出るため、強制的に0とする。
if( !ctype_digit( strval( $empty ) ) ) {
$empty = 0;
}
$ssr_html .= "<span class=\"shortcode-star-rating\">";
$ssr_html .= str_repeat( '<span class="dashicons dashicons-star-filled"></span>', (int)$filled );
$ssr_html .= str_repeat( '<span class="dashicons dashicons-star-half"></span>', $half );
$ssr_html .= str_repeat( '<span class="dashicons dashicons-star-empty"></span>', $empty );
if( $numeric == "yes" ) {
if( $type == "percent" ) {
$ssr_html .= "<span class=\"ssr-int\">(" . $rating . "%)</span>";
} else {
$ssr_html .= "<span class=\"ssr-int\">(" . $rating . "/" . $max . ")</span>";
}
}
$ssr_html .= "</span>";
// 「リッチスニペット表示オプション有効」「記事ページ」「ショートコードの重複無し」の場合だけJSON LDを表示
if( $schema == 'true' && is_single() && $this->duplication_check( $post->post_content ) ) {
$item = $post->post_title;
$author = get_the_author( $post->post_author );
$date = $post->post_date;
$json_ld = $this->get_json_ld( $item, $author, $rating, $max, $date );
add_action( 'wp_footer', function() { echo $json_ld; } );
}
return $ssr_html;
}
/**
* リッチスニペット用JSON-LDの組み立て
* @see Google仕様解説 https://developers.google.com/search/docs/data-types/reviews?visit_id=1-636245363966023220-2672943764&hl=ja&rd=1#review-snippets
* @see テストツール https://search.google.com/structured-data/testing-tool
*/
public function get_json_ld( $item, $author, $rating, $max, $date ) {
global $post;
$output = <<<_EOM_
<!-- JSON-LD for Review snippets by Shortcode Star Rating -->
<script type="application/ld+json">
{
"@context": "http://schema.org/",
"@type": "Review",
"itemReviewed": {
"@type": "Thing",
"name": "{$item}"
},
"author": {
"@type": "Person",
"name": "{$author}"
},
"reviewRating": {
"@type": "Rating",
"ratingValue": "{$rating}",
"bestRating": "{$max}"
},
"datePublished": "$date"
}
</script>
_EOM_;
//日付フォーマット ISO8601 2017-03-08T11:33:55+09:00
echo $output . "\n";
}
/**
* ショートコードが複数使われているかのチェック
*/
public function duplication_check( $string ) {
$count = substr_count( $string, '[star');
if ( $count === 1 ) {
return true;
} else {
return false;
}
}
/**
* Add quicktag for shortcode.
*/
public function appthemes_add_quicktags() {
if ( wp_script_is( 'quicktags' ) ){
?>
<script type="text/javascript">
QTags.addButton( 'shortcode_star_rating', 'Star Rating', '[star rating=\"\"]', '', 'r', 'Sortcode Rating' );
</script>
<?php
}
}
/**
* Add admin menu.
*/
public function ssr_add_pages() {
add_options_page( 'Shortcode Star Rating', 'Shortcode Star Rating', 'level_8', __FILE__, array( &$this, 'ssr_plugin_options' ) );
}
/**
* Add admin menu.
*/
public function ssr_plugin_options() {
if( isset( $_POST['action'] ) && $_POST['action'] == 'update' ) {
$this->color = $_POST[ 'ssr_star_color' ];
if( isset( $_POST['ssr_star_size'] ) && $_POST['ssr_star_size'] == 'yes' ) {
$this->size = $_POST[ 'ssr_star_size' ];
} else {
$this->size = 'no';
}
if( preg_match( '/\A[0-9A-Fa-f]{6}\z/', $this->color ) || preg_match( '/\A#{1}[0-9A-Fa-f]{6}\z/', $this->color ) ) {
if( preg_match( '/\A[0-9A-Fa-f]{6}\z/', $this->color ) ) {
$this->color = '#' . $this->color;
}
} else {
$this->color = get_option( 'ssr_star_color' );
echo '<div class="updated"><p>' . __( 'Invalid HEX color code.', 'shortcode-star-rating' ) . '</p></div>';
}
update_option( 'ssr_star_color', $this->color );
update_option( 'ssr_star_size', $this->size );
add_action( 'admin_notices', array(&$this, 'colorErrorMes' ) );
}
if( get_option( 'ssr_star_size' ) == 'yes' ) {
$this->checked = ' checked="checked"';
}
// バージョン取得
$data = get_file_data( __FILE__, array( 'version' => 'Version' ) );
$version = $data['version'];
?>
<div class="wrap">
<h2>Shortcode Star Rating <?php echo 'Version ' . $version; ?></h2>
<form method="post" action="<?php echo str_replace( '%7E', '~', $_SERVER['REQUEST_URI'] ); ?>">
<?php wp_nonce_field('update-options'); ?>
<table class="form-table">
<tr valign="top">
<th scope="row"><?php _e( 'Star Color', 'shortcode-star-rating' ); ?></th>
<td>
<input type="text" name="ssr_star_color" value="<?php echo get_option( 'ssr_star_color' ); ?>" />
<p class="description"><?php printf( __( 'Please enter in the Hex color. (Default: %s)', 'shortcode-star-rating' ), '#FCAE00' ); ?></p>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php _e( 'Adjust Star Size', 'shortcode-star-rating' ); ?></th>
<td>
<input type="checkbox" id ="ssr-star-size" name="ssr_star_size" value="yes"<?php echo $this->checked; ?> /> <label for="ssr-star-size"><?php _e( 'Fit in a perent box', 'shortcode-star-rating' ); ?></label>
<p class="description"><?php _e( 'If there is no check, 20px font size is set.', 'shortcode-star-rating' ); ?></p>
</td>
</tr>
</table>
<input type="hidden" name="action" value="update" />
<input type="hidden" name="page_options" value="new_option_name,some_other_option,option_etc" />
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e( 'Save Changes' ); ?>" />
</p>
</form>
</div>
<?php
}
}