Skip to content

Instantly share code, notes, and snippets.

@kkarpieszuk
Created April 20, 2023 08:52
Show Gist options
  • Save kkarpieszuk/6052e9aa7d2b95f97a6945b2ea2dbb7f to your computer and use it in GitHub Desktop.
Save kkarpieszuk/6052e9aa7d2b95f97a6945b2ea2dbb7f to your computer and use it in GitHub Desktop.
diff --git a/wpforms/src/Admin/Payments/Views/Overview/Page.php b/wpforms/src/Admin/Payments/Views/Overview/Page.php
index a1879f46d..534dfd825 100644
--- a/wpforms/src/Admin/Payments/Views/Overview/Page.php
+++ b/wpforms/src/Admin/Payments/Views/Overview/Page.php
@@ -118,7 +118,7 @@ class Page implements PaymentsViewsInterface {
static $mode;
- if ( ValueValidator::is_valid_mode( $mode ) ) {
+ if ( ValueValidator::is_valid( $mode, 'mode' ) ) {
return $mode;
}
@@ -127,7 +127,7 @@ class Page implements PaymentsViewsInterface {
$user_id = get_current_user_id();
$meta_key = 'wpforms-payments-mode';
- if ( ValueValidator::is_valid_mode( $mode ) ) {
+ if ( ValueValidator::is_valid( $mode, 'mode' ) ) {
update_user_meta( $user_id, $meta_key, $mode );
return $mode;
@@ -135,7 +135,7 @@ class Page implements PaymentsViewsInterface {
$mode = get_user_meta( $user_id, $meta_key, true );
- return ValueValidator::is_valid_mode( $mode ) ? $mode : 'live';
+ return ValueValidator::is_valid( $mode, 'mode' ) ? $mode : 'live';
}
/**
diff --git a/wpforms/src/Admin/Payments/Views/Overview/Table.php b/wpforms/src/Admin/Payments/Views/Overview/Table.php
index c23587db4..1497fe4c9 100644
--- a/wpforms/src/Admin/Payments/Views/Overview/Table.php
+++ b/wpforms/src/Admin/Payments/Views/Overview/Table.php
@@ -338,11 +338,11 @@ class Table extends \WP_List_Table {
*/
private function get_column_gateway( array $item ) {
- if ( ! isset( $item['gateway'] ) || ! ValueValidator::is_valid_gateway( $item['gateway'] ) ) {
+ if ( ! isset( $item['gateway'] ) || ! ValueValidator::is_valid( $item['gateway'], 'gateway' ) ) {
return '';
}
- return ValueValidator::get_allowed_gateways()[ $item['gateway'] ];
+ return ValueValidator::get_allowed_gateway()[ $item['gateway'] ];
}
/**
@@ -397,11 +397,11 @@ class Table extends \WP_List_Table {
*/
private function get_column_status( array $item ) {
- if ( ! isset( $item['status'] ) || ! ValueValidator::is_valid_status( $item['status'] ) ) {
+ if ( ! isset( $item['status'] ) || ! ValueValidator::is_valid( $item['status'], 'status' ) ) {
return '';
}
- return sprintf( '<span class="wpforms-payment-status status-%1$s">%2$s</span>', strtolower( $item['status'] ), ValueValidator::get_allowed_statuses()[ $item['status'] ] );
+ return sprintf( '<span class="wpforms-payment-status status-%1$s">%2$s</span>', strtolower( $item['status'] ), ValueValidator::get_allowed_status()[ $item['status'] ] );
}
/**
@@ -443,11 +443,11 @@ class Table extends \WP_List_Table {
*/
private function get_column_type( array $item ) {
- if ( ! isset( $item['type'] ) || ValueValidator::is_valid_type( $item['type'] ) ) {
+ if ( ! isset( $item['type'] ) || ValueValidator::is_valid( $item['type'], 'type' ) ) {
return esc_html__( 'N/A', 'wpforms-lite' );
}
- return ValueValidator::get_allowed_types()[ $item['type'] ];
+ return ValueValidator::get_allowed_type()[ $item['type'] ];
}
/**
diff --git a/wpforms/src/Db/Payments/Payment.php b/wpforms/src/Db/Payments/Payment.php
index b6c11b77e..21e82db6a 100644
--- a/wpforms/src/Db/Payments/Payment.php
+++ b/wpforms/src/Db/Payments/Payment.php
@@ -116,7 +116,7 @@ class Payment extends WPForms_DB {
public function add( $data, $type = '' ) {
// Return early if the status is not allowed.
- if ( isset( $data['status'] ) && ! ValueValidator::is_valid_status( $data['status'] ) ) {
+ if ( isset( $data['status'] ) && ! ValueValidator::is_valid( $data['status'], 'status' ) ) {
return 0;
}
@@ -164,7 +164,7 @@ class Payment extends WPForms_DB {
return false;
}
- if ( isset( $data['status'] ) && ! ValueValidator::is_valid_status( $data['status'] ) ) {
+ if ( isset( $data['status'] ) && ! ValueValidator::is_valid( $data['status'], 'status' ) ) {
return false;
}
@@ -236,7 +236,7 @@ class Payment extends WPForms_DB {
global $wpdb;
- $subscription_types = wpforms_wpdb_prepare_in( array_keys( ValueValidator::get_allowed_subscription_types() ) );
+ $subscription_types = wpforms_wpdb_prepare_in( array_keys( ValueValidator::get_allowed_subscription_type() ) );
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.NoCaching
// @todo: consider using prepare.
@@ -271,7 +271,7 @@ class Payment extends WPForms_DB {
$query = '';
// If it's a valid mode, add it to a WHERE clause.
- if ( isset( ValueValidator::get_allowed_modes()[ $args['mode'] ] ) ) {
+ if ( isset( ValueValidator::get_allowed_mode()[ $args['mode'] ] ) ) {
$query .= $wpdb->prepare( ' AND mode = %s', $args['mode'] );
}
diff --git a/wpforms/src/Db/Payments/ValueValidator.php b/wpforms/src/Db/Payments/ValueValidator.php
index afe4474f2..acd3b77f7 100644
--- a/wpforms/src/Db/Payments/ValueValidator.php
+++ b/wpforms/src/Db/Payments/ValueValidator.php
@@ -12,32 +12,39 @@ namespace WPForms\Db\Payments;
class ValueValidator {
/**
- * Get allowed modes.
+ * Check if value is valid for the given column.
*
* @since {VERSION}
*
- * @return array
+ * @param string $value Value to check if is valid.
+ * @param string $column Database column name.
+ *
+ * @return bool
*/
- public static function get_allowed_modes() {
+ public static function is_valid( $value, $column ) {
- return [
- 'live' => esc_html__( 'Live', 'wpforms-lite' ),
- 'test' => esc_html__( 'Test', 'wpforms-lite' ),
- ];
+ $method = 'get_allowed_' . $column;
+
+ if ( ! method_exists( __CLASS__, $method ) ) {
+ return false;
+ }
+
+ return isset( self::$method()[ $value ] );
}
/**
- * Check if mode is valid.
+ * Get allowed modes.
*
* @since {VERSION}
*
- * @param string $mode Mode.
- *
- * @return bool
+ * @return array
*/
- public static function is_valid_mode( $mode ) {
+ public static function get_allowed_mode() {
- return isset( self::get_allowed_modes()[ $mode ] );
+ return [
+ 'live' => esc_html__( 'Live', 'wpforms-lite' ),
+ 'test' => esc_html__( 'Test', 'wpforms-lite' ),
+ ];
}
/**
@@ -47,7 +54,7 @@ class ValueValidator {
*
* @return array
*/
- public static function get_allowed_gateways() {
+ public static function get_allowed_gateway() {
return [
'paypal_standard' => esc_html__( 'PayPal Standard', 'wpforms-lite' ),
@@ -58,20 +65,6 @@ class ValueValidator {
];
}
- /**
- * Check if gateway is valid.
- *
- * @since {VERSION}
- *
- * @param string $gateway Gateway.
- *
- * @return bool
- */
- public static function is_valid_gateway( $gateway ) {
-
- return isset( self::get_allowed_gateways()[ $gateway ] );
- }
-
/**
* Get allowed statuses.
*
@@ -79,7 +72,7 @@ class ValueValidator {
*
* @return array
*/
- public static function get_allowed_statuses() {
+ public static function get_allowed_status() {
return [
'completed' => __( 'Completed', 'wpforms-lite' ),
@@ -91,20 +84,6 @@ class ValueValidator {
];
}
- /**
- * Check if status is valid.
- *
- * @since {VERSION}
- *
- * @param string $status Status.
- *
- * @return bool
- */
- public static function is_valid_status( $status ) {
-
- return isset( self::get_allowed_statuses()[ $status ] );
- }
-
/**
* Get allowed types.
*
@@ -112,30 +91,16 @@ class ValueValidator {
*
* @return array
*/
- public static function get_allowed_types() {
+ public static function get_allowed_type() {
return array_merge(
[
'one-time' => __( 'One-time', 'wpforms-lite' ),
],
- self::get_allowed_subscription_types()
+ self::get_allowed_subscription_type()
);
}
- /**
- * Check if type is valid.
- *
- * @since {VERSION}
- *
- * @param string $type Type.
- *
- * @return bool
- */
- public static function is_valid_type( $type ) {
-
- return isset( self::get_allowed_types()[ $type ] );
- }
-
/**
* Get allowed subscription types.
*
@@ -143,25 +108,11 @@ class ValueValidator {
*
* @return array
*/
- public static function get_allowed_subscription_types() {
+ public static function get_allowed_subscription_type() {
return [
'subscription' => __( 'Subscription', 'wpforms-lite' ),
'renewal' => __( 'Renewal', 'wpforms-lite' ),
];
}
-
- /**
- * Check if subscription type is valid.
- *
- * @since {VERSION}
- *
- * @param string $type Type.
- *
- * @return bool
- */
- public static function is_valid_subscription_type( $type ) {
-
- return isset( self::get_allowed_subscription_types()[ $type ] );
- }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment