PluginProbe
Tiered Pricing Table for WooCommerce / 8.1.0
Tiered Pricing Table for WooCommerce v8.1.0
8.1.0 8.0.2 7.1.7 7.1.5 7.1.4 7.1.1 6.5.0 6.4.0 6.1.0 trunk 1.0 1.1 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2 2.2.3 2.3.0 2.3.1 2.3.2 All 92 releases
tier-pricing-table / src / Addons / GlobalTieredPricing / CPT / StatusViews.php

StatusViews.php in Tiered Pricing Table for WooCommerce 8.1.0, at src/Addons/GlobalTieredPricing/CPT/StatusViews.php

123 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php namespace TierPricingTable\Addons\GlobalTieredPricing\CPT;
2
3 use TierPricingTable\Addons\GlobalTieredPricing\GlobalPricingRule;
4 use WP_Query;
5
6 /**
7 * The views above the rules list: Active, Scheduled, Expired, Suspended and Skipped with their counts,
8 * each filtering the list. They replace WordPress's "Published" view, which they add up to; All, Drafts
9 * and Trash stay.
10 */
11 class StatusViews {
12
13 const QUERY_VAR = 'tpt_status';
14
15 public function __construct() {
16 add_filter( 'views_edit-' . GlobalTieredPricingCPT::SLUG, array( $this, 'views' ) );
17 add_action( 'pre_get_posts', array( $this, 'filterList' ) );
18 }
19
20 /**
21 * @return array<string, string> status => label, in display order.
22 */
23 public static function labels(): array {
24 return array(
25 'active' => __( 'Active', 'tier-pricing-table' ),
26 'scheduled' => __( 'Scheduled', 'tier-pricing-table' ),
27 'expired' => __( 'Expired', 'tier-pricing-table' ),
28 'suspended' => __( 'Suspended', 'tier-pricing-table' ),
29 'skipped' => __( 'Skipped', 'tier-pricing-table' ),
30 );
31 }
32
33 /**
34 * Published rules grouped by effective status (ids), computed once per request.
35 *
36 * @return array<string, int[]>
37 */
38 public static function groups(): array {
39 static $groups = null;
40
41 if ( null !== $groups ) {
42 return $groups;
43 }
44
45 $groups = array_fill_keys( array_keys( self::labels() ), array() );
46
47 $ids = get_posts( array(
48 'post_type' => GlobalTieredPricingCPT::SLUG,
49 'post_status' => 'publish',
50 'numberposts' => - 1,
51 'fields' => 'ids',
52 ) );
53
54 foreach ( $ids as $id ) {
55 $groups[ GlobalPricingRule::build( $id )->getEffectiveStatus() ][] = (int) $id;
56 }
57
58 return $groups;
59 }
60
61 public static function current(): string {
62 // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- a read-only list filter
63 $status = isset( $_GET[ self::QUERY_VAR ] ) ? sanitize_key( wp_unslash( $_GET[ self::QUERY_VAR ] ) ) : '';
64
65 return isset( self::labels()[ $status ] ) ? $status : '';
66 }
67
68 public function views( array $views ): array {
69 $groups = self::groups();
70 $current = self::current();
71 $added = array();
72
73 foreach ( self::labels() as $status => $label ) {
74 $count = count( $groups[ $status ] );
75
76 if ( ! $count ) {
77 continue; // like WordPress, empty statuses are not listed
78 }
79
80 $added[ 'tpt_' . $status ] = sprintf(
81 '<a href="%s"%s>%s <span class="count">(%s)</span></a>',
82 esc_url( add_query_arg( array( 'post_type' => GlobalTieredPricingCPT::SLUG, self::QUERY_VAR => $status ), admin_url( 'edit.php' ) ) ),
83 $current === $status ? ' class="current" aria-current="page"' : '',
84 esc_html( $label ),
85 esc_html( number_format_i18n( $count ) )
86 );
87 }
88
89 unset( $views['publish'] );
90
91 $result = array();
92 foreach ( $views as $key => $view ) {
93 $result[ $key ] = $view;
94 if ( 'all' === $key ) {
95 $result += $added;
96 }
97 }
98
99 if ( ! isset( $views['all'] ) ) {
100 $result = $added + $result;
101 }
102
103 return $result;
104 }
105
106 public function filterList( WP_Query $query ) {
107 if ( ! is_admin() || ! $query->is_main_query() || GlobalTieredPricingCPT::SLUG !== $query->get( 'post_type' ) ) {
108 return;
109 }
110
111 $status = self::current();
112
113 if ( ! $status ) {
114 return;
115 }
116
117 $ids = self::groups()[ $status ];
118
119 $query->set( 'post_status', 'publish' );
120 $query->set( 'post__in', $ids ? $ids : array( 0 ) );
121 }
122 }
123