File: /var/www/vhosts/uyarreklam.com.tr/httpdocs/HelperTraits.tar
GTINMigrationUtilities.php 0000644 00000012613 15154237627 0011603 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\HelperTraits;
use Automattic\WooCommerce\GoogleListingsAndAds\Jobs\MigrateGTIN;
use Automattic\WooCommerce\GoogleListingsAndAds\Options\OptionsAwareTrait;
use Automattic\WooCommerce\GoogleListingsAndAds\Options\OptionsInterface;
use Exception;
use WC_Product;
defined( 'ABSPATH' ) || exit;
/**
* Trait GTINMigrationUtilities
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\HelperTraits
*/
trait GTINMigrationUtilities {
use OptionsAwareTrait;
/**
* Get the version from when the GTIN should be hidden in the Google for WooCommerce tab.
*
* @return string
*/
protected function get_gtin_hidden_version(): string {
return '2.8.7';
}
/**
* Get the version from when the GTIN field is available in core.
* 9.2 is the version when GTIN field was added in Woo Core.
*
* @return bool
*/
protected function is_gtin_available_in_core(): bool {
return version_compare( WC_VERSION, '9.2', '>=' ) && method_exists( WC_Product::class, 'get_global_unique_id' );
}
/**
* If GTIN field should be hidden, this is when initial installed version is after the GTIN migration logic.
*
* @return bool
*/
protected function should_hide_gtin(): bool {
// Don't hide in case GTIN is not available in core.
if ( ! $this->is_gtin_available_in_core() ) {
return false;
}
$first_install_version = $this->options()->get( OptionsInterface::INSTALL_VERSION, false );
return $first_install_version && version_compare( $first_install_version, $this->get_gtin_hidden_version(), '>' );
}
/**
* Get the status for the migration of GTIN.
*
* GTIN_MIGRATION_COMPLETED: GTIN is not available on that WC version or the initial version installed after GTIN migration.
* GTIN_MIGRATION_READY: GTIN is available in core and on read-only mode in the extension. It's ready for migration.
* GTIN_MIGRATION_STARTED: GTIN migration is started
* GTIN_MIGRATION_COMPLETED: GTIN Migration is completed
*
* @return string
*/
protected function get_gtin_migration_status(): string {
// If the current version doesn't show GTIN field or the GTIN field is not available in core.
if ( ! $this->is_gtin_available_in_core() || $this->should_hide_gtin() ) {
return MigrateGTIN::GTIN_MIGRATION_UNAVAILABLE;
}
return $this->options()->get( OptionsInterface::GTIN_MIGRATION_STATUS, MigrateGTIN::GTIN_MIGRATION_READY );
}
/**
*
* Get the options object.
* Notice classes with OptionsAwareTrait only get the options object auto-loaded if
* they are registered in the Container class.
* If they are instantiated on the fly (like the input fields), then this won't get done.
* That's why we need to fetch it from the container in case options field is null.
*
* @return OptionsInterface
*/
protected function options(): OptionsInterface {
return $this->options ?? woogle_get_container()->get( OptionsInterface::class );
}
/**
* Prepares the GTIN to be saved.
*
* @param string $gtin
* @return string
*/
protected function prepare_gtin( string $gtin ): string {
return str_replace( '-', '', $gtin );
}
/**
* Gets the message when the GTIN is invalid.
*
* @param WC_Product $product
* @param string $gtin
* @return string
*/
protected function error_gtin_invalid( WC_Product $product, string $gtin ): string {
return sprintf( 'GTIN [ %s ] has been skipped for Product ID: %s - %s. Invalid GTIN was found.', $gtin, $product->get_id(), $product->get_name() );
}
/**
* Gets the message when the GTIN is already in the Product Inventory
*
* @param WC_Product $product
* @return string
*/
protected function error_gtin_already_set( WC_Product $product ): string {
return sprintf( 'GTIN has been skipped for Product ID: %s - %s. GTIN was found in Product Inventory tab.', $product->get_id(), $product->get_name() );
}
/**
* Gets the message when the GTIN is not found.
*
* @param WC_Product $product
* @return string
*/
protected function error_gtin_not_found( WC_Product $product ): string {
return sprintf( 'GTIN has been skipped for Product ID: %s - %s. No GTIN was found', $product->get_id(), $product->get_name() );
}
/**
* Gets the message when the GTIN had an error when saving.
*
* @param WC_Product $product
* @param string $gtin
* @param Exception $e
*
* @return string
*/
protected function error_gtin_not_saved( WC_Product $product, string $gtin, Exception $e ): string {
return sprintf( 'GTIN [ %s ] for Product ID: %s - %s has an error - %s', $gtin, $product->get_id(), $product->get_name(), $e->getMessage() );
}
/**
* Gets the message when the GTIN is successfully migrated.
*
* @param WC_Product $product
* @param string $gtin
*
* @return string
*/
protected function successful_migrated_gtin( WC_Product $product, string $gtin ): string {
return sprintf( 'GTIN [ %s ] has been migrated for Product ID: %s - %s', $gtin, $product->get_id(), $product->get_name() );
}
/**
* Gets the GTIN value
*
* @param WC_Product $product The product
* @return string|null
*/
protected function get_gtin( WC_Product $product ): ?string {
/**
* Filters the value of the GTIN before performing the migration.
* This value will be he one that we copy inside the Product Inventory GTIN.
*/
return apply_filters( 'woocommerce_gla_gtin_migration_value', $this->attribute_manager->get_value( $product, 'gtin' ), $product );
}
}
ISO3166Awareness.php 0000644 00000001274 15154237627 0010120 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\HelperTraits;
use Automattic\WooCommerce\GoogleListingsAndAds\Vendor\League\ISO3166\ISO3166DataProvider;
defined( 'ABSPATH' ) || exit;
/**
* Trait ISO3166Awareness
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\HelperTraits
*/
trait ISO3166Awareness {
/**
* The object implementing the ISO3166DataProvider interface.
*
* @var ISO3166DataProvider
*/
protected $iso3166_data_provider;
/**
* @param ISO3166DataProvider $provider
*
* @return void
*/
public function set_iso3166_provider( ISO3166DataProvider $provider ): void {
$this->iso3166_data_provider = $provider;
}
}
Utilities.php 0000644 00000005505 15154237627 0007251 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\HelperTraits;
use Automattic\WooCommerce\GoogleListingsAndAds\Options\OptionsAwareTrait;
use Automattic\WooCommerce\GoogleListingsAndAds\Options\OptionsInterface;
defined( 'ABSPATH' ) || exit;
/**
* Trait Utilities
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\HelperTraits
*/
trait Utilities {
use OptionsAwareTrait;
/**
* Does the store have (x) orders
*
* @param integer $count Number of orders to check for
* @param array $status Order statuses to check for
* @return boolean
*/
protected function has_orders( $count = 5, $status = [ 'wc-completed' ] ): bool {
$args = [
'status' => $status,
'limit' => $count,
'return' => 'ids',
'orderby' => 'date',
'order' => 'ASC',
];
return $count === count( wc_get_orders( $args ) );
}
/**
* Test how long GLA has been active.
*
* @param int $seconds Time in seconds to check.
* @return bool Whether or not GLA has been active for $seconds.
*/
protected function gla_active_for( $seconds ): bool {
$gla_installed = $this->options->get( OptionsInterface::INSTALL_TIMESTAMP, false );
if ( false === $gla_installed ) {
return false;
}
return ( ( time() - $gla_installed ) >= $seconds );
}
/**
* Test how long GLA has been setup for.
*
* @param int $seconds Time in seconds to check.
* @return bool Whether or not GLA has been active for $seconds.
*/
protected function gla_setup_for( $seconds ): bool {
$gla_completed_setup = $this->options->get( OptionsInterface::MC_SETUP_COMPLETED_AT, false );
if ( false === $gla_completed_setup ) {
return false;
}
return ( ( time() - $gla_completed_setup ) >= $seconds );
}
/**
* Is Jetpack connected?
*
* @since 1.12.5
*
* @return boolean
*/
protected function is_jetpack_connected(): bool {
return boolval( $this->options->get( OptionsInterface::JETPACK_CONNECTED, false ) );
}
/**
* Encode data to Base64URL
*
* @since 2.8.0
*
* @param string $data The string that will be base64 URL encoded.
*
* @return string
*/
protected function base64url_encode( $data ): string {
$b64 = base64_encode( $data );
// Convert Base64 to Base64URL by replacing "+" with "-" and "/" with "_"
$url = strtr( $b64, '+/', '-_' );
// Remove padding character from the end of line and return the Base64URL result
return rtrim( $url, '=' );
}
/**
* Decode Base64URL string
*
* @since 2.8.0
*
* @param string $data The data that will be base64 URL encoded.
*
* @return boolean|string
*/
protected function base64url_decode( $data ): string {
// Convert Base64URL to Base64 by replacing "-" with "+" and "_" with "/"
$b64 = strtr( $data, '-_', '+/' );
// Decode Base64 string and return the original data
return base64_decode( $b64 );
}
}
ViewHelperTrait.php 0000644 00000002756 15154237627 0010361 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\HelperTraits;
use Automattic\WooCommerce\GoogleListingsAndAds\PluginHelper;
defined( 'ABSPATH' ) || exit;
/**
* Trait ViewHelperTrait
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\HelperTraits
*/
trait ViewHelperTrait {
use PluginHelper;
/**
* Returns the list of allowed HTML tags used for view sanitization.
*
* @return array
*/
protected function get_allowed_html_form_tags(): array {
$allowed_attributes = [
'aria-describedby' => true,
'aria-details' => true,
'aria-label' => true,
'aria-labelledby' => true,
'aria-hidden' => true,
'class' => true,
'id' => true,
'style' => true,
'title' => true,
'role' => true,
'data-*' => true,
'action' => true,
'value' => true,
'name' => true,
'selected' => true,
'type' => true,
'disabled' => true,
];
return array_merge(
wp_kses_allowed_html( 'post' ),
[
'form' => $allowed_attributes,
'input' => $allowed_attributes,
'select' => $allowed_attributes,
'option' => $allowed_attributes,
]
);
}
/**
* Appends a prefix to the given ID and returns it.
*
* @param string $id
*
* @return string
*
* @since 1.1.0
*/
protected function prefix_id( string $id ): string {
return "{$this->get_slug()}_$id";
}
}