| 1 |
<?php |
| 2 |
|
| 3 |
namespace Better_Payment\Lite\Classes; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
/** |
| 10 |
* Reading the subscription data Pro's recurring and split checkouts write. |
| 11 |
* |
| 12 |
* Transitional copy for Better Payment Pro <= 1.4.3; delete when |
| 13 |
* ProCompatibility::MIN_PRO_VERSION >= 1.4.4. |
| 14 |
* Pro copy: better-payment-pro/includes/SubscriptionTransactionData.php. |
| 15 |
* Slug: subscription_transaction_data. |
| 16 |
* |
| 17 |
* Only Pro's checkout stores `subscription_*` and `is_payment_split_payment` in a |
| 18 |
* transaction's `form_fields_info`. Lite still reads those keys in five places, and |
| 19 |
* each now asks a hook instead: |
| 20 |
* |
| 21 |
* | Lite call site | Hook Pro fills | Method here | |
| 22 |
* | ------------------------------------------------ | ------------------------------------------------ | ---------------------------- | |
| 23 |
* | `API\UserAPI` Subscriptions tab row | `better_payment/user_api/subscription_row` | subscription_row() | |
| 24 |
* | `Helper::payment_type_label()` (API + CSV) | `better_payment/transaction/payment_type` | payment_type() | |
| 25 |
* | `Export::form_fields_info_formatted()` | `better_payment/export/form_fields_info` | export_fields() | |
| 26 |
* | `Import::build_form_fields()` | `better_payment/import/form_fields_info` | import_fields() | |
| 27 |
* | `Admin\DB::get_user_transactions_paginated()` | `better_payment/db/user_transactions_type_where` | subscriptions_type_where() | |
| 28 |
* |
| 29 |
* Every call site runs the method here only while lite_copy_applies(): an active |
| 30 |
* Pro that does not declare the slug. A free install never runs it, and a Pro that |
| 31 |
* declares the slug fills the hook itself. Pro's SubscriptionTransactionDataParityTest |
| 32 |
* fails when the method bodies here and in Pro drift. |
| 33 |
* |
| 34 |
* @since 2.4.0 |
| 35 |
*/ |
| 36 |
class ProSubscriptionTransactionData { |
| 37 |
|
| 38 |
/** |
| 39 |
* Feature slug Pro declares once it reads the data itself. |
| 40 |
*/ |
| 41 |
const FEATURE = 'subscription_transaction_data'; |
| 42 |
|
| 43 |
/** |
| 44 |
* Whether Lite's copy runs on this request. |
| 45 |
* |
| 46 |
* @return bool |
| 47 |
*/ |
| 48 |
public static function lite_copy_applies(): bool { |
| 49 |
return ProCompatibility::lite_copy_applies( self::FEATURE ); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Add the subscription columns to a `/user-transactions?tab=subscriptions` row. |
| 54 |
* |
| 55 |
* @param mixed $row REST row built by UserAPI. |
| 56 |
* @param mixed $form_fields_info The transaction's unserialized form_fields_info. |
| 57 |
* @return array |
| 58 |
*/ |
| 59 |
public static function subscription_row( $row, $form_fields_info = [] ): array { |
| 60 |
$row = is_array( $row ) ? $row : []; |
| 61 |
$form_fields_info = is_array( $form_fields_info ) ? $form_fields_info : []; |
| 62 |
|
| 63 |
$row['subscription_id'] = sanitize_text_field( $form_fields_info['subscription_id'] ?? '' ); |
| 64 |
$row['subscription_plan_id'] = sanitize_text_field( $form_fields_info['subscription_plan_id'] ?? '' ); |
| 65 |
$row['subscription_status'] = sanitize_text_field( $form_fields_info['subscription_status'] ?? '' ); |
| 66 |
$row['subscription_interval'] = sanitize_text_field( $form_fields_info['subscription_interval'] ?? '' ); |
| 67 |
$row['subscription_created_date'] = intval( $form_fields_info['subscription_created_date'] ?? 0 ); |
| 68 |
$row['subscription_current_period_end'] = intval( $form_fields_info['subscription_current_period_end'] ?? 0 ); |
| 69 |
$row['is_payment_split_payment'] = ! empty( $form_fields_info['is_payment_split_payment'] ) ? intval( $form_fields_info['is_payment_split_payment'] ) : 0; |
| 70 |
$row['subscription_product_name'] = ''; |
| 71 |
|
| 72 |
return $row; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* The payment type label of a transaction: "Subscription" when Pro's checkout |
| 77 |
* stored a subscription id, otherwise the label passed in. |
| 78 |
* |
| 79 |
* @param mixed $label Label so far ('One Time' from Lite). |
| 80 |
* @param mixed $form_fields_info The transaction's unserialized form_fields_info. |
| 81 |
* @return string |
| 82 |
*/ |
| 83 |
public static function payment_type( $label, $form_fields_info = [] ): string { |
| 84 |
if ( is_array( $form_fields_info ) && ! empty( $form_fields_info['subscription_id'] ) ) { |
| 85 |
return 'Subscription'; |
| 86 |
} |
| 87 |
|
| 88 |
return is_string( $label ) ? $label : 'One Time'; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Append the subscription keys to a CSV export's `form_fields_info` cell. |
| 93 |
* |
| 94 |
* @param mixed $fields Cell fields so far (customer_name, customer_email). |
| 95 |
* @param mixed $form_fields_info The transaction's unserialized form_fields_info. |
| 96 |
* @return array |
| 97 |
*/ |
| 98 |
public static function export_fields( $fields, $form_fields_info = [] ): array { |
| 99 |
$fields = is_array( $fields ) ? $fields : []; |
| 100 |
$form_fields_info = is_array( $form_fields_info ) ? $form_fields_info : []; |
| 101 |
|
| 102 |
$fields['subscription_id'] = isset( $form_fields_info['subscription_id'] ) ? sanitize_text_field( $form_fields_info['subscription_id'] ) : ''; |
| 103 |
$fields['subscription_customer_id'] = isset( $form_fields_info['subscription_customer_id'] ) ? sanitize_text_field( $form_fields_info['subscription_customer_id'] ) : ''; |
| 104 |
$fields['subscription_plan_id'] = isset( $form_fields_info['subscription_plan_id'] ) ? sanitize_text_field( $form_fields_info['subscription_plan_id'] ) : ''; |
| 105 |
$fields['subscription_interval'] = ! empty( $form_fields_info['subscription_interval'] ) ? sanitize_text_field( $form_fields_info['subscription_interval'] ) : ''; |
| 106 |
$fields['subscription_current_period_start'] = ! empty( $form_fields_info['subscription_current_period_start'] ) ? sanitize_text_field( $form_fields_info['subscription_current_period_start'] ) : ''; |
| 107 |
$fields['subscription_current_period_end'] = ! empty( $form_fields_info['subscription_current_period_end'] ) ? sanitize_text_field( $form_fields_info['subscription_current_period_end'] ) : ''; |
| 108 |
$fields['subscription_status'] = ! empty( $form_fields_info['subscription_status'] ) ? sanitize_text_field( $form_fields_info['subscription_status'] ) : ''; |
| 109 |
$fields['subscription_created_date'] = ! empty( $form_fields_info['subscription_created_date'] ) ? sanitize_text_field( $form_fields_info['subscription_created_date'] ) : ''; |
| 110 |
$fields['is_payment_split_payment'] = ! empty( $form_fields_info['is_payment_split_payment'] ) ? intval( $form_fields_info['is_payment_split_payment'] ) : 0; |
| 111 |
|
| 112 |
return $fields; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Copy the subscription keys of a CSV row whose payment type is "Subscription" |
| 117 |
* into the imported transaction's `form_fields_info`. |
| 118 |
* |
| 119 |
* @param mixed $fields form_fields_info built so far. |
| 120 |
* @param mixed $record The CSV row, keyed by header. |
| 121 |
* @param mixed $form_fields_info The row's `form_fields_info` cell, decoded by |
| 122 |
* Import::decode_form_fields_info() (never an object). |
| 123 |
* @return array |
| 124 |
*/ |
| 125 |
public static function import_fields( $fields, $record = [], $form_fields_info = [] ): array { |
| 126 |
$fields = is_array( $fields ) ? $fields : []; |
| 127 |
$record = is_array( $record ) ? $record : []; |
| 128 |
$form_fields_info = is_array( $form_fields_info ) ? $form_fields_info : []; |
| 129 |
|
| 130 |
if ( empty( $record['payment_type'] ) || 'subscription' !== strtolower( (string) $record['payment_type'] ) ) { |
| 131 |
return $fields; |
| 132 |
} |
| 133 |
|
| 134 |
$keys = [ |
| 135 |
'subscription_id', |
| 136 |
'subscription_customer_id', |
| 137 |
'subscription_plan_id', |
| 138 |
'subscription_interval', |
| 139 |
'subscription_current_period_start', |
| 140 |
'subscription_current_period_end', |
| 141 |
'subscription_status', |
| 142 |
'subscription_created_date', |
| 143 |
'is_payment_split_payment', |
| 144 |
]; |
| 145 |
|
| 146 |
foreach ( $keys as $key ) { |
| 147 |
$fields[ $key ] = isset( $form_fields_info[ $key ] ) ? sanitize_text_field( $form_fields_info[ $key ] ) : ''; |
| 148 |
} |
| 149 |
|
| 150 |
return $fields; |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* The SQL condition that narrows a user's transactions to subscription rows. |
| 155 |
* |
| 156 |
* @param mixed $where Condition so far (null when nothing supplies one). |
| 157 |
* @param string $type Requested row type. |
| 158 |
* @return mixed The condition for 'subscriptions', otherwise $where unchanged. |
| 159 |
*/ |
| 160 |
public static function subscriptions_type_where( $where, $type = '' ) { |
| 161 |
if ( 'subscriptions' !== $type ) { |
| 162 |
return $where; |
| 163 |
} |
| 164 |
|
| 165 |
return " AND form_fields_info LIKE '%\"subscription_id\"%'"; |
| 166 |
} |
| 167 |
} |
| 168 |
|