Plugin Directory

Changeset 2768836


Ignore:
Timestamp:
08/10/2022 11:06:45 AM (4 years ago)
Author:
frpet
Message:

Better woocommerce support

Location:
easytranslate/trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • easytranslate/trunk/README.txt

    r2762429 r2768836  
    55Requires at least: 4.7
    66Tested up to: 6.0
    7 Stable tag: 4.2
     7Stable tag: 4.3
    88License: GPLv2 or later
    99License URI: https://www.gnu.org/licenses/gpl-2.0.html
  • easytranslate/trunk/admin/class-easy-translate-post-translation-list.php

    r2674447 r2768836  
    338338                    $options = get_option(EASY_TRANSLATE_INTEGRATION_OPTION);
    339339                    $service = new Easy_Translate_API_Laas(Easy_Translate_API_Integration::map_fields($options));
    340                     $response = $service->translate($this->source_language, array_keys($_REQUEST['target_languages']),
    341                         $content, $workflow, $name);
     340                    $response = $service->translate(
     341                        $this->source_language,
     342                        array_keys($_REQUEST['target_languages']),
     343                        $content,
     344                        $workflow,
     345                        $name
     346                    );
    342347                    if ($response['error'] ?? false) {
    343348                        add_settings_error(
     
    357362                    );
    358363                    $this->update_string_status($a_string_id, self::STATUS_WAITING);
    359                     $this->update_duplicate_status($_REQUEST['posts'], array_keys($_REQUEST['target_languages']),
    360                         self::STATUS_WAITING);
     364                    $this->update_duplicate_status(
     365                        $_REQUEST['posts'],
     366                        array_keys($_REQUEST['target_languages']),
     367                        self::STATUS_WAITING
     368                    );
    361369                } else {
    362370                    add_settings_error(
  • easytranslate/trunk/admin/class-easy-translate-translation-list.php

    r2762424 r2768836  
    1212     * Translation statuses.
    1313     */
    14     const STATUS_NOT_TRANSLATED = 0;
    15     const STATUS_WAITING = 1;
    16     const STATUS_PARTIAL = 2;
    17     const STATUS_NEEDS_UPDATE = 3;
    18     const STATUS_DUPLICATE = 9;
    19     const STATUS_COMPLETE = 10;
     14    const STATUS_NOT_TRANSLATED = ICL_TM_NOT_TRANSLATED;
     15    const STATUS_WAITING = ICL_TM_WAITING_FOR_TRANSLATOR;
     16    const STATUS_PARTIAL = ICL_STRING_TRANSLATION_PARTIAL;
     17    const STATUS_NEEDS_UPDATE = ICL_TM_NEEDS_UPDATE;
     18    const STATUS_DUPLICATE = ICL_TM_DUPLICATE;
     19    const STATUS_COMPLETE = ICL_TM_COMPLETE;
    2020
    2121    /**
     
    379379    public function invalid_nonce_redirect()
    380380    {
    381 
    382381        wp_die(__('Invalid Nonce', EASY_TRANSLATE),
    383382            __('Error', EASY_TRANSLATE),
     
    450449     * Updates post content and title.
    451450     *
     451     * @param int    $source_post_id
    452452     * @param int    $post_id
    453453     * @param string $post_content
    454454     * @param string $post_title
    455      * @return bool|int
    456      */
    457     public static function update_post($post_id, $post_content, $post_title = '')
    458     {
    459 
    460         $set_title = '';
     455     * @param string $post_excerpt
     456     * @param array  $woocommerce
     457     */
     458    public static function update_post(
     459        int $source_post_id,
     460        int $post_id,
     461        string $post_content = '',
     462        string $post_title = '',
     463        string $post_excerpt = '',
     464        array $woocommerce = []
     465    ) {
     466        $update = '';
    461467        if ($post_title) {
    462             $set_title = ', post_title="' . $post_title . '"';
     468            $update = ', post_title="' . esc_sql($post_title) . '"';
     469        }
     470        if ($post_excerpt) {
     471            $update = ', post_excerpt="' . esc_sql($post_excerpt) . '"';
    463472        }
    464473
    465474        global $wpdb;
    466475        $post_content = esc_sql($post_content);
    467         $post_id = intval($post_id);
    468         $query = "UPDATE {$wpdb->prefix}posts SET post_content = '{$post_content}' {$set_title} WHERE id = {$post_id}";
    469 
    470         return $wpdb->query($query);
    471 
     476        $query = "UPDATE {$wpdb->prefix}posts SET post_content = '{$post_content}' {$update} WHERE id = {$post_id}";
     477        $wpdb->query($query);
     478
     479        if (empty($woocommerce)) {
     480            return;
     481        }
     482
     483        if (isset($woocommerce['_product_attributes'])) {
     484            $post_meta_fields = get_post_meta($post_id);
     485            $attributes = [];
     486            if (isset($post_meta_fields['_product_attributes']) && !empty($product_attributes = $post_meta_fields['_product_attributes'])) {
     487                foreach (unserialize($product_attributes[0]) as $field_name => $values) {
     488                    if (!isset($woocommerce['_product_attributes'][$field_name])) {
     489                        continue;
     490                    }
     491                    $attributes[$field_name] = $values;
     492                    $attributes[$field_name]['name'] = $woocommerce['_product_attributes'][$field_name]['name'];
     493                    $attributes[$field_name]['value'] = $woocommerce['_product_attributes'][$field_name]['value'];
     494                }
     495
     496                if (!empty($attributes)) {
     497                    update_post_meta($post_id, '_product_attributes', $attributes);
     498                }
     499            }
     500        }
     501        if (isset($woocommerce['_purchase_note'])) {
     502            update_post_meta($post_id, '_purchase_note', $woocommerce['_purchase_note']);
     503        }
    472504    }
    473505
     
    566598     * Return post content.
    567599     *
    568      * @param int  $post_id
    569      * @param     $a_string_id
    570      * @param bool $updated
     600     * @param int   $post_id
     601     * @param array $a_string_id
     602     * @param bool  $updated
    571603     * @return array
    572604     */
    573     public static function get_post_content($post_id, &$a_string_id, $updated = false)
    574     {
    575 
     605    public static function get_post_content(int $post_id, array &$a_string_id, $updated = false): array
     606    {
    576607        $content = [];
    577608        $package_id = self::get_post_package_id($post_id);
     
    595626                $content['post_full'][$post_id]['title'] = $post->post_title;
    596627            }
     628            if ($post->post_excerpt) {
     629                $content['post_full'][$post_id]['excerpt'] = $post->post_excerpt;
     630            }
     631
     632            $post_meta_fields = get_post_meta($post_id);
     633            if (isset($post_meta_fields['_product_attributes']) && !empty($product_attributes = $post_meta_fields['_product_attributes'])) {
     634                foreach (unserialize($product_attributes[0]) as $field_name => $values) {
     635                    $content['post_full'][$post_id]['woocommerce']['_product_attributes'][$field_name]['name'] = $values['name'];
     636                    $content['post_full'][$post_id]['woocommerce']['_product_attributes'][$field_name]['value'] = $values['value'];
     637                }
     638            }
     639            if (isset($post_meta_fields['_purchase_note']) && !empty($purchase_note = $post_meta_fields['_purchase_note'])) {
     640                $content['post_full'][$post_id]['woocommerce']['_purchase_note'] = $purchase_note;
     641            }
    597642        }
    598643
  • easytranslate/trunk/admin/class-easy-translate-translation-updater.php

    r2674447 r2768836  
    1717    public function run($post_id)
    1818    {
    19 
    20         if (get_post_meta($post_id, 'et_auto_translate', true) && Easy_Translate_Translation_List::compare_post_source_language($post_id) && Easy_Translate_Translation_List::check_dependencies()) {
    21             $a_string_id = array();
     19        if (get_post_meta($post_id, 'et_auto_translate', true)
     20            && Easy_Translate_Translation_List::compare_post_source_language($post_id)
     21            && Easy_Translate_Translation_List::check_dependencies()
     22        ) {
     23            $a_string_id = [];
    2224            $content[] = Easy_Translate_Translation_List::get_post_content($post_id, $a_string_id, false);
    2325
     
    2729
    2830                if ($this->update_strings($source, $target_languages, $content)) {
    29                     Easy_Translate_Translation_List::update_string_status($a_string_id, Easy_Translate_Translation_List::STATUS_WAITING);
    30                     Easy_Translate_Translation_List::update_duplicate_status(array($post_id), $target_languages, Easy_Translate_Translation_List::STATUS_WAITING);
     31                    Easy_Translate_Translation_List::update_string_status(
     32                        $a_string_id,
     33                        Easy_Translate_Translation_List::STATUS_WAITING
     34                    );
     35                    Easy_Translate_Translation_List::update_duplicate_status(
     36                        [$post_id],
     37                        $target_languages,
     38                        Easy_Translate_Translation_List::STATUS_WAITING
     39                    );
    3140                }
    3241            }
    3342        }
    34 
    3543    }
    3644
     
    3947     *
    4048     * @param string $source
    41      * @param array $target
    42      * @param array $content
     49     * @param array  $target
     50     * @param array  $content
    4351     * @return bool
    4452     */
    4553    protected function update_strings($source, $target, $content)
    4654    {
    47 
    4855        $options = get_option(EASY_TRANSLATE_INTEGRATION_OPTION);
    4956        $service = new Easy_Translate_API_Laas(Easy_Translate_API_Integration::map_fields($options));
     
    5158
    5259        return !isset($result['error']);
    53 
    5460    }
    55 
    5661}
  • easytranslate/trunk/admin/class-easy-translate-translation.php

    r2674447 r2768836  
    1313     * Translation status.
    1414     */
    15     const STATUS_COMPLETE = 10;
     15    const STATUS_COMPLETE = ICL_TM_COMPLETE;
    1616
    1717    /**
     
    4545    protected function post_strings_handler($content, $target_language)
    4646    {
    47 
    4847        if (isset(current($content)['title']) && current($content)['title']) {
    4948            if ($source_translations = Easy_Translate_Post_Translation_List::get_translations_by_element_id(key($content))) {
    50                 if ($translations = Easy_Translate_Post_Translation_List::get_translations_by_source($source_translations,
    51                     $target_language)) {
    52                     Easy_Translate_Post_Translation_List::update_post_title($translations['element_id'],
    53                         current($content)['title']);
     49                $translations = Easy_Translate_Post_Translation_List::get_translations_by_source(
     50                    $source_translations,
     51                    $target_language
     52                );
     53                if ($translations) {
     54                    Easy_Translate_Post_Translation_List::update_post_title(
     55                        $translations['element_id'],
     56                        current($content)['title']
     57                    );
    5458                }
    5559            }
    5660        }
     61
    5762        $this->strings_handler(current($content)['content'], $target_language);
    58 
    5963    }
    6064
     
    6771    protected function post_full_handler($content, $target_language)
    6872    {
    69 
    7073        $source_post_id = key($content);
    71         $post_title = current($content)['title'] ?? '';
    72         $post_content = current($content)['content'];
     74        $post_title = $content[$source_post_id]['title'] ?? null;
     75        $post_content = $content[$source_post_id]['content'] ?? null;
     76        $post_excerpt = $content[$source_post_id]['excerpt'] ?? null;
     77        $woocommerce = $content[$source_post_id]['woocommerce'] ?? [];
    7378
    7479        if ($source_translations = Easy_Translate_Post_Translation_List::get_translations_by_element_id($source_post_id)) {
    75 
    76             if ($translations = Easy_Translate_Post_Translation_List::get_translations_by_source($source_translations,
    77                 $target_language)) {
    78 
     80            $translations = Easy_Translate_Post_Translation_List::get_translations_by_source(
     81                $source_translations,
     82                $target_language
     83            );
     84            if ($translations) {
    7985                if (isset($translations['element_id'])) {
    80                     Easy_Translate_Post_Translation_List::update_post($translations['element_id'], $post_content,
    81                         $post_title);
    82                     Easy_Translate_Post_Translation_List::update_translations_status($translations['translation_id'],
    83                         self::STATUS_COMPLETE);
     86                    Easy_Translate_Post_Translation_List::update_post(
     87                        $source_post_id,
     88                        $translations['element_id'],
     89                        $post_content,
     90                        $post_title,
     91                        $post_excerpt,
     92                        $woocommerce
     93                    );
     94                    Easy_Translate_Post_Translation_List::update_translations_status(
     95                        $translations['translation_id'],
     96                        self::STATUS_COMPLETE
     97                    );
    8498                }
    8599            }
    86100        }
    87 
    88101    }
    89102
     
    112125    protected function fetch_content($task)
    113126    {
    114 
    115127        $options = get_option(EASY_TRANSLATE_INTEGRATION_OPTION);
    116128        $service = new Easy_Translate_API_Laas(Easy_Translate_API_Integration::map_fields($options));
  • easytranslate/trunk/easy-translate.php

    r2762424 r2768836  
    1515 * Plugin URI:        https://www.easytranslate.com/en/integrations/cms/wordpress-plugin/
    1616 * Description:       This is a short description of what the plugin does. It's displayed in the WordPress admin area.
    17  * Version:           1.6.2
     17 * Version:           1.7.0
    1818 * Author:            EasyTranslate
    1919 * Author URI:        https://easytranslate.com
     
    3434 * Rename this for your plugin and update it as you release new versions.
    3535 */
    36 define('EASY_TRANSLATE_VERSION', '1.6.2');
     36define('EASY_TRANSLATE_VERSION', '1.7.0');
    3737
    3838/**
Note: See TracChangeset for help on using the changeset viewer.