| @@ -1,0 +1,238 @@ | ||
| 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( 'WpssoOpenGraphNS' ) ) { | |
| 19 | + | |
| 20 | + class WpssoOpenGraphNS { | |
| 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->add_html_attributes_filter(); | |
| 34 | + | |
| 35 | + $this->p->util->add_plugin_filters( $this, array( | |
| 36 | + 'og_data_https_ogp_me_ns_article' => 2, | |
| 37 | + 'og_data_https_ogp_me_ns_book' => 2, | |
| 38 | + 'og_data_https_ogp_me_ns_product' => 2, | |
| 39 | + ) ); | |
| 40 | + } | |
| 41 | + | |
| 42 | + private function add_html_attributes_filter() { | |
| 43 | + | |
| 44 | + $filter_name = SucomUtil::get_const( 'WPSSO_HTML_ATTR_FILTER_NAME', 'language_attributes' ); | |
| 45 | + $filter_prio = SucomUtil::get_const( 'WPSSO_HTML_ATTR_FILTER_PRIO', 1000 ); | |
| 46 | + | |
| 47 | + if ( empty( $filter_name ) || 'none' === $filter_name ) { | |
| 48 | + | |
| 49 | + if ( $this->p->debug->enabled ) { | |
| 50 | + | |
| 51 | + $this->p->debug->log( 'skipped filter_html_attributes - filter name is empty or disabled' ); | |
| 52 | + } | |
| 53 | + | |
| 54 | + } else { | |
| 55 | + | |
| 56 | + add_filter( $filter_name, array( $this, 'filter_html_attributes' ), $filter_prio, 1 ); | |
| 57 | + | |
| 58 | + if ( $this->p->debug->enabled ) { | |
| 59 | + | |
| 60 | + $this->p->debug->log( 'added filter_html_attributes filter for ' . $filter_name ); | |
| 61 | + } | |
| 62 | + } | |
| 63 | + | |
| 64 | + } | |
| 65 | + | |
| 66 | + public function filter_html_attributes( $html_attr ) { | |
| 67 | + | |
| 68 | + if ( $this->p->debug->enabled ) { | |
| 69 | + | |
| 70 | + $this->p->debug->log_args( array ( | |
| 71 | + 'html_attr' => $html_attr, | |
| 72 | + ) ); | |
| 73 | + } | |
| 74 | + | |
| 75 | + if ( $this->p->debug->enabled ) { | |
| 76 | + | |
| 77 | + $this->p->debug->log( 'required call to WpssoPage->get_mod()' ); | |
| 78 | + } | |
| 79 | + | |
| 80 | + $use_post = apply_filters( 'wpsso_use_post', false ); | |
| 81 | + | |
| 82 | + $mod = $this->p->page->get_mod( $use_post ); | |
| 83 | + | |
| 84 | + $type_id = $this->p->og->get_mod_og_type_id( $mod ); | |
| 85 | + | |
| 86 | + $og_ns = array( | |
| 87 | + 'og' => 'https://ogp.me/ns#', | |
| 88 | + 'fb' => 'https://ogp.me/ns/fb#', | |
| 89 | + ); | |
| 90 | + | |
| 91 | + /* | |
| 92 | + * Check that the og_type is known and add it's namespace value. | |
| 93 | + * | |
| 94 | + * Example: article, place, product, website, etc. | |
| 95 | + */ | |
| 96 | + if ( ! empty( $this->p->cf[ 'head' ][ 'og_type_ns' ][ $type_id ] ) ) { | |
| 97 | + | |
| 98 | + $og_ns[ $type_id ] = $this->p->cf[ 'head' ][ 'og_type_ns' ][ $type_id ]; | |
| 99 | + } | |
| 100 | + | |
| 101 | + $og_ns = apply_filters( 'wpsso_og_ns', $og_ns, $mod ); | |
| 102 | + | |
| 103 | + if ( SucomUtilWP::is_amp() ) { // Returns null, true, or false. | |
| 104 | + | |
| 105 | + /* | |
| 106 | + * Nothing to do. | |
| 107 | + */ | |
| 108 | + | |
| 109 | + } else { | |
| 110 | + | |
| 111 | + $html_attr = ' ' . $html_attr; // Prepare the string for testing. | |
| 112 | + | |
| 113 | + /* | |
| 114 | + * Find and remove an existing prefix attribute value. | |
| 115 | + */ | |
| 116 | + if ( strpos( $html_attr, 'prefix=' ) ) { | |
| 117 | + | |
| 118 | + /* | |
| 119 | + * s = A dot metacharacter in the pattern matches all characters, including newlines. | |
| 120 | + * | |
| 121 | + * See https://www.php.net/manual/en/reference.pcre.pattern.modifiers.php | |
| 122 | + */ | |
| 123 | + if ( preg_match( '/^(.*)\sprefix=["\']([^"\']*)["\'](.*)$/s', $html_attr, $match ) ) { | |
| 124 | + | |
| 125 | + $html_attr = $match[1] . $match[3]; // Remove the prefix. | |
| 126 | + } | |
| 127 | + } | |
| 128 | + | |
| 129 | + $attr_val = ''; | |
| 130 | + | |
| 131 | + foreach ( $og_ns as $name => $url ) { | |
| 132 | + | |
| 133 | + if ( false === strpos( $attr_val, ' ' . $name . ': ' . $url ) ) { | |
| 134 | + | |
| 135 | + $attr_val .= ' ' . $name . ': ' . $url; | |
| 136 | + } | |
| 137 | + } | |
| 138 | + | |
| 139 | + $html_attr .= ' prefix="' . trim( $attr_val ) . '"'; | |
| 140 | + } | |
| 141 | + | |
| 142 | + return trim( $html_attr ); | |
| 143 | + } | |
| 144 | + | |
| 145 | + /* | |
| 146 | + * The output from this method is provided to the JSON data filters, so be careful when removing any array | |
| 147 | + * elements. If you need to remove array elements after the Schema JSON-LD markup has been created, but before the | |
| 148 | + * meta tags have been generated, use the WpssoOpenGraph->sanitize_mt_array() method. | |
| 149 | + */ | |
| 150 | + public function filter_og_data_https_ogp_me_ns_article( array $mt_og, array $mod ) { | |
| 151 | + | |
| 152 | + if ( $this->p->debug->enabled ) { | |
| 153 | + | |
| 154 | + $this->p->debug->mark(); | |
| 155 | + } | |
| 156 | + | |
| 157 | + return $mt_og; | |
| 158 | + } | |
| 159 | + | |
| 160 | + /* | |
| 161 | + * The output from this method is provided to the JSON data filters, so be careful when removing any array | |
| 162 | + * elements. If you need to remove array elements after the Schema JSON-LD markup has been created, but before the | |
| 163 | + * meta tags have been generated, use the WpssoOpenGraph->sanitize_mt_array() method. | |
| 164 | + */ | |
| 165 | + public function filter_og_data_https_ogp_me_ns_book( array $mt_og, array $mod ) { | |
| 166 | + | |
| 167 | + if ( $this->p->debug->enabled ) { | |
| 168 | + | |
| 169 | + $this->p->debug->mark(); | |
| 170 | + } | |
| 171 | + | |
| 172 | + return $mt_og; | |
| 173 | + } | |
| 174 | + | |
| 175 | + /* | |
| 176 | + * The output from this method is provided to the JSON data filters, so be careful when removing any array | |
| 177 | + * elements. If you need to remove array elements after the Schema JSON-LD markup has been created, but before the | |
| 178 | + * meta tags have been generated, use the WpssoOpenGraph->sanitize_mt_array() method. | |
| 179 | + */ | |
| 180 | + public function filter_og_data_https_ogp_me_ns_product( array $mt_og, array $mod ) { | |
| 181 | + | |
| 182 | + if ( $this->p->debug->enabled ) { | |
| 183 | + | |
| 184 | + $this->p->debug->mark(); | |
| 185 | + } | |
| 186 | + | |
| 187 | + $mt_og[ 'product:retailer_item_id' ] = $mod[ 'id' ]; | |
| 188 | + $mt_og[ 'product:retailer_category' ] = $this->p->og->get_product_retailer_category( $mod ); | |
| 189 | + $mt_og[ 'product:awards' ] = $this->p->og->get_product_awards( $mod ); | |
| 190 | + | |
| 191 | + /* | |
| 192 | + * Maybe fix an empty product description. | |
| 193 | + */ | |
| 194 | + if ( empty( $mt_og[ 'product:description' ] ) ) { | |
| 195 | + | |
| 196 | + if ( ! empty( $mt_og[ 'og:description' ] ) ) { | |
| 197 | + | |
| 198 | + $mt_og[ 'product:description' ] = $mt_og[ 'og:description' ]; | |
| 199 | + } | |
| 200 | + } | |
| 201 | + | |
| 202 | + WpssoOpenGraph::check_mt_value_gtin( $mt_og, $mt_pre = 'product' ); | |
| 203 | + WpssoOpenGraph::check_mt_value_price( $mt_og, $mt_pre = 'product' ); | |
| 204 | + WpssoOpenGraph::check_mt_value_energy_efficiency( $mt_og, $mt_pre = 'product' ); | |
| 205 | + | |
| 206 | + /* | |
| 207 | + * Check product offers and variants. | |
| 208 | + */ | |
| 209 | + foreach ( array( 'product:offers', 'product:variants' ) as $mt_name ) { | |
| 210 | + | |
| 211 | + if ( ! empty( $mt_og[ $mt_name ] ) && is_array( $mt_og[ $mt_name ] ) ) { | |
| 212 | + | |
| 213 | + foreach ( $mt_og[ $mt_name ] as $num => &$mt_single ) { // Allow changes to the variation array. | |
| 214 | + | |
| 215 | + $mt_single[ 'product:item_group_id' ] = $mod[ 'id' ]; | |
| 216 | + | |
| 217 | + /* | |
| 218 | + * Maybe fix an empty variation descriptions. | |
| 219 | + */ | |
| 220 | + if ( empty( $mt_single[ 'product:description' ] ) ) { | |
| 221 | + | |
| 222 | + if ( ! empty( $mt_og[ 'product:description' ] ) ) { | |
| 223 | + | |
| 224 | + $mt_single[ 'product:description' ] = $mt_og[ 'product:description' ]; | |
| 225 | + } | |
| 226 | + } | |
| 227 | + | |
| 228 | + WpssoOpenGraph::check_mt_value_gtin( $mt_single, $mt_pre = 'product' ); | |
| 229 | + WpssoOpenGraph::check_mt_value_price( $mt_single, $mt_pre = 'product' ); | |
| 230 | + WpssoOpenGraph::check_mt_value_energy_efficiency( $mt_single, $mt_pre = 'product' ); | |
| 231 | + } | |
| 232 | + } | |
| 233 | + } | |
| 234 | + | |
| 235 | + return $mt_og; | |
| 236 | + } | |
| 237 | + } | |
| 238 | +} | |