Created
March 12, 2026 16:47
-
-
Save goldenapples/9c68d8ff594bfb0dfb88ec32ba5e30ff to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Import Salesforce ID mappings to RCP Groups. | |
| * | |
| * Usage: | |
| * wp eval-file import-sfdc-mappings.php <csv-path> | |
| * wp eval-file import-sfdc-mappings.php <csv-path> dry-run | |
| * | |
| * Pass "dry-run" as a second argument to preview changes without writing. | |
| */ | |
| if ( ! defined( 'ABSPATH' ) ) { | |
| WP_CLI::error( 'This script must be run with wp eval-file.' ); | |
| } | |
| if ( ! function_exists( 'rcpga_get_group' ) || ! function_exists( 'rcpga_update_group' ) ) { | |
| WP_CLI::error( 'RCP Group Accounts plugin is not active.' ); | |
| } | |
| // Parse arguments from $args (provided by wp eval-file). | |
| $csv_path = $args[0] ?? null; | |
| $dry_run = ( $args[1] ?? '' ) === 'dry-run'; | |
| if ( ! $csv_path ) { | |
| WP_CLI::error( 'Usage: wp eval-file import-sfdc-mappings.php <csv-path> [dry-run]' ); | |
| } | |
| if ( ! file_exists( $csv_path ) ) { | |
| WP_CLI::error( sprintf( 'CSV file not found: %s', $csv_path ) ); | |
| } | |
| $handle = fopen( $csv_path, 'r' ); | |
| if ( ! $handle ) { | |
| WP_CLI::error( sprintf( 'Could not open CSV file: %s', $csv_path ) ); | |
| } | |
| // Read and validate header row. | |
| $header = fgetcsv( $handle ); | |
| if ( ! $header ) { | |
| fclose( $handle ); | |
| WP_CLI::error( 'CSV file is empty or unreadable.' ); | |
| } | |
| // Strip BOM that Excel/Google Sheets may add to UTF-8 CSVs. | |
| $header[0] = preg_replace( '/^\x{FEFF}/u', '', $header[0] ); | |
| $rcp_id_col = array_search( 'RCP IDs', $header, true ); | |
| $sfdc_id_col = array_search( 'SFDC Acct ID', $header, true ); | |
| if ( $rcp_id_col === false || $sfdc_id_col === false ) { | |
| fclose( $handle ); | |
| WP_CLI::error( 'CSV must contain "RCP IDs" and "SFDC Acct ID" columns.' ); | |
| } | |
| if ( $dry_run ) { | |
| WP_CLI::log( 'π DRY RUN β no changes will be made.' ); | |
| } | |
| $updated = 0; | |
| $skipped = 0; | |
| $errors = 0; | |
| $unchanged = 0; | |
| $row_num = 1; | |
| while ( ( $row = fgetcsv( $handle ) ) !== false ) { | |
| $row_num++; | |
| $rcp_id = trim( $row[ $rcp_id_col ] ?? '' ); | |
| $sfdc_id = trim( $row[ $sfdc_id_col ] ?? '' ); | |
| // Skip rows with missing data. | |
| if ( $rcp_id === '' || $sfdc_id === '' ) { | |
| WP_CLI::warning( sprintf( 'Row %d: Missing RCP ID or SFDC ID, skipping.', $row_num ) ); | |
| $skipped++; | |
| continue; | |
| } | |
| // Validate RCP ID is numeric. | |
| if ( ! is_numeric( $rcp_id ) ) { | |
| WP_CLI::warning( sprintf( 'Row %d: Invalid RCP ID "%s", skipping.', $row_num, $rcp_id ) ); | |
| $skipped++; | |
| continue; | |
| } | |
| $group = rcpga_get_group( (int) $rcp_id ); | |
| if ( ! $group ) { | |
| WP_CLI::warning( sprintf( 'Row %d: RCP group %s not found, skipping.', $row_num, $rcp_id ) ); | |
| $errors++; | |
| continue; | |
| } | |
| $current_sfdc_id = $group->get_salesforce_id(); | |
| // Skip if already set to the same value. | |
| if ( $current_sfdc_id === $sfdc_id ) { | |
| $unchanged++; | |
| continue; | |
| } | |
| if ( $current_sfdc_id !== '' ) { | |
| WP_CLI::log( sprintf( | |
| 'Row %d: RCP group %s (%s) β overwriting "%s" with "%s".', | |
| $row_num, | |
| $rcp_id, | |
| $group->get_name(), | |
| $current_sfdc_id, | |
| $sfdc_id | |
| ) ); | |
| } | |
| if ( $dry_run ) { | |
| WP_CLI::log( sprintf( | |
| '[DRY RUN] Would update RCP group %s (%s) β SFDC ID: %s', | |
| $rcp_id, | |
| $group->get_name(), | |
| $sfdc_id | |
| ) ); | |
| $updated++; | |
| continue; | |
| } | |
| $result = rcpga_update_group( (int) $rcp_id, [ | |
| 'salesforce_id' => $sfdc_id, | |
| ] ); | |
| if ( ! $result ) { | |
| WP_CLI::warning( sprintf( 'Row %d: Failed to update RCP group %s.', $row_num, $rcp_id ) ); | |
| $errors++; | |
| continue; | |
| } | |
| $updated++; | |
| } | |
| fclose( $handle ); | |
| WP_CLI::log( '' ); | |
| WP_CLI::success( sprintf( | |
| 'Import complete. Updated: %d | Unchanged: %d | Skipped: %d | Errors: %d', | |
| $updated, | |
| $unchanged, | |
| $skipped, | |
| $errors | |
| ) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment