@@ -13,464 +13,464 @@ discard block |
||
| 13 | 13 | */ |
| 14 | 14 | abstract class GetPaid_Payment_Gateway { |
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Set if the place checkout button should be renamed on selection. |
|
| 18 | - * |
|
| 19 | - * @var string |
|
| 20 | - */ |
|
| 21 | - public $checkout_button_text; |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * Boolean whether the method is enabled. |
|
| 25 | - * |
|
| 26 | - * @var bool |
|
| 27 | - */ |
|
| 28 | - public $enabled = true; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * Payment method id. |
|
| 32 | - * |
|
| 33 | - * @var string |
|
| 34 | - */ |
|
| 35 | - public $id; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * Payment method order. |
|
| 39 | - * |
|
| 40 | - * @var int |
|
| 41 | - */ |
|
| 42 | - public $order = 10; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * Payment method title for the frontend. |
|
| 46 | - * |
|
| 47 | - * @var string |
|
| 48 | - */ |
|
| 49 | - public $title; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * Payment method description for the frontend. |
|
| 53 | - * |
|
| 54 | - * @var string |
|
| 55 | - */ |
|
| 56 | - public $description; |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * Gateway title. |
|
| 60 | - * |
|
| 61 | - * @var string |
|
| 62 | - */ |
|
| 63 | - public $method_title = ''; |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Gateway description. |
|
| 67 | - * |
|
| 68 | - * @var string |
|
| 69 | - */ |
|
| 70 | - public $method_description = ''; |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * Countries this gateway is allowed for. |
|
| 74 | - * |
|
| 75 | - * @var array |
|
| 76 | - */ |
|
| 77 | - public $countries; |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * Currencies this gateway is allowed for. |
|
| 81 | - * |
|
| 82 | - * @var array |
|
| 83 | - */ |
|
| 84 | - public $currencies; |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * Currencies this gateway is not allowed for. |
|
| 88 | - * |
|
| 89 | - * @var array |
|
| 90 | - */ |
|
| 91 | - public $exclude_currencies; |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Maximum transaction amount, zero does not define a maximum. |
|
| 95 | - * |
|
| 96 | - * @var int |
|
| 97 | - */ |
|
| 98 | - public $max_amount = 0; |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * Optional URL to view a transaction. |
|
| 102 | - * |
|
| 103 | - * @var string |
|
| 104 | - */ |
|
| 105 | - public $view_transaction_url = ''; |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Optional URL to view a subscription. |
|
| 109 | - * |
|
| 110 | - * @var string |
|
| 111 | - */ |
|
| 112 | - public $view_subscription_url = ''; |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * Optional label to show for "new payment method" in the payment |
|
| 116 | - * method/token selection radio selection. |
|
| 117 | - * |
|
| 118 | - * @var string |
|
| 119 | - */ |
|
| 120 | - public $new_method_label = ''; |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * Contains a user's saved tokens for this gateway. |
|
| 124 | - * |
|
| 125 | - * @var array |
|
| 126 | - */ |
|
| 127 | - protected $tokens = array(); |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * An array of features that this gateway supports. |
|
| 131 | - * |
|
| 132 | - * @var array |
|
| 133 | - */ |
|
| 134 | - protected $supports = array(); |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Class constructor. |
|
| 138 | - */ |
|
| 139 | - public function __construct() { |
|
| 140 | - |
|
| 141 | - // Register gateway. |
|
| 142 | - add_filter( 'wpinv_payment_gateways', array( $this, 'register_gateway' ) ); |
|
| 143 | - |
|
| 144 | - $this->enabled = wpinv_is_gateway_active( $this->id ); |
|
| 145 | - |
|
| 146 | - // Add support for various features. |
|
| 147 | - foreach ( $this->supports as $feature ) { |
|
| 148 | - add_filter( "wpinv_{$this->id}_support_{$feature}", '__return_true' ); |
|
| 149 | - add_filter( "getpaid_{$this->id}_support_{$feature}", '__return_true' ); |
|
| 150 | - add_filter( "getpaid_{$this->id}_supports_{$feature}", '__return_true' ); |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - // Invoice addons. |
|
| 154 | - if ( $this->supports( 'addons' ) ) { |
|
| 155 | - add_action( "getpaid_process_{$this->id}_invoice_addons", array( $this, 'process_addons' ), 10, 2 ); |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - // Gateway settings. |
|
| 159 | - add_filter( "wpinv_gateway_settings_{$this->id}", array( $this, 'admin_settings' ) ); |
|
| 16 | + /** |
|
| 17 | + * Set if the place checkout button should be renamed on selection. |
|
| 18 | + * |
|
| 19 | + * @var string |
|
| 20 | + */ |
|
| 21 | + public $checkout_button_text; |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * Boolean whether the method is enabled. |
|
| 25 | + * |
|
| 26 | + * @var bool |
|
| 27 | + */ |
|
| 28 | + public $enabled = true; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * Payment method id. |
|
| 32 | + * |
|
| 33 | + * @var string |
|
| 34 | + */ |
|
| 35 | + public $id; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * Payment method order. |
|
| 39 | + * |
|
| 40 | + * @var int |
|
| 41 | + */ |
|
| 42 | + public $order = 10; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * Payment method title for the frontend. |
|
| 46 | + * |
|
| 47 | + * @var string |
|
| 48 | + */ |
|
| 49 | + public $title; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * Payment method description for the frontend. |
|
| 53 | + * |
|
| 54 | + * @var string |
|
| 55 | + */ |
|
| 56 | + public $description; |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * Gateway title. |
|
| 60 | + * |
|
| 61 | + * @var string |
|
| 62 | + */ |
|
| 63 | + public $method_title = ''; |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Gateway description. |
|
| 67 | + * |
|
| 68 | + * @var string |
|
| 69 | + */ |
|
| 70 | + public $method_description = ''; |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * Countries this gateway is allowed for. |
|
| 74 | + * |
|
| 75 | + * @var array |
|
| 76 | + */ |
|
| 77 | + public $countries; |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * Currencies this gateway is allowed for. |
|
| 81 | + * |
|
| 82 | + * @var array |
|
| 83 | + */ |
|
| 84 | + public $currencies; |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * Currencies this gateway is not allowed for. |
|
| 88 | + * |
|
| 89 | + * @var array |
|
| 90 | + */ |
|
| 91 | + public $exclude_currencies; |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Maximum transaction amount, zero does not define a maximum. |
|
| 95 | + * |
|
| 96 | + * @var int |
|
| 97 | + */ |
|
| 98 | + public $max_amount = 0; |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * Optional URL to view a transaction. |
|
| 102 | + * |
|
| 103 | + * @var string |
|
| 104 | + */ |
|
| 105 | + public $view_transaction_url = ''; |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * Optional URL to view a subscription. |
|
| 109 | + * |
|
| 110 | + * @var string |
|
| 111 | + */ |
|
| 112 | + public $view_subscription_url = ''; |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * Optional label to show for "new payment method" in the payment |
|
| 116 | + * method/token selection radio selection. |
|
| 117 | + * |
|
| 118 | + * @var string |
|
| 119 | + */ |
|
| 120 | + public $new_method_label = ''; |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * Contains a user's saved tokens for this gateway. |
|
| 124 | + * |
|
| 125 | + * @var array |
|
| 126 | + */ |
|
| 127 | + protected $tokens = array(); |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * An array of features that this gateway supports. |
|
| 131 | + * |
|
| 132 | + * @var array |
|
| 133 | + */ |
|
| 134 | + protected $supports = array(); |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Class constructor. |
|
| 138 | + */ |
|
| 139 | + public function __construct() { |
|
| 140 | + |
|
| 141 | + // Register gateway. |
|
| 142 | + add_filter( 'wpinv_payment_gateways', array( $this, 'register_gateway' ) ); |
|
| 143 | + |
|
| 144 | + $this->enabled = wpinv_is_gateway_active( $this->id ); |
|
| 145 | + |
|
| 146 | + // Add support for various features. |
|
| 147 | + foreach ( $this->supports as $feature ) { |
|
| 148 | + add_filter( "wpinv_{$this->id}_support_{$feature}", '__return_true' ); |
|
| 149 | + add_filter( "getpaid_{$this->id}_support_{$feature}", '__return_true' ); |
|
| 150 | + add_filter( "getpaid_{$this->id}_supports_{$feature}", '__return_true' ); |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + // Invoice addons. |
|
| 154 | + if ( $this->supports( 'addons' ) ) { |
|
| 155 | + add_action( "getpaid_process_{$this->id}_invoice_addons", array( $this, 'process_addons' ), 10, 2 ); |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + // Gateway settings. |
|
| 159 | + add_filter( "wpinv_gateway_settings_{$this->id}", array( $this, 'admin_settings' ) ); |
|
| 160 | 160 | |
| 161 | 161 | |
| 162 | - // Gateway checkout fiellds. |
|
| 163 | - add_action( "wpinv_{$this->id}_cc_form", array( $this, 'payment_fields' ), 10, 2 ); |
|
| 164 | - |
|
| 165 | - // Process payment. |
|
| 166 | - add_action( "getpaid_gateway_{$this->id}", array( $this, 'process_payment' ), 10, 3 ); |
|
| 167 | - |
|
| 168 | - // Change the checkout button text. |
|
| 169 | - if ( ! empty( $this->checkout_button_text ) ) { |
|
| 170 | - add_filter( "getpaid_gateway_{$this->id}_checkout_button_label", array( $this, 'rename_checkout_button' ) ); |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - // Check if a gateway is valid for a given currency. |
|
| 174 | - add_filter( "getpaid_gateway_{$this->id}_is_valid_for_currency", array( $this, 'validate_currency' ), 10, 2 ); |
|
| 175 | - |
|
| 176 | - // Generate the transaction url. |
|
| 177 | - add_filter( "getpaid_gateway_{$this->id}_transaction_url", array( $this, 'filter_transaction_url' ), 10, 2 ); |
|
| 178 | - |
|
| 179 | - // Generate the subscription url. |
|
| 180 | - add_filter( 'getpaid_remote_subscription_profile_url', array( $this, 'generate_subscription_url' ), 10, 2 ); |
|
| 181 | - |
|
| 182 | - // Confirm payments. |
|
| 183 | - add_filter( "wpinv_payment_confirm_{$this->id}", array( $this, 'confirm_payment' ), 10, 2 ); |
|
| 184 | - |
|
| 185 | - // Verify IPNs. |
|
| 186 | - add_action( "wpinv_verify_{$this->id}_ipn", array( $this, 'verify_ipn' ) ); |
|
| 187 | - |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - /** |
|
| 191 | - * Checks if this gateway is a given gateway. |
|
| 192 | - * |
|
| 193 | - * @since 1.0.19 |
|
| 194 | - * @return bool |
|
| 195 | - */ |
|
| 196 | - public function is( $gateway ) { |
|
| 197 | - return $gateway == $this->id; |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - /** |
|
| 201 | - * Returns a users saved tokens for this gateway. |
|
| 202 | - * |
|
| 203 | - * @since 1.0.19 |
|
| 204 | - * @return array |
|
| 205 | - */ |
|
| 206 | - public function get_tokens( $sandbox = null ) { |
|
| 207 | - |
|
| 208 | - if ( is_user_logged_in() && $this->supports( 'tokens' ) && 0 == count( $this->tokens ) ) { |
|
| 209 | - $tokens = get_user_meta( get_current_user_id(), "getpaid_{$this->id}_tokens", true ); |
|
| 210 | - |
|
| 211 | - if ( is_array( $tokens ) ) { |
|
| 212 | - $this->tokens = $tokens; |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - if ( ! is_bool( $sandbox ) ) { |
|
| 218 | - return $this->tokens; |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - // Filter tokens. |
|
| 222 | - $args = array( 'type' => $sandbox ? 'sandbox' : 'live' ); |
|
| 223 | - return wp_list_filter( $this->tokens, $args ); |
|
| 224 | - |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - /** |
|
| 228 | - * Saves a token for this gateway. |
|
| 229 | - * |
|
| 230 | - * @since 1.0.19 |
|
| 231 | - */ |
|
| 232 | - public function save_token( $token ) { |
|
| 233 | - |
|
| 234 | - $tokens = $this->get_tokens(); |
|
| 235 | - $tokens[] = $token; |
|
| 236 | - |
|
| 237 | - update_user_meta( get_current_user_id(), "getpaid_{$this->id}_tokens", $tokens ); |
|
| 238 | - |
|
| 239 | - $this->tokens = $tokens; |
|
| 240 | - |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - /** |
|
| 244 | - * Return the title for admin screens. |
|
| 245 | - * |
|
| 246 | - * @return string |
|
| 247 | - */ |
|
| 248 | - public function get_method_title() { |
|
| 249 | - return apply_filters( 'getpaid_gateway_method_title', $this->method_title, $this ); |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - /** |
|
| 253 | - * Return the description for admin screens. |
|
| 254 | - * |
|
| 255 | - * @return string |
|
| 256 | - */ |
|
| 257 | - public function get_method_description() { |
|
| 258 | - return apply_filters( 'getpaid_gateway_method_description', $this->method_description, $this ); |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - /** |
|
| 262 | - * Get the success url. |
|
| 263 | - * |
|
| 264 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 265 | - * @return string |
|
| 266 | - */ |
|
| 267 | - public function get_return_url( $invoice ) { |
|
| 268 | - |
|
| 269 | - // Payment success url |
|
| 270 | - $return_url = add_query_arg( |
|
| 271 | - array( |
|
| 272 | - 'payment-confirm' => $this->id, |
|
| 273 | - 'invoice_key' => $invoice->get_key(), |
|
| 274 | - 'utm_nooverride' => 1 |
|
| 275 | - ), |
|
| 276 | - wpinv_get_success_page_uri() |
|
| 277 | - ); |
|
| 278 | - |
|
| 279 | - return apply_filters( 'getpaid_gateway_success_url', $return_url, $invoice, $this ); |
|
| 280 | - } |
|
| 281 | - |
|
| 282 | - /** |
|
| 283 | - * Confirms payments when rendering the success page. |
|
| 284 | - * |
|
| 285 | - * @param string $content Success page content. |
|
| 286 | - * @return string |
|
| 287 | - */ |
|
| 288 | - public function confirm_payment( $content ) { |
|
| 289 | - |
|
| 290 | - // Retrieve the invoice. |
|
| 291 | - $invoice_id = getpaid_get_current_invoice_id(); |
|
| 292 | - $invoice = wpinv_get_invoice( $invoice_id ); |
|
| 293 | - |
|
| 294 | - // Ensure that it exists and that it is pending payment. |
|
| 295 | - if ( empty( $invoice_id ) || ! $invoice->needs_payment() ) { |
|
| 296 | - return $content; |
|
| 297 | - } |
|
| 298 | - |
|
| 299 | - // Can the user view this invoice?? |
|
| 300 | - if ( ! wpinv_user_can_view_invoice( $invoice ) ) { |
|
| 301 | - return $content; |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - // Show payment processing indicator. |
|
| 305 | - return wpinv_get_template_html( 'wpinv-payment-processing.php', compact( 'invoice' ) ); |
|
| 306 | - } |
|
| 307 | - |
|
| 308 | - /** |
|
| 309 | - * Processes ipns and marks payments as complete. |
|
| 310 | - * |
|
| 311 | - * @return void |
|
| 312 | - */ |
|
| 313 | - public function verify_ipn() {} |
|
| 314 | - |
|
| 315 | - /** |
|
| 316 | - * Processes invoice addons. |
|
| 317 | - * |
|
| 318 | - * @param WPInv_Invoice $invoice |
|
| 319 | - * @param GetPaid_Form_Item[] $items |
|
| 320 | - * @return WPInv_Invoice |
|
| 321 | - */ |
|
| 322 | - public function process_addons( $invoice, $items ) { |
|
| 323 | - |
|
| 324 | - } |
|
| 325 | - |
|
| 326 | - /** |
|
| 327 | - * Get a link to the transaction on the 3rd party gateway site (if applicable). |
|
| 328 | - * |
|
| 329 | - * @param string $transaction_url transaction url. |
|
| 330 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 331 | - * @return string transaction URL, or empty string. |
|
| 332 | - */ |
|
| 333 | - public function filter_transaction_url( $transaction_url, $invoice ) { |
|
| 334 | - |
|
| 335 | - $transaction_id = $invoice->get_transaction_id(); |
|
| 336 | - |
|
| 337 | - if ( ! empty( $this->view_transaction_url ) && ! empty( $transaction_id ) ) { |
|
| 338 | - $transaction_url = sprintf( $this->view_transaction_url, $transaction_id ); |
|
| 339 | - $replace = $this->is_sandbox( $invoice ) ? 'sandbox' : ''; |
|
| 340 | - $transaction_url = str_replace( '{sandbox}', $replace, $transaction_url ); |
|
| 341 | - } |
|
| 342 | - |
|
| 343 | - return $transaction_url; |
|
| 344 | - } |
|
| 345 | - |
|
| 346 | - /** |
|
| 347 | - * Get a link to the subscription on the 3rd party gateway site (if applicable). |
|
| 348 | - * |
|
| 349 | - * @param string $subscription_url transaction url. |
|
| 350 | - * @param WPInv_Subscription $subscription Subscription objectt. |
|
| 351 | - * @return string subscription URL, or empty string. |
|
| 352 | - */ |
|
| 353 | - public function generate_subscription_url( $subscription_url, $subscription ) { |
|
| 354 | - |
|
| 355 | - $profile_id = $subscription->get_profile_id(); |
|
| 356 | - |
|
| 357 | - if ( $this->id == $subscription->get_gateway() && ! empty( $this->view_subscription_url ) && ! empty( $profile_id ) ) { |
|
| 358 | - |
|
| 359 | - $subscription_url = sprintf( $this->view_subscription_url, $profile_id ); |
|
| 360 | - $replace = $this->is_sandbox( $subscription->get_parent_invoice() ) ? 'sandbox' : ''; |
|
| 361 | - $subscription_url = str_replace( '{sandbox}', $replace, $subscription_url ); |
|
| 362 | - |
|
| 363 | - } |
|
| 364 | - |
|
| 365 | - return $subscription_url; |
|
| 366 | - } |
|
| 367 | - |
|
| 368 | - /** |
|
| 369 | - * Check if the gateway is available for use. |
|
| 370 | - * |
|
| 371 | - * @return bool |
|
| 372 | - */ |
|
| 373 | - public function is_available() { |
|
| 374 | - return ! empty( $this->enabled ); |
|
| 375 | - } |
|
| 376 | - |
|
| 377 | - /** |
|
| 378 | - * Return the gateway's title. |
|
| 379 | - * |
|
| 380 | - * @return string |
|
| 381 | - */ |
|
| 382 | - public function get_title() { |
|
| 383 | - return apply_filters( 'getpaid_gateway_title', $this->title, $this ); |
|
| 384 | - } |
|
| 385 | - |
|
| 386 | - /** |
|
| 387 | - * Return the gateway's description. |
|
| 388 | - * |
|
| 389 | - * @return string |
|
| 390 | - */ |
|
| 391 | - public function get_description() { |
|
| 392 | - return apply_filters( 'getpaid_gateway_description', $this->description, $this ); |
|
| 393 | - } |
|
| 394 | - |
|
| 395 | - /** |
|
| 396 | - * Process Payment. |
|
| 397 | - * |
|
| 398 | - * |
|
| 399 | - * @param WPInv_Invoice $invoice Invoice. |
|
| 400 | - * @param array $submission_data Posted checkout fields. |
|
| 401 | - * @param GetPaid_Payment_Form_Submission $submission Checkout submission. |
|
| 402 | - * @return void |
|
| 403 | - */ |
|
| 404 | - public function process_payment( $invoice, $submission_data, $submission ) { |
|
| 405 | - // Process the payment then either redirect to the success page or the gateway. |
|
| 406 | - do_action( 'getpaid_process_invoice_payment_' . $this->id, $invoice, $submission_data, $submission ); |
|
| 407 | - } |
|
| 408 | - |
|
| 409 | - /** |
|
| 410 | - * Process refund. |
|
| 411 | - * |
|
| 412 | - * If the gateway declares 'refunds' support, this will allow it to refund. |
|
| 413 | - * a passed in amount. |
|
| 414 | - * |
|
| 415 | - * @param WPInv_Invoice $invoice Invoice. |
|
| 416 | - * @param float $amount Refund amount. |
|
| 417 | - * @param string $reason Refund reason. |
|
| 418 | - * @return WP_Error|bool True or false based on success, or a WP_Error object. |
|
| 419 | - */ |
|
| 420 | - public function process_refund( $invoice, $amount = null, $reason = '' ) { |
|
| 421 | - return apply_filters( 'getpaid_process_invoice_refund_' . $this->id, false, $invoice, $amount, $reason ); |
|
| 422 | - } |
|
| 423 | - |
|
| 424 | - /** |
|
| 425 | - * Displays the payment fields, credit cards etc. |
|
| 426 | - * |
|
| 427 | - * @param int $invoice_id 0 or invoice id. |
|
| 428 | - * @param GetPaid_Payment_Form $form Current payment form. |
|
| 429 | - */ |
|
| 430 | - public function payment_fields( $invoice_id, $form ) { |
|
| 431 | - do_action( 'getpaid_getpaid_gateway_payment_fields_' . $this->id, $invoice_id, $form ); |
|
| 432 | - } |
|
| 433 | - |
|
| 434 | - /** |
|
| 435 | - * Filters the gateway settings. |
|
| 436 | - * |
|
| 437 | - * @param array $admin_settings |
|
| 438 | - */ |
|
| 439 | - public function admin_settings( $admin_settings ) { |
|
| 440 | - return $admin_settings; |
|
| 441 | - } |
|
| 442 | - |
|
| 443 | - /** |
|
| 444 | - * Retrieves the value of a gateway setting. |
|
| 445 | - * |
|
| 446 | - * @param string $option |
|
| 447 | - */ |
|
| 448 | - public function get_option( $option, $default = false ) { |
|
| 449 | - return wpinv_get_option( $this->id . '_' . $option, $default ); |
|
| 450 | - } |
|
| 451 | - |
|
| 452 | - /** |
|
| 453 | - * Check if a gateway supports a given feature. |
|
| 454 | - * |
|
| 455 | - * Gateways should override this to declare support (or lack of support) for a feature. |
|
| 456 | - * For backward compatibility, gateways support 'products' by default, but nothing else. |
|
| 457 | - * |
|
| 458 | - * @param string $feature string The name of a feature to test support for. |
|
| 459 | - * @return bool True if the gateway supports the feature, false otherwise. |
|
| 460 | - * @since 1.0.19 |
|
| 461 | - */ |
|
| 462 | - public function supports( $feature ) { |
|
| 463 | - return getpaid_payment_gateway_supports( $this->id, $feature ); |
|
| 464 | - } |
|
| 465 | - |
|
| 466 | - /** |
|
| 467 | - * Returns the credit card form html. |
|
| 468 | - * |
|
| 469 | - * @param bool $save whether or not to display the save button. |
|
| 470 | - */ |
|
| 162 | + // Gateway checkout fiellds. |
|
| 163 | + add_action( "wpinv_{$this->id}_cc_form", array( $this, 'payment_fields' ), 10, 2 ); |
|
| 164 | + |
|
| 165 | + // Process payment. |
|
| 166 | + add_action( "getpaid_gateway_{$this->id}", array( $this, 'process_payment' ), 10, 3 ); |
|
| 167 | + |
|
| 168 | + // Change the checkout button text. |
|
| 169 | + if ( ! empty( $this->checkout_button_text ) ) { |
|
| 170 | + add_filter( "getpaid_gateway_{$this->id}_checkout_button_label", array( $this, 'rename_checkout_button' ) ); |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + // Check if a gateway is valid for a given currency. |
|
| 174 | + add_filter( "getpaid_gateway_{$this->id}_is_valid_for_currency", array( $this, 'validate_currency' ), 10, 2 ); |
|
| 175 | + |
|
| 176 | + // Generate the transaction url. |
|
| 177 | + add_filter( "getpaid_gateway_{$this->id}_transaction_url", array( $this, 'filter_transaction_url' ), 10, 2 ); |
|
| 178 | + |
|
| 179 | + // Generate the subscription url. |
|
| 180 | + add_filter( 'getpaid_remote_subscription_profile_url', array( $this, 'generate_subscription_url' ), 10, 2 ); |
|
| 181 | + |
|
| 182 | + // Confirm payments. |
|
| 183 | + add_filter( "wpinv_payment_confirm_{$this->id}", array( $this, 'confirm_payment' ), 10, 2 ); |
|
| 184 | + |
|
| 185 | + // Verify IPNs. |
|
| 186 | + add_action( "wpinv_verify_{$this->id}_ipn", array( $this, 'verify_ipn' ) ); |
|
| 187 | + |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + /** |
|
| 191 | + * Checks if this gateway is a given gateway. |
|
| 192 | + * |
|
| 193 | + * @since 1.0.19 |
|
| 194 | + * @return bool |
|
| 195 | + */ |
|
| 196 | + public function is( $gateway ) { |
|
| 197 | + return $gateway == $this->id; |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + /** |
|
| 201 | + * Returns a users saved tokens for this gateway. |
|
| 202 | + * |
|
| 203 | + * @since 1.0.19 |
|
| 204 | + * @return array |
|
| 205 | + */ |
|
| 206 | + public function get_tokens( $sandbox = null ) { |
|
| 207 | + |
|
| 208 | + if ( is_user_logged_in() && $this->supports( 'tokens' ) && 0 == count( $this->tokens ) ) { |
|
| 209 | + $tokens = get_user_meta( get_current_user_id(), "getpaid_{$this->id}_tokens", true ); |
|
| 210 | + |
|
| 211 | + if ( is_array( $tokens ) ) { |
|
| 212 | + $this->tokens = $tokens; |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + if ( ! is_bool( $sandbox ) ) { |
|
| 218 | + return $this->tokens; |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + // Filter tokens. |
|
| 222 | + $args = array( 'type' => $sandbox ? 'sandbox' : 'live' ); |
|
| 223 | + return wp_list_filter( $this->tokens, $args ); |
|
| 224 | + |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + /** |
|
| 228 | + * Saves a token for this gateway. |
|
| 229 | + * |
|
| 230 | + * @since 1.0.19 |
|
| 231 | + */ |
|
| 232 | + public function save_token( $token ) { |
|
| 233 | + |
|
| 234 | + $tokens = $this->get_tokens(); |
|
| 235 | + $tokens[] = $token; |
|
| 236 | + |
|
| 237 | + update_user_meta( get_current_user_id(), "getpaid_{$this->id}_tokens", $tokens ); |
|
| 238 | + |
|
| 239 | + $this->tokens = $tokens; |
|
| 240 | + |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + /** |
|
| 244 | + * Return the title for admin screens. |
|
| 245 | + * |
|
| 246 | + * @return string |
|
| 247 | + */ |
|
| 248 | + public function get_method_title() { |
|
| 249 | + return apply_filters( 'getpaid_gateway_method_title', $this->method_title, $this ); |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + /** |
|
| 253 | + * Return the description for admin screens. |
|
| 254 | + * |
|
| 255 | + * @return string |
|
| 256 | + */ |
|
| 257 | + public function get_method_description() { |
|
| 258 | + return apply_filters( 'getpaid_gateway_method_description', $this->method_description, $this ); |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + /** |
|
| 262 | + * Get the success url. |
|
| 263 | + * |
|
| 264 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 265 | + * @return string |
|
| 266 | + */ |
|
| 267 | + public function get_return_url( $invoice ) { |
|
| 268 | + |
|
| 269 | + // Payment success url |
|
| 270 | + $return_url = add_query_arg( |
|
| 271 | + array( |
|
| 272 | + 'payment-confirm' => $this->id, |
|
| 273 | + 'invoice_key' => $invoice->get_key(), |
|
| 274 | + 'utm_nooverride' => 1 |
|
| 275 | + ), |
|
| 276 | + wpinv_get_success_page_uri() |
|
| 277 | + ); |
|
| 278 | + |
|
| 279 | + return apply_filters( 'getpaid_gateway_success_url', $return_url, $invoice, $this ); |
|
| 280 | + } |
|
| 281 | + |
|
| 282 | + /** |
|
| 283 | + * Confirms payments when rendering the success page. |
|
| 284 | + * |
|
| 285 | + * @param string $content Success page content. |
|
| 286 | + * @return string |
|
| 287 | + */ |
|
| 288 | + public function confirm_payment( $content ) { |
|
| 289 | + |
|
| 290 | + // Retrieve the invoice. |
|
| 291 | + $invoice_id = getpaid_get_current_invoice_id(); |
|
| 292 | + $invoice = wpinv_get_invoice( $invoice_id ); |
|
| 293 | + |
|
| 294 | + // Ensure that it exists and that it is pending payment. |
|
| 295 | + if ( empty( $invoice_id ) || ! $invoice->needs_payment() ) { |
|
| 296 | + return $content; |
|
| 297 | + } |
|
| 298 | + |
|
| 299 | + // Can the user view this invoice?? |
|
| 300 | + if ( ! wpinv_user_can_view_invoice( $invoice ) ) { |
|
| 301 | + return $content; |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + // Show payment processing indicator. |
|
| 305 | + return wpinv_get_template_html( 'wpinv-payment-processing.php', compact( 'invoice' ) ); |
|
| 306 | + } |
|
| 307 | + |
|
| 308 | + /** |
|
| 309 | + * Processes ipns and marks payments as complete. |
|
| 310 | + * |
|
| 311 | + * @return void |
|
| 312 | + */ |
|
| 313 | + public function verify_ipn() {} |
|
| 314 | + |
|
| 315 | + /** |
|
| 316 | + * Processes invoice addons. |
|
| 317 | + * |
|
| 318 | + * @param WPInv_Invoice $invoice |
|
| 319 | + * @param GetPaid_Form_Item[] $items |
|
| 320 | + * @return WPInv_Invoice |
|
| 321 | + */ |
|
| 322 | + public function process_addons( $invoice, $items ) { |
|
| 323 | + |
|
| 324 | + } |
|
| 325 | + |
|
| 326 | + /** |
|
| 327 | + * Get a link to the transaction on the 3rd party gateway site (if applicable). |
|
| 328 | + * |
|
| 329 | + * @param string $transaction_url transaction url. |
|
| 330 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 331 | + * @return string transaction URL, or empty string. |
|
| 332 | + */ |
|
| 333 | + public function filter_transaction_url( $transaction_url, $invoice ) { |
|
| 334 | + |
|
| 335 | + $transaction_id = $invoice->get_transaction_id(); |
|
| 336 | + |
|
| 337 | + if ( ! empty( $this->view_transaction_url ) && ! empty( $transaction_id ) ) { |
|
| 338 | + $transaction_url = sprintf( $this->view_transaction_url, $transaction_id ); |
|
| 339 | + $replace = $this->is_sandbox( $invoice ) ? 'sandbox' : ''; |
|
| 340 | + $transaction_url = str_replace( '{sandbox}', $replace, $transaction_url ); |
|
| 341 | + } |
|
| 342 | + |
|
| 343 | + return $transaction_url; |
|
| 344 | + } |
|
| 345 | + |
|
| 346 | + /** |
|
| 347 | + * Get a link to the subscription on the 3rd party gateway site (if applicable). |
|
| 348 | + * |
|
| 349 | + * @param string $subscription_url transaction url. |
|
| 350 | + * @param WPInv_Subscription $subscription Subscription objectt. |
|
| 351 | + * @return string subscription URL, or empty string. |
|
| 352 | + */ |
|
| 353 | + public function generate_subscription_url( $subscription_url, $subscription ) { |
|
| 354 | + |
|
| 355 | + $profile_id = $subscription->get_profile_id(); |
|
| 356 | + |
|
| 357 | + if ( $this->id == $subscription->get_gateway() && ! empty( $this->view_subscription_url ) && ! empty( $profile_id ) ) { |
|
| 358 | + |
|
| 359 | + $subscription_url = sprintf( $this->view_subscription_url, $profile_id ); |
|
| 360 | + $replace = $this->is_sandbox( $subscription->get_parent_invoice() ) ? 'sandbox' : ''; |
|
| 361 | + $subscription_url = str_replace( '{sandbox}', $replace, $subscription_url ); |
|
| 362 | + |
|
| 363 | + } |
|
| 364 | + |
|
| 365 | + return $subscription_url; |
|
| 366 | + } |
|
| 367 | + |
|
| 368 | + /** |
|
| 369 | + * Check if the gateway is available for use. |
|
| 370 | + * |
|
| 371 | + * @return bool |
|
| 372 | + */ |
|
| 373 | + public function is_available() { |
|
| 374 | + return ! empty( $this->enabled ); |
|
| 375 | + } |
|
| 376 | + |
|
| 377 | + /** |
|
| 378 | + * Return the gateway's title. |
|
| 379 | + * |
|
| 380 | + * @return string |
|
| 381 | + */ |
|
| 382 | + public function get_title() { |
|
| 383 | + return apply_filters( 'getpaid_gateway_title', $this->title, $this ); |
|
| 384 | + } |
|
| 385 | + |
|
| 386 | + /** |
|
| 387 | + * Return the gateway's description. |
|
| 388 | + * |
|
| 389 | + * @return string |
|
| 390 | + */ |
|
| 391 | + public function get_description() { |
|
| 392 | + return apply_filters( 'getpaid_gateway_description', $this->description, $this ); |
|
| 393 | + } |
|
| 394 | + |
|
| 395 | + /** |
|
| 396 | + * Process Payment. |
|
| 397 | + * |
|
| 398 | + * |
|
| 399 | + * @param WPInv_Invoice $invoice Invoice. |
|
| 400 | + * @param array $submission_data Posted checkout fields. |
|
| 401 | + * @param GetPaid_Payment_Form_Submission $submission Checkout submission. |
|
| 402 | + * @return void |
|
| 403 | + */ |
|
| 404 | + public function process_payment( $invoice, $submission_data, $submission ) { |
|
| 405 | + // Process the payment then either redirect to the success page or the gateway. |
|
| 406 | + do_action( 'getpaid_process_invoice_payment_' . $this->id, $invoice, $submission_data, $submission ); |
|
| 407 | + } |
|
| 408 | + |
|
| 409 | + /** |
|
| 410 | + * Process refund. |
|
| 411 | + * |
|
| 412 | + * If the gateway declares 'refunds' support, this will allow it to refund. |
|
| 413 | + * a passed in amount. |
|
| 414 | + * |
|
| 415 | + * @param WPInv_Invoice $invoice Invoice. |
|
| 416 | + * @param float $amount Refund amount. |
|
| 417 | + * @param string $reason Refund reason. |
|
| 418 | + * @return WP_Error|bool True or false based on success, or a WP_Error object. |
|
| 419 | + */ |
|
| 420 | + public function process_refund( $invoice, $amount = null, $reason = '' ) { |
|
| 421 | + return apply_filters( 'getpaid_process_invoice_refund_' . $this->id, false, $invoice, $amount, $reason ); |
|
| 422 | + } |
|
| 423 | + |
|
| 424 | + /** |
|
| 425 | + * Displays the payment fields, credit cards etc. |
|
| 426 | + * |
|
| 427 | + * @param int $invoice_id 0 or invoice id. |
|
| 428 | + * @param GetPaid_Payment_Form $form Current payment form. |
|
| 429 | + */ |
|
| 430 | + public function payment_fields( $invoice_id, $form ) { |
|
| 431 | + do_action( 'getpaid_getpaid_gateway_payment_fields_' . $this->id, $invoice_id, $form ); |
|
| 432 | + } |
|
| 433 | + |
|
| 434 | + /** |
|
| 435 | + * Filters the gateway settings. |
|
| 436 | + * |
|
| 437 | + * @param array $admin_settings |
|
| 438 | + */ |
|
| 439 | + public function admin_settings( $admin_settings ) { |
|
| 440 | + return $admin_settings; |
|
| 441 | + } |
|
| 442 | + |
|
| 443 | + /** |
|
| 444 | + * Retrieves the value of a gateway setting. |
|
| 445 | + * |
|
| 446 | + * @param string $option |
|
| 447 | + */ |
|
| 448 | + public function get_option( $option, $default = false ) { |
|
| 449 | + return wpinv_get_option( $this->id . '_' . $option, $default ); |
|
| 450 | + } |
|
| 451 | + |
|
| 452 | + /** |
|
| 453 | + * Check if a gateway supports a given feature. |
|
| 454 | + * |
|
| 455 | + * Gateways should override this to declare support (or lack of support) for a feature. |
|
| 456 | + * For backward compatibility, gateways support 'products' by default, but nothing else. |
|
| 457 | + * |
|
| 458 | + * @param string $feature string The name of a feature to test support for. |
|
| 459 | + * @return bool True if the gateway supports the feature, false otherwise. |
|
| 460 | + * @since 1.0.19 |
|
| 461 | + */ |
|
| 462 | + public function supports( $feature ) { |
|
| 463 | + return getpaid_payment_gateway_supports( $this->id, $feature ); |
|
| 464 | + } |
|
| 465 | + |
|
| 466 | + /** |
|
| 467 | + * Returns the credit card form html. |
|
| 468 | + * |
|
| 469 | + * @param bool $save whether or not to display the save button. |
|
| 470 | + */ |
|
| 471 | 471 | public function get_cc_form( $save = false ) { |
| 472 | 472 | |
| 473 | - ob_start(); |
|
| 473 | + ob_start(); |
|
| 474 | 474 | |
| 475 | 475 | $id_prefix = esc_attr( uniqid( $this->id ) ); |
| 476 | 476 | |
@@ -565,11 +565,11 @@ discard block |
||
| 565 | 565 | 'name' => $this->id . '[cc_cvv2]', |
| 566 | 566 | 'id' => "$id_prefix-cc-cvv2", |
| 567 | 567 | 'label' => __( 'CCV', 'invoicing' ), |
| 568 | - 'label_type' => 'vertical', |
|
| 569 | - 'class' => 'form-control-sm', |
|
| 570 | - 'extra_attributes' => array( |
|
| 571 | - 'autocomplete' => "cc-csc", |
|
| 572 | - ), |
|
| 568 | + 'label_type' => 'vertical', |
|
| 569 | + 'class' => 'form-control-sm', |
|
| 570 | + 'extra_attributes' => array( |
|
| 571 | + 'autocomplete' => "cc-csc", |
|
| 572 | + ), |
|
| 573 | 573 | ) |
| 574 | 574 | ); |
| 575 | 575 | ?> |
@@ -579,192 +579,192 @@ discard block |
||
| 579 | 579 | |
| 580 | 580 | <?php |
| 581 | 581 | |
| 582 | - if ( $save ) { |
|
| 583 | - echo $this->save_payment_method_checkbox(); |
|
| 584 | - } |
|
| 582 | + if ( $save ) { |
|
| 583 | + echo $this->save_payment_method_checkbox(); |
|
| 584 | + } |
|
| 585 | 585 | |
| 586 | - ?> |
|
| 586 | + ?> |
|
| 587 | 587 | </div> |
| 588 | 588 | |
| 589 | 589 | </div> |
| 590 | 590 | <?php |
| 591 | 591 | |
| 592 | - return ob_get_clean(); |
|
| 592 | + return ob_get_clean(); |
|
| 593 | + |
|
| 594 | + } |
|
| 593 | 595 | |
| 596 | + /** |
|
| 597 | + * Displays a new payment method entry form. |
|
| 598 | + * |
|
| 599 | + * @since 1.0.19 |
|
| 600 | + */ |
|
| 601 | + public function new_payment_method_entry( $form ) { |
|
| 602 | + echo "<div class='getpaid-new-payment-method-form' style='display:none;'>$form</div>"; |
|
| 594 | 603 | } |
| 595 | 604 | |
| 596 | - /** |
|
| 597 | - * Displays a new payment method entry form. |
|
| 598 | - * |
|
| 599 | - * @since 1.0.19 |
|
| 600 | - */ |
|
| 601 | - public function new_payment_method_entry( $form ) { |
|
| 602 | - echo "<div class='getpaid-new-payment-method-form' style='display:none;'>$form</div>"; |
|
| 603 | - } |
|
| 604 | - |
|
| 605 | - /** |
|
| 606 | - * Grab and display our saved payment methods. |
|
| 607 | - * |
|
| 608 | - * @since 1.0.19 |
|
| 609 | - */ |
|
| 610 | - public function saved_payment_methods() { |
|
| 611 | - $html = '<ul class="getpaid-saved-payment-methods list-unstyled m-0 mt-2" data-count="' . esc_attr( count( $this->get_tokens( $this->is_sandbox() ) ) ) . '">'; |
|
| 612 | - |
|
| 613 | - foreach ( $this->get_tokens( $this->is_sandbox() ) as $token ) { |
|
| 614 | - $html .= $this->get_saved_payment_method_option_html( $token ); |
|
| 615 | - } |
|
| 616 | - |
|
| 617 | - $html .= $this->get_new_payment_method_option_html(); |
|
| 618 | - $html .= '</ul>'; |
|
| 619 | - |
|
| 620 | - echo apply_filters( 'getpaid_payment_gateway_form_saved_payment_methods_html', $html, $this ); |
|
| 621 | - } |
|
| 622 | - |
|
| 623 | - /** |
|
| 624 | - * Gets saved payment method HTML from a token. |
|
| 625 | - * |
|
| 626 | - * @since 1.0.19 |
|
| 627 | - * @param array $token Payment Token. |
|
| 628 | - * @return string Generated payment method HTML |
|
| 629 | - */ |
|
| 630 | - public function get_saved_payment_method_option_html( $token ) { |
|
| 631 | - |
|
| 632 | - return sprintf( |
|
| 633 | - '<li class="getpaid-payment-method form-group"> |
|
| 605 | + /** |
|
| 606 | + * Grab and display our saved payment methods. |
|
| 607 | + * |
|
| 608 | + * @since 1.0.19 |
|
| 609 | + */ |
|
| 610 | + public function saved_payment_methods() { |
|
| 611 | + $html = '<ul class="getpaid-saved-payment-methods list-unstyled m-0 mt-2" data-count="' . esc_attr( count( $this->get_tokens( $this->is_sandbox() ) ) ) . '">'; |
|
| 612 | + |
|
| 613 | + foreach ( $this->get_tokens( $this->is_sandbox() ) as $token ) { |
|
| 614 | + $html .= $this->get_saved_payment_method_option_html( $token ); |
|
| 615 | + } |
|
| 616 | + |
|
| 617 | + $html .= $this->get_new_payment_method_option_html(); |
|
| 618 | + $html .= '</ul>'; |
|
| 619 | + |
|
| 620 | + echo apply_filters( 'getpaid_payment_gateway_form_saved_payment_methods_html', $html, $this ); |
|
| 621 | + } |
|
| 622 | + |
|
| 623 | + /** |
|
| 624 | + * Gets saved payment method HTML from a token. |
|
| 625 | + * |
|
| 626 | + * @since 1.0.19 |
|
| 627 | + * @param array $token Payment Token. |
|
| 628 | + * @return string Generated payment method HTML |
|
| 629 | + */ |
|
| 630 | + public function get_saved_payment_method_option_html( $token ) { |
|
| 631 | + |
|
| 632 | + return sprintf( |
|
| 633 | + '<li class="getpaid-payment-method form-group"> |
|
| 634 | 634 | <label> |
| 635 | 635 | <input name="getpaid-%1$s-payment-method" type="radio" value="%2$s" data-currency="%5$s" style="width:auto;" class="getpaid-saved-payment-method-token-input" %4$s /> |
| 636 | 636 | <span>%3$s</span> |
| 637 | 637 | </label> |
| 638 | 638 | </li>', |
| 639 | - esc_attr( $this->id ), |
|
| 640 | - esc_attr( $token['id'] ), |
|
| 641 | - esc_html( $token['name'] ), |
|
| 642 | - checked( empty( $token['default'] ), false, false ), |
|
| 643 | - empty( $token['currency'] ) ? 'none' : esc_attr( $token['currency'] ) |
|
| 644 | - ); |
|
| 645 | - |
|
| 646 | - } |
|
| 647 | - |
|
| 648 | - /** |
|
| 649 | - * Displays a radio button for entering a new payment method (new CC details) instead of using a saved method. |
|
| 650 | - * |
|
| 651 | - * @since 1.0.19 |
|
| 652 | - */ |
|
| 653 | - public function get_new_payment_method_option_html() { |
|
| 654 | - |
|
| 655 | - $label = apply_filters( 'getpaid_new_payment_method_label', $this->new_method_label ? $this->new_method_label : __( 'Use a new payment method', 'invoicing' ), $this ); |
|
| 656 | - |
|
| 657 | - return sprintf( |
|
| 658 | - '<li class="getpaid-new-payment-method"> |
|
| 639 | + esc_attr( $this->id ), |
|
| 640 | + esc_attr( $token['id'] ), |
|
| 641 | + esc_html( $token['name'] ), |
|
| 642 | + checked( empty( $token['default'] ), false, false ), |
|
| 643 | + empty( $token['currency'] ) ? 'none' : esc_attr( $token['currency'] ) |
|
| 644 | + ); |
|
| 645 | + |
|
| 646 | + } |
|
| 647 | + |
|
| 648 | + /** |
|
| 649 | + * Displays a radio button for entering a new payment method (new CC details) instead of using a saved method. |
|
| 650 | + * |
|
| 651 | + * @since 1.0.19 |
|
| 652 | + */ |
|
| 653 | + public function get_new_payment_method_option_html() { |
|
| 654 | + |
|
| 655 | + $label = apply_filters( 'getpaid_new_payment_method_label', $this->new_method_label ? $this->new_method_label : __( 'Use a new payment method', 'invoicing' ), $this ); |
|
| 656 | + |
|
| 657 | + return sprintf( |
|
| 658 | + '<li class="getpaid-new-payment-method"> |
|
| 659 | 659 | <label> |
| 660 | 660 | <input name="getpaid-%1$s-payment-method" type="radio" data-currency="none" value="new" style="width:auto;" /> |
| 661 | 661 | <span>%2$s</span> |
| 662 | 662 | </label> |
| 663 | 663 | </li>', |
| 664 | - esc_attr( $this->id ), |
|
| 665 | - esc_html( $label ) |
|
| 666 | - ); |
|
| 667 | - |
|
| 668 | - } |
|
| 669 | - |
|
| 670 | - /** |
|
| 671 | - * Outputs a checkbox for saving a new payment method to the database. |
|
| 672 | - * |
|
| 673 | - * @since 1.0.19 |
|
| 674 | - */ |
|
| 675 | - public function save_payment_method_checkbox() { |
|
| 676 | - |
|
| 677 | - return aui()->input( |
|
| 678 | - array( |
|
| 679 | - 'type' => 'checkbox', |
|
| 680 | - 'name' => esc_attr( "getpaid-$this->id-new-payment-method" ), |
|
| 681 | - 'id' => esc_attr( uniqid( $this->id ) ), |
|
| 682 | - 'required' => false, |
|
| 683 | - 'label' => esc_html__( 'Save payment method', 'invoicing' ), |
|
| 684 | - 'value' => 'true', |
|
| 685 | - 'checked' => true, |
|
| 686 | - 'wrap_class' => 'getpaid-save-payment-method pt-1 pb-1', |
|
| 687 | - ) |
|
| 688 | - ); |
|
| 689 | - |
|
| 690 | - } |
|
| 691 | - |
|
| 692 | - /** |
|
| 693 | - * Registers the gateway. |
|
| 694 | - * |
|
| 695 | - * @return array |
|
| 696 | - */ |
|
| 697 | - public function register_gateway( $gateways ) { |
|
| 698 | - |
|
| 699 | - $gateways[ $this->id ] = array( |
|
| 700 | - |
|
| 701 | - 'admin_label' => $this->method_title, |
|
| 664 | + esc_attr( $this->id ), |
|
| 665 | + esc_html( $label ) |
|
| 666 | + ); |
|
| 667 | + |
|
| 668 | + } |
|
| 669 | + |
|
| 670 | + /** |
|
| 671 | + * Outputs a checkbox for saving a new payment method to the database. |
|
| 672 | + * |
|
| 673 | + * @since 1.0.19 |
|
| 674 | + */ |
|
| 675 | + public function save_payment_method_checkbox() { |
|
| 676 | + |
|
| 677 | + return aui()->input( |
|
| 678 | + array( |
|
| 679 | + 'type' => 'checkbox', |
|
| 680 | + 'name' => esc_attr( "getpaid-$this->id-new-payment-method" ), |
|
| 681 | + 'id' => esc_attr( uniqid( $this->id ) ), |
|
| 682 | + 'required' => false, |
|
| 683 | + 'label' => esc_html__( 'Save payment method', 'invoicing' ), |
|
| 684 | + 'value' => 'true', |
|
| 685 | + 'checked' => true, |
|
| 686 | + 'wrap_class' => 'getpaid-save-payment-method pt-1 pb-1', |
|
| 687 | + ) |
|
| 688 | + ); |
|
| 689 | + |
|
| 690 | + } |
|
| 691 | + |
|
| 692 | + /** |
|
| 693 | + * Registers the gateway. |
|
| 694 | + * |
|
| 695 | + * @return array |
|
| 696 | + */ |
|
| 697 | + public function register_gateway( $gateways ) { |
|
| 698 | + |
|
| 699 | + $gateways[ $this->id ] = array( |
|
| 700 | + |
|
| 701 | + 'admin_label' => $this->method_title, |
|
| 702 | 702 | 'checkout_label' => $this->title, |
| 703 | - 'ordering' => $this->order, |
|
| 703 | + 'ordering' => $this->order, |
|
| 704 | 704 | |
| 705 | - ); |
|
| 705 | + ); |
|
| 706 | 706 | |
| 707 | - return $gateways; |
|
| 707 | + return $gateways; |
|
| 708 | 708 | |
| 709 | - } |
|
| 709 | + } |
|
| 710 | 710 | |
| 711 | - /** |
|
| 712 | - * Checks whether or not this is a sandbox request. |
|
| 713 | - * |
|
| 714 | - * @param WPInv_Invoice|null $invoice Invoice object or null. |
|
| 715 | - * @return bool |
|
| 716 | - */ |
|
| 717 | - public function is_sandbox( $invoice = null ) { |
|
| 711 | + /** |
|
| 712 | + * Checks whether or not this is a sandbox request. |
|
| 713 | + * |
|
| 714 | + * @param WPInv_Invoice|null $invoice Invoice object or null. |
|
| 715 | + * @return bool |
|
| 716 | + */ |
|
| 717 | + public function is_sandbox( $invoice = null ) { |
|
| 718 | 718 | |
| 719 | - if ( ! empty( $invoice ) && ! $invoice->needs_payment() ) { |
|
| 720 | - return $invoice->get_mode() == 'test'; |
|
| 721 | - } |
|
| 719 | + if ( ! empty( $invoice ) && ! $invoice->needs_payment() ) { |
|
| 720 | + return $invoice->get_mode() == 'test'; |
|
| 721 | + } |
|
| 722 | 722 | |
| 723 | - return wpinv_is_test_mode( $this->id ); |
|
| 723 | + return wpinv_is_test_mode( $this->id ); |
|
| 724 | 724 | |
| 725 | - } |
|
| 725 | + } |
|
| 726 | 726 | |
| 727 | - /** |
|
| 728 | - * Renames the checkout button |
|
| 729 | - * |
|
| 730 | - * @return string |
|
| 731 | - */ |
|
| 732 | - public function rename_checkout_button() { |
|
| 733 | - return $this->checkout_button_text; |
|
| 734 | - } |
|
| 727 | + /** |
|
| 728 | + * Renames the checkout button |
|
| 729 | + * |
|
| 730 | + * @return string |
|
| 731 | + */ |
|
| 732 | + public function rename_checkout_button() { |
|
| 733 | + return $this->checkout_button_text; |
|
| 734 | + } |
|
| 735 | 735 | |
| 736 | - /** |
|
| 737 | - * Validate gateway currency |
|
| 738 | - * |
|
| 739 | - * @return bool |
|
| 740 | - */ |
|
| 741 | - public function validate_currency( $validation, $currency ) { |
|
| 736 | + /** |
|
| 737 | + * Validate gateway currency |
|
| 738 | + * |
|
| 739 | + * @return bool |
|
| 740 | + */ |
|
| 741 | + public function validate_currency( $validation, $currency ) { |
|
| 742 | 742 | |
| 743 | - // Required currencies. |
|
| 744 | - if ( ! empty( $this->currencies ) && ! in_array( $currency, $this->currencies ) ) { |
|
| 745 | - return false; |
|
| 746 | - } |
|
| 743 | + // Required currencies. |
|
| 744 | + if ( ! empty( $this->currencies ) && ! in_array( $currency, $this->currencies ) ) { |
|
| 745 | + return false; |
|
| 746 | + } |
|
| 747 | 747 | |
| 748 | - // Excluded currencies. |
|
| 749 | - if ( ! empty( $this->exclude_currencies ) && in_array( $currency, $this->exclude_currencies ) ) { |
|
| 750 | - return false; |
|
| 751 | - } |
|
| 748 | + // Excluded currencies. |
|
| 749 | + if ( ! empty( $this->exclude_currencies ) && in_array( $currency, $this->exclude_currencies ) ) { |
|
| 750 | + return false; |
|
| 751 | + } |
|
| 752 | 752 | |
| 753 | - return $validation; |
|
| 754 | - } |
|
| 753 | + return $validation; |
|
| 754 | + } |
|
| 755 | 755 | |
| 756 | - /** |
|
| 757 | - * Displays an error |
|
| 758 | - * |
|
| 759 | - */ |
|
| 760 | - public function show_error( $code, $message, $type ) { |
|
| 756 | + /** |
|
| 757 | + * Displays an error |
|
| 758 | + * |
|
| 759 | + */ |
|
| 760 | + public function show_error( $code, $message, $type ) { |
|
| 761 | 761 | |
| 762 | - if ( is_admin() ) { |
|
| 763 | - getpaid_admin()->{"show_$type"}( $message ); |
|
| 764 | - } |
|
| 762 | + if ( is_admin() ) { |
|
| 763 | + getpaid_admin()->{"show_$type"}( $message ); |
|
| 764 | + } |
|
| 765 | 765 | |
| 766 | - wpinv_set_error( $code, $message, $type ); |
|
| 766 | + wpinv_set_error( $code, $message, $type ); |
|
| 767 | 767 | |
| 768 | - } |
|
| 768 | + } |
|
| 769 | 769 | |
| 770 | 770 | } |
@@ -13,58 +13,58 @@ discard block |
||
| 13 | 13 | class GetPaid_Authorize_Net_Gateway extends GetPaid_Authorize_Net_Legacy_Gateway { |
| 14 | 14 | |
| 15 | 15 | /** |
| 16 | - * Payment method id. |
|
| 17 | - * |
|
| 18 | - * @var string |
|
| 19 | - */ |
|
| 16 | + * Payment method id. |
|
| 17 | + * |
|
| 18 | + * @var string |
|
| 19 | + */ |
|
| 20 | 20 | public $id = 'authorizenet'; |
| 21 | 21 | |
| 22 | 22 | /** |
| 23 | - * An array of features that this gateway supports. |
|
| 24 | - * |
|
| 25 | - * @var array |
|
| 26 | - */ |
|
| 23 | + * An array of features that this gateway supports. |
|
| 24 | + * |
|
| 25 | + * @var array |
|
| 26 | + */ |
|
| 27 | 27 | protected $supports = array( 'subscription', 'sandbox', 'tokens', 'addons', 'single_subscription_group', 'multiple_subscription_groups' ); |
| 28 | 28 | |
| 29 | 29 | /** |
| 30 | - * Payment method order. |
|
| 31 | - * |
|
| 32 | - * @var int |
|
| 33 | - */ |
|
| 30 | + * Payment method order. |
|
| 31 | + * |
|
| 32 | + * @var int |
|
| 33 | + */ |
|
| 34 | 34 | public $order = 4; |
| 35 | 35 | |
| 36 | 36 | /** |
| 37 | - * Endpoint for requests from Authorize.net. |
|
| 38 | - * |
|
| 39 | - * @var string |
|
| 40 | - */ |
|
| 41 | - protected $notify_url; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * Endpoint for requests to Authorize.net. |
|
| 45 | - * |
|
| 46 | - * @var string |
|
| 47 | - */ |
|
| 37 | + * Endpoint for requests from Authorize.net. |
|
| 38 | + * |
|
| 39 | + * @var string |
|
| 40 | + */ |
|
| 41 | + protected $notify_url; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * Endpoint for requests to Authorize.net. |
|
| 45 | + * |
|
| 46 | + * @var string |
|
| 47 | + */ |
|
| 48 | 48 | protected $endpoint; |
| 49 | 49 | |
| 50 | 50 | /** |
| 51 | - * Currencies this gateway is allowed for. |
|
| 52 | - * |
|
| 53 | - * @var array |
|
| 54 | - */ |
|
| 55 | - public $currencies = array( 'USD', 'CAD', 'GBP', 'DKK', 'NOK', 'PLN', 'SEK', 'AUD', 'EUR', 'NZD' ); |
|
| 51 | + * Currencies this gateway is allowed for. |
|
| 52 | + * |
|
| 53 | + * @var array |
|
| 54 | + */ |
|
| 55 | + public $currencies = array( 'USD', 'CAD', 'GBP', 'DKK', 'NOK', 'PLN', 'SEK', 'AUD', 'EUR', 'NZD' ); |
|
| 56 | 56 | |
| 57 | 57 | /** |
| 58 | - * URL to view a transaction. |
|
| 59 | - * |
|
| 60 | - * @var string |
|
| 61 | - */ |
|
| 58 | + * URL to view a transaction. |
|
| 59 | + * |
|
| 60 | + * @var string |
|
| 61 | + */ |
|
| 62 | 62 | public $view_transaction_url = 'https://{sandbox}authorize.net/ui/themes/sandbox/Transaction/TransactionReceipt.aspx?transid=%s'; |
| 63 | 63 | |
| 64 | 64 | /** |
| 65 | - * Class constructor. |
|
| 66 | - */ |
|
| 67 | - public function __construct() { |
|
| 65 | + * Class constructor. |
|
| 66 | + */ |
|
| 67 | + public function __construct() { |
|
| 68 | 68 | |
| 69 | 69 | $this->title = __( 'Credit Card / Debit Card', 'invoicing' ); |
| 70 | 70 | $this->method_title = __( 'Authorize.Net', 'invoicing' ); |
@@ -76,11 +76,11 @@ discard block |
||
| 76 | 76 | } |
| 77 | 77 | |
| 78 | 78 | /** |
| 79 | - * Displays the payment method select field. |
|
| 80 | - * |
|
| 81 | - * @param int $invoice_id 0 or invoice id. |
|
| 82 | - * @param GetPaid_Payment_Form $form Current payment form. |
|
| 83 | - */ |
|
| 79 | + * Displays the payment method select field. |
|
| 80 | + * |
|
| 81 | + * @param int $invoice_id 0 or invoice id. |
|
| 82 | + * @param GetPaid_Payment_Form $form Current payment form. |
|
| 83 | + */ |
|
| 84 | 84 | public function payment_fields( $invoice_id, $form ) { |
| 85 | 85 | |
| 86 | 86 | // Let the user select a payment method. |
@@ -91,16 +91,16 @@ discard block |
||
| 91 | 91 | } |
| 92 | 92 | |
| 93 | 93 | /** |
| 94 | - * Creates a customer profile. |
|
| 95 | - * |
|
| 96 | - * |
|
| 97 | - * @param WPInv_Invoice $invoice Invoice. |
|
| 94 | + * Creates a customer profile. |
|
| 95 | + * |
|
| 96 | + * |
|
| 97 | + * @param WPInv_Invoice $invoice Invoice. |
|
| 98 | 98 | * @param array $submission_data Posted checkout fields. |
| 99 | 99 | * @param bool $save Whether or not to save the payment as a token. |
| 100 | 100 | * @link https://developer.authorize.net/api/reference/index.html#customer-profiles-create-customer-profile |
| 101 | - * @return string|WP_Error Payment profile id. |
|
| 102 | - */ |
|
| 103 | - public function create_customer_profile( $invoice, $submission_data, $save = true ) { |
|
| 101 | + * @return string|WP_Error Payment profile id. |
|
| 102 | + */ |
|
| 103 | + public function create_customer_profile( $invoice, $submission_data, $save = true ) { |
|
| 104 | 104 | |
| 105 | 105 | // Remove non-digits from the number |
| 106 | 106 | $submission_data['authorizenet']['cc_number'] = preg_replace('/\D/', '', $submission_data['authorizenet']['cc_number'] ); |
@@ -175,14 +175,14 @@ discard block |
||
| 175 | 175 | } |
| 176 | 176 | |
| 177 | 177 | /** |
| 178 | - * Retrieves a customer profile. |
|
| 179 | - * |
|
| 180 | - * |
|
| 181 | - * @param string $profile_id profile id. |
|
| 182 | - * @return string|WP_Error Profile id. |
|
| 178 | + * Retrieves a customer profile. |
|
| 179 | + * |
|
| 180 | + * |
|
| 181 | + * @param string $profile_id profile id. |
|
| 182 | + * @return string|WP_Error Profile id. |
|
| 183 | 183 | * @link https://developer.authorize.net/api/reference/index.html#customer-profiles-get-customer-profile |
| 184 | - */ |
|
| 185 | - public function get_customer_profile( $profile_id ) { |
|
| 184 | + */ |
|
| 185 | + public function get_customer_profile( $profile_id ) { |
|
| 186 | 186 | |
| 187 | 187 | // Generate args. |
| 188 | 188 | $args = array( |
@@ -197,17 +197,17 @@ discard block |
||
| 197 | 197 | } |
| 198 | 198 | |
| 199 | 199 | /** |
| 200 | - * Creates a customer profile. |
|
| 201 | - * |
|
| 202 | - * |
|
| 200 | + * Creates a customer profile. |
|
| 201 | + * |
|
| 202 | + * |
|
| 203 | 203 | * @param string $profile_id profile id. |
| 204 | - * @param WPInv_Invoice $invoice Invoice. |
|
| 204 | + * @param WPInv_Invoice $invoice Invoice. |
|
| 205 | 205 | * @param array $submission_data Posted checkout fields. |
| 206 | 206 | * @param bool $save Whether or not to save the payment as a token. |
| 207 | 207 | * @link https://developer.authorize.net/api/reference/index.html#customer-profiles-create-customer-profile |
| 208 | - * @return string|WP_Error Profile id. |
|
| 209 | - */ |
|
| 210 | - public function create_customer_payment_profile( $customer_profile, $invoice, $submission_data, $save ) { |
|
| 208 | + * @return string|WP_Error Profile id. |
|
| 209 | + */ |
|
| 210 | + public function create_customer_payment_profile( $customer_profile, $invoice, $submission_data, $save ) { |
|
| 211 | 211 | |
| 212 | 212 | // Remove non-digits from the number |
| 213 | 213 | $submission_data['authorizenet']['cc_number'] = preg_replace('/\D/', '', $submission_data['authorizenet']['cc_number'] ); |
@@ -282,13 +282,13 @@ discard block |
||
| 282 | 282 | } |
| 283 | 283 | |
| 284 | 284 | /** |
| 285 | - * Retrieves payment details from cache. |
|
| 286 | - * |
|
| 287 | - * |
|
| 285 | + * Retrieves payment details from cache. |
|
| 286 | + * |
|
| 287 | + * |
|
| 288 | 288 | * @param array $payment_details. |
| 289 | - * @return array|false Profile id. |
|
| 290 | - */ |
|
| 291 | - public function retrieve_payment_profile_from_cache( $payment_details, $customer_profile, $invoice ) { |
|
| 289 | + * @return array|false Profile id. |
|
| 290 | + */ |
|
| 291 | + public function retrieve_payment_profile_from_cache( $payment_details, $customer_profile, $invoice ) { |
|
| 292 | 292 | |
| 293 | 293 | $cached_information = get_option( 'getpaid_authorize_net_cached_profiles', array() ); |
| 294 | 294 | $payment_details = hash_hmac( 'sha256', json_encode( $payment_details ), SECURE_AUTH_KEY ); |
@@ -313,13 +313,13 @@ discard block |
||
| 313 | 313 | } |
| 314 | 314 | |
| 315 | 315 | /** |
| 316 | - * Securely adds payment details to cache. |
|
| 317 | - * |
|
| 318 | - * |
|
| 316 | + * Securely adds payment details to cache. |
|
| 317 | + * |
|
| 318 | + * |
|
| 319 | 319 | * @param array $payment_details. |
| 320 | 320 | * @param string $payment_profile_id. |
| 321 | - */ |
|
| 322 | - public function add_payment_profile_to_cache( $payment_details, $payment_profile_id ) { |
|
| 321 | + */ |
|
| 322 | + public function add_payment_profile_to_cache( $payment_details, $payment_profile_id ) { |
|
| 323 | 323 | |
| 324 | 324 | $cached_information = get_option( 'getpaid_authorize_net_cached_profiles', array() ); |
| 325 | 325 | $cached_information = is_array( $cached_information ) ? $cached_information : array(); |
@@ -331,15 +331,15 @@ discard block |
||
| 331 | 331 | } |
| 332 | 332 | |
| 333 | 333 | /** |
| 334 | - * Retrieves a customer payment profile. |
|
| 335 | - * |
|
| 336 | - * |
|
| 337 | - * @param string $customer_profile_id customer profile id. |
|
| 334 | + * Retrieves a customer payment profile. |
|
| 335 | + * |
|
| 336 | + * |
|
| 337 | + * @param string $customer_profile_id customer profile id. |
|
| 338 | 338 | * @param string $payment_profile_id payment profile id. |
| 339 | - * @return string|WP_Error Profile id. |
|
| 339 | + * @return string|WP_Error Profile id. |
|
| 340 | 340 | * @link https://developer.authorize.net/api/reference/index.html#customer-profiles-get-customer-payment-profile |
| 341 | - */ |
|
| 342 | - public function get_customer_payment_profile( $customer_profile_id, $payment_profile_id ) { |
|
| 341 | + */ |
|
| 342 | + public function get_customer_payment_profile( $customer_profile_id, $payment_profile_id ) { |
|
| 343 | 343 | |
| 344 | 344 | // Generate args. |
| 345 | 345 | $args = array( |
@@ -355,15 +355,15 @@ discard block |
||
| 355 | 355 | } |
| 356 | 356 | |
| 357 | 357 | /** |
| 358 | - * Charges a customer payment profile. |
|
| 359 | - * |
|
| 358 | + * Charges a customer payment profile. |
|
| 359 | + * |
|
| 360 | 360 | * @param string $customer_profile_id customer profile id. |
| 361 | 361 | * @param string $payment_profile_id payment profile id. |
| 362 | - * @param WPInv_Invoice $invoice Invoice. |
|
| 362 | + * @param WPInv_Invoice $invoice Invoice. |
|
| 363 | 363 | * @link https://developer.authorize.net/api/reference/index.html#payment-transactions-charge-a-customer-profile |
| 364 | - * @return WP_Error|object |
|
| 365 | - */ |
|
| 366 | - public function charge_customer_payment_profile( $customer_profile_id, $payment_profile_id, $invoice ) { |
|
| 364 | + * @return WP_Error|object |
|
| 365 | + */ |
|
| 366 | + public function charge_customer_payment_profile( $customer_profile_id, $payment_profile_id, $invoice ) { |
|
| 367 | 367 | |
| 368 | 368 | // Generate args. |
| 369 | 369 | $args = array( |
@@ -409,41 +409,41 @@ discard block |
||
| 409 | 409 | } |
| 410 | 410 | |
| 411 | 411 | /** |
| 412 | - * Processes a customer charge. |
|
| 413 | - * |
|
| 412 | + * Processes a customer charge. |
|
| 413 | + * |
|
| 414 | 414 | * @param stdClass $result Api response. |
| 415 | - * @param WPInv_Invoice $invoice Invoice. |
|
| 416 | - */ |
|
| 417 | - public function process_charge_response( $result, $invoice ) { |
|
| 415 | + * @param WPInv_Invoice $invoice Invoice. |
|
| 416 | + */ |
|
| 417 | + public function process_charge_response( $result, $invoice ) { |
|
| 418 | 418 | |
| 419 | 419 | wpinv_clear_errors(); |
| 420 | - $response_code = (int) $result->transactionResponse->responseCode; |
|
| 420 | + $response_code = (int) $result->transactionResponse->responseCode; |
|
| 421 | 421 | |
| 422 | - // Succeeded. |
|
| 423 | - if ( 1 == $response_code || 4 == $response_code ) { |
|
| 422 | + // Succeeded. |
|
| 423 | + if ( 1 == $response_code || 4 == $response_code ) { |
|
| 424 | 424 | |
| 425 | - // Maybe set a transaction id. |
|
| 426 | - if ( ! empty( $result->transactionResponse->transId ) ) { |
|
| 427 | - $invoice->set_transaction_id( $result->transactionResponse->transId ); |
|
| 428 | - } |
|
| 425 | + // Maybe set a transaction id. |
|
| 426 | + if ( ! empty( $result->transactionResponse->transId ) ) { |
|
| 427 | + $invoice->set_transaction_id( $result->transactionResponse->transId ); |
|
| 428 | + } |
|
| 429 | 429 | |
| 430 | - $invoice->add_note( sprintf( __( 'Authentication code: %s (%s).', 'invoicing' ), $result->transactionResponse->authCode, $result->transactionResponse->accountNumber ), false, false, true ); |
|
| 430 | + $invoice->add_note( sprintf( __( 'Authentication code: %s (%s).', 'invoicing' ), $result->transactionResponse->authCode, $result->transactionResponse->accountNumber ), false, false, true ); |
|
| 431 | 431 | |
| 432 | - if ( 1 == $response_code ) { |
|
| 433 | - return $invoice->mark_paid(); |
|
| 434 | - } |
|
| 432 | + if ( 1 == $response_code ) { |
|
| 433 | + return $invoice->mark_paid(); |
|
| 434 | + } |
|
| 435 | 435 | |
| 436 | - $invoice->set_status( 'wpi-onhold' ); |
|
| 437 | - $invoice->add_note( |
|
| 436 | + $invoice->set_status( 'wpi-onhold' ); |
|
| 437 | + $invoice->add_note( |
|
| 438 | 438 | sprintf( |
| 439 | 439 | __( 'Held for review: %s', 'invoicing' ), |
| 440 | 440 | $result->transactionResponse->messages->message[0]->description |
| 441 | 441 | ) |
| 442 | - ); |
|
| 442 | + ); |
|
| 443 | 443 | |
| 444 | - return $invoice->save(); |
|
| 444 | + return $invoice->save(); |
|
| 445 | 445 | |
| 446 | - } |
|
| 446 | + } |
|
| 447 | 447 | |
| 448 | 448 | wpinv_set_error( 'card_declined', __( 'Credit card declined.', 'invoicing' ) ); |
| 449 | 449 | |
@@ -455,13 +455,13 @@ discard block |
||
| 455 | 455 | } |
| 456 | 456 | |
| 457 | 457 | /** |
| 458 | - * Returns payment information. |
|
| 459 | - * |
|
| 460 | - * |
|
| 461 | - * @param array $card Card details. |
|
| 462 | - * @return array |
|
| 463 | - */ |
|
| 464 | - public function get_payment_information( $card ) { |
|
| 458 | + * Returns payment information. |
|
| 459 | + * |
|
| 460 | + * |
|
| 461 | + * @param array $card Card details. |
|
| 462 | + * @return array |
|
| 463 | + */ |
|
| 464 | + public function get_payment_information( $card ) { |
|
| 465 | 465 | return array( |
| 466 | 466 | |
| 467 | 467 | 'creditCard' => array ( |
@@ -474,25 +474,25 @@ discard block |
||
| 474 | 474 | } |
| 475 | 475 | |
| 476 | 476 | /** |
| 477 | - * Returns the customer profile meta name. |
|
| 478 | - * |
|
| 479 | - * |
|
| 480 | - * @param WPInv_Invoice $invoice Invoice. |
|
| 481 | - * @return string |
|
| 482 | - */ |
|
| 483 | - public function get_customer_profile_meta_name( $invoice ) { |
|
| 477 | + * Returns the customer profile meta name. |
|
| 478 | + * |
|
| 479 | + * |
|
| 480 | + * @param WPInv_Invoice $invoice Invoice. |
|
| 481 | + * @return string |
|
| 482 | + */ |
|
| 483 | + public function get_customer_profile_meta_name( $invoice ) { |
|
| 484 | 484 | return $this->is_sandbox( $invoice ) ? 'getpaid_authorizenet_sandbox_customer_profile_id' : 'getpaid_authorizenet_customer_profile_id'; |
| 485 | 485 | } |
| 486 | 486 | |
| 487 | 487 | /** |
| 488 | - * Validates the submitted data. |
|
| 489 | - * |
|
| 490 | - * |
|
| 491 | - * @param array $submission_data Posted checkout fields. |
|
| 488 | + * Validates the submitted data. |
|
| 489 | + * |
|
| 490 | + * |
|
| 491 | + * @param array $submission_data Posted checkout fields. |
|
| 492 | 492 | * @param WPInv_Invoice $invoice |
| 493 | - * @return WP_Error|string The payment profile id |
|
| 494 | - */ |
|
| 495 | - public function validate_submission_data( $submission_data, $invoice ) { |
|
| 493 | + * @return WP_Error|string The payment profile id |
|
| 494 | + */ |
|
| 495 | + public function validate_submission_data( $submission_data, $invoice ) { |
|
| 496 | 496 | |
| 497 | 497 | // Validate authentication details. |
| 498 | 498 | $auth = $this->get_auth_params(); |
@@ -524,13 +524,13 @@ discard block |
||
| 524 | 524 | } |
| 525 | 525 | |
| 526 | 526 | /** |
| 527 | - * Returns invoice line items. |
|
| 528 | - * |
|
| 529 | - * |
|
| 530 | - * @param WPInv_Invoice $invoice Invoice. |
|
| 531 | - * @return array |
|
| 532 | - */ |
|
| 533 | - public function get_line_items( $invoice ) { |
|
| 527 | + * Returns invoice line items. |
|
| 528 | + * |
|
| 529 | + * |
|
| 530 | + * @param WPInv_Invoice $invoice Invoice. |
|
| 531 | + * @return array |
|
| 532 | + */ |
|
| 533 | + public function get_line_items( $invoice ) { |
|
| 534 | 534 | $items = array(); |
| 535 | 535 | |
| 536 | 536 | foreach ( $invoice->get_items() as $item ) { |
@@ -568,15 +568,15 @@ discard block |
||
| 568 | 568 | } |
| 569 | 569 | |
| 570 | 570 | /** |
| 571 | - * Process Payment. |
|
| 572 | - * |
|
| 573 | - * |
|
| 574 | - * @param WPInv_Invoice $invoice Invoice. |
|
| 575 | - * @param array $submission_data Posted checkout fields. |
|
| 576 | - * @param GetPaid_Payment_Form_Submission $submission Checkout submission. |
|
| 577 | - * @return array |
|
| 578 | - */ |
|
| 579 | - public function process_payment( $invoice, $submission_data, $submission ) { |
|
| 571 | + * Process Payment. |
|
| 572 | + * |
|
| 573 | + * |
|
| 574 | + * @param WPInv_Invoice $invoice Invoice. |
|
| 575 | + * @param array $submission_data Posted checkout fields. |
|
| 576 | + * @param GetPaid_Payment_Form_Submission $submission Checkout submission. |
|
| 577 | + * @return array |
|
| 578 | + */ |
|
| 579 | + public function process_payment( $invoice, $submission_data, $submission ) { |
|
| 580 | 580 | |
| 581 | 581 | // Validate the submitted data. |
| 582 | 582 | $payment_profile_id = $this->validate_submission_data( $submission_data, $invoice ); |
@@ -609,45 +609,45 @@ discard block |
||
| 609 | 609 | |
| 610 | 610 | exit; |
| 611 | 611 | |
| 612 | - } |
|
| 612 | + } |
|
| 613 | 613 | |
| 614 | - /** |
|
| 615 | - * Processes the initial payment. |
|
| 616 | - * |
|
| 614 | + /** |
|
| 615 | + * Processes the initial payment. |
|
| 616 | + * |
|
| 617 | 617 | * @param WPInv_Invoice $invoice Invoice. |
| 618 | - */ |
|
| 619 | - protected function process_initial_payment( $invoice ) { |
|
| 618 | + */ |
|
| 619 | + protected function process_initial_payment( $invoice ) { |
|
| 620 | 620 | |
| 621 | - $payment_profile_id = get_post_meta( $invoice->get_id(), 'getpaid_authorizenet_profile_id', true ); |
|
| 621 | + $payment_profile_id = get_post_meta( $invoice->get_id(), 'getpaid_authorizenet_profile_id', true ); |
|
| 622 | 622 | $customer_profile = get_user_meta( $invoice->get_user_id(), $this->get_customer_profile_meta_name( $invoice ), true ); |
| 623 | - $result = $this->charge_customer_payment_profile( $customer_profile, $payment_profile_id, $invoice ); |
|
| 623 | + $result = $this->charge_customer_payment_profile( $customer_profile, $payment_profile_id, $invoice ); |
|
| 624 | 624 | |
| 625 | - // Do we have an error? |
|
| 626 | - if ( is_wp_error( $result ) ) { |
|
| 627 | - wpinv_set_error( $result->get_error_code(), $result->get_error_message() ); |
|
| 628 | - wpinv_send_back_to_checkout( $invoice ); |
|
| 629 | - } |
|
| 625 | + // Do we have an error? |
|
| 626 | + if ( is_wp_error( $result ) ) { |
|
| 627 | + wpinv_set_error( $result->get_error_code(), $result->get_error_message() ); |
|
| 628 | + wpinv_send_back_to_checkout( $invoice ); |
|
| 629 | + } |
|
| 630 | 630 | |
| 631 | - // Process the response. |
|
| 632 | - $this->process_charge_response( $result, $invoice ); |
|
| 631 | + // Process the response. |
|
| 632 | + $this->process_charge_response( $result, $invoice ); |
|
| 633 | 633 | |
| 634 | - if ( wpinv_get_errors() ) { |
|
| 635 | - wpinv_send_back_to_checkout( $invoice ); |
|
| 636 | - } |
|
| 634 | + if ( wpinv_get_errors() ) { |
|
| 635 | + wpinv_send_back_to_checkout( $invoice ); |
|
| 636 | + } |
|
| 637 | 637 | |
| 638 | - } |
|
| 638 | + } |
|
| 639 | 639 | |
| 640 | 640 | /** |
| 641 | - * Processes recurring payments. |
|
| 642 | - * |
|
| 641 | + * Processes recurring payments. |
|
| 642 | + * |
|
| 643 | 643 | * @param WPInv_Invoice $invoice Invoice. |
| 644 | 644 | * @param WPInv_Subscription[]|WPInv_Subscription $subscriptions Subscriptions. |
| 645 | - */ |
|
| 646 | - public function process_subscription( $invoice, $subscriptions ) { |
|
| 645 | + */ |
|
| 646 | + public function process_subscription( $invoice, $subscriptions ) { |
|
| 647 | 647 | |
| 648 | 648 | // Check if there is an initial amount to charge. |
| 649 | 649 | if ( (float) $invoice->get_total() > 0 ) { |
| 650 | - $this->process_initial_payment( $invoice ); |
|
| 650 | + $this->process_initial_payment( $invoice ); |
|
| 651 | 651 | } |
| 652 | 652 | |
| 653 | 653 | // Activate the subscriptions. |
@@ -665,36 +665,36 @@ discard block |
||
| 665 | 665 | } |
| 666 | 666 | } |
| 667 | 667 | |
| 668 | - // Redirect to the success page. |
|
| 668 | + // Redirect to the success page. |
|
| 669 | 669 | wpinv_send_to_success_page( array( 'invoice_key' => $invoice->get_key() ) ); |
| 670 | 670 | |
| 671 | 671 | } |
| 672 | 672 | |
| 673 | - /** |
|
| 674 | - * (Maybe) renews an authorize.net subscription profile. |
|
| 675 | - * |
|
| 676 | - * |
|
| 673 | + /** |
|
| 674 | + * (Maybe) renews an authorize.net subscription profile. |
|
| 675 | + * |
|
| 676 | + * |
|
| 677 | 677 | * @param WPInv_Subscription $subscription |
| 678 | - */ |
|
| 679 | - public function maybe_renew_subscription( $subscription ) { |
|
| 678 | + */ |
|
| 679 | + public function maybe_renew_subscription( $subscription ) { |
|
| 680 | 680 | |
| 681 | 681 | // Ensure its our subscription && it's active. |
| 682 | 682 | if ( $this->id == $subscription->get_gateway() && $subscription->has_status( 'active trialling' ) ) { |
| 683 | 683 | $this->renew_subscription( $subscription ); |
| 684 | 684 | } |
| 685 | 685 | |
| 686 | - } |
|
| 686 | + } |
|
| 687 | 687 | |
| 688 | 688 | /** |
| 689 | - * Renews a subscription. |
|
| 690 | - * |
|
| 689 | + * Renews a subscription. |
|
| 690 | + * |
|
| 691 | 691 | * @param WPInv_Subscription $subscription |
| 692 | - */ |
|
| 693 | - public function renew_subscription( $subscription ) { |
|
| 692 | + */ |
|
| 693 | + public function renew_subscription( $subscription ) { |
|
| 694 | 694 | |
| 695 | - // Generate the renewal invoice. |
|
| 696 | - $new_invoice = $subscription->create_payment(); |
|
| 697 | - $old_invoice = $subscription->get_parent_payment(); |
|
| 695 | + // Generate the renewal invoice. |
|
| 696 | + $new_invoice = $subscription->create_payment(); |
|
| 697 | + $old_invoice = $subscription->get_parent_payment(); |
|
| 698 | 698 | |
| 699 | 699 | if ( empty( $new_invoice ) ) { |
| 700 | 700 | $old_invoice->add_note( __( 'Error generating a renewal invoice.', 'invoicing' ), false, false, false ); |
@@ -703,37 +703,37 @@ discard block |
||
| 703 | 703 | } |
| 704 | 704 | |
| 705 | 705 | // Charge the payment method. |
| 706 | - $payment_profile_id = get_post_meta( $old_invoice->get_id(), 'getpaid_authorizenet_profile_id', true ); |
|
| 707 | - $customer_profile = get_user_meta( $old_invoice->get_user_id(), $this->get_customer_profile_meta_name( $old_invoice ), true ); |
|
| 708 | - $result = $this->charge_customer_payment_profile( $customer_profile, $payment_profile_id, $new_invoice ); |
|
| 709 | - |
|
| 710 | - // Do we have an error? |
|
| 711 | - if ( is_wp_error( $result ) ) { |
|
| 712 | - |
|
| 713 | - $old_invoice->add_note( |
|
| 714 | - sprintf( __( 'Error renewing subscription : ( %s ).', 'invoicing' ), $result->get_error_message() ), |
|
| 715 | - true, |
|
| 716 | - false, |
|
| 717 | - true |
|
| 718 | - ); |
|
| 719 | - $subscription->failing(); |
|
| 720 | - return; |
|
| 721 | - |
|
| 722 | - } |
|
| 723 | - |
|
| 724 | - // Process the response. |
|
| 725 | - $this->process_charge_response( $result, $new_invoice ); |
|
| 726 | - |
|
| 727 | - if ( wpinv_get_errors() ) { |
|
| 728 | - |
|
| 729 | - $old_invoice->add_note( |
|
| 730 | - sprintf( __( 'Error renewing subscription : ( %s ).', 'invoicing' ), getpaid_get_errors_html() ), |
|
| 731 | - true, |
|
| 732 | - false, |
|
| 733 | - true |
|
| 734 | - ); |
|
| 735 | - $subscription->failing(); |
|
| 736 | - return; |
|
| 706 | + $payment_profile_id = get_post_meta( $old_invoice->get_id(), 'getpaid_authorizenet_profile_id', true ); |
|
| 707 | + $customer_profile = get_user_meta( $old_invoice->get_user_id(), $this->get_customer_profile_meta_name( $old_invoice ), true ); |
|
| 708 | + $result = $this->charge_customer_payment_profile( $customer_profile, $payment_profile_id, $new_invoice ); |
|
| 709 | + |
|
| 710 | + // Do we have an error? |
|
| 711 | + if ( is_wp_error( $result ) ) { |
|
| 712 | + |
|
| 713 | + $old_invoice->add_note( |
|
| 714 | + sprintf( __( 'Error renewing subscription : ( %s ).', 'invoicing' ), $result->get_error_message() ), |
|
| 715 | + true, |
|
| 716 | + false, |
|
| 717 | + true |
|
| 718 | + ); |
|
| 719 | + $subscription->failing(); |
|
| 720 | + return; |
|
| 721 | + |
|
| 722 | + } |
|
| 723 | + |
|
| 724 | + // Process the response. |
|
| 725 | + $this->process_charge_response( $result, $new_invoice ); |
|
| 726 | + |
|
| 727 | + if ( wpinv_get_errors() ) { |
|
| 728 | + |
|
| 729 | + $old_invoice->add_note( |
|
| 730 | + sprintf( __( 'Error renewing subscription : ( %s ).', 'invoicing' ), getpaid_get_errors_html() ), |
|
| 731 | + true, |
|
| 732 | + false, |
|
| 733 | + true |
|
| 734 | + ); |
|
| 735 | + $subscription->failing(); |
|
| 736 | + return; |
|
| 737 | 737 | |
| 738 | 738 | } |
| 739 | 739 | |
@@ -742,13 +742,13 @@ discard block |
||
| 742 | 742 | } |
| 743 | 743 | |
| 744 | 744 | /** |
| 745 | - * Processes invoice addons. |
|
| 746 | - * |
|
| 747 | - * @param WPInv_Invoice $invoice |
|
| 748 | - * @param GetPaid_Form_Item[] $items |
|
| 749 | - * @return WPInv_Invoice |
|
| 750 | - */ |
|
| 751 | - public function process_addons( $invoice, $items ) { |
|
| 745 | + * Processes invoice addons. |
|
| 746 | + * |
|
| 747 | + * @param WPInv_Invoice $invoice |
|
| 748 | + * @param GetPaid_Form_Item[] $items |
|
| 749 | + * @return WPInv_Invoice |
|
| 750 | + */ |
|
| 751 | + public function process_addons( $invoice, $items ) { |
|
| 752 | 752 | |
| 753 | 753 | global $getpaid_authorize_addons; |
| 754 | 754 | |
@@ -768,7 +768,7 @@ discard block |
||
| 768 | 768 | $invoice->recalculate_total(); |
| 769 | 769 | |
| 770 | 770 | $payment_profile_id = get_post_meta( $invoice->get_id(), 'getpaid_authorizenet_profile_id', true ); |
| 771 | - $customer_profile = get_user_meta( $invoice->get_user_id(), $this->get_customer_profile_meta_name( $invoice ), true ); |
|
| 771 | + $customer_profile = get_user_meta( $invoice->get_user_id(), $this->get_customer_profile_meta_name( $invoice ), true ); |
|
| 772 | 772 | |
| 773 | 773 | add_filter( 'getpaid_authorizenet_charge_customer_payment_profile_args', array( $this, 'filter_addons_request' ), 10, 2 ); |
| 774 | 774 | $result = $this->charge_customer_payment_profile( $customer_profile, $payment_profile_id, $invoice ); |
@@ -783,11 +783,11 @@ discard block |
||
| 783 | 783 | } |
| 784 | 784 | |
| 785 | 785 | /** |
| 786 | - * Processes invoice addons. |
|
| 787 | - * |
|
| 786 | + * Processes invoice addons. |
|
| 787 | + * |
|
| 788 | 788 | * @param array $args |
| 789 | - * @return array |
|
| 790 | - */ |
|
| 789 | + * @return array |
|
| 790 | + */ |
|
| 791 | 791 | public function filter_addons_request( $args ) { |
| 792 | 792 | |
| 793 | 793 | global $getpaid_authorize_addons; |
@@ -821,11 +821,11 @@ discard block |
||
| 821 | 821 | } |
| 822 | 822 | |
| 823 | 823 | /** |
| 824 | - * Filters the gateway settings. |
|
| 825 | - * |
|
| 826 | - * @param array $admin_settings |
|
| 827 | - */ |
|
| 828 | - public function admin_settings( $admin_settings ) { |
|
| 824 | + * Filters the gateway settings. |
|
| 825 | + * |
|
| 826 | + * @param array $admin_settings |
|
| 827 | + */ |
|
| 828 | + public function admin_settings( $admin_settings ) { |
|
| 829 | 829 | |
| 830 | 830 | $currencies = sprintf( |
| 831 | 831 | __( 'Supported Currencies: %s', 'invoicing' ), |
@@ -865,7 +865,7 @@ discard block |
||
| 865 | 865 | 'readonly' => true, |
| 866 | 866 | ); |
| 867 | 867 | |
| 868 | - return $admin_settings; |
|
| 869 | - } |
|
| 868 | + return $admin_settings; |
|
| 869 | + } |
|
| 870 | 870 | |
| 871 | 871 | } |
@@ -16,165 +16,165 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class GetPaid_MaxMind_Geolocation { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * The service responsible for interacting with the MaxMind database. |
|
| 21 | - * |
|
| 22 | - * @var GetPaid_MaxMind_Database_Service |
|
| 23 | - */ |
|
| 24 | - private $database_service; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * Initialize the integration. |
|
| 28 | - */ |
|
| 29 | - public function __construct() { |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * Supports overriding the database service to be used. |
|
| 33 | - * |
|
| 34 | - * @since 1.0.19 |
|
| 35 | - * @return mixed|null The geolocation database service. |
|
| 36 | - */ |
|
| 37 | - $this->database_service = apply_filters( 'getpaid_maxmind_geolocation_database_service', null ); |
|
| 38 | - if ( null === $this->database_service ) { |
|
| 39 | - $this->database_service = new GetPaid_MaxMind_Database_Service( $this->get_database_prefix() ); |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - // Bind to the scheduled updater action. |
|
| 43 | - add_action( 'getpaid_update_geoip_databases', array( $this, 'update_database' ) ); |
|
| 44 | - |
|
| 45 | - // Bind to the geolocation filter for MaxMind database lookups. |
|
| 46 | - add_filter( 'getpaid_get_geolocation', array( $this, 'get_geolocation' ), 10, 2 ); |
|
| 47 | - |
|
| 48 | - // Handle maxmind key updates. |
|
| 49 | - add_filter( 'wpinv_settings_sanitize_maxmind_license_key', array( $this, 'handle_key_updates' ) ); |
|
| 50 | - |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * Get database service. |
|
| 55 | - * |
|
| 56 | - * @return GetPaid_MaxMind_Database_Service|null |
|
| 57 | - */ |
|
| 58 | - public function get_database_service() { |
|
| 59 | - return $this->database_service; |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * Checks to make sure that the license key is valid. |
|
| 64 | - * |
|
| 65 | - * @param string $license_key The new license key. |
|
| 66 | - * @return string |
|
| 67 | - */ |
|
| 68 | - public function handle_key_updates( $license_key ) { |
|
| 69 | - |
|
| 70 | - // Trim whitespaces and strip slashes. |
|
| 71 | - $license_key = trim( $license_key ); |
|
| 72 | - |
|
| 73 | - // Abort if the license key is empty or unchanged. |
|
| 74 | - if ( empty( $license_key ) ) { |
|
| 75 | - return $license_key; |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - // Abort if a database exists and the license key is unchaged. |
|
| 79 | - if ( file_exists( $this->database_service->get_database_path() && $license_key == wpinv_get_option( 'maxmind_license_key' ) ) ) { |
|
| 80 | - return $license_key; |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - // Check the license key by attempting to download the Geolocation database. |
|
| 84 | - $tmp_database_path = $this->database_service->download_database( $license_key ); |
|
| 85 | - if ( is_wp_error( $tmp_database_path ) ) { |
|
| 86 | - getpaid_admin()->show_error( $tmp_database_path->get_error_message() ); |
|
| 87 | - return $license_key; |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - $this->update_database( /** @scrutinizer ignore-type */ $tmp_database_path ); |
|
| 91 | - |
|
| 92 | - return $license_key; |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * Updates the database used for geolocation queries. |
|
| 97 | - * |
|
| 98 | - * @param string $tmp_database_path Temporary database path. |
|
| 99 | - */ |
|
| 100 | - public function update_database( $tmp_database_path = null ) { |
|
| 101 | - |
|
| 102 | - // Allow us to easily interact with the filesystem. |
|
| 103 | - require_once ABSPATH . 'wp-admin/includes/file.php'; |
|
| 104 | - WP_Filesystem(); |
|
| 105 | - global $wp_filesystem; |
|
| 106 | - |
|
| 107 | - // Remove any existing archives to comply with the MaxMind TOS. |
|
| 108 | - $target_database_path = $this->database_service->get_database_path(); |
|
| 109 | - |
|
| 110 | - // If there's no database path, we can't store the database. |
|
| 111 | - if ( empty( $target_database_path ) ) { |
|
| 112 | - return; |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - if ( $wp_filesystem->exists( $target_database_path ) ) { |
|
| 116 | - $wp_filesystem->delete( $target_database_path ); |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - // We can't download a database if there's no license key configured. |
|
| 120 | - $license_key = wpinv_get_option( 'maxmind_license_key' ); |
|
| 121 | - if ( empty( $license_key ) ) { |
|
| 122 | - return; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - if ( empty( $tmp_database_path ) ) { |
|
| 126 | - $tmp_database_path = $this->database_service->download_database( $license_key ); |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - if ( is_wp_error( $tmp_database_path ) ) { |
|
| 130 | - wpinv_error_log( $tmp_database_path->get_error_message() ); |
|
| 131 | - return; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - // Move the new database into position. |
|
| 135 | - $wp_filesystem->move( $tmp_database_path, $target_database_path, true ); |
|
| 136 | - $wp_filesystem->delete( dirname( $tmp_database_path ) ); |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - /** |
|
| 140 | - * Performs a geolocation lookup against the MaxMind database for the given IP address. |
|
| 141 | - * |
|
| 142 | - * @param array $data Geolocation data. |
|
| 143 | - * @param string $ip_address The IP address to geolocate. |
|
| 144 | - * @return array Geolocation including country code, state, city and postcode based on an IP address. |
|
| 145 | - */ |
|
| 146 | - public function get_geolocation( $data, $ip_address ) { |
|
| 147 | - |
|
| 148 | - if ( ! empty( $data['country'] ) || empty( $ip_address ) ) { |
|
| 149 | - return $data; |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - $country_code = $this->database_service->get_iso_country_code_for_ip( $ip_address ); |
|
| 153 | - |
|
| 154 | - return array( |
|
| 155 | - 'country' => $country_code, |
|
| 156 | - 'state' => '', |
|
| 157 | - 'city' => '', |
|
| 158 | - 'postcode' => '', |
|
| 159 | - ); |
|
| 160 | - |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * Fetches the prefix for the MaxMind database file. |
|
| 165 | - * |
|
| 166 | - * @return string |
|
| 167 | - */ |
|
| 168 | - private function get_database_prefix() { |
|
| 169 | - |
|
| 170 | - $prefix = get_option( 'wpinv_maxmind_database_prefix' ); |
|
| 171 | - |
|
| 172 | - if ( empty( $prefix ) ) { |
|
| 173 | - $prefix = md5( uniqid( 'wpinv' ) ); |
|
| 174 | - update_option( 'wpinv_maxmind_database_prefix', $prefix ); |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - return $prefix; |
|
| 178 | - } |
|
| 19 | + /** |
|
| 20 | + * The service responsible for interacting with the MaxMind database. |
|
| 21 | + * |
|
| 22 | + * @var GetPaid_MaxMind_Database_Service |
|
| 23 | + */ |
|
| 24 | + private $database_service; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * Initialize the integration. |
|
| 28 | + */ |
|
| 29 | + public function __construct() { |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * Supports overriding the database service to be used. |
|
| 33 | + * |
|
| 34 | + * @since 1.0.19 |
|
| 35 | + * @return mixed|null The geolocation database service. |
|
| 36 | + */ |
|
| 37 | + $this->database_service = apply_filters( 'getpaid_maxmind_geolocation_database_service', null ); |
|
| 38 | + if ( null === $this->database_service ) { |
|
| 39 | + $this->database_service = new GetPaid_MaxMind_Database_Service( $this->get_database_prefix() ); |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + // Bind to the scheduled updater action. |
|
| 43 | + add_action( 'getpaid_update_geoip_databases', array( $this, 'update_database' ) ); |
|
| 44 | + |
|
| 45 | + // Bind to the geolocation filter for MaxMind database lookups. |
|
| 46 | + add_filter( 'getpaid_get_geolocation', array( $this, 'get_geolocation' ), 10, 2 ); |
|
| 47 | + |
|
| 48 | + // Handle maxmind key updates. |
|
| 49 | + add_filter( 'wpinv_settings_sanitize_maxmind_license_key', array( $this, 'handle_key_updates' ) ); |
|
| 50 | + |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * Get database service. |
|
| 55 | + * |
|
| 56 | + * @return GetPaid_MaxMind_Database_Service|null |
|
| 57 | + */ |
|
| 58 | + public function get_database_service() { |
|
| 59 | + return $this->database_service; |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * Checks to make sure that the license key is valid. |
|
| 64 | + * |
|
| 65 | + * @param string $license_key The new license key. |
|
| 66 | + * @return string |
|
| 67 | + */ |
|
| 68 | + public function handle_key_updates( $license_key ) { |
|
| 69 | + |
|
| 70 | + // Trim whitespaces and strip slashes. |
|
| 71 | + $license_key = trim( $license_key ); |
|
| 72 | + |
|
| 73 | + // Abort if the license key is empty or unchanged. |
|
| 74 | + if ( empty( $license_key ) ) { |
|
| 75 | + return $license_key; |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + // Abort if a database exists and the license key is unchaged. |
|
| 79 | + if ( file_exists( $this->database_service->get_database_path() && $license_key == wpinv_get_option( 'maxmind_license_key' ) ) ) { |
|
| 80 | + return $license_key; |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + // Check the license key by attempting to download the Geolocation database. |
|
| 84 | + $tmp_database_path = $this->database_service->download_database( $license_key ); |
|
| 85 | + if ( is_wp_error( $tmp_database_path ) ) { |
|
| 86 | + getpaid_admin()->show_error( $tmp_database_path->get_error_message() ); |
|
| 87 | + return $license_key; |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + $this->update_database( /** @scrutinizer ignore-type */ $tmp_database_path ); |
|
| 91 | + |
|
| 92 | + return $license_key; |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * Updates the database used for geolocation queries. |
|
| 97 | + * |
|
| 98 | + * @param string $tmp_database_path Temporary database path. |
|
| 99 | + */ |
|
| 100 | + public function update_database( $tmp_database_path = null ) { |
|
| 101 | + |
|
| 102 | + // Allow us to easily interact with the filesystem. |
|
| 103 | + require_once ABSPATH . 'wp-admin/includes/file.php'; |
|
| 104 | + WP_Filesystem(); |
|
| 105 | + global $wp_filesystem; |
|
| 106 | + |
|
| 107 | + // Remove any existing archives to comply with the MaxMind TOS. |
|
| 108 | + $target_database_path = $this->database_service->get_database_path(); |
|
| 109 | + |
|
| 110 | + // If there's no database path, we can't store the database. |
|
| 111 | + if ( empty( $target_database_path ) ) { |
|
| 112 | + return; |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + if ( $wp_filesystem->exists( $target_database_path ) ) { |
|
| 116 | + $wp_filesystem->delete( $target_database_path ); |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + // We can't download a database if there's no license key configured. |
|
| 120 | + $license_key = wpinv_get_option( 'maxmind_license_key' ); |
|
| 121 | + if ( empty( $license_key ) ) { |
|
| 122 | + return; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + if ( empty( $tmp_database_path ) ) { |
|
| 126 | + $tmp_database_path = $this->database_service->download_database( $license_key ); |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + if ( is_wp_error( $tmp_database_path ) ) { |
|
| 130 | + wpinv_error_log( $tmp_database_path->get_error_message() ); |
|
| 131 | + return; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + // Move the new database into position. |
|
| 135 | + $wp_filesystem->move( $tmp_database_path, $target_database_path, true ); |
|
| 136 | + $wp_filesystem->delete( dirname( $tmp_database_path ) ); |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + /** |
|
| 140 | + * Performs a geolocation lookup against the MaxMind database for the given IP address. |
|
| 141 | + * |
|
| 142 | + * @param array $data Geolocation data. |
|
| 143 | + * @param string $ip_address The IP address to geolocate. |
|
| 144 | + * @return array Geolocation including country code, state, city and postcode based on an IP address. |
|
| 145 | + */ |
|
| 146 | + public function get_geolocation( $data, $ip_address ) { |
|
| 147 | + |
|
| 148 | + if ( ! empty( $data['country'] ) || empty( $ip_address ) ) { |
|
| 149 | + return $data; |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + $country_code = $this->database_service->get_iso_country_code_for_ip( $ip_address ); |
|
| 153 | + |
|
| 154 | + return array( |
|
| 155 | + 'country' => $country_code, |
|
| 156 | + 'state' => '', |
|
| 157 | + 'city' => '', |
|
| 158 | + 'postcode' => '', |
|
| 159 | + ); |
|
| 160 | + |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * Fetches the prefix for the MaxMind database file. |
|
| 165 | + * |
|
| 166 | + * @return string |
|
| 167 | + */ |
|
| 168 | + private function get_database_prefix() { |
|
| 169 | + |
|
| 170 | + $prefix = get_option( 'wpinv_maxmind_database_prefix' ); |
|
| 171 | + |
|
| 172 | + if ( empty( $prefix ) ) { |
|
| 173 | + $prefix = md5( uniqid( 'wpinv' ) ); |
|
| 174 | + update_option( 'wpinv_maxmind_database_prefix', $prefix ); |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + return $prefix; |
|
| 178 | + } |
|
| 179 | 179 | |
| 180 | 180 | } |
@@ -13,30 +13,30 @@ discard block |
||
| 13 | 13 | class GetPaid_Manual_Gateway extends GetPaid_Payment_Gateway { |
| 14 | 14 | |
| 15 | 15 | /** |
| 16 | - * Payment method id. |
|
| 17 | - * |
|
| 18 | - * @var string |
|
| 19 | - */ |
|
| 16 | + * Payment method id. |
|
| 17 | + * |
|
| 18 | + * @var string |
|
| 19 | + */ |
|
| 20 | 20 | public $id = 'manual'; |
| 21 | 21 | |
| 22 | 22 | /** |
| 23 | - * An array of features that this gateway supports. |
|
| 24 | - * |
|
| 25 | - * @var array |
|
| 26 | - */ |
|
| 23 | + * An array of features that this gateway supports. |
|
| 24 | + * |
|
| 25 | + * @var array |
|
| 26 | + */ |
|
| 27 | 27 | protected $supports = array( 'subscription', 'addons', 'single_subscription_group', 'multiple_subscription_groups' ); |
| 28 | 28 | |
| 29 | 29 | /** |
| 30 | - * Payment method order. |
|
| 31 | - * |
|
| 32 | - * @var int |
|
| 33 | - */ |
|
| 34 | - public $order = 11; |
|
| 30 | + * Payment method order. |
|
| 31 | + * |
|
| 32 | + * @var int |
|
| 33 | + */ |
|
| 34 | + public $order = 11; |
|
| 35 | 35 | |
| 36 | 36 | /** |
| 37 | - * Class constructor. |
|
| 38 | - */ |
|
| 39 | - public function __construct() { |
|
| 37 | + * Class constructor. |
|
| 38 | + */ |
|
| 39 | + public function __construct() { |
|
| 40 | 40 | parent::__construct(); |
| 41 | 41 | |
| 42 | 42 | $this->title = __( 'Test Gateway', 'invoicing' ); |
@@ -46,15 +46,15 @@ discard block |
||
| 46 | 46 | } |
| 47 | 47 | |
| 48 | 48 | /** |
| 49 | - * Process Payment. |
|
| 50 | - * |
|
| 51 | - * |
|
| 52 | - * @param WPInv_Invoice $invoice Invoice. |
|
| 53 | - * @param array $submission_data Posted checkout fields. |
|
| 54 | - * @param GetPaid_Payment_Form_Submission $submission Checkout submission. |
|
| 55 | - * @return array |
|
| 56 | - */ |
|
| 57 | - public function process_payment( $invoice, $submission_data, $submission ) { |
|
| 49 | + * Process Payment. |
|
| 50 | + * |
|
| 51 | + * |
|
| 52 | + * @param WPInv_Invoice $invoice Invoice. |
|
| 53 | + * @param array $submission_data Posted checkout fields. |
|
| 54 | + * @param GetPaid_Payment_Form_Submission $submission Checkout submission. |
|
| 55 | + * @return array |
|
| 56 | + */ |
|
| 57 | + public function process_payment( $invoice, $submission_data, $submission ) { |
|
| 58 | 58 | |
| 59 | 59 | // Mark it as paid. |
| 60 | 60 | $invoice->mark_paid(); |
@@ -85,12 +85,12 @@ discard block |
||
| 85 | 85 | } |
| 86 | 86 | |
| 87 | 87 | /** |
| 88 | - * (Maybe) renews a manual subscription profile. |
|
| 89 | - * |
|
| 90 | - * |
|
| 88 | + * (Maybe) renews a manual subscription profile. |
|
| 89 | + * |
|
| 90 | + * |
|
| 91 | 91 | * @param WPInv_Subscription $subscription |
| 92 | - */ |
|
| 93 | - public function maybe_renew_subscription( $subscription ) { |
|
| 92 | + */ |
|
| 93 | + public function maybe_renew_subscription( $subscription ) { |
|
| 94 | 94 | |
| 95 | 95 | // Ensure its our subscription && it's active. |
| 96 | 96 | if ( $this->id == $subscription->get_gateway() && $subscription->has_status( 'active trialling' ) ) { |
@@ -110,13 +110,13 @@ discard block |
||
| 110 | 110 | } |
| 111 | 111 | |
| 112 | 112 | /** |
| 113 | - * Processes invoice addons. |
|
| 114 | - * |
|
| 115 | - * @param WPInv_Invoice $invoice |
|
| 116 | - * @param GetPaid_Form_Item[] $items |
|
| 117 | - * @return WPInv_Invoice |
|
| 118 | - */ |
|
| 119 | - public function process_addons( $invoice, $items ) { |
|
| 113 | + * Processes invoice addons. |
|
| 114 | + * |
|
| 115 | + * @param WPInv_Invoice $invoice |
|
| 116 | + * @param GetPaid_Form_Item[] $items |
|
| 117 | + * @return WPInv_Invoice |
|
| 118 | + */ |
|
| 119 | + public function process_addons( $invoice, $items ) { |
|
| 120 | 120 | |
| 121 | 121 | foreach ( $items as $item ) { |
| 122 | 122 | $invoice->add_item( $item ); |
@@ -9,7 +9,7 @@ discard block |
||
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 12 | - exit; |
|
| 12 | + exit; |
|
| 13 | 13 | } |
| 14 | 14 | |
| 15 | 15 | /** |
@@ -21,356 +21,356 @@ discard block |
||
| 21 | 21 | */ |
| 22 | 22 | abstract class GetPaid_Data { |
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * ID for this object. |
|
| 26 | - * |
|
| 27 | - * @since 1.0.19 |
|
| 28 | - * @var int |
|
| 29 | - */ |
|
| 30 | - protected $id = 0; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * Core data for this object. Name value pairs (name + default value). |
|
| 34 | - * |
|
| 35 | - * @since 1.0.19 |
|
| 36 | - * @var array |
|
| 37 | - */ |
|
| 38 | - protected $data = array(); |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * Core data changes for this object. |
|
| 42 | - * |
|
| 43 | - * @since 1.0.19 |
|
| 44 | - * @var array |
|
| 45 | - */ |
|
| 46 | - protected $changes = array(); |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * This is false until the object is read from the DB. |
|
| 50 | - * |
|
| 51 | - * @since 1.0.19 |
|
| 52 | - * @var bool |
|
| 53 | - */ |
|
| 54 | - protected $object_read = false; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * This is the name of this object type. |
|
| 58 | - * |
|
| 59 | - * @since 1.0.19 |
|
| 60 | - * @var string |
|
| 61 | - */ |
|
| 62 | - protected $object_type = 'data'; |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Extra data for this object. Name value pairs (name + default value). |
|
| 66 | - * Used as a standard way for sub classes (like item types) to add |
|
| 67 | - * additional information to an inherited class. |
|
| 68 | - * |
|
| 69 | - * @since 1.0.19 |
|
| 70 | - * @var array |
|
| 71 | - */ |
|
| 72 | - protected $extra_data = array(); |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * Set to _data on construct so we can track and reset data if needed. |
|
| 76 | - * |
|
| 77 | - * @since 1.0.19 |
|
| 78 | - * @var array |
|
| 79 | - */ |
|
| 80 | - protected $default_data = array(); |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * Contains a reference to the data store for this class. |
|
| 84 | - * |
|
| 85 | - * @since 1.0.19 |
|
| 86 | - * @var GetPaid_Data_Store |
|
| 87 | - */ |
|
| 88 | - protected $data_store; |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Stores meta in cache for future reads. |
|
| 92 | - * A group must be set to to enable caching. |
|
| 93 | - * |
|
| 94 | - * @since 1.0.19 |
|
| 95 | - * @var string |
|
| 96 | - */ |
|
| 97 | - protected $cache_group = ''; |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * Stores the last error. |
|
| 101 | - * |
|
| 102 | - * @since 1.0.19 |
|
| 103 | - * @var string |
|
| 104 | - */ |
|
| 105 | - public $last_error = ''; |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Stores additional meta data. |
|
| 109 | - * |
|
| 110 | - * @since 1.0.19 |
|
| 111 | - * @var array |
|
| 112 | - */ |
|
| 113 | - protected $meta_data = null; |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * Default constructor. |
|
| 117 | - * |
|
| 118 | - * @param int|object|array|string $read ID to load from the DB (optional) or already queried data. |
|
| 119 | - */ |
|
| 120 | - public function __construct( $read = 0 ) { |
|
| 121 | - $this->data = array_merge( $this->data, $this->extra_data ); |
|
| 122 | - $this->default_data = $this->data; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * Only store the object ID to avoid serializing the data object instance. |
|
| 127 | - * |
|
| 128 | - * @return array |
|
| 129 | - */ |
|
| 130 | - public function __sleep() { |
|
| 131 | - return array( 'id' ); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * Re-run the constructor with the object ID. |
|
| 136 | - * |
|
| 137 | - * If the object no longer exists, remove the ID. |
|
| 138 | - */ |
|
| 139 | - public function __wakeup() { |
|
| 140 | - $this->__construct( absint( $this->id ) ); |
|
| 141 | - |
|
| 142 | - if ( ! empty( $this->last_error ) ) { |
|
| 143 | - $this->set_id( 0 ); |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * When the object is cloned, make sure meta is duplicated correctly. |
|
| 150 | - * |
|
| 151 | - * @since 1.0.19 |
|
| 152 | - */ |
|
| 153 | - public function __clone() { |
|
| 154 | - $this->maybe_read_meta_data(); |
|
| 155 | - if ( ! empty( $this->meta_data ) ) { |
|
| 156 | - foreach ( $this->meta_data as $array_key => $meta ) { |
|
| 157 | - $this->meta_data[ $array_key ] = clone $meta; |
|
| 158 | - if ( ! empty( $meta->id ) ) { |
|
| 159 | - $this->meta_data[ $array_key ]->id = null; |
|
| 160 | - } |
|
| 161 | - } |
|
| 162 | - } |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * Get the data store. |
|
| 167 | - * |
|
| 168 | - * @since 1.0.19 |
|
| 169 | - * @return object |
|
| 170 | - */ |
|
| 171 | - public function get_data_store() { |
|
| 172 | - return $this->data_store; |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * Get the object type. |
|
| 177 | - * |
|
| 178 | - * @since 1.0.19 |
|
| 179 | - * @return string |
|
| 180 | - */ |
|
| 181 | - public function get_object_type() { |
|
| 182 | - return $this->object_type; |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - /** |
|
| 186 | - * Returns the unique ID for this object. |
|
| 187 | - * |
|
| 188 | - * @since 1.0.19 |
|
| 189 | - * @return int |
|
| 190 | - */ |
|
| 191 | - public function get_id() { |
|
| 192 | - return $this->id; |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - /** |
|
| 196 | - * Get form status. |
|
| 197 | - * |
|
| 198 | - * @since 1.0.19 |
|
| 199 | - * @param string $context View or edit context. |
|
| 200 | - * @return string |
|
| 201 | - */ |
|
| 202 | - public function get_status( $context = 'view' ) { |
|
| 203 | - return $this->get_prop( 'status', $context ); |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - /** |
|
| 207 | - * Delete an object, set the ID to 0, and return result. |
|
| 208 | - * |
|
| 209 | - * @since 1.0.19 |
|
| 210 | - * @param bool $force_delete Should the data be deleted permanently. |
|
| 211 | - * @return bool result |
|
| 212 | - */ |
|
| 213 | - public function delete( $force_delete = false ) { |
|
| 214 | - if ( $this->data_store && $this->exists() ) { |
|
| 215 | - $this->data_store->delete( $this, array( 'force_delete' => $force_delete ) ); |
|
| 216 | - $this->set_id( 0 ); |
|
| 217 | - return true; |
|
| 218 | - } |
|
| 219 | - return false; |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - /** |
|
| 223 | - * Save should create or update based on object existence. |
|
| 224 | - * |
|
| 225 | - * @since 1.0.19 |
|
| 226 | - * @return int |
|
| 227 | - */ |
|
| 228 | - public function save() { |
|
| 229 | - if ( ! $this->data_store ) { |
|
| 230 | - return $this->get_id(); |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - /** |
|
| 234 | - * Trigger action before saving to the DB. Allows you to adjust object props before save. |
|
| 235 | - * |
|
| 236 | - * @param GetPaid_Data $this The object being saved. |
|
| 237 | - * @param GetPaid_Data_Store_WP $data_store The data store persisting the data. |
|
| 238 | - */ |
|
| 239 | - do_action( 'getpaid_before_' . $this->object_type . '_object_save', $this, $this->data_store ); |
|
| 240 | - |
|
| 241 | - if ( $this->get_id() ) { |
|
| 242 | - $this->data_store->update( $this ); |
|
| 243 | - } else { |
|
| 244 | - $this->data_store->create( $this ); |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - /** |
|
| 248 | - * Trigger action after saving to the DB. |
|
| 249 | - * |
|
| 250 | - * @param GetPaid_Data $this The object being saved. |
|
| 251 | - * @param GetPaid_Data_Store_WP $data_store The data store persisting the data. |
|
| 252 | - */ |
|
| 253 | - do_action( 'getpaid_after_' . $this->object_type . '_object_save', $this, $this->data_store ); |
|
| 254 | - |
|
| 255 | - return $this->get_id(); |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - /** |
|
| 259 | - * Change data to JSON format. |
|
| 260 | - * |
|
| 261 | - * @since 1.0.19 |
|
| 262 | - * @return string Data in JSON format. |
|
| 263 | - */ |
|
| 264 | - public function __toString() { |
|
| 265 | - return wp_json_encode( $this->get_data() ); |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - /** |
|
| 269 | - * Returns all data for this object. |
|
| 270 | - * |
|
| 271 | - * @since 1.0.19 |
|
| 272 | - * @return array |
|
| 273 | - */ |
|
| 274 | - public function get_data() { |
|
| 275 | - return array_merge( array( 'id' => $this->get_id() ), $this->data, array( 'meta_data' => $this->get_meta_data() ) ); |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - /** |
|
| 279 | - * Returns array of expected data keys for this object. |
|
| 280 | - * |
|
| 281 | - * @since 1.0.19 |
|
| 282 | - * @return array |
|
| 283 | - */ |
|
| 284 | - public function get_data_keys() { |
|
| 285 | - return array_keys( $this->data ); |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - /** |
|
| 289 | - * Returns all "extra" data keys for an object (for sub objects like item types). |
|
| 290 | - * |
|
| 291 | - * @since 1.0.19 |
|
| 292 | - * @return array |
|
| 293 | - */ |
|
| 294 | - public function get_extra_data_keys() { |
|
| 295 | - return array_keys( $this->extra_data ); |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - /** |
|
| 299 | - * Filter null meta values from array. |
|
| 300 | - * |
|
| 301 | - * @since 1.0.19 |
|
| 302 | - * @param mixed $meta Meta value to check. |
|
| 303 | - * @return bool |
|
| 304 | - */ |
|
| 305 | - protected function filter_null_meta( $meta ) { |
|
| 306 | - return ! is_null( $meta->value ); |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - /** |
|
| 310 | - * Get All Meta Data. |
|
| 311 | - * |
|
| 312 | - * @since 1.0.19 |
|
| 313 | - * @return array of objects. |
|
| 314 | - */ |
|
| 315 | - public function get_meta_data() { |
|
| 316 | - $this->maybe_read_meta_data(); |
|
| 317 | - return array_values( array_filter( $this->meta_data, array( $this, 'filter_null_meta' ) ) ); |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - /** |
|
| 321 | - * Check if the key is an internal one. |
|
| 322 | - * |
|
| 323 | - * @since 1.0.19 |
|
| 324 | - * @param string $key Key to check. |
|
| 325 | - * @return bool true if it's an internal key, false otherwise |
|
| 326 | - */ |
|
| 327 | - protected function is_internal_meta_key( $key ) { |
|
| 328 | - $internal_meta_key = ! empty( $key ) && $this->data_store && in_array( $key, $this->data_store->get_internal_meta_keys(), true ); |
|
| 329 | - |
|
| 330 | - if ( ! $internal_meta_key ) { |
|
| 331 | - return false; |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - $has_setter_or_getter = is_callable( array( $this, 'set_' . $key ) ) || is_callable( array( $this, 'get_' . $key ) ); |
|
| 335 | - |
|
| 336 | - if ( ! $has_setter_or_getter ) { |
|
| 337 | - return false; |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - /* translators: %s: $key Key to check */ |
|
| 341 | - getpaid_doing_it_wrong( __FUNCTION__, sprintf( __( 'Generic add/update/get meta methods should not be used for internal meta data, including "%s". Use getters and setters.', 'invoicing' ), $key ), '1.0.19' ); |
|
| 342 | - |
|
| 343 | - return true; |
|
| 344 | - } |
|
| 345 | - |
|
| 346 | - /** |
|
| 347 | - * Magic method for setting data fields. |
|
| 348 | - * |
|
| 349 | - * This method does not update custom fields in the database. |
|
| 350 | - * |
|
| 351 | - * @since 1.0.19 |
|
| 352 | - * @access public |
|
| 353 | - * |
|
| 354 | - */ |
|
| 355 | - public function __set( $key, $value ) { |
|
| 356 | - |
|
| 357 | - if ( 'id' == strtolower( $key ) ) { |
|
| 358 | - return $this->set_id( $value ); |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - if ( method_exists( $this, "set_$key") ) { |
|
| 362 | - |
|
| 363 | - /* translators: %s: $key Key to set */ |
|
| 364 | - getpaid_doing_it_wrong( __FUNCTION__, sprintf( __( 'Object data such as "%s" should not be accessed directly. Use getters and setters.', 'invoicing' ), $key ), '1.0.19' ); |
|
| 365 | - |
|
| 366 | - call_user_func( array( $this, "set_$key" ), $value ); |
|
| 367 | - } else { |
|
| 368 | - $this->set_prop( $key, $value ); |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - } |
|
| 372 | - |
|
| 373 | - /** |
|
| 24 | + /** |
|
| 25 | + * ID for this object. |
|
| 26 | + * |
|
| 27 | + * @since 1.0.19 |
|
| 28 | + * @var int |
|
| 29 | + */ |
|
| 30 | + protected $id = 0; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * Core data for this object. Name value pairs (name + default value). |
|
| 34 | + * |
|
| 35 | + * @since 1.0.19 |
|
| 36 | + * @var array |
|
| 37 | + */ |
|
| 38 | + protected $data = array(); |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * Core data changes for this object. |
|
| 42 | + * |
|
| 43 | + * @since 1.0.19 |
|
| 44 | + * @var array |
|
| 45 | + */ |
|
| 46 | + protected $changes = array(); |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * This is false until the object is read from the DB. |
|
| 50 | + * |
|
| 51 | + * @since 1.0.19 |
|
| 52 | + * @var bool |
|
| 53 | + */ |
|
| 54 | + protected $object_read = false; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * This is the name of this object type. |
|
| 58 | + * |
|
| 59 | + * @since 1.0.19 |
|
| 60 | + * @var string |
|
| 61 | + */ |
|
| 62 | + protected $object_type = 'data'; |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Extra data for this object. Name value pairs (name + default value). |
|
| 66 | + * Used as a standard way for sub classes (like item types) to add |
|
| 67 | + * additional information to an inherited class. |
|
| 68 | + * |
|
| 69 | + * @since 1.0.19 |
|
| 70 | + * @var array |
|
| 71 | + */ |
|
| 72 | + protected $extra_data = array(); |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * Set to _data on construct so we can track and reset data if needed. |
|
| 76 | + * |
|
| 77 | + * @since 1.0.19 |
|
| 78 | + * @var array |
|
| 79 | + */ |
|
| 80 | + protected $default_data = array(); |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * Contains a reference to the data store for this class. |
|
| 84 | + * |
|
| 85 | + * @since 1.0.19 |
|
| 86 | + * @var GetPaid_Data_Store |
|
| 87 | + */ |
|
| 88 | + protected $data_store; |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Stores meta in cache for future reads. |
|
| 92 | + * A group must be set to to enable caching. |
|
| 93 | + * |
|
| 94 | + * @since 1.0.19 |
|
| 95 | + * @var string |
|
| 96 | + */ |
|
| 97 | + protected $cache_group = ''; |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * Stores the last error. |
|
| 101 | + * |
|
| 102 | + * @since 1.0.19 |
|
| 103 | + * @var string |
|
| 104 | + */ |
|
| 105 | + public $last_error = ''; |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * Stores additional meta data. |
|
| 109 | + * |
|
| 110 | + * @since 1.0.19 |
|
| 111 | + * @var array |
|
| 112 | + */ |
|
| 113 | + protected $meta_data = null; |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * Default constructor. |
|
| 117 | + * |
|
| 118 | + * @param int|object|array|string $read ID to load from the DB (optional) or already queried data. |
|
| 119 | + */ |
|
| 120 | + public function __construct( $read = 0 ) { |
|
| 121 | + $this->data = array_merge( $this->data, $this->extra_data ); |
|
| 122 | + $this->default_data = $this->data; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * Only store the object ID to avoid serializing the data object instance. |
|
| 127 | + * |
|
| 128 | + * @return array |
|
| 129 | + */ |
|
| 130 | + public function __sleep() { |
|
| 131 | + return array( 'id' ); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * Re-run the constructor with the object ID. |
|
| 136 | + * |
|
| 137 | + * If the object no longer exists, remove the ID. |
|
| 138 | + */ |
|
| 139 | + public function __wakeup() { |
|
| 140 | + $this->__construct( absint( $this->id ) ); |
|
| 141 | + |
|
| 142 | + if ( ! empty( $this->last_error ) ) { |
|
| 143 | + $this->set_id( 0 ); |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * When the object is cloned, make sure meta is duplicated correctly. |
|
| 150 | + * |
|
| 151 | + * @since 1.0.19 |
|
| 152 | + */ |
|
| 153 | + public function __clone() { |
|
| 154 | + $this->maybe_read_meta_data(); |
|
| 155 | + if ( ! empty( $this->meta_data ) ) { |
|
| 156 | + foreach ( $this->meta_data as $array_key => $meta ) { |
|
| 157 | + $this->meta_data[ $array_key ] = clone $meta; |
|
| 158 | + if ( ! empty( $meta->id ) ) { |
|
| 159 | + $this->meta_data[ $array_key ]->id = null; |
|
| 160 | + } |
|
| 161 | + } |
|
| 162 | + } |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * Get the data store. |
|
| 167 | + * |
|
| 168 | + * @since 1.0.19 |
|
| 169 | + * @return object |
|
| 170 | + */ |
|
| 171 | + public function get_data_store() { |
|
| 172 | + return $this->data_store; |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * Get the object type. |
|
| 177 | + * |
|
| 178 | + * @since 1.0.19 |
|
| 179 | + * @return string |
|
| 180 | + */ |
|
| 181 | + public function get_object_type() { |
|
| 182 | + return $this->object_type; |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + /** |
|
| 186 | + * Returns the unique ID for this object. |
|
| 187 | + * |
|
| 188 | + * @since 1.0.19 |
|
| 189 | + * @return int |
|
| 190 | + */ |
|
| 191 | + public function get_id() { |
|
| 192 | + return $this->id; |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + /** |
|
| 196 | + * Get form status. |
|
| 197 | + * |
|
| 198 | + * @since 1.0.19 |
|
| 199 | + * @param string $context View or edit context. |
|
| 200 | + * @return string |
|
| 201 | + */ |
|
| 202 | + public function get_status( $context = 'view' ) { |
|
| 203 | + return $this->get_prop( 'status', $context ); |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + /** |
|
| 207 | + * Delete an object, set the ID to 0, and return result. |
|
| 208 | + * |
|
| 209 | + * @since 1.0.19 |
|
| 210 | + * @param bool $force_delete Should the data be deleted permanently. |
|
| 211 | + * @return bool result |
|
| 212 | + */ |
|
| 213 | + public function delete( $force_delete = false ) { |
|
| 214 | + if ( $this->data_store && $this->exists() ) { |
|
| 215 | + $this->data_store->delete( $this, array( 'force_delete' => $force_delete ) ); |
|
| 216 | + $this->set_id( 0 ); |
|
| 217 | + return true; |
|
| 218 | + } |
|
| 219 | + return false; |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + /** |
|
| 223 | + * Save should create or update based on object existence. |
|
| 224 | + * |
|
| 225 | + * @since 1.0.19 |
|
| 226 | + * @return int |
|
| 227 | + */ |
|
| 228 | + public function save() { |
|
| 229 | + if ( ! $this->data_store ) { |
|
| 230 | + return $this->get_id(); |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + /** |
|
| 234 | + * Trigger action before saving to the DB. Allows you to adjust object props before save. |
|
| 235 | + * |
|
| 236 | + * @param GetPaid_Data $this The object being saved. |
|
| 237 | + * @param GetPaid_Data_Store_WP $data_store The data store persisting the data. |
|
| 238 | + */ |
|
| 239 | + do_action( 'getpaid_before_' . $this->object_type . '_object_save', $this, $this->data_store ); |
|
| 240 | + |
|
| 241 | + if ( $this->get_id() ) { |
|
| 242 | + $this->data_store->update( $this ); |
|
| 243 | + } else { |
|
| 244 | + $this->data_store->create( $this ); |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + /** |
|
| 248 | + * Trigger action after saving to the DB. |
|
| 249 | + * |
|
| 250 | + * @param GetPaid_Data $this The object being saved. |
|
| 251 | + * @param GetPaid_Data_Store_WP $data_store The data store persisting the data. |
|
| 252 | + */ |
|
| 253 | + do_action( 'getpaid_after_' . $this->object_type . '_object_save', $this, $this->data_store ); |
|
| 254 | + |
|
| 255 | + return $this->get_id(); |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + /** |
|
| 259 | + * Change data to JSON format. |
|
| 260 | + * |
|
| 261 | + * @since 1.0.19 |
|
| 262 | + * @return string Data in JSON format. |
|
| 263 | + */ |
|
| 264 | + public function __toString() { |
|
| 265 | + return wp_json_encode( $this->get_data() ); |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + /** |
|
| 269 | + * Returns all data for this object. |
|
| 270 | + * |
|
| 271 | + * @since 1.0.19 |
|
| 272 | + * @return array |
|
| 273 | + */ |
|
| 274 | + public function get_data() { |
|
| 275 | + return array_merge( array( 'id' => $this->get_id() ), $this->data, array( 'meta_data' => $this->get_meta_data() ) ); |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + /** |
|
| 279 | + * Returns array of expected data keys for this object. |
|
| 280 | + * |
|
| 281 | + * @since 1.0.19 |
|
| 282 | + * @return array |
|
| 283 | + */ |
|
| 284 | + public function get_data_keys() { |
|
| 285 | + return array_keys( $this->data ); |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + /** |
|
| 289 | + * Returns all "extra" data keys for an object (for sub objects like item types). |
|
| 290 | + * |
|
| 291 | + * @since 1.0.19 |
|
| 292 | + * @return array |
|
| 293 | + */ |
|
| 294 | + public function get_extra_data_keys() { |
|
| 295 | + return array_keys( $this->extra_data ); |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + /** |
|
| 299 | + * Filter null meta values from array. |
|
| 300 | + * |
|
| 301 | + * @since 1.0.19 |
|
| 302 | + * @param mixed $meta Meta value to check. |
|
| 303 | + * @return bool |
|
| 304 | + */ |
|
| 305 | + protected function filter_null_meta( $meta ) { |
|
| 306 | + return ! is_null( $meta->value ); |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + /** |
|
| 310 | + * Get All Meta Data. |
|
| 311 | + * |
|
| 312 | + * @since 1.0.19 |
|
| 313 | + * @return array of objects. |
|
| 314 | + */ |
|
| 315 | + public function get_meta_data() { |
|
| 316 | + $this->maybe_read_meta_data(); |
|
| 317 | + return array_values( array_filter( $this->meta_data, array( $this, 'filter_null_meta' ) ) ); |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + /** |
|
| 321 | + * Check if the key is an internal one. |
|
| 322 | + * |
|
| 323 | + * @since 1.0.19 |
|
| 324 | + * @param string $key Key to check. |
|
| 325 | + * @return bool true if it's an internal key, false otherwise |
|
| 326 | + */ |
|
| 327 | + protected function is_internal_meta_key( $key ) { |
|
| 328 | + $internal_meta_key = ! empty( $key ) && $this->data_store && in_array( $key, $this->data_store->get_internal_meta_keys(), true ); |
|
| 329 | + |
|
| 330 | + if ( ! $internal_meta_key ) { |
|
| 331 | + return false; |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + $has_setter_or_getter = is_callable( array( $this, 'set_' . $key ) ) || is_callable( array( $this, 'get_' . $key ) ); |
|
| 335 | + |
|
| 336 | + if ( ! $has_setter_or_getter ) { |
|
| 337 | + return false; |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + /* translators: %s: $key Key to check */ |
|
| 341 | + getpaid_doing_it_wrong( __FUNCTION__, sprintf( __( 'Generic add/update/get meta methods should not be used for internal meta data, including "%s". Use getters and setters.', 'invoicing' ), $key ), '1.0.19' ); |
|
| 342 | + |
|
| 343 | + return true; |
|
| 344 | + } |
|
| 345 | + |
|
| 346 | + /** |
|
| 347 | + * Magic method for setting data fields. |
|
| 348 | + * |
|
| 349 | + * This method does not update custom fields in the database. |
|
| 350 | + * |
|
| 351 | + * @since 1.0.19 |
|
| 352 | + * @access public |
|
| 353 | + * |
|
| 354 | + */ |
|
| 355 | + public function __set( $key, $value ) { |
|
| 356 | + |
|
| 357 | + if ( 'id' == strtolower( $key ) ) { |
|
| 358 | + return $this->set_id( $value ); |
|
| 359 | + } |
|
| 360 | + |
|
| 361 | + if ( method_exists( $this, "set_$key") ) { |
|
| 362 | + |
|
| 363 | + /* translators: %s: $key Key to set */ |
|
| 364 | + getpaid_doing_it_wrong( __FUNCTION__, sprintf( __( 'Object data such as "%s" should not be accessed directly. Use getters and setters.', 'invoicing' ), $key ), '1.0.19' ); |
|
| 365 | + |
|
| 366 | + call_user_func( array( $this, "set_$key" ), $value ); |
|
| 367 | + } else { |
|
| 368 | + $this->set_prop( $key, $value ); |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + } |
|
| 372 | + |
|
| 373 | + /** |
|
| 374 | 374 | * Margic method for retrieving a property. |
| 375 | 375 | */ |
| 376 | 376 | public function __get( $key ) { |
@@ -378,10 +378,10 @@ discard block |
||
| 378 | 378 | // Check if we have a helper method for that. |
| 379 | 379 | if ( method_exists( $this, 'get_' . $key ) ) { |
| 380 | 380 | |
| 381 | - if ( 'post_type' != $key ) { |
|
| 382 | - /* translators: %s: $key Key to set */ |
|
| 383 | - getpaid_doing_it_wrong( __FUNCTION__, sprintf( __( 'Object data such as "%s" should not be accessed directly. Use getters and setters.', 'invoicing' ), $key ), '1.0.19' ); |
|
| 384 | - } |
|
| 381 | + if ( 'post_type' != $key ) { |
|
| 382 | + /* translators: %s: $key Key to set */ |
|
| 383 | + getpaid_doing_it_wrong( __FUNCTION__, sprintf( __( 'Object data such as "%s" should not be accessed directly. Use getters and setters.', 'invoicing' ), $key ), '1.0.19' ); |
|
| 384 | + } |
|
| 385 | 385 | |
| 386 | 386 | return call_user_func( array( $this, 'get_' . $key ) ); |
| 387 | 387 | } |
@@ -391,515 +391,515 @@ discard block |
||
| 391 | 391 | return $this->post->$key; |
| 392 | 392 | } |
| 393 | 393 | |
| 394 | - return $this->get_prop( $key ); |
|
| 395 | - |
|
| 396 | - } |
|
| 397 | - |
|
| 398 | - /** |
|
| 399 | - * Get Meta Data by Key. |
|
| 400 | - * |
|
| 401 | - * @since 1.0.19 |
|
| 402 | - * @param string $key Meta Key. |
|
| 403 | - * @param bool $single return first found meta with key, or all with $key. |
|
| 404 | - * @param string $context What the value is for. Valid values are view and edit. |
|
| 405 | - * @return mixed |
|
| 406 | - */ |
|
| 407 | - public function get_meta( $key = '', $single = true, $context = 'view' ) { |
|
| 408 | - |
|
| 409 | - // Check if this is an internal meta key. |
|
| 410 | - $_key = str_replace( '_wpinv', '', $key ); |
|
| 411 | - $_key = str_replace( 'wpinv', '', $_key ); |
|
| 412 | - if ( $this->is_internal_meta_key( $key ) ) { |
|
| 413 | - $function = 'get_' . $_key; |
|
| 414 | - |
|
| 415 | - if ( is_callable( array( $this, $function ) ) ) { |
|
| 416 | - return $this->{$function}(); |
|
| 417 | - } |
|
| 418 | - } |
|
| 419 | - |
|
| 420 | - // Read the meta data if not yet read. |
|
| 421 | - $this->maybe_read_meta_data(); |
|
| 422 | - $meta_data = $this->get_meta_data(); |
|
| 423 | - $array_keys = array_keys( wp_list_pluck( $meta_data, 'key' ), $key, true ); |
|
| 424 | - $value = $single ? '' : array(); |
|
| 425 | - |
|
| 426 | - if ( ! empty( $array_keys ) ) { |
|
| 427 | - // We don't use the $this->meta_data property directly here because we don't want meta with a null value (i.e. meta which has been deleted via $this->delete_meta_data()). |
|
| 428 | - if ( $single ) { |
|
| 429 | - $value = $meta_data[ current( $array_keys ) ]->value; |
|
| 430 | - } else { |
|
| 431 | - $value = array_intersect_key( $meta_data, array_flip( $array_keys ) ); |
|
| 432 | - } |
|
| 433 | - } |
|
| 434 | - |
|
| 435 | - if ( 'view' === $context ) { |
|
| 436 | - $value = apply_filters( $this->get_hook_prefix() . $key, $value, $this ); |
|
| 437 | - } |
|
| 438 | - |
|
| 439 | - return $value; |
|
| 440 | - } |
|
| 441 | - |
|
| 442 | - /** |
|
| 443 | - * See if meta data exists, since get_meta always returns a '' or array(). |
|
| 444 | - * |
|
| 445 | - * @since 1.0.19 |
|
| 446 | - * @param string $key Meta Key. |
|
| 447 | - * @return boolean |
|
| 448 | - */ |
|
| 449 | - public function meta_exists( $key = '' ) { |
|
| 450 | - $this->maybe_read_meta_data(); |
|
| 451 | - $array_keys = wp_list_pluck( $this->get_meta_data(), 'key' ); |
|
| 452 | - return in_array( $key, $array_keys, true ); |
|
| 453 | - } |
|
| 454 | - |
|
| 455 | - /** |
|
| 456 | - * Set all meta data from array. |
|
| 457 | - * |
|
| 458 | - * @since 1.0.19 |
|
| 459 | - * @param array $data Key/Value pairs. |
|
| 460 | - */ |
|
| 461 | - public function set_meta_data( $data ) { |
|
| 462 | - if ( ! empty( $data ) && is_array( $data ) ) { |
|
| 463 | - $this->maybe_read_meta_data(); |
|
| 464 | - foreach ( $data as $meta ) { |
|
| 465 | - $meta = (array) $meta; |
|
| 466 | - if ( isset( $meta['key'], $meta['value'], $meta['id'] ) ) { |
|
| 467 | - $this->meta_data[] = new GetPaid_Meta_Data( |
|
| 468 | - array( |
|
| 469 | - 'id' => $meta['id'], |
|
| 470 | - 'key' => $meta['key'], |
|
| 471 | - 'value' => $meta['value'], |
|
| 472 | - ) |
|
| 473 | - ); |
|
| 474 | - } |
|
| 475 | - } |
|
| 476 | - } |
|
| 477 | - } |
|
| 478 | - |
|
| 479 | - /** |
|
| 480 | - * Add meta data. |
|
| 481 | - * |
|
| 482 | - * @since 1.0.19 |
|
| 483 | - * |
|
| 484 | - * @param string $key Meta key. |
|
| 485 | - * @param string|array $value Meta value. |
|
| 486 | - * @param bool $unique Should this be a unique key?. |
|
| 487 | - */ |
|
| 488 | - public function add_meta_data( $key, $value, $unique = false ) { |
|
| 489 | - if ( $this->is_internal_meta_key( $key ) ) { |
|
| 490 | - $function = 'set_' . $key; |
|
| 491 | - |
|
| 492 | - if ( is_callable( array( $this, $function ) ) ) { |
|
| 493 | - return $this->{$function}( $value ); |
|
| 494 | - } |
|
| 495 | - } |
|
| 496 | - |
|
| 497 | - $this->maybe_read_meta_data(); |
|
| 498 | - if ( $unique ) { |
|
| 499 | - $this->delete_meta_data( $key ); |
|
| 500 | - } |
|
| 501 | - $this->meta_data[] = new GetPaid_Meta_Data( |
|
| 502 | - array( |
|
| 503 | - 'key' => $key, |
|
| 504 | - 'value' => $value, |
|
| 505 | - ) |
|
| 506 | - ); |
|
| 507 | - |
|
| 508 | - $this->save(); |
|
| 509 | - } |
|
| 510 | - |
|
| 511 | - /** |
|
| 512 | - * Update meta data by key or ID, if provided. |
|
| 513 | - * |
|
| 514 | - * @since 1.0.19 |
|
| 515 | - * |
|
| 516 | - * @param string $key Meta key. |
|
| 517 | - * @param string|array $value Meta value. |
|
| 518 | - * @param int $meta_id Meta ID. |
|
| 519 | - */ |
|
| 520 | - public function update_meta_data( $key, $value, $meta_id = 0 ) { |
|
| 521 | - if ( $this->is_internal_meta_key( $key ) ) { |
|
| 522 | - $function = 'set_' . $key; |
|
| 523 | - |
|
| 524 | - if ( is_callable( array( $this, $function ) ) ) { |
|
| 525 | - return $this->{$function}( $value ); |
|
| 526 | - } |
|
| 527 | - } |
|
| 528 | - |
|
| 529 | - $this->maybe_read_meta_data(); |
|
| 530 | - |
|
| 531 | - $array_key = false; |
|
| 532 | - |
|
| 533 | - if ( $meta_id ) { |
|
| 534 | - $array_keys = array_keys( wp_list_pluck( $this->meta_data, 'id' ), $meta_id, true ); |
|
| 535 | - $array_key = $array_keys ? current( $array_keys ) : false; |
|
| 536 | - } else { |
|
| 537 | - // Find matches by key. |
|
| 538 | - $matches = array(); |
|
| 539 | - foreach ( $this->meta_data as $meta_data_array_key => $meta ) { |
|
| 540 | - if ( $meta->key === $key ) { |
|
| 541 | - $matches[] = $meta_data_array_key; |
|
| 542 | - } |
|
| 543 | - } |
|
| 544 | - |
|
| 545 | - if ( ! empty( $matches ) ) { |
|
| 546 | - // Set matches to null so only one key gets the new value. |
|
| 547 | - foreach ( $matches as $meta_data_array_key ) { |
|
| 548 | - $this->meta_data[ $meta_data_array_key ]->value = null; |
|
| 549 | - } |
|
| 550 | - $array_key = current( $matches ); |
|
| 551 | - } |
|
| 552 | - } |
|
| 553 | - |
|
| 554 | - if ( false !== $array_key ) { |
|
| 555 | - $meta = $this->meta_data[ $array_key ]; |
|
| 556 | - $meta->key = $key; |
|
| 557 | - $meta->value = $value; |
|
| 558 | - } else { |
|
| 559 | - $this->add_meta_data( $key, $value, true ); |
|
| 560 | - } |
|
| 561 | - } |
|
| 562 | - |
|
| 563 | - /** |
|
| 564 | - * Delete meta data. |
|
| 565 | - * |
|
| 566 | - * @since 1.0.19 |
|
| 567 | - * @param string $key Meta key. |
|
| 568 | - */ |
|
| 569 | - public function delete_meta_data( $key ) { |
|
| 570 | - $this->maybe_read_meta_data(); |
|
| 571 | - $array_keys = array_keys( wp_list_pluck( $this->meta_data, 'key' ), $key, true ); |
|
| 572 | - |
|
| 573 | - if ( $array_keys ) { |
|
| 574 | - foreach ( $array_keys as $array_key ) { |
|
| 575 | - $this->meta_data[ $array_key ]->value = null; |
|
| 576 | - } |
|
| 577 | - } |
|
| 578 | - } |
|
| 579 | - |
|
| 580 | - /** |
|
| 581 | - * Delete meta data. |
|
| 582 | - * |
|
| 583 | - * @since 1.0.19 |
|
| 584 | - * @param int $mid Meta ID. |
|
| 585 | - */ |
|
| 586 | - public function delete_meta_data_by_mid( $mid ) { |
|
| 587 | - $this->maybe_read_meta_data(); |
|
| 588 | - $array_keys = array_keys( wp_list_pluck( $this->meta_data, 'id' ), (int) $mid, true ); |
|
| 589 | - |
|
| 590 | - if ( $array_keys ) { |
|
| 591 | - foreach ( $array_keys as $array_key ) { |
|
| 592 | - $this->meta_data[ $array_key ]->value = null; |
|
| 593 | - } |
|
| 594 | - } |
|
| 595 | - } |
|
| 596 | - |
|
| 597 | - /** |
|
| 598 | - * Read meta data if null. |
|
| 599 | - * |
|
| 600 | - * @since 1.0.19 |
|
| 601 | - */ |
|
| 602 | - protected function maybe_read_meta_data() { |
|
| 603 | - if ( is_null( $this->meta_data ) ) { |
|
| 604 | - $this->read_meta_data(); |
|
| 605 | - } |
|
| 606 | - } |
|
| 607 | - |
|
| 608 | - /** |
|
| 609 | - * Read Meta Data from the database. Ignore any internal properties. |
|
| 610 | - * Uses it's own caches because get_metadata does not provide meta_ids. |
|
| 611 | - * |
|
| 612 | - * @since 1.0.19 |
|
| 613 | - * @param bool $force_read True to force a new DB read (and update cache). |
|
| 614 | - */ |
|
| 615 | - public function read_meta_data( $force_read = false ) { |
|
| 616 | - |
|
| 617 | - // Reset meta data. |
|
| 618 | - $this->meta_data = array(); |
|
| 619 | - |
|
| 620 | - // Maybe abort early. |
|
| 621 | - if ( ! $this->get_id() || ! $this->data_store ) { |
|
| 622 | - return; |
|
| 623 | - } |
|
| 624 | - |
|
| 625 | - // Only read from cache if the cache key is set. |
|
| 626 | - $cache_key = null; |
|
| 627 | - if ( ! $force_read && ! empty( $this->cache_group ) ) { |
|
| 628 | - $cache_key = GetPaid_Cache_Helper::get_cache_prefix( $this->cache_group ) . GetPaid_Cache_Helper::get_cache_prefix( 'object_' . $this->get_id() ) . 'object_meta_' . $this->get_id(); |
|
| 629 | - $raw_meta_data = wp_cache_get( $cache_key, $this->cache_group ); |
|
| 630 | - } |
|
| 631 | - |
|
| 632 | - // Should we force read? |
|
| 633 | - if ( empty( $raw_meta_data ) ) { |
|
| 634 | - $raw_meta_data = $this->data_store->read_meta( $this ); |
|
| 635 | - |
|
| 636 | - if ( ! empty( $cache_key ) ) { |
|
| 637 | - wp_cache_set( $cache_key, $raw_meta_data, $this->cache_group ); |
|
| 638 | - } |
|
| 639 | - |
|
| 640 | - } |
|
| 641 | - |
|
| 642 | - // Set meta data. |
|
| 643 | - if ( is_array( $raw_meta_data ) ) { |
|
| 644 | - |
|
| 645 | - foreach ( $raw_meta_data as $meta ) { |
|
| 646 | - $this->meta_data[] = new GetPaid_Meta_Data( |
|
| 647 | - array( |
|
| 648 | - 'id' => (int) $meta->meta_id, |
|
| 649 | - 'key' => $meta->meta_key, |
|
| 650 | - 'value' => maybe_unserialize( $meta->meta_value ), |
|
| 651 | - ) |
|
| 652 | - ); |
|
| 653 | - } |
|
| 654 | - |
|
| 655 | - } |
|
| 656 | - |
|
| 657 | - } |
|
| 658 | - |
|
| 659 | - /** |
|
| 660 | - * Update Meta Data in the database. |
|
| 661 | - * |
|
| 662 | - * @since 1.0.19 |
|
| 663 | - */ |
|
| 664 | - public function save_meta_data() { |
|
| 665 | - if ( ! $this->data_store || is_null( $this->meta_data ) ) { |
|
| 666 | - return; |
|
| 667 | - } |
|
| 668 | - foreach ( $this->meta_data as $array_key => $meta ) { |
|
| 669 | - if ( is_null( $meta->value ) ) { |
|
| 670 | - if ( ! empty( $meta->id ) ) { |
|
| 671 | - $this->data_store->delete_meta( $this, $meta ); |
|
| 672 | - unset( $this->meta_data[ $array_key ] ); |
|
| 673 | - } |
|
| 674 | - } elseif ( empty( $meta->id ) ) { |
|
| 675 | - $meta->id = $this->data_store->add_meta( $this, $meta ); |
|
| 676 | - $meta->apply_changes(); |
|
| 677 | - } else { |
|
| 678 | - if ( $meta->get_changes() ) { |
|
| 679 | - $this->data_store->update_meta( $this, $meta ); |
|
| 680 | - $meta->apply_changes(); |
|
| 681 | - } |
|
| 682 | - } |
|
| 683 | - } |
|
| 684 | - if ( ! empty( $this->cache_group ) ) { |
|
| 685 | - $cache_key = GetPaid_Cache_Helper::get_cache_prefix( $this->cache_group ) . GetPaid_Cache_Helper::get_cache_prefix( 'object_' . $this->get_id() ) . 'object_meta_' . $this->get_id(); |
|
| 686 | - wp_cache_delete( $cache_key, $this->cache_group ); |
|
| 687 | - } |
|
| 688 | - } |
|
| 689 | - |
|
| 690 | - /** |
|
| 691 | - * Set ID. |
|
| 692 | - * |
|
| 693 | - * @since 1.0.19 |
|
| 694 | - * @param int $id ID. |
|
| 695 | - */ |
|
| 696 | - public function set_id( $id ) { |
|
| 697 | - $this->id = absint( $id ); |
|
| 698 | - } |
|
| 699 | - |
|
| 700 | - /** |
|
| 701 | - * Sets item status. |
|
| 702 | - * |
|
| 703 | - * @since 1.0.19 |
|
| 704 | - * @param string $status New status. |
|
| 705 | - * @return array details of change. |
|
| 706 | - */ |
|
| 707 | - public function set_status( $status ) { |
|
| 394 | + return $this->get_prop( $key ); |
|
| 395 | + |
|
| 396 | + } |
|
| 397 | + |
|
| 398 | + /** |
|
| 399 | + * Get Meta Data by Key. |
|
| 400 | + * |
|
| 401 | + * @since 1.0.19 |
|
| 402 | + * @param string $key Meta Key. |
|
| 403 | + * @param bool $single return first found meta with key, or all with $key. |
|
| 404 | + * @param string $context What the value is for. Valid values are view and edit. |
|
| 405 | + * @return mixed |
|
| 406 | + */ |
|
| 407 | + public function get_meta( $key = '', $single = true, $context = 'view' ) { |
|
| 408 | + |
|
| 409 | + // Check if this is an internal meta key. |
|
| 410 | + $_key = str_replace( '_wpinv', '', $key ); |
|
| 411 | + $_key = str_replace( 'wpinv', '', $_key ); |
|
| 412 | + if ( $this->is_internal_meta_key( $key ) ) { |
|
| 413 | + $function = 'get_' . $_key; |
|
| 414 | + |
|
| 415 | + if ( is_callable( array( $this, $function ) ) ) { |
|
| 416 | + return $this->{$function}(); |
|
| 417 | + } |
|
| 418 | + } |
|
| 419 | + |
|
| 420 | + // Read the meta data if not yet read. |
|
| 421 | + $this->maybe_read_meta_data(); |
|
| 422 | + $meta_data = $this->get_meta_data(); |
|
| 423 | + $array_keys = array_keys( wp_list_pluck( $meta_data, 'key' ), $key, true ); |
|
| 424 | + $value = $single ? '' : array(); |
|
| 425 | + |
|
| 426 | + if ( ! empty( $array_keys ) ) { |
|
| 427 | + // We don't use the $this->meta_data property directly here because we don't want meta with a null value (i.e. meta which has been deleted via $this->delete_meta_data()). |
|
| 428 | + if ( $single ) { |
|
| 429 | + $value = $meta_data[ current( $array_keys ) ]->value; |
|
| 430 | + } else { |
|
| 431 | + $value = array_intersect_key( $meta_data, array_flip( $array_keys ) ); |
|
| 432 | + } |
|
| 433 | + } |
|
| 434 | + |
|
| 435 | + if ( 'view' === $context ) { |
|
| 436 | + $value = apply_filters( $this->get_hook_prefix() . $key, $value, $this ); |
|
| 437 | + } |
|
| 438 | + |
|
| 439 | + return $value; |
|
| 440 | + } |
|
| 441 | + |
|
| 442 | + /** |
|
| 443 | + * See if meta data exists, since get_meta always returns a '' or array(). |
|
| 444 | + * |
|
| 445 | + * @since 1.0.19 |
|
| 446 | + * @param string $key Meta Key. |
|
| 447 | + * @return boolean |
|
| 448 | + */ |
|
| 449 | + public function meta_exists( $key = '' ) { |
|
| 450 | + $this->maybe_read_meta_data(); |
|
| 451 | + $array_keys = wp_list_pluck( $this->get_meta_data(), 'key' ); |
|
| 452 | + return in_array( $key, $array_keys, true ); |
|
| 453 | + } |
|
| 454 | + |
|
| 455 | + /** |
|
| 456 | + * Set all meta data from array. |
|
| 457 | + * |
|
| 458 | + * @since 1.0.19 |
|
| 459 | + * @param array $data Key/Value pairs. |
|
| 460 | + */ |
|
| 461 | + public function set_meta_data( $data ) { |
|
| 462 | + if ( ! empty( $data ) && is_array( $data ) ) { |
|
| 463 | + $this->maybe_read_meta_data(); |
|
| 464 | + foreach ( $data as $meta ) { |
|
| 465 | + $meta = (array) $meta; |
|
| 466 | + if ( isset( $meta['key'], $meta['value'], $meta['id'] ) ) { |
|
| 467 | + $this->meta_data[] = new GetPaid_Meta_Data( |
|
| 468 | + array( |
|
| 469 | + 'id' => $meta['id'], |
|
| 470 | + 'key' => $meta['key'], |
|
| 471 | + 'value' => $meta['value'], |
|
| 472 | + ) |
|
| 473 | + ); |
|
| 474 | + } |
|
| 475 | + } |
|
| 476 | + } |
|
| 477 | + } |
|
| 478 | + |
|
| 479 | + /** |
|
| 480 | + * Add meta data. |
|
| 481 | + * |
|
| 482 | + * @since 1.0.19 |
|
| 483 | + * |
|
| 484 | + * @param string $key Meta key. |
|
| 485 | + * @param string|array $value Meta value. |
|
| 486 | + * @param bool $unique Should this be a unique key?. |
|
| 487 | + */ |
|
| 488 | + public function add_meta_data( $key, $value, $unique = false ) { |
|
| 489 | + if ( $this->is_internal_meta_key( $key ) ) { |
|
| 490 | + $function = 'set_' . $key; |
|
| 491 | + |
|
| 492 | + if ( is_callable( array( $this, $function ) ) ) { |
|
| 493 | + return $this->{$function}( $value ); |
|
| 494 | + } |
|
| 495 | + } |
|
| 496 | + |
|
| 497 | + $this->maybe_read_meta_data(); |
|
| 498 | + if ( $unique ) { |
|
| 499 | + $this->delete_meta_data( $key ); |
|
| 500 | + } |
|
| 501 | + $this->meta_data[] = new GetPaid_Meta_Data( |
|
| 502 | + array( |
|
| 503 | + 'key' => $key, |
|
| 504 | + 'value' => $value, |
|
| 505 | + ) |
|
| 506 | + ); |
|
| 507 | + |
|
| 508 | + $this->save(); |
|
| 509 | + } |
|
| 510 | + |
|
| 511 | + /** |
|
| 512 | + * Update meta data by key or ID, if provided. |
|
| 513 | + * |
|
| 514 | + * @since 1.0.19 |
|
| 515 | + * |
|
| 516 | + * @param string $key Meta key. |
|
| 517 | + * @param string|array $value Meta value. |
|
| 518 | + * @param int $meta_id Meta ID. |
|
| 519 | + */ |
|
| 520 | + public function update_meta_data( $key, $value, $meta_id = 0 ) { |
|
| 521 | + if ( $this->is_internal_meta_key( $key ) ) { |
|
| 522 | + $function = 'set_' . $key; |
|
| 523 | + |
|
| 524 | + if ( is_callable( array( $this, $function ) ) ) { |
|
| 525 | + return $this->{$function}( $value ); |
|
| 526 | + } |
|
| 527 | + } |
|
| 528 | + |
|
| 529 | + $this->maybe_read_meta_data(); |
|
| 530 | + |
|
| 531 | + $array_key = false; |
|
| 532 | + |
|
| 533 | + if ( $meta_id ) { |
|
| 534 | + $array_keys = array_keys( wp_list_pluck( $this->meta_data, 'id' ), $meta_id, true ); |
|
| 535 | + $array_key = $array_keys ? current( $array_keys ) : false; |
|
| 536 | + } else { |
|
| 537 | + // Find matches by key. |
|
| 538 | + $matches = array(); |
|
| 539 | + foreach ( $this->meta_data as $meta_data_array_key => $meta ) { |
|
| 540 | + if ( $meta->key === $key ) { |
|
| 541 | + $matches[] = $meta_data_array_key; |
|
| 542 | + } |
|
| 543 | + } |
|
| 544 | + |
|
| 545 | + if ( ! empty( $matches ) ) { |
|
| 546 | + // Set matches to null so only one key gets the new value. |
|
| 547 | + foreach ( $matches as $meta_data_array_key ) { |
|
| 548 | + $this->meta_data[ $meta_data_array_key ]->value = null; |
|
| 549 | + } |
|
| 550 | + $array_key = current( $matches ); |
|
| 551 | + } |
|
| 552 | + } |
|
| 553 | + |
|
| 554 | + if ( false !== $array_key ) { |
|
| 555 | + $meta = $this->meta_data[ $array_key ]; |
|
| 556 | + $meta->key = $key; |
|
| 557 | + $meta->value = $value; |
|
| 558 | + } else { |
|
| 559 | + $this->add_meta_data( $key, $value, true ); |
|
| 560 | + } |
|
| 561 | + } |
|
| 562 | + |
|
| 563 | + /** |
|
| 564 | + * Delete meta data. |
|
| 565 | + * |
|
| 566 | + * @since 1.0.19 |
|
| 567 | + * @param string $key Meta key. |
|
| 568 | + */ |
|
| 569 | + public function delete_meta_data( $key ) { |
|
| 570 | + $this->maybe_read_meta_data(); |
|
| 571 | + $array_keys = array_keys( wp_list_pluck( $this->meta_data, 'key' ), $key, true ); |
|
| 572 | + |
|
| 573 | + if ( $array_keys ) { |
|
| 574 | + foreach ( $array_keys as $array_key ) { |
|
| 575 | + $this->meta_data[ $array_key ]->value = null; |
|
| 576 | + } |
|
| 577 | + } |
|
| 578 | + } |
|
| 579 | + |
|
| 580 | + /** |
|
| 581 | + * Delete meta data. |
|
| 582 | + * |
|
| 583 | + * @since 1.0.19 |
|
| 584 | + * @param int $mid Meta ID. |
|
| 585 | + */ |
|
| 586 | + public function delete_meta_data_by_mid( $mid ) { |
|
| 587 | + $this->maybe_read_meta_data(); |
|
| 588 | + $array_keys = array_keys( wp_list_pluck( $this->meta_data, 'id' ), (int) $mid, true ); |
|
| 589 | + |
|
| 590 | + if ( $array_keys ) { |
|
| 591 | + foreach ( $array_keys as $array_key ) { |
|
| 592 | + $this->meta_data[ $array_key ]->value = null; |
|
| 593 | + } |
|
| 594 | + } |
|
| 595 | + } |
|
| 596 | + |
|
| 597 | + /** |
|
| 598 | + * Read meta data if null. |
|
| 599 | + * |
|
| 600 | + * @since 1.0.19 |
|
| 601 | + */ |
|
| 602 | + protected function maybe_read_meta_data() { |
|
| 603 | + if ( is_null( $this->meta_data ) ) { |
|
| 604 | + $this->read_meta_data(); |
|
| 605 | + } |
|
| 606 | + } |
|
| 607 | + |
|
| 608 | + /** |
|
| 609 | + * Read Meta Data from the database. Ignore any internal properties. |
|
| 610 | + * Uses it's own caches because get_metadata does not provide meta_ids. |
|
| 611 | + * |
|
| 612 | + * @since 1.0.19 |
|
| 613 | + * @param bool $force_read True to force a new DB read (and update cache). |
|
| 614 | + */ |
|
| 615 | + public function read_meta_data( $force_read = false ) { |
|
| 616 | + |
|
| 617 | + // Reset meta data. |
|
| 618 | + $this->meta_data = array(); |
|
| 619 | + |
|
| 620 | + // Maybe abort early. |
|
| 621 | + if ( ! $this->get_id() || ! $this->data_store ) { |
|
| 622 | + return; |
|
| 623 | + } |
|
| 624 | + |
|
| 625 | + // Only read from cache if the cache key is set. |
|
| 626 | + $cache_key = null; |
|
| 627 | + if ( ! $force_read && ! empty( $this->cache_group ) ) { |
|
| 628 | + $cache_key = GetPaid_Cache_Helper::get_cache_prefix( $this->cache_group ) . GetPaid_Cache_Helper::get_cache_prefix( 'object_' . $this->get_id() ) . 'object_meta_' . $this->get_id(); |
|
| 629 | + $raw_meta_data = wp_cache_get( $cache_key, $this->cache_group ); |
|
| 630 | + } |
|
| 631 | + |
|
| 632 | + // Should we force read? |
|
| 633 | + if ( empty( $raw_meta_data ) ) { |
|
| 634 | + $raw_meta_data = $this->data_store->read_meta( $this ); |
|
| 635 | + |
|
| 636 | + if ( ! empty( $cache_key ) ) { |
|
| 637 | + wp_cache_set( $cache_key, $raw_meta_data, $this->cache_group ); |
|
| 638 | + } |
|
| 639 | + |
|
| 640 | + } |
|
| 641 | + |
|
| 642 | + // Set meta data. |
|
| 643 | + if ( is_array( $raw_meta_data ) ) { |
|
| 644 | + |
|
| 645 | + foreach ( $raw_meta_data as $meta ) { |
|
| 646 | + $this->meta_data[] = new GetPaid_Meta_Data( |
|
| 647 | + array( |
|
| 648 | + 'id' => (int) $meta->meta_id, |
|
| 649 | + 'key' => $meta->meta_key, |
|
| 650 | + 'value' => maybe_unserialize( $meta->meta_value ), |
|
| 651 | + ) |
|
| 652 | + ); |
|
| 653 | + } |
|
| 654 | + |
|
| 655 | + } |
|
| 656 | + |
|
| 657 | + } |
|
| 658 | + |
|
| 659 | + /** |
|
| 660 | + * Update Meta Data in the database. |
|
| 661 | + * |
|
| 662 | + * @since 1.0.19 |
|
| 663 | + */ |
|
| 664 | + public function save_meta_data() { |
|
| 665 | + if ( ! $this->data_store || is_null( $this->meta_data ) ) { |
|
| 666 | + return; |
|
| 667 | + } |
|
| 668 | + foreach ( $this->meta_data as $array_key => $meta ) { |
|
| 669 | + if ( is_null( $meta->value ) ) { |
|
| 670 | + if ( ! empty( $meta->id ) ) { |
|
| 671 | + $this->data_store->delete_meta( $this, $meta ); |
|
| 672 | + unset( $this->meta_data[ $array_key ] ); |
|
| 673 | + } |
|
| 674 | + } elseif ( empty( $meta->id ) ) { |
|
| 675 | + $meta->id = $this->data_store->add_meta( $this, $meta ); |
|
| 676 | + $meta->apply_changes(); |
|
| 677 | + } else { |
|
| 678 | + if ( $meta->get_changes() ) { |
|
| 679 | + $this->data_store->update_meta( $this, $meta ); |
|
| 680 | + $meta->apply_changes(); |
|
| 681 | + } |
|
| 682 | + } |
|
| 683 | + } |
|
| 684 | + if ( ! empty( $this->cache_group ) ) { |
|
| 685 | + $cache_key = GetPaid_Cache_Helper::get_cache_prefix( $this->cache_group ) . GetPaid_Cache_Helper::get_cache_prefix( 'object_' . $this->get_id() ) . 'object_meta_' . $this->get_id(); |
|
| 686 | + wp_cache_delete( $cache_key, $this->cache_group ); |
|
| 687 | + } |
|
| 688 | + } |
|
| 689 | + |
|
| 690 | + /** |
|
| 691 | + * Set ID. |
|
| 692 | + * |
|
| 693 | + * @since 1.0.19 |
|
| 694 | + * @param int $id ID. |
|
| 695 | + */ |
|
| 696 | + public function set_id( $id ) { |
|
| 697 | + $this->id = absint( $id ); |
|
| 698 | + } |
|
| 699 | + |
|
| 700 | + /** |
|
| 701 | + * Sets item status. |
|
| 702 | + * |
|
| 703 | + * @since 1.0.19 |
|
| 704 | + * @param string $status New status. |
|
| 705 | + * @return array details of change. |
|
| 706 | + */ |
|
| 707 | + public function set_status( $status ) { |
|
| 708 | 708 | $old_status = $this->get_status(); |
| 709 | 709 | |
| 710 | - $this->set_prop( 'status', $status ); |
|
| 711 | - |
|
| 712 | - return array( |
|
| 713 | - 'from' => $old_status, |
|
| 714 | - 'to' => $status, |
|
| 715 | - ); |
|
| 716 | - } |
|
| 717 | - |
|
| 718 | - /** |
|
| 719 | - * Set all props to default values. |
|
| 720 | - * |
|
| 721 | - * @since 1.0.19 |
|
| 722 | - */ |
|
| 723 | - public function set_defaults() { |
|
| 724 | - $this->data = $this->default_data; |
|
| 725 | - $this->changes = array(); |
|
| 726 | - $this->set_object_read( false ); |
|
| 727 | - } |
|
| 728 | - |
|
| 729 | - /** |
|
| 730 | - * Set object read property. |
|
| 731 | - * |
|
| 732 | - * @since 1.0.19 |
|
| 733 | - * @param boolean $read Should read?. |
|
| 734 | - */ |
|
| 735 | - public function set_object_read( $read = true ) { |
|
| 736 | - $this->object_read = (bool) $read; |
|
| 737 | - } |
|
| 738 | - |
|
| 739 | - /** |
|
| 740 | - * Get object read property. |
|
| 741 | - * |
|
| 742 | - * @since 1.0.19 |
|
| 743 | - * @return boolean |
|
| 744 | - */ |
|
| 745 | - public function get_object_read() { |
|
| 746 | - return (bool) $this->object_read; |
|
| 747 | - } |
|
| 748 | - |
|
| 749 | - /** |
|
| 750 | - * Set a collection of props in one go, collect any errors, and return the result. |
|
| 751 | - * Only sets using public methods. |
|
| 752 | - * |
|
| 753 | - * @since 1.0.19 |
|
| 754 | - * |
|
| 755 | - * @param array $props Key value pairs to set. Key is the prop and should map to a setter function name. |
|
| 756 | - * @param string $context In what context to run this. |
|
| 757 | - * |
|
| 758 | - * @return bool|WP_Error |
|
| 759 | - */ |
|
| 760 | - public function set_props( $props, $context = 'set' ) { |
|
| 761 | - $errors = false; |
|
| 762 | - |
|
| 763 | - $props = wp_unslash( $props ); |
|
| 764 | - foreach ( $props as $prop => $value ) { |
|
| 765 | - try { |
|
| 766 | - /** |
|
| 767 | - * Checks if the prop being set is allowed, and the value is not null. |
|
| 768 | - */ |
|
| 769 | - if ( is_null( $value ) || in_array( $prop, array( 'prop', 'date_prop', 'meta_data' ), true ) ) { |
|
| 770 | - continue; |
|
| 771 | - } |
|
| 772 | - $setter = "set_$prop"; |
|
| 773 | - |
|
| 774 | - if ( is_callable( array( $this, $setter ) ) ) { |
|
| 775 | - $this->{$setter}( $value ); |
|
| 776 | - } |
|
| 777 | - } catch ( Exception $e ) { |
|
| 778 | - if ( ! $errors ) { |
|
| 779 | - $errors = new WP_Error(); |
|
| 780 | - } |
|
| 781 | - $errors->add( $e->getCode(), $e->getMessage() ); |
|
| 782 | - $this->last_error = $e->getMessage(); |
|
| 783 | - } |
|
| 784 | - } |
|
| 785 | - |
|
| 786 | - return $errors && count( $errors->get_error_codes() ) ? $errors : true; |
|
| 787 | - } |
|
| 788 | - |
|
| 789 | - /** |
|
| 790 | - * Sets a prop for a setter method. |
|
| 791 | - * |
|
| 792 | - * This stores changes in a special array so we can track what needs saving |
|
| 793 | - * the the DB later. |
|
| 794 | - * |
|
| 795 | - * @since 1.0.19 |
|
| 796 | - * @param string $prop Name of prop to set. |
|
| 797 | - * @param mixed $value Value of the prop. |
|
| 798 | - */ |
|
| 799 | - protected function set_prop( $prop, $value ) { |
|
| 800 | - if ( array_key_exists( $prop, $this->data ) ) { |
|
| 801 | - if ( true === $this->object_read ) { |
|
| 802 | - if ( $value !== $this->data[ $prop ] || array_key_exists( $prop, $this->changes ) ) { |
|
| 803 | - $this->changes[ $prop ] = $value; |
|
| 804 | - } |
|
| 805 | - } else { |
|
| 806 | - $this->data[ $prop ] = $value; |
|
| 807 | - } |
|
| 808 | - } |
|
| 809 | - } |
|
| 810 | - |
|
| 811 | - /** |
|
| 812 | - * Return data changes only. |
|
| 813 | - * |
|
| 814 | - * @since 1.0.19 |
|
| 815 | - * @return array |
|
| 816 | - */ |
|
| 817 | - public function get_changes() { |
|
| 818 | - return $this->changes; |
|
| 819 | - } |
|
| 820 | - |
|
| 821 | - /** |
|
| 822 | - * Merge changes with data and clear. |
|
| 823 | - * |
|
| 824 | - * @since 1.0.19 |
|
| 825 | - */ |
|
| 826 | - public function apply_changes() { |
|
| 827 | - $this->data = array_replace( $this->data, $this->changes ); |
|
| 828 | - $this->changes = array(); |
|
| 829 | - } |
|
| 830 | - |
|
| 831 | - /** |
|
| 832 | - * Prefix for action and filter hooks on data. |
|
| 833 | - * |
|
| 834 | - * @since 1.0.19 |
|
| 835 | - * @return string |
|
| 836 | - */ |
|
| 837 | - protected function get_hook_prefix() { |
|
| 838 | - return 'wpinv_get_' . $this->object_type . '_'; |
|
| 839 | - } |
|
| 840 | - |
|
| 841 | - /** |
|
| 842 | - * Gets a prop for a getter method. |
|
| 843 | - * |
|
| 844 | - * Gets the value from either current pending changes, or the data itself. |
|
| 845 | - * Context controls what happens to the value before it's returned. |
|
| 846 | - * |
|
| 847 | - * @since 1.0.19 |
|
| 848 | - * @param string $prop Name of prop to get. |
|
| 849 | - * @param string $context What the value is for. Valid values are view and edit. |
|
| 850 | - * @return mixed |
|
| 851 | - */ |
|
| 852 | - protected function get_prop( $prop, $context = 'view' ) { |
|
| 853 | - $value = null; |
|
| 854 | - |
|
| 855 | - if ( array_key_exists( $prop, $this->data ) ) { |
|
| 856 | - $value = array_key_exists( $prop, $this->changes ) ? $this->changes[ $prop ] : $this->data[ $prop ]; |
|
| 857 | - |
|
| 858 | - if ( 'view' === $context ) { |
|
| 859 | - $value = apply_filters( $this->get_hook_prefix() . $prop, $value, $this ); |
|
| 860 | - } |
|
| 861 | - } |
|
| 862 | - |
|
| 863 | - return $value; |
|
| 864 | - } |
|
| 865 | - |
|
| 866 | - /** |
|
| 867 | - * Sets a date prop whilst handling formatting and datetime objects. |
|
| 868 | - * |
|
| 869 | - * @since 1.0.19 |
|
| 870 | - * @param string $prop Name of prop to set. |
|
| 871 | - * @param string|integer $value Value of the prop. |
|
| 872 | - */ |
|
| 873 | - protected function set_date_prop( $prop, $value ) { |
|
| 874 | - |
|
| 875 | - if ( empty( $value ) ) { |
|
| 876 | - $this->set_prop( $prop, null ); |
|
| 877 | - return; |
|
| 878 | - } |
|
| 879 | - $this->set_prop( $prop, $value ); |
|
| 880 | - |
|
| 881 | - } |
|
| 882 | - |
|
| 883 | - /** |
|
| 884 | - * When invalid data is found, throw an exception unless reading from the DB. |
|
| 885 | - * |
|
| 886 | - * @since 1.0.19 |
|
| 887 | - * @param string $code Error code. |
|
| 888 | - * @param string $message Error message. |
|
| 889 | - */ |
|
| 890 | - protected function error( $code, $message ) { |
|
| 891 | - $this->last_error = $message; |
|
| 892 | - } |
|
| 893 | - |
|
| 894 | - /** |
|
| 895 | - * Checks if the object is saved in the database |
|
| 896 | - * |
|
| 897 | - * @since 1.0.19 |
|
| 898 | - * @return bool |
|
| 899 | - */ |
|
| 900 | - public function exists() { |
|
| 901 | - $id = $this->get_id(); |
|
| 902 | - return ! empty( $id ); |
|
| 903 | - } |
|
| 710 | + $this->set_prop( 'status', $status ); |
|
| 711 | + |
|
| 712 | + return array( |
|
| 713 | + 'from' => $old_status, |
|
| 714 | + 'to' => $status, |
|
| 715 | + ); |
|
| 716 | + } |
|
| 717 | + |
|
| 718 | + /** |
|
| 719 | + * Set all props to default values. |
|
| 720 | + * |
|
| 721 | + * @since 1.0.19 |
|
| 722 | + */ |
|
| 723 | + public function set_defaults() { |
|
| 724 | + $this->data = $this->default_data; |
|
| 725 | + $this->changes = array(); |
|
| 726 | + $this->set_object_read( false ); |
|
| 727 | + } |
|
| 728 | + |
|
| 729 | + /** |
|
| 730 | + * Set object read property. |
|
| 731 | + * |
|
| 732 | + * @since 1.0.19 |
|
| 733 | + * @param boolean $read Should read?. |
|
| 734 | + */ |
|
| 735 | + public function set_object_read( $read = true ) { |
|
| 736 | + $this->object_read = (bool) $read; |
|
| 737 | + } |
|
| 738 | + |
|
| 739 | + /** |
|
| 740 | + * Get object read property. |
|
| 741 | + * |
|
| 742 | + * @since 1.0.19 |
|
| 743 | + * @return boolean |
|
| 744 | + */ |
|
| 745 | + public function get_object_read() { |
|
| 746 | + return (bool) $this->object_read; |
|
| 747 | + } |
|
| 748 | + |
|
| 749 | + /** |
|
| 750 | + * Set a collection of props in one go, collect any errors, and return the result. |
|
| 751 | + * Only sets using public methods. |
|
| 752 | + * |
|
| 753 | + * @since 1.0.19 |
|
| 754 | + * |
|
| 755 | + * @param array $props Key value pairs to set. Key is the prop and should map to a setter function name. |
|
| 756 | + * @param string $context In what context to run this. |
|
| 757 | + * |
|
| 758 | + * @return bool|WP_Error |
|
| 759 | + */ |
|
| 760 | + public function set_props( $props, $context = 'set' ) { |
|
| 761 | + $errors = false; |
|
| 762 | + |
|
| 763 | + $props = wp_unslash( $props ); |
|
| 764 | + foreach ( $props as $prop => $value ) { |
|
| 765 | + try { |
|
| 766 | + /** |
|
| 767 | + * Checks if the prop being set is allowed, and the value is not null. |
|
| 768 | + */ |
|
| 769 | + if ( is_null( $value ) || in_array( $prop, array( 'prop', 'date_prop', 'meta_data' ), true ) ) { |
|
| 770 | + continue; |
|
| 771 | + } |
|
| 772 | + $setter = "set_$prop"; |
|
| 773 | + |
|
| 774 | + if ( is_callable( array( $this, $setter ) ) ) { |
|
| 775 | + $this->{$setter}( $value ); |
|
| 776 | + } |
|
| 777 | + } catch ( Exception $e ) { |
|
| 778 | + if ( ! $errors ) { |
|
| 779 | + $errors = new WP_Error(); |
|
| 780 | + } |
|
| 781 | + $errors->add( $e->getCode(), $e->getMessage() ); |
|
| 782 | + $this->last_error = $e->getMessage(); |
|
| 783 | + } |
|
| 784 | + } |
|
| 785 | + |
|
| 786 | + return $errors && count( $errors->get_error_codes() ) ? $errors : true; |
|
| 787 | + } |
|
| 788 | + |
|
| 789 | + /** |
|
| 790 | + * Sets a prop for a setter method. |
|
| 791 | + * |
|
| 792 | + * This stores changes in a special array so we can track what needs saving |
|
| 793 | + * the the DB later. |
|
| 794 | + * |
|
| 795 | + * @since 1.0.19 |
|
| 796 | + * @param string $prop Name of prop to set. |
|
| 797 | + * @param mixed $value Value of the prop. |
|
| 798 | + */ |
|
| 799 | + protected function set_prop( $prop, $value ) { |
|
| 800 | + if ( array_key_exists( $prop, $this->data ) ) { |
|
| 801 | + if ( true === $this->object_read ) { |
|
| 802 | + if ( $value !== $this->data[ $prop ] || array_key_exists( $prop, $this->changes ) ) { |
|
| 803 | + $this->changes[ $prop ] = $value; |
|
| 804 | + } |
|
| 805 | + } else { |
|
| 806 | + $this->data[ $prop ] = $value; |
|
| 807 | + } |
|
| 808 | + } |
|
| 809 | + } |
|
| 810 | + |
|
| 811 | + /** |
|
| 812 | + * Return data changes only. |
|
| 813 | + * |
|
| 814 | + * @since 1.0.19 |
|
| 815 | + * @return array |
|
| 816 | + */ |
|
| 817 | + public function get_changes() { |
|
| 818 | + return $this->changes; |
|
| 819 | + } |
|
| 820 | + |
|
| 821 | + /** |
|
| 822 | + * Merge changes with data and clear. |
|
| 823 | + * |
|
| 824 | + * @since 1.0.19 |
|
| 825 | + */ |
|
| 826 | + public function apply_changes() { |
|
| 827 | + $this->data = array_replace( $this->data, $this->changes ); |
|
| 828 | + $this->changes = array(); |
|
| 829 | + } |
|
| 830 | + |
|
| 831 | + /** |
|
| 832 | + * Prefix for action and filter hooks on data. |
|
| 833 | + * |
|
| 834 | + * @since 1.0.19 |
|
| 835 | + * @return string |
|
| 836 | + */ |
|
| 837 | + protected function get_hook_prefix() { |
|
| 838 | + return 'wpinv_get_' . $this->object_type . '_'; |
|
| 839 | + } |
|
| 840 | + |
|
| 841 | + /** |
|
| 842 | + * Gets a prop for a getter method. |
|
| 843 | + * |
|
| 844 | + * Gets the value from either current pending changes, or the data itself. |
|
| 845 | + * Context controls what happens to the value before it's returned. |
|
| 846 | + * |
|
| 847 | + * @since 1.0.19 |
|
| 848 | + * @param string $prop Name of prop to get. |
|
| 849 | + * @param string $context What the value is for. Valid values are view and edit. |
|
| 850 | + * @return mixed |
|
| 851 | + */ |
|
| 852 | + protected function get_prop( $prop, $context = 'view' ) { |
|
| 853 | + $value = null; |
|
| 854 | + |
|
| 855 | + if ( array_key_exists( $prop, $this->data ) ) { |
|
| 856 | + $value = array_key_exists( $prop, $this->changes ) ? $this->changes[ $prop ] : $this->data[ $prop ]; |
|
| 857 | + |
|
| 858 | + if ( 'view' === $context ) { |
|
| 859 | + $value = apply_filters( $this->get_hook_prefix() . $prop, $value, $this ); |
|
| 860 | + } |
|
| 861 | + } |
|
| 862 | + |
|
| 863 | + return $value; |
|
| 864 | + } |
|
| 865 | + |
|
| 866 | + /** |
|
| 867 | + * Sets a date prop whilst handling formatting and datetime objects. |
|
| 868 | + * |
|
| 869 | + * @since 1.0.19 |
|
| 870 | + * @param string $prop Name of prop to set. |
|
| 871 | + * @param string|integer $value Value of the prop. |
|
| 872 | + */ |
|
| 873 | + protected function set_date_prop( $prop, $value ) { |
|
| 874 | + |
|
| 875 | + if ( empty( $value ) ) { |
|
| 876 | + $this->set_prop( $prop, null ); |
|
| 877 | + return; |
|
| 878 | + } |
|
| 879 | + $this->set_prop( $prop, $value ); |
|
| 880 | + |
|
| 881 | + } |
|
| 882 | + |
|
| 883 | + /** |
|
| 884 | + * When invalid data is found, throw an exception unless reading from the DB. |
|
| 885 | + * |
|
| 886 | + * @since 1.0.19 |
|
| 887 | + * @param string $code Error code. |
|
| 888 | + * @param string $message Error message. |
|
| 889 | + */ |
|
| 890 | + protected function error( $code, $message ) { |
|
| 891 | + $this->last_error = $message; |
|
| 892 | + } |
|
| 893 | + |
|
| 894 | + /** |
|
| 895 | + * Checks if the object is saved in the database |
|
| 896 | + * |
|
| 897 | + * @since 1.0.19 |
|
| 898 | + * @return bool |
|
| 899 | + */ |
|
| 900 | + public function exists() { |
|
| 901 | + $id = $this->get_id(); |
|
| 902 | + return ! empty( $id ); |
|
| 903 | + } |
|
| 904 | 904 | |
| 905 | 905 | } |
@@ -13,300 +13,300 @@ |
||
| 13 | 13 | class GetPaid_Subscription_Notification_Emails { |
| 14 | 14 | |
| 15 | 15 | /** |
| 16 | - * The array of subscription email actions. |
|
| 17 | - * |
|
| 18 | - * @param array |
|
| 19 | - */ |
|
| 20 | - public $subscription_actions; |
|
| 16 | + * The array of subscription email actions. |
|
| 17 | + * |
|
| 18 | + * @param array |
|
| 19 | + */ |
|
| 20 | + public $subscription_actions; |
|
| 21 | 21 | |
| 22 | 22 | /** |
| 23 | - * Class constructor |
|
| 23 | + * Class constructor |
|
| 24 | 24 | * |
| 25 | - */ |
|
| 26 | - public function __construct() { |
|
| 27 | - |
|
| 28 | - $this->subscription_actions = apply_filters( |
|
| 29 | - 'getpaid_notification_email_subscription_triggers', |
|
| 30 | - array( |
|
| 31 | - 'getpaid_subscription_trialling' => 'subscription_trial', |
|
| 32 | - 'getpaid_subscription_cancelled' => 'subscription_cancelled', |
|
| 33 | - 'getpaid_subscription_expired' => 'subscription_expired', |
|
| 34 | - 'getpaid_subscription_completed' => 'subscription_complete', |
|
| 35 | - 'getpaid_daily_maintenance' => 'renewal_reminder', |
|
| 36 | - ) |
|
| 37 | - ); |
|
| 38 | - |
|
| 39 | - $this->init_hooks(); |
|
| 25 | + */ |
|
| 26 | + public function __construct() { |
|
| 27 | + |
|
| 28 | + $this->subscription_actions = apply_filters( |
|
| 29 | + 'getpaid_notification_email_subscription_triggers', |
|
| 30 | + array( |
|
| 31 | + 'getpaid_subscription_trialling' => 'subscription_trial', |
|
| 32 | + 'getpaid_subscription_cancelled' => 'subscription_cancelled', |
|
| 33 | + 'getpaid_subscription_expired' => 'subscription_expired', |
|
| 34 | + 'getpaid_subscription_completed' => 'subscription_complete', |
|
| 35 | + 'getpaid_daily_maintenance' => 'renewal_reminder', |
|
| 36 | + ) |
|
| 37 | + ); |
|
| 38 | + |
|
| 39 | + $this->init_hooks(); |
|
| 40 | 40 | |
| 41 | 41 | } |
| 42 | 42 | |
| 43 | 43 | /** |
| 44 | - * Registers email hooks. |
|
| 45 | - */ |
|
| 46 | - public function init_hooks() { |
|
| 47 | - |
|
| 48 | - add_filter( 'getpaid_get_email_merge_tags', array( $this, 'subscription_merge_tags' ), 10, 2 ); |
|
| 49 | - foreach ( $this->subscription_actions as $hook => $email_type ) { |
|
| 50 | - |
|
| 51 | - $email = new GetPaid_Notification_Email( $email_type ); |
|
| 52 | - |
|
| 53 | - if ( ! $email->is_active() ) { |
|
| 54 | - continue; |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - if ( method_exists( $this, $email_type ) ) { |
|
| 58 | - add_action( $hook, array( $this, $email_type ), 100, 2 ); |
|
| 59 | - continue; |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - do_action( 'getpaid_subscription_notification_email_register_hook', $email_type, $hook ); |
|
| 63 | - |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * Filters subscription merge tags. |
|
| 70 | - * |
|
| 71 | - * @param array $merge_tags |
|
| 72 | - * @param mixed|WPInv_Invoice|WPInv_Subscription $object |
|
| 73 | - */ |
|
| 74 | - public function subscription_merge_tags( $merge_tags, $object ) { |
|
| 75 | - |
|
| 76 | - if ( is_a( $object, 'WPInv_Subscription' ) ) { |
|
| 77 | - $merge_tags = array_merge( |
|
| 78 | - $merge_tags, |
|
| 79 | - $this->get_subscription_merge_tags( $object ) |
|
| 80 | - ); |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - return $merge_tags; |
|
| 84 | - |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * Generates subscription merge tags. |
|
| 89 | - * |
|
| 90 | - * @param WPInv_Subscription $subscription |
|
| 91 | - * @return array |
|
| 92 | - */ |
|
| 93 | - public function get_subscription_merge_tags( $subscription ) { |
|
| 94 | - |
|
| 95 | - // Abort if it does not exist. |
|
| 96 | - if ( ! $subscription->get_id() ) { |
|
| 97 | - return array(); |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - $invoice = $subscription->get_parent_invoice(); |
|
| 101 | - return array( |
|
| 102 | - '{subscription_renewal_date}' => getpaid_format_date_value( $subscription->get_next_renewal_date(), __( 'Never', 'invoicing' ) ), |
|
| 103 | - '{subscription_created}' => getpaid_format_date_value( $subscription->get_date_created() ), |
|
| 104 | - '{subscription_status}' => sanitize_text_field( $subscription->get_status_label() ), |
|
| 105 | - '{subscription_profile_id}' => sanitize_text_field( $subscription->get_profile_id() ), |
|
| 106 | - '{subscription_id}' => absint( $subscription->get_id() ), |
|
| 107 | - '{subscription_recurring_amount}' => sanitize_text_field( wpinv_price( $subscription->get_recurring_amount(), $invoice->get_currency() ) ), |
|
| 108 | - '{subscription_initial_amount}' => sanitize_text_field( wpinv_price( $subscription->get_initial_amount(), $invoice->get_currency() ) ), |
|
| 109 | - '{subscription_recurring_period}' => getpaid_get_subscription_period_label( $subscription->get_period(), $subscription->get_frequency(), '' ), |
|
| 110 | - '{subscription_bill_times}' => $subscription->get_bill_times(), |
|
| 111 | - '{subscription_url}' => esc_url( $subscription->get_view_url() ), |
|
| 112 | - ); |
|
| 113 | - |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * Checks if we should send a notification for a subscription. |
|
| 118 | - * |
|
| 119 | - * @param WPInv_Invoice $invoice |
|
| 120 | - * @return bool |
|
| 121 | - */ |
|
| 122 | - public function should_send_notification( $invoice ) { |
|
| 123 | - return 0 != $invoice->get_id(); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * Returns notification recipients. |
|
| 128 | - * |
|
| 129 | - * @param WPInv_Invoice $invoice |
|
| 130 | - * @return array |
|
| 131 | - */ |
|
| 132 | - public function get_recipients( $invoice ) { |
|
| 133 | - $recipients = array( $invoice->get_email() ); |
|
| 134 | - |
|
| 135 | - $cc = $invoice->get_email_cc(); |
|
| 136 | - |
|
| 137 | - if ( ! empty( $cc ) ) { |
|
| 138 | - $cc = array_map( 'sanitize_email', wpinv_parse_list( $cc ) ); |
|
| 139 | - $recipients = array_filter( array_unique( array_merge( $recipients, $cc ) ) ); |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - return $recipients; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * Helper function to send an email. |
|
| 147 | - * |
|
| 148 | - * @param WPInv_Subscription $subscription |
|
| 149 | - * @param GetPaid_Notification_Email $email |
|
| 150 | - * @param string $type |
|
| 151 | - * @param array $extra_args Extra template args. |
|
| 152 | - */ |
|
| 153 | - public function send_email( $subscription, $email, $type, $extra_args = array() ) { |
|
| 154 | - |
|
| 155 | - // Abort in case the parent invoice does not exist. |
|
| 156 | - $invoice = $subscription->get_parent_invoice(); |
|
| 157 | - if ( ! $this->should_send_notification( $invoice ) ) { |
|
| 158 | - return; |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - if ( apply_filters( 'getpaid_skip_subscription_email', false, $type, $subscription ) ) { |
|
| 162 | - return; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - do_action( 'getpaid_before_send_subscription_notification', $type, $subscription, $email ); |
|
| 166 | - |
|
| 167 | - $recipients = $this->get_recipients( $invoice ); |
|
| 168 | - $mailer = new GetPaid_Notification_Email_Sender(); |
|
| 169 | - $merge_tags = $email->get_merge_tags(); |
|
| 170 | - $content = $email->get_content( $merge_tags, $extra_args ); |
|
| 171 | - $subject = $email->add_merge_tags( $email->get_subject(), $merge_tags ); |
|
| 172 | - $attachments = $email->get_attachments(); |
|
| 173 | - |
|
| 174 | - $result = $mailer->send( |
|
| 175 | - apply_filters( 'getpaid_subscription_email_recipients', wpinv_parse_list( $recipients ), $email ), |
|
| 176 | - $subject, |
|
| 177 | - $content, |
|
| 178 | - $attachments |
|
| 179 | - ); |
|
| 180 | - |
|
| 181 | - // Maybe send a copy to the admin. |
|
| 182 | - if ( $email->include_admin_bcc() ) { |
|
| 183 | - $mailer->send( |
|
| 184 | - wpinv_get_admin_email(), |
|
| 185 | - $subject . __( ' - ADMIN BCC COPY', 'invoicing' ), |
|
| 186 | - $content, |
|
| 187 | - $attachments |
|
| 188 | - ); |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - if ( $result ) { |
|
| 192 | - $invoice->add_system_note( |
|
| 193 | - sprintf( |
|
| 194 | - __( 'Successfully sent %s notification email to %s.', 'invoicing' ), |
|
| 195 | - sanitize_key( $type ), |
|
| 196 | - $email->is_admin_email() ? __( 'admin' ) : __( 'the customer' ) |
|
| 197 | - ) |
|
| 198 | - ); |
|
| 199 | - } else { |
|
| 200 | - $invoice->add_system_note( |
|
| 201 | - sprintf( |
|
| 202 | - __( 'Failed sending %s notification email to %s.', 'invoicing' ), |
|
| 203 | - sanitize_key( $type ), |
|
| 204 | - $email->is_admin_email() ? __( 'admin' ) : __( 'the customer' ) |
|
| 205 | - ) |
|
| 206 | - ); |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - do_action( 'getpaid_after_send_subscription_notification', $type, $subscription, $email ); |
|
| 210 | - |
|
| 211 | - } |
|
| 44 | + * Registers email hooks. |
|
| 45 | + */ |
|
| 46 | + public function init_hooks() { |
|
| 47 | + |
|
| 48 | + add_filter( 'getpaid_get_email_merge_tags', array( $this, 'subscription_merge_tags' ), 10, 2 ); |
|
| 49 | + foreach ( $this->subscription_actions as $hook => $email_type ) { |
|
| 50 | + |
|
| 51 | + $email = new GetPaid_Notification_Email( $email_type ); |
|
| 52 | + |
|
| 53 | + if ( ! $email->is_active() ) { |
|
| 54 | + continue; |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + if ( method_exists( $this, $email_type ) ) { |
|
| 58 | + add_action( $hook, array( $this, $email_type ), 100, 2 ); |
|
| 59 | + continue; |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + do_action( 'getpaid_subscription_notification_email_register_hook', $email_type, $hook ); |
|
| 63 | + |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + } |
|
| 212 | 67 | |
| 213 | 68 | /** |
| 214 | - * Sends a new trial notification. |
|
| 215 | - * |
|
| 216 | - * @param WPInv_Subscription $subscription |
|
| 217 | - */ |
|
| 218 | - public function subscription_trial( $subscription ) { |
|
| 69 | + * Filters subscription merge tags. |
|
| 70 | + * |
|
| 71 | + * @param array $merge_tags |
|
| 72 | + * @param mixed|WPInv_Invoice|WPInv_Subscription $object |
|
| 73 | + */ |
|
| 74 | + public function subscription_merge_tags( $merge_tags, $object ) { |
|
| 219 | 75 | |
| 220 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 221 | - $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 76 | + if ( is_a( $object, 'WPInv_Subscription' ) ) { |
|
| 77 | + $merge_tags = array_merge( |
|
| 78 | + $merge_tags, |
|
| 79 | + $this->get_subscription_merge_tags( $object ) |
|
| 80 | + ); |
|
| 81 | + } |
|
| 222 | 82 | |
| 223 | - } |
|
| 83 | + return $merge_tags; |
|
| 224 | 84 | |
| 225 | - /** |
|
| 226 | - * Sends a cancelled subscription notification. |
|
| 227 | - * |
|
| 228 | - * @param WPInv_Subscription $subscription |
|
| 229 | - */ |
|
| 230 | - public function subscription_cancelled( $subscription ) { |
|
| 85 | + } |
|
| 231 | 86 | |
| 232 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 233 | - $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 87 | + /** |
|
| 88 | + * Generates subscription merge tags. |
|
| 89 | + * |
|
| 90 | + * @param WPInv_Subscription $subscription |
|
| 91 | + * @return array |
|
| 92 | + */ |
|
| 93 | + public function get_subscription_merge_tags( $subscription ) { |
|
| 94 | + |
|
| 95 | + // Abort if it does not exist. |
|
| 96 | + if ( ! $subscription->get_id() ) { |
|
| 97 | + return array(); |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + $invoice = $subscription->get_parent_invoice(); |
|
| 101 | + return array( |
|
| 102 | + '{subscription_renewal_date}' => getpaid_format_date_value( $subscription->get_next_renewal_date(), __( 'Never', 'invoicing' ) ), |
|
| 103 | + '{subscription_created}' => getpaid_format_date_value( $subscription->get_date_created() ), |
|
| 104 | + '{subscription_status}' => sanitize_text_field( $subscription->get_status_label() ), |
|
| 105 | + '{subscription_profile_id}' => sanitize_text_field( $subscription->get_profile_id() ), |
|
| 106 | + '{subscription_id}' => absint( $subscription->get_id() ), |
|
| 107 | + '{subscription_recurring_amount}' => sanitize_text_field( wpinv_price( $subscription->get_recurring_amount(), $invoice->get_currency() ) ), |
|
| 108 | + '{subscription_initial_amount}' => sanitize_text_field( wpinv_price( $subscription->get_initial_amount(), $invoice->get_currency() ) ), |
|
| 109 | + '{subscription_recurring_period}' => getpaid_get_subscription_period_label( $subscription->get_period(), $subscription->get_frequency(), '' ), |
|
| 110 | + '{subscription_bill_times}' => $subscription->get_bill_times(), |
|
| 111 | + '{subscription_url}' => esc_url( $subscription->get_view_url() ), |
|
| 112 | + ); |
|
| 234 | 113 | |
| 235 | - } |
|
| 114 | + } |
|
| 236 | 115 | |
| 237 | - /** |
|
| 238 | - * Sends a subscription expired notification. |
|
| 239 | - * |
|
| 240 | - * @param WPInv_Subscription $subscription |
|
| 241 | - */ |
|
| 242 | - public function subscription_expired( $subscription ) { |
|
| 116 | + /** |
|
| 117 | + * Checks if we should send a notification for a subscription. |
|
| 118 | + * |
|
| 119 | + * @param WPInv_Invoice $invoice |
|
| 120 | + * @return bool |
|
| 121 | + */ |
|
| 122 | + public function should_send_notification( $invoice ) { |
|
| 123 | + return 0 != $invoice->get_id(); |
|
| 124 | + } |
|
| 243 | 125 | |
| 244 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 245 | - $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 126 | + /** |
|
| 127 | + * Returns notification recipients. |
|
| 128 | + * |
|
| 129 | + * @param WPInv_Invoice $invoice |
|
| 130 | + * @return array |
|
| 131 | + */ |
|
| 132 | + public function get_recipients( $invoice ) { |
|
| 133 | + $recipients = array( $invoice->get_email() ); |
|
| 246 | 134 | |
| 247 | - } |
|
| 135 | + $cc = $invoice->get_email_cc(); |
|
| 248 | 136 | |
| 249 | - /** |
|
| 250 | - * Sends a completed subscription notification. |
|
| 251 | - * |
|
| 252 | - * @param WPInv_Subscription $subscription |
|
| 253 | - */ |
|
| 254 | - public function subscription_complete( $subscription ) { |
|
| 137 | + if ( ! empty( $cc ) ) { |
|
| 138 | + $cc = array_map( 'sanitize_email', wpinv_parse_list( $cc ) ); |
|
| 139 | + $recipients = array_filter( array_unique( array_merge( $recipients, $cc ) ) ); |
|
| 140 | + } |
|
| 255 | 141 | |
| 256 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 257 | - $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 142 | + return $recipients; |
|
| 143 | + } |
|
| 258 | 144 | |
| 259 | - } |
|
| 145 | + /** |
|
| 146 | + * Helper function to send an email. |
|
| 147 | + * |
|
| 148 | + * @param WPInv_Subscription $subscription |
|
| 149 | + * @param GetPaid_Notification_Email $email |
|
| 150 | + * @param string $type |
|
| 151 | + * @param array $extra_args Extra template args. |
|
| 152 | + */ |
|
| 153 | + public function send_email( $subscription, $email, $type, $extra_args = array() ) { |
|
| 154 | + |
|
| 155 | + // Abort in case the parent invoice does not exist. |
|
| 156 | + $invoice = $subscription->get_parent_invoice(); |
|
| 157 | + if ( ! $this->should_send_notification( $invoice ) ) { |
|
| 158 | + return; |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + if ( apply_filters( 'getpaid_skip_subscription_email', false, $type, $subscription ) ) { |
|
| 162 | + return; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + do_action( 'getpaid_before_send_subscription_notification', $type, $subscription, $email ); |
|
| 166 | + |
|
| 167 | + $recipients = $this->get_recipients( $invoice ); |
|
| 168 | + $mailer = new GetPaid_Notification_Email_Sender(); |
|
| 169 | + $merge_tags = $email->get_merge_tags(); |
|
| 170 | + $content = $email->get_content( $merge_tags, $extra_args ); |
|
| 171 | + $subject = $email->add_merge_tags( $email->get_subject(), $merge_tags ); |
|
| 172 | + $attachments = $email->get_attachments(); |
|
| 173 | + |
|
| 174 | + $result = $mailer->send( |
|
| 175 | + apply_filters( 'getpaid_subscription_email_recipients', wpinv_parse_list( $recipients ), $email ), |
|
| 176 | + $subject, |
|
| 177 | + $content, |
|
| 178 | + $attachments |
|
| 179 | + ); |
|
| 180 | + |
|
| 181 | + // Maybe send a copy to the admin. |
|
| 182 | + if ( $email->include_admin_bcc() ) { |
|
| 183 | + $mailer->send( |
|
| 184 | + wpinv_get_admin_email(), |
|
| 185 | + $subject . __( ' - ADMIN BCC COPY', 'invoicing' ), |
|
| 186 | + $content, |
|
| 187 | + $attachments |
|
| 188 | + ); |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + if ( $result ) { |
|
| 192 | + $invoice->add_system_note( |
|
| 193 | + sprintf( |
|
| 194 | + __( 'Successfully sent %s notification email to %s.', 'invoicing' ), |
|
| 195 | + sanitize_key( $type ), |
|
| 196 | + $email->is_admin_email() ? __( 'admin' ) : __( 'the customer' ) |
|
| 197 | + ) |
|
| 198 | + ); |
|
| 199 | + } else { |
|
| 200 | + $invoice->add_system_note( |
|
| 201 | + sprintf( |
|
| 202 | + __( 'Failed sending %s notification email to %s.', 'invoicing' ), |
|
| 203 | + sanitize_key( $type ), |
|
| 204 | + $email->is_admin_email() ? __( 'admin' ) : __( 'the customer' ) |
|
| 205 | + ) |
|
| 206 | + ); |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + do_action( 'getpaid_after_send_subscription_notification', $type, $subscription, $email ); |
|
| 260 | 210 | |
| 261 | - /** |
|
| 262 | - * Sends a subscription renewal reminder notification. |
|
| 263 | - * |
|
| 264 | - */ |
|
| 265 | - public function renewal_reminder() { |
|
| 211 | + } |
|
| 266 | 212 | |
| 267 | - $email = new GetPaid_Notification_Email( __FUNCTION__ ); |
|
| 213 | + /** |
|
| 214 | + * Sends a new trial notification. |
|
| 215 | + * |
|
| 216 | + * @param WPInv_Subscription $subscription |
|
| 217 | + */ |
|
| 218 | + public function subscription_trial( $subscription ) { |
|
| 268 | 219 | |
| 269 | - // Fetch reminder days. |
|
| 270 | - $reminder_days = array_unique( wp_parse_id_list( $email->get_option( 'days' ) ) ); |
|
| 220 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 221 | + $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 271 | 222 | |
| 272 | - // Abort if non is set. |
|
| 273 | - if ( empty( $reminder_days ) ) { |
|
| 274 | - return; |
|
| 275 | - } |
|
| 223 | + } |
|
| 276 | 224 | |
| 277 | - // Fetch matching subscriptions. |
|
| 225 | + /** |
|
| 226 | + * Sends a cancelled subscription notification. |
|
| 227 | + * |
|
| 228 | + * @param WPInv_Subscription $subscription |
|
| 229 | + */ |
|
| 230 | + public function subscription_cancelled( $subscription ) { |
|
| 231 | + |
|
| 232 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 233 | + $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 234 | + |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + /** |
|
| 238 | + * Sends a subscription expired notification. |
|
| 239 | + * |
|
| 240 | + * @param WPInv_Subscription $subscription |
|
| 241 | + */ |
|
| 242 | + public function subscription_expired( $subscription ) { |
|
| 243 | + |
|
| 244 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 245 | + $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 246 | + |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + /** |
|
| 250 | + * Sends a completed subscription notification. |
|
| 251 | + * |
|
| 252 | + * @param WPInv_Subscription $subscription |
|
| 253 | + */ |
|
| 254 | + public function subscription_complete( $subscription ) { |
|
| 255 | + |
|
| 256 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 257 | + $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 258 | + |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + /** |
|
| 262 | + * Sends a subscription renewal reminder notification. |
|
| 263 | + * |
|
| 264 | + */ |
|
| 265 | + public function renewal_reminder() { |
|
| 266 | + |
|
| 267 | + $email = new GetPaid_Notification_Email( __FUNCTION__ ); |
|
| 268 | + |
|
| 269 | + // Fetch reminder days. |
|
| 270 | + $reminder_days = array_unique( wp_parse_id_list( $email->get_option( 'days' ) ) ); |
|
| 271 | + |
|
| 272 | + // Abort if non is set. |
|
| 273 | + if ( empty( $reminder_days ) ) { |
|
| 274 | + return; |
|
| 275 | + } |
|
| 276 | + |
|
| 277 | + // Fetch matching subscriptions. |
|
| 278 | 278 | $args = array( |
| 279 | 279 | 'number' => -1, |
| 280 | - 'count_total' => false, |
|
| 281 | - 'status' => 'trialling active', |
|
| 280 | + 'count_total' => false, |
|
| 281 | + 'status' => 'trialling active', |
|
| 282 | 282 | 'date_expires_query' => array( |
| 283 | - 'relation' => 'OR' |
|
| 283 | + 'relation' => 'OR' |
|
| 284 | 284 | ), |
| 285 | - ); |
|
| 285 | + ); |
|
| 286 | 286 | |
| 287 | - foreach ( $reminder_days as $days ) { |
|
| 288 | - $date = date_parse( date( 'Y-m-d', strtotime( "+$days days", current_time( 'timestamp' ) ) ) ); |
|
| 287 | + foreach ( $reminder_days as $days ) { |
|
| 288 | + $date = date_parse( date( 'Y-m-d', strtotime( "+$days days", current_time( 'timestamp' ) ) ) ); |
|
| 289 | 289 | |
| 290 | - $args['date_expires_query'][] = array( |
|
| 291 | - 'year' => $date['year'], |
|
| 292 | - 'month' => $date['month'], |
|
| 293 | - 'day' => $date['day'], |
|
| 294 | - ); |
|
| 290 | + $args['date_expires_query'][] = array( |
|
| 291 | + 'year' => $date['year'], |
|
| 292 | + 'month' => $date['month'], |
|
| 293 | + 'day' => $date['day'], |
|
| 294 | + ); |
|
| 295 | 295 | |
| 296 | - } |
|
| 296 | + } |
|
| 297 | 297 | |
| 298 | - $subscriptions = new GetPaid_Subscriptions_Query( $args ); |
|
| 298 | + $subscriptions = new GetPaid_Subscriptions_Query( $args ); |
|
| 299 | 299 | |
| 300 | 300 | foreach ( $subscriptions as $subscription ) { |
| 301 | 301 | |
| 302 | - // Skip packages. |
|
| 303 | - if ( get_post_meta( $subscription->get_product_id(), '_wpinv_type', true ) != 'package' ) { |
|
| 304 | - $email->object = $subscription; |
|
| 305 | - $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 306 | - } |
|
| 302 | + // Skip packages. |
|
| 303 | + if ( get_post_meta( $subscription->get_product_id(), '_wpinv_type', true ) != 'package' ) { |
|
| 304 | + $email->object = $subscription; |
|
| 305 | + $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 306 | + } |
|
| 307 | 307 | |
| 308 | - } |
|
| 308 | + } |
|
| 309 | 309 | |
| 310 | - } |
|
| 310 | + } |
|
| 311 | 311 | |
| 312 | 312 | } |
@@ -4,7 +4,7 @@ discard block |
||
| 4 | 4 | * |
| 5 | 5 | */ |
| 6 | 6 | if ( ! defined( 'ABSPATH' ) ) { |
| 7 | - exit; |
|
| 7 | + exit; |
|
| 8 | 8 | } |
| 9 | 9 | add_ThickBox(); |
| 10 | 10 | ?> |
@@ -14,18 +14,18 @@ discard block |
||
| 14 | 14 | <?php if ( $tabs ){ ?> |
| 15 | 15 | <nav class="nav-tab-wrapper wpi-nav-tab-wrapper"> |
| 16 | 16 | <?php |
| 17 | - foreach ( $tabs as $name => $label ) { |
|
| 18 | - echo '<a href="' . admin_url( 'admin.php?page=wpi-addons&tab=' . $name ) . '" class="nav-tab ' . ( $current_tab == $name ? 'nav-tab-active' : '' ) . '">' . $label . '</a>'; |
|
| 19 | - } |
|
| 20 | - do_action( 'wpi_addons_tabs' ); |
|
| 21 | - ?> |
|
| 17 | + foreach ( $tabs as $name => $label ) { |
|
| 18 | + echo '<a href="' . admin_url( 'admin.php?page=wpi-addons&tab=' . $name ) . '" class="nav-tab ' . ( $current_tab == $name ? 'nav-tab-active' : '' ) . '">' . $label . '</a>'; |
|
| 19 | + } |
|
| 20 | + do_action( 'wpi_addons_tabs' ); |
|
| 21 | + ?> |
|
| 22 | 22 | </nav> |
| 23 | 23 | |
| 24 | 24 | <?php |
| 25 | 25 | |
| 26 | - if($current_tab == 'membership'){ |
|
| 26 | + if($current_tab == 'membership'){ |
|
| 27 | 27 | |
| 28 | - ?> |
|
| 28 | + ?> |
|
| 29 | 29 | |
| 30 | 30 | <div class="wpi-membership-tab-conatiner"> |
| 31 | 31 | <div class="membership-content"> |
@@ -36,9 +36,9 @@ discard block |
||
| 36 | 36 | <h2><?php _e("Have a membership key?","invoicing");?></h2> |
| 37 | 37 | <p> |
| 38 | 38 | <?php |
| 39 | - $wpeu_admin = new External_Updates_Admin('wpinvoicing.com','1'); |
|
| 40 | - echo $wpeu_admin->render_licence_actions('wpinvoicing.com', 'membership',array(95, 106, 108,12351)); |
|
| 41 | - ?> |
|
| 39 | + $wpeu_admin = new External_Updates_Admin('wpinvoicing.com','1'); |
|
| 40 | + echo $wpeu_admin->render_licence_actions('wpinvoicing.com', 'membership',array(95, 106, 108,12351)); |
|
| 41 | + ?> |
|
| 42 | 42 | </p> |
| 43 | 43 | <?php }?> |
| 44 | 44 | |
@@ -48,13 +48,13 @@ discard block |
||
| 48 | 48 | <div class="feature-list"> |
| 49 | 49 | <ul> |
| 50 | 50 | <?php |
| 51 | - $addon_obj = new WPInv_Admin_Addons(); |
|
| 52 | - if ($addons = $addon_obj->get_section_data( 'addons' ) ) { |
|
| 53 | - foreach ( $addons as $addon ) { |
|
| 54 | - echo '<li><i class="far fa-check-circle fa-sm"></i> '.esc_html( $addon->info->title ).'</li>'; |
|
| 55 | - } |
|
| 56 | - } |
|
| 57 | - ?> |
|
| 51 | + $addon_obj = new WPInv_Admin_Addons(); |
|
| 52 | + if ($addons = $addon_obj->get_section_data( 'addons' ) ) { |
|
| 53 | + foreach ( $addons as $addon ) { |
|
| 54 | + echo '<li><i class="far fa-check-circle fa-sm"></i> '.esc_html( $addon->info->title ).'</li>'; |
|
| 55 | + } |
|
| 56 | + } |
|
| 57 | + ?> |
|
| 58 | 58 | </ul> |
| 59 | 59 | |
| 60 | 60 | <div class="feature-cta"> |
@@ -65,12 +65,12 @@ discard block |
||
| 65 | 65 | <h3><?php _e("Included Gateways:","invoicing");?></h3> |
| 66 | 66 | <ul> |
| 67 | 67 | <?php |
| 68 | - if ($addons = $addon_obj->get_section_data( 'gateways' ) ) { |
|
| 69 | - foreach ( $addons as $addon ) { |
|
| 70 | - echo '<li><i class="far fa-check-circle fa-sm"></i> '.esc_html( $addon->info->title ).'</li>'; |
|
| 71 | - } |
|
| 72 | - } |
|
| 73 | - ?> |
|
| 68 | + if ($addons = $addon_obj->get_section_data( 'gateways' ) ) { |
|
| 69 | + foreach ( $addons as $addon ) { |
|
| 70 | + echo '<li><i class="far fa-check-circle fa-sm"></i> '.esc_html( $addon->info->title ).'</li>'; |
|
| 71 | + } |
|
| 72 | + } |
|
| 73 | + ?> |
|
| 74 | 74 | </ul> |
| 75 | 75 | </div> |
| 76 | 76 | |
@@ -81,8 +81,8 @@ discard block |
||
| 81 | 81 | <div class="testimonial-content"> |
| 82 | 82 | <div class="t-image"> |
| 83 | 83 | <?php |
| 84 | - echo '<img src="' . plugins_url( 'images/t-image2.png', dirname(__FILE__) ) . '" > '; |
|
| 85 | - ?> |
|
| 84 | + echo '<img src="' . plugins_url( 'images/t-image2.png', dirname(__FILE__) ) . '" > '; |
|
| 85 | + ?> |
|
| 86 | 86 | </div> |
| 87 | 87 | <div class="t-content"> |
| 88 | 88 | <p> |
@@ -101,8 +101,8 @@ discard block |
||
| 101 | 101 | <div class="testimonial-content"> |
| 102 | 102 | <div class="t-image"> |
| 103 | 103 | <?php |
| 104 | - echo '<img src="' . plugins_url( 'images/t-image1.png', dirname(__FILE__) ) . '" > '; |
|
| 105 | - ?> |
|
| 104 | + echo '<img src="' . plugins_url( 'images/t-image1.png', dirname(__FILE__) ) . '" > '; |
|
| 105 | + ?> |
|
| 106 | 106 | </div> |
| 107 | 107 | <div class="t-content"> |
| 108 | 108 | <p> |
@@ -126,21 +126,21 @@ discard block |
||
| 126 | 126 | </div> |
| 127 | 127 | </div> |
| 128 | 128 | <?php |
| 129 | - }else{ |
|
| 130 | - $installed_plugins = get_plugins(); |
|
| 129 | + }else{ |
|
| 130 | + $installed_plugins = get_plugins(); |
|
| 131 | 131 | $addon_obj = new WPInv_Admin_Addons(); |
| 132 | - if ($addons = $addon_obj->get_section_data( $current_tab ) ) : |
|
| 133 | - //print_r($addons); |
|
| 134 | - ?> |
|
| 132 | + if ($addons = $addon_obj->get_section_data( $current_tab ) ) : |
|
| 133 | + //print_r($addons); |
|
| 134 | + ?> |
|
| 135 | 135 | <ul class="wpi-products"><?php foreach ( $addons as $addon ) : |
| 136 | 136 | if(965==$addon->info->id){continue;}// don't show quote add on |
| 137 | - ?><li class="wpi-product"> |
|
| 137 | + ?><li class="wpi-product"> |
|
| 138 | 138 | <div class="wpi-product-title"> |
| 139 | 139 | <h3><?php |
| 140 | - if ( ! empty( $addon->info->excerpt) ){ |
|
| 141 | - echo wpi_help_tip( $addon->info->excerpt ); |
|
| 142 | - } |
|
| 143 | - echo esc_html( $addon->info->title ); ?></h3> |
|
| 140 | + if ( ! empty( $addon->info->excerpt) ){ |
|
| 141 | + echo wpi_help_tip( $addon->info->excerpt ); |
|
| 142 | + } |
|
| 143 | + echo esc_html( $addon->info->title ); ?></h3> |
|
| 144 | 144 | </div> |
| 145 | 145 | |
| 146 | 146 | <span class="wpi-product-image"> |
@@ -148,32 +148,32 @@ discard block |
||
| 148 | 148 | <img src="<?php echo esc_attr( $addon->info->thumbnail ); ?>"/> |
| 149 | 149 | <?php endif; |
| 150 | 150 | |
| 151 | - if ( 'stripe-payment-gateway' == $addon->info->slug ) { |
|
| 152 | - $addon->info->slug = 'getpaid-stripe-payments'; |
|
| 153 | - $addon->info->link = 'https://wordpress.org/plugins/getpaid-stripe-payments/'; |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - if(isset($addon->info->link) && substr( $addon->info->link, 0, 21 ) === "https://wordpress.org"){ |
|
| 157 | - echo '<a href="'.admin_url('/plugin-install.php?tab=plugin-information&plugin='.$addon->info->slug).'&width=770&height=660&TB_iframe=true" class="thickbox" >'; |
|
| 158 | - echo '<span class="wpi-product-info">'.__('More info','invoicing').'</span>'; |
|
| 159 | - echo '</a>'; |
|
| 160 | - }elseif(isset($addon->info->link) && ( substr( $addon->info->link, 0, 23 ) === "https://wpinvoicing.com" || substr( $addon->info->link, 0, 21 ) === "https://wpgetpaid.com" ) ){ |
|
| 161 | - if(defined('WP_EASY_UPDATES_ACTIVE')){ |
|
| 162 | - $url = admin_url('/plugin-install.php?tab=plugin-information&plugin='.$addon->info->slug.'&width=770&height=660&item_id='.$addon->info->id.'&update_url=https://wpgetpaid.com&TB_iframe=true'); |
|
| 163 | - }else{ |
|
| 164 | - // if installed show activation link |
|
| 165 | - if(isset($installed_plugins['wp-easy-updates/external-updates.php'])){ |
|
| 166 | - $url = '#TB_inline?width=600&height=50&inlineId=wpi-wpeu-required-activation'; |
|
| 167 | - }else{ |
|
| 168 | - $url = '#TB_inline?width=600&height=50&inlineId=wpi-wpeu-required-for-external'; |
|
| 169 | - } |
|
| 170 | - } |
|
| 171 | - echo '<a href="'.$url.'" class="thickbox">'; |
|
| 172 | - echo '<span class="wpi-product-info">'.__('More info','invoicing').'</span>'; |
|
| 173 | - echo '</a>'; |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - ?> |
|
| 151 | + if ( 'stripe-payment-gateway' == $addon->info->slug ) { |
|
| 152 | + $addon->info->slug = 'getpaid-stripe-payments'; |
|
| 153 | + $addon->info->link = 'https://wordpress.org/plugins/getpaid-stripe-payments/'; |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + if(isset($addon->info->link) && substr( $addon->info->link, 0, 21 ) === "https://wordpress.org"){ |
|
| 157 | + echo '<a href="'.admin_url('/plugin-install.php?tab=plugin-information&plugin='.$addon->info->slug).'&width=770&height=660&TB_iframe=true" class="thickbox" >'; |
|
| 158 | + echo '<span class="wpi-product-info">'.__('More info','invoicing').'</span>'; |
|
| 159 | + echo '</a>'; |
|
| 160 | + }elseif(isset($addon->info->link) && ( substr( $addon->info->link, 0, 23 ) === "https://wpinvoicing.com" || substr( $addon->info->link, 0, 21 ) === "https://wpgetpaid.com" ) ){ |
|
| 161 | + if(defined('WP_EASY_UPDATES_ACTIVE')){ |
|
| 162 | + $url = admin_url('/plugin-install.php?tab=plugin-information&plugin='.$addon->info->slug.'&width=770&height=660&item_id='.$addon->info->id.'&update_url=https://wpgetpaid.com&TB_iframe=true'); |
|
| 163 | + }else{ |
|
| 164 | + // if installed show activation link |
|
| 165 | + if(isset($installed_plugins['wp-easy-updates/external-updates.php'])){ |
|
| 166 | + $url = '#TB_inline?width=600&height=50&inlineId=wpi-wpeu-required-activation'; |
|
| 167 | + }else{ |
|
| 168 | + $url = '#TB_inline?width=600&height=50&inlineId=wpi-wpeu-required-for-external'; |
|
| 169 | + } |
|
| 170 | + } |
|
| 171 | + echo '<a href="'.$url.'" class="thickbox">'; |
|
| 172 | + echo '<span class="wpi-product-info">'.__('More info','invoicing').'</span>'; |
|
| 173 | + echo '</a>'; |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + ?> |
|
| 177 | 177 | |
| 178 | 178 | </span> |
| 179 | 179 | |
@@ -181,15 +181,15 @@ discard block |
||
| 181 | 181 | <span class="wpi-product-button"> |
| 182 | 182 | <?php |
| 183 | 183 | $addon_obj->output_button( $addon ); |
| 184 | - ?> |
|
| 184 | + ?> |
|
| 185 | 185 | </span> |
| 186 | 186 | |
| 187 | 187 | <span class="wpi-price"><?php //print_r($addon); //echo wp_kses_post( $addon->price ); ?></span></li><?php endforeach; ?></ul> |
| 188 | 188 | <?php endif; |
| 189 | - } |
|
| 189 | + } |
|
| 190 | 190 | |
| 191 | - } |
|
| 192 | - ?> |
|
| 191 | + } |
|
| 192 | + ?> |
|
| 193 | 193 | |
| 194 | 194 | |
| 195 | 195 | <div class="clearfix" ></div> |
@@ -208,8 +208,8 @@ discard block |
||
| 208 | 208 | <input class="wpeu-licence-key" type="text" placeholder="<?php _e("Enter your licence key",'invoicing');?>"> <button class="button-primary wpeu-licence-popup-button" ><?php _e("Install",'invoicing');?></button> |
| 209 | 209 | <br> |
| 210 | 210 | <?php |
| 211 | - echo sprintf( __('%sFind your licence key here%s OR %sBuy one here%s', 'invoicing'), '<a href="https://wpinvoicing.com/your-account/" target="_blank">','</a>','<a class="wpeu-licence-link" href="https://wpinvoicing.com/downloads/category/addons/" target="_blank">','</a>' ); |
|
| 212 | - ?> |
|
| 211 | + echo sprintf( __('%sFind your licence key here%s OR %sBuy one here%s', 'invoicing'), '<a href="https://wpinvoicing.com/your-account/" target="_blank">','</a>','<a class="wpeu-licence-link" href="https://wpinvoicing.com/downloads/category/addons/" target="_blank">','</a>' ); |
|
| 212 | + ?> |
|
| 213 | 213 | </span> |
| 214 | 214 | </div> |
| 215 | 215 | |
@@ -13,9 +13,9 @@ discard block |
||
| 13 | 13 | |
| 14 | 14 | |
| 15 | 15 | function wpinv_get_default_country() { |
| 16 | - $country = wpinv_get_option( 'default_country', 'UK' ); |
|
| 16 | + $country = wpinv_get_option( 'default_country', 'UK' ); |
|
| 17 | 17 | |
| 18 | - return apply_filters( 'wpinv_default_country', $country ); |
|
| 18 | + return apply_filters( 'wpinv_default_country', $country ); |
|
| 19 | 19 | } |
| 20 | 20 | |
| 21 | 21 | /** |
@@ -36,7 +36,7 @@ discard block |
||
| 36 | 36 | */ |
| 37 | 37 | function wpinv_sanitize_country( $country ) { |
| 38 | 38 | |
| 39 | - // Enure the country is specified |
|
| 39 | + // Enure the country is specified |
|
| 40 | 40 | if ( empty( $country ) ) { |
| 41 | 41 | $country = wpinv_get_default_country(); |
| 42 | 42 | } |
@@ -66,9 +66,9 @@ discard block |
||
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | function wpinv_get_default_state() { |
| 69 | - $state = wpinv_get_option( 'default_state', '' ); |
|
| 69 | + $state = wpinv_get_option( 'default_state', '' ); |
|
| 70 | 70 | |
| 71 | - return apply_filters( 'wpinv_default_state', $state ); |
|
| 71 | + return apply_filters( 'wpinv_default_state', $state ); |
|
| 72 | 72 | } |
| 73 | 73 | |
| 74 | 74 | function wpinv_state_name( $state_code = '', $country_code = '' ) { |
@@ -305,11 +305,11 @@ discard block |
||
| 305 | 305 | |
| 306 | 306 | $country = wpinv_sanitize_country( $country ); |
| 307 | 307 | |
| 308 | - foreach ( wpinv_get_continents( 'countries' ) as $continent_code => $countries ) { |
|
| 309 | - if ( false !== array_search( $country, $countries, true ) ) { |
|
| 310 | - return $continent_code; |
|
| 311 | - } |
|
| 312 | - } |
|
| 308 | + foreach ( wpinv_get_continents( 'countries' ) as $continent_code => $countries ) { |
|
| 309 | + if ( false !== array_search( $country, $countries, true ) ) { |
|
| 310 | + return $continent_code; |
|
| 311 | + } |
|
| 312 | + } |
|
| 313 | 313 | |
| 314 | 314 | return ''; |
| 315 | 315 | |
@@ -601,33 +601,33 @@ discard block |
||
| 601 | 601 | } |
| 602 | 602 | |
| 603 | 603 | function wpinv_get_states_field() { |
| 604 | - if( empty( $_POST['country'] ) ) { |
|
| 605 | - $_POST['country'] = wpinv_get_default_country(); |
|
| 606 | - } |
|
| 607 | - $states = wpinv_get_country_states( sanitize_text_field( $_POST['country'] ) ); |
|
| 604 | + if( empty( $_POST['country'] ) ) { |
|
| 605 | + $_POST['country'] = wpinv_get_default_country(); |
|
| 606 | + } |
|
| 607 | + $states = wpinv_get_country_states( sanitize_text_field( $_POST['country'] ) ); |
|
| 608 | 608 | |
| 609 | - if( !empty( $states ) ) { |
|
| 610 | - $sanitized_field_name = sanitize_text_field( $_POST['field_name'] ); |
|
| 609 | + if( !empty( $states ) ) { |
|
| 610 | + $sanitized_field_name = sanitize_text_field( $_POST['field_name'] ); |
|
| 611 | 611 | |
| 612 | 612 | $class = isset( $_POST['class'] ) ? esc_attr( $_POST['class'] ) : ''; |
| 613 | 613 | $class .= " $sanitized_field_name getpaid_js_field-state custom-select wpinv-select wpi_select2"; |
| 614 | 614 | |
| 615 | 615 | $args = array( |
| 616 | - 'name' => $sanitized_field_name, |
|
| 617 | - 'id' => $sanitized_field_name, |
|
| 618 | - 'class' => implode( ' ', array_unique( explode( ' ', $class ) ) ), |
|
| 619 | - 'options' => array_merge( array( '' => '' ), $states ), |
|
| 620 | - 'show_option_all' => false, |
|
| 621 | - 'show_option_none' => false |
|
| 622 | - ); |
|
| 623 | - |
|
| 624 | - $response = wpinv_html_select( $args ); |
|
| 625 | - |
|
| 626 | - } else { |
|
| 627 | - $response = 'nostates'; |
|
| 628 | - } |
|
| 616 | + 'name' => $sanitized_field_name, |
|
| 617 | + 'id' => $sanitized_field_name, |
|
| 618 | + 'class' => implode( ' ', array_unique( explode( ' ', $class ) ) ), |
|
| 619 | + 'options' => array_merge( array( '' => '' ), $states ), |
|
| 620 | + 'show_option_all' => false, |
|
| 621 | + 'show_option_none' => false |
|
| 622 | + ); |
|
| 623 | + |
|
| 624 | + $response = wpinv_html_select( $args ); |
|
| 625 | + |
|
| 626 | + } else { |
|
| 627 | + $response = 'nostates'; |
|
| 628 | + } |
|
| 629 | 629 | |
| 630 | - return $response; |
|
| 630 | + return $response; |
|
| 631 | 631 | } |
| 632 | 632 | |
| 633 | 633 | function wpinv_default_billing_country( $country = '', $user_id = 0 ) { |
@@ -645,46 +645,46 @@ discard block |
||
| 645 | 645 | */ |
| 646 | 646 | function wpinv_get_address_formats() { |
| 647 | 647 | |
| 648 | - return apply_filters( 'wpinv_localisation_address_formats', |
|
| 649 | - array( |
|
| 650 | - 'default' => "{{name}}\n{{company}}\n{{address}}\n{{city}}\n{{state}}\n{{zip}}\n{{country}}", |
|
| 651 | - 'AU' => "{{name}}\n{{company}}\n{{address}}\n{{city}}\n{{state}} {{zip}}\n{{country}}", |
|
| 652 | - 'AT' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 653 | - 'BE' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 654 | - 'CA' => "{{company}}\n{{name}}\n{{address}}\n{{city}} {{state_code}} {{zip}}\n{{country}}", |
|
| 655 | - 'CH' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 656 | - 'CL' => "{{company}}\n{{name}}\n{{address}}\n{{state}}\n{{zip}} {{city}}\n{{country}}", |
|
| 657 | - 'CN' => "{{country}} {{zip}}\n{{state}}, {{city}}, {{address}}\n{{company}}\n{{name}}", |
|
| 658 | - 'CZ' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 659 | - 'DE' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 660 | - 'EE' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 661 | - 'FI' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 662 | - 'DK' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 663 | - 'FR' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city_upper}}\n{{country}}", |
|
| 664 | - 'HK' => "{{company}}\n{{first_name}} {{last_name_upper}}\n{{address}}\n{{city_upper}}\n{{state_upper}}\n{{country}}", |
|
| 665 | - 'HU' => "{{name}}\n{{company}}\n{{city}}\n{{address}}\n{{zip}}\n{{country}}", |
|
| 666 | - 'IN' => "{{company}}\n{{name}}\n{{address}}\n{{city}} {{zip}}\n{{state}}, {{country}}", |
|
| 667 | - 'IS' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 668 | - 'IT' => "{{company}}\n{{name}}\n{{address}}\n{{zip}}\n{{city}}\n{{state_upper}}\n{{country}}", |
|
| 669 | - 'JP' => "{{zip}}\n{{state}} {{city}} {{address}}\n{{company}}\n{{last_name}} {{first_name}}\n{{country}}", |
|
| 670 | - 'TW' => "{{company}}\n{{last_name}} {{first_name}}\n{{address}}\n{{state}}, {{city}} {{zip}}\n{{country}}", |
|
| 671 | - 'LI' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 672 | - 'NL' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 673 | - 'NZ' => "{{name}}\n{{company}}\n{{address}}\n{{city}} {{zip}}\n{{country}}", |
|
| 674 | - 'NO' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 675 | - 'PL' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 676 | - 'PT' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 677 | - 'SK' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 678 | - 'RS' => "{{name}}\n{{company}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 679 | - 'SI' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 680 | - 'ES' => "{{name}}\n{{company}}\n{{address}}\n{{zip}} {{city}}\n{{state}}\n{{country}}", |
|
| 681 | - 'SE' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 682 | - 'TR' => "{{name}}\n{{company}}\n{{address}}\n{{zip}} {{city}} {{state}}\n{{country}}", |
|
| 683 | - 'UG' => "{{name}}\n{{company}}\n{{address}}\n{{city}}\n{{state}}, {{country}}", |
|
| 684 | - 'US' => "{{name}}\n{{company}}\n{{address}}\n{{city}}, {{state_code}} {{zip}}\n{{country}}", |
|
| 685 | - 'VN' => "{{name}}\n{{company}}\n{{address}}\n{{city}}\n{{country}}", |
|
| 686 | - ) |
|
| 687 | - ); |
|
| 648 | + return apply_filters( 'wpinv_localisation_address_formats', |
|
| 649 | + array( |
|
| 650 | + 'default' => "{{name}}\n{{company}}\n{{address}}\n{{city}}\n{{state}}\n{{zip}}\n{{country}}", |
|
| 651 | + 'AU' => "{{name}}\n{{company}}\n{{address}}\n{{city}}\n{{state}} {{zip}}\n{{country}}", |
|
| 652 | + 'AT' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 653 | + 'BE' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 654 | + 'CA' => "{{company}}\n{{name}}\n{{address}}\n{{city}} {{state_code}} {{zip}}\n{{country}}", |
|
| 655 | + 'CH' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 656 | + 'CL' => "{{company}}\n{{name}}\n{{address}}\n{{state}}\n{{zip}} {{city}}\n{{country}}", |
|
| 657 | + 'CN' => "{{country}} {{zip}}\n{{state}}, {{city}}, {{address}}\n{{company}}\n{{name}}", |
|
| 658 | + 'CZ' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 659 | + 'DE' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 660 | + 'EE' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 661 | + 'FI' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 662 | + 'DK' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 663 | + 'FR' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city_upper}}\n{{country}}", |
|
| 664 | + 'HK' => "{{company}}\n{{first_name}} {{last_name_upper}}\n{{address}}\n{{city_upper}}\n{{state_upper}}\n{{country}}", |
|
| 665 | + 'HU' => "{{name}}\n{{company}}\n{{city}}\n{{address}}\n{{zip}}\n{{country}}", |
|
| 666 | + 'IN' => "{{company}}\n{{name}}\n{{address}}\n{{city}} {{zip}}\n{{state}}, {{country}}", |
|
| 667 | + 'IS' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 668 | + 'IT' => "{{company}}\n{{name}}\n{{address}}\n{{zip}}\n{{city}}\n{{state_upper}}\n{{country}}", |
|
| 669 | + 'JP' => "{{zip}}\n{{state}} {{city}} {{address}}\n{{company}}\n{{last_name}} {{first_name}}\n{{country}}", |
|
| 670 | + 'TW' => "{{company}}\n{{last_name}} {{first_name}}\n{{address}}\n{{state}}, {{city}} {{zip}}\n{{country}}", |
|
| 671 | + 'LI' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 672 | + 'NL' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 673 | + 'NZ' => "{{name}}\n{{company}}\n{{address}}\n{{city}} {{zip}}\n{{country}}", |
|
| 674 | + 'NO' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 675 | + 'PL' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 676 | + 'PT' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 677 | + 'SK' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 678 | + 'RS' => "{{name}}\n{{company}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 679 | + 'SI' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 680 | + 'ES' => "{{name}}\n{{company}}\n{{address}}\n{{zip}} {{city}}\n{{state}}\n{{country}}", |
|
| 681 | + 'SE' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
| 682 | + 'TR' => "{{name}}\n{{company}}\n{{address}}\n{{zip}} {{city}} {{state}}\n{{country}}", |
|
| 683 | + 'UG' => "{{name}}\n{{company}}\n{{address}}\n{{city}}\n{{state}}, {{country}}", |
|
| 684 | + 'US' => "{{name}}\n{{company}}\n{{address}}\n{{city}}, {{state_code}} {{zip}}\n{{country}}", |
|
| 685 | + 'VN' => "{{name}}\n{{company}}\n{{address}}\n{{city}}\n{{country}}", |
|
| 686 | + ) |
|
| 687 | + ); |
|
| 688 | 688 | } |
| 689 | 689 | |
| 690 | 690 | /** |
@@ -701,21 +701,21 @@ discard block |
||
| 701 | 701 | } |
| 702 | 702 | |
| 703 | 703 | // Get all formats. |
| 704 | - $formats = wpinv_get_address_formats(); |
|
| 704 | + $formats = wpinv_get_address_formats(); |
|
| 705 | 705 | |
| 706 | - // Get format for the specified country. |
|
| 707 | - $format = ( $country && isset( $formats[ $country ] ) ) ? $formats[ $country ] : $formats['default']; |
|
| 706 | + // Get format for the specified country. |
|
| 707 | + $format = ( $country && isset( $formats[ $country ] ) ) ? $formats[ $country ] : $formats['default']; |
|
| 708 | 708 | |
| 709 | 709 | /** |
| 710 | - * Filters the address format to use on Invoices. |
|
| 710 | + * Filters the address format to use on Invoices. |
|
| 711 | 711 | * |
| 712 | 712 | * New lines will be replaced by a `br` element. Double new lines will be replaced by a paragraph. HTML tags are allowed. |
| 713 | - * |
|
| 714 | - * @since 1.0.13 |
|
| 715 | - * |
|
| 716 | - * @param string $format The address format to use. |
|
| 713 | + * |
|
| 714 | + * @since 1.0.13 |
|
| 715 | + * |
|
| 716 | + * @param string $format The address format to use. |
|
| 717 | 717 | * @param string $country The country who's address format is being retrieved. |
| 718 | - */ |
|
| 718 | + */ |
|
| 719 | 719 | return apply_filters( 'wpinv_get_full_address_format', $format, $country ); |
| 720 | 720 | } |
| 721 | 721 | |
@@ -736,8 +736,8 @@ discard block |
||
| 736 | 736 | 'country' => '', |
| 737 | 737 | 'zip' => '', |
| 738 | 738 | 'first_name' => '', |
| 739 | - 'last_name' => '', |
|
| 740 | - 'company' => '', |
|
| 739 | + 'last_name' => '', |
|
| 740 | + 'company' => '', |
|
| 741 | 741 | ); |
| 742 | 742 | |
| 743 | 743 | $args = map_deep( wp_parse_args( $billing_details, $default_args ), 'trim' ); |
@@ -758,14 +758,14 @@ discard block |
||
| 758 | 758 | $args['country_code']= $country; |
| 759 | 759 | |
| 760 | 760 | /** |
| 761 | - * Filters the address format replacements to use on Invoices. |
|
| 761 | + * Filters the address format replacements to use on Invoices. |
|
| 762 | 762 | * |
| 763 | - * |
|
| 764 | - * @since 1.0.13 |
|
| 765 | - * |
|
| 766 | - * @param array $replacements The address replacements to use. |
|
| 763 | + * |
|
| 764 | + * @since 1.0.13 |
|
| 765 | + * |
|
| 766 | + * @param array $replacements The address replacements to use. |
|
| 767 | 767 | * @param array $billing_details The billing details to use. |
| 768 | - */ |
|
| 768 | + */ |
|
| 769 | 769 | $replacements = apply_filters( 'wpinv_get_invoice_address_replacements', $args, $billing_details ); |
| 770 | 770 | |
| 771 | 771 | $return = array(); |
@@ -788,5 +788,5 @@ discard block |
||
| 788 | 788 | * @return string |
| 789 | 789 | */ |
| 790 | 790 | function wpinv_trim_formatted_address_line( $line ) { |
| 791 | - return trim( $line, ', ' ); |
|
| 791 | + return trim( $line, ', ' ); |
|
| 792 | 792 | } |
| 793 | 793 | \ No newline at end of file |
@@ -20,13 +20,13 @@ |
||
| 20 | 20 | <title><?php esc_html_e( 'GetPaid › Setup Wizard', 'invoicing' ); ?></title> |
| 21 | 21 | <?php |
| 22 | 22 | getpaid_admin()->enqeue_scripts(); |
| 23 | - wp_enqueue_style( 'font-awesome', 'https://use.fontawesome.com/releases/v5.13.0/css/all.css', array(), 'v5.13.0' ); |
|
| 24 | - wp_print_styles( 'select2' ); |
|
| 23 | + wp_enqueue_style( 'font-awesome', 'https://use.fontawesome.com/releases/v5.13.0/css/all.css', array(), 'v5.13.0' ); |
|
| 24 | + wp_print_styles( 'select2' ); |
|
| 25 | 25 | wp_print_scripts( 'select2' ); |
| 26 | - wp_print_scripts( 'wpinv-admin-script' ); |
|
| 26 | + wp_print_scripts( 'wpinv-admin-script' ); |
|
| 27 | 27 | do_action( 'admin_print_styles' ); |
| 28 | 28 | do_action( 'admin_head' ); |
| 29 | - ?> |
|
| 29 | + ?> |
|
| 30 | 30 | <style> |
| 31 | 31 | body, p{ |
| 32 | 32 | font-size: 16px; |