PluginProbe ʕ •ᴥ•ʔ
WooCommerce Square / 3.8.1
WooCommerce Square v3.8.1
5.4.3 5.4.2 5.4.1 5.4.0 trunk 1.0.25 1.0.26 1.0.27 1.0.28 1.0.29 1.0.30 1.0.31 1.0.32 1.0.33 1.0.34 1.0.35 1.0.36 1.0.37 1.0.38 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.7.0 2.8.0 2.9.0 2.9.1 3.0.0 3.0.1 3.0.2 3.0.3 3.1.0 3.2.0 3.3.0 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.6.1 3.7.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.9.0 4.0.0 4.1.0 4.2.0 4.2.1 4.2.2 4.2.3 4.3.0 4.3.1 4.3.2 4.4.0 4.4.1 4.4.2 4.5.0 4.5.1 4.5.2 4.6.0 4.6.1 4.6.2 4.6.3 4.6.4 4.7.0 4.7.1 4.7.2 4.7.3 4.7.4 4.8.0 4.8.1 4.8.2 4.8.3 4.8.4 4.8.5 4.8.6 4.8.7 4.8.8 4.9.0 4.9.1 4.9.2 4.9.3 4.9.4 4.9.5 4.9.6 4.9.7 4.9.8 4.9.9 5.0.0 5.0.1 5.1.0 5.1.1 5.1.2 5.2.0 5.3.0 5.3.1 5.3.2 5.3.3
woocommerce-square / includes / Handlers / Category.php
woocommerce-square / includes / Handlers Last commit date
Product 3 years ago Background_Job.php 3 years ago Category.php 3 years ago Connection.php 3 years ago Email.php 3 years ago Order.php 3 years ago Product.php 3 years ago Products.php 3 years ago Sync.php 3 years ago
Category.php
237 lines
1 <?php
2 /**
3 * WooCommerce Square
4 *
5 * This source file is subject to the GNU General Public License v3.0
6 * that is bundled with this package in the file license.txt.
7 * It is also available through the world-wide-web at this URL:
8 * http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 or later
9 * If you did not receive a copy of the license and are unable to
10 * obtain it through the world-wide-web, please send an email
11 * to license@woocommerce.com so we can send you a copy immediately.
12 *
13 * DISCLAIMER
14 *
15 * Do not edit or add to this file if you wish to upgrade WooCommerce Square to newer
16 * versions in the future. If you wish to customize WooCommerce Square for your
17 * needs please refer to https://docs.woocommerce.com/document/woocommerce-square/
18 *
19 * @author WooCommerce
20 * @copyright Copyright: (c) 2019, Automattic, Inc.
21 * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 or later
22 */
23
24 namespace WooCommerce\Square\Handlers;
25
26 defined( 'ABSPATH' ) || exit;
27
28 /**
29 * Category handler class.
30 *
31 * @since 2.0.0
32 */
33 class Category {
34
35
36 const CATEGORY_MAP_META_KEY = 'wc_square_category_map';
37
38 const SQUARE_ID_META_KEY = 'square_cat_id';
39
40 const SQUARE_VERSION_META_KEY = 'square_cat_version';
41
42
43 /**
44 * Gets the full category map.
45 *
46 * @since 2.0.0
47 *
48 * @return array
49 */
50 public static function get_map() {
51
52 return get_option( self::CATEGORY_MAP_META_KEY, array() );
53 }
54
55
56 /**
57 * Updates the full category map.
58 *
59 * @since 2.0.0
60 *
61 * @param array $map
62 */
63 public static function update_map( $map ) {
64
65 update_option( self::CATEGORY_MAP_META_KEY, $map );
66 }
67
68
69 /**
70 * Gets the mapping for a single category ID.
71 *
72 * @since 2.0.0
73 *
74 * @param int $category_id the category ID
75 * @return array
76 */
77 public static function get_mapping( $category_id ) {
78
79 $category_id = (int) $category_id;
80 $map = self::get_map();
81
82 if ( isset( $map[ $category_id ] ) ) {
83
84 return $map[ $category_id ];
85 }
86
87 return array();
88 }
89
90
91 /**
92 * Returns a Square ID if known, or a temporary ID to be used in API calls.
93 *
94 * @since 2.0.0
95 *
96 * @param int $category_id
97 * @return string
98 */
99 public static function get_square_id( $category_id ) {
100
101 $mapping = self::get_mapping( $category_id );
102
103 return isset( $mapping['square_id'] ) ? $mapping['square_id'] : '#category_' . $category_id;
104 }
105
106
107 /**
108 * Gets the Square version for the given category (if known).
109 *
110 * @since 2.0.0
111 *
112 * @param int $category_id
113 * @return int
114 */
115 public static function get_square_version( $category_id ) {
116
117 $mapping = self::get_mapping( $category_id );
118
119 return isset( $mapping['square_version'] ) ? (int) $mapping['square_version'] : 0;
120 }
121
122
123 /**
124 * Adds a mapping for a category.
125 *
126 * @since 2.0.0
127 *
128 * @param int $category_id the category ID
129 * @param string $square_id the Square Catalog Item ID
130 * @param string $square_version the Square Item version
131 *
132 * @return array the updated full map
133 */
134 public static function update_mapping( $category_id, $square_id, $square_version ) {
135
136 $map = self::get_map();
137
138 $map[ $category_id ] = array(
139 'square_id' => $square_id,
140 'square_version' => $square_version,
141 );
142
143 self::update_map( $map );
144
145 return $map;
146 }
147
148
149 /**
150 * Imports or updates local category data for a remote CatalogObject.
151 *
152 * @since 2.0.0
153 *
154 * @param \Square\Models\CatalogObject $catalog_object the catalog object
155 * @return int|null the category ID, if found
156 */
157 public static function import_or_update( $catalog_object ) {
158
159 $id = $catalog_object->getId();
160 $version = $catalog_object->getVersion();
161 $name = $catalog_object->getCategoryData()->getName();
162
163 // look for category ID by the square ID
164 $category_id = self::get_category_id_by_square_id( $id );
165
166 // if not found, search for the category by name
167 if ( ! $category_id ) {
168
169 if ( $category = get_term_by( 'name', $name, 'product_cat', ARRAY_A ) ) {
170
171 $category_id = isset( $category['term_id'] ) ? absint( $category['term_id'] ) : null;
172 }
173 }
174
175 // if still not found, create a new category
176 if ( ! $category_id ) {
177
178 $inserted_term = wp_insert_term( $name, 'product_cat' );
179
180 $category_id = isset( $inserted_term['term_id'] ) ? $inserted_term['term_id'] : null;
181 }
182
183 if ( $category_id ) {
184 wp_update_term( $category_id, 'product_cat', array( 'name' => $name ) );
185 self::update_square_meta( $category_id, $id, $version );
186 }
187
188 return $category_id;
189 }
190
191
192 /**
193 * Updates a category's Square metadata.
194 *
195 * @since 2.0.0
196 *
197 * @param int $category_id the category ID
198 * @param string $square_id the square ID
199 * @param string $square_version the square version
200 */
201 public static function update_square_meta( $category_id, $square_id, $square_version ) {
202
203 update_term_meta( $category_id, self::SQUARE_ID_META_KEY, $square_id );
204 update_term_meta( $category_id, self::SQUARE_VERSION_META_KEY, $square_version );
205 }
206
207
208 /**
209 * Gets a category ID from a known square ID.
210 *
211 * @since 2.0.0
212 *
213 * @param string $square_id the square ID
214 * @return int|null
215 */
216 public static function get_category_id_by_square_id( $square_id ) {
217 global $wpdb;
218
219 $query = $wpdb->prepare(
220 "
221 SELECT t.term_id FROM {$wpdb->prefix}terms AS t
222 LEFT JOIN {$wpdb->prefix}term_taxonomy AS tt ON t.term_id = tt.term_id
223 LEFT JOIN {$wpdb->prefix}termmeta AS tm ON t.term_id = tm.term_id
224 WHERE tt.taxonomy = 'product_cat'
225 AND tm.meta_key = '%s'
226 AND tm.meta_value = '%s'
227 ",
228 self::SQUARE_ID_META_KEY,
229 $square_id
230 );
231
232 return $wpdb->get_var( $query );
233 }
234
235
236 }
237