diff --git a/classes/models/FrmEntry.php b/classes/models/FrmEntry.php index 7a1c30ca72..6ba86a39f9 100644 --- a/classes/models/FrmEntry.php +++ b/classes/models/FrmEntry.php @@ -87,10 +87,8 @@ public static function is_duplicate( $new_values, $values ) { unset( $check_val['name'] ); } - $check_val = apply_filters( 'frm_duplicate_check_val', $check_val ); - - global $wpdb; - $entry_exists = FrmDb::get_col( $wpdb->prefix . 'frm_items', $check_val, 'id', array( 'order_by' => 'created_at DESC' ) ); + $check_val = apply_filters( 'frm_duplicate_check_val', $check_val ); + $entry_exists = FrmDb::get_col( 'frm_items', $check_val, 'id', array( 'order_by' => 'created_at DESC' ) ); if ( ! $entry_exists || ! isset( $values['item_meta'] ) ) { return false; @@ -596,14 +594,12 @@ public static function get_meta( $entry ) { * @return bool */ public static function exists( $id ) { - global $wpdb; - if ( FrmDb::check_cache( $id, 'frm_entry' ) ) { return true; } $where = is_numeric( $id ) ? array( 'id' => $id ) : array( 'item_key' => $id ); - $id = FrmDb::get_var( $wpdb->prefix . 'frm_items', $where ); + $id = FrmDb::get_var( 'frm_items', $where ); return $id && $id > 0; } diff --git a/classes/models/FrmForm.php b/classes/models/FrmForm.php index c1a95a684c..d086091a9f 100644 --- a/classes/models/FrmForm.php +++ b/classes/models/FrmForm.php @@ -715,7 +715,7 @@ public static function destroy( $id ) { $id = $form->id; // Disconnect the entries from this form - $entries = FrmDb::get_col( $wpdb->prefix . 'frm_items', array( 'form_id' => $id ) ); + $entries = FrmDb::get_col( 'frm_items', array( 'form_id' => $id ) ); foreach ( $entries as $entry_id ) { FrmEntry::destroy( $entry_id ); @@ -753,9 +753,7 @@ public static function destroy( $id ) { * @return int The number of forms deleted */ public static function scheduled_delete( $delete_timestamp = '' ) { - global $wpdb; - - $trash_forms = FrmDb::get_results( $wpdb->prefix . 'frm_forms', array( 'status' => 'trash' ), 'id, parent_form_id, options' ); + $trash_forms = FrmDb::get_results( 'frm_forms', array( 'status' => 'trash' ), 'id, parent_form_id, options' ); if ( ! $trash_forms ) { return 0; diff --git a/classes/views/xml/forms_xml.php b/classes/views/xml/forms_xml.php index eec78ad8dd..15dc06c4d0 100644 --- a/classes/views/xml/forms_xml.php +++ b/classes/views/xml/forms_xml.php @@ -15,7 +15,7 @@ // fetch 20 posts at a time rather than loading the entire table into memory while ( $next_set = array_splice( $item_ids, 0, 20 ) ) { - $forms = FrmDb::get_results( $wpdb->prefix . 'frm_forms', array( 'id' => $next_set ) ); + $forms = FrmDb::get_results( 'frm_forms', array( 'id' => $next_set ) ); // Begin Loop foreach ( $forms as $form ) { @@ -34,7 +34,7 @@ parent_form_id ); ?> prefix . 'frm_fields', array( 'form_id' => $form->id ), '*', array( 'order_by' => 'field_order' ) ); + $fields = FrmDb::get_results( 'frm_fields', array( 'form_id' => $form->id ), '*', array( 'order_by' => 'field_order' ) ); foreach ( $fields as $field ) { FrmXMLHelper::prepare_field_for_export( $field ); diff --git a/phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/RemoveRedundantWpdbPrefixSniff.php b/phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/RemoveRedundantWpdbPrefixSniff.php new file mode 100644 index 0000000000..7103de4c5b --- /dev/null +++ b/phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/RemoveRedundantWpdbPrefixSniff.php @@ -0,0 +1,177 @@ +prefix usage in FrmDb function calls. + * + * @package Formidable\Sniffs + */ + +namespace Formidable\Sniffs\CodeAnalysis; + +use PHP_CodeSniffer\Sniffs\Sniff; +use PHP_CodeSniffer\Files\File; + +/** + * Detects and removes redundant $wpdb->prefix in FrmDb::get_var, FrmDb::get_col, and FrmDb::get_results calls. + * + * Bad: + * FrmDb::get_var( $wpdb->prefix . 'frm_forms', $where ); + * + * Good: + * FrmDb::get_var( 'frm_forms', $where ); + * + * The FrmDb functions automatically add $wpdb->prefix when the table name is a simple string without spaces. + */ +class RemoveRedundantWpdbPrefixSniff implements Sniff { + + /** + * Target FrmDb methods. + * + * @var array + */ + private $targetMethods = array( + 'get_var', + 'get_col', + 'get_results', + ); + + /** + * Returns an array of tokens this test wants to listen for. + * + * @return array + */ + public function register() { + return array( T_STRING ); + } + + /** + * Processes this test, when one of its tokens is encountered. + * + * @param File $phpcsFile The file being scanned. + * @param int $stackPtr The position of the current token in the stack passed in $tokens. + * + * @return void + */ + public function process( File $phpcsFile, $stackPtr ) { + $tokens = $phpcsFile->getTokens(); + + // Check if this is "FrmDb". + if ( $tokens[ $stackPtr ]['content'] !== 'FrmDb' ) { + return; + } + + // Find the :: operator. + $doubleColon = $phpcsFile->findNext( T_WHITESPACE, $stackPtr + 1, null, true ); + + if ( false === $doubleColon || $tokens[ $doubleColon ]['code'] !== T_DOUBLE_COLON ) { + return; + } + + // Find the method name. + $methodToken = $phpcsFile->findNext( T_WHITESPACE, $doubleColon + 1, null, true ); + + if ( false === $methodToken || $tokens[ $methodToken ]['code'] !== T_STRING ) { + return; + } + + $methodName = $tokens[ $methodToken ]['content']; + + if ( ! in_array( $methodName, $this->targetMethods, true ) ) { + return; + } + + // Find the opening parenthesis. + $openParen = $phpcsFile->findNext( T_WHITESPACE, $methodToken + 1, null, true ); + + if ( false === $openParen || $tokens[ $openParen ]['code'] !== T_OPEN_PARENTHESIS ) { + return; + } + + // Check the first argument for $wpdb->prefix . 'simple_string' pattern. + $firstArgStart = $phpcsFile->findNext( T_WHITESPACE, $openParen + 1, null, true ); + + if ( false === $firstArgStart ) { + return; + } + + // Look for $wpdb. + if ( $tokens[ $firstArgStart ]['code'] !== T_VARIABLE || $tokens[ $firstArgStart ]['content'] !== '$wpdb' ) { + return; + } + + // Find the -> operator. + $objectOp = $phpcsFile->findNext( T_WHITESPACE, $firstArgStart + 1, null, true ); + + if ( false === $objectOp || $tokens[ $objectOp ]['code'] !== T_OBJECT_OPERATOR ) { + return; + } + + // Find "prefix". + $prefixToken = $phpcsFile->findNext( T_WHITESPACE, $objectOp + 1, null, true ); + + if ( false === $prefixToken || $tokens[ $prefixToken ]['code'] !== T_STRING || $tokens[ $prefixToken ]['content'] !== 'prefix' ) { + return; + } + + // Find the . concatenation operator. + $concatOp = $phpcsFile->findNext( T_WHITESPACE, $prefixToken + 1, null, true ); + + if ( false === $concatOp || $tokens[ $concatOp ]['code'] !== T_STRING_CONCAT ) { + return; + } + + // Find the string literal. + $stringToken = $phpcsFile->findNext( T_WHITESPACE, $concatOp + 1, null, true ); + + if ( false === $stringToken || $tokens[ $stringToken ]['code'] !== T_CONSTANT_ENCAPSED_STRING ) { + return; + } + + // Get the string value (without quotes). + $stringValue = substr( $tokens[ $stringToken ]['content'], 1, -1 ); + + // Check if it's a simple string (no spaces). + if ( strpos( $stringValue, ' ' ) !== false ) { + return; + } + + // Check that the next token after the string is either a comma or closing paren (simple first arg). + $afterString = $phpcsFile->findNext( T_WHITESPACE, $stringToken + 1, null, true ); + + if ( false === $afterString ) { + return; + } + + if ( $tokens[ $afterString ]['code'] !== T_COMMA && $tokens[ $afterString ]['code'] !== T_CLOSE_PARENTHESIS ) { + return; + } + + $fix = $phpcsFile->addFixableError( + 'Redundant $wpdb->prefix in FrmDb::%s() call. The function adds the prefix automatically for simple table names. Use "%s" instead.', + $firstArgStart, + 'Found', + array( $methodName, $stringValue ) + ); + + if ( true === $fix ) { + $phpcsFile->fixer->beginChangeset(); + + // Remove $wpdb->prefix . and keep just the string. + for ( $i = $firstArgStart; $i <= $concatOp; $i++ ) { + $phpcsFile->fixer->replaceToken( $i, '' ); + } + + // Also remove any whitespace between concat and string. + $nextNonWhitespace = $phpcsFile->findNext( T_WHITESPACE, $concatOp + 1, $stringToken, true ); + + if ( false === $nextNonWhitespace ) { + for ( $i = $concatOp + 1; $i < $stringToken; $i++ ) { + $phpcsFile->fixer->replaceToken( $i, '' ); + } + } + + $phpcsFile->fixer->endChangeset(); + } + } +} diff --git a/phpcs.xml b/phpcs.xml index d9ae94e6ba..9e0d424839 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -252,6 +252,7 @@ +