From 59837dc2684d719bb6f43b0392fb40dd2ae3a0eb Mon Sep 17 00:00:00 2001 From: Halil Beycan <50718965+BeycanDeveloper@users.noreply.github.com> Date: Wed, 24 Jan 2024 17:35:36 +0800 Subject: [PATCH] phpcs configuration --- .gitattributes | 1 + .github/workflows/phpcs.yaml | 34 +++ classes/class.pmpro_ajax_api.php | 37 ++- classes/lite/class.pmpro_register_hooks.php | 77 +++--- .../lite/class.pmpro_transaction_model.php | 14 +- classes/lite/class.pmprogateway_cryptopay.php | 105 +++++---- classes/pro/class.pmpro_register_hooks.php | 71 +++--- classes/pro/class.pmpro_transaction_model.php | 14 +- classes/pro/class.pmprogateway_cryptopay.php | 102 ++++---- composer.json | 7 + ...ency-payments-for-paid-memberships-pro.php | 220 +++++++++--------- index.php | 4 +- phpcs.xml | 34 +++ 13 files changed, 441 insertions(+), 279 deletions(-) create mode 100644 .gitattributes create mode 100644 .github/workflows/phpcs.yaml create mode 100644 composer.json create mode 100644 phpcs.xml diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..94f480d --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text=auto eol=lf \ No newline at end of file diff --git a/.github/workflows/phpcs.yaml b/.github/workflows/phpcs.yaml new file mode 100644 index 0000000..4e3c48e --- /dev/null +++ b/.github/workflows/phpcs.yaml @@ -0,0 +1,34 @@ +name: PHPCS Check + +on: + push: + branches: + - master + +jobs: + phpcs: + name: PHPCS Check + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v2 + + - name: Set up PHP + uses: shivammathur/setup-php@v2 + with: + php-version: '8.1' + + - name: Install PHPCS + run: | + composer config --global --no-plugins allow-plugins.dealerdirect/phpcodesniffer-composer-installer true + composer require --dev squizlabs/php_codesniffer=* slevomat/coding-standard + + - name: Run PHPCS + run: | + composer phpcs --standard=phpcs.xml . + exit_status=$? + if [ $exit_status -ne 0 ]; then + echo "PHPCS check failed. Please fix the issues before merging." + exit 1 + fi \ No newline at end of file diff --git a/classes/class.pmpro_ajax_api.php b/classes/class.pmpro_ajax_api.php index 8c2631b..4125551 100644 --- a/classes/class.pmpro_ajax_api.php +++ b/classes/class.pmpro_ajax_api.php @@ -1,23 +1,32 @@ get_var("SELECT id FROM $wpdb->pmpro_discount_codes WHERE code = '" . esc_sql($discountCode) . "' LIMIT 1"); + $discountId = $wpdb->get_var( + "SELECT id FROM $wpdb->pmpro_discount_codes WHERE code = '" . esc_sql($discountCode) . "' LIMIT 1" + ); - $discountPrice = $wpdb->get_var("SELECT initial_payment FROM $wpdb->pmpro_discount_codes_levels WHERE code_id = '" . esc_sql($discountId) . "' LIMIT 1"); + $discountPrice = $wpdb->get_var( + "SELECT initial_payment + FROM $wpdb->pmpro_discount_codes_levels + WHERE code_id = '" . esc_sql($discountId) . "' LIMIT 1" + ); Response::success(null, [ 'amount' => floatval($discountPrice), @@ -44,4 +59,4 @@ public function pmpro_cryptopay_use_discount() : void } } -new PMPro_Ajax_Api(); \ No newline at end of file +new PMPro_Ajax_Api(); diff --git a/classes/lite/class.pmpro_register_hooks.php b/classes/lite/class.pmpro_register_hooks.php index 877f34a..4fa1715 100644 --- a/classes/lite/class.pmpro_register_hooks.php +++ b/classes/lite/class.pmpro_register_hooks.php @@ -1,25 +1,34 @@ function($tx) { + 'orderId' => function ($tx) { return '' . $tx->orderId . ''; } ], @@ -27,8 +36,8 @@ public function __construct() ['updatedAt'] ); } - - Hook::addFilter('init_pmpro_lite', function(object $data) { + + Hook::addFilter('init_pmpro_lite', function (object $data) { global $pmpro_levels; if (!isset($pmpro_levels[$data->params->pmpro->levelId])) { @@ -38,7 +47,13 @@ public function __construct() return $data; }); - function pmpro_check_discount_code_lite(object $order, object &$level) { + /** + * @param object $order + * @param object $level + * @return void + */ + function pmpro_check_discount_code_lite(object $order, object &$level): void + { global $wpdb; if (isset($order->discountCode)) { $codeCheck = pmpro_checkDiscountCode($order->discountCode, $level->id, true); @@ -56,18 +71,18 @@ function pmpro_check_discount_code_lite(object $order, object &$level) { } } - Hook::addAction('payment_started_pmpro_lite', function(object $data) { + Hook::addAction('payment_started_pmpro_lite', function (object $data): void { global $pmpro_levels; $currentUser = wp_get_current_user(); - + $order = new \MemberOrder(); $level = $pmpro_levels[$data->params->pmpro->levelId]; pmpro_check_discount_code_lite($data->order, $level); - if(empty($order->code)) { + if (empty($order->code)) { $order->code = $order->getRandomCode(); } - + // Set order values. $order->membership_id = $level->id; $order->membership_name = $level->name; @@ -122,14 +137,14 @@ function pmpro_check_discount_code_lite(object $order, object &$level) { $data->model->update(['orderId' => $order->id], ['hash' => $data->hash]); }); - Hook::addAction('payment_finished_pmpro_lite', function(object $data) { + Hook::addAction('payment_finished_pmpro_lite', function (object $data): void { global $pmpro_levels, $wpdb; $orderId = ($data->model->findOneBy(['hash' => $data->hash]))->orderId; $order = new \MemberOrder($orderId); - if (!$order->id) { - return; - } + if (!$order->id) { + return; + } if (!$data->status) { $order->status = "error"; @@ -140,12 +155,12 @@ function pmpro_check_discount_code_lite(object $order, object &$level) { $level = $pmpro_levels[$data->params->pmpro->levelId]; pmpro_check_discount_code_lite($data->order, $level); - $startdate = current_time( "mysql" ); + $startdate = current_time("mysql"); if (!empty($level->expiration_number)) { - if( $level->expiration_period == 'Hour' ){ - $enddate = date( "Y-m-d H:i:s", strtotime( "+ " . $level->expiration_number . " " . $level->expiration_period, current_time( "timestamp" ) ) ); + if ($level->expiration_period == 'Hour') { + $enddate = date("Y-m-d H:i:s", strtotime("+ " . $level->expiration_number . " " . $level->expiration_period, current_time("timestamp"))); } else { - $enddate = date( "Y-m-d 23:59:59", strtotime( "+ " . $level->expiration_number . " " . $level->expiration_period, current_time( "timestamp" ) ) ); + $enddate = date("Y-m-d 23:59:59", strtotime("+ " . $level->expiration_number . " " . $level->expiration_period, current_time("timestamp"))); } } else { $enddate = "NULL"; @@ -154,21 +169,21 @@ function pmpro_check_discount_code_lite(object $order, object &$level) { $discountCodeId = ""; if (isset($data->order->discountCode)) { $codeCheck = pmpro_checkDiscountCode($data->order->discountCode, $level->id, true); - + if ($codeCheck[0] == false) { $useDiscountCode = false; } else { $useDiscountCode = true; } - - //update membership_user table. + + // update membership_user table. if (!empty($data->order->discountCode) && !empty($useDiscountCode)) { $discountCodeId = $wpdb->get_var("SELECT id FROM $wpdb->pmpro_discount_codes WHERE code = '" . esc_sql($data->order->discountCode) . "' LIMIT 1"); - - $wpdb->query("INSERT INTO $wpdb->pmpro_discount_codes_uses (code_id, user_id, order_id, timestamp) VALUES('" . $discountCodeId . "', '" . $data->userId . "', '" . intval($orderId) . "', '" . current_time( "mysql" ) . "')"); + + $wpdb->query("INSERT INTO $wpdb->pmpro_discount_codes_uses (code_id, user_id, order_id, timestamp) VALUES('" . $discountCodeId . "', '" . $data->userId . "', '" . intval($orderId) . "', '" . current_time("mysql") . "')"); } } - + $userLevel = array( 'user_id' => $data->userId, 'membership_id' => $level->id, @@ -189,8 +204,8 @@ function pmpro_check_discount_code_lite(object $order, object &$level) { $order->status = "success"; $order->saveOrder(); }); - - Hook::addFilter('payment_redirect_urls_pmpro_lite', function(object $data) { + + Hook::addFilter('payment_redirect_urls_pmpro_lite', function (object $data) { return [ 'success' => pmpro_url("confirmation", "?level=" . $data->params->pmpro->levelId), 'failed' => pmpro_url("account") @@ -200,4 +215,4 @@ function pmpro_check_discount_code_lite(object $order, object &$level) { } } -new PMPro_Register_Hooks_Lite(); \ No newline at end of file +new PMPro_Register_Hooks_Lite(); diff --git a/classes/lite/class.pmpro_transaction_model.php b/classes/lite/class.pmpro_transaction_model.php index 35475b4..eebf3c1 100644 --- a/classes/lite/class.pmpro_transaction_model.php +++ b/classes/lite/class.pmpro_transaction_model.php @@ -1,11 +1,19 @@ gateway = $gateway; - return $this->gateway; - } + return parent::__construct($gateway); + } /** * @return void */ - public static function init() + public static function init(): void { if (!is_user_logged_in()) { add_filter('pmpro_skip_account_fields', '__return_true'); - } - + } + add_filter('pmpro_gateways', ['PMProGateway_cryptopay_lite', 'pmpro_gateways']); add_filter('pmpro_payment_options', ['PMProGateway_cryptopay_lite', 'pmpro_payment_options']); add_filter('pmpro_required_billing_fields', ['PMProGateway_cryptopay_lite', 'pmpro_required_billing_fields']); @@ -39,25 +46,25 @@ public static function init() } add_action('wp_enqueue_scripts', ['PMProGateway_cryptopay_lite', 'pmpro_load_scripts']); - } + } /** - * @param array $gateways - * @return array + * @param array $gateways + * @return array */ - public static function pmpro_gateways(array $gateways) : array + public static function pmpro_gateways(array $gateways): array { - if (empty($gateways['cryptopay_lite'])) { - $gateways['cryptopay_lite'] = __('CryptoPay Lite', 'pmpro-cryptopay'); - } + if (empty($gateways['cryptopay_lite'])) { + $gateways['cryptopay_lite'] = __('CryptoPay Lite', 'pmpro-cryptopay'); + } - return $gateways; - } + return $gateways; + } /** - * @return array + * @return array */ - public static function getGatewayOptions() : array + public static function getGatewayOptions(): array { return [ 'currency', @@ -67,41 +74,41 @@ public static function getGatewayOptions() : array } /** - * @param array $options - * @return array + * @param array $options + * @return array */ - public static function pmpro_payment_options(array $options) : array + public static function pmpro_payment_options(array $options): array { return array_merge(self::getGatewayOptions(), $options); } - + /** - * @param array $fields - * @return array + * @param array $fields + * @return array */ - public static function pmpro_required_billing_fields(array $fields) : array + public static function pmpro_required_billing_fields(array $fields): array { - unset($fields['bfirstname']); - unset($fields['blastname']); - unset($fields['baddress1']); - unset($fields['bcity']); - unset($fields['bstate']); - unset($fields['bzipcode']); - unset($fields['bphone']); - unset($fields['bemail']); - unset($fields['bcountry']); - unset($fields['CardType']); - unset($fields['AccountNumber']); - unset($fields['ExpirationMonth']); - unset($fields['ExpirationYear']); - unset($fields['CVV']); - return $fields; - } + unset($fields['bfirstname']); + unset($fields['blastname']); + unset($fields['baddress1']); + unset($fields['bcity']); + unset($fields['bstate']); + unset($fields['bzipcode']); + unset($fields['bphone']); + unset($fields['bemail']); + unset($fields['bcountry']); + unset($fields['CardType']); + unset($fields['AccountNumber']); + unset($fields['ExpirationMonth']); + unset($fields['ExpirationYear']); + unset($fields['CVV']); + return $fields; + } /** * @return void */ - public static function pmpro_checkout() + public static function pmpro_checkout(): void { global $gateway, $pmpro_level, $discount_code; @@ -109,7 +116,7 @@ public static function pmpro_checkout() ?>
- + function($tx) { + 'orderId' => function ($tx) { return '' . $tx->orderId . ''; } ], @@ -27,8 +36,8 @@ public function __construct() ['updatedAt'] ); } - - Hook::addFilter('init_pmpro', function(object $data) { + + Hook::addFilter('init_pmpro', function (object $data) { global $pmpro_levels; if (!isset($pmpro_levels[$data->params->pmpro->levelId])) { @@ -38,7 +47,13 @@ public function __construct() return $data; }); - function pmpro_check_discount_code(object $order, object &$level) { + /** + * @param object $order + * @param object $level + * @return void + */ + function pmpro_check_discount_code(object $order, object &$level): void + { global $wpdb; if (isset($order->discountCode)) { $codeCheck = pmpro_checkDiscountCode($order->discountCode, $level->id, true); @@ -56,18 +71,18 @@ function pmpro_check_discount_code(object $order, object &$level) { } } - Hook::addAction('payment_started_pmpro', function(object $data) { + Hook::addAction('payment_started_pmpro', function (object $data): void { global $pmpro_levels; $currentUser = wp_get_current_user(); - + $order = new \MemberOrder(); $level = $pmpro_levels[$data->params->pmpro->levelId]; pmpro_check_discount_code($data->order, $level); - if(empty($order->code)) { + if (empty($order->code)) { $order->code = $order->getRandomCode(); } - + // Set order values. $order->membership_id = $level->id; $order->membership_name = $level->name; @@ -122,7 +137,7 @@ function pmpro_check_discount_code(object $order, object &$level) { $data->model->update(['orderId' => $order->id], ['hash' => $data->hash]); }); - Hook::addAction('payment_finished_pmpro', function(object $data) { + Hook::addAction('payment_finished_pmpro', function (object $data): void { global $pmpro_levels, $wpdb; $orderId = ($data->model->findOneBy(['hash' => $data->hash]))->orderId; @@ -140,12 +155,12 @@ function pmpro_check_discount_code(object $order, object &$level) { $level = $pmpro_levels[$data->params->pmpro->levelId]; pmpro_check_discount_code($data->order, $level); - $startdate = current_time( "mysql" ); + $startdate = current_time("mysql"); if (!empty($level->expiration_number)) { - if( $level->expiration_period == 'Hour' ){ - $enddate = date( "Y-m-d H:i:s", strtotime( "+ " . $level->expiration_number . " " . $level->expiration_period, current_time( "timestamp" ) ) ); + if ($level->expiration_period == 'Hour') { + $enddate = date("Y-m-d H:i:s", strtotime("+ " . $level->expiration_number . " " . $level->expiration_period, current_time("timestamp"))); } else { - $enddate = date( "Y-m-d 23:59:59", strtotime( "+ " . $level->expiration_number . " " . $level->expiration_period, current_time( "timestamp" ) ) ); + $enddate = date("Y-m-d 23:59:59", strtotime("+ " . $level->expiration_number . " " . $level->expiration_period, current_time("timestamp"))); } } else { $enddate = "NULL"; @@ -154,21 +169,21 @@ function pmpro_check_discount_code(object $order, object &$level) { $discountCodeId = ""; if (isset($data->order->discountCode)) { $codeCheck = pmpro_checkDiscountCode($data->order->discountCode, $level->id, true); - + if ($codeCheck[0] == false) { $useDiscountCode = false; } else { $useDiscountCode = true; } - - //update membership_user table. + + // update membership_user table. if (!empty($data->order->discountCode) && !empty($useDiscountCode)) { $discountCodeId = $wpdb->get_var("SELECT id FROM $wpdb->pmpro_discount_codes WHERE code = '" . esc_sql($data->order->discountCode) . "' LIMIT 1"); - - $wpdb->query("INSERT INTO $wpdb->pmpro_discount_codes_uses (code_id, user_id, order_id, timestamp) VALUES('" . $discountCodeId . "', '" . $data->userId . "', '" . intval($orderId) . "', '" . current_time( "mysql" ) . "')"); + + $wpdb->query("INSERT INTO $wpdb->pmpro_discount_codes_uses (code_id, user_id, order_id, timestamp) VALUES('" . $discountCodeId . "', '" . $data->userId . "', '" . intval($orderId) . "', '" . current_time("mysql") . "')"); } } - + $userLevel = array( 'user_id' => $data->userId, 'membership_id' => $level->id, @@ -189,8 +204,8 @@ function pmpro_check_discount_code(object $order, object &$level) { $order->status = "success"; $order->saveOrder(); }); - - Hook::addFilter('payment_redirect_urls_pmpro', function(object $data) { + + Hook::addFilter('payment_redirect_urls_pmpro', function (object $data) { return [ 'success' => pmpro_url("confirmation", "?level=" . $data->params->pmpro->levelId), 'failed' => pmpro_url("account"), @@ -201,4 +216,4 @@ function pmpro_check_discount_code(object $order, object &$level) { } } -new PMPro_Register_Hooks(); \ No newline at end of file +new PMPro_Register_Hooks(); diff --git a/classes/pro/class.pmpro_transaction_model.php b/classes/pro/class.pmpro_transaction_model.php index f1a23f6..a5bd913 100644 --- a/classes/pro/class.pmpro_transaction_model.php +++ b/classes/pro/class.pmpro_transaction_model.php @@ -1,11 +1,19 @@ $gateways + * @return array */ - public static function pmpro_gateways(array $gateways) : array + public static function pmpro_gateways(array $gateways): array { - if (empty($gateways['cryptopay'])) { - $gateways['cryptopay'] = __('CryptoPay', 'pmpro-cryptopay'); - } + if (empty($gateways['cryptopay'])) { + $gateways['cryptopay'] = __('CryptoPay', 'pmpro-cryptopay'); + } - return $gateways; - } + return $gateways; + } /** - * @return array + * @return array */ - public static function getGatewayOptions() : array + public static function getGatewayOptions(): array { return [ 'currency', @@ -66,41 +74,41 @@ public static function getGatewayOptions() : array } /** - * @param array $options - * @return array + * @param array $options + * @return array */ - public static function pmpro_payment_options(array $options) : array + public static function pmpro_payment_options(array $options): array { return array_merge(self::getGatewayOptions(), $options); } /** - * @param array $fields - * @return array + * @param array $fields + * @return array */ - static function pmpro_required_billing_fields(array $fields) : array + public static function pmpro_required_billing_fields(array $fields): array { - unset($fields['bfirstname']); - unset($fields['blastname']); - unset($fields['baddress1']); - unset($fields['bcity']); - unset($fields['bstate']); - unset($fields['bzipcode']); - unset($fields['bphone']); - unset($fields['bemail']); - unset($fields['bcountry']); - unset($fields['CardType']); - unset($fields['AccountNumber']); - unset($fields['ExpirationMonth']); - unset($fields['ExpirationYear']); - unset($fields['CVV']); - return $fields; - } + unset($fields['bfirstname']); + unset($fields['blastname']); + unset($fields['baddress1']); + unset($fields['bcity']); + unset($fields['bstate']); + unset($fields['bzipcode']); + unset($fields['bphone']); + unset($fields['bemail']); + unset($fields['bcountry']); + unset($fields['CardType']); + unset($fields['AccountNumber']); + unset($fields['ExpirationMonth']); + unset($fields['ExpirationYear']); + unset($fields['CVV']); + return $fields; + } /** * @return void */ - public static function pmpro_checkout() + public static function pmpro_checkout(): void { global $gateway, $pmpro_level, $discount_code; @@ -108,7 +116,7 @@ public static function pmpro_checkout() ?>
- + createTable(); - } - if (class_exists(LiteLoader::class)) { - require_once __DIR__ . '/classes/lite/class.pmpro_transaction_model.php'; - (new PMPro_Transaction_Model_Lite())->createTable(); - } +register_activation_hook(PMPRO_CRYPTOPAY_FILE, function (): void { + if (class_exists(Loader::class)) { + require_once __DIR__ . '/classes/pro/class.pmpro_transaction_model.php'; + (new PMPro_Transaction_Model())->createTable(); + } + if (class_exists(LiteLoader::class)) { + require_once __DIR__ . '/classes/lite/class.pmpro_transaction_model.php'; + (new PMPro_Transaction_Model_Lite())->createTable(); + } }); -function pmpro_cryptopay_addModels() { - if (class_exists(Loader::class)) { - require_once __DIR__ . '/classes/pro/class.pmpro_transaction_model.php'; - Hook::addFilter('models', function($models) { - return array_merge($models, [ - 'pmpro' => new PMPro_Transaction_Model() - ]); - }); - } - - if (class_exists(LiteLoader::class)) { - require_once __DIR__ . '/classes/lite/class.pmpro_transaction_model.php'; - LiteHook::addFilter('models', function($models) { - return array_merge($models, [ - 'pmpro_lite' => new PMPro_Transaction_Model_Lite() - ]); - }); - } +/** + * Add models to the plugin. + * @return void + */ +function pmpro_cryptopay_addModels(): void +{ + if (class_exists(Loader::class)) { + require_once __DIR__ . '/classes/pro/class.pmpro_transaction_model.php'; + Hook::addFilter('models', function ($models) { + return array_merge($models, [ + 'pmpro' => new PMPro_Transaction_Model() + ]); + }); + } + + if (class_exists(LiteLoader::class)) { + require_once __DIR__ . '/classes/lite/class.pmpro_transaction_model.php'; + LiteHook::addFilter('models', function ($models) { + return array_merge($models, [ + 'pmpro_lite' => new PMPro_Transaction_Model_Lite() + ]); + }); + } } pmpro_cryptopay_addModels(); -add_action('plugins_loaded', function() { - - pmpro_cryptopay_addModels(); - - load_plugin_textdomain('pmpro-cryptopay', false, basename(__DIR__) . '/languages'); - - if (defined('PMPRO_DIR') == false) { - return add_action('admin_notices', function () { - ?> -
-

'.esc_html__('clicking here', 'pmpro-cryptopay').''); ?>

-
- - - -
-

'.esc_html__('clicking here', 'pmpro-cryptopay').''); ?>

-
- ' . esc_html__('clicking here', 'pmpro-cryptopay') . ''); + printf('

%2$s

', esc_attr($class), $message); + }); + return; + } + + if ((class_exists(Loader::class) || class_exists(LiteLoader::class))) { + require_once __DIR__ . '/classes/class.pmpro_ajax_api.php'; + + if (class_exists(Loader::class)) { + require_once __DIR__ . '/classes/pro/class.pmpro_register_hooks.php'; + require_once __DIR__ . '/classes/pro/class.pmprogateway_cryptopay.php'; + } + + if (class_exists(LiteLoader::class)) { + require_once __DIR__ . '/classes/lite/class.pmpro_register_hooks.php'; + require_once __DIR__ . '/classes/lite/class.pmprogateway_cryptopay.php'; + } + + add_action('admin_footer', function (): void { + ?> + + +
+

' . esc_html__('clicking here', 'pmpro-cryptopay') . ''); ?>

+
+ + + + + BeycanPressStandard + + ./**/*.php + + */views/* + */vendor/* + */PluginHero/templates/* + + + + + + + + + + + + + + + + + + + + + + +