Skip to content
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
10 changes: 3 additions & 7 deletions classes/models/FrmEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
6 changes: 2 additions & 4 deletions classes/models/FrmForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions classes/views/xml/forms_xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand All @@ -34,7 +34,7 @@
<parent_form_id><?php echo esc_html( $form->parent_form_id ); ?></parent_form_id>
<?php

$fields = FrmDb::get_results( $wpdb->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 );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
<?php
/**
* Formidable_Sniffs_CodeAnalysis_RemoveRedundantWpdbPrefixSniff
*
* Detects redundant $wpdb->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();
}
}
}
1 change: 1 addition & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@
<rule ref="Formidable.CodeAnalysis.PreferStrictComparison" />
<rule ref="Formidable.CodeAnalysis.PreferStrictInArray" />
<rule ref="Formidable.CodeAnalysis.PreferObGetClean" />
<rule ref="Formidable.CodeAnalysis.RemoveRedundantWpdbPrefix" />
<rule ref="Formidable.CodeAnalysis.PreferKsesEcho" />
<rule ref="Formidable.CodeAnalysis.MoveVariableBelowEarlyReturn" />
<rule ref="Formidable.CodeAnalysis.FlipNegativeTernary" />
Expand Down