Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add synchronization of RRM publication information #10087

Merged
merged 7 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions includes/Modules/Reader_Revenue_Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
use Google\Site_Kit\Modules\Reader_Revenue_Manager\Post_Product_ID;
use Google\Site_Kit\Modules\Reader_Revenue_Manager\Term_Product_ID;
use Google\Site_Kit\Modules\Reader_Revenue_Manager\Settings;
use Google\Site_Kit\Modules\Reader_Revenue_Manager\Synchronize_OnboardingState;
use Google\Site_Kit\Modules\Reader_Revenue_Manager\Synchronize_Publication;
use Google\Site_Kit\Modules\Reader_Revenue_Manager\Tag_Guard;
use Google\Site_Kit\Modules\Reader_Revenue_Manager\Tag_Matchers;
use Google\Site_Kit\Modules\Reader_Revenue_Manager\Web_Tag;
Expand Down Expand Up @@ -75,11 +75,11 @@ final class Reader_Revenue_Manager extends Module implements Module_With_Scopes,
public function register() {
$this->register_scopes_hook();

$synchronize_onboarding_state = new Synchronize_OnboardingState(
$synchronize_publication = new Synchronize_Publication(
$this,
$this->user_options
);
$synchronize_onboarding_state->register();
$synchronize_publication->register();

if ( Feature_Flags::enabled( 'rrmModuleV2' ) && $this->is_connected() ) {
$post_meta = new Post_Meta();
Expand All @@ -92,8 +92,8 @@ public function register() {
$term_product_id->register();
}

add_action( 'load-toplevel_page_googlesitekit-dashboard', array( $synchronize_onboarding_state, 'maybe_schedule_synchronize_onboarding_state' ) );
add_action( 'load-toplevel_page_googlesitekit-settings', array( $synchronize_onboarding_state, 'maybe_schedule_synchronize_onboarding_state' ) );
add_action( 'load-toplevel_page_googlesitekit-dashboard', array( $synchronize_publication, 'maybe_schedule_synchronize_publication' ) );
add_action( 'load-toplevel_page_googlesitekit-settings', array( $synchronize_publication, 'maybe_schedule_synchronize_publication' ) );

// Reader Revenue Manager tag placement logic.
add_action( 'template_redirect', array( $this, 'register_tag' ) );
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
/**
* Class Google\Site_Kit\Modules\Reader_Revenue_Manager\Synchronize_OnboardingState
* Class Google\Site_Kit\Modules\Reader_Revenue_Manager\Synchronize_Publication
*
* @package Google\Site_Kit\Modules\Reader_Revenue_Manager
* @copyright 2024 Google LLC
* @copyright 2025 Google LLC
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @link https://sitekit.withgoogle.com
*/
Expand All @@ -12,20 +12,23 @@

use Google\Site_Kit\Core\Permissions\Permissions;
use Google\Site_Kit\Core\Storage\User_Options;
use Google\Site_Kit\Core\Util\Feature_Flags;
use Google\Site_Kit\Modules\Reader_Revenue_Manager;
use Google\Site_Kit_Dependencies\Google\Service\SubscribewithGoogle\Publication;
use Google\Site_Kit_Dependencies\Google\Service\SubscribewithGoogle\PaymentOptions;

/**
* Class for synchronizing the onboarding state.
*
* @since 1.141.0
* @since n.e.x.t
* @access private
* @ignore
*/
class Synchronize_OnboardingState {
class Synchronize_Publication {
/**
* Cron event name for synchronizing the onboarding state.
* Cron event name for synchronizing the publication info.
*/
const CRON_SYNCHRONIZE_ONBOARDING_STATE = 'googlesitekit_cron_synchronize_onboarding_state';
const CRON_SYNCHRONIZE_PUBLICATION = 'googlesitekit_cron_synchronize_publication';

/**
* Reader_Revenue_Manager instance.
Expand All @@ -44,7 +47,7 @@ class Synchronize_OnboardingState {
/**
* Constructor.
*
* @since 1.141.0
* @since n.e.x.t
*
* @param Reader_Revenue_Manager $reader_revenue_manager Reader Revenue Manager instance.
* @param User_Options $user_options User_Options instance.
Expand All @@ -57,13 +60,13 @@ public function __construct( Reader_Revenue_Manager $reader_revenue_manager, Use
/**
* Registers functionality through WordPress hooks.
*
* @since 1.141.0
* @since n.e.x.t
*
* @return void
*/
public function register() {
add_action(
self::CRON_SYNCHRONIZE_ONBOARDING_STATE,
self::CRON_SYNCHRONIZE_PUBLICATION,
function () {
$this->synchronize_publication_data();
}
Expand All @@ -73,7 +76,7 @@ function () {
/**
* Cron callback for synchronizing the publication.
*
* @since 1.141.0
* @since n.e.x.t
*
* @return void
*/
Expand All @@ -82,39 +85,23 @@ protected function synchronize_publication_data() {
$restore_user = $this->user_options->switch_user( $owner_id );

if ( user_can( $owner_id, Permissions::VIEW_AUTHENTICATED_DASHBOARD ) ) {
$this->synchronize_onboarding_state();
}

$restore_user();
}

/**
* Synchronizes the onboarding state.
*
* @since 1.141.0
*
* @return void
*/
protected function synchronize_onboarding_state() {
$connected = $this->reader_revenue_manager->is_connected();

// If not connected, return early.
if ( ! $connected ) {
return;
}
$connected = $this->reader_revenue_manager->is_connected();

$settings = $this->reader_revenue_manager->get_settings()->get();
$publication_id = $settings['publicationID'];
$onboarding_state = $settings['publicationOnboardingState'];
// If not connected, return early.
if ( ! $connected ) {
return;
}

if ( 'ONBOARDING_COMPLETE' !== $onboarding_state ) {
$publications = $this->reader_revenue_manager->get_data( 'publications' );

// If publications is empty, return early.
if ( empty( $publications ) ) {
return;
}

$settings = $this->reader_revenue_manager->get_settings()->get();
$publication_id = $settings['publicationID'];

$filtered_publications = array_filter(
$publications,
function ( $pub ) use ( $publication_id ) {
Expand All @@ -131,32 +118,95 @@ function ( $pub ) use ( $publication_id ) {
$filtered_publications = array_values( $filtered_publications );
$publication = $filtered_publications[0];

if ( $publication->getOnboardingState() !== $onboarding_state ) {
$this->reader_revenue_manager->get_settings()->merge(
array(
'publicationOnboardingState' => $publication->getOnboardingState(),
'publicationOnboardingStateChanged' => true,
)
);
$onboarding_state = $settings['publicationOnboardingState'];
$new_onboarding_state = $publication->getOnboardingState();

$new_settings = array(
'publicationOnboardingState' => $new_onboarding_state,
);

// Let the client know if the onboarding state has changed.
if ( $new_onboarding_state !== $onboarding_state ) {
$new_settings['publicationOnboardingStateChanged'] = true;
}

if ( Feature_Flags::enabled( 'rrmModuleV2' ) ) {
$new_settings['productIDs'] = $this->get_product_ids( $publication );
$new_settings['paymentOption'] = $this->get_payment_option( $publication );
}

$this->reader_revenue_manager->get_settings()->merge( $new_settings );
}

$restore_user();
}

/**
* Returns the products IDs for the given publication.
*
* @since n.e.x.t
*
* @param Publication $publication Publication object.
* @return array Product IDs.
*/
protected function get_product_ids( Publication $publication ) {
$products = $publication->getProducts();
$product_ids = array();

if ( ! empty( $products ) ) {
foreach ( $products as $product ) {
$name = $product->getName();

// Extract the product ID from the name, which is in
// the format of `publicationID:productID`.
if ( strpos( $name, ':' ) !== false ) {
$product_ids[] = substr( $name, strpos( $name, ':' ) + 1 );
}
}
}

return $product_ids;
}

/**
* Returns the payment option for the given publication.
*
* @since n.e.x.t
*
* @param Publication $publication Publication object.
* @return string Payment option.
*/
protected function get_payment_option( Publication $publication ) {
$payment_options = $publication->getPaymentOptions();
$payment_option = '';

if ( $payment_options instanceof PaymentOptions ) {
foreach ( $payment_options as $option => $value ) {
if ( true === $value ) {
$payment_option = $option;
break;
}
}
}

return $payment_option;
}

/**
* Maybe schedule the synchronize onboarding state cron event.
*
* @since 1.141.0
* @since n.e.x.t
*
* @return void
*/
public function maybe_schedule_synchronize_onboarding_state() {
public function maybe_schedule_synchronize_publication() {
$connected = $this->reader_revenue_manager->is_connected();
$cron_already_scheduled = wp_next_scheduled( self::CRON_SYNCHRONIZE_ONBOARDING_STATE );
$cron_already_scheduled = wp_next_scheduled( self::CRON_SYNCHRONIZE_PUBLICATION );

if ( $connected && ! $cron_already_scheduled ) {
wp_schedule_single_event(
time() + HOUR_IN_SECONDS,
self::CRON_SYNCHRONIZE_ONBOARDING_STATE
self::CRON_SYNCHRONIZE_PUBLICATION
);
}
}
Expand Down
Loading
Loading