Пример #1
0
 /**
  * Output the Google+ specific title
  */
 public function google_plus_title()
 {
     if (is_singular()) {
         $title = YMBESEO_Meta::get_value('google-plus-title');
         /**
          * Filter: 'YMBESEO_googleplus_title' - Allow developers to change the Google+ specific title
          *
          * @api string $title The title string
          */
         $title = trim(apply_filters('YMBESEO_googleplus_title', $title));
         if (is_string($title) && $title !== '') {
             $title = YMBESEO_replace_vars($title, get_post());
             echo '<meta itemprop="name" content="', esc_attr($title), '">', "\n";
         }
     }
 }
Пример #2
0
 /**
  * Calculate the page analysis results for post.
  *
  * @todo [JRF => whomever] check whether the results of this method are always checked with is_wp_error()
  * @todo [JRF => whomever] check the usage of this method as it's quite intense/heavy, see if it's only
  * used when really necessary
  * @todo [JRF => whomever] see if we can get rid of the passing by reference of $results as it makes
  * the code obfuscated
  *
  * @param  object $post Post to calculate the results for.
  *
  * @return  array|WP_Error
  */
 function calculate_results($post)
 {
     $options = YMBESEO_Options::get_all();
     if (!class_exists('DOMDocument')) {
         $result = new WP_Error('no-domdocument', sprintf(__("Your hosting environment does not support PHP's %sDocument Object Model%s.", 'ymbeseo'), '<a href="http://php.net/manual/en/book.dom.php">', '</a>') . ' ' . __("To enjoy all the benefits of the page analysis feature, you'll need to (get your host to) install it.", 'ymbeseo'));
         return $result;
     }
     if (!is_array($post) && !is_object($post)) {
         $result = new WP_Error('no-post', __('No post content to analyse.', 'ymbeseo'));
         return $result;
     } elseif (self::get_value('focuskw', $post->ID) === '') {
         $result = new WP_Error('no-focuskw', sprintf(__('No focus keyword was set for this %s. If you do not set a focus keyword, no score can be calculated.', 'ymbeseo'), $post->post_type));
         self::set_value('linkdex', 0, $post->ID);
         return $result;
     } elseif (apply_filters('YMBESEO_use_page_analysis', true) !== true) {
         $result = new WP_Error('page-analysis-disabled', sprintf(__('Page Analysis has been disabled.', 'ymbeseo'), $post->post_type));
         return $result;
     }
     $results = array();
     $job = array();
     $sampleurl = $this->get_sample_permalink($post);
     $job['pageUrl'] = preg_replace('`%(?:post|page)name%`', $sampleurl[1], $sampleurl[0]);
     $job['pageSlug'] = urldecode($post->post_name);
     $job['keyword'] = self::get_value('focuskw', $post->ID);
     $job['keyword_folded'] = $this->strip_separators_and_fold($job['keyword']);
     $job['post_id'] = $post->ID;
     $job['post_type'] = $post->post_type;
     $dom = new domDocument();
     $dom->strictErrorChecking = false;
     $dom->preserveWhiteSpace = false;
     /**
      * Filter: 'YMBESEO_pre_analysis_post_content' - Make the post content filterable before calculating the page analysis
      *
      * @api string $post_content The post content
      *
      * @param object $post The post.
      */
     $post_content = apply_filters('YMBESEO_pre_analysis_post_content', $post->post_content, $post);
     // Check if the post content is not empty.
     if (!empty($post_content)) {
         @$dom->loadHTML($post_content);
     }
     unset($post_content);
     $xpath = new DOMXPath($dom);
     // Check if this focus keyword has been used already.
     $this->check_double_focus_keyword($job, $results);
     // Keyword.
     $this->score_keyword($job['keyword'], $results);
     // Title.
     $title = self::get_value('title', $post->ID);
     if ($title !== '') {
         $job['title'] = $title;
     } else {
         if (isset($options['title-' . $post->post_type]) && $options['title-' . $post->post_type] !== '') {
             $title_template = $options['title-' . $post->post_type];
         } else {
             $title_template = '%%title%% - %%sitename%%';
         }
         $job['title'] = YMBESEO_replace_vars($title_template, $post);
     }
     unset($title);
     $this->score_title($job, $results);
     // Meta description.
     $description = '';
     $desc_meta = self::get_value('metadesc', $post->ID);
     if ($desc_meta !== '') {
         $description = $desc_meta;
     } elseif (isset($options['metadesc-' . $post->post_type]) && $options['metadesc-' . $post->post_type] !== '') {
         $description = YMBESEO_replace_vars($options['metadesc-' . $post->post_type], $post);
     }
     unset($desc_meta);
     self::$meta_length = apply_filters('YMBESEO_metadesc_length', self::$meta_length, $post);
     $this->score_description($job, $results, $description, self::$meta_length);
     unset($description);
     // Body.
     $body = $this->get_body($post);
     $firstp = $this->get_first_paragraph($body);
     $this->score_body($job, $results, $body, $firstp);
     unset($firstp);
     // URL.
     $this->score_url($job, $results);
     // Headings.
     $headings = $this->get_headings($body);
     $this->score_headings($job, $results, $headings);
     unset($headings);
     // Images.
     $imgs = array();
     $imgs['count'] = substr_count($body, '<img');
     $imgs = $this->get_images_alt_text($post->ID, $body, $imgs);
     // Check featured image.
     if (function_exists('has_post_thumbnail') && has_post_thumbnail()) {
         $imgs['count'] += 1;
         if (empty($imgs['alts'])) {
             $imgs['alts'] = array();
         }
         $imgs['alts'][] = $this->strtolower_utf8(get_post_meta(get_post_thumbnail_id($post->ID), '_wp_attachment_image_alt', true));
     }
     $this->score_images_alt_text($job, $results, $imgs);
     unset($imgs);
     unset($body);
     // Anchors.
     $anchors = $this->get_anchor_texts($xpath);
     $count = $this->get_anchor_count($xpath);
     $this->score_anchor_texts($job, $results, $anchors, $count);
     unset($anchors, $count, $dom);
     $results = apply_filters('YMBESEO_linkdex_results', $results, $job, $post);
     $this->aasort($results, 'val');
     $overall = 0;
     $overall_max = 0;
     foreach ($results as $result) {
         $overall += $result['val'];
         $overall_max += 9;
     }
     unset($result);
     if ($overall < 1) {
         $overall = 1;
     }
     $score = YMBESEO_Utils::calc(YMBESEO_Utils::calc($overall, '/', $overall_max), '*', 100, true);
     if (!is_wp_error($score)) {
         self::set_value('linkdex', absint($score), $post->ID);
         $results['total'] = $score;
     }
     return $results;
 }
Пример #3
0
/**
 * Used in the editor to replace vars for the snippet preview
 */
function YMBESEO_ajax_replace_vars()
{
    global $post;
    check_ajax_referer('wpseo-replace-vars');
    $post = get_post(intval(filter_input(INPUT_POST, 'post_id')));
    $omit = array('excerpt', 'excerpt_only', 'title');
    echo YMBESEO_replace_vars(stripslashes(filter_input(INPUT_POST, 'string')), $post, $omit);
    die;
}
Пример #4
0
 /**
  * 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));
 }
Пример #5
0
 /**
  * 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('[&hellip;]', '&hellip;', 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;
 }