/** * Generates the meta description text. */ private function generate_metadesc() { global $post, $wp_query; $metadesc = ''; $metadesc_override = false; $post_type = ''; $template = ''; if (is_object($post) && (isset($post->post_type) && $post->post_type !== '')) { $post_type = $post->post_type; } if (is_singular()) { if ($metadesc === '' && $post_type !== '' && isset($this->options['metadesc-' . $post_type])) { $template = $this->options['metadesc-' . $post_type]; $term = $post; } $metadesc_override = YMBESEO_Meta::get_value('metadesc'); } else { if (is_search()) { $metadesc = ''; } elseif ($this->is_home_posts_page()) { $template = $this->options['metadesc-home-wpseo']; $term = array(); if (empty($template)) { $template = get_bloginfo('description'); } } elseif ($this->is_posts_page()) { $metadesc = YMBESEO_Meta::get_value('metadesc', get_option('page_for_posts')); if ($metadesc === '' && $post_type !== '' && isset($this->options['metadesc-' . $post_type])) { $page = get_post(get_option('page_for_posts')); $template = $this->options['metadesc-' . $post_type]; $term = $page; } } elseif ($this->is_home_static_page()) { $metadesc = YMBESEO_Meta::get_value('metadesc'); if ($metadesc === '' && $post_type !== '' && isset($this->options['metadesc-' . $post_type])) { $template = $this->options['metadesc-' . $post_type]; } } elseif (is_category() || is_tag() || is_tax()) { $term = $wp_query->get_queried_object(); $metadesc_override = YMBESEO_Taxonomy_Meta::get_term_meta($term, $term->taxonomy, 'desc'); if (is_object($term) && isset($term->taxonomy, $this->options['metadesc-tax-' . $term->taxonomy])) { $template = $this->options['metadesc-tax-' . $term->taxonomy]; } } elseif (is_author()) { $author_id = get_query_var('author'); $metadesc = get_the_author_meta('YMBESEO_metadesc', $author_id); if ((!is_string($metadesc) || $metadesc === '') && '' !== $this->options['metadesc-author-wpseo']) { $template = $this->options['metadesc-author-wpseo']; } } elseif (is_post_type_archive()) { $post_type = get_query_var('post_type'); if (is_array($post_type)) { $post_type = reset($post_type); } if (isset($this->options['metadesc-ptarchive-' . $post_type])) { $template = $this->options['metadesc-ptarchive-' . $post_type]; } } elseif (is_archive()) { $template = $this->options['metadesc-archive-wpseo']; } // If we're on a paginated page, and the template doesn't change for paginated pages, bail. if ((!is_string($metadesc) || $metadesc === '') && get_query_var('paged') && get_query_var('paged') > 1 && $template !== '') { if (strpos($template, '%%page') === false) { $metadesc = ''; } } } $post_data = $post; if (is_string($metadesc_override) && '' !== $metadesc_override) { $metadesc = $metadesc_override; if (isset($term)) { $post_data = $term; } } else { if ((!is_string($metadesc) || '' === $metadesc) && '' !== $template) { if (!isset($term)) { $term = $wp_query->get_queried_object(); } $metadesc = $template; $post_data = $term; } } $metadesc = YMBESEO_replace_vars($metadesc, $post_data); /** * Filter: 'YMBESEO_metadesc' - Allow changing the Yoast SEO meta description sentence. * * @api string $metadesc The description sentence. */ $this->metadesc = apply_filters('YMBESEO_metadesc', trim($metadesc)); }
/** * Retrieve link url and text based on term object * * @param object $term Term object. * * @return array Array of link text and url */ private function get_link_info_for_term($term) { $link = array(); $bctitle = YMBESEO_Taxonomy_Meta::get_term_meta($term, $term->taxonomy, 'bctitle'); if (!is_string($bctitle) || $bctitle === '') { $bctitle = $term->name; } $link['url'] = get_term_link($term); $link['text'] = $bctitle; return $link; }
/** * Add extra default options received from a filter */ public function enrich_defaults() { $extra_defaults_per_term = apply_filters('YMBESEO_add_extra_taxmeta_term_defaults', array()); if (is_array($extra_defaults_per_term)) { self::$defaults_per_term = array_merge($extra_defaults_per_term, self::$defaults_per_term); } }
/** * Update the taxonomy meta data on save. * * @param int $term_id ID of the term to save data for. * @param int $tt_id The taxonomy_term_id for the term. * @param string $taxonomy The taxonomy the term belongs to. */ function update_term($term_id, $tt_id, $taxonomy) { $tax_meta = get_option('YMBESEO_taxonomy_meta'); /* Create post array with only our values */ $new_meta_data = array(); foreach (YMBESEO_Taxonomy_Meta::$defaults_per_term as $key => $default) { if (isset($_POST[$key])) { $new_meta_data[$key] = $_POST[$key]; } } unset($key, $default); /* Validate the post values */ $old = YMBESEO_Taxonomy_Meta::get_term_meta($term_id, $taxonomy); $clean = YMBESEO_Taxonomy_Meta::validate_term_meta_data($new_meta_data, $old); /* Add/remove the result to/from the original option value */ if ($clean !== array()) { $tax_meta[$taxonomy][$term_id] = $clean; } else { unset($tax_meta[$taxonomy][$term_id]); if (isset($tax_meta[$taxonomy]) && $tax_meta[$taxonomy] === array()) { unset($tax_meta[$taxonomy]); } } // Prevent complete array validation. $tax_meta['YMBESEO_already_validated'] = true; update_option('YMBESEO_taxonomy_meta', $tax_meta); }
/** * Build a sub-sitemap for a specific taxonomy -- example.com/tax-sitemap.xml * * @param string $taxonomy Registered taxonomy's slug. */ function build_tax_map($taxonomy) { if (isset($this->options['taxonomies-' . $taxonomy->name . '-not_in_sitemap']) && $this->options['taxonomies-' . $taxonomy->name . '-not_in_sitemap'] === true || in_array($taxonomy, array('link_category', 'nav_menu', 'post_format')) || apply_filters('YMBESEO_sitemap_exclude_taxonomy', false, $taxonomy->name)) { $this->bad_sitemap = true; return; } global $wpdb; $output = ''; $steps = $this->max_entries; $n = (int) $this->n; $offset = $n > 1 ? ($n - 1) * $this->max_entries : 0; /** * Filter: 'YMBESEO_sitemap_exclude_empty_terms' - Allow people to include empty terms in sitemap * * @api bool $hide_empty Whether or not to hide empty terms, defaults to true. * * @param object $taxonomy The taxonomy we're getting terms for. */ $hide_empty = apply_filters('YMBESEO_sitemap_exclude_empty_terms', true, $taxonomy); $terms = get_terms($taxonomy->name, array('hide_empty' => $hide_empty)); $terms = array_splice($terms, $offset, $steps); if (is_array($terms) && $terms !== array()) { foreach ($terms as $c) { $url = array(); $tax_noindex = YMBESEO_Taxonomy_Meta::get_term_meta($c, $c->taxonomy, 'noindex'); $tax_sitemap_inc = YMBESEO_Taxonomy_Meta::get_term_meta($c, $c->taxonomy, 'sitemap_include'); if (is_string($tax_noindex) && $tax_noindex === 'noindex' && (!is_string($tax_sitemap_inc) || $tax_sitemap_inc !== 'always')) { continue; } if ($tax_sitemap_inc === 'never') { continue; } $url['loc'] = YMBESEO_Taxonomy_Meta::get_term_meta($c, $c->taxonomy, 'canonical'); if (!is_string($url['loc']) || $url['loc'] === '') { $url['loc'] = get_term_link($c, $c->taxonomy); if ($this->options['trailingslash'] === true) { $url['loc'] = trailingslashit($url['loc']); } } if ($c->count > 10) { $url['pri'] = 0.6; } else { if ($c->count > 3) { $url['pri'] = 0.4; } else { $url['pri'] = 0.2; } } // Grab last modified date. $sql = $wpdb->prepare("\n\t\t\t\t\t\tSELECT MAX(p.post_modified_gmt) AS lastmod\n\t\t\t\t\t\tFROM\t{$wpdb->posts} AS p\n\t\t\t\t\t\tINNER JOIN {$wpdb->term_relationships} AS term_rel\n\t\t\t\t\t\t\tON\t\tterm_rel.object_id = p.ID\n\t\t\t\t\t\tINNER JOIN {$wpdb->term_taxonomy} AS term_tax\n\t\t\t\t\t\t\tON\t\tterm_tax.term_taxonomy_id = term_rel.term_taxonomy_id\n\t\t\t\t\t\t\tAND\t\tterm_tax.taxonomy = %s\n\t\t\t\t\t\t\tAND\t\tterm_tax.term_id = %d\n\t\t\t\t\t\tWHERE\tp.post_status IN ('publish','inherit')\n\t\t\t\t\t\t\tAND\t\tp.post_password = ''", $c->taxonomy, $c->term_id); $url['mod'] = $wpdb->get_var($sql); $url['chf'] = $this->filter_frequency($c->taxonomy . '_term', 'weekly', $url['loc']); // Use this filter to adjust the entry before it gets added to the sitemap. $url = apply_filters('YMBESEO_sitemap_entry', $url, 'term', $c); if (is_array($url) && $url !== array()) { $output .= $this->sitemap_url($url); } } unset($c, $url, $tax_noindex, $tax_sitemap_inc, $sql); } if (empty($output)) { $this->bad_sitemap = true; return; } $this->sitemap = '<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '; $this->sitemap .= 'xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" '; $this->sitemap .= 'xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n"; if (is_string($output) && trim($output) !== '') { $this->sitemap .= $output; } else { // If the sitemap is empty, add the homepage URL to make sure it doesn't throw errors in GWT. $this->sitemap .= $this->sitemap_url(home_url()); } $this->sitemap .= '</urlset>'; }
/** * Output the OpenGraph description, specific OG description first, if not, grab the meta description. * * @param bool $echo Whether to echo or return the description. * * @return string $ogdesc */ public function description($echo = true) { $ogdesc = ''; $frontend = YMBESEO_Frontend::get_instance(); if (is_front_page()) { if (isset($this->options['og_frontpage_desc']) && $this->options['og_frontpage_desc'] !== '') { $ogdesc = YMBESEO_replace_vars($this->options['og_frontpage_desc'], null); } else { $ogdesc = $frontend->metadesc(false); } } $is_posts_page = $frontend->is_posts_page(); if (is_singular() || $is_posts_page) { $post_id = $is_posts_page ? get_option('page_for_posts') : get_the_ID(); $post = get_post($post_id); $ogdesc = YMBESEO_Meta::get_value('opengraph-description', $post_id); // Replace Yoast SEO Variables. $ogdesc = YMBESEO_replace_vars($ogdesc, $post); // Use metadesc if $ogdesc is empty. if ($ogdesc === '') { $ogdesc = $frontend->metadesc(false); } // Tag og:description is still blank so grab it from get_the_excerpt(). if (!is_string($ogdesc) || is_string($ogdesc) && $ogdesc === '') { $ogdesc = str_replace('[…]', '…', strip_tags(get_the_excerpt())); } } if (is_category() || is_tag() || is_tax()) { $ogdesc = $frontend->metadesc(false); if ('' == $ogdesc) { $ogdesc = trim(strip_tags(term_description())); } if ('' == $ogdesc) { $term = $GLOBALS['wp_query']->get_queried_object(); $ogdesc = YMBESEO_Taxonomy_Meta::get_term_meta($term, $term->taxonomy, 'desc'); } } // Strip shortcodes if any. $ogdesc = strip_shortcodes($ogdesc); /** * Filter: 'YMBESEO_opengraph_desc' - Allow changing the OpenGraph description * * @api string $ogdesc The description string. */ $ogdesc = trim(apply_filters('YMBESEO_opengraph_desc', $ogdesc)); if (is_string($ogdesc) && $ogdesc !== '') { if ($echo !== false) { $this->og_tag('og:description', $ogdesc); } } return $ogdesc; }
/** * Retrieve a taxonomy term's meta value. * * @deprecated 1.5.0 * @deprecated use YMBESEO_Taxonomy_Meta::get_term_meta() * @see YMBESEO_Taxonomy_Meta::get_term_meta() * * @param string|object $term Term to get the meta value for. * @param string $taxonomy Name of the taxonomy to which the term is attached. * @param string $meta Meta value to get. * * @return bool|mixed value when the meta exists, false when it does not */ function YMBESEO_get_term_meta($term, $taxonomy, $meta) { _deprecated_function(__FUNCTION__, 'WPSEO 1.5.0', 'YMBESEO_Taxonomy_Meta::get_term_meta()'); return YMBESEO_Taxonomy_Meta::get_term_meta($term, $taxonomy, $meta); }