Plugin Directory

Changeset 3463654


Ignore:
Timestamp:
02/17/2026 03:57:25 PM (6 weeks ago)
Author:
nszolnoki
Message:

feat: added support of 2+2 character language codes (v1.2)

Location:
gridly-integration
Files:
13 added
2 edited

Legend:

Unmodified
Added
Removed
  • gridly-integration/trunk/gridly-integration.php

    r3454373 r3463654  
    55 * Plugin URI:        https://www.gridly.com/integrations/
    66 * Description:       Description: An integration that you can use to send and receive your WordPress pages with Gridly.
    7  * Version:           1.1
     7 * Version:           1.2
    88 * Requires at least: 6.2
    99 * Requires PHP:      7.2
     
    2020// 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.
    2121define('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/
     23if (!defined('GRDL_LOG_API')) {
     24    define('GRDL_LOG_API', false);
     25}
    2226
    2327function grdl_invalidate_cache(string $key): void {
    2428    wp_cache_delete($key, GRDL_CACHE_GROUP);
     29}
     30
     31function 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
     39function 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
     55function 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);
    2579}
    2680$GRDL_WP_PLUGIN_isWpmlIsntalled = false;
     
    138192    $csv_content = '';
    139193
    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']);
    141197    $csv_content .= implode(',', array_map(function($header) {
    142198        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";
    144200
    145201    // Loop through each trans-unit in the XLIFF file and append to CSV content with quotation marks around each item
     
    175231    $csv   = array_map('str_getcsv', explode("\n", $csv_data));
    176232
    177     // Get the header row of the CSV
     233    // Get the header row of the CSV (headers from Gridly are in ptBR format)
    178234    $header = array_shift($csv);
    179235
    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);
    182238
    183239    // Loop through the CSV rows and update the XLIFF as needed
     
    258314        )
    259315    );
     316    grdl_log_api_response('POST', $url, $response, array('job_id' => $job_id));
    260317
    261318    // Check for errors
     
    508565{
    509566    // Use the WordPress HTTP API to make the GET request
     567    $url      = 'https://api.gridly.com/v1/views/' . $profile->view_id;
    510568    $response = wp_remote_get(
    511         esc_url('https://api.gridly.com/v1/views/' . $profile->view_id),
     569        esc_url($url),
    512570        array(
    513571            'headers' => array(
     
    517575        )
    518576    );
     577    grdl_log_api_response('GET', $url, $response, array('action' => 'get_view', 'lang_col' => $lang_col));
    519578
    520579    // Check if the request was successful
     
    525584    }
    526585
    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)) {
    530590
    531591        try {
    532592            $request_body = '{
    533                 "id": "' . $lang_col . '",
    534                 "name": "' . $lang_col . '",
     593                "id": "' . $lang_col_g . '",
     594                "name": "' . $lang_col_g . '",
    535595                "type": "language",
    536                 "languageCode": "' . $lang_col . '"
     596                "languageCode": "' . $lang_col_g . '"
    537597            }';
    538598
    539599            if ($target) {
    540600                $request_data = '{
    541                     "id": "' . $lang_col . '",
     601                    "id": "' . $lang_col_g . '",
    542602                    "localizationType": "targetLanguage",
    543                     "name": "' . $lang_col . '",
     603                    "name": "' . $lang_col_g . '",
    544604                    "type": "language",
    545                     "languageCode": "' . $lang_col . '"
     605                    "languageCode": "' . $lang_col_g . '"
    546606                }';
    547607            }
    548608
    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,
    551612                array(
    552613                    'headers' => array(
     
    557618                )
    558619            );
     620            grdl_log_api_response('POST', $col_url, $response, array('action' => 'create_column', 'lang_col' => $lang_col));
    559621            $setting_data = GRDL_WP_PLUGIN_GET_GRIDLY_GENERAL_SETTINGS();
    560622            $set_dependencies = $setting_data->set_dependencies;
     
    587649    );
    588650
    589     // Define the request data
     651    // Define the request data (source_lang and target_lang must be in Gridly format, e.g. ptBR)
    590652    $request_body = wp_json_encode(array(
    591         'sourceColumnId' => $source_lang,
    592         'targetColumnId' => $target_lang
     653        'sourceColumnId' => grdl_lang_to_gridly($source_lang),
     654        'targetColumnId' => grdl_lang_to_gridly($target_lang)
    593655    ));
    594656
     
    601663        )
    602664    );
     665    grdl_log_api_response('POST', $url, $response, array('action' => 'create_dependency', 'source' => $source_lang, 'target' => $target_lang));
    603666
    604667    // Check for errors and log the response
     
    700763
    701764        // 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,
    704768            array(
    705769                'headers' => array(
     
    708772            )
    709773        );
     774        grdl_log_api_response('GET', $export_url, $response, array('job_id' => $job_id, 'action' => 'export'));
    710775
    711776        if (is_wp_error($response)) {
  • gridly-integration/trunk/readme.txt

    r3453819 r3463654  
    55Requires at least: 6.2
    66Tested up to: 6.9
    7 Stable tag: 1.1
     7Stable tag: 1.2
    88License: GPLv2 or later
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    8080
    8181== Changelog ==
     82= 1.2 =
     83* Added support of 2+2 character language codes
    8284
    8385= 1.1 =
Note: See TracChangeset for help on using the changeset viewer.