Plugin Directory

Changeset 3267615


Ignore:
Timestamp:
04/07/2025 03:42:55 AM (12 months ago)
Author:
mvpis
Message:

Updates and Improvements

Location:
fluentc-translation
Files:
83 added
6 edited

Legend:

Unmodified
Added
Removed
  • fluentc-translation/trunk/bootstrap.php

    r3267055 r3267615  
    8383            'FluentC\Actions\Translationstatus',
    8484            'FluentC\Actions\Heartbeat',
     85            'FluentC\Actions\Permalinks',
    8586        );
    8687
  • fluentc-translation/trunk/fluentc_wordpress_plugin.php

    r3267055 r3267615  
    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.5.2
     9 * Version: 2.5.3
    1010 * Author: FluentC
    1111 * Author URI: https://www.fluentc.ai
     
    1717define( 'FLUENTC_DIR', __DIR__ );
    1818define( 'FLUENTC_SLUG', 'fluentc_translation' );
    19 define( 'FLUENTC_TRANSLATION_VERSION', "2.5.2" );
     19define( 'FLUENTC_TRANSLATION_VERSION', "2.5.3" );
    2020define( 'POLYLANG_VERSION', "2.5.2" );
    2121define( 'FLUENTC_TRANSLATION_PLUGIN_DIR', plugin_dir_path(__FILE__) );
  • fluentc-translation/trunk/readme.txt

    r3267055 r3267615  
    55Requires at least: 4.6
    66Tested up to: 6.6.2
    7 Stable tag: 2.5.2
     7Stable tag: 2.5.3
    88Requires PHP: 7.3
    99License: GPLv2 or later
  • fluentc-translation/trunk/src/class-fluentc-manager.php

    r3267055 r3267615  
    99namespace FluentC;
    1010
     11use FluentC\Utils\Language;
    1112use FluentC\Models\FluentC_Links_Model;
    1213
     
    1718    public $links_model;
    1819
     20    protected $fluentc_languages;
    1921    public function __construct() {
    2022        $this->links_model = new FluentC_Links_Model();
     23        $this->fluentc_languages = new Language();
    2124        add_action( 'init', array( $this, 'init_hooks' ), 99 );
    2225    }
     
    4346     */
    4447    public function is_fluentc_active() {
    45         return true; // Replace with your logic to check if FluentC is active.
     48        $has_api_key = !empty(get_option('fluentc_api_key'));
     49        if ($has_api_key) {
     50            return true;
     51        }
     52        else {
     53            return false;
     54        }
     55       
    4656    }
    4757
     
    5060     */
    5161    public function get_fluentc_locale() {
    52         return 'en'; // Replace with your logic to get the current locale.
     62       
     63        return $this->fluentc_languages->fluentc_site_language();
    5364    }
    5465}
  • fluentc-translation/trunk/src/class-polylang.php

    r3267055 r3267615  
    1212use FluentC\Services\PLL_Language;
    1313use FluentC\Services\Connect;
     14use FluentC\Utils\Language;
    1415
    1516if ( ! defined( 'ABSPATH' ) ) {
     
    2122    public $model;
    2223
     24    protected $fluentc_language;
    2325    protected $fluentc_connenct;
    2426
     
    2830        $this->model = new PLL_Language();
    2931        $this->fluentc_connenct = new Connect();
     32        $this->fluentc_language = new Language();
    3033        $this->options = $this->links_model->options;
    3134        add_action( 'init', array( $this, 'init_hooks' ), 1 );
     
    6265
    6366    /**
     67 * Allow the canonical redirect for default language if "Hide URL language information for default language"
     68 * is turned on in FluentC/Polylang settings
     69 *
     70 * @param array $query The WordPress query variables
     71 * @param array $old_query Original query variables
     72 * @param array $uri_parts Parts of the URI including language information
     73 * @param array $pm_query Permalink Manager query info
     74 * @param string $content_type Type of content being accessed
     75 *
     76 * @return array Modified query
     77 */
     78public function pl_allow_canonical_redirect($query, $old_query, $uri_parts, $pm_query, $content_type) {
     79    // First check if we have polylang global set up
     80    global $polylang;
     81
     82    if (empty($polylang) || empty($polylang->links_model) || empty($polylang->links_model->options)) {
     83        return $query;
     84    }
     85
     86    // Run only if we have ID and language data
     87    if (!empty($pm_query['id']) && !empty($pm_query['lang'])) {
     88        // Get language from URL using our implementation
     89        $url_lang = $polylang->links_model->get_language_from_url();
     90       
     91        // Get default language using existing FluentC function
     92        $fluentc_language = new \FluentC\Utils\Language();
     93        $def_lang = $fluentc_language->fluentc_site_language();
     94       
     95        // Get hide_default option from our FluentC links model
     96        $hide_default = !empty($polylang->links_model->options['hide_default']);
     97        $force_lang = isset($polylang->options['force_lang']) ? $polylang->options['force_lang'] : 1;
     98       
     99        // A. Check if URL contains default language code but "hide default" is enabled
     100        //    OR using different domains for languages (force_lang == 3)
     101        if (($url_lang == $def_lang && $hide_default) || $force_lang == 3) {
     102            unset($query['do_not_redirect']);
     103        }
     104        // B. Check if URL does NOT contain default language code but "hide default" is disabled
     105        else if (empty($url_lang) && !$hide_default) {
     106            unset($query['do_not_redirect']);
     107        }
     108    }
     109
     110    return $query;
     111}
     112
     113
     114/**
     115 * Detect the language of requested content and add it to $uri_parts array
     116 *
     117 * @param array $uri_parts Current URI parts
     118 * @param string $request_url The requested URL
     119 * @param string $endpoints URL endpoints string
     120 * @return array Modified URI parts with language information
     121 */
     122public function detect_uri_language($uri_parts, $request_url, $endpoints) {
     123    // Get available languages
     124    $fluentc_connect = new \FluentC\Services\Connect();
     125    $widgetapikey = get_option('fluentc_api_key');
     126    $languages_list = $fluentc_connect->get_language_list($widgetapikey);
     127   
     128    if (empty($languages_list) || !is_array($languages_list)) {
     129        return $uri_parts;
     130    }
     131   
     132    $languages_pattern = implode('|', $languages_list);
     133   
     134    // Check URL configuration (domain per language)
     135    if (!empty($this->options['force_lang']) && $this->options['force_lang'] == 3) {
     136        if (!empty($this->options['domains'])) {
     137            $domains = (array)$this->options['domains'];
     138           
     139            // Clean domains for comparison
     140            foreach ($domains as &$domain) {
     141                $domain = preg_replace('/((http(s)?:\/\/(www\.)?)|(www\.))?(.+?)\/?$/', 'http://$6', $domain);
     142            }
     143           
     144            // Remove domain from request URL
     145            $request_url = trim(str_replace($domains, "", $request_url), "/");
     146        }
     147       
     148        $default_language = "";
     149    } else {
     150        $default_language = $this->fluentc_language->fluentc_site_language();
     151    }
     152   
     153    // Parse the URL to extract language and other parts
     154    if (!empty($languages_pattern)) {
     155        preg_match("/^(?:({$languages_pattern})\/)?(.+?)(?|\/({$endpoints})(?|\/(.*)|$)|\/()([\d]+)\/?)?$/i", $request_url, $regex_parts);
     156       
     157        $uri_parts['lang'] = (!empty($regex_parts[1])) ? $regex_parts[1] : $default_language;
     158        $uri_parts['uri'] = (!empty($regex_parts[2])) ? $regex_parts[2] : "";
     159        $uri_parts['endpoint'] = (!empty($regex_parts[3])) ? $regex_parts[3] : "";
     160        $uri_parts['endpoint_value'] = (!empty($regex_parts[4])) ? $regex_parts[4] : "";
     161    }
     162   
     163    return $uri_parts;
     164}
     165
     166/**
     167 * Append language code as URL prefix
     168 *
     169 * @param string $base URL base
     170 * @param mixed $element Post or term object
     171 * @param string $language_code Optional language code override
     172 * @return string Modified URL base with language prefix
     173 */
     174public function prepend_lang_prefix($base, $element, $language_code = '') {
     175    if (!empty($element) && empty($language_code)) {
     176        $language_code = $this->fluentc_language->get_fluentc_language($element);
     177       
     178        // Use language parameter from GET if in admin and no language detected
     179        $language_code = (is_admin() && empty($language_code) && !empty($_GET['lang']))
     180            ? sanitize_key($_GET['lang'])
     181            : $language_code;
     182    }
     183   
     184    // Adjust URL base
     185    if (!empty($language_code)) {
     186        $default_language_code = $this->fluentc_language->fluentc_site_language();
     187        $home_url = get_home_url();
     188       
     189        // Check if default language code should be hidden
     190        $hide_prefix_for_default_lang = !empty($this->links_model->options['hide_default']);
     191       
     192        // Check URL configuration
     193        $force_lang = isset($this->options['force_lang']) ? $this->options['force_lang'] : 1;
     194       
     195        // A. Different domain per language
     196        if ($force_lang == 3) {
     197            if (!empty($this->options['domains']) && !empty($this->options['domains'][$language_code])) {
     198                $base = trim($this->options['domains'][$language_code], "/");
     199               
     200                // Append URL scheme
     201                if (!preg_match("~^(?:f|ht)tps?://~i", $base)) {
     202                    $scheme = parse_url($home_url, PHP_URL_SCHEME);
     203                    $base = "{$scheme}://{$base}";
     204                }
     205            }
     206        }
     207        // B. Language as subdomain
     208        else if ($force_lang == 2) {
     209            if ($hide_prefix_for_default_lang && ($default_language_code == $language_code)) {
     210                return $base;
     211            } else {
     212                $base = preg_replace('/(https?:\/\/)/', "$1{$language_code}.", $home_url);
     213            }
     214        }
     215        // C. Language in URL path (default)
     216        else {
     217            if ($hide_prefix_for_default_lang && ($default_language_code == $language_code)) {
     218                return $base;
     219            } else {
     220                $base .= "/{$language_code}";
     221            }
     222        }
     223    }
     224   
     225    return $base;
     226}
     227
     228/**
     229 * Support for pagination endpoints translated by Polylang
     230 *
     231 * @param string $endpoints Current endpoints string
     232 * @return string Modified endpoints including translated pagination endpoint
     233 */
     234public function pl_translate_pagination_endpoint($endpoints) {
     235    $pagination_endpoint = $this->pl_get_translated_slugs('paged');
     236   
     237    if (!empty($pagination_endpoint) && !empty($pagination_endpoint['translations'])) {
     238        $current_language = $this->fluentc_language->get_fluentc_language();
     239       
     240        if (!empty($current_language) && !empty($pagination_endpoint['translations'][$current_language])) {
     241            $endpoints .= "|" . $pagination_endpoint['translations'][$current_language];
     242        }
     243    }
     244   
     245    return $endpoints;
     246}
     247
     248/**
     249 * Get back the original name of the translated endpoint
     250 *
     251 * @param array $uri_parts Current URI parts
     252 * @param string $request_url The requested URL
     253 * @param string $endpoints URL endpoints string
     254 * @return array Modified URI parts
     255 */
     256public function pl_detect_pagination_endpoint($uri_parts, $request_url, $endpoints) {
     257    if (!empty($uri_parts['endpoint'])) {
     258        $pagination_endpoint = $this->pl_get_translated_slugs('paged');
     259       
     260        if (!empty($pagination_endpoint['translations']) &&
     261            in_array($uri_parts['endpoint'], $pagination_endpoint['translations'])) {
     262            $uri_parts['endpoint'] = $pagination_endpoint['slug'];
     263        }
     264    }
     265   
     266    return $uri_parts;
     267}
     268
     269/**
     270 * Display the language code in URI editor table
     271 *
     272 * @param string $output Current output HTML
     273 * @param string $column Column name
     274 * @param object $element Post or term object
     275 * @return string Modified output with language information
     276 */
     277public function uri_editor_get_lang_col($output, $column, $element) {
     278    $language_code = $this->fluentc_language->get_fluentc_language($element);
     279   
     280    if (!empty($language_code)) {
     281        $output .= sprintf(" | <span><strong>%s:</strong> %s</span>",
     282                          __('Language', 'fluentc-translation'),
     283                          $language_code);
     284    }
     285   
     286    return $output;
     287}
     288
     289/**
     290 * Add language filter to URI editor
     291 *
     292 * @param string $html Current filter HTML
     293 * @param string $content_type Content type being filtered
     294 * @return string Modified HTML with language filter
     295 */
     296public function uri_editor_filter_lang($html, $content_type) {
     297    $choices = [];
     298   
     299    // Get languages from FluentC
     300    $fluentc_connect = new \FluentC\Services\Connect();
     301    $widgetapikey = get_option('fluentc_api_key');
     302    $languages = $fluentc_connect->get_display_language_list($widgetapikey);
     303   
     304    if (!empty($languages)) {
     305        foreach ($languages as $language) {
     306            $choices[$language[1]] = $language[0];
     307        }
     308       
     309        // Add "All languages" option
     310        $choices = array_merge(['all' => __('All languages', 'fluentc-translation')], $choices);
     311       
     312        // Create a select field
     313        // Note: This depends on how your UI elements are generated
     314        $select_field = '<select name="langcode" id="langcode">';
     315        $current_lang = isset($_REQUEST['langcode']) ? esc_attr($_REQUEST['langcode']) : '';
     316       
     317        foreach ($choices as $value => $label) {
     318            $selected = ($current_lang === $value) ? ' selected="selected"' : '';
     319            $select_field .= '<option value="' . esc_attr($value) . '"' . $selected . '>' . esc_html($label) . '</option>';
     320        }
     321       
     322        $select_field .= '</select>';
     323       
     324        $html = '<div class="alignleft actions">' . $select_field . '</div>';
     325    }
     326   
     327    return $html;
     328}
     329
     330/**
     331 * Get original slugs without translation
     332 * Since FluentC doesn't translate slugs, this returns the original slug structure
     333 *
     334 * @param string $slug Optional slug to retrieve
     335 * @return array Array with slug structure but no translations
     336 */
     337public function pl_get_translated_slugs($slug = '') {
     338    // For Permalink Manager compatibility, return a structure with the original slug
     339    // but no actual translations since FluentC doesn't translate slugs
     340   
     341    if (!empty($slug)) {
     342        // Return a basic structure with just the original slug
     343        // For pagination, 'page' is the standard
     344        $default_slug = ($slug === 'paged') ? 'page' : $slug;
     345       
     346        // Get current language
     347        $current_language = $this->fluentc_language->get_fluentc_language();
     348       
     349        // Get all available languages
     350        $fluentc_connect = new \FluentC\Services\Connect();
     351        $widgetapikey = get_option('fluentc_api_key');
     352        $languages = $fluentc_connect->get_language_list($widgetapikey);
     353       
     354        // Initialize translations array
     355        $translations = [];
     356       
     357        // For each language, use the same original slug
     358        // This emulates Polylang's structure without actual translations
     359        if (!empty($languages) && is_array($languages)) {
     360            foreach ($languages as $lang_code) {
     361                $translations[$lang_code] = $default_slug;
     362            }
     363        } elseif (!empty($current_language)) {
     364            // Fallback if languages list is unavailable
     365            $translations[$current_language] = $default_slug;
     366        }
     367       
     368        return [
     369            'slug' => $default_slug,
     370            'translations' => $translations
     371        ];
     372    }
     373   
     374    // Return empty array if no slug specified
     375    return [];
     376}
     377
     378/**
     379 * Filter SQL query in URI editor by language
     380 *
     381 * @param string $sql_query Current SQL query
     382 * @param array $sql_parts SQL query parts
     383 * @param bool $is_taxonomy Whether query is for taxonomies
     384 * @return string Modified SQL query filtered by language
     385 */
     386public function uri_editor_filter_sql_query($sql_query, $sql_parts, $is_taxonomy) {
     387    global $wpdb;
     388   
     389    if (isset($_GET['langcode']) && $_GET['langcode'] !== 'all') {
     390        $lang = esc_sql($_GET['langcode']);
     391       
     392        // We need different approaches for taxonomies vs posts
     393        if ($is_taxonomy) {
     394            // Use term relationships table to filter terms by language
     395            $join = "{$sql_parts['start']} INNER JOIN {$wpdb->term_relationships} AS tr ON tt.term_id = tr.object_id ";
     396           
     397            // This is a simplified approach - proper implementation would need actual language term taxonomy ID
     398            $lang_term_ttid = 0; // This should be dynamically determined
     399            if ($lang_term_ttid) {
     400                $join .= "AND tr.term_taxonomy_id = {$lang_term_ttid} ";
     401            }
     402        } else {
     403            // For posts, create a join to filter by language meta
     404            $join = "{$sql_parts['start']} INNER JOIN {$wpdb->postmeta} AS pm ON p.ID = pm.post_id AND pm.meta_key = 'fluentc_language' AND pm.meta_value = '{$lang}' ";
     405        }
     406       
     407        // Replace the original SQL start with our join
     408        if (!empty($join)) {
     409            $sql_query = str_replace($sql_parts['start'], $join, $sql_query);
     410        }
     411    }
     412   
     413    return $sql_query;
     414}
     415    /**
    64416     * Set locale function to replace Polylang functionality
    65417     */
  • fluentc-translation/trunk/src/models/class-fluentc-links-model.php

    r3267055 r3267615  
    1616        $this->options = $this->initialize_options();
    1717    }
    18 
     18    /**
     19 * Extract language code from the current URL
     20 * Mimics Polylang's PLL_Links_Model::get_language_from_url() method
     21 *
     22 * @return string|false Language slug if found, false otherwise
     23 */
     24public function get_language_from_url() {
     25    // Use existing FluentC Language utility class
     26    $fluentc_language = new \FluentC\Utils\Language();
     27   
     28    // Use your existing get_fluentc_language_url_from_url method if present
     29    $request_uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
     30   
     31    // Check if we already have a method to extract language from URL
     32    if (method_exists($fluentc_language, 'get_fluentc_language_url_from_url')) {
     33        $lang_prefix = $fluentc_language->get_fluentc_language_url_from_url($request_uri);
     34       
     35        // Extract language code from the prefix (removing slashes)
     36        if (!empty($lang_prefix)) {
     37            return trim($lang_prefix, '/');
     38        }
     39    }
     40   
     41    // Fallback to standard language detection
     42    $current_lang = $fluentc_language->get_fluentc_language();
     43   
     44    // If no language detected in URL but we have a current language set
     45    // (from other sources like cookie or session), return that
     46    return $current_lang ?: false;
     47}
    1948    /**
    2049     * Initialize the options property.
Note: See TracChangeset for help on using the changeset viewer.