Plugin Directory

Changeset 3338467


Ignore:
Timestamp:
08/03/2025 02:23:46 PM (8 months ago)
Author:
vaksin
Message:

0.0.7

  • Change cache storage from database to files
  • Add ability to translate http header links
Location:
just-translate
Files:
298 added
7 edited

Legend:

Unmodified
Added
Removed
  • just-translate/trunk/admin/translations.php

    r3336773 r3338467  
    11<?php
    22
     3use WPJT\Modules\JT_Cache;
    34use WPJT\Modules\JT_Query;
    45
     
    2930
    3031        JT_Query::clear_html_cache();
     32        JT_Cache::clear_translated();
    3133    }
    3234   
  • just-translate/trunk/helper/jt-util.php

    r3337063 r3338467  
    244244        // phpcs:disable WordPress.PHP.DevelopmentFunctions.error_log_print_r  --  used only on development mode
    245245        if(!defined('WPJT_DEV_MODE')) define('WPJT_DEV_MODE', false);
    246         if(!WPJT_DEV_MODE) return;
    247        
     246        if(!WPJT_DEV_MODE) return;       
    248247        echo '<pre>';       
    249248        print_r($var);
    250249        echo '</pre>';
    251 
    252250        if($exit) die();
    253251        // phpcs:enable WordPress.PHP.DevelopmentFunctions.error_log_print_r
    254252    }
     253
     254    public static function get_device_type() {
     255        $ua = isset( $_SERVER['HTTP_USER_AGENT'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) : '';
     256        if (preg_match('/ipad|tablet|(android(?!.*mobile))|kindle|silk|playbook|nexus 7|nexus 10|xoom/', $ua)) {
     257            return 'tablet';
     258        }
     259        if (preg_match('/mobile|iphone|ipod|android.*mobile|blackberry|iemobile|opera mini|windows phone/', $ua)) {
     260            return 'mobile';
     261        }
     262        return 'desktop';
     263    }   
    255264}
  • just-translate/trunk/just-translate.php

    r3337063 r3338467  
    33 * Plugin Name:       Just Translate
    44 * Description:       Automatically captures and translates text strings using a custom translation panel with multi-language support.
    5  * Version:           0.0.6
     5 * Version:           0.0.7
    66 * Requires at least: 6.5
    77 * Requires PHP:      8.1
     
    1717if (!defined('ABSPATH')) exit;
    1818
    19 define( 'WPJT_VERSION', '0.0.6' );
     19define( 'WPJT_VERSION', '0.0.7' );
    2020define( 'WPJT_PLUGIN_FILE', __FILE__  );
    2121define( 'WPJT_PATH', plugin_dir_path( WPJT_PLUGIN_FILE ) );
    2222define( 'WPJT_URL', plugin_dir_url( WPJT_PLUGIN_FILE ) );
     23
     24define( 'WPJT_CACHE_DIR', WP_CONTENT_DIR . '/cache/wpjt/' );
     25
    2326
    2427// include
  • just-translate/trunk/modules/jt-query.php

    r3336773 r3338467  
    130130    }
    131131
     132    public static function get_html_cache_key(string $uri, string $source_locale_code, string $target_locale_code): ?string {       
     133        $uri_key = md5($uri);
     134
     135        $cache_key = "{$uri_key}|{$source_locale_code}|{$target_locale_code}";
     136        $html_key = wp_cache_get($cache_key, 'wpjt_html_key');       
     137        if($html_key === false) {
     138            global $wpdb;
     139            $html_key = $wpdb->get_var($wpdb->prepare(
     140                "SELECT html_key FROM %i
     141                WHERE uri_key = %s AND source_lang_code = %s AND target_lang_code = %s"
     142                , [$wpdb->prefix . JT_Table::HTML_CACHE_TABLE, $uri_key, $source_locale_code, $target_locale_code]
     143            ));     
     144           
     145            if($html_key){
     146                wp_cache_set($cache_key, $html_key, 'wpjt_html_key');
     147            }
     148        }
     149
     150        return $html_key;
     151    }
    132152
    133153    /**
     
    156176
    157177    public static function clear_html_cache(){
    158         global $wpdb;
     178        // phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching -- no caching needed       
     179        global $wpdb;
     180       
     181        // $result = $wpdb->query($wpdb->prepare(
     182        //     "TRUNCATE TABLE %i",
     183        //     [$wpdb->prefix . JT_Table::HTML_CACHE_TABLE]
     184        // ));
     185       
    159186        $result = $wpdb->query($wpdb->prepare(
    160             "TRUNCATE TABLE %i",
     187            "DELETE FROM %i WHERE source_lang_code!=target_lang_code",
    161188            [$wpdb->prefix . JT_Table::HTML_CACHE_TABLE]
    162         ));
     189        ));       
    163190        return $result;
     191        // phpcs:enable WordPress.DB.DirectDatabaseQuery.NoCaching
    164192    }
    165193
     
    472500            $slug_id = $wpdb->get_var($wpdb->prepare(
    473501                "SELECT id FROM %i
    474                 WHERE lang_code = %s AND slug = %s",
    475                 [$wpdb->prefix . JT_Table::SLUG_SOURCE_TABLE, $source_locale_code, $slug]
     502                WHERE slug = %s AND lang_code = %s",
     503                [$wpdb->prefix . JT_Table::SLUG_SOURCE_TABLE, $slug, $source_locale_code]
    476504            ));
    477505
  • just-translate/trunk/modules/jt-translator.php

    r3337063 r3338467  
    22namespace WPJT\Modules;
    33
     4use WPJT\Classes\JT_Locale;
    45use WPJT\Classes\JT_Path;
    56use WPJT\Classes\JT_String;
     
    259260
    260261        $parsed = wp_parse_url($url);
    261 
    262262        $path = isset($parsed['path']) ? $parsed['path'] : '' ;
     263
     264        if(self::should_skip_path($path)) return $url;
     265
    263266        $path = JT_Path::get_instance($path)->value($locale_code);       
    264267
    265         $query = isset($parsed['query']) ? $parsed['query'] : '';
     268        $query = isset($parsed['query']) ? '?' . $parsed['query'] : '';
    266269
    267270        return home_url($path). $query ;
     
    292295            '/wp-includes/',
    293296            '/wp-json/'
    294         ];       
     297        ];
     298
    295299        foreach($skip_prefixes as $skip_prefix) {
    296             if( substr($path, 0, strlen($skip_prefix))  === $skip_prefix ) {
     300            if( substr($path, 0, strlen($skip_prefix)) === $skip_prefix ) {
    297301                return apply_filters('wpjt_should_skip_path', true, $path);
    298302            }
     
    301305        return apply_filters('wpjt_should_skip_path', false, $path);;
    302306    }
     307
     308    public static function html_translate_http_header(){       
     309        $active_code = JT_Locale::get_active_code();
     310
     311        $headers = headers_list();
     312        $header_links = [];
     313       
     314        // Kumpulkan semua link yang ada
     315        foreach ($headers as $header) {
     316            if (stripos($header, 'Link:') === 0) {
     317                $links = explode(', ', substr($header, 6));
     318                foreach ($links as $link) {
     319                    $header_links[] = $link;
     320                }
     321            }
     322        }
     323       
     324        if (empty($header_links)) {
     325            return false;
     326        }
     327
     328        header_remove('Link');
     329        foreach ($header_links as $link) {
     330            $parts = explode('; ', $link);
     331            $uri = trim($parts[0], '<>');
     332            $translated_url = self::translate_url($uri, $active_code);
     333            header('Link: ' . '<' . $translated_url . '>; ' . implode('; ', array_slice($parts, 1)), false);
     334        }
     335               
     336        return true;       
     337    }
    303338}
  • just-translate/trunk/modules/jt.php

    r3337063 r3338467  
    1919        $parsed = wp_parse_url( WPJT_SITE_URL );
    2020        $path = isset($parsed['path']) ? $parsed['path'] : '';               
    21         $query = isset($parsed['query']) ? $parsed['query'] : '';
     21        $query = isset($parsed['query']) ? '?' . $parsed['query'] : '';
    2222
    2323        /** jika pathnya kosong, ini adalah home di source */
     
    4747        }
    4848
     49        $source_path = $o_path->value(JT_Locale::get_source_code());
     50        $source_uri = $source_path . $query;       
     51       
    4952        /** set REQUEST_URI dan PATH_INFO sesuai dengan source path agar dikenali oleh WP request */
    50         $source_path = $o_path->value(JT_Locale::get_source_code());
    51         $_SERVER['REQUEST_URI'] = $source_path . $query;
     53        $_SERVER['REQUEST_URI'] = $source_uri;
    5254        $_SERVER['PATH_INFO'] = $source_path;
    5355
     56        add_filter('redirect_canonical', function($redirect_url) use($o_path) {
     57            if ( !strpos( $redirect_url, site_url() ) === 0 ) return $redirect_url;   
     58
     59            if($o_path->is_source) return $redirect_url;
     60
     61            $parsed = wp_parse_url( $redirect_url );
     62            $path = isset($parsed['path']) ? $parsed['path'] : '';               
     63            $query = isset($parsed['query']) ? '?' . $parsed['query'] : '';
     64
     65            $rpath = JT_Path::get_instance($path);
     66            if($rpath->is_translated($o_path->locale_code)){
     67                return home_url($rpath->value($o_path->locale_code) . $query);
     68            }
     69
     70            return $redirect_url;
     71        });             
     72       
    5473        self::$loaded=true;
    5574
     
    7089     */
    7190    public static function cb_init_action(): void{
     91        $uri = isset($_SERVER['REQUEST_URI']) ? sanitize_url( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';   
     92
    7293        wp_register_style('wpjt_translations', WPJT_URL . '/admin/css/translations.css', [], '0.0.1');
    7394        wp_register_style('wpjt_language_switcher', WPJT_URL . '/frontend/css/language-switcher.css', [], '0.0.1');
     
    86107        // nanti mungkin perlu kondisi tambahan
    87108        ob_start(function($output){
    88             $active_code = JT_Locale::get_active_code();
    89             header('Content-Language: ' . $active_code);
    90 
    91             // return JT_Translator::translate_html($output, $active_code);
    92 
    93             if(JT_Locale::get_source_code() != $active_code){
    94                 $uri = isset($_SERVER['REQUEST_URI']) ? sanitize_url( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';
    95                 $html_key = sha1($output);
    96                 $translated_html = JT_Query::get_html_cache($uri, JT_Locale::get_source_code(), $active_code, $html_key);
    97 
    98                 if($translated_html){
    99                     return $translated_html ;
    100                 } else {
    101                     $translated_html = JT_Translator::translate_html($output, $active_code);
    102                     JT_Query::set_html_cache($uri, JT_Locale::get_source_code(), $active_code, $translated_html, $html_key);
    103                 }
    104             } else {
     109            $active_code = JT_Locale::get_active_code();                       
     110            $uri = isset($_SERVER['REQUEST_URI']) ? sanitize_url( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';           
     111            $html_key = sha1($output);           
     112            $translated_html = JT_Cache::get_html_cache($uri, $active_code, $html_key);
     113           
     114            /** translate header, saat ini baru translate Link */           
     115            if(!$translated_html){               
     116                header('Content-Language: ' . $active_code);
    105117                $translated_html = JT_Translator::translate_html($output, $active_code);
     118                JT_Cache::set_html_cache($uri, $active_code, $translated_html, $html_key);
    106119            }
    107 
     120           
     121            JT_Translator::html_translate_http_header();           
    108122            return $translated_html;
    109         });   
     123        });
    110124    }   
    111125
  • just-translate/trunk/readme.txt

    r3337063 r3338467  
    66Tested up to: 6.8
    77Requires PHP: 8.1
    8 Stable tag: 0.0.6
     8Stable tag: 0.0.7
    99License: GPLv2 or later 
    1010License URI: https://www.gnu.org/licenses/gpl-2.0.html 
     
    111111
    112112== Changelog ==
     113= 0.0.7 =
     114* Change cache storage from database to files
     115* Add ability to translate http header links
     116
    113117= 0.0.6 =
    114118* Fix internal link recognition when no scheme or host is specified
     119* Code refactoring
     120
     121= 0.0.5 =
     122* improve perfomance (cache the translated html)
    115123* Code refactoring
    116124
     
    132140== Upgrade Notice ==
    133141
    134 = 0.0.6 =
    135 * Fix internal link recognition when no scheme or host is specified
    136 * Code refactoring
     142= 0.0.7 =
     143* Change cache storage from database to files
     144* Add ability to translate http header links
Note: See TracChangeset for help on using the changeset viewer.