Plugin Directory

Changeset 2795813


Ignore:
Timestamp:
10/07/2022 10:11:19 PM (3 years ago)
Author:
frpet
Message:

Yoast SEO support + Better WooCommerce handling

Location:
easytranslate/trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • easytranslate/trunk/README.txt

    r2780745 r2795813  
    55Requires at least: 4.7
    66Tested up to: 6.0
    7 Stable tag: 4.7
     7Stable tag: 4.8
    88License: GPLv2 or later
    99License URI: https://www.gnu.org/licenses/gpl-2.0.html
     
    4040* Save time! Build your custom workflows (when this happens > do this)
    4141* String Library > enabling you to localise your strings faster. This is especially useful within Apps
     42* Support for WooCommerce + WPML
     43* Support for Yoast SEO + WPML
    4244
    4345== Installation ==
  • easytranslate/trunk/admin/class-easy-translate-post-translation-list.php

    r2772023 r2795813  
    2626        'oembed_cache',
    2727        'user_request',
     28        'product_variation',
    2829    ];
    2930
     
    417418              <?php
    418419              foreach ($post_types as $post_type) {
     420                  if (in_array($post_type, self::EXCLUDED_POST_TYPES)) {
     421                      continue;
     422                  }
    419423                  ?>
    420424                <option <?php selected($current_post_type, $post_type); ?> value="<?= $post_type ?>">
  • easytranslate/trunk/admin/class-easy-translate-translation-list.php

    r2780745 r2795813  
    99abstract class Easy_Translate_Translation_List extends WP_List_Table
    1010{
     11    const YOAST_SEO_FIELDS = [
     12        '_yoast_wpseo_opengraph-description',
     13        '_yoast_wpseo_opengraph-title',
     14        '_yoast_wpseo_twitter-description',
     15        '_yoast_wpseo_twitter-title',
     16        '_yoast_wpseo_focuskw',
     17        '_yoast_wpseo_title',
     18        '_yoast_wpseo_metadesc',
     19        '_yoast_wpseo_bctitle',
     20    ];
    1121    /**
    1222     * Translation statuses.
     
    434444     * @param int    $source_post_id
    435445     * @param int    $post_id
     446     * @param string $target_language
    436447     * @param string $post_content
    437448     * @param string $post_title
    438449     * @param string $post_excerpt
    439450     * @param array  $woocommerce
     451     * @param array  $yoast_seo
    440452     */
    441453    public static function update_post(
    442454        int $source_post_id,
    443455        int $post_id,
     456        string $target_language,
    444457        string $post_content = '',
    445458        string $post_title = '',
    446459        string $post_excerpt = '',
    447         array $woocommerce = []
     460        array $woocommerce = [],
     461        array $yoast_seo = []
    448462    ) {
    449463        $update = '';
     
    458472        $post_content = esc_sql($post_content);
    459473        $query = "UPDATE {$wpdb->prefix}posts SET post_content = '{$post_content}' {$update} WHERE id = {$post_id}";
    460         $wpdb->query($query);
    461 
    462         if (empty($woocommerce)) {
     474
     475        self::update_woocommerce_translations($source_post_id, $post_id, $target_language, $woocommerce);
     476        self::update_yoast_translations($post_id, $yoast_seo);
     477
     478        delete_post_meta($post_id, '_icl_lang_duplicate_of', $source_post_id);
     479    }
     480
     481    public static function update_yoast_translations(string $post_id, array $yoast_ceo)
     482    {
     483        if (empty($yoast_ceo)) {
    463484            return;
    464485        }
    465486
    466         if (isset($woocommerce['_product_attributes'])) {
    467             $post_meta_fields = get_post_meta($post_id);
    468             $attributes = [];
    469             if (isset($post_meta_fields['_product_attributes']) && !empty($product_attributes = $post_meta_fields['_product_attributes'])) {
    470                 foreach (unserialize($product_attributes[0]) as $field_name => $values) {
    471                     if (!isset($woocommerce['_product_attributes'][$field_name])) {
    472                         continue;
    473                     }
    474                     $attributes[$field_name] = $values;
    475                     $attributes[$field_name]['name'] = $woocommerce['_product_attributes'][$field_name]['name'];
    476                     $attributes[$field_name]['value'] = $woocommerce['_product_attributes'][$field_name]['value'];
    477                 }
    478 
    479                 if (!empty($attributes)) {
    480                     update_post_meta($post_id, '_product_attributes', $attributes);
    481                 }
    482             }
     487        foreach ($yoast_ceo as $key => $translation) {
     488            update_post_meta($post_id, $key, $translation);
     489        }
     490    }
     491
     492    public static function update_woocommerce_translations(
     493        string $source_post_id,
     494        string $post_id,
     495        string $target_language,
     496        array $woocommerce
     497    ) {
     498        // WooCommerce WPML bridge is not installed
     499        if (empty($woocommerce) || !class_exists('woocommerce_wpml')) {
     500            return;
     501        }
     502
     503        $translations = [];
     504        foreach (($woocommerce['_product_attributes'] ?? []) as $key => $value) {
     505            $translations[md5($key)] = $value['value'];
     506            $translations[md5("{$key}_name")] = $value['name'];
    483507        }
    484508        if (isset($woocommerce['_purchase_note'])) {
    485             update_post_meta($post_id, '_purchase_note', $woocommerce['_purchase_note']);
    486         }
     509            $translations[md5('_purchase_note')] = $woocommerce['_purchase_note'];
     510        }
     511        foreach (($woocommerce['_variations'] ?? []) as $variation_id => $value) {
     512            $translations[md5("_variation_description{$variation_id}")] = $value['description'];
     513        }
     514
     515        $woo_wpml = new woocommerce_wpml();
     516        $woo_wpml->init();
     517        do_action('wcml_before_sync_product_data', $source_post_id, $post_id, $target_language);
     518
     519        $woo_wpml->sync_product_data->duplicate_product_post_meta($source_post_id, $post_id, $translations);
     520        $woo_wpml->sync_product_data->sync_product_taxonomies($source_post_id, $post_id, $target_language);
     521        $woo_wpml->attributes->sync_product_attr($source_post_id, $post_id, $target_language, $translations);
     522        $woo_wpml->attributes->sync_default_product_attr($source_post_id, $post_id, $target_language);
     523        $woo_wpml->sync_variations_data->sync_product_variations(
     524            $source_post_id,
     525            $post_id,
     526            $target_language,
     527            [
     528                'editor_translations' => $translations,
     529                'is_troubleshooting'  => true,
     530            ]
     531        );
     532        $woo_wpml->sync_product_data->sync_linked_products($source_post_id, $post_id, $target_language);
     533        $woo_wpml->sync_product_data->sync_product_stock(wc_get_product($source_post_id), wc_get_product($post_id));
     534        $woo_wpml->media->sync_thumbnail_id($source_post_id, $post_id, $target_language);
    487535    }
    488536
     
    614662                $content['post_full'][$post_id]['excerpt'] = $post->post_excerpt;
    615663            }
    616 
    617664            $post_meta_fields = get_post_meta($post_id);
     665
     666            foreach (self::YOAST_SEO_FIELDS as $yoast_field_name) {
     667                if (isset($post_meta_fields[$yoast_field_name][0]) && !empty($post_meta_fields[$yoast_field_name][0])) {
     668                    $content['post_full'][$post_id]['yoast_seo'][$yoast_field_name] = $post_meta_fields[$yoast_field_name][0];
     669                }
     670            }
     671
    618672            if (isset($post_meta_fields['_product_attributes']) && !empty($product_attributes = $post_meta_fields['_product_attributes'])) {
    619673                foreach (unserialize($product_attributes[0]) as $field_name => $values) {
     
    622676                }
    623677            }
    624             if (isset($post_meta_fields['_purchase_note']) && !empty($purchase_note = $post_meta_fields['_purchase_note'])) {
    625                 $content['post_full'][$post_id]['woocommerce']['_purchase_note'] = $purchase_note;
     678
     679            if (isset($post_meta_fields['_purchase_note'][0]) && !empty($purchase_note = $post_meta_fields['_purchase_note'][0])) {
     680                $content['post_full'][$post_id]['woocommerce']['_purchase_note'] = $purchase_note[0];
     681            }
     682
     683            // WooCommerce WPML bridge is not installed
     684            if (class_exists('woocommerce_wpml')) {
     685                $woo_wpml = new woocommerce_wpml();
     686                $woo_wpml->init();
     687
     688                foreach ($woo_wpml->sync_variations_data->get_product_variations($post_id) as $variation) {
     689                    $description = get_post_meta($variation->ID, '_variation_description', true);
     690                    if (!empty($description)) {
     691                        $content['post_full'][$post_id]['woocommerce']['_variations'][$variation->ID]['description'] = $description;
     692                    }
     693                }
    626694            }
    627695        }
    628696
    629697        return $content;
    630 
    631698    }
    632699
     
    640707    protected static function get_strings_by_package_id($package_id, $updated = false)
    641708    {
    642 
    643         //todo: needs update or refactor
    644         //        if ($updated) {
    645         //            $where = '(status = ' . self::STATUS_NEEDS_UPDATE . ' OR status = ' . self::STATUS_NOT_TRANSLATED . ' OR status = ' . self::STATUS_DUPLICATE . ')';
    646         //        } else {
    647         //            $where = 'status <> ' . self::STATUS_COMPLETE;
    648         //        }
    649 
    650709        global $wpdb;
    651710        $package_id = esc_sql($package_id);
     
    653712
    654713        return $wpdb->get_results($query, ARRAY_A);
    655 
    656714    }
    657715
  • easytranslate/trunk/admin/class-easy-translate-translation.php

    r2770454 r2795813  
    7676        $post_excerpt = $content[$source_post_id]['excerpt'] ?? '';
    7777        $woocommerce = $content[$source_post_id]['woocommerce'] ?? [];
     78        $yoast_seo = $content[$source_post_id]['yoast_seo'] ?? [];
    7879
    7980        if ($source_translations = Easy_Translate_Post_Translation_List::get_translations_by_element_id($source_post_id)) {
     
    8788                        $source_post_id,
    8889                        $translations['element_id'],
     90                        $target_language,
    8991                        $post_content,
    9092                        $post_title,
    9193                        $post_excerpt,
    92                         $woocommerce
     94                        $woocommerce,
     95                        $yoast_seo
    9396                    );
    9497                    Easy_Translate_Post_Translation_List::update_translations_status(
  • easytranslate/trunk/easy-translate.php

    r2780745 r2795813  
    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.7.4
     17 * Version:           1.8.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.7.4');
     36define('EASY_TRANSLATE_VERSION', '1.8.0');
    3737
    3838/**
Note: See TracChangeset for help on using the changeset viewer.