Skip to content

Fix square location ID issues#2536

Merged
Crabcyborg merged 8 commits into
masterfrom
fix_square_location_id_issues
Oct 8, 2025
Merged

Fix square location ID issues#2536
Crabcyborg merged 8 commits into
masterfrom
fix_square_location_id_issues

Conversation

@Crabcyborg
Copy link
Copy Markdown
Contributor

@Crabcyborg Crabcyborg commented Oct 8, 2025

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Oct 8, 2025

Walkthrough

Adds mode-aware (auto/test/live) handling to Square currency and location retrieval, bumps DB version to 104, and adds a migrate_to_104 migration that refreshes test/live location IDs via the new per-mode getters; also expands an E2E test's accepted H1 banner text.

Changes

Cohort / File(s) Summary of Changes
Mode-aware currency & location
square/helpers/FrmSquareLiteConnectHelper.php
- Added mode parameter to get_location_id($force = false, $mode = 'auto') and get_merchant_currency($force = false, $mode = 'auto').
- Threaded frm_square_api_mode / testMode into request bodies when mode != 'auto'.
- Read/write location_id and currency under mode-specific option keys.
- check_server_for_oauth_merchant_id now calls per-mode getters.
DB version bump
classes/helpers/FrmAppHelper.php
- Incremented public static $db_version from 103 to 104.
Migration to 104
classes/models/FrmMigrate.php
- Added migrate_to_104() and added 104 to migration flow.
- Migration ensures at least one Lite mode exists and calls FrmSquareLiteConnectHelper::get_location_id(true, $mode) for 'test' and 'live' to refresh IDs.
- Note: migrate_to_104() appears duplicated in the file.
Test adjustment
tests/cypress/e2e/Forms/formPageDataValidation.cy.js
- Expanded acceptable H1 heading list to include an additional upgrade-banner text.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor Caller as Caller
  participant Helper as FrmSquareLiteConnectHelper
  participant Options as WP_Options
  participant Server as Square_Backend

  Caller->>Helper: get_location_id(force?, mode)
  Helper->>Options: read location_id[mode]
  alt cached && !force
    Options-->>Helper: cached location_id
    Helper-->>Caller: return location_id
  else
    Helper->>Server: POST /merchant/locations {frm_square_api_mode, testMode?}
    Server-->>Helper: location_id
    Helper->>Options: save location_id[mode]
    Helper-->>Caller: return location_id
  end

  Caller->>Helper: get_merchant_currency(force?, mode)
  Helper->>Options: read currency[mode]
  alt cached && !force
    Options-->>Helper: cached currency
    Helper-->>Caller: return currency
  else
    Helper->>Server: POST /merchant/currency {frm_square_api_mode, testMode?}
    Server-->>Helper: currency
    Helper->>Options: save currency[mode]
    Helper-->>Caller: return currency
  end
Loading
sequenceDiagram
  autonumber
  actor Migrator as FrmMigrate
  participant Helper as FrmSquareLiteConnectHelper

  Migrator->>Migrator: migrate_to_104()
  Migrator->>Migrator: ensure at least one Lite mode set
  Migrator->>Helper: get_location_id(true, 'test')
  Helper-->>Migrator: refreshed test location
  Migrator->>Helper: get_location_id(true, 'live')
  Helper-->>Migrator: refreshed live location
  Migrator-->>Migrator: complete migration
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested reviewers

  • lauramekaj1

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Title Check ✅ Passed The title concisely highlights a real and significant aspect of the proposed changes by referring to the square location ID fix, making it clear and specific without unnecessary detail.
Description Check ✅ Passed The description clearly references the related support ticket and a pre-release build, demonstrating relevance to the changeset without being off-topic.
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix_square_location_id_issues

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
square/helpers/FrmSquareLiteConnectHelper.php (2)

551-572: Consider refactoring to eliminate side effects and improve maintainability.

The method has several concerns:

  1. Side effect on global state (line 561): Modifying $_POST['testMode'] in a getter method is unexpected and could affect other code that reads $_POST later in the request. This is done to communicate the mode to get_mode_value_from_post() (line 526), but it's a fragile approach.

  2. Missing input validation: The $mode parameter is not validated. If a caller passes an invalid value (e.g., 'invalid'), line 561 would treat it as 'live' (ternary defaults to 0), and get_active_mode_option_name_suffix() would construct an incorrect option name like 'frm_square_connect_merchant_location_id_invalid'.

  3. Code duplication: Lines 559-563 are duplicated in get_merchant_currency() (lines 672-676).

Consider these improvements:

Option 1 (preferred): Pass mode through the call chain instead of modifying $_POST:

// Extend post_with_authenticated_body to accept optional mode
private static function post_with_authenticated_body( $action, $additional_body = array(), $mode = null ) {
    $body = array_merge( self::get_standard_authenticated_body( $mode ), $additional_body );
    // ... rest of method
}

private static function get_standard_authenticated_body( $mode = null ) {
    if ( null === $mode ) {
        $mode = self::get_mode_value_from_post();
    }
    // ... use $mode directly instead of reading from $_POST
}

Option 2: At minimum, validate the mode parameter:

public static function get_location_id( $force = false, $mode = 'auto' ) {
    // Validate mode
    if ( ! in_array( $mode, array( 'auto', 'live', 'test' ), true ) ) {
        $mode = 'auto';
    }
    // ... rest of method
}

Option 3: Extract the duplicate logic into a helper method:

private static function prepare_mode_request_body( $mode ) {
    $request_body = array();
    if ( 'auto' !== $mode ) {
        $_POST['testMode'] = 'test' === $mode ? 1 : 0;
        $request_body['frm_square_api_mode'] = $mode;
    }
    return $request_body;
}

664-685: Same concerns as get_location_id().

This method has the same issues as get_location_id():

  • Modifies $_POST['testMode'] as a side effect (line 674)
  • Missing input validation for the $mode parameter
  • Duplicate code (lines 672-676 are identical to lines 559-563 in get_location_id())

Apply the same refactoring suggestions from the previous comment to eliminate side effects, add validation, and extract the duplicate logic into a shared helper method.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f882406 and 8fd27b6.

📒 Files selected for processing (1)
  • square/helpers/FrmSquareLiteConnectHelper.php (5 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
square/helpers/FrmSquareLiteConnectHelper.php (2)
square/js/settings.js (1)
  • mode (7-7)
square/controllers/FrmSquareLiteActionsController.php (1)
  • get_location_id (554-556)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (6)
  • GitHub Check: PHP 7.4 tests in WP trunk
  • GitHub Check: PHP 8 tests in WP trunk
  • GitHub Check: PHP 7.4 tests in WP trunk
  • GitHub Check: PHP 8 tests in WP trunk
  • GitHub Check: Cypress
  • GitHub Check: Cypress
🔇 Additional comments (1)
square/helpers/FrmSquareLiteConnectHelper.php (1)

454-455: LGTM!

The calls correctly pass the mode parameter to fetch mode-specific currency and location data.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
classes/models/FrmMigrate.php (1)

328-328: Minor pre-existing issue: duplicate migration number.

The migrations array contains 16 twice. While this doesn't affect the current PR, consider removing the duplicate in a future cleanup.

Apply this diff to remove the duplicate:

-		$migrations = array( 16, 11, 16, 17, 23, 25, 86, 90, 97, 98, 101, 104 );
+		$migrations = array( 11, 16, 17, 23, 25, 86, 90, 97, 98, 101, 104 );
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8fd27b6 and db2d128.

📒 Files selected for processing (2)
  • classes/helpers/FrmAppHelper.php (1 hunks)
  • classes/models/FrmMigrate.php (2 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
classes/models/FrmMigrate.php (1)
square/helpers/FrmSquareLiteConnectHelper.php (1)
  • FrmSquareLiteConnectHelper (6-726)
🪛 PHPMD (2.15.0)
classes/models/FrmMigrate.php

406-413: Avoid unused private methods such as 'migrate_to_104'. (undefined)

(UnusedPrivateMethod)

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
  • GitHub Check: Run PHP Syntax inspection (8.3)
  • GitHub Check: PHP 8 tests in WP trunk
  • GitHub Check: PHP 7.4 tests in WP trunk
  • GitHub Check: Cypress
🔇 Additional comments (2)
classes/helpers/FrmAppHelper.php (1)

13-13: LGTM!

The database version increment aligns correctly with the new migrate_to_104() method added in FrmMigrate.php.

classes/models/FrmMigrate.php (1)

406-413: Verify get_location_id() handles missing merchant credentials. We couldn’t locate post_with_authenticated_body() in the helper—please confirm it skips the API call or returns false (without error) when no merchant ID is configured for the requested mode.

Comment thread classes/models/FrmMigrate.php
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
tests/cypress/e2e/Forms/formPageDataValidation.cy.js (1)

33-33: Separate promotional banner update from Square integration scope.

The new “No Brainer Sale” heading is unrelated to the Square location-ID fixes and makes this brittle banner test require updates on every marketing change. Consider extracting banner-text validation into its own PR (or suite) and improving resilience by:

  • Matching generic promo patterns (e.g. /get \d+% off/i) instead of exact strings
  • Verifying key elements (upgrade CTA presence, pricing info) rather than full text

Questions:

  1. Was this promo-text change intentionally bundled with the Square location-ID work?
  2. If so, is there any hidden dependency between the two?
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between db2d128 and d0a6ccb.

📒 Files selected for processing (1)
  • tests/cypress/e2e/Forms/formPageDataValidation.cy.js (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
  • GitHub Check: Cypress
  • GitHub Check: Run PHP Syntax inspection (8.3)
  • GitHub Check: PHP 8 tests in WP trunk
  • GitHub Check: PHP 7.4 tests in WP trunk

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d0a6ccb and a5ca867.

📒 Files selected for processing (1)
  • classes/models/FrmMigrate.php (2 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
classes/models/FrmMigrate.php (1)
square/helpers/FrmSquareLiteConnectHelper.php (2)
  • FrmSquareLiteConnectHelper (6-726)
  • get_merchant_id (356-361)
🪛 PHPMD (2.15.0)
classes/models/FrmMigrate.php

406-414: Avoid unused private methods such as 'migrate_to_104'. (undefined)

(UnusedPrivateMethod)

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
  • GitHub Check: Cypress
  • GitHub Check: Run PHP Syntax inspection (8.3)
  • GitHub Check: PHP 8 tests in WP trunk
  • GitHub Check: PHP 7.4 tests in WP trunk
🔇 Additional comments (1)
classes/models/FrmMigrate.php (1)

328-328: LGTM!

The migration number is correctly added to the array and aligns with the updated database version.

Comment thread classes/models/FrmMigrate.php
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
tests/cypress/e2e/Forms/formPageDataValidation.cy.js (1)

21-21: LGTM! Consider extracting banner text patterns for maintainability.

The addition of the new promotional text variant is correct and consistent with the existing validation logic.

However, the condition is growing long with multiple text patterns. For better maintainability, consider extracting these into a helper:

+const isValidUpgradeBanner = (text) => {
+  const validPatterns = [
+    'upgrading to PRO',
+    'Get 60% Off Pro!',
+    'Get the Deal',
+    'upgrading for 60% off during our No Brainer Sale!',
+  ];
+  return validPatterns.some(pattern => text.includes(pattern)) || 
+         text.match(/GET \d+% OFF|SAVE \d+%/);
+};
+
 cy.get( '.frm-upgrade-bar .frm-upgrade-bar-inner > a, #frm_sale_banner a:not(.dismiss)' )
   .then( $el => {
     const text = $el.text().trim();
     const href = $el.attr( 'href' );

-    if ( href && ( text.includes( 'upgrading to PRO' ) || text.includes( 'Get 60% Off Pro!' ) || text.includes( 'Get the Deal' ) || text.match( /GET \d+% OFF|SAVE \d+%/ ) || text.includes( 'upgrading for 60% off during our No Brainer Sale!' ) ) ) {
+    if ( href && isValidUpgradeBanner(text) ) {

This makes it easier to add/remove promotional text variants in the future.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a5ca867 and 61bd5f6.

📒 Files selected for processing (1)
  • tests/cypress/e2e/Forms/formPageDataValidation.cy.js (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
  • GitHub Check: Run PHP Syntax inspection (8.3)
  • GitHub Check: Cypress
  • GitHub Check: PHP 7.4 tests in WP trunk
  • GitHub Check: PHP 8 tests in WP trunk

@Crabcyborg Crabcyborg merged commit 16022b3 into master Oct 8, 2025
16 checks passed
@Crabcyborg Crabcyborg deleted the fix_square_location_id_issues branch October 8, 2025 17:22
stephywells pushed a commit that referenced this pull request Apr 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant