reader_revenue_manager = $reader_revenue_manager; $this->user_options = $user_options; } /** * Registers functionality through WordPress hooks. * * @since 1.146.0 * * @return void */ public function register() { add_action( self::CRON_SYNCHRONIZE_PUBLICATION, function () { $this->synchronize_publication_data(); } ); } /** * Cron callback for synchronizing the publication. * * @since 1.146.0 * * @return void */ protected function synchronize_publication_data() { $owner_id = $this->reader_revenue_manager->get_owner_id(); $restore_user = $this->user_options->switch_user( $owner_id ); if ( user_can( $owner_id, Permissions::VIEW_AUTHENTICATED_DASHBOARD ) ) { $connected = $this->reader_revenue_manager->is_connected(); // If not connected, return early. if ( ! $connected ) { return; } $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 ) { return $pub->getPublicationId() === $publication_id; } ); // If there are no filtered publications, return early. if ( empty( $filtered_publications ) ) { return; } // Re-index the filtered array to ensure sequential keys. $filtered_publications = array_values( $filtered_publications ); $publication = $filtered_publications[0]; $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 1.146.0 * * @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 1.146.0 * * @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.146.0 * * @return void */ public function maybe_schedule_synchronize_publication() { $connected = $this->reader_revenue_manager->is_connected(); $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_PUBLICATION ); } } }