diff --git a/.wp-env.json b/.wp-env.json new file mode 100644 index 0000000..72f5948 --- /dev/null +++ b/.wp-env.json @@ -0,0 +1,13 @@ +{ + "core": null, + "plugins": [ + ".", + "../../../", + "https://downloads.wordpress.org/plugin/formidable.5.4.4.zip", + "https://downloads.wordpress.org/plugin/query-monitor.zip", + "https://downloads.wordpress.org/plugin/one-time-login.zip" + ], + "config": { + "PRONAMIC_PAY_DEBUG": true + } +} diff --git a/CHANGELOG.md b/CHANGELOG.md index ecc5ab0..3ccd46c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ This projects adheres to [Semantic Versioning](http://semver.org/) and [Keep a C ## [Unreleased][unreleased] - +## [4.2.0] - 2022-09-26 +- Updated for new payment methods and fields registration. + ## [4.1.1] - 2022-08-15 - Fixed not showing error messages if form success action is not 'message'. @@ -78,7 +81,8 @@ This projects adheres to [Semantic Versioning](http://semver.org/) and [Keep a C ## 1.0.0 - 2015-11-05 - First release. -[unreleased]: https://github.com/pronamic/wp-pronamic-pay-formidable-forms/compare/4.1.1...HEAD +[unreleased]: https://github.com/pronamic/wp-pronamic-pay-formidable-forms/compare/4.2.0...HEAD +[4.2.0]: https://github.com/pronamic/wp-pronamic-pay-formidable-forms/compare/4.1.1...4.2.0 [4.1.1]: https://github.com/pronamic/wp-pronamic-pay-formidable-forms/compare/4.1.0...4.1.1 [4.1.0]: https://github.com/wp-pay-extensions/formidable-forms/compare/4.0.0...4.1.0 [4.0.0]: https://github.com/wp-pay-extensions/formidable-forms/compare/3.0.0...4.0.0 diff --git a/composer.json b/composer.json index 6794715..243431f 100644 --- a/composer.json +++ b/composer.json @@ -60,7 +60,7 @@ }, "scripts": { "coveralls": "vendor/bin/php-coveralls -v", - "phpcbf": "vendor/bin/phpcbf", + "phpcbf": "XDEBUG_MODE=off vendor/bin/phpcbf", "phpcs": "XDEBUG_MODE=off vendor/bin/phpcs -s -v", "phplint": "find src tests -name '*.php' | xargs -n 1 -P 4 php -l", "phpmd": "vendor/bin/phpmd src,tests text phpmd.ruleset.xml --suffixes php", diff --git a/package.json b/package.json index f041fc0..c4b9658 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "formidable-forms", - "version": "4.1.1", + "version": "4.2.0", "description": "Formidable Forms driver for the WordPress payment processing library.", "repository": { "type": "git", @@ -33,6 +33,7 @@ }, "homepage": "http://www.wp-pay.org/extensions/formidable-forms/", "devDependencies": { + "@wordpress/env": "^5.2.0", "autoprefixer": "^10.2.1", "grunt": "^1.3.0", "grunt-contrib-compass": "^1.1.1", @@ -46,5 +47,9 @@ "grunt-postcss": "^0.9.0", "grunt-sass-lint": "^0.2.4", "load-grunt-tasks": "^5.1.0" + }, + "scripts": { + "start": "wp-env start", + "login": "wp-env run cli wp user one-time-login admin" } } diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 6a42d95..8d37295 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -6,6 +6,6 @@ parameters: bootstrapFiles: - tests/phpstan/bootstrap.php scanDirectories: - - wp-content/formidable + - wp-content/plugins/formidable paths: - src/ diff --git a/pronamic-pay-formidable-forms.php b/pronamic-pay-formidable-forms.php new file mode 100644 index 0000000..4670801 --- /dev/null +++ b/pronamic-pay-formidable-forms.php @@ -0,0 +1,41 @@ + + * @copyright 2005-2022 Pronamic + * @license GPL-3.0-or-later + * @package Pronamic\WordPress\Pay\Extensions\FormidableForms + */ + +add_filter( + 'pronamic_pay_plugin_integrations', + function ( $integrations ) { + foreach ( $integrations as $integration ) { + if ( $integration instanceof \Pronamic\WordPress\Pay\Extensions\FormidableForms\Extension ) { + return $integrations; + } + } + + $integrations[] = new \Pronamic\WordPress\Pay\Extensions\FormidableForms\Extension(); + + return $integrations; + } +); diff --git a/src/BankSelectFieldType.php b/src/BankSelectFieldType.php index 1b62b6e..3732f4d 100644 --- a/src/BankSelectFieldType.php +++ b/src/BankSelectFieldType.php @@ -3,6 +3,7 @@ namespace Pronamic\WordPress\Pay\Extensions\FormidableForms; use Pronamic\WordPress\Pay\Core\PaymentMethods; +use Pronamic\WordPress\Pay\Fields\IDealIssuerSelectField; use Pronamic\WordPress\Pay\Plugin; use Pronamic\WordPress\Pay\Util; @@ -31,16 +32,16 @@ class BankSelectFieldType { */ public function __construct() { // @link https://github.com/wp-premium/formidable/blob/2.0.21/classes/models/FrmField.php#L10-L23 - add_filter( 'frm_available_fields', array( $this, 'available_fields' ) ); + add_filter( 'frm_available_fields', [ $this, 'available_fields' ] ); // @link https://github.com/wp-premium/formidable/blob/2.0.21/classes/controllers/FrmFieldsController.php#L74 - add_filter( 'frm_before_field_created', array( $this, 'before_field_created' ) ); + add_filter( 'frm_before_field_created', [ $this, 'before_field_created' ] ); // @link https://github.com/wp-premium/formidable/blob/2.0.21/classes/views/frm-fields/show-build.php#L64 - add_action( 'frm_display_added_fields', array( $this, 'display_added_fields' ) ); + add_action( 'frm_display_added_fields', [ $this, 'display_added_fields' ] ); // @link https://github.com/wp-premium/formidable/blob/2.0.21/classes/views/frm-fields/input.php#L171 - add_action( 'frm_form_fields', array( $this, 'form_fields' ) ); + add_action( 'frm_form_fields', [ $this, 'form_fields' ] ); } /** @@ -127,25 +128,23 @@ private function render_field( $field ) { return; } - // Always use iDEAL payment method for issuer field. - $payment_method = $gateway->get_payment_method(); + $issuer_field = $gateway->first_payment_method_field( PaymentMethods::IDEAL, IDealIssuerSelectField::class ); - $gateway->set_payment_method( PaymentMethods::IDEAL ); + if ( null === $issuer_field ) { + return; + } try { - $issuer_field = $gateway->get_issuer_field(); - - $choices = $issuer_field['choices']; - $options = Util::select_options_grouped( $choices ); - printf( ''; } catch ( \Exception $e ) { @@ -155,8 +154,5 @@ private function render_field( $field ) { esc_html( $e->getMessage() ) ); } - - // Reset payment method to original value. - $gateway->set_payment_method( $payment_method ); } } diff --git a/src/Extension.php b/src/Extension.php index 697e163..eb6cf70 100644 --- a/src/Extension.php +++ b/src/Extension.php @@ -50,9 +50,9 @@ class Extension extends AbstractPluginIntegration { */ public function __construct() { parent::__construct( - array( + [ 'name' => __( 'Formidable Forms', 'pronamic_ideal' ), - ) + ] ); // Dependencies. @@ -67,33 +67,33 @@ public function __construct() { * @return void */ public function setup() { - add_filter( 'pronamic_payment_source_text_' . self::SLUG, array( $this, 'source_text' ), 10, 2 ); - add_filter( 'pronamic_payment_source_description_' . self::SLUG, array( $this, 'source_description' ), 10, 2 ); + add_filter( 'pronamic_payment_source_text_' . self::SLUG, [ $this, 'source_text' ], 10, 2 ); + add_filter( 'pronamic_payment_source_description_' . self::SLUG, [ $this, 'source_description' ], 10, 2 ); // Check if dependencies are met and integration is active. if ( ! $this->is_active() ) { return; } - add_action( 'pronamic_payment_status_update_' . self::SLUG, array( $this, 'update_status' ), 10, 2 ); - add_filter( 'pronamic_payment_source_url_' . self::SLUG, array( $this, 'source_url' ), 10, 2 ); + add_action( 'pronamic_payment_status_update_' . self::SLUG, [ $this, 'update_status' ], 10, 2 ); + add_filter( 'pronamic_payment_source_url_' . self::SLUG, [ $this, 'source_url' ], 10, 2 ); - add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); + add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ] ); // @link https://github.com/wp-premium/formidable/blob/2.0.21/classes/controllers/FrmFormActionsController.php#L39-L57 // @link https://github.com/wp-premium/formidable-paypal/blob/3.02/controllers/FrmPaymentSettingsController.php#L11 - add_action( 'frm_registered_form_actions', array( $this, 'registered_form_actions' ) ); + add_action( 'frm_registered_form_actions', [ $this, 'registered_form_actions' ] ); // @link https://github.com/wp-premium/formidable/blob/2.0.21/classes/controllers/FrmFormActionsController.php#L299-L308 // @link https://github.com/wp-premium/formidable-paypal/blob/3.02/controllers/FrmPaymentsController.php#L28-L29 - add_action( 'frm_trigger_pronamic_pay_create_action', array( $this, 'create_action' ), 10, 3 ); + add_action( 'frm_trigger_pronamic_pay_create_action', [ $this, 'create_action' ], 10, 3 ); // @link https://github.com/wp-premium/formidable-paypal/blob/3.06/controllers/FrmPaymentSettingsController.php#L15-L19 - add_filter( 'frm_action_triggers', array( $this, 'add_payment_trigger' ) ); - add_filter( 'frm_email_action_options', array( $this, 'add_trigger_to_action' ) ); - add_filter( 'frm_twilio_action_options', array( $this, 'add_trigger_to_action' ) ); - add_filter( 'frm_mailchimp_action_options', array( $this, 'add_trigger_to_action' ) ); - add_filter( 'frm_register_action_options', array( $this, 'add_payment_trigger_to_register_user_action' ) ); + add_filter( 'frm_action_triggers', [ $this, 'add_payment_trigger' ] ); + add_filter( 'frm_email_action_options', [ $this, 'add_trigger_to_action' ] ); + add_filter( 'frm_twilio_action_options', [ $this, 'add_trigger_to_action' ] ); + add_filter( 'frm_mailchimp_action_options', [ $this, 'add_trigger_to_action' ] ); + add_filter( 'frm_register_action_options', [ $this, 'add_payment_trigger_to_register_user_action' ] ); // Field types. $this->field_type_bank_select = new BankSelectFieldType(); @@ -121,14 +121,14 @@ public function admin_enqueue_scripts() { wp_register_style( 'pronamic-pay-formidable-forms', plugins_url( 'css/admin' . $min . '.css', dirname( __FILE__ ) ), - array(), + [], '1.0.0' ); wp_register_script( 'pronamic-pay-formidable-forms', plugins_url( 'js/admin' . $min . '.js', dirname( __FILE__ ) ), - array( 'jquery' ), + [ 'jquery' ], '1.0.0', true ); @@ -246,11 +246,11 @@ public static function source_text( $text, Payment $payment ) { $text .= sprintf( '%s', add_query_arg( - array( + [ 'page' => 'formidable-entries', 'frm_action' => 'show', 'id' => $payment->get_source_id(), - ), + ], admin_url( 'admin.php' ) ), sprintf( @@ -285,11 +285,11 @@ public function source_description( $description, Payment $payment ) { */ public function source_url( $url, Payment $payment ) { $url = add_query_arg( - array( + [ 'page' => 'formidable-entries', 'frm_action' => 'show', 'id' => $payment->get_source_id(), - ), + ], admin_url( 'admin.php' ) ); @@ -332,13 +332,13 @@ public function create_action( $action, $entry, $form ) { // @link https://github.com/wp-premium/formidable-paypal/blob/3.02/controllers/FrmPaymentsController.php#L268-L269 // @link https://github.com/wp-premium/formidable/blob/2.0.21/classes/models/FrmEntry.php#L698-L711 - add_action( 'frm_after_create_entry', array( $this, 'redirect_for_payment' ), 50, 2 ); + add_action( 'frm_after_create_entry', [ $this, 'redirect_for_payment' ], 50, 2 ); // Delay notifications. if ( ! self::$send_email_now && isset( $action->post_content['pronamic_pay_delay_notifications'] ) && 'on' === $action->post_content['pronamic_pay_delay_notifications'] ) { remove_action( 'frm_trigger_email_action', 'FrmNotification::trigger_email', 10 ); add_filter( 'frm_to_email', '__return_empty_array', 20 ); - add_filter( 'frm_send_new_user_notification', array( __CLASS__, 'stop_registration_email' ), 10, 3 ); + add_filter( 'frm_send_new_user_notification', [ __CLASS__, 'stop_registration_email' ], 10, 3 ); } } @@ -410,11 +410,6 @@ public function redirect_for_payment( $entry_id, $form_id ) { $payment->set_payment_method( PaymentMethods::IDEAL ); } - // Check if gateway requires payment method. - if ( empty( $payment_method ) && $gateway->payment_method_is_required() ) { - $payment->set_payment_method( PaymentMethods::IDEAL ); - } - // Issuer. $payment->set_meta( 'issuer', FormidableFormsHelper::get_issuer_from_form_entry( $form_id, $entry ) ); @@ -433,9 +428,9 @@ public function redirect_for_payment( $entry_id, $form_id ) { if ( wp_doing_ajax() ) { // Do not use `wp_send_json_success()` as Formidable Forms doesn't properly handle the content type. echo wp_json_encode( - array( + [ 'redirect' => $payment->get_pay_redirect_url(), - ) + ] ); exit; @@ -462,7 +457,7 @@ function( $method, $form ) { add_filter( 'frm_main_feedback', function ( $message, $form, $entry_id ) use ( $e ) { - $atts = array( + $atts = [ 'class' => FrmFormsHelper::form_error_class(), 'form' => $form, 'entry_id' => $entry_id, @@ -471,7 +466,7 @@ function ( $message, $form, $entry_id ) use ( $e ) { \esc_html( Plugin::get_default_error_message() ), \esc_html( sprintf( '%s: %s', $e->getCode(), $e->getMessage() ) ) ), - ); + ]; return FrmFormsHelper::get_success_message( $atts ); }, diff --git a/src/FormidableFormsHelper.php b/src/FormidableFormsHelper.php index 3ad50e1..190649d 100644 --- a/src/FormidableFormsHelper.php +++ b/src/FormidableFormsHelper.php @@ -114,10 +114,10 @@ public static function get_payment_method_from_action_entry( $action, $entry ) { if ( ! empty( $payment_method_field ) && isset( $entry->metas[ $payment_method_field ] ) ) { $payment_method = $entry->metas[ $payment_method_field ]; - $replacements = array( + $replacements = [ 'pronamic_pay_' => '', 'pronamic_pay' => '', - ); + ]; $payment_method = strtr( $payment_method, $replacements ); @@ -171,7 +171,7 @@ public static function get_amount_from_field( $action, $entry ) { $values = $entry->metas[ $amount_field ]; if ( ! \is_array( $values ) ) { - $values = array( $values ); + $values = [ $values ]; } // Add values to amount. diff --git a/src/PaymentAction.php b/src/PaymentAction.php index ed0aa6b..ddec932 100644 --- a/src/PaymentAction.php +++ b/src/PaymentAction.php @@ -33,15 +33,15 @@ public function __construct() { parent::__construct( self::SLUG, __( 'Pronamic Pay', 'pronamic_ideal' ), - array( + [ // @link https://github.com/wp-premium/formidable/blob/2.0.21/classes/views/frm-form-actions/form_action.php#L14 'classes' => 'pronamic-pay-formidable-icon frm-inverse', 'color' => '#ffb200', 'active' => true, - 'event' => array( 'create' ), + 'event' => [ 'create' ], 'priority' => 9, // trigger before emails are sent so they can be stopped. 'limit' => 99, - ) + ] ); } @@ -54,7 +54,7 @@ public function __construct() { * @param array $instance Current settings. * @param array $args Arguments. */ - public function form( $instance, $args = array() ) { + public function form( $instance, $args = [] ) { $form_fields = $this->get_field_options( $args['form']->id ); include \dirname( __FILE__ ) . '/../views/payment-settings.php'; @@ -71,10 +71,10 @@ public function form( $instance, $args = array() ) { */ private function get_field_options( $form_id ) { $form_fields = FrmField::getAll( - array( + [ 'fi.form_id' => \absint( $form_id ), - 'fi.type not' => array( 'divider', 'end_divider', 'html', 'break', 'captcha', 'rte', 'form' ), - ), + 'fi.type not' => [ 'divider', 'end_divider', 'html', 'break', 'captcha', 'rte', 'form' ], + ], 'field_order' ); diff --git a/src/PaymentMethodSelectFieldType.php b/src/PaymentMethodSelectFieldType.php index 5feaa87..1511394 100644 --- a/src/PaymentMethodSelectFieldType.php +++ b/src/PaymentMethodSelectFieldType.php @@ -43,31 +43,31 @@ class PaymentMethodSelectFieldType { */ public function __construct() { // @link https://github.com/wp-premium/formidable/blob/2.0.21/classes/models/FrmField.php#L10-L23 - add_filter( 'frm_available_fields', array( $this, 'available_fields' ) ); + add_filter( 'frm_available_fields', [ $this, 'available_fields' ] ); // @link https://github.com/wp-premium/formidable/blob/2.0.21/classes/controllers/FrmFieldsController.php#L74 - add_filter( 'frm_before_field_created', array( $this, 'before_field_created' ) ); + add_filter( 'frm_before_field_created', [ $this, 'before_field_created' ] ); // @link https://formidableforms.com/knowledgebase/add-a-new-field/#kb-save-field-options - add_filter( 'frm_update_field_options', array( $this, 'update_field_options' ), 10, 3 ); + add_filter( 'frm_update_field_options', [ $this, 'update_field_options' ], 10, 3 ); // @link https://formidableforms.com/knowledgebase/frm_setup_edit_fields_vars/ - add_filter( 'frm_setup_edit_fields_vars', array( $this, 'edit_fields_vars' ), 10, 1 ); + add_filter( 'frm_setup_edit_fields_vars', [ $this, 'edit_fields_vars' ], 10, 1 ); - add_filter( 'frm_switch_field_types', array( $this, 'switch_field_types' ), 10, 2 ); + add_filter( 'frm_switch_field_types', [ $this, 'switch_field_types' ], 10, 2 ); // @link https://github.com/wp-premium/formidable/blob/2.0.21/classes/views/frm-fields/show-build.php#L64 - add_action( 'frm_display_added_fields', array( $this, 'display_added_fields' ) ); + add_action( 'frm_display_added_fields', [ $this, 'display_added_fields' ] ); // @link https://github.com/wp-premium/formidable/blob/2.0.21/classes/views/frm-fields/input.php#L171 - add_action( 'frm_form_fields', array( $this, 'form_fields' ) ); + add_action( 'frm_form_fields', [ $this, 'form_fields' ] ); // @link https://formidableforms.com/knowledgebase/add-a-new-field/#kb-modify-value-displayed-when-viewing-entry - add_filter( 'frm_display_' . self::ID . '_value_custom', array( $this, 'display_field_value' ) ); + add_filter( 'frm_display_' . self::ID . '_value_custom', [ $this, 'display_field_value' ] ); - add_filter( 'frm_bulk_field_choices', array( $this, 'bulk_field_choices' ) ); + add_filter( 'frm_bulk_field_choices', [ $this, 'bulk_field_choices' ] ); - add_action( 'wp_ajax_frm_import_options', array( $this, 'import_options' ), 9 ); + add_action( 'wp_ajax_frm_import_options', [ $this, 'import_options' ], 9 ); } /** @@ -91,10 +91,10 @@ public function available_fields( $fields ) { } // Add icon in Formidable Forms 3.0+. - $fields[ self::ID ] = array( + $fields[ self::ID ] = [ 'name' => __( 'Payment Method', 'pronamic_ideal' ), 'icon' => 'frm_icon_font ' . $icon, - ); + ]; } return $fields; @@ -114,9 +114,9 @@ public function before_field_created( $field_data ) { if ( self::ID === $field_data['type'] ) { $field_data['name'] = __( 'Choose a payment method', 'pronamic_ideal' ); - $defaults = array( + $defaults = [ 'separate_value' => 1, - ); + ]; $field_data['field_options'] = array_merge( $field_data['field_options'], $defaults ); } @@ -168,12 +168,12 @@ public function switch_field_types( $field_types, $type ) { $this->in_field_options = false; - return array( - self::ID => array( + return [ + self::ID => [ 'name' => __( 'Payment Methods', 'pronamic_ideal' ), 'icon' => 'frm_icon_font frm_', - ), - ); + ], + ]; } /** @@ -299,7 +299,7 @@ public function display_field_value( $value ) { * @return array */ public function bulk_field_choices( $choices ) { - $methods = array(); + $methods = []; $payment_methods = $this->get_payment_methods(); @@ -357,10 +357,10 @@ public function import_options() { $value = trim( $values[1] ); if ( $label !== $value ) { - $options[ $option_key ] = array( + $options[ $option_key ] = [ 'label' => $label, 'value' => $value, - ); + ]; } } @@ -375,40 +375,35 @@ public function import_options() { * Get payment method options. */ public function get_payment_methods() { - $payment_methods = array(); - $config_id = get_option( 'pronamic_pay_config_id' ); $gateway = Plugin::get_gateway( $config_id ); - if ( ! $gateway ) { - return $payment_methods; + if ( null === $gateway ) { + return []; } try { - $options = $gateway->get_payment_method_field_options(); - } catch ( \Exception $e ) { - return $payment_methods; - } + $payment_methods = $gateway->get_payment_methods( + [ + 'status' => [ '', 'active' ], + ] + ); - foreach ( $options as $payment_method => $name ) { - $value = 'pronamic_pay'; + $result = []; - if ( ! empty( $payment_method ) ) { - $value = sprintf( 'pronamic_pay_%s', $payment_method ); - } + foreach ( $payment_methods as $payment_method ) { + $value = sprintf( 'pronamic_pay_%s', $payment_method->get_id() ); - // Ignore unsupported recurring-only payment methods. - if ( \in_array( $payment_method, array_keys( PaymentMethods::get_direct_debit_methods() ), true ) ) { - continue; + $result[] = [ + 'label' => $payment_method->get_name(), + 'value' => $value, + ]; } - $payment_methods[] = array( - 'label' => $name, - 'value' => $value, - ); + return $result; + } catch ( \Exception $e ) { + return []; } - - return $payment_methods; } } diff --git a/views/payment-settings.php b/views/payment-settings.php index 281a3b4..18f3667 100644 --- a/views/payment-settings.php +++ b/views/payment-settings.php @@ -32,9 +32,9 @@ esc_attr( $this->get_field_name( 'pronamic_pay_amount_field' ) ) ); - $options = array( + $options = [ '' => __( '— Select Field —', 'pronamic_ideal' ), - ); + ]; foreach ( $form_fields as $field ) { $options[ $field->id ] = FrmAppHelper::truncate( $field->name, 50, 1 ); @@ -68,9 +68,9 @@ esc_attr( $this->get_field_name( 'pronamic_pay_payment_method_field' ) ) ); - $options = array( + $options = [ '' => __( '— Select Field —', 'pronamic_ideal' ), - ); + ]; foreach ( $form_fields as $field ) { if ( PaymentMethodSelectFieldType::ID !== $field->type ) {