PageRenderTime 52ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/plugins/seo-ultimate/modules/canonical/canonical.php

https://gitlab.com/Gashler/sg
PHP | 237 lines | 159 code | 46 blank | 32 comment | 45 complexity | 38d609b19228c0e963fe1585f7a08649 MD5 | raw file
  1. <?php
  2. /**
  3. * Canonicalizer Module
  4. *
  5. * @since 0.3
  6. */
  7. if (class_exists('SU_Module')) {
  8. class SU_Canonical extends SU_Module {
  9. static function get_module_title() { return __('Canonicalizer', 'seo-ultimate'); }
  10. static function get_parent_module() { return 'misc'; }
  11. function get_settings_key() { return 'canonical'; }
  12. function init() {
  13. add_filter('su_get_setting-canonical-canonical_url_scheme', array(&$this, 'filter_canonical_url_scheme'));
  14. //If the canonical tags are enabled, then...
  15. if ($this->get_setting('link_rel_canonical')) {
  16. //...remove WordPress's default canonical tags (since they only handle posts/pages/attachments)
  17. remove_action('wp_head', 'rel_canonical');
  18. //...and add our custom canonical tags.
  19. add_action('su_head', array(&$this, 'link_rel_canonical_tag'));
  20. }
  21. if ($this->get_setting('http_link_rel_canonical'))
  22. add_action('template_redirect', array(&$this, 'http_link_rel_canonical'), 11, 0);
  23. //Should we remove nonexistent pagination?
  24. if ($this->get_setting('remove_nonexistent_pagination'))
  25. add_action('template_redirect', array(&$this, 'remove_nonexistent_pagination'), 11);
  26. }
  27. function admin_page_contents() {
  28. $this->child_admin_form_start();
  29. $this->checkboxes(array(
  30. 'link_rel_canonical' => __('Generate <code>&lt;link rel=&quot;canonical&quot; /&gt;</code> meta tags', 'seo-ultimate')
  31. , 'http_link_rel_canonical' => __('Send <code>rel=&quot;canonical&quot;</code> HTTP headers', 'seo-ultimate')
  32. ), __('Canonical URL Generation', 'seo-ultimate'));
  33. $this->radiobuttons('canonical_url_scheme', array(
  34. '' => __('Use <code>http://</code> or <code>https://</code> depending on how the visitor accessed the page', 'seo-ultimate')
  35. , 'http' => __('Make all canonical URLs begin with <code>http://</code>', 'seo-ultimate')
  36. , 'https' => __('Make all canonical URLs begin with <code>https://</code>', 'seo-ultimate')
  37. ), __('Canonical URL Scheme', 'seo-ultimate'));
  38. $this->checkboxes(array(
  39. 'remove_nonexistent_pagination' => __('Redirect requests for nonexistent pagination', 'seo-ultimate')
  40. ), __('Automated 301 Redirects', 'seo-ultimate'));
  41. $this->child_admin_form_end();
  42. }
  43. function link_rel_canonical_tag() {
  44. //Display the canonical tag if a canonical URL is available
  45. if ($url = $this->get_canonical_url()) {
  46. $url = su_esc_attr($url);
  47. echo "\t<link rel=\"canonical\" href=\"$url\" />\n";
  48. }
  49. }
  50. function http_link_rel_canonical() {
  51. if (headers_sent())
  52. return;
  53. if ($url = $this->get_canonical_url()) {
  54. $url = su_esc_attr($url);
  55. header("Link: <$url>; rel=\"canonical\"", false);
  56. }
  57. }
  58. /**
  59. * Returns the canonical URL to put in the link-rel-canonical tag.
  60. *
  61. * This function is modified from the GPL-licensed {@link http://wordpress.org/extend/plugins/canonical/ Canonical URLs} plugin,
  62. * which in turn was heavily based on the {@link http://svn.fucoder.com/fucoder/permalink-redirect/ Permalink Redirect} plugin.
  63. */
  64. function get_canonical_url() {
  65. global $wp_query, $wp_rewrite;
  66. //404s and search results don't have canonical URLs
  67. if ($wp_query->is_404 || $wp_query->is_search) return false;
  68. //Are there posts in the current Loop?
  69. $haspost = count($wp_query->posts) > 0;
  70. //Handling special case with '?m=yyyymmddHHMMSS'.
  71. if (get_query_var('m')) {
  72. $m = preg_replace('/[^0-9]/', '', get_query_var('m'));
  73. switch (strlen($m)) {
  74. case 4: // Yearly
  75. $link = get_year_link($m);
  76. break;
  77. case 6: // Monthly
  78. $link = get_month_link(substr($m, 0, 4), substr($m, 4, 2));
  79. break;
  80. case 8: // Daily
  81. $link = get_day_link(substr($m, 0, 4), substr($m, 4, 2),
  82. substr($m, 6, 2));
  83. break;
  84. default:
  85. //Since there is no code for producing canonical archive links for is_time, we will give up and not try to produce a link.
  86. return false;
  87. }
  88. //Posts and pages
  89. } elseif (($wp_query->is_single || $wp_query->is_page) && $haspost) {
  90. $post = $wp_query->posts[0];
  91. $link = get_permalink($post->ID);
  92. if (is_front_page()) $link = trailingslashit($link);
  93. //Author archives
  94. } elseif ($wp_query->is_author && $haspost) {
  95. $author = get_userdata(get_query_var('author'));
  96. if ($author === false) return false;
  97. $link = get_author_posts_url($author->ID, $author->user_nicename);
  98. //Category archives
  99. } elseif ($wp_query->is_category && $haspost) {
  100. $link = get_category_link(get_query_var('cat'));
  101. //Tag archives
  102. } else if ($wp_query->is_tag && $haspost) {
  103. $tag = get_term_by('slug',get_query_var('tag'),'post_tag');
  104. if (!empty($tag->term_id)) $link = get_tag_link($tag->term_id);
  105. //Day archives
  106. } elseif ($wp_query->is_day && $haspost) {
  107. $link = get_day_link(get_query_var('year'),
  108. get_query_var('monthnum'),
  109. get_query_var('day'));
  110. //Month archives
  111. } elseif ($wp_query->is_month && $haspost) {
  112. $link = get_month_link(get_query_var('year'),
  113. get_query_var('monthnum'));
  114. //Year archives
  115. } elseif ($wp_query->is_year && $haspost) {
  116. $link = get_year_link(get_query_var('year'));
  117. //Homepage
  118. } elseif ($wp_query->is_home) {
  119. if ((get_option('show_on_front') == 'page') && ($pageid = get_option('page_for_posts')))
  120. $link = trailingslashit(get_permalink($pageid));
  121. else
  122. $link = trailingslashit(get_option('home'));
  123. //Other
  124. } else
  125. return false;
  126. //Handle pagination
  127. $page = get_query_var('paged');
  128. if ($page && $page > 1) {
  129. if ($wp_rewrite->using_permalinks()) {
  130. $link = trailingslashit($link) ."page/$page";
  131. $link = user_trailingslashit($link, 'paged');
  132. } else {
  133. $link = add_query_arg( 'paged', $page, $link );
  134. }
  135. }
  136. //Handle protocol change
  137. if ($scheme = $this->get_setting('canonical_url_scheme', 'http'))
  138. $link = preg_replace('@^https?://@', "$scheme://", $link);
  139. //Return the canonical URL
  140. return $link;
  141. }
  142. function remove_nonexistent_pagination() {
  143. if (!is_admin()) {
  144. global $wp_rewrite, $wp_query;
  145. $url = suurl::current();
  146. if (is_singular()) {
  147. $num = absint(get_query_var('page'));
  148. $post = $wp_query->get_queried_object();
  149. $max = count(explode('<!--nextpage-->', $post->post_content));
  150. if ($max > 0 && ($num == 1 || ($num > 1 && $num > $max))) {
  151. if ($wp_rewrite->using_permalinks())
  152. wp_redirect(preg_replace('|/[0-9]{1,9}/?$|', '/', $url), 301);
  153. else
  154. wp_redirect(remove_query_arg('page', $url), 301);
  155. }
  156. } elseif (is_404() && $num = absint(get_query_var('paged'))) {
  157. if ($wp_rewrite->using_permalinks())
  158. wp_redirect(preg_replace('|/page/[0-9]{1,9}/?$|', '/', $url), 301);
  159. else
  160. wp_redirect(remove_query_arg('paged', $url), 301);
  161. }
  162. }
  163. }
  164. function filter_canonical_url_scheme($scheme) {
  165. return sustr::preg_filter('a-z', $scheme);
  166. }
  167. function add_help_tabs($screen) {
  168. $overview = __("
  169. <ul>
  170. <li><strong>What it does:</strong> Canonicalizer will point Google to the correct URL for your homepage and each of your posts, Pages, categories, tags, date archives, and author archives.</li>
  171. <li><strong>Why it helps:</strong> If Google comes across an alternate URL by which one of those items can be accessed, it will be able to find the correct URL and won&#8217;t penalize you for having two identical pages on your site.</li>
  172. <li><strong>How to use it:</strong> Just check the three checkboxes. If your site is accessible using both <code>http://</code> and <code>https://</code>, be sure to set the preferred one under &#8220;Canonical URL Scheme.&#8221;</li>
  173. </ul>
  174. ", 'seo-ultimate');
  175. if ($this->has_enabled_parent()) {
  176. $screen->add_help_tab(array(
  177. 'id' => 'su-canonical-help'
  178. , 'title' => __('Canonicalizer', 'seo-ultimate')
  179. , 'content' =>
  180. '<h3>' . __('Overview', 'seo-ultimate') . '</h3>' . $overview
  181. ));
  182. } else {
  183. $screen->add_help_tab(array(
  184. 'id' => 'su-canonical-overview'
  185. , 'title' => __('Overview', 'seo-ultimate')
  186. , 'content' => $overview));
  187. }
  188. }
  189. }
  190. }
  191. ?>