/** * Build WP_Query object for the given slider * * @see Hyyan_Slider_Shortcode::FILTER_SHORTCODE_QueryArgs * * @param string $slider slider name * @param string $order slides order * @param string $orderBy order slides by ? * * @return \WP_Query * @throws \RuntimeException if the slider does not exist */ function hyyan_slider_query($slider, $order = 'DESC', $orderBy = 'rand') { $slider = esc_attr((string) $slider); /** check for term existance */ if (!term_exists($slider, Hyyan_Slider::CUSTOM_TAXONOMY)) { throw new \RuntimeException(sprintf('Can not build query for %s slider - term does not exist', $slider)); } if (function_exists('pll_get_term')) { // get slider object $term = get_term_by('slug', $slider, Hyyan_Slider::CUSTOM_TAXONOMY); $slider = $term->slug; if ($term) { // find the id of translated slider $id = pll_get_term($term->term_id); // get translated slider object $trans_term = get_term($id, Hyyan_Slider::CUSTOM_TAXONOMY); if ($trans_term && !$trans_term instanceof WP_Error) { $slider = $trans_term->slug; } } } /** * Query object * * @see Hyyan_Slider_Shortcode::FILTER_SHORTCODE_QueryArgs * * @var WP_Query */ $query = new WP_Query(apply_filters(Hyyan_Slider_Events::FILTER_SHORTCODE_QueryArgs, array('post_type' => Hyyan_Slider::CUSTOM_POST, 'taxonomy' => Hyyan_Slider::CUSTOM_TAXONOMY, 'term' => $slider, 'post_status' => 'publish', 'order' => esc_attr($order), 'orderby' => esc_attr($orderBy), 'posts_per_page' => -1, 'lang' => null))); return $query; }
public function shim_untranslated($query_args, $args) { $def_lang = pll_default_language(); $cur_lang = pll_current_language(); $mixed = $cur_lang != $def_lang; if (apply_filters('listify_polylang_only_selected', false)) { return $query_args; } if (isset($query_args['tax_query']) && $mixed) { $taxes = $query_args['tax_query']; foreach ($taxes as $key => $tax) { $terms = $tax['terms']; $trans = array(); if (is_array($terms)) { foreach ($terms as $term) { // annoying since we have slugs but get an id but need back to slugs $obj = get_term_by('slug', $term, $tax['taxonomy']); $trans = pll_get_term($obj->term_id, $def_lang); $trans = get_term_by('id', $trans, $tax['taxonomy']); $query_args['tax_query'][$key]['terms'] = array_merge($terms, array($trans->slug)); } } } } $terms = get_terms('post_translations'); $exclude = array(); foreach ($terms as $translation) { $trans = unserialize($translation->description); if ($mixed) { $exclude[] = $trans[$def_lang]; } } if ($mixed) { if (isset($query_args['post__in'])) { $query_args['post__in'] = array_diff($query_args['post__in'], $exclude); unset($query_args['post__not_in']); } else { $query_args['post__not_in'] = $exclude; } } return $query_args; }
/** * Layered Nav Init * * @global array $_chosen_attributes * * @return false if not layered nav filter */ public function layeredNavInit() { if (!(is_active_widget(false, false, 'woocommerce_layered_nav', true) && !is_admin())) { return false; } global $_chosen_attributes; $attributes = wc_get_attribute_taxonomies(); foreach ($attributes as $tax) { $attribute = wc_sanitize_taxonomy_name($tax->attribute_name); $taxonomy = wc_attribute_taxonomy_name($attribute); $name = 'filter_' . $attribute; if (!(!empty($_GET[$name]) && taxonomy_exists($taxonomy))) { continue; } $terms = explode(',', $_GET[$name]); $termsTranslations = array(); foreach ($terms as $ID) { $translation = pll_get_term($ID); $termsTranslations[] = $translation ? $translation : $ID; } $_GET[$name] = implode(',', $termsTranslations); $_chosen_attributes[$taxonomy]['terms'] = $termsTranslations; } }
protected function translate_tax_query_recursive($tax_queries) { foreach ($tax_queries as $key => $q) { if (isset($q['taxonomy']) && $this->model->is_translated_taxonomy($q['taxonomy'])) { $arr = array(); $field = isset($q['field']) && in_array($q['field'], array('slug', 'name')) ? $q['field'] : 'term_id'; foreach ((array) $q['terms'] as $t) { $arr[] = ($tag = get_term_by($field, $t, $q['taxonomy'])) && ($tr_id = pll_get_term($tag->term_id)) && !is_wp_error($tr = get_term($tr_id, $q['taxonomy'])) ? $tr->{$field} : $t; } $tax_queries[$key]['terms'] = $arr; } elseif (is_array($q)) { $tax_queries[$key] = $this->translate_tax_query_recursive($q); } } return $tax_queries; }
/** * Twenty Fourteen * Translates the featured tag id in featured content settings * Mainly to allow hiding it when requested in featured content options * Acts only on frontend * * @since 1.4 * * @param array $settings featured content settings * @return array modified $settings */ public function twenty_fourteen_option_featured_content($settings) { if (!PLL_ADMIN && $settings['tag-id'] && ($tr = pll_get_term($settings['tag-id']))) { $settings['tag-id'] = $tr; } return $settings; }
/** * Tax filtering (home/blog posts filtered by cat) * @param array of term ids */ function tc_pll_translate_tax($term_ids) { if (!(is_array($term_ids) && !empty($term_ids))) { return $term_ids; } $translated_terms = array(); foreach ($term_ids as $id) { $translated_term = pll_get_term($id); $translated_terms[] = $translated_term ? $translated_term : $id; } return array_unique($translated_terms); }
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 "PLL_Frontend_Links->get_translation_url()". */ public function pll_translation_url_filter($url, $lang) { global $wp_query, $polylang; if (is_category()) { $term = get_category_by_slug($wp_query->get('category_name')); $translated_term = get_term(pll_get_term($term->term_id, $lang), $term->taxonomy); return home_url('/' . $lang . '/' . $translated_term->slug); } elseif (is_archive()) { $post_type = $wp_query->query_vars['post_type']; // If the post type is handle, let the "$this->get_post_type_archive_link" // function handle this. if (isset($this->post_types[$post_type])) { return $this->get_post_type_archive_link($post_type, $lang); } } return $url; }
private function getPolylangRedirectUrl($lang_code) { global $polylang; $lang = $polylang->model->get_language($lang_code); $from = $this->site_url . $this->request_uri; if (is_singular() && !is_front_page()) { $translated_post_id = pll_get_post(get_queried_object_id(), $lang->slug); if ($translated_post_id && get_post_status($translated_post_id) === 'publish') { return get_permalink($translated_post_id); } else { return new WP_Error('404', __('Page Not Found', 'wp-geo-redirect')); } } if (is_front_page()) { return pll_home_url($lang->slug); } if (is_home()) { if (get_option('show_on_front') === 'page') { $translated_post_id = pll_get_post(get_option('page_for_posts'), $lang->slug); if ($translated_post_id && get_post_status($translated_post_id) === 'publish') { return get_permalink($translated_post_id); } else { return new WP_Error('404', __('Page Not Found', 'wp-geo-redirect')); } } else { return pll_home_url($lang->slug); } } if (is_search()) { return $polylang->links_model->switch_language_in_link($from, $lang); } if (is_tax() || is_category() || is_tag()) { $translated_term_id = pll_get_term(get_queried_object_id(), $lang->slug); if (get_queried_object_id() === $translated_term_id) { return; } return get_term_link($translated_term_id, get_queried_object()->taxonomy); } return $polylang->links_model->switch_language_in_link($from, $lang); }
/** * Translate Category IDS for category report * * @global \Polylang $polylang * @global \WooCommerce $woocommerce * * @return false if woocommerce or polylang not found */ public function translateCategoryIDS() { global $polylang, $woocommerce; if (!$polylang || !$woocommerce) { return false; } /* Check for product_ids */ if (!isset($_GET['show_categories'])) { return false; } if (!static::isCombine() && (isset($_GET['lang']) && esc_attr($_GET['lang']) !== 'all')) { $IDS = (array) $_GET['show_categories']; $extendedIDS = array(); $lang = esc_attr($_GET['lang']); foreach ($IDS as $ID) { $translation = pll_get_term($ID, $lang); if ($translation) { $extendedIDS[] = $translation; } } if (!empty($extendedIDS)) { $_GET['show_categories'] = $extendedIDS; } } }
/** * Twenty Fourteen * Translates the featured tag id in featured content settings * Mainly to allow hiding it when requested in featured content options * Acts only on frontend * * @since 1.4 * * @param array $settings featured content settings * @return array modified $settings */ public function twenty_fourteen_option_featured_content($settings) { if (PLL() instanceof PLL_Frontend && $settings['tag-id'] && ($tr = pll_get_term($settings['tag-id']))) { $settings['tag-id'] = $tr; } return $settings; }
public function get_terms_args($args, $taxonomies) { if (!empty($args['include']) && $this->model->is_translated_taxonomy($taxonomies)) { foreach (wp_parse_id_list($args['include']) as $id) { $arr[] = ($tr = pll_get_term($id)) ? $tr : $id; } $args['include'] = $arr; } return $args; }
/** * Get variations default attributes translation. * * Get the translation of the default attributes of product passed by id, in * a given language, if one is passed, otherwise in all available languages. * * @param int $product_id (required) Product id. * @param string $lang (optional) Language slug. * * @return array Indexed array, with language slug as key, of attributes * pairs [attribute] => attribute slug */ public static function getDefaultAttributesTranslation($product_id, $lang = '') { $product = wc_get_product($product_id); $translated_attributes = array(); if ($product && 'variable' === $product->product_type) { $default_attributes = $product->get_variation_default_attributes(); $terms = array(); // Array of terms: if the term is taxonomy each value is a term object, otherwise an array (term slug => term value) $langs = array(); foreach ($default_attributes as $key => $value) { $term = get_term_by('name', $value, $key); if ($term && pll_is_translated_taxonomy($term->taxonomy)) { $terms[] = $term; } else { $terms[] = array($key => $value); } } // For each product translation, get the translated default attributes if (empty($lang)) { $langs = pll_languages_list(); } else { $langs[] = $lang; // get translation for a specific language } foreach ($langs as $lang) { $translated_terms = array(); foreach ($terms as $term) { if (is_object($term)) { $translated_term_id = pll_get_term($term->term_id, $lang); $translated_term = get_term_by('id', $translated_term_id, $term->taxonomy); $translated_terms[$translated_term->taxonomy] = $translated_term->slug; } else { $translated_terms[key($term)] = $term[key($term)]; } } $translated_attributes[$lang] = $translated_terms; } } return $translated_attributes; }
/** * Get an array with all available translations for a given term * * @param int $termID * @return array Indexed array with as key the language code, and as value the post id */ public function getTranslatedTerms($termID) { $languages = pll_the_languages(array('raw' => 1)); $result = array(); foreach ($languages as $lang) { $lang = $lang['slug']; $transTermID = pll_get_term($termID, $lang); if (is_int($transTermID) && $transTermID !== $termID) { $result[$lang] = $transTermID; } } return $result; }
/** * 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 ) }
/** * 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); } } } }
<?php global $page_class; $page_class = 'page-business'; get_header(); add_action('parse_query', function ($wp_query) { if (!$wp_query->is_main_query()) { return; } $wp_query->set('orderby', 'ID'); $wp_query->set('order', 'ASC'); }); ?> <section class="banner" style="background-image:url('<?php echo get_stylesheet_directory_uri() . '/img/' . get_category(pll_get_term($wp_query->query_vars['cat'], 'en'))->slug; ?> -banner.jpg')"></section> <section class="breadcrumb-box"> <div class="container"> <ul class="breadcrumb"> <li> <a href="<?php echo site_url(); ?> "><?php echo pll__('Home'); ?> </a> <span>》</span> </li>
public function upgrade_1_2_3() { // old version < 1.1 // multilingal locations and switcher item were stored in a dedicated option if (version_compare($this->options['version'], '1.1', '<')) { if ($menu_lang = get_option('polylang_nav_menus')) { foreach ($menu_lang as $location => $arr) { if (!in_array($location, array_keys(get_registered_nav_menus()))) { continue; } $switch_options = array_slice($arr, -5, 5); $translations = array_diff_key($arr, $switch_options); $has_switcher = array_shift($switch_options); foreach (get_terms('language', array('hide_empty' => 0)) as $lang) { // move nav menus locations if (!empty($translations[$lang->slug])) { $locations[$location][$lang->slug] = $translations[$lang->slug]; } // create the menu items for the language switcher if (!empty($has_switcher)) { $menu_item_db_id = wp_update_nav_menu_item($translations[$lang->slug], 0, array('menu-item-title' => __('Language switcher', 'polylang'), 'menu-item-url' => '#pll_switcher', 'menu-item-status' => 'publish')); update_post_meta($menu_item_db_id, '_pll_menu_item', $switch_options); } } } if (!empty($locations)) { $this->options['nav_menus'][get_option('stylesheet')] = $locations; } delete_option('polylang_nav_menus'); } } elseif (empty($this->options['nav_menus'])) { $menus = get_theme_mod('nav_menu_locations'); if (is_array($menus)) { // if old version < 1.2 // clean the WP option as it was a bad idea to pollute it if (version_compare($this->options['version'], '1.2', '<')) { foreach ($menus as $loc => $menu) { if ($pos = strpos($loc, '#')) { unset($menus[$loc]); } } set_theme_mod('nav_menu_locations', $menus); } // get the multilingual locations foreach ($menus as $loc => $menu) { foreach (get_terms('language', array('hide_empty' => 0)) as $lang) { $arr[$loc][$lang->slug] = pll_get_term($menu, $lang); } } if (!empty($arr)) { $this->options['nav_menus'][get_option('stylesheet')] = $arr; } } } }
public static function has_language_content($language) { // posts $objects = get_objects_in_term($language->term_id, 'language'); if (!empty($objects)) { return true; } // terms, only the default category is accepted $objects = get_objects_in_term($language->tl_term_id, 'term_language'); return count($objects) > 1 || isset($objects[0]) && $objects[0] != pll_get_term(get_option('default_category'), $language->slug); }