| 1 |
<?php |
| 2 |
/** |
| 3 |
* Paid Member Subscriptions ↔ Form Builder integration. |
| 4 |
* |
| 5 |
* Gate at hook time because PMS may load after PB. PB owns this bridge so classic |
| 6 |
* parity does not wait on a PMS release. Front-end render stays in PMS. |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 10 |
|
| 11 |
/** |
| 12 |
* Whether PMS's Profile Builder integration is loaded (same signal as classic Manage Fields). |
| 13 |
*/ |
| 14 |
function wppb_fb_pms_integration_active() { |
| 15 |
return function_exists( 'pms_pb_manage_field_types' ); |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Field-type labels PMS contributes. Must match wppbFieldType in the two blocks' block.json. |
| 20 |
*/ |
| 21 |
function wppb_fb_pms_field_types() { |
| 22 |
return array( 'Subscription Plans', 'PMS Billing Fields' ); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Flatten a PMS plan-output HTML snippet to plain text for the canvas preview. |
| 27 |
* |
| 28 |
* @param string $html One PMS plan-output snippet. |
| 29 |
* @param bool $drop_plain_dividers Drop bare `pms-divider` spans (front end hides them). |
| 30 |
* Duration divider (`pms-duration-divider`) is kept. |
| 31 |
* @return string |
| 32 |
*/ |
| 33 |
function wppb_fb_pms_plan_text( $html, $drop_plain_dividers = false ) { |
| 34 |
if ( ! is_string( $html ) || '' === $html ) { |
| 35 |
return ''; |
| 36 |
} |
| 37 |
|
| 38 |
if ( $drop_plain_dividers ) { |
| 39 |
$html = preg_replace( '#<span class="pms-divider">.*?</span>#s', '', $html ); |
| 40 |
} |
| 41 |
|
| 42 |
$text = html_entity_decode( wp_strip_all_tags( $html ), ENT_QUOTES, 'UTF-8' ); |
| 43 |
|
| 44 |
return trim( preg_replace( '/\s+/u', ' ', $text ) ); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Price + billing cycles + duration. Uses `wppb_register` so the same filters apply as on the form. |
| 49 |
*/ |
| 50 |
function wppb_fb_pms_plan_price_text( $plan ) { |
| 51 |
if ( ! function_exists( 'pms_get_output_subscription_plan_price' ) ) { |
| 52 |
return ''; |
| 53 |
} |
| 54 |
return wppb_fb_pms_plan_text( pms_get_output_subscription_plan_price( $plan, 'wppb_register' ), true ); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Free-trial suffix. Empty when none; PMS may also suppress for the current user (e.g. admin who used the trial). |
| 59 |
*/ |
| 60 |
function wppb_fb_pms_plan_trial_text( $plan ) { |
| 61 |
if ( ! function_exists( 'pms_get_output_subscription_plan_trial' ) ) { |
| 62 |
return ''; |
| 63 |
} |
| 64 |
return wppb_fb_pms_plan_text( pms_get_output_subscription_plan_trial( $plan, 'wppb_register' ) ); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Sign-up-fee suffix. Empty when none or no gateway supports fees. |
| 69 |
*/ |
| 70 |
function wppb_fb_pms_plan_sign_up_fee_text( $plan ) { |
| 71 |
if ( ! function_exists( 'pms_get_output_subscription_plan_sign_up_fee' ) ) { |
| 72 |
return ''; |
| 73 |
} |
| 74 |
return wppb_fb_pms_plan_text( pms_get_output_subscription_plan_sign_up_fee( $plan ) ); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Plan description for the preview, via the same filter as the front end. |
| 79 |
*/ |
| 80 |
function wppb_fb_pms_plan_description_text( $plan ) { |
| 81 |
$description = ! empty( $plan->description ) ? htmlspecialchars_decode( esc_html( $plan->description ) ) : ''; |
| 82 |
$description = apply_filters( 'pms_output_subscription_plan_description', $description, $plan ); |
| 83 |
|
| 84 |
return wppb_fb_pms_plan_text( $description ); |
| 85 |
} |
| 86 |
|
| 87 |
/* 1. Gating — both PMS field types active so their blocks are insertable. */ |
| 88 |
add_filter( 'wppb_fb_enabled_addon_field_types', 'wppb_fb_pms_enable_field_types' ); |
| 89 |
function wppb_fb_pms_enable_field_types( $types ) { |
| 90 |
if ( ! wppb_fb_pms_integration_active() ) { |
| 91 |
return $types; |
| 92 |
} |
| 93 |
if ( ! is_array( $types ) ) $types = array(); |
| 94 |
|
| 95 |
foreach ( wppb_fb_pms_field_types() as $field_type ) { |
| 96 |
if ( ! in_array( $field_type, $types, true ) ) { |
| 97 |
$types[] = $field_type; |
| 98 |
} |
| 99 |
} |
| 100 |
return $types; |
| 101 |
} |
| 102 |
|
| 103 |
/* 2. Editor payload — plans + billing fields for the two blocks' controls. */ |
| 104 |
add_action( 'wppb_fb_enqueue_editor_assets_late', 'wppb_fb_pms_enqueue_editor_payload' ); |
| 105 |
function wppb_fb_pms_enqueue_editor_payload() { |
| 106 |
if ( ! wppb_fb_pms_integration_active() ) { |
| 107 |
return; |
| 108 |
} |
| 109 |
|
| 110 |
// `all` first, then each plan by id — matches classic option order / values. |
| 111 |
$subscription_plans = array( |
| 112 |
array( 'value' => 'all', 'label' => __( 'All', 'profile-builder' ) ), |
| 113 |
); |
| 114 |
if ( function_exists( 'pms_get_subscription_plans' ) ) { |
| 115 |
foreach ( pms_get_subscription_plans() as $plan ) { |
| 116 |
$subscription_plans[] = array( |
| 117 |
'value' => (string) $plan->id, |
| 118 |
'label' => $plan->name, |
| 119 |
'price' => wppb_fb_pms_plan_price_text( $plan ), |
| 120 |
'trial' => wppb_fb_pms_plan_trial_text( $plan ), |
| 121 |
'signUpFee' => wppb_fb_pms_plan_sign_up_fee_text( $plan ), |
| 122 |
'description' => wppb_fb_pms_plan_description_text( $plan ), |
| 123 |
); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
// Default-plan select: no `all`; "Choose..." = -1. Value/label only for SelectControl. |
| 128 |
$subscription_plan_select = array( |
| 129 |
array( 'value' => '-1', 'label' => __( 'Choose...', 'profile-builder' ) ), |
| 130 |
); |
| 131 |
foreach ( $subscription_plans as $plan ) { |
| 132 |
if ( $plan['value'] === 'all' ) continue; |
| 133 |
$subscription_plan_select[] = array( |
| 134 |
'value' => $plan['value'], |
| 135 |
'label' => $plan['label'], |
| 136 |
); |
| 137 |
} |
| 138 |
|
| 139 |
$billing_fields = array(); |
| 140 |
if ( function_exists( 'pms_pb_get_billing_fields' ) ) { |
| 141 |
foreach ( pms_pb_get_billing_fields() as $slug => $field_data ) { |
| 142 |
$billing_fields[] = array( |
| 143 |
'value' => (string) $slug, |
| 144 |
'label' => isset( $field_data['label'] ) ? $field_data['label'] : (string) $slug, |
| 145 |
); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
$payload = array( |
| 150 |
'subscriptionPlans' => array_values( $subscription_plans ), |
| 151 |
'subscriptionPlanSelect' => array_values( $subscription_plan_select ), |
| 152 |
'billingFields' => array_values( $billing_fields ), |
| 153 |
'subscriptionsUrl' => admin_url( 'edit.php?post_type=pms-subscription' ), |
| 154 |
'addonsUrl' => admin_url( 'admin.php?page=pms-addons-page' ), |
| 155 |
); |
| 156 |
|
| 157 |
wp_add_inline_script( |
| 158 |
'wppb-form-editor-bundle', |
| 159 |
'window.wppbFb = window.wppbFb || {}; window.wppbFb.pms = ' . wp_json_encode( $payload ) . ';', |
| 160 |
'before' |
| 161 |
); |
| 162 |
} |
| 163 |
|