PluginProbe
Easy Quotes / trunk
Easy Quotes vtrunk
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.1.0 1.1.1 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 All 28 releases
easy-quotes / includes / post-type.php

post-type.php in Easy Quotes trunk, at includes/post-type.php

511 lines 19.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // Exit if accessed directly.
4 if ( ! defined( 'ABSPATH' ) ) {
5 exit;
6 }
7
8 class Easy_Quotes_Post_Type {
9
10 function __construct() {
11 add_action( 'init', array($this, 'post_type') );
12 add_action( 'init', array($this, 'taxonomy'), 30 );
13 add_action( 'save_post_quote', array($this, 'save_meta') );
14 add_action( 'admin_enqueue_scripts', array($this, 'enqueue_scripts') );
15 add_filter( 'manage_quote_posts_columns', array($this,'quote_posts_columns') );
16 add_action( 'manage_quote_posts_custom_column', array($this, 'quote_posts_custom_column'), 10, 2 );
17 add_filter( 'manage_edit-quote_sortable_columns', array($this, 'quote_sortable_columns') );
18 add_action( 'pre_get_posts', array($this, 'pre_get_posts') );
19 add_action( 'quick_edit_custom_box', array($this, 'quick_edit_custom_box'), 10, 2 );
20 add_action( 'bulk_edit_custom_box', array($this, 'quick_edit_custom_box'), 10, 2 );
21 add_action( 'restrict_manage_posts', array($this, 'restrict_manage_posts') );
22 }
23
24 /**
25 * Registering Post_Type "quote"
26 *
27 * @return void
28 */
29 function post_type()
30 {
31 $labels = array(
32 'name' => _x('Quotes', 'Post type general name', 'easy-quotes'),
33 'singular_name' => _x('Quote', 'Post type singular name', 'easy-quotes'),
34 'menu_name' => _x('Quotes', 'Admin Menu text', 'easy-quotes'),
35 'name_admin_bar' => _x('Quote', 'Add New on Toolbar', 'easy-quotes'),
36 'add_new_item' => __('Add New Quote', 'easy-quotes'),
37 'add_new' => __('Add New', 'easy-quotes'),
38 'edit_item' => __('Edit Quote', 'easy-quotes'),
39 'new_item' => __('New Quote', 'easy-quotes'),
40 'view_item' => __('View Quote', 'easy-quotes'),
41 'view_items' => __('View Quotes', 'easy-quotes'),
42 'search_items' => __('Search Quotes', 'easy-quotes'),
43 'not_found' => __('No quotes found.', 'easy-quotes'),
44 'not_found_in_trash' => __('No quotes found in Trash.', 'easy-quotes'),
45 'all_items' => __('All Quotes', 'easy-quotes'),
46 'archives' => __('Quote Archives', 'easy-quotes'),
47 'attributes' => __('Quote Attributes', 'easy-quotes'),
48 'insert_into_item' => __('Insert into quote', 'easy-quotes'),
49 'uploaded_to_this_item' => __('Uploaded to this quote', 'easy-quotes'),
50 'filter_items_list' => __('Filter quotes list', 'easy-quotes'),
51 'items_list_navigation' => __('Quotes list navigation', 'easy-quotes'),
52 'items_list' => __('Quotes list', 'easy-quotes'),
53 'item_published' => __('Quote published.', 'easy-quotes'),
54 'item_published_privately' => __('Quote published privately.', 'easy-quotes'),
55 'item_reverted_to_draft' => __('Quote reverted to draft.', 'easy-quotes'),
56 'item_scheduled' => __('Quote scheduled.', 'easy-quotes'),
57 'item_updated' => __('Quote updated.', 'easy-quotes'),
58 'item_link' => __('Quote Link', 'easy-quotes'),
59 'item_link_description' => __('A link to a quote.', 'easy-quotes')
60 );
61 $args = array(
62 'labels' => $labels,
63 'description' => __('Add Quotes to your wordpress site', 'easy-quotes'),
64 'public' => true,
65 'show_ui' => true,
66 'menu_icon' => 'dashicons-editor-quote',
67 'show_in_rest' => true,
68 'supports' => array('title', 'editor'),
69 'register_meta_box_cb' => array($this, 'register_meta_box'),
70 'has_archive' => 'quotes'
71 );
72 register_post_type('quote', $args);
73 }
74
75 /**
76 * Register Taxonomy "Category"
77 *
78 * @return void
79 */
80 function taxonomy() {
81 $labels = array(
82 'name' => _x( 'Categories', 'taxonomy general name', 'easy-quotes' ),
83 'singular_name' => _x( 'Category', 'taxonomy singular name', 'easy-quotes' ),
84 'search_items' => __( 'Search Category', 'easy-quotes' ),
85 'all_items' => __( 'All Categories', 'easy-quotes' ),
86 'parent_item' => __( 'Parent Category', 'easy-quotes' ),
87 'parent_item_colon' => __( 'Parent Category:', 'easy-quotes' ),
88 'edit_item' => __( 'Edit Category', 'easy-quotes' ),
89 'update_item' => __( 'Update Category', 'easy-quotes' ),
90 'add_new_item' => __( 'Add New Category', 'easy-quotes' ),
91 'new_item_name' => __( 'New Category Name', 'easy-quotes' ),
92 'menu_name' => __( 'Categories', 'easy-quotes' )
93 );
94 $args = array(
95 'hierarchical' => true,
96 'labels' => $labels,
97 'show_ui' => true,
98 'show_admin_column' => true,
99 'query_var' => true,
100 'rewrite' => array( 'slug' => 'quote-category' ),
101 'show_in_rest' => true,
102 'rest_base' => 'quote-category',
103 'rest_controller_class' => 'WP_REST_Terms_Controller',
104 );
105 register_taxonomy( 'quote-category', array( 'quote' ), $args );
106 }
107
108 /**
109 * Is the current Page the Quote Admin List View
110 *
111 * @param string $hook_suffix
112 * @return boolean
113 */
114 function isQuoteListView( $hook_suffix ) {
115 if ($hook_suffix != 'edit.php')
116 return false;
117 if (get_post_type() === 'quote')
118 return true;
119 return false;
120 }
121
122 /**
123 * Is the current Page the Quote Edit (Editor)
124 *
125 * @param string $hook_suffix
126 * @return boolean
127 */
128 function isQuoteEdit( $hook_suffix ) {
129 if (!($hook_suffix === 'post.php' || $hook_suffix === 'post-new.php'))
130 return false;
131 if (get_post_type() === 'quote')
132 return true;
133 return false;
134 }
135
136 /**
137 * Enqueue_Admin_Scripts
138 *
139 * @return void
140 */
141 function enqueue_scripts( $hook_suffix ) {
142
143 $listView = $this->isQuoteListView( $hook_suffix );
144 $quoteEdit = $this->isQuoteEdit( $hook_suffix );
145
146 if ($listView || $quoteEdit) {
147 wp_enqueue_style(
148 'quote-css',
149 EASY_QUOTES_PLUGIN_URL.'admin/css/style.css',
150 array(),
151 EASY_QUOTES_VERSION
152 );
153
154 wp_enqueue_script(
155 'custom-quickedit-box',
156 EASY_QUOTES_PLUGIN_URL.'admin/js/quickedit.js',
157 array( 'inline-edit-post' ),
158 EASY_QUOTES_VERSION,
159 true
160 );
161
162 if ($quoteEdit) {
163 wp_enqueue_script(
164 'quote-rating',
165 EASY_QUOTES_PLUGIN_URL.'admin/js/quoterating.js',
166 array(),
167 EASY_QUOTES_VERSION,
168 true
169 );
170 }
171 }
172 }
173
174 /**
175 * Register_meta_box
176 *
177 * @return void
178 */
179 function register_meta_box() {
180 add_meta_box(
181 'quote-meta',
182 __('Meta', 'easy-quotes'),
183 array($this, 'get_meta_html'),
184 'quote',
185 'side'
186 );
187 }
188
189 /**
190 * Echoes html for the meta fields quote_author and quote_date and quote_rating
191 *
192 * @param [WP_Post] $post
193 */
194 function get_meta_html( $post ) {
195 $quote_author = get_post_meta( $post->ID, 'quote_author', true );
196 $quote_date = get_post_meta( $post->ID, 'quote_date', true );
197 $quote_rating = get_post_meta( $post->ID, 'quote_rating', true );
198 if ( empty( $quote_rating ) ) {
199 $quote_rating = "0";
200 }
201
202 echo '<div class="form-row">';
203 echo '<label for="quote_author" aria-label="' . esc_attr__( 'Quote Author', 'easy-quotes' ) . '">' . esc_html__( 'Author:', 'easy-quotes' ) . '</label>';
204 echo '<input type="text" id="quote_author" name="quote_author" value="' . esc_attr( $quote_author ) . '" />';
205 echo '</div>';
206
207 echo '<div class="form-row">';
208 echo '<label for="quote_date" aria-label="' . esc_attr__( 'Quote Date', 'easy-quotes' ) . '">' . esc_html__( 'Date:', 'easy-quotes' ) . '</label>';
209 echo '<input type="text" id="quote_date" name="quote_date" value="' . esc_attr( $quote_date ) . '" />';
210 echo '</div>';
211
212 echo '<div class="form-row">';
213 echo '<label for="quote_rating" aria-label="' . esc_attr__( 'Quote Stars', 'easy-quotes' ) . '">' . esc_html__( 'Rating:', 'easy-quotes' ) . '</label>';
214 echo '<input type="number" id="quote_rating" name="quote_rating" step="0.1" min="0" max="5" value="' . esc_attr( $quote_rating ) . '" />';
215 echo '</div>';
216
217 echo '<div class="form-row">';
218 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- get_stars() returns safe, sanitized SVG
219 echo Easy_Quotes_Stars::get_stars( (float) $quote_rating );
220 echo '</div>';
221 }
222
223 /**
224 * Save Meta Data
225 *
226 * @param integer $post_id
227 * @return void
228 */
229 function save_meta( $post_id ) {
230 // Check nonce
231 if (!$this->is_nonce_valid($post_id))
232 return;
233
234 // Autosave?
235 if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
236 return;
237
238 // Check the user's permissions.
239 if ( isset( $_POST['post_type'] ) && 'quote' == sanitize_text_field( wp_unslash( $_POST['post_type'] ) ) ) {
240 if ( ! current_user_can( 'edit_page', $post_id ) ) {
241 return;
242 }
243 }
244 else {
245 if ( ! current_user_can( 'edit_post', $post_id ) ) {
246 return;
247 }
248 }
249
250 // All checks done now safe the data
251 $this->update_quote_meta($post_id, 'quote_author');
252 $this->update_quote_meta($post_id, 'quote_date');
253 $this->update_quote_meta($post_id, 'quote_rating', 'number');
254 }
255
256 /**
257 * Update and sanitize custom meta string data
258 *
259 * @param integer $post_id
260 * @param string $meta_key
261 * @return boolean true if success
262 */
263 function update_quote_meta($post_id, $meta_key = '', $type = 'text')
264 {
265 // Quick Edit and "normal" inside Quote Edit
266 if (isset($_POST[$meta_key])) {
267 if ($type == 'number')
268 $value = filter_input(INPUT_POST, $meta_key, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);
269 else
270 $value = sanitize_text_field( wp_unslash( $_POST[$meta_key] ) );
271 update_post_meta( $post_id, $meta_key, $value);
272 return true;
273 }
274 // BULK edit
275 if (isset($_GET[$meta_key])) {
276 if ($type == 'number')
277 $value = filter_input(INPUT_GET, $meta_key, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);
278 else
279 $value = sanitize_text_field( wp_unslash( $_GET[$meta_key] ) );
280 if (!empty($value)) {
281 update_post_meta( $post_id, $meta_key, $value);
282 return true;
283 }
284 }
285 return false;
286 }
287
288 /**
289 * Check for the right Wordpress nonce fields and verifies them
290 *
291 * @param integer $post_id
292 * @return boolean
293 */
294 function is_nonce_valid( $post_id ) {
295 // In Quote Save
296 if ( isset($_POST['_wpnonce']) ) {
297 $nonce = sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) );
298 if ( wp_verify_nonce( $nonce, 'update-post_' . $post_id) ) {
299 return true;
300 }
301 }
302
303 // In Quick Edit Save
304 if ( isset($_POST['_inline_edit']) ) {
305 $nonce = sanitize_text_field( wp_unslash( $_POST['_inline_edit'] ) );
306 if ( wp_verify_nonce( $nonce, 'inlineeditnonce' ) ) {
307 return true;
308 }
309 }
310
311 // In Bulk Edit Save
312 if ( isset($_REQUEST['_wpnonce'])) {
313 $suffix = _get_list_table( 'WP_Posts_List_Table' )->_args['plural'];
314 $nonce = sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) );
315 if ( wp_verify_nonce( $nonce, 'bulk-' . $suffix) ) {
316 return true;
317 }
318 }
319 return false;
320 }
321
322 /**
323 * Add extra columns in admin Quote List View
324 *
325 * @param [array] $columns
326 * @return array
327 */
328 function quote_posts_columns( $columns )
329 {
330 return
331 array (
332 'cb' => '<input type="checkbox" />',
333 'title' => __('Title', 'easy-quotes'),
334 'quote' => __('Quote', 'easy-quotes'),
335 'taxonomy-quote-category' => __('Categories', 'easy-quotes'),
336 'quote_author' => __('Author', 'easy-quotes'),
337 'quote_date' => __('Date', 'easy-quotes'),
338 'quote_rating' => __('Rating', 'easy-quotes'),
339 'date' => __('Status', 'easy-quotes')
340 );
341 }
342
343 /**
344 * Return extra columns data in admin Quote List View
345 *
346 * @param [string] $column_key
347 * @param int $post_id
348 * @return void
349 */
350 function quote_posts_custom_column( $column_key, $post_id ) {
351 switch ($column_key) {
352 case 'quote':
353 $quote = get_the_content(null, false, $post_id);
354 $quote = wp_trim_words($quote, 15, ' (...)');
355 if (!empty($quote)) {
356 // Escape content and allow paragraph tags from wpautop
357 echo wp_kses_post( wpautop( esc_html( $quote ) ) );
358 }
359 break;
360
361 case 'quote_author':
362 $author = get_post_meta($post_id, 'quote_author', true);
363 if (!empty($author))
364 echo esc_html($author);
365 break;
366
367 case 'quote_date':
368 $date = get_post_meta($post_id, 'quote_date', true);
369 if (!empty($date))
370 echo esc_html($date);
371 break;
372
373 case 'quote_rating':
374 $rating = get_post_meta($post_id, 'quote_rating', true);
375 if(empty($rating))
376 $rating = "0";
377 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- get_stars() returns safe, sanitized SVG
378 echo Easy_Quotes_Stars::get_stars((float)$rating);
379 echo '<span data-value="'.esc_attr($rating).'">'.esc_html(number_format_i18n($rating,1) . ' ' . __('out of 5', 'easy-quotes') ).'</span>';
380 break;
381 }
382 }
383
384 /**
385 * Return sortable columns
386 *
387 * @param [array] $columns
388 * @return array
389 */
390 function quote_sortable_columns ($columns) {
391 $columns['quote_author'] = 'quote_author';
392 $columns['quote_date'] = 'quote_date';
393 $columns['quote_rating'] = 'quote_rating';
394 return $columns;
395 }
396
397 /**
398 * Correct the type of query for sorting
399 *
400 * @param [type] $query
401 * @return void
402 */
403 function pre_get_posts($query) {
404 if (!is_admin())
405 return;
406
407 $orderby = $query->get('orderby');
408 if ($orderby === 'quote_author') {
409 $query->set('meta_key', 'quote_author');
410 $query->set('orderby', 'meta_value');
411 return;
412 }
413
414 if ($orderby === 'quote_date') {
415 $query->set('meta_key', 'quote_date');
416 $query->set('orderby', 'meta_value_date');
417 return;
418 }
419
420 if ($orderby === 'quote_rating') {
421 $query->set('meta_key', 'quote_rating');
422 $query->set('orderby', 'meta_value_num');
423 return;
424 }
425 }
426
427 /**
428 * Adds a Quick Edit Custom Box to Post-Type 'quote'
429 *
430 * @param string $column_name
431 * @param string $post_type
432 * @return void
433 */
434 function quick_edit_custom_box($column_name, $post_type) {
435 if (!($post_type === 'quote'))
436 return;
437
438 switch ($column_name) {
439 case 'quote_author':
440 echo
441 '<fieldset class="inline-edit-col-right">
442 <div class="inline-edit-col">
443 <label>
444 <span class="title">' . esc_html__( 'Author', 'easy-quotes' ) . '</span>
445 <span class="input-text-wrap">
446 <input class="quote_author" type="text" name="quote_author" value="">
447 </span>
448 </label>
449 </div>
450 </fieldset>';
451 break;
452 case 'quote_date':
453 echo
454 '<fieldset class="inline-edit-col-right">
455 <div class="inline-edit-col">
456 <label>
457 <span class="title">' . esc_html__( 'Date', 'easy-quotes' ) . '</span>
458 <span class="input-text-wrap">
459 <input class="quote_date" type="text" name="quote_date" value="">
460 </span>
461 </label>
462 </div>
463 </fieldset>';
464 break;
465 case 'quote_rating':
466 echo
467 '<fieldset class="inline-edit-col-right">
468 <div class="inline-edit-col">
469 <label>
470 <span class="title">' . esc_html__( 'Rating', 'easy-quotes' ) . '</span>
471 <span class="input-text-wrap">
472 <input class="quote_rating" type="number" min="0" max="5" step="0.1" name="quote_rating" value="">
473 </span>
474 </label>
475 </div>
476 </fieldset>';
477 break;
478 }
479 }
480
481 /**
482 * Adds Category-dropdown to Admin View 'Quote'
483 *
484 * @param string $post_type
485 * @return void
486 */
487 function restrict_manage_posts( $post_type ) {
488 if ( 'quote' !== $post_type ) {
489 return;
490 }
491
492 $slug = 'quote-category';
493 $taxonomy = get_taxonomy( 'quote-category' );
494 $category = "";
495 if ( isset( $_GET[ $slug ] ) ) {
496 $category = sanitize_title( wp_unslash($_GET[ $slug ]) );
497 }
498
499 wp_dropdown_categories( array(
500 'show_option_all' => esc_html( $taxonomy->labels->all_items ),
501 'taxonomy' => $slug,
502 'name' => $slug,
503 'orderby' => 'name',
504 'value_field' => 'slug',
505 'selected' => $category,
506 'hierarchical' => true,
507 ) );
508 }
509
510 }
511