Skip to content

Adds HPOS compatibility#2345

Merged
ibndawood merged 6 commits intodevelopfrom
add/2260-hpos-compatibility
Oct 31, 2022
Merged

Adds HPOS compatibility#2345
ibndawood merged 6 commits intodevelopfrom
add/2260-hpos-compatibility

Conversation

@ibndawood
Copy link
Copy Markdown
Contributor

@ibndawood ibndawood commented Oct 28, 2022

Changes proposed in this Pull Request:

Closes #2260.

Thank you @mikkamp for your research on HPOS compatibility. This PR is built on top of @mikkamp's inputs. Since the HPOS upgrade recipe is updated, I ran the check again. I concur with the findings in #2260.

This PR:

  • Adds a check to set the screen_id based on order data store. The screen_id is used to enqueue facebook-for-woocommerce-modal, wc-facebook-google-product-category-fields and to render the render_modal_template.
  • Sets the order and orders page URL in the acceptance test.
  • Sets the HPOS compatibility to true.
  • Do the changed files pass phpcs checks? Please remove phpcs:ignore comments in changed files and fix any issues, or delete if not practical.

Detailed test instructions:

  1. Enable HPOS and initial sync of orders by following the instructions given here: https://github.com/woocommerce/woocommerce/wiki/High-Performance-Order-Storage-Upgrade-Recipe-Book#how-to-enable-hpos
  2. Generate the build for this branch and install.
  3. Navigate to WooCommerce > Settings > Advanced > Custom data stores and set Data store for orders option to Use the WooCommerce orders tables.
  4. Navigate to WooCommerce > Orders and click on any single order.
  5. Click on view source and verify facebook-for-woocommerce-modal, wc-facebook-google-product-category-fields and tmpl-facebook-for-woocommerce-modal are enqueued.
  6. Navigate to WooCommerce > Settings > Advanced > Custom data stores and set Data store for orders option to Use the WordPress posts table.
  7. Navigate to WooCommerce > Orders and click on any single order.
  8. Click on view source and verify facebook-for-woocommerce-modal, wc-facebook-google-product-category-fields and tmpl-facebook-for-woocommerce-modal are enqueued.

Additional details:

As noted in #2260, there are changes that need to be addressed in the SkyVerge framework for HPOS compatibility. However, based on internal discussions, the part of the framework code that requires changing is not used by the plugin. I need additional eyes here to verify that the matches listed in this comment #2260 (comment) do not affect the plugin.

I am unable to run the Acceptance test which has 2 other code changes.

Changelog entry

Add - HPOS Compatibility

@github-actions github-actions bot added changelog: add A new feature, function, or functionality was added. type: enhancement The issue is a request for an enhancement. labels Oct 28, 2022
@ibndawood ibndawood marked this pull request as ready for review October 28, 2022 15:00
@ibndawood ibndawood requested a review from a team October 28, 2022 15:01
Copy link
Copy Markdown
Contributor

@rawdreeg rawdreeg left a comment

Choose a reason for hiding this comment

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

Thanks, @ibndawood. I could verify facebook-for-woocommerce-modal, wc-facebook-google-product-category-fields and tmpl-facebook-for-woocommerce-modal are enqueued correctly enqueued.

@ibndawood ibndawood merged commit 80f4fa9 into develop Oct 31, 2022
@ibndawood ibndawood deleted the add/2260-hpos-compatibility branch October 31, 2022 12:59
@ibndawood ibndawood mentioned this pull request Nov 15, 2022
facebook-github-bot pushed a commit that referenced this pull request Mar 20, 2025
Summary:
# Add REST API Framework for local APIs called by AJAX

## Changes proposed in this Pull Request:
- Introduces a comprehensive REST API framework for Facebook for WooCommerce
- Migrates existing API functionality to the new structured system
- Improves security with proper nonce handling and request validation
- Adds JavaScript integration for secure client-side API calls
- Provides extensive documentation for future development

Closes #2345 (Replace with actual issue number if applicable)

## Description

This PR introduces a new REST API framework for Facebook for WooCommerce that provides a structured, secure, and extensible way to handle API requests between the WordPress admin and Facebook services.

The previous implementation used ad-hoc REST endpoints with inconsistent validation and error handling. This new framework standardizes how we define endpoints, validate requests, and expose functionality to JavaScript, making the codebase more maintainable and secure.

### Key Components

1. **Controller** (`includes/API/REST/Controller.php`):
   - Central registry for all REST API endpoints
   - Manages endpoint registration and initialization
   - Maintains constants for endpoint handlers and JS-enabled requests

2. **AbstractRESTEndpoint** (`includes/API/REST/AbstractRESTEndpoint.php`):
   - Base class for all endpoint handlers
   - Provides common functionality for permission checks and response formatting
   - Handles JS API definitions generation

3. **Request** (`includes/API/REST/Request.php`):
   - Base class for request validation and parameter handling
   - Sanitizes and validates all incoming data
   - Provides structured access to request parameters

4. **JS_Exposable** (`includes/API/REST/Traits/JS_Exposable.php`):
   - Trait for exposing endpoints to JavaScript
   - Defines interface for JS API integration
   - Generates API definitions for client-side consumption

5. **InitializeRestAPI** (`includes/API/REST/InitializeRestAPI.php`):
   - Initializes the REST API framework
   - Generates JavaScript API definitions
   - Handles script enqueuing and localization

### Security Improvements

The new framework significantly improves security:

- Nonces are no longer exposed globally in JavaScript
- Each page that needs API access creates its own API client with a fresh nonce
- All request parameters are properly validated and sanitized
- Structured error handling prevents leaking sensitive information
- Clear separation between public and private endpoints

### Migration Strategy

We've migrated the existing functionality from `MetaExtension` to the new framework:

- Settings update endpoint (`/settings/update`)
- Uninstall endpoint (`/settings/uninstall`)

The Connection screen has been updated to use the new API framework, ensuring backward compatibility while leveraging the improved architecture.

## Do the changed files pass phpcs checks?

Yes, all new and modified files pass phpcs checks. No phpcs:ignore comments were added.

## Screenshots [Validating old functionality is not broken]

![Screenshot 2025-03-12 at 5 24 03 PM](https://github.com/user-attachments/assets/89871e6b-3c46-4592-8376-7a34ef3c07fc)
![Screenshot 2025-03-12 at 5 25 12 PM](https://github.com/user-attachments/assets/b2cc9c13-e3a3-485b-aa44-e42129e9437a)

## Detailed test instructions

### 1. Basic Functionality

1. Install and activate the plugin
2. Go to WooCommerce > Facebook
3. Verify the connection page loads correctly with no JavaScript errors
4. Check browser console for any errors related to API initialization

### 2. Legacy Connection Flow

1. If not connected, click "Get Started"
2. Complete the Facebook connection flow
3. Verify you're redirected back to WooCommerce and connection is established
4. Check that catalog ID, pixel ID, and other settings are properly saved
5. Verify the connection details are displayed correctly on the settings page

### 3. Enhanced Onboarding

1. Connect to Facebook using the enhanced onboarding flow
2. Verify the iframe splash page loads correctly and displays the Facebook onboarding interface
3. Complete the connection process through the iframe
4. Verify the connection to MICE is established and settings are saved correctly
5. Check that the iframe resizes properly when content changes

### 4. Disconnection Flow

1. With an active connection, click "Disconnect"
2. Confirm the disconnection in the confirmation dialog
3. Verify all Facebook integration settings are cleared
4. Verify the connection page returns to the unconnected state
5. Verify you can reconnect successfully after disconnection

### 5. JavaScript API Testing

1. Open browser developer tools (F12)
2. Go to Console tab
3. Test API client creation:
   ```javascript
   // Get a fresh nonce (you'll need to be on the settings page)
   const nonce = wp_create_nonce('wp_rest');

   // Create API client
   const fbAPI = FacebookWooCommerceAPIFactory(nonce);

   // Examine available methods
   console.log(Object.keys(fbAPI));
   ```

4. Test uninstall endpoint (only if you're willing to disconnect):
   ```javascript
   fbAPI.uninstallSettings()
     .then(response => console.log('Success:', response))
     .catch(error => console.error('Error:', error));
   ```

5. Verify the page reloads and connection is removed

### 6. Error Handling

1. Test invalid API calls to verify error handling:
   ```javascript
   // Missing required parameters
   fbAPI.updateSettings({})
     .then(response => console.log('Success:', response))
     .catch(error => console.log('Error correctly caught:', error));
   ```

2. Verify appropriate error messages are displayed

## Additional details

- The framework is designed to be extensible, making it easy to add new endpoints in the future
- Comprehensive documentation on new endpoint process is provided in `includes/API/REST/README.md`

Pull Request resolved: #2928

Test Plan:
Imported from GitHub, without a `Test Plan:` line.

**!---- (auto-generated) DO NOT EDIT OR PUT ANYTHING AFTER THIS LINE ----!**
MFTRunTestsScript Run / Test Suite: sa_checkout / Test Collection: www / Diff Version V9
https://internalfb.com/intern/testinfra/testrun/9570149282153152
MFTRunTestsScript Run / Test Suite: sa_checkout / Test Collection: bloks / Diff Version V9
https://internalfb.com/intern/testinfra/testrun/1970325111022726

Reviewed By: vinkmeta

Differential Revision: D70733325

Pulled By: sol-loup

fbshipit-source-id: f2275644ac6447d3940294d916020f1a484ca92e
@sol-loup sol-loup mentioned this pull request Mar 25, 2025
7 tasks
SayanPandey pushed a commit to SayanPandey/facebook-for-woocommerce that referenced this pull request Apr 1, 2025
Summary:
# Add REST API Framework for local APIs called by AJAX

## Changes proposed in this Pull Request:
- Introduces a comprehensive REST API framework for Facebook for WooCommerce
- Migrates existing API functionality to the new structured system
- Improves security with proper nonce handling and request validation
- Adds JavaScript integration for secure client-side API calls
- Provides extensive documentation for future development

Closes facebook#2345 (Replace with actual issue number if applicable)

## Description

This PR introduces a new REST API framework for Facebook for WooCommerce that provides a structured, secure, and extensible way to handle API requests between the WordPress admin and Facebook services.

The previous implementation used ad-hoc REST endpoints with inconsistent validation and error handling. This new framework standardizes how we define endpoints, validate requests, and expose functionality to JavaScript, making the codebase more maintainable and secure.

### Key Components

1. **Controller** (`includes/API/REST/Controller.php`):
   - Central registry for all REST API endpoints
   - Manages endpoint registration and initialization
   - Maintains constants for endpoint handlers and JS-enabled requests

2. **AbstractRESTEndpoint** (`includes/API/REST/AbstractRESTEndpoint.php`):
   - Base class for all endpoint handlers
   - Provides common functionality for permission checks and response formatting
   - Handles JS API definitions generation

3. **Request** (`includes/API/REST/Request.php`):
   - Base class for request validation and parameter handling
   - Sanitizes and validates all incoming data
   - Provides structured access to request parameters

4. **JS_Exposable** (`includes/API/REST/Traits/JS_Exposable.php`):
   - Trait for exposing endpoints to JavaScript
   - Defines interface for JS API integration
   - Generates API definitions for client-side consumption

5. **InitializeRestAPI** (`includes/API/REST/InitializeRestAPI.php`):
   - Initializes the REST API framework
   - Generates JavaScript API definitions
   - Handles script enqueuing and localization

### Security Improvements

The new framework significantly improves security:

- Nonces are no longer exposed globally in JavaScript
- Each page that needs API access creates its own API client with a fresh nonce
- All request parameters are properly validated and sanitized
- Structured error handling prevents leaking sensitive information
- Clear separation between public and private endpoints

### Migration Strategy

We've migrated the existing functionality from `MetaExtension` to the new framework:

- Settings update endpoint (`/settings/update`)
- Uninstall endpoint (`/settings/uninstall`)

The Connection screen has been updated to use the new API framework, ensuring backward compatibility while leveraging the improved architecture.

## Do the changed files pass phpcs checks?

Yes, all new and modified files pass phpcs checks. No phpcs:ignore comments were added.

## Screenshots [Validating old functionality is not broken]

![Screenshot 2025-03-12 at 5 24 03 PM](https://github.com/user-attachments/assets/89871e6b-3c46-4592-8376-7a34ef3c07fc)
![Screenshot 2025-03-12 at 5 25 12 PM](https://github.com/user-attachments/assets/b2cc9c13-e3a3-485b-aa44-e42129e9437a)

## Detailed test instructions

### 1. Basic Functionality

1. Install and activate the plugin
2. Go to WooCommerce > Facebook
3. Verify the connection page loads correctly with no JavaScript errors
4. Check browser console for any errors related to API initialization

### 2. Legacy Connection Flow

1. If not connected, click "Get Started"
2. Complete the Facebook connection flow
3. Verify you're redirected back to WooCommerce and connection is established
4. Check that catalog ID, pixel ID, and other settings are properly saved
5. Verify the connection details are displayed correctly on the settings page

### 3. Enhanced Onboarding

1. Connect to Facebook using the enhanced onboarding flow
2. Verify the iframe splash page loads correctly and displays the Facebook onboarding interface
3. Complete the connection process through the iframe
4. Verify the connection to MICE is established and settings are saved correctly
5. Check that the iframe resizes properly when content changes

### 4. Disconnection Flow

1. With an active connection, click "Disconnect"
2. Confirm the disconnection in the confirmation dialog
3. Verify all Facebook integration settings are cleared
4. Verify the connection page returns to the unconnected state
5. Verify you can reconnect successfully after disconnection

### 5. JavaScript API Testing

1. Open browser developer tools (F12)
2. Go to Console tab
3. Test API client creation:
   ```javascript
   // Get a fresh nonce (you'll need to be on the settings page)
   const nonce = wp_create_nonce('wp_rest');

   // Create API client
   const fbAPI = FacebookWooCommerceAPIFactory(nonce);

   // Examine available methods
   console.log(Object.keys(fbAPI));
   ```

4. Test uninstall endpoint (only if you're willing to disconnect):
   ```javascript
   fbAPI.uninstallSettings()
     .then(response => console.log('Success:', response))
     .catch(error => console.error('Error:', error));
   ```

5. Verify the page reloads and connection is removed

### 6. Error Handling

1. Test invalid API calls to verify error handling:
   ```javascript
   // Missing required parameters
   fbAPI.updateSettings({})
     .then(response => console.log('Success:', response))
     .catch(error => console.log('Error correctly caught:', error));
   ```

2. Verify appropriate error messages are displayed

## Additional details

- The framework is designed to be extensible, making it easy to add new endpoints in the future
- Comprehensive documentation on new endpoint process is provided in `includes/API/REST/README.md`

Pull Request resolved: facebook#2928

Test Plan:
Imported from GitHub, without a `Test Plan:` line.

**!---- (auto-generated) DO NOT EDIT OR PUT ANYTHING AFTER THIS LINE ----!**
MFTRunTestsScript Run / Test Suite: sa_checkout / Test Collection: www / Diff Version V9
https://internalfb.com/intern/testinfra/testrun/9570149282153152
MFTRunTestsScript Run / Test Suite: sa_checkout / Test Collection: bloks / Diff Version V9
https://internalfb.com/intern/testinfra/testrun/1970325111022726

Reviewed By: vinkmeta

Differential Revision: D70733325

Pulled By: sol-loup

fbshipit-source-id: f2275644ac6447d3940294d916020f1a484ca92e
mradmeta pushed a commit that referenced this pull request Apr 14, 2025
Summary:
# Add REST API Framework for local APIs called by AJAX

## Changes proposed in this Pull Request:
- Introduces a comprehensive REST API framework for Facebook for WooCommerce
- Migrates existing API functionality to the new structured system
- Improves security with proper nonce handling and request validation
- Adds JavaScript integration for secure client-side API calls
- Provides extensive documentation for future development

Closes #2345 (Replace with actual issue number if applicable)

## Description

This PR introduces a new REST API framework for Facebook for WooCommerce that provides a structured, secure, and extensible way to handle API requests between the WordPress admin and Facebook services.

The previous implementation used ad-hoc REST endpoints with inconsistent validation and error handling. This new framework standardizes how we define endpoints, validate requests, and expose functionality to JavaScript, making the codebase more maintainable and secure.

### Key Components

1. **Controller** (`includes/API/REST/Controller.php`):
   - Central registry for all REST API endpoints
   - Manages endpoint registration and initialization
   - Maintains constants for endpoint handlers and JS-enabled requests

2. **AbstractRESTEndpoint** (`includes/API/REST/AbstractRESTEndpoint.php`):
   - Base class for all endpoint handlers
   - Provides common functionality for permission checks and response formatting
   - Handles JS API definitions generation

3. **Request** (`includes/API/REST/Request.php`):
   - Base class for request validation and parameter handling
   - Sanitizes and validates all incoming data
   - Provides structured access to request parameters

4. **JS_Exposable** (`includes/API/REST/Traits/JS_Exposable.php`):
   - Trait for exposing endpoints to JavaScript
   - Defines interface for JS API integration
   - Generates API definitions for client-side consumption

5. **InitializeRestAPI** (`includes/API/REST/InitializeRestAPI.php`):
   - Initializes the REST API framework
   - Generates JavaScript API definitions
   - Handles script enqueuing and localization

### Security Improvements

The new framework significantly improves security:

- Nonces are no longer exposed globally in JavaScript
- Each page that needs API access creates its own API client with a fresh nonce
- All request parameters are properly validated and sanitized
- Structured error handling prevents leaking sensitive information
- Clear separation between public and private endpoints

### Migration Strategy

We've migrated the existing functionality from `MetaExtension` to the new framework:

- Settings update endpoint (`/settings/update`)
- Uninstall endpoint (`/settings/uninstall`)

The Connection screen has been updated to use the new API framework, ensuring backward compatibility while leveraging the improved architecture.

## Do the changed files pass phpcs checks?

Yes, all new and modified files pass phpcs checks. No phpcs:ignore comments were added.

## Screenshots [Validating old functionality is not broken]

![Screenshot 2025-03-12 at 5 24 03 PM](https://github.com/user-attachments/assets/89871e6b-3c46-4592-8376-7a34ef3c07fc)
![Screenshot 2025-03-12 at 5 25 12 PM](https://github.com/user-attachments/assets/b2cc9c13-e3a3-485b-aa44-e42129e9437a)

## Detailed test instructions

### 1. Basic Functionality

1. Install and activate the plugin
2. Go to WooCommerce > Facebook
3. Verify the connection page loads correctly with no JavaScript errors
4. Check browser console for any errors related to API initialization

### 2. Legacy Connection Flow

1. If not connected, click "Get Started"
2. Complete the Facebook connection flow
3. Verify you're redirected back to WooCommerce and connection is established
4. Check that catalog ID, pixel ID, and other settings are properly saved
5. Verify the connection details are displayed correctly on the settings page

### 3. Enhanced Onboarding

1. Connect to Facebook using the enhanced onboarding flow
2. Verify the iframe splash page loads correctly and displays the Facebook onboarding interface
3. Complete the connection process through the iframe
4. Verify the connection to MICE is established and settings are saved correctly
5. Check that the iframe resizes properly when content changes

### 4. Disconnection Flow

1. With an active connection, click "Disconnect"
2. Confirm the disconnection in the confirmation dialog
3. Verify all Facebook integration settings are cleared
4. Verify the connection page returns to the unconnected state
5. Verify you can reconnect successfully after disconnection

### 5. JavaScript API Testing

1. Open browser developer tools (F12)
2. Go to Console tab
3. Test API client creation:
   ```javascript
   // Get a fresh nonce (you'll need to be on the settings page)
   const nonce = wp_create_nonce('wp_rest');

   // Create API client
   const fbAPI = FacebookWooCommerceAPIFactory(nonce);

   // Examine available methods
   console.log(Object.keys(fbAPI));
   ```

4. Test uninstall endpoint (only if you're willing to disconnect):
   ```javascript
   fbAPI.uninstallSettings()
     .then(response => console.log('Success:', response))
     .catch(error => console.error('Error:', error));
   ```

5. Verify the page reloads and connection is removed

### 6. Error Handling

1. Test invalid API calls to verify error handling:
   ```javascript
   // Missing required parameters
   fbAPI.updateSettings({})
     .then(response => console.log('Success:', response))
     .catch(error => console.log('Error correctly caught:', error));
   ```

2. Verify appropriate error messages are displayed

## Additional details

- The framework is designed to be extensible, making it easy to add new endpoints in the future
- Comprehensive documentation on new endpoint process is provided in `includes/API/REST/README.md`

Pull Request resolved: #2928

Test Plan:
Imported from GitHub, without a `Test Plan:` line.

**!---- (auto-generated) DO NOT EDIT OR PUT ANYTHING AFTER THIS LINE ----!**
MFTRunTestsScript Run / Test Suite: sa_checkout / Test Collection: www / Diff Version V9
https://internalfb.com/intern/testinfra/testrun/9570149282153152
MFTRunTestsScript Run / Test Suite: sa_checkout / Test Collection: bloks / Diff Version V9
https://internalfb.com/intern/testinfra/testrun/1970325111022726

Reviewed By: vinkmeta

Differential Revision: D70733325

Pulled By: sol-loup

fbshipit-source-id: f2275644ac6447d3940294d916020f1a484ca92e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog: add A new feature, function, or functionality was added. type: enhancement The issue is a request for an enhancement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HPOS compatibility

2 participants