← Back
Editing: actions.php
<?php /** * Simple Pay: Actions * * @package SimplePay\Core\Post_Types\Simple_Pay\Actions * @copyright Copyright (c) 2022, Sandhills Development, LLC * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License * @since 3.8.0 */ namespace SimplePay\Core\Post_Types\Simple_Pay\Actions; use Exception; use SimplePay\Core\API; use SimplePay\Vendor\Stripe\Exception\ApiErrorException; use WP_Error; if ( ! defined( 'ABSPATH' ) ) { exit; } /** * Saves the Payment Form's settings. * * @since 3.8.0 * * @param int $post_id Current Payment Form ID. * @param \WP_Post $post Current Payment Form \WP_Post object. * @param bool $update Whether this is an existing Payment Form or update. */ function save( $post_id, $post, $update ) { // Bail if we have no Payment Form. if ( empty( $post_id ) || empty( $post ) ) { return; } // Bail if doing an autosave or is a revision. if ( defined( 'DOING_AUTOSAVE' ) || is_int( wp_is_post_revision( $post ) ) || is_int( wp_is_post_autosave( $post ) ) ) { return; } // Bail if we cannot verify our nonce. if ( empty( $_POST['simpay_meta_nonce'] ) || ! wp_verify_nonce( wp_unslash( $_POST['simpay_meta_nonce'] ), 'simpay_save_data' ) // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized ) { return; } // Bail if the current user does not have permissions. if ( ! current_user_can( 'edit_post', $post_id ) ) { return; } $form = simpay_get_form( $post_id ); if ( false === $form ) { return; } // Payment Mode. $livemode = isset( $_POST['_livemode'] ) && '' !== $_POST['_livemode'] ? absint( $_POST['_livemode'] ) : ''; update_post_meta( $post_id, '_livemode_prev', get_post_meta( $post_id, '_livemode', true ) ); update_post_meta( $post_id, '_livemode', $livemode ); // Success redirect type. $success_redirect_type = isset( $_POST['_success_redirect_type'] ) ? esc_attr( wp_unslash( $_POST['_success_redirect_type'] ) ) // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized : 'dedicated'; update_post_meta( $post_id, '_success_redirect_type', $success_redirect_type ); // Success message. if ( isset( $_POST['_success_message'] ) && ! empty( $_POST['_success_message'] ) ) { $success_message = wp_kses_post( wp_unslash( $_POST['_success_message'] ) ); update_post_meta( $post_id, '_success_message', wp_slash( $success_message ) ); } else { delete_post_meta( $post_id, '_success_message' ); } // Success redirect page. $success_redirect_page = isset( $_POST['_success_redirect_page'] ) ? esc_attr( wp_unslash( $_POST['_success_redirect_page'] ) ) // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized : ''; update_post_meta( $post_id, '_success_redirect_page', $success_redirect_page ); // Success redirect URL. $success_redirect_url = isset( $_POST['_success_redirect_url'] ) ? esc_url( wp_unslash( $_POST['_success_redirect_url'] ) ) // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized : ''; update_post_meta( $post_id, '_success_redirect_url', $success_redirect_url ); // Company name. $company_name = isset( $_POST['_company_name'] ) ? sanitize_text_field( wp_unslash( $_POST['_company_name'] ) ) : ''; update_post_meta( $post_id, '_company_name', wp_slash( $company_name ) ); // Item description. $item_description = isset( $_POST['_item_description'] ) ? sanitize_text_field( wp_unslash( $_POST['_item_description'] ) ) : ''; update_post_meta( $post_id, '_item_description', wp_slash( $item_description ) ); // Image URL. $image_url = isset( $_POST['_image_url'] ) ? sanitize_text_field( wp_unslash( $_POST['_image_url'] ) ) : ''; update_post_meta( $post_id, '_image_url', $image_url ); // Recurring amount format (per-form override). Pro-only feature. if ( false === simpay_get_license()->is_lite() ) { $recurring_amount_format = isset( $_POST['_recurring_amount_format'] ) ? sanitize_text_field( wp_unslash( $_POST['_recurring_amount_format'] ) ) : ''; update_post_meta( $post_id, '_recurring_amount_format', $recurring_amount_format ); } // Submit type. $checkout_submit_type = isset( $_POST['_checkout_submit_type'] ) ? sanitize_text_field( wp_unslash( $_POST['_checkout_submit_type'] ) ) : 'pay'; update_post_meta( $post_id, '_checkout_submit_type', $checkout_submit_type ); // Transaction description source. $transaction_description_source = isset( $_POST['_transaction_description_source'] ) ? sanitize_text_field( wp_unslash( $_POST['_transaction_description_source'] ) ) : ''; update_post_meta( $post_id, '_transaction_description_source', $transaction_description_source ); // Transaction description custom text. $transaction_description_custom = isset( $_POST['_transaction_description_custom'] ) ? sanitize_text_field( wp_unslash( $_POST['_transaction_description_custom'] ) ) : ''; // `update_post_meta()` unslashes its value, so re-slash the already // unslashed text to keep literal backslashes. update_post_meta( $post_id, '_transaction_description_custom', wp_slash( $transaction_description_custom ) ); // Billing address. $enable_billing_address = isset( $_POST['_enable_billing_address'] ) ? 'yes' : 'no'; update_post_meta( $post_id, '_enable_billing_address', $enable_billing_address ); // Shipping address. $enable_shipping_address = isset( $_POST['_enable_shipping_address'] ) ? 'yes' : 'no'; update_post_meta( $post_id, '_enable_shipping_address', $enable_shipping_address ); // Phone number. $enable_phone = isset( $_POST['_enable_phone'] ) ? 'yes' : 'no'; update_post_meta( $post_id, '_enable_phone', $enable_phone ); // Promotion codes. $enable_promotion_codes = isset( $_POST['_enable_promotion_codes'] ) ? 'yes' : 'no'; update_post_meta( $post_id, '_enable_promotion_codes', $enable_promotion_codes ); // Tax ID. $enable_tax_id = isset( $_POST['_enable_tax_id'] ) ? 'yes' : 'no'; update_post_meta( $post_id, '_enable_tax_id', $enable_tax_id ); // Quantity. $enable_quantity = isset( $_POST['_enable_quantity'] ) ? 'yes' : 'no'; update_post_meta( $post_id, '_enable_quantity', $enable_quantity ); // Trust badge: hide-only model. The meta represents the EXCEPTION to the // default (badge shown). Write 'yes' only when the seller has actively // asked us to hide the badge; write 'no' otherwise so the stored value // mirrors the checkbox state. Absent meta means the badge is shown. $hide_trust_badge = isset( $_POST['_simpay_hide_trust_badge'] ) ? 'yes' : 'no'; update_post_meta( $post_id, '_simpay_hide_trust_badge', $hide_trust_badge ); // Stripe Link: override-only model. The meta represents the EXCEPTION // to Stripe's default (Link enabled). Write 'yes' only when the seller // has actively asked us to hide Link; write 'no' otherwise so the // stored value mirrors the checkbox state. Default behavior (Link on) // matches absent meta + this 'no' value via simpay_get_saved_meta() // default in PaymentRequestUtils::maybe_remove_stripe_link(). $disable_stripe_link = isset( $_POST['_disable_stripe_link'] ) ? 'yes' : 'no'; update_post_meta( $post_id, '_disable_stripe_link', $disable_stripe_link ); // Custom fields. $fields = isset( $_POST['_simpay_custom_field'] ) ? wp_unslash( $_POST['_simpay_custom_field'] ) // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized : array(); $conditional_logic_excluded_types = function_exists( '\SimplePay\Pro\Post_Types\Simple_Pay\Util\get_conditional_logic_excluded_field_types' ) ? \SimplePay\Pro\Post_Types\Simple_Pay\Util\get_conditional_logic_excluded_field_types() : array(); foreach ( $fields as $type => $instances ) { if ( ! is_array( $instances ) ) { continue; } foreach ( $instances as $index => $instance ) { if ( in_array( $type, $conditional_logic_excluded_types, true ) && isset( $fields[ $type ][ $index ]['conditional_logic'] ) ) { unset( $fields[ $type ][ $index ]['conditional_logic'] ); } } } update_post_meta( $post_id, '_custom_fields', wp_slash( $fields ) ); // Payment Methods. // Gets superseded by the Pro save routine. $default_payment_methods = array( 'card', ); $allowed_payment_methods = array( 'card', 'alipay', 'ideal', 'fpx', 'p24', ); $payment_methods = isset( $_POST['_simpay_payment_methods'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['_simpay_payment_methods'] ) ) : $default_payment_methods; // Remove any payment methods that are not allowed. $payment_methods = array_filter( $payment_methods, function ( $payment_method ) use ( $allowed_payment_methods ) { return in_array( $payment_method, $allowed_payment_methods, true ); } ); // Format the payment methods for storage (to match the Pro format). Keyed // by the payment method ID with an array of the payment method ID (and additional // configuration settings in Pro). if ( empty( $payment_methods ) ) { $payment_methods = $default_payment_methods; } $_payment_methods = array(); foreach ( $payment_methods as $payment_method ) { $_payment_methods[ $payment_method ] = array( 'id' => $payment_method, ); } update_post_meta( $post_id, '_payment_methods', array( 'stripe-checkout' => $_payment_methods, ) ); if ( false === $update ) { /** * Allows further action to be taken when a Payment Form is created. * * @since 3.0.0 * * @param int $post_id Payment Form ID. */ do_action( 'simpay_form_created', $post_id ); } /** * Allows further action to be taken when a Payment Form is updated. * * @since 3.0.0 * * @param int $post_id Payment Form ID. * @param \WP_Post $post Payment Form \WP_Post object. * @param \SimplePay\Core\Abstracts\Form $form Payment Form. */ do_action( 'simpay_save_form_settings', $post_id, $post, $form ); } add_action( 'save_post_simple-pay', __NAMESPACE__ . '\\save', 10, 3 ); /** * Saves the Payment Form's container Product. * * @since 4.1.0 * * @param int $post_id Payment Form ID. * @param \WP_Post $post Payment Form \WP_Post object. * @param \SimplePay\Core\Abstracts\Form $form Payment Form. * @return \SimplePay\Vendor\Stripe\Product */ function save_product( $post_id, $post, $form ) { $error = new WP_Error(); $product_args = array(); $product_key = true === $form->is_livemode() ? '_simpay_product_live' : '_simpay_product_test'; $form_product = get_post_meta( $post_id, $product_key, true ); // Name. $title = get_post_meta( $form->id, '_company_name', true ); $name = ! empty( $title ) ? $title : get_bloginfo( 'name' ); // https://github.com/wpsimplepay/wp-simple-pay-pro/issues/1598. if ( empty( $name ) ) { $name = sprintf( /* translators: %d: Payment Form ID. */ __( 'WP Simple Pay - Form %d', 'stripe' ), $form->id ); } $product_args['name'] = esc_html( $name ); // Description. Optional. $description = get_post_meta( $form->id, '_item_description', true ); // Images. Optional. if ( '' !== $form->image_url ) { $product_args['images'] = array( $form->image_url, ); } // Tax code for automatic tax calculation. $tax_status = get_post_meta( $form->id, '_tax_status', true ); if ( 'automatic' === $tax_status ) { $tax_code = get_post_meta( $form->id, '_tax_code', true ); $product_args['tax_code'] = $tax_code; } try { if ( empty( $form_product ) ) { if ( ! empty( $description ) ) { $product_args['description'] = sanitize_text_field( $description ); } $product = API\Products\create( $product_args, $form->get_api_request_args() ); update_post_meta( $form->id, $product_key, $product->id ); $form_product = $product->id; } else { $product_args['description'] = sanitize_text_field( $description ); // Try to update an existing product. try { $product = API\Products\update( $form_product, $product_args, $form->get_api_request_args() ); // Create a new one if the previous cannot be updated. } catch ( \Exception $e ) { $product = API\Products\create( $product_args, $form->get_api_request_args() ); update_post_meta( $form->id, $product_key, $product->id ); $form_product = $product->id; } } } catch ( ApiErrorException $e ) { $error->add( $e->getStripeCode(), $e->getMessage(), array( 'status' => $e->getHttpStatus(), ) ); } catch ( Exception $e ) { $error->add( 'simpay-update-payment-form', $e->getMessage(), array( 'status' => 500, ) ); } if ( ! empty( $error->errors ) ) { $redirect = add_query_arg( array( 'post_type' => 'simple-pay', 'post' => $post_id, 'action' => 'edit', 'simpay-stripe-api-error' => $error->get_error_message(), ), admin_url( 'post.php' ) ); wp_safe_redirect( $redirect ); exit; } return $product; } /** * Determines if skipping or creating a price is needed. * * @since 4.14.0 * * @param array $price Price data. * @param array $recurring_args Recurring args. * @return bool */ function should_skip_price_update( $price, $recurring_args ) { $unit_amount_current = isset( $price['unit_amount_current'] ) ? sanitize_text_field( $price['unit_amount_current'] ) : false; $currency_current = isset( $price['currency_current'] ) ? sanitize_text_field( $price['currency_current'] ) : false; $current_amount_type = isset( $price['amount_type_current'] ) ? sanitize_text_field( $price['amount_type_current'] ) : false; $amount_type = isset( $price['amount_type'] ) ? sanitize_text_field( $price['amount_type'] ) : false; $currency = sanitize_text_field( $price['currency'] ); $unit_amount = sanitize_text_field( $price['unit_amount'] ); $unsaved_price = isset( $price['unsaved'] ) && 'yes' === $price['unsaved'] ? true : false; $is_recurring_interval_changed = false; $recurring_interval_current = isset( $price['recurring_interval_current'] ) ? sanitize_text_field( $price['recurring_interval_current'] ) : false; if ( ! empty( $recurring_args ) ) { $is_recurring_interval_changed = isset( $recurring_args['interval'] ) && $recurring_args['interval'] !== $recurring_interval_current; } return false !== $currency_current && $currency_current === $currency && false !== $unit_amount_current && $unit_amount_current === $unit_amount && false !== $amount_type && $amount_type === $current_amount_type && ! $unsaved_price && ! $is_recurring_interval_changed; } /** * Saves the Payment Form's Prices. * * @since 4.1.0 * * @param int $post_id Payment Form ID. * @param \WP_Post $post Payment Form \WP_Post object. * @param \SimplePay\Core\Abstracts\Form $form Payment Form. */ function save_prices( $post_id, $post, $form ) { $error = new WP_Error(); $product = save_product( $post_id, $post, $form ); $prices = isset( $_POST['_simpay_prices'] ) // phpcs:ignore WordPress.Security.NonceVerification.Missing ? wp_unslash( $_POST['_simpay_prices'] ) // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized : array(); $_prices = array(); $prices_key = true === $form->is_livemode() ? '_simpay_prices_live' : '_simpay_prices_test'; $livemode_prev = get_post_meta( $form->id, '_livemode_prev', true ); if ( '' === $livemode_prev ) { $livemode_prev = ! simpay_is_test_mode(); } // Let the syncing handle the Prices if the mode has changed. if ( (bool) $livemode_prev !== $form->is_livemode() ) { delete_post_meta( $post_id, sprintf( '%s_modified', $prices_key ), time() ); return; } foreach ( $prices as $instance_id => $price ) { $price_args = array( 'default' => isset( $price['default'] ) || 1 === count( $prices ), 'required' => isset( $price['required'] ) && 'on' === $price['required'], ); $recurring_args = array(); $line_item_args = array(); // Setup shared arguments. $currency = sanitize_text_field( $price['currency'] ); $unit_amount = sanitize_text_field( $price['unit_amount'] ); $amount_type = isset( $price['amount_type'] ) ? sanitize_text_field( $price['amount_type'] ) : false; $license = simpay_get_license(); $is_zero_decimal = simpay_is_zero_decimal( $currency ); $currency_min_amount = simpay_get_currency_minimum( $currency ); $currency_max_amount = simpay_get_currency_maximum( $currency ); $unit_amount = $is_zero_decimal ? simpay_unformat_currency( $unit_amount ) : simpay_convert_amount_to_cents( $unit_amount ); if ( $unit_amount < $currency_min_amount ) { $unit_amount = $currency_min_amount; } $amount_type = 'recurring' === $price['amount_type'] ? 'recurring' : 'one-time'; $can_recur = isset( $price['can_recur'] ); $label = isset( $price['label'] ) ? sanitize_text_field( $price['label'] ) : ''; if ( 'recurring' === $amount_type || $can_recur ) { $recurring = isset( $price['recurring'] ) ? $price['recurring'] : array(); if ( isset( $recurring['id'] ) ) { $recurring_args['id'] = $recurring['id']; } $recurring_args['interval'] = isset( $recurring['interval'] ) ? sanitize_text_field( $recurring['interval'] ) : 'month'; $recurring_args['interval_count'] = isset( $recurring['interval_count'] ) ? (int) $recurring['interval_count'] : 1; $invoice_limit = isset( $recurring['invoice_limit'] ) ? (int) $recurring['invoice_limit'] : ''; if ( ! empty( $invoice_limit ) ) { $recurring_args['invoice_limit'] = $invoice_limit; } $trial_period_days = isset( $recurring['trial_period_days'] ) ? (int) $recurring['trial_period_days'] : ''; if ( ! empty( $trial_period_days ) ) { $recurring_args['trial_period_days'] = $trial_period_days; } $line_items = isset( $price['line_items'] ) ? $price['line_items'] : array(); $setup_fee_amount = isset( $line_items['subscription-setup-fee'] ) ? $line_items['subscription-setup-fee']['unit_amount'] : ''; if ( ! empty( $setup_fee_amount ) ) { $line_item_args[] = array( 'unit_amount' => $is_zero_decimal ? simpay_unformat_currency( $setup_fee_amount ) : simpay_convert_amount_to_cents( $setup_fee_amount ), 'currency' => $currency, ); } $plan_fee_amount = isset( $line_items['plan-setup-fee'] ) ? $line_items['plan-setup-fee']['unit_amount'] : ''; if ( ! empty( $plan_fee_amount ) ) { $line_item_args[] = array( 'unit_amount' => $is_zero_decimal ? simpay_unformat_currency( $plan_fee_amount ) : simpay_convert_amount_to_cents( $plan_fee_amount ), 'currency' => $currency, ); } } else { $recurring_args = array(); } // Short circuit for Lite. If the current price data is // the same as the new price data, do nothing. if ( should_skip_price_update( $price, $recurring_args ) ) { $price_id = sanitize_text_field( $price['id_current'] ); $_prices[ $instance_id ] = array_merge( $price_args, array( 'id' => $price_id, ) ); continue; } // Custom price data. if ( isset( $price['custom'] ) ) { $price_args['id'] = $price['id']; $price_args['unit_amount'] = $unit_amount; $price_args['currency'] = $currency; $unit_amount_min = isset( $price['unit_amount_min'] ) ? sanitize_text_field( $is_zero_decimal ? $price['unit_amount_min'] : simpay_convert_amount_to_cents( $price['unit_amount_min'] ) ) : $currency_min_amount; if ( $unit_amount_min < $currency_min_amount ) { $unit_amount_min = $currency_min_amount; } $unit_amount_max = isset( $price['unit_amount_max'] ) ? sanitize_text_field( $is_zero_decimal ? simpay_unformat_currency( $price['unit_amount_max'] ) : simpay_convert_amount_to_cents( $price['unit_amount_max'] ) ) : ''; if ( $unit_amount_max < $unit_amount_min ) { $unit_amount_max = ''; } $price_args['unit_amount_min'] = $unit_amount_min; if ( ! empty( $unit_amount_max ) ) { $price_args['unit_amount_max'] = $unit_amount_max; } // Defined Price. } else { // Existing Price. if ( simpay_payment_form_prices_is_defined_price( $price['id'] ) && ! $license->is_lite() ) { $price_args['id'] = $price['id']; // Update tax_behavior if not previously set, and using automatic taxes. $tax_status = get_post_meta( $form->id, '_tax_status', true ); if ( 'automatic' === $tax_status ) { try { $tax_behavior = get_post_meta( $form->id, '_tax_behavior', true ); $existing_price = API\Prices\retrieve( $price_args['id'], $form->get_api_request_args() ); if ( 'unspecified' === $existing_price->tax_behavior && ! empty( $tax_behavior ) ) { API\Prices\update( $price_args['id'], array( 'tax_behavior' => $tax_behavior, ), $form->get_api_request_args() ); } // Also update recurring, if needed. if ( ! empty( $recurring_args ) && ! empty( $recurring_args['id'] ) && ! empty( $tax_behavior ) ) { $recurring_price = API\Prices\retrieve( $recurring_args['id'], $form->get_api_request_args() ); if ( 'unspecified' === $recurring_price->tax_behavior ) { API\Prices\update( $recurring_args['id'], array( 'tax_behavior' => $tax_behavior, ), $form->get_api_request_args() ); } } } catch ( Exception $e ) { // Do nothing, it can't be changed if already set. } } // Create a new Price. } else { $stripe_price_args = array( 'unit_amount' => $unit_amount, 'currency' => $currency, 'product' => $product->id, ); if ( 'recurring' === $amount_type ) { $stripe_price_args['recurring'] = array( 'interval' => $recurring_args['interval'], 'interval_count' => $recurring_args['interval_count'], ); } $tax_status = get_post_meta( $form->id, '_tax_status', true ); if ( 'automatic' === $tax_status ) { $tax_behavior = get_post_meta( $form->id, '_tax_behavior', true ); if ( ! empty( $tax_behavior ) ) { $stripe_price_args['tax_behavior'] = $tax_behavior; } } try { $stripe_price = API\Prices\create( $stripe_price_args, $form->get_api_request_args() ); $price_args['id'] = $stripe_price->id; if ( $can_recur ) { $stripe_recurring_price = API\Prices\create( array_merge( $stripe_price_args, array( 'recurring' => array( 'interval' => $recurring_args['interval'], 'interval_count' => $recurring_args['interval_count'], ), ) ), $form->get_api_request_args() ); $recurring_args['id'] = $stripe_recurring_price->id; } } catch ( ApiErrorException $e ) { $error->add( $e->getStripeCode(), $e->getMessage(), array( 'status' => $e->getHttpStatus(), ) ); } catch ( Exception $e ) { $error->add( 'simpay-update-payment-form', $e->getMessage(), array( 'status' => 500, ) ); } } } if ( ! empty( $error->errors ) ) { $redirect = add_query_arg( array( 'post_type' => 'simple-pay', 'post' => $post_id, 'action' => 'edit', 'simpay-stripe-api-error' => $error->get_error_message(), ), admin_url( 'post.php' ) ); wp_safe_redirect( $redirect ); exit; } // Label. if ( ! empty( $label ) ) { $price_args['label'] = $label; } // Optional recurring. $price_args['can_recur'] = $can_recur; // Recurring. if ( ! empty( $recurring_args ) ) { // Remove data that can be retrieved from Stripe. if ( ! isset( $price['custom'] ) ) { unset( $recurring_args['interval'] ); unset( $recurring_args['interval_count'] ); } if ( ! empty( $recurring_args ) ) { $price_args['recurring'] = $recurring_args; } } // Line items. if ( ! empty( $line_item_args ) ) { $price_args['line_items'] = $line_item_args; } // Quantity. if ( isset( $price['quantity_toggle'] ) && ! empty( $price['quantity_toggle'] ) ) { $price_args['quantity_toggle'] = $price['quantity_toggle']; } // Quantity label. if ( isset( $price['quantity_label'] ) && ! empty( $price['quantity_label'] ) ) { $price_args['quantity_label'] = $price['quantity_label']; } // Quantity minimum. if ( isset( $price['quantity_minimum'] ) && ! empty( $price['quantity_minimum'] ) ) { $price_args['quantity_minimum'] = $price['quantity_minimum']; } // Quantity maximum. if ( isset( $price['quantity_maximum'] ) && ! empty( $price['quantity_maximum'] ) ) { $price_args['quantity_maximum'] = $price['quantity_maximum']; } // Recurring amount toggle label. if ( isset( $price['recurring_amount_toggle_label'] ) && ! empty( $price['recurring_amount_toggle_label'] ) ) { $price_args['recurring_amount_toggle_label'] = $price['recurring_amount_toggle_label']; } if ( isset( $price['can_recur_selected_by_default'] ) && ! empty( $price['can_recur_selected_by_default'] ) ) { $price_args['can_recur_selected_by_default'] = $price['can_recur_selected_by_default']; } $_prices[ $instance_id ] = $price_args; } update_post_meta( $post_id, $prices_key, wp_slash( $_prices ) ); update_post_meta( $post_id, sprintf( '%s_modified', $prices_key ), time() ); } add_action( 'simpay_save_form_settings', __NAMESPACE__ . '\\save_prices', 30, 3 ); /** * Redirects the Payment Form back to the current Payment Form settings tab. * * @since 3.8.0 * * @param string $location Location to redirect to. * @param int $post_id Payment Form ID. * @return string URL to redirect to. */ function save_redirect( $location, $post_id ) { $post = get_post( $post_id ); if ( 'simple-pay' !== $post->post_type ) { return $location; } $hash = isset( $_REQUEST['simpay_form_settings_tab'] ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended ? sanitize_text_field( wp_unslash( $_REQUEST['simpay_form_settings_tab'] ) ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended : '#form-display-options-settings-panel'; $location .= $hash; return $location; } add_filter( 'redirect_post_location', __NAMESPACE__ . '\\save_redirect', 10, 2 ); /** * Duplicates an existing Payment Form. * * @since 3.8.0 */ function duplicate() { // Bail if no post is found. if ( ! isset( $_REQUEST['form'] ) ) { return; } // Bail if no action is found, or not duplicating. if ( ! isset( $_REQUEST['simpay-action'] ) || 'duplicate' !== $_REQUEST['simpay-action'] ) { return; } $form_id = absint( $_REQUEST['form'] ); // Bail if the request is invalid. The nonce is tied to the form ID so it // cannot be reused to duplicate a different post. check_admin_referer( 'simpay-duplicate-payment-form_' . $form_id ); $post = get_post( $form_id ); // Bail if post cannot be found, or is not a payment form. if ( empty( $post ) || 'simple-pay' !== $post->post_type ) { return; } // Bail if the current user cannot edit the payment form being duplicated. if ( ! current_user_can( 'edit_post', $post->ID ) ) { wp_die( esc_html__( 'Sorry, you are not allowed to duplicate this payment form.', 'stripe' ), '', array( 'response' => 403, ) ); } // Allow empty post. add_filter( 'wp_insert_post_empty_content', '__return_false' ); // Insert the new post using the original post values. $duplicate = wp_insert_post( array( 'post_author' => wp_get_current_user()->ID, 'post_type' => $post->post_type, 'post_status' => $post->post_status, ) ); // If the new post did not get inserted then exit now. if ( ! $duplicate ) { return; } // Duplicate metadata. global $wpdb; $wpdb->query( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $wpdb->prepare( "INSERT INTO {$wpdb->prefix}postmeta (post_id, meta_key, meta_value) SELECT %d, meta_key, meta_value FROM {$wpdb->prefix}postmeta WHERE post_id = %d", $duplicate, $post->ID ) ); // Remove the linked container Product so a fresh Stripe Product is created // on first save for the duplicate. delete_post_meta( $duplicate, '_simpay_product_live' ); delete_post_meta( $duplicate, '_simpay_product_test' ); // Clear only the Stripe Price IDs from each price entry, preserving the // price configuration (label, amount, currency, recurring interval, etc.). // New Stripe Prices will be created under the new Product on first save. foreach ( array( '_simpay_prices_live', '_simpay_prices_test' ) as $prices_key ) { $prices = get_post_meta( $duplicate, $prices_key, true ); if ( empty( $prices ) || ! is_array( $prices ) ) { continue; } foreach ( $prices as $price_uuid => $price ) { if ( ! is_array( $price ) ) { continue; } // Drop the Stripe Price ID so a new one is created on save. unset( $price['id'] ); // Drop the Stripe Price ID for the linked recurring price (if any). if ( isset( $price['recurring'] ) && is_array( $price['recurring'] ) ) { unset( $price['recurring']['id'] ); } $prices[ $price_uuid ] = $price; } update_post_meta( $duplicate, $prices_key, $prices ); } // Delete the prices "modified" timestamps so sync re-creates Stripe Prices // on the next save of the duplicate. delete_post_meta( $duplicate, '_simpay_prices_live_modified' ); delete_post_meta( $duplicate, '_simpay_prices_test_modified' ); // Update form title to append - Duplicate. $form_name = get_post_meta( $duplicate, '_company_name', true ); update_post_meta( $duplicate, '_company_name', /* translators: %s Payment form name. */ sprintf( __( '%s - Duplicate', 'stripe' ), $form_name ) ); $redirect = add_query_arg( array( 'post' => $duplicate, 'action' => 'edit', 'message' => 299, ), admin_url( 'post.php' ) ); wp_safe_redirect( $redirect ); } add_action( 'admin_init', __NAMESPACE__ . '\\duplicate' ); /** * Saves a submitted Payment Page or Confirmation Page background color. * * The `$_POST` key matches the meta key. A missing, empty, malformed or * non-string value keeps the saved color (`sanitize_hex_color()` returns * `null` or an empty string for those). The `#428bca` default is stored only * when no color has been saved yet, because the page output reads the meta * without a fallback. * * @since 4.17.5 * * @param int $post_id Payment Form ID. * @param string $meta_key Meta key, and `$_POST` key, of the color. * @return void */ function save_background_color_setting( $post_id, $meta_key ) { $background_color = isset( $_POST[ $meta_key ] ) && is_string( $_POST[ $meta_key ] ) // phpcs:ignore WordPress.Security.NonceVerification.Missing ? sanitize_hex_color( wp_unslash( $_POST[ $meta_key ] ) ) // phpcs:ignore WordPress.Security.NonceVerification.Missing : null; if ( empty( $background_color ) ) { if ( metadata_exists( 'post', $post_id, $meta_key ) ) { return; } $background_color = '#428bca'; } update_post_meta( $post_id, $meta_key, $background_color ); } /** * Saves the Payment Form's Confirmation Page settings. * * @since 4.5.0 * * @param int $post_id Payment Form ID. * @param \WP_Post $post Payment Form \WP_Post object. * @param \SimplePay\Core\Abstracts\Form $form Payment Form. */ function save_confirmation_page( $post_id, $post, $form ) { // Import customization config from payment page. $enable_confirmation_page = isset( $_POST['_confirmation_page_use_payment_page_config'] ) // phpcs:ignore WordPress.Security.NonceVerification.Missing ? 'yes' : 'no'; update_post_meta( $post_id, '_confirmation_page_use_payment_page_config', $enable_confirmation_page ); // Show Title/Description. $confirmation_page_title_description = isset( $_POST['_confirmation_page_title_description'] ) // phpcs:ignore WordPress.Security.NonceVerification.Missing ? 'yes' : 'no'; update_post_meta( $post_id, '_confirmation_page_title_description', $confirmation_page_title_description ); // Color scheme. save_background_color_setting( $post_id, '_confirmation_page_background_color' ); // Footer text. $footer_text = isset( $_POST['_confirmation_page_footer_text'] ) // phpcs:ignore WordPress.Security.NonceVerification.Missing ? sanitize_text_field( wp_unslash( $_POST['_confirmation_page_footer_text'] ) ) // phpcs:ignore WordPress.Security.NonceVerification.Missing : ''; update_post_meta( $post_id, '_confirmation_page_footer_text', // `update_post_meta()` unslashes its value, so re-slash the already // unslashed text to keep literal backslashes. wp_slash( $footer_text ) ); // Powered by. $confirmation_page_powered_by = isset( $_POST['_confirmation_page_powered_by'] ) // phpcs:ignore WordPress.Security.NonceVerification.Missing ? 'yes' : 'no'; update_post_meta( $post_id, '_confirmation_page_powered_by', $confirmation_page_powered_by ); // Image URL. // `esc_url_raw()` (not `esc_url()`) since the value is stored, not output. $confirmation_page_image = isset( $_POST['_confirmation_page_image_url'] ) && is_string( $_POST['_confirmation_page_image_url'] ) // phpcs:ignore WordPress.Security.NonceVerification.Missing ? esc_url_raw( wp_unslash( $_POST['_confirmation_page_image_url'] ) ) // phpcs:ignore WordPress.Security.NonceVerification.Missing : ''; update_post_meta( $post_id, '_confirmation_page_image_url', wp_slash( $confirmation_page_image ) ); // Self confirmation. $confirmation_page_self_confirmation = isset( $_POST['_confirmation_page_self_confirmation'] ) // phpcs:ignore WordPress.Security.NonceVerification.Missing ? 'yes' : 'no'; update_post_meta( $post_id, '_confirmation_page_self_confirmation', $confirmation_page_self_confirmation ); } add_action( 'simpay_save_form_settings', __NAMESPACE__ . '\\save_confirmation_page', 50, 3 );
Save File
Cancel