| @@ -6,10 +6,8 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace SpringDevs\Subscription\Illuminate\Plans; |
| 9 | 9 | |
| 10 | -use SpringDevs\Subscription\Illuminate\Multilingual; | |
| 11 | - | |
| 12 | 10 | /** |
| 13 | 11 | * Plan data-access layer. |
| 14 | 12 | * |
| 15 | 13 | * Read/write repository over the three plan tables (subscrpt_plan_group, |
| @@ -154,36 +152,30 @@ | ||
| 154 | 152 | if ( ! $product_id ) { |
| 155 | 153 | return array(); |
| 156 | 154 | } |
| 157 | 155 | |
| 158 | - // A translation is a different post from the product its plans were | |
| 159 | - // attached to, so read across every language of it. One cache entry | |
| 160 | - // serves them all. Without WPML / Polylang this is just the product. | |
| 161 | - $product_ids = Multilingual::translation_ids( $product_id ); | |
| 162 | - $cache_key = 'product_' . Multilingual::canonical_id( $product_id ); | |
| 163 | - $resolved = wp_cache_get( $cache_key, self::CACHE_GROUP ); | |
| 156 | + $cache_key = 'product_' . $product_id; | |
| 157 | + $resolved = wp_cache_get( $cache_key, self::CACHE_GROUP ); | |
| 164 | 158 | |
| 165 | 159 | if ( false === $resolved ) { |
| 166 | - $batch = self::resolve_for_products( $product_ids ); | |
| 167 | - $resolved = count( $product_ids ) > 1 ? self::merge_translation_rows( $batch ) : ( $batch[ $product_id ] ?? array() ); | |
| 160 | + $batch = self::resolve_for_products( array( $product_id ) ); | |
| 161 | + $resolved = $batch[ $product_id ] ?? array(); | |
| 168 | 162 | wp_cache_set( $cache_key, $resolved, self::CACHE_GROUP ); |
| 169 | 163 | } |
| 170 | 164 | |
| 171 | 165 | if ( $variation_id ) { |
| 172 | - $variation_ids = Multilingual::translation_ids( $variation_id ); | |
| 173 | - $resolved = array_values( | |
| 166 | + $variation_id = absint( $variation_id ); | |
| 167 | + $resolved = array_values( | |
| 174 | 168 | array_filter( |
| 175 | 169 | $resolved, |
| 176 | - static function ( $row ) use ( $variation_ids ) { | |
| 170 | + static function ( $row ) use ( $variation_id ) { | |
| 177 | 171 | // vid 0 = applies to the parent / all variations. |
| 178 | - return 0 === (int) $row['vid'] || in_array( (int) $row['vid'], $variation_ids, true ); | |
| 172 | + return 0 === (int) $row['vid'] || $variation_id === (int) $row['vid']; | |
| 179 | 173 | } |
| 180 | 174 | ) |
| 181 | 175 | ); |
| 182 | 176 | } |
| 183 | 177 | |
| 184 | - $resolved = Multilingual::translate_plan_rows( $resolved ); | |
| 185 | - | |
| 186 | 178 | /** |
| 187 | 179 | * Filter the resolved plan rows for a product. |
| 188 | 180 | * |
| 189 | 181 | * Pro's extension point to inject taxonomy- and variation-resolved rows. |
| @@ -271,42 +263,8 @@ | ||
| 271 | 263 | return $map; |
| 272 | 264 | } |
| 273 | 265 | |
| 274 | 266 | /** |
| 275 | - * Merge the plan rows of every language of one product into one list. | |
| 276 | - * | |
| 277 | - * A plan attached in two languages would otherwise be offered twice, so a | |
| 278 | - * plan appears once per variation it applies to, in group → plan order. | |
| 279 | - * | |
| 280 | - * @param array<int, array> $batch Map of product id → rows, from {@see resolve_for_products()}. | |
| 281 | - * | |
| 282 | - * @return array | |
| 283 | - */ | |
| 284 | - private static function merge_translation_rows( array $batch ) { | |
| 285 | - $merged = array(); | |
| 286 | - | |
| 287 | - foreach ( $batch as $rows ) { | |
| 288 | - foreach ( $rows as $row ) { | |
| 289 | - $vid = (int) $row['vid'] ? Multilingual::canonical_id( $row['vid'] ) : 0; | |
| 290 | - $key = (int) $row['plan_id'] . ':' . $vid; | |
| 291 | - | |
| 292 | - if ( ! isset( $merged[ $key ] ) ) { | |
| 293 | - $merged[ $key ] = $row; | |
| 294 | - } | |
| 295 | - } | |
| 296 | - } | |
| 297 | - | |
| 298 | - usort( | |
| 299 | - $merged, | |
| 300 | - static function ( $a, $b ) { | |
| 301 | - return array( (int) $a['plan_group_id'], (int) $a['plan_id'] ) <=> array( (int) $b['plan_group_id'], (int) $b['plan_id'] ); | |
| 302 | - } | |
| 303 | - ); | |
| 304 | - | |
| 305 | - return $merged; | |
| 306 | - } | |
| 307 | - | |
| 308 | - /** | |
| 309 | 267 | * Decode a JSON column to an array, tolerating null / plain strings. |
| 310 | 268 | * |
| 311 | 269 | * @param string|null $value Raw column value. |
| 312 | 270 | * |
| @@ -348,17 +306,14 @@ | ||
| 348 | 306 | $relation_table = self::relation_table(); |
| 349 | 307 | $plan_table = self::plan_table(); |
| 350 | 308 | $group_table = self::group_table(); |
| 351 | 309 | |
| 352 | - // Every language of the product, as the storefront resolver reads them. | |
| 353 | - $product_ids = Multilingual::translation_ids( $product_id ); | |
| 354 | - $where = 'r.type = %d AND r.oid IN (' . implode( ',', array_fill( 0, count( $product_ids ), '%d' ) ) . ')'; | |
| 355 | - $params = array_merge( array( self::REL_PRODUCT ), $product_ids ); | |
| 310 | + $where = 'r.type = %d AND r.oid = %d'; | |
| 311 | + $params = array( self::REL_PRODUCT, $product_id ); | |
| 356 | 312 | |
| 357 | 313 | if ( $variation_id ) { |
| 358 | - $variation_ids = Multilingual::translation_ids( $variation_id ); | |
| 359 | - $where .= ' AND r.vid IN (' . implode( ',', array_fill( 0, count( $variation_ids ), '%d' ) ) . ')'; | |
| 360 | - $params = array_merge( $params, $variation_ids ); | |
| 314 | + $where .= ' AND r.vid = %d'; | |
| 315 | + $params[] = absint( $variation_id ); | |
| 361 | 316 | } |
| 362 | 317 | |
| 363 | 318 | // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare |
| 364 | 319 | $rows = $wpdb->get_results( |
| @@ -464,30 +419,8 @@ | ||
| 464 | 419 | return $row ? self::decode_plan_row( $row ) : null; |
| 465 | 420 | } |
| 466 | 421 | |
| 467 | 422 | /** |
| 468 | - * Every non-empty group and plan title, any status, for string translation. | |
| 469 | - * | |
| 470 | - * @return array{groups:array<int,string>,plans:array<int,string>} Titles by id. | |
| 471 | - */ | |
| 472 | - public static function get_titles() { | |
| 473 | - global $wpdb; | |
| 474 | - | |
| 475 | - $group_table = self::group_table(); | |
| 476 | - $plan_table = self::plan_table(); | |
| 477 | - | |
| 478 | - // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery | |
| 479 | - $groups = $wpdb->get_results( "SELECT id, title FROM {$group_table} WHERE title <> '' ORDER BY id ASC", ARRAY_A ); | |
| 480 | - $plans = $wpdb->get_results( "SELECT id, title FROM {$plan_table} WHERE title <> '' ORDER BY id ASC", ARRAY_A ); | |
| 481 | - // phpcs:enable | |
| 482 | - | |
| 483 | - return array( | |
| 484 | - 'groups' => array_column( (array) $groups, 'title', 'id' ), | |
| 485 | - 'plans' => array_column( (array) $plans, 'title', 'id' ), | |
| 486 | - ); | |
| 487 | - } | |
| 488 | - | |
| 489 | - /** | |
| 490 | 423 | * Fetch relations for a plan term. |
| 491 | 424 | * |
| 492 | 425 | * @param int $plan_id Plan id. |
| 493 | 426 | * |
| @@ -1090,10 +1023,8 @@ | ||
| 1090 | 1023 | $product_id = absint( $product_id ); |
| 1091 | 1024 | |
| 1092 | 1025 | if ( $product_id ) { |
| 1093 | 1026 | wp_cache_delete( 'product_' . $product_id, self::CACHE_GROUP ); |
| 1094 | - // The key every translation of the product shares, when that is another id. | |
| 1095 | - wp_cache_delete( 'product_' . Multilingual::canonical_id( $product_id ), self::CACHE_GROUP ); | |
| 1096 | 1027 | return; |
| 1097 | 1028 | } |
| 1098 | 1029 | |
| 1099 | 1030 | // No targeted product: drop the whole group when the backend supports it. |