Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve plugin code standards #118

Merged
merged 5 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
Contributors: mondu-ai, arthurmmoreira, tikohov20
Tags: mondu, woocommerce, e-commerce, ecommerce, store, sales, sell, woo, woo commerce, shop, cart, shopping cart, sell online, checkout, payment, payments, bnpl, b2b
Requires at least: 5.9.0
Tested up to: 6.2.2
Stable tag: 2.1.7
Tested up to: 6.5.3
Stable tag: 3.0.0
Requires PHP: 7.4
License: GPLv3
License URI: https://www.gnu.org/licenses/gpl-3.0.html
Expand Down
16 changes: 8 additions & 8 deletions assets/src/js/mondublocks.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
(function() {
const { __ } = wp.i18n;
const { createElement } = window.wp.element;
const { decodeEntities } = window.wp.htmlEntities;
const { registerPaymentMethod } = window.wc.wcBlocksRegistry;
(function () {
const { __ } = wp.i18n;
const { createElement } = window.wp.element;
const { decodeEntities } = window.wp.htmlEntities;
const { registerPaymentMethod } = window.wc.wcBlocksRegistry;
const { registerPaymentMethodExtensionCallbacks } = window.wc.wcBlocksRegistry;
const settings = window.wc.wcSettings.getSetting( 'mondu_blocks_data', {} );
const settings = window.wc.wcSettings.getSetting( 'mondu_blocks_data', {} );

settings.available_countries = settings.available_countries || [];
settings.gateways = settings.gateways || {};
settings.gateways = settings.gateways || {};

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

registerPaymentMethodExtensionCallbacks('mondu',
Object.keys(settings.gateways).reduce((previousValue, currentValue) => {
previousValue[currentValue] = function(arg) {
previousValue[currentValue] = function (arg) {
return settings.available_countries.includes(arg.billingAddress.country)
}

Expand Down
8 changes: 4 additions & 4 deletions mondu-buy-now-pay-later.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: Mondu Buy Now Pay Later
* Plugin URI: https://github.com/mondu-ai/bnpl-checkout-woocommerce/releases
* Description: Mondu provides B2B E-commerce and B2B marketplaces with an online payment solution to buy now and pay later.
* Version: 2.2.1
* Version: 3.0.0
* Author: Mondu
* Author URI: https://mondu.ai
*
Expand All @@ -27,7 +27,7 @@
die( 'Direct access not allowed' );
}

define( 'MONDU_PLUGIN_VERSION', '2.2.1' );
define( 'MONDU_PLUGIN_VERSION', '3.0.0' );
define( 'MONDU_PLUGIN_FILE', __FILE__ );
define( 'MONDU_PLUGIN_PATH', __DIR__ );
define( 'MONDU_PLUGIN_BASENAME', plugin_basename(MONDU_PLUGIN_FILE) );
Expand All @@ -46,7 +46,7 @@ function mondu_env( $name, $default_value ) {
mondu_env( 'MONDU_WEBHOOKS_URL', get_home_url() );

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

function mondu_activate() {
}
Expand All @@ -63,7 +63,7 @@ function mondu_deactivate() {
register_deactivation_hook( MONDU_PLUGIN_FILE, 'mondu_deactivate' );

// Here because this needs to happen before plugins_loaded hook
add_action('before_woocommerce_init', function() {
add_action('before_woocommerce_init', function () {
if ( class_exists( FeaturesUtil::class ) ) {
FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__ );
FeaturesUtil::declare_compatibility( 'cart_checkout_blocks', __FILE__ );
Expand Down
7 changes: 6 additions & 1 deletion pbs-rules-set.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@
<exclude name="WordPress.Security.ValidatedSanitizedInput.MissingUnslash" />
<exclude name="WordPress.WP.I18n.NonSingularStringLiteralText" />
<exclude name="WordPress.WhiteSpace.OperatorSpacing.NoSpaceAfter" />
<exclude name="Generic.Arrays.DisallowShortArraySyntax"/>
<exclude name="Generic.Commenting.DocComment.MissingShort"/>
<exclude name="Universal.Arrays.DisallowShortArraySyntax"/>
<exclude name="WordPress.Security.EscapeOutput.ExceptionNotEscaped"/>
</rule>

<rule ref="WooCommerce-Core">
<exclude name="Core.Commenting.CommentTags.AuthorTag" />
<exclude name="WordPress.PHP.DontExtract" />
<exclude name="Generic.Arrays.DisallowShortArraySyntax" />
<exclude name="Generic.WhiteSpace.ScopeIndent.Incorrect" />
</rule>

Expand All @@ -46,6 +49,8 @@
</properties>
</rule>

<rule ref="Generic.Arrays.DisallowLongArraySyntax"/>

<rule ref="PHPCompatibility">
<exclude-pattern>tests/</exclude-pattern>
</rule>
Expand Down
82 changes: 58 additions & 24 deletions src/Mondu/Admin/Option/Account.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<?php
/**
* Account options
*
* @package Mondu
*/

namespace Mondu\Admin\Option;

Expand All @@ -8,77 +13,106 @@
die( 'Direct access not allowed' );
}

/**
* Class Account
*/
class Account extends Helper {
/**
* Register the settings
*/
public function register() {
register_setting('mondu', Plugin::OPTION_NAME);

/*
/**
* General Settings
*/
add_settings_section(
'mondu_account_settings_general',
__('Settings', 'woocommerce'),
__( 'Settings', 'woocommerce' ),
[],
'mondu-settings-account'
);
add_settings_field(
'sandbox_or_production',
__('Sandbox or production', 'mondu'),
__( 'Sandbox or production', 'mondu' ),
[ $this, 'field_sandbox_or_production' ],
'mondu-settings-account',
'mondu_account_settings_general',
[
'label_for' => 'sandbox_or_production',
'tip' => __('Mondu\'s environment to use.', 'mondu'),
'tip' => __( 'Mondu\'s environment to use.', 'mondu' ),
]
);
add_settings_field('api_token',
__('API Token', 'mondu'),
add_settings_field(
'api_token',
__( 'API Token', 'mondu' ),
[ $this, 'field_api_token' ],
'mondu-settings-account',
'mondu_account_settings_general',
[
'label_for' => 'api_token',
'tip' => __('API Token provided by Mondu.', 'mondu'),
'tip' => __( 'API Token provided by Mondu.', 'mondu' ),
]
);
add_settings_field('send_line_items',
__('Send line items', 'mondu'),
add_settings_field(
'send_line_items',
__( 'Send line items', 'mondu' ),
[ $this, 'field_send_line_items' ],
'mondu-settings-account',
'mondu_account_settings_general',
[
'label_for' => 'send_line_items',
'tip' => __('Send the line items when creating order and invoice.', 'mondu'),
'tip' => __( 'Send the line items when creating order and invoice.', 'mondu' ),
]
);
}

/**
* Field for sandbox or production
*
* @param array $args arguments.
*/
public function field_sandbox_or_production( $args = [] ) {
$this->selectField(Plugin::OPTION_NAME, 'sandbox_or_production', [
'sandbox' => __('Sandbox', 'mondu'),
'production' => __('Production', 'mondu'),
], $args['tip']);
$this->selectField( Plugin::OPTION_NAME, 'sandbox_or_production', [
'sandbox' => __( 'Sandbox', 'mondu' ),
'production' => __( 'Production', 'mondu' ),
], $args['tip'] );
}

/**
* Field for API token
*
* @param array $args arguments.
*/
public function field_api_token( $args = [] ) {
$this->textField(Plugin::OPTION_NAME, 'api_token', $args['tip']);
$this->textField( Plugin::OPTION_NAME, 'api_token', $args['tip'] );
}

/**
* Field for send line items
*
* @param array $args arguments.
*/
public function field_send_line_items( $args = [] ) {
$this->selectField(Plugin::OPTION_NAME, 'send_line_items', [
'yes' => __('Yes', 'mondu'),
'order' => __('Send line items only for orders', 'mondu'),
'no' => __('No', 'mondu'),
], $args['tip']);
$this->selectField( Plugin::OPTION_NAME, 'send_line_items', [
'yes' => __( 'Yes', 'mondu' ),
'order' => __( 'Send line items only for orders', 'mondu' ),
'no' => __( 'No', 'mondu' ),
], $args['tip'] );
}

/**
* Render the account options
*
* @param mixed $validation_error validation error.
* @param mixed $webhooks_error webhooks error.
*/
public function render( $validation_error = null, $webhooks_error = null ) {
if ( !current_user_can('manage_options') ) {
wp_die(esc_html__('You do not have sufficient permissions to access this page.'));
if ( !current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'You do not have sufficient permissions to access this page.' ) );
}
$credentials_validated = get_option('_mondu_credentials_validated');
$webhooks_registered = get_option('_mondu_webhooks_registered');
$credentials_validated = get_option( '_mondu_credentials_validated' );
$webhooks_registered = get_option( '_mondu_webhooks_registered' );

include MONDU_VIEW_PATH . '/admin/options.php';
}
Expand Down
52 changes: 43 additions & 9 deletions src/Mondu/Admin/Option/Helper.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php

/**
* Helper class for options.
*
* @package Mondu
*/
namespace Mondu\Admin\Option;

use Mondu\Plugin;
Expand All @@ -8,37 +12,67 @@
die( 'Direct access not allowed' );
}

/**
* Class Helper
*
* @package Mondu
*/
abstract class Helper {
/**
* The global settings.
*
* @var false|mixed|null
*/
protected $global_settings;

/**
* Helper constructor.
*/
public function __construct() {
$this->global_settings = get_option(Plugin::OPTION_NAME);
$this->global_settings = get_option( Plugin::OPTION_NAME );
}

/**
* Text field rendering.
*
* @param $option_name
* @param $field_name
* @param $tip
* @return void
*/
protected function textField( $option_name, $field_name, $tip = '' ) {
$field_id = $field_name;
$field_value = isset($this->global_settings[ $field_name ]) ? $this->global_settings[ $field_name ] : '';
$field_value = isset( $this->global_settings[ $field_name ] ) ? $this->global_settings[ $field_name ] : '';
$field_name = $option_name . '[' . $field_name . ']';

?>
<span class="woocommerce-help-tip" data-tip="<?php echo esc_attr($tip); ?>"></span>
<input type="text" id="<?php echo esc_attr($field_id); ?>" name="<?php echo esc_attr($field_name); ?>" value="<?php echo esc_attr($field_value); ?>" />
<span class="woocommerce-help-tip" data-tip="<?php echo esc_attr( $tip ); ?>"></span>
<input type="text" id="<?php echo esc_attr( $field_id ); ?>" name="<?php echo esc_attr( $field_name ); ?>" value="<?php echo esc_attr( $field_value ); ?>" />
<?php
}

/**
* Select field rendering.
*
* @param $option_name
* @param $field_name
* @param $options
* @param $tip
* @return void
*/
protected function selectField( $option_name, $field_name, $options, $tip ) {
$field_id = $field_name;
$field_value = isset($this->global_settings[ $field_name ]) ? $this->global_settings[ $field_name ] : '';
$field_value = isset( $this->global_settings[ $field_name ] ) ? $this->global_settings[ $field_name ] : '';
$field_name = $option_name . '[' . $field_name . ']';

?>
<span class="woocommerce-help-tip" data-tip="<?php echo esc_attr($tip); ?>"></span>
<select id="<?php echo esc_attr($field_id); ?>" name="<?php echo esc_attr($field_name); ?>">
<span class="woocommerce-help-tip" data-tip="<?php echo esc_attr( $tip ); ?>"></span>
<select id="<?php echo esc_attr( $field_id ); ?>" name="<?php echo esc_attr( $field_name ); ?>">
<?php
foreach ( $options as $value => $label ) {
?>
<?php $selected = $field_value === $value ? 'selected' : ''; ?>
<option value="<?php echo esc_attr($value); ?>" <?php echo esc_attr($selected); ?>><?php echo esc_attr($label); ?></option>
<option value="<?php echo esc_attr( $value ); ?>" <?php echo esc_attr( $selected ); ?>><?php echo esc_attr( $label ); ?></option>
<?php
}
?>
Expand Down
Loading
Loading