'
. sprintf( __( "Title display in search engines is limited to 70 chars, %s chars left.", 'wordpress-seo' ), "" ) . " "
. sprintf( __( "If the SEO Title is empty, the preview shows what the plugin generates based on your %stitle template%s.", 'wordpress-seo' ), "", "" ) . '
" . sprintf( __( "If the meta description is empty, the preview shows what the plugin generates based on your %smeta description template%s.", 'wordpress-seo' ), "", "" ) . "
"
);
if ( isset( $options['usemetakeywords'] ) && $options['usemetakeywords'] ) {
$mbs['metakeywords'] = array(
"name" => "metakeywords",
"std" => "",
"class" => "metakeywords",
"type" => "text",
"title" => __( "Meta Keywords", 'wordpress-seo' ),
"description" => sprintf( __( "If you type something above it will override your %smeta keywords template%s.", 'wordpress-seo' ), "", "" )
);
}
// Apply filters before entering the advanced section
$mbs = apply_filters( 'wpseo_metabox_entries', $mbs );
return $mbs;
}
/**
* Retrieve the meta boxes for the advanced tab.
*
* @return array
*/
function get_advanced_meta_boxes() {
global $post;
$post_type = '';
if ( isset( $post->post_type ) )
$post_type = $post->post_type;
else if ( !isset( $post->post_type ) && isset( $_GET['post_type'] ) )
$post_type = $_GET['post_type'];
$options = get_wpseo_options();
$mbs = array();
$mbs['meta-robots-noindex'] = array(
"name" => "meta-robots-noindex",
"std" => "-",
"title" => __( "Meta Robots Index", 'wordpress-seo' ),
"type" => "select",
"options" => array(
"0" => sprintf( __( "Default for post type, currently: %s", 'wordpress-seo' ), ( isset( $options['noindex-' . $post_type] ) && $options['noindex-' . $post_type] ) ? 'noindex' : 'index' ),
"2" => "index",
"1" => "noindex",
),
);
$mbs['meta-robots-nofollow'] = array(
"name" => "meta-robots-nofollow",
"std" => "follow",
"title" => __( "Meta Robots Follow", 'wordpress-seo' ),
"type" => "radio",
"options" => array(
"0" => __( "Follow", 'wordpress-seo' ),
"1" => __( "Nofollow", 'wordpress-seo' ),
),
);
$mbs['meta-robots-adv'] = array(
"name" => "meta-robots-adv",
"std" => "none",
"type" => "multiselect",
"title" => __( "Meta Robots Advanced", 'wordpress-seo' ),
"description" => __( "Advanced meta robots settings for this page.", 'wordpress-seo' ),
"options" => array(
"noodp" => __( "NO ODP", 'wordpress-seo' ),
"noydir" => __( "NO YDIR", 'wordpress-seo' ),
"noarchive" => __( "No Archive", 'wordpress-seo' ),
"nosnippet" => __( "No Snippet", 'wordpress-seo' ),
),
);
if ( isset( $options['breadcrumbs-enable'] ) && $options['breadcrumbs-enable'] ) {
$mbs['bctitle'] = array(
"name" => "bctitle",
"std" => "",
"type" => "text",
"title" => __( "Breadcrumbs title", 'wordpress-seo' ),
"description" => __( "Title to use for this page in breadcrumb paths", 'wordpress-seo' ),
);
}
if ( isset( $options['enablexmlsitemap'] ) && $options['enablexmlsitemap'] ) {
$mbs['sitemap-include'] = array(
"name" => "sitemap-include",
"std" => "-",
"type" => "select",
"title" => __( "Include in Sitemap", 'wordpress-seo' ),
"description" => __( "Should this page be in the XML Sitemap at all times, regardless of Robots Meta settings?", 'wordpress-seo' ),
"options" => array(
"-" => __( "Auto detect", 'wordpress-seo' ),
"always" => __( "Always include", 'wordpress-seo' ),
"never" => __( "Never include", 'wordpress-seo' ),
),
);
$mbs['sitemap-prio'] = array(
"name" => "sitemap-prio",
"std" => "-",
"type" => "select",
"title" => __( "Sitemap Priority", 'wordpress-seo' ),
"description" => __( "The priority given to this page in the XML sitemap.", 'wordpress-seo' ),
"options" => array(
"-" => __( "Automatic prioritization", 'wordpress-seo' ),
"1" => __( "1 - Highest priority", 'wordpress-seo' ),
"0.9" => "0.9",
"0.8" => "0.8 - " . __( "Default for first tier pages", 'wordpress-seo' ),
"0.7" => "0.7",
"0.6" => "0.6 - " . __( "Default for second tier pages and posts", 'wordpress-seo' ),
"0.5" => "0.5 - " . __( "Medium priority", 'wordpress-seo' ),
"0.4" => "0.4",
"0.3" => "0.3",
"0.2" => "0.2",
"0.1" => "0.1 - " . __( "Lowest priority", 'wordpress-seo' ),
),
);
}
$mbs['canonical'] = array(
"name" => "canonical",
"std" => "",
"type" => "text",
"title" => __( "Canonical URL", 'wordpress-seo' ),
"description" => sprintf( __( "The canonical URL that this page should point to, leave empty to default to permalink. %sCross domain canonical%s supported too.", 'wordpress-seo' ), "", "" )
);
$mbs['redirect'] = array(
"name" => "redirect",
"std" => "",
"type" => "text",
"title" => __( "301 Redirect", 'wordpress-seo' ),
"description" => __( "The URL that this page should redirect to.", 'wordpress-seo' )
);
// Apply filters for in advanced section
$mbs = apply_filters( 'wpseo_metabox_entries_advanced', $mbs );
return $mbs;
}
/**
* Output the meta box
*/
function meta_box() {
if ( isset( $_GET['post'] ) ) {
$post_id = (int) $_GET['post'];
$post = get_post( $post_id );
} else {
global $post;
}
$options = get_wpseo_options();
?>
' . sprintf( __( 'This page analysis brought to you by the collaboration of Yoast and %sLinkdex%s. Linkdex is an SEO suite that helps you optimize your site and offers you all the SEO tools you\'ll need. Yoast uses %sLinkdex%s and highly recommends you do too!', 'wordpress-seo' ), '', '', '', '' ) . '
';
if ( WP_DEBUG )
$output .= '
(' . $perc_score . '%)
';
$output = '
' . __( 'To update this page analysis, save as draft or update and check this tab again', 'wordpress-seo' ) . '.
' . $output;
unset( $results );
return $output;
}
/**
* Calculate the page analysis results for post.
*
* @param object $post Post to calculate the results for.
* @return array
*/
function calculate_results( $post ) {
$options = get_wpseo_options();
if ( !class_exists( 'DOMDocument' ) ) {
$result = new WP_Error( 'no-domdocument', sprintf( __( "Your hosting environment does not support PHP's %sDocument Object Model%s.", 'wordpress-seo' ), '', '' ) . ' ' . __( "To enjoy all the benefits of the page analysis feature, you'll need to (get your host to) install it.", 'wordpress-seo' ) );
return $result;
}
if ( !wpseo_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.', 'wordpress-seo' ), $post->post_type ) );
wpseo_set_value( 'linkdex', 0, $post->ID );
return $result;
}
$results = array();
$job = array();
$sampleurl = get_sample_permalink( $post );
$job["pageUrl"] = preg_replace( '/%(post|page)name%/', $sampleurl[1], $sampleurl[0] );
$job["pageSlug"] = urldecode( $post->post_name );
$job["keyword"] = trim( wpseo_get_value( 'focuskw' ) );
$job["keyword_folded"] = $this->strip_separators_and_fold( $job["keyword"] );
$dom = new domDocument;
$dom->strictErrorChecking = false;
$dom->preserveWhiteSpace = false;
@$dom->loadHTML( $post->post_content );
$xpath = new DOMXPath( $dom );
$statistics = new Yoast_TextStatistics;
// Keyword
$this->score_keyword( $job['keyword'], $results );
// Title
if ( wpseo_get_value( 'title' ) ) {
$title = wpseo_get_value( '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%%';
$title = wpseo_replace_vars( $title_template, (array) $post );
}
$this->score_title( $job, $results, $title, $statistics );
unset( $title );
// Meta description
$description = '';
if ( wpseo_get_value( 'metadesc' ) ) {
$description = wpseo_get_value( 'metadesc' );
} else {
if ( isset( $options['metadesc-' . $post->post_type] ) && !empty( $options['metadesc-' . $post->post_type] ) )
$description = wpseo_replace_vars( $options['metadesc-' . $post->post_type], (array) $post );
}
$meta_length = apply_filters( 'wpseo_metadesc_length', 156, $post );
$this->score_description( $job, $results, $description, $statistics, $meta_length );
unset( $description );
// Body
$body = $this->get_body( $post );
$firstp = $this->get_first_paragraph( $post );
$this->score_body( $job, $results, $body, $firstp, $statistics );
unset( $body );
unset( $firstp );
// URL
$this->score_url( $job, $results, $statistics );
// Headings
$headings = $this->get_headings( $post->post_content );
$this->score_headings( $job, $results, $headings );
unset( $headings );
// Images
$imgs = array();
$imgs['count'] = $this->get_image_count( $xpath );
$imgs = $this->get_images_alt_text( $post, $imgs );
$this->score_images_alt_text( $job, $results, $imgs );
unset( $imgs );
// 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 );
$this->aasort( $results, 'val' );
$overall = 0;
$overall_max = 0;
foreach ( $results as $result ) {
$overall += $result['val'];
$overall_max += 9;
}
if ( $overall < 1 )
$overall = 1;
$score = round( ( $overall / $overall_max ) * 100 );
wpseo_set_value( 'linkdex', $score, $post->ID );
return $results;
}
/**
* Save the score result to the results array.
*
* @param array $results The results array used to store results.
* @param int $scoreValue The score value.
* @param string $scoreMessage The score message.
*/
function save_score_result( &$results, $scoreValue, $scoreMessage ) {
$score = array(
'val' => $scoreValue,
'msg' => $scoreMessage
);
$results[] = $score;
}
/**
* Clean up the input string.
*
* @param string $inputString String to clean up.
* @param bool $removeOptionalCharacters Whether or not to do a cleanup of optional chars too.
* @return string
*/
function strip_separators_and_fold( $inputString, $removeOptionalCharacters = false ) {
$keywordCharactersAlwaysReplacedBySpace = array( ",", "'", "\"", "?", "’", "“", "”", "|", "/" );
$keywordCharactersRemovedOrReplaced = array( "_", "-" );
$keywordWordsRemoved = array( " a ", " in ", " an ", " on ", " for ", " the ", " and " );
// lower
$inputString = $this->strtolower_utf8( $inputString );
// default characters replaced by space
$inputString = str_replace( $keywordCharactersAlwaysReplacedBySpace, ' ', $inputString );
// standardise whitespace
$inputString = preg_replace( '/\s+/', ' ', $inputString );
// deal with the separators that can be either removed or replaced by space
if ( $removeOptionalCharacters ) {
// remove word separators with a space
$inputString = str_replace( $keywordWordsRemoved, ' ', $inputString );
$inputString = str_replace( $keywordCharactersRemovedOrReplaced, '', $inputString );
} else {
$inputString = str_replace( $keywordCharactersRemovedOrReplaced, ' ', $inputString );
}
// standardise whitespace again
$inputString = preg_replace( '/\s+/', ' ', $inputString );
return trim( $inputString );
}
/**
* Check whether the keyword contains stopwords.
*
* @param string $keyword The keyword to check for stopwords.
* @param array $results The results array.
*/
function score_keyword( $keyword, &$results ) {
global $wpseo_admin;
$keywordStopWord = __( "The keyword for this page contains one or more %sstop words%s, consider removing them. Found '%s'.", 'wordpress-seo' );
if ( $wpseo_admin->stopwords_check( $keyword ) !== false )
$this->save_score_result( $results, 5, sprintf( $keywordStopWord, "", "", $wpseo_admin->stopwords_check( $keyword ) ) );
}
/**
* Check whether the keyword is contained in the URL.
*
* @param array $job The job array holding both the keyword and the URLs.
* @param array $results The results array.
* @param object $statistics Object of class Yoast_TextStatistics used to calculate lengths.
*/
function score_url( $job, &$results, $statistics ) {
global $wpseo_admin;
$urlGood = __( "The keyword / phrase appears in the URL for this page.", 'wordpress-seo' );
$urlMedium = __( "The keyword / phrase does not appear in the URL for this page. If you decide to rename the URL be sure to check the old URL 301 redirects to the new one!", 'wordpress-seo' );
$urlStopWords = __( "The slug for this page contains one or more stop words, consider removing them.", 'wordpress-seo' );
$longSlug = __( "The slug for this page is a bit long, consider shortening it.", 'wordpress-seo' );
$needle = $this->strip_separators_and_fold( $job["keyword"] );
$haystack1 = $this->strip_separators_and_fold( $job["pageUrl"], true );
$haystack2 = $this->strip_separators_and_fold( $job["pageUrl"], false );
if ( stripos( $haystack1, $needle ) || stripos( $haystack2, $needle ) )
$this->save_score_result( $results, 9, $urlGood );
else
$this->save_score_result( $results, 6, $urlMedium );
// Check for Stop Words in the slug
if ( $wpseo_admin->stopwords_check( $job["pageSlug"], true ) !== false )
$this->save_score_result( $results, 5, $urlStopWords );
// Check if the slug isn't too long relative to the length of the keyword
if ( ( $statistics->text_length( $job["keyword"] ) + 20 ) < $statistics->text_length( $job["pageSlug"] ) && 40 < $statistics->text_length( $job["pageSlug"] ) )
$this->save_score_result( $results, 5, $longSlug );
}
/**
* Check whether the keyword is contained in the title.
*
* @param array $job The job array holding both the keyword versions.
* @param array $results The results array.
* @param string $title The title to check against keywords.
* @param object $statistics Object of class Yoast_TextStatistics used to calculate lengths.
*/
function score_title( $job, &$results, $title, $statistics ) {
$scoreTitleMinLength = 40;
$scoreTitleMaxLength = 70;
$scoreTitleKeywordLimit = 0;
$scoreTitleMissing = __( "Please create a page title.", 'wordpress-seo' );
$scoreTitleCorrectLength = __( "The page title is more than 40 characters and less than the recommended 70 character limit.", 'wordpress-seo' );
$scoreTitleTooShort = __( "The page title contains %d characters, which is less than the recommended minimum of 40 characters. Use the space to add keyword variations or create compelling call-to-action copy.", 'wordpress-seo' );
$scoreTitleTooLong = __( "The page title contains %d characters, which is more than the viewable limit of 70 characters; some words will not be visible to users in your listing.", 'wordpress-seo' );
$scoreTitleKeywordMissing = __( "The keyword / phrase %s does not appear in the page title.", 'wordpress-seo' );
$scoreTitleKeywordBeginning = __( "The page title contains keyword / phrase, at the beginning which is considered to improve rankings.", 'wordpress-seo' );
$scoreTitleKeywordEnd = __( "The page title contains keyword / phrase, but it does not appear at the beginning; try and move it to the beginning.", 'wordpress-seo' );
if ( $title == "" ) {
$this->save_score_result( $results, 1, $scoreTitleMissing );
} else {
$length = $statistics->text_length( $title );
if ( $length < $scoreTitleMinLength )
$this->save_score_result( $results, 6, sprintf( $scoreTitleTooShort, $length ) );
else if ( $length > $scoreTitleMaxLength )
$this->save_score_result( $results, 6, sprintf( $scoreTitleTooLong, $length ) );
else
$this->save_score_result( $results, 9, $scoreTitleCorrectLength );
// TODO MA Keyword/Title matching is exact match with separators removed, but should extend to distributed match
$needle_position = stripos( $title, $job["keyword_folded"] );
if ( $needle_position === false ) {
$needle_position = stripos( $title, $job["keyword"] );
}
if ( $needle_position === false )
$this->save_score_result( $results, 2, sprintf( $scoreTitleKeywordMissing, $job["keyword_folded"] ) );
else if ( $needle_position <= $scoreTitleKeywordLimit )
$this->save_score_result( $results, 9, $scoreTitleKeywordBeginning );
else
$this->save_score_result( $results, 6, $scoreTitleKeywordEnd );
}
}
/**
* Check whether the document contains outbound links and whether it's anchor text matches the keyword.
*
* @param array $job The job array holding both the keyword versions.
* @param array $results The results array.
* @param array $anchor_texts The array holding all anchors in the document.
* @param array $count The number of anchors in the document, grouped by type.
*/
function score_anchor_texts( $job, &$results, $anchor_texts, $count ) {
$scoreNoLinks = __( "No outbound links appear in this page, consider adding some as appropriate.", 'wordpress-seo' );
$scoreKeywordInOutboundLink = __( "You're linking to another page with the keyword you want this page to rank for, consider changing that if you truly want this page to rank.", 'wordpress-seo' );
$scoreLinksDofollow = __( "This page has %s outbound link(s).", 'wordpress-seo' );
$scoreLinksNofollow = __( "This page has %s outbound link(s), all nofollowed.", 'wordpress-seo' );
$scoreLinks = __( "This page has %s nofollowed link(s) and %s normal outbound link(s).", 'wordpress-seo' );
if ( $count['external']['nofollow'] == 0 && $count['external']['dofollow'] == 0 ) {
$this->save_score_result( $results, 6, $scoreNoLinks );
} else {
$found = false;
foreach ( $anchor_texts as $anchor_text ) {
if ( $this->strtolower_utf8( $anchor_text ) == $job["keyword_folded"] )
$found = true;
}
if ( $found )
$this->save_score_result( $results, 2, $scoreKeywordInOutboundLink );
if ( $count['external']['nofollow'] == 0 && $count['external']['dofollow'] > 0 ) {
$this->save_score_result( $results, 9, sprintf( $scoreLinksDofollow, $count['external']['dofollow'] ) );
} else if ( $count['external']['nofollow'] > 0 && $count['external']['dofollow'] == 0 ) {
$this->save_score_result( $results, 7, sprintf( $scoreLinksNofollow, $count['external']['nofollow'] ) );
} else {
$this->save_score_result( $results, 8, sprintf( $scoreLinks, $count['external']['nofollow'], $count['external']['dofollow'] ) );
}
}
}
/**
* Retrieve the anchor texts used in the current document.
*
* @param object $xpath An XPATH object of the current document.
* @return array
*/
function get_anchor_texts( &$xpath ) {
$query = "//a|//A";
$dom_objects = $xpath->query( $query );
$anchor_texts = array();
foreach ( $dom_objects as $dom_object ) {
if ( $dom_object->attributes->getNamedItem( 'href' ) ) {
$href = $dom_object->attributes->getNamedItem( 'href' )->textContent;
if ( substr( $href, 0, 4 ) == 'http' )
$anchor_texts['external'] = $dom_object->textContent;
}
}
unset( $dom_objects );
return $anchor_texts;
}
/**
* 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 )
);
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 ( substr( $href, 0, 1 ) == "/" || substr( $href, 0, strlen( $wpurl ) ) == $wpurl )
$type = "internal";
else if ( 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;
}
/**
* Check whether the images alt texts contain the keyword.
*
* @param array $job The job array holding both the keyword versions.
* @param array $results The results array.
* @param array $imgs The array with images alt texts.
*/
function score_images_alt_text( $job, &$results, $imgs ) {
$scoreImagesNoImages = __( "No images appear in this page, consider adding some as appropriate.", 'wordpress-seo' );
$scoreImagesNoAlt = __( "The images on this page are missing alt tags.", 'wordpress-seo' );
$scoreImagesAltKeywordIn = __( "The images on this page contain alt tags with the target keyword / phrase.", 'wordpress-seo' );
$scoreImagesAltKeywordMissing = __( "The images on this page do not have alt tags containing your keyword / phrase.", 'wordpress-seo' );
if ( $imgs['count'] == 0 ) {
$this->save_score_result( $results, 3, $scoreImagesNoImages );
} else if ( count( $imgs['alts'] ) == 0 && $imgs['count'] != 0 ) {
$this->save_score_result( $results, 5, $scoreImagesNoAlt );
} else {
$found = false;
foreach ( $imgs['alts'] as $alt ) {
$haystack1 = $this->strip_separators_and_fold( $alt, true );
$haystack2 = $this->strip_separators_and_fold( $alt, false );
if ( strrpos( $haystack1, $job["keyword_folded"] ) !== false )
$found = true;
else if ( strrpos( $haystack2, $job["keyword_folded"] ) !== false )
$found = true;
}
if ( $found )
$this->save_score_result( $results, 9, $scoreImagesAltKeywordIn );
else
$this->save_score_result( $results, 5, $scoreImagesAltKeywordMissing );
}
}
/**
* Retrieve the alt texts from the images.
*
* @param object $post The post to find images in.
* @param array $imgs The array holding the image information.
* @return array The updated images array.
*/
function get_images_alt_text( $post, $imgs ) {
preg_match_all( '/]+ alt=(["\'])([^\\1]+)\\1[^>]+>/im', $post->post_content, $matches );
$imgs['alts'] = array();
foreach ( $matches[2] as $alt ) {
$imgs['alts'][] = $this->strtolower_utf8( $alt );
}
if ( preg_match_all( '/\[gallery/', $post->post_content, $matches ) ) {
$attachments = get_children( array( 'post_parent' => $post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'fields' => 'ids' ) );
foreach ( $attachments as $att_id ) {
$alt = get_post_meta( $att_id, '_wp_attachment_image_alt', true );
if ( $alt && !empty( $alt ) )
$imgs['alts'][] = $alt;
$imgs['count']++;
}
}
return $imgs;
}
/**
* Use XPATH to count the number of images.
*
* @param object $xpath An XPATH object of the document
* @return int Image count
*/
function get_image_count( &$xpath ) {
$query = "//img|//IMG";
$dom_objects = $xpath->query( $query );
return count( $dom_objects );
}
/**
* Score the headings for keyword appearance.
*
* @param array $job The array holding the keywords.
* @param array $results The results array.
* @param array $headings The headings found in the document.
*/
function score_headings( $job, &$results, $headings ) {
$scoreHeadingsNone = __( "No subheading tags (like an H2) appear in the copy.", 'wordpress-seo' );
$scoreHeadingsKeywordIn = __( "Keyword / keyphrase appears in %s (out of %s) subheadings in the copy. While not a major ranking factor, this is beneficial.", 'wordpress-seo' );
$scoreHeadingsKeywordMissing = __( "You have not used your keyword / keyphrase in any subheading (such as an H2) in your copy.", 'wordpress-seo' );
$headingCount = count( $headings );
if ( $headingCount == 0 )
$this->save_score_result( $results, 7, $scoreHeadingsNone );
else {
$found = 0;
foreach ( $headings as $heading ) {
$haystack1 = $this->strip_separators_and_fold( $heading, true );
$haystack2 = $this->strip_separators_and_fold( $heading, false );
if ( strrpos( $haystack1, $job["keyword_folded"] ) !== false )
$found++;
else if ( strrpos( $haystack2, $job["keyword_folded"] ) !== false )
$found++;
}
if ( $found )
$this->save_score_result( $results, 9, sprintf( $scoreHeadingsKeywordIn, $found, $headingCount ) );
else
$this->save_score_result( $results, 3, $scoreHeadingsKeywordMissing );
}
}
/**
* Fetch all headings and return their content.
*
* @param string $postcontent Post content to find headings in.
* @return array Array of heading texts.
*/
function get_headings( $postcontent ) {
preg_match_all( '/]+)?>(.*)?<\/h\\1>/i', $postcontent, $matches );
$headings = array();
foreach ( $matches[3] as $heading ) {
$headings[] = $this->strtolower_utf8( $heading );
}
return $headings;
}
/**
* Score the meta description for length and keyword appearance.
*
* @param array $job The array holding the keywords.
* @param array $results The results array.
* @param string $description The meta description.
* @param object $statistics Object of class Yoast_TextStatistics used to calculate lengths.
* @param int $maxlength The maximum length of the meta description.
*/
function score_description( $job, &$results, $description, $statistics, $maxlength = 155 ) {
$scoreDescriptionMinLength = 120;
$scoreDescriptionCorrectLength = __( "In the specified meta description, consider: How does it compare to the competition? Could it be made more appealing?", 'wordpress-seo' );
$scoreDescriptionTooShort = __( "The meta description is under 120 characters, however up to %s characters are available. %s", 'wordpress-seo' );
$scoreDescriptionTooLong = __( "The specified meta description is over %s characters, reducing it will ensure the entire description is visible. %s", 'wordpress-seo' );
$scoreDescriptionMissing = __( "No meta description has been specified, search engines will display copy from the page instead.", 'wordpress-seo' );
$scoreDescriptionKeywordIn = __( "The meta description contains the primary keyword / phrase.", 'wordpress-seo' );
$scoreDescriptionKeywordMissing = __( "A meta description has been specified, but it does not contain the target keyword / phrase.", 'wordpress-seo' );
$metaShorter = '';
if ( $maxlength != 155 )
$metaShorter = __( "The available space is shorter than the usual 155 characters because Google will also include the publication date in the snippet.", 'wordpress-seo' );
if ( $description == "" ) {
$this->save_score_result( $results, 1, $scoreDescriptionMissing );
} else {
$length = $statistics->text_length( $description );
if ( $length < $scoreDescriptionMinLength )
$this->save_score_result( $results, 6, sprintf( $scoreDescriptionTooShort, $maxlength, $metaShorter ) );
else if ( $length <= $maxlength )
$this->save_score_result( $results, 9, $scoreDescriptionCorrectLength );
else
$this->save_score_result( $results, 6, sprintf( $scoreDescriptionTooLong, $maxlength, $metaShorter ) );
// TODO MA Keyword/Title matching is exact match with separators removed, but should extend to distributed match
$haystack1 = $this->strip_separators_and_fold( $description, true );
$haystack2 = $this->strip_separators_and_fold( $description, false );
if ( strrpos( $haystack1, $job["keyword_folded"] ) === false && strrpos( $haystack2, $job["keyword_folded"] ) === false )
$this->save_score_result( $results, 3, $scoreDescriptionKeywordMissing );
else
$this->save_score_result( $results, 9, $scoreDescriptionKeywordIn );
}
}
/**
* Score the body for length and keyword appearance.
*
* @param array $job The array holding the keywords.
* @param array $results The results array.
* @param string $body The body.
* @param string $firstp The first paragraph.
* @param object $statistics Object of class Yoast_TextStatistics used to calculate lengths.
*/
function score_body( $job, &$results, $body, $firstp, $statistics ) {
$scoreBodyGoodLimit = 300;
$scoreBodyOKLimit = 250;
$scoreBodyPoorLimit = 200;
$scoreBodyBadLimit = 100;
$scoreBodyGoodLength = __( "There are %d words contained in the body copy, this is greater than the 300 word recommended minimum.", 'wordpress-seo' );
$scoreBodyPoorLength = __( "There are %d words contained in the body copy, this is below the 300 word recommended minimum. Add more useful content on this topic for readers.", 'wordpress-seo' );
$scoreBodyOKLength = __( "There are %d words contained in the body copy, this is slightly below the 300 word recommended minimum, add a bit more copy.", 'wordpress-seo' );
$scoreBodyBadLength = __( "There are %d words contained in the body copy. This is far too low and should be increased.", 'wordpress-seo' );
$scoreKeywordDensityLow = __( "The keyword density is %s%%, which is a bit low, the keyword was found %s times.", 'wordpress-seo' );
$scoreKeywordDensityHigh = __( "The keyword density is %s%%, which is over the advised 4.5%% maximum, the keyword was found %s times.", 'wordpress-seo' );
$scoreKeywordDensityGood = __( "The keyword density is %s%%, which is great, the keyword was found %s times.", 'wordpress-seo' );
$scoreFirstParagraphLow = __( "The keyword doesn't appear in the first paragraph of the copy, make sure the topic is clear immediately.", 'wordpress-seo' );
$scoreFirstParagraphHigh = __( "The keyword appears in the first paragraph of the copy.", 'wordpress-seo' );
$fleschurl = '' . __( 'Flesch Reading Ease', 'wordpress-seo' ) . '';
$scoreFlesch = __( "The copy scores %s in the %s test, which is considered %s to read. %s", 'wordpress-seo' );
// Replace images with their alt tags, then strip all tags
$body = preg_replace( '/(]+)?alt="([^"]+)"([^>]+)>)/', '$3', $body );
$body = strip_tags( $body );
// Copy length check
$wordCount = $statistics->word_count( $body );
if ( $wordCount < $scoreBodyBadLimit )
$this->save_score_result( $results, -20, sprintf( $scoreBodyBadLength, $wordCount ) );
else if ( $wordCount < $scoreBodyPoorLimit )
$this->save_score_result( $results, -10, sprintf( $scoreBodyPoorLength, $wordCount ) );
else if ( $wordCount < $scoreBodyOKLimit )
$this->save_score_result( $results, 5, sprintf( $scoreBodyPoorLength, $wordCount ) );
else if ( $wordCount < $scoreBodyGoodLimit )
$this->save_score_result( $results, 7, sprintf( $scoreBodyOKLength, $wordCount ) );
else
$this->save_score_result( $results, 9, sprintf( $scoreBodyGoodLength, $wordCount ) );
$body = $this->strtolower_utf8( $body );
// Keyword Density check
$keywordDensity = 0;
if ( $wordCount > 0 ) {
$keywordCount = preg_match_all( "/" . preg_quote( $job["keyword"], '/' ) . "/msiU", $body, $res );
$keywordWordCount = str_word_count( $job["keyword"] );
if ( $keywordCount > 0 && $keywordWordCount > 0 )
$keywordDensity = number_format( ( ( $keywordCount / ( $wordCount - ( ( $keywordWordCount - 1 ) * $keywordWordCount ) ) ) * 100 ), 2 );
if ( $keywordDensity < 1 ) {
$this->save_score_result( $results, 4, sprintf( $scoreKeywordDensityLow, $keywordDensity, $keywordCount ) );
} else if ( $keywordDensity > 4.5 ) {
$this->save_score_result( $results, -50, sprintf( $scoreKeywordDensityHigh, $keywordDensity, $keywordCount ) );
} else {
$this->save_score_result( $results, 9, sprintf( $scoreKeywordDensityGood, $keywordDensity, $keywordCount ) );
}
}
$firstp = $this->strtolower_utf8( $firstp );
// First Paragraph Test
if ( stripos( $firstp, $job["keyword"] ) === false && stripos( $firstp, $job["keyword_folded"] ) === false ) {
$this->save_score_result( $results, 3, $scoreFirstParagraphLow );
} else {
$this->save_score_result( $results, 9, $scoreFirstParagraphHigh );
}
$lang = get_bloginfo( 'language' );
if ( substr( $lang, 0, 2 ) == 'en' && $wordCount > 100 ) {
// Flesch Reading Ease check
$flesch = $statistics->flesch_kincaid_reading_ease( $body );
$note = '';
$level = '';
$score = 1;
if ( $flesch >= 90 ) {
$level = __( 'very easy', 'wordpress-seo' );
$score = 9;
} else if ( $flesch >= 80 ) {
$level = __( 'easy', 'wordpress-seo' );
$score = 9;
} else if ( $flesch >= 70 ) {
$level = __( 'fairly easy', 'wordpress-seo' );
$score = 8;
} else if ( $flesch >= 60 ) {
$level = __( 'OK', 'wordpress-seo' );
$score = 7;
} else if ( $flesch >= 50 ) {
$level = __( 'fairly difficult', 'wordpress-seo' );
$note = __( 'Try to make shorter sentences to improve readability.', 'wordpress-seo' );
$score = 6;
} else if ( $flesch >= 30 ) {
$level = __( 'difficult', 'wordpress-seo' );
$note = __( 'Try to make shorter sentences, using less difficult words to improve readability.', 'wordpress-seo' );
$score = 5;
} else if ( $flesch >= 0 ) {
$level = __( 'very difficult', 'wordpress-seo' );
$note = __( 'Try to make shorter sentences, using less difficult words to improve readability.', 'wordpress-seo' );
$score = 4;
}
$this->save_score_result( $results, $score, sprintf( $scoreFlesch, $flesch, $fleschurl, $level, $note ) );
}
}
/**
* Retrieve the body from the post.
*
* @param object $post The post object.
* @return string The post content.
*/
function get_body( $post ) {
// Strip shortcodes, for obvious reasons
$origHtml = wpseo_strip_shortcode( $post->post_content );
if ( trim( $origHtml ) == '' )
return '';
$htmdata2 = preg_replace( "/\n|\r/", " ", $origHtml );
if ( $htmdata2 == null )
$htmdata2 = $origHtml;
else
unset( $origHtml );
$htmdata3 = preg_replace( "/<(\x20*script|script).*?(\/>|\/script>)/", "", $htmdata2 );
if ( $htmdata3 == null )
$htmdata3 = $htmdata2;
else
unset( $htmdata2 );
$htmdata4 = preg_replace( "//", "", $htmdata3 );
if ( $htmdata4 == null )
$htmdata4 = $htmdata3;
else
unset( $htmdata3 );
$htmdata5 = preg_replace( "/<(\x20*style|style).*?(\/>|\/style>)/", "", $htmdata4 );
if ( $htmdata5 == null )
$htmdata5 = $htmdata4;
else
unset( $htmdata4 );
return $htmdata5;
}
/**
* Retrieve the first paragraph from the post.
*
* @param object $post The post to retrieve the first paragraph from.
* @return string
*/
function get_first_paragraph( $post ) {
// To determine the first paragraph we first need to autop the content, then match the first paragraph and return.
$res = preg_match( '/
(.*)<\/p>/', wpautop( $post->post_content ), $matches );
if ( $res )
return $matches[1];
return false;
}
}
$wpseo_metabox = new WPSEO_Metabox();