@@ -7,38 +7,38 @@ |
||
| 7 | 7 | * @since 3.8.0 |
| 8 | 8 | */ |
| 9 | 9 | class Wordlift_Entity_Property_Service extends Wordlift_Simple_Property_Service { |
| 10 | - /** |
|
| 11 | - * @var \Wordlift_Entity_Service $entity_service |
|
| 12 | - */ |
|
| 13 | - private $entity_service; |
|
| 14 | - |
|
| 15 | - /** |
|
| 16 | - * Wordlift_Entity_Property_Service constructor. |
|
| 17 | - * |
|
| 18 | - * @param \Wordlift_Entity_Service $entity_service |
|
| 19 | - */ |
|
| 20 | - public function __construct( $entity_service ) { |
|
| 21 | - |
|
| 22 | - $this->entity_service = $entity_service; |
|
| 23 | - |
|
| 24 | - } |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * {@inheritdoc} |
|
| 28 | - */ |
|
| 29 | - public function get( $post_id, $meta_key ) { |
|
| 30 | - |
|
| 31 | - $entity_service = $this->entity_service; |
|
| 32 | - |
|
| 33 | - // Map each returned value to a Wordlift_Property_Entity_Reference. |
|
| 34 | - return array_map( function ( $item ) use ( $entity_service ) { |
|
| 35 | - |
|
| 36 | - // If the $item is a number and it's an existing post, return the |
|
| 37 | - // URI of the referenced entity. Otherwise return the value. |
|
| 38 | - return is_numeric( $item ) && NULL !== get_post( $item ) |
|
| 39 | - ? new Wordlift_Property_Entity_Reference( $entity_service->get_uri( $item ) ) |
|
| 40 | - : $item; |
|
| 41 | - }, get_post_meta( $post_id, $meta_key ) ); |
|
| 42 | - } |
|
| 10 | + /** |
|
| 11 | + * @var \Wordlift_Entity_Service $entity_service |
|
| 12 | + */ |
|
| 13 | + private $entity_service; |
|
| 14 | + |
|
| 15 | + /** |
|
| 16 | + * Wordlift_Entity_Property_Service constructor. |
|
| 17 | + * |
|
| 18 | + * @param \Wordlift_Entity_Service $entity_service |
|
| 19 | + */ |
|
| 20 | + public function __construct( $entity_service ) { |
|
| 21 | + |
|
| 22 | + $this->entity_service = $entity_service; |
|
| 23 | + |
|
| 24 | + } |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * {@inheritdoc} |
|
| 28 | + */ |
|
| 29 | + public function get( $post_id, $meta_key ) { |
|
| 30 | + |
|
| 31 | + $entity_service = $this->entity_service; |
|
| 32 | + |
|
| 33 | + // Map each returned value to a Wordlift_Property_Entity_Reference. |
|
| 34 | + return array_map( function ( $item ) use ( $entity_service ) { |
|
| 35 | + |
|
| 36 | + // If the $item is a number and it's an existing post, return the |
|
| 37 | + // URI of the referenced entity. Otherwise return the value. |
|
| 38 | + return is_numeric( $item ) && NULL !== get_post( $item ) |
|
| 39 | + ? new Wordlift_Property_Entity_Reference( $entity_service->get_uri( $item ) ) |
|
| 40 | + : $item; |
|
| 41 | + }, get_post_meta( $post_id, $meta_key ) ); |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | 44 | } |
@@ -17,7 +17,7 @@ discard block |
||
| 17 | 17 | * |
| 18 | 18 | * @param \Wordlift_Entity_Service $entity_service |
| 19 | 19 | */ |
| 20 | - public function __construct( $entity_service ) { |
|
| 20 | + public function __construct($entity_service) { |
|
| 21 | 21 | |
| 22 | 22 | $this->entity_service = $entity_service; |
| 23 | 23 | |
@@ -26,19 +26,19 @@ discard block |
||
| 26 | 26 | /** |
| 27 | 27 | * {@inheritdoc} |
| 28 | 28 | */ |
| 29 | - public function get( $post_id, $meta_key ) { |
|
| 29 | + public function get($post_id, $meta_key) { |
|
| 30 | 30 | |
| 31 | 31 | $entity_service = $this->entity_service; |
| 32 | 32 | |
| 33 | 33 | // Map each returned value to a Wordlift_Property_Entity_Reference. |
| 34 | - return array_map( function ( $item ) use ( $entity_service ) { |
|
| 34 | + return array_map(function($item) use ($entity_service) { |
|
| 35 | 35 | |
| 36 | 36 | // If the $item is a number and it's an existing post, return the |
| 37 | 37 | // URI of the referenced entity. Otherwise return the value. |
| 38 | - return is_numeric( $item ) && NULL !== get_post( $item ) |
|
| 39 | - ? new Wordlift_Property_Entity_Reference( $entity_service->get_uri( $item ) ) |
|
| 38 | + return is_numeric($item) && NULL !== get_post($item) |
|
| 39 | + ? new Wordlift_Property_Entity_Reference($entity_service->get_uri($item)) |
|
| 40 | 40 | : $item; |
| 41 | - }, get_post_meta( $post_id, $meta_key ) ); |
|
| 41 | + }, get_post_meta($post_id, $meta_key)); |
|
| 42 | 42 | } |
| 43 | 43 | |
| 44 | 44 | } |
@@ -15,34 +15,34 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class Wordlift_Property_Getter_Factory { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * Create a {@link Wordlift_Property_Getter} instance. |
|
| 20 | - * @since 3.8.0 |
|
| 21 | - * |
|
| 22 | - * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 23 | - * |
|
| 24 | - * @return \Wordlift_Property_Getter A {@link Wordlift_Property_Getter} instance. |
|
| 25 | - */ |
|
| 26 | - public static function create( $entity_service ) { |
|
| 18 | + /** |
|
| 19 | + * Create a {@link Wordlift_Property_Getter} instance. |
|
| 20 | + * @since 3.8.0 |
|
| 21 | + * |
|
| 22 | + * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 23 | + * |
|
| 24 | + * @return \Wordlift_Property_Getter A {@link Wordlift_Property_Getter} instance. |
|
| 25 | + */ |
|
| 26 | + public static function create( $entity_service ) { |
|
| 27 | 27 | |
| 28 | - $property_getter = new Wordlift_Property_Getter( new Wordlift_Simple_Property_Service() ); |
|
| 29 | - $property_getter->register( new Wordlift_Entity_Property_Service( $entity_service ), array( |
|
| 30 | - Wordlift_Schema_Service::FIELD_FOUNDER, |
|
| 31 | - Wordlift_Schema_Service::FIELD_AUTHOR, |
|
| 32 | - Wordlift_Schema_Service::FIELD_KNOWS, |
|
| 33 | - Wordlift_Schema_Service::FIELD_BIRTH_PLACE, |
|
| 34 | - Wordlift_Schema_Service::FIELD_AFFILIATION, |
|
| 35 | - ) ); |
|
| 36 | - $property_getter->register( new Wordlift_Location_Property_Service( $entity_service ), array( |
|
| 37 | - Wordlift_Schema_Service::FIELD_LOCATION, |
|
| 38 | - ) ); |
|
| 39 | - $property_getter->register( new Wordlift_Url_Property_Service(), array( Wordlift_Url_Property_Service::META_KEY ) ); |
|
| 40 | - $property_getter->register( new Wordlift_Double_Property_Service(), array( |
|
| 41 | - Wordlift_Schema_Service::FIELD_GEO_LATITUDE, |
|
| 42 | - Wordlift_Schema_Service::FIELD_GEO_LONGITUDE, |
|
| 43 | - ) ); |
|
| 28 | + $property_getter = new Wordlift_Property_Getter( new Wordlift_Simple_Property_Service() ); |
|
| 29 | + $property_getter->register( new Wordlift_Entity_Property_Service( $entity_service ), array( |
|
| 30 | + Wordlift_Schema_Service::FIELD_FOUNDER, |
|
| 31 | + Wordlift_Schema_Service::FIELD_AUTHOR, |
|
| 32 | + Wordlift_Schema_Service::FIELD_KNOWS, |
|
| 33 | + Wordlift_Schema_Service::FIELD_BIRTH_PLACE, |
|
| 34 | + Wordlift_Schema_Service::FIELD_AFFILIATION, |
|
| 35 | + ) ); |
|
| 36 | + $property_getter->register( new Wordlift_Location_Property_Service( $entity_service ), array( |
|
| 37 | + Wordlift_Schema_Service::FIELD_LOCATION, |
|
| 38 | + ) ); |
|
| 39 | + $property_getter->register( new Wordlift_Url_Property_Service(), array( Wordlift_Url_Property_Service::META_KEY ) ); |
|
| 40 | + $property_getter->register( new Wordlift_Double_Property_Service(), array( |
|
| 41 | + Wordlift_Schema_Service::FIELD_GEO_LATITUDE, |
|
| 42 | + Wordlift_Schema_Service::FIELD_GEO_LONGITUDE, |
|
| 43 | + ) ); |
|
| 44 | 44 | |
| 45 | - return $property_getter; |
|
| 46 | - } |
|
| 45 | + return $property_getter; |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | 48 | } |
| 49 | 49 | \ No newline at end of file |
@@ -1,11 +1,11 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -require_once( 'class-wordlift-property-getter.php' ); |
|
| 4 | -require_once( 'class-wordlift-simple-property-service.php' ); |
|
| 5 | -require_once( 'class-wordlift-entity-property-service.php' ); |
|
| 6 | -require_once( 'class-wordlift-location-property-service.php' ); |
|
| 7 | -require_once( 'class-wordlift-url-property-service.php' ); |
|
| 8 | -require_once( 'class-wordlift-double-property-service.php' ); |
|
| 3 | +require_once('class-wordlift-property-getter.php'); |
|
| 4 | +require_once('class-wordlift-simple-property-service.php'); |
|
| 5 | +require_once('class-wordlift-entity-property-service.php'); |
|
| 6 | +require_once('class-wordlift-location-property-service.php'); |
|
| 7 | +require_once('class-wordlift-url-property-service.php'); |
|
| 8 | +require_once('class-wordlift-double-property-service.php'); |
|
| 9 | 9 | |
| 10 | 10 | /** |
| 11 | 11 | * A Wordlift_Property_Getter_Factory, which instantiate a configured |
@@ -23,24 +23,24 @@ discard block |
||
| 23 | 23 | * |
| 24 | 24 | * @return \Wordlift_Property_Getter A {@link Wordlift_Property_Getter} instance. |
| 25 | 25 | */ |
| 26 | - public static function create( $entity_service ) { |
|
| 26 | + public static function create($entity_service) { |
|
| 27 | 27 | |
| 28 | - $property_getter = new Wordlift_Property_Getter( new Wordlift_Simple_Property_Service() ); |
|
| 29 | - $property_getter->register( new Wordlift_Entity_Property_Service( $entity_service ), array( |
|
| 28 | + $property_getter = new Wordlift_Property_Getter(new Wordlift_Simple_Property_Service()); |
|
| 29 | + $property_getter->register(new Wordlift_Entity_Property_Service($entity_service), array( |
|
| 30 | 30 | Wordlift_Schema_Service::FIELD_FOUNDER, |
| 31 | 31 | Wordlift_Schema_Service::FIELD_AUTHOR, |
| 32 | 32 | Wordlift_Schema_Service::FIELD_KNOWS, |
| 33 | 33 | Wordlift_Schema_Service::FIELD_BIRTH_PLACE, |
| 34 | 34 | Wordlift_Schema_Service::FIELD_AFFILIATION, |
| 35 | - ) ); |
|
| 36 | - $property_getter->register( new Wordlift_Location_Property_Service( $entity_service ), array( |
|
| 35 | + )); |
|
| 36 | + $property_getter->register(new Wordlift_Location_Property_Service($entity_service), array( |
|
| 37 | 37 | Wordlift_Schema_Service::FIELD_LOCATION, |
| 38 | - ) ); |
|
| 39 | - $property_getter->register( new Wordlift_Url_Property_Service(), array( Wordlift_Url_Property_Service::META_KEY ) ); |
|
| 40 | - $property_getter->register( new Wordlift_Double_Property_Service(), array( |
|
| 38 | + )); |
|
| 39 | + $property_getter->register(new Wordlift_Url_Property_Service(), array(Wordlift_Url_Property_Service::META_KEY)); |
|
| 40 | + $property_getter->register(new Wordlift_Double_Property_Service(), array( |
|
| 41 | 41 | Wordlift_Schema_Service::FIELD_GEO_LATITUDE, |
| 42 | 42 | Wordlift_Schema_Service::FIELD_GEO_LONGITUDE, |
| 43 | - ) ); |
|
| 43 | + )); |
|
| 44 | 44 | |
| 45 | 45 | return $property_getter; |
| 46 | 46 | } |
@@ -35,17 +35,17 @@ |
||
| 35 | 35 | // |
| 36 | 36 | // } |
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * {@inheritdoc} |
|
| 40 | - */ |
|
| 41 | - public function get( $post_id, $meta_key ) { |
|
| 38 | + /** |
|
| 39 | + * {@inheritdoc} |
|
| 40 | + */ |
|
| 41 | + public function get( $post_id, $meta_key ) { |
|
| 42 | 42 | |
| 43 | - return array_map( function ( $item ) { |
|
| 43 | + return array_map( function ( $item ) { |
|
| 44 | 44 | |
| 45 | - return $item instanceof Wordlift_Property_Entity_Reference |
|
| 46 | - ? $item |
|
| 47 | - : array( '@type' => 'Place', 'name' => $item ); |
|
| 48 | - }, parent::get( $post_id, $meta_key ) ); |
|
| 49 | - } |
|
| 45 | + return $item instanceof Wordlift_Property_Entity_Reference |
|
| 46 | + ? $item |
|
| 47 | + : array( '@type' => 'Place', 'name' => $item ); |
|
| 48 | + }, parent::get( $post_id, $meta_key ) ); |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | 51 | } |
@@ -38,14 +38,14 @@ |
||
| 38 | 38 | /** |
| 39 | 39 | * {@inheritdoc} |
| 40 | 40 | */ |
| 41 | - public function get( $post_id, $meta_key ) { |
|
| 41 | + public function get($post_id, $meta_key) { |
|
| 42 | 42 | |
| 43 | - return array_map( function ( $item ) { |
|
| 43 | + return array_map(function($item) { |
|
| 44 | 44 | |
| 45 | 45 | return $item instanceof Wordlift_Property_Entity_Reference |
| 46 | 46 | ? $item |
| 47 | - : array( '@type' => 'Place', 'name' => $item ); |
|
| 48 | - }, parent::get( $post_id, $meta_key ) ); |
|
| 47 | + : array('@type' => 'Place', 'name' => $item); |
|
| 48 | + }, parent::get($post_id, $meta_key)); |
|
| 49 | 49 | } |
| 50 | 50 | |
| 51 | 51 | } |
@@ -13,117 +13,117 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class Wordlift_Jsonld_Service { |
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * A {@link Wordlift_Entity_Service} instance. |
|
| 18 | - * |
|
| 19 | - * @since 3.8.0 |
|
| 20 | - * @access private |
|
| 21 | - * @var Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 22 | - */ |
|
| 23 | - private $entity_service; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * A {@link Wordlift_Entity_To_Jsonld_Converter} instance. |
|
| 27 | - * |
|
| 28 | - * @since 3.8.0 |
|
| 29 | - * @access private |
|
| 30 | - * @var \Wordlift_Entity_Post_To_Jsonld_Converter A {@link Wordlift_Entity_To_Jsonld_Converter} instance. |
|
| 31 | - */ |
|
| 32 | - private $entity_to_jsonld_converter; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * Create a JSON-LD service. |
|
| 36 | - * |
|
| 37 | - * @since 3.8.0 |
|
| 38 | - * |
|
| 39 | - * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 40 | - * @param \Wordlift_Entity_Post_To_Jsonld_Converter $entity_to_jsonld_converter A {@link Wordlift_Entity_Post_To_Jsonld_Converter} instance. |
|
| 41 | - */ |
|
| 42 | - public function __construct( $entity_service, $entity_to_jsonld_converter ) { |
|
| 43 | - |
|
| 44 | - $this->entity_service = $entity_service; |
|
| 45 | - $this->entity_to_jsonld_converter = $entity_to_jsonld_converter; |
|
| 46 | - |
|
| 47 | - add_action( 'wp_footer', array( $this, 'wp_footer' ), PHP_INT_MAX ); |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * Hook to WP's wp_footer action and load the JSON-LD data. |
|
| 52 | - * |
|
| 53 | - * @since 3.8.0 |
|
| 54 | - */ |
|
| 55 | - public function wp_footer() { |
|
| 56 | - |
|
| 57 | - // We only care about singular pages. |
|
| 58 | - if ( ! is_singular() ) { |
|
| 59 | - return; |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - // Get the entities related to the current post (and that are published). |
|
| 63 | - $post_id = get_the_ID(); |
|
| 64 | - $posts = $this->entity_service->is_entity( $post_id ) |
|
| 65 | - ? array( get_the_ID() ) |
|
| 66 | - : array_unique( wl_core_get_related_entity_ids( $post_id, array( |
|
| 67 | - 'status' => 'publish', |
|
| 68 | - ) ) ); |
|
| 69 | - |
|
| 70 | - // Build the URL to load the JSON-LD asynchronously. |
|
| 71 | - $url = admin_url( 'admin-ajax.php?action=wl_jsonld' ); |
|
| 72 | - $entity_service = $this->entity_service; |
|
| 73 | - $data = implode( '&', array_map( function ( $item ) use ( $entity_service ) { |
|
| 74 | - return 'uri[]=' . rawurldecode( $entity_service->get_uri( $item ) ); |
|
| 75 | - }, $posts ) ); |
|
| 76 | - |
|
| 77 | - // Print the Javascript code. |
|
| 78 | - echo <<<EOF |
|
| 16 | + /** |
|
| 17 | + * A {@link Wordlift_Entity_Service} instance. |
|
| 18 | + * |
|
| 19 | + * @since 3.8.0 |
|
| 20 | + * @access private |
|
| 21 | + * @var Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 22 | + */ |
|
| 23 | + private $entity_service; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * A {@link Wordlift_Entity_To_Jsonld_Converter} instance. |
|
| 27 | + * |
|
| 28 | + * @since 3.8.0 |
|
| 29 | + * @access private |
|
| 30 | + * @var \Wordlift_Entity_Post_To_Jsonld_Converter A {@link Wordlift_Entity_To_Jsonld_Converter} instance. |
|
| 31 | + */ |
|
| 32 | + private $entity_to_jsonld_converter; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * Create a JSON-LD service. |
|
| 36 | + * |
|
| 37 | + * @since 3.8.0 |
|
| 38 | + * |
|
| 39 | + * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 40 | + * @param \Wordlift_Entity_Post_To_Jsonld_Converter $entity_to_jsonld_converter A {@link Wordlift_Entity_Post_To_Jsonld_Converter} instance. |
|
| 41 | + */ |
|
| 42 | + public function __construct( $entity_service, $entity_to_jsonld_converter ) { |
|
| 43 | + |
|
| 44 | + $this->entity_service = $entity_service; |
|
| 45 | + $this->entity_to_jsonld_converter = $entity_to_jsonld_converter; |
|
| 46 | + |
|
| 47 | + add_action( 'wp_footer', array( $this, 'wp_footer' ), PHP_INT_MAX ); |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * Hook to WP's wp_footer action and load the JSON-LD data. |
|
| 52 | + * |
|
| 53 | + * @since 3.8.0 |
|
| 54 | + */ |
|
| 55 | + public function wp_footer() { |
|
| 56 | + |
|
| 57 | + // We only care about singular pages. |
|
| 58 | + if ( ! is_singular() ) { |
|
| 59 | + return; |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + // Get the entities related to the current post (and that are published). |
|
| 63 | + $post_id = get_the_ID(); |
|
| 64 | + $posts = $this->entity_service->is_entity( $post_id ) |
|
| 65 | + ? array( get_the_ID() ) |
|
| 66 | + : array_unique( wl_core_get_related_entity_ids( $post_id, array( |
|
| 67 | + 'status' => 'publish', |
|
| 68 | + ) ) ); |
|
| 69 | + |
|
| 70 | + // Build the URL to load the JSON-LD asynchronously. |
|
| 71 | + $url = admin_url( 'admin-ajax.php?action=wl_jsonld' ); |
|
| 72 | + $entity_service = $this->entity_service; |
|
| 73 | + $data = implode( '&', array_map( function ( $item ) use ( $entity_service ) { |
|
| 74 | + return 'uri[]=' . rawurldecode( $entity_service->get_uri( $item ) ); |
|
| 75 | + }, $posts ) ); |
|
| 76 | + |
|
| 77 | + // Print the Javascript code. |
|
| 78 | + echo <<<EOF |
|
| 79 | 79 | <script type="text/javascript"><!-- |
| 80 | 80 | (function($) { $( window ).on( 'load', function() { $.post('$url','$data').done(function(data) { |
| 81 | 81 | $('head').append( '<script type="application/ld+json">'+JSON.stringify(data)+'</s' + 'cript>' ); |
| 82 | 82 | }); }); })(jQuery); |
| 83 | 83 | // --></script> |
| 84 | 84 | EOF; |
| 85 | - } |
|
| 85 | + } |
|
| 86 | 86 | |
| 87 | - /** |
|
| 88 | - * Process calls to the AJAX 'wl_jsonld' endpoint. |
|
| 89 | - * |
|
| 90 | - * @since 3.8.0 |
|
| 91 | - */ |
|
| 92 | - public function get() { |
|
| 87 | + /** |
|
| 88 | + * Process calls to the AJAX 'wl_jsonld' endpoint. |
|
| 89 | + * |
|
| 90 | + * @since 3.8.0 |
|
| 91 | + */ |
|
| 92 | + public function get() { |
|
| 93 | 93 | |
| 94 | - // If no URI has been provided return an empty array. |
|
| 95 | - if ( ! isset( $_REQUEST['uri'] ) ) { |
|
| 96 | - wp_send_json( array() ); |
|
| 97 | - } |
|
| 94 | + // If no URI has been provided return an empty array. |
|
| 95 | + if ( ! isset( $_REQUEST['uri'] ) ) { |
|
| 96 | + wp_send_json( array() ); |
|
| 97 | + } |
|
| 98 | 98 | |
| 99 | - // Get an array of URIs to parse. |
|
| 100 | - $uris = is_array( $_REQUEST['uri'] ) ? $_REQUEST['uri'] : array( $_REQUEST['uri'] ); |
|
| 99 | + // Get an array of URIs to parse. |
|
| 100 | + $uris = is_array( $_REQUEST['uri'] ) ? $_REQUEST['uri'] : array( $_REQUEST['uri'] ); |
|
| 101 | 101 | |
| 102 | - // An array of references which is captured when converting an URI to a |
|
| 103 | - // json which we gather to further expand our json-ld. |
|
| 104 | - $references = array(); |
|
| 102 | + // An array of references which is captured when converting an URI to a |
|
| 103 | + // json which we gather to further expand our json-ld. |
|
| 104 | + $references = array(); |
|
| 105 | 105 | |
| 106 | - // Set a reference to the entity_to_jsonld_converter to use in the closures. |
|
| 107 | - $entity_to_jsonld_converter = $this->entity_to_jsonld_converter; |
|
| 106 | + // Set a reference to the entity_to_jsonld_converter to use in the closures. |
|
| 107 | + $entity_to_jsonld_converter = $this->entity_to_jsonld_converter; |
|
| 108 | 108 | |
| 109 | - // Convert each URI to a JSON-LD array, while gathering referenced entities. |
|
| 110 | - // in the references array. |
|
| 111 | - $jsonld = array_merge( |
|
| 112 | - array_map( function ( $item ) use ( $entity_to_jsonld_converter, &$references ) { |
|
| 109 | + // Convert each URI to a JSON-LD array, while gathering referenced entities. |
|
| 110 | + // in the references array. |
|
| 111 | + $jsonld = array_merge( |
|
| 112 | + array_map( function ( $item ) use ( $entity_to_jsonld_converter, &$references ) { |
|
| 113 | 113 | |
| 114 | - return $entity_to_jsonld_converter->convert( $item, $references ); |
|
| 115 | - }, $uris ), |
|
| 116 | - // Convert each URI in the references array to JSON-LD. We don't output |
|
| 117 | - // entities already output above (hence the array_diff). |
|
| 118 | - array_map( function ( $item ) use ( $entity_to_jsonld_converter, &$references ) { |
|
| 114 | + return $entity_to_jsonld_converter->convert( $item, $references ); |
|
| 115 | + }, $uris ), |
|
| 116 | + // Convert each URI in the references array to JSON-LD. We don't output |
|
| 117 | + // entities already output above (hence the array_diff). |
|
| 118 | + array_map( function ( $item ) use ( $entity_to_jsonld_converter, &$references ) { |
|
| 119 | 119 | |
| 120 | - return $entity_to_jsonld_converter->convert( $item, $references ); |
|
| 121 | - }, array_diff( $references, $uris ) ) |
|
| 122 | - ); |
|
| 120 | + return $entity_to_jsonld_converter->convert( $item, $references ); |
|
| 121 | + }, array_diff( $references, $uris ) ) |
|
| 122 | + ); |
|
| 123 | 123 | |
| 124 | - // Finally send the JSON-LD. |
|
| 125 | - wp_send_json( $jsonld ); |
|
| 124 | + // Finally send the JSON-LD. |
|
| 125 | + wp_send_json( $jsonld ); |
|
| 126 | 126 | |
| 127 | - } |
|
| 127 | + } |
|
| 128 | 128 | |
| 129 | 129 | } |
@@ -39,12 +39,12 @@ discard block |
||
| 39 | 39 | * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
| 40 | 40 | * @param \Wordlift_Entity_Post_To_Jsonld_Converter $entity_to_jsonld_converter A {@link Wordlift_Entity_Post_To_Jsonld_Converter} instance. |
| 41 | 41 | */ |
| 42 | - public function __construct( $entity_service, $entity_to_jsonld_converter ) { |
|
| 42 | + public function __construct($entity_service, $entity_to_jsonld_converter) { |
|
| 43 | 43 | |
| 44 | 44 | $this->entity_service = $entity_service; |
| 45 | 45 | $this->entity_to_jsonld_converter = $entity_to_jsonld_converter; |
| 46 | 46 | |
| 47 | - add_action( 'wp_footer', array( $this, 'wp_footer' ), PHP_INT_MAX ); |
|
| 47 | + add_action('wp_footer', array($this, 'wp_footer'), PHP_INT_MAX); |
|
| 48 | 48 | } |
| 49 | 49 | |
| 50 | 50 | /** |
@@ -55,24 +55,24 @@ discard block |
||
| 55 | 55 | public function wp_footer() { |
| 56 | 56 | |
| 57 | 57 | // We only care about singular pages. |
| 58 | - if ( ! is_singular() ) { |
|
| 58 | + if ( ! is_singular()) { |
|
| 59 | 59 | return; |
| 60 | 60 | } |
| 61 | 61 | |
| 62 | 62 | // Get the entities related to the current post (and that are published). |
| 63 | 63 | $post_id = get_the_ID(); |
| 64 | - $posts = $this->entity_service->is_entity( $post_id ) |
|
| 65 | - ? array( get_the_ID() ) |
|
| 66 | - : array_unique( wl_core_get_related_entity_ids( $post_id, array( |
|
| 64 | + $posts = $this->entity_service->is_entity($post_id) |
|
| 65 | + ? array(get_the_ID()) |
|
| 66 | + : array_unique(wl_core_get_related_entity_ids($post_id, array( |
|
| 67 | 67 | 'status' => 'publish', |
| 68 | - ) ) ); |
|
| 68 | + ))); |
|
| 69 | 69 | |
| 70 | 70 | // Build the URL to load the JSON-LD asynchronously. |
| 71 | - $url = admin_url( 'admin-ajax.php?action=wl_jsonld' ); |
|
| 71 | + $url = admin_url('admin-ajax.php?action=wl_jsonld'); |
|
| 72 | 72 | $entity_service = $this->entity_service; |
| 73 | - $data = implode( '&', array_map( function ( $item ) use ( $entity_service ) { |
|
| 74 | - return 'uri[]=' . rawurldecode( $entity_service->get_uri( $item ) ); |
|
| 75 | - }, $posts ) ); |
|
| 73 | + $data = implode('&', array_map(function($item) use ($entity_service) { |
|
| 74 | + return 'uri[]='.rawurldecode($entity_service->get_uri($item)); |
|
| 75 | + }, $posts)); |
|
| 76 | 76 | |
| 77 | 77 | // Print the Javascript code. |
| 78 | 78 | echo <<<EOF |
@@ -92,12 +92,12 @@ discard block |
||
| 92 | 92 | public function get() { |
| 93 | 93 | |
| 94 | 94 | // If no URI has been provided return an empty array. |
| 95 | - if ( ! isset( $_REQUEST['uri'] ) ) { |
|
| 96 | - wp_send_json( array() ); |
|
| 95 | + if ( ! isset($_REQUEST['uri'])) { |
|
| 96 | + wp_send_json(array()); |
|
| 97 | 97 | } |
| 98 | 98 | |
| 99 | 99 | // Get an array of URIs to parse. |
| 100 | - $uris = is_array( $_REQUEST['uri'] ) ? $_REQUEST['uri'] : array( $_REQUEST['uri'] ); |
|
| 100 | + $uris = is_array($_REQUEST['uri']) ? $_REQUEST['uri'] : array($_REQUEST['uri']); |
|
| 101 | 101 | |
| 102 | 102 | // An array of references which is captured when converting an URI to a |
| 103 | 103 | // json which we gather to further expand our json-ld. |
@@ -109,20 +109,20 @@ discard block |
||
| 109 | 109 | // Convert each URI to a JSON-LD array, while gathering referenced entities. |
| 110 | 110 | // in the references array. |
| 111 | 111 | $jsonld = array_merge( |
| 112 | - array_map( function ( $item ) use ( $entity_to_jsonld_converter, &$references ) { |
|
| 112 | + array_map(function($item) use ($entity_to_jsonld_converter, &$references) { |
|
| 113 | 113 | |
| 114 | - return $entity_to_jsonld_converter->convert( $item, $references ); |
|
| 115 | - }, $uris ), |
|
| 114 | + return $entity_to_jsonld_converter->convert($item, $references); |
|
| 115 | + }, $uris), |
|
| 116 | 116 | // Convert each URI in the references array to JSON-LD. We don't output |
| 117 | 117 | // entities already output above (hence the array_diff). |
| 118 | - array_map( function ( $item ) use ( $entity_to_jsonld_converter, &$references ) { |
|
| 118 | + array_map(function($item) use ($entity_to_jsonld_converter, &$references) { |
|
| 119 | 119 | |
| 120 | - return $entity_to_jsonld_converter->convert( $item, $references ); |
|
| 121 | - }, array_diff( $references, $uris ) ) |
|
| 120 | + return $entity_to_jsonld_converter->convert($item, $references); |
|
| 121 | + }, array_diff($references, $uris)) |
|
| 122 | 122 | ); |
| 123 | 123 | |
| 124 | 124 | // Finally send the JSON-LD. |
| 125 | - wp_send_json( $jsonld ); |
|
| 125 | + wp_send_json($jsonld); |
|
| 126 | 126 | |
| 127 | 127 | } |
| 128 | 128 | |