PluginProbe
Tiered Pricing Table for WooCommerce / 8.0.2
Tiered Pricing Table for WooCommerce v8.0.2
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 2.3.3 All 91 releases
tier-pricing-table / src / Addons / NonLoggedInUsers / Visibility / VisibilityMeta.php

VisibilityMeta.php in Tiered Pricing Table for WooCommerce 8.0.2, at src/Addons/NonLoggedInUsers/Visibility/VisibilityMeta.php

157 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php namespace TierPricingTable\Addons\NonLoggedInUsers\Visibility;
2
3 use TierPricingTable\Addons\NonLoggedInUsers\Roles;
4
5 /**
6 * Where the visibility rules live: product meta and product_cat term meta.
7 */
8 class VisibilityMeta {
9
10 const PRODUCT_MODE = '_tpt_visibility_mode';
11 const PRODUCT_ROLES = '_tpt_visibility_roles';
12
13 const TERM_MODE = 'tpt_visibility_mode';
14 const TERM_ROLES = 'tpt_visibility_roles';
15
16 /** Bumped whenever a rule or a product's categories change; part of every cache key. */
17 const VERSION_OPTION = 'tpt_visibility_version';
18
19 public static function getProductRule( int $productId ): array {
20 return VisibilityRule::normalize(
21 get_post_meta( $productId, self::PRODUCT_MODE, true ),
22 get_post_meta( $productId, self::PRODUCT_ROLES, true )
23 );
24 }
25
26 public static function saveProductRule( int $productId, $mode, $roles ) {
27 $rule = VisibilityRule::normalize( $mode, $roles );
28
29 if ( VisibilityRule::MODE_INHERIT === $rule['mode'] ) {
30 delete_post_meta( $productId, self::PRODUCT_MODE );
31 delete_post_meta( $productId, self::PRODUCT_ROLES );
32 } else {
33 update_post_meta( $productId, self::PRODUCT_MODE, $rule['mode'] );
34 update_post_meta( $productId, self::PRODUCT_ROLES, implode( ',', $rule['roles'] ) );
35 }
36
37 self::bumpVersion();
38 }
39
40 public static function getCategoryRule( int $termId ): array {
41 $rule = VisibilityRule::normalize(
42 get_term_meta( $termId, self::TERM_MODE, true ),
43 get_term_meta( $termId, self::TERM_ROLES, true )
44 );
45
46 // a category has no parent rule to inherit from; "inherit" simply means no restriction
47 if ( VisibilityRule::MODE_INHERIT === $rule['mode'] ) {
48 $rule['mode'] = VisibilityRule::MODE_EVERYONE;
49 }
50
51 return $rule;
52 }
53
54 public static function saveCategoryRule( int $termId, $mode, $roles ) {
55 $rule = VisibilityRule::normalize( $mode, $roles );
56
57 if ( ! VisibilityRule::isRestricting( $rule ) ) {
58 delete_term_meta( $termId, self::TERM_MODE );
59 delete_term_meta( $termId, self::TERM_ROLES );
60 } else {
61 update_term_meta( $termId, self::TERM_MODE, $rule['mode'] );
62 update_term_meta( $termId, self::TERM_ROLES, implode( ',', $rule['roles'] ) );
63 }
64
65 self::bumpVersion();
66 }
67
68 /**
69 * The rules of every category of a product, parents included.
70 *
71 * @return array[]
72 */
73 public static function getProductCategoryRules( int $productId ): array {
74 $termIds = wp_get_post_terms( $productId, 'product_cat', array( 'fields' => 'ids' ) );
75
76 if ( is_wp_error( $termIds ) || empty( $termIds ) ) {
77 return array();
78 }
79
80 $all = array();
81
82 foreach ( $termIds as $termId ) {
83 $all[] = (int) $termId;
84
85 foreach ( get_ancestors( (int) $termId, 'product_cat', 'taxonomy' ) as $ancestor ) {
86 $all[] = (int) $ancestor;
87 }
88 }
89
90 $rules = array();
91
92 foreach ( array_unique( $all ) as $termId ) {
93 $rule = self::getCategoryRule( $termId );
94
95 if ( VisibilityRule::isRestricting( $rule ) ) {
96 $rules[] = $rule;
97 }
98 }
99
100 return $rules;
101 }
102
103 /**
104 * Term ids of the categories that carry a restricting rule.
105 *
106 * @return int[]
107 */
108 public static function getRestrictedCategoryIds(): array {
109 $terms = get_terms( array(
110 'taxonomy' => 'product_cat',
111 'hide_empty' => false,
112 'fields' => 'ids',
113 'meta_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
114 array(
115 'key' => self::TERM_MODE,
116 'value' => array( VisibilityRule::MODE_INCLUDE, VisibilityRule::MODE_EXCLUDE ),
117 'compare' => 'IN',
118 ),
119 ),
120 'tpt_visibility' => true, // our own get_terms filter leaves this query alone
121 ) );
122
123 return is_wp_error( $terms ) ? array() : array_map( 'intval', $terms );
124 }
125
126 /**
127 * The roles a rule can name, for the admin checkbox lists: customer-facing roles plus guests.
128 *
129 * @return array<string, string> role key => label
130 */
131 public static function getRoleOptions(): array {
132 return Roles::getOptions();
133 }
134
135 public static function getModeOptions( bool $forProduct ): array {
136 $options = array();
137
138 if ( $forProduct ) {
139 $options[ VisibilityRule::MODE_INHERIT ] = __( 'Follow the category rules (default)', 'tier-pricing-table' );
140 }
141
142 $options[ VisibilityRule::MODE_EVERYONE ] = __( 'Everyone', 'tier-pricing-table' );
143 $options[ VisibilityRule::MODE_INCLUDE ] = __( 'Only the selected roles', 'tier-pricing-table' );
144 $options[ VisibilityRule::MODE_EXCLUDE ] = __( 'Everyone except the selected roles', 'tier-pricing-table' );
145
146 return $options;
147 }
148
149 public static function getVersion(): string {
150 return (string) get_option( self::VERSION_OPTION, '1' );
151 }
152
153 public static function bumpVersion() {
154 update_option( self::VERSION_OPTION, (string) time(), false );
155 }
156 }
157