@@ -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 | } |
@@ -59,9 +59,9 @@ discard block |
||
| 59 | 59 | * |
| 60 | 60 | * @since 3.20.0 |
| 61 | 61 | */ |
| 62 | - public function __construct( $entity_uri_service, $post_id_to_jsonld_converter ) { |
|
| 62 | + public function __construct($entity_uri_service, $post_id_to_jsonld_converter) { |
|
| 63 | 63 | |
| 64 | - add_action( 'wp_head', array( $this, 'wp_head' ) ); |
|
| 64 | + add_action('wp_head', array($this, 'wp_head')); |
|
| 65 | 65 | |
| 66 | 66 | $this->api_service = Default_Api_Service::get_instance(); |
| 67 | 67 | $this->entity_uri_service = $entity_uri_service; |
@@ -84,31 +84,31 @@ discard block |
||
| 84 | 84 | * |
| 85 | 85 | * @return array|boolean |
| 86 | 86 | */ |
| 87 | - public function get_carousel_jsonld( $id = null ) { |
|
| 88 | - $posts = $this->get_posts( $id ); |
|
| 87 | + public function get_carousel_jsonld($id = null) { |
|
| 88 | + $posts = $this->get_posts($id); |
|
| 89 | 89 | $post_jsonld = array(); |
| 90 | - if ( ! is_array( $posts ) || count( $posts ) < 2 ) { |
|
| 90 | + if ( ! is_array($posts) || count($posts) < 2) { |
|
| 91 | 91 | // Bail out if no posts are present. |
| 92 | 92 | return false; |
| 93 | 93 | } |
| 94 | 94 | |
| 95 | - if ( $id !== null ) { |
|
| 96 | - $term = get_term( $id ); |
|
| 97 | - $post_jsonld['description'] = wp_strip_all_tags( strip_shortcodes( $term->description ) ); |
|
| 98 | - $thumbnail_id = get_term_meta( $id, 'thumbnail_id', true ); |
|
| 99 | - if ( ! empty( $thumbnail_id ) ) { |
|
| 100 | - $post_jsonld['image'] = wp_get_attachment_url( $thumbnail_id ); |
|
| 95 | + if ($id !== null) { |
|
| 96 | + $term = get_term($id); |
|
| 97 | + $post_jsonld['description'] = wp_strip_all_tags(strip_shortcodes($term->description)); |
|
| 98 | + $thumbnail_id = get_term_meta($id, 'thumbnail_id', true); |
|
| 99 | + if ( ! empty($thumbnail_id)) { |
|
| 100 | + $post_jsonld['image'] = wp_get_attachment_url($thumbnail_id); |
|
| 101 | 101 | } |
| 102 | 102 | } |
| 103 | 103 | |
| 104 | 104 | // More than 2 items are present, so construct the post_jsonld data |
| 105 | 105 | $post_jsonld['@context'] = 'https://schema.org'; |
| 106 | 106 | $post_jsonld['@type'] = 'ItemList'; |
| 107 | - $post_jsonld['url'] = $this->get_term_url( $id ); |
|
| 107 | + $post_jsonld['url'] = $this->get_term_url($id); |
|
| 108 | 108 | $post_jsonld['itemListElement'] = array(); |
| 109 | 109 | $position = 1; |
| 110 | 110 | |
| 111 | - foreach ( $posts as $post_id ) { |
|
| 111 | + foreach ($posts as $post_id) { |
|
| 112 | 112 | $result = array( |
| 113 | 113 | '@type' => 'ListItem', |
| 114 | 114 | 'position' => $position, |
@@ -117,10 +117,10 @@ discard block |
||
| 117 | 117 | * |
| 118 | 118 | * See https://developers.google.com/search/docs/data-types/carousel |
| 119 | 119 | */ |
| 120 | - 'url' => apply_filters( 'wl_carousel_post_list_item_url', get_permalink( $post_id ), $post_id ), |
|
| 120 | + 'url' => apply_filters('wl_carousel_post_list_item_url', get_permalink($post_id), $post_id), |
|
| 121 | 121 | ); |
| 122 | - array_push( $post_jsonld['itemListElement'], $result ); |
|
| 123 | - ++ $position; |
|
| 122 | + array_push($post_jsonld['itemListElement'], $result); |
|
| 123 | + ++$position; |
|
| 124 | 124 | } |
| 125 | 125 | |
| 126 | 126 | return $post_jsonld; |
@@ -133,25 +133,25 @@ discard block |
||
| 133 | 133 | * |
| 134 | 134 | * @return array|int[]|string[]|WP_Error|null |
| 135 | 135 | */ |
| 136 | - private function get_posts( $id ) { |
|
| 136 | + private function get_posts($id) { |
|
| 137 | 137 | global $wp_query; |
| 138 | 138 | |
| 139 | - if ( $wp_query->posts !== null ) { |
|
| 139 | + if ($wp_query->posts !== null) { |
|
| 140 | 140 | return array_map( |
| 141 | - function ( $post ) { |
|
| 141 | + function($post) { |
|
| 142 | 142 | return $post->ID; |
| 143 | 143 | }, |
| 144 | 144 | $wp_query->posts |
| 145 | 145 | ); |
| 146 | 146 | } |
| 147 | 147 | |
| 148 | - if ( $id === null ) { |
|
| 148 | + if ($id === null) { |
|
| 149 | 149 | return null; |
| 150 | 150 | } |
| 151 | 151 | |
| 152 | - $term = get_term( $id ); |
|
| 152 | + $term = get_term($id); |
|
| 153 | 153 | |
| 154 | - return get_objects_in_term( $id, $term->taxonomy ); |
|
| 154 | + return get_objects_in_term($id, $term->taxonomy); |
|
| 155 | 155 | } |
| 156 | 156 | |
| 157 | 157 | /** |
@@ -163,28 +163,28 @@ discard block |
||
| 163 | 163 | $query_object = get_queried_object(); |
| 164 | 164 | |
| 165 | 165 | // Check if it is a term page. |
| 166 | - if ( ! $query_object instanceof WP_Term ) { |
|
| 166 | + if ( ! $query_object instanceof WP_Term) { |
|
| 167 | 167 | return; |
| 168 | 168 | } |
| 169 | 169 | |
| 170 | 170 | // Bail out if `wl_jsonld_enabled` isn't enabled. |
| 171 | - if ( ! apply_filters( 'wl_jsonld_enabled', true ) ) { |
|
| 171 | + if ( ! apply_filters('wl_jsonld_enabled', true)) { |
|
| 172 | 172 | return; |
| 173 | 173 | } |
| 174 | 174 | |
| 175 | 175 | $term_id = $query_object->term_id; |
| 176 | 176 | |
| 177 | - $jsonld = $this->get( $term_id, Jsonld_Context_Enum::PAGE ); |
|
| 177 | + $jsonld = $this->get($term_id, Jsonld_Context_Enum::PAGE); |
|
| 178 | 178 | |
| 179 | 179 | // Bail out if the JSON-LD is empty. |
| 180 | - if ( empty( $jsonld ) ) { |
|
| 180 | + if (empty($jsonld)) { |
|
| 181 | 181 | return; |
| 182 | 182 | } |
| 183 | 183 | |
| 184 | - $jsonld_string = wp_json_encode( $jsonld ); |
|
| 184 | + $jsonld_string = wp_json_encode($jsonld); |
|
| 185 | 185 | |
| 186 | - $jsonld_term_html_output = '<script type="application/ld+json" id="wl-jsonld-term">' . $jsonld_string . '</script>'; |
|
| 187 | - $jsonld_term_html_output = apply_filters( 'wl_jsonld_term_html_output', $jsonld_term_html_output, $term_id ); |
|
| 186 | + $jsonld_term_html_output = '<script type="application/ld+json" id="wl-jsonld-term">'.$jsonld_string.'</script>'; |
|
| 187 | + $jsonld_term_html_output = apply_filters('wl_jsonld_term_html_output', $jsonld_term_html_output, $term_id); |
|
| 188 | 188 | |
| 189 | 189 | echo $jsonld_term_html_output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- It's an application/ld+json output. |
| 190 | 190 | |
@@ -199,7 +199,7 @@ discard block |
||
| 199 | 199 | * |
| 200 | 200 | * @return array |
| 201 | 201 | */ |
| 202 | - public function get( $id, $context, $is_recursive_call = false ) { |
|
| 202 | + public function get($id, $context, $is_recursive_call = false) { |
|
| 203 | 203 | /** |
| 204 | 204 | * Support for carousel rich snippet, get jsonld data present |
| 205 | 205 | * for all the posts shown in the term page, and add the jsonld data |
@@ -211,17 +211,17 @@ discard block |
||
| 211 | 211 | */ |
| 212 | 212 | $jsonld_array = array(); |
| 213 | 213 | |
| 214 | - if ( Jsonld_Context_Enum::PAGE === $context ) { |
|
| 215 | - $carousel_data = $this->get_carousel_jsonld( $id ); |
|
| 216 | - if ( $carousel_data ) { |
|
| 214 | + if (Jsonld_Context_Enum::PAGE === $context) { |
|
| 215 | + $carousel_data = $this->get_carousel_jsonld($id); |
|
| 216 | + if ($carousel_data) { |
|
| 217 | 217 | $jsonld_array[] = $carousel_data; |
| 218 | 218 | } |
| 219 | 219 | } |
| 220 | 220 | |
| 221 | - $entities_jsonld_array = $this->get_entity_jsonld( $id, $context ); |
|
| 221 | + $entities_jsonld_array = $this->get_entity_jsonld($id, $context); |
|
| 222 | 222 | |
| 223 | 223 | $result = array( |
| 224 | - 'jsonld' => array_merge( $jsonld_array, $entities_jsonld_array ), |
|
| 224 | + 'jsonld' => array_merge($jsonld_array, $entities_jsonld_array), |
|
| 225 | 225 | 'references' => array(), |
| 226 | 226 | ); |
| 227 | 227 | |
@@ -231,22 +231,22 @@ discard block |
||
| 231 | 231 | * @var $id int Term id |
| 232 | 232 | * @var $jsonld_array array An array containing jsonld for term and entities. |
| 233 | 233 | */ |
| 234 | - $arr = apply_filters( 'wl_term_jsonld_array', $result, $id ); |
|
| 234 | + $arr = apply_filters('wl_term_jsonld_array', $result, $id); |
|
| 235 | 235 | |
| 236 | 236 | $references = array(); |
| 237 | 237 | |
| 238 | 238 | // Don't expand nested references, it will lead to an infinite loop. |
| 239 | - if ( ! $is_recursive_call ) { |
|
| 239 | + if ( ! $is_recursive_call) { |
|
| 240 | 240 | /** |
| 241 | 241 | * @since 3.32.0 |
| 242 | 242 | * Expand the references returned by this filter. |
| 243 | 243 | */ |
| 244 | - $references = $this->expand_references( $arr['references'] ); |
|
| 244 | + $references = $this->expand_references($arr['references']); |
|
| 245 | 245 | } |
| 246 | 246 | |
| 247 | - $jsonld_array = array_merge( $arr['jsonld'], $references ); |
|
| 247 | + $jsonld_array = array_merge($arr['jsonld'], $references); |
|
| 248 | 248 | |
| 249 | - $this->set_terms_request( $jsonld_array, $id, $context ); |
|
| 249 | + $this->set_terms_request($jsonld_array, $id, $context); |
|
| 250 | 250 | |
| 251 | 251 | return $jsonld_array; |
| 252 | 252 | } |
@@ -258,17 +258,17 @@ discard block |
||
| 258 | 258 | * |
| 259 | 259 | * @return array|false|int|mixed|string|WP_Error|WP_Term|null |
| 260 | 260 | */ |
| 261 | - private function get_term_url( $id ) { |
|
| 262 | - if ( null === $id ) { |
|
| 263 | - return isset( $_SERVER['REQUEST_URI'] ) ? filter_var( wp_unslash( $_SERVER['REQUEST_URI'] ), FILTER_SANITIZE_URL ) : ''; |
|
| 261 | + private function get_term_url($id) { |
|
| 262 | + if (null === $id) { |
|
| 263 | + return isset($_SERVER['REQUEST_URI']) ? filter_var(wp_unslash($_SERVER['REQUEST_URI']), FILTER_SANITIZE_URL) : ''; |
|
| 264 | 264 | } |
| 265 | 265 | |
| 266 | - $maybe_url = get_term_meta( $id, Wordlift_Url_Property_Service::META_KEY, true ); |
|
| 267 | - if ( ! empty( $maybe_url ) ) { |
|
| 266 | + $maybe_url = get_term_meta($id, Wordlift_Url_Property_Service::META_KEY, true); |
|
| 267 | + if ( ! empty($maybe_url)) { |
|
| 268 | 268 | return $maybe_url; |
| 269 | 269 | } |
| 270 | 270 | |
| 271 | - return get_term_link( $id ); |
|
| 271 | + return get_term_link($id); |
|
| 272 | 272 | } |
| 273 | 273 | |
| 274 | 274 | /** |
@@ -280,30 +280,30 @@ discard block |
||
| 280 | 280 | * @return array |
| 281 | 281 | */ |
| 282 | 282 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 283 | - private function get_entity_jsonld( $term_id, $context ) { |
|
| 283 | + private function get_entity_jsonld($term_id, $context) { |
|
| 284 | 284 | |
| 285 | 285 | // The `_wl_entity_id` are URIs. |
| 286 | - $entity_ids = get_term_meta( $term_id, '_wl_entity_id' ); |
|
| 286 | + $entity_ids = get_term_meta($term_id, '_wl_entity_id'); |
|
| 287 | 287 | $entity_uri_service = $this->entity_uri_service; |
| 288 | 288 | |
| 289 | 289 | $wordlift_jsonld_service = Wordlift_Jsonld_Service::get_instance(); |
| 290 | 290 | |
| 291 | 291 | $local_entity_ids = array_filter( |
| 292 | 292 | $entity_ids, |
| 293 | - function ( $uri ) use ( $entity_uri_service ) { |
|
| 294 | - return $entity_uri_service->is_internal( $uri ); |
|
| 293 | + function($uri) use ($entity_uri_service) { |
|
| 294 | + return $entity_uri_service->is_internal($uri); |
|
| 295 | 295 | } |
| 296 | 296 | ); |
| 297 | 297 | |
| 298 | 298 | // Bail out if there are no entities. |
| 299 | - if ( empty( $local_entity_ids ) ) { |
|
| 299 | + if (empty($local_entity_ids)) { |
|
| 300 | 300 | return array(); |
| 301 | 301 | } |
| 302 | 302 | |
| 303 | - $post = $this->entity_uri_service->get_entity( array_shift( $local_entity_ids ) ); |
|
| 304 | - $entities_jsonld = $wordlift_jsonld_service->get_jsonld( false, $post->ID ); |
|
| 303 | + $post = $this->entity_uri_service->get_entity(array_shift($local_entity_ids)); |
|
| 304 | + $entities_jsonld = $wordlift_jsonld_service->get_jsonld(false, $post->ID); |
|
| 305 | 305 | // Reset the `url` to the term page. |
| 306 | - $entities_jsonld[0]['url'] = get_term_link( $term_id ); |
|
| 306 | + $entities_jsonld[0]['url'] = get_term_link($term_id); |
|
| 307 | 307 | |
| 308 | 308 | return $entities_jsonld; |
| 309 | 309 | } |
@@ -313,24 +313,24 @@ discard block |
||
| 313 | 313 | * |
| 314 | 314 | * @return array |
| 315 | 315 | */ |
| 316 | - private function expand_references( $references ) { |
|
| 317 | - if ( ! is_array( $references ) ) { |
|
| 316 | + private function expand_references($references) { |
|
| 317 | + if ( ! is_array($references)) { |
|
| 318 | 318 | return array(); |
| 319 | 319 | } |
| 320 | 320 | $references_jsonld = array(); |
| 321 | 321 | // Expand the references. |
| 322 | - foreach ( $references as $reference ) { |
|
| 323 | - if ( $reference instanceof Term_Reference ) { |
|
| 322 | + foreach ($references as $reference) { |
|
| 323 | + if ($reference instanceof Term_Reference) { |
|
| 324 | 324 | // Second level references won't be expanded. |
| 325 | - $references_jsonld[] = current( $this->get( $reference->get_id(), Jsonld_Context_Enum::UNKNOWN, true ) ); |
|
| 326 | - } elseif ( is_numeric( $reference ) ) { |
|
| 325 | + $references_jsonld[] = current($this->get($reference->get_id(), Jsonld_Context_Enum::UNKNOWN, true)); |
|
| 326 | + } elseif (is_numeric($reference)) { |
|
| 327 | 327 | $ref_2 = array(); |
| 328 | 328 | $ref_info_2 = array(); |
| 329 | - $references_jsonld[] = $this->post_id_to_jsonld_converter->convert( $reference, $ref_2, $ref_info_2, new Relations() ); |
|
| 330 | - } elseif ( $reference instanceof Post_Reference ) { |
|
| 329 | + $references_jsonld[] = $this->post_id_to_jsonld_converter->convert($reference, $ref_2, $ref_info_2, new Relations()); |
|
| 330 | + } elseif ($reference instanceof Post_Reference) { |
|
| 331 | 331 | $ref_2 = array(); |
| 332 | 332 | $ref_info_2 = array(); |
| 333 | - $references_jsonld[] = $this->post_id_to_jsonld_converter->convert( $reference->get_id(), $ref_2, $ref_info_2, new Relations() ); |
|
| 333 | + $references_jsonld[] = $this->post_id_to_jsonld_converter->convert($reference->get_id(), $ref_2, $ref_info_2, new Relations()); |
|
| 334 | 334 | } |
| 335 | 335 | } |
| 336 | 336 | |
@@ -344,9 +344,9 @@ discard block |
||
| 344 | 344 | * @param $term_id int The term id for which the jsonld is generated. |
| 345 | 345 | * @param $context int A context for the JSON-LD generation, valid values in Jsonld_Context_Enum |
| 346 | 346 | */ |
| 347 | - private function set_terms_request( $jsonld_arr, $term_id, $context ) { |
|
| 347 | + private function set_terms_request($jsonld_arr, $term_id, $context) { |
|
| 348 | 348 | // If context is not PAGE or the array is empty, return early. |
| 349 | - if ( Jsonld_Context_Enum::PAGE !== $context || empty( $jsonld_arr[0] ) ) { |
|
| 349 | + if (Jsonld_Context_Enum::PAGE !== $context || empty($jsonld_arr[0])) { |
|
| 350 | 350 | return; |
| 351 | 351 | } |
| 352 | 352 | |
@@ -358,45 +358,45 @@ discard block |
||
| 358 | 358 | |
| 359 | 359 | // Fetch the initial 'about' and 'mentions' counts from term meta. |
| 360 | 360 | $counts = [ |
| 361 | - 'about' => get_term_meta( $term_id, 'wl_about_count', true ) ? : 0, |
|
| 362 | - 'mentions' => get_term_meta( $term_id, 'wl_mentions_count', true ) ? : 0, |
|
| 361 | + 'about' => get_term_meta($term_id, 'wl_about_count', true) ?: 0, |
|
| 362 | + 'mentions' => get_term_meta($term_id, 'wl_mentions_count', true) ?: 0, |
|
| 363 | 363 | ]; |
| 364 | 364 | |
| 365 | 365 | // Iterate over the counts array. |
| 366 | - foreach ( $counts as $key => $count ) { |
|
| 366 | + foreach ($counts as $key => $count) { |
|
| 367 | 367 | // Check if data has 'about' or 'mentions' and the count is different from the existing meta value. |
| 368 | - if ( ! empty( $data[ $key ] ) ) { |
|
| 369 | - $new_count = count( $data[ $key ] ); |
|
| 370 | - if ( $count !== $new_count ) { |
|
| 368 | + if ( ! empty($data[$key])) { |
|
| 369 | + $new_count = count($data[$key]); |
|
| 370 | + if ($count !== $new_count) { |
|
| 371 | 371 | // Set flag to true if counts have changed. |
| 372 | 372 | $change_status = true; |
| 373 | 373 | |
| 374 | 374 | // Update the counts array with new count. |
| 375 | - $counts[ $key ] = $new_count; |
|
| 375 | + $counts[$key] = $new_count; |
|
| 376 | 376 | |
| 377 | 377 | // Update term meta with new count. |
| 378 | - update_term_meta( $term_id, 'wl_' . $key . '_count', $new_count ); |
|
| 378 | + update_term_meta($term_id, 'wl_'.$key.'_count', $new_count); |
|
| 379 | 379 | } |
| 380 | 380 | } |
| 381 | 381 | } |
| 382 | 382 | |
| 383 | 383 | // If the count has changed, make the API request. |
| 384 | - if ( $change_status ) { |
|
| 384 | + if ($change_status) { |
|
| 385 | 385 | $this->api_service->request( |
| 386 | 386 | 'POST', |
| 387 | 387 | '/plugin/events', |
| 388 | - [ 'Content-Type' => 'application/json' ], |
|
| 389 | - wp_json_encode( [ |
|
| 388 | + ['Content-Type' => 'application/json'], |
|
| 389 | + wp_json_encode([ |
|
| 390 | 390 | 'source' => 'jsonld', |
| 391 | 391 | 'args' => [ |
| 392 | - [ 'about_count' => $counts['about'] ], |
|
| 393 | - [ 'mentions_count' => $counts['mentions'] ], |
|
| 392 | + ['about_count' => $counts['about']], |
|
| 393 | + ['mentions_count' => $counts['mentions']], |
|
| 394 | 394 | ], |
| 395 | - 'url' => $this->get_term_url( $term_id ), |
|
| 396 | - ] ), |
|
| 395 | + 'url' => $this->get_term_url($term_id), |
|
| 396 | + ]), |
|
| 397 | 397 | 0.001, |
| 398 | 398 | null, |
| 399 | - [ 'blocking' => false ] |
|
| 399 | + ['blocking' => false] |
|
| 400 | 400 | ); |
| 401 | 401 | } |
| 402 | 402 | } |