| 1 |
<?php |
| 2 |
/* |
| 3 |
* License: GPLv3 |
| 4 |
* License URI: https://www.gnu.org/licenses/gpl.txt |
| 5 |
* Copyright 2016-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 ( ! class_exists( 'WpssoJsonTypeThing' ) ) { |
| 14 |
|
| 15 |
class WpssoJsonTypeThing { |
| 16 |
|
| 17 |
private $p; // Wpsso class object. |
| 18 |
|
| 19 |
/* |
| 20 |
* Instantiated by Wpsso->init_json_filters(). |
| 21 |
*/ |
| 22 |
public function __construct( &$plugin ) { |
| 23 |
|
| 24 |
$this->p =& $plugin; |
| 25 |
|
| 26 |
if ( $this->p->debug->enabled ) { |
| 27 |
|
| 28 |
$this->p->debug->mark(); |
| 29 |
} |
| 30 |
|
| 31 |
$this->p->util->add_plugin_filters( $this, array( |
| 32 |
'json_data_https_schema_org_thing' => 5, |
| 33 |
) ); |
| 34 |
} |
| 35 |
|
| 36 |
/* |
| 37 |
* Common filter for all Schema types. |
| 38 |
* |
| 39 |
* Adds the url, name, description, and if true, the main entity property. |
| 40 |
* |
| 41 |
* Does not add images, videos, author or organization markup since this will depend on the Schema type (Article, |
| 42 |
* Product, Place, etc.). |
| 43 |
*/ |
| 44 |
public function filter_json_data_https_schema_org_thing( $json_data, $mod, $mt_og, $page_type_id, $is_main ) { |
| 45 |
|
| 46 |
if ( $this->p->debug->enabled ) { |
| 47 |
|
| 48 |
$this->p->debug->mark(); |
| 49 |
} |
| 50 |
|
| 51 |
$page_type_url = $this->p->schema->get_schema_type_url( $page_type_id ); |
| 52 |
$json_ret = WpssoSchema::get_schema_type_context( $page_type_url ); |
| 53 |
|
| 54 |
/* |
| 55 |
* See https://schema.org/additionalType. |
| 56 |
*/ |
| 57 |
if ( $this->p->debug->enabled ) { |
| 58 |
|
| 59 |
$this->p->debug->log( 'getting additional types' ); |
| 60 |
} |
| 61 |
|
| 62 |
$json_ret[ 'additionalType' ] = array(); |
| 63 |
|
| 64 |
if ( $mod[ 'obj' ] && $mod[ 'id' ] ) { |
| 65 |
|
| 66 |
$md_opts = $mod[ 'obj' ]->get_options( $mod[ 'id' ] ); |
| 67 |
|
| 68 |
if ( is_array( $md_opts ) ) { // Just in case. |
| 69 |
|
| 70 |
foreach ( SucomUtil::preg_grep_keys( '/^schema_addl_type_url_[0-9]+$/', $md_opts ) as $url ) { |
| 71 |
|
| 72 |
if ( false !== filter_var( $url, FILTER_VALIDATE_URL ) ) { // Just in case. |
| 73 |
|
| 74 |
$json_ret[ 'additionalType' ][] = $url; |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
$filter_name = 'wpsso_json_prop_https_schema_org_additionaltype'; |
| 81 |
|
| 82 |
if ( $this->p->debug->enabled ) { |
| 83 |
|
| 84 |
$this->p->debug->log( 'applying filters "' . $filter_name . '"' ); |
| 85 |
} |
| 86 |
|
| 87 |
$json_ret[ 'additionalType' ] = apply_filters( $filter_name, $json_ret[ 'additionalType' ], $mod, $mt_og, $page_type_id, $is_main ); |
| 88 |
|
| 89 |
/* |
| 90 |
* See https://schema.org/url. |
| 91 |
*/ |
| 92 |
if ( $this->p->debug->enabled ) { |
| 93 |
|
| 94 |
$this->p->debug->log( 'getting url (fragment anchor or canonical url)' ); |
| 95 |
} |
| 96 |
|
| 97 |
if ( empty( $mod[ 'is_public' ] ) ) { // Since WPSSO Core v7.0.0. |
| 98 |
|
| 99 |
$json_ret[ 'url' ] = $this->p->util->get_fragment_anchor( $mod ); // Since WPSSO Core v7.0.0. |
| 100 |
|
| 101 |
} else $json_ret[ 'url' ] = $this->p->util->get_canonical_url( $mod ); |
| 102 |
|
| 103 |
/* |
| 104 |
* See https://schema.org/sameAs. |
| 105 |
*/ |
| 106 |
if ( $this->p->debug->enabled ) { |
| 107 |
|
| 108 |
$this->p->debug->log( 'getting same as' ); |
| 109 |
} |
| 110 |
|
| 111 |
$json_ret[ 'sameAs' ] = array( $this->p->util->get_canonical_url( $mod, $add_page = true ) ); |
| 112 |
|
| 113 |
if ( ! empty( $mod[ 'is_public' ] ) ) { // Since WPSSO Core v7.0.0. |
| 114 |
|
| 115 |
if ( ! empty( $mt_og[ 'og:url' ] ) ) { |
| 116 |
|
| 117 |
if ( $this->p->debug->enabled ) { |
| 118 |
|
| 119 |
$this->p->debug->log( 'sameAs og URL = ' . $mt_og[ 'og:url' ] ); |
| 120 |
} |
| 121 |
|
| 122 |
$json_ret[ 'sameAs' ][] = $mt_og[ 'og:url' ]; |
| 123 |
} |
| 124 |
|
| 125 |
/* |
| 126 |
* Add the post shortlink, but only if the link rel shortlink tag is enabled. |
| 127 |
*/ |
| 128 |
if ( $mod[ 'is_post' ] && $mod[ 'id' ] ) { |
| 129 |
|
| 130 |
/* |
| 131 |
* WpssoUtil->is_shortlink_disabled() returns true if: |
| 132 |
* |
| 133 |
* - The 'add_link_rel_shortlink' option is unchecked. |
| 134 |
* - The 'wpsso_add_link_rel_shortlink' filter returns false. |
| 135 |
* - The 'wpsso_shortlink_disabled' filter returns true. |
| 136 |
*/ |
| 137 |
if ( ! $this->p->util->is_shortlink_disabled() ) { |
| 138 |
|
| 139 |
if ( $shortlink = $this->p->util->get_shortlink( $mod, $context = 'post' ) ) { |
| 140 |
|
| 141 |
if ( $this->p->debug->enabled ) { |
| 142 |
|
| 143 |
$this->p->debug->log( 'sameAs shortlink URL = ' . $shortlink ); |
| 144 |
} |
| 145 |
|
| 146 |
$json_ret[ 'sameAs' ][] = $shortlink; |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
/* |
| 152 |
* Add the shortened URL for posts (which may be different to the shortlink), terms, and users. |
| 153 |
*/ |
| 154 |
if ( ! empty( $mt_og[ 'og:url' ] ) ) { // Just in case. |
| 155 |
|
| 156 |
/* |
| 157 |
* Shorten URL using the selected shortening service. |
| 158 |
*/ |
| 159 |
$short_url = $this->p->util->shorten_url( $mt_og[ 'og:url' ], $mod ); |
| 160 |
|
| 161 |
if ( $this->p->debug->enabled ) { |
| 162 |
|
| 163 |
$this->p->debug->log( 'sameAs short URL = ' . $short_url ); |
| 164 |
} |
| 165 |
|
| 166 |
if ( $short_url !== $mt_og[ 'og:url' ] ) { // Just in case. |
| 167 |
|
| 168 |
$json_ret[ 'sameAs' ][] = $short_url; |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
/* |
| 174 |
* Get additional sameAs URLs from the post/term/user custom meta. |
| 175 |
*/ |
| 176 |
if ( $mod[ 'obj' ] && $mod[ 'id' ] ) { |
| 177 |
|
| 178 |
if ( $this->p->debug->enabled ) { |
| 179 |
|
| 180 |
$this->p->debug->log( 'getting options for sameAs custom URLs' ); |
| 181 |
} |
| 182 |
|
| 183 |
$md_opts = $mod[ 'obj' ]->get_options( $mod[ 'id' ] ); |
| 184 |
|
| 185 |
if ( is_array( $md_opts ) ) { // Just in case |
| 186 |
|
| 187 |
foreach ( SucomUtil::preg_grep_keys( '/^schema_sameas_url_[0-9]+$/', $md_opts ) as $url ) { |
| 188 |
|
| 189 |
if ( $this->p->debug->enabled ) { |
| 190 |
|
| 191 |
$this->p->debug->log( 'sameAs custom URL = ' . $url ); |
| 192 |
} |
| 193 |
|
| 194 |
$json_ret[ 'sameAs' ][] = SucomUtil::esc_url_encode( $url ); |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
$filter_name = 'wpsso_json_prop_https_schema_org_sameas'; |
| 200 |
|
| 201 |
if ( $this->p->debug->enabled ) { |
| 202 |
|
| 203 |
$this->p->debug->log( 'applying filters "' . $filter_name . '"' ); |
| 204 |
} |
| 205 |
|
| 206 |
$json_ret[ 'sameAs' ] = apply_filters( $filter_name, $json_ret[ 'sameAs' ], $mod, $mt_og, $page_type_id, $is_main ); |
| 207 |
|
| 208 |
if ( $this->p->debug->enabled ) { |
| 209 |
|
| 210 |
$this->p->debug->log( 'calling check_prop_value_sameas()' ); |
| 211 |
} |
| 212 |
|
| 213 |
WpssoSchema::check_prop_value_sameas( $json_ret ); |
| 214 |
|
| 215 |
if ( $this->p->debug->enabled ) { |
| 216 |
|
| 217 |
$this->p->debug->log_arr( 'sameAs', $json_ret[ 'sameAs' ] ); |
| 218 |
} |
| 219 |
|
| 220 |
/* |
| 221 |
* See https://schema.org/name. |
| 222 |
* See https://schema.org/alternateName. |
| 223 |
*/ |
| 224 |
$json_ret[ 'name' ] = $this->p->page->get_title( $mod, $md_key = 'schema_title', $max_len = 'schema_title' ); |
| 225 |
|
| 226 |
$json_ret[ 'alternateName' ] = $this->p->page->get_title( $mod, $md_key = 'schema_title_alt', $max_len = 'schema_title_alt' ); |
| 227 |
|
| 228 |
if ( $json_ret[ 'name' ] === $json_ret[ 'alternateName' ] ) { // Prevent duplicate values. |
| 229 |
|
| 230 |
unset( $json_ret[ 'alternateName' ] ); |
| 231 |
} |
| 232 |
|
| 233 |
/* |
| 234 |
* See https://schema.org/description. |
| 235 |
*/ |
| 236 |
$json_ret[ 'description' ] = $this->p->page->get_description( $mod, $md_key = 'schema_desc', $max_len = 'schema_desc' ); |
| 237 |
|
| 238 |
/* |
| 239 |
* See https://schema.org/potentialAction. |
| 240 |
*/ |
| 241 |
$json_ret[ 'potentialAction' ] = array(); |
| 242 |
|
| 243 |
$filter_name = 'wpsso_json_prop_https_schema_org_potentialaction'; |
| 244 |
|
| 245 |
if ( $this->p->debug->enabled ) { |
| 246 |
|
| 247 |
$this->p->debug->log( 'applying filters "' . $filter_name . '"' ); |
| 248 |
} |
| 249 |
|
| 250 |
$json_ret[ 'potentialAction' ] = apply_filters( $filter_name, $json_ret[ 'potentialAction' ], $mod, $mt_og, $page_type_id, $is_main ); |
| 251 |
|
| 252 |
if ( $this->p->debug->enabled ) { |
| 253 |
|
| 254 |
$this->p->debug->log( 'merging json_data and json_ret arrays' ); |
| 255 |
} |
| 256 |
|
| 257 |
return WpssoSchema::return_data_from_filter( $json_data, $json_ret, $is_main ); |
| 258 |
} |
| 259 |
} |
| 260 |
} |
| 261 |
|