Plugin Directory

Changeset 3388470


Ignore:
Timestamp:
11/02/2025 07:24:46 PM (5 months ago)
Author:
mvpis
Message:

Updating URL and translation management.

Location:
fluentc-translation
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • fluentc-translation/tags/2.7.7/fluentc_wordpress_plugin.php

    r3388462 r3388470  
    77 * Plugin URI: https://www.fluentc.ai
    88 * Description: A plugin that enables website owners to easily install the FluentC Translation on their WordPress site.
    9  * Version: 2.7.6
     9 * Version: 2.7.7
    1010 * Author: FluentC
    1111 * Author URI: https://www.fluentc.ai
  • fluentc-translation/tags/2.7.7/src/actions/class-wordpress.php

    r3388462 r3388470  
    168168        // Add hook to refresh language code after it might have been set.
    169169        add_action('fluentc_language_set', array($this, 'refresh_language_code'));
     170
     171        // Translate WP Grid Builder REST responses when available
     172        add_filter('wp_grid_builder/rest_api/response', array($this, 'filter_wpgb_rest_response'), 10, 2);
    170173       
    171174    }
     
    267270        }
    268271       
    269           // Modified condition - handle AJAX requests differently
    270     $is_f_ajax = defined('DOING_AJAX') && DOING_AJAX;
    271    
    272     // Allow AJAX requests with language parameter to proceed
    273     if ((is_admin() && !$is_f_ajax) ||
    274         is_404() ||
    275         defined('DOING_CRON') ||
    276         wp_is_xml_request() ||
    277         ($is_f_ajax && !isset($_REQUEST['fluentc_language']))) {
    278         return;
    279     }
     272        // Modified condition - handle AJAX requests differently
     273        $is_f_ajax = defined('DOING_AJAX') && DOING_AJAX;
     274       
     275        // Allow AJAX requests with language parameter to proceed or if hitting custom handler
     276        if ((is_admin() && !$is_f_ajax) ||
     277            is_404() ||
     278            defined('DOING_CRON') ||
     279            wp_is_xml_request() ||
     280            ($is_f_ajax && !isset($_REQUEST['fluentc_language']))) {
     281            return;
     282        }
    280283        if (is_null($this->language_code) || $this->language_code === $this->site_language) {
    281284            return;
     
    482485    }
    483486
     487
     488    /**
     489     * Filter WP Grid Builder REST API responses for translation by processing the entire payload.
     490     *
     491     * @param mixed            $response The REST response (array, object, string or WP_REST_Response).
     492     * @param \WP_REST_Request $request  The current REST request instance.
     493     * @return mixed The filtered response.
     494     */
     495    public function filter_wpgb_rest_response($response, $request) {
     496        if (is_wp_error($response)) {
     497            return $response;
     498        }
     499
     500        if (is_null($this->language_code) || $this->language_code === $this->site_language) {
     501            return $response;
     502        }
     503
     504        if ($response instanceof \WP_REST_Response) {
     505            $data = $response->get_data();
     506            $translated = $this->translate_rest_payload($data);
     507            if (!is_null($translated)) {
     508                $response->set_data($translated);
     509            }
     510            return $response;
     511        }
     512
     513        if (is_array($response) || is_object($response)) {
     514            $translated = $this->translate_rest_payload($response);
     515            return is_null($translated) ? $response : $translated;
     516        }
     517
     518        if (is_string($response)) {
     519            $translated = $this->filter_content($response);
     520            return $translated ?: $response;
     521        }
     522
     523        return $response;
     524    }
     525
     526    /**
     527     * Translate an entire REST payload by sending the JSON document to the translation service.
     528     *
     529     * @param mixed $payload Original payload (array|object).
     530     * @return array|null Translated payload array or null on failure.
     531     */
     532    private function translate_rest_payload($payload) {
     533        $json = wp_json_encode($payload);
     534        if ($json === false || $json === null) {
     535            return null;
     536        }
     537
     538        $translated = $this->filter_content($json);
     539        if (empty($translated) || $translated === $json) {
     540            return null;
     541        }
     542
     543        $decoded = json_decode($translated, true);
     544        if (json_last_error() !== JSON_ERROR_NONE) {
     545            error_log('FluentC: Unable to decode translated REST payload: ' . json_last_error_msg());
     546            return null;
     547        }
     548
     549        return $decoded;
     550    }
     551
     552
    484553    /**
    485554     * Process JSON content
  • fluentc-translation/trunk/fluentc_wordpress_plugin.php

    r3388462 r3388470  
    77 * Plugin URI: https://www.fluentc.ai
    88 * Description: A plugin that enables website owners to easily install the FluentC Translation on their WordPress site.
    9  * Version: 2.7.6
     9 * Version: 2.7.7
    1010 * Author: FluentC
    1111 * Author URI: https://www.fluentc.ai
  • fluentc-translation/trunk/src/actions/class-wordpress.php

    r3388462 r3388470  
    168168        // Add hook to refresh language code after it might have been set.
    169169        add_action('fluentc_language_set', array($this, 'refresh_language_code'));
     170
     171        // Translate WP Grid Builder REST responses when available
     172        add_filter('wp_grid_builder/rest_api/response', array($this, 'filter_wpgb_rest_response'), 10, 2);
    170173       
    171174    }
     
    267270        }
    268271       
    269           // Modified condition - handle AJAX requests differently
    270     $is_f_ajax = defined('DOING_AJAX') && DOING_AJAX;
    271    
    272     // Allow AJAX requests with language parameter to proceed
    273     if ((is_admin() && !$is_f_ajax) ||
    274         is_404() ||
    275         defined('DOING_CRON') ||
    276         wp_is_xml_request() ||
    277         ($is_f_ajax && !isset($_REQUEST['fluentc_language']))) {
    278         return;
    279     }
     272        // Modified condition - handle AJAX requests differently
     273        $is_f_ajax = defined('DOING_AJAX') && DOING_AJAX;
     274       
     275        // Allow AJAX requests with language parameter to proceed or if hitting custom handler
     276        if ((is_admin() && !$is_f_ajax) ||
     277            is_404() ||
     278            defined('DOING_CRON') ||
     279            wp_is_xml_request() ||
     280            ($is_f_ajax && !isset($_REQUEST['fluentc_language']))) {
     281            return;
     282        }
    280283        if (is_null($this->language_code) || $this->language_code === $this->site_language) {
    281284            return;
     
    482485    }
    483486
     487
     488    /**
     489     * Filter WP Grid Builder REST API responses for translation by processing the entire payload.
     490     *
     491     * @param mixed            $response The REST response (array, object, string or WP_REST_Response).
     492     * @param \WP_REST_Request $request  The current REST request instance.
     493     * @return mixed The filtered response.
     494     */
     495    public function filter_wpgb_rest_response($response, $request) {
     496        if (is_wp_error($response)) {
     497            return $response;
     498        }
     499
     500        if (is_null($this->language_code) || $this->language_code === $this->site_language) {
     501            return $response;
     502        }
     503
     504        if ($response instanceof \WP_REST_Response) {
     505            $data = $response->get_data();
     506            $translated = $this->translate_rest_payload($data);
     507            if (!is_null($translated)) {
     508                $response->set_data($translated);
     509            }
     510            return $response;
     511        }
     512
     513        if (is_array($response) || is_object($response)) {
     514            $translated = $this->translate_rest_payload($response);
     515            return is_null($translated) ? $response : $translated;
     516        }
     517
     518        if (is_string($response)) {
     519            $translated = $this->filter_content($response);
     520            return $translated ?: $response;
     521        }
     522
     523        return $response;
     524    }
     525
     526    /**
     527     * Translate an entire REST payload by sending the JSON document to the translation service.
     528     *
     529     * @param mixed $payload Original payload (array|object).
     530     * @return array|null Translated payload array or null on failure.
     531     */
     532    private function translate_rest_payload($payload) {
     533        $json = wp_json_encode($payload);
     534        if ($json === false || $json === null) {
     535            return null;
     536        }
     537
     538        $translated = $this->filter_content($json);
     539        if (empty($translated) || $translated === $json) {
     540            return null;
     541        }
     542
     543        $decoded = json_decode($translated, true);
     544        if (json_last_error() !== JSON_ERROR_NONE) {
     545            error_log('FluentC: Unable to decode translated REST payload: ' . json_last_error_msg());
     546            return null;
     547        }
     548
     549        return $decoded;
     550    }
     551
     552
    484553    /**
    485554     * Process JSON content
Note: See TracChangeset for help on using the changeset viewer.