Agentic
4 months ago
BlockTemplates
4 weeks ago
EmailImprovements
1 year ago
EmailPreview
4 weeks ago
Emails
1 year ago
ImportExport
1 year ago
Logging
1 year ago
Marketing
2 years ago
Notes
4 weeks ago
Onboarding
5 months ago
Orders
4 weeks ago
ProductForm
2 years ago
ProductReviews
4 weeks ago
RemoteFreeExtensions
4 weeks ago
Schedulers
4 weeks ago
Settings
2 weeks ago
Suggestions
4 weeks ago
WCPayPromotion
10 months ago
ActivityPanels.php
3 years ago
Analytics.php
4 weeks ago
CategoryLookup.php
4 years ago
Coupons.php
1 year ago
CouponsMovedTrait.php
5 months ago
CustomerEffortScoreTracks.php
7 months ago
Events.php
2 months ago
FeaturePlugin.php
4 months ago
Homescreen.php
1 month ago
Loader.php
4 weeks ago
Marketing.php
1 year ago
Marketplace.php
5 months ago
MobileAppBanner.php
4 years ago
OrderMilestoneEasterEgg.php
4 weeks ago
RemoteInboxNotifications.php
3 years ago
Settings.php
2 weeks ago
ShippingLabelBanner.php
1 year ago
ShippingLabelBannerDisplayRules.php
1 year ago
SiteHealth.php
3 years ago
Survey.php
4 years ago
SystemStatusReport.php
1 year ago
Translations.php
1 year ago
WCAdminAssets.php
2 weeks ago
WCAdminSharedSettings.php
1 year ago
WCAdminUser.php
9 months ago
WcPayWelcomePage.php
1 year ago
CategoryLookup.php
310 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Keeps the product category lookup table in sync with live data. |
| 4 | */ |
| 5 | |
| 6 | namespace Automattic\WooCommerce\Internal\Admin; |
| 7 | |
| 8 | defined( 'ABSPATH' ) || exit; |
| 9 | |
| 10 | /** |
| 11 | * \Automattic\WooCommerce\Internal\Admin\CategoryLookup class. |
| 12 | */ |
| 13 | class CategoryLookup { |
| 14 | |
| 15 | /** |
| 16 | * Stores changes to categories we need to sync. |
| 17 | * |
| 18 | * @var array |
| 19 | */ |
| 20 | protected $edited_product_cats = array(); |
| 21 | |
| 22 | /** |
| 23 | * The single instance of the class. |
| 24 | * |
| 25 | * @var object |
| 26 | */ |
| 27 | protected static $instance = null; |
| 28 | |
| 29 | /** |
| 30 | * Constructor |
| 31 | * |
| 32 | * @return void |
| 33 | */ |
| 34 | protected function __construct() {} |
| 35 | |
| 36 | /** |
| 37 | * Get class instance. |
| 38 | * |
| 39 | * @return object Instance. |
| 40 | */ |
| 41 | final public static function instance() { |
| 42 | if ( null === static::$instance ) { |
| 43 | static::$instance = new static(); |
| 44 | } |
| 45 | return static::$instance; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Init hooks. |
| 50 | */ |
| 51 | public function init() { |
| 52 | add_action( 'generate_category_lookup_table', array( $this, 'regenerate' ) ); |
| 53 | add_action( 'edit_product_cat', array( $this, 'before_edit' ), 99 ); |
| 54 | add_action( 'edited_product_cat', array( $this, 'on_edit' ), 99 ); |
| 55 | add_action( 'created_product_cat', array( $this, 'on_create' ), 99 ); |
| 56 | add_action( 'init', array( $this, 'define_category_lookup_tables_in_wpdb' ) ); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Regenerate all lookup table data. |
| 61 | */ |
| 62 | public function regenerate() { |
| 63 | global $wpdb; |
| 64 | |
| 65 | $wpdb->query( "TRUNCATE TABLE $wpdb->wc_category_lookup" ); |
| 66 | |
| 67 | $terms = get_terms( |
| 68 | 'product_cat', |
| 69 | array( |
| 70 | 'hide_empty' => false, |
| 71 | 'fields' => 'id=>parent', |
| 72 | ) |
| 73 | ); |
| 74 | |
| 75 | $hierarchy = array(); |
| 76 | $inserts = array(); |
| 77 | |
| 78 | $this->unflatten_terms( $hierarchy, $terms, 0 ); |
| 79 | $this->get_term_insert_values( $inserts, $hierarchy ); |
| 80 | |
| 81 | if ( ! $inserts ) { |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | $insert_string = implode( |
| 86 | '),(', |
| 87 | array_map( |
| 88 | function( $item ) { |
| 89 | return implode( ',', $item ); |
| 90 | }, |
| 91 | $inserts |
| 92 | ) |
| 93 | ); |
| 94 | |
| 95 | $wpdb->query( "INSERT IGNORE INTO $wpdb->wc_category_lookup (category_tree_id,category_id) VALUES ({$insert_string})" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Store edits so we know when the parent ID changes. |
| 100 | * |
| 101 | * @param int $category_id Term ID being edited. |
| 102 | */ |
| 103 | public function before_edit( $category_id ) { |
| 104 | $category = get_term( $category_id, 'product_cat' ); |
| 105 | $this->edited_product_cats[ $category_id ] = $category->parent; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * When a product category gets edited, see if we need to sync the table. |
| 110 | * |
| 111 | * @param int $category_id Term ID being edited. |
| 112 | */ |
| 113 | public function on_edit( $category_id ) { |
| 114 | global $wpdb; |
| 115 | |
| 116 | if ( ! isset( $this->edited_product_cats[ $category_id ] ) ) { |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | $category_object = get_term( $category_id, 'product_cat' ); |
| 121 | $prev_parent = $this->edited_product_cats[ $category_id ]; |
| 122 | $new_parent = $category_object->parent; |
| 123 | |
| 124 | // No edits - no need to modify relationships. |
| 125 | if ( $prev_parent === $new_parent ) { |
| 126 | return; |
| 127 | } |
| 128 | |
| 129 | $this->delete( $category_id, $prev_parent ); |
| 130 | $this->update( $category_id ); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * When a product category gets created, add a new lookup row. |
| 135 | * |
| 136 | * @param int $category_id Term ID being created. |
| 137 | */ |
| 138 | public function on_create( $category_id ) { |
| 139 | // If WooCommerce is being installed on a multisite, lookup tables haven't been created yet. |
| 140 | if ( 'yes' === get_transient( 'wc_installing' ) ) { |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | $this->update( $category_id ); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Delete lookup table data from a tree. |
| 149 | * |
| 150 | * @param int $category_id Category ID to delete. |
| 151 | * @param int $category_tree_id Tree to delete from. |
| 152 | * @return void |
| 153 | */ |
| 154 | protected function delete( $category_id, $category_tree_id ) { |
| 155 | global $wpdb; |
| 156 | |
| 157 | if ( ! $category_tree_id ) { |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | $ancestors = get_ancestors( $category_tree_id, 'product_cat', 'taxonomy' ); |
| 162 | $ancestors[] = $category_tree_id; |
| 163 | $children = get_term_children( $category_id, 'product_cat' ); |
| 164 | $children[] = $category_id; |
| 165 | $id_list = implode( ',', array_map( 'intval', array_unique( array_filter( $children ) ) ) ); |
| 166 | |
| 167 | foreach ( $ancestors as $ancestor ) { |
| 168 | $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->wc_category_lookup WHERE category_tree_id = %d AND category_id IN ({$id_list})", $ancestor ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Updates lookup table data for a category by ID. |
| 174 | * |
| 175 | * @param int $category_id Category ID to update. |
| 176 | */ |
| 177 | protected function update( $category_id ) { |
| 178 | global $wpdb; |
| 179 | |
| 180 | $ancestors = get_ancestors( $category_id, 'product_cat', 'taxonomy' ); |
| 181 | $children = get_term_children( $category_id, 'product_cat' ); |
| 182 | $inserts = array(); |
| 183 | $inserts[] = $this->get_insert_sql( $category_id, $category_id ); |
| 184 | $children_ids = array_map( 'intval', array_unique( array_filter( $children ) ) ); |
| 185 | |
| 186 | foreach ( $ancestors as $ancestor ) { |
| 187 | $inserts[] = $this->get_insert_sql( $category_id, $ancestor ); |
| 188 | |
| 189 | foreach ( $children_ids as $child_category_id ) { |
| 190 | $inserts[] = $this->get_insert_sql( $child_category_id, $ancestor ); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | $insert_string = implode( ',', $inserts ); |
| 195 | |
| 196 | $wpdb->query( "INSERT IGNORE INTO $wpdb->wc_category_lookup (category_id, category_tree_id) VALUES {$insert_string}" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Get category lookup table values to insert. |
| 201 | * |
| 202 | * @param int $category_id Category ID to insert. |
| 203 | * @param int $category_tree_id Tree to insert into. |
| 204 | * @return string |
| 205 | */ |
| 206 | protected function get_insert_sql( $category_id, $category_tree_id ) { |
| 207 | global $wpdb; |
| 208 | return $wpdb->prepare( '(%d,%d)', $category_id, $category_tree_id ); |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Used to construct insert query recursively. |
| 213 | * |
| 214 | * @param array $inserts Array of data to insert. |
| 215 | * @param array $terms Terms to insert. |
| 216 | * @param array $parents Parent IDs the terms belong to. |
| 217 | */ |
| 218 | protected function get_term_insert_values( &$inserts, $terms, $parents = array() ) { |
| 219 | foreach ( $terms as $term ) { |
| 220 | $insert_parents = array_merge( array( $term['term_id'] ), $parents ); |
| 221 | |
| 222 | foreach ( $insert_parents as $parent ) { |
| 223 | $inserts[] = array( |
| 224 | $parent, |
| 225 | $term['term_id'], |
| 226 | ); |
| 227 | } |
| 228 | |
| 229 | $this->get_term_insert_values( $inserts, $term['descendants'], $insert_parents ); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Convert flat terms array into nested array. |
| 235 | * |
| 236 | * @param array $hierarchy Array to put terms into. |
| 237 | * @param array $terms Array of terms (id=>parent). |
| 238 | * @param integer $parent Parent ID. |
| 239 | */ |
| 240 | protected function unflatten_terms( &$hierarchy, &$terms, $parent = 0 ) { |
| 241 | foreach ( $terms as $term_id => $parent_id ) { |
| 242 | if ( (int) $parent_id === $parent ) { |
| 243 | $hierarchy[ $term_id ] = array( |
| 244 | 'term_id' => $term_id, |
| 245 | 'descendants' => array(), |
| 246 | ); |
| 247 | unset( $terms[ $term_id ] ); |
| 248 | } |
| 249 | } |
| 250 | foreach ( $hierarchy as $term_id => $terms_array ) { |
| 251 | $this->unflatten_terms( $hierarchy[ $term_id ]['descendants'], $terms, $term_id ); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Get category descendants. |
| 257 | * |
| 258 | * @param int $category_id The category ID to lookup. |
| 259 | * @return array |
| 260 | */ |
| 261 | protected function get_descendants( $category_id ) { |
| 262 | global $wpdb; |
| 263 | |
| 264 | return wp_parse_id_list( |
| 265 | $wpdb->get_col( |
| 266 | $wpdb->prepare( |
| 267 | "SELECT category_id FROM $wpdb->wc_category_lookup WHERE category_tree_id = %d", |
| 268 | $category_id |
| 269 | ) |
| 270 | ) |
| 271 | ); |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Return all ancestor category ids for a category. |
| 276 | * |
| 277 | * @param int $category_id The category ID to lookup. |
| 278 | * @return array |
| 279 | */ |
| 280 | protected function get_ancestors( $category_id ) { |
| 281 | global $wpdb; |
| 282 | |
| 283 | return wp_parse_id_list( |
| 284 | $wpdb->get_col( |
| 285 | $wpdb->prepare( |
| 286 | "SELECT category_tree_id FROM $wpdb->wc_category_lookup WHERE category_id = %d", |
| 287 | $category_id |
| 288 | ) |
| 289 | ) |
| 290 | ); |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * Add category lookup table to $wpdb object. |
| 295 | */ |
| 296 | public static function define_category_lookup_tables_in_wpdb() { |
| 297 | global $wpdb; |
| 298 | |
| 299 | // List of tables without prefixes. |
| 300 | $tables = array( |
| 301 | 'wc_category_lookup' => 'wc_category_lookup', |
| 302 | ); |
| 303 | |
| 304 | foreach ( $tables as $name => $table ) { |
| 305 | $wpdb->$name = $wpdb->prefix . $table; |
| 306 | $wpdb->tables[] = $table; |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 |