File: /var/www/vhosts/uyarreklam.com.tr/httpdocs/Notifications.tar
AbstractItemNotificationJob.php 0000644 00000010505 15154716021 0012644 0 ustar 00 <?php
declare(strict_types=1);
namespace Automattic\WooCommerce\GoogleListingsAndAds\Jobs\Notifications;
use Automattic\WooCommerce\GoogleListingsAndAds\Exception\InvalidValue;
use Automattic\WooCommerce\GoogleListingsAndAds\Value\NotificationStatus;
defined( 'ABSPATH' ) || exit;
/**
* Class AbstractItemNotificationJob
* Generic class for the Notification Jobs containing items
*
* @since 2.8.0
* @package Automattic\WooCommerce\GoogleListingsAndAds\Jobs\Notifications
*/
abstract class AbstractItemNotificationJob extends AbstractNotificationJob {
/**
* Logic when processing the items
*
* @param array $args Arguments with the item id and the topic
*/
protected function process_items( array $args ): void {
if ( ! isset( $args['item_id'] ) || ! isset( $args['topic'] ) ) {
do_action(
'woocommerce_gla_error',
'Error sending the Notification. Topic and Item ID are mandatory',
__METHOD__
);
return;
}
$item = $args['item_id'];
$topic = $args['topic'];
$data = $args['data'] ?? [];
try {
if ( $this->can_process( $item, $topic ) && $this->notifications_service->notify( $topic, $item, $data ) ) {
$this->set_status( $item, $this->get_after_notification_status( $topic ) );
$this->handle_notified( $topic, $item );
}
} catch ( InvalidValue $e ) {
do_action(
'woocommerce_gla_error',
sprintf( 'Error sending Notification for - Item ID: %s - Topic: %s - Data %s. Product was deleted from the database before the notification was sent.', $item, $topic, wp_json_encode( $data ) ),
__METHOD__
);
}
}
/**
* Set the notification status for the item.
*
* @param int $item_id
* @param string $status
* @throws InvalidValue If the given ID doesn't reference a valid product.
*/
protected function set_status( int $item_id, string $status ): void {
$item = $this->get_item( $item_id );
$this->get_helper()->set_notification_status( $item, $status );
}
/**
* Get the Notification Status after the notification happens
*
* @param string $topic
* @return string
*/
protected function get_after_notification_status( string $topic ): string {
if ( $this->is_create_topic( $topic ) ) {
return NotificationStatus::NOTIFICATION_CREATED;
} elseif ( $this->is_delete_topic( $topic ) ) {
return NotificationStatus::NOTIFICATION_DELETED;
} else {
return NotificationStatus::NOTIFICATION_UPDATED;
}
}
/**
* Checks if the item can be processed based on the topic.
* This is needed because the item can change the Notification Status before
* the Job process the item.
*
* @param int $item_id
* @param string $topic
* @throws InvalidValue If the given ID doesn't reference a valid product.
* @return bool
*/
protected function can_process( int $item_id, string $topic ): bool {
$item = $this->get_item( $item_id );
if ( $this->is_create_topic( $topic ) ) {
return $this->get_helper()->should_trigger_create_notification( $item );
} elseif ( $this->is_delete_topic( $topic ) ) {
return $this->get_helper()->should_trigger_delete_notification( $item );
} else {
return $this->get_helper()->should_trigger_update_notification( $item );
}
}
/**
* Handle the item after the notification.
*
* @param string $topic
* @param int $item
* @throws InvalidValue If the given ID doesn't reference a valid product.
*/
protected function handle_notified( string $topic, int $item ): void {
if ( $this->is_delete_topic( $topic ) ) {
$this->get_helper()->mark_as_unsynced( $this->get_item( $item ) );
}
if ( $this->is_create_topic( $topic ) ) {
$this->get_helper()->mark_as_notified( $this->get_item( $item ) );
}
}
/**
* If a topic is a delete topic
*
* @param string $topic The topic to check
*
* @return bool
*/
protected function is_delete_topic( $topic ): bool {
return str_contains( $topic, '.delete' );
}
/**
* If a topic is a create topic
*
* @param string $topic The topic to check
*
* @return bool
*/
protected function is_create_topic( $topic ): bool {
return str_contains( $topic, '.create' );
}
/**
* Get the item
*
* @param int $item_id
* @return \WC_Product|\WC_Coupon
*/
abstract protected function get_item( int $item_id );
/**
* Get the helper
*
* @return HelperNotificationInterface
*/
abstract public function get_helper(): HelperNotificationInterface;
}
AbstractNotificationJob.php 0000644 00000005372 15154716021 0012033 0 ustar 00 <?php
declare(strict_types=1);
namespace Automattic\WooCommerce\GoogleListingsAndAds\Jobs\Notifications;
use Automattic\WooCommerce\GoogleListingsAndAds\ActionScheduler\ActionSchedulerInterface;
use Automattic\WooCommerce\GoogleListingsAndAds\API\WP\NotificationsService;
use Automattic\WooCommerce\GoogleListingsAndAds\Jobs\AbstractActionSchedulerJob;
use Automattic\WooCommerce\GoogleListingsAndAds\Jobs\ActionSchedulerJobMonitor;
use Automattic\WooCommerce\GoogleListingsAndAds\Jobs\JobInterface;
defined( 'ABSPATH' ) || exit;
/**
* Class AbstractNotificationJob
* Generic class for the Notifications Jobs
*
* @since 2.8.0
* @package Automattic\WooCommerce\GoogleListingsAndAds\Jobs\Notifications
*/
abstract class AbstractNotificationJob extends AbstractActionSchedulerJob implements JobInterface {
/**
* @var NotificationsService $notifications_service
*/
protected $notifications_service;
/**
* Notifications Jobs constructor.
*
* @param ActionSchedulerInterface $action_scheduler
* @param ActionSchedulerJobMonitor $monitor
* @param NotificationsService $notifications_service
*/
public function __construct(
ActionSchedulerInterface $action_scheduler,
ActionSchedulerJobMonitor $monitor,
NotificationsService $notifications_service
) {
$this->notifications_service = $notifications_service;
parent::__construct( $action_scheduler, $monitor );
}
/**
* Get the parent job name
*
* @return string
*/
public function get_name(): string {
return 'notifications/' . $this->get_job_name();
}
/**
* Schedule the Job
*
* @param array $args
*/
public function schedule( array $args = [] ): void {
if ( $this->can_schedule( [ $args ] ) ) {
$this->action_scheduler->schedule_immediate(
$this->get_process_item_hook(),
[ $args ]
);
}
}
/**
* Can the job be scheduled.
*
* @param array|null $args
*
* @return bool Returns true if the job can be scheduled.
*/
public function can_schedule( $args = [] ): bool {
/**
* Allow users to disable the notification job schedule.
*
* @since 2.8.0
*
* @param bool $value The current filter value. By default, it is the result of `$this->can_schedule` function.
* @param string $job_name The current Job name.
* @param array $args The arguments for the schedule function with the item id and the topic.
*/
return apply_filters( 'woocommerce_gla_notification_job_can_schedule', $this->notifications_service->is_ready() && parent::can_schedule( $args ), $this->get_job_name(), $args );
}
/**
* Get the child job name
*
* @return string
*/
abstract public function get_job_name(): string;
/**
* Logic when processing the items
*
* @param array $args
*/
abstract protected function process_items( array $args ): void;
}
CouponNotificationJob.php 0000644 00000003606 15154716021 0011531 0 ustar 00 <?php
declare(strict_types=1);
namespace Automattic\WooCommerce\GoogleListingsAndAds\Jobs\Notifications;
use Automattic\WooCommerce\GoogleListingsAndAds\ActionScheduler\ActionSchedulerInterface;
use Automattic\WooCommerce\GoogleListingsAndAds\API\WP\NotificationsService;
use Automattic\WooCommerce\GoogleListingsAndAds\Coupon\CouponHelper;
use Automattic\WooCommerce\GoogleListingsAndAds\Jobs\ActionSchedulerJobMonitor;
use Automattic\WooCommerce\GoogleListingsAndAds\Jobs\Notifications\HelperNotificationInterface;
defined( 'ABSPATH' ) || exit;
/**
* Class CouponNotificationJob
* Class for the Coupons Notifications Jobs
*
* @since 2.8.0
* @package Automattic\WooCommerce\GoogleListingsAndAds\Jobs\Notifications
*/
class CouponNotificationJob extends AbstractItemNotificationJob {
/**
* @var CouponHelper $helper
*/
protected $helper;
/**
* Notifications Jobs constructor.
*
* @param ActionSchedulerInterface $action_scheduler
* @param ActionSchedulerJobMonitor $monitor
* @param NotificationsService $notifications_service
* @param HelperNotificationInterface $coupon_helper
*/
public function __construct(
ActionSchedulerInterface $action_scheduler,
ActionSchedulerJobMonitor $monitor,
NotificationsService $notifications_service,
HelperNotificationInterface $coupon_helper
) {
$this->helper = $coupon_helper;
parent::__construct( $action_scheduler, $monitor, $notifications_service );
}
/**
* Get the coupon
*
* @param int $item_id
* @return \WC_Coupon
*/
protected function get_item( int $item_id ) {
return $this->helper->get_wc_coupon( $item_id );
}
/**
* Get the Coupon Helper
*
* @return HelperNotificationInterface
*/
public function get_helper(): HelperNotificationInterface {
return $this->helper;
}
/**
* Get the job name
*
* @return string
*/
public function get_job_name(): string {
return 'coupons';
}
}
HelperNotificationInterface.php 0000644 00000002714 15154716021 0012672 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Jobs\Notifications;
defined( 'ABSPATH' ) || exit;
/**
* Interface HelperNotificationInterface
*
* @since 2.8.0
* @package Automattic\WooCommerce\GoogleListingsAndAds\Jobs\Notifications
*/
interface HelperNotificationInterface {
/**
* Checks if the item can be processed based on the topic.
*
* @param WC_Product|WC_Coupon $item
*
* @return bool
*/
public function should_trigger_create_notification( $item ): bool;
/**
* Indicates if the item ready for sending a delete Notification.
*
* @param WC_Product|WC_Coupon $item
*
* @return bool
*/
public function should_trigger_delete_notification( $item ): bool;
/**
* Indicates if the item ready for sending an update Notification.
*
* @param WC_Product|WC_Coupon $item
*
* @return bool
*/
public function should_trigger_update_notification( $item ): bool;
/**
* Marks the item as unsynced.
*
* @param WC_Product|WC_Coupon $item
*
* @return void
*/
public function mark_as_unsynced( $item ): void;
/**
* Set the notification status for an item.
*
* @param WC_Product|WC_Coupon $item
* @param string $status
*
* @return void
*/
public function set_notification_status( $item, $status ): void;
/**
* Marks the item as notified.
*
* @param WC_Product|WC_Coupon $item
*
* @return void
*/
public function mark_as_notified( $item ): void;
}
ProductNotificationJob.php 0000644 00000005143 15154716021 0011704 0 ustar 00 <?php
declare(strict_types=1);
namespace Automattic\WooCommerce\GoogleListingsAndAds\Jobs\Notifications;
use Automattic\WooCommerce\GoogleListingsAndAds\ActionScheduler\ActionSchedulerInterface;
use Automattic\WooCommerce\GoogleListingsAndAds\API\WP\NotificationsService;
use Automattic\WooCommerce\GoogleListingsAndAds\Exception\InvalidValue;
use Automattic\WooCommerce\GoogleListingsAndAds\Jobs\ActionSchedulerJobMonitor;
use Automattic\WooCommerce\GoogleListingsAndAds\PluginHelper;
use Automattic\WooCommerce\GoogleListingsAndAds\Product\ProductHelper;
defined( 'ABSPATH' ) || exit;
/**
* Class ProductNotificationJob
* Class for the Product Notifications Jobs
*
* @since 2.8.0
* @package Automattic\WooCommerce\GoogleListingsAndAds\Jobs\Notifications
*/
class ProductNotificationJob extends AbstractItemNotificationJob {
use PluginHelper;
/**
* @var ProductHelper $helper
*/
protected $helper;
/**
* Notifications Jobs constructor.
*
* @param ActionSchedulerInterface $action_scheduler
* @param ActionSchedulerJobMonitor $monitor
* @param NotificationsService $notifications_service
* @param HelperNotificationInterface $helper
*/
public function __construct(
ActionSchedulerInterface $action_scheduler,
ActionSchedulerJobMonitor $monitor,
NotificationsService $notifications_service,
HelperNotificationInterface $helper
) {
$this->helper = $helper;
parent::__construct( $action_scheduler, $monitor, $notifications_service );
}
/**
* Override Product Notification adding Offer ID for deletions.
* The offer_id might match the real offer ID or not, depending on whether the product has been synced by us or not.
* Google should check on their side if the product actually exists.
*
* @param array $args Arguments with the item id and the topic.
*/
protected function process_items( $args ): void {
if ( isset( $args['topic'] ) && isset( $args['item_id'] ) && $this->is_delete_topic( $args['topic'] ) ) {
$args['data'] = [ 'offer_id' => $this->helper->get_offer_id( $args['item_id'] ) ];
}
parent::process_items( $args );
}
/**
* Get the product
*
* @param int $item_id
* @throws InvalidValue If the given ID doesn't reference a valid product.
*
* @return \WC_Product
*/
protected function get_item( int $item_id ) {
return $this->helper->get_wc_product( $item_id );
}
/**
* Get the Product Helper
*
* @return ProductHelper
*/
public function get_helper(): HelperNotificationInterface {
return $this->helper;
}
/**
* Get the job name
*
* @return string
*/
public function get_job_name(): string {
return 'products';
}
}
SettingsNotificationJob.php 0000644 00000001372 15154716021 0012064 0 ustar 00 <?php
declare(strict_types=1);
namespace Automattic\WooCommerce\GoogleListingsAndAds\Jobs\Notifications;
defined( 'ABSPATH' ) || exit;
/**
* Class SettingsNotificationJob
* Class for the Settings Notifications
*
* @since 2.8.0
* @package Automattic\WooCommerce\GoogleListingsAndAds\Jobs\Notifications
*/
class SettingsNotificationJob extends AbstractNotificationJob {
/**
* Logic when processing the items
*
* @param array $args Arguments for the notification
*/
protected function process_items( array $args ): void {
$this->notifications_service->notify( $this->notifications_service::TOPIC_SETTINGS_UPDATED );
}
/**
* Get the job name
*
* @return string
*/
public function get_job_name(): string {
return 'settings';
}
}
ShippingNotificationJob.php 0000644 00000001407 15154716021 0012044 0 ustar 00 <?php
declare(strict_types=1);
namespace Automattic\WooCommerce\GoogleListingsAndAds\Jobs\Notifications;
defined( 'ABSPATH' ) || exit;
/**
* Class ShippingNotificationJob
* Class for the Shipping Notifications
*
* @since 2.8.0
* @package Automattic\WooCommerce\GoogleListingsAndAds\Jobs\Notifications
*/
class ShippingNotificationJob extends AbstractNotificationJob {
/**
* Get the job name
*
* @return string
*/
public function get_job_name(): string {
return 'shipping';
}
/**
* Logic when processing the items
*
* @param array $args Arguments for the notification
*/
protected function process_items( array $args ): void {
$this->notifications_service->notify( $this->notifications_service::TOPIC_SHIPPING_UPDATED, null, $args );
}
}