@@ -23,55 +23,55 @@ |
||
| 23 | 23 | */ |
| 24 | 24 | class Wordlift_Post_Excerpt_Helper { |
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * Get the text excerpt for the provided {@link WP_Post}. |
|
| 28 | - * |
|
| 29 | - * Since anyone can hook on the excerpt generation filters, and |
|
| 30 | - * amend it with non textual content, we play it self and generate |
|
| 31 | - * the excerpt ourselves, mimicking the way WordPress core does it. |
|
| 32 | - * |
|
| 33 | - * @since 3.10.0 |
|
| 34 | - * |
|
| 35 | - * @param WP_Post $post The {@link WP_Post}. |
|
| 36 | - * @param int $length The desired excerpt length. |
|
| 37 | - * @param string $more The desired more string. |
|
| 38 | - * |
|
| 39 | - * @return string The excerpt. |
|
| 40 | - */ |
|
| 41 | - public static function get_text_excerpt( $post, $length = 55, $more = '...' ) { |
|
| 26 | + /** |
|
| 27 | + * Get the text excerpt for the provided {@link WP_Post}. |
|
| 28 | + * |
|
| 29 | + * Since anyone can hook on the excerpt generation filters, and |
|
| 30 | + * amend it with non textual content, we play it self and generate |
|
| 31 | + * the excerpt ourselves, mimicking the way WordPress core does it. |
|
| 32 | + * |
|
| 33 | + * @since 3.10.0 |
|
| 34 | + * |
|
| 35 | + * @param WP_Post $post The {@link WP_Post}. |
|
| 36 | + * @param int $length The desired excerpt length. |
|
| 37 | + * @param string $more The desired more string. |
|
| 38 | + * |
|
| 39 | + * @return string The excerpt. |
|
| 40 | + */ |
|
| 41 | + public static function get_text_excerpt( $post, $length = 55, $more = '...' ) { |
|
| 42 | 42 | |
| 43 | - /* |
|
| 43 | + /* |
|
| 44 | 44 | * Apply the `wl_post_content` filter, in case 3rd parties want to change the post content, e.g. |
| 45 | 45 | * because the content is written elsewhere. |
| 46 | 46 | * |
| 47 | 47 | * @since 3.20.0 |
| 48 | 48 | */ |
| 49 | - $post_content = apply_filters( 'wl_post_content', $post->post_content, $post ); |
|
| 49 | + $post_content = apply_filters( 'wl_post_content', $post->post_content, $post ); |
|
| 50 | 50 | |
| 51 | - // Filter shortcode content in post_content, before using it for trimming |
|
| 52 | - $post_content = do_shortcode( $post_content ); |
|
| 51 | + // Filter shortcode content in post_content, before using it for trimming |
|
| 52 | + $post_content = do_shortcode( $post_content ); |
|
| 53 | 53 | |
| 54 | - // Get the excerpt and trim it. Use the `post_excerpt` if available. |
|
| 55 | - $excerpt = wp_trim_words( ! empty( $post->post_excerpt ) ? $post->post_excerpt : $post_content, $length, $more ); |
|
| 54 | + // Get the excerpt and trim it. Use the `post_excerpt` if available. |
|
| 55 | + $excerpt = wp_trim_words( ! empty( $post->post_excerpt ) ? $post->post_excerpt : $post_content, $length, $more ); |
|
| 56 | 56 | |
| 57 | - // Remove shortcodes and decode html entities. |
|
| 58 | - return wp_strip_all_tags( html_entity_decode( self::strip_all_shortcodes( $excerpt ) ) ); |
|
| 59 | - } |
|
| 57 | + // Remove shortcodes and decode html entities. |
|
| 58 | + return wp_strip_all_tags( html_entity_decode( self::strip_all_shortcodes( $excerpt ) ) ); |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * Remove all the shortcodes from the content. We're using our own function |
|
| 63 | - * because WordPress' own `strip_shortcodes` only takes into consideration |
|
| 64 | - * shortcodes for installed plugins/themes. |
|
| 65 | - * |
|
| 66 | - * @since 3.12.0 |
|
| 67 | - * |
|
| 68 | - * @param string $content The content with shortcodes. |
|
| 69 | - * |
|
| 70 | - * @return string The content without shortcodes. |
|
| 71 | - */ |
|
| 72 | - private static function strip_all_shortcodes( $content ) { |
|
| 61 | + /** |
|
| 62 | + * Remove all the shortcodes from the content. We're using our own function |
|
| 63 | + * because WordPress' own `strip_shortcodes` only takes into consideration |
|
| 64 | + * shortcodes for installed plugins/themes. |
|
| 65 | + * |
|
| 66 | + * @since 3.12.0 |
|
| 67 | + * |
|
| 68 | + * @param string $content The content with shortcodes. |
|
| 69 | + * |
|
| 70 | + * @return string The content without shortcodes. |
|
| 71 | + */ |
|
| 72 | + private static function strip_all_shortcodes( $content ) { |
|
| 73 | 73 | |
| 74 | - return preg_replace( '/\[[^]]+\]/', '', $content ); |
|
| 75 | - } |
|
| 74 | + return preg_replace( '/\[[^]]+\]/', '', $content ); |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | 77 | } |
@@ -38,7 +38,7 @@ discard block |
||
| 38 | 38 | * |
| 39 | 39 | * @return string The excerpt. |
| 40 | 40 | */ |
| 41 | - public static function get_text_excerpt( $post, $length = 55, $more = '...' ) { |
|
| 41 | + public static function get_text_excerpt($post, $length = 55, $more = '...') { |
|
| 42 | 42 | |
| 43 | 43 | /* |
| 44 | 44 | * Apply the `wl_post_content` filter, in case 3rd parties want to change the post content, e.g. |
@@ -46,16 +46,16 @@ discard block |
||
| 46 | 46 | * |
| 47 | 47 | * @since 3.20.0 |
| 48 | 48 | */ |
| 49 | - $post_content = apply_filters( 'wl_post_content', $post->post_content, $post ); |
|
| 49 | + $post_content = apply_filters('wl_post_content', $post->post_content, $post); |
|
| 50 | 50 | |
| 51 | 51 | // Filter shortcode content in post_content, before using it for trimming |
| 52 | - $post_content = do_shortcode( $post_content ); |
|
| 52 | + $post_content = do_shortcode($post_content); |
|
| 53 | 53 | |
| 54 | 54 | // Get the excerpt and trim it. Use the `post_excerpt` if available. |
| 55 | - $excerpt = wp_trim_words( ! empty( $post->post_excerpt ) ? $post->post_excerpt : $post_content, $length, $more ); |
|
| 55 | + $excerpt = wp_trim_words( ! empty($post->post_excerpt) ? $post->post_excerpt : $post_content, $length, $more); |
|
| 56 | 56 | |
| 57 | 57 | // Remove shortcodes and decode html entities. |
| 58 | - return wp_strip_all_tags( html_entity_decode( self::strip_all_shortcodes( $excerpt ) ) ); |
|
| 58 | + return wp_strip_all_tags(html_entity_decode(self::strip_all_shortcodes($excerpt))); |
|
| 59 | 59 | } |
| 60 | 60 | |
| 61 | 61 | /** |
@@ -69,9 +69,9 @@ discard block |
||
| 69 | 69 | * |
| 70 | 70 | * @return string The content without shortcodes. |
| 71 | 71 | */ |
| 72 | - private static function strip_all_shortcodes( $content ) { |
|
| 72 | + private static function strip_all_shortcodes($content) { |
|
| 73 | 73 | |
| 74 | - return preg_replace( '/\[[^]]+\]/', '', $content ); |
|
| 74 | + return preg_replace('/\[[^]]+\]/', '', $content); |
|
| 75 | 75 | } |
| 76 | 76 | |
| 77 | 77 | } |
@@ -11,167 +11,167 @@ |
||
| 11 | 11 | |
| 12 | 12 | class Jsonld_Generator { |
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * @var \Wordlift_Entity_Type_Service |
|
| 16 | - */ |
|
| 17 | - private $entity_type_service; |
|
| 18 | - /** |
|
| 19 | - * @var \Wordlift_Property_Getter |
|
| 20 | - */ |
|
| 21 | - private $property_getter; |
|
| 22 | - /** |
|
| 23 | - * @var Type_Service |
|
| 24 | - */ |
|
| 25 | - private $term_entity_type_service; |
|
| 26 | - /** |
|
| 27 | - * @var \Wordlift_Entity_Service |
|
| 28 | - */ |
|
| 29 | - private $entity_service; |
|
| 30 | - |
|
| 31 | - public function __construct( $entity_type_service, $property_getter ) { |
|
| 32 | - $this->entity_type_service = $entity_type_service; |
|
| 33 | - $this->property_getter = $property_getter; |
|
| 34 | - $this->term_entity_type_service = Type_Service::get_instance(); |
|
| 35 | - $this->entity_service = \Wordlift_Entity_Service::get_instance(); |
|
| 36 | - } |
|
| 37 | - |
|
| 38 | - public function init() { |
|
| 39 | - add_filter( 'wl_term_jsonld_array', array( $this, 'wl_term_jsonld_array' ), 10, 2 ); |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - public function wl_term_jsonld_array( $data, $term_id ) { |
|
| 43 | - $jsonld = $data['jsonld']; |
|
| 44 | - $references = $data['references']; |
|
| 45 | - |
|
| 46 | - $term_jsonld_data = $this->get_jsonld_data_for_term( $term_id ); |
|
| 47 | - |
|
| 48 | - // Return early if we dont have the entity data |
|
| 49 | - // for the term. |
|
| 50 | - if ( ! $term_jsonld_data ) { |
|
| 51 | - return $data; |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - $term_jsonld = $term_jsonld_data['jsonld']; |
|
| 55 | - |
|
| 56 | - $references = array_merge( $references, $term_jsonld_data['references'] ); |
|
| 57 | - |
|
| 58 | - array_unshift( $jsonld, $term_jsonld ); |
|
| 59 | - |
|
| 60 | - return array( |
|
| 61 | - 'jsonld' => $jsonld, |
|
| 62 | - 'references' => $references, |
|
| 63 | - ); |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - private function get_jsonld_data_for_term( $term_id ) { |
|
| 67 | - |
|
| 68 | - $id = $this->entity_service->get_uri( $term_id, Object_Type_Enum::TERM ); |
|
| 69 | - |
|
| 70 | - // If we don't have a dataset URI, then don't publish the term data |
|
| 71 | - // on this page. |
|
| 72 | - if ( ! $id ) { |
|
| 73 | - return false; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - $references = array(); |
|
| 77 | - $term = get_term( $term_id ); |
|
| 78 | - $permalink = get_term_link( $term ); |
|
| 79 | - |
|
| 80 | - $custom_fields = $this->entity_type_service->get_custom_fields_for_term( $term_id ); |
|
| 81 | - $term = get_term( $term_id ); |
|
| 82 | - $jsonld = array( |
|
| 83 | - '@context' => 'http://schema.org', |
|
| 84 | - 'name' => $term->name, |
|
| 85 | - '@type' => $this->term_entity_type_service->get_entity_types_labels( $term_id ), |
|
| 86 | - '@id' => $id, |
|
| 87 | - 'description' => wp_strip_all_tags( self::strip_all_shortcodes( $term->description ) ), |
|
| 88 | - ); |
|
| 89 | - |
|
| 90 | - if ( ! $custom_fields || ! is_array( $custom_fields ) ) { |
|
| 91 | - return $jsonld; |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - foreach ( $custom_fields as $key => $value ) { |
|
| 95 | - $name = $this->relative_to_schema_context( $value['predicate'] ); |
|
| 96 | - $value = $this->property_getter->get( $term_id, $key, Object_Type_Enum::TERM ); |
|
| 97 | - $value = $this->process_value( $value, $references ); |
|
| 98 | - if ( ! isset( $value ) || |
|
| 99 | - is_array( $value ) && empty( $value ) || |
|
| 100 | - is_string( $value ) && empty( $value ) ) { |
|
| 101 | - continue; |
|
| 102 | - } |
|
| 103 | - $jsonld[ $name ] = $value; |
|
| 104 | - |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - $jsonld = \Wordlift_Entity_Post_To_Jsonld_Converter::post_process( $jsonld ); |
|
| 108 | - |
|
| 109 | - if ( $permalink ) { |
|
| 110 | - $jsonld['mainEntityOfPage'] = $permalink; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - return apply_filters( |
|
| 114 | - 'wl_no_vocabulary_term_jsonld_array', |
|
| 115 | - array( |
|
| 116 | - 'jsonld' => $jsonld, |
|
| 117 | - 'references' => $references, |
|
| 118 | - ), |
|
| 119 | - $term_id |
|
| 120 | - ); |
|
| 121 | - |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - private function relative_to_schema_context( $predicate ) { |
|
| 125 | - return str_replace( 'http://schema.org/', '', $predicate ); |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - private function process_value( $value, &$references ) { |
|
| 129 | - |
|
| 130 | - if ( is_array( $value ) |
|
| 131 | - && count( $value ) > 0 |
|
| 132 | - && $value[0] instanceof \Wordlift_Property_Entity_Reference ) { |
|
| 133 | - |
|
| 134 | - // All of the references from the custom fields are post references. |
|
| 135 | - $references = array_merge( |
|
| 136 | - $references, |
|
| 137 | - array_map( |
|
| 138 | - function ( $property_entity_reference ) { |
|
| 139 | - /** |
|
| 140 | - * @var $property_entity_reference \Wordlift_Property_Entity_Reference |
|
| 141 | - */ |
|
| 142 | - return $property_entity_reference->to_reference(); |
|
| 143 | - }, |
|
| 144 | - $value |
|
| 145 | - ) |
|
| 146 | - ); |
|
| 147 | - |
|
| 148 | - return array_map( |
|
| 149 | - function ( $reference ) { |
|
| 150 | - /** |
|
| 151 | - * @var $reference \Wordlift_Property_Entity_Reference |
|
| 152 | - */ |
|
| 153 | - return array( '@id' => $reference->get_url() ); |
|
| 154 | - }, |
|
| 155 | - $value |
|
| 156 | - ); |
|
| 157 | - |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - return $value; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * Remove all the shortcodes from the content. We're using our own function |
|
| 165 | - * because WordPress' own `strip_shortcodes` only takes into consideration |
|
| 166 | - * shortcodes for installed plugins/themes. |
|
| 167 | - * |
|
| 168 | - * @since 3.12.0 |
|
| 169 | - * |
|
| 170 | - * @param string $content The content with shortcodes. |
|
| 171 | - * |
|
| 172 | - * @return string The content without shortcodes. |
|
| 173 | - */ |
|
| 174 | - private static function strip_all_shortcodes( $content ) { |
|
| 175 | - return preg_replace( '/\[[^]]+\]/', '', $content ); |
|
| 176 | - } |
|
| 14 | + /** |
|
| 15 | + * @var \Wordlift_Entity_Type_Service |
|
| 16 | + */ |
|
| 17 | + private $entity_type_service; |
|
| 18 | + /** |
|
| 19 | + * @var \Wordlift_Property_Getter |
|
| 20 | + */ |
|
| 21 | + private $property_getter; |
|
| 22 | + /** |
|
| 23 | + * @var Type_Service |
|
| 24 | + */ |
|
| 25 | + private $term_entity_type_service; |
|
| 26 | + /** |
|
| 27 | + * @var \Wordlift_Entity_Service |
|
| 28 | + */ |
|
| 29 | + private $entity_service; |
|
| 30 | + |
|
| 31 | + public function __construct( $entity_type_service, $property_getter ) { |
|
| 32 | + $this->entity_type_service = $entity_type_service; |
|
| 33 | + $this->property_getter = $property_getter; |
|
| 34 | + $this->term_entity_type_service = Type_Service::get_instance(); |
|
| 35 | + $this->entity_service = \Wordlift_Entity_Service::get_instance(); |
|
| 36 | + } |
|
| 37 | + |
|
| 38 | + public function init() { |
|
| 39 | + add_filter( 'wl_term_jsonld_array', array( $this, 'wl_term_jsonld_array' ), 10, 2 ); |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + public function wl_term_jsonld_array( $data, $term_id ) { |
|
| 43 | + $jsonld = $data['jsonld']; |
|
| 44 | + $references = $data['references']; |
|
| 45 | + |
|
| 46 | + $term_jsonld_data = $this->get_jsonld_data_for_term( $term_id ); |
|
| 47 | + |
|
| 48 | + // Return early if we dont have the entity data |
|
| 49 | + // for the term. |
|
| 50 | + if ( ! $term_jsonld_data ) { |
|
| 51 | + return $data; |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + $term_jsonld = $term_jsonld_data['jsonld']; |
|
| 55 | + |
|
| 56 | + $references = array_merge( $references, $term_jsonld_data['references'] ); |
|
| 57 | + |
|
| 58 | + array_unshift( $jsonld, $term_jsonld ); |
|
| 59 | + |
|
| 60 | + return array( |
|
| 61 | + 'jsonld' => $jsonld, |
|
| 62 | + 'references' => $references, |
|
| 63 | + ); |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + private function get_jsonld_data_for_term( $term_id ) { |
|
| 67 | + |
|
| 68 | + $id = $this->entity_service->get_uri( $term_id, Object_Type_Enum::TERM ); |
|
| 69 | + |
|
| 70 | + // If we don't have a dataset URI, then don't publish the term data |
|
| 71 | + // on this page. |
|
| 72 | + if ( ! $id ) { |
|
| 73 | + return false; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + $references = array(); |
|
| 77 | + $term = get_term( $term_id ); |
|
| 78 | + $permalink = get_term_link( $term ); |
|
| 79 | + |
|
| 80 | + $custom_fields = $this->entity_type_service->get_custom_fields_for_term( $term_id ); |
|
| 81 | + $term = get_term( $term_id ); |
|
| 82 | + $jsonld = array( |
|
| 83 | + '@context' => 'http://schema.org', |
|
| 84 | + 'name' => $term->name, |
|
| 85 | + '@type' => $this->term_entity_type_service->get_entity_types_labels( $term_id ), |
|
| 86 | + '@id' => $id, |
|
| 87 | + 'description' => wp_strip_all_tags( self::strip_all_shortcodes( $term->description ) ), |
|
| 88 | + ); |
|
| 89 | + |
|
| 90 | + if ( ! $custom_fields || ! is_array( $custom_fields ) ) { |
|
| 91 | + return $jsonld; |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + foreach ( $custom_fields as $key => $value ) { |
|
| 95 | + $name = $this->relative_to_schema_context( $value['predicate'] ); |
|
| 96 | + $value = $this->property_getter->get( $term_id, $key, Object_Type_Enum::TERM ); |
|
| 97 | + $value = $this->process_value( $value, $references ); |
|
| 98 | + if ( ! isset( $value ) || |
|
| 99 | + is_array( $value ) && empty( $value ) || |
|
| 100 | + is_string( $value ) && empty( $value ) ) { |
|
| 101 | + continue; |
|
| 102 | + } |
|
| 103 | + $jsonld[ $name ] = $value; |
|
| 104 | + |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + $jsonld = \Wordlift_Entity_Post_To_Jsonld_Converter::post_process( $jsonld ); |
|
| 108 | + |
|
| 109 | + if ( $permalink ) { |
|
| 110 | + $jsonld['mainEntityOfPage'] = $permalink; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + return apply_filters( |
|
| 114 | + 'wl_no_vocabulary_term_jsonld_array', |
|
| 115 | + array( |
|
| 116 | + 'jsonld' => $jsonld, |
|
| 117 | + 'references' => $references, |
|
| 118 | + ), |
|
| 119 | + $term_id |
|
| 120 | + ); |
|
| 121 | + |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + private function relative_to_schema_context( $predicate ) { |
|
| 125 | + return str_replace( 'http://schema.org/', '', $predicate ); |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + private function process_value( $value, &$references ) { |
|
| 129 | + |
|
| 130 | + if ( is_array( $value ) |
|
| 131 | + && count( $value ) > 0 |
|
| 132 | + && $value[0] instanceof \Wordlift_Property_Entity_Reference ) { |
|
| 133 | + |
|
| 134 | + // All of the references from the custom fields are post references. |
|
| 135 | + $references = array_merge( |
|
| 136 | + $references, |
|
| 137 | + array_map( |
|
| 138 | + function ( $property_entity_reference ) { |
|
| 139 | + /** |
|
| 140 | + * @var $property_entity_reference \Wordlift_Property_Entity_Reference |
|
| 141 | + */ |
|
| 142 | + return $property_entity_reference->to_reference(); |
|
| 143 | + }, |
|
| 144 | + $value |
|
| 145 | + ) |
|
| 146 | + ); |
|
| 147 | + |
|
| 148 | + return array_map( |
|
| 149 | + function ( $reference ) { |
|
| 150 | + /** |
|
| 151 | + * @var $reference \Wordlift_Property_Entity_Reference |
|
| 152 | + */ |
|
| 153 | + return array( '@id' => $reference->get_url() ); |
|
| 154 | + }, |
|
| 155 | + $value |
|
| 156 | + ); |
|
| 157 | + |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + return $value; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * Remove all the shortcodes from the content. We're using our own function |
|
| 165 | + * because WordPress' own `strip_shortcodes` only takes into consideration |
|
| 166 | + * shortcodes for installed plugins/themes. |
|
| 167 | + * |
|
| 168 | + * @since 3.12.0 |
|
| 169 | + * |
|
| 170 | + * @param string $content The content with shortcodes. |
|
| 171 | + * |
|
| 172 | + * @return string The content without shortcodes. |
|
| 173 | + */ |
|
| 174 | + private static function strip_all_shortcodes( $content ) { |
|
| 175 | + return preg_replace( '/\[[^]]+\]/', '', $content ); |
|
| 176 | + } |
|
| 177 | 177 | } |
@@ -28,7 +28,7 @@ discard block |
||
| 28 | 28 | */ |
| 29 | 29 | private $entity_service; |
| 30 | 30 | |
| 31 | - public function __construct( $entity_type_service, $property_getter ) { |
|
| 31 | + public function __construct($entity_type_service, $property_getter) { |
|
| 32 | 32 | $this->entity_type_service = $entity_type_service; |
| 33 | 33 | $this->property_getter = $property_getter; |
| 34 | 34 | $this->term_entity_type_service = Type_Service::get_instance(); |
@@ -36,26 +36,26 @@ discard block |
||
| 36 | 36 | } |
| 37 | 37 | |
| 38 | 38 | public function init() { |
| 39 | - add_filter( 'wl_term_jsonld_array', array( $this, 'wl_term_jsonld_array' ), 10, 2 ); |
|
| 39 | + add_filter('wl_term_jsonld_array', array($this, 'wl_term_jsonld_array'), 10, 2); |
|
| 40 | 40 | } |
| 41 | 41 | |
| 42 | - public function wl_term_jsonld_array( $data, $term_id ) { |
|
| 42 | + public function wl_term_jsonld_array($data, $term_id) { |
|
| 43 | 43 | $jsonld = $data['jsonld']; |
| 44 | 44 | $references = $data['references']; |
| 45 | 45 | |
| 46 | - $term_jsonld_data = $this->get_jsonld_data_for_term( $term_id ); |
|
| 46 | + $term_jsonld_data = $this->get_jsonld_data_for_term($term_id); |
|
| 47 | 47 | |
| 48 | 48 | // Return early if we dont have the entity data |
| 49 | 49 | // for the term. |
| 50 | - if ( ! $term_jsonld_data ) { |
|
| 50 | + if ( ! $term_jsonld_data) { |
|
| 51 | 51 | return $data; |
| 52 | 52 | } |
| 53 | 53 | |
| 54 | 54 | $term_jsonld = $term_jsonld_data['jsonld']; |
| 55 | 55 | |
| 56 | - $references = array_merge( $references, $term_jsonld_data['references'] ); |
|
| 56 | + $references = array_merge($references, $term_jsonld_data['references']); |
|
| 57 | 57 | |
| 58 | - array_unshift( $jsonld, $term_jsonld ); |
|
| 58 | + array_unshift($jsonld, $term_jsonld); |
|
| 59 | 59 | |
| 60 | 60 | return array( |
| 61 | 61 | 'jsonld' => $jsonld, |
@@ -63,50 +63,50 @@ discard block |
||
| 63 | 63 | ); |
| 64 | 64 | } |
| 65 | 65 | |
| 66 | - private function get_jsonld_data_for_term( $term_id ) { |
|
| 66 | + private function get_jsonld_data_for_term($term_id) { |
|
| 67 | 67 | |
| 68 | - $id = $this->entity_service->get_uri( $term_id, Object_Type_Enum::TERM ); |
|
| 68 | + $id = $this->entity_service->get_uri($term_id, Object_Type_Enum::TERM); |
|
| 69 | 69 | |
| 70 | 70 | // If we don't have a dataset URI, then don't publish the term data |
| 71 | 71 | // on this page. |
| 72 | - if ( ! $id ) { |
|
| 72 | + if ( ! $id) { |
|
| 73 | 73 | return false; |
| 74 | 74 | } |
| 75 | 75 | |
| 76 | 76 | $references = array(); |
| 77 | - $term = get_term( $term_id ); |
|
| 78 | - $permalink = get_term_link( $term ); |
|
| 77 | + $term = get_term($term_id); |
|
| 78 | + $permalink = get_term_link($term); |
|
| 79 | 79 | |
| 80 | - $custom_fields = $this->entity_type_service->get_custom_fields_for_term( $term_id ); |
|
| 81 | - $term = get_term( $term_id ); |
|
| 80 | + $custom_fields = $this->entity_type_service->get_custom_fields_for_term($term_id); |
|
| 81 | + $term = get_term($term_id); |
|
| 82 | 82 | $jsonld = array( |
| 83 | 83 | '@context' => 'http://schema.org', |
| 84 | 84 | 'name' => $term->name, |
| 85 | - '@type' => $this->term_entity_type_service->get_entity_types_labels( $term_id ), |
|
| 85 | + '@type' => $this->term_entity_type_service->get_entity_types_labels($term_id), |
|
| 86 | 86 | '@id' => $id, |
| 87 | - 'description' => wp_strip_all_tags( self::strip_all_shortcodes( $term->description ) ), |
|
| 87 | + 'description' => wp_strip_all_tags(self::strip_all_shortcodes($term->description)), |
|
| 88 | 88 | ); |
| 89 | 89 | |
| 90 | - if ( ! $custom_fields || ! is_array( $custom_fields ) ) { |
|
| 90 | + if ( ! $custom_fields || ! is_array($custom_fields)) { |
|
| 91 | 91 | return $jsonld; |
| 92 | 92 | } |
| 93 | 93 | |
| 94 | - foreach ( $custom_fields as $key => $value ) { |
|
| 95 | - $name = $this->relative_to_schema_context( $value['predicate'] ); |
|
| 96 | - $value = $this->property_getter->get( $term_id, $key, Object_Type_Enum::TERM ); |
|
| 97 | - $value = $this->process_value( $value, $references ); |
|
| 98 | - if ( ! isset( $value ) || |
|
| 99 | - is_array( $value ) && empty( $value ) || |
|
| 100 | - is_string( $value ) && empty( $value ) ) { |
|
| 94 | + foreach ($custom_fields as $key => $value) { |
|
| 95 | + $name = $this->relative_to_schema_context($value['predicate']); |
|
| 96 | + $value = $this->property_getter->get($term_id, $key, Object_Type_Enum::TERM); |
|
| 97 | + $value = $this->process_value($value, $references); |
|
| 98 | + if ( ! isset($value) || |
|
| 99 | + is_array($value) && empty($value) || |
|
| 100 | + is_string($value) && empty($value)) { |
|
| 101 | 101 | continue; |
| 102 | 102 | } |
| 103 | - $jsonld[ $name ] = $value; |
|
| 103 | + $jsonld[$name] = $value; |
|
| 104 | 104 | |
| 105 | 105 | } |
| 106 | 106 | |
| 107 | - $jsonld = \Wordlift_Entity_Post_To_Jsonld_Converter::post_process( $jsonld ); |
|
| 107 | + $jsonld = \Wordlift_Entity_Post_To_Jsonld_Converter::post_process($jsonld); |
|
| 108 | 108 | |
| 109 | - if ( $permalink ) { |
|
| 109 | + if ($permalink) { |
|
| 110 | 110 | $jsonld['mainEntityOfPage'] = $permalink; |
| 111 | 111 | } |
| 112 | 112 | |
@@ -121,21 +121,21 @@ discard block |
||
| 121 | 121 | |
| 122 | 122 | } |
| 123 | 123 | |
| 124 | - private function relative_to_schema_context( $predicate ) { |
|
| 125 | - return str_replace( 'http://schema.org/', '', $predicate ); |
|
| 124 | + private function relative_to_schema_context($predicate) { |
|
| 125 | + return str_replace('http://schema.org/', '', $predicate); |
|
| 126 | 126 | } |
| 127 | 127 | |
| 128 | - private function process_value( $value, &$references ) { |
|
| 128 | + private function process_value($value, &$references) { |
|
| 129 | 129 | |
| 130 | - if ( is_array( $value ) |
|
| 131 | - && count( $value ) > 0 |
|
| 132 | - && $value[0] instanceof \Wordlift_Property_Entity_Reference ) { |
|
| 130 | + if (is_array($value) |
|
| 131 | + && count($value) > 0 |
|
| 132 | + && $value[0] instanceof \Wordlift_Property_Entity_Reference) { |
|
| 133 | 133 | |
| 134 | 134 | // All of the references from the custom fields are post references. |
| 135 | 135 | $references = array_merge( |
| 136 | 136 | $references, |
| 137 | 137 | array_map( |
| 138 | - function ( $property_entity_reference ) { |
|
| 138 | + function($property_entity_reference) { |
|
| 139 | 139 | /** |
| 140 | 140 | * @var $property_entity_reference \Wordlift_Property_Entity_Reference |
| 141 | 141 | */ |
@@ -146,11 +146,11 @@ discard block |
||
| 146 | 146 | ); |
| 147 | 147 | |
| 148 | 148 | return array_map( |
| 149 | - function ( $reference ) { |
|
| 149 | + function($reference) { |
|
| 150 | 150 | /** |
| 151 | 151 | * @var $reference \Wordlift_Property_Entity_Reference |
| 152 | 152 | */ |
| 153 | - return array( '@id' => $reference->get_url() ); |
|
| 153 | + return array('@id' => $reference->get_url()); |
|
| 154 | 154 | }, |
| 155 | 155 | $value |
| 156 | 156 | ); |
@@ -171,7 +171,7 @@ discard block |
||
| 171 | 171 | * |
| 172 | 172 | * @return string The content without shortcodes. |
| 173 | 173 | */ |
| 174 | - private static function strip_all_shortcodes( $content ) { |
|
| 175 | - return preg_replace( '/\[[^]]+\]/', '', $content ); |
|
| 174 | + private static function strip_all_shortcodes($content) { |
|
| 175 | + return preg_replace('/\[[^]]+\]/', '', $content); |
|
| 176 | 176 | } |
| 177 | 177 | } |