ShopperList.php
1 month ago
ShopperListItem.php
2 weeks ago
ShopperListRenderer.php
1 month ago
ShopperListsController.php
1 month ago
ShopperListsController.php
161 lines
| 1 | <?php |
| 2 | declare( strict_types = 1 ); |
| 3 | |
| 4 | namespace Automattic\WooCommerce\Internal\ShopperLists; |
| 5 | |
| 6 | use Automattic\WooCommerce\Internal\Features\FeaturesController; |
| 7 | use Automattic\WooCommerce\Internal\RegisterHooksInterface; |
| 8 | use Automattic\WooCommerce\Utilities\FeaturesUtil; |
| 9 | |
| 10 | /** |
| 11 | * Tracks which shopper-list types are turned on and registers the |
| 12 | * user-facing pieces that depend on each. |
| 13 | * |
| 14 | * @internal Just for internal use. |
| 15 | */ |
| 16 | final class ShopperListsController implements RegisterHooksInterface { |
| 17 | |
| 18 | /** |
| 19 | * Known list slugs and the feature flag that controls each. |
| 20 | */ |
| 21 | private const SUPPORTED_LISTS = array( |
| 22 | 'saved-for-later' => 'cart_save_for_later', |
| 23 | 'wishlist' => 'product_wishlist', |
| 24 | ); |
| 25 | |
| 26 | /** |
| 27 | * Wishlist My Account endpoint slug. Wrapped in a method (rather than |
| 28 | * a constant) so a future filter or settings hook can override it |
| 29 | * without touching every call site. |
| 30 | */ |
| 31 | public function get_wishlist_endpoint(): string { |
| 32 | return 'wishlist'; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Whether a given list type is on, or whether any list type is on |
| 37 | * when no slug is passed. |
| 38 | * |
| 39 | * @param string|null $list_slug List slug, or null to ask about any type. |
| 40 | */ |
| 41 | public function is_enabled( ?string $list_slug = null ): bool { |
| 42 | if ( null === $list_slug ) { |
| 43 | foreach ( self::SUPPORTED_LISTS as $feature ) { |
| 44 | if ( FeaturesUtil::feature_is_enabled( $feature ) ) { |
| 45 | return true; |
| 46 | } |
| 47 | } |
| 48 | return false; |
| 49 | } |
| 50 | $feature = self::SUPPORTED_LISTS[ $list_slug ] ?? null; |
| 51 | return null !== $feature && FeaturesUtil::feature_is_enabled( $feature ); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Slugs of all currently-enabled lists, in declaration order. |
| 56 | * |
| 57 | * @return string[] |
| 58 | */ |
| 59 | public function get_enabled_slugs(): array { |
| 60 | return array_keys( |
| 61 | array_filter( |
| 62 | self::SUPPORTED_LISTS, |
| 63 | static fn( string $feature ): bool => FeaturesUtil::feature_is_enabled( $feature ) |
| 64 | ) |
| 65 | ); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Register hooks. |
| 70 | */ |
| 71 | public function register(): void { |
| 72 | add_action( FeaturesController::FEATURE_ENABLED_CHANGED_ACTION, array( $this, 'maybe_flush_rewrite_rules' ), 10, 1 ); |
| 73 | add_action( 'init', array( $this, 'maybe_register_wishlist_endpoint' ), 5 ); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Register the wishlist endpoint. |
| 78 | */ |
| 79 | public function maybe_register_wishlist_endpoint(): void { |
| 80 | if ( ! $this->is_enabled( 'wishlist' ) ) { |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | $endpoint = $this->get_wishlist_endpoint(); |
| 85 | add_filter( 'woocommerce_get_query_vars', array( $this, 'add_wishlist_query_var' ) ); |
| 86 | add_filter( 'woocommerce_account_menu_items', array( $this, 'add_wishlist_menu_item' ) ); |
| 87 | add_filter( 'woocommerce_endpoint_' . $endpoint . '_title', array( $this, 'wishlist_endpoint_title' ) ); |
| 88 | add_action( 'woocommerce_account_' . $endpoint . '_endpoint', array( $this, 'render_wishlist_endpoint' ) ); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Flush rewrite rules when the wishlist feature is turned on or off. |
| 93 | * |
| 94 | * @param string $feature_id The feature that changed. |
| 95 | */ |
| 96 | public function maybe_flush_rewrite_rules( string $feature_id ): void { |
| 97 | if ( 'product_wishlist' === $feature_id ) { |
| 98 | update_option( 'woocommerce_queue_flush_rewrite_rules', 'yes' ); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Register the `wishlist` query var. |
| 104 | * |
| 105 | * @param array $vars Existing query vars keyed by slug. |
| 106 | */ |
| 107 | public function add_wishlist_query_var( $vars ): array { |
| 108 | if ( ! is_array( $vars ) ) { |
| 109 | return array(); |
| 110 | } |
| 111 | |
| 112 | $endpoint = $this->get_wishlist_endpoint(); |
| 113 | $vars[ $endpoint ] = $endpoint; |
| 114 | return $vars; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Insert the Wishlist link just before the logout link. |
| 119 | * |
| 120 | * @param array $items Existing menu items keyed by slug. |
| 121 | */ |
| 122 | public function add_wishlist_menu_item( $items ): array { |
| 123 | if ( ! is_array( $items ) ) { |
| 124 | return array(); |
| 125 | } |
| 126 | |
| 127 | $wishlist_endpoint = $this->get_wishlist_endpoint(); |
| 128 | $wishlist_label = __( 'Wishlist', 'woocommerce' ); |
| 129 | |
| 130 | // Insert the wishlist item before the logout item, or at the end if not present. |
| 131 | $logout_pos = array_search( 'customer-logout', array_keys( $items ), true ); |
| 132 | if ( false === $logout_pos ) { |
| 133 | $items[ $wishlist_endpoint ] = $wishlist_label; |
| 134 | } else { |
| 135 | $items = array_slice( $items, 0, $logout_pos, true ) |
| 136 | + array( $wishlist_endpoint => $wishlist_label ) |
| 137 | + array_slice( $items, $logout_pos, null, true ); |
| 138 | } |
| 139 | return $items; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Wishlist endpoint page title. |
| 144 | * |
| 145 | * @param string $title Default title. |
| 146 | */ |
| 147 | public function wishlist_endpoint_title( $title ): string { |
| 148 | return __( 'Wishlist', 'woocommerce' ); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Render the wishlist endpoint by dispatching to the |
| 153 | * `woocommerce/wishlist` block. The block handles the empty state, |
| 154 | * logged-out guard, asset enqueues, and item rendering. |
| 155 | */ |
| 156 | public function render_wishlist_endpoint(): void { |
| 157 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- the block string is a static literal; `do_blocks()` invokes the registered block's render callback, which is responsible for its own escaping. |
| 158 | echo do_blocks( '<!-- wp:woocommerce/wishlist /-->' ); |
| 159 | } |
| 160 | } |
| 161 |