$retry_time = get_option( Actions::RETRY_AFTER_PREFIX . 'immediate-send' ); if ( $retry_time ) { // If expired delete but don't send. Send will occurr in new request to avoid race conditions. if ( microtime( true ) > $retry_time ) { update_option( Actions::RETRY_AFTER_PREFIX . 'immediate-send', false, false ); } return false; } // Obtain send Lock. $lock = new Lock(); $lock_expiration = $lock->attempt( self::LOCK_NAME ); // Return if unable to obtain lock. if ( false === $lock_expiration ) { return; } // Send Full Sync actions. $success = $this->send(); // Remove lock. if ( $success ) { $lock->remove( self::LOCK_NAME, $lock_expiration ); } } /** * Immediately send the next items to full sync. * * @access public */ public function send() { $config = $this->get_status()['config']; $max_duration = Settings::get_setting( 'full_sync_send_duration' ); $send_until = microtime( true ) + $max_duration; $progress = $this->get_status()['progress']; foreach ( $this->get_remaining_modules_to_send() as $module ) { $progress[ $module->name() ] = $module->send_full_sync_actions( $config[ $module->name() ], $progress[ $module->name() ], $send_until ); if ( isset( $progress[ $module->name() ]['error'] ) ) { unset( $progress[ $module->name() ]['error'] ); $this->update_status( array( 'progress' => $progress ) ); return false; } elseif ( ! $progress[ $module->name() ]['finished'] ) { $this->update_status( array( 'progress' => $progress ) ); return true; } } $this->send_full_sync_end(); $this->update_status( array( 'progress' => $progress ) ); return true; } /** * Get Modules that are configured to Full Sync and haven't finished sending * * @return array */ public function get_remaining_modules_to_send() { $status = $this->get_status(); return array_filter( Modules::get_modules(), /** * Select configured and not finished modules. * * @return bool * @var $module Module */ function ( $module ) use ( $status ) { // Skip module if not configured for this sync or module is done. if ( ! isset( $status['config'][ $module->name() ] ) ) { return false; } if ( ! $status['config'][ $module->name() ] ) { return false; } if ( isset( $status['progress'][ $module->name() ]['finished'] ) ) { if ( true === $status['progress'][ $module->name() ]['finished'] ) { return false; } } return true; } ); } /** * Send 'jetpack_full_sync_end' and update 'finished' status. * * @access public */ public function send_full_sync_end() { $range = $this->get_content_range(); /** * Fires when a full sync ends. This action is serialized * and sent to the server. * * @param string $checksum Deprecated since 7.3.0 - @see https://github.com/Automattic/jetpack/pull/11945/ * @param array $range Range of the sync items, containing min and max IDs for some item types. * * @since 4.2.0 * @since 7.3.0 Added $range arg. */ do_action( 'jetpack_full_sync_end', '', $range ); $this->send_action( 'jetpack_full_sync_end', array( '', $range ) ); // Setting autoload to true means that it's faster to check whether we should continue enqueuing. $this->update_status( array( 'finished' => time() ) ); } /** * Empty Function as we don't close buffers on Immediate Full Sync. * * @param array $actions an array of actions, ignored for queueless sync. */ public function update_sent_progress_action( $actions ) { } // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable }