Ejemplo n.º 1
0
 /**
  * Correct the shop page to display products from currrent language only.
  *
  * @param \WP $wp wordpress instance
  *
  * @return bool false if the current language is the same as default
  *              language or if the "pagename" var is empty
  */
 public function correctShopPage(\WP $wp)
 {
     global $polylang;
     $shopID = wc_get_page_id('shop');
     $shopOnFront = 'page' === get_option('show_on_front') && in_array(get_option('page_on_front'), PLL()->model->post->get_translations($shopID));
     $vars = array('pagename', 'page', 'name');
     foreach ($vars as $var) {
         if (isset($wp->query_vars[$var])) {
             $shopOnFront = false;
             break;
         }
     }
     if (!$shopOnFront) {
         if (!empty($wp->query_vars['pagename'])) {
             $shopPage = get_post($shopID);
             /* Explode by / for children page */
             $page = explode('/', $wp->query_vars['pagename']);
             if (isset($shopPage->post_name) && $shopPage->post_name == $page[count($page) - 1]) {
                 unset($wp->query_vars['page']);
                 unset($wp->query_vars['pagename']);
                 $wp->query_vars['post_type'] = 'product';
             }
         }
     } else {
         $wp->query_vars['post_type'] = 'product';
     }
 }
Ejemplo n.º 2
0
 /**
  * Registers the Polylang language meta box.
  *
  * @param  object $post WP_Post post object.
  * @param  array  $metabox Array of metabox arguments.
  *
  * @return void.
  */
 public static function polylangMetaBoxRender($post, $metabox)
 {
     $post_id = $post->ID;
     $post_type = $metabox['args']['post_type'];
     if ($lg = PLL()->model->post->get_language($post_id)) {
         $lang = $lg;
     } elseif (!empty($_GET['new_lang'])) {
         $lang = PLL()->model->get_language($_GET['new_lang']);
     } else {
         $lang = PLL()->pref_lang;
     }
     $text_domain = Plugin::TEXT_DOMAIN;
     $languages = PLL()->model->get_languages_list();
     $pll_dropdown = new PLL_Walker_Dropdown();
     $dropdown = $pll_dropdown->walk($languages, array('name' => 'post_lang_choice', 'class' => 'tags-input', 'selected' => $lang ? $lang->slug : '', 'flag' => true));
     $masterLink = '';
     foreach ($languages as $language) {
         $languagePost = PLL()->model->post->get_translation($post_id, $language);
         if (!$languagePost) {
             continue;
         }
         $languageMasterMeta = get_post_meta($languagePost, Events::TRAPP_META_MASTER, true);
         if ($languageMasterMeta) {
             $masterLink = sprintf('<a href="%s">%s</a>', get_edit_post_link($languagePost), $language->name);
             break;
         }
     }
     foreach ($languages as $key_language => $language) {
         if ($language->term_id == $lang->term_id) {
             unset($languages[$key_language]);
             $languages = array_values($languages);
             break;
         }
     }
     wp_nonce_field('pll_language', '_pll_nonce');
     // These shold really exist in some other methods.. whenever the structure has been very defined
     $is_autopost = get_post_status($post) == 'auto-draft';
     $is_master = get_post_meta($post->ID, Events::TRAPP_META_MASTER, true);
     $has_trapp_key = get_post_meta($post->ID, Events::TRAPP_META_KEY, true);
     $trapp_link_key = Events::TRAPP_META_LINK;
     if ($is_autopost) {
         include self::getView('admin/metabox-translations-post/language.php');
     } else {
         include self::getView('admin/metabox-translations-post/language-edit.php');
         if (!$is_master) {
             include self::getView('admin/metabox-translations-post/language-edit-translation.php');
         }
         include self::getView('admin/metabox-translations-post/translations.php');
         if ($is_master || !$has_trapp_key) {
             $deadline = get_post_meta($post->ID, Events::TRAPP_META_DEADLINE, true);
             if (empty($deadline)) {
                 $deadline = date('Y-m-d', current_time('timestamp'));
             }
             include self::getView('admin/metabox-translations-post/trapp.php');
         }
     }
 }
Ejemplo n.º 3
0
 /**
  * Get the translations IDS of the given product ID
  *
  * @global \Polylang $polylang
  *
  * @param integer $ID             the product ID
  * @param boolean $excludeDefault ture to exclude defualt language
  *
  * @return array associative array with language code as key and ID of translations
  *               as value.
  */
 public static function getProductTranslationsArrayByID($ID, $excludeDefault = false)
 {
     global $polylang;
     $IDS = PLL()->model->post->get_translations('post', $ID);
     // $IDS = $polylang->model->get_translations('post', $ID);
     if (true === $excludeDefault) {
         unset($IDS[pll_default_language()]);
     }
     return $IDS;
 }
Ejemplo n.º 4
0
 /**
  * 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;
 }
Ejemplo n.º 5
0
 /**
  * Splits the one item of backend in several items on frontend
  * take care to menu_order as it is used later in wp_nav_menu
  *
  * @since 1.1.1
  *
  * @param array $items menu items
  * @return array modified items
  */
 public function wp_get_nav_menu_items($items)
 {
     if (doing_action('customize_register')) {
         // needed since WP 4.3, doing_action available since WP 3.9
         return $items;
     }
     // The customizer menus does not sort the items and we need them to be sorted before splitting the language switcher
     usort($items, array($this, 'usort_menu_items'));
     $new_items = array();
     $offset = 0;
     foreach ($items as $key => $item) {
         if ($options = get_post_meta($item->ID, '_pll_menu_item', true)) {
             $i = 0;
             $switcher = new PLL_Switcher();
             $args = array_merge(array('raw' => 1), $options);
             $the_languages = $switcher->the_languages(PLL()->links, $args);
             // parent item for dropdown
             if (!empty($options['dropdown'])) {
                 $item->title = $options['show_flags'] && $options['show_names'] ? $this->curlang->flag . '&nbsp;' . esc_html($this->curlang->name) : ($options['show_flags'] ? $this->curlang->flag : esc_html($this->curlang->name));
                 $item->url = '';
                 $item->classes = array('pll-parent-menu-item');
                 $new_items[] = $item;
                 $offset++;
             }
             foreach ($the_languages as $lang) {
                 $lang_item = clone $item;
                 $lang_item->ID = $lang_item->ID . '-' . $lang['slug'];
                 // A unique ID
                 $lang_item->title = $options['show_flags'] && $options['show_names'] ? $lang['flag'] . '<span style="margin-left:0.3em;">' . esc_html($lang['name']) . '</span>' : ($options['show_flags'] ? $lang['flag'] : esc_html($lang['name']));
                 $lang_item->url = $lang['url'];
                 $lang_item->lang = $lang['locale'];
                 // Save this for use in nav_menu_link_attributes
                 $lang_item->classes = $lang['classes'];
                 $lang_item->menu_order += $offset + $i++;
                 if (!empty($options['dropdown'])) {
                     $lang_item->menu_item_parent = $item->db_id;
                     $lang_item->db_id = 0;
                     // to avoid recursion
                 }
                 $new_items[] = $lang_item;
             }
             $offset += $i - 1;
         } else {
             $item->menu_order += $offset;
             $new_items[] = $item;
         }
     }
     return $new_items;
 }
Ejemplo n.º 6
0
 /**
  * Defines two WPML constants once the language has been defined
  * The compatibility with WPML is not perfect on admin side as the constants are defined
  * in 'setup_theme' by Polylang ( based on user info ) and 'plugins_loaded' by WPML ( based on cookie )
  *
  * @since 0.9.5
  */
 public function define_constants()
 {
     if (!empty(PLL()->curlang)) {
         if (!defined('ICL_LANGUAGE_CODE')) {
             define('ICL_LANGUAGE_CODE', PLL()->curlang->slug);
         }
         if (!defined('ICL_LANGUAGE_NAME')) {
             define('ICL_LANGUAGE_NAME', PLL()->curlang->name);
         }
     } elseif (!PLL() instanceof PLL_Frontend) {
         if (!defined('ICL_LANGUAGE_CODE')) {
             define('ICL_LANGUAGE_CODE', 'all');
         }
         if (!defined('ICL_LANGUAGE_NAME')) {
             define('ICL_LANGUAGE_NAME', '');
         }
     }
 }
Ejemplo n.º 7
0
 /**
  * Finds the wpml-config.xml files to parse and setup filters
  *
  * @since 1.0
  */
 public function init()
 {
     $this->xmls = array();
     // Theme
     if (file_exists($file = ($template = get_template_directory()) . '/wpml-config.xml') && false !== ($xml = simplexml_load_file($file))) {
         $this->xmls[get_template()] = $xml;
     }
     // Child theme
     if (($stylesheet = get_stylesheet_directory()) !== $template && file_exists($file = $stylesheet . '/wpml-config.xml') && false !== ($xml = simplexml_load_file($file))) {
         $this->xmls[get_stylesheet()] = $xml;
     }
     // Plugins
     // Don't forget sitewide active plugins thanks to Reactorshop http://wordpress.org/support/topic/polylang-and-yoast-seo-plugin/page/2?replies=38#post-4801829
     $plugins = is_multisite() && ($sitewide_plugins = get_site_option('active_sitewide_plugins')) && is_array($sitewide_plugins) ? array_keys($sitewide_plugins) : array();
     $plugins = array_merge($plugins, get_option('active_plugins'));
     foreach ($plugins as $plugin) {
         if (file_exists($file = WP_PLUGIN_DIR . '/' . dirname($plugin) . '/wpml-config.xml') && false !== ($xml = simplexml_load_file($file))) {
             $this->xmls[dirname($plugin)] = $xml;
         }
     }
     // Custom
     if (file_exists($file = PLL_LOCAL_DIR . '/wpml-config.xml') && false !== ($xml = simplexml_load_file($file))) {
         $this->xmls['Polylang'] = $xml;
     }
     if (!empty($this->xmls)) {
         add_filter('pll_copy_post_metas', array($this, 'copy_post_metas'), 10, 2);
         add_filter('pll_get_post_types', array($this, 'translate_types'), 10, 2);
         add_filter('pll_get_taxonomies', array($this, 'translate_taxonomies'), 10, 2);
         foreach ($this->xmls as $context => $xml) {
             foreach ($xml->xpath('admin-texts/key') as $key) {
                 $attributes = $key->attributes();
                 $name = (string) $attributes['name'];
                 if (PLL() instanceof PLL_Frontend) {
                     $this->options[$name] = $key;
                     add_filter('option_' . $name, array($this, 'translate_strings'));
                 } else {
                     $this->register_string_recursive($context, get_option($name), $key);
                 }
             }
         }
     }
 }
Ejemplo n.º 8
0
 public function wp_get_nav_menu_items($items)
 {
     if (doing_action('customize_register')) {
         // needed since WP 4.3, doing_action available since WP 3.9
         return $items;
     }
     // the customizer menus does not sort the items and we need them to be sorted before splitting the language switcher
     usort($items, array($this, 'usort_menu_items'));
     $new_items = array();
     $offset = 0;
     foreach ($items as $key => $item) {
         if ($options = get_post_meta($item->ID, '_pll_menu_item', true)) {
             $i = 0;
             $switcher = new PLL_Switcher();
             $args = array_merge(array('raw' => 1), $options);
             $the_languages = $switcher->the_languages(PLL()->links, $args);
             foreach ($the_languages as $lang) {
                 $lang_item = clone $item;
                 $lang_item->ID = $lang_item->ID . '-' . $lang['slug'];
                 // a unique ID
                 $lang_item->title = $options['show_flags'] && $options['show_names'] ? $lang['flag'] . '&nbsp;' . esc_html($lang['name']) : ($options['show_flags'] ? $lang['flag'] : esc_html($lang['name']));
                 $lang_item->url = $lang['url'];
                 $lang_item->lang = $lang['locale'];
                 // save this for use in nav_menu_link_attributes
                 $lang_item->classes = $lang['classes'];
                 $lang_item->menu_order += $offset + $i++;
                 $new_items[] = $lang_item;
             }
             $offset += $i - 1;
         } else {
             $item->menu_order += $offset;
             $new_items[] = $item;
         }
     }
     return $new_items;
 }
Ejemplo n.º 9
0
 /**
  * Get the current language by looking at the current HTTP_HOST
  *
  * @return null|PLL_Language
  */
 public function get_current_language()
 {
     if ($this->languages_is_enabled()) {
         return PLL()->model->get_language(pll_current_language());
     }
     return null;
 }
Ejemplo n.º 10
0
 /**
  * Jetpack
  * Adds opengraph support for locale and translations
  *
  * @since 1.6
  *
  * @param array $tags opengraph tags to output
  * @return array
  */
 public function jetpack_ogp($tags)
 {
     if (did_action('pll_init')) {
         foreach (PLL()->model->get_languages_list() as $language) {
             if ($language->slug != PLL()->curlang->slug && PLL()->links->get_translation_url($language) && ($fb_locale = self::get_fb_locale($language))) {
                 $tags['og:locale:alternate'][] = $fb_locale;
             }
             if ($language->slug == PLL()->curlang->slug && ($fb_locale = self::get_fb_locale($language))) {
                 $tags['og:locale'] = $fb_locale;
             }
         }
     }
     return $tags;
 }
 /**
  * Filter the data for inline (Quick and Bulk) editing
  *
  * Adds item-specific translations data for the JS quick and bulk edit functions.
  *
  * @since 2.11
  *
  * @param	string	$inline_data	The HTML markup for inline data.
  * @param	object	$item			The current Media Library item.
  *
  * @return	string	updated HTML markup for inline data.
  */
 public static function mla_list_table_build_inline_data($inline_data, $item)
 {
     global $polylang;
     $item_id = $item->ID;
     if (self::$polylang_1dot8_plus) {
         $old_lang = PLL()->model->post->get_language($item_id);
         $translations = PLL()->model->post->get_translations($item_id);
     } else {
         $old_lang = $polylang->model->get_post_language($item_id);
         $translations = $polylang->model->get_translations('post', $item_id);
     }
     if (isset($old_lang->slug)) {
         $old_lang = $old_lang->slug;
         $translations[$old_lang] = $item_id;
     } else {
         $old_lang = '';
     }
     $translations = json_encode($translations);
     $inline_data .= "\n\t<div class=\"lang\">{$old_lang}</div>";
     $inline_data .= "\n\t<div class=\"old_lang\">{$old_lang}</div>";
     $inline_data .= "\n\t<div class=\"inline_lang_choice\">{$old_lang}</div>";
     $inline_data .= "\n\t<div class=\"inline_translations\">{$translations}</div>";
     return $inline_data;
 }
Ejemplo n.º 12
0
 /**
  * Updates an exiting Trapp entry with a new revision.
  *
  * @return void.
  */
 public function updateTrappRevision()
 {
     $service = new ServiceTranslation();
     $service = $service->getById($this->trappId);
     if (!empty($_POST['trapp_deadline'])) {
         $deadline = esc_attr($_POST['trapp_deadline']);
         update_post_meta($this->postId, self::TRAPP_META_DEADLINE, $deadline);
         $deadline = new DateTime($deadline);
         $service->setDeadline($deadline);
     }
     if (!empty($_POST['trapp_comment'])) {
         $service->setComment(esc_attr($_POST['trapp_comment']));
     }
     if (isset($_POST['trapp_start'])) {
         $service->setState('state-missing');
     }
     $newFields = [];
     $arrayIgnore = [];
     $serviceFields = $service->getFields();
     $fieldGroups = Mappings::getFields(get_post_type($this->post));
     foreach ($fieldGroups as $fieldGroup) {
         foreach ($fieldGroup['fields'] as $field) {
             $field['group'] = $fieldGroup['title'];
             foreach ($serviceFields as $fieldId => $serviceField) {
                 if ($field['label'] == $serviceField->getLabel()) {
                     $value = Mappings::getValue($field['type'], $this->postId, $this->post, $field['args']);
                     if (!empty($value)) {
                         $serviceFields[$fieldId]->setValue($value);
                     }
                     continue 2;
                 } elseif ($field['type'] == 'post_meta_array') {
                     $value = Mappings::getValue($field['type'], $this->postId, $this->post, $field['args']);
                     $label = $serviceField->getLabel();
                     if (is_array($value) && array_key_exists($label, $value)) {
                         $serviceFields[$fieldId]->setValue($value[$label]);
                         $arrayIgnore[] = $label;
                     }
                 }
             }
             $newFields[] = $field;
         }
     }
     $service->setFields($serviceFields);
     if (!empty($newFields)) {
         foreach ($newFields as $newField) {
             $field = Mappings::translationField($newField, $this->postId, $this->post, $arrayIgnore);
             if (!empty($field)) {
                 if (is_array($field)) {
                     foreach ($field as $singleField) {
                         $service->addField($singleField);
                     }
                 } else {
                     $service->addField($field);
                 }
             }
         }
     }
     $post_translations = [];
     foreach ($service->getRelatedTranslations() as $serviceTranslation) {
         if ($serviceTranslation->isOriginal()) {
             continue;
         }
         $post_translations[] = $serviceTranslation->getLocale();
     }
     if (!empty($_POST['trapp_tr_lang'])) {
         foreach ($_POST['trapp_tr_lang'] as $trapp_lang => $active) {
             $trapp_lang = esc_attr($trapp_lang);
             $trapp_lang = PLL()->model->get_language($trapp_lang);
             if (!$trapp_lang) {
                 continue;
             }
             $locale = $this->filterLocale($trapp_lang->locale);
             if (in_array($locale, $post_translations)) {
                 continue;
             }
             $service->addTranslatation($locale);
         }
     }
     $row = $service->update();
     do_action('bp_trapp_after_save_post', $row, $this->post);
 }
Ejemplo n.º 13
0
                    <td class="pll-language-column"><?php 
            printf('
                        <input type="hidden" name="post_tr_lang[%1$s]" id="htr_lang_%1$s" value="1"/>
                        <input type="checkbox" name="trapp_tr_lang[%1$s]" id="htrapp_lang_%1$s" value="1" %2$s %3$s />', esc_attr($language->slug), checked(!empty($value), true, false), disabled(!empty($value), true, false));
            ?>
                    </td>
                <?php 
        }
        ?>

                    <?php 
        if ($value) {
            ?>

                        <td class="pll-edit-column"><?php 
            echo PLL()->links->edit_post_translation_link($value);
            ?>
</td>

                    <?php 
            if ($trapp_uri = get_post_meta($value, $trapp_link_key, true)) {
                ?>
                        <td class="pll-language-column"><?php 
                printf('<a class="button" href="%1$s">%2$s</a>', esc_url($trapp_uri), __('Edit in TRAPP', $text_domain));
                ?>
</td>
                    <?php 
            }
            ?>

                    <?php 
Ejemplo n.º 14
0
 /**
  * Find out if a specific language is enabled for the site
  *
  * @since 2.0
  *
  * @param mixed  $null not used
  * @param string $slug language code
  * @return bool
  */
 public function wpml_language_is_active($null, $slug)
 {
     $language = PLL()->model->get_language($slug);
     return empty($language->active) || true === $language->active;
 }
Ejemplo n.º 15
0
 /**
  * Get the translations IDS of the given term ID.
  *
  * @global \Polylang $polylang
  *
  * @param int  $ID             term id
  * @param bool $excludeDefault true to exclude default language
  *
  * @return array associative array with language code as key and ID of translations
  *               as value
  */
 public static function getTermTranslationsArrayByID($ID, $excludeDefault = false)
 {
     global $polylang;
     $IDS = PLL()->model->term->get_translations($ID);
     if (true === $excludeDefault) {
         unset($IDS[pll_default_language()]);
     }
     return $IDS;
 }
 /**
  * Fix "get_term_link" for this taxonomy.
  */
 public function term_link_filter($termlink, $term, $taxonomy)
 {
     // Check if the post type is handle.
     if (isset($this->taxonomies[$taxonomy])) {
         global $wp_rewrite, $polylang;
         if (!is_object($term)) {
             if (is_int($term)) {
                 $term = get_term($term, $taxonomy);
             } else {
                 $term = get_term_by('slug', $term, $taxonomy);
             }
         }
         if (!is_object($term)) {
             $term = new WP_Error('invalid_term', __('Empty Term'));
         }
         if (is_wp_error($term)) {
             return $term;
         }
         // Get the term language.
         $term_language = PLL()->model->term->get_language($term->term_id);
         if ($term_language) {
             $lang = $term_language->slug;
         } else {
             $lang = pll_default_language();
         }
         // Check if the language is handle.
         if (isset($this->taxonomies[$taxonomy]->translated_slugs[$lang])) {
             $taxonomy = $term->taxonomy;
             $termlink = $this->taxonomies[$taxonomy]->translated_struct[$lang];
             $slug = $term->slug;
             $t = get_taxonomy($taxonomy);
             if (empty($termlink)) {
                 if ('category' == $taxonomy) {
                     $termlink = '?cat=' . $term->term_id;
                 } elseif ($t->query_var) {
                     $termlink = "?{$t->query_var}={$slug}";
                 } else {
                     $termlink = "?taxonomy={$taxonomy}&term={$slug}";
                 }
                 $termlink = home_url($termlink);
             } else {
                 if ($t->rewrite['hierarchical']) {
                     $hierarchical_slugs = array();
                     $ancestors = get_ancestors($term->term_id, $taxonomy);
                     foreach ((array) $ancestors as $ancestor) {
                         $ancestor_term = get_term($ancestor, $taxonomy);
                         $hierarchical_slugs[] = $ancestor_term->slug;
                     }
                     $hierarchical_slugs = array_reverse($hierarchical_slugs);
                     $hierarchical_slugs[] = $slug;
                     $termlink = str_replace("%{$taxonomy}%", implode('/', $hierarchical_slugs), $termlink);
                 } else {
                     $termlink = str_replace("%{$taxonomy}%", $slug, $termlink);
                 }
                 $termlink = home_url(user_trailingslashit($termlink, 'category'));
             }
             // Back Compat filters.
             if ('post_tag' == $taxonomy) {
                 $termlink = apply_filters('tag_link', $termlink, $term->term_id);
             } elseif ('category' == $taxonomy) {
                 $termlink = apply_filters('category_link', $termlink, $term->term_id);
             }
         }
     }
     return $termlink;
 }
Ejemplo n.º 17
0
 /**
  * Unlink orginal product translations from the new copy
  *
  * @global \Polylang $polylang
  *
  * @param integer $ID the new product ID
  */
 public function unlinkOrginalProductTranslations($ID)
 {
     global $polylang;
     PLL()->model->post->delete_translation($ID);
     // $polylang->model-> deprecated
 }
Ejemplo n.º 18
0
 function wpml_get_language_information($post_id = null)
 {
     if (empty($post_id)) {
         $post_id = get_the_ID();
     }
     // FIXME WPML may return a WP_Error object
     return false === ($lang = PLL()->model->post->get_language($post_id) ? array() : array('locale' => $lang->locale, 'text_direction' => $lang->is_rtl, 'display_name' => $lang->name, 'native_name' => $lang->name, 'different_language' => $lang->slug != pll_current_language()));
 }
Ejemplo n.º 19
0
/**
 * Fetch the polylang where clause.
 *
 * @since 0.2.0
 *
 * @param  string $lang The current language slug.
 *
 * @return string
 */
function polylang_slug_model_post_where_clause($lang = '')
{
    if (function_exists('PLL')) {
        return PLL()->model->post->where_clause($lang);
    } elseif (array_key_exists('polylang', $GLOBALS)) {
        global $polylang;
        return $polylang->model->where_clause($lang, 'post');
    } else {
        return;
    }
}
Ejemplo n.º 20
0
 /**
  * Filter stock by langauge
  *
  * Filter the stock table according to choosen langauge
  *
  * @global \Polylang $polylang
  * @param string $query stock query
  *
  * @return string final stock query
  */
 public function filterStockByLangauge($query)
 {
     global $polylang;
     $lang = ($current = pll_current_language()) ? array($current) : pll_languages_list();
     $join = PLL()->model->post->join_clause();
     // $polylang->model-> deprecated
     $where = PLL()->model->post->where_clause($lang);
     // $polylang->model-> deprecated
     return str_replace('WHERE 1=1', "{$join} WHERE 1=1 {$where}", $query);
 }
Ejemplo n.º 21
0
 /**
  * Filter stock by language.
  *
  * Filter the stock table according to choosen language
  *
  * @param string $query stock query
  *
  * @return string final stock query
  */
 public function filterStockByLanguage($query)
 {
     $lang = ($current = pll_current_language()) ? array($current) : pll_languages_list();
     $join = PLL()->model->post->join_clause(Utilities::polylangVersionCheck('2.0') ? 'posts' : 'post');
     $where = PLL()->model->post->where_clause($lang, 'post');
     return str_replace('WHERE 1=1', "{$join} WHERE 1=1 {$where}", $query);
 }
Ejemplo n.º 22
0
 /**
  * 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;
 }
Ejemplo n.º 23
0
    /**
     * modified version of WP get_calendar function to filter the query
     *
     * @since 0.5
     *
     * @param bool $initial Optional, default is true. Use initial calendar names.
     * @param bool $echo Optional, default is true. Set to false for return.
     * @return string|null String when retrieving, null when displaying.
     */
    static function get_calendar($initial = true, $echo = true)
    {
        global $wpdb, $m, $monthnum, $year, $wp_locale, $posts;
        $join_clause = PLL()->model->post->join_clause();
        #added#
        $where_clause = PLL()->model->post->where_clause(PLL()->curlang);
        #added#
        $key = md5(PLL()->curlang->slug . $m . $monthnum . $year);
        #modified#
        $cache = wp_cache_get('get_calendar', 'calendar');
        if ($cache && is_array($cache) && isset($cache[$key])) {
            /** This filter is documented in wp-includes/general-template.php */
            $output = apply_filters('get_calendar', $cache[$key]);
            if ($echo) {
                echo $output;
                return;
            }
            return $output;
        }
        if (!is_array($cache)) {
            $cache = array();
        }
        // Quick check. If we have no posts at all, abort!
        if (!$posts) {
            $gotsome = $wpdb->get_var("SELECT 1 as test FROM {$wpdb->posts} WHERE post_type = 'post' AND post_status = 'publish' LIMIT 1");
            if (!$gotsome) {
                $cache[$key] = '';
                wp_cache_set('get_calendar', $cache, 'calendar');
                return;
            }
        }
        if (isset($_GET['w'])) {
            $w = (int) $_GET['w'];
        }
        // week_begins = 0 stands for Sunday
        $week_begins = (int) get_option('start_of_week');
        $ts = current_time('timestamp');
        // Let's figure out when we are
        if (!empty($monthnum) && !empty($year)) {
            $thismonth = zeroise(intval($monthnum), 2);
            $thisyear = (int) $year;
        } elseif (!empty($w)) {
            // We need to get the month from MySQL
            $thisyear = (int) substr($m, 0, 4);
            //it seems MySQL's weeks disagree with PHP's
            $d = ($w - 1) * 7 + 6;
            $thismonth = $wpdb->get_var("SELECT DATE_FORMAT((DATE_ADD('{$thisyear}0101', INTERVAL {$d} DAY) ), '%m')");
        } elseif (!empty($m)) {
            $thisyear = (int) substr($m, 0, 4);
            if (strlen($m) < 6) {
                $thismonth = '01';
            } else {
                $thismonth = zeroise((int) substr($m, 4, 2), 2);
            }
        } else {
            $thisyear = gmdate('Y', $ts);
            $thismonth = gmdate('m', $ts);
        }
        $unixmonth = mktime(0, 0, 0, $thismonth, 1, $thisyear);
        $last_day = date('t', $unixmonth);
        // Get the next and previous month and year with at least one post
        $previous = $wpdb->get_row("SELECT MONTH( post_date ) AS month, YEAR( post_date ) AS year\n\t\t\tFROM {$wpdb->posts} {$join_clause}\n\t\t\tWHERE post_date < '{$thisyear}-{$thismonth}-01'\n\t\t\tAND post_type = 'post' AND post_status = 'publish' {$where_clause}\n\t\t\t\tORDER BY post_date DESC\n\t\t\t\tLIMIT 1");
        #modified#
        $next = $wpdb->get_row("SELECT MONTH( post_date ) AS month, YEAR( post_date ) AS year\n\t\t\tFROM {$wpdb->posts} {$join_clause}\n\t\t\tWHERE post_date > '{$thisyear}-{$thismonth}-{$last_day} 23:59:59'\n\t\t\tAND post_type = 'post' AND post_status = 'publish' {$where_clause}\n\t\t\t\tORDER BY post_date ASC\n\t\t\t\tLIMIT 1");
        #modified#
        /* translators: Calendar caption: 1: month name, 2: 4-digit year */
        $calendar_caption = _x('%1$s %2$s', 'calendar caption');
        $calendar_output = '<table id="wp-calendar">
		<caption>' . sprintf($calendar_caption, $wp_locale->get_month($thismonth), date('Y', $unixmonth)) . '</caption>
		<thead>
		<tr>';
        $myweek = array();
        for ($wdcount = 0; $wdcount <= 6; $wdcount++) {
            $myweek[] = $wp_locale->get_weekday(($wdcount + $week_begins) % 7);
        }
        foreach ($myweek as $wd) {
            $day_name = $initial ? $wp_locale->get_weekday_initial($wd) : $wp_locale->get_weekday_abbrev($wd);
            $wd = esc_attr($wd);
            $calendar_output .= "\n\t\t<th scope=\"col\" title=\"{$wd}\">{$day_name}</th>";
        }
        $calendar_output .= '
		</tr>
		</thead>

		<tfoot>
		<tr>';
        if ($previous) {
            $calendar_output .= "\n\t\t" . '<td colspan="3" id="prev"><a href="' . get_month_link($previous->year, $previous->month) . '">&laquo; ' . $wp_locale->get_month_abbrev($wp_locale->get_month($previous->month)) . '</a></td>';
        } else {
            $calendar_output .= "\n\t\t" . '<td colspan="3" id="prev" class="pad">&nbsp;</td>';
        }
        $calendar_output .= "\n\t\t" . '<td class="pad">&nbsp;</td>';
        if ($next) {
            $calendar_output .= "\n\t\t" . '<td colspan="3" id="next"><a href="' . get_month_link($next->year, $next->month) . '">' . $wp_locale->get_month_abbrev($wp_locale->get_month($next->month)) . ' &raquo;</a></td>';
        } else {
            $calendar_output .= "\n\t\t" . '<td colspan="3" id="next" class="pad">&nbsp;</td>';
        }
        $calendar_output .= '
		</tr>
		</tfoot>

		<tbody>
		<tr>';
        $daywithpost = array();
        // Get days with posts
        $dayswithposts = $wpdb->get_results("SELECT DISTINCT DAYOFMONTH( post_date )\n\t\t\tFROM {$wpdb->posts} {$join_clause}\n\t\t\tWHERE post_date >= '{$thisyear}-{$thismonth}-01 00:00:00'\n\t\t\tAND post_type = 'post' AND post_status = 'publish' {$where_clause}\n\t\t\tAND post_date <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59'", ARRAY_N);
        #modified#
        if ($dayswithposts) {
            foreach ((array) $dayswithposts as $daywith) {
                $daywithpost[] = $daywith[0];
            }
        }
        // See how much we should pad in the beginning
        $pad = calendar_week_mod(date('w', $unixmonth) - $week_begins);
        if (0 != $pad) {
            $calendar_output .= "\n\t\t" . '<td colspan="' . esc_attr($pad) . '" class="pad">&nbsp;</td>';
        }
        $newrow = false;
        $daysinmonth = (int) date('t', $unixmonth);
        for ($day = 1; $day <= $daysinmonth; ++$day) {
            if (isset($newrow) && $newrow) {
                $calendar_output .= "\n\t</tr>\n\t<tr>\n\t\t";
            }
            $newrow = false;
            if ($day == gmdate('j', $ts) && $thismonth == gmdate('m', $ts) && $thisyear == gmdate('Y', $ts)) {
                $calendar_output .= '<td id="today">';
            } else {
                $calendar_output .= '<td>';
            }
            if (in_array($day, $daywithpost)) {
                // any posts today?
                $date_format = date(_x('F j, Y', 'daily archives date format'), strtotime("{$thisyear}-{$thismonth}-{$day}"));
                $label = sprintf(__('Posts published on %s'), $date_format);
                $calendar_output .= sprintf('<a href="%s" aria-label="%s">%s</a>', get_day_link($thisyear, $thismonth, $day), esc_attr($label), $day);
            } else {
                $calendar_output .= $day;
            }
            $calendar_output .= '</td>';
            if (6 == calendar_week_mod(date('w', mktime(0, 0, 0, $thismonth, $day, $thisyear)) - $week_begins)) {
                $newrow = true;
            }
        }
        $pad = 7 - calendar_week_mod(date('w', mktime(0, 0, 0, $thismonth, $day, $thisyear)) - $week_begins);
        if ($pad != 0 && $pad != 7) {
            $calendar_output .= "\n\t\t" . '<td class="pad" colspan="' . esc_attr($pad) . '">&nbsp;</td>';
        }
        $calendar_output .= "\n\t</tr>\n\t</tbody>\n\t</table>";
        $cache[$key] = $calendar_output;
        wp_cache_set('get_calendar', $cache, 'calendar');
        if ($echo) {
            /**
             * Filter the HTML calendar output.
             *
             * @since 3.0.0
             *
             * @param string $calendar_output HTML output of the calendar.
             */
            echo apply_filters('get_calendar', $calendar_output);
            return;
        }
        /** This filter is documented in wp-includes/general-template.php */
        return apply_filters('get_calendar', $calendar_output);
    }
Ejemplo n.º 24
0
function pll_count_posts($lang, $args = array())
{
    return PLL()->model->count_posts(PLL()->model->get_language($lang), $args);
}
Ejemplo n.º 25
0
 /**
  * remaps terms languages
  *
  * @since 1.2
  *
  * @param array $terms array of terms in 'term_translations' taxonomy
  */
 function remap_terms_relations(&$terms)
 {
     global $wpdb;
     foreach ($terms as $term) {
         $translations = unserialize($term['term_description']);
         foreach ($translations as $slug => $old_id) {
             if ($old_id && !empty($this->processed_terms[$old_id]) && ($lang = PLL()->model->get_language($slug))) {
                 // language relationship
                 $trs[] = $wpdb->prepare('( %d, %d )', $this->processed_terms[$old_id], $lang->tl_term_taxonomy_id);
                 // translation relationship
                 $trs[] = $wpdb->prepare('( %d, %d )', $this->processed_terms[$old_id], get_term($this->processed_terms[$term['term_id']], 'term_translations')->term_taxonomy_id);
             }
         }
     }
     // insert term_relationships
     if (!empty($trs)) {
         $trs = array_unique($trs);
         // make sure we don't attempt to insert already existing term relationships
         $existing_trs = $wpdb->get_results("\n\t\t\t\tSELECT tr.object_id, tr.term_taxonomy_id FROM {$wpdb->term_relationships} AS tr\n\t\t\t\tINNER JOIN {$wpdb->term_taxonomy} AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id\n\t\t\t\tWHERE tt.taxonomy IN ( 'term_language', 'term_translations' )\n\t\t\t");
         foreach ($existing_trs as $key => $tr) {
             $existing_trs[$key] = $wpdb->prepare('( %d, %d )', $tr->object_id, $tr->term_taxonomy_id);
         }
         $trs = array_diff($trs, $existing_trs);
         if (!empty($trs)) {
             $wpdb->query("INSERT INTO {$wpdb->term_relationships} ( object_id, term_taxonomy_id ) VALUES " . implode(',', $trs));
         }
     }
 }
Ejemplo n.º 26
0
 /**
  * Correct the locale for orders emails , Othe emails must be handled
  * correctly out of the box
  *
  * @global \Polylang $polylang
  * @global \WooCommerce $woocommerce
  *
  * @param string $locale current locale
  *
  * @return string locale
  */
 public function correctLocale($locale)
 {
     global $polylang, $woocommerce;
     if (!$polylang || !$woocommerce) {
         return $locale;
     }
     $refer = isset($_GET['action']) && esc_attr($_GET['action'] === 'woocommerce_mark_order_status');
     // Should use sanitize_text_field() instead of esc_attr?
     /* ******add-on to have multilanguage on note and refund mails ********* */
     if (isset($_POST['note_type']) && $_POST['note_type'] == 'customer') {
         $refer = true;
     }
     if (isset($_POST['refund_amount']) && $_POST['refund_amount'] > 0) {
         $refer = true;
     }
     /* ******add-on to have multilanguage on note and refund mails ********* */
     if (!is_admin() && !isset($_REQUEST['ipn_track_id']) || defined('DOING_AJAX') && !$refer) {
         return $locale;
     }
     if ('GET' === filter_input(INPUT_SERVER, 'REQUEST_METHOD') && !$refer) {
         return $locale;
     }
     $ID = false;
     if (!isset($_REQUEST['ipn_track_id'])) {
         $search = array('post', 'post_ID', 'pll_post_id', 'order_id');
         foreach ($search as $value) {
             if (isset($_REQUEST[$value])) {
                 $ID = esc_attr($_REQUEST[$value]);
                 break;
             }
         }
     } else {
         $ID = $this->getOrderIDFromIPNRequest();
     }
     if (get_post_type($ID) !== 'shop_order' && !$refer) {
         return $locale;
     }
     $orderLanguage = Order::getOrderLangauge($ID);
     if ($orderLanguage) {
         $entity = Utilities::getLanguageEntity($orderLanguage);
         if ($entity) {
             $polylang->curlang = PLL()->model->get_language($entity->locale);
             $GLOBALS['text_direction'] = $entity->is_rtl ? 'rtl' : 'ltr';
             if (class_exists('WP_Locale')) {
                 $GLOBALS['wp_locale'] = new \WP_Locale();
             }
             return $entity->locale;
         }
     }
     return $locale;
 }
Ejemplo n.º 27
0
 /**
  * Fix My Account Link In Menus
  *
  * The method will remove endpoints from my account page link in wp menus
  *
  * @global \Polylang $polylang
  *
  * @param array $items menu items
  *
  * @return array menu items
  *
  * @todo Find a better solution
  */
 public function fixMyAccountLinkInMenus(array $items = array())
 {
     global $polylang;
     $translations = PLL()->model->post->get_translations(wc_get_page_id('myaccount'));
     foreach ($items as $item) {
         if (in_array($item->object_id, $translations)) {
             $vars = WC()->query->get_query_vars();
             foreach ($vars as $key => $value) {
                 if (false !== ($pos = strpos($item->url, $value))) {
                     $item->url = substr($item->url, 0, $pos);
                 }
             }
         }
     }
     return $items;
 }