/** * Replaces the text to be used in the title element, if a replacement text has been set. */ function amt_custom_title_tag($title) { if (is_feed() || is_search() || is_404()) { return $title; } // Get the options $options = get_option('add_meta_tags_opts'); // Get current post object $post = get_queried_object(); $processed_title = amt_get_title_for_title_element($options, $post); if (!empty($processed_title)) { $processed_title = str_replace('%title%', $title, $processed_title); return esc_attr($processed_title); } // WordPress adds multipage information if a custom title is not set. return $title; }
function amt_metadata_analysis($default_text, $metadata_block_head, $metadata_block_footer, $metadata_block_content_filter) { // Analysis is appended only o content pages if (!is_singular()) { return $default_text; } // Check the filter based switch if (!apply_filters('amt_metadata_analysis_enable', true)) { return $default_text; } // // Collect data // $options = amt_get_options(); $post = amt_get_queried_object(); if (!isset($post->ID) || $post->ID <= 0) { return $default_text; } // Content and stats // Post content $post_content = strtolower(amt_get_clean_post_content($options, $post)); $post_content_length = strlen($post_content); //var_dump($post_content); // Total words if (function_exists('wordstats_words')) { $post_word_count = wordstats_words($post_content); // provided by the word-statistics-plugin by FD } else { $post_word_count = str_word_count($post_content); } // Total sentences if (function_exists('wordstats_sentences')) { $post_sentence_count = wordstats_sentences($post_content); // provided by the word-statistics-plugin by FD } else { $post_sentence_count = preg_match_all('/[.!?\\r]/', $post_content, $dummy); } // Total syllables // TODO: Find better function $post_syllable_count = preg_match_all('/[aeiouy]/', $post_content, $dummy); // Titles // Original $post_title = strtolower(strip_tags(get_the_title($post->ID))); // Title HTML element if ($options['enable_advanced_title_management'] == '1') { // If Advanced Title management is enabled, use this directly: $post_title_html_element = strtolower(amt_get_title_for_title_element($options, $post)); } else { if (version_compare(get_bloginfo('version'), '4.4', '>=')) { // Since WP 4.4 // - https://make.wordpress.org/core/2015/10/20/document-title-in-4-4/ //$post_title_html_element = strtolower( apply_filters('document_title_parts', array('title' => $post_title) ) ); //$post_title_html_element = wp_get_document_title(); $post_title_html_element = strtolower(get_wp_title_rss()); } else { // Reverting back to the one argument version of the fitlering function. //$post_title_html_element = strtolower( apply_filters('wp_title', $post_title) ); // Until testing is performed on old WP versions we just use post title $post_title_html_element = $post_title; } } //var_dump($post_title_html_element); // Title in metadata $post_title_metadata = strtolower(amt_get_title_for_metadata($options, $post)); //var_dump($post_title_metadata); // URL $post_url = str_replace(get_bloginfo('url'), '', amt_get_permalink_for_multipage($post)); //var_dump($post_url); // Description $description = ''; if (array_key_exists('basic:description', $metadata_block_head)) { $description = strtolower(preg_replace('#^.*content="([^"]+)".*$#', '$1', $metadata_block_head['basic:description'])); } //var_dump($description); // Keywords $keywords = array(); if (array_key_exists('basic:keywords', $metadata_block_head)) { $keywords_content = strtolower(preg_replace('#^.*content="([^"]+)".*$#', '$1', $metadata_block_head['basic:keywords'])); $keywords = explode(',', str_replace(', ', ',', $keywords_content)); } //var_dump($keywords); // Keyword matching pattern //$keyword_matching_pattern = '#(?:%s)#'; //$keyword_matching_pattern = '#(?:%s)[[:^alpha:]]#'; //$keyword_matching_pattern = '#(?:%s)[[:^alpha:]]?#'; $keyword_matching_pattern = '#(?:%s)(?:[[:^alpha:]]|$)#'; $keyword_matching_pattern = apply_filters('amt_metadata_analysis_keyword_matching_pattern', $keyword_matching_pattern); // Whether to use topic keywords field or the keywords from the 'keywords' meta tag. $use_keywords = false; // First check for a field that contains topic keywords. $topic_keywords_field_name = apply_filters('amt_metadata_analysis_topic_keywords_field', 'topic_keywords'); $topic_keywords_field_value = get_post_meta($post->ID, $topic_keywords_field_name, true); if (!empty($topic_keywords_field_value)) { $topic_keywords = explode(',', str_replace(', ', ',', $topic_keywords_field_value)); } else { $topic_keywords = $keywords; $use_keywords = true; //var_dump($topic_keywords); } $BR = PHP_EOL; if ($options['review_mode_omit_notices'] == '0') { $output = $default_text . $BR . $BR; //$output .= $BR . '<span class="">Text analysis</span>' . $BR; } else { $output = ''; } if ($options['review_mode_omit_notices'] == '0') { $output .= 'Metadata Overview' . $BR; $output .= '=================' . $BR; //$output .= 'This overview has been generated by the Add-Meta-Tags plugin for statistical and' . $BR; //$output .= 'informational purposes only. Please do not modify or base your work upon this report.' . $BR . $BR; $output .= 'NOTICE: Add-Meta-Tags does not provide SEO advice and does not rate your content.' . $BR; $output .= 'This <a target="_blank" href="http://www.codetrax.org/projects/wp-add-meta-tags/wiki/Metadata_Overview">overview</a> has been generated for statistical and informational purposes only.' . $BR . $BR; //$output .= '<a target="_blank" href="http://www.codetrax.org/projects/wp-add-meta-tags/wiki/FAQ#Is-Add-Meta-Tags-an-SEO-plugin">Read more</a> about the mentality upon which the development of this plugin has been based.' . $BR; //$output .= 'Please use this statistical information to identify keyword overstuffing' . $BR; //$output .= 'and stay away from following any patterns or being bound by the numbers.' . $BR . $BR; } if ($use_keywords) { $output .= sprintf('This overview has been based on post keywords, because the Custom Field \'<em>%s</em>\' could not be found.', $topic_keywords_field_name) . $BR; } else { $output .= sprintf('This overview has been based on <em>topic keywords</em> retrieved from the Custom Field \'<em>%s</em>\'.', $topic_keywords_field_name) . $BR; } if ($options['review_mode_omit_notices'] == '0') { $output .= 'Keyword Analysis' . $BR; $output .= '----------------' . $BR; } else { $output .= $BR; } $output .= '<table class="amt-ht-table">'; $output .= '<tr> <th>Topic Keyword</th> <th>Content</th> <th>Description</th> <th>Keywords</th> <th>Post Title</th> <th>HTML title</th> <th>Metadata titles</th> <th>Post URL</th> </tr>'; foreach ($topic_keywords as $topic_keyword) { $output .= sprintf('<tr> <td>%s</td>', $topic_keyword); $is_into = array(); // Check content $is_into['content'] = ''; $occurrences = preg_match_all(sprintf($keyword_matching_pattern, $topic_keyword), $post_content, $matches); if ($occurrences) { // Only for content $topic_keyword_desnity = round($occurrences / $post_word_count * 100, 2); $output .= sprintf(' <td>%d (%.2f%%)</td>', $occurrences, $topic_keyword_desnity); } else { $output .= '<td> </td>'; } // Check description $occurrences = preg_match_all(sprintf($keyword_matching_pattern, $topic_keyword), $description, $matches); if ($occurrences) { $output .= sprintf(' <td>%d</td>', $occurrences); } else { $output .= '<td> </td>'; } // Check keywords if ($use_keywords) { $output .= '<td>N/A</td>'; $is_into['keywords'] = 'N/A'; } elseif (in_array($topic_keyword, $keywords)) { // Always 1 $output .= '<td>1</td>'; } else { $output .= '<td> </td>'; } // Check original title $occurrences = preg_match_all(sprintf($keyword_matching_pattern, $topic_keyword), $post_title, $matches); if ($occurrences) { $output .= sprintf(' <td>%d</td>', $occurrences); } else { $output .= '<td> </td>'; } // Check title element $occurrences = preg_match_all(sprintf($keyword_matching_pattern, $topic_keyword), $post_title_html_element, $matches); if ($occurrences) { $output .= sprintf(' <td>%d</td>', $occurrences); } else { $output .= '<td> </td>'; } // Check metadata titles $occurrences = preg_match_all(sprintf($keyword_matching_pattern, $topic_keyword), $post_title_metadata, $matches); if ($occurrences) { $output .= sprintf(' <td>%d</td>', $occurrences); } else { $output .= '<td> </td>'; } // Check post URL $occurrences = preg_match_all(sprintf($keyword_matching_pattern, $topic_keyword), $post_url, $matches); if ($occurrences) { $output .= sprintf(' <td>%d</td>', $occurrences); } else { $output .= '<td> </td>'; } // Close row $output .= ' </tr>' . $BR; } $output .= '</table>' . $BR; // Topic Keywords Distribution Graph if ($options['review_mode_omit_notices'] == '0') { $output .= 'Topic Keywords Distribution Graph' . $BR; $output .= '---------------------------------' . $BR; $output .= 'The following text based graph shows how the <em>topic keywords</em> are distributed within your content.' . $BR; $output .= 'You can use it to identify incidents of keyword overstuffing.' . $BR . $BR; } //$output .= $BR . $BR; $total_bars = 39; // zero based $step = $post_content_length / $total_bars; // Debug //$output .= $BR . $post_content_length . ' ' . $step . $BR; $max_weight = null; $weights = array(); // Reset weights for ($x = 0; $x <= $total_bars; $x++) { $weights[$x] = 1; } foreach ($topic_keywords as $topic_keyword) { // Use preg_match_all with PREG_OFFSET_CAPTURE -- http://php.net/manual/en/function.preg-match-all.php $topic_keyword_occurrences = preg_match_all(sprintf($keyword_matching_pattern, $topic_keyword), $post_content, $matches, PREG_OFFSET_CAPTURE); //var_dump($matches); if (!empty($topic_keyword_occurrences)) { foreach ($matches[0] as $match) { $pos = $match[1]; $step_index = absint($pos / $step); $weights[$step_index] = $weights[$step_index] + 1; if ($weights[$step_index] > $max_weight) { $max_weight = $weights[$step_index]; } // Debug //$output .= sprintf('kw: %s, pos: %s, step index: %s, step weight: %s', $topic_keyword, $pos, $step_index, $weights[$step_index]) . $BR; } } } //var_dump($weights); for ($x = $max_weight - 1; $x >= 1; $x--) { $line = ''; for ($y = 0; $y <= $total_bars; $y++) { if ($weights[$y] > $x) { $line .= '#'; } else { $line .= ' '; } } $output .= $line . $BR; } // Currently this text based ruler is used as base line. $output .= str_repeat('---------+', ($total_bars + 1) / 10) . $BR; if ($options['review_mode_omit_notices'] == '0') { $output .= $BR . '<code>#</code>: indicates a single occurrence of a <em>topic keyword</em>.' . $BR; } else { $output .= $BR; } // Stats and scores by algos provided by the word-statistics-plugin by FD if (function_exists('wordstats_words')) { // Readability Tests if ($options['review_mode_omit_notices'] == '0') { $output .= $BR . 'Readability Scores and Text Statistics' . $BR; $output .= '--------------------------------------' . $BR; $output .= 'These readability scores and text statistics are based on algorithms provided by the <em>FD Word Statistics Plugin</em>.' . $BR . $BR; } if (function_exists('wordstats_words')) { $output .= sprintf(' ● Total words: <strong>%d</strong>', wordstats_words($post_content)) . $BR; } if (function_exists('wordstats_sentences')) { $output .= sprintf(' ● Total sentences: <strong>%d</strong>', wordstats_sentences($post_content)) . $BR; } if (function_exists('wordstats_flesch_kincaid')) { $output .= sprintf(' ● Flesch-Kincaid US grade level: <strong>%.1f</strong> <em>(For instance, a score of 9.3 means suitable for a 9th grade student in the US, <a target="_blank" href="%s">read more</a>.)</em>', wordstats_flesch_kincaid($post_content), 'https://en.wikipedia.org/wiki/Flesch%E2%80%93Kincaid_readability_tests#Flesch.E2.80.93Kincaid_grade_level') . $BR; } if (function_exists('wordstats_flesch')) { $output .= sprintf(' ● Flesch reading ease: <strong>%.1f%%</strong> <em>(avg 11 y.o. student: 90-100%%, 13-15 y.o. students: 60-70%%, university graduates: 0-30%%, <a target="_blank" href="%s">read more</a>.)</em>', wordstats_flesch($post_content), 'https://en.wikipedia.org/wiki/Flesch%E2%80%93Kincaid_readability_tests#Flesch_reading_ease') . $BR; } if (function_exists('wordstats_fog')) { $output .= sprintf(' ● Gunning fog index: <strong>%.1f</strong> <em>(wide audience: < 12, near universal understanding: < 8, <a target="_blank" href="%s">read more</a>.)</em>', wordstats_fog($post_content), 'https://en.wikipedia.org/wiki/Gunning_fog_index') . $BR; } $output .= $BR; } else { if ($options['review_mode_omit_notices'] == '0') { $output .= $BR . $BR . 'Note: There is experimental support for <em>FD Word Statistics Plugin</em>.'; $output .= $BR . 'If installed, you can get some readability scores and text statistics here.' . $BR . $BR; } } return $output; }