| 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 |
/* |
| 14 |
* Integration module for the Rank Math SEO plugin. |
| 15 |
* |
| 16 |
* See https://wordpress.org/plugins/seo-by-rank-math/. |
| 17 |
*/ |
| 18 |
if ( ! class_exists( 'WpssoIntegSeoRankmath' ) ) { |
| 19 |
|
| 20 |
class WpssoIntegSeoRankmath { |
| 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 |
$this->p->util->add_plugin_filters( $this, array( |
| 34 |
'primary_term_id' => 4, |
| 35 |
'primary_terms' => 3, |
| 36 |
'title_seed' => 5, |
| 37 |
'description_seed' => 4, |
| 38 |
'post_url' => 2, |
| 39 |
'term_url' => 2, |
| 40 |
), 100 ); |
| 41 |
|
| 42 |
if ( is_admin() ) { |
| 43 |
|
| 44 |
$this->p->util->add_plugin_filters( $this, array( 'admin_page_style_css' => 1 ), 100 ); |
| 45 |
|
| 46 |
} else { |
| 47 |
|
| 48 |
add_action( 'rank_math/head', array( $this, 'cleanup_rankmath_actions' ), -2000, 0 ); |
| 49 |
|
| 50 |
add_filter( 'rank_math/json_ld', array( $this, 'cleanup_rankmath_json_ld' ), PHP_INT_MAX, 1 ); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
public function filter_primary_term_id( $primary_term_id, $mod, $tax_slug, $is_custom ) { |
| 55 |
|
| 56 |
if ( $this->p->debug->enabled ) { |
| 57 |
|
| 58 |
$this->p->debug->mark(); |
| 59 |
} |
| 60 |
|
| 61 |
if ( $is_custom ) { |
| 62 |
|
| 63 |
if ( $this->p->debug->enabled ) { |
| 64 |
|
| 65 |
$this->p->debug->log( 'exiting early: $is_custom is true' ); |
| 66 |
} |
| 67 |
|
| 68 |
return $primary_term_id; |
| 69 |
|
| 70 |
} elseif ( empty( $mod[ 'is_post' ] ) || empty( $mod[ 'id' ] ) ) { |
| 71 |
|
| 72 |
if ( $this->p->debug->enabled ) { |
| 73 |
|
| 74 |
$this->p->debug->log( 'exiting early: not post object or no post ID' ); |
| 75 |
} |
| 76 |
|
| 77 |
return $primary_term_id; |
| 78 |
|
| 79 |
} elseif ( empty( $tax_slug ) ) { |
| 80 |
|
| 81 |
if ( $this->p->debug->enabled ) { |
| 82 |
|
| 83 |
$this->p->debug->log( 'exiting early: $tax_slug is empty' ); |
| 84 |
} |
| 85 |
|
| 86 |
return $primary_term_id; |
| 87 |
|
| 88 |
} else { |
| 89 |
|
| 90 |
$meta_key = 'rank_math_primary_category'; |
| 91 |
|
| 92 |
if ( $this->p->debug->enabled ) { |
| 93 |
|
| 94 |
$this->p->debug->log( 'calling get_metadata() for ' . $meta_key ); |
| 95 |
} |
| 96 |
|
| 97 |
if ( $ret = get_metadata( 'post', $mod[ 'id' ], $meta_key, $single = true ) ) { |
| 98 |
|
| 99 |
return $ret; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
return $primary_term_id; |
| 104 |
} |
| 105 |
|
| 106 |
public function filter_primary_terms( $primary_terms, $mod, $tax_slug ) { |
| 107 |
|
| 108 |
if ( $this->p->debug->enabled ) { |
| 109 |
|
| 110 |
$this->p->debug->mark(); |
| 111 |
} |
| 112 |
|
| 113 |
if ( $primary_term_id = $this->filter_primary_term_id( false, $mod, $tax_slug, $is_custom = false ) ) { |
| 114 |
|
| 115 |
if ( empty( $primary_terms[ $primary_term_id ] ) ) { |
| 116 |
|
| 117 |
$term_obj = get_term( $primary_term_id ); |
| 118 |
|
| 119 |
if ( isset( $term_obj->term_id ) ) { // Just in case. |
| 120 |
|
| 121 |
$primary_terms[ $term_obj->term_id ] = $term_obj->name; |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
return $primary_terms; |
| 127 |
} |
| 128 |
|
| 129 |
public function filter_title_seed( $title_text, $mod, $num_hashtags, $md_key, $title_sep ) { |
| 130 |
|
| 131 |
if ( $this->p->debug->enabled ) { |
| 132 |
|
| 133 |
$this->p->debug->mark(); |
| 134 |
} |
| 135 |
|
| 136 |
$title_text = WpssoAbstractWpMeta::get_mod_meta( $mod, $meta_key = 'rank_math_title', $single = true ); |
| 137 |
|
| 138 |
if ( empty( $title_text ) ) { |
| 139 |
|
| 140 |
$title_text = ( new class { use \RankMath\Helpers\Api; } )::get_settings( 'titles.pt_' . $mod[ 'post_type' ] . '_default_snippet_name' ); |
| 141 |
|
| 142 |
if ( $this->p->debug->enabled ) { |
| 143 |
|
| 144 |
$this->p->debug->log( 'default_snippet_name = ' . $title_text ); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
$title_text = $this->maybe_convert_vars( $title_text, $mod ); |
| 149 |
|
| 150 |
return $title_text; |
| 151 |
} |
| 152 |
|
| 153 |
public function filter_description_seed( $desc_text, $mod, $num_hashtags, $md_key ) { |
| 154 |
|
| 155 |
if ( $this->p->debug->enabled ) { |
| 156 |
|
| 157 |
$this->p->debug->mark(); |
| 158 |
} |
| 159 |
|
| 160 |
$desc_text = WpssoAbstractWpMeta::get_mod_meta( $mod, $meta_key = 'rank_math_description', $single = true ); |
| 161 |
|
| 162 |
if ( empty( $desc_text ) ) { |
| 163 |
|
| 164 |
$desc_text = ( new class { use \RankMath\Helpers\Api; } )::get_settings( 'titles.pt_' . $mod[ 'post_type' ] . '_default_snippet_desc' ); |
| 165 |
|
| 166 |
if ( $this->p->debug->enabled ) { |
| 167 |
|
| 168 |
$this->p->debug->log( 'default_snippet_desc = ' . $desc_text ); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
$desc_text = $this->maybe_convert_vars( $desc_text, $mod ); |
| 173 |
|
| 174 |
return $desc_text; |
| 175 |
} |
| 176 |
|
| 177 |
public function filter_post_url( $url, $mod ) { |
| 178 |
|
| 179 |
if ( $this->p->debug->enabled ) { |
| 180 |
|
| 181 |
$this->p->debug->mark(); |
| 182 |
} |
| 183 |
|
| 184 |
$canonical = ''; |
| 185 |
|
| 186 |
if ( $mod[ 'id' ] ) { |
| 187 |
|
| 188 |
$canonical = get_metadata( 'post', $mod[ 'id' ], $meta_key = 'rank_math_canonical_url', $single = true ); |
| 189 |
} |
| 190 |
|
| 191 |
if ( ! empty( $canonical ) ) { |
| 192 |
|
| 193 |
return $canonical; |
| 194 |
} |
| 195 |
|
| 196 |
return $url; |
| 197 |
} |
| 198 |
|
| 199 |
public function filter_term_url( $url, $mod ) { |
| 200 |
|
| 201 |
if ( $this->p->debug->enabled ) { |
| 202 |
|
| 203 |
$this->p->debug->mark(); |
| 204 |
} |
| 205 |
|
| 206 |
$canonical = ''; |
| 207 |
|
| 208 |
if ( $mod[ 'id' ] ) { |
| 209 |
|
| 210 |
$canonical = get_metadata( 'term', $mod[ 'id' ], $meta_key = 'rank_math_canonical_url', $single = true ); |
| 211 |
} |
| 212 |
|
| 213 |
if ( ! empty( $canonical ) ) { |
| 214 |
|
| 215 |
return $canonical; |
| 216 |
} |
| 217 |
|
| 218 |
return $url; |
| 219 |
} |
| 220 |
|
| 221 |
private function maybe_convert_vars( $value, array $mod ) { |
| 222 |
|
| 223 |
if ( false !== strpos( $value, '%' ) ) { |
| 224 |
|
| 225 |
$value = preg_replace( '/%+([^%]+)%+/', '%%$1%%', $value ); // Convert inline variable names. |
| 226 |
|
| 227 |
$value = $this->p->util->inline->replace_variables( $value, $mod ); |
| 228 |
} |
| 229 |
|
| 230 |
return $value; |
| 231 |
} |
| 232 |
|
| 233 |
/* |
| 234 |
* Fix Rank Math CSS on back-end pages. |
| 235 |
*/ |
| 236 |
public function filter_admin_page_style_css( $custom_style_css ) { |
| 237 |
|
| 238 |
if ( $this->p->debug->enabled ) { |
| 239 |
|
| 240 |
$this->p->debug->mark(); |
| 241 |
} |
| 242 |
|
| 243 |
/* |
| 244 |
* Fix the width of Rank Math list table columns. |
| 245 |
*/ |
| 246 |
$custom_style_css .= ' |
| 247 |
table.wp-list-table > thead > tr > th.column-rank_math_seo_details, |
| 248 |
table.wp-list-table > tbody > tr > td.column-rank_math_seo_details { |
| 249 |
width:170px; |
| 250 |
} |
| 251 |
'; |
| 252 |
|
| 253 |
/* |
| 254 |
* The "Social" metabox tab and its options cannot be disabled, so hide them instead. |
| 255 |
*/ |
| 256 |
$custom_style_css .= ' |
| 257 |
.rank-math-tabs > div > a[href="#setting-panel-social"] { display: none; } |
| 258 |
.rank-math-tabs-content .setting-panel-social { display: none; } |
| 259 |
'; |
| 260 |
|
| 261 |
/* |
| 262 |
* The "Schema" metabox tab and its options cannot be disabled, so hide them instead. |
| 263 |
*/ |
| 264 |
if ( ! empty( $this->p->avail[ 'p' ][ 'schema' ] ) ) { |
| 265 |
|
| 266 |
$custom_style_css .= ' |
| 267 |
.rank-math-tabs > div > a[href="#setting-panel-richsnippet"] { display: none; } |
| 268 |
.rank-math-tabs-content .setting-panel-richsnippet { display: none; } |
| 269 |
'; |
| 270 |
} |
| 271 |
|
| 272 |
return $custom_style_css; |
| 273 |
} |
| 274 |
|
| 275 |
/* |
| 276 |
* Disable Rank Math Facebook and X (Twitter) meta tags. |
| 277 |
*/ |
| 278 |
public function cleanup_rankmath_actions() { |
| 279 |
|
| 280 |
if ( $this->p->debug->enabled ) { |
| 281 |
|
| 282 |
$this->p->debug->mark(); |
| 283 |
} |
| 284 |
|
| 285 |
/* |
| 286 |
* Disable Rank Math social meta tags. |
| 287 |
*/ |
| 288 |
remove_all_actions( 'rank_math/opengraph/facebook' ); |
| 289 |
remove_all_actions( 'rank_math/opengraph/slack' ); |
| 290 |
remove_all_actions( 'rank_math/opengraph/twitter' ); |
| 291 |
} |
| 292 |
|
| 293 |
/* |
| 294 |
* Disable Rank Math Schema markup. |
| 295 |
*/ |
| 296 |
public function cleanup_rankmath_json_ld( $data ) { |
| 297 |
|
| 298 |
if ( $this->p->debug->enabled ) { |
| 299 |
|
| 300 |
$this->p->debug->mark(); |
| 301 |
} |
| 302 |
|
| 303 |
/* |
| 304 |
* Remove everything except for the BreadcrumbList markup. |
| 305 |
* |
| 306 |
* The WPSSO BC add-on removes the BreadcrumbList markup. |
| 307 |
*/ |
| 308 |
if ( ! empty( $this->p->avail[ 'p' ][ 'schema' ] ) ) { |
| 309 |
|
| 310 |
return SucomUtil::preg_grep_keys( '/^BreadcrumbList$/', $data ); |
| 311 |
} |
| 312 |
|
| 313 |
return $data; |
| 314 |
} |
| 315 |
} |
| 316 |
} |
| 317 |
|