| 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( 'WpssoJsonTypeWebsite' ) ) { |
| 14 |
|
| 15 |
class WpssoJsonTypeWebsite { |
| 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_website' => 5, |
| 33 |
) ); |
| 34 |
} |
| 35 |
|
| 36 |
public function filter_json_data_https_schema_org_website( $json_data, $mod, $mt_og, $page_type_id, $is_main ) { |
| 37 |
|
| 38 |
if ( $this->p->debug->enabled ) { |
| 39 |
|
| 40 |
$this->p->debug->mark(); |
| 41 |
} |
| 42 |
|
| 43 |
$json_ret = WpssoSchema::get_schema_type_context( 'https://schema.org/WebSite', array( |
| 44 |
'url' => SucomUtilWP::get_home_url( $this->p->options, $mod ), |
| 45 |
) ); |
| 46 |
|
| 47 |
foreach ( array( |
| 48 |
'name' => SucomUtilWP::get_site_name( $this->p->options, $mod ), |
| 49 |
'alternateName' => SucomUtilWP::get_site_name_alt( $this->p->options, $mod ), |
| 50 |
'description' => SucomUtilWP::get_site_description( $this->p->options, $mod ), |
| 51 |
) as $key => $value ) { |
| 52 |
|
| 53 |
if ( ! empty( $value ) ) { |
| 54 |
|
| 55 |
$json_ret[ $key ] = $value; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
/* |
| 60 |
* Potential Action (SearchAction, OrderAction, etc.) for Google's Sitelinks search box. |
| 61 |
* |
| 62 |
* Hook the 'wpsso_json_ld_search_url' filter and return false if you wish to disable / skip the Potential |
| 63 |
* Action property. |
| 64 |
* |
| 65 |
* The 'wpsso_json_prop_https_schema_org_potentialaction' filter may already be applied by the JSON data |
| 66 |
* filters, so do not re-apply it here. |
| 67 |
* |
| 68 |
* Note: Add this markup only to the homepage (aka WebSite markup), not to any other pages. |
| 69 |
* |
| 70 |
* See https://developers.google.com/search/docs/appearance/structured-data/sitelinks-searchbox. |
| 71 |
*/ |
| 72 |
$search_url = SucomUtil::esc_url_encode( get_bloginfo( 'url' ) ) . '?s={search_term_string}'; |
| 73 |
|
| 74 |
$search_url = apply_filters( 'wpsso_json_ld_search_url', $search_url ); |
| 75 |
|
| 76 |
if ( ! empty( $search_url ) ) { |
| 77 |
|
| 78 |
$json_ret[ 'potentialAction' ][] = WpssoSchema::get_schema_type_context( 'https://schema.org/SearchAction', array( |
| 79 |
'target' => $search_url, |
| 80 |
'query-input' => 'required name=search_term_string', |
| 81 |
) ); |
| 82 |
|
| 83 |
} elseif ( $this->p->debug->enabled ) { |
| 84 |
|
| 85 |
$this->p->debug->log( 'skipping search action: search url is empty' ); |
| 86 |
} |
| 87 |
|
| 88 |
if ( $this->p->debug->enabled ) { |
| 89 |
|
| 90 |
$this->p->debug->log( 'merging json_data and json_ret arrays' ); |
| 91 |
} |
| 92 |
|
| 93 |
return WpssoSchema::return_data_from_filter( $json_data, $json_ret, $is_main ); |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|