@@ -21,535 +21,535 @@ |
||
| 21 | 21 | */ |
| 22 | 22 | class Wordlift_Jsonld_Service { |
| 23 | 23 | |
| 24 | - private static $creative_work_types = array( |
|
| 25 | - 'AmpStory', |
|
| 26 | - 'ArchiveComponent', |
|
| 27 | - 'Article', |
|
| 28 | - 'Atlas', |
|
| 29 | - 'Blog', |
|
| 30 | - 'Book', |
|
| 31 | - 'Chapter', |
|
| 32 | - 'Claim', |
|
| 33 | - 'Clip', |
|
| 34 | - 'Code', |
|
| 35 | - 'Collection', |
|
| 36 | - 'ComicStory', |
|
| 37 | - 'Comment', |
|
| 38 | - 'Conversation', |
|
| 39 | - 'Course', |
|
| 40 | - 'CreativeWork', |
|
| 41 | - 'CreativeWorkSeason', |
|
| 42 | - 'CreativeWorkSeries', |
|
| 43 | - 'DataCatalog', |
|
| 44 | - 'Dataset', |
|
| 45 | - 'DefinedTermSet', |
|
| 46 | - 'Diet', |
|
| 47 | - 'DigitalDocument', |
|
| 48 | - 'Drawing', |
|
| 49 | - 'EducationalOccupationalCredential', |
|
| 50 | - 'Episode', |
|
| 51 | - 'ExercisePlan', |
|
| 52 | - 'Game', |
|
| 53 | - 'Guide', |
|
| 54 | - 'HowTo', |
|
| 55 | - 'HowToDirection', |
|
| 56 | - 'HowToSection', |
|
| 57 | - 'HowToStep', |
|
| 58 | - 'HowToTip', |
|
| 59 | - 'HyperToc', |
|
| 60 | - 'HyperTocEntry', |
|
| 61 | - 'LearningResource', |
|
| 62 | - 'Legislation', |
|
| 63 | - 'Manuscript', |
|
| 64 | - 'Map', |
|
| 65 | - 'MathSolver', |
|
| 66 | - 'MediaObject', |
|
| 67 | - 'Menu', |
|
| 68 | - 'MenuSection', |
|
| 69 | - 'Message', |
|
| 70 | - 'Movie', |
|
| 71 | - 'MusicComposition', |
|
| 72 | - 'MusicPlaylist', |
|
| 73 | - 'MusicRecording', |
|
| 74 | - 'Painting', |
|
| 75 | - 'Photograph', |
|
| 76 | - 'Play', |
|
| 77 | - 'Poster', |
|
| 78 | - 'PublicationIssue', |
|
| 79 | - 'PublicationVolume', |
|
| 80 | - 'Quotation', |
|
| 81 | - 'Review', |
|
| 82 | - 'Sculpture', |
|
| 83 | - 'Season', |
|
| 84 | - 'SheetMusic', |
|
| 85 | - 'ShortStory', |
|
| 86 | - 'SoftwareApplication', |
|
| 87 | - 'SoftwareSourceCode', |
|
| 88 | - 'SpecialAnnouncement', |
|
| 89 | - 'Thesis', |
|
| 90 | - 'TvSeason', |
|
| 91 | - 'TvSeries', |
|
| 92 | - 'VisualArtwork', |
|
| 93 | - 'WebContent', |
|
| 94 | - 'WebPage', |
|
| 95 | - 'WebPageElement', |
|
| 96 | - 'WebSite', |
|
| 97 | - 'AdvertiserContentArticle', |
|
| 98 | - 'NewsArticle', |
|
| 99 | - 'Report', |
|
| 100 | - 'SatiricalArticle', |
|
| 101 | - 'ScholarlyArticle', |
|
| 102 | - 'SocialMediaPosting', |
|
| 103 | - 'TechArticle', |
|
| 104 | - 'AnalysisNewsArticle', |
|
| 105 | - 'AskPublicNewsArticle', |
|
| 106 | - 'BackgroundNewsArticle', |
|
| 107 | - 'OpinionNewsArticle', |
|
| 108 | - 'ReportageNewsArticle', |
|
| 109 | - 'ReviewNewsArticle', |
|
| 110 | - 'MedicalScholarlyArticle', |
|
| 111 | - 'BlogPosting', |
|
| 112 | - 'DiscussionForumPosting', |
|
| 113 | - 'LiveBlogPosting', |
|
| 114 | - 'ApiReference', |
|
| 115 | - 'Audiobook', |
|
| 116 | - 'MovieClip', |
|
| 117 | - 'RadioClip', |
|
| 118 | - 'TvClip', |
|
| 119 | - 'VideoGameClip', |
|
| 120 | - 'ProductCollection', |
|
| 121 | - 'ComicCoverArt', |
|
| 122 | - 'Answer', |
|
| 123 | - 'CorrectionComment', |
|
| 124 | - 'Question', |
|
| 125 | - 'PodcastSeason', |
|
| 126 | - 'RadioSeason', |
|
| 127 | - 'TvSeason', |
|
| 128 | - 'BookSeries', |
|
| 129 | - 'MovieSeries', |
|
| 130 | - 'Periodical', |
|
| 131 | - 'PodcastSeries', |
|
| 132 | - 'RadioSeries', |
|
| 133 | - 'TvSeries', |
|
| 134 | - 'VideoGameSeries', |
|
| 135 | - 'ComicSeries', |
|
| 136 | - 'Newspaper', |
|
| 137 | - 'DataFeed', |
|
| 138 | - 'CompleteDataFeed', |
|
| 139 | - 'CategoryCodeSet', |
|
| 140 | - 'NoteDigitalDocument', |
|
| 141 | - 'PresentationDigitalDocument', |
|
| 142 | - 'SpreadsheetDigitalDocument', |
|
| 143 | - 'TextDigitalDocument', |
|
| 144 | - 'PodcastEpisode', |
|
| 145 | - 'RadioEpisode', |
|
| 146 | - 'TvEpisode', |
|
| 147 | - 'VideoGame', |
|
| 148 | - 'Recipe', |
|
| 149 | - 'Course', |
|
| 150 | - 'Quiz', |
|
| 151 | - 'LegislationObject', |
|
| 152 | - 'AudioObject', |
|
| 153 | - 'DModel', |
|
| 154 | - 'DataDownload', |
|
| 155 | - 'ImageObject', |
|
| 156 | - 'LegislationObject', |
|
| 157 | - 'MusicVideoObject', |
|
| 158 | - 'VideoObject', |
|
| 159 | - 'Audiobook', |
|
| 160 | - 'Barcode', |
|
| 161 | - 'EmailMessage', |
|
| 162 | - 'MusicAlbum', |
|
| 163 | - 'MusicRelease', |
|
| 164 | - 'ComicIssue', |
|
| 165 | - 'ClaimReview', |
|
| 166 | - 'CriticReview', |
|
| 167 | - 'EmployerReview', |
|
| 168 | - 'MediaReview', |
|
| 169 | - 'Recommendation', |
|
| 170 | - 'UserReview', |
|
| 171 | - 'ReviewNewsArticle', |
|
| 172 | - 'MobileApplication', |
|
| 173 | - 'VideoGame', |
|
| 174 | - 'WebApplication', |
|
| 175 | - 'CoverArt', |
|
| 176 | - 'ComicCoverArt', |
|
| 177 | - 'HealthTopicContent', |
|
| 178 | - 'AboutPage', |
|
| 179 | - 'CheckoutPage', |
|
| 180 | - 'CollectionPage', |
|
| 181 | - 'ContactPage', |
|
| 182 | - 'FaqPage', |
|
| 183 | - 'ItemPage', |
|
| 184 | - 'MedicalWebPage', |
|
| 185 | - 'ProfilePage', |
|
| 186 | - 'QaPage', |
|
| 187 | - 'RealEstateListing', |
|
| 188 | - 'SearchResultsPage', |
|
| 189 | - 'MediaGallery', |
|
| 190 | - 'ImageGallery', |
|
| 191 | - 'VideoGallery', |
|
| 192 | - 'SiteNavigationElement', |
|
| 193 | - 'Table', |
|
| 194 | - 'WpAdBlock', |
|
| 195 | - 'WpFooter', |
|
| 196 | - 'WpHeader', |
|
| 197 | - 'WpSideBar', |
|
| 198 | - ); |
|
| 199 | - /** |
|
| 200 | - * The singleton instance for the JSON-LD service. |
|
| 201 | - * |
|
| 202 | - * @since 3.15.1 |
|
| 203 | - * |
|
| 204 | - * @var \Wordlift_Jsonld_Service $instance The singleton instance for the JSON-LD service. |
|
| 205 | - */ |
|
| 206 | - private static $instance; |
|
| 207 | - /** |
|
| 208 | - * A {@link Wordlift_Entity_Service} instance. |
|
| 209 | - * |
|
| 210 | - * @since 3.8.0 |
|
| 211 | - * @access private |
|
| 212 | - * @var Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 213 | - */ |
|
| 214 | - private $entity_service; |
|
| 215 | - /** |
|
| 216 | - * A {@link Wordlift_Term_JsonLd_Adapter} instance. |
|
| 217 | - * |
|
| 218 | - * @since 3.32.0 |
|
| 219 | - * @access private |
|
| 220 | - * @var Wordlift_Term_JsonLd_Adapter $entity_service A {@link Wordlift_Term_JsonLd_Adapter} instance. |
|
| 221 | - */ |
|
| 222 | - private $term_jsonld_adapter; |
|
| 223 | - /** |
|
| 224 | - * A {@link Wordlift_Post_Converter} instance. |
|
| 225 | - * |
|
| 226 | - * @since 3.8.0 |
|
| 227 | - * @access private |
|
| 228 | - * @var \Wordlift_Post_Converter A {@link Wordlift_Post_Converter} instance. |
|
| 229 | - */ |
|
| 230 | - private $converter; |
|
| 231 | - /** |
|
| 232 | - * A {@link Wordlift_Website_Jsonld_Converter} instance. |
|
| 233 | - * |
|
| 234 | - * @since 3.14.0 |
|
| 235 | - * @access private |
|
| 236 | - * @var \Wordlift_Website_Jsonld_Converter A {@link Wordlift_Website_Jsonld_Converter} instance. |
|
| 237 | - */ |
|
| 238 | - private $website_converter; |
|
| 239 | - /** |
|
| 240 | - * The {@link Api_Service} used to communicate with the remote APIs. |
|
| 241 | - * |
|
| 242 | - * @access private |
|
| 243 | - * @var Default_Api_Service |
|
| 244 | - */ |
|
| 245 | - private $api_service; |
|
| 246 | - |
|
| 247 | - /** |
|
| 248 | - * Create a JSON-LD service. |
|
| 249 | - * |
|
| 250 | - * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 251 | - * @param \Wordlift_Post_Converter $converter A {@link Wordlift_Uri_To_Jsonld_Converter} instance. |
|
| 252 | - * @param \Wordlift_Website_Jsonld_Converter $website_converter A {@link Wordlift_Website_Jsonld_Converter} instance. |
|
| 253 | - * @param \Wordlift_Term_JsonLd_Adapter $term_jsonld_adapter |
|
| 254 | - * |
|
| 255 | - * @since 3.8.0 |
|
| 256 | - */ |
|
| 257 | - public function __construct( $entity_service, $converter, $website_converter, $term_jsonld_adapter ) { |
|
| 258 | - |
|
| 259 | - $this->api_service = Default_Api_Service::get_instance(); |
|
| 260 | - $this->entity_service = $entity_service; |
|
| 261 | - $this->converter = $converter; |
|
| 262 | - $this->website_converter = $website_converter; |
|
| 263 | - $this->term_jsonld_adapter = $term_jsonld_adapter; |
|
| 264 | - self::$instance = $this; |
|
| 265 | - |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - /** |
|
| 269 | - * Get the singleton instance for the JSON-LD service. |
|
| 270 | - * |
|
| 271 | - * @return \Wordlift_Jsonld_Service The singleton instance for the JSON-LD service. |
|
| 272 | - * @since 3.15.1 |
|
| 273 | - */ |
|
| 274 | - public static function get_instance() { |
|
| 275 | - |
|
| 276 | - return self::$instance; |
|
| 277 | - } |
|
| 278 | - |
|
| 279 | - /** |
|
| 280 | - * Process calls to the AJAX 'wl_jsonld' endpoint. |
|
| 281 | - * |
|
| 282 | - * @since 3.8.0 |
|
| 283 | - */ |
|
| 284 | - public function get() { |
|
| 285 | - // Clear the buffer to be sure someone doesn't mess with our response. |
|
| 286 | - // |
|
| 287 | - // See https://github.com/insideout10/wordlift-plugin/issues/406. |
|
| 288 | - // See https://codex.wordpress.org/AJAX_in_Plugins. |
|
| 289 | - // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
| 290 | - @ob_clean(); |
|
| 291 | - |
|
| 292 | - // Get the parameter from the request. |
|
| 293 | - $is_homepage = isset( $_REQUEST['homepage'] ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 294 | - $post_id = isset( $_REQUEST['id'] ) && is_numeric( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : null; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 295 | - |
|
| 296 | - // Send the generated JSON-LD. |
|
| 297 | - $this->send_jsonld( $this->get_jsonld( $is_homepage, $post_id ) ); |
|
| 298 | - |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - /** |
|
| 302 | - * A close of WP's own `wp_send_json` function which uses `application/ld+json` as content type. |
|
| 303 | - * |
|
| 304 | - * @param mixed $response Variable (usually an array or object) to encode as JSON, |
|
| 305 | - * then print and die. |
|
| 306 | - * |
|
| 307 | - * @since 3.18.5 |
|
| 308 | - */ |
|
| 309 | - private function send_jsonld( $response ) { |
|
| 310 | - // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
| 311 | - @header( 'Content-Type: application/ld+json; charset=' . get_option( 'blog_charset' ) ); |
|
| 312 | - echo wp_json_encode( $response ); |
|
| 313 | - if ( apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { |
|
| 314 | - wp_die(); |
|
| 315 | - } else { |
|
| 316 | - die; |
|
| 317 | - } |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - /** |
|
| 321 | - * Get the JSON-LD. |
|
| 322 | - * |
|
| 323 | - * @param bool $is_homepage Whether the JSON-LD for the homepage is being requested. |
|
| 324 | - * @param int|null $post_id The JSON-LD for the specified {@link WP_Post} id. |
|
| 325 | - * @param int $context A context for the JSON-LD generation, valid values in Jsonld_Context_Enum. |
|
| 326 | - * |
|
| 327 | - * @return array A JSON-LD structure. |
|
| 328 | - * @since 3.15.1 |
|
| 329 | - */ |
|
| 330 | - public function get_jsonld( $is_homepage = false, $post_id = null, $context = Jsonld_Context_Enum::UNKNOWN ) { |
|
| 331 | - // Tell NewRelic to ignore us, otherwise NewRelic customers might receive |
|
| 332 | - // e-mails with a low apdex score. |
|
| 333 | - // |
|
| 334 | - // See https://github.com/insideout10/wordlift-plugin/issues/521 |
|
| 335 | - Wordlift_NewRelic_Adapter::ignore_apdex(); |
|
| 336 | - |
|
| 337 | - // Switch to Website converter if is home page. |
|
| 338 | - if ( $is_homepage ) { |
|
| 339 | - /** |
|
| 340 | - * Filter: 'wordlift_disable_website_json_ld' - Allow disabling of the json+ld output. |
|
| 341 | - * |
|
| 342 | - * @since 3.14.0 |
|
| 343 | - * @api bool $display_search Whether or not to display json+ld search on the frontend. |
|
| 344 | - */ |
|
| 345 | - if ( apply_filters( 'wordlift_disable_website_json_ld', false ) ) { |
|
| 346 | - return array(); |
|
| 347 | - } |
|
| 348 | - |
|
| 349 | - // Set a reference to the website_converter. |
|
| 350 | - $website_converter = $this->website_converter; |
|
| 351 | - |
|
| 352 | - // Send JSON-LD. |
|
| 353 | - return $website_converter->create_schema(); |
|
| 354 | - } |
|
| 355 | - |
|
| 356 | - // If no id has been provided return an empty array. |
|
| 357 | - if ( ! isset( $post_id ) ) { |
|
| 358 | - return array(); |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - // An array of references which is captured when converting an URI to a |
|
| 362 | - // json which we gather to further expand our json-ld. |
|
| 363 | - $references = array(); |
|
| 364 | - $references_infos = array(); |
|
| 365 | - |
|
| 366 | - // Set a reference to the entity_to_jsonld_converter to use in the closures. |
|
| 367 | - $entity_to_jsonld_converter = $this->converter; |
|
| 368 | - |
|
| 369 | - $relations = new Relations(); |
|
| 370 | - $jsonld = $entity_to_jsonld_converter->convert( $post_id, $references, $references_infos, $relations ); |
|
| 371 | - |
|
| 372 | - $graph = new Graph( $jsonld, $entity_to_jsonld_converter, Wordlift_Term_JsonLd_Adapter::get_instance() ); |
|
| 373 | - |
|
| 374 | - $schema_type = is_array( $jsonld['@type'] ) ? $jsonld['@type'] : array( $jsonld['@type'] ); |
|
| 375 | - |
|
| 376 | - // Add `about`/`mentions` only for `CreativeWork` and descendants. |
|
| 377 | - if ( array_intersect( $schema_type, self::$creative_work_types ) ) { |
|
| 378 | - |
|
| 379 | - foreach ( $relations->toArray() as $relation ) { |
|
| 380 | - |
|
| 381 | - // Setting about or mentions by label match is currently supported only for posts |
|
| 382 | - if ( Object_Type_Enum::POST !== $relation->get_object()->get_type() ) { |
|
| 383 | - continue; |
|
| 384 | - } |
|
| 385 | - |
|
| 386 | - // Add the `mentions`/`about` prop. |
|
| 387 | - $this->add_mention_or_about( $jsonld, $post_id, $relation ); |
|
| 388 | - } |
|
| 389 | - $graph->set_main_jsonld( $jsonld ); |
|
| 390 | - } |
|
| 391 | - |
|
| 392 | - $jsonld_arr = $graph->add_references( $references ) |
|
| 393 | - ->add_relations( $relations ) |
|
| 394 | - ->add_required_reference_infos( $references_infos ) |
|
| 395 | - ->render( $context ); |
|
| 396 | - |
|
| 397 | - /** |
|
| 398 | - * Filter name: wl_after_get_jsonld |
|
| 399 | - * |
|
| 400 | - * @return array |
|
| 401 | - * @since 3.27.2 |
|
| 402 | - * @var $jsonld_arr array The final jsonld before outputting to page. |
|
| 403 | - * @var $post_id int The post id for which the jsonld is generated. |
|
| 404 | - */ |
|
| 405 | - $jsonld_arr = apply_filters( 'wl_after_get_jsonld', $jsonld_arr, $post_id, $context ); |
|
| 406 | - |
|
| 407 | - $this->set_events_request( $jsonld_arr, $post_id, $context ); |
|
| 408 | - |
|
| 409 | - return $jsonld_arr; |
|
| 410 | - } |
|
| 411 | - |
|
| 412 | - /** |
|
| 413 | - * Write the JSON-LD in the head. |
|
| 414 | - * |
|
| 415 | - * This function isn't actually used, but may be used to quickly enable writing the JSON-LD synchronously to the |
|
| 416 | - * document head, using the `wp_head` hook. |
|
| 417 | - * |
|
| 418 | - * @since 3.18.5 |
|
| 419 | - */ |
|
| 420 | - public function wp_head() { |
|
| 421 | - |
|
| 422 | - // Determine whether this is the home page or whether we're displaying a single post. |
|
| 423 | - $is_homepage = is_home() || is_front_page(); |
|
| 424 | - $post_id = is_singular() ? get_the_ID() : null; |
|
| 425 | - |
|
| 426 | - $jsonld = wp_json_encode( $this->get_jsonld( $is_homepage, $post_id, Jsonld_Context_Enum::PAGE ) ); |
|
| 427 | - ?> |
|
| 24 | + private static $creative_work_types = array( |
|
| 25 | + 'AmpStory', |
|
| 26 | + 'ArchiveComponent', |
|
| 27 | + 'Article', |
|
| 28 | + 'Atlas', |
|
| 29 | + 'Blog', |
|
| 30 | + 'Book', |
|
| 31 | + 'Chapter', |
|
| 32 | + 'Claim', |
|
| 33 | + 'Clip', |
|
| 34 | + 'Code', |
|
| 35 | + 'Collection', |
|
| 36 | + 'ComicStory', |
|
| 37 | + 'Comment', |
|
| 38 | + 'Conversation', |
|
| 39 | + 'Course', |
|
| 40 | + 'CreativeWork', |
|
| 41 | + 'CreativeWorkSeason', |
|
| 42 | + 'CreativeWorkSeries', |
|
| 43 | + 'DataCatalog', |
|
| 44 | + 'Dataset', |
|
| 45 | + 'DefinedTermSet', |
|
| 46 | + 'Diet', |
|
| 47 | + 'DigitalDocument', |
|
| 48 | + 'Drawing', |
|
| 49 | + 'EducationalOccupationalCredential', |
|
| 50 | + 'Episode', |
|
| 51 | + 'ExercisePlan', |
|
| 52 | + 'Game', |
|
| 53 | + 'Guide', |
|
| 54 | + 'HowTo', |
|
| 55 | + 'HowToDirection', |
|
| 56 | + 'HowToSection', |
|
| 57 | + 'HowToStep', |
|
| 58 | + 'HowToTip', |
|
| 59 | + 'HyperToc', |
|
| 60 | + 'HyperTocEntry', |
|
| 61 | + 'LearningResource', |
|
| 62 | + 'Legislation', |
|
| 63 | + 'Manuscript', |
|
| 64 | + 'Map', |
|
| 65 | + 'MathSolver', |
|
| 66 | + 'MediaObject', |
|
| 67 | + 'Menu', |
|
| 68 | + 'MenuSection', |
|
| 69 | + 'Message', |
|
| 70 | + 'Movie', |
|
| 71 | + 'MusicComposition', |
|
| 72 | + 'MusicPlaylist', |
|
| 73 | + 'MusicRecording', |
|
| 74 | + 'Painting', |
|
| 75 | + 'Photograph', |
|
| 76 | + 'Play', |
|
| 77 | + 'Poster', |
|
| 78 | + 'PublicationIssue', |
|
| 79 | + 'PublicationVolume', |
|
| 80 | + 'Quotation', |
|
| 81 | + 'Review', |
|
| 82 | + 'Sculpture', |
|
| 83 | + 'Season', |
|
| 84 | + 'SheetMusic', |
|
| 85 | + 'ShortStory', |
|
| 86 | + 'SoftwareApplication', |
|
| 87 | + 'SoftwareSourceCode', |
|
| 88 | + 'SpecialAnnouncement', |
|
| 89 | + 'Thesis', |
|
| 90 | + 'TvSeason', |
|
| 91 | + 'TvSeries', |
|
| 92 | + 'VisualArtwork', |
|
| 93 | + 'WebContent', |
|
| 94 | + 'WebPage', |
|
| 95 | + 'WebPageElement', |
|
| 96 | + 'WebSite', |
|
| 97 | + 'AdvertiserContentArticle', |
|
| 98 | + 'NewsArticle', |
|
| 99 | + 'Report', |
|
| 100 | + 'SatiricalArticle', |
|
| 101 | + 'ScholarlyArticle', |
|
| 102 | + 'SocialMediaPosting', |
|
| 103 | + 'TechArticle', |
|
| 104 | + 'AnalysisNewsArticle', |
|
| 105 | + 'AskPublicNewsArticle', |
|
| 106 | + 'BackgroundNewsArticle', |
|
| 107 | + 'OpinionNewsArticle', |
|
| 108 | + 'ReportageNewsArticle', |
|
| 109 | + 'ReviewNewsArticle', |
|
| 110 | + 'MedicalScholarlyArticle', |
|
| 111 | + 'BlogPosting', |
|
| 112 | + 'DiscussionForumPosting', |
|
| 113 | + 'LiveBlogPosting', |
|
| 114 | + 'ApiReference', |
|
| 115 | + 'Audiobook', |
|
| 116 | + 'MovieClip', |
|
| 117 | + 'RadioClip', |
|
| 118 | + 'TvClip', |
|
| 119 | + 'VideoGameClip', |
|
| 120 | + 'ProductCollection', |
|
| 121 | + 'ComicCoverArt', |
|
| 122 | + 'Answer', |
|
| 123 | + 'CorrectionComment', |
|
| 124 | + 'Question', |
|
| 125 | + 'PodcastSeason', |
|
| 126 | + 'RadioSeason', |
|
| 127 | + 'TvSeason', |
|
| 128 | + 'BookSeries', |
|
| 129 | + 'MovieSeries', |
|
| 130 | + 'Periodical', |
|
| 131 | + 'PodcastSeries', |
|
| 132 | + 'RadioSeries', |
|
| 133 | + 'TvSeries', |
|
| 134 | + 'VideoGameSeries', |
|
| 135 | + 'ComicSeries', |
|
| 136 | + 'Newspaper', |
|
| 137 | + 'DataFeed', |
|
| 138 | + 'CompleteDataFeed', |
|
| 139 | + 'CategoryCodeSet', |
|
| 140 | + 'NoteDigitalDocument', |
|
| 141 | + 'PresentationDigitalDocument', |
|
| 142 | + 'SpreadsheetDigitalDocument', |
|
| 143 | + 'TextDigitalDocument', |
|
| 144 | + 'PodcastEpisode', |
|
| 145 | + 'RadioEpisode', |
|
| 146 | + 'TvEpisode', |
|
| 147 | + 'VideoGame', |
|
| 148 | + 'Recipe', |
|
| 149 | + 'Course', |
|
| 150 | + 'Quiz', |
|
| 151 | + 'LegislationObject', |
|
| 152 | + 'AudioObject', |
|
| 153 | + 'DModel', |
|
| 154 | + 'DataDownload', |
|
| 155 | + 'ImageObject', |
|
| 156 | + 'LegislationObject', |
|
| 157 | + 'MusicVideoObject', |
|
| 158 | + 'VideoObject', |
|
| 159 | + 'Audiobook', |
|
| 160 | + 'Barcode', |
|
| 161 | + 'EmailMessage', |
|
| 162 | + 'MusicAlbum', |
|
| 163 | + 'MusicRelease', |
|
| 164 | + 'ComicIssue', |
|
| 165 | + 'ClaimReview', |
|
| 166 | + 'CriticReview', |
|
| 167 | + 'EmployerReview', |
|
| 168 | + 'MediaReview', |
|
| 169 | + 'Recommendation', |
|
| 170 | + 'UserReview', |
|
| 171 | + 'ReviewNewsArticle', |
|
| 172 | + 'MobileApplication', |
|
| 173 | + 'VideoGame', |
|
| 174 | + 'WebApplication', |
|
| 175 | + 'CoverArt', |
|
| 176 | + 'ComicCoverArt', |
|
| 177 | + 'HealthTopicContent', |
|
| 178 | + 'AboutPage', |
|
| 179 | + 'CheckoutPage', |
|
| 180 | + 'CollectionPage', |
|
| 181 | + 'ContactPage', |
|
| 182 | + 'FaqPage', |
|
| 183 | + 'ItemPage', |
|
| 184 | + 'MedicalWebPage', |
|
| 185 | + 'ProfilePage', |
|
| 186 | + 'QaPage', |
|
| 187 | + 'RealEstateListing', |
|
| 188 | + 'SearchResultsPage', |
|
| 189 | + 'MediaGallery', |
|
| 190 | + 'ImageGallery', |
|
| 191 | + 'VideoGallery', |
|
| 192 | + 'SiteNavigationElement', |
|
| 193 | + 'Table', |
|
| 194 | + 'WpAdBlock', |
|
| 195 | + 'WpFooter', |
|
| 196 | + 'WpHeader', |
|
| 197 | + 'WpSideBar', |
|
| 198 | + ); |
|
| 199 | + /** |
|
| 200 | + * The singleton instance for the JSON-LD service. |
|
| 201 | + * |
|
| 202 | + * @since 3.15.1 |
|
| 203 | + * |
|
| 204 | + * @var \Wordlift_Jsonld_Service $instance The singleton instance for the JSON-LD service. |
|
| 205 | + */ |
|
| 206 | + private static $instance; |
|
| 207 | + /** |
|
| 208 | + * A {@link Wordlift_Entity_Service} instance. |
|
| 209 | + * |
|
| 210 | + * @since 3.8.0 |
|
| 211 | + * @access private |
|
| 212 | + * @var Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 213 | + */ |
|
| 214 | + private $entity_service; |
|
| 215 | + /** |
|
| 216 | + * A {@link Wordlift_Term_JsonLd_Adapter} instance. |
|
| 217 | + * |
|
| 218 | + * @since 3.32.0 |
|
| 219 | + * @access private |
|
| 220 | + * @var Wordlift_Term_JsonLd_Adapter $entity_service A {@link Wordlift_Term_JsonLd_Adapter} instance. |
|
| 221 | + */ |
|
| 222 | + private $term_jsonld_adapter; |
|
| 223 | + /** |
|
| 224 | + * A {@link Wordlift_Post_Converter} instance. |
|
| 225 | + * |
|
| 226 | + * @since 3.8.0 |
|
| 227 | + * @access private |
|
| 228 | + * @var \Wordlift_Post_Converter A {@link Wordlift_Post_Converter} instance. |
|
| 229 | + */ |
|
| 230 | + private $converter; |
|
| 231 | + /** |
|
| 232 | + * A {@link Wordlift_Website_Jsonld_Converter} instance. |
|
| 233 | + * |
|
| 234 | + * @since 3.14.0 |
|
| 235 | + * @access private |
|
| 236 | + * @var \Wordlift_Website_Jsonld_Converter A {@link Wordlift_Website_Jsonld_Converter} instance. |
|
| 237 | + */ |
|
| 238 | + private $website_converter; |
|
| 239 | + /** |
|
| 240 | + * The {@link Api_Service} used to communicate with the remote APIs. |
|
| 241 | + * |
|
| 242 | + * @access private |
|
| 243 | + * @var Default_Api_Service |
|
| 244 | + */ |
|
| 245 | + private $api_service; |
|
| 246 | + |
|
| 247 | + /** |
|
| 248 | + * Create a JSON-LD service. |
|
| 249 | + * |
|
| 250 | + * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 251 | + * @param \Wordlift_Post_Converter $converter A {@link Wordlift_Uri_To_Jsonld_Converter} instance. |
|
| 252 | + * @param \Wordlift_Website_Jsonld_Converter $website_converter A {@link Wordlift_Website_Jsonld_Converter} instance. |
|
| 253 | + * @param \Wordlift_Term_JsonLd_Adapter $term_jsonld_adapter |
|
| 254 | + * |
|
| 255 | + * @since 3.8.0 |
|
| 256 | + */ |
|
| 257 | + public function __construct( $entity_service, $converter, $website_converter, $term_jsonld_adapter ) { |
|
| 258 | + |
|
| 259 | + $this->api_service = Default_Api_Service::get_instance(); |
|
| 260 | + $this->entity_service = $entity_service; |
|
| 261 | + $this->converter = $converter; |
|
| 262 | + $this->website_converter = $website_converter; |
|
| 263 | + $this->term_jsonld_adapter = $term_jsonld_adapter; |
|
| 264 | + self::$instance = $this; |
|
| 265 | + |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + /** |
|
| 269 | + * Get the singleton instance for the JSON-LD service. |
|
| 270 | + * |
|
| 271 | + * @return \Wordlift_Jsonld_Service The singleton instance for the JSON-LD service. |
|
| 272 | + * @since 3.15.1 |
|
| 273 | + */ |
|
| 274 | + public static function get_instance() { |
|
| 275 | + |
|
| 276 | + return self::$instance; |
|
| 277 | + } |
|
| 278 | + |
|
| 279 | + /** |
|
| 280 | + * Process calls to the AJAX 'wl_jsonld' endpoint. |
|
| 281 | + * |
|
| 282 | + * @since 3.8.0 |
|
| 283 | + */ |
|
| 284 | + public function get() { |
|
| 285 | + // Clear the buffer to be sure someone doesn't mess with our response. |
|
| 286 | + // |
|
| 287 | + // See https://github.com/insideout10/wordlift-plugin/issues/406. |
|
| 288 | + // See https://codex.wordpress.org/AJAX_in_Plugins. |
|
| 289 | + // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
| 290 | + @ob_clean(); |
|
| 291 | + |
|
| 292 | + // Get the parameter from the request. |
|
| 293 | + $is_homepage = isset( $_REQUEST['homepage'] ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 294 | + $post_id = isset( $_REQUEST['id'] ) && is_numeric( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : null; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 295 | + |
|
| 296 | + // Send the generated JSON-LD. |
|
| 297 | + $this->send_jsonld( $this->get_jsonld( $is_homepage, $post_id ) ); |
|
| 298 | + |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + /** |
|
| 302 | + * A close of WP's own `wp_send_json` function which uses `application/ld+json` as content type. |
|
| 303 | + * |
|
| 304 | + * @param mixed $response Variable (usually an array or object) to encode as JSON, |
|
| 305 | + * then print and die. |
|
| 306 | + * |
|
| 307 | + * @since 3.18.5 |
|
| 308 | + */ |
|
| 309 | + private function send_jsonld( $response ) { |
|
| 310 | + // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
| 311 | + @header( 'Content-Type: application/ld+json; charset=' . get_option( 'blog_charset' ) ); |
|
| 312 | + echo wp_json_encode( $response ); |
|
| 313 | + if ( apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { |
|
| 314 | + wp_die(); |
|
| 315 | + } else { |
|
| 316 | + die; |
|
| 317 | + } |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + /** |
|
| 321 | + * Get the JSON-LD. |
|
| 322 | + * |
|
| 323 | + * @param bool $is_homepage Whether the JSON-LD for the homepage is being requested. |
|
| 324 | + * @param int|null $post_id The JSON-LD for the specified {@link WP_Post} id. |
|
| 325 | + * @param int $context A context for the JSON-LD generation, valid values in Jsonld_Context_Enum. |
|
| 326 | + * |
|
| 327 | + * @return array A JSON-LD structure. |
|
| 328 | + * @since 3.15.1 |
|
| 329 | + */ |
|
| 330 | + public function get_jsonld( $is_homepage = false, $post_id = null, $context = Jsonld_Context_Enum::UNKNOWN ) { |
|
| 331 | + // Tell NewRelic to ignore us, otherwise NewRelic customers might receive |
|
| 332 | + // e-mails with a low apdex score. |
|
| 333 | + // |
|
| 334 | + // See https://github.com/insideout10/wordlift-plugin/issues/521 |
|
| 335 | + Wordlift_NewRelic_Adapter::ignore_apdex(); |
|
| 336 | + |
|
| 337 | + // Switch to Website converter if is home page. |
|
| 338 | + if ( $is_homepage ) { |
|
| 339 | + /** |
|
| 340 | + * Filter: 'wordlift_disable_website_json_ld' - Allow disabling of the json+ld output. |
|
| 341 | + * |
|
| 342 | + * @since 3.14.0 |
|
| 343 | + * @api bool $display_search Whether or not to display json+ld search on the frontend. |
|
| 344 | + */ |
|
| 345 | + if ( apply_filters( 'wordlift_disable_website_json_ld', false ) ) { |
|
| 346 | + return array(); |
|
| 347 | + } |
|
| 348 | + |
|
| 349 | + // Set a reference to the website_converter. |
|
| 350 | + $website_converter = $this->website_converter; |
|
| 351 | + |
|
| 352 | + // Send JSON-LD. |
|
| 353 | + return $website_converter->create_schema(); |
|
| 354 | + } |
|
| 355 | + |
|
| 356 | + // If no id has been provided return an empty array. |
|
| 357 | + if ( ! isset( $post_id ) ) { |
|
| 358 | + return array(); |
|
| 359 | + } |
|
| 360 | + |
|
| 361 | + // An array of references which is captured when converting an URI to a |
|
| 362 | + // json which we gather to further expand our json-ld. |
|
| 363 | + $references = array(); |
|
| 364 | + $references_infos = array(); |
|
| 365 | + |
|
| 366 | + // Set a reference to the entity_to_jsonld_converter to use in the closures. |
|
| 367 | + $entity_to_jsonld_converter = $this->converter; |
|
| 368 | + |
|
| 369 | + $relations = new Relations(); |
|
| 370 | + $jsonld = $entity_to_jsonld_converter->convert( $post_id, $references, $references_infos, $relations ); |
|
| 371 | + |
|
| 372 | + $graph = new Graph( $jsonld, $entity_to_jsonld_converter, Wordlift_Term_JsonLd_Adapter::get_instance() ); |
|
| 373 | + |
|
| 374 | + $schema_type = is_array( $jsonld['@type'] ) ? $jsonld['@type'] : array( $jsonld['@type'] ); |
|
| 375 | + |
|
| 376 | + // Add `about`/`mentions` only for `CreativeWork` and descendants. |
|
| 377 | + if ( array_intersect( $schema_type, self::$creative_work_types ) ) { |
|
| 378 | + |
|
| 379 | + foreach ( $relations->toArray() as $relation ) { |
|
| 380 | + |
|
| 381 | + // Setting about or mentions by label match is currently supported only for posts |
|
| 382 | + if ( Object_Type_Enum::POST !== $relation->get_object()->get_type() ) { |
|
| 383 | + continue; |
|
| 384 | + } |
|
| 385 | + |
|
| 386 | + // Add the `mentions`/`about` prop. |
|
| 387 | + $this->add_mention_or_about( $jsonld, $post_id, $relation ); |
|
| 388 | + } |
|
| 389 | + $graph->set_main_jsonld( $jsonld ); |
|
| 390 | + } |
|
| 391 | + |
|
| 392 | + $jsonld_arr = $graph->add_references( $references ) |
|
| 393 | + ->add_relations( $relations ) |
|
| 394 | + ->add_required_reference_infos( $references_infos ) |
|
| 395 | + ->render( $context ); |
|
| 396 | + |
|
| 397 | + /** |
|
| 398 | + * Filter name: wl_after_get_jsonld |
|
| 399 | + * |
|
| 400 | + * @return array |
|
| 401 | + * @since 3.27.2 |
|
| 402 | + * @var $jsonld_arr array The final jsonld before outputting to page. |
|
| 403 | + * @var $post_id int The post id for which the jsonld is generated. |
|
| 404 | + */ |
|
| 405 | + $jsonld_arr = apply_filters( 'wl_after_get_jsonld', $jsonld_arr, $post_id, $context ); |
|
| 406 | + |
|
| 407 | + $this->set_events_request( $jsonld_arr, $post_id, $context ); |
|
| 408 | + |
|
| 409 | + return $jsonld_arr; |
|
| 410 | + } |
|
| 411 | + |
|
| 412 | + /** |
|
| 413 | + * Write the JSON-LD in the head. |
|
| 414 | + * |
|
| 415 | + * This function isn't actually used, but may be used to quickly enable writing the JSON-LD synchronously to the |
|
| 416 | + * document head, using the `wp_head` hook. |
|
| 417 | + * |
|
| 418 | + * @since 3.18.5 |
|
| 419 | + */ |
|
| 420 | + public function wp_head() { |
|
| 421 | + |
|
| 422 | + // Determine whether this is the home page or whether we're displaying a single post. |
|
| 423 | + $is_homepage = is_home() || is_front_page(); |
|
| 424 | + $post_id = is_singular() ? get_the_ID() : null; |
|
| 425 | + |
|
| 426 | + $jsonld = wp_json_encode( $this->get_jsonld( $is_homepage, $post_id, Jsonld_Context_Enum::PAGE ) ); |
|
| 427 | + ?> |
|
| 428 | 428 | <script type="application/ld+json"><?php echo esc_html( $jsonld ); ?></script> |
| 429 | 429 | <?php |
| 430 | - } |
|
| 431 | - |
|
| 432 | - /** |
|
| 433 | - * @param array $jsonld |
|
| 434 | - * @param Relation $relation |
|
| 435 | - * |
|
| 436 | - * @return void |
|
| 437 | - */ |
|
| 438 | - private function add_mention_or_about( &$jsonld, $post_id, $relation ) { |
|
| 439 | - $content_service = Wordpress_Content_Service::get_instance(); |
|
| 440 | - $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 441 | - |
|
| 442 | - $object = $relation->get_object(); |
|
| 443 | - $entity_uri = $content_service->get_entity_id( $object ); |
|
| 444 | - $labels = $entity_service->get_labels( $object->get_id(), $object->get_type() ); |
|
| 445 | - |
|
| 446 | - $escaped_labels = array_map( |
|
| 447 | - function ( $value ) { |
|
| 448 | - return preg_quote( $value, '/' ); |
|
| 449 | - }, |
|
| 450 | - $labels |
|
| 451 | - ); |
|
| 452 | - |
|
| 453 | - $matches = false; |
|
| 454 | - |
|
| 455 | - // When the title is empty, then we shouldn't yield a match to about section. |
|
| 456 | - if ( array_filter( $escaped_labels ) ) { |
|
| 457 | - // Check if the labels match any part of the title. |
|
| 458 | - $post = get_post( $post_id ); |
|
| 459 | - $matches = $this->check_title_match( $escaped_labels, $post->post_title ); |
|
| 460 | - } |
|
| 461 | - |
|
| 462 | - if ( $entity_uri ) { |
|
| 463 | - // If the title matches, assign the entity to the about, otherwise to the mentions. |
|
| 464 | - $property_name = $matches ? 'about' : 'mentions'; |
|
| 465 | - $jsonld[ $property_name ] = isset( $jsonld[ $property_name ] ) ? (array) $jsonld[ $property_name ] : array(); |
|
| 466 | - $jsonld[ $property_name ][] = array( '@id' => $entity_uri ); |
|
| 467 | - } |
|
| 468 | - |
|
| 469 | - } |
|
| 470 | - |
|
| 471 | - /** |
|
| 472 | - * Check if the labels match any part of the title. |
|
| 473 | - * |
|
| 474 | - * @param $labels array The labels to check. |
|
| 475 | - * @param $title string The title to check. |
|
| 476 | - * |
|
| 477 | - * @return boolean |
|
| 478 | - */ |
|
| 479 | - public function check_title_match( $labels, $title ) { |
|
| 480 | - |
|
| 481 | - // If the title is empty, then we shouldn't yield a match to about section. |
|
| 482 | - if ( empty( $title ) ) { |
|
| 483 | - return false; |
|
| 484 | - } |
|
| 485 | - |
|
| 486 | - // Check if the labels match any part of the title. |
|
| 487 | - return 1 === preg_match( '/\b(' . implode( '|', $labels ) . ')\b/iu', $title ); |
|
| 488 | - |
|
| 489 | - } |
|
| 490 | - |
|
| 491 | - /** |
|
| 492 | - * Set events request. |
|
| 493 | - * |
|
| 494 | - * @param $jsonld_arr array The final jsonld before outputting to page. |
|
| 495 | - * @param $post_id int The post id for which the jsonld is generated. |
|
| 496 | - * @param $context int A context for the JSON-LD generation, valid values in Jsonld_Context_Enum |
|
| 497 | - */ |
|
| 498 | - private function set_events_request( $jsonld_arr, $post_id, $context ) { |
|
| 499 | - // If context is not PAGE or the array is empty, return early. |
|
| 500 | - if ( Jsonld_Context_Enum::PAGE !== $context || empty( $jsonld_arr[0] ) ) { |
|
| 501 | - return; |
|
| 502 | - } |
|
| 503 | - |
|
| 504 | - // Flag to indicate if we should make an API request. |
|
| 505 | - $change_status = false; |
|
| 506 | - |
|
| 507 | - // Get data from the array. |
|
| 508 | - $data = $jsonld_arr[0]; |
|
| 509 | - |
|
| 510 | - // Fetch the initial 'about' and 'mentions' counts from post meta. |
|
| 511 | - $counts = [ |
|
| 512 | - 'about' => get_post_meta( $post_id, 'wl_about_count', true ) ? : 0, |
|
| 513 | - 'mentions' => get_post_meta( $post_id, 'wl_mentions_count', true ) ? : 0, |
|
| 514 | - ]; |
|
| 515 | - |
|
| 516 | - // Iterate over the counts array. |
|
| 517 | - foreach ( $counts as $key => $count ) { |
|
| 518 | - // Check if data has 'about' or 'mentions' and the count is different from the existing meta value. |
|
| 519 | - if ( ! empty( $data[ $key ] ) ) { |
|
| 520 | - $new_count = count( $data[ $key ] ); |
|
| 521 | - if ( $count !== $new_count ) { |
|
| 522 | - // Set flag to true if counts have changed. |
|
| 523 | - $change_status = true; |
|
| 524 | - |
|
| 525 | - // Update the counts array with new count. |
|
| 526 | - $counts[ $key ] = $new_count; |
|
| 527 | - |
|
| 528 | - // Update post meta with new count. |
|
| 529 | - update_post_meta( $post_id, 'wl_' . $key . '_count', $new_count ); |
|
| 530 | - } |
|
| 531 | - } |
|
| 532 | - } |
|
| 533 | - |
|
| 534 | - // If the count has changed, make the API request. |
|
| 535 | - if ( $change_status ) { |
|
| 536 | - $this->api_service->request( |
|
| 537 | - 'POST', |
|
| 538 | - '/plugin/events', |
|
| 539 | - [ 'Content-Type' => 'application/json' ], |
|
| 540 | - wp_json_encode( [ |
|
| 541 | - 'source' => 'jsonld', |
|
| 542 | - 'args' => [ |
|
| 543 | - [ 'about_count' => $counts['about'] ], |
|
| 544 | - [ 'mentions_count' => $counts['mentions'] ], |
|
| 545 | - ], |
|
| 546 | - 'url' => get_permalink( $post_id ), |
|
| 547 | - ] ), |
|
| 548 | - 0.001, |
|
| 549 | - null, |
|
| 550 | - [ 'blocking' => false ] |
|
| 551 | - ); |
|
| 552 | - } |
|
| 553 | - } |
|
| 430 | + } |
|
| 431 | + |
|
| 432 | + /** |
|
| 433 | + * @param array $jsonld |
|
| 434 | + * @param Relation $relation |
|
| 435 | + * |
|
| 436 | + * @return void |
|
| 437 | + */ |
|
| 438 | + private function add_mention_or_about( &$jsonld, $post_id, $relation ) { |
|
| 439 | + $content_service = Wordpress_Content_Service::get_instance(); |
|
| 440 | + $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 441 | + |
|
| 442 | + $object = $relation->get_object(); |
|
| 443 | + $entity_uri = $content_service->get_entity_id( $object ); |
|
| 444 | + $labels = $entity_service->get_labels( $object->get_id(), $object->get_type() ); |
|
| 445 | + |
|
| 446 | + $escaped_labels = array_map( |
|
| 447 | + function ( $value ) { |
|
| 448 | + return preg_quote( $value, '/' ); |
|
| 449 | + }, |
|
| 450 | + $labels |
|
| 451 | + ); |
|
| 452 | + |
|
| 453 | + $matches = false; |
|
| 454 | + |
|
| 455 | + // When the title is empty, then we shouldn't yield a match to about section. |
|
| 456 | + if ( array_filter( $escaped_labels ) ) { |
|
| 457 | + // Check if the labels match any part of the title. |
|
| 458 | + $post = get_post( $post_id ); |
|
| 459 | + $matches = $this->check_title_match( $escaped_labels, $post->post_title ); |
|
| 460 | + } |
|
| 461 | + |
|
| 462 | + if ( $entity_uri ) { |
|
| 463 | + // If the title matches, assign the entity to the about, otherwise to the mentions. |
|
| 464 | + $property_name = $matches ? 'about' : 'mentions'; |
|
| 465 | + $jsonld[ $property_name ] = isset( $jsonld[ $property_name ] ) ? (array) $jsonld[ $property_name ] : array(); |
|
| 466 | + $jsonld[ $property_name ][] = array( '@id' => $entity_uri ); |
|
| 467 | + } |
|
| 468 | + |
|
| 469 | + } |
|
| 470 | + |
|
| 471 | + /** |
|
| 472 | + * Check if the labels match any part of the title. |
|
| 473 | + * |
|
| 474 | + * @param $labels array The labels to check. |
|
| 475 | + * @param $title string The title to check. |
|
| 476 | + * |
|
| 477 | + * @return boolean |
|
| 478 | + */ |
|
| 479 | + public function check_title_match( $labels, $title ) { |
|
| 480 | + |
|
| 481 | + // If the title is empty, then we shouldn't yield a match to about section. |
|
| 482 | + if ( empty( $title ) ) { |
|
| 483 | + return false; |
|
| 484 | + } |
|
| 485 | + |
|
| 486 | + // Check if the labels match any part of the title. |
|
| 487 | + return 1 === preg_match( '/\b(' . implode( '|', $labels ) . ')\b/iu', $title ); |
|
| 488 | + |
|
| 489 | + } |
|
| 490 | + |
|
| 491 | + /** |
|
| 492 | + * Set events request. |
|
| 493 | + * |
|
| 494 | + * @param $jsonld_arr array The final jsonld before outputting to page. |
|
| 495 | + * @param $post_id int The post id for which the jsonld is generated. |
|
| 496 | + * @param $context int A context for the JSON-LD generation, valid values in Jsonld_Context_Enum |
|
| 497 | + */ |
|
| 498 | + private function set_events_request( $jsonld_arr, $post_id, $context ) { |
|
| 499 | + // If context is not PAGE or the array is empty, return early. |
|
| 500 | + if ( Jsonld_Context_Enum::PAGE !== $context || empty( $jsonld_arr[0] ) ) { |
|
| 501 | + return; |
|
| 502 | + } |
|
| 503 | + |
|
| 504 | + // Flag to indicate if we should make an API request. |
|
| 505 | + $change_status = false; |
|
| 506 | + |
|
| 507 | + // Get data from the array. |
|
| 508 | + $data = $jsonld_arr[0]; |
|
| 509 | + |
|
| 510 | + // Fetch the initial 'about' and 'mentions' counts from post meta. |
|
| 511 | + $counts = [ |
|
| 512 | + 'about' => get_post_meta( $post_id, 'wl_about_count', true ) ? : 0, |
|
| 513 | + 'mentions' => get_post_meta( $post_id, 'wl_mentions_count', true ) ? : 0, |
|
| 514 | + ]; |
|
| 515 | + |
|
| 516 | + // Iterate over the counts array. |
|
| 517 | + foreach ( $counts as $key => $count ) { |
|
| 518 | + // Check if data has 'about' or 'mentions' and the count is different from the existing meta value. |
|
| 519 | + if ( ! empty( $data[ $key ] ) ) { |
|
| 520 | + $new_count = count( $data[ $key ] ); |
|
| 521 | + if ( $count !== $new_count ) { |
|
| 522 | + // Set flag to true if counts have changed. |
|
| 523 | + $change_status = true; |
|
| 524 | + |
|
| 525 | + // Update the counts array with new count. |
|
| 526 | + $counts[ $key ] = $new_count; |
|
| 527 | + |
|
| 528 | + // Update post meta with new count. |
|
| 529 | + update_post_meta( $post_id, 'wl_' . $key . '_count', $new_count ); |
|
| 530 | + } |
|
| 531 | + } |
|
| 532 | + } |
|
| 533 | + |
|
| 534 | + // If the count has changed, make the API request. |
|
| 535 | + if ( $change_status ) { |
|
| 536 | + $this->api_service->request( |
|
| 537 | + 'POST', |
|
| 538 | + '/plugin/events', |
|
| 539 | + [ 'Content-Type' => 'application/json' ], |
|
| 540 | + wp_json_encode( [ |
|
| 541 | + 'source' => 'jsonld', |
|
| 542 | + 'args' => [ |
|
| 543 | + [ 'about_count' => $counts['about'] ], |
|
| 544 | + [ 'mentions_count' => $counts['mentions'] ], |
|
| 545 | + ], |
|
| 546 | + 'url' => get_permalink( $post_id ), |
|
| 547 | + ] ), |
|
| 548 | + 0.001, |
|
| 549 | + null, |
|
| 550 | + [ 'blocking' => false ] |
|
| 551 | + ); |
|
| 552 | + } |
|
| 553 | + } |
|
| 554 | 554 | |
| 555 | 555 | } |
@@ -254,7 +254,7 @@ discard block |
||
| 254 | 254 | * |
| 255 | 255 | * @since 3.8.0 |
| 256 | 256 | */ |
| 257 | - public function __construct( $entity_service, $converter, $website_converter, $term_jsonld_adapter ) { |
|
| 257 | + public function __construct($entity_service, $converter, $website_converter, $term_jsonld_adapter) { |
|
| 258 | 258 | |
| 259 | 259 | $this->api_service = Default_Api_Service::get_instance(); |
| 260 | 260 | $this->entity_service = $entity_service; |
@@ -290,11 +290,11 @@ discard block |
||
| 290 | 290 | @ob_clean(); |
| 291 | 291 | |
| 292 | 292 | // Get the parameter from the request. |
| 293 | - $is_homepage = isset( $_REQUEST['homepage'] ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 294 | - $post_id = isset( $_REQUEST['id'] ) && is_numeric( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : null; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 293 | + $is_homepage = isset($_REQUEST['homepage']); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 294 | + $post_id = isset($_REQUEST['id']) && is_numeric($_REQUEST['id']) ? intval($_REQUEST['id']) : null; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 295 | 295 | |
| 296 | 296 | // Send the generated JSON-LD. |
| 297 | - $this->send_jsonld( $this->get_jsonld( $is_homepage, $post_id ) ); |
|
| 297 | + $this->send_jsonld($this->get_jsonld($is_homepage, $post_id)); |
|
| 298 | 298 | |
| 299 | 299 | } |
| 300 | 300 | |
@@ -306,11 +306,11 @@ discard block |
||
| 306 | 306 | * |
| 307 | 307 | * @since 3.18.5 |
| 308 | 308 | */ |
| 309 | - private function send_jsonld( $response ) { |
|
| 309 | + private function send_jsonld($response) { |
|
| 310 | 310 | // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
| 311 | - @header( 'Content-Type: application/ld+json; charset=' . get_option( 'blog_charset' ) ); |
|
| 312 | - echo wp_json_encode( $response ); |
|
| 313 | - if ( apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { |
|
| 311 | + @header('Content-Type: application/ld+json; charset='.get_option('blog_charset')); |
|
| 312 | + echo wp_json_encode($response); |
|
| 313 | + if (apply_filters('wp_doing_ajax', defined('DOING_AJAX') && DOING_AJAX)) { |
|
| 314 | 314 | wp_die(); |
| 315 | 315 | } else { |
| 316 | 316 | die; |
@@ -327,7 +327,7 @@ discard block |
||
| 327 | 327 | * @return array A JSON-LD structure. |
| 328 | 328 | * @since 3.15.1 |
| 329 | 329 | */ |
| 330 | - public function get_jsonld( $is_homepage = false, $post_id = null, $context = Jsonld_Context_Enum::UNKNOWN ) { |
|
| 330 | + public function get_jsonld($is_homepage = false, $post_id = null, $context = Jsonld_Context_Enum::UNKNOWN) { |
|
| 331 | 331 | // Tell NewRelic to ignore us, otherwise NewRelic customers might receive |
| 332 | 332 | // e-mails with a low apdex score. |
| 333 | 333 | // |
@@ -335,14 +335,14 @@ discard block |
||
| 335 | 335 | Wordlift_NewRelic_Adapter::ignore_apdex(); |
| 336 | 336 | |
| 337 | 337 | // Switch to Website converter if is home page. |
| 338 | - if ( $is_homepage ) { |
|
| 338 | + if ($is_homepage) { |
|
| 339 | 339 | /** |
| 340 | 340 | * Filter: 'wordlift_disable_website_json_ld' - Allow disabling of the json+ld output. |
| 341 | 341 | * |
| 342 | 342 | * @since 3.14.0 |
| 343 | 343 | * @api bool $display_search Whether or not to display json+ld search on the frontend. |
| 344 | 344 | */ |
| 345 | - if ( apply_filters( 'wordlift_disable_website_json_ld', false ) ) { |
|
| 345 | + if (apply_filters('wordlift_disable_website_json_ld', false)) { |
|
| 346 | 346 | return array(); |
| 347 | 347 | } |
| 348 | 348 | |
@@ -354,7 +354,7 @@ discard block |
||
| 354 | 354 | } |
| 355 | 355 | |
| 356 | 356 | // If no id has been provided return an empty array. |
| 357 | - if ( ! isset( $post_id ) ) { |
|
| 357 | + if ( ! isset($post_id)) { |
|
| 358 | 358 | return array(); |
| 359 | 359 | } |
| 360 | 360 | |
@@ -367,32 +367,32 @@ discard block |
||
| 367 | 367 | $entity_to_jsonld_converter = $this->converter; |
| 368 | 368 | |
| 369 | 369 | $relations = new Relations(); |
| 370 | - $jsonld = $entity_to_jsonld_converter->convert( $post_id, $references, $references_infos, $relations ); |
|
| 370 | + $jsonld = $entity_to_jsonld_converter->convert($post_id, $references, $references_infos, $relations); |
|
| 371 | 371 | |
| 372 | - $graph = new Graph( $jsonld, $entity_to_jsonld_converter, Wordlift_Term_JsonLd_Adapter::get_instance() ); |
|
| 372 | + $graph = new Graph($jsonld, $entity_to_jsonld_converter, Wordlift_Term_JsonLd_Adapter::get_instance()); |
|
| 373 | 373 | |
| 374 | - $schema_type = is_array( $jsonld['@type'] ) ? $jsonld['@type'] : array( $jsonld['@type'] ); |
|
| 374 | + $schema_type = is_array($jsonld['@type']) ? $jsonld['@type'] : array($jsonld['@type']); |
|
| 375 | 375 | |
| 376 | 376 | // Add `about`/`mentions` only for `CreativeWork` and descendants. |
| 377 | - if ( array_intersect( $schema_type, self::$creative_work_types ) ) { |
|
| 377 | + if (array_intersect($schema_type, self::$creative_work_types)) { |
|
| 378 | 378 | |
| 379 | - foreach ( $relations->toArray() as $relation ) { |
|
| 379 | + foreach ($relations->toArray() as $relation) { |
|
| 380 | 380 | |
| 381 | 381 | // Setting about or mentions by label match is currently supported only for posts |
| 382 | - if ( Object_Type_Enum::POST !== $relation->get_object()->get_type() ) { |
|
| 382 | + if (Object_Type_Enum::POST !== $relation->get_object()->get_type()) { |
|
| 383 | 383 | continue; |
| 384 | 384 | } |
| 385 | 385 | |
| 386 | 386 | // Add the `mentions`/`about` prop. |
| 387 | - $this->add_mention_or_about( $jsonld, $post_id, $relation ); |
|
| 387 | + $this->add_mention_or_about($jsonld, $post_id, $relation); |
|
| 388 | 388 | } |
| 389 | - $graph->set_main_jsonld( $jsonld ); |
|
| 389 | + $graph->set_main_jsonld($jsonld); |
|
| 390 | 390 | } |
| 391 | 391 | |
| 392 | - $jsonld_arr = $graph->add_references( $references ) |
|
| 393 | - ->add_relations( $relations ) |
|
| 394 | - ->add_required_reference_infos( $references_infos ) |
|
| 395 | - ->render( $context ); |
|
| 392 | + $jsonld_arr = $graph->add_references($references) |
|
| 393 | + ->add_relations($relations) |
|
| 394 | + ->add_required_reference_infos($references_infos) |
|
| 395 | + ->render($context); |
|
| 396 | 396 | |
| 397 | 397 | /** |
| 398 | 398 | * Filter name: wl_after_get_jsonld |
@@ -402,9 +402,9 @@ discard block |
||
| 402 | 402 | * @var $jsonld_arr array The final jsonld before outputting to page. |
| 403 | 403 | * @var $post_id int The post id for which the jsonld is generated. |
| 404 | 404 | */ |
| 405 | - $jsonld_arr = apply_filters( 'wl_after_get_jsonld', $jsonld_arr, $post_id, $context ); |
|
| 405 | + $jsonld_arr = apply_filters('wl_after_get_jsonld', $jsonld_arr, $post_id, $context); |
|
| 406 | 406 | |
| 407 | - $this->set_events_request( $jsonld_arr, $post_id, $context ); |
|
| 407 | + $this->set_events_request($jsonld_arr, $post_id, $context); |
|
| 408 | 408 | |
| 409 | 409 | return $jsonld_arr; |
| 410 | 410 | } |
@@ -423,9 +423,9 @@ discard block |
||
| 423 | 423 | $is_homepage = is_home() || is_front_page(); |
| 424 | 424 | $post_id = is_singular() ? get_the_ID() : null; |
| 425 | 425 | |
| 426 | - $jsonld = wp_json_encode( $this->get_jsonld( $is_homepage, $post_id, Jsonld_Context_Enum::PAGE ) ); |
|
| 426 | + $jsonld = wp_json_encode($this->get_jsonld($is_homepage, $post_id, Jsonld_Context_Enum::PAGE)); |
|
| 427 | 427 | ?> |
| 428 | - <script type="application/ld+json"><?php echo esc_html( $jsonld ); ?></script> |
|
| 428 | + <script type="application/ld+json"><?php echo esc_html($jsonld); ?></script> |
|
| 429 | 429 | <?php |
| 430 | 430 | } |
| 431 | 431 | |
@@ -435,17 +435,17 @@ discard block |
||
| 435 | 435 | * |
| 436 | 436 | * @return void |
| 437 | 437 | */ |
| 438 | - private function add_mention_or_about( &$jsonld, $post_id, $relation ) { |
|
| 438 | + private function add_mention_or_about(&$jsonld, $post_id, $relation) { |
|
| 439 | 439 | $content_service = Wordpress_Content_Service::get_instance(); |
| 440 | 440 | $entity_service = Wordlift_Entity_Service::get_instance(); |
| 441 | 441 | |
| 442 | 442 | $object = $relation->get_object(); |
| 443 | - $entity_uri = $content_service->get_entity_id( $object ); |
|
| 444 | - $labels = $entity_service->get_labels( $object->get_id(), $object->get_type() ); |
|
| 443 | + $entity_uri = $content_service->get_entity_id($object); |
|
| 444 | + $labels = $entity_service->get_labels($object->get_id(), $object->get_type()); |
|
| 445 | 445 | |
| 446 | 446 | $escaped_labels = array_map( |
| 447 | - function ( $value ) { |
|
| 448 | - return preg_quote( $value, '/' ); |
|
| 447 | + function($value) { |
|
| 448 | + return preg_quote($value, '/'); |
|
| 449 | 449 | }, |
| 450 | 450 | $labels |
| 451 | 451 | ); |
@@ -453,17 +453,17 @@ discard block |
||
| 453 | 453 | $matches = false; |
| 454 | 454 | |
| 455 | 455 | // When the title is empty, then we shouldn't yield a match to about section. |
| 456 | - if ( array_filter( $escaped_labels ) ) { |
|
| 456 | + if (array_filter($escaped_labels)) { |
|
| 457 | 457 | // Check if the labels match any part of the title. |
| 458 | - $post = get_post( $post_id ); |
|
| 459 | - $matches = $this->check_title_match( $escaped_labels, $post->post_title ); |
|
| 458 | + $post = get_post($post_id); |
|
| 459 | + $matches = $this->check_title_match($escaped_labels, $post->post_title); |
|
| 460 | 460 | } |
| 461 | 461 | |
| 462 | - if ( $entity_uri ) { |
|
| 462 | + if ($entity_uri) { |
|
| 463 | 463 | // If the title matches, assign the entity to the about, otherwise to the mentions. |
| 464 | 464 | $property_name = $matches ? 'about' : 'mentions'; |
| 465 | - $jsonld[ $property_name ] = isset( $jsonld[ $property_name ] ) ? (array) $jsonld[ $property_name ] : array(); |
|
| 466 | - $jsonld[ $property_name ][] = array( '@id' => $entity_uri ); |
|
| 465 | + $jsonld[$property_name] = isset($jsonld[$property_name]) ? (array) $jsonld[$property_name] : array(); |
|
| 466 | + $jsonld[$property_name][] = array('@id' => $entity_uri); |
|
| 467 | 467 | } |
| 468 | 468 | |
| 469 | 469 | } |
@@ -476,15 +476,15 @@ discard block |
||
| 476 | 476 | * |
| 477 | 477 | * @return boolean |
| 478 | 478 | */ |
| 479 | - public function check_title_match( $labels, $title ) { |
|
| 479 | + public function check_title_match($labels, $title) { |
|
| 480 | 480 | |
| 481 | 481 | // If the title is empty, then we shouldn't yield a match to about section. |
| 482 | - if ( empty( $title ) ) { |
|
| 482 | + if (empty($title)) { |
|
| 483 | 483 | return false; |
| 484 | 484 | } |
| 485 | 485 | |
| 486 | 486 | // Check if the labels match any part of the title. |
| 487 | - return 1 === preg_match( '/\b(' . implode( '|', $labels ) . ')\b/iu', $title ); |
|
| 487 | + return 1 === preg_match('/\b('.implode('|', $labels).')\b/iu', $title); |
|
| 488 | 488 | |
| 489 | 489 | } |
| 490 | 490 | |
@@ -495,9 +495,9 @@ discard block |
||
| 495 | 495 | * @param $post_id int The post id for which the jsonld is generated. |
| 496 | 496 | * @param $context int A context for the JSON-LD generation, valid values in Jsonld_Context_Enum |
| 497 | 497 | */ |
| 498 | - private function set_events_request( $jsonld_arr, $post_id, $context ) { |
|
| 498 | + private function set_events_request($jsonld_arr, $post_id, $context) { |
|
| 499 | 499 | // If context is not PAGE or the array is empty, return early. |
| 500 | - if ( Jsonld_Context_Enum::PAGE !== $context || empty( $jsonld_arr[0] ) ) { |
|
| 500 | + if (Jsonld_Context_Enum::PAGE !== $context || empty($jsonld_arr[0])) { |
|
| 501 | 501 | return; |
| 502 | 502 | } |
| 503 | 503 | |
@@ -509,45 +509,45 @@ discard block |
||
| 509 | 509 | |
| 510 | 510 | // Fetch the initial 'about' and 'mentions' counts from post meta. |
| 511 | 511 | $counts = [ |
| 512 | - 'about' => get_post_meta( $post_id, 'wl_about_count', true ) ? : 0, |
|
| 513 | - 'mentions' => get_post_meta( $post_id, 'wl_mentions_count', true ) ? : 0, |
|
| 512 | + 'about' => get_post_meta($post_id, 'wl_about_count', true) ?: 0, |
|
| 513 | + 'mentions' => get_post_meta($post_id, 'wl_mentions_count', true) ?: 0, |
|
| 514 | 514 | ]; |
| 515 | 515 | |
| 516 | 516 | // Iterate over the counts array. |
| 517 | - foreach ( $counts as $key => $count ) { |
|
| 517 | + foreach ($counts as $key => $count) { |
|
| 518 | 518 | // Check if data has 'about' or 'mentions' and the count is different from the existing meta value. |
| 519 | - if ( ! empty( $data[ $key ] ) ) { |
|
| 520 | - $new_count = count( $data[ $key ] ); |
|
| 521 | - if ( $count !== $new_count ) { |
|
| 519 | + if ( ! empty($data[$key])) { |
|
| 520 | + $new_count = count($data[$key]); |
|
| 521 | + if ($count !== $new_count) { |
|
| 522 | 522 | // Set flag to true if counts have changed. |
| 523 | 523 | $change_status = true; |
| 524 | 524 | |
| 525 | 525 | // Update the counts array with new count. |
| 526 | - $counts[ $key ] = $new_count; |
|
| 526 | + $counts[$key] = $new_count; |
|
| 527 | 527 | |
| 528 | 528 | // Update post meta with new count. |
| 529 | - update_post_meta( $post_id, 'wl_' . $key . '_count', $new_count ); |
|
| 529 | + update_post_meta($post_id, 'wl_'.$key.'_count', $new_count); |
|
| 530 | 530 | } |
| 531 | 531 | } |
| 532 | 532 | } |
| 533 | 533 | |
| 534 | 534 | // If the count has changed, make the API request. |
| 535 | - if ( $change_status ) { |
|
| 535 | + if ($change_status) { |
|
| 536 | 536 | $this->api_service->request( |
| 537 | 537 | 'POST', |
| 538 | 538 | '/plugin/events', |
| 539 | - [ 'Content-Type' => 'application/json' ], |
|
| 540 | - wp_json_encode( [ |
|
| 539 | + ['Content-Type' => 'application/json'], |
|
| 540 | + wp_json_encode([ |
|
| 541 | 541 | 'source' => 'jsonld', |
| 542 | 542 | 'args' => [ |
| 543 | - [ 'about_count' => $counts['about'] ], |
|
| 544 | - [ 'mentions_count' => $counts['mentions'] ], |
|
| 543 | + ['about_count' => $counts['about']], |
|
| 544 | + ['mentions_count' => $counts['mentions']], |
|
| 545 | 545 | ], |
| 546 | - 'url' => get_permalink( $post_id ), |
|
| 547 | - ] ), |
|
| 546 | + 'url' => get_permalink($post_id), |
|
| 547 | + ]), |
|
| 548 | 548 | 0.001, |
| 549 | 549 | null, |
| 550 | - [ 'blocking' => false ] |
|
| 550 | + ['blocking' => false] |
|
| 551 | 551 | ); |
| 552 | 552 | } |
| 553 | 553 | } |