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

fix(nrh): fixes for ESP contact syncing when Reader Revenue platform is NRH #3779

Open
wants to merge 6 commits into
base: release
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
3 changes: 3 additions & 0 deletions includes/cli/class-ras-esp-sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,9 @@ private static function sync_contacts( $config ) {
* @return bool
*/
private static function user_has_active_subscriptions( $user_id ) {
if ( ! function_exists( 'wcs_get_users_subscriptions' ) ) {
return false;
}
$subcriptions = array_reduce(
array_keys( \wcs_get_users_subscriptions( $user_id ) ),
function( $acc, $subscription_id ) {
Expand Down
3 changes: 2 additions & 1 deletion includes/plugins/class-teams-for-memberships.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

defined( 'ABSPATH' ) || exit;

use Newspack\Donations;
use Newspack\Reader_Activation\Sync;

/**
Expand All @@ -31,7 +32,7 @@ public static function init() {
* @return bool True if enabled, false otherwise.
*/
private static function is_enabled() {
return class_exists( 'WC_Memberships_For_Teams_Loader' );
return Donations::is_platform_wc() && class_exists( 'WC_Memberships_For_Teams_Loader' );
}

/**
Expand Down
2 changes: 0 additions & 2 deletions includes/reader-activation/sync/class-esp-sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,6 @@ public static function sync( $contact, $context = '' ) {
*/
$contact = \apply_filters( 'newspack_esp_sync_contact', $contact, $context );

$contact = Sync\Metadata::normalize_contact_data( $contact );

$result = \Newspack_Newsletters_Contacts::upsert( $contact, $master_list_id, $context );

return \is_wp_error( $result ) ? $result : true;
Expand Down
14 changes: 13 additions & 1 deletion includes/reader-activation/sync/class-metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Newspack\Reader_Activation\Sync;

use Newspack\Donations;
use Newspack\Reader_Activation;
use Newspack\Logger;

Expand Down Expand Up @@ -35,19 +36,29 @@ class Metadata {
*/
public static $keys = [];

/**
* Initializes hooks.
*/
public static function init_hooks() {
\add_filter( 'newspack_newsletters_contact_data', [ __CLASS__, 'normalize_contact_data' ] );
}

/**
* Get the metadata keys map for Reader Activation.
*
* @return array List of fields.
*/
public static function get_keys() {
if ( empty( self::$keys ) ) {
// Only get Woo fields if using Woo.
$fields = Donations::is_platform_wc() ? self::get_all_fields() : self::get_basic_fields();

/**
* Filters the list of key/value pairs for metadata fields to be synced to the connected ESP.
*
* @param array $keys The list of key/value pairs for metadata fields to be synced to the connected ESP.
*/
self::$keys = \apply_filters( 'newspack_ras_metadata_keys', self::get_all_fields() );
self::$keys = \apply_filters( 'newspack_ras_metadata_keys', $fields );
}
return self::$keys;
}
Expand Down Expand Up @@ -446,3 +457,4 @@ public static function normalize_contact_data( $contact ) {
return apply_filters( 'newspack_esp_sync_normalize_contact', $contact );
}
}
Metadata::init_hooks();
7 changes: 5 additions & 2 deletions includes/reader-activation/sync/class-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static function should_sync_order( $order ) {
* @return \WC_Order|false Order object or false.
*/
private static function get_current_product_order_for_sync( $customer ) {
if ( ! is_a( $customer, 'WC_Customer' ) ) {
if ( ! class_exists( 'WC_Customer' ) || ! is_a( $customer, 'WC_Customer' ) ) {
return false;
}

Expand Down Expand Up @@ -99,6 +99,9 @@ private static function get_current_product_order_for_sync( $customer ) {
* @return ?WCS_Subscription A Subscription object or null.
*/
private static function get_most_recent_cancelled_or_expired_subscription( $user_id ) {
if ( ! function_exists( 'wcs_get_users_subscriptions' ) ) {
return;
}
$subscriptions = array_reduce(
array_keys( \wcs_get_users_subscriptions( $user_id ) ),
function( $acc, $subscription_id ) {
Expand Down Expand Up @@ -376,7 +379,7 @@ private static function get_order_metadata( $order, $payment_page_url = false )
* @return array|false Contact data or false.
*/
public static function get_contact_from_customer( $customer, $payment_page_url = false ) {
if ( ! is_a( $customer, 'WC_Customer' ) ) {
if ( ! class_exists( 'WC_Customer' ) || ! is_a( $customer, 'WC_Customer' ) ) {
$customer = new \WC_Customer( $customer );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,9 @@ public static function rate_limit_payment_methods( $is_valid ) {
* @return int[] Array of active subscription IDs.
*/
public static function get_active_subscriptions_for_user( $user_id, $product_ids = [] ) {
if ( ! function_exists( 'wcs_get_users_subscriptions' ) ) {
return [];
}
$subcriptions = array_reduce(
array_keys( \wcs_get_users_subscriptions( $user_id ) ),
function( $acc, $subscription_id ) use ( $product_ids ) {
Expand Down
8 changes: 5 additions & 3 deletions src/wizards/readerRevenue/views/donation/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
TextControl,
Wizard,
} from '../../../../components/src';
import { READER_REVENUE_WIZARD_SLUG } from '../../constants';
import { NEWSPACK, READER_REVENUE_WIZARD_SLUG } from '../../constants';

type FrequencySlug = 'once' | 'month' | 'year';

Expand Down Expand Up @@ -349,6 +349,8 @@ const BillingFields = () => {
const Donation = () => {
const wizardData = Wizard.useWizardData( 'reader-revenue' ) as WizardData;
const { saveWizardSettings } = useDispatch( Wizard.STORE_NAMESPACE );
const { platform_data } = wizardData;
const usedPlatform = platform_data?.platform;
const onSaveDonationSettings = () =>
saveWizardSettings( {
slug: READER_REVENUE_WIZARD_SLUG,
Expand Down Expand Up @@ -407,7 +409,7 @@ const Donation = () => {
) }
<DonationAmounts />
</ActionCard>
<ActionCard
{ NEWSPACK === usedPlatform && ( <ActionCard
description={ __( 'Configure options for modal checkouts.', 'newspack-plugin' ) }
hasGreyHeader={ true }
isMedium
Expand All @@ -419,7 +421,7 @@ const Donation = () => {
}
>
<BillingFields />
</ActionCard>
</ActionCard> ) }
</>
);
};
Expand Down