Changeset 3463654
- Timestamp:
- 02/17/2026 03:57:25 PM (6 weeks ago)
- Location:
- gridly-integration
- Files:
-
- 13 added
- 2 edited
-
tags/1.2 (added)
-
tags/1.2/Resources (added)
-
tags/1.2/Resources/CSS (added)
-
tags/1.2/Resources/CSS/style.css (added)
-
tags/1.2/Resources/Images (added)
-
tags/1.2/Resources/Images/gridly.ico (added)
-
tags/1.2/Resources/html (added)
-
tags/1.2/Resources/html/gridly.html (added)
-
tags/1.2/Resources/js (added)
-
tags/1.2/Resources/js/bootstrap.bundle.min.js (added)
-
tags/1.2/Resources/js/gridly.js (added)
-
tags/1.2/gridly-integration.php (added)
-
tags/1.2/readme.txt (added)
-
trunk/gridly-integration.php (modified) (13 diffs)
-
trunk/readme.txt (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
gridly-integration/trunk/gridly-integration.php
r3454373 r3463654 5 5 * Plugin URI: https://www.gridly.com/integrations/ 6 6 * Description: Description: An integration that you can use to send and receive your WordPress pages with Gridly. 7 * Version: 1. 17 * Version: 1.2 8 8 * Requires at least: 6.2 9 9 * Requires PHP: 7.2 … … 20 20 // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.SchemaChange -- Custom plugin tables (gridly_*) and WPML integration; no core API available. Schema changes only at install/migration. 21 21 define('GRDL_CACHE_GROUP', 'gridly_integration'); 22 // Set GRDL_LOG_API to true in wp-config.php to enable API response logging to wp-content/uploads/gridly-integration/logs/ 23 if (!defined('GRDL_LOG_API')) { 24 define('GRDL_LOG_API', false); 25 } 22 26 23 27 function grdl_invalidate_cache(string $key): void { 24 28 wp_cache_delete($key, GRDL_CACHE_GROUP); 29 } 30 31 function grdl_lang_to_gridly(string $code): string { 32 $parts = explode('-', strtolower($code), 2); 33 if (count($parts) < 2) { 34 return $code; 35 } 36 return $parts[0] . strtoupper($parts[1]); 37 } 38 39 function grdl_lang_from_gridly(string $code): string { 40 if (strlen($code) < 2) { 41 return $code; 42 } 43 $result = ''; 44 for ($i = 0; $i < strlen($code); $i++) { 45 $c = $code[$i]; 46 if ($i > 0 && $c >= 'A' && $c <= 'Z') { 47 $result .= '-' . strtolower($c); 48 } else { 49 $result .= $c; 50 } 51 } 52 return $result; 53 } 54 55 function grdl_log_api_response(string $method, string $url, $response, array $context = []): void { 56 if (!GRDL_LOG_API) { 57 return; 58 } 59 $upload_dir = wp_upload_dir(); 60 $log_dir = $upload_dir['basedir'] . '/gridly-integration/logs/'; 61 if (!is_dir($log_dir)) { 62 wp_mkdir_p($log_dir); 63 } 64 $log_file = $log_dir . 'api-' . gmdate('Y-m-d') . '.log'; 65 $entry = array( 66 'time' => gmdate('Y-m-d H:i:s'), 67 'method' => $method, 68 'url' => $url, 69 'context' => $context, 70 ); 71 if (is_wp_error($response)) { 72 $entry['error'] = $response->get_error_message(); 73 } else { 74 $entry['code'] = wp_remote_retrieve_response_code($response); 75 $body = wp_remote_retrieve_body($response); 76 $entry['body'] = strlen($body) > 2000 ? substr($body, 0, 2000) . '...[truncated]' : $body; 77 } 78 file_put_contents($log_file, wp_json_encode($entry, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . "\n---\n", FILE_APPEND | LOCK_EX); 25 79 } 26 80 $GRDL_WP_PLUGIN_isWpmlIsntalled = false; … … 138 192 $csv_content = ''; 139 193 140 // Add the header row to the CSV content with quotation marks around each item 194 // Add the header row to the CSV content with quotation marks around each item (use Gridly format for lang codes: pt-br -> ptBR) 195 $source_lang = grdl_lang_to_gridly((string)$xliff->file['source-language']); 196 $target_lang = grdl_lang_to_gridly((string)$xliff->file['target-language']); 141 197 $csv_content .= implode(',', array_map(function($header) { 142 198 return '"' . str_replace('"', '""', $header) . '"'; // Escape any inner double quotes in the header 143 }, array('_recordId', '_pathTag', (string)$xliff->file['source-language'], (string)$xliff->file['target-language']))) . "\n";199 }, array('_recordId', '_pathTag', $source_lang, $target_lang))) . "\n"; 144 200 145 201 // Loop through each trans-unit in the XLIFF file and append to CSV content with quotation marks around each item … … 175 231 $csv = array_map('str_getcsv', explode("\n", $csv_data)); 176 232 177 // Get the header row of the CSV 233 // Get the header row of the CSV (headers from Gridly are in ptBR format) 178 234 $header = array_shift($csv); 179 235 180 // Get the column index of the given language 181 $lang_col = array_search( $target_lang, $header);236 // Get the column index of the given language (convert target_lang to Gridly format for lookup) 237 $lang_col = array_search(grdl_lang_to_gridly($target_lang), $header); 182 238 183 239 // Loop through the CSV rows and update the XLIFF as needed … … 258 314 ) 259 315 ); 316 grdl_log_api_response('POST', $url, $response, array('job_id' => $job_id)); 260 317 261 318 // Check for errors … … 508 565 { 509 566 // Use the WordPress HTTP API to make the GET request 567 $url = 'https://api.gridly.com/v1/views/' . $profile->view_id; 510 568 $response = wp_remote_get( 511 esc_url( 'https://api.gridly.com/v1/views/' . $profile->view_id),569 esc_url($url), 512 570 array( 513 571 'headers' => array( … … 517 575 ) 518 576 ); 577 grdl_log_api_response('GET', $url, $response, array('action' => 'get_view', 'lang_col' => $lang_col)); 519 578 520 579 // Check if the request was successful … … 525 584 } 526 585 527 $columns = GRDL_WP_PLUGIN_GET_COLUMN_NAMES(wp_remote_retrieve_body($response)); 528 529 if (!in_array($lang_col, $columns)) { 586 $columns = GRDL_WP_PLUGIN_GET_COLUMN_NAMES(wp_remote_retrieve_body($response)); 587 $lang_col_g = grdl_lang_to_gridly($lang_col); 588 589 if (!in_array($lang_col_g, $columns)) { 530 590 531 591 try { 532 592 $request_body = '{ 533 "id": "' . $lang_col . '",534 "name": "' . $lang_col . '",593 "id": "' . $lang_col_g . '", 594 "name": "' . $lang_col_g . '", 535 595 "type": "language", 536 "languageCode": "' . $lang_col . '"596 "languageCode": "' . $lang_col_g . '" 537 597 }'; 538 598 539 599 if ($target) { 540 600 $request_data = '{ 541 "id": "' . $lang_col . '",601 "id": "' . $lang_col_g . '", 542 602 "localizationType": "targetLanguage", 543 "name": "' . $lang_col . '",603 "name": "' . $lang_col_g . '", 544 604 "type": "language", 545 "languageCode": "' . $lang_col . '"605 "languageCode": "' . $lang_col_g . '" 546 606 }'; 547 607 } 548 608 549 $response = wp_remote_post( 550 'https://api.gridly.com/v1/views/' . $profile->view_id . '/columns', 609 $col_url = 'https://api.gridly.com/v1/views/' . $profile->view_id . '/columns'; 610 $response = wp_remote_post( 611 $col_url, 551 612 array( 552 613 'headers' => array( … … 557 618 ) 558 619 ); 620 grdl_log_api_response('POST', $col_url, $response, array('action' => 'create_column', 'lang_col' => $lang_col)); 559 621 $setting_data = GRDL_WP_PLUGIN_GET_GRIDLY_GENERAL_SETTINGS(); 560 622 $set_dependencies = $setting_data->set_dependencies; … … 587 649 ); 588 650 589 // Define the request data 651 // Define the request data (source_lang and target_lang must be in Gridly format, e.g. ptBR) 590 652 $request_body = wp_json_encode(array( 591 'sourceColumnId' => $source_lang,592 'targetColumnId' => $target_lang653 'sourceColumnId' => grdl_lang_to_gridly($source_lang), 654 'targetColumnId' => grdl_lang_to_gridly($target_lang) 593 655 )); 594 656 … … 601 663 ) 602 664 ); 665 grdl_log_api_response('POST', $url, $response, array('action' => 'create_dependency', 'source' => $source_lang, 'target' => $target_lang)); 603 666 604 667 // Check for errors and log the response … … 700 763 701 764 // Use the WordPress HTTP API to make the GET request 702 $response = wp_remote_get( 703 'https://api.gridly.com/v1/views/' . $grid_data->view_id . '/export', 765 $export_url = 'https://api.gridly.com/v1/views/' . $grid_data->view_id . '/export'; 766 $response = wp_remote_get( 767 $export_url, 704 768 array( 705 769 'headers' => array( … … 708 772 ) 709 773 ); 774 grdl_log_api_response('GET', $export_url, $response, array('job_id' => $job_id, 'action' => 'export')); 710 775 711 776 if (is_wp_error($response)) { -
gridly-integration/trunk/readme.txt
r3453819 r3463654 5 5 Requires at least: 6.2 6 6 Tested up to: 6.9 7 Stable tag: 1. 17 Stable tag: 1.2 8 8 License: GPLv2 or later 9 9 License URI: http://www.gnu.org/licenses/gpl-2.0.html … … 80 80 81 81 == Changelog == 82 = 1.2 = 83 * Added support of 2+2 character language codes 82 84 83 85 = 1.1 =
Note: See TracChangeset
for help on using the changeset viewer.