/** * @param int $postId the post id * @return string language of the post or false if not found */ public static function getPostLanguage($postId) { if (function_exists('pll_get_post_language')) { return pll_get_post_language($postId); } return false; }
/** * Sync porduct meta * * @return false if the current post type is not "porduct" */ public function syncProductsMeta() { $currentScreen = get_current_screen(); if ($currentScreen->post_type !== 'product') { return false; } // sync product meta with polylang add_filter('pll_copy_post_metas', array(__CLASS__, 'getProductMetaToCopy')); $ID = false; $disable = false; /* * Disable editing product meta for translation * * if the "post" is defined in $_GET then we should check if the current * porduct has a translation and it is the same as the default translation * lang defined in polylang then porduct meta editing must by enabled * * if the "new_lang" is defined or if the current page is the "edit" * page then porduct meta editing must by disabled */ if (isset($_GET['post'])) { $ID = absint($_GET['post']); $disable = $ID && pll_get_post_language($ID) != pll_default_language(); } elseif (isset($_GET['new_lang']) || $currentScreen->base == 'edit') { $disable = isset($_GET['new_lang']) && esc_attr($_GET['new_lang']) != pll_default_language() ? true : false; $ID = isset($_GET['from_post']) ? absint($_GET['from_post']) : false; } // disable fields edit for translation if ($disable) { add_action('admin_print_scripts', array($this, 'addFieldsLocker'), 100); } /* sync selected prodcut type */ $this->syncSelectedProdcutType($ID); }
/** * Get product attributes in right language * * @param array $args array of arguments for get_terms function in WooCommerce attributes html markup * * @return array */ public function getProductAttributesInLanguage($args) { $lang = ''; global $post; if (isset($_GET['new_lang'])) { $lang = esc_attr($_GET['new_lang']); } elseif (!empty($post)) { $lang = pll_get_post_language($post->ID); } else { $lang = PLL()->pref_lang; } $args['lang'] = $lang; return $args; }
public function display_payment_language($payment_id) { ?> <div class="column-container"> <div class="column"> <strong><?php _e('Language:', 'edd-polylang'); ?> </strong> <input type="text" name="edd-payment-language" value="<?php echo esc_attr(pll_get_post_language($payment_id, 'name')); ?> " class="medium-text"/> </div> </div> <?php }
/** * Change the product stock in the given order item. * * @param array $item the order data * @param string $action STOCK_REDUCE_ACTION | STOCK_INCREASE_ACTION */ protected function change(array $item, $action = self::STOCK_REDUCE_ACTION) { $productID = $item['product_id']; $productObject = wc_get_product($productID); $productLang = pll_get_post_language($productID); $variationID = $item['variation_id']; /* Handle Products */ if ($productObject && $productLang) { /* Get the translations IDS */ $translations = Utilities::getProductTranslationsArrayByObject($productObject); /* Remove the current product from translation array */ unset($translations[$productLang]); $isManageStock = $productObject->managing_stock(); $isVariation = $variationID && $variationID > 0; $method = $action === self::STOCK_REDUCE_ACTION ? 'reduce_stock' : 'increase_stock'; $change = $action === self::STOCK_REDUCE_ACTION ? $item['qty'] : $item['change']; /* Sync stock for all translation */ foreach ($translations as $ID) { /* Only if product is managing stock */ if ($isManageStock) { if ($translation = wc_get_product($ID)) { $translation->{$method}($change); } } $general = Settings::getOption('general', MetasList::getID(), array('total_sales')); if (in_array('total_sales', $general)) { update_post_meta($ID, 'total_sales', get_post_meta($productID, 'total_sales', true)); } } /* Handle variation */ if ($isVariation) { $posts = Variation::getRelatedVariation($variationID); foreach ($posts as $post) { if ($post->ID == $variationID) { continue; } $variation = wc_get_product($post); if ($variation && $variation->managing_stock()) { $variation->{$method}($change); } } } } }
/** * Sync product meta. * * @return false if the current post type is not "product" */ public function syncProductsMeta() { // sync product meta with polylang add_filter('pll_copy_post_metas', array(__CLASS__, 'getProductMetaToCopy')); // Shipping Class translation is not supported after WooCommerce 2.6 but it is // still implemented by WooCommerce as a taxonomy. Therefore Polylang will not // copy the Shipping Class meta. We need to take care of it. if (Utilities::woocommerceVersionCheck('2.6')) { add_action('wp_insert_post', array($this, 'syncShippingClass'), 10, 3); } $currentScreen = get_current_screen(); if ($currentScreen->post_type !== 'product') { return false; } $ID = false; $disable = false; /* * Disable editing product meta for translation * * if the "post" is defined in $_GET then we should check if the current * product has a translation and it is the same as the default translation * lang defined in polylang then product meta editing must by enabled * * if the "new_lang" is defined or if the current page is the "edit" * page then product meta editing must by disabled */ if (isset($_GET['post'])) { $ID = absint($_GET['post']); $disable = $ID && pll_get_post_language($ID) != pll_default_language(); } elseif (isset($_GET['new_lang']) || $currentScreen->base == 'edit') { $disable = isset($_GET['new_lang']) && esc_attr($_GET['new_lang']) != pll_default_language() ? true : false; $ID = isset($_GET['from_post']) ? absint($_GET['from_post']) : false; // Add the '_translation_porduct_type' meta, for the case where // the product was created before plugin acivation. $this->addProductTypeMeta($ID); } // disable fields edit for translation if ($disable) { add_action('admin_print_scripts', array($this, 'addFieldsLocker'), 100); } /* sync selected product type */ $this->syncSelectedproductType($ID); }
/** * Checks if the slug is unique within language. * * @since 0.1.0 * * @global wpdb $wpdb WordPress database abstraction object. * * @param string $slug The desired slug (post_name). * @param int $post_ID Post ID. * @param string $post_status No uniqueness checks are made if the post is still draft or pending. * @param string $post_type Post type. * @param int $post_parent Post parent ID. * * @return string Unique slug for the post within language, based on $post_name (with a -1, -2, etc. suffix). */ function polylang_slug_unique_slug_in_language($slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug) { // Return slug if it was not changed. if ($original_slug === $slug) { return $slug; } global $wpdb; // Get language of a post $lang = pll_get_post_language($post_ID); $options = get_option('polylang'); // return the slug if Polylang does not return post language or has incompatable redirect setting or is not translated post type. if (empty($lang) || 0 === $options['force_lang'] || !pll_is_translated_post_type($post_type)) { return $slug; } // " INNER JOIN $wpdb->term_relationships AS pll_tr ON pll_tr.object_id = ID". $join_clause = polylang_slug_model_post_join_clause(); // " AND pll_tr.term_taxonomy_id IN (" . implode(',', $languages) . ")". $where_clause = polylang_slug_model_post_where_clause($lang); // Polylang does not translate attachements - skip if it is one. // @TODO Recheck this with the Polylang settings if ('attachment' == $post_type) { // Attachment slugs must be unique across all types. $check_sql = "SELECT post_name FROM {$wpdb->posts} {$join_clause} WHERE post_name = %s AND ID != %d {$where_clause} LIMIT 1"; $post_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $original_slug, $post_ID)); } elseif (is_post_type_hierarchical($post_type)) { // Page slugs must be unique within their own trees. Pages are in a separate // namespace than posts so page slugs are allowed to overlap post slugs. $check_sql = "SELECT ID FROM {$wpdb->posts} {$join_clause} WHERE post_name = %s AND post_type IN ( %s, 'attachment' ) AND ID != %d AND post_parent = %d {$where_clause} LIMIT 1"; $post_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $original_slug, $post_type, $post_ID, $post_parent)); } else { // Post slugs must be unique across all posts. $check_sql = "SELECT post_name FROM {$wpdb->posts} {$join_clause} WHERE post_name = %s AND post_type = %s AND ID != %d {$where_clause} LIMIT 1"; $post_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $original_slug, $post_type, $post_ID)); } if (!$post_name_check) { return $original_slug; } else { return $slug; } }
/** * Sync porduct meta * * @return false if the current post type is not "product" */ public function syncProductsMeta() { // sync product meta with polylang add_filter('pll_copy_post_metas', array(__CLASS__, 'getProductMetaToCopy')); $currentScreen = get_current_screen(); if ($currentScreen->post_type !== 'product') { return false; } $ID = false; $disable = false; /* * Disable editing product meta for product translations * * In case of a "Add or update product" ($GET['post'] is set), and the * product language is different from the default, it is a product translation * and editing the product metadata should be disabled. * * In case of a "Add product translation" ($GET['new_lang'] is set), or the * 'edit' page, editing product metadata should be disabled. */ if (isset($_GET['post'])) { // Add or update product $ID = absint($_GET['post']); $disable = $ID && pll_get_post_language($ID) != pll_default_language(); } elseif (isset($_GET['new_lang']) || $currentScreen->base == 'edit') { // Add product translation $ID = isset($_GET['from_post']) ? absint($_GET['from_post']) : false; $disable = isset($_GET['new_lang']) && esc_attr($_GET['new_lang']) != pll_default_language() ? true : false; // Add the '_translation_porduct_type' meta,for the case the product // was created before plugin acivation. $this->add_product_type_meta($ID); } // disable fields edit for product translations if ($disable) { add_action('admin_print_scripts', array($this, 'addFieldsLocker'), 100); } // sync the product type selection in the product data settings box $this->sync_product_type_selection($ID); }
/** * Translate Variation for given variable product * * @param integer $ID product variable ID * @param \WP_Post $post Product Post * @param boolean $update true if update , false otherwise * * @return boolean */ public function duplicateVariations($ID, \WP_Post $post, $update) { if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return false; } global $pagenow; if (!in_array($pagenow, array('post.php', 'post-new.php'))) { return false; } $product = wc_get_product($ID); if (!$product) { return false; } $from = null; if (pll_get_post_language($ID) == pll_default_language()) { $from = $product; } else { if (isset($_GET['from_post'])) { /* * This check will make sure that variation , will be * created for brand new products which are not saved yet by user */ $from = Utilities::getProductTranslationByID(esc_attr($_GET['from_post']), pll_default_language()); } else { $from = Utilities::getProductTranslationByObject($product, pll_default_language()); } } if (!$from instanceof \WC_Product_Variable) { return false; } $langs = pll_languages_list(); foreach ($langs as $lang) { $variation = new Variation($from, Utilities::getProductTranslationByObject($product, $lang)); remove_action('save_post', array($this, __FUNCTION__), 10); $variation->duplicate(); add_action('save_post', array($this, __FUNCTION__), 10, 3); } }
public function jetpack_widget_get_top_posts($posts, $post_ids, $count) { foreach ($posts as $k => $post) { if (pll_current_language() !== pll_get_post_language($post['post_id'])) { unset($posts[$k]); } } return $posts; }
/** * Jetpack * Allows to make sure that related posts are in the correct language * * @since 1.8 * * @param array $filters Array of ElasticSearch filters based on the post_id and args. * @param string $post_id Post ID of the post for which we are retrieving Related Posts. * @return array */ function jetpack_relatedposts_filter_filters($filters, $post_id) { $slug = sanitize_title(pll_get_post_language($post_id, 'name')); $filters[] = array('term' => array('taxonomy.language.slug' => $slug)); return $filters; }
public function ajax_get_current_status() { $lgtm =& $GLOBALS['wp_lingotek']->model; $pllm = $GLOBALS['polylang']->model; $languages = pll_languages_list(array('fields' => 'locale')); $object_ids = $_POST['check_ids']; if ($object_ids === null) { return; } $terms = isset($_POST['terms_translations']); //The main array consists of //ids and nonces. Each id has a source language, languages with statuses, and a workbench link $content_metadata = array(); foreach ($object_ids as $object_id) { $id = $object_id; $type = $terms ? 'term' : 'post'; if (isset($_POST['taxonomy'])) { $taxonomy = $_POST['taxonomy']; if (strpos($_POST['taxonomy'], '&')) { $taxonomy = strstr($_POST['taxonomy'], '&', true); } } else { $taxonomy = get_post_type($id); } $content_metadata[$id] = array('existing_trans' => false, 'source' => false, 'doc_id' => null, 'source_id' => null, 'source_status' => null); $document = $lgtm->get_group($type, $object_id); if ($document && !isset($document->source) && count($document->desc_array) >= 3) { $content_metadata[$id]['existing_trans'] = true; } if ($document && isset($document->source) && isset($document->document_id) && isset($document->status) && isset($document->translations)) { if ($document->source !== (int) $object_id) { $document = $lgtm->get_group($type, $document->source); } $source_id = $document->source !== null ? $document->source : $object_id; $source_language = $terms ? pll_get_term_language($document->source, 'locale') : pll_get_post_language($document->source, 'locale'); $existing_translations = $pllm->get_translations($type, $source_id); if (count($existing_translations) > 1) { $content_metadata[$id]['existing_trans'] = true; } $content_metadata[$id]['source'] = $source_language; $content_metadata[$id]['doc_id'] = $document->document_id; $content_metadata[$id]['source_id'] = $document->source; $content_metadata[$id]['source_status'] = $document->status; $target_status = $document->status == 'edited' || $document->status == null ? 'edited' : 'current'; $content_metadata[$id][$source_language]['status'] = $document->source == $object_id ? $document->status : $target_status; if (is_array($document->translations)) { foreach ($document->translations as $locale => $translation_status) { $content_metadata[$id][$locale]['status'] = $translation_status; $workbench_link = Lingotek_Actions::workbench_link($document->document_id, $locale); $content_metadata[$id][$locale]['workbench_link'] = $workbench_link; } } //fills in missing languages, makes life easier for the updater foreach ($languages as $language) { foreach ($content_metadata as $group => $status) { $language_obj = $pllm->get_language($source_language); $target_lang_obj = $pllm->get_language($language); $profile = Lingotek_Model::get_profile($taxonomy, $language_obj, $group); if ($profile['profile'] != 'disabled' && $status['source'] != false) { if (!isset($status[$language])) { $content_metadata[$group][$language]['status'] = "none"; if ($document->is_disabled_target($pllm->get_language($source_language), $pllm->get_language($language)) || isset($document->desc_array[$target_lang_obj->slug]) && !isset($document->source)) { $content_metadata[$group][$language]['status'] = 'disabled'; } } } } } } $language = $type == 'post' ? pll_get_post_language($id) : pll_get_term_language($id); $language = $pllm->get_language($language); if ($language) { $profile = Lingotek_Model::get_profile($taxonomy, $language, $id); if ($profile['profile'] == 'disabled' && $content_metadata[$id]['source'] == false) { $content_metadata[$id]['source'] = 'disabled'; } } } //get the nonces associated with the different actions $content_metadata['request_nonce'] = $this->lingotek_get_matching_nonce('lingotek-request'); $content_metadata['download_nonce'] = $this->lingotek_get_matching_nonce('lingotek-download'); $content_metadata['upload_nonce'] = $this->lingotek_get_matching_nonce('lingotek-upload'); $content_metadata['status_nonce'] = $this->lingotek_get_matching_nonce('lingotek-status'); wp_send_json($content_metadata); }
/** * Get the order language * * @param integer $ID order ID * * @return string|false language in success , false otherwise */ public static function getOrderLangauge($ID) { return pll_get_post_language($ID); }
public function edit_form_top() { global $post_ID; printf('<input type="hidden" id="post_lang_choice" name="post_lang_choice" value="%s" />', pll_get_post_language($post_ID)); wp_nonce_field('pll_language', '_pll_nonce'); }
/** * listing_images_default() * * Callback function to set the default * gallery with translated images if * these are available. * * @access public * @param array $field_args * @param object $field * @uses pll_get_post_language() * @uses pll_default_language() * @uses pll_get_post() * @uses get_post_meta() * @return array * * @since 1.0.0 */ public function listing_images_default($field_args, $field) { // Get post language $post_lang = pll_get_post_language($field->object_id); // Get from post early $from_post = isset($_REQUEST['from_post']) ? $_REQUEST['from_post'] : false; // If from_post is not available anymore, use current post ID if (!$from_post) { $from_post = $field->object_id; } // Get post ID of default language $origial = pll_get_post($from_post, pll_default_language()); // Get original gallery $gallery = get_post_meta($origial, '_gallery', true); if (empty($gallery)) { return; } // Set up translated gallery $gallery_lang = array(); foreach ($gallery as $id => $url) { // Get ID of image translation $id_lang = pll_get_post($id, $post_lang); if ($id_lang) { // When available, set new ID $gallery_lang[$id_lang] = $url; } } // If there are image translations, set new gallery default if (!empty($gallery_lang)) { return $gallery_lang; } }
public function skip_default_attributes_meta($check, $object_id, $meta_key, $meta_value) { // Ignore if not default attribute if ('_default_attributes' === $meta_key) { $product = wc_get_product($object_id); // Don't let anyone delete the meta. NO ONE! if ($product && current_filter() === 'delete_post_metadata') { return false; } // _default_attributes meta should be unique if ($product && current_filter() === 'add_post_metadata') { $old_value = get_post_meta($product->id, '_default_attributes'); return empty($old_value) ? $check : false; } // Maybe is Variable Product // New translations of Variable Products are first created as simple if ($product && Utilities::maybe_variable_product($product)) { // Try Polylang first $lang = pll_get_post_language($product->id); if (!$lang) { // Must be a new translation and Polylang doesn't stored the language yet $lang = isset($_GET['new_lang']) ? $_GET['new_lang'] : ''; } foreach ($meta_value as $key => $value) { $term = get_term_by('slug', $value, $key); if ($term && pll_is_translated_taxonomy($term->taxonomy)) { $translated_term_id = pll_get_term($term->term_id, $lang); $translated_term = get_term_by('id', $translated_term_id, $term->taxonomy); // If meta is taxonomy managed by Polylang and is in the // correct language process, otherwise return false to // stop execution return $value === $translated_term->slug ? $check : false; } } } } return $check; }
/** * Fix "get_permalink" for this post type. */ public function post_type_link_filter($post_link, $post, $leavename, $sample) { // Check if the post type is handle. if (isset($this->post_types[$post->post_type])) { // We always check for the post language. $lang = pll_get_post_language($post->ID, 'slug'); if (!$lang) { // If post has no language assigned, use default language as fallback. $lang = pll_default_language(); } // Build URL. Lang prefix is already handle. return home_url('/' . $this->post_types[$post->post_type]->translated_slugs[$lang]->rewrite['slug'] . '/' . ($leavename ? "%{$post->post_type}%" : get_page_uri($post->ID))); } return $post_link; }
/** * Get current language information from Multilingual plugins * * @since 2.6.6 * * @return array */ public static function get_current_language() { /** * @var $sitepress SitePress object * @var $polylang object */ /* * @todo wpml-comp Remove global object usage */ global $sitepress, $polylang; $lang_data = false; $translator = false; $current_language = false; // Multilingual support if (did_action('wpml_loaded') && apply_filters('wpml_setting', true, 'auto_adjust_ids')) { // WPML support $translator = 'WPML'; // Get the global current language (if set) $wpml_language = apply_filters('wpml_current_language', null); $current_language = $wpml_language != 'all' ? $wpml_language : ''; } elseif ((function_exists('PLL') || is_object($polylang)) && function_exists('pll_current_language')) { // Polylang support $translator = 'PLL'; // Get the global current language (if set) $current_language = pll_current_language('slug'); } /** * Admin functions that overwrite the current language * * @since 2.6.6 */ if (is_admin() && !empty($translator)) { if ($translator == 'PLL') { /** * Polylang support * Get the current user's perferred language. * This is a user meta setting that will overwrite the language returned from pll_current_language() * @see polylang/admin/admin-base.php -> init_user() */ $current_language = get_user_meta(get_current_user_id(), 'pll_filter_content', true); } // Get current language based on the object language if available if (function_exists('get_current_screen')) { $current_screen = get_current_screen(); /** * Overwrite the current language if needed for post types */ if (isset($current_screen->base) && ($current_screen->base == 'post' || $current_screen->base == 'edit')) { if (!empty($_GET['post'])) { /** * WPML support * In WPML the current language is always set to default on an edit screen * We need to overwrite this when the current object is not-translatable to enable relationships with different languages */ if ($translator == 'WPML' && !apply_filters('wpml_is_translated_post_type', false, get_post_type($_GET['post']))) { // Overwrite the current language to nothing if this is a NOT-translatable post_type $current_language = ''; } /** * Polylang support (1.5.4+) * In polylang the preferred language could be anything. * We only want the related objects if they are not translatable OR the same language as the current object */ if ($translator == 'PLL' && function_exists('pll_get_post_language') && pll_is_translated_post_type(get_post_type($_GET['post']))) { // Overwrite the current language if this is a translateable post_type $current_language = pll_get_post_language((int) $_GET['post']); } } /** * Polylang support (1.0.1+) * In polylang the preferred language could be anything. * When we're adding a new object and language is set we only want the related objects if they are not translatable OR the same language */ if ($translator == 'PLL' && !empty($_GET['new_lang']) && !empty($_GET['post_type']) && pll_is_translated_post_type(sanitize_text_field($_GET['post_type']))) { $current_language = $_GET['new_lang']; } /** * Overwrite the current language if needed for taxonomies */ } elseif (isset($current_screen->base) && ($current_screen->base == 'term' || $current_screen->base == 'edit-tags')) { // @todo MAYBE: Similar function like get_post_type for taxonomies so we don't need to check for $_GET['taxonomy'] if (!empty($_GET['taxonomy'])) { /* * @todo wpml-comp API call for taxonomy needed! * Suggested API call: * add_filter( 'wpml_is_translated_taxonomy', $_GET['taxonomy'], 10, 2 ); */ /** * WPML support * In WPML the current language is always set to default on an edit screen * We need to overwrite this when the current object is not-translatable to enable relationships with different languages */ if ($translator == 'WPML' && method_exists($sitepress, 'is_translated_taxonomy') && !$sitepress->is_translated_taxonomy($_GET['taxonomy'])) { // Overwrite the current language to nothing if this is a NOT-translatable taxonomy $current_language = ''; } /** * Polylang support (1.5.4+) * In polylang the preferred language could be anything. * We only want the related objects if they are not translatable OR the same language as the current object */ if ($translator == 'PLL' && !empty($_GET['tag_ID']) && function_exists('pll_get_term_language') && pll_is_translated_taxonomy(sanitize_text_field($_GET['taxonomy']))) { // Overwrite the current language if this is a translatable taxonomy $current_language = pll_get_term_language((int) $_GET['tag_ID']); } } /** * Polylang support (1.0.1+) * In polylang the preferred language could be anything. * When we're adding a new object and language is set we only want the related objects if they are not translatable OR the same language */ if ($translator == 'PLL' && !empty($_GET['new_lang']) && !empty($_GET['taxonomy']) && pll_is_translated_taxonomy(sanitize_text_field($_GET['taxonomy']))) { $current_language = $_GET['new_lang']; } } } } $current_language = pods_sanitize(sanitize_text_field($current_language)); if (!empty($current_language)) { // We need to return language data $lang_data = array('language' => $current_language, 't_id' => 0, 'tt_id' => 0, 'term' => null); /** * Polylang support * Get the language taxonomy object for the current language */ if ($translator == 'PLL') { $current_language_t = false; // Get the language term object if (function_exists('PLL') && isset(PLL()->model) && method_exists(PLL()->model, 'get_language')) { // Polylang 1.8 and newer $current_language_t = PLL()->model->get_language($current_language); } elseif (is_object($polylang) && isset($polylang->model) && method_exists($polylang->model, 'get_language')) { // Polylang 1.2 - 1.7.x $current_language_t = $polylang->model->get_language($current_language); } elseif (is_object($polylang) && method_exists($polylang, 'get_language')) { // Polylang 1.1.x and older $current_language_t = $polylang->get_language($current_language); } // If the language object exists, add it! if ($current_language_t && !empty($current_language_t->term_id)) { $lang_data['t_id'] = (int) $current_language_t->term_id; $lang_data['tt_id'] = (int) $current_language_t->term_taxonomy_id; $lang_data['term'] = $current_language_t; } } } /** * Override language data used by Pods. * * @since 2.6.6 * * @param array|false $lang_data { * Language data * * @type string $language Language slug * @type int $t_id Language term_id * @type int $tt_id Language term_taxonomy_id * @type WP_Term $term Language term object * } * @param string|boolean $translator Language plugin used */ $lang_data = apply_filters('pods_get_current_language', $lang_data, $translator); return $lang_data; }
/** * dashboard_column_lang() * * Display language column * in dashboard. * * @uses wp_list_pluck() * @uses pll_the_languages() * @uses pll_get_post_language() * * @since 1.0.0 */ public function dashboard_column_lang($post) { $languages = wp_list_pluck(pll_the_languages(array('raw' => 1)), 'flag', 'slug'); $lang = pll_get_post_language($post->ID); echo '<div style="text-align:center"><img src="' . $languages[$lang] . '" /></div>'; }
/** * Translates Woocommerce email subjects and headings content to the order language. * * @param string $string Subject or heading not translated * @param WC_Order $order Order object * @param string $string_type Type of string to translate <subject | heading> * @param string $email_type Email template * * @return string Translated string, returns the original $string on error */ public function translateEmailStringToOrderLanguage($string, $order, $string_type, $email_type) { if (empty($order)) { return $string; // Returns the original $string on error (no order to get language from) } // Get order language $order_language = pll_get_post_language($order->id, 'locale'); if ($order_language == '') { $order_language = pll_current_language('locale'); } // Get setting used to register string in the Polylang strings translation table $_string = $string; // Store original string to return in case of error if (false == ($string = $this->getEmailSetting($string_type, $email_type)) && !isset($this->default_settings[$email_type . '_' . $string_type])) { return $_string; // No setting in Polylang strings translations table nor default string found to translate } // Switch language $this->switchLanguage($order_language); if ($string) { // Retrieve translation from Polylang Strings Translations table $string = pll__($string); } else { // If no user translation found in Polylang Strings Translations table, use WooCommerce default translation $string = __($this->default_settings[$email_type . '_' . $string_type], 'woocommerce'); } $find = array(); $replace = array(); $find['order-date'] = '{order_date}'; $find['order-number'] = '{order_number}'; $find['site_title'] = '{site_title}'; $replace['order-date'] = date_i18n(wc_date_format(), strtotime($order->order_date)); $replace['order-number'] = $order->get_order_number(); $replace['site_title'] = get_bloginfo('name'); $string = str_replace($find, $replace, $string); return $string; }
/** * Custom add to cart handler for variable products * * Based on function add_to_cart_handler_variable( $product_id ) from * <install_dir>/wp-content/plugins/woocommerce/includes/class-wc-form-handler.php * but using $url as argument.Therefore we use the initial bits from * add_to_cart_action( $url ). * * @param string $url Add to cart url (e.g. https://www.yourdomain.com/?add-to-cart=123&quantity=1&variation_id=117&attribute_size=Small&attribute_color=Black ) */ public function add_to_cart_handler_variable($url) { // From add_to_cart_action( $url ) if (empty($_REQUEST['add-to-cart']) || !is_numeric($_REQUEST['add-to-cart'])) { return; } $product_id = apply_filters('woocommerce_add_to_cart_product_id', absint($_REQUEST['add-to-cart'])); $was_added_to_cart = false; $adding_to_cart = wc_get_product($product_id); if (!$adding_to_cart) { return; } // End: From add_to_cart_action( $url ) // From add_to_cart_handler_variable( $product_id ) $variation_id = empty($_REQUEST['variation_id']) ? '' : absint($_REQUEST['variation_id']); $quantity = empty($_REQUEST['quantity']) ? 1 : wc_stock_amount($_REQUEST['quantity']); $missing_attributes = array(); $variations = array(); $attributes = $adding_to_cart->get_attributes(); // If no variation ID is set, attempt to get a variation ID from posted attributes. if (empty($variation_id)) { $variation_id = $adding_to_cart->get_matching_variation(wp_unslash($_POST)); } /** * Custom code to check if a translation of the product is already in the * cart,* and in that case, replace the variation being added to the cart * by the respective translation in the language of the product already * in the cart. * NOTE: The product_id is filtered by $this->add_to_cart() and holds the * id of the product translation, if one exists in the cart. */ if ($product_id != absint($_REQUEST['add-to-cart'])) { // There is a translation of the product already in the cart: // Get the language of the product in the cart $lang = pll_get_post_language($product_id); // Get the respective variation in the language of the product in the cart $variation = $this->get_variation_translation($variation_id, $lang); $variation_id = $variation->variation_id; } else { $variation = wc_get_product($variation_id); } /** * End of custom code. */ //$variation = wc_get_product( $variation_id ); // Verify all attributes foreach ($attributes as $attribute) { if (!$attribute['is_variation']) { continue; } $taxonomy = 'attribute_' . sanitize_title($attribute['name']); if (isset($_REQUEST[$taxonomy])) { // Get value from post data if ($attribute['is_taxonomy']) { // Don't use wc_clean as it destroys sanitized characters $value = sanitize_title(stripslashes($_REQUEST[$taxonomy])); /** * Custom code to check if a translation of the product is already in the cart, * and in that case, replace the variation attribute being added to the cart by * the respective translation in the language of the product already in the cart * NOTE: The product_id is filtered by $this->add_to_cart() and holds the id of * the product translation, if one exists in the cart. */ if ($product_id != absint($_REQUEST['add-to-cart'])) { // Get the translation of the term $term = get_term_by('slug', $value, $attribute['name']); $_term = get_term_by('id', pll_get_term(absint($term->term_id), $lang), $attribute['name']); if ($_term) { $value = $_term->slug; } } /** * End of custom code. */ } else { $value = wc_clean(stripslashes($_REQUEST[$taxonomy])); } // Get valid value from variation $valid_value = isset($variation->variation_data[$taxonomy]) ? $variation->variation_data[$taxonomy] : ''; // Allow if valid if ('' === $valid_value || $valid_value === $value) { $variations[$taxonomy] = $value; continue; } } else { $missing_attributes[] = wc_attribute_label($attribute['name']); } } if (!empty($missing_attributes)) { wc_add_notice(sprintf(_n('%s is a required field', '%s are required fields', sizeof($missing_attributes), 'woocommerce'), wc_format_list_of_items($missing_attributes)), 'error'); } elseif (empty($variation_id)) { wc_add_notice(__('Please choose product options…', 'woocommerce'), 'error'); } else { // Add to cart validation $passed_validation = apply_filters('woocommerce_add_to_cart_validation', true, $product_id, $quantity, $variation_id, $variations); if ($passed_validation && WC()->cart->add_to_cart($product_id, $quantity, $variation_id, $variations) !== false) { wc_add_to_cart_message(array($product_id => $quantity), true); //return true; Doing an action, no return needed but we need to set $was_added_to_cart to trigger the redirect $was_added_to_cart = true; } else { $was_added_to_cart = false; } } //return false; Doing an action, no return needed but we need to set $was_added_to_cart to trigger the redirect // End: From add_to_cart_handler_variable( $product_id ) /** * Because this is a custom handler we need to take care of the rediret * to the cart. Again we use the code from add_to_cart_action( $url ) */ // From add_to_cart_action( $url ) // If we added the product to the cart we can now optionally do a redirect. if ($was_added_to_cart && wc_notice_count('error') === 0) { // If has custom URL redirect there if ($url = apply_filters('woocommerce_add_to_cart_redirect', $url)) { wp_safe_redirect($url); exit; } elseif (get_option('woocommerce_cart_redirect_after_add') === 'yes') { wp_safe_redirect(wc_get_cart_url()); exit; } } // End: From add_to_cart_action( $url ) }
public function getPostLocale() { $locale = pll_get_post_language($this->postId, 'locale'); $locale = $this->filterLocale($locale); return $locale; }
/** * Whether to ignore a published post. * * Currently only ignores Polylang translations. * * @param $post_id * @return bool */ protected static function ignore_published_post($post_id) { if (self::is_wpml_translation($post_id)) { return true; } if (!function_exists('pll_default_language')) { return false; } $default_slug = pll_default_language('slug'); $post_slug = pll_get_post_language($post_id, 'slug'); return $default_slug !== $post_slug; }
<?php if (has_post_thumbnail()) { the_post_thumbnail('blog-thumbnail', array('alt' => '' . get_the_title() . '')); } else { echo '<img src="' . get_bloginfo('template_url') . '/img/default.png" alt="Without Image">'; } ?> <div class="language"> <?php pll_get_post_language($post_id); ?> </div> </a></div> <div> <h2><a href="<?php the_permalink(); ?> " rel="bookmark"><?php the_title(); pll_get_post_language($post_id); ?> </a></h2> <div><?php the_excerpt(); ?> </div> </div> </article>
/** * Get the language of a post * * @return string Post language code */ function filter_get_post_language($language_code, $post) { $post_language = isset($post) ? pll_get_post_language($post->ID, 'slug') : null; return $post_language; }
/** * Retrive list of translated objects given a post or term ID or object. * * @param string $type Post type or taxonomy. * @param int|WP_Post $id Optional. Post or Term ID or object. Default current post. * @param bool $add_self Optional. If no translations, return self. Default is "false". * @return array $translations List of translated posts or terms. * * @package WordPress\Skeleton\Polylang */ function pll_get_translations($type, $id, $add_self = false) { global $polylang; if (!is_object($polylang)) { return false; } $translations = $polylang->model->get_translations($type, $id); if (empty($translations) && $add_self) { if ($type == 'post' || $polylang->is_translated_post_type($type)) { $translations = [pll_get_post_language($id) => $id]; } elseif ($type == 'term' || $polylang->is_translated_taxonomy($type)) { $translations = [pll_get_term_language($id) => $id]; } } if (is_array($translations) && !empty($translations)) { foreach ($translations as $language => $object_id) { $translations[$language] = is_tax($type) ? get_term($object_id, $type) : get_post($object_id); } } return $translations; }
/** * Copy variation meta * * The method follow the same method polylang use to sync metas between * translations * * @param integer $from product variation ID * @param integer $to product variation ID */ protected function copyVariationMetas($from, $to) { /* copy or synchronize post metas and allow plugins to do the same */ $metas = get_post_custom($from); /* get public and protected meta keys */ $keys = array_unique(array_merge(array_keys($metas), array_keys(get_post_custom($to)))); /* synchronize */ foreach ($keys as $key) { /* * the synchronization process of multiple values custom fields is * easier if we delete all metas first */ delete_post_meta($to, $key); if (isset($metas[$key])) { // Some attributes are taxonomies managed by Polylang. // For taxonomies we need to ask Polylang for the term translation if (substr($key, 0, 10) == 'attribute_') { $translated = array(); $tax = str_replace('attribute_', '', $key); foreach ($metas[$key] as $termSlug) { $term = get_term_by('slug', $termSlug, $tax); if ($term) { $lang = isset($_GET['new_lang']) ? esc_attr($_GET['new_lang']) : pll_get_post_language($this->to->id); $translated[] = get_term_by('id', pll_get_term($term->term_id, $lang), $tax)->slug; } else { $translated[] = $termSlug; } } $metas[$key] = $translated; } foreach ($metas[$key] as $value) { /* * Important: always maybe_unserialize value coming from * get_post_custom. See codex: https://codex.wordpress.org/Function_Reference/get_post_custom */ $value = maybe_unserialize($value); add_post_meta($to, $key, $value); } } } }
/** * Filter Permalink * * Filter permalinks to include the current endpoint translation * * @global \WP_Post $post * @global \WP $wp * * @param string $link post permalink * @param integer $ID post ID * * @return string filtered permalink */ public function filterPermalink($link, $ID) { global $post, $wp; if (!isset($post->ID) || is_admin()) { return $link; } $pageLang = pll_get_post_language($post->ID); if (icl_object_id($ID, 'page', false, $pageLang) !== $post->ID) { return $link; } $endpoints = WC()->query->get_query_vars(); foreach ($endpoints as $key => $endpoint) { if (!isset($wp->query_vars[$key])) { continue; } if (in_array($key, array('pay', 'order-received'))) { $endpoint = get_option('woocommerce_checkout_' . str_replace('-', '_', $key) . '_endpoint'); } else { $endpoint = get_option('woocommerce_myaccount_' . str_replace('-', '_', $key) . '_endpoint'); } $link = $this->rebuildUrl($this->getEndpointTranslation($endpoint), $wp->query_vars[$key], $link); } return $link; }
/** * Sets the locale on the settings page based upon to the locale of the post * * @param $postId */ private static function set_current_lang_from_post_id($postId) { if (self::$settings->languages_is_enabled()) { self::$settings->set_current_locale(pll_get_post_language($postId)); } }
public function getPostTranslations() { $language = pll_get_post_language($this->postId); $translations = pll_get_post_translations($this->postId); if (array_key_exists($language, $translations)) { unset($translations[$language]); } return $translations; }