| 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( 'WpssoJsonTypeArticle' ) ) { |
| 14 |
|
| 15 |
class WpssoJsonTypeArticle { |
| 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_article' => 5, |
| 33 |
) ); |
| 34 |
} |
| 35 |
|
| 36 |
/* |
| 37 |
* See https://developers.google.com/search/docs/appearance/structured-data/article. |
| 38 |
*/ |
| 39 |
public function filter_json_data_https_schema_org_article( $json_data, $mod, $mt_og, $page_type_id, $is_main ) { |
| 40 |
|
| 41 |
if ( $this->p->debug->enabled ) { |
| 42 |
|
| 43 |
$this->p->debug->mark(); |
| 44 |
} |
| 45 |
|
| 46 |
$json_ret = array(); |
| 47 |
|
| 48 |
/* |
| 49 |
* See https://schema.org/articleSection. |
| 50 |
*/ |
| 51 |
WpssoSchema::add_data_itemprop_from_assoc( $json_ret, $mt_og, array( |
| 52 |
'articleSection' => 'article:section', |
| 53 |
) ); |
| 54 |
|
| 55 |
if ( ! empty( $mt_og[ 'article:reading_mins' ] ) ) { |
| 56 |
|
| 57 |
$json_ret[ 'timeRequired' ] = 'PT' . $mt_og[ 'article:reading_mins' ] . 'M'; |
| 58 |
} |
| 59 |
|
| 60 |
/* |
| 61 |
* See https://schema.org/articleBody. |
| 62 |
*/ |
| 63 |
if ( ! empty( $this->p->options[ 'schema_def_add_articlebody_prop' ] ) ) { |
| 64 |
|
| 65 |
/* |
| 66 |
* The CreativeWork 'text' property may be empty if 'schema_def_add_text_prop' is unchecked. |
| 67 |
*/ |
| 68 |
if ( empty( $json_data[ 'text' ] ) ) { |
| 69 |
|
| 70 |
$json_ret[ 'articleBody' ] = $this->p->page->get_text( $mod, $md_key = 'schema_text', $max_len = 'schema_text' ); |
| 71 |
|
| 72 |
} else $json_ret[ 'articleBody' ] = $json_data[ 'text' ]; |
| 73 |
} |
| 74 |
|
| 75 |
unset( $json_data[ 'text' ] ); // Prefer the articleBody property. |
| 76 |
|
| 77 |
/* |
| 78 |
* See https://schema.org/speakable. |
| 79 |
*/ |
| 80 |
if ( ! empty( $this->p->options[ 'plugin_speakable_css_csv' ] ) ) { |
| 81 |
|
| 82 |
$speakable_css_selector = SucomUtil::explode_csv( $this->p->options[ 'plugin_speakable_css_csv' ] ); |
| 83 |
|
| 84 |
if ( ! empty( $speakable_css_selector ) ) { |
| 85 |
|
| 86 |
$json_ret[ 'speakable' ] = WpssoSchema::get_schema_type_context( 'https://schema.org/SpeakableSpecification', array( |
| 87 |
'cssSelector' => $speakable_css_selector, |
| 88 |
) ); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
if ( $this->p->debug->enabled ) { |
| 93 |
|
| 94 |
$this->p->debug->log( 'merging json_data and json_ret arrays' ); |
| 95 |
} |
| 96 |
|
| 97 |
return WpssoSchema::return_data_from_filter( $json_data, $json_ret, $is_main ); |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|