-
Notifications
You must be signed in to change notification settings - Fork 33
/
class-post-type.php
556 lines (434 loc) · 13 KB
/
class-post-type.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
<?php
/**
* GoDaddy Reseller Store post types class.
*
* Handles the Reseller Store post types.
*
* @class Reseller_Store/Post_Type
* @package Reseller_Store/Plugin
* @category Class
* @author GoDaddy
* @since 1.0.0
*/
namespace Reseller_Store;
use stdClass;
if ( ! defined( 'ABSPATH' ) ) {
// @codeCoverageIgnoreStart
exit;
// @codeCoverageIgnoreEnd
}
final class Post_Type {
/**
* Post type slug.
*
* @since 0.2.0
*
* @var string
*/
const SLUG = 'reseller_product';
/**
* Post type menu position.
*
* @since 0.2.0
*
* @var int
*/
const MENU_POSITION = 52;
/**
* Post type default permalink base.
*
* @since 0.2.0
*
* @var string
*/
public static $default_permalink_base;
/**
* Hold error object.
*
* @since 0.2.0
*
* @var WP_Error
*/
private $error;
/**
* Class constructor.
*
* @since 0.2.0
*/
public function __construct() {
self::$default_permalink_base = sanitize_title( esc_html_x( 'products', 'slug name', 'reseller-store' ) );
add_action( 'init', [ $this, 'register' ] );
add_action( 'admin_head', [ $this, 'column_styles' ] );
add_action( 'manage_posts_custom_column', [ $this, 'column_content' ], 10, 2 );
add_action( 'delete_post', [ $this, 'delete_imported_product' ] );
add_filter( 'manage_' . self::SLUG . '_posts_columns', [ $this, 'columns' ] );
add_filter( 'posts_clauses', [ $this, 'order_by_price_clause' ], 10, 2 );
add_filter( 'post_type_labels_' . self::SLUG, [ $this, 'post_screen_edit_heading' ] );
add_filter( 'the_content', [ $this, 'append_add_to_cart_form' ] );
add_filter( 'the_excerpt', [ $this, 'append_add_to_cart_form' ] );
add_filter(
'edit_' . self::SLUG . '_per_page', function () {
return 50;
}
);
add_filter(
'manage_edit-' . self::SLUG . '_sortable_columns', function ( $columns ) {
// @codingStandardsIgnoreStart
return array_merge( $columns, [ 'price' => 'price' ] );
// @codingStandardsIgnoreEnd
}
);
add_filter(
'view_mode_post_types', function ( $post_types ) {
// @codingStandardsIgnoreStart
return array_diff_key( $post_types, [ self::SLUG => self::SLUG ] );
// @codingStandardsIgnoreEnd
}
);
add_action( 'save_post', [ $this, 'republish_post' ] );
}
/**
* Republishes the product post from the catalog api
*
* @since 1.1.0
*
* @param int $post_id Product post ID.
*
* @return void
*/
public function republish_post( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if ( get_post_status( $post_id ) !== 'publish' ) {
return;
}
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
if ( ! isset( $_POST['republish_product'] ) || ! 'Reset' === $_POST['republish_product'] ) {
return;
}
if ( ! isset( $_POST['post_type'] ) || ! self::SLUG === $_POST['post_type'] ) {
return;
}
// Unhook this function so it doesn't loop infinitely.
remove_action( 'save_post', [ $this, 'republish_post' ] );
$result = $this->reset_product_data( $post_id );
// Re-hook this function.
add_action( 'save_post', [ $this, 'republish_post' ] );
if ( ! is_wp_error( $result ) ) {
return;
}
/**
* If there is an error, display it in an admin notice.
*/
add_action(
'edit_form_top', function () use ( $result ) {
echo printf(
'<div class="notice notice-error is-dismissible"><p>Error: %s</p></div>',
esc_html(
sprintf(
$result->get_error_message(),
$result->get_error_data( $result->get_error_code() )
)
)
);
}, 0
);
}
/**
* Return the post type custom permalink base.
*
* @since 0.2.0
*
* @return string
*/
public static function permalink_base() {
$permalinks = (array) rstore_get_option( 'permalinks', [] );
$permalink_base = ! empty( $permalinks['product_base'] ) ? $permalinks['product_base'] : self::$default_permalink_base;
return sanitize_title( $permalink_base );
}
/**
* Register the custom post type.
*
* @action init
* @since 0.2.0
*/
public function register() {
$labels = [
'name' => esc_html_x( 'Products', 'post type general name', 'reseller-store' ),
'singular_name' => esc_html_x( 'Product', 'post type singular name', 'reseller-store' ),
'menu_name' => esc_html_x( 'Reseller Store', 'admin menu', 'reseller-store' ),
'name_admin_bar' => esc_html_x( 'Reseller Product', 'add new on admin bar', 'reseller-store' ),
'add_new' => esc_html_x( 'Add New', 'product', 'reseller-store' ),
'add_new_item' => esc_html__( 'Add New Products', 'reseller-store' ),
'edit_item' => esc_html__( 'Edit Product', 'reseller-store' ),
'new_item' => esc_html__( 'New Product', 'reseller-store' ),
'view_item' => esc_html__( 'View Product', 'reseller-store' ),
'view_items' => esc_html__( 'View Products', 'reseller-store' ),
'search_items' => esc_html__( 'Search Products', 'reseller-store' ),
'not_found' => esc_html__( 'No products found.', 'reseller-store' ),
'not_found_in_trash' => esc_html__( 'No products found in Trash.', 'reseller-store' ),
'parent_item_colon' => esc_html__( 'Parent Products:', 'reseller-store' ),
'all_items' => esc_html__( 'All Products', 'reseller-store' ),
'archives' => esc_html__( 'Product Archives', 'reseller-store' ),
'insert_into_item' => esc_html__( 'Insert into product', 'reseller-store' ),
'uploaded_to_this_item' => esc_html__( 'Uploaded to this product', 'reseller-store' ),
'featured_image' => esc_html__( 'Product Image', 'reseller-store' ),
'set_featured_image' => esc_html__( 'Set product image', 'reseller-store' ),
'remove_featured_image' => esc_html__( 'Remove product image', 'reseller-store' ),
'use_featured_image' => esc_html__( 'Use as featured image', 'reseller-store' ),
];
$args = [
'labels' => $labels,
'description' => esc_html__( 'This is where you can add new products to your Reseller Store.', 'reseller-store' ),
'menu_icon' => 'dashicons-cart',
'menu_position' => self::MENU_POSITION,
'public' => true,
'show_in_rest' => true,
'rest_base' => self::permalink_base(),
'query_var' => self::permalink_base(),
'has_archive' => true,
'supports' => [ 'title', 'editor', 'thumbnail' ],
'rewrite' => [
'slug' => self::permalink_base(),
'with_front' => false,
'feeds' => true,
],
];
/**
* Filter the post type args.
*
* @since 0.2.0
*
* @var array
*/
$args = (array) apply_filters( 'rstore_product_args', $args );
register_post_type( self::SLUG, $args );
}
/**
* Reset a product's data.
*
* @since 0.2.0
*
* @param int $post_id Product post ID.
*
* @return true|\WP_Error
*/
public function reset_product_data( $post_id ) {
$product_id = rstore_get_product_meta( $post_id, 'id' );
if ( false === $product_id ) {
return new \WP_Error(
'invalid_product_id',
esc_html__( 'Product id not found or invalid.', 'reseller-store' )
);
}
$product = rstore_get_product( $product_id );
if ( is_wp_error( $product ) ) {
return $product;
}
$import = new Import( $product, $post_id );
if ( is_wp_error( $import ) ) {
return $import;
}
return $import->import_product();
}
/**
* Print styles for custom columns on the edit screen.
*
* @action admin_head
* @since 0.2.0
*/
public function column_styles() {
if ( ! rstore_is_admin_uri( 'post_type=' . self::SLUG, false ) ) {
return;
}
?>
<style type="text/css">
#screen-options-wrap .rstore-image {
display: inline;
font-family: inherit;
font-size: inherit;
line-height: inherit;
-webkit-font-smoothing: auto;
-moz-osx-font-smoothing: auto;
}
#screen-options-wrap .rstore-image:before {
display: none;
}
#screen-options-wrap .rstore-image .screen-reader-text {
visibility: visible;
position: static;
}
table.wp-list-table .column-image {
width: 52px;
text-align: center;
white-space: nowrap;
}
table.wp-list-table .column-title {
width: 33%;
}
@media only screen and (max-width: 782px) {
.post-type-<?php echo esc_attr( self::SLUG ); ?> .wp-list-table .column-image {
display: none;
text-align: left;
padding-bottom: 0;
}
.post-type-<?php echo esc_attr( self::SLUG ); ?> .wp-list-table .column-image img {
max-width: 32px;
height: auto;
}
.post-type-<?php echo esc_attr( self::SLUG ); ?> .wp-list-table tr td.column-image::before {
display: none !important;
}
}
</style>
<?php
}
/**
* Add custom columns.
*
* @filter manage_{post_type}_posts_columns
* @since 0.2.0
*
* @param array $columns Admin post columns.
*
* @return array
*/
public function columns( $columns ) {
// Insert before Title column.
$columns = rstore_array_insert(
$columns,
[
'image' => sprintf(
'<span class="rstore-image dashicons dashicons-format-image" title="%1$s"><span class="screen-reader-text">%1$s</span></span>',
__( 'Product Image', 'reseller-store' )
),
],
(int) array_search( 'title', array_values( array_flip( $columns ) ), true )
);
// Insert after Title column.
$columns = rstore_array_insert(
$columns,
// @codingStandardsIgnoreStart
[ 'price' => esc_html__( 'Price', 'reseller-store' ) ],
// @codingStandardsIgnoreEnd
(int) array_search( 'title', array_values( array_flip( $columns ) ), true ) + 1
);
return $columns;
}
/**
* Display custom column content.
*
* @action manage_posts_custom_column
* @since 0.2.0
*
* @param string $column Admin column name.
* @param int $post_id Post ID.
*/
public function column_content( $column, $post_id ) {
if ( 'image' === $column ) {
echo get_the_post_thumbnail( $post_id, [ 40, 40 ] );
}
if ( 'price' === $column ) {
$list = rstore_get_product_meta( $post_id, 'listPrice' );
$sale = rstore_get_product_meta( $post_id, 'salePrice' );
printf( // xss ok.
'%s%s',
( $sale ) ? sprintf( '<del>%s</del><br>', esc_html( $list ) ) : '',
( $sale ) ? esc_html( $sale ) : esc_html( $list )
);
}
}
/**
* Mark an imported product as deleted.
*
* @action delete_post
* @since 0.2.0
*
* @param int $post_id Product post ID.
*
* @return bool Returns `true` on success, `false` on failure.
*/
public function delete_imported_product( $post_id ) {
if ( Post_Type::SLUG !== get_post_type( $post_id ) ) {
return false;
}
// Re-fetch products from the API to ensure `rstore_has_all_products()` is accurate.
rstore_delete_transient( 'products' );
$imported = (array) rstore_get_option( 'imported', [] );
unset( $imported[ $post_id ] );
return rstore_update_option( 'imported', $imported );
}
/**
* Strip `$` from meta values when ordering by price.
*
* @filter posts_clauses
* @global wpdb $wpdb
* @since 0.2.0
*
* @param array $clauses $wpdb query clauses.
* @param object $wp_query $wp_query instance.
*
* @return array
*/
public function order_by_price_clause( $clauses, $wp_query ) {
global $wpdb;
if ( self::SLUG === $wp_query->get( 'post_type' ) && 'price' === $wp_query->get( 'orderby' ) ) {
$order = ( 'DESC' === strtoupper( $wp_query->get( 'order' ) ) ) ? 'DESC' : 'ASC';
$clauses['join'] .= $wpdb->prepare(
" LEFT JOIN `{$wpdb->postmeta}` pm ON ( `{$wpdb->posts}`.`ID` = pm.`post_id` AND pm.`meta_key` = %s ) ",
rstore_prefix( 'listPrice' )
);
$clauses['orderby'] = " CONVERT( REPLACE( pm.`meta_value`, '$', '' ), DECIMAL( 13, 2 ) ) {$order}"; // xss ok.
}
return $clauses;
}
/**
* Customize the edit heading on the post screen.
*
* @filter post_type_labels_{post_type}
* @since 0.2.0
*
* @param stdClass $labels Product labels.
*
* @return array
*/
public function post_screen_edit_heading( $labels ) {
if ( ! rstore_is_admin_uri( 'post.php?post=' ) ) {
return $labels;
}
$post_id = filter_input( INPUT_GET, 'post', FILTER_SANITIZE_NUMBER_INT );
$title = ( $post_id > 0 ) ? rstore_get_product_meta( $post_id, 'title' ) : null;
/* translators: product title */
$labels->edit_item = ( $title ) ? sprintf( esc_html__( 'Edit: %s', 'reseller-store' ), $title ) : $labels->edit_item;
return $labels;
}
/**
* Append an `Add to cart` form the end of product post content.
*
* @action the_content
* @global WP_Post $post
* @since 0.2.0
*
* @param string $content Product content.
*
* @return string
*/
public function append_add_to_cart_form( $content ) {
global $post;
if ( ! isset( $post ) ) {
return $content;
}
$is_rest_request = ( defined( 'REST_REQUEST' ) && REST_REQUEST );
if ( self::SLUG === $post->post_type && ! is_feed() && ! $is_rest_request ) {
if ( property_exists( $post, 'rstore_widget' ) && true === $post->rstore_widget ) {
return $content;
}
$content .= rstore_price( $post->ID, false );
$content .= rstore_add_to_cart_form( $post->ID, false );
}
return $content;
}
}