/** * Returns the number of posts for a given post_type, date and language combination that is published. * * @param string $lang language code to check * @param null|int $year * @param null|int $month * @param null|int $day * @param string|array $post_type * * @return bool * * @used-by \WPML_Languages::add_date_or_cpt_url_to_ls_lang to determine what languages to show in the Language Switcher */ public function archive_query_has_posts($lang, $year = null, $month = null, $day = null, $post_type = 'post') { $cache_args = array(); $cache_args['lang'] = $lang; $cache_args['year'] = $year; $cache_args['month'] = $month; $cache_args['day'] = $day; $cache_args['post_type'] = $post_type; $cache_key = md5(json_encode($cache_args)); $cache_group = 'archive_query_has_posts'; $cache = new WPML_WP_Cache($cache_group); $found = false; $result = $cache->get($cache_key, $found); if (!$found) { $post_status_snippet = $this->wp_api->current_user_can('read') ? "p.post_status IN (" . wpml_prepare_in(array('publish', 'private')) . ") " : "p.post_status = 'publish'"; $post_type_snippet = is_array($post_type) ? " AND post_type IN (" . wpml_prepare_in($post_type) . ") " : $this->wpdb->prepare(' AND p.post_type = %s ', $post_type); $year_snippet = (bool) $year === true ? $this->wpdb->prepare(' AND year(p.post_date) = %d ', $year) : ''; $month_snippet = (bool) $month === true ? $this->wpdb->prepare(' AND month(p.post_date) = %d ', $month) : ''; $day_snippet = (bool) $day === true ? $this->wpdb->prepare(' AND day(p.post_date) = %d ', $day) : ''; $lang_snippet = $this->wpdb->prepare(' AND t.language_code = %s ', $lang); $result = $this->wpdb->get_var("\n SELECT p.ID FROM {$this->wpdb->posts} p\n\t\t\t\t\t\tJOIN {$this->wpdb->prefix}icl_translations t\n\t\t\t\t\t\t\tON p.ID = t.element_id AND t.element_type = CONCAT('post_', p.post_type)\n\t\t\t\t\t\tWHERE " . $post_status_snippet . $year_snippet . $month_snippet . $day_snippet . $post_type_snippet . $lang_snippet . "\n\t\t\t\t\t\tLIMIT 1"); $cache->set($cache_key, $result); } return (bool) $result; }
public function action_handler() { $current_action = current_filter(); foreach ($this->valid_caches as $cache_name => $clear_actions) { foreach ($clear_actions['clear_actions'] as $clear_action) { if ($current_action == $clear_action) { $cache = new WPML_WP_Cache($cache_name); $cache->flush_group_cache(); } } } }
/** * @return WPML_TM_ICL_Translations[] */ public function translations() { if ((bool) $this->related === false) { $trid = $this->trid(); $found = false; $cache = new WPML_WP_Cache('WPML_TM_ICL_Translations::translations'); $this->related = $cache->get($trid, $found); if (!$found) { $translation_ids = $this->wpdb->get_results("SELECT translation_id, language_code\n\t\t\t\t FROM {$this->wpdb->prefix}{$this->table}\n\t\t\t\t WHERE trid = " . $trid); foreach ($translation_ids as $row) { $this->related[$row->language_code] = $this->tm_records->icl_translations_by_translation_id($row->translation_id); } $cache->set($trid, $this->related); } } return $this->related; }
public function get($type) { $cache_key = $type; $cache_group = 'wpml_pre_option_page'; $cache_found = false; $cache = new WPML_WP_Cache($cache_group); $results = $cache->get($cache_key, $cache_found); if ((!$cache_found || !isset($results[$type])) && !$this->switched || $this->switched && $this->sitepress->get_setting('setup_complete')) { $results[$type] = array(); // Fetch for all languages and cache them. $values = $this->wpdb->get_results($this->wpdb->prepare("\tSELECT element_id, language_code\n\t\t\t\t\t\tFROM {$this->wpdb->prefix}icl_translations\n\t\t\t\t\t\tWHERE trid =\n\t\t\t\t\t\t\t(SELECT trid\n\t\t\t\t\t\t\t FROM {$this->wpdb->prefix}icl_translations\n\t\t\t\t\t\t\t WHERE element_type = 'post_page'\n\t\t\t\t\t\t\t AND element_id = (SELECT option_value\n\t\t\t\t\t\t\t\t\t\t\t FROM {$this->wpdb->options}\n\t\t\t\t\t\t\t\t\t\t\t WHERE option_name=%s\n\t\t\t\t\t\t\t\t\t\t\t LIMIT 1))\n\t\t\t\t\t\t", $type)); foreach ($values as $lang_result) { $results[$type][$lang_result->language_code] = $lang_result->element_id; } if ($results) { $cache->set($cache_key, $results); } } return isset($results[$type][$this->lang]) ? $results[$type][$this->lang] : ''; }
/** * SitePress Template functions * @package wpml-core */ function wpml_site_uses_icl() { global $wpdb; $setting = 'site_does_not_use_icl'; if (icl_get_setting($setting, false)) { return false; } $cache = new WPML_WP_Cache('wpml-checks'); $found = false; $site_uses_icl = $cache->get('site_uses_icl', $found); if (!$found) { $site_uses_icl = false; $table_exists = $wpdb->get_var("SHOW TABLES LIKE '{$wpdb->prefix}icl_translation_status'"); if ($table_exists) { $icl_job_count_query = "SELECT rid\n\t\t\t\t\t\t\tFROM {$wpdb->prefix}icl_translation_status\n\t\t\t\t\t\t\tWHERE translation_service = 'icanlocalize'\n\t\t\t\t\t\t\tLIMIT 1;"; $site_uses_icl = (bool) $wpdb->get_var($icl_job_count_query); } $cache->set('site_uses_icl', $site_uses_icl); } if (icl_get_setting('setup_complete', false) && !$site_uses_icl) { icl_set_setting($setting, true, true); } return $site_uses_icl; }
public function get_locale($code) { if (!$code) { return false; } $found = false; $cache_key = 'get_locale' . $code; $cache = new WPML_WP_Cache(''); $locale = $cache->get($cache_key, $found); if ($found) { return $locale; } $all_locales_data = $this->wpdb->get_results("SELECT code, locale FROM {$this->wpdb->prefix}icl_locale_map"); /** @var array $all_locales_data */ foreach ($all_locales_data as $locales_data) { $all_locales[$locales_data->code] = $locales_data->locale; } $locale = isset($all_locales[$code]) ? $all_locales[$code] : false; if (false === $locale) { $this_locale_data_query = "SELECT code, default_locale FROM {$this->wpdb->prefix}icl_languages WHERE code = %s"; $this_locale_data_prepare = $this->wpdb->prepare($this_locale_data_query, $code); $this_locale_data = $this->wpdb->get_row($this_locale_data_prepare); if ($this_locale_data) { $locale = $this_locale_data->default_locale; } } $cache->set($cache_key, $locale); return $locale; }
/** * Filters the permalink pointing at a taxonomy archive to correctly reflect the language of its underlying term * * @param string $permalink url pointing at a term's archive * @param Object|int $tag term object or term_id of the term * @param string $taxonomy the term's taxonomy * * @return string */ public function tax_permalink_filter($permalink, $tag, $taxonomy) { /** @var WPML_Term_Translation $wpml_term_translations */ global $wpml_term_translations; $tag = is_object($tag) ? $tag : get_term($tag, $taxonomy); $tag_id = $tag ? $tag->term_taxonomy_id : 0; $cached_permalink_key = $tag_id . '.' . $taxonomy; $cache_group = 'icl_tax_permalink_filter'; $found = false; $cache = new WPML_WP_Cache($cache_group); $cached_permalink = $cache->get($cached_permalink_key, $found); if ($found === true) { return $cached_permalink; } $term_language = $tag_id ? $wpml_term_translations->get_element_lang_code($tag_id) : false; $permalink = (bool) $term_language === true ? $this->convert_url($permalink, $term_language) : $permalink; $cache->set($cached_permalink_key, $permalink); return $permalink; }
/** * @param string $lang_code */ public function clear_string_filter($lang_code) { unset($this->string_filters[$lang_code]); $display_cache = new WPML_WP_Cache('wpml_display_filter'); $display_cache->flush_group_cache(); }
public function flush_cache() { $this->languages_details = null; $this->element_translations = null; $this->wpml_cache->flush_group_cache(); }
private function clear_ls_languages_cache() { $cache = new WPML_WP_Cache('ls_languages'); $cache->flush_group_cache(); }
function add_posts_management_column($columns) { $new_columns = $columns; global $posts; if (count($this->get_active_languages()) <= 1 || get_query_var('post_status') == 'trash') { return $columns; } if (isset($_POST['action']) && $_POST['action'] == 'inline-save' && $_POST['post_ID']) { $p = new stdClass(); $p->ID = $_POST['post_ID']; $posts = array($p); } elseif (empty($posts)) { return $columns; } if (is_null($this->post_status_display)) { $post_ids = array(); foreach ($posts as $p) { $post_ids[] = $p->ID; } $this->post_status_display = new WPML_Post_Status_Display($this->get_active_languages()); } $active_languages = $this->get_active_languages(); $active_languages = apply_filters('wpml_active_languages_access', $active_languages, array('action' => 'edit')); $languages = array(); foreach ($active_languages as $v) { if ($v['code'] == $this->get_current_language()) { continue; } $languages[] = $v['code']; } if (count($languages) > 0) { $flags_cache = new WPML_WP_Cache('add_posts_management_column'); $key = md5(serialize($languages) . $this->admin_language); $found = false; $flags_column = $flags_cache->get($key, $found); if (!$found) { $res = $this->wpdb->get_results($this->wpdb->prepare("\r\n\t\t\t\t\t\t\t\t\t\t\t\tSELECT f.lang_code, f.flag, f.from_template, l.name\r\n\t\t\t\t\t\t\t\t\t\t\t\tFROM {$this->wpdb->prefix}icl_flags f\r\n\t\t\t\t\t\t\t\t\t\t\t\t\tJOIN {$this->wpdb->prefix}icl_languages_translations l ON f.lang_code = l.language_code\r\n\t\t\t\t\t\t\t\t\t\t\t\tWHERE l.display_language_code = %s AND f.lang_code IN(" . wpml_prepare_in($languages) . ")\r\n\t\t\t\t\t\t\t\t\t\t\t\t", $this->admin_language)); foreach ($res as $r) { if ($r->from_template) { $wp_upload_dir = wp_upload_dir(); $flag_path = $wp_upload_dir['baseurl'] . '/flags/'; } else { $flag_path = ICL_PLUGIN_URL . '/res/flags/'; } $flags[$r->lang_code] = '<img src="' . $flag_path . $r->flag . '" width="18" height="12" alt="' . $r->name . '" title="' . $r->name . '" style="margin:2px" />'; } $flags_column = ''; foreach ($active_languages as $v) { if (isset($flags[$v['code']])) { $flags_column .= $flags[$v['code']]; } } $flags_cache->set($key, $flags_column); } $new_columns = array(); foreach ($columns as $k => $v) { $new_columns[$k] = $v; if (($k === 'title' || $k === 'name') && !isset($new_columns['icl_translations'])) { $new_columns['icl_translations'] = $flags_column; } } } return $new_columns; }
/** * Try to parse the URL to find a related post or term * * @param string $url * @param string $lang_code * * @return string|bool */ private function resolve_object_url($url, $lang_code) { global $sitepress, $wp_query, $wpml_term_translations, $wpml_post_translations; // todo: pass as a dependencies $this->resolving_url = true; $new_url = false; $cache_key = md5($url); $cache_group = 'resolve_object_url'; $cache_found = false; $cache = new WPML_WP_Cache($cache_group); $translations = $cache->get($cache_key, $cache_found); if (!$cache_found && is_object($wp_query)) { $sitepress->set_wp_query(); // Make sure $sitepress->wp_query is set $_wp_query_back = clone $wp_query; unset($wp_query); global $wp_query; // make it global again after unset $tmp_wp_query = $sitepress->get_wp_query(); $wp_query = is_object($tmp_wp_query) ? clone $tmp_wp_query : clone $_wp_query_back; unset($tmp_wp_query); $languages_helper = new WPML_Languages($wpml_term_translations, $sitepress, $wpml_post_translations); list($translations, $wp_query) = $languages_helper->get_ls_translations($wp_query, $_wp_query_back, $sitepress->get_wp_query()); // restore current $wp_query unset($wp_query); global $wp_query; // make it global again after unset $wp_query = clone $_wp_query_back; unset($_wp_query_back); $cache->set($cache_key, $translations); } if ($translations && isset($translations[$lang_code]->element_type)) { $current_lang = $sitepress->get_current_language(); $sitepress->switch_lang($lang_code); $element = explode('_', $translations[$lang_code]->element_type); $type = array_shift($element); $subtype = implode('_', $element); switch ($type) { case 'post': $new_url = get_permalink($translations[$lang_code]->element_id); break; case 'tax': $term = get_term($translations[$lang_code]->element_id, $subtype); $new_url = get_term_link($term); break; } $sitepress->switch_lang($current_lang); } $this->resolving_url = false; return $new_url; }
function get_ls_languages($template_args = array()) { /** @var $wp_query WP_Query */ global $wp_query, $wpml_post_translations, $wpml_term_translations; $this->set_wp_query(); $current_language = $this->get_current_language(); $default_language = $this->get_default_language(); $cache_key_args = $template_args ? array_filter($template_args) : array('default'); $cache_key_args[] = $current_language; $cache_key_args[] = $default_language; if (isset($this->wp_query->request)) { $cache_key_args[] = $this->wp_query->request; } $cache_key_args = array_filter($cache_key_args); $cache_key = md5(wp_json_encode($cache_key_args)); $cache_group = 'ls_languages'; $found = false; $cache = new WPML_WP_Cache($cache_group); $ls_languages = $cache->get($cache_key, $found); if ($found) { return $ls_languages; } // use original wp_query for this // backup current $wp_query if (!isset($wp_query)) { return $this->get_active_languages(); } $_wp_query_back = clone $wp_query; unset($wp_query); global $wp_query; // make it global again after unset $wp_query = clone $this->wp_query; $w_active_languages = $this->get_active_languages(); if (isset($template_args['skip_missing'])) { //override default setting $icl_lso_link_empty = !$template_args['skip_missing']; } else { $icl_lso_link_empty = $this->settings['icl_lso_link_empty']; } $languages_helper = new WPML_Languages($wpml_term_translations, $this, $wpml_post_translations); list($translations, $wp_query) = $languages_helper->get_ls_translations($wp_query, $_wp_query_back, $this->wp_query); // 2. determine url foreach ($w_active_languages as $k => $lang) { $skip_lang = false; if (is_singular() || isset($_wp_query_back->query['name']) && isset($_wp_query_back->query['post_type']) || !empty($this->wp_query->queried_object_id) && $this->wp_query->queried_object_id == get_option('page_for_posts')) { $this_lang_tmp = $this->this_lang; $this->switch_lang($lang['code']); $lang_page_on_front = get_option('page_on_front'); $lang_page_for_posts = get_option('page_for_posts'); if ($lang_page_on_front) { $lang_page_on_front = icl_object_id($lang_page_on_front, 'page', false, $lang['code']); } if ($lang_page_for_posts) { $lang_page_for_posts = icl_object_id($lang_page_for_posts, 'page', false, $lang['code']); } if ('page' === get_option('show_on_front') && !empty($translations[$lang['code']]) && $translations[$lang['code']]->element_id == $lang_page_on_front) { $lang['translated_url'] = $this->language_url($lang['code']); } elseif ('page' == get_option('show_on_front') && !empty($translations[$lang['code']]) && $translations[$lang['code']]->element_id && $translations[$lang['code']]->element_id == $lang_page_for_posts) { if ($lang_page_for_posts) { $lang['translated_url'] = get_permalink($lang_page_for_posts); } else { $lang['translated_url'] = $this->language_url($lang['code']); } } else { if (!empty($translations[$lang['code']]) && isset($translations[$lang['code']]->post_title)) { $this->switch_lang($lang['code']); $lang['translated_url'] = get_permalink($translations[$lang['code']]->element_id); $lang['missing'] = 0; $this->switch_lang($current_language); } else { if ($icl_lso_link_empty) { if (!empty($template_args['link_empty_to'])) { $lang['translated_url'] = str_replace('{%lang}', $lang['code'], $template_args['link_empty_to']); } else { $lang['translated_url'] = $this->language_url($lang['code']); } } else { $skip_lang = true; } $lang['missing'] = 1; } } $this->this_lang = $this_lang_tmp; } elseif (is_category() || is_tax() || is_tag()) { global $icl_adjust_id_url_filter_off; $icl_adjust_id_url_filter_off = true; list($lang, $skip_lang) = $languages_helper->add_tax_url_to_ls_lang($lang, $translations, $icl_lso_link_empty, $skip_lang); $icl_adjust_id_url_filter_off = false; } elseif (is_author()) { global $authordata; if (empty($authordata)) { $authordata = get_userdata(get_query_var('author')); } remove_filter('home_url', array($this, 'home_url'), 1); remove_filter('author_link', array($this, 'author_link')); list($lang, $skip_lang) = $languages_helper->add_author_url_to_ls_lang($lang, $authordata, $icl_lso_link_empty, $skip_lang); add_filter('home_url', array($this, 'home_url'), 1, 4); add_filter('author_link', array($this, 'author_link')); } elseif (is_archive() && !is_tag()) { global $icl_archive_url_filter_off; $icl_archive_url_filter_off = true; remove_filter('post_type_archive_link', array($this, 'post_type_archive_link_filter'), 10); list($lang, $skip_lang) = $languages_helper->add_date_or_cpt_url_to_ls_lang($lang, $this->wp_query, $icl_lso_link_empty, $skip_lang); add_filter('post_type_archive_link', array($this, 'post_type_archive_link_filter'), 10, 2); $icl_archive_url_filter_off = false; } elseif (is_search()) { $url_glue = strpos($this->language_url($lang['code']), '?') === false ? '?' : '&'; $lang['translated_url'] = $this->language_url($lang['code']) . $url_glue . 's=' . urlencode($wp_query->query['s']); } else { global $icl_language_switcher_preview; if ($icl_lso_link_empty || is_home() || is_404() || 'page' === get_option('show_on_front') && ($this->wp_query->queried_object_id == get_option('page_on_front') || $this->wp_query->queried_object_id == get_option('page_for_posts')) || $icl_language_switcher_preview || WPML_Root_Page::is_current_request_root()) { $lang['translated_url'] = $this->language_url($lang['code']); $skip_lang = false; } else { $skip_lang = true; unset($w_active_languages[$k]); } } if (!$skip_lang) { $w_active_languages[$k] = $lang; } else { unset($w_active_languages[$k]); } } // 3. foreach ($w_active_languages as $k => $v) { $w_active_languages[$k] = $languages_helper->get_ls_language($k, $current_language, $w_active_languages[$k]); } // 4. pass GET parameters $parameters_copied = apply_filters('icl_lang_sel_copy_parameters', array_map('trim', explode(',', wpml_get_setting_filter('', 'icl_lang_sel_copy_parameters')))); if ($parameters_copied) { foreach ($_GET as $k => $v) { if (in_array($k, $parameters_copied)) { $gets_passed[$k] = $v; } } } if (!empty($gets_passed)) { $gets_passed = http_build_query($gets_passed); foreach ($w_active_languages as $code => $al) { if (empty($al['missing'])) { $glue = false !== strpos($w_active_languages[$code]['url'], '?') ? '&' : '?'; $w_active_languages[$code]['url'] .= $glue . $gets_passed; } } } // restore current $wp_query unset($wp_query); global $wp_query; // make it global again after unset $wp_query = clone $_wp_query_back; unset($_wp_query_back); $w_active_languages = apply_filters('icl_ls_languages', $w_active_languages); $w_active_languages = $languages_helper->sort_ls_languages($w_active_languages, $template_args); // Change the url, in case languages in subdomains are set. if ($this->settings['language_negotiation_type'] == 2) { foreach ($w_active_languages as $lang => $element) { $w_active_languages[$lang]['url'] = $this->convert_url($element['url'], $lang); } } if (isset($this->wp_query->query_vars['post_type']) && !is_array($this->wp_query->query_vars['post_type']) && !empty($this->wp_query->query_vars['post_type']) && !$this->is_translated_post_type($this->wp_query->query_vars['post_type'])) { foreach ($w_active_languages as $lang => $element) { unset($w_active_languages[$lang]); } } wp_reset_query(); $cache->set($cache_key, $w_active_languages); return $w_active_languages; }
/** * Create new instance of WPML_WP_Cache for each group and flush cache for group. * @param array $groups */ private function flush_object_cache_for_groups($groups = array()) { if (!empty($groups)) { foreach ($groups as $group) { $cache = new WPML_WP_Cache($group); $cache->flush_group_cache(); } } }
/** * @todo Improve this query since only ID is used no need for all fields. * * @return array */ private function get_acf_groups() { $found = false; $cache = new WPML_WP_Cache($this->cache_group); $result = $cache->get($this->cache_key_for_fields_groups, $found); if (!$found) { $result = get_posts(array('post_type' => 'acf-field-group', 'posts_per_page' => -1, 'orderby' => 'menu_order title', 'order' => 'asc', 'suppress_filters' => false, 'post_status' => array('publish', 'acf-disabled'), 'update_post_meta_cache' => false)); $cache->set($this->cache_key_for_fields_groups, $result); } return $result; }