Skip to content

Commit

Permalink
Update shipment tracking provider rewrite
Browse files Browse the repository at this point in the history
* add missing docblock
* only create shipment tracking manager once
* display a error message is the selected povider is not available
  • Loading branch information
petitphp committed Sep 25, 2024
1 parent 4c22dfb commit cdab635
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 13 deletions.
22 changes: 22 additions & 0 deletions src/Admin/Notices.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
class Notices {

public function __construct() {
add_action( 'admin_notices', array( $this, 'unavailable_selected_shipping_tracking_provider' ) );
add_action( 'admin_notices', array( $this, 'admin_notices' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
}
Expand Down Expand Up @@ -66,4 +67,25 @@ public function display_notice() {
<?php
}

public function unavailable_selected_shipping_tracking_provider(): void {
$manager = ShoppingFeedHelper::wc_tracking_provider_manager();
$selected_provider = $manager->get_selected_provider( false );
if ( ! $selected_provider->is_available() ) {
?>
<div class="notice notice-error">
<p>
<span class="dashicons dashicons-warning"></span>
<?php echo wp_kses_post(
sprintf(
// translators: 1: the provider name, 2: the plugin settings link
__( 'The selected shipment tracking provider <strong>"%1$s"</strong> is not available. Please go the <a href="%2$s">shipping settings</a> to choose a new one.', 'shopping-feed' ),
$selected_provider->name(),
add_query_arg( 'tab', 'shipping-settings', ShoppingFeedHelper::get_setting_link() )
)
); ?>
</p>
</div>
<?php
}
}
}
12 changes: 10 additions & 2 deletions src/Admin/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -1238,13 +1238,14 @@ function () {
'shipping_tracking_provider',
__( 'Tracking provider', 'shopping-feed' ),
function () {
$manager = ShipmentTrackingManager::create();
$manager = ShoppingFeedHelper::wc_tracking_provider_manager();
$selected_provider = $this->sf_shipping_options['tracking_provider'] ?? '';
?>
<select id="tracking_provider" name="<?php echo esc_html( sprintf( '%s[tracking_provider]', self::SF_SHIPPING_OPTIONS ) ); ?>">
<option value=""><?php esc_html_e( 'Disable', 'shopping-feed' ); ?></option>
<?php foreach ( $manager->get_providers() as $provider ) : ?>
<option value="<?php echo esc_attr( $provider->id() ); ?>"
<?php selected( $provider->id(), $this->sf_shipping_options['tracking_provider'] ?? '' ); ?>
<?php selected( $selected_provider, $provider->id() ); ?>
<?php disabled( ! $provider->is_available() ); ?>>
<?php echo esc_html( $provider->name() ); ?>
<?php
Expand All @@ -1260,6 +1261,13 @@ function () {
</option>
<?php endforeach; ?>
</select>
<?php if ( ! $manager->get_selected_provider( false )->is_available() ) : ?>
<p class="description"
id="tagline-description">
<span class="dashicons dashicons-warning"></span>
<strong><?php esc_attr_e( 'The selected provider is not available.', 'shopping-feed' ); ?></strong>
</p>
<?php endif; ?>
<p class="description"
id="tagline-description">
<?php esc_attr_e( 'Choose the provider used to retrieve tracking information.', 'shopping-feed' ); ?>
Expand Down
13 changes: 13 additions & 0 deletions src/ShipmentTracking/Provider/AdvancedShipmentTracking.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,33 @@
*/
class AdvancedShipmentTracking implements ShipmentTrackingProvider {

/**
* @inerhitDoc
*/
public function id(): string {
return 'advanced_shipment_tracking';
}

/**
* @inerhitDoc
*/
public function name(): string {
return 'Advanced Shipment Tracking / Advanced Shipment Tracking Pro';
}

/**
* @inerhitDoc
*/
public function is_available(): bool {
return defined( 'SHIPMENT_TRACKING_PATH' );
}

/**
* @inheritDoc
*/
public function get_tracking_data( \WC_Order $order ): ShipmentTrackingData {
$tracking_data = new ShipmentTrackingData();
$tracking_info = [];
if ( function_exists( 'ast_get_tracking_items' ) ) { // AST >= 3.0 & AST Pro
$tracking_info = ast_get_tracking_items( $order->get_id() );
} elseif ( class_exists( '\WC_Advanced_Shipment_Tracking_Actions' ) ) { // AST < 3.0
Expand Down
13 changes: 13 additions & 0 deletions src/ShipmentTracking/Provider/NullShipmentTracking.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,35 @@

namespace ShoppingFeed\ShoppingFeedWC\ShipmentTracking\Provider;

use ShoppingFeed\ShoppingFeedWC\ShipmentTracking\ShipmentTrackingData;
use ShoppingFeed\ShoppingFeedWC\ShipmentTracking\ShipmentTrackingProvider;

class NullShipmentTracking implements ShipmentTrackingProvider {

/**
* @inheritDoc
*/
public function id(): string {
return 'default';
}

/**
* @inheritDoc
*/
public function name(): string {
return 'Default';
}

/**
* @inheritDoc
*/
public function is_available(): bool {
return true;
}

/**
* @inheritDoc
*/
public function get_tracking_data( \WC_Order $order ): ShipmentTrackingData {
return new ShipmentTrackingData();
}
Expand Down
12 changes: 12 additions & 0 deletions src/ShipmentTracking/Provider/PhWoocommerceShipmentTracking.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,30 @@
*/
class PhWoocommerceShipmentTracking implements ShipmentTrackingProvider {

/**
* @inheritDoc
*/
public function id(): string {
return 'woo_shipment_tracking';
}

/**
* @inheritDoc
*/
public function name(): string {
return 'WooCommerce Shipment Tracking Pro';
}

/**
* @inheritDoc
*/
public function is_available(): bool {
return defined( 'PH_SHIPMENT_TRACKING_PLUGIN_VERSION' );
}

/**
* @inheritDoc
*/
public function get_tracking_data( \WC_Order $order ): ShipmentTrackingData {
$tracking_data = new ShipmentTrackingData();
$ph_tracking_data = $order->get_meta( 'wf_wc_shipment_result' );
Expand Down
14 changes: 13 additions & 1 deletion src/ShipmentTracking/Provider/ShoppingfeedAdvanced.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,30 @@
*/
class ShoppingfeedAdvanced implements ShipmentTrackingProvider {

/**
* @inheritDoc
*/
public function id(): string {
return 'sf_advanced';
}

/**
* @inheritDoc
*/
public function name(): string {
return 'ShoppingFeed Advanced';
}

/**
* @inheritDoc
*/
public function is_available(): bool {
return defined( 'SFA_PaLUGIN_VERSION' );
return defined( 'SFA_PLUGIN_VERSION' );
}

/**
* @inheritDoc
*/
public function get_tracking_data( \WC_Order $order ): ShipmentTrackingData {
$tracking_data = new ShipmentTrackingData();
$tracking_number = (string) $order->get_meta( TRACKING_NUMBER_FIELD_SLUG );
Expand Down
24 changes: 23 additions & 1 deletion src/ShipmentTracking/ShipmentTrackingData.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,49 @@ class ShipmentTrackingData {
* @param string $tracking_number
* @param string $tracking_link
*
* @return ShipmentTrackingData
* @return self
*/
public function add_tracking_data( string $tracking_number, string $tracking_link = '' ): self {
$this->tracking_data[ $tracking_number ] = [
'tracking_number' => $tracking_number,
'tracking_link' => $tracking_link,
];

return $this;
}

/**
* Check if tracking data are available.
*
* @return bool
*/
public function has_tracking_data(): bool {
return ! empty( $this->tracking_data );
}

/**
* Get an array of tracking data.
*
* @return array{int, string[]}
*/
public function get_tracking_data(): array {
return $this->tracking_data;
}

/**
* Get a list of tracking numbers.
*
* @return string[]
*/
public function get_tracking_numbers(): array {
return wp_list_pluck( $this->tracking_data, 'tracking_number' );
}

/**
* Get a list of tracking links.
*
* @return string[]
*/
public function get_tracking_links(): array {
return wp_list_pluck( $this->tracking_data, 'tracking_link' );
}
Expand Down
57 changes: 50 additions & 7 deletions src/ShipmentTracking/ShipmentTrackingManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,31 @@ class ShipmentTrackingManager {
*/
private $selected_provider;

/**
* Instantiate an `ShipmentTrackingManager`.
*
* @param array $shipping_options
*
* @return self
*/
public static function create( array $shipping_options = [] ): self {
$manager = new self();

$shipment_tracking_providers = [
$providers = [
new ShoppingfeedAdvanced(),
new AdvancedShipmentTracking(),
new PhWoocommerceShipmentTracking(),
];
$shipment_tracking_providers = apply_filters( 'sf_shipment_tracking_providers', $shipment_tracking_providers );
array_map( [ $manager, 'add_provider' ], $shipment_tracking_providers );

/**
* Filter list of available shipment tracking providers.
*
* @since 6.8
*
* @param ShipmentTrackingProvider[] $providers list of providers
*/
$providers = apply_filters( 'sf_shipment_tracking_providers', $providers );
array_map( [ $manager, 'register_provider' ], $providers );

if ( isset( $shipping_options['tracking_provider'] ) ) {
$manager->selected_provider = $shipping_options['tracking_provider'];
Expand All @@ -38,15 +53,23 @@ public static function create( array $shipping_options = [] ): self {
}

/**
* Register provider.
*
* @param ShipmentTrackingProvider $provider
*
* @return void
*/
public function add_provider( ShipmentTrackingProvider $provider ): void {
$this->providers[ $provider->id() ] = $provider;
public function register_provider( $provider ): void {
if ( is_a( $provider, ShipmentTrackingProvider::class ) ) {
$this->providers[ $provider->id() ] = $provider;
}
}

/**
* Get a list of providers.
*
* @param bool $available_only only include available providers.
*
* @return ShipmentTrackingProvider[]
*/
public function get_providers( bool $available_only = false ): array {
Expand All @@ -63,12 +86,32 @@ function ( $provider ) {
return $providers;
}

public function get_selected_provider(): ShipmentTrackingProvider {
return isset( $this->providers[ $this->selected_provider ] )
/**
* Get the chosen provider.
*
* @param bool $check_availability ensure selected provider is available, use default one if not.
*
* @return ShipmentTrackingProvider
*/
public function get_selected_provider( bool $check_availability = true ): ShipmentTrackingProvider {
$provider = isset( $this->providers[ $this->selected_provider ] )
? $this->providers[ $this->selected_provider ]
: new NullShipmentTracking();

if ( $check_availability ) {
$provider = $provider->is_available() ? $provider : new NullShipmentTracking();
}

return $provider;
}

/**
* Get tracking data for an order.
*
* @param \WC_Order $order
*
* @return ShipmentTrackingData
*/
public function get_tracking_data( \WC_Order $order ): ShipmentTrackingData {
return $this->get_selected_provider()->get_tracking_data( $order );
}
Expand Down
18 changes: 16 additions & 2 deletions src/ShoppingFeedHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use ShoppingFeed\ShoppingFeedWC\Admin\Options;
use ShoppingFeed\ShoppingFeedWC\ShipmentTracking\ShipmentTrackingManager;
use ShoppingFeed\ShoppingFeedWC\ShipmentTracking\ShipmentTrackingProvider;
use ShoppingFeed\ShoppingFeedWC\Url\Rewrite;
use WC_Logger;

Expand Down Expand Up @@ -706,7 +707,7 @@ public static function sf_order_statuses_to_import() {
* @return string
*/
public static function wc_tracking_number( $wc_order ) {
$manager = ShipmentTrackingManager::create();
$manager = self::wc_tracking_provider_manager();

$tracking_number = '';
$tracking_data = $manager->get_selected_provider()->get_tracking_data( $wc_order );
Expand Down Expand Up @@ -738,7 +739,7 @@ public static function wc_tracking_number( $wc_order ) {
* @return string
*/
public static function wc_tracking_link( $wc_order ) {
$manager = ShipmentTrackingManager::create();
$manager = self::wc_tracking_provider_manager();

$tracking_link = '';
$tracking_data = $manager->get_selected_provider()->get_tracking_data( $wc_order );
Expand All @@ -762,6 +763,19 @@ public static function wc_tracking_link( $wc_order ) {
return $tracking_link;
}

public static function wc_tracking_provider_manager(): ShipmentTrackingManager {
static $manager;
if ( ! $manager ) {
$shipping_options = self::get_sf_shipping_options();
if ( ! is_array( $shipping_options ) ) {
$shipping_options = [];
}
$manager = ShipmentTrackingManager::create( $shipping_options );
}

return $manager;
}

/**
* Default quantity if product quantity is unset
* @return int
Expand Down

0 comments on commit cdab635

Please sign in to comment.