@@ -11,77 +11,77 @@ |
||
| 11 | 11 | |
| 12 | 12 | class Webhooks_Manager { |
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * Registering the actions to call up sync_many or sync_delete methods |
|
| 16 | - */ |
|
| 17 | - |
|
| 18 | - public function __construct() { |
|
| 19 | - add_action( 'wl_sync__sync_many', array( $this, 'sync_many' ), 10 ); |
|
| 20 | - add_action( 'wl_sync__delete_one', array( $this, 'sync_delete' ), 10, 3 ); |
|
| 21 | - } |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * Method to call up webhook with post requested |
|
| 25 | - * |
|
| 26 | - * @param array $hashes |
|
| 27 | - */ |
|
| 28 | - |
|
| 29 | - public function sync_many( $hashes ) { |
|
| 30 | - |
|
| 31 | - $urls = explode( "\n", get_option( Webhooks_Loader::URLS_OPTION_NAME, '' ) ); |
|
| 32 | - if ( empty( $urls ) ) { |
|
| 33 | - return; |
|
| 34 | - } |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * Allow 3rd parties to filter out the objects that we want to send via webhooks. |
|
| 38 | - * @since 3.34.0 |
|
| 39 | - * @var Sync_Object_Adapter[] $filtered_objects |
|
| 40 | - */ |
|
| 41 | - $filtered_hashes = apply_filters( 'wl_webhooks__sync_many__objects', $hashes ); |
|
| 42 | - |
|
| 43 | - foreach ( $urls as $url ) { |
|
| 44 | - foreach ( $filtered_hashes as $hash ) { |
|
| 45 | - $jsonld = $hash[2]; |
|
| 46 | - $filtered_url = apply_filters( 'wl_webhooks__sync_many__url', $url, $hash ); |
|
| 47 | - wp_remote_request( $filtered_url, apply_filters( 'wl_webhooks__sync_many__args', array( |
|
| 48 | - 'blocking' => false, |
|
| 49 | - 'method' => 'PUT', |
|
| 50 | - 'headers' => array( 'content-type' => 'application/json; ' . get_bloginfo( 'charset' ) ), |
|
| 51 | - 'body' => $jsonld |
|
| 52 | - ), $hash ) ); |
|
| 53 | - } |
|
| 54 | - } |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * Method to call up webhook with delete requested |
|
| 59 | - * |
|
| 60 | - * @param string $type |
|
| 61 | - * @param int $object_id |
|
| 62 | - * @param string $uri |
|
| 63 | - */ |
|
| 64 | - |
|
| 65 | - public function sync_delete( $type, $object_id, $uri ) { |
|
| 66 | - |
|
| 67 | - $urls = explode( "\n", get_option( Webhooks_Loader::URLS_OPTION_NAME, '' ) ); |
|
| 68 | - if ( empty( $urls ) ) { |
|
| 69 | - return; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - if ( ! apply_filters( 'wl_webhooks__sync_delete', true, $type, $object_id, $uri ) ) { |
|
| 73 | - return; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - foreach ( $urls as $template_url ) { |
|
| 77 | - $url = add_query_arg( array( 'uri' => $uri ), $template_url ); |
|
| 78 | - $filtered_url = apply_filters( 'wl_webhooks__sync_delete__url', $url, $type, $object_id, $uri ); |
|
| 79 | - wp_remote_request( $filtered_url, apply_filters( 'wl_webhooks__sync_delete__args', array( |
|
| 80 | - 'blocking' => false, |
|
| 81 | - 'method' => 'DELETE', |
|
| 82 | - ) ) ); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - } |
|
| 14 | + /** |
|
| 15 | + * Registering the actions to call up sync_many or sync_delete methods |
|
| 16 | + */ |
|
| 17 | + |
|
| 18 | + public function __construct() { |
|
| 19 | + add_action( 'wl_sync__sync_many', array( $this, 'sync_many' ), 10 ); |
|
| 20 | + add_action( 'wl_sync__delete_one', array( $this, 'sync_delete' ), 10, 3 ); |
|
| 21 | + } |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * Method to call up webhook with post requested |
|
| 25 | + * |
|
| 26 | + * @param array $hashes |
|
| 27 | + */ |
|
| 28 | + |
|
| 29 | + public function sync_many( $hashes ) { |
|
| 30 | + |
|
| 31 | + $urls = explode( "\n", get_option( Webhooks_Loader::URLS_OPTION_NAME, '' ) ); |
|
| 32 | + if ( empty( $urls ) ) { |
|
| 33 | + return; |
|
| 34 | + } |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * Allow 3rd parties to filter out the objects that we want to send via webhooks. |
|
| 38 | + * @since 3.34.0 |
|
| 39 | + * @var Sync_Object_Adapter[] $filtered_objects |
|
| 40 | + */ |
|
| 41 | + $filtered_hashes = apply_filters( 'wl_webhooks__sync_many__objects', $hashes ); |
|
| 42 | + |
|
| 43 | + foreach ( $urls as $url ) { |
|
| 44 | + foreach ( $filtered_hashes as $hash ) { |
|
| 45 | + $jsonld = $hash[2]; |
|
| 46 | + $filtered_url = apply_filters( 'wl_webhooks__sync_many__url', $url, $hash ); |
|
| 47 | + wp_remote_request( $filtered_url, apply_filters( 'wl_webhooks__sync_many__args', array( |
|
| 48 | + 'blocking' => false, |
|
| 49 | + 'method' => 'PUT', |
|
| 50 | + 'headers' => array( 'content-type' => 'application/json; ' . get_bloginfo( 'charset' ) ), |
|
| 51 | + 'body' => $jsonld |
|
| 52 | + ), $hash ) ); |
|
| 53 | + } |
|
| 54 | + } |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * Method to call up webhook with delete requested |
|
| 59 | + * |
|
| 60 | + * @param string $type |
|
| 61 | + * @param int $object_id |
|
| 62 | + * @param string $uri |
|
| 63 | + */ |
|
| 64 | + |
|
| 65 | + public function sync_delete( $type, $object_id, $uri ) { |
|
| 66 | + |
|
| 67 | + $urls = explode( "\n", get_option( Webhooks_Loader::URLS_OPTION_NAME, '' ) ); |
|
| 68 | + if ( empty( $urls ) ) { |
|
| 69 | + return; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + if ( ! apply_filters( 'wl_webhooks__sync_delete', true, $type, $object_id, $uri ) ) { |
|
| 73 | + return; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + foreach ( $urls as $template_url ) { |
|
| 77 | + $url = add_query_arg( array( 'uri' => $uri ), $template_url ); |
|
| 78 | + $filtered_url = apply_filters( 'wl_webhooks__sync_delete__url', $url, $type, $object_id, $uri ); |
|
| 79 | + wp_remote_request( $filtered_url, apply_filters( 'wl_webhooks__sync_delete__args', array( |
|
| 80 | + 'blocking' => false, |
|
| 81 | + 'method' => 'DELETE', |
|
| 82 | + ) ) ); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + } |
|
| 86 | 86 | |
| 87 | 87 | } |
@@ -16,8 +16,8 @@ discard block |
||
| 16 | 16 | */ |
| 17 | 17 | |
| 18 | 18 | public function __construct() { |
| 19 | - add_action( 'wl_sync__sync_many', array( $this, 'sync_many' ), 10 ); |
|
| 20 | - add_action( 'wl_sync__delete_one', array( $this, 'sync_delete' ), 10, 3 ); |
|
| 19 | + add_action('wl_sync__sync_many', array($this, 'sync_many'), 10); |
|
| 20 | + add_action('wl_sync__delete_one', array($this, 'sync_delete'), 10, 3); |
|
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | /** |
@@ -26,10 +26,10 @@ discard block |
||
| 26 | 26 | * @param array $hashes |
| 27 | 27 | */ |
| 28 | 28 | |
| 29 | - public function sync_many( $hashes ) { |
|
| 29 | + public function sync_many($hashes) { |
|
| 30 | 30 | |
| 31 | - $urls = explode( "\n", get_option( Webhooks_Loader::URLS_OPTION_NAME, '' ) ); |
|
| 32 | - if ( empty( $urls ) ) { |
|
| 31 | + $urls = explode("\n", get_option(Webhooks_Loader::URLS_OPTION_NAME, '')); |
|
| 32 | + if (empty($urls)) { |
|
| 33 | 33 | return; |
| 34 | 34 | } |
| 35 | 35 | |
@@ -38,18 +38,18 @@ discard block |
||
| 38 | 38 | * @since 3.34.0 |
| 39 | 39 | * @var Sync_Object_Adapter[] $filtered_objects |
| 40 | 40 | */ |
| 41 | - $filtered_hashes = apply_filters( 'wl_webhooks__sync_many__objects', $hashes ); |
|
| 41 | + $filtered_hashes = apply_filters('wl_webhooks__sync_many__objects', $hashes); |
|
| 42 | 42 | |
| 43 | - foreach ( $urls as $url ) { |
|
| 44 | - foreach ( $filtered_hashes as $hash ) { |
|
| 43 | + foreach ($urls as $url) { |
|
| 44 | + foreach ($filtered_hashes as $hash) { |
|
| 45 | 45 | $jsonld = $hash[2]; |
| 46 | - $filtered_url = apply_filters( 'wl_webhooks__sync_many__url', $url, $hash ); |
|
| 47 | - wp_remote_request( $filtered_url, apply_filters( 'wl_webhooks__sync_many__args', array( |
|
| 46 | + $filtered_url = apply_filters('wl_webhooks__sync_many__url', $url, $hash); |
|
| 47 | + wp_remote_request($filtered_url, apply_filters('wl_webhooks__sync_many__args', array( |
|
| 48 | 48 | 'blocking' => false, |
| 49 | 49 | 'method' => 'PUT', |
| 50 | - 'headers' => array( 'content-type' => 'application/json; ' . get_bloginfo( 'charset' ) ), |
|
| 50 | + 'headers' => array('content-type' => 'application/json; '.get_bloginfo('charset')), |
|
| 51 | 51 | 'body' => $jsonld |
| 52 | - ), $hash ) ); |
|
| 52 | + ), $hash)); |
|
| 53 | 53 | } |
| 54 | 54 | } |
| 55 | 55 | } |
@@ -62,24 +62,24 @@ discard block |
||
| 62 | 62 | * @param string $uri |
| 63 | 63 | */ |
| 64 | 64 | |
| 65 | - public function sync_delete( $type, $object_id, $uri ) { |
|
| 65 | + public function sync_delete($type, $object_id, $uri) { |
|
| 66 | 66 | |
| 67 | - $urls = explode( "\n", get_option( Webhooks_Loader::URLS_OPTION_NAME, '' ) ); |
|
| 68 | - if ( empty( $urls ) ) { |
|
| 67 | + $urls = explode("\n", get_option(Webhooks_Loader::URLS_OPTION_NAME, '')); |
|
| 68 | + if (empty($urls)) { |
|
| 69 | 69 | return; |
| 70 | 70 | } |
| 71 | 71 | |
| 72 | - if ( ! apply_filters( 'wl_webhooks__sync_delete', true, $type, $object_id, $uri ) ) { |
|
| 72 | + if ( ! apply_filters('wl_webhooks__sync_delete', true, $type, $object_id, $uri)) { |
|
| 73 | 73 | return; |
| 74 | 74 | } |
| 75 | 75 | |
| 76 | - foreach ( $urls as $template_url ) { |
|
| 77 | - $url = add_query_arg( array( 'uri' => $uri ), $template_url ); |
|
| 78 | - $filtered_url = apply_filters( 'wl_webhooks__sync_delete__url', $url, $type, $object_id, $uri ); |
|
| 79 | - wp_remote_request( $filtered_url, apply_filters( 'wl_webhooks__sync_delete__args', array( |
|
| 76 | + foreach ($urls as $template_url) { |
|
| 77 | + $url = add_query_arg(array('uri' => $uri), $template_url); |
|
| 78 | + $filtered_url = apply_filters('wl_webhooks__sync_delete__url', $url, $type, $object_id, $uri); |
|
| 79 | + wp_remote_request($filtered_url, apply_filters('wl_webhooks__sync_delete__args', array( |
|
| 80 | 80 | 'blocking' => false, |
| 81 | 81 | 'method' => 'DELETE', |
| 82 | - ) ) ); |
|
| 82 | + ))); |
|
| 83 | 83 | } |
| 84 | 84 | |
| 85 | 85 | } |
@@ -12,159 +12,159 @@ |
||
| 12 | 12 | |
| 13 | 13 | class Jsonld_Generator { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * @var \Wordlift_Entity_Type_Service |
|
| 17 | - */ |
|
| 18 | - private $entity_type_service; |
|
| 19 | - /** |
|
| 20 | - * @var \Wordlift_Property_Getter |
|
| 21 | - */ |
|
| 22 | - private $property_getter; |
|
| 23 | - /** |
|
| 24 | - * @var Type_Service |
|
| 25 | - */ |
|
| 26 | - private $term_entity_type_service; |
|
| 27 | - /** |
|
| 28 | - * @var \Wordlift_Entity_Service |
|
| 29 | - */ |
|
| 30 | - private $entity_service; |
|
| 31 | - |
|
| 32 | - public function __construct( $entity_type_service, $property_getter ) { |
|
| 33 | - $this->entity_type_service = $entity_type_service; |
|
| 34 | - $this->property_getter = $property_getter; |
|
| 35 | - $this->term_entity_type_service = Type_Service::get_instance(); |
|
| 36 | - $this->entity_service = \Wordlift_Entity_Service::get_instance(); |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - public function init() { |
|
| 40 | - add_filter( 'wl_term_jsonld_array', array( $this, 'wl_term_jsonld_array' ), 10, 2 ); |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - public function wl_term_jsonld_array( $data, $term_id ) { |
|
| 44 | - $jsonld = $data['jsonld']; |
|
| 45 | - $references = $data['references']; |
|
| 46 | - |
|
| 47 | - $term_jsonld_data = $this->get_jsonld_data_for_term( $term_id ); |
|
| 48 | - |
|
| 49 | - // Return early if we dont have the entity data |
|
| 50 | - // for the term. |
|
| 51 | - if ( ! $term_jsonld_data ) { |
|
| 52 | - return $data; |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - $term_jsonld = $term_jsonld_data['jsonld']; |
|
| 56 | - |
|
| 57 | - $references = array_merge( $references, $term_jsonld_data['references'] ); |
|
| 58 | - |
|
| 59 | - |
|
| 60 | - array_unshift( $jsonld, $term_jsonld ); |
|
| 61 | - |
|
| 62 | - return array( |
|
| 63 | - 'jsonld' => $jsonld, |
|
| 64 | - 'references' => $references |
|
| 65 | - ); |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - private function get_jsonld_data_for_term( $term_id ) { |
|
| 69 | - |
|
| 70 | - $id = wl_get_term_entity_uri( $term_id ); |
|
| 71 | - |
|
| 72 | - // If we dont have a dataset URI, then dont publish the term data |
|
| 73 | - // on this page. |
|
| 74 | - if ( ! $id ) { |
|
| 75 | - return false; |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - $references = array(); |
|
| 79 | - $term = get_term( $term_id ); |
|
| 80 | - $permalink = get_term_link( $term ); |
|
| 81 | - |
|
| 82 | - $custom_fields = $this->entity_type_service->get_custom_fields_for_term( $term_id ); |
|
| 83 | - $term = get_term( $term_id ); |
|
| 84 | - $jsonld = array( |
|
| 85 | - '@context' => 'http://schema.org', |
|
| 86 | - 'name' => $term->name, |
|
| 87 | - '@type' => $this->term_entity_type_service->get_entity_types_labels( $term_id ), |
|
| 88 | - '@id' => $id, |
|
| 89 | - 'description' => $term->description, |
|
| 90 | - ); |
|
| 91 | - |
|
| 92 | - if ( ! $custom_fields || ! is_array( $custom_fields ) ) { |
|
| 93 | - return $jsonld; |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - foreach ( $custom_fields as $key => $value ) { |
|
| 97 | - $name = $this->relative_to_schema_context( $value['predicate'] ); |
|
| 98 | - $value = $this->property_getter->get( $term_id, $key, Object_Type_Enum::TERM ); |
|
| 99 | - $value = $this->process_value( $value, $references ); |
|
| 100 | - if ( ! $value ) { |
|
| 101 | - continue; |
|
| 102 | - } |
|
| 103 | - $jsonld[ $name ] = $value; |
|
| 104 | - |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - if ( $permalink ) { |
|
| 108 | - $jsonld['mainEntityOfPage'] = $permalink; |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - $this->add_url( $jsonld, $term_id ); |
|
| 112 | - |
|
| 113 | - return apply_filters( 'wl_no_vocabulary_term_jsonld_array', array( |
|
| 114 | - 'jsonld' => $jsonld, |
|
| 115 | - 'references' => $references |
|
| 116 | - ), $term_id ); |
|
| 117 | - |
|
| 118 | - |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - private function add_url( &$jsonld, $term_id ) { |
|
| 122 | - $urls = get_term_meta( $term_id, 'wl_schema_url' ); |
|
| 123 | - if ( empty( $urls ) ) { |
|
| 124 | - return; |
|
| 125 | - } |
|
| 15 | + /** |
|
| 16 | + * @var \Wordlift_Entity_Type_Service |
|
| 17 | + */ |
|
| 18 | + private $entity_type_service; |
|
| 19 | + /** |
|
| 20 | + * @var \Wordlift_Property_Getter |
|
| 21 | + */ |
|
| 22 | + private $property_getter; |
|
| 23 | + /** |
|
| 24 | + * @var Type_Service |
|
| 25 | + */ |
|
| 26 | + private $term_entity_type_service; |
|
| 27 | + /** |
|
| 28 | + * @var \Wordlift_Entity_Service |
|
| 29 | + */ |
|
| 30 | + private $entity_service; |
|
| 31 | + |
|
| 32 | + public function __construct( $entity_type_service, $property_getter ) { |
|
| 33 | + $this->entity_type_service = $entity_type_service; |
|
| 34 | + $this->property_getter = $property_getter; |
|
| 35 | + $this->term_entity_type_service = Type_Service::get_instance(); |
|
| 36 | + $this->entity_service = \Wordlift_Entity_Service::get_instance(); |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + public function init() { |
|
| 40 | + add_filter( 'wl_term_jsonld_array', array( $this, 'wl_term_jsonld_array' ), 10, 2 ); |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + public function wl_term_jsonld_array( $data, $term_id ) { |
|
| 44 | + $jsonld = $data['jsonld']; |
|
| 45 | + $references = $data['references']; |
|
| 46 | + |
|
| 47 | + $term_jsonld_data = $this->get_jsonld_data_for_term( $term_id ); |
|
| 48 | + |
|
| 49 | + // Return early if we dont have the entity data |
|
| 50 | + // for the term. |
|
| 51 | + if ( ! $term_jsonld_data ) { |
|
| 52 | + return $data; |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + $term_jsonld = $term_jsonld_data['jsonld']; |
|
| 56 | + |
|
| 57 | + $references = array_merge( $references, $term_jsonld_data['references'] ); |
|
| 58 | + |
|
| 59 | + |
|
| 60 | + array_unshift( $jsonld, $term_jsonld ); |
|
| 61 | + |
|
| 62 | + return array( |
|
| 63 | + 'jsonld' => $jsonld, |
|
| 64 | + 'references' => $references |
|
| 65 | + ); |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + private function get_jsonld_data_for_term( $term_id ) { |
|
| 69 | + |
|
| 70 | + $id = wl_get_term_entity_uri( $term_id ); |
|
| 71 | + |
|
| 72 | + // If we dont have a dataset URI, then dont publish the term data |
|
| 73 | + // on this page. |
|
| 74 | + if ( ! $id ) { |
|
| 75 | + return false; |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + $references = array(); |
|
| 79 | + $term = get_term( $term_id ); |
|
| 80 | + $permalink = get_term_link( $term ); |
|
| 81 | + |
|
| 82 | + $custom_fields = $this->entity_type_service->get_custom_fields_for_term( $term_id ); |
|
| 83 | + $term = get_term( $term_id ); |
|
| 84 | + $jsonld = array( |
|
| 85 | + '@context' => 'http://schema.org', |
|
| 86 | + 'name' => $term->name, |
|
| 87 | + '@type' => $this->term_entity_type_service->get_entity_types_labels( $term_id ), |
|
| 88 | + '@id' => $id, |
|
| 89 | + 'description' => $term->description, |
|
| 90 | + ); |
|
| 91 | + |
|
| 92 | + if ( ! $custom_fields || ! is_array( $custom_fields ) ) { |
|
| 93 | + return $jsonld; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + foreach ( $custom_fields as $key => $value ) { |
|
| 97 | + $name = $this->relative_to_schema_context( $value['predicate'] ); |
|
| 98 | + $value = $this->property_getter->get( $term_id, $key, Object_Type_Enum::TERM ); |
|
| 99 | + $value = $this->process_value( $value, $references ); |
|
| 100 | + if ( ! $value ) { |
|
| 101 | + continue; |
|
| 102 | + } |
|
| 103 | + $jsonld[ $name ] = $value; |
|
| 104 | + |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + if ( $permalink ) { |
|
| 108 | + $jsonld['mainEntityOfPage'] = $permalink; |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + $this->add_url( $jsonld, $term_id ); |
|
| 112 | + |
|
| 113 | + return apply_filters( 'wl_no_vocabulary_term_jsonld_array', array( |
|
| 114 | + 'jsonld' => $jsonld, |
|
| 115 | + 'references' => $references |
|
| 116 | + ), $term_id ); |
|
| 117 | + |
|
| 118 | + |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + private function add_url( &$jsonld, $term_id ) { |
|
| 122 | + $urls = get_term_meta( $term_id, 'wl_schema_url' ); |
|
| 123 | + if ( empty( $urls ) ) { |
|
| 124 | + return; |
|
| 125 | + } |
|
| 126 | 126 | |
| 127 | - $permalink = get_term_link( $term_id ); |
|
| 128 | - $jsonld['url'] = array_map( function ( $item ) use ( $permalink ) { |
|
| 129 | - return str_replace( '<permalink>', $permalink, $item ); |
|
| 130 | - }, $urls ); |
|
| 131 | - } |
|
| 127 | + $permalink = get_term_link( $term_id ); |
|
| 128 | + $jsonld['url'] = array_map( function ( $item ) use ( $permalink ) { |
|
| 129 | + return str_replace( '<permalink>', $permalink, $item ); |
|
| 130 | + }, $urls ); |
|
| 131 | + } |
|
| 132 | 132 | |
| 133 | - private function relative_to_schema_context( $predicate ) { |
|
| 134 | - return str_replace( 'http://schema.org/', '', $predicate ); |
|
| 135 | - } |
|
| 133 | + private function relative_to_schema_context( $predicate ) { |
|
| 134 | + return str_replace( 'http://schema.org/', '', $predicate ); |
|
| 135 | + } |
|
| 136 | 136 | |
| 137 | - private function process_value( $value, &$references ) { |
|
| 137 | + private function process_value( $value, &$references ) { |
|
| 138 | 138 | |
| 139 | - if ( ! $value ) { |
|
| 140 | - return false; |
|
| 141 | - } |
|
| 139 | + if ( ! $value ) { |
|
| 140 | + return false; |
|
| 141 | + } |
|
| 142 | 142 | |
| 143 | - if ( is_array( $value ) |
|
| 144 | - && count( $value ) > 0 |
|
| 145 | - && $value[0] instanceof \Wordlift_Property_Entity_Reference ) { |
|
| 143 | + if ( is_array( $value ) |
|
| 144 | + && count( $value ) > 0 |
|
| 145 | + && $value[0] instanceof \Wordlift_Property_Entity_Reference ) { |
|
| 146 | 146 | |
| 147 | - // All of the references from the custom fields are post references. |
|
| 148 | - $references = array_merge( $references, array_map( function ( $property_entity_reference ) { |
|
| 149 | - /** |
|
| 150 | - * @var $property_entity_reference \Wordlift_Property_Entity_Reference |
|
| 151 | - */ |
|
| 152 | - return new Post_Reference( $property_entity_reference->get_id() ); |
|
| 153 | - }, $value ) ); |
|
| 147 | + // All of the references from the custom fields are post references. |
|
| 148 | + $references = array_merge( $references, array_map( function ( $property_entity_reference ) { |
|
| 149 | + /** |
|
| 150 | + * @var $property_entity_reference \Wordlift_Property_Entity_Reference |
|
| 151 | + */ |
|
| 152 | + return new Post_Reference( $property_entity_reference->get_id() ); |
|
| 153 | + }, $value ) ); |
|
| 154 | 154 | |
| 155 | 155 | |
| 156 | - $that = $this; |
|
| 156 | + $that = $this; |
|
| 157 | 157 | |
| 158 | - return array_map( function ( $reference ) use ( $that ) { |
|
| 159 | - /** |
|
| 160 | - * @var $reference \Wordlift_Property_Entity_Reference |
|
| 161 | - */ |
|
| 162 | - return array( '@id' => $that->entity_service->get_uri( $reference->get_id() ) ); |
|
| 163 | - }, $value ); |
|
| 164 | - |
|
| 165 | - } |
|
| 158 | + return array_map( function ( $reference ) use ( $that ) { |
|
| 159 | + /** |
|
| 160 | + * @var $reference \Wordlift_Property_Entity_Reference |
|
| 161 | + */ |
|
| 162 | + return array( '@id' => $that->entity_service->get_uri( $reference->get_id() ) ); |
|
| 163 | + }, $value ); |
|
| 164 | + |
|
| 165 | + } |
|
| 166 | 166 | |
| 167 | - return $value; |
|
| 168 | - } |
|
| 167 | + return $value; |
|
| 168 | + } |
|
| 169 | 169 | |
| 170 | 170 | } |
| 171 | 171 | \ No newline at end of file |
@@ -29,7 +29,7 @@ discard block |
||
| 29 | 29 | */ |
| 30 | 30 | private $entity_service; |
| 31 | 31 | |
| 32 | - public function __construct( $entity_type_service, $property_getter ) { |
|
| 32 | + public function __construct($entity_type_service, $property_getter) { |
|
| 33 | 33 | $this->entity_type_service = $entity_type_service; |
| 34 | 34 | $this->property_getter = $property_getter; |
| 35 | 35 | $this->term_entity_type_service = Type_Service::get_instance(); |
@@ -37,27 +37,27 @@ discard block |
||
| 37 | 37 | } |
| 38 | 38 | |
| 39 | 39 | public function init() { |
| 40 | - add_filter( 'wl_term_jsonld_array', array( $this, 'wl_term_jsonld_array' ), 10, 2 ); |
|
| 40 | + add_filter('wl_term_jsonld_array', array($this, 'wl_term_jsonld_array'), 10, 2); |
|
| 41 | 41 | } |
| 42 | 42 | |
| 43 | - public function wl_term_jsonld_array( $data, $term_id ) { |
|
| 43 | + public function wl_term_jsonld_array($data, $term_id) { |
|
| 44 | 44 | $jsonld = $data['jsonld']; |
| 45 | 45 | $references = $data['references']; |
| 46 | 46 | |
| 47 | - $term_jsonld_data = $this->get_jsonld_data_for_term( $term_id ); |
|
| 47 | + $term_jsonld_data = $this->get_jsonld_data_for_term($term_id); |
|
| 48 | 48 | |
| 49 | 49 | // Return early if we dont have the entity data |
| 50 | 50 | // for the term. |
| 51 | - if ( ! $term_jsonld_data ) { |
|
| 51 | + if ( ! $term_jsonld_data) { |
|
| 52 | 52 | return $data; |
| 53 | 53 | } |
| 54 | 54 | |
| 55 | 55 | $term_jsonld = $term_jsonld_data['jsonld']; |
| 56 | 56 | |
| 57 | - $references = array_merge( $references, $term_jsonld_data['references'] ); |
|
| 57 | + $references = array_merge($references, $term_jsonld_data['references']); |
|
| 58 | 58 | |
| 59 | 59 | |
| 60 | - array_unshift( $jsonld, $term_jsonld ); |
|
| 60 | + array_unshift($jsonld, $term_jsonld); |
|
| 61 | 61 | |
| 62 | 62 | return array( |
| 63 | 63 | 'jsonld' => $jsonld, |
@@ -65,102 +65,102 @@ discard block |
||
| 65 | 65 | ); |
| 66 | 66 | } |
| 67 | 67 | |
| 68 | - private function get_jsonld_data_for_term( $term_id ) { |
|
| 68 | + private function get_jsonld_data_for_term($term_id) { |
|
| 69 | 69 | |
| 70 | - $id = wl_get_term_entity_uri( $term_id ); |
|
| 70 | + $id = wl_get_term_entity_uri($term_id); |
|
| 71 | 71 | |
| 72 | 72 | // If we dont have a dataset URI, then dont publish the term data |
| 73 | 73 | // on this page. |
| 74 | - if ( ! $id ) { |
|
| 74 | + if ( ! $id) { |
|
| 75 | 75 | return false; |
| 76 | 76 | } |
| 77 | 77 | |
| 78 | 78 | $references = array(); |
| 79 | - $term = get_term( $term_id ); |
|
| 80 | - $permalink = get_term_link( $term ); |
|
| 79 | + $term = get_term($term_id); |
|
| 80 | + $permalink = get_term_link($term); |
|
| 81 | 81 | |
| 82 | - $custom_fields = $this->entity_type_service->get_custom_fields_for_term( $term_id ); |
|
| 83 | - $term = get_term( $term_id ); |
|
| 82 | + $custom_fields = $this->entity_type_service->get_custom_fields_for_term($term_id); |
|
| 83 | + $term = get_term($term_id); |
|
| 84 | 84 | $jsonld = array( |
| 85 | 85 | '@context' => 'http://schema.org', |
| 86 | 86 | 'name' => $term->name, |
| 87 | - '@type' => $this->term_entity_type_service->get_entity_types_labels( $term_id ), |
|
| 87 | + '@type' => $this->term_entity_type_service->get_entity_types_labels($term_id), |
|
| 88 | 88 | '@id' => $id, |
| 89 | 89 | 'description' => $term->description, |
| 90 | 90 | ); |
| 91 | 91 | |
| 92 | - if ( ! $custom_fields || ! is_array( $custom_fields ) ) { |
|
| 92 | + if ( ! $custom_fields || ! is_array($custom_fields)) { |
|
| 93 | 93 | return $jsonld; |
| 94 | 94 | } |
| 95 | 95 | |
| 96 | - foreach ( $custom_fields as $key => $value ) { |
|
| 97 | - $name = $this->relative_to_schema_context( $value['predicate'] ); |
|
| 98 | - $value = $this->property_getter->get( $term_id, $key, Object_Type_Enum::TERM ); |
|
| 99 | - $value = $this->process_value( $value, $references ); |
|
| 100 | - if ( ! $value ) { |
|
| 96 | + foreach ($custom_fields as $key => $value) { |
|
| 97 | + $name = $this->relative_to_schema_context($value['predicate']); |
|
| 98 | + $value = $this->property_getter->get($term_id, $key, Object_Type_Enum::TERM); |
|
| 99 | + $value = $this->process_value($value, $references); |
|
| 100 | + if ( ! $value) { |
|
| 101 | 101 | continue; |
| 102 | 102 | } |
| 103 | - $jsonld[ $name ] = $value; |
|
| 103 | + $jsonld[$name] = $value; |
|
| 104 | 104 | |
| 105 | 105 | } |
| 106 | 106 | |
| 107 | - if ( $permalink ) { |
|
| 107 | + if ($permalink) { |
|
| 108 | 108 | $jsonld['mainEntityOfPage'] = $permalink; |
| 109 | 109 | } |
| 110 | 110 | |
| 111 | - $this->add_url( $jsonld, $term_id ); |
|
| 111 | + $this->add_url($jsonld, $term_id); |
|
| 112 | 112 | |
| 113 | - return apply_filters( 'wl_no_vocabulary_term_jsonld_array', array( |
|
| 113 | + return apply_filters('wl_no_vocabulary_term_jsonld_array', array( |
|
| 114 | 114 | 'jsonld' => $jsonld, |
| 115 | 115 | 'references' => $references |
| 116 | - ), $term_id ); |
|
| 116 | + ), $term_id); |
|
| 117 | 117 | |
| 118 | 118 | |
| 119 | 119 | } |
| 120 | 120 | |
| 121 | - private function add_url( &$jsonld, $term_id ) { |
|
| 122 | - $urls = get_term_meta( $term_id, 'wl_schema_url' ); |
|
| 123 | - if ( empty( $urls ) ) { |
|
| 121 | + private function add_url(&$jsonld, $term_id) { |
|
| 122 | + $urls = get_term_meta($term_id, 'wl_schema_url'); |
|
| 123 | + if (empty($urls)) { |
|
| 124 | 124 | return; |
| 125 | 125 | } |
| 126 | 126 | |
| 127 | - $permalink = get_term_link( $term_id ); |
|
| 128 | - $jsonld['url'] = array_map( function ( $item ) use ( $permalink ) { |
|
| 129 | - return str_replace( '<permalink>', $permalink, $item ); |
|
| 130 | - }, $urls ); |
|
| 127 | + $permalink = get_term_link($term_id); |
|
| 128 | + $jsonld['url'] = array_map(function($item) use ($permalink) { |
|
| 129 | + return str_replace('<permalink>', $permalink, $item); |
|
| 130 | + }, $urls); |
|
| 131 | 131 | } |
| 132 | 132 | |
| 133 | - private function relative_to_schema_context( $predicate ) { |
|
| 134 | - return str_replace( 'http://schema.org/', '', $predicate ); |
|
| 133 | + private function relative_to_schema_context($predicate) { |
|
| 134 | + return str_replace('http://schema.org/', '', $predicate); |
|
| 135 | 135 | } |
| 136 | 136 | |
| 137 | - private function process_value( $value, &$references ) { |
|
| 137 | + private function process_value($value, &$references) { |
|
| 138 | 138 | |
| 139 | - if ( ! $value ) { |
|
| 139 | + if ( ! $value) { |
|
| 140 | 140 | return false; |
| 141 | 141 | } |
| 142 | 142 | |
| 143 | - if ( is_array( $value ) |
|
| 144 | - && count( $value ) > 0 |
|
| 145 | - && $value[0] instanceof \Wordlift_Property_Entity_Reference ) { |
|
| 143 | + if (is_array($value) |
|
| 144 | + && count($value) > 0 |
|
| 145 | + && $value[0] instanceof \Wordlift_Property_Entity_Reference) { |
|
| 146 | 146 | |
| 147 | 147 | // All of the references from the custom fields are post references. |
| 148 | - $references = array_merge( $references, array_map( function ( $property_entity_reference ) { |
|
| 148 | + $references = array_merge($references, array_map(function($property_entity_reference) { |
|
| 149 | 149 | /** |
| 150 | 150 | * @var $property_entity_reference \Wordlift_Property_Entity_Reference |
| 151 | 151 | */ |
| 152 | - return new Post_Reference( $property_entity_reference->get_id() ); |
|
| 153 | - }, $value ) ); |
|
| 152 | + return new Post_Reference($property_entity_reference->get_id()); |
|
| 153 | + }, $value)); |
|
| 154 | 154 | |
| 155 | 155 | |
| 156 | 156 | $that = $this; |
| 157 | 157 | |
| 158 | - return array_map( function ( $reference ) use ( $that ) { |
|
| 158 | + return array_map(function($reference) use ($that) { |
|
| 159 | 159 | /** |
| 160 | 160 | * @var $reference \Wordlift_Property_Entity_Reference |
| 161 | 161 | */ |
| 162 | - return array( '@id' => $that->entity_service->get_uri( $reference->get_id() ) ); |
|
| 163 | - }, $value ); |
|
| 162 | + return array('@id' => $that->entity_service->get_uri($reference->get_id())); |
|
| 163 | + }, $value); |
|
| 164 | 164 | |
| 165 | 165 | } |
| 166 | 166 | |