File: /var/www/vhosts/uyarreklam.com.tr/httpdocs/GoogleAdapter.tar
AbstractRateGroupAdapter.php 0000644 00000004343 15154770620 0012165 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Shipping\GoogleAdapter;
use Automattic\WooCommerce\GoogleListingsAndAds\Exception\InvalidValue;
use Automattic\WooCommerce\GoogleListingsAndAds\Shipping\LocationRate;
use Automattic\WooCommerce\GoogleListingsAndAds\Vendor\Google\Service\ShoppingContent\Price;
use Automattic\WooCommerce\GoogleListingsAndAds\Vendor\Google\Service\ShoppingContent\RateGroup;
use Automattic\WooCommerce\GoogleListingsAndAds\Vendor\Google\Service\ShoppingContent\Value;
defined( 'ABSPATH' ) || exit;
/**
* Class AbstractRateGroupAdapter
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Shipping
*
* @since 2.1.0
*/
abstract class AbstractRateGroupAdapter extends RateGroup {
/**
* Initialize this object's properties from an array.
*
* @param array $properties Used to seed this object's properties.
*
* @throws InvalidValue When the required parameters are not provided, or they are invalid.
*/
public function mapTypes( $properties ) {
if ( empty( $properties['currency'] ) || ! is_string( $properties['currency'] ) ) {
throw new InvalidValue( 'The value of "currency" must be a non empty string.' );
}
if ( empty( $properties['location_rates'] ) || ! is_array( $properties['location_rates'] ) ) {
throw new InvalidValue( 'The value of "location_rates" must be a non empty array.' );
}
$this->map_location_rates( $properties['location_rates'], $properties['currency'] );
// Remove the extra data before calling the parent method since it doesn't expect them.
unset( $properties['currency'] );
unset( $properties['location_rates'] );
parent::mapTypes( $properties );
}
/**
* @param float $rate
* @param string $currency
*
* @return Value
*/
protected function create_value_object( float $rate, string $currency ): Value {
$price = new Price(
[
'currency' => $currency,
'value' => $rate,
]
);
return new Value( [ 'flatRate' => $price ] );
}
/**
* Map the location rates to the class properties.
*
* @param LocationRate[] $location_rates
* @param string $currency
*
* @return void
*/
abstract protected function map_location_rates( array $location_rates, string $currency ): void;
}
AbstractShippingSettingsAdapter.php 0000644 00000006327 15154770620 0013563 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Shipping\GoogleAdapter;
use Automattic\WooCommerce\GoogleListingsAndAds\Exception\InvalidValue;
use Automattic\WooCommerce\GoogleListingsAndAds\Vendor\Google\Service\ShoppingContent\DeliveryTime;
use Automattic\WooCommerce\GoogleListingsAndAds\Vendor\Google\Service\ShoppingContent\ShippingSettings as GoogleShippingSettings;
defined( 'ABSPATH' ) || exit;
/**
* Class AbstractShippingSettingsAdapter
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Shipping
*
* @since 2.1.0
*/
abstract class AbstractShippingSettingsAdapter extends GoogleShippingSettings {
/**
* @var string
*/
protected $currency;
/**
* @var array
*/
protected $delivery_times;
/**
* Initialize this object's properties from an array.
*
* @param array $properties Used to seed this object's properties.
*
* @return void
*
* @throws InvalidValue When the required parameters are not provided, or they are invalid.
*/
public function mapTypes( $properties ) {
$this->validate_gla_data( $properties );
$this->currency = $properties['currency'];
$this->delivery_times = $properties['delivery_times'];
$this->map_gla_data( $properties );
$this->unset_gla_data( $properties );
parent::mapTypes( $properties );
}
/**
* Return estimated delivery time for a given country in days.
*
* @param string $country
*
* @return DeliveryTime
*
* @throws InvalidValue If no delivery time can be found for the country.
*/
protected function get_delivery_time( string $country ): DeliveryTime {
if ( ! array_key_exists( $country, $this->delivery_times ) ) {
throw new InvalidValue( 'No estimated delivery time provided for country: ' . $country );
}
$time = new DeliveryTime();
$time->setMinHandlingTimeInDays( 0 );
$time->setMaxHandlingTimeInDays( 0 );
$time->setMinTransitTimeInDays( (int) $this->delivery_times[ $country ]['time'] );
$time->setMaxTransitTimeInDays( (int) $this->delivery_times[ $country ]['max_time'] );
return $time;
}
/**
* Validates the input array provided to this class.
*
* @param array $data
*
* @throws InvalidValue When the required parameters are not provided, or they are invalid.
*
* @link AbstractShippingSettingsAdapter::mapTypes() The $data input comes from this method.
*/
protected function validate_gla_data( array $data ): void {
if ( empty( $data['currency'] ) || ! is_string( $data['currency'] ) ) {
throw new InvalidValue( 'The value of "currency" must be a non empty string.' );
}
if ( empty( $data['delivery_times'] ) || ! is_array( $data['delivery_times'] ) ) {
throw new InvalidValue( 'The value of "delivery_times" must be a non empty array.' );
}
}
/**
* Remove the extra data we added to the input array since the MC API doesn't expect them (and it will fail).
*
* @param array $data
*/
protected function unset_gla_data( array &$data ): void {
unset( $data['currency'] );
unset( $data['delivery_times'] );
}
/**
* Parses the already validated input data and maps the provided shipping rates into MC shipping settings.
*
* @param array $data Validated data.
*/
abstract protected function map_gla_data( array $data ): void;
}
DBShippingSettingsAdapter.php 0000644 00000012501 15154770620 0012274 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Shipping\GoogleAdapter;
use Automattic\WooCommerce\GoogleListingsAndAds\Exception\InvalidValue;
use Automattic\WooCommerce\GoogleListingsAndAds\Vendor\Google\Service\ShoppingContent\Price;
use Automattic\WooCommerce\GoogleListingsAndAds\Vendor\Google\Service\ShoppingContent\RateGroup;
use Automattic\WooCommerce\GoogleListingsAndAds\Vendor\Google\Service\ShoppingContent\Service as GoogleShippingService;
use Automattic\WooCommerce\GoogleListingsAndAds\Vendor\Google\Service\ShoppingContent\Value;
defined( 'ABSPATH' ) || exit;
/**
* Class DBShippingSettingsAdapter
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Shipping
*
* @since 2.1.0
*/
class DBShippingSettingsAdapter extends AbstractShippingSettingsAdapter {
/**
* Parses the already validated input data and maps the provided shipping rates into MC shipping settings.
*
* @param array $data Validated data.
*/
protected function map_gla_data( array $data ): void {
$this->map_db_rates( $data['db_rates'] );
}
/**
* Validates the input array provided to this class.
*
* @param array $data
*
* @throws InvalidValue When the required parameters are not provided, or they are invalid.
*
* @link AbstractShippingSettingsAdapter::mapTypes() The $data input comes from this method.
*/
protected function validate_gla_data( array $data ): void {
parent::validate_gla_data( $data );
if ( empty( $data['db_rates'] ) || ! is_array( $data['db_rates'] ) ) {
throw new InvalidValue( 'The value of "db_rates" must be a non empty associated array of shipping rates.' );
}
}
/**
* Remove the extra data we added to the input array since the MC API doesn't expect them (and it will fail).
*
* @param array $data
*/
protected function unset_gla_data( array &$data ): void {
unset( $data['db_rates'] );
parent::unset_gla_data( $data );
}
/**
* Map the shipping rates stored for each country in DB to MC shipping settings.
*
* @param array[] $db_rates
*
* @return void
*/
protected function map_db_rates( array $db_rates ) {
$services = [];
foreach ( $db_rates as ['country' => $country, 'rate' => $rate, 'options' => $options] ) {
// No negative rates.
if ( $rate < 0 ) {
continue;
}
$service = $this->create_shipping_service( $country, $this->currency, (float) $rate );
if ( isset( $options['free_shipping_threshold'] ) ) {
$minimum_order_value = (float) $options['free_shipping_threshold'];
if ( $rate > 0 ) {
// Add a conditional free-shipping service if the current rate is not free.
$services[] = $this->create_conditional_free_shipping_service( $country, $this->currency, $minimum_order_value );
} else {
// Set the minimum order value if the current rate is free.
$service->setMinimumOrderValue(
new Price(
[
'value' => $minimum_order_value,
'currency' => $this->currency,
]
)
);
}
}
$services[] = $service;
}
$this->setServices( $services );
}
/**
* Create a rate group object for the shopping settings.
*
* @param string $currency
* @param float $rate
*
* @return RateGroup
*/
protected function create_rate_group_object( string $currency, float $rate ): RateGroup {
$price = new Price();
$price->setCurrency( $currency );
$price->setValue( $rate );
$value = new Value();
$value->setFlatRate( $price );
$rate_group = new RateGroup();
$rate_group->setSingleValue( $value );
$name = sprintf(
/* translators: %1 is the shipping rate, %2 is the currency (e.g. USD) */
__( 'Flat rate - %1$s %2$s', 'google-listings-and-ads' ),
$rate,
$currency
);
$rate_group->setName( $name );
return $rate_group;
}
/**
* Create a shipping service object.
*
* @param string $country
* @param string $currency
* @param float $rate
*
* @return GoogleShippingService
*/
protected function create_shipping_service( string $country, string $currency, float $rate ): GoogleShippingService {
$unique = sprintf( '%04x', wp_rand( 0, 0xffff ) );
$service = new GoogleShippingService();
$service->setActive( true );
$service->setDeliveryCountry( $country );
$service->setCurrency( $currency );
$service->setName(
sprintf(
/* translators: %1 is a random 4-digit string, %2 is the rate, %3 is the currency, %4 is the country code */
__( '[%1$s] Google for WooCommerce generated service - %2$s %3$s to %4$s', 'google-listings-and-ads' ),
$unique,
$rate,
$currency,
$country
)
);
$service->setRateGroups( [ $this->create_rate_group_object( $currency, $rate ) ] );
$service->setDeliveryTime( $this->get_delivery_time( $country ) );
return $service;
}
/**
* Create a free shipping service.
*
* @param string $country
* @param string $currency
* @param float $minimum_order_value
*
* @return GoogleShippingService
*/
protected function create_conditional_free_shipping_service( string $country, string $currency, float $minimum_order_value ): GoogleShippingService {
$service = $this->create_shipping_service( $country, $currency, 0 );
// Set the minimum order value to be eligible for free shipping.
$service->setMinimumOrderValue(
new Price(
[
'value' => $minimum_order_value,
'currency' => $currency,
]
)
);
return $service;
}
}
PostcodesRateGroupAdapter.php 0000644 00000003147 15154770620 0012366 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Shipping\GoogleAdapter;
use Automattic\WooCommerce\GoogleListingsAndAds\Shipping\LocationRate;
use Automattic\WooCommerce\GoogleListingsAndAds\Vendor\Google\Service\ShoppingContent\Headers;
use Automattic\WooCommerce\GoogleListingsAndAds\Vendor\Google\Service\ShoppingContent\Row;
use Automattic\WooCommerce\GoogleListingsAndAds\Vendor\Google\Service\ShoppingContent\Table;
defined( 'ABSPATH' ) || exit;
/**
* Class PostcodesRateGroupAdapter
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Shipping
*
* @since 2.1.0
*/
class PostcodesRateGroupAdapter extends AbstractRateGroupAdapter {
/**
* Map the location rates to the class properties.
*
* @param LocationRate[] $location_rates
* @param string $currency
*
* @return void
*/
protected function map_location_rates( array $location_rates, string $currency ): void {
$postal_codes = [];
$rows = [];
foreach ( $location_rates as $location_rate ) {
$region = $location_rate->get_location()->get_shipping_region();
if ( empty( $region ) ) {
continue;
}
$postcode_name = $region->get_id();
$postal_codes[ $postcode_name ] = $postcode_name;
$rows[ $postcode_name ] = new Row( [ 'cells' => [ $this->create_value_object( $location_rate->get_shipping_rate()->get_rate(), $currency ) ] ] );
}
$table = new Table(
[
'rowHeaders' => new Headers( [ 'postalCodeGroupNames' => array_values( $postal_codes ) ] ),
'rows' => array_values( $rows ),
]
);
$this->setMainTable( $table );
}
}
StatesRateGroupAdapter.php 0000644 00000003236 15154770620 0011665 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Shipping\GoogleAdapter;
use Automattic\WooCommerce\GoogleListingsAndAds\Shipping\LocationRate;
use Automattic\WooCommerce\GoogleListingsAndAds\Vendor\Google\Service\ShoppingContent\Headers;
use Automattic\WooCommerce\GoogleListingsAndAds\Vendor\Google\Service\ShoppingContent\LocationIdSet;
use Automattic\WooCommerce\GoogleListingsAndAds\Vendor\Google\Service\ShoppingContent\Row;
use Automattic\WooCommerce\GoogleListingsAndAds\Vendor\Google\Service\ShoppingContent\Table;
defined( 'ABSPATH' ) || exit;
/**
* Class StatesRateGroupAdapter
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Shipping
*
* @since 2.1.0
*/
class StatesRateGroupAdapter extends AbstractRateGroupAdapter {
/**
* Map the location rates to the class properties.
*
* @param LocationRate[] $location_rates
* @param string $currency
*
* @return void
*/
protected function map_location_rates( array $location_rates, string $currency ): void {
$location_id_sets = [];
$rows = [];
foreach ( $location_rates as $location_rate ) {
$location_id = $location_rate->get_location()->get_google_id();
$location_id_sets[ $location_id ] = new LocationIdSet( [ 'locationIds' => [ $location_id ] ] );
$rows[ $location_id ] = new Row( [ 'cells' => [ $this->create_value_object( $location_rate->get_shipping_rate()->get_rate(), $currency ) ] ] );
}
$table = new Table(
[
'rowHeaders' => new Headers( [ 'locations' => array_values( $location_id_sets ) ] ),
'rows' => array_values( $rows ),
]
);
$this->setMainTable( $table );
}
}
WCShippingSettingsAdapter.php 0000644 00000020520 15154770620 0012320 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Shipping\GoogleAdapter;
use Automattic\WooCommerce\GoogleListingsAndAds\Exception\InvalidArgument;
use Automattic\WooCommerce\GoogleListingsAndAds\Exception\InvalidClass;
use Automattic\WooCommerce\GoogleListingsAndAds\Exception\InvalidValue;
use Automattic\WooCommerce\GoogleListingsAndAds\Shipping\CountryRatesCollection;
use Automattic\WooCommerce\GoogleListingsAndAds\Shipping\LocationRate;
use Automattic\WooCommerce\GoogleListingsAndAds\Shipping\ServiceRatesCollection;
use Automattic\WooCommerce\GoogleListingsAndAds\Shipping\ShippingLocation;
use Automattic\WooCommerce\GoogleListingsAndAds\Vendor\Google\Service\ShoppingContent\PostalCodeGroup;
use Automattic\WooCommerce\GoogleListingsAndAds\Vendor\Google\Service\ShoppingContent\PostalCodeRange;
use Automattic\WooCommerce\GoogleListingsAndAds\Vendor\Google\Service\ShoppingContent\Price;
use Automattic\WooCommerce\GoogleListingsAndAds\Vendor\Google\Service\ShoppingContent\RateGroup;
use Automattic\WooCommerce\GoogleListingsAndAds\Vendor\Google\Service\ShoppingContent\Service as GoogleShippingService;
use Automattic\WooCommerce\GoogleListingsAndAds\Vendor\Google\Service\ShoppingContent\Value;
defined( 'ABSPATH' ) || exit;
/**
* Class WCShippingSettingsAdapter
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Shipping
*
* @since 2.1.0
*/
class WCShippingSettingsAdapter extends AbstractShippingSettingsAdapter {
/**
* Parses the already validated input data and maps the provided shipping rates into MC shipping settings.
*
* @param array $data Validated data.
*/
protected function map_gla_data( array $data ): void {
$this->map_rates_collections( $data['rates_collections'] );
}
/**
* Validates the input array provided to this class.
*
* @param array $data
*
* @throws InvalidValue When the required parameters are not provided, or they are invalid.
*
* @link AbstractShippingSettingsAdapter::mapTypes() The $data input comes from this method.
*/
protected function validate_gla_data( array $data ): void {
parent::validate_gla_data( $data );
if ( empty( $data['rates_collections'] ) || ! is_array( $data['rates_collections'] ) ) {
throw new InvalidValue( 'The value of "rates_collections" must be a non empty array of CountryRatesCollection objects.' );
} else {
$this->validate_rates_collections( $data['rates_collections'] );
}
}
/**
* Remove the extra data we added to the input array since the MC API doesn't expect them (and it will fail).
*
* @param array $data
*/
protected function unset_gla_data( array &$data ): void {
unset( $data['rates_collections'] );
parent::unset_gla_data( $data );
}
/**
* Map the collections of location rates for each country to the shipping settings.
*
* @param CountryRatesCollection[] $rates_collections
*
* @return void
*/
protected function map_rates_collections( array $rates_collections ) {
$postcode_groups = [];
$services = [];
foreach ( $rates_collections as $rates_collection ) {
$postcode_groups = array_merge( $postcode_groups, $this->get_location_rates_postcode_groups( $rates_collection->get_location_rates() ) );
foreach ( $rates_collection->get_rates_grouped_by_service() as $service_collection ) {
$services[] = $this->create_shipping_service( $service_collection );
}
}
$this->setServices( $services );
$this->setPostalCodeGroups( array_values( $postcode_groups ) );
}
/**
* @param LocationRate[] $location_rates
* @param string $shipping_area
* @param array $applicable_classes
*
* @return RateGroup
*
* @throws InvalidArgument If an invalid value is provided for the shipping_area argument.
*/
protected function create_rate_group( array $location_rates, string $shipping_area, array $applicable_classes = [] ): RateGroup {
switch ( $shipping_area ) {
case ShippingLocation::COUNTRY_AREA:
// Each country can only have one global rate.
$country_rate = $location_rates[ array_key_first( $location_rates ) ];
$rate_group = $this->create_single_value_rate_group( $country_rate, $applicable_classes );
break;
case ShippingLocation::POSTCODE_AREA:
$rate_group = new PostcodesRateGroupAdapter(
[
'location_rates' => $location_rates,
'currency' => $this->currency,
'applicableShippingLabels' => $applicable_classes,
]
);
break;
case ShippingLocation::STATE_AREA:
$rate_group = new StatesRateGroupAdapter(
[
'location_rates' => $location_rates,
'currency' => $this->currency,
'applicableShippingLabels' => $applicable_classes,
]
);
break;
default:
throw new InvalidArgument( 'Invalid shipping area.' );
}
return $rate_group;
}
/**
* Create a shipping service object.
*
* @param ServiceRatesCollection $service_collection
*
* @return GoogleShippingService
*/
protected function create_shipping_service( ServiceRatesCollection $service_collection ): GoogleShippingService {
$rate_groups = [];
$shipping_area = $service_collection->get_shipping_area();
foreach ( $service_collection->get_rates_grouped_by_shipping_class() as $class => $location_rates ) {
$applicable_classes = ! empty( $class ) ? [ $class ] : [];
$rate_groups[ $class ] = $this->create_rate_group( $location_rates, $shipping_area, $applicable_classes );
}
$country = $service_collection->get_country();
$name = sprintf(
/* translators: %1 is a random 4-digit string, %2 is the country code */
__( '[%1$s] Google for WooCommerce generated service - %2$s', 'google-listings-and-ads' ),
sprintf( '%04x', wp_rand( 0, 0xffff ) ),
$country
);
$service = new GoogleShippingService(
[
'active' => true,
'deliveryCountry' => $country,
'currency' => $this->currency,
'name' => $name,
'deliveryTime' => $this->get_delivery_time( $country ),
'rateGroups' => array_values( $rate_groups ),
]
);
$min_order_amount = $service_collection->get_min_order_amount();
if ( $min_order_amount ) {
$min_order_value = new Price(
[
'currency' => $this->currency,
'value' => $min_order_amount,
]
);
$service->setMinimumOrderValue( $min_order_value );
}
return $service;
}
/**
* Extract and return the postcode groups for the given location rates.
*
* @param LocationRate[] $location_rates
*
* @return PostalCodeGroup[]
*/
protected function get_location_rates_postcode_groups( array $location_rates ): array {
$postcode_groups = [];
foreach ( $location_rates as $location_rate ) {
$location = $location_rate->get_location();
if ( empty( $location->get_shipping_region() ) ) {
continue;
}
$region = $location->get_shipping_region();
$postcode_ranges = [];
foreach ( $region->get_postcode_ranges() as $postcode_range ) {
$postcode_ranges[] = new PostalCodeRange(
[
'postalCodeRangeBegin' => $postcode_range->get_start_code(),
'postalCodeRangeEnd' => $postcode_range->get_end_code(),
]
);
}
$postcode_groups[ $region->get_id() ] = new PostalCodeGroup(
[
'name' => $region->get_id(),
'country' => $location->get_country(),
'postalCodeRanges' => $postcode_ranges,
]
);
}
return $postcode_groups;
}
/**
* @param LocationRate $location_rate
* @param string[] $shipping_classes
*
* @return RateGroup
*/
protected function create_single_value_rate_group( LocationRate $location_rate, array $shipping_classes = [] ): RateGroup {
$price = new Price(
[
'currency' => $this->currency,
'value' => $location_rate->get_shipping_rate()->get_rate(),
]
);
return new RateGroup(
[
'singleValue' => new Value( [ 'flatRate' => $price ] ),
'applicableShippingLabels' => $shipping_classes,
]
);
}
/**
* @param array $rates_collections
*
* @throws InvalidClass If any of the objects in the array is not an instance of CountryRatesCollection.
*/
protected function validate_rates_collections( array $rates_collections ) {
array_walk(
$rates_collections,
function ( $obj ) {
if ( ! $obj instanceof CountryRatesCollection ) {
throw new InvalidValue( 'All values of the "rates_collections" array must be an instance of CountryRatesCollection.' );
}
}
);
}
}