예제 #1
0
 /**
  * Count the number of anchors and group them by type.
  *
  * @param object $xpath An XPATH object of the current document.
  *
  * @return array
  */
 function get_anchor_count(&$xpath)
 {
     $query = '//a|//A';
     $dom_objects = $xpath->query($query);
     $count = array('total' => 0, 'internal' => array('nofollow' => 0, 'dofollow' => 0), 'external' => array('nofollow' => 0, 'dofollow' => 0), 'other' => array('nofollow' => 0, 'dofollow' => 0));
     if (is_object($dom_objects) && is_a($dom_objects, 'DOMNodeList') && $dom_objects->length > 0) {
         foreach ($dom_objects as $dom_object) {
             $count['total']++;
             if ($dom_object->attributes->getNamedItem('href')) {
                 $href = $dom_object->attributes->getNamedItem('href')->textContent;
                 $wpurl = get_bloginfo('url');
                 if (YMBESEO_Utils::is_url_relative($href) === true || substr($href, 0, strlen($wpurl)) === $wpurl) {
                     $type = 'internal';
                 } elseif (substr($href, 0, 4) == 'http') {
                     $type = 'external';
                 } else {
                     $type = 'other';
                 }
                 if ($dom_object->attributes->getNamedItem('rel')) {
                     $link_rel = $dom_object->attributes->getNamedItem('rel')->textContent;
                     if (stripos($link_rel, 'nofollow') !== false) {
                         $count[$type]['nofollow']++;
                     } else {
                         $count[$type]['dofollow']++;
                     }
                 } else {
                     $count[$type]['dofollow']++;
                 }
             }
         }
     }
     return $count;
 }
예제 #2
0
 /**
  * This function normally outputs the canonical but is also used in other places to retrieve
  * the canonical URL for the current page.
  *
  * @return void
  */
 private function generate_canonical()
 {
     $canonical = false;
     $canonical_override = false;
     // Set decent canonicals for homepage, singulars and taxonomy pages.
     if (is_singular()) {
         $obj = get_queried_object();
         $canonical = get_permalink($obj->ID);
         $this->canonical_unpaged = $canonical;
         $canonical_override = YMBESEO_Meta::get_value('canonical');
         // Fix paginated pages canonical, but only if the page is truly paginated.
         if (get_query_var('page') > 1) {
             $num_pages = substr_count($obj->post_content, '<!--nextpage-->') + 1;
             if ($num_pages && get_query_var('page') <= $num_pages) {
                 if (!$GLOBALS['wp_rewrite']->using_permalinks()) {
                     $canonical = add_query_arg('page', get_query_var('page'), $canonical);
                 } else {
                     $canonical = user_trailingslashit(trailingslashit($canonical) . get_query_var('page'));
                 }
             }
         }
     } else {
         if (is_search()) {
             $canonical = get_search_link();
         } elseif (is_front_page()) {
             $canonical = home_url();
         } elseif ($this->is_posts_page()) {
             $canonical = get_permalink(get_option('page_for_posts'));
         } elseif (is_tax() || is_tag() || is_category()) {
             $term = get_queried_object();
             $canonical_override = YMBESEO_Taxonomy_Meta::get_term_meta($term, $term->taxonomy, 'canonical');
             $canonical = get_term_link($term, $term->taxonomy);
         } elseif (is_post_type_archive()) {
             $post_type = get_query_var('post_type');
             if (is_array($post_type)) {
                 $post_type = reset($post_type);
             }
             $canonical = get_post_type_archive_link($post_type);
         } elseif (is_author()) {
             $canonical = get_author_posts_url(get_query_var('author'), get_query_var('author_name'));
         } elseif (is_archive()) {
             if (is_date()) {
                 if (is_day()) {
                     $canonical = get_day_link(get_query_var('year'), get_query_var('monthnum'), get_query_var('day'));
                 } elseif (is_month()) {
                     $canonical = get_month_link(get_query_var('year'), get_query_var('monthnum'));
                 } elseif (is_year()) {
                     $canonical = get_year_link(get_query_var('year'));
                 }
             }
         }
         $this->canonical_unpaged = $canonical;
         if ($canonical && get_query_var('paged') > 1) {
             global $wp_rewrite;
             if (!$wp_rewrite->using_permalinks()) {
                 if (is_front_page()) {
                     $canonical = trailingslashit($canonical);
                 }
                 $canonical = add_query_arg('paged', get_query_var('paged'), $canonical);
             } else {
                 if (is_front_page()) {
                     $canonical = YMBESEO_xml_sitemaps_base_url('');
                 }
                 $canonical = user_trailingslashit(trailingslashit($canonical) . trailingslashit($wp_rewrite->pagination_base) . get_query_var('paged'));
             }
         }
     }
     $this->canonical_no_override = $canonical;
     if (is_string($canonical) && $canonical !== '') {
         // Force canonical links to be absolute, relative is NOT an option.
         if (YMBESEO_Utils::is_url_relative($canonical) === true) {
             $canonical = $this->base_url($canonical);
         }
     }
     /**
      * Filter: 'YMBESEO_canonical' - Allow filtering of the canonical URL put out by Yoast SEO
      *
      * @api string $canonical The canonical URL
      */
     $canonical = apply_filters('YMBESEO_canonical', $canonical);
     if (is_string($canonical_override) && $canonical_override !== '') {
         $this->canonical = $canonical_override;
     } else {
         $this->canonical = $canonical;
     }
 }
예제 #3
0
 /**
  * Display an OpenGraph image tag
  *
  * @param string $img - Source URL to the image.
  *
  * @return bool
  */
 private function add_image($img)
 {
     // Filter: 'YMBESEO_opengraph_image' - Allow changing the OpenGraph image.
     $img = trim(apply_filters('YMBESEO_opengraph_image', $img));
     if (empty($img)) {
         return false;
     }
     if (YMBESEO_Utils::is_url_relative($img) === true) {
         $img = $this->get_relative_path($img);
     }
     if (in_array($img, $this->images)) {
         return false;
     }
     array_push($this->images, $img);
     return true;
 }
예제 #4
0
 /**
  * Parsing the matched images
  *
  * @param array  $matches
  * @param object $p
  * @param string $scheme
  * @param string $host
  *
  * @return array
  */
 private function parse_matched_images($matches, $p, $scheme, $host)
 {
     $return = array();
     foreach ($matches[0] as $img) {
         if (preg_match('`src=["\']([^"\']+)["\']`', $img, $match)) {
             $src = $match[1];
             if (YMBESEO_Utils::is_url_relative($src) === true) {
                 if ($src[0] !== '/') {
                     continue;
                 } else {
                     // The URL is relative, we'll have to make it absolute.
                     $src = $this->home_url . $src;
                 }
             } elseif (strpos($src, 'http') !== 0) {
                 // Protocol relative url, we add the scheme as the standard requires a protocol.
                 $src = $scheme . ':' . $src;
             }
             if (strpos($src, $host) === false) {
                 continue;
             }
             if ($src != esc_url($src)) {
                 continue;
             }
             if (isset($return[$src])) {
                 continue;
             }
             $image = array('src' => apply_filters('YMBESEO_xml_sitemap_img_src', $src, $p));
             if (preg_match('`title=["\']([^"\']+)["\']`', $img, $title_match)) {
                 $image['title'] = str_replace(array('-', '_'), ' ', $title_match[1]);
             }
             unset($title_match);
             if (preg_match('`alt=["\']([^"\']+)["\']`', $img, $alt_match)) {
                 $image['alt'] = str_replace(array('-', '_'), ' ', $alt_match[1]);
             }
             unset($alt_match);
             $image = apply_filters('YMBESEO_xml_sitemap_img', $image, $p);
             $return[] = $image;
         }
         unset($match, $src);
     }
     return $return;
 }
예제 #5
0
/**
 * Check whether a url is relative
 *
 * @deprecated 1.6.1
 * @deprecated use YMBESEO_Utils::is_url_relative()
 * @see        YMBESEO_Utils::is_url_relative()
 *
 * @param string $url
 *
 * @return bool
 */
function YMBESEO_is_url_relative($url)
{
    _deprecated_function(__FUNCTION__, 'WPSEO 1.6.1', 'YMBESEO_Utils::is_url_relative()');
    return YMBESEO_Utils::is_url_relative($url);
}