| 1 |
<?php |
| 2 |
/* |
| 3 |
* License: GPLv3 |
| 4 |
* License URI: https://www.gnu.org/licenses/gpl.txt |
| 5 |
* Copyright 2012-2026 Jean-Sebastien Morisset (https://wpsso.com/) |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
|
| 10 |
die( 'These aren\'t the droids you\'re looking for.' ); |
| 11 |
} |
| 12 |
|
| 13 |
if ( ! defined( 'WPSSO_PLUGINDIR' ) ) { |
| 14 |
|
| 15 |
die( 'Do. Or do not. There is no try.' ); |
| 16 |
} |
| 17 |
|
| 18 |
if ( ! class_exists( 'WpssoLinkRel' ) ) { |
| 19 |
|
| 20 |
class WpssoLinkRel { |
| 21 |
|
| 22 |
private $p; // Wpsso class object. |
| 23 |
|
| 24 |
public function __construct( &$plugin ) { |
| 25 |
|
| 26 |
$this->p =& $plugin; |
| 27 |
|
| 28 |
if ( $this->p->debug->enabled ) { |
| 29 |
|
| 30 |
$this->p->debug->mark(); |
| 31 |
} |
| 32 |
|
| 33 |
/* |
| 34 |
* Always disable the WordPress shortlink meta tag. |
| 35 |
*/ |
| 36 |
if ( $this->p->debug->enabled ) { |
| 37 |
|
| 38 |
$this->p->debug->log( 'removing default wp_shortlink_wp_head action' ); |
| 39 |
} |
| 40 |
|
| 41 |
remove_action( 'wp_head', 'wp_shortlink_wp_head' ); |
| 42 |
|
| 43 |
add_action( 'wp_head', array( $this, 'maybe_disable_rel_canonical' ), -1000 ); |
| 44 |
|
| 45 |
if ( ! empty( $this->p->avail[ 'amp' ][ 'any' ] ) ) { |
| 46 |
|
| 47 |
add_action( 'amp_post_template_head', array( $this, 'maybe_disable_rel_canonical' ), -1000 ); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
/* |
| 52 |
* Maybe disable the WordPress and AMP canonical tags. |
| 53 |
* |
| 54 |
* Called by 'wp_head' and 'amp_post_template_head' actions. |
| 55 |
*/ |
| 56 |
public function maybe_disable_rel_canonical() { |
| 57 |
|
| 58 |
if ( $this->p->debug->enabled ) { |
| 59 |
|
| 60 |
$this->p->debug->mark(); |
| 61 |
} |
| 62 |
|
| 63 |
/* |
| 64 |
* WpssoUtil->is_canonical_disabled() returns true if: |
| 65 |
* |
| 66 |
* - An SEO plugin is active. |
| 67 |
* - The 'add_link_rel_canonical' option is unchecked. |
| 68 |
* - The 'wpsso_add_link_rel_canonical' filter returns false. |
| 69 |
* - The 'wpsso_canonical_disabled' filter returns true. |
| 70 |
*/ |
| 71 |
if ( $this->p->util->is_canonical_disabled() ) { |
| 72 |
|
| 73 |
if ( $this->p->debug->enabled ) { |
| 74 |
|
| 75 |
$this->p->debug->log( 'exiting early: canonical is disabled' ); |
| 76 |
} |
| 77 |
|
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
/* |
| 82 |
* If WPSSO is providing the canonical URL, then disable the WordPress and AMP canonical meta tags. |
| 83 |
*/ |
| 84 |
$current = function_exists( 'current_action' ) ? current_action() : current_filter(); |
| 85 |
|
| 86 |
switch( $current ) { |
| 87 |
|
| 88 |
case 'wp_head': |
| 89 |
|
| 90 |
remove_filter( $current, 'rel_canonical' ); |
| 91 |
remove_action( $current, 'amp_frontend_add_canonical' ); |
| 92 |
|
| 93 |
break; |
| 94 |
|
| 95 |
case 'amp_post_template_head': |
| 96 |
|
| 97 |
remove_action( $current, 'amp_post_template_add_canonical' ); |
| 98 |
|
| 99 |
break; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
/* |
| 104 |
* Link Relation URL Tags. |
| 105 |
*/ |
| 106 |
public function get_array( array $mod, array $mt_og = array(), $author_id = false ) { |
| 107 |
|
| 108 |
if ( $this->p->debug->enabled ) { |
| 109 |
|
| 110 |
$this->p->debug->mark(); |
| 111 |
} |
| 112 |
|
| 113 |
$link_rel = apply_filters( 'wpsso_link_rel_seed', array(), $mod ); |
| 114 |
|
| 115 |
/* |
| 116 |
* Link rel canonical. |
| 117 |
* |
| 118 |
* WpssoUtil->is_canonical_disabled() returns true if: |
| 119 |
* |
| 120 |
* - An SEO plugin is active. |
| 121 |
* - The 'add_link_rel_canonical' option is unchecked. |
| 122 |
* - The 'wpsso_add_link_rel_canonical' filter returns false. |
| 123 |
* - The 'wpsso_canonical_disabled' filter returns true. |
| 124 |
*/ |
| 125 |
if ( ! $this->p->util->is_canonical_disabled() ) { |
| 126 |
|
| 127 |
$link_rel[ 'canonical' ] = $this->p->util->get_canonical_url( $mod ); |
| 128 |
|
| 129 |
} elseif ( $this->p->debug->enabled ) { |
| 130 |
|
| 131 |
$this->p->debug->log( 'skipping canonical: canonical is disabled' ); |
| 132 |
} |
| 133 |
|
| 134 |
/* |
| 135 |
* Link rel shortlink. |
| 136 |
* |
| 137 |
* WpssoUtil->is_shortlink_disabled() returns true if: |
| 138 |
* |
| 139 |
* - The 'add_link_rel_shortlink' option is unchecked. |
| 140 |
* - The 'wpsso_add_link_rel_shortlink' filter returns false. |
| 141 |
* - The 'wpsso_shortlink_disabled' filter returns true. |
| 142 |
*/ |
| 143 |
if ( ! $this->p->util->is_shortlink_disabled() ) { |
| 144 |
|
| 145 |
$canonical_url = $this->p->util->get_canonical_url( $mod ); |
| 146 |
|
| 147 |
$shortlink = ''; |
| 148 |
|
| 149 |
if ( $mod[ 'is_post' ] && $mod[ 'id' ] ) { |
| 150 |
|
| 151 |
$shortlink = $this->p->util->get_shortlink( $mod, $context = 'post' ); |
| 152 |
|
| 153 |
} elseif ( ! empty( $canonical_url ) ) { // Just in case. |
| 154 |
|
| 155 |
/* |
| 156 |
* Shorten URL using the selected shortening service. |
| 157 |
*/ |
| 158 |
if ( $this->p->debug->enabled ) { |
| 159 |
|
| 160 |
$this->p->debug->log( 'calling WpssoUtil->shorten_url() to shorten the canonical URL' ); |
| 161 |
} |
| 162 |
|
| 163 |
$shortlink = $this->p->util->shorten_url( $canonical_url, $mod ); |
| 164 |
} |
| 165 |
|
| 166 |
if ( empty( $shortlink ) ) { |
| 167 |
|
| 168 |
if ( $this->p->debug->enabled ) { |
| 169 |
|
| 170 |
$this->p->debug->log( 'skipping shortlink: short url is empty' ); |
| 171 |
} |
| 172 |
|
| 173 |
} elseif ( $shortlink === $canonical_url ) { |
| 174 |
|
| 175 |
if ( $this->p->debug->enabled ) { |
| 176 |
|
| 177 |
$this->p->debug->log( 'skipping shortlink: short url is identical to canonical url' ); |
| 178 |
} |
| 179 |
|
| 180 |
} else { |
| 181 |
|
| 182 |
$link_rel[ 'shortlink' ] = $shortlink; |
| 183 |
} |
| 184 |
|
| 185 |
} elseif ( $this->p->debug->enabled ) { |
| 186 |
|
| 187 |
$this->p->debug->log( 'skipping shortlink: shortlink is disabled' ); |
| 188 |
} |
| 189 |
|
| 190 |
return apply_filters( 'wpsso_link_rel', $link_rel, $mod ); |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
|