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
20 changes: 11 additions & 9 deletions classes/helpers/FrmEntriesListHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,18 +278,20 @@ protected function display_tablenav( $which ) {
protected function extra_tablenav( $which ) {
$form_id = FrmAppHelper::simple_get( 'form', 'absint' );

if ( $which === 'top' && ! $form_id ) {
echo '<div class="alignleft actions">';
if ( $which !== 'top' || $form_id ) {
return;
}

// Override the referrer to prevent it from being used for the screen options.
echo '<input type="hidden" name="_wp_http_referer" value="" />';
echo '<div class="alignleft actions">';

echo '<label for="form" class="screen-reader-text">' . esc_html__( 'Filter by form', 'formidable' ) . '</label>';
// Override the referrer to prevent it from being used for the screen options.
echo '<input type="hidden" name="_wp_http_referer" value="" />';

FrmFormsHelper::forms_dropdown( 'form', $form_id, array( 'blank' => __( 'View all forms', 'formidable' ) ) );
submit_button( __( 'Filter', 'formidable' ), 'filter_action action', '', false, array( 'id' => 'post-query-submit' ) );
echo '</div>';
}
echo '<label for="form" class="screen-reader-text">' . esc_html__( 'Filter by form', 'formidable' ) . '</label>';

FrmFormsHelper::forms_dropdown( 'form', $form_id, array( 'blank' => __( 'View all forms', 'formidable' ) ) );
submit_button( __( 'Filter', 'formidable' ), 'filter_action action', '', false, array( 'id' => 'post-query-submit' ) );
echo '</div>';
}

/**
Expand Down
20 changes: 11 additions & 9 deletions classes/helpers/FrmXMLHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -961,16 +961,18 @@ private static function update_custom_style_setting_on_import( &$form ) {
private static function update_custom_style_setting_after_import( $form_id ) {
$form = FrmForm::getOne( $form_id );

if ( $form && isset( $form->options['old_style'] ) ) {
$form = (array) $form;
$saved_style = $form['options']['custom_style'];
$form['options']['custom_style'] = $form['options']['old_style'];
self::update_custom_style_setting_on_import( $form );
$has_changed = $form['options']['custom_style'] != $saved_style && $form['options']['custom_style'] != $form['options']['old_style']; // phpcs:ignore Universal.Operators.StrictComparisons, SlevomatCodingStandard.Files.LineLength.LineTooLong
if ( ! $form || ! isset( $form->options['old_style'] ) ) {
return;
}

if ( $has_changed ) {
FrmForm::update( $form['id'], $form );
}
$form = (array) $form;
$saved_style = $form['options']['custom_style'];
$form['options']['custom_style'] = $form['options']['old_style'];
self::update_custom_style_setting_on_import( $form );
$has_changed = $form['options']['custom_style'] != $saved_style && $form['options']['custom_style'] != $form['options']['old_style']; // phpcs:ignore Universal.Operators.StrictComparisons, SlevomatCodingStandard.Files.LineLength.LineTooLong

if ( $has_changed ) {
FrmForm::update( $form['id'], $form );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@
* }
* }
*
* Also catches:
* function example($value) {
* $var = get_value();
* if ($var && $condition) {
* // 5+ lines of code
* return $result;
* }
* }
*
* Good:
* function example($value) {
* if (! $value) {
Expand Down Expand Up @@ -89,10 +98,16 @@ public function process( File $phpcsFile, $stackPtr ) {

// Check if the first statement is an if.
if ( $tokens[ $firstStatement ]['code'] !== T_IF ) {
return;
}
// If the first statement isn't an if, check if it's a simple assignment
// followed by an if statement (pattern: $var = value; if ($var && condition) {...})
$ifToken = $this->findIfAfterSimpleSetup( $phpcsFile, $firstStatement, $scopeCloser );

$ifToken = $firstStatement;
if ( false === $ifToken ) {
return;
}
} else {
$ifToken = $firstStatement;
}

// Check if this if has an else or elseif - skip those.
if ( isset( $tokens[ $ifToken ]['scope_closer'] ) ) {
Expand All @@ -110,7 +125,7 @@ public function process( File $phpcsFile, $stackPtr ) {

$ifScopeCloser = $tokens[ $ifToken ]['scope_closer'];

// Check if the if statement is the only thing in the function body.
// Check if the if statement is the main logic in the function body.
// The next non-whitespace/non-comment token after the if's closing brace should be the function's closing brace.
$afterIf = $phpcsFile->findNext(
array( T_WHITESPACE, T_COMMENT, T_DOC_COMMENT, T_DOC_COMMENT_OPEN_TAG, T_DOC_COMMENT_CLOSE_TAG, T_DOC_COMMENT_STAR, T_DOC_COMMENT_STRING, T_DOC_COMMENT_TAG, T_DOC_COMMENT_WHITESPACE ),
Expand Down Expand Up @@ -538,4 +553,81 @@ private function getConditionString( File $phpcsFile, $ifToken ) {

return $condition;
}

/**
* Find an if statement that comes after simple setup code.
*
* @param File $phpcsFile The file being scanned.
* @param int $firstToken The first token in the function.
* @param int $scopeCloser The function's scope closer.
*
* @return false|int Position of the if token, or false if not found.
*/
private function findIfAfterSimpleSetup( File $phpcsFile, $firstToken, $scopeCloser ) {
$tokens = $phpcsFile->getTokens();

// Allow simple assignments: $var = value;
$current = $firstToken;
$setupStatements = 0;
$maxSetupStatements = 3; // Allow up to 3 simple setup statements

while ( $current < $scopeCloser && $setupStatements < $maxSetupStatements ) {
// Skip whitespace
if ( $tokens[ $current ]['code'] === T_WHITESPACE ) {
$current++;
continue;
}

// Check if this is a simple assignment
if ( $this->isSimpleAssignment( $phpcsFile, $current ) ) {
$setupStatements++;
// Move to the end of this statement (semicolon)
$semicolon = $phpcsFile->findNext( T_SEMICOLON, $current + 1, $scopeCloser );

if ( false === $semicolon ) {
return false;
}
$current = $semicolon + 1;
continue;
}

// If we find an if after the setup, return it
if ( $tokens[ $current ]['code'] === T_IF ) {
return $current;
}

// Anything else means this pattern doesn't match
return false;
}

return false;
}

/**
* Check if a token represents a simple assignment statement.
*
* @param File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the token to check.
*
* @return bool True if this is a simple assignment.
*/
private function isSimpleAssignment( File $phpcsFile, $stackPtr ) {
$tokens = $phpcsFile->getTokens();

// Look for pattern: $variable = value;
if ( $tokens[ $stackPtr ]['code'] !== T_VARIABLE ) {
return false;
}

// Next non-whitespace should be =
$next = $phpcsFile->findNext( T_WHITESPACE, $stackPtr + 1, null, true );

if ( false === $next || $tokens[ $next ]['code'] !== T_EQUAL ) {
return false;
}

// Find the semicolon to confirm this is a complete assignment
$semicolon = $phpcsFile->findNext( T_SEMICOLON, $next + 1, null );
return false !== $semicolon;
}
}