CardIcons
1 year ago
CouponsController.php
7 months ago
IppFunctions.php
1 year ago
MobileMessagingHandler.php
2 years ago
OrderActionsRestController.php
3 months ago
OrderAttributionBlocksController.php
1 year ago
OrderAttributionController.php
5 months ago
OrderNoteGroup.php
3 months ago
OrderStatusRestController.php
5 months ago
PaymentInfo.php
10 months ago
PointOfSaleEmailHandler.php
3 months ago
PointOfSaleOrderUtil.php
4 weeks ago
TaxesController.php
3 years ago
OrderAttributionBlocksController.php
177 lines
| 1 | <?php |
| 2 | declare( strict_types=1 ); |
| 3 | |
| 4 | namespace Automattic\WooCommerce\Internal\Orders; |
| 5 | |
| 6 | use Automattic\Jetpack\Constants; |
| 7 | use Automattic\WooCommerce\Internal\Features\FeaturesController; |
| 8 | use Automattic\WooCommerce\Internal\RegisterHooksInterface; |
| 9 | use Automattic\WooCommerce\StoreApi\Schemas\ExtendSchema; |
| 10 | use Automattic\WooCommerce\StoreApi\Schemas\V1\CheckoutSchema; |
| 11 | use Automattic\WooCommerce\Internal\Traits\ScriptDebug; |
| 12 | use WP_Error; |
| 13 | |
| 14 | /** |
| 15 | * Class OrderAttributionBlocksController |
| 16 | * |
| 17 | * @since 8.5.0 |
| 18 | */ |
| 19 | class OrderAttributionBlocksController implements RegisterHooksInterface { |
| 20 | |
| 21 | use ScriptDebug; |
| 22 | |
| 23 | /** |
| 24 | * Instance of the features controller. |
| 25 | * |
| 26 | * @var FeaturesController |
| 27 | */ |
| 28 | private $features_controller; |
| 29 | |
| 30 | /** |
| 31 | * ExtendSchema instance. |
| 32 | * |
| 33 | * @var ExtendSchema |
| 34 | */ |
| 35 | private $extend_schema; |
| 36 | |
| 37 | /** |
| 38 | * Instance of the order attribution controller. |
| 39 | * |
| 40 | * @var OrderAttributionController |
| 41 | */ |
| 42 | private $order_attribution_controller; |
| 43 | |
| 44 | /** |
| 45 | * Bind dependencies on init. |
| 46 | * |
| 47 | * @internal |
| 48 | * |
| 49 | * @param ExtendSchema $extend_schema ExtendSchema instance. |
| 50 | * @param FeaturesController $features_controller Features controller. |
| 51 | * @param OrderAttributionController $order_attribution_controller Instance of the order attribution controller. |
| 52 | */ |
| 53 | final public function init( |
| 54 | ExtendSchema $extend_schema, |
| 55 | FeaturesController $features_controller, |
| 56 | OrderAttributionController $order_attribution_controller |
| 57 | ) { |
| 58 | $this->extend_schema = $extend_schema; |
| 59 | $this->features_controller = $features_controller; |
| 60 | $this->order_attribution_controller = $order_attribution_controller; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Register this class instance to the appropriate hooks. |
| 65 | * |
| 66 | * @return void |
| 67 | */ |
| 68 | public function register() { |
| 69 | add_action( 'init', array( $this, 'on_init' ) ); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Hook into WordPress on init. |
| 74 | */ |
| 75 | public function on_init() { |
| 76 | // Bail if the feature is not enabled. |
| 77 | if ( ! $this->features_controller->feature_is_enabled( 'order_attribution' ) ) { |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | $this->extend_api(); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Extend the Store API. |
| 86 | * |
| 87 | * @return void |
| 88 | */ |
| 89 | private function extend_api() { |
| 90 | $this->extend_schema->register_endpoint_data( |
| 91 | array( |
| 92 | 'endpoint' => CheckoutSchema::IDENTIFIER, |
| 93 | 'namespace' => 'woocommerce/order-attribution', |
| 94 | 'schema_callback' => $this->get_schema_callback(), |
| 95 | ) |
| 96 | ); |
| 97 | // Update order based on extended data. |
| 98 | add_action( |
| 99 | 'woocommerce_store_api_checkout_update_order_from_request', |
| 100 | function ( $order, $request ) { |
| 101 | $extensions = $request->get_param( 'extensions' ); |
| 102 | $params = $extensions['woocommerce/order-attribution'] ?? array(); |
| 103 | |
| 104 | if ( empty( $params ) ) { |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | // Check if this order already has any attribution data to prevent duplicates attribution data. |
| 109 | if ( $this->order_attribution_controller->has_attribution( $order ) ) { |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Run an action to save order attribution data. |
| 115 | * |
| 116 | * @since 8.5.0 |
| 117 | * |
| 118 | * @param WC_Order $order The order object. |
| 119 | * @param array $params Unprefixed order attribution data. |
| 120 | */ |
| 121 | do_action( 'woocommerce_order_save_attribution_data', $order, $params ); |
| 122 | }, |
| 123 | 10, |
| 124 | 2 |
| 125 | ); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Get the schema callback. |
| 130 | * |
| 131 | * @return callable |
| 132 | */ |
| 133 | private function get_schema_callback() { |
| 134 | return function() { |
| 135 | $schema = array(); |
| 136 | $field_names = $this->order_attribution_controller->get_field_names(); |
| 137 | |
| 138 | $validate_callback = function( $value ) { |
| 139 | if ( ! is_string( $value ) && null !== $value ) { |
| 140 | return new WP_Error( |
| 141 | 'api-error', |
| 142 | sprintf( |
| 143 | /* translators: %s is the property type */ |
| 144 | esc_html__( 'Value of type %s was posted to the order attribution callback', 'woocommerce' ), |
| 145 | gettype( $value ) |
| 146 | ) |
| 147 | ); |
| 148 | } |
| 149 | |
| 150 | return true; |
| 151 | }; |
| 152 | |
| 153 | $sanitize_callback = function( $value ) { |
| 154 | return sanitize_text_field( $value ); |
| 155 | }; |
| 156 | |
| 157 | foreach ( $field_names as $field_name ) { |
| 158 | $schema[ $field_name ] = array( |
| 159 | 'description' => sprintf( |
| 160 | /* translators: %s is the field name */ |
| 161 | __( 'Order attribution field: %s', 'woocommerce' ), |
| 162 | esc_html( $field_name ) |
| 163 | ), |
| 164 | 'type' => array( 'string', 'null' ), |
| 165 | 'context' => array(), |
| 166 | 'arg_options' => array( |
| 167 | 'validate_callback' => $validate_callback, |
| 168 | 'sanitize_callback' => $sanitize_callback, |
| 169 | ), |
| 170 | ); |
| 171 | } |
| 172 | |
| 173 | return $schema; |
| 174 | }; |
| 175 | } |
| 176 | } |
| 177 |