Product
1 month ago
Async_Request.php
2 years ago
Background_Job.php
2 months ago
Category.php
1 month ago
Connection.php
4 months ago
Email.php
1 year ago
Order.php
1 day ago
Order_Sync.php
10 months ago
Product.php
1 month ago
Products.php
1 day ago
Sync.php
3 years ago
Category.php
319 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 | if ( is_wp_error( $inserted_term ) ) { |
| 181 | wc_square()->log( 'Error inserting category: ' . $inserted_term->get_error_message() ); |
| 182 | return null; |
| 183 | } |
| 184 | |
| 185 | $category_id = isset( $inserted_term['term_id'] ) ? $inserted_term['term_id'] : null; |
| 186 | } |
| 187 | |
| 188 | if ( $category_id ) { |
| 189 | wp_update_term( $category_id, 'product_cat', array( 'name' => $name ) ); |
| 190 | self::update_square_meta( $category_id, $id, $version ); |
| 191 | } |
| 192 | |
| 193 | return $category_id; |
| 194 | } |
| 195 | |
| 196 | |
| 197 | /** |
| 198 | * Updates a category's Square metadata. |
| 199 | * |
| 200 | * @since 2.0.0 |
| 201 | * |
| 202 | * @param int $category_id the category ID |
| 203 | * @param string $square_id the square ID |
| 204 | * @param string $square_version the square version |
| 205 | */ |
| 206 | public static function update_square_meta( $category_id, $square_id, $square_version ) { |
| 207 | |
| 208 | update_term_meta( $category_id, self::SQUARE_ID_META_KEY, $square_id ); |
| 209 | update_term_meta( $category_id, self::SQUARE_VERSION_META_KEY, $square_version ); |
| 210 | } |
| 211 | |
| 212 | |
| 213 | /** |
| 214 | * Gets a category ID from a known square ID. |
| 215 | * |
| 216 | * @since 2.0.0 |
| 217 | * |
| 218 | * @param string $square_id the square ID |
| 219 | * @return int|null |
| 220 | */ |
| 221 | public static function get_category_id_by_square_id( $square_id ) { |
| 222 | global $wpdb; |
| 223 | |
| 224 | return $wpdb->get_var( |
| 225 | $wpdb->prepare( |
| 226 | " |
| 227 | SELECT t.term_id FROM {$wpdb->prefix}terms AS t |
| 228 | LEFT JOIN {$wpdb->prefix}term_taxonomy AS tt ON t.term_id = tt.term_id |
| 229 | LEFT JOIN {$wpdb->prefix}termmeta AS tm ON t.term_id = tm.term_id |
| 230 | WHERE tt.taxonomy = 'product_cat' |
| 231 | AND tm.meta_key = %s |
| 232 | AND tm.meta_value = %s |
| 233 | ", |
| 234 | self::SQUARE_ID_META_KEY, |
| 235 | $square_id |
| 236 | ) |
| 237 | ); |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Get category ID from ITEM catalog object. |
| 242 | * |
| 243 | * @param \Square\Models\CatalogItem $catalog_item the catalog item object |
| 244 | * @return string|null |
| 245 | */ |
| 246 | public static function get_square_category_id( $catalog_item ) { |
| 247 | $catalog_category_id = null; |
| 248 | // Try to get the category from the reporting category first. |
| 249 | $catalog_category = $catalog_item->getReportingCategory(); |
| 250 | |
| 251 | // If no reporting category, try to get the first category from the categories list. |
| 252 | if ( empty( $catalog_category ) ) { |
| 253 | $catalog_categories = $catalog_item->getCategories(); |
| 254 | if ( ! empty( $catalog_categories ) ) { |
| 255 | $catalog_category = $catalog_categories[0]; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | // If we have a category, get the ID. |
| 260 | if ( ! empty( $catalog_category ) ) { |
| 261 | $catalog_category_id = $catalog_category->getId(); |
| 262 | } |
| 263 | |
| 264 | return $catalog_category_id; |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Get the WooCommerce category IDs from a catalog item. |
| 269 | * |
| 270 | * @since 5.4.0 |
| 271 | * |
| 272 | * @param \Square\Models\CatalogItem $catalog_item the catalog item object |
| 273 | * @return array |
| 274 | */ |
| 275 | public static function get_category_ids_from_catalog_item( $catalog_item ) { |
| 276 | $category_ids = array(); |
| 277 | $missing_category_ids = array(); |
| 278 | |
| 279 | if ( empty( $catalog_item ) || ! $catalog_item instanceof \Square\Models\CatalogItem ) { |
| 280 | return $category_ids; |
| 281 | } |
| 282 | |
| 283 | if ( $catalog_item->getCategories() && is_array( $catalog_item->getCategories() ) ) { |
| 284 | foreach ( $catalog_item->getCategories() as $category ) { |
| 285 | if ( $category instanceof \Square\Models\CatalogObjectCategory ) { |
| 286 | $category_id = self::get_category_id_by_square_id( $category->getId() ); |
| 287 | if ( $category_id ) { |
| 288 | $category_ids[] = $category_id; |
| 289 | } else { |
| 290 | $missing_category_ids[] = $category->getId(); |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | // Fetch and import missing categories. |
| 297 | if ( ! empty( $missing_category_ids ) ) { |
| 298 | try { |
| 299 | $response = wc_square()->get_api()->batch_retrieve_catalog_objects( $missing_category_ids ); |
| 300 | if ( $response->get_data() instanceof \Square\Models\BatchRetrieveCatalogObjectsResponse ) { |
| 301 | $missing_categories = $response->get_data()->getObjects(); |
| 302 | if ( $missing_categories && is_array( $missing_categories ) ) { |
| 303 | foreach ( $missing_categories as $missing_category ) { |
| 304 | $imported_category_id = self::import_or_update( $missing_category ); |
| 305 | if ( $imported_category_id ) { |
| 306 | $category_ids[] = $imported_category_id; |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | } catch ( \Exception $e ) { |
| 312 | wc_square()->log( 'Error fetching missing categories for product ' . $catalog_item->getName() . ': ' . $e->getMessage() ); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | return array_unique( $category_ids ); |
| 317 | } |
| 318 | } |
| 319 |