Skip to content

Commit e666c6f

Browse files
Improve plugin code standards (#118)
Co-authored-by: Tigran <[email protected]>
1 parent 457b571 commit e666c6f

31 files changed

+1235
-530
lines changed

README.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
Contributors: mondu-ai, arthurmmoreira, tikohov20
33
Tags: mondu, woocommerce, e-commerce, ecommerce, store, sales, sell, woo, woo commerce, shop, cart, shopping cart, sell online, checkout, payment, payments, bnpl, b2b
44
Requires at least: 5.9.0
5-
Tested up to: 6.2.2
6-
Stable tag: 2.1.7
5+
Tested up to: 6.5.3
6+
Stable tag: 3.0.0
77
Requires PHP: 7.4
88
License: GPLv3
99
License URI: https://www.gnu.org/licenses/gpl-3.0.html

assets/src/js/mondublocks.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
(function() {
2-
const { __ } = wp.i18n;
3-
const { createElement } = window.wp.element;
4-
const { decodeEntities } = window.wp.htmlEntities;
5-
const { registerPaymentMethod } = window.wc.wcBlocksRegistry;
1+
(function () {
2+
const { __ } = wp.i18n;
3+
const { createElement } = window.wp.element;
4+
const { decodeEntities } = window.wp.htmlEntities;
5+
const { registerPaymentMethod } = window.wc.wcBlocksRegistry;
66
const { registerPaymentMethodExtensionCallbacks } = window.wc.wcBlocksRegistry;
7-
const settings = window.wc.wcSettings.getSetting( 'mondu_blocks_data', {} );
7+
const settings = window.wc.wcSettings.getSetting( 'mondu_blocks_data', {} );
88

99
settings.available_countries = settings.available_countries || [];
10-
settings.gateways = settings.gateways || {};
10+
settings.gateways = settings.gateways || {};
1111

1212
// This is not used anywhere, it's added for automatic translation generation with wp cli command
1313
const translations = [
@@ -56,7 +56,7 @@
5656

5757
registerPaymentMethodExtensionCallbacks('mondu',
5858
Object.keys(settings.gateways).reduce((previousValue, currentValue) => {
59-
previousValue[currentValue] = function(arg) {
59+
previousValue[currentValue] = function (arg) {
6060
return settings.available_countries.includes(arg.billingAddress.country)
6161
}
6262

mondu-buy-now-pay-later.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Plugin Name: Mondu Buy Now Pay Later
44
* Plugin URI: https://github.com/mondu-ai/bnpl-checkout-woocommerce/releases
55
* Description: Mondu provides B2B E-commerce and B2B marketplaces with an online payment solution to buy now and pay later.
6-
* Version: 2.2.1
6+
* Version: 3.0.0
77
* Author: Mondu
88
* Author URI: https://mondu.ai
99
*
@@ -27,7 +27,7 @@
2727
die( 'Direct access not allowed' );
2828
}
2929

30-
define( 'MONDU_PLUGIN_VERSION', '2.2.1' );
30+
define( 'MONDU_PLUGIN_VERSION', '3.0.0' );
3131
define( 'MONDU_PLUGIN_FILE', __FILE__ );
3232
define( 'MONDU_PLUGIN_PATH', __DIR__ );
3333
define( 'MONDU_PLUGIN_BASENAME', plugin_basename(MONDU_PLUGIN_FILE) );
@@ -46,7 +46,7 @@ function mondu_env( $name, $default_value ) {
4646
mondu_env( 'MONDU_WEBHOOKS_URL', get_home_url() );
4747

4848
require_once 'src/autoload.php';
49-
add_action('plugins_loaded', [ new \Mondu\Plugin(), 'init' ]);
49+
add_action( 'plugins_loaded', [ new \Mondu\Plugin(), 'init' ] );
5050

5151
function mondu_activate() {
5252
}
@@ -63,7 +63,7 @@ function mondu_deactivate() {
6363
register_deactivation_hook( MONDU_PLUGIN_FILE, 'mondu_deactivate' );
6464

6565
// Here because this needs to happen before plugins_loaded hook
66-
add_action('before_woocommerce_init', function() {
66+
add_action('before_woocommerce_init', function () {
6767
if ( class_exists( FeaturesUtil::class ) ) {
6868
FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__ );
6969
FeaturesUtil::declare_compatibility( 'cart_checkout_blocks', __FILE__ );

pbs-rules-set.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,15 @@
2525
<exclude name="WordPress.Security.ValidatedSanitizedInput.MissingUnslash" />
2626
<exclude name="WordPress.WP.I18n.NonSingularStringLiteralText" />
2727
<exclude name="WordPress.WhiteSpace.OperatorSpacing.NoSpaceAfter" />
28+
<exclude name="Generic.Arrays.DisallowShortArraySyntax"/>
29+
<exclude name="Generic.Commenting.DocComment.MissingShort"/>
30+
<exclude name="Universal.Arrays.DisallowShortArraySyntax"/>
31+
<exclude name="WordPress.Security.EscapeOutput.ExceptionNotEscaped"/>
2832
</rule>
2933

3034
<rule ref="WooCommerce-Core">
3135
<exclude name="Core.Commenting.CommentTags.AuthorTag" />
3236
<exclude name="WordPress.PHP.DontExtract" />
33-
<exclude name="Generic.Arrays.DisallowShortArraySyntax" />
3437
<exclude name="Generic.WhiteSpace.ScopeIndent.Incorrect" />
3538
</rule>
3639

@@ -46,6 +49,8 @@
4649
</properties>
4750
</rule>
4851

52+
<rule ref="Generic.Arrays.DisallowLongArraySyntax"/>
53+
4954
<rule ref="PHPCompatibility">
5055
<exclude-pattern>tests/</exclude-pattern>
5156
</rule>

src/Mondu/Admin/Option/Account.php

Lines changed: 58 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
<?php
2+
/**
3+
* Account options
4+
*
5+
* @package Mondu
6+
*/
27

38
namespace Mondu\Admin\Option;
49

@@ -8,77 +13,106 @@
813
die( 'Direct access not allowed' );
914
}
1015

16+
/**
17+
* Class Account
18+
*/
1119
class Account extends Helper {
20+
/**
21+
* Register the settings
22+
*/
1223
public function register() {
1324
register_setting('mondu', Plugin::OPTION_NAME);
1425

15-
/*
26+
/**
1627
* General Settings
1728
*/
1829
add_settings_section(
1930
'mondu_account_settings_general',
20-
__('Settings', 'woocommerce'),
31+
__( 'Settings', 'woocommerce' ),
2132
[],
2233
'mondu-settings-account'
2334
);
2435
add_settings_field(
2536
'sandbox_or_production',
26-
__('Sandbox or production', 'mondu'),
37+
__( 'Sandbox or production', 'mondu' ),
2738
[ $this, 'field_sandbox_or_production' ],
2839
'mondu-settings-account',
2940
'mondu_account_settings_general',
3041
[
3142
'label_for' => 'sandbox_or_production',
32-
'tip' => __('Mondu\'s environment to use.', 'mondu'),
43+
'tip' => __( 'Mondu\'s environment to use.', 'mondu' ),
3344
]
3445
);
35-
add_settings_field('api_token',
36-
__('API Token', 'mondu'),
46+
add_settings_field(
47+
'api_token',
48+
__( 'API Token', 'mondu' ),
3749
[ $this, 'field_api_token' ],
3850
'mondu-settings-account',
3951
'mondu_account_settings_general',
4052
[
4153
'label_for' => 'api_token',
42-
'tip' => __('API Token provided by Mondu.', 'mondu'),
54+
'tip' => __( 'API Token provided by Mondu.', 'mondu' ),
4355
]
4456
);
45-
add_settings_field('send_line_items',
46-
__('Send line items', 'mondu'),
57+
add_settings_field(
58+
'send_line_items',
59+
__( 'Send line items', 'mondu' ),
4760
[ $this, 'field_send_line_items' ],
4861
'mondu-settings-account',
4962
'mondu_account_settings_general',
5063
[
5164
'label_for' => 'send_line_items',
52-
'tip' => __('Send the line items when creating order and invoice.', 'mondu'),
65+
'tip' => __( 'Send the line items when creating order and invoice.', 'mondu' ),
5366
]
5467
);
5568
}
5669

70+
/**
71+
* Field for sandbox or production
72+
*
73+
* @param array $args arguments.
74+
*/
5775
public function field_sandbox_or_production( $args = [] ) {
58-
$this->selectField(Plugin::OPTION_NAME, 'sandbox_or_production', [
59-
'sandbox' => __('Sandbox', 'mondu'),
60-
'production' => __('Production', 'mondu'),
61-
], $args['tip']);
76+
$this->selectField( Plugin::OPTION_NAME, 'sandbox_or_production', [
77+
'sandbox' => __( 'Sandbox', 'mondu' ),
78+
'production' => __( 'Production', 'mondu' ),
79+
], $args['tip'] );
6280
}
6381

82+
/**
83+
* Field for API token
84+
*
85+
* @param array $args arguments.
86+
*/
6487
public function field_api_token( $args = [] ) {
65-
$this->textField(Plugin::OPTION_NAME, 'api_token', $args['tip']);
88+
$this->textField( Plugin::OPTION_NAME, 'api_token', $args['tip'] );
6689
}
6790

91+
/**
92+
* Field for send line items
93+
*
94+
* @param array $args arguments.
95+
*/
6896
public function field_send_line_items( $args = [] ) {
69-
$this->selectField(Plugin::OPTION_NAME, 'send_line_items', [
70-
'yes' => __('Yes', 'mondu'),
71-
'order' => __('Send line items only for orders', 'mondu'),
72-
'no' => __('No', 'mondu'),
73-
], $args['tip']);
97+
$this->selectField( Plugin::OPTION_NAME, 'send_line_items', [
98+
'yes' => __( 'Yes', 'mondu' ),
99+
'order' => __( 'Send line items only for orders', 'mondu' ),
100+
'no' => __( 'No', 'mondu' ),
101+
], $args['tip'] );
74102
}
75103

104+
/**
105+
* Render the account options
106+
*
107+
* @param mixed $validation_error validation error.
108+
* @param mixed $webhooks_error webhooks error.
109+
*/
76110
public function render( $validation_error = null, $webhooks_error = null ) {
77-
if ( !current_user_can('manage_options') ) {
78-
wp_die(esc_html__('You do not have sufficient permissions to access this page.'));
111+
if ( !current_user_can( 'manage_options' ) ) {
112+
wp_die( esc_html__( 'You do not have sufficient permissions to access this page.' ) );
79113
}
80-
$credentials_validated = get_option('_mondu_credentials_validated');
81-
$webhooks_registered = get_option('_mondu_webhooks_registered');
114+
$credentials_validated = get_option( '_mondu_credentials_validated' );
115+
$webhooks_registered = get_option( '_mondu_webhooks_registered' );
82116

83117
include MONDU_VIEW_PATH . '/admin/options.php';
84118
}

src/Mondu/Admin/Option/Helper.php

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
<?php
2-
2+
/**
3+
* Helper class for options.
4+
*
5+
* @package Mondu
6+
*/
37
namespace Mondu\Admin\Option;
48

59
use Mondu\Plugin;
@@ -8,37 +12,67 @@
812
die( 'Direct access not allowed' );
913
}
1014

15+
/**
16+
* Class Helper
17+
*
18+
* @package Mondu
19+
*/
1120
abstract class Helper {
21+
/**
22+
* The global settings.
23+
*
24+
* @var false|mixed|null
25+
*/
1226
protected $global_settings;
1327

28+
/**
29+
* Helper constructor.
30+
*/
1431
public function __construct() {
15-
$this->global_settings = get_option(Plugin::OPTION_NAME);
32+
$this->global_settings = get_option( Plugin::OPTION_NAME );
1633
}
1734

35+
/**
36+
* Text field rendering.
37+
*
38+
* @param $option_name
39+
* @param $field_name
40+
* @param $tip
41+
* @return void
42+
*/
1843
protected function textField( $option_name, $field_name, $tip = '' ) {
1944
$field_id = $field_name;
20-
$field_value = isset($this->global_settings[ $field_name ]) ? $this->global_settings[ $field_name ] : '';
45+
$field_value = isset( $this->global_settings[ $field_name ] ) ? $this->global_settings[ $field_name ] : '';
2146
$field_name = $option_name . '[' . $field_name . ']';
2247

2348
?>
24-
<span class="woocommerce-help-tip" data-tip="<?php echo esc_attr($tip); ?>"></span>
25-
<input type="text" id="<?php echo esc_attr($field_id); ?>" name="<?php echo esc_attr($field_name); ?>" value="<?php echo esc_attr($field_value); ?>" />
49+
<span class="woocommerce-help-tip" data-tip="<?php echo esc_attr( $tip ); ?>"></span>
50+
<input type="text" id="<?php echo esc_attr( $field_id ); ?>" name="<?php echo esc_attr( $field_name ); ?>" value="<?php echo esc_attr( $field_value ); ?>" />
2651
<?php
2752
}
2853

54+
/**
55+
* Select field rendering.
56+
*
57+
* @param $option_name
58+
* @param $field_name
59+
* @param $options
60+
* @param $tip
61+
* @return void
62+
*/
2963
protected function selectField( $option_name, $field_name, $options, $tip ) {
3064
$field_id = $field_name;
31-
$field_value = isset($this->global_settings[ $field_name ]) ? $this->global_settings[ $field_name ] : '';
65+
$field_value = isset( $this->global_settings[ $field_name ] ) ? $this->global_settings[ $field_name ] : '';
3266
$field_name = $option_name . '[' . $field_name . ']';
3367

3468
?>
35-
<span class="woocommerce-help-tip" data-tip="<?php echo esc_attr($tip); ?>"></span>
36-
<select id="<?php echo esc_attr($field_id); ?>" name="<?php echo esc_attr($field_name); ?>">
69+
<span class="woocommerce-help-tip" data-tip="<?php echo esc_attr( $tip ); ?>"></span>
70+
<select id="<?php echo esc_attr( $field_id ); ?>" name="<?php echo esc_attr( $field_name ); ?>">
3771
<?php
3872
foreach ( $options as $value => $label ) {
3973
?>
4074
<?php $selected = $field_value === $value ? 'selected' : ''; ?>
41-
<option value="<?php echo esc_attr($value); ?>" <?php echo esc_attr($selected); ?>><?php echo esc_attr($label); ?></option>
75+
<option value="<?php echo esc_attr( $value ); ?>" <?php echo esc_attr( $selected ); ?>><?php echo esc_attr( $label ); ?></option>
4276
<?php
4377
}
4478
?>

0 commit comments

Comments
 (0)