DataStore
1 month ago
Providers
3 months ago
Fulfillment.php
1 month ago
FulfillmentException.php
3 months ago
FulfillmentOrderNotes.php
3 months ago
FulfillmentUtils.php
2 months ago
FulfillmentsController.php
3 months ago
FulfillmentsManager.php
2 months ago
FulfillmentsRenderer.php
1 month ago
FulfillmentsSettings.php
3 months ago
FulfillmentsTracker.php
3 months ago
OrderFulfillmentsRestController.php
1 month ago
ShippingProviders.php
3 months ago
FulfillmentsController.php
264 lines
| 1 | <?php declare(strict_types=1); |
| 2 | |
| 3 | namespace Automattic\WooCommerce\Admin\Features\Fulfillments; |
| 4 | |
| 5 | use Automattic\WooCommerce\Admin\Features\Fulfillments\DataStore\FulfillmentsDataStore; |
| 6 | use Automattic\WooCommerce\Internal\Features\FeaturesController; |
| 7 | use Automattic\WooCommerce\Internal\Utilities\DatabaseUtil; |
| 8 | |
| 9 | /** |
| 10 | * Class FulfillmentsController |
| 11 | * |
| 12 | * Base controller for fulfillments management. |
| 13 | */ |
| 14 | class FulfillmentsController { |
| 15 | /** |
| 16 | * Provides the list of classes that this controller provides. |
| 17 | * |
| 18 | * @var string[] |
| 19 | */ |
| 20 | private $provides = array( |
| 21 | FulfillmentsManager::class, |
| 22 | FulfillmentsRenderer::class, |
| 23 | FulfillmentsSettings::class, |
| 24 | OrderFulfillmentsRestController::class, |
| 25 | ); |
| 26 | |
| 27 | /** |
| 28 | * Initialize the controller. |
| 29 | * |
| 30 | * @return void |
| 31 | */ |
| 32 | public function register() { |
| 33 | add_filter( 'woocommerce_data_stores', array( $this, 'register_data_stores' ) ); |
| 34 | add_action( 'init', array( $this, 'initialize_fulfillments' ), 10, 0 ); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Register the fulfillments data store via the woocommerce_data_stores filter. |
| 39 | * |
| 40 | * This allows extensions to replace the data store with a custom implementation |
| 41 | * by filtering 'woocommerce_data_stores' or 'woocommerce_order-fulfillment_data_store'. |
| 42 | * |
| 43 | * @param array $data_stores Data stores. |
| 44 | * @return array |
| 45 | */ |
| 46 | public function register_data_stores( $data_stores ) { |
| 47 | if ( ! is_array( $data_stores ) ) { |
| 48 | return $data_stores; |
| 49 | } |
| 50 | |
| 51 | // Check the option directly instead of using FeaturesController::feature_is_enabled() |
| 52 | // because the woocommerce_data_stores filter can fire before the 'init' action |
| 53 | // (e.g. from WC Payments during 'plugins_loaded'), and feature_is_enabled() would |
| 54 | // trigger translation loading too early, causing _load_textdomain_just_in_time warnings. |
| 55 | if ( 'yes' !== get_option( 'woocommerce_feature_fulfillments_enabled', 'no' ) ) { |
| 56 | return $data_stores; |
| 57 | } |
| 58 | |
| 59 | $data_stores['order-fulfillment'] = FulfillmentsDataStore::class; |
| 60 | return $data_stores; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Initialize the fulfillments controller. |
| 65 | */ |
| 66 | public function initialize_fulfillments() { |
| 67 | $container = wc_get_container(); |
| 68 | $features_controller = $container->get( FeaturesController::class ); |
| 69 | |
| 70 | // If fulfillments feature is not enabled, do not add the DB tables, and don't register the controller. |
| 71 | if ( ! $features_controller->feature_is_enabled( 'fulfillments' ) ) { |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | // Create the database tables if they do not exist. |
| 76 | $this->maybe_create_db_tables(); |
| 77 | |
| 78 | // Register the custom shipping providers taxonomy. |
| 79 | $this->register_custom_shipping_providers_taxonomy(); |
| 80 | |
| 81 | // Register the classes that this controller provides. |
| 82 | foreach ( $this->provides as $class ) { |
| 83 | $class = $container->get( $class ); |
| 84 | if ( method_exists( $class, 'register' ) ) { |
| 85 | $class->register(); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Register the custom shipping providers taxonomy. |
| 92 | */ |
| 93 | private function register_custom_shipping_providers_taxonomy(): void { |
| 94 | if ( taxonomy_exists( 'wc_fulfillment_shipping_provider' ) ) { |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | register_taxonomy( |
| 99 | 'wc_fulfillment_shipping_provider', |
| 100 | array(), |
| 101 | array( |
| 102 | 'labels' => array( |
| 103 | 'name' => __( 'Shipping providers', 'woocommerce' ), |
| 104 | 'singular_name' => __( 'Shipping provider', 'woocommerce' ), |
| 105 | 'add_new_item' => __( 'Add new shipping provider', 'woocommerce' ), |
| 106 | 'edit_item' => __( 'Edit shipping provider', 'woocommerce' ), |
| 107 | ), |
| 108 | 'public' => false, |
| 109 | 'show_ui' => false, |
| 110 | 'hierarchical' => false, |
| 111 | 'show_in_rest' => false, |
| 112 | 'show_admin_column' => false, // phpcs:ignore WordPress.Arrays.MultipleStatementAlignment.DoubleArrowNotAligned |
| 113 | 'show_in_nav_menus' => false, // phpcs:ignore WordPress.Arrays.MultipleStatementAlignment.DoubleArrowNotAligned |
| 114 | 'show_tagcloud' => false, |
| 115 | 'query_var' => false, |
| 116 | 'rewrite' => false, |
| 117 | ) |
| 118 | ); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Create the database tables if they do not exist. |
| 123 | * |
| 124 | * @return void |
| 125 | */ |
| 126 | private function maybe_create_db_tables(): void { |
| 127 | global $wpdb; |
| 128 | |
| 129 | if ( get_option( 'woocommerce_fulfillments_db_tables_created', false ) ) { |
| 130 | // Verify the tables actually exist (the option may be stale after DB resets in tests). |
| 131 | $table_exists = $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}wc_order_fulfillments'" ); |
| 132 | if ( $table_exists ) { |
| 133 | return; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | // Drop the tables if they exist, to ensure a clean slate. |
| 138 | // If one table exists and the other does not, it will be an issue. |
| 139 | $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}wc_order_fulfillments" ); |
| 140 | $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}wc_order_fulfillment_meta" ); |
| 141 | |
| 142 | // Bulk delete order fulfillment status meta from legacy and HPOS order tables. |
| 143 | $this->bulk_delete_order_fulfillment_status_meta(); |
| 144 | |
| 145 | $collate = ''; |
| 146 | $container = wc_get_container(); |
| 147 | $database_util = $container->get( DatabaseUtil::class ); |
| 148 | |
| 149 | $max_index_length = $database_util->get_max_index_length(); |
| 150 | if ( $wpdb->has_cap( 'collation' ) ) { |
| 151 | $collate = $wpdb->get_charset_collate(); |
| 152 | } |
| 153 | |
| 154 | $schema = "CREATE TABLE {$wpdb->prefix}wc_order_fulfillments ( |
| 155 | fulfillment_id bigint(20) unsigned NOT NULL AUTO_INCREMENT, |
| 156 | entity_type varchar(255) NOT NULL, |
| 157 | entity_id bigint(20) unsigned NOT NULL, |
| 158 | status varchar(255) NOT NULL, |
| 159 | is_fulfilled tinyint(1) NOT NULL DEFAULT 0, |
| 160 | date_updated datetime NOT NULL, |
| 161 | date_deleted datetime NULL, |
| 162 | PRIMARY KEY (fulfillment_id), |
| 163 | KEY entity_type_id (entity_type({$max_index_length}), entity_id) |
| 164 | ) $collate; |
| 165 | CREATE TABLE {$wpdb->prefix}wc_order_fulfillment_meta ( |
| 166 | meta_id bigint(20) unsigned NOT NULL AUTO_INCREMENT, |
| 167 | fulfillment_id bigint(20) unsigned NOT NULL, |
| 168 | meta_key varchar(255) NULL, |
| 169 | meta_value longtext NULL, |
| 170 | date_updated datetime NOT NULL, |
| 171 | date_deleted datetime NULL, |
| 172 | PRIMARY KEY (meta_id), |
| 173 | KEY meta_key (meta_key({$max_index_length})), |
| 174 | KEY fulfillment_id (fulfillment_id) |
| 175 | ) $collate;"; |
| 176 | |
| 177 | $database_util->dbdelta( $schema ); |
| 178 | |
| 179 | // Update the option to indicate that the tables have been created. |
| 180 | update_option( 'woocommerce_fulfillments_db_tables_created', true ); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Bulk delete fulfillment status meta for specific order IDs, or all orders if no order ID specified. |
| 185 | * |
| 186 | * This method deletes the fulfillment status meta for the specified order IDs from both the legacy postmeta table |
| 187 | * and the HPOS meta table. |
| 188 | * |
| 189 | * @param array<int> $order_ids Array of order IDs to delete fulfillment status meta for. |
| 190 | */ |
| 191 | private function bulk_delete_order_fulfillment_status_meta( $order_ids = array() ): void { |
| 192 | $this->delete_legacy_order_fulfillment_meta( $order_ids ); |
| 193 | $this->delete_hpos_order_fulfillment_meta( $order_ids ); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Delete fulfillment status meta from legacy postmeta table. |
| 198 | * |
| 199 | * @param array<int> $order_ids Array of order IDs to delete fulfillment status meta for. |
| 200 | */ |
| 201 | private function delete_legacy_order_fulfillment_meta( $order_ids = array() ) { |
| 202 | global $wpdb; |
| 203 | |
| 204 | if ( ! empty( $order_ids ) ) { |
| 205 | $order_params = array_merge( array( '_fulfillment_status' ), $order_ids ); |
| 206 | $wpdb->query( |
| 207 | $wpdb->prepare( |
| 208 | "DELETE pm FROM {$wpdb->postmeta} pm |
| 209 | INNER JOIN {$wpdb->posts} p ON pm.post_id = p.ID |
| 210 | WHERE p.post_type = 'shop_order' |
| 211 | AND pm.meta_key = %s |
| 212 | AND pm.post_id IN (" . implode( ',', array_fill( 0, count( $order_ids ), '%d' ) ) . ')', |
| 213 | ...$order_params |
| 214 | ) |
| 215 | ); |
| 216 | } else { |
| 217 | $wpdb->query( |
| 218 | $wpdb->prepare( |
| 219 | "DELETE pm FROM {$wpdb->postmeta} pm |
| 220 | INNER JOIN {$wpdb->posts} p ON pm.post_id = p.ID |
| 221 | WHERE p.post_type = 'shop_order' |
| 222 | AND pm.meta_key = %s", |
| 223 | '_fulfillment_status' |
| 224 | ) |
| 225 | ); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Delete fulfillment status meta from HPOS meta table. |
| 231 | * |
| 232 | * @param array<int> $order_ids Array of order IDs to delete fulfillment status meta for. |
| 233 | */ |
| 234 | private function delete_hpos_order_fulfillment_meta( $order_ids = array() ): void { |
| 235 | global $wpdb; |
| 236 | |
| 237 | // Check if HPOS meta table exists. |
| 238 | $table_name = $wpdb->prefix . 'wc_orders_meta'; |
| 239 | if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table_name ) ) !== $table_name ) { |
| 240 | return; |
| 241 | } |
| 242 | |
| 243 | if ( ! empty( $order_ids ) ) { |
| 244 | $order_params = array_merge( array( '_fulfillment_status' ), $order_ids ); |
| 245 | $wpdb->query( |
| 246 | $wpdb->prepare( |
| 247 | "DELETE FROM {$wpdb->prefix}wc_orders_meta |
| 248 | WHERE meta_key = %s |
| 249 | AND order_id IN (" . implode( ',', array_fill( 0, count( $order_ids ), '%d' ) ) . ')', |
| 250 | ...$order_params |
| 251 | ) |
| 252 | ); |
| 253 | } else { |
| 254 | $wpdb->query( |
| 255 | $wpdb->prepare( |
| 256 | "DELETE FROM {$wpdb->prefix}wc_orders_meta |
| 257 | WHERE meta_key = %s", |
| 258 | '_fulfillment_status' |
| 259 | ) |
| 260 | ); |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 |