File: /var/www/vhosts/uyarreklam.com.tr/httpdocs/MetaBox.tar
AbstractMetaBox.php 0000644 00000007644 15154761760 0010327 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Admin\MetaBox;
use Automattic\WooCommerce\GoogleListingsAndAds\Admin\Admin;
use Automattic\WooCommerce\GoogleListingsAndAds\HelperTraits\ViewHelperTrait;
use Automattic\WooCommerce\GoogleListingsAndAds\View\ViewException;
use WP_Post;
defined( 'ABSPATH' ) || exit;
/**
* Class AbstractMetaBox
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Admin\MetaBox
*/
abstract class AbstractMetaBox implements MetaBoxInterface {
use ViewHelperTrait;
protected const VIEW_PATH = 'meta-box';
/**
* @var Admin
*/
protected $admin;
/**
* AbstractMetaBox constructor.
*
* @param Admin $admin
*/
protected function __construct( Admin $admin ) {
$this->admin = $admin;
}
/**
* The context within the screen where the box should display. Available contexts vary from screen to
* screen. Post edit screen contexts include 'normal', 'side', and 'advanced'. Comments screen contexts
* include 'normal' and 'side'. Menus meta boxes (accordion sections) all use the 'side' context.
*
* Global default is 'advanced'.
*
* @return string
*/
public function get_context(): string {
return self::CONTEXT_ADVANCED;
}
/***
* The priority within the context where the box should show.
*
* Accepts 'high', 'core', 'default', or 'low'. Default 'default'.
*
* @return string
*/
public function get_priority(): string {
return self::PRIORITY_DEFAULT;
}
/**
* Data that should be set as the $args property of the box array (which is the second parameter passed to your callback).
*
* @return array
*/
public function get_callback_args(): array {
return [];
}
/**
* Returns an array of CSS classes to apply to the box.
*
* @return array
*/
public function get_classes(): array {
return [];
}
/**
* Function that fills the box with the desired content.
*
* The function should echo its output.
*
* @return callable
*/
public function get_callback(): callable {
return [ $this, 'handle_callback' ];
}
/**
* Called by WordPress when rendering the meta box.
*
* The function should echo its output.
*
* @param WP_Post $post The WordPress post object the box is loaded for.
* @param array $data Array of box data passed to the callback by WordPress.
*
* @return void
*
* @throws ViewException If the meta box view can't be rendered.
*/
public function handle_callback( WP_Post $post, array $data ): void {
$args = $data['args'] ?? [];
$context = $this->get_view_context( $post, $args );
echo wp_kses( $this->render( $context ), $this->get_allowed_html_form_tags() );
}
/**
* Render the meta box.
*
* The view templates need to be placed under 'views/meta-box' and named
* using the meta box ID specified by the `get_id` method.
*
* @param array $context Optional. Contextual information to use while
* rendering. Defaults to an empty array.
*
* @return string Rendered result.
*
* @throws ViewException If the view doesn't exist or can't be loaded.
*
* @see self::get_id To see and modify the view file name.
*/
public function render( array $context = [] ): string {
$view_path = path_join( self::VIEW_PATH, $this->get_id() );
return $this->admin->get_view( $view_path, $context );
}
/**
* Appends a prefix to the given field ID and returns it.
*
* @param string $field_id
*
* @return string
*
* @since 1.1.0
*/
protected function prefix_field_id( string $field_id ): string {
$box_id = $this->prefix_id( $this->get_id() );
return "{$box_id}_{$field_id}";
}
/**
* Returns an array of variables to be used in the view.
*
* @param WP_Post $post The WordPress post object the box is loaded for.
* @param array $args Array of data passed to the callback. Defined by `get_callback_args`.
*
* @return array
*/
abstract protected function get_view_context( WP_Post $post, array $args ): array;
}
ChannelVisibilityMetaBox.php 0000644 00000013006 15154761760 0012171 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Admin\MetaBox;
use Automattic\WooCommerce\GoogleListingsAndAds\Admin\Admin;
use Automattic\WooCommerce\GoogleListingsAndAds\Exception\InvalidValue;
use Automattic\WooCommerce\GoogleListingsAndAds\MerchantCenter\MerchantCenterService;
use Automattic\WooCommerce\GoogleListingsAndAds\PluginHelper;
use Automattic\WooCommerce\GoogleListingsAndAds\Product\ProductHelper;
use Automattic\WooCommerce\GoogleListingsAndAds\Product\ProductMetaHandler;
use Automattic\WooCommerce\GoogleListingsAndAds\Product\ProductSyncer;
use Automattic\WooCommerce\GoogleListingsAndAds\Value\ChannelVisibility;
use WC_Product;
use WP_Post;
defined( 'ABSPATH' ) || exit;
/**
* Class ChannelVisibilityMetaBox
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Admin\MetaBox
*/
class ChannelVisibilityMetaBox extends SubmittableMetaBox {
use PluginHelper;
/**
* @var ProductMetaHandler
*/
protected $meta_handler;
/**
* @var ProductHelper
*/
protected $product_helper;
/**
* @var MerchantCenterService
*/
protected $merchant_center;
/**
* ChannelVisibilityMetaBox constructor.
*
* @param Admin $admin
* @param ProductMetaHandler $meta_handler
* @param ProductHelper $product_helper
* @param MerchantCenterService $merchant_center
*/
public function __construct( Admin $admin, ProductMetaHandler $meta_handler, ProductHelper $product_helper, MerchantCenterService $merchant_center ) {
$this->meta_handler = $meta_handler;
$this->product_helper = $product_helper;
$this->merchant_center = $merchant_center;
parent::__construct( $admin );
}
/**
* Meta box ID (used in the 'id' attribute for the meta box).
*
* @return string
*/
public function get_id(): string {
return 'channel_visibility';
}
/**
* Title of the meta box.
*
* @return string
*/
public function get_title(): string {
return __( 'Channel visibility', 'google-listings-and-ads' );
}
/**
* The screen on which to show the box (such as a post type, 'link', or 'comment').
*
* Default is the current screen.
*
* @return string
*/
public function get_screen(): string {
return self::SCREEN_PRODUCT;
}
/**
* The context within the screen where the box should display. Available contexts vary from screen to
* screen. Post edit screen contexts include 'normal', 'side', and 'advanced'. Comments screen contexts
* include 'normal' and 'side'. Menus meta boxes (accordion sections) all use the 'side' context.
*
* Global default is 'advanced'.
*
* @return string
*/
public function get_context(): string {
return self::CONTEXT_SIDE;
}
/**
* Returns an array of CSS classes to apply to the box.
*
* @return array
*/
public function get_classes(): array {
return [ 'gla_meta_box' ];
}
/**
* Returns an array of variables to be used in the view.
*
* @param WP_Post $post The WordPress post object the box is loaded for.
* @param array $args Array of data passed to the callback. Defined by `get_callback_args`.
*
* @return array
*/
protected function get_view_context( WP_Post $post, array $args ): array {
$product_id = absint( $post->ID );
$product = $this->product_helper->get_wc_product( $product_id );
return [
'field_id' => $this->get_visibility_field_id(),
'product_id' => $product_id,
'product' => $product,
'channel_visibility' => $this->product_helper->get_channel_visibility( $product ),
'sync_status' => $this->meta_handler->get_sync_status( $product ),
'issues' => $this->product_helper->get_validation_errors( $product ),
'is_setup_complete' => $this->merchant_center->is_setup_complete(),
'get_started_url' => $this->get_start_url(),
];
}
/**
* Register a service.
*/
public function register(): void {
add_action( 'woocommerce_new_product', [ $this, 'handle_submission' ], 10, 2 );
add_action( 'woocommerce_update_product', [ $this, 'handle_submission' ], 10, 2 );
}
/**
* @param int $product_id
* @param WC_Product $product
*/
public function handle_submission( int $product_id, WC_Product $product ) {
/**
* Array of `true` values for each product IDs already handled by this method. Used to prevent double submission.
*
* @var bool[] $already_updated
*/
static $already_updated = [];
$field_id = $this->get_visibility_field_id();
// phpcs:disable WordPress.Security.NonceVerification
// nonce is verified by self::verify_nonce
if ( ! $this->verify_nonce() || ! isset( $_POST[ $field_id ] ) || isset( $already_updated[ $product_id ] ) ) {
return;
}
// only update the value for supported product types
if ( ! in_array( $product->get_type(), ProductSyncer::get_supported_product_types(), true ) ) {
return;
}
try {
$visibility = empty( $_POST[ $field_id ] ) ?
ChannelVisibility::cast( ChannelVisibility::SYNC_AND_SHOW ) :
ChannelVisibility::cast( sanitize_key( $_POST[ $field_id ] ) );
// phpcs:enable WordPress.Security.NonceVerification
$this->meta_handler->update_visibility( $product, $visibility );
$already_updated[ $product_id ] = true;
} catch ( InvalidValue $exception ) {
// silently log the exception and do not set the product's visibility if an invalid visibility value is sent.
do_action( 'woocommerce_gla_exception', $exception, __METHOD__ );
}
}
/**
* @return string
*
* @since 1.1.0
*/
protected function get_visibility_field_id(): string {
return $this->prefix_field_id( 'visibility' );
}
}
CouponChannelVisibilityMetaBox.php 0000644 00000014274 15154761761 0013366 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Admin\MetaBox;
use Automattic\WooCommerce\GoogleListingsAndAds\Admin\Admin;
use Automattic\WooCommerce\GoogleListingsAndAds\Exception\InvalidValue;
use Automattic\WooCommerce\GoogleListingsAndAds\Coupon\CouponHelper;
use Automattic\WooCommerce\GoogleListingsAndAds\Coupon\CouponMetaHandler;
use Automattic\WooCommerce\GoogleListingsAndAds\Coupon\CouponSyncer;
use Automattic\WooCommerce\GoogleListingsAndAds\MerchantCenter\MerchantCenterService;
use Automattic\WooCommerce\GoogleListingsAndAds\MerchantCenter\TargetAudience;
use Automattic\WooCommerce\GoogleListingsAndAds\Value\ChannelVisibility;
use WC_Coupon;
use WP_Post;
defined( 'ABSPATH' ) || exit;
/**
* Class CouponChannelVisibilityMetaBox
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Admin\MetaBox
*/
class CouponChannelVisibilityMetaBox extends SubmittableMetaBox {
/**
* @var CouponMetaHandler
*/
protected $meta_handler;
/**
* @var CouponHelper
*/
protected $coupon_helper;
/**
* @var MerchantCenterService
*/
protected $merchant_center;
/**
* @var TargetAudience
*/
protected $target_audience;
/**
* CouponChannelVisibilityMetaBox constructor.
*
* @param Admin $admin
* @param CouponMetaHandler $meta_handler
* @param CouponHelper $coupon_helper
* @param MerchantCenterService $merchant_center
* @param TargetAudience $target_audience
*/
public function __construct( Admin $admin, CouponMetaHandler $meta_handler, CouponHelper $coupon_helper, MerchantCenterService $merchant_center, TargetAudience $target_audience ) {
$this->meta_handler = $meta_handler;
$this->coupon_helper = $coupon_helper;
$this->merchant_center = $merchant_center;
$this->target_audience = $target_audience;
parent::__construct( $admin );
}
/**
* Meta box ID (used in the 'id' attribute for the meta box).
*
* @return string
*/
public function get_id(): string {
return 'coupon_channel_visibility';
}
/**
* Title of the meta box.
*
* @return string
*/
public function get_title(): string {
return __( 'Channel visibility', 'google-listings-and-ads' );
}
/**
* The screen on which to show the box (such as a post type, 'link', or 'comment').
*
* Default is the current screen.
*
* @return string
*/
public function get_screen(): string {
return self::SCREEN_COUPON;
}
/**
* The context within the screen where the box should display. Available contexts vary from screen to
* screen. Post edit screen contexts include 'normal', 'side', and 'advanced'. Comments screen contexts
* include 'normal' and 'side'. Menus meta boxes (accordion sections) all use the 'side' context.
*
* Global default is 'advanced'.
*
* @return string
*/
public function get_context(): string {
return self::CONTEXT_SIDE;
}
/**
* Returns an array of CSS classes to apply to the box.
*
* @return array
*/
public function get_classes(): array {
$shown_types = array_map(
function ( string $coupon_type ) {
return "show_if_{$coupon_type}";
},
CouponSyncer::get_supported_coupon_types()
);
$hidden_types = array_map(
function ( string $coupon_type ) {
return "hide_if_{$coupon_type}";
},
CouponSyncer::get_hidden_coupon_types()
);
return array_merge( $shown_types, $hidden_types );
}
/**
* Returns an array of variables to be used in the view.
*
* @param WP_Post $post The WordPress post object the box is loaded for.
* @param array $args Array of data passed to the callback. Defined by `get_callback_args`.
*
* @return array
*/
protected function get_view_context( WP_Post $post, array $args ): array {
$coupon_id = absint( $post->ID );
$coupon = $this->coupon_helper->get_wc_coupon( $coupon_id );
$target_country = $this->target_audience->get_main_target_country();
return [
'field_id' => $this->get_visibility_field_id(),
'coupon_id' => $coupon_id,
'coupon' => $coupon,
'channel_visibility' => $this->coupon_helper->get_channel_visibility( $coupon ),
'sync_status' => $this->meta_handler->get_sync_status( $coupon ),
'issues' => $this->coupon_helper->get_validation_errors( $coupon ),
'is_setup_complete' => $this->merchant_center->is_setup_complete(),
'is_channel_supported' => $this->merchant_center->is_promotion_supported_country( $target_country ),
'get_started_url' => $this->get_start_url(),
];
}
/**
* Register a service.
*/
public function register(): void {
add_action( 'woocommerce_new_coupon', [ $this, 'handle_submission' ], 10, 2 );
add_action( 'woocommerce_update_coupon', [ $this, 'handle_submission' ], 10, 2 );
}
/**
* @param int $coupon_id
* @param WC_Coupon $coupon
*/
public function handle_submission( int $coupon_id, WC_Coupon $coupon ) {
/**
* Array of `true` values for each coupon IDs already handled by this method. Used to prevent double submission.
*
* @var bool[] $already_updated
*/
static $already_updated = [];
$field_id = $this->get_visibility_field_id();
// phpcs:disable WordPress.Security.NonceVerification
// nonce is verified by self::verify_nonce
if ( ! $this->verify_nonce() || ! isset( $_POST[ $field_id ] ) || isset( $already_updated[ $coupon_id ] ) ) {
return;
}
// Only update the value for supported coupon types
if ( ! CouponSyncer::is_coupon_supported( $coupon ) ) {
return;
}
try {
$visibility = empty( $_POST[ $field_id ] ) ?
ChannelVisibility::cast( ChannelVisibility::DONT_SYNC_AND_SHOW ) :
ChannelVisibility::cast( sanitize_key( $_POST[ $field_id ] ) );
// phpcs:enable WordPress.Security.NonceVerification
$this->meta_handler->update_visibility( $coupon, $visibility );
$already_updated[ $coupon_id ] = true;
} catch ( InvalidValue $exception ) {
// silently log the exception and do not set the coupon's visibility if an invalid visibility value is sent.
do_action( 'woocommerce_gla_exception', $exception, __METHOD__ );
}
}
/**
* @return string
*
* @since 1.1.0
*/
protected function get_visibility_field_id(): string {
return $this->prefix_field_id( 'visibility' );
}
}
MetaBoxInitializer.php 0000644 00000002543 15154761762 0011042 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Admin\MetaBox;
use Automattic\WooCommerce\GoogleListingsAndAds\Admin\Admin;
use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\AdminConditional;
use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Conditional;
use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Registerable;
use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Service;
defined( 'ABSPATH' ) || exit;
/**
* Class MetaBoxInitializer
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Admin\MetaBox
*/
class MetaBoxInitializer implements Service, Registerable, Conditional {
use AdminConditional;
/**
* @var Admin
*/
protected $admin;
/**
* @var MetaBoxInterface[]
*/
protected $meta_boxes;
/**
* MetaBoxInitializer constructor.
*
* @param Admin $admin
* @param MetaBoxInterface[] $meta_boxes
*/
public function __construct( Admin $admin, array $meta_boxes ) {
$this->admin = $admin;
$this->meta_boxes = $meta_boxes;
}
/**
* Register a service.
*/
public function register(): void {
add_action( 'add_meta_boxes', [ $this, 'register_meta_boxes' ] );
}
/**
* Registers the meta boxes.
*/
public function register_meta_boxes() {
array_walk( $this->meta_boxes, [ $this->admin, 'add_meta_box' ] );
}
}
MetaBoxInterface.php 0000644 00000004405 15154761762 0010456 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Admin\MetaBox;
use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Renderable;
use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Service;
defined( 'ABSPATH' ) || exit;
interface MetaBoxInterface extends Renderable, Service {
public const SCREEN_PRODUCT = 'product';
public const SCREEN_COUPON = 'shop_coupon';
public const CONTEXT_NORMAL = 'normal';
public const CONTEXT_SIDE = 'side';
public const CONTEXT_ADVANCED = 'advanced';
public const PRIORITY_DEFAULT = 'default';
public const PRIORITY_LOW = 'low';
public const PRIORITY_CORE = 'core';
public const PRIORITY_HIGH = 'high';
/**
* Meta box ID (used in the 'id' attribute for the meta box).
*
* @return string
*/
public function get_id(): string;
/**
* Title of the meta box.
*
* @return string
*/
public function get_title(): string;
/**
* Function that fills the box with the desired content.
*
* The function should echo its output.
*
* @return callable
*/
public function get_callback(): callable;
/**
* The screen or screens on which to show the box (such as a post type, 'link', or 'comment').
*
* Default is the current screen.
*
* @return string
*/
public function get_screen(): string;
/**
* The context within the screen where the box should display. Available contexts vary from screen to
* screen. Post edit screen contexts include 'normal', 'side', and 'advanced'. Comments screen contexts
* include 'normal' and 'side'. Menus meta boxes (accordion sections) all use the 'side' context.
*
* Global default is 'advanced'.
*
* @return string
*/
public function get_context(): string;
/***
* The priority within the context where the box should show.
*
* Accepts 'high', 'core', 'default', or 'low'. Default 'default'.
*
* @return string
*/
public function get_priority(): string;
/**
* Data that should be set as the $args property of the box array (which is the second parameter passed to your callback).
*
* @return array
*/
public function get_callback_args(): array;
/**
* Returns an array of CSS classes to apply to the box.
*
* @return array
*/
public function get_classes(): array;
}
SubmittableMetaBox.php 0000644 00000001336 15154761762 0011031 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Admin\MetaBox;
use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Registerable;
defined( 'ABSPATH' ) || exit;
/**
* Class SubmittableMetaBox
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Admin\MetaBox
*/
abstract class SubmittableMetaBox extends AbstractMetaBox implements Registerable {
/**
* Verifies the WooCommerce meta box nonce.
*
* @return bool True is nonce is provided and valid, false otherwise.
*/
protected function verify_nonce(): bool {
return ! empty( $_POST['woocommerce_meta_nonce'] ) && wp_verify_nonce( sanitize_key( $_POST['woocommerce_meta_nonce'] ), 'woocommerce_save_data' );
}
}