Skip to content

Commit

Permalink
🔀 Merge branch 'trunk'
Browse files Browse the repository at this point in the history
# Conflicts:
#	modules/ppcp-settings/resources/js/data/common/hooks.js
  • Loading branch information
stracker-phil committed Feb 12, 2025
2 parents 92a0977 + 8b9b664 commit ff81617
Show file tree
Hide file tree
Showing 57 changed files with 815 additions and 723 deletions.
1 change: 1 addition & 0 deletions modules/ppcp-paypal-subscriptions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"devDependencies": {
"@babel/core": "^7.26",
"@babel/preset-env": "^7.26",
"@wordpress/i18n": "^5.11",
"babel-loader": "^9.2",
"cross-env": "^7.0.3",
"file-loader": "^6.2.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { __ } from '@wordpress/i18n';
document.addEventListener( 'DOMContentLoaded', () => {
const variations = document.querySelector( '.woocommerce_variations' );

const disableFields = ( productId ) => {
const variations = document.querySelector( '.woocommerce_variations' );
if ( variations ) {
const children = variations.children;
for ( let i = 0; i < children.length; i++ ) {
Expand Down Expand Up @@ -68,16 +70,94 @@ document.addEventListener( 'DOMContentLoaded', () => {
soldIndividually.setAttribute( 'disabled', 'disabled' );
};

const checkSubscriptionPeriodsInterval = (period, period_interval, price, linkBtn) => {
if (
( period === 'year' && parseInt( period_interval ) > 1 ) ||
( period === 'month' && parseInt( period_interval ) > 12 ) ||
( period === 'week' && parseInt( period_interval ) > 52 ) ||
( period === 'day' && parseInt( period_interval ) > 356 ) ||
( ! price || parseInt( price ) <= 0 )
) {
linkBtn.disabled = true;
linkBtn.checked = false;
if (! price || parseInt( price ) <= 0 ) {
linkBtn.setAttribute('title', __( 'Prices must be above zero for PayPal Subscriptions!', 'woocommerce-paypal-subscriptions' ) );
} else {
linkBtn.setAttribute('title', __( 'Not allowed period intervall combination for PayPal Subscriptions!', 'woocommerce-paypal-subscriptions' ) );
}

} else {
linkBtn.disabled = false;
linkBtn.removeAttribute('title');
}
}

const setupProducts = () => {
jQuery( '.wc_input_subscription_period' ).on( 'change', (e) => {
const linkBtn = e.target.parentElement.parentElement.parentElement.parentElement.querySelector('input[name="_ppcp_enable_subscription_product"]');
const period_interval = e.target.parentElement.querySelector('select.wc_input_subscription_period_interval')?.value;
const period = e.target.value;
const price = e.target.parentElement.querySelector('input.wc_input_subscription_price')?.value;

checkSubscriptionPeriodsInterval(period, period_interval, price, linkBtn);
});

jQuery( '.wc_input_subscription_period_interval' ).on( 'change', (e) => {
const linkBtn = e.target.parentElement.parentElement.parentElement.parentElement.querySelector('input[name="_ppcp_enable_subscription_product"]');
const period_interval = e.target.value;
const period = e.target.parentElement.querySelector('select.wc_input_subscription_period')?.value;
const price = e.target.parentElement.querySelector('input.wc_input_subscription_price')?.value;

checkSubscriptionPeriodsInterval(period, period_interval, price, linkBtn);
});

jQuery( '.wc_input_subscription_price' ).on( 'change', (e) => {
const linkBtn = e.target.parentElement.parentElement.parentElement.parentElement.querySelector('input[name="_ppcp_enable_subscription_product"]');
const period_interval = e.target.parentElement.querySelector('select.wc_input_subscription_period_interval')?.value;
const period = e.target.parentElement.querySelector('select.wc_input_subscription_period')?.value;
const price = e.target.value;

checkSubscriptionPeriodsInterval(period, period_interval, price, linkBtn);
});

jQuery( '.wc_input_subscription_price' ).trigger( 'change' );

PayPalCommerceGatewayPayPalSubscriptionProducts?.forEach(
( product ) => {
if ( product.product_connected === 'yes' ) {
disableFields( product.product_id );
}

const unlinkBtn = document.getElementById(
`ppcp-unlink-sub-plan-${ product.product_id }`
);
const linkBtn = document.getElementById(
`ppcp_enable_subscription_product-${ product.product_id }`
);
linkBtn?.addEventListener( 'click', ( event ) => {
const unlinkBtnP = document.getElementById(
`ppcp-enable-subscription-${ product.product_id }`
);
const titleP = document.getElementById(
`ppcp_subscription_plan_name_p-${ product.product_id }`
);
if (event.target.checked === true) {
if ( unlinkBtnP ) {
unlinkBtnP.style.display = 'none';
}
if ( titleP ) {
titleP.style.display = 'block';
}
} else {
if ( unlinkBtnP ) {
unlinkBtnP.style.display = 'block';
}
if ( titleP ) {
titleP.style.display = 'none';
}
}
});

const unlinkBtn = document.getElementById(
`ppcp-unlink-sub-plan-${ product.product_id }`
);
unlinkBtn?.addEventListener( 'click', ( event ) => {
event.preventDefault();
unlinkBtn.disabled = true;
Expand Down Expand Up @@ -110,23 +190,22 @@ document.addEventListener( 'DOMContentLoaded', () => {
}

const enableSubscription = document.getElementById(
'ppcp-enable-subscription'
'ppcp-enable-subscription-' + data.data.product_id
);
const product =
document.getElementById( 'pcpp-product' );
const plan = document.getElementById( 'pcpp-plan' );
const product = document.getElementById( 'pcpp-product-' + data.data.product_id );
const plan = document.getElementById( 'pcpp-plan-' + data.data.product_id );
enableSubscription.style.display = 'none';
product.style.display = 'none';
plan.style.display = 'none';

const enable_subscription_product =
document.getElementById(
'ppcp_enable_subscription_product'
'ppcp_enable_subscription_product-' + data.data.product_id
);
enable_subscription_product.disabled = true;

const planUnlinked =
document.getElementById( 'pcpp-plan-unlinked' );
document.getElementById( 'pcpp-plan-unlinked-' + data.data.product_id );
planUnlinked.style.display = 'block';

setTimeout( () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function handle_request(): void {
}
}

wp_send_json_success();
wp_send_json_success( array( 'product_id' => (string) $product_id ) );
} catch ( Exception $error ) {
wp_send_json_error();
}
Expand Down
71 changes: 43 additions & 28 deletions modules/ppcp-paypal-subscriptions/src/PayPalSubscriptionsModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -562,11 +562,16 @@ function( $hook ) use ( $c ) {
wp_enqueue_script(
'ppcp-paypal-subscription',
untrailingslashit( $module_url ) . '/assets/js/paypal-subscription.js',
array( 'jquery' ),
array( 'jquery', 'wc-admin-product-editor' ),
$c->get( 'ppcp.asset-version' ),
true
);

wp_set_script_translations(
'ppcp-paypal-subscription',
'woocommerce-paypal-payments'
);

$products = array( $this->set_product_config( $product ) );
if ( $product->get_type() === 'variable-subscription' ) {
$products = array();
Expand Down Expand Up @@ -780,10 +785,10 @@ private function render_paypal_subscription_fields( WC_Product $product, Environ
echo sprintf(
// translators: %1$s and %2$s are label open and close tags.
esc_html__( '%1$sConnect to PayPal%2$s', 'woocommerce-paypal-payments' ),
'<label for="_ppcp_enable_subscription_product" style="' . esc_attr( $style ) . '">',
'<label for="_ppcp_enable_subscription_product-' . esc_attr( (string) $product->get_id() ) . '" style="' . esc_attr( $style ) . '">',
'</label>'
);
echo '<input type="checkbox" id="ppcp_enable_subscription_product" name="_ppcp_enable_subscription_product" value="yes" ' . checked( $enable_subscription_product, 'yes', false ) . '/>';
echo '<input type="checkbox" id="ppcp_enable_subscription_product-' . esc_attr( (string) $product->get_id() ) . '" name="_ppcp_enable_subscription_product" value="yes" ' . checked( $enable_subscription_product, 'yes', false ) . '/>';
echo sprintf(
// translators: %1$s and %2$s are label open and close tags.
esc_html__( '%1$sConnect Product to PayPal Subscriptions Plan%2$s', 'woocommerce-paypal-payments' ),
Expand All @@ -797,41 +802,51 @@ private function render_paypal_subscription_fields( WC_Product $product, Environ
$subscription_product = $product->get_meta( 'ppcp_subscription_product' );
$subscription_plan = $product->get_meta( 'ppcp_subscription_plan' );
$subscription_plan_name = $product->get_meta( '_ppcp_subscription_plan_name' );
if ( $subscription_product && $subscription_plan ) {
if ( $subscription_product || $subscription_plan ) {
$display_unlink_p = 'display:none;';
if ( $enable_subscription_product !== 'yes' ) {
echo sprintf(
// translators: %1$s and %2$s are button and wrapper html tags.
esc_html__( '%1$sUnlink PayPal Subscription Plan%2$s', 'woocommerce-paypal-payments' ),
'<p class="form-field" id="ppcp-enable-subscription"><label></label><button class="button" id="ppcp-unlink-sub-plan-' . esc_attr( (string) $product->get_id() ) . '">',
'</button><span class="spinner is-active" id="spinner-unlink-plan" style="float: none; display:none;"></span></p>'
);
echo sprintf(
// translators: %1$s and %2$s is open and closing paragraph tag.
esc_html__( '%1$sPlan unlinked successfully ✔️%2$s', 'woocommerce-paypal-payments' ),
'<p class="form-field" id="pcpp-plan-unlinked" style="display: none;">',
'</p>'
);
$display_unlink_p = '';
}

$host = $environment->current_environment_is( Environment::SANDBOX ) ? 'https://www.sandbox.paypal.com' : 'https://www.paypal.com';
echo sprintf(
// translators: %1$s and %2$s are wrapper html tags.
esc_html__( '%1$sProduct%2$s', 'woocommerce-paypal-payments' ),
'<p class="form-field" id="pcpp-product"><label style="' . esc_attr( $style ) . '">',
'</label><a href="' . esc_url( $host . '/billing/plans/products/' . $subscription_product['id'] ) . '" target="_blank">' . esc_attr( $subscription_product['id'] ) . '</a></p>'
// translators: %1$s and %2$s are button and wrapper html tags.
esc_html__( '%1$sUnlink PayPal Subscription Plan%2$s', 'woocommerce-paypal-payments' ),
'<p class="form-field ppcp-enable-subscription" id="ppcp-enable-subscription-' . esc_attr( (string) $product->get_id() ) . '" style="' . esc_attr( $display_unlink_p ) . '"><label></label><button class="button ppcp-unlink-sub-plan" id="ppcp-unlink-sub-plan-' . esc_attr( (string) $product->get_id() ) . '">',
'</button><span class="spinner is-active" id="spinner-unlink-plan" style="float: none; display:none;"></span></p>'
);
echo sprintf(
// translators: %1$s and %2$s are wrapper html tags.
esc_html__( '%1$sPlan%2$s', 'woocommerce-paypal-payments' ),
'<p class="form-field" id="pcpp-plan"><label style="' . esc_attr( $style ) . '">',
'</label><a href="' . esc_url( $host . '/billing/plans/' . $subscription_plan['id'] ) . '" target="_blank">' . esc_attr( $subscription_plan['id'] ) . '</a></p>'
// translators: %1$s and %2$s is open and closing paragraph tag.
esc_html__( '%1$sPlan unlinked successfully ✔️%2$s', 'woocommerce-paypal-payments' ),
'<p class="form-field pcpp-plan-unlinked" id="pcpp-plan-unlinked-' . esc_attr( (string) $product->get_id() ) . '" style="display: none;">',
'</p>'
);

$host = $environment->current_environment_is( Environment::SANDBOX ) ? 'https://www.sandbox.paypal.com' : 'https://www.paypal.com';
if ( $subscription_product ) {
echo sprintf(
// translators: %1$s and %2$s are wrapper html tags.
esc_html__( '%1$sProduct%2$s', 'woocommerce-paypal-payments' ),
'<p class="form-field pcpp-product" id="pcpp-product-' . esc_attr( (string) $product->get_id() ) . '"><label style="' . esc_attr( $style ) . '">',
'</label><a href="' . esc_url( $host . '/billing/plans/products/' . $subscription_product['id'] ) . '" target="_blank">' . esc_attr( $subscription_product['id'] ) . '</a></p>'
);
}
if ( $subscription_plan ) {
echo sprintf(
// translators: %1$s and %2$s are wrapper html tags.
esc_html__( '%1$sPlan%2$s', 'woocommerce-paypal-payments' ),
'<p class="form-field pcpp-plan" id="pcpp-plan-' . esc_attr( (string) $product->get_id() ) . '"><label style="' . esc_attr( $style ) . '">',
'</label><a href="' . esc_url( $host . '/billing/plans/' . $subscription_plan['id'] ) . '" target="_blank">' . esc_attr( $subscription_plan['id'] ) . '</a></p>'
);
}
} else {
$display_plan_name_p = '';
if ( $enable_subscription_product !== 'yes' && $product->get_name() !== 'AUTO-DRAFT' ) {
$display_plan_name_p = 'display:none;';
}
echo sprintf(
// translators: %1$s and %2$s are wrapper html tags.
esc_html__( '%1$sPlan Name%2$s', 'woocommerce-paypal-payments' ),
'<p class="form-field"><label for="_ppcp_subscription_plan_name">',
'</label><input type="text" class="short" id="ppcp_subscription_plan_name" name="_ppcp_subscription_plan_name" value="' . esc_attr( $subscription_plan_name ) . '"></p>'
'<p class="form-field ppcp_subscription_plan_name_p" id="ppcp_subscription_plan_name_p-' . esc_attr( (string) $product->get_id() ) . '" style="' . esc_attr( $display_plan_name_p ) . '"><label for="_ppcp_subscription_plan_name-' . esc_attr( (string) $product->get_id() ) . '">',
'</label><input type="text" class="short ppcp_subscription_plan_name" id="ppcp_subscription_plan_name-' . esc_attr( (string) $product->get_id() ) . '" name="_ppcp_subscription_plan_name" value="' . esc_attr( $subscription_plan_name ) . '"></p>'
);
}
}
Expand Down
26 changes: 24 additions & 2 deletions modules/ppcp-paypal-subscriptions/src/SubscriptionsApiHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,32 @@ private function billing_cycles( WC_Product $product ): array {
$sequence++;
}

$interval = $product->get_meta( '_subscription_period' );
$period_interval = (int) $product->get_meta( '_subscription_period_interval' );
$update = false;
if ( $interval === 'day' && $period_interval > 365 ) {
$period_interval = 365;
$update = true;
} elseif ( $interval === 'week' && $period_interval > 52 ) {
$period_interval = 52;
$update = true;
} elseif ( $interval === 'month' && $period_interval > 12 ) {
$period_interval = 12;
$update = true;
} elseif ( $interval === 'year' && $period_interval > 1 ) {
$period_interval = 1;
$update = true;
}
if ( $update ) {
$product->add_meta_data( '_subscription_period_interval', (string) $period_interval, true );
$product->save();
$this->logger->warning( sprintf( 'Subscription plan on PayPal is to high for %1$s interval change it to maximum of %2$d', $interval, $period_interval ) );
}

$billing_cycles[] = ( new BillingCycle(
array(
'interval_unit' => $product->get_meta( '_subscription_period' ),
'interval_count' => $product->get_meta( '_subscription_period_interval' ),
'interval_unit' => $interval,
'interval_count' => $period_interval,
),
$sequence,
'REGULAR',
Expand Down
Loading

0 comments on commit ff81617

Please sign in to comment.