first commit

This commit is contained in:
2024-07-15 11:28:08 +02:00
commit f52d538ea5
21891 changed files with 6161164 additions and 0 deletions

View File

@@ -0,0 +1,72 @@
<?php
/**
* Default data provider. Can get data from shipment.
*/
class WPDesk_Flexible_Shipping_Rest_Api_Order_Data_Provider_Default implements WPDesk_Flexible_Shipping_Rest_Api_Order_Data_Provider {
const COMMON_KEYS_TO_REMOVE = array(
'_fs_method',
'_shipping_method',
'_package',
'_packages',
);
/**
* Keys to remove.
*
* @var array
*/
protected $keys_to_remove = array();
/**
* Get data from shipment.
*
* @param WPDesk_Flexible_Shipping_Shipment $shipment .
*
* @return array
*/
public function get_data_from_shipment( WPDesk_Flexible_Shipping_Shipment $shipment ) {
return $this->remove_internal_data_from_shipment_data( get_post_meta( $shipment->get_id() ) );
}
/**
* Filter data.
*
* @param array $data .
*
* @return array
*/
protected function remove_internal_data_from_shipment_data( array $data ) {
$keys_to_remove = array_merge( self::COMMON_KEYS_TO_REMOVE, $this->keys_to_remove );
foreach ( $keys_to_remove as $key ) {
if ( isset( $data[ $key ] ) ) {
unset( $data[ $key ] );
}
}
return $this->format_data( $data );
}
/**
* Format data.
*
* @param array $data .
*
* @return array
*/
private function format_data( array $data ) {
$formatted_data = array();
foreach ( $data as $key => $value ) {
if ( is_array( $value ) && isset( $value[0] ) ) {
$formatted_data[ $key ] = $value[0];
} else {
$formatted_data[ $key ] = $value;
}
}
return $formatted_data;
}
}

View File

@@ -0,0 +1,40 @@
<?php
/**
* Data providers.
* Collects data providers and can return provider per integration or default provider.
*/
class WPDesk_Flexible_Shipping_Rest_Api_Order_Data_Providers_Collection {
/**
* Providers.
*
* @var WPDesk_Flexible_Shipping_Rest_Api_Order_Data_Provider[]
*/
private $providers = array();
/**
* Add provider.
*
* @param string $integration .
* @param WPDesk_Flexible_Shipping_Rest_Api_Order_Data_Provider $provider .
*/
public function set_provider( $integration, WPDesk_Flexible_Shipping_Rest_Api_Order_Data_Provider $provider ) {
$this->providers[ $integration ] = $provider;
}
/**
* Get provider for integration.
*
* @param string $integration .
*
* @return WPDesk_Flexible_Shipping_Rest_Api_Order_Data_Provider
*/
public function get_provider_for_integration( $integration ) {
if ( isset( $this->providers[ $integration ] ) ) {
return $this->providers[ $integration ];
}
return new WPDesk_Flexible_Shipping_Rest_Api_Order_Data_Provider_Default();
}
}

View File

@@ -0,0 +1,27 @@
<?php
/**
* Data providers factory.
*/
class WPDesk_Flexible_Shipping_Rest_Api_Order_Data_Providers_Factory {
/**
* Providers.
*
* @var WPDesk_Flexible_Shipping_Rest_Api_Order_Data_Providers_Collection
*/
private static $data_providers = null;
/**
* Get data providers.
*
* @return WPDesk_Flexible_Shipping_Rest_Api_Order_Data_Providers_Collection
*/
public static function get_providers() {
if ( empty( self::$data_providers ) ) {
self::$data_providers = new WPDesk_Flexible_Shipping_Rest_Api_Order_Data_Providers_Collection();
}
return self::$data_providers;
}
}

View File

@@ -0,0 +1,69 @@
<?php
use FSVendor\WPDesk\PluginBuilder\Plugin\Hookable;
/**
* Can append shipments data to WooCommerce REST API Order response.
*/
class WPDesk_Flexible_Shipping_Rest_Api_Order_Response_Data_Appender implements Hookable {
const REST_API_DATA_KEY = 'fs_shipping_lines';
/**
* Hooks.
*/
public function hooks() {
add_filter( 'woocommerce_rest_prepare_shop_order_object', array( $this, 'maybe_append_shipment_to_order_data' ), 10, 3 );
}
/**
* Appends shipment data if exists to order in REST API response.
*
* @param WP_REST_Response $response .
* @param WC_Order $order .
* @param WP_REST_Request $request .
*
* @return WP_REST_Response
*/
public function maybe_append_shipment_to_order_data( $response, $order, $request ) {
$shipments = fs_get_order_shipments( $order->get_id() );
if ( ! empty( $shipments ) ) {
return $this->append_shipment_to_order_data( $response, $order, $request, $shipments );
}
return $response;
}
/**
* Appends shipment data to order in REST API response.
*
* @param WP_REST_Response $response .
* @param WC_Order $order .
* @param WP_REST_Request $request .
* @param WPDesk_Flexible_Shipping_Shipment[] $shipments .
*
* @return WP_REST_Response
*/
private function append_shipment_to_order_data( $response, $order, $request, $shipments ) {
$response_data = $response->get_data();
if ( empty( $response_data[ self::REST_API_DATA_KEY ] ) ) {
$response_data[ self::REST_API_DATA_KEY ] = array();
}
$providers = WPDesk_Flexible_Shipping_Rest_Api_Order_Data_Providers_Factory::get_providers();
foreach ( $shipments as $shipment ) {
$integration = $shipment->get_integration();
$data_provider = $providers->get_provider_for_integration( $integration );
$response_data[ self::REST_API_DATA_KEY ][] = $data_provider->get_data_from_shipment( $shipment );
}
$response->set_data( $response_data );
return $response;
}
}

View File

@@ -0,0 +1,17 @@
<?php
/**
* Defines interface that REST API Order Data Provider should implement.
*/
interface WPDesk_Flexible_Shipping_Rest_Api_Order_Data_Provider {
/**
* Get data from shipment.
*
* @param WPDesk_Flexible_Shipping_Shipment $shipment .
*
* @return array
*/
public function get_data_from_shipment( WPDesk_Flexible_Shipping_Shipment $shipment );
}