diff --git a/includes/Framework/BatchLogHandler.php b/includes/Framework/BatchLogHandler.php new file mode 100644 index 000000000..dfd8e8e46 --- /dev/null +++ b/includes/Framework/BatchLogHandler.php @@ -0,0 +1,52 @@ +add_hooks(); + + // build the batch log handler instance + $this->init_batch_log_handler(); } @@ -176,6 +182,15 @@ protected function init_lifecycle_handler() { $this->lifecycle_handler = new \WooCommerce\Facebook\Lifecycle( $this ); } + /** + * Builds the batch log handler instance. + * + * @since 3.5.0 + */ + protected function init_batch_log_handler() { + $this->batch_log_handler = new BatchLogHandler(); + } + /** * Adds the action & filter hooks. diff --git a/includes/Utilities/Heartbeat.php b/includes/Utilities/Heartbeat.php index d2c458de9..ce2db1bb6 100644 --- a/includes/Utilities/Heartbeat.php +++ b/includes/Utilities/Heartbeat.php @@ -16,6 +16,11 @@ */ class Heartbeat { + /** + * Hook name for hourly heartbeat. + */ + const EVERY_5_MINUTES = 'facebook_for_woocommerce_5_minute_heartbeat'; + /** * Hook name for hourly heartbeat. */ @@ -36,6 +41,11 @@ class Heartbeat { */ protected $daily_cron_name = 'facebook_for_woocommerce_daily_heartbeat_cron'; + /** + * @var string + */ + protected $every_5_minute_cron_name = 'facebook_for_woocommerce_5_minute_heartbeat_cron'; + /** * @var WC_Queue_Interface */ @@ -54,9 +64,11 @@ public function __construct( WC_Queue_Interface $queue ) { * Add hooks. */ public function init() { + add_filter( 'cron_schedules', array( $this, 'five_minutes_cron_schedules' ) ); add_action( 'init', array( $this, 'schedule_cron_events' ) ); add_action( $this->hourly_cron_name, array( $this, 'schedule_hourly_action' ) ); add_action( $this->daily_cron_name, array( $this, 'schedule_daily_action' ) ); + add_action( $this->every_5_minute_cron_name, array( $this, 'schedule_every_5_minute_action' ) ); } /** @@ -72,6 +84,27 @@ public function schedule_cron_events() { if ( ! wp_next_scheduled( $this->daily_cron_name ) ) { wp_schedule_event( time(), 'daily', $this->daily_cron_name ); } + if ( ! wp_next_scheduled( $this->every_5_minute_cron_name ) ) { + wp_schedule_event( time(), 'five_minutes', $this->every_5_minute_cron_name ); + } + } + + /** + * Function that add a defination of interval for cron job + * + * @param string $schedules pluin system data + * + * @since 3.5.0 + * + * @internal + */ + public function five_minutes_cron_schedules( $schedules ) { + $schedules['five_minutes'] = array( + 'interval' => 300, + 'display' => __( 'Five Minutes', 'facebook-for-woocommerce' ), + ); + + return $schedules; } /** @@ -90,4 +123,11 @@ public function schedule_hourly_action() { public function schedule_daily_action() { $this->queue->add( self::DAILY ); } + + /** + * Schedule the every 5 minute heartbeat action to run immediately. + */ + public function schedule_every_5_minute_action() { + $this->queue->add( self::EVERY_5_MINUTES ); + } } diff --git a/includes/fbutils.php b/includes/fbutils.php index b5a9bc64d..e292a52d4 100644 --- a/includes/fbutils.php +++ b/includes/fbutils.php @@ -860,6 +860,7 @@ public static function prepare_product_variation_data_items_batch( $product ) { /** * Utility function for sending exception logs to Meta. + * @since 3.5.0 */ public static function logExceptionImmediatelyToMeta(Throwable $error, array $context = []) { /** @@ -876,6 +877,7 @@ public static function logExceptionImmediatelyToMeta(Throwable $error, array $co /** * Utility function for sending telemetry logs to Meta. + * @since 3.5.0 */ public static function logTelemetryToMeta(string $message, array $context = []) { /** @@ -883,11 +885,12 @@ public static function logTelemetryToMeta(string $message, array $context = []) * $context is an array of data that will be sent to Meta, includes commerce_merchant_settings_id, * catalog_id, order_id, promotion_id, flow_name, flow_step, extra_data and etc. */ - - // TODO: Implement push logging request to global message queue function. - $response = null; - - return $response; + + // Push logging request to global message queue function. + $context['extra_data'] = ['message' => $message]; + $logs = get_transient( 'global_telemetry_message_queue' ); + $logs[] = $context; + set_transient( 'global_telemetry_message_queue', $logs, HOUR_IN_SECONDS ); } /**