@@ -18,225 +18,225 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class Wordlift_Term_JsonLd_Adapter { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 23 | - * |
|
| 24 | - * @since 3.20.0 |
|
| 25 | - * @access private |
|
| 26 | - * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 27 | - */ |
|
| 28 | - private $entity_uri_service; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * The {@link Wordlift_Jsonld_Service} instance. |
|
| 32 | - * |
|
| 33 | - * @since 3.20.0 |
|
| 34 | - * @access private |
|
| 35 | - * @var \Wordlift_Jsonld_Service $jsonld_service The {@link Wordlift_Jsonld_Service} instance. |
|
| 36 | - */ |
|
| 37 | - private $jsonld_service; |
|
| 38 | - |
|
| 39 | - private static $instance; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * Wordlift_Term_JsonLd_Adapter constructor. |
|
| 43 | - * |
|
| 44 | - * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 45 | - * @param \Wordlift_Jsonld_Service $jsonld_service The {@link Wordlift_Jsonld_Service} instance. |
|
| 46 | - * |
|
| 47 | - * @since 3.20.0 |
|
| 48 | - * |
|
| 49 | - */ |
|
| 50 | - public function __construct( $entity_uri_service, $jsonld_service ) { |
|
| 51 | - |
|
| 52 | - add_action( 'wp_head', array( $this, 'wp_head' ) ); |
|
| 53 | - |
|
| 54 | - $this->entity_uri_service = $entity_uri_service; |
|
| 55 | - $this->jsonld_service = $jsonld_service; |
|
| 56 | - |
|
| 57 | - self::$instance = $this; |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - public static function get_instance() { |
|
| 61 | - |
|
| 62 | - return self::$instance; |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Adds carousel json ld data to term page if the conditions match |
|
| 67 | - * |
|
| 68 | - * @return array|boolean |
|
| 69 | - */ |
|
| 70 | - public function get_carousel_jsonld( $id = null ) { |
|
| 71 | - $posts = $this->get_posts( $id ); |
|
| 72 | - $post_jsonld = array(); |
|
| 73 | - if ( ! is_array( $posts ) || count( $posts ) < 2 ) { |
|
| 74 | - // Bail out if no posts are present. |
|
| 75 | - return false; |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - if ( ! is_null( $id ) ) { |
|
| 79 | - $term = get_term( $id ); |
|
| 80 | - $post_jsonld['description'] = strip_tags( strip_shortcodes( $term->description ) ); |
|
| 81 | - $thumbnail_id = get_term_meta( $id, 'thumbnail_id', true ); |
|
| 82 | - if ( ! empty( $thumbnail_id ) ) { |
|
| 83 | - $post_jsonld['image'] = wp_get_attachment_url( $thumbnail_id ); |
|
| 84 | - } |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - // More than 2 items are present, so construct the post_jsonld data |
|
| 88 | - $post_jsonld['@context'] = 'https://schema.org'; |
|
| 89 | - $post_jsonld['@type'] = 'ItemList'; |
|
| 90 | - $post_jsonld['url'] = $this->get_term_url( $id ); |
|
| 91 | - $post_jsonld['itemListElement'] = array(); |
|
| 92 | - $position = 1; |
|
| 93 | - |
|
| 94 | - foreach ( $posts as $post_id ) { |
|
| 95 | - $result = array( |
|
| 96 | - '@type' => 'ListItem', |
|
| 97 | - 'position' => $position, |
|
| 98 | - /** |
|
| 99 | - * We can't use `item` here unless we change the URL for the item to point to the current page. |
|
| 100 | - * |
|
| 101 | - * See https://developers.google.com/search/docs/data-types/carousel |
|
| 102 | - */ |
|
| 103 | - 'url' => apply_filters( 'wl_carousel_post_list_item_url', get_permalink( $post_id ), $post_id ), |
|
| 104 | - ); |
|
| 105 | - array_push( $post_jsonld['itemListElement'], $result ); |
|
| 106 | - $position += 1; |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - return $post_jsonld; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - private function get_posts( $id ) { |
|
| 113 | - global $wp_query; |
|
| 114 | - |
|
| 115 | - if ( ! is_null( $wp_query->posts ) ) { |
|
| 116 | - return array_map( function ( $post ) { |
|
| 117 | - return $post->ID; |
|
| 118 | - }, $wp_query->posts ); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - if ( is_null( $id ) ) { |
|
| 122 | - return null; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - $term = get_term( $id ); |
|
| 126 | - |
|
| 127 | - return get_objects_in_term( $id, $term->taxonomy ); |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * Hook to `wp_head` to print the JSON-LD. |
|
| 132 | - * |
|
| 133 | - * @since 3.20.0 |
|
| 134 | - */ |
|
| 135 | - public function wp_head() { |
|
| 136 | - $query_object = get_queried_object(); |
|
| 137 | - |
|
| 138 | - // Check if it is a term page. |
|
| 139 | - if ( ! $query_object instanceof WP_Term ) { |
|
| 140 | - return; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - // Bail out if `wl_jsonld_enabled` isn't enabled. |
|
| 144 | - if ( ! apply_filters( 'wl_jsonld_enabled', true ) ) { |
|
| 145 | - return; |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - $term_id = $query_object->term_id; |
|
| 149 | - |
|
| 150 | - $jsonld = $this->get( $term_id ); |
|
| 151 | - |
|
| 152 | - // Bail out if the JSON-LD is empty. |
|
| 153 | - if ( empty( $jsonld ) || empty( $jsonld['jsonld'] ) ) { |
|
| 154 | - return; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - $jsonld_string = wp_json_encode( $jsonld['jsonld'] ); |
|
| 158 | - |
|
| 159 | - echo "<script type=\"application/ld+json\" id=\"wl-jsonld-term\">$jsonld_string</script>"; |
|
| 160 | - |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - public function get( $id ) { |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * Support for carousel rich snippet, get jsonld data present |
|
| 167 | - * for all the posts shown in the term page, and add the jsonld data |
|
| 168 | - * to list |
|
| 169 | - * |
|
| 170 | - * see here: https://developers.google.com/search/docs/data-types/carousel |
|
| 171 | - * |
|
| 172 | - * @since 3.26.0 |
|
| 173 | - */ |
|
| 174 | - $carousel_data = $this->get_carousel_jsonld( $id ); |
|
| 175 | - $jsonld_array = array(); |
|
| 176 | - if ( $carousel_data ) { |
|
| 177 | - $jsonld_array[] = $carousel_data; |
|
| 178 | - } |
|
| 179 | - $context = empty( $carousel_data ) ? Jsonld_Context_Enum::PAGE : Jsonld_Context_Enum::CAROUSEL; |
|
| 180 | - $entities_jsonld_array = $this->get_entity_jsonld( $id, $context ); |
|
| 181 | - |
|
| 182 | - $result = array( |
|
| 183 | - 'jsonld' => array_merge( $jsonld_array, $entities_jsonld_array ), |
|
| 184 | - 'references' => array() |
|
| 185 | - ); |
|
| 186 | - |
|
| 187 | - /** |
|
| 188 | - * @since 3.26.3 |
|
| 189 | - * Filter: wl_term_jsonld_array |
|
| 190 | - * @var $id int Term id |
|
| 191 | - * @var $jsonld_array array An array containing jsonld for term and entities. |
|
| 192 | - */ |
|
| 193 | - $arr = apply_filters( 'wl_term_jsonld_array', $result, $id ); |
|
| 194 | - |
|
| 195 | - return $arr['jsonld']; |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - private function get_term_url( $id ) { |
|
| 199 | - |
|
| 200 | - if ( is_null( $id ) ) { |
|
| 201 | - return $_SERVER['REQUEST_URI']; |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - $maybe_url = get_term_meta( $id, Wordlift_Url_Property_Service::META_KEY, true ); |
|
| 205 | - if ( ! empty( $maybe_url ) ) { |
|
| 206 | - return $maybe_url; |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - return get_term_link( $id ); |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - /** |
|
| 213 | - * Return jsonld for entities bound to terms. |
|
| 214 | - * |
|
| 215 | - * @param int $term_id Term ID. |
|
| 216 | - * @param int $context A context for the JSON-LD generation, valid values in Jsonld_Context_Enum. |
|
| 217 | - * |
|
| 218 | - * @return array |
|
| 219 | - */ |
|
| 220 | - private function get_entity_jsonld( $term_id, $context ) { |
|
| 221 | - // The `_wl_entity_id` are URIs. |
|
| 222 | - $entity_ids = get_term_meta( $term_id, '_wl_entity_id' ); |
|
| 223 | - $entity_uri_service = $this->entity_uri_service; |
|
| 224 | - |
|
| 225 | - $local_entity_ids = array_filter( $entity_ids, function ( $uri ) use ( $entity_uri_service ) { |
|
| 226 | - return $entity_uri_service->is_internal( $uri ); |
|
| 227 | - } ); |
|
| 228 | - |
|
| 229 | - // Bail out if there are no entities. |
|
| 230 | - if ( empty( $local_entity_ids ) ) { |
|
| 231 | - return array(); |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - $post = $this->entity_uri_service->get_entity( array_shift( $local_entity_ids ) ); |
|
| 235 | - $jsonld = $this->jsonld_service->get_jsonld( false, $post->ID, $context ); |
|
| 236 | - // Reset the `url` to the term page. |
|
| 237 | - $jsonld[0]['url'] = get_term_link( $term_id ); |
|
| 238 | - |
|
| 239 | - return $jsonld; |
|
| 240 | - } |
|
| 21 | + /** |
|
| 22 | + * The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 23 | + * |
|
| 24 | + * @since 3.20.0 |
|
| 25 | + * @access private |
|
| 26 | + * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 27 | + */ |
|
| 28 | + private $entity_uri_service; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * The {@link Wordlift_Jsonld_Service} instance. |
|
| 32 | + * |
|
| 33 | + * @since 3.20.0 |
|
| 34 | + * @access private |
|
| 35 | + * @var \Wordlift_Jsonld_Service $jsonld_service The {@link Wordlift_Jsonld_Service} instance. |
|
| 36 | + */ |
|
| 37 | + private $jsonld_service; |
|
| 38 | + |
|
| 39 | + private static $instance; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * Wordlift_Term_JsonLd_Adapter constructor. |
|
| 43 | + * |
|
| 44 | + * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 45 | + * @param \Wordlift_Jsonld_Service $jsonld_service The {@link Wordlift_Jsonld_Service} instance. |
|
| 46 | + * |
|
| 47 | + * @since 3.20.0 |
|
| 48 | + * |
|
| 49 | + */ |
|
| 50 | + public function __construct( $entity_uri_service, $jsonld_service ) { |
|
| 51 | + |
|
| 52 | + add_action( 'wp_head', array( $this, 'wp_head' ) ); |
|
| 53 | + |
|
| 54 | + $this->entity_uri_service = $entity_uri_service; |
|
| 55 | + $this->jsonld_service = $jsonld_service; |
|
| 56 | + |
|
| 57 | + self::$instance = $this; |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + public static function get_instance() { |
|
| 61 | + |
|
| 62 | + return self::$instance; |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Adds carousel json ld data to term page if the conditions match |
|
| 67 | + * |
|
| 68 | + * @return array|boolean |
|
| 69 | + */ |
|
| 70 | + public function get_carousel_jsonld( $id = null ) { |
|
| 71 | + $posts = $this->get_posts( $id ); |
|
| 72 | + $post_jsonld = array(); |
|
| 73 | + if ( ! is_array( $posts ) || count( $posts ) < 2 ) { |
|
| 74 | + // Bail out if no posts are present. |
|
| 75 | + return false; |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + if ( ! is_null( $id ) ) { |
|
| 79 | + $term = get_term( $id ); |
|
| 80 | + $post_jsonld['description'] = strip_tags( strip_shortcodes( $term->description ) ); |
|
| 81 | + $thumbnail_id = get_term_meta( $id, 'thumbnail_id', true ); |
|
| 82 | + if ( ! empty( $thumbnail_id ) ) { |
|
| 83 | + $post_jsonld['image'] = wp_get_attachment_url( $thumbnail_id ); |
|
| 84 | + } |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + // More than 2 items are present, so construct the post_jsonld data |
|
| 88 | + $post_jsonld['@context'] = 'https://schema.org'; |
|
| 89 | + $post_jsonld['@type'] = 'ItemList'; |
|
| 90 | + $post_jsonld['url'] = $this->get_term_url( $id ); |
|
| 91 | + $post_jsonld['itemListElement'] = array(); |
|
| 92 | + $position = 1; |
|
| 93 | + |
|
| 94 | + foreach ( $posts as $post_id ) { |
|
| 95 | + $result = array( |
|
| 96 | + '@type' => 'ListItem', |
|
| 97 | + 'position' => $position, |
|
| 98 | + /** |
|
| 99 | + * We can't use `item` here unless we change the URL for the item to point to the current page. |
|
| 100 | + * |
|
| 101 | + * See https://developers.google.com/search/docs/data-types/carousel |
|
| 102 | + */ |
|
| 103 | + 'url' => apply_filters( 'wl_carousel_post_list_item_url', get_permalink( $post_id ), $post_id ), |
|
| 104 | + ); |
|
| 105 | + array_push( $post_jsonld['itemListElement'], $result ); |
|
| 106 | + $position += 1; |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + return $post_jsonld; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + private function get_posts( $id ) { |
|
| 113 | + global $wp_query; |
|
| 114 | + |
|
| 115 | + if ( ! is_null( $wp_query->posts ) ) { |
|
| 116 | + return array_map( function ( $post ) { |
|
| 117 | + return $post->ID; |
|
| 118 | + }, $wp_query->posts ); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + if ( is_null( $id ) ) { |
|
| 122 | + return null; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + $term = get_term( $id ); |
|
| 126 | + |
|
| 127 | + return get_objects_in_term( $id, $term->taxonomy ); |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * Hook to `wp_head` to print the JSON-LD. |
|
| 132 | + * |
|
| 133 | + * @since 3.20.0 |
|
| 134 | + */ |
|
| 135 | + public function wp_head() { |
|
| 136 | + $query_object = get_queried_object(); |
|
| 137 | + |
|
| 138 | + // Check if it is a term page. |
|
| 139 | + if ( ! $query_object instanceof WP_Term ) { |
|
| 140 | + return; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + // Bail out if `wl_jsonld_enabled` isn't enabled. |
|
| 144 | + if ( ! apply_filters( 'wl_jsonld_enabled', true ) ) { |
|
| 145 | + return; |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + $term_id = $query_object->term_id; |
|
| 149 | + |
|
| 150 | + $jsonld = $this->get( $term_id ); |
|
| 151 | + |
|
| 152 | + // Bail out if the JSON-LD is empty. |
|
| 153 | + if ( empty( $jsonld ) || empty( $jsonld['jsonld'] ) ) { |
|
| 154 | + return; |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + $jsonld_string = wp_json_encode( $jsonld['jsonld'] ); |
|
| 158 | + |
|
| 159 | + echo "<script type=\"application/ld+json\" id=\"wl-jsonld-term\">$jsonld_string</script>"; |
|
| 160 | + |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + public function get( $id ) { |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * Support for carousel rich snippet, get jsonld data present |
|
| 167 | + * for all the posts shown in the term page, and add the jsonld data |
|
| 168 | + * to list |
|
| 169 | + * |
|
| 170 | + * see here: https://developers.google.com/search/docs/data-types/carousel |
|
| 171 | + * |
|
| 172 | + * @since 3.26.0 |
|
| 173 | + */ |
|
| 174 | + $carousel_data = $this->get_carousel_jsonld( $id ); |
|
| 175 | + $jsonld_array = array(); |
|
| 176 | + if ( $carousel_data ) { |
|
| 177 | + $jsonld_array[] = $carousel_data; |
|
| 178 | + } |
|
| 179 | + $context = empty( $carousel_data ) ? Jsonld_Context_Enum::PAGE : Jsonld_Context_Enum::CAROUSEL; |
|
| 180 | + $entities_jsonld_array = $this->get_entity_jsonld( $id, $context ); |
|
| 181 | + |
|
| 182 | + $result = array( |
|
| 183 | + 'jsonld' => array_merge( $jsonld_array, $entities_jsonld_array ), |
|
| 184 | + 'references' => array() |
|
| 185 | + ); |
|
| 186 | + |
|
| 187 | + /** |
|
| 188 | + * @since 3.26.3 |
|
| 189 | + * Filter: wl_term_jsonld_array |
|
| 190 | + * @var $id int Term id |
|
| 191 | + * @var $jsonld_array array An array containing jsonld for term and entities. |
|
| 192 | + */ |
|
| 193 | + $arr = apply_filters( 'wl_term_jsonld_array', $result, $id ); |
|
| 194 | + |
|
| 195 | + return $arr['jsonld']; |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + private function get_term_url( $id ) { |
|
| 199 | + |
|
| 200 | + if ( is_null( $id ) ) { |
|
| 201 | + return $_SERVER['REQUEST_URI']; |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + $maybe_url = get_term_meta( $id, Wordlift_Url_Property_Service::META_KEY, true ); |
|
| 205 | + if ( ! empty( $maybe_url ) ) { |
|
| 206 | + return $maybe_url; |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + return get_term_link( $id ); |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + /** |
|
| 213 | + * Return jsonld for entities bound to terms. |
|
| 214 | + * |
|
| 215 | + * @param int $term_id Term ID. |
|
| 216 | + * @param int $context A context for the JSON-LD generation, valid values in Jsonld_Context_Enum. |
|
| 217 | + * |
|
| 218 | + * @return array |
|
| 219 | + */ |
|
| 220 | + private function get_entity_jsonld( $term_id, $context ) { |
|
| 221 | + // The `_wl_entity_id` are URIs. |
|
| 222 | + $entity_ids = get_term_meta( $term_id, '_wl_entity_id' ); |
|
| 223 | + $entity_uri_service = $this->entity_uri_service; |
|
| 224 | + |
|
| 225 | + $local_entity_ids = array_filter( $entity_ids, function ( $uri ) use ( $entity_uri_service ) { |
|
| 226 | + return $entity_uri_service->is_internal( $uri ); |
|
| 227 | + } ); |
|
| 228 | + |
|
| 229 | + // Bail out if there are no entities. |
|
| 230 | + if ( empty( $local_entity_ids ) ) { |
|
| 231 | + return array(); |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + $post = $this->entity_uri_service->get_entity( array_shift( $local_entity_ids ) ); |
|
| 235 | + $jsonld = $this->jsonld_service->get_jsonld( false, $post->ID, $context ); |
|
| 236 | + // Reset the `url` to the term page. |
|
| 237 | + $jsonld[0]['url'] = get_term_link( $term_id ); |
|
| 238 | + |
|
| 239 | + return $jsonld; |
|
| 240 | + } |
|
| 241 | 241 | |
| 242 | 242 | } |
@@ -47,9 +47,9 @@ discard block |
||
| 47 | 47 | * @since 3.20.0 |
| 48 | 48 | * |
| 49 | 49 | */ |
| 50 | - public function __construct( $entity_uri_service, $jsonld_service ) { |
|
| 50 | + public function __construct($entity_uri_service, $jsonld_service) { |
|
| 51 | 51 | |
| 52 | - add_action( 'wp_head', array( $this, 'wp_head' ) ); |
|
| 52 | + add_action('wp_head', array($this, 'wp_head')); |
|
| 53 | 53 | |
| 54 | 54 | $this->entity_uri_service = $entity_uri_service; |
| 55 | 55 | $this->jsonld_service = $jsonld_service; |
@@ -67,31 +67,31 @@ discard block |
||
| 67 | 67 | * |
| 68 | 68 | * @return array|boolean |
| 69 | 69 | */ |
| 70 | - public function get_carousel_jsonld( $id = null ) { |
|
| 71 | - $posts = $this->get_posts( $id ); |
|
| 70 | + public function get_carousel_jsonld($id = null) { |
|
| 71 | + $posts = $this->get_posts($id); |
|
| 72 | 72 | $post_jsonld = array(); |
| 73 | - if ( ! is_array( $posts ) || count( $posts ) < 2 ) { |
|
| 73 | + if ( ! is_array($posts) || count($posts) < 2) { |
|
| 74 | 74 | // Bail out if no posts are present. |
| 75 | 75 | return false; |
| 76 | 76 | } |
| 77 | 77 | |
| 78 | - if ( ! is_null( $id ) ) { |
|
| 79 | - $term = get_term( $id ); |
|
| 80 | - $post_jsonld['description'] = strip_tags( strip_shortcodes( $term->description ) ); |
|
| 81 | - $thumbnail_id = get_term_meta( $id, 'thumbnail_id', true ); |
|
| 82 | - if ( ! empty( $thumbnail_id ) ) { |
|
| 83 | - $post_jsonld['image'] = wp_get_attachment_url( $thumbnail_id ); |
|
| 78 | + if ( ! is_null($id)) { |
|
| 79 | + $term = get_term($id); |
|
| 80 | + $post_jsonld['description'] = strip_tags(strip_shortcodes($term->description)); |
|
| 81 | + $thumbnail_id = get_term_meta($id, 'thumbnail_id', true); |
|
| 82 | + if ( ! empty($thumbnail_id)) { |
|
| 83 | + $post_jsonld['image'] = wp_get_attachment_url($thumbnail_id); |
|
| 84 | 84 | } |
| 85 | 85 | } |
| 86 | 86 | |
| 87 | 87 | // More than 2 items are present, so construct the post_jsonld data |
| 88 | 88 | $post_jsonld['@context'] = 'https://schema.org'; |
| 89 | 89 | $post_jsonld['@type'] = 'ItemList'; |
| 90 | - $post_jsonld['url'] = $this->get_term_url( $id ); |
|
| 90 | + $post_jsonld['url'] = $this->get_term_url($id); |
|
| 91 | 91 | $post_jsonld['itemListElement'] = array(); |
| 92 | 92 | $position = 1; |
| 93 | 93 | |
| 94 | - foreach ( $posts as $post_id ) { |
|
| 94 | + foreach ($posts as $post_id) { |
|
| 95 | 95 | $result = array( |
| 96 | 96 | '@type' => 'ListItem', |
| 97 | 97 | 'position' => $position, |
@@ -100,31 +100,31 @@ discard block |
||
| 100 | 100 | * |
| 101 | 101 | * See https://developers.google.com/search/docs/data-types/carousel |
| 102 | 102 | */ |
| 103 | - 'url' => apply_filters( 'wl_carousel_post_list_item_url', get_permalink( $post_id ), $post_id ), |
|
| 103 | + 'url' => apply_filters('wl_carousel_post_list_item_url', get_permalink($post_id), $post_id), |
|
| 104 | 104 | ); |
| 105 | - array_push( $post_jsonld['itemListElement'], $result ); |
|
| 105 | + array_push($post_jsonld['itemListElement'], $result); |
|
| 106 | 106 | $position += 1; |
| 107 | 107 | } |
| 108 | 108 | |
| 109 | 109 | return $post_jsonld; |
| 110 | 110 | } |
| 111 | 111 | |
| 112 | - private function get_posts( $id ) { |
|
| 112 | + private function get_posts($id) { |
|
| 113 | 113 | global $wp_query; |
| 114 | 114 | |
| 115 | - if ( ! is_null( $wp_query->posts ) ) { |
|
| 116 | - return array_map( function ( $post ) { |
|
| 115 | + if ( ! is_null($wp_query->posts)) { |
|
| 116 | + return array_map(function($post) { |
|
| 117 | 117 | return $post->ID; |
| 118 | - }, $wp_query->posts ); |
|
| 118 | + }, $wp_query->posts); |
|
| 119 | 119 | } |
| 120 | 120 | |
| 121 | - if ( is_null( $id ) ) { |
|
| 121 | + if (is_null($id)) { |
|
| 122 | 122 | return null; |
| 123 | 123 | } |
| 124 | 124 | |
| 125 | - $term = get_term( $id ); |
|
| 125 | + $term = get_term($id); |
|
| 126 | 126 | |
| 127 | - return get_objects_in_term( $id, $term->taxonomy ); |
|
| 127 | + return get_objects_in_term($id, $term->taxonomy); |
|
| 128 | 128 | } |
| 129 | 129 | |
| 130 | 130 | /** |
@@ -136,31 +136,31 @@ discard block |
||
| 136 | 136 | $query_object = get_queried_object(); |
| 137 | 137 | |
| 138 | 138 | // Check if it is a term page. |
| 139 | - if ( ! $query_object instanceof WP_Term ) { |
|
| 139 | + if ( ! $query_object instanceof WP_Term) { |
|
| 140 | 140 | return; |
| 141 | 141 | } |
| 142 | 142 | |
| 143 | 143 | // Bail out if `wl_jsonld_enabled` isn't enabled. |
| 144 | - if ( ! apply_filters( 'wl_jsonld_enabled', true ) ) { |
|
| 144 | + if ( ! apply_filters('wl_jsonld_enabled', true)) { |
|
| 145 | 145 | return; |
| 146 | 146 | } |
| 147 | 147 | |
| 148 | 148 | $term_id = $query_object->term_id; |
| 149 | 149 | |
| 150 | - $jsonld = $this->get( $term_id ); |
|
| 150 | + $jsonld = $this->get($term_id); |
|
| 151 | 151 | |
| 152 | 152 | // Bail out if the JSON-LD is empty. |
| 153 | - if ( empty( $jsonld ) || empty( $jsonld['jsonld'] ) ) { |
|
| 153 | + if (empty($jsonld) || empty($jsonld['jsonld'])) { |
|
| 154 | 154 | return; |
| 155 | 155 | } |
| 156 | 156 | |
| 157 | - $jsonld_string = wp_json_encode( $jsonld['jsonld'] ); |
|
| 157 | + $jsonld_string = wp_json_encode($jsonld['jsonld']); |
|
| 158 | 158 | |
| 159 | 159 | echo "<script type=\"application/ld+json\" id=\"wl-jsonld-term\">$jsonld_string</script>"; |
| 160 | 160 | |
| 161 | 161 | } |
| 162 | 162 | |
| 163 | - public function get( $id ) { |
|
| 163 | + public function get($id) { |
|
| 164 | 164 | |
| 165 | 165 | /** |
| 166 | 166 | * Support for carousel rich snippet, get jsonld data present |
@@ -171,16 +171,16 @@ discard block |
||
| 171 | 171 | * |
| 172 | 172 | * @since 3.26.0 |
| 173 | 173 | */ |
| 174 | - $carousel_data = $this->get_carousel_jsonld( $id ); |
|
| 174 | + $carousel_data = $this->get_carousel_jsonld($id); |
|
| 175 | 175 | $jsonld_array = array(); |
| 176 | - if ( $carousel_data ) { |
|
| 176 | + if ($carousel_data) { |
|
| 177 | 177 | $jsonld_array[] = $carousel_data; |
| 178 | 178 | } |
| 179 | - $context = empty( $carousel_data ) ? Jsonld_Context_Enum::PAGE : Jsonld_Context_Enum::CAROUSEL; |
|
| 180 | - $entities_jsonld_array = $this->get_entity_jsonld( $id, $context ); |
|
| 179 | + $context = empty($carousel_data) ? Jsonld_Context_Enum::PAGE : Jsonld_Context_Enum::CAROUSEL; |
|
| 180 | + $entities_jsonld_array = $this->get_entity_jsonld($id, $context); |
|
| 181 | 181 | |
| 182 | 182 | $result = array( |
| 183 | - 'jsonld' => array_merge( $jsonld_array, $entities_jsonld_array ), |
|
| 183 | + 'jsonld' => array_merge($jsonld_array, $entities_jsonld_array), |
|
| 184 | 184 | 'references' => array() |
| 185 | 185 | ); |
| 186 | 186 | |
@@ -190,23 +190,23 @@ discard block |
||
| 190 | 190 | * @var $id int Term id |
| 191 | 191 | * @var $jsonld_array array An array containing jsonld for term and entities. |
| 192 | 192 | */ |
| 193 | - $arr = apply_filters( 'wl_term_jsonld_array', $result, $id ); |
|
| 193 | + $arr = apply_filters('wl_term_jsonld_array', $result, $id); |
|
| 194 | 194 | |
| 195 | 195 | return $arr['jsonld']; |
| 196 | 196 | } |
| 197 | 197 | |
| 198 | - private function get_term_url( $id ) { |
|
| 198 | + private function get_term_url($id) { |
|
| 199 | 199 | |
| 200 | - if ( is_null( $id ) ) { |
|
| 200 | + if (is_null($id)) { |
|
| 201 | 201 | return $_SERVER['REQUEST_URI']; |
| 202 | 202 | } |
| 203 | 203 | |
| 204 | - $maybe_url = get_term_meta( $id, Wordlift_Url_Property_Service::META_KEY, true ); |
|
| 205 | - if ( ! empty( $maybe_url ) ) { |
|
| 204 | + $maybe_url = get_term_meta($id, Wordlift_Url_Property_Service::META_KEY, true); |
|
| 205 | + if ( ! empty($maybe_url)) { |
|
| 206 | 206 | return $maybe_url; |
| 207 | 207 | } |
| 208 | 208 | |
| 209 | - return get_term_link( $id ); |
|
| 209 | + return get_term_link($id); |
|
| 210 | 210 | } |
| 211 | 211 | |
| 212 | 212 | /** |
@@ -217,24 +217,24 @@ discard block |
||
| 217 | 217 | * |
| 218 | 218 | * @return array |
| 219 | 219 | */ |
| 220 | - private function get_entity_jsonld( $term_id, $context ) { |
|
| 220 | + private function get_entity_jsonld($term_id, $context) { |
|
| 221 | 221 | // The `_wl_entity_id` are URIs. |
| 222 | - $entity_ids = get_term_meta( $term_id, '_wl_entity_id' ); |
|
| 222 | + $entity_ids = get_term_meta($term_id, '_wl_entity_id'); |
|
| 223 | 223 | $entity_uri_service = $this->entity_uri_service; |
| 224 | 224 | |
| 225 | - $local_entity_ids = array_filter( $entity_ids, function ( $uri ) use ( $entity_uri_service ) { |
|
| 226 | - return $entity_uri_service->is_internal( $uri ); |
|
| 225 | + $local_entity_ids = array_filter($entity_ids, function($uri) use ($entity_uri_service) { |
|
| 226 | + return $entity_uri_service->is_internal($uri); |
|
| 227 | 227 | } ); |
| 228 | 228 | |
| 229 | 229 | // Bail out if there are no entities. |
| 230 | - if ( empty( $local_entity_ids ) ) { |
|
| 230 | + if (empty($local_entity_ids)) { |
|
| 231 | 231 | return array(); |
| 232 | 232 | } |
| 233 | 233 | |
| 234 | - $post = $this->entity_uri_service->get_entity( array_shift( $local_entity_ids ) ); |
|
| 235 | - $jsonld = $this->jsonld_service->get_jsonld( false, $post->ID, $context ); |
|
| 234 | + $post = $this->entity_uri_service->get_entity(array_shift($local_entity_ids)); |
|
| 235 | + $jsonld = $this->jsonld_service->get_jsonld(false, $post->ID, $context); |
|
| 236 | 236 | // Reset the `url` to the term page. |
| 237 | - $jsonld[0]['url'] = get_term_link( $term_id ); |
|
| 237 | + $jsonld[0]['url'] = get_term_link($term_id); |
|
| 238 | 238 | |
| 239 | 239 | return $jsonld; |
| 240 | 240 | } |
@@ -21,157 +21,157 @@ discard block |
||
| 21 | 21 | */ |
| 22 | 22 | class Jsonld_Endpoint { |
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * The {@link Wordlift_Jsonld_Service} instance. |
|
| 26 | - * |
|
| 27 | - * @var Wordlift_Jsonld_Service The {@link Wordlift_Jsonld_Service} instance. |
|
| 28 | - */ |
|
| 29 | - private $jsonld_service; |
|
| 30 | - /** |
|
| 31 | - * @var \Wordlift_Entity_Uri_Service |
|
| 32 | - */ |
|
| 33 | - private $entity_uri_service; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * Jsonld_Endpoint constructor. |
|
| 37 | - * |
|
| 38 | - * @param Jsonld_Service $jsonld_service |
|
| 39 | - * @param \Wordlift_Entity_Uri_Service $entity_uri_service |
|
| 40 | - */ |
|
| 41 | - public function __construct( $jsonld_service, $entity_uri_service ) { |
|
| 42 | - |
|
| 43 | - $this->jsonld_service = $jsonld_service; |
|
| 44 | - $this->entity_uri_service = $entity_uri_service; |
|
| 45 | - |
|
| 46 | - // PHP 5.3 compatibility. |
|
| 47 | - $that = $this; |
|
| 48 | - add_action( 'rest_api_init', function () use ( $that ) { |
|
| 49 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/(?P<id>\d+)', array( |
|
| 50 | - 'methods' => WP_REST_Server::READABLE, |
|
| 51 | - 'callback' => array( $that, 'jsonld_using_post_id' ), |
|
| 52 | - 'permission_callback' => '__return_true', |
|
| 53 | - 'args' => array( |
|
| 54 | - 'id' => array( |
|
| 55 | - 'validate_callback' => function ( $param, $request, $key ) { |
|
| 56 | - return is_numeric( $param ); |
|
| 57 | - }, |
|
| 58 | - 'sanitize_callback' => 'absint', |
|
| 59 | - ), |
|
| 60 | - ) |
|
| 61 | - ) ); |
|
| 62 | - |
|
| 63 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/http/(?P<item_id>.*)', array( |
|
| 64 | - 'methods' => 'GET', |
|
| 65 | - 'callback' => array( $that, 'jsonld_using_item_id' ), |
|
| 66 | - 'permission_callback' => '__return_true', |
|
| 67 | - ) ); |
|
| 68 | - |
|
| 69 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/post-meta/(?P<meta_key>[^/]+)', array( |
|
| 70 | - 'methods' => 'GET', |
|
| 71 | - 'callback' => array( $that, 'jsonld_using_post_meta' ), |
|
| 72 | - 'permission_callback' => '__return_true', |
|
| 73 | - ) ); |
|
| 74 | - |
|
| 75 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/meta/(?P<meta_key>[^/]+)', array( |
|
| 76 | - 'methods' => 'GET', |
|
| 77 | - 'callback' => array( $that, 'jsonld_using_meta' ), |
|
| 78 | - 'permission_callback' => '__return_true', |
|
| 79 | - ) ); |
|
| 80 | - |
|
| 81 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/(?P<post_type>.*)/(?P<post_name>.*)', array( |
|
| 82 | - 'methods' => 'GET', |
|
| 83 | - 'callback' => array( $that, 'jsonld_using_get_page_by_path' ), |
|
| 84 | - 'permission_callback' => '__return_true', |
|
| 85 | - ) ); |
|
| 86 | - |
|
| 87 | - } ); |
|
| 88 | - |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * Callback for the JSON-LD request. |
|
| 93 | - * |
|
| 94 | - * @param array $request { |
|
| 95 | - * The request array. |
|
| 96 | - * |
|
| 97 | - * @type int $id The post id. |
|
| 98 | - * } |
|
| 99 | - * |
|
| 100 | - * @return WP_REST_Response |
|
| 101 | - * @throws \Exception |
|
| 102 | - */ |
|
| 103 | - public function jsonld_using_post_id( $request ) { |
|
| 104 | - |
|
| 105 | - $post_id = $request['id']; |
|
| 106 | - $type = ( 0 === $post_id ? Object_Type_Enum::HOMEPAGE : Object_Type_Enum::POST ); |
|
| 107 | - |
|
| 108 | - // Send the generated JSON-LD. |
|
| 109 | - $data = $this->jsonld_service->get( $type, $post_id, Jsonld_Context_Enum::REST ); |
|
| 110 | - |
|
| 111 | - return Jsonld_Response_Helper::to_response( $data ); |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * Provide a JSON-LD given the itemId. |
|
| 116 | - * |
|
| 117 | - * @param array $request { |
|
| 118 | - * The request array. |
|
| 119 | - * |
|
| 120 | - * @type string $item_id The entity item id. |
|
| 121 | - * } |
|
| 122 | - * |
|
| 123 | - * @return WP_REST_Response |
|
| 124 | - * @throws \Exception |
|
| 125 | - */ |
|
| 126 | - public function jsonld_using_item_id( $request ) { |
|
| 127 | - |
|
| 128 | - $item_id = 'http://' . $request['item_id']; |
|
| 129 | - $post = $this->entity_uri_service->get_entity( $item_id ); |
|
| 130 | - |
|
| 131 | - if ( ! is_a( $post, 'WP_Post' ) ) { |
|
| 132 | - return new WP_REST_Response( esc_html( "$item_id not found." ), 404, array( 'Content-Type' => 'text/html' ) ); |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - return $this->jsonld_using_post_id( array( 'id' => $post->ID, ) ); |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - public function jsonld_using_get_page_by_path( $request ) { |
|
| 139 | - |
|
| 140 | - $post_name = $request['post_name']; |
|
| 141 | - $post_type = $request['post_type']; |
|
| 142 | - |
|
| 143 | - global $wpdb; |
|
| 144 | - |
|
| 145 | - $sql = " |
|
| 24 | + /** |
|
| 25 | + * The {@link Wordlift_Jsonld_Service} instance. |
|
| 26 | + * |
|
| 27 | + * @var Wordlift_Jsonld_Service The {@link Wordlift_Jsonld_Service} instance. |
|
| 28 | + */ |
|
| 29 | + private $jsonld_service; |
|
| 30 | + /** |
|
| 31 | + * @var \Wordlift_Entity_Uri_Service |
|
| 32 | + */ |
|
| 33 | + private $entity_uri_service; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * Jsonld_Endpoint constructor. |
|
| 37 | + * |
|
| 38 | + * @param Jsonld_Service $jsonld_service |
|
| 39 | + * @param \Wordlift_Entity_Uri_Service $entity_uri_service |
|
| 40 | + */ |
|
| 41 | + public function __construct( $jsonld_service, $entity_uri_service ) { |
|
| 42 | + |
|
| 43 | + $this->jsonld_service = $jsonld_service; |
|
| 44 | + $this->entity_uri_service = $entity_uri_service; |
|
| 45 | + |
|
| 46 | + // PHP 5.3 compatibility. |
|
| 47 | + $that = $this; |
|
| 48 | + add_action( 'rest_api_init', function () use ( $that ) { |
|
| 49 | + register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/(?P<id>\d+)', array( |
|
| 50 | + 'methods' => WP_REST_Server::READABLE, |
|
| 51 | + 'callback' => array( $that, 'jsonld_using_post_id' ), |
|
| 52 | + 'permission_callback' => '__return_true', |
|
| 53 | + 'args' => array( |
|
| 54 | + 'id' => array( |
|
| 55 | + 'validate_callback' => function ( $param, $request, $key ) { |
|
| 56 | + return is_numeric( $param ); |
|
| 57 | + }, |
|
| 58 | + 'sanitize_callback' => 'absint', |
|
| 59 | + ), |
|
| 60 | + ) |
|
| 61 | + ) ); |
|
| 62 | + |
|
| 63 | + register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/http/(?P<item_id>.*)', array( |
|
| 64 | + 'methods' => 'GET', |
|
| 65 | + 'callback' => array( $that, 'jsonld_using_item_id' ), |
|
| 66 | + 'permission_callback' => '__return_true', |
|
| 67 | + ) ); |
|
| 68 | + |
|
| 69 | + register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/post-meta/(?P<meta_key>[^/]+)', array( |
|
| 70 | + 'methods' => 'GET', |
|
| 71 | + 'callback' => array( $that, 'jsonld_using_post_meta' ), |
|
| 72 | + 'permission_callback' => '__return_true', |
|
| 73 | + ) ); |
|
| 74 | + |
|
| 75 | + register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/meta/(?P<meta_key>[^/]+)', array( |
|
| 76 | + 'methods' => 'GET', |
|
| 77 | + 'callback' => array( $that, 'jsonld_using_meta' ), |
|
| 78 | + 'permission_callback' => '__return_true', |
|
| 79 | + ) ); |
|
| 80 | + |
|
| 81 | + register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/(?P<post_type>.*)/(?P<post_name>.*)', array( |
|
| 82 | + 'methods' => 'GET', |
|
| 83 | + 'callback' => array( $that, 'jsonld_using_get_page_by_path' ), |
|
| 84 | + 'permission_callback' => '__return_true', |
|
| 85 | + ) ); |
|
| 86 | + |
|
| 87 | + } ); |
|
| 88 | + |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * Callback for the JSON-LD request. |
|
| 93 | + * |
|
| 94 | + * @param array $request { |
|
| 95 | + * The request array. |
|
| 96 | + * |
|
| 97 | + * @type int $id The post id. |
|
| 98 | + * } |
|
| 99 | + * |
|
| 100 | + * @return WP_REST_Response |
|
| 101 | + * @throws \Exception |
|
| 102 | + */ |
|
| 103 | + public function jsonld_using_post_id( $request ) { |
|
| 104 | + |
|
| 105 | + $post_id = $request['id']; |
|
| 106 | + $type = ( 0 === $post_id ? Object_Type_Enum::HOMEPAGE : Object_Type_Enum::POST ); |
|
| 107 | + |
|
| 108 | + // Send the generated JSON-LD. |
|
| 109 | + $data = $this->jsonld_service->get( $type, $post_id, Jsonld_Context_Enum::REST ); |
|
| 110 | + |
|
| 111 | + return Jsonld_Response_Helper::to_response( $data ); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * Provide a JSON-LD given the itemId. |
|
| 116 | + * |
|
| 117 | + * @param array $request { |
|
| 118 | + * The request array. |
|
| 119 | + * |
|
| 120 | + * @type string $item_id The entity item id. |
|
| 121 | + * } |
|
| 122 | + * |
|
| 123 | + * @return WP_REST_Response |
|
| 124 | + * @throws \Exception |
|
| 125 | + */ |
|
| 126 | + public function jsonld_using_item_id( $request ) { |
|
| 127 | + |
|
| 128 | + $item_id = 'http://' . $request['item_id']; |
|
| 129 | + $post = $this->entity_uri_service->get_entity( $item_id ); |
|
| 130 | + |
|
| 131 | + if ( ! is_a( $post, 'WP_Post' ) ) { |
|
| 132 | + return new WP_REST_Response( esc_html( "$item_id not found." ), 404, array( 'Content-Type' => 'text/html' ) ); |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + return $this->jsonld_using_post_id( array( 'id' => $post->ID, ) ); |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + public function jsonld_using_get_page_by_path( $request ) { |
|
| 139 | + |
|
| 140 | + $post_name = $request['post_name']; |
|
| 141 | + $post_type = $request['post_type']; |
|
| 142 | + |
|
| 143 | + global $wpdb; |
|
| 144 | + |
|
| 145 | + $sql = " |
|
| 146 | 146 | SELECT ID |
| 147 | 147 | FROM $wpdb->posts |
| 148 | 148 | WHERE post_name = %s |
| 149 | 149 | AND post_type = %s |
| 150 | 150 | "; |
| 151 | 151 | |
| 152 | - $post_id = $wpdb->get_var( $wpdb->prepare( $sql, $post_name, $post_type ) ); |
|
| 152 | + $post_id = $wpdb->get_var( $wpdb->prepare( $sql, $post_name, $post_type ) ); |
|
| 153 | 153 | |
| 154 | - if ( is_null( $post_id ) ) { |
|
| 155 | - return new WP_REST_Response( esc_html( "$post_name of type $post_type not found." ), 404, array( 'Content-Type' => 'text/html' ) ); |
|
| 156 | - } |
|
| 154 | + if ( is_null( $post_id ) ) { |
|
| 155 | + return new WP_REST_Response( esc_html( "$post_name of type $post_type not found." ), 404, array( 'Content-Type' => 'text/html' ) ); |
|
| 156 | + } |
|
| 157 | 157 | |
| 158 | - return $this->jsonld_using_post_id( array( 'id' => $post_id, ) ); |
|
| 159 | - } |
|
| 158 | + return $this->jsonld_using_post_id( array( 'id' => $post_id, ) ); |
|
| 159 | + } |
|
| 160 | 160 | |
| 161 | - /** |
|
| 162 | - * @param WP_REST_Request $request |
|
| 163 | - * |
|
| 164 | - * @return WP_REST_Response |
|
| 165 | - * @throws \Exception |
|
| 166 | - */ |
|
| 167 | - public function jsonld_using_post_meta( $request ) { |
|
| 161 | + /** |
|
| 162 | + * @param WP_REST_Request $request |
|
| 163 | + * |
|
| 164 | + * @return WP_REST_Response |
|
| 165 | + * @throws \Exception |
|
| 166 | + */ |
|
| 167 | + public function jsonld_using_post_meta( $request ) { |
|
| 168 | 168 | |
| 169 | - $meta_key = $request['meta_key']; |
|
| 170 | - $meta_value = urldecode( current( $request->get_query_params( 'meta_value' ) ) ); |
|
| 169 | + $meta_key = $request['meta_key']; |
|
| 170 | + $meta_value = urldecode( current( $request->get_query_params( 'meta_value' ) ) ); |
|
| 171 | 171 | |
| 172 | - global $wpdb; |
|
| 172 | + global $wpdb; |
|
| 173 | 173 | |
| 174 | - $sql = " |
|
| 174 | + $sql = " |
|
| 175 | 175 | SELECT post_id AS ID |
| 176 | 176 | FROM $wpdb->postmeta |
| 177 | 177 | WHERE meta_key = %s |
@@ -179,24 +179,24 @@ discard block |
||
| 179 | 179 | LIMIT 1 |
| 180 | 180 | "; |
| 181 | 181 | |
| 182 | - $post_id = $wpdb->get_var( $wpdb->prepare( $sql, $meta_key, $meta_value ) ); |
|
| 182 | + $post_id = $wpdb->get_var( $wpdb->prepare( $sql, $meta_key, $meta_value ) ); |
|
| 183 | 183 | |
| 184 | - if ( is_null( $post_id ) ) { |
|
| 185 | - return new WP_REST_Response( esc_html( "Post with meta key $meta_key and value $meta_value not found." ), 404, array( 'Content-Type' => 'text/html' ) ); |
|
| 186 | - } |
|
| 184 | + if ( is_null( $post_id ) ) { |
|
| 185 | + return new WP_REST_Response( esc_html( "Post with meta key $meta_key and value $meta_value not found." ), 404, array( 'Content-Type' => 'text/html' ) ); |
|
| 186 | + } |
|
| 187 | 187 | |
| 188 | - return $this->jsonld_using_post_id( array( 'id' => $post_id, ) ); |
|
| 189 | - } |
|
| 188 | + return $this->jsonld_using_post_id( array( 'id' => $post_id, ) ); |
|
| 189 | + } |
|
| 190 | 190 | |
| 191 | - public function jsonld_using_meta( $request ) { |
|
| 191 | + public function jsonld_using_meta( $request ) { |
|
| 192 | 192 | |
| 193 | - global $wpdb; |
|
| 193 | + global $wpdb; |
|
| 194 | 194 | |
| 195 | - $meta_key = $request['meta_key']; |
|
| 196 | - $meta_value = urldecode( current( $request->get_query_params( 'meta_value' ) ) ); |
|
| 195 | + $meta_key = $request['meta_key']; |
|
| 196 | + $meta_value = urldecode( current( $request->get_query_params( 'meta_value' ) ) ); |
|
| 197 | 197 | |
| 198 | - $results = $wpdb->get_results( $wpdb->prepare( |
|
| 199 | - " |
|
| 198 | + $results = $wpdb->get_results( $wpdb->prepare( |
|
| 199 | + " |
|
| 200 | 200 | SELECT pm.post_id AS id, %s AS type |
| 201 | 201 | FROM {$wpdb->postmeta} pm |
| 202 | 202 | INNER JOIN {$wpdb->posts} p |
@@ -207,23 +207,23 @@ discard block |
||
| 207 | 207 | FROM {$wpdb->termmeta} |
| 208 | 208 | WHERE meta_key = %s AND meta_value = %s |
| 209 | 209 | ", |
| 210 | - Object_Type_Enum::POST, |
|
| 211 | - $meta_key, |
|
| 212 | - $meta_value, |
|
| 213 | - Object_Type_Enum::TERM, |
|
| 214 | - $meta_key, |
|
| 215 | - $meta_value |
|
| 216 | - ) ); |
|
| 210 | + Object_Type_Enum::POST, |
|
| 211 | + $meta_key, |
|
| 212 | + $meta_value, |
|
| 213 | + Object_Type_Enum::TERM, |
|
| 214 | + $meta_key, |
|
| 215 | + $meta_value |
|
| 216 | + ) ); |
|
| 217 | 217 | |
| 218 | - $jsonld_service = $this->jsonld_service; |
|
| 218 | + $jsonld_service = $this->jsonld_service; |
|
| 219 | 219 | |
| 220 | - $data = array_reduce( $results, function ( $carry, $result ) use ( $jsonld_service ) { |
|
| 221 | - $jsonld = $jsonld_service->get( $result->type, $result->id, Jsonld_Context_Enum::REST ); |
|
| 220 | + $data = array_reduce( $results, function ( $carry, $result ) use ( $jsonld_service ) { |
|
| 221 | + $jsonld = $jsonld_service->get( $result->type, $result->id, Jsonld_Context_Enum::REST ); |
|
| 222 | 222 | |
| 223 | - return array_merge( $carry, $jsonld ); |
|
| 224 | - }, array() ); |
|
| 223 | + return array_merge( $carry, $jsonld ); |
|
| 224 | + }, array() ); |
|
| 225 | 225 | |
| 226 | - return Jsonld_Response_Helper::to_response( $data ); |
|
| 227 | - } |
|
| 226 | + return Jsonld_Response_Helper::to_response( $data ); |
|
| 227 | + } |
|
| 228 | 228 | |
| 229 | 229 | } |
@@ -38,51 +38,51 @@ discard block |
||
| 38 | 38 | * @param Jsonld_Service $jsonld_service |
| 39 | 39 | * @param \Wordlift_Entity_Uri_Service $entity_uri_service |
| 40 | 40 | */ |
| 41 | - public function __construct( $jsonld_service, $entity_uri_service ) { |
|
| 41 | + public function __construct($jsonld_service, $entity_uri_service) { |
|
| 42 | 42 | |
| 43 | 43 | $this->jsonld_service = $jsonld_service; |
| 44 | 44 | $this->entity_uri_service = $entity_uri_service; |
| 45 | 45 | |
| 46 | 46 | // PHP 5.3 compatibility. |
| 47 | 47 | $that = $this; |
| 48 | - add_action( 'rest_api_init', function () use ( $that ) { |
|
| 49 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/(?P<id>\d+)', array( |
|
| 48 | + add_action('rest_api_init', function() use ($that) { |
|
| 49 | + register_rest_route(WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/(?P<id>\d+)', array( |
|
| 50 | 50 | 'methods' => WP_REST_Server::READABLE, |
| 51 | - 'callback' => array( $that, 'jsonld_using_post_id' ), |
|
| 51 | + 'callback' => array($that, 'jsonld_using_post_id'), |
|
| 52 | 52 | 'permission_callback' => '__return_true', |
| 53 | 53 | 'args' => array( |
| 54 | 54 | 'id' => array( |
| 55 | - 'validate_callback' => function ( $param, $request, $key ) { |
|
| 56 | - return is_numeric( $param ); |
|
| 55 | + 'validate_callback' => function($param, $request, $key) { |
|
| 56 | + return is_numeric($param); |
|
| 57 | 57 | }, |
| 58 | 58 | 'sanitize_callback' => 'absint', |
| 59 | 59 | ), |
| 60 | 60 | ) |
| 61 | - ) ); |
|
| 61 | + )); |
|
| 62 | 62 | |
| 63 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/http/(?P<item_id>.*)', array( |
|
| 63 | + register_rest_route(WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/http/(?P<item_id>.*)', array( |
|
| 64 | 64 | 'methods' => 'GET', |
| 65 | - 'callback' => array( $that, 'jsonld_using_item_id' ), |
|
| 65 | + 'callback' => array($that, 'jsonld_using_item_id'), |
|
| 66 | 66 | 'permission_callback' => '__return_true', |
| 67 | - ) ); |
|
| 67 | + )); |
|
| 68 | 68 | |
| 69 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/post-meta/(?P<meta_key>[^/]+)', array( |
|
| 69 | + register_rest_route(WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/post-meta/(?P<meta_key>[^/]+)', array( |
|
| 70 | 70 | 'methods' => 'GET', |
| 71 | - 'callback' => array( $that, 'jsonld_using_post_meta' ), |
|
| 71 | + 'callback' => array($that, 'jsonld_using_post_meta'), |
|
| 72 | 72 | 'permission_callback' => '__return_true', |
| 73 | - ) ); |
|
| 73 | + )); |
|
| 74 | 74 | |
| 75 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/meta/(?P<meta_key>[^/]+)', array( |
|
| 75 | + register_rest_route(WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/meta/(?P<meta_key>[^/]+)', array( |
|
| 76 | 76 | 'methods' => 'GET', |
| 77 | - 'callback' => array( $that, 'jsonld_using_meta' ), |
|
| 77 | + 'callback' => array($that, 'jsonld_using_meta'), |
|
| 78 | 78 | 'permission_callback' => '__return_true', |
| 79 | - ) ); |
|
| 79 | + )); |
|
| 80 | 80 | |
| 81 | - register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/(?P<post_type>.*)/(?P<post_name>.*)', array( |
|
| 81 | + register_rest_route(WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/(?P<post_type>.*)/(?P<post_name>.*)', array( |
|
| 82 | 82 | 'methods' => 'GET', |
| 83 | - 'callback' => array( $that, 'jsonld_using_get_page_by_path' ), |
|
| 83 | + 'callback' => array($that, 'jsonld_using_get_page_by_path'), |
|
| 84 | 84 | 'permission_callback' => '__return_true', |
| 85 | - ) ); |
|
| 85 | + )); |
|
| 86 | 86 | |
| 87 | 87 | } ); |
| 88 | 88 | |
@@ -100,15 +100,15 @@ discard block |
||
| 100 | 100 | * @return WP_REST_Response |
| 101 | 101 | * @throws \Exception |
| 102 | 102 | */ |
| 103 | - public function jsonld_using_post_id( $request ) { |
|
| 103 | + public function jsonld_using_post_id($request) { |
|
| 104 | 104 | |
| 105 | 105 | $post_id = $request['id']; |
| 106 | - $type = ( 0 === $post_id ? Object_Type_Enum::HOMEPAGE : Object_Type_Enum::POST ); |
|
| 106 | + $type = (0 === $post_id ? Object_Type_Enum::HOMEPAGE : Object_Type_Enum::POST); |
|
| 107 | 107 | |
| 108 | 108 | // Send the generated JSON-LD. |
| 109 | - $data = $this->jsonld_service->get( $type, $post_id, Jsonld_Context_Enum::REST ); |
|
| 109 | + $data = $this->jsonld_service->get($type, $post_id, Jsonld_Context_Enum::REST); |
|
| 110 | 110 | |
| 111 | - return Jsonld_Response_Helper::to_response( $data ); |
|
| 111 | + return Jsonld_Response_Helper::to_response($data); |
|
| 112 | 112 | } |
| 113 | 113 | |
| 114 | 114 | /** |
@@ -123,19 +123,19 @@ discard block |
||
| 123 | 123 | * @return WP_REST_Response |
| 124 | 124 | * @throws \Exception |
| 125 | 125 | */ |
| 126 | - public function jsonld_using_item_id( $request ) { |
|
| 126 | + public function jsonld_using_item_id($request) { |
|
| 127 | 127 | |
| 128 | - $item_id = 'http://' . $request['item_id']; |
|
| 129 | - $post = $this->entity_uri_service->get_entity( $item_id ); |
|
| 128 | + $item_id = 'http://'.$request['item_id']; |
|
| 129 | + $post = $this->entity_uri_service->get_entity($item_id); |
|
| 130 | 130 | |
| 131 | - if ( ! is_a( $post, 'WP_Post' ) ) { |
|
| 132 | - return new WP_REST_Response( esc_html( "$item_id not found." ), 404, array( 'Content-Type' => 'text/html' ) ); |
|
| 131 | + if ( ! is_a($post, 'WP_Post')) { |
|
| 132 | + return new WP_REST_Response(esc_html("$item_id not found."), 404, array('Content-Type' => 'text/html')); |
|
| 133 | 133 | } |
| 134 | 134 | |
| 135 | - return $this->jsonld_using_post_id( array( 'id' => $post->ID, ) ); |
|
| 135 | + return $this->jsonld_using_post_id(array('id' => $post->ID,)); |
|
| 136 | 136 | } |
| 137 | 137 | |
| 138 | - public function jsonld_using_get_page_by_path( $request ) { |
|
| 138 | + public function jsonld_using_get_page_by_path($request) { |
|
| 139 | 139 | |
| 140 | 140 | $post_name = $request['post_name']; |
| 141 | 141 | $post_type = $request['post_type']; |
@@ -149,13 +149,13 @@ discard block |
||
| 149 | 149 | AND post_type = %s |
| 150 | 150 | "; |
| 151 | 151 | |
| 152 | - $post_id = $wpdb->get_var( $wpdb->prepare( $sql, $post_name, $post_type ) ); |
|
| 152 | + $post_id = $wpdb->get_var($wpdb->prepare($sql, $post_name, $post_type)); |
|
| 153 | 153 | |
| 154 | - if ( is_null( $post_id ) ) { |
|
| 155 | - return new WP_REST_Response( esc_html( "$post_name of type $post_type not found." ), 404, array( 'Content-Type' => 'text/html' ) ); |
|
| 154 | + if (is_null($post_id)) { |
|
| 155 | + return new WP_REST_Response(esc_html("$post_name of type $post_type not found."), 404, array('Content-Type' => 'text/html')); |
|
| 156 | 156 | } |
| 157 | 157 | |
| 158 | - return $this->jsonld_using_post_id( array( 'id' => $post_id, ) ); |
|
| 158 | + return $this->jsonld_using_post_id(array('id' => $post_id,)); |
|
| 159 | 159 | } |
| 160 | 160 | |
| 161 | 161 | /** |
@@ -164,10 +164,10 @@ discard block |
||
| 164 | 164 | * @return WP_REST_Response |
| 165 | 165 | * @throws \Exception |
| 166 | 166 | */ |
| 167 | - public function jsonld_using_post_meta( $request ) { |
|
| 167 | + public function jsonld_using_post_meta($request) { |
|
| 168 | 168 | |
| 169 | 169 | $meta_key = $request['meta_key']; |
| 170 | - $meta_value = urldecode( current( $request->get_query_params( 'meta_value' ) ) ); |
|
| 170 | + $meta_value = urldecode(current($request->get_query_params('meta_value'))); |
|
| 171 | 171 | |
| 172 | 172 | global $wpdb; |
| 173 | 173 | |
@@ -179,23 +179,23 @@ discard block |
||
| 179 | 179 | LIMIT 1 |
| 180 | 180 | "; |
| 181 | 181 | |
| 182 | - $post_id = $wpdb->get_var( $wpdb->prepare( $sql, $meta_key, $meta_value ) ); |
|
| 182 | + $post_id = $wpdb->get_var($wpdb->prepare($sql, $meta_key, $meta_value)); |
|
| 183 | 183 | |
| 184 | - if ( is_null( $post_id ) ) { |
|
| 185 | - return new WP_REST_Response( esc_html( "Post with meta key $meta_key and value $meta_value not found." ), 404, array( 'Content-Type' => 'text/html' ) ); |
|
| 184 | + if (is_null($post_id)) { |
|
| 185 | + return new WP_REST_Response(esc_html("Post with meta key $meta_key and value $meta_value not found."), 404, array('Content-Type' => 'text/html')); |
|
| 186 | 186 | } |
| 187 | 187 | |
| 188 | - return $this->jsonld_using_post_id( array( 'id' => $post_id, ) ); |
|
| 188 | + return $this->jsonld_using_post_id(array('id' => $post_id,)); |
|
| 189 | 189 | } |
| 190 | 190 | |
| 191 | - public function jsonld_using_meta( $request ) { |
|
| 191 | + public function jsonld_using_meta($request) { |
|
| 192 | 192 | |
| 193 | 193 | global $wpdb; |
| 194 | 194 | |
| 195 | 195 | $meta_key = $request['meta_key']; |
| 196 | - $meta_value = urldecode( current( $request->get_query_params( 'meta_value' ) ) ); |
|
| 196 | + $meta_value = urldecode(current($request->get_query_params('meta_value'))); |
|
| 197 | 197 | |
| 198 | - $results = $wpdb->get_results( $wpdb->prepare( |
|
| 198 | + $results = $wpdb->get_results($wpdb->prepare( |
|
| 199 | 199 | " |
| 200 | 200 | SELECT pm.post_id AS id, %s AS type |
| 201 | 201 | FROM {$wpdb->postmeta} pm |
@@ -213,17 +213,17 @@ discard block |
||
| 213 | 213 | Object_Type_Enum::TERM, |
| 214 | 214 | $meta_key, |
| 215 | 215 | $meta_value |
| 216 | - ) ); |
|
| 216 | + )); |
|
| 217 | 217 | |
| 218 | 218 | $jsonld_service = $this->jsonld_service; |
| 219 | 219 | |
| 220 | - $data = array_reduce( $results, function ( $carry, $result ) use ( $jsonld_service ) { |
|
| 221 | - $jsonld = $jsonld_service->get( $result->type, $result->id, Jsonld_Context_Enum::REST ); |
|
| 220 | + $data = array_reduce($results, function($carry, $result) use ($jsonld_service) { |
|
| 221 | + $jsonld = $jsonld_service->get($result->type, $result->id, Jsonld_Context_Enum::REST); |
|
| 222 | 222 | |
| 223 | - return array_merge( $carry, $jsonld ); |
|
| 224 | - }, array() ); |
|
| 223 | + return array_merge($carry, $jsonld); |
|
| 224 | + }, array()); |
|
| 225 | 225 | |
| 226 | - return Jsonld_Response_Helper::to_response( $data ); |
|
| 226 | + return Jsonld_Response_Helper::to_response($data); |
|
| 227 | 227 | } |
| 228 | 228 | |
| 229 | 229 | } |
@@ -6,79 +6,79 @@ |
||
| 6 | 6 | |
| 7 | 7 | class Jsonld_Article_Wrapper { |
| 8 | 8 | |
| 9 | - private static $article_types = array( |
|
| 10 | - 'Article', |
|
| 11 | - 'AdvertiserContentArticle', |
|
| 12 | - 'NewsArticle', |
|
| 13 | - 'AnalysisNewsArticle', |
|
| 14 | - 'AskPublicNewsArticle', |
|
| 15 | - 'BackgroundNewsArticle', |
|
| 16 | - 'OpinionNewsArticle', |
|
| 17 | - 'ReportageNewsArticle', |
|
| 18 | - 'ReviewNewsArticle', |
|
| 19 | - 'Report', |
|
| 20 | - 'SatiricalArticle', |
|
| 21 | - 'ScholarlyArticle', |
|
| 22 | - 'MedicalScholarlyArticle', |
|
| 23 | - 'SocialMediaPosting', |
|
| 24 | - 'BlogPosting', |
|
| 25 | - 'LiveBlogPosting', |
|
| 26 | - 'DiscussionForumPosting', |
|
| 27 | - 'TechArticle', |
|
| 28 | - 'APIReference' |
|
| 29 | - ); |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * @var Wordlift_Post_To_Jsonld_Converter |
|
| 33 | - */ |
|
| 34 | - private $post_to_jsonld_converter; |
|
| 35 | - |
|
| 36 | - public function __construct( $post_to_jsonld_converter ) { |
|
| 37 | - |
|
| 38 | - $this->post_to_jsonld_converter = $post_to_jsonld_converter; |
|
| 39 | - |
|
| 40 | - add_filter( 'wl_after_get_jsonld', array( $this, 'after_get_jsonld' ), 10, 3 ); |
|
| 41 | - |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - public function after_get_jsonld( $jsonld, $post_id, $context ) { |
|
| 45 | - |
|
| 46 | - if ( Jsonld_Context_Enum::PAGE !== $context || ! is_array( $jsonld ) || ! isset( $jsonld[0] ) |
|
| 47 | - || ! is_array( $jsonld[0] ) ) { |
|
| 48 | - return $jsonld; |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - // Copy the 1st array element |
|
| 52 | - $post_jsonld = $jsonld[0]; |
|
| 53 | - |
|
| 54 | - // Don't wrap in article if the json-ld is already about an Article (or its descendants). `@type` must be |
|
| 55 | - // in the schema.org context, i.e. `Article`, not `http://schema.org/Article`. |
|
| 56 | - if ( ! isset( $post_jsonld['@id'] ) || ! isset( $post_jsonld['@type'] ) || $this->is_article( $post_jsonld['@type'] ) ) { |
|
| 57 | - return $jsonld; |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - // Convert the post as Article. |
|
| 61 | - $article_jsonld = $this->post_to_jsonld_converter->convert( $post_id ); |
|
| 62 | - $article_jsonld['@id'] = $post_jsonld['@id'] . '/wrapper'; |
|
| 63 | - // Reset the type, since by default the type assigned via the Entity Type taxonomy is used. |
|
| 64 | - $article_jsonld['@type'] = 'Article'; |
|
| 65 | - $article_jsonld['about'] = array( '@id' => $post_jsonld['@id'] ); |
|
| 66 | - |
|
| 67 | - // Copy over the URLs. |
|
| 68 | - if ( isset( $post_jsonld['url'] ) ) { |
|
| 69 | - $article_jsonld['url'] = $post_jsonld['url']; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - array_unshift( $jsonld, $article_jsonld ); |
|
| 73 | - |
|
| 74 | - return $jsonld; |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - private function is_article( $schema_types ) { |
|
| 78 | - |
|
| 79 | - $array_intersect = array_intersect( self::$article_types, ( array ) $schema_types ); |
|
| 80 | - |
|
| 81 | - return ! empty( $array_intersect ); |
|
| 82 | - } |
|
| 9 | + private static $article_types = array( |
|
| 10 | + 'Article', |
|
| 11 | + 'AdvertiserContentArticle', |
|
| 12 | + 'NewsArticle', |
|
| 13 | + 'AnalysisNewsArticle', |
|
| 14 | + 'AskPublicNewsArticle', |
|
| 15 | + 'BackgroundNewsArticle', |
|
| 16 | + 'OpinionNewsArticle', |
|
| 17 | + 'ReportageNewsArticle', |
|
| 18 | + 'ReviewNewsArticle', |
|
| 19 | + 'Report', |
|
| 20 | + 'SatiricalArticle', |
|
| 21 | + 'ScholarlyArticle', |
|
| 22 | + 'MedicalScholarlyArticle', |
|
| 23 | + 'SocialMediaPosting', |
|
| 24 | + 'BlogPosting', |
|
| 25 | + 'LiveBlogPosting', |
|
| 26 | + 'DiscussionForumPosting', |
|
| 27 | + 'TechArticle', |
|
| 28 | + 'APIReference' |
|
| 29 | + ); |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * @var Wordlift_Post_To_Jsonld_Converter |
|
| 33 | + */ |
|
| 34 | + private $post_to_jsonld_converter; |
|
| 35 | + |
|
| 36 | + public function __construct( $post_to_jsonld_converter ) { |
|
| 37 | + |
|
| 38 | + $this->post_to_jsonld_converter = $post_to_jsonld_converter; |
|
| 39 | + |
|
| 40 | + add_filter( 'wl_after_get_jsonld', array( $this, 'after_get_jsonld' ), 10, 3 ); |
|
| 41 | + |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + public function after_get_jsonld( $jsonld, $post_id, $context ) { |
|
| 45 | + |
|
| 46 | + if ( Jsonld_Context_Enum::PAGE !== $context || ! is_array( $jsonld ) || ! isset( $jsonld[0] ) |
|
| 47 | + || ! is_array( $jsonld[0] ) ) { |
|
| 48 | + return $jsonld; |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + // Copy the 1st array element |
|
| 52 | + $post_jsonld = $jsonld[0]; |
|
| 53 | + |
|
| 54 | + // Don't wrap in article if the json-ld is already about an Article (or its descendants). `@type` must be |
|
| 55 | + // in the schema.org context, i.e. `Article`, not `http://schema.org/Article`. |
|
| 56 | + if ( ! isset( $post_jsonld['@id'] ) || ! isset( $post_jsonld['@type'] ) || $this->is_article( $post_jsonld['@type'] ) ) { |
|
| 57 | + return $jsonld; |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + // Convert the post as Article. |
|
| 61 | + $article_jsonld = $this->post_to_jsonld_converter->convert( $post_id ); |
|
| 62 | + $article_jsonld['@id'] = $post_jsonld['@id'] . '/wrapper'; |
|
| 63 | + // Reset the type, since by default the type assigned via the Entity Type taxonomy is used. |
|
| 64 | + $article_jsonld['@type'] = 'Article'; |
|
| 65 | + $article_jsonld['about'] = array( '@id' => $post_jsonld['@id'] ); |
|
| 66 | + |
|
| 67 | + // Copy over the URLs. |
|
| 68 | + if ( isset( $post_jsonld['url'] ) ) { |
|
| 69 | + $article_jsonld['url'] = $post_jsonld['url']; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + array_unshift( $jsonld, $article_jsonld ); |
|
| 73 | + |
|
| 74 | + return $jsonld; |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + private function is_article( $schema_types ) { |
|
| 78 | + |
|
| 79 | + $array_intersect = array_intersect( self::$article_types, ( array ) $schema_types ); |
|
| 80 | + |
|
| 81 | + return ! empty( $array_intersect ); |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | 84 | } |
| 85 | 85 | \ No newline at end of file |
@@ -33,18 +33,18 @@ discard block |
||
| 33 | 33 | */ |
| 34 | 34 | private $post_to_jsonld_converter; |
| 35 | 35 | |
| 36 | - public function __construct( $post_to_jsonld_converter ) { |
|
| 36 | + public function __construct($post_to_jsonld_converter) { |
|
| 37 | 37 | |
| 38 | 38 | $this->post_to_jsonld_converter = $post_to_jsonld_converter; |
| 39 | 39 | |
| 40 | - add_filter( 'wl_after_get_jsonld', array( $this, 'after_get_jsonld' ), 10, 3 ); |
|
| 40 | + add_filter('wl_after_get_jsonld', array($this, 'after_get_jsonld'), 10, 3); |
|
| 41 | 41 | |
| 42 | 42 | } |
| 43 | 43 | |
| 44 | - public function after_get_jsonld( $jsonld, $post_id, $context ) { |
|
| 44 | + public function after_get_jsonld($jsonld, $post_id, $context) { |
|
| 45 | 45 | |
| 46 | - if ( Jsonld_Context_Enum::PAGE !== $context || ! is_array( $jsonld ) || ! isset( $jsonld[0] ) |
|
| 47 | - || ! is_array( $jsonld[0] ) ) { |
|
| 46 | + if (Jsonld_Context_Enum::PAGE !== $context || ! is_array($jsonld) || ! isset($jsonld[0]) |
|
| 47 | + || ! is_array($jsonld[0])) { |
|
| 48 | 48 | return $jsonld; |
| 49 | 49 | } |
| 50 | 50 | |
@@ -53,32 +53,32 @@ discard block |
||
| 53 | 53 | |
| 54 | 54 | // Don't wrap in article if the json-ld is already about an Article (or its descendants). `@type` must be |
| 55 | 55 | // in the schema.org context, i.e. `Article`, not `http://schema.org/Article`. |
| 56 | - if ( ! isset( $post_jsonld['@id'] ) || ! isset( $post_jsonld['@type'] ) || $this->is_article( $post_jsonld['@type'] ) ) { |
|
| 56 | + if ( ! isset($post_jsonld['@id']) || ! isset($post_jsonld['@type']) || $this->is_article($post_jsonld['@type'])) { |
|
| 57 | 57 | return $jsonld; |
| 58 | 58 | } |
| 59 | 59 | |
| 60 | 60 | // Convert the post as Article. |
| 61 | - $article_jsonld = $this->post_to_jsonld_converter->convert( $post_id ); |
|
| 62 | - $article_jsonld['@id'] = $post_jsonld['@id'] . '/wrapper'; |
|
| 61 | + $article_jsonld = $this->post_to_jsonld_converter->convert($post_id); |
|
| 62 | + $article_jsonld['@id'] = $post_jsonld['@id'].'/wrapper'; |
|
| 63 | 63 | // Reset the type, since by default the type assigned via the Entity Type taxonomy is used. |
| 64 | 64 | $article_jsonld['@type'] = 'Article'; |
| 65 | - $article_jsonld['about'] = array( '@id' => $post_jsonld['@id'] ); |
|
| 65 | + $article_jsonld['about'] = array('@id' => $post_jsonld['@id']); |
|
| 66 | 66 | |
| 67 | 67 | // Copy over the URLs. |
| 68 | - if ( isset( $post_jsonld['url'] ) ) { |
|
| 68 | + if (isset($post_jsonld['url'])) { |
|
| 69 | 69 | $article_jsonld['url'] = $post_jsonld['url']; |
| 70 | 70 | } |
| 71 | 71 | |
| 72 | - array_unshift( $jsonld, $article_jsonld ); |
|
| 72 | + array_unshift($jsonld, $article_jsonld); |
|
| 73 | 73 | |
| 74 | 74 | return $jsonld; |
| 75 | 75 | } |
| 76 | 76 | |
| 77 | - private function is_article( $schema_types ) { |
|
| 77 | + private function is_article($schema_types) { |
|
| 78 | 78 | |
| 79 | - $array_intersect = array_intersect( self::$article_types, ( array ) $schema_types ); |
|
| 79 | + $array_intersect = array_intersect(self::$article_types, (array) $schema_types); |
|
| 80 | 80 | |
| 81 | - return ! empty( $array_intersect ); |
|
| 81 | + return ! empty($array_intersect); |
|
| 82 | 82 | } |
| 83 | 83 | |
| 84 | 84 | } |
| 85 | 85 | \ No newline at end of file |
@@ -4,13 +4,13 @@ |
||
| 4 | 4 | |
| 5 | 5 | class Jsonld_Context_Enum { |
| 6 | 6 | |
| 7 | - const UNKNOWN = 0; |
|
| 8 | - /** Web Page output (typically in the head). */ |
|
| 9 | - const PAGE = 1; |
|
| 10 | - /** Carousel. */ |
|
| 11 | - const CAROUSEL = 2; |
|
| 12 | - const FAQ = 3; |
|
| 13 | - /** REST services. */ |
|
| 14 | - const REST = 3; |
|
| 7 | + const UNKNOWN = 0; |
|
| 8 | + /** Web Page output (typically in the head). */ |
|
| 9 | + const PAGE = 1; |
|
| 10 | + /** Carousel. */ |
|
| 11 | + const CAROUSEL = 2; |
|
| 12 | + const FAQ = 3; |
|
| 13 | + /** REST services. */ |
|
| 14 | + const REST = 3; |
|
| 15 | 15 | |
| 16 | 16 | } |
@@ -10,78 +10,78 @@ |
||
| 10 | 10 | |
| 11 | 11 | class Jsonld_Service { |
| 12 | 12 | |
| 13 | - /** |
|
| 14 | - * @var Jsonld_Service |
|
| 15 | - */ |
|
| 16 | - private static $instance; |
|
| 17 | - |
|
| 18 | - /** |
|
| 19 | - * @var Wordlift_Jsonld_Service |
|
| 20 | - */ |
|
| 21 | - private $legacy_jsonld_service; |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * @var Wordlift_Term_JsonLd_Adapter |
|
| 25 | - */ |
|
| 26 | - private $term_jsonld_service; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * @var Jsonld_User_Service |
|
| 30 | - */ |
|
| 31 | - private $jsonld_user_service; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * Jsonld_Service constructor. |
|
| 35 | - * |
|
| 36 | - * @param Wordlift_Jsonld_Service $legacy_jsonld_service |
|
| 37 | - * @param Wordlift_Term_JsonLd_Adapter $term_jsonld_adapter |
|
| 38 | - * @param Jsonld_User_Service $jsonld_user_service |
|
| 39 | - * |
|
| 40 | - * @throws Exception |
|
| 41 | - */ |
|
| 42 | - public function __construct( $legacy_jsonld_service, $term_jsonld_adapter, $jsonld_user_service ) { |
|
| 43 | - |
|
| 44 | - Assertions::assert_of_type( $legacy_jsonld_service, 'Wordlift_Jsonld_Service' ); |
|
| 45 | - Assertions::assert_of_type( $term_jsonld_adapter, 'Wordlift_Term_JsonLd_Adapter' ); |
|
| 46 | - Assertions::assert_of_type( $jsonld_user_service, 'Wordlift\Jsonld\Jsonld_User_Service' ); |
|
| 47 | - |
|
| 48 | - $this->legacy_jsonld_service = $legacy_jsonld_service; |
|
| 49 | - $this->term_jsonld_service = $term_jsonld_adapter; |
|
| 50 | - $this->jsonld_user_service = $jsonld_user_service; |
|
| 51 | - $this->jsonld_user_service->set_jsonld_service( $this ); |
|
| 52 | - |
|
| 53 | - self::$instance = $this; |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - public static function get_instance() { |
|
| 57 | - return self::$instance; |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * Get the JSON-LD structure for the specified type and id. |
|
| 62 | - * |
|
| 63 | - * @param int $type The requested type, one of 'HOMEPAGE', 'POST' or 'TERM'. Default 'POST'. |
|
| 64 | - * @param int|null $id The id. Default `null`. |
|
| 65 | - * @param int $context A context for the JSON-LD generation, valid values in Jsonld_Context_Enum. |
|
| 66 | - * |
|
| 67 | - * @return array The JSON-LD structure. |
|
| 68 | - * @throws Exception Throws an exception if the type isn't recognized. |
|
| 69 | - */ |
|
| 70 | - public function get( $type = Object_Type_Enum::POST, $id = null, $context = Jsonld_Context_Enum::UNKNOWN ) { |
|
| 71 | - |
|
| 72 | - switch ( $type ) { |
|
| 73 | - case Object_Type_Enum::HOMEPAGE: |
|
| 74 | - return $this->legacy_jsonld_service->get_jsonld( true, $id, $context ); |
|
| 75 | - case Object_Type_Enum::POST: |
|
| 76 | - return $this->legacy_jsonld_service->get_jsonld( false, $id, $context ); |
|
| 77 | - case Object_Type_Enum::TERM: |
|
| 78 | - return $this->term_jsonld_service->get( $id ); |
|
| 79 | - case Object_Type_Enum::USER: |
|
| 80 | - return $this->jsonld_user_service->get( $id ); |
|
| 81 | - default: |
|
| 82 | - throw new Exception( "Unknown type $type. Allowed types: 'HOMEPAGE', 'POST', 'TERM', 'USER'." ); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - } |
|
| 13 | + /** |
|
| 14 | + * @var Jsonld_Service |
|
| 15 | + */ |
|
| 16 | + private static $instance; |
|
| 17 | + |
|
| 18 | + /** |
|
| 19 | + * @var Wordlift_Jsonld_Service |
|
| 20 | + */ |
|
| 21 | + private $legacy_jsonld_service; |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * @var Wordlift_Term_JsonLd_Adapter |
|
| 25 | + */ |
|
| 26 | + private $term_jsonld_service; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * @var Jsonld_User_Service |
|
| 30 | + */ |
|
| 31 | + private $jsonld_user_service; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * Jsonld_Service constructor. |
|
| 35 | + * |
|
| 36 | + * @param Wordlift_Jsonld_Service $legacy_jsonld_service |
|
| 37 | + * @param Wordlift_Term_JsonLd_Adapter $term_jsonld_adapter |
|
| 38 | + * @param Jsonld_User_Service $jsonld_user_service |
|
| 39 | + * |
|
| 40 | + * @throws Exception |
|
| 41 | + */ |
|
| 42 | + public function __construct( $legacy_jsonld_service, $term_jsonld_adapter, $jsonld_user_service ) { |
|
| 43 | + |
|
| 44 | + Assertions::assert_of_type( $legacy_jsonld_service, 'Wordlift_Jsonld_Service' ); |
|
| 45 | + Assertions::assert_of_type( $term_jsonld_adapter, 'Wordlift_Term_JsonLd_Adapter' ); |
|
| 46 | + Assertions::assert_of_type( $jsonld_user_service, 'Wordlift\Jsonld\Jsonld_User_Service' ); |
|
| 47 | + |
|
| 48 | + $this->legacy_jsonld_service = $legacy_jsonld_service; |
|
| 49 | + $this->term_jsonld_service = $term_jsonld_adapter; |
|
| 50 | + $this->jsonld_user_service = $jsonld_user_service; |
|
| 51 | + $this->jsonld_user_service->set_jsonld_service( $this ); |
|
| 52 | + |
|
| 53 | + self::$instance = $this; |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + public static function get_instance() { |
|
| 57 | + return self::$instance; |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * Get the JSON-LD structure for the specified type and id. |
|
| 62 | + * |
|
| 63 | + * @param int $type The requested type, one of 'HOMEPAGE', 'POST' or 'TERM'. Default 'POST'. |
|
| 64 | + * @param int|null $id The id. Default `null`. |
|
| 65 | + * @param int $context A context for the JSON-LD generation, valid values in Jsonld_Context_Enum. |
|
| 66 | + * |
|
| 67 | + * @return array The JSON-LD structure. |
|
| 68 | + * @throws Exception Throws an exception if the type isn't recognized. |
|
| 69 | + */ |
|
| 70 | + public function get( $type = Object_Type_Enum::POST, $id = null, $context = Jsonld_Context_Enum::UNKNOWN ) { |
|
| 71 | + |
|
| 72 | + switch ( $type ) { |
|
| 73 | + case Object_Type_Enum::HOMEPAGE: |
|
| 74 | + return $this->legacy_jsonld_service->get_jsonld( true, $id, $context ); |
|
| 75 | + case Object_Type_Enum::POST: |
|
| 76 | + return $this->legacy_jsonld_service->get_jsonld( false, $id, $context ); |
|
| 77 | + case Object_Type_Enum::TERM: |
|
| 78 | + return $this->term_jsonld_service->get( $id ); |
|
| 79 | + case Object_Type_Enum::USER: |
|
| 80 | + return $this->jsonld_user_service->get( $id ); |
|
| 81 | + default: |
|
| 82 | + throw new Exception( "Unknown type $type. Allowed types: 'HOMEPAGE', 'POST', 'TERM', 'USER'." ); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + } |
|
| 86 | 86 | |
| 87 | 87 | } |
@@ -39,16 +39,16 @@ discard block |
||
| 39 | 39 | * |
| 40 | 40 | * @throws Exception |
| 41 | 41 | */ |
| 42 | - public function __construct( $legacy_jsonld_service, $term_jsonld_adapter, $jsonld_user_service ) { |
|
| 42 | + public function __construct($legacy_jsonld_service, $term_jsonld_adapter, $jsonld_user_service) { |
|
| 43 | 43 | |
| 44 | - Assertions::assert_of_type( $legacy_jsonld_service, 'Wordlift_Jsonld_Service' ); |
|
| 45 | - Assertions::assert_of_type( $term_jsonld_adapter, 'Wordlift_Term_JsonLd_Adapter' ); |
|
| 46 | - Assertions::assert_of_type( $jsonld_user_service, 'Wordlift\Jsonld\Jsonld_User_Service' ); |
|
| 44 | + Assertions::assert_of_type($legacy_jsonld_service, 'Wordlift_Jsonld_Service'); |
|
| 45 | + Assertions::assert_of_type($term_jsonld_adapter, 'Wordlift_Term_JsonLd_Adapter'); |
|
| 46 | + Assertions::assert_of_type($jsonld_user_service, 'Wordlift\Jsonld\Jsonld_User_Service'); |
|
| 47 | 47 | |
| 48 | 48 | $this->legacy_jsonld_service = $legacy_jsonld_service; |
| 49 | 49 | $this->term_jsonld_service = $term_jsonld_adapter; |
| 50 | 50 | $this->jsonld_user_service = $jsonld_user_service; |
| 51 | - $this->jsonld_user_service->set_jsonld_service( $this ); |
|
| 51 | + $this->jsonld_user_service->set_jsonld_service($this); |
|
| 52 | 52 | |
| 53 | 53 | self::$instance = $this; |
| 54 | 54 | } |
@@ -67,19 +67,19 @@ discard block |
||
| 67 | 67 | * @return array The JSON-LD structure. |
| 68 | 68 | * @throws Exception Throws an exception if the type isn't recognized. |
| 69 | 69 | */ |
| 70 | - public function get( $type = Object_Type_Enum::POST, $id = null, $context = Jsonld_Context_Enum::UNKNOWN ) { |
|
| 70 | + public function get($type = Object_Type_Enum::POST, $id = null, $context = Jsonld_Context_Enum::UNKNOWN) { |
|
| 71 | 71 | |
| 72 | - switch ( $type ) { |
|
| 72 | + switch ($type) { |
|
| 73 | 73 | case Object_Type_Enum::HOMEPAGE: |
| 74 | - return $this->legacy_jsonld_service->get_jsonld( true, $id, $context ); |
|
| 74 | + return $this->legacy_jsonld_service->get_jsonld(true, $id, $context); |
|
| 75 | 75 | case Object_Type_Enum::POST: |
| 76 | - return $this->legacy_jsonld_service->get_jsonld( false, $id, $context ); |
|
| 76 | + return $this->legacy_jsonld_service->get_jsonld(false, $id, $context); |
|
| 77 | 77 | case Object_Type_Enum::TERM: |
| 78 | - return $this->term_jsonld_service->get( $id ); |
|
| 78 | + return $this->term_jsonld_service->get($id); |
|
| 79 | 79 | case Object_Type_Enum::USER: |
| 80 | - return $this->jsonld_user_service->get( $id ); |
|
| 80 | + return $this->jsonld_user_service->get($id); |
|
| 81 | 81 | default: |
| 82 | - throw new Exception( "Unknown type $type. Allowed types: 'HOMEPAGE', 'POST', 'TERM', 'USER'." ); |
|
| 82 | + throw new Exception("Unknown type $type. Allowed types: 'HOMEPAGE', 'POST', 'TERM', 'USER'."); |
|
| 83 | 83 | } |
| 84 | 84 | |
| 85 | 85 | } |
@@ -51,7 +51,7 @@ discard block |
||
| 51 | 51 | |
| 52 | 52 | // If this file is called directly, abort. |
| 53 | 53 | if ( ! defined( 'WPINC' ) ) { |
| 54 | - die; |
|
| 54 | + die; |
|
| 55 | 55 | } |
| 56 | 56 | |
| 57 | 57 | require_once plugin_dir_path( __FILE__ ) . 'vendor/autoload.php'; |
@@ -74,7 +74,7 @@ discard block |
||
| 74 | 74 | */ |
| 75 | 75 | function wl_write_log( $log ) { |
| 76 | 76 | |
| 77 | - Wordlift_Log_Service::get_instance()->debug( $log ); |
|
| 77 | + Wordlift_Log_Service::get_instance()->debug( $log ); |
|
| 78 | 78 | |
| 79 | 79 | } |
| 80 | 80 | |
@@ -91,7 +91,7 @@ discard block |
||
| 91 | 91 | */ |
| 92 | 92 | function wl_write_log_hide_key( $text ) { |
| 93 | 93 | |
| 94 | - return str_ireplace( wl_configuration_get_key(), '<hidden>', $text ); |
|
| 94 | + return str_ireplace( wl_configuration_get_key(), '<hidden>', $text ); |
|
| 95 | 95 | } |
| 96 | 96 | |
| 97 | 97 | /** |
@@ -99,21 +99,21 @@ discard block |
||
| 99 | 99 | * see http://vip.wordpress.com/documentation/register-additional-html-attributes-for-tinymce-and-wp-kses/ |
| 100 | 100 | */ |
| 101 | 101 | function wordlift_allowed_post_tags() { |
| 102 | - global $allowedposttags; |
|
| 103 | - |
|
| 104 | - $tags = array( 'span' ); |
|
| 105 | - $new_attributes = array( |
|
| 106 | - 'itemscope' => array(), |
|
| 107 | - 'itemtype' => array(), |
|
| 108 | - 'itemprop' => array(), |
|
| 109 | - 'itemid' => array(), |
|
| 110 | - ); |
|
| 111 | - |
|
| 112 | - foreach ( $tags as $tag ) { |
|
| 113 | - if ( isset( $allowedposttags[ $tag ] ) && is_array( $allowedposttags[ $tag ] ) ) { |
|
| 114 | - $allowedposttags[ $tag ] = array_merge( $allowedposttags[ $tag ], $new_attributes ); |
|
| 115 | - } |
|
| 116 | - } |
|
| 102 | + global $allowedposttags; |
|
| 103 | + |
|
| 104 | + $tags = array( 'span' ); |
|
| 105 | + $new_attributes = array( |
|
| 106 | + 'itemscope' => array(), |
|
| 107 | + 'itemtype' => array(), |
|
| 108 | + 'itemprop' => array(), |
|
| 109 | + 'itemid' => array(), |
|
| 110 | + ); |
|
| 111 | + |
|
| 112 | + foreach ( $tags as $tag ) { |
|
| 113 | + if ( isset( $allowedposttags[ $tag ] ) && is_array( $allowedposttags[ $tag ] ) ) { |
|
| 114 | + $allowedposttags[ $tag ] = array_merge( $allowedposttags[ $tag ], $new_attributes ); |
|
| 115 | + } |
|
| 116 | + } |
|
| 117 | 117 | } |
| 118 | 118 | |
| 119 | 119 | // add allowed post tags. |
@@ -124,18 +124,18 @@ discard block |
||
| 124 | 124 | */ |
| 125 | 125 | function wordlift_admin_enqueue_scripts() { |
| 126 | 126 | |
| 127 | - // Added for compatibility with WordPress 3.9 (see http://make.wordpress.org/core/2014/04/16/jquery-ui-and-wpdialogs-in-wordpress-3-9/) |
|
| 128 | - wp_enqueue_script( 'wpdialogs' ); |
|
| 129 | - wp_enqueue_style( 'wp-jquery-ui-dialog' ); |
|
| 127 | + // Added for compatibility with WordPress 3.9 (see http://make.wordpress.org/core/2014/04/16/jquery-ui-and-wpdialogs-in-wordpress-3-9/) |
|
| 128 | + wp_enqueue_script( 'wpdialogs' ); |
|
| 129 | + wp_enqueue_style( 'wp-jquery-ui-dialog' ); |
|
| 130 | 130 | |
| 131 | - wp_enqueue_style( 'wordlift-reloaded', plugin_dir_url( __FILE__ ) . 'css/wordlift-reloaded.min.css' ); |
|
| 131 | + wp_enqueue_style( 'wordlift-reloaded', plugin_dir_url( __FILE__ ) . 'css/wordlift-reloaded.min.css' ); |
|
| 132 | 132 | |
| 133 | - wp_enqueue_script( 'jquery-ui-autocomplete' ); |
|
| 133 | + wp_enqueue_script( 'jquery-ui-autocomplete' ); |
|
| 134 | 134 | |
| 135 | - // Disable auto-save for custom entity posts only |
|
| 136 | - if ( Wordlift_Entity_Service::TYPE_NAME === get_post_type() ) { |
|
| 137 | - wp_dequeue_script( 'autosave' ); |
|
| 138 | - } |
|
| 135 | + // Disable auto-save for custom entity posts only |
|
| 136 | + if ( Wordlift_Entity_Service::TYPE_NAME === get_post_type() ) { |
|
| 137 | + wp_dequeue_script( 'autosave' ); |
|
| 138 | + } |
|
| 139 | 139 | |
| 140 | 140 | } |
| 141 | 141 | |
@@ -151,18 +151,18 @@ discard block |
||
| 151 | 151 | */ |
| 152 | 152 | function wordlift_allowed_html( $allowedtags, $context ) { |
| 153 | 153 | |
| 154 | - if ( 'post' !== $context ) { |
|
| 155 | - return $allowedtags; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - return array_merge_recursive( $allowedtags, array( |
|
| 159 | - 'span' => array( |
|
| 160 | - 'itemscope' => true, |
|
| 161 | - 'itemtype' => true, |
|
| 162 | - 'itemid' => true, |
|
| 163 | - 'itemprop' => true, |
|
| 164 | - ), |
|
| 165 | - ) ); |
|
| 154 | + if ( 'post' !== $context ) { |
|
| 155 | + return $allowedtags; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + return array_merge_recursive( $allowedtags, array( |
|
| 159 | + 'span' => array( |
|
| 160 | + 'itemscope' => true, |
|
| 161 | + 'itemtype' => true, |
|
| 162 | + 'itemid' => true, |
|
| 163 | + 'itemprop' => true, |
|
| 164 | + ), |
|
| 165 | + ) ); |
|
| 166 | 166 | } |
| 167 | 167 | |
| 168 | 168 | add_filter( 'wp_kses_allowed_html', 'wordlift_allowed_html', 10, 2 ); |
@@ -176,16 +176,16 @@ discard block |
||
| 176 | 176 | */ |
| 177 | 177 | function wl_get_coordinates( $post_id ) { |
| 178 | 178 | |
| 179 | - $latitude = wl_schema_get_value( $post_id, 'latitude' ); |
|
| 180 | - $longitude = wl_schema_get_value( $post_id, 'longitude' ); |
|
| 179 | + $latitude = wl_schema_get_value( $post_id, 'latitude' ); |
|
| 180 | + $longitude = wl_schema_get_value( $post_id, 'longitude' ); |
|
| 181 | 181 | |
| 182 | - // DO NOT set latitude/longitude to 0/0 as default values. It's a specific |
|
| 183 | - // place on the globe:"The zero/zero point of this system is located in the |
|
| 184 | - // Gulf of Guinea about 625 km (390 mi) south of Tema, Ghana." |
|
| 185 | - return array( |
|
| 186 | - 'latitude' => isset( $latitude[0] ) && is_numeric( $latitude[0] ) ? $latitude[0] : '', |
|
| 187 | - 'longitude' => isset( $longitude[0] ) && is_numeric( $longitude[0] ) ? $longitude[0] : '', |
|
| 188 | - ); |
|
| 182 | + // DO NOT set latitude/longitude to 0/0 as default values. It's a specific |
|
| 183 | + // place on the globe:"The zero/zero point of this system is located in the |
|
| 184 | + // Gulf of Guinea about 625 km (390 mi) south of Tema, Ghana." |
|
| 185 | + return array( |
|
| 186 | + 'latitude' => isset( $latitude[0] ) && is_numeric( $latitude[0] ) ? $latitude[0] : '', |
|
| 187 | + 'longitude' => isset( $longitude[0] ) && is_numeric( $longitude[0] ) ? $longitude[0] : '', |
|
| 188 | + ); |
|
| 189 | 189 | } |
| 190 | 190 | |
| 191 | 191 | /** |
@@ -199,9 +199,9 @@ discard block |
||
| 199 | 199 | */ |
| 200 | 200 | function wl_get_image_urls( $post_id ) { |
| 201 | 201 | |
| 202 | - return Wordlift_Storage_Factory::get_instance() |
|
| 203 | - ->post_images() |
|
| 204 | - ->get( $post_id ); |
|
| 202 | + return Wordlift_Storage_Factory::get_instance() |
|
| 203 | + ->post_images() |
|
| 204 | + ->get( $post_id ); |
|
| 205 | 205 | |
| 206 | 206 | } |
| 207 | 207 | |
@@ -215,24 +215,24 @@ discard block |
||
| 215 | 215 | */ |
| 216 | 216 | function wl_get_attachment_for_source_url( $parent_post_id, $source_url ) { |
| 217 | 217 | |
| 218 | - // wl_write_log( "wl_get_attachment_for_source_url [ parent post id :: $parent_post_id ][ source url :: $source_url ]" ); |
|
| 218 | + // wl_write_log( "wl_get_attachment_for_source_url [ parent post id :: $parent_post_id ][ source url :: $source_url ]" ); |
|
| 219 | 219 | |
| 220 | - $posts = get_posts( array( |
|
| 221 | - 'post_type' => 'attachment', |
|
| 222 | - 'posts_per_page' => 1, |
|
| 223 | - 'post_status' => 'any', |
|
| 224 | - 'post_parent' => $parent_post_id, |
|
| 225 | - 'meta_key' => 'wl_source_url', |
|
| 226 | - 'meta_value' => $source_url, |
|
| 227 | - ) ); |
|
| 220 | + $posts = get_posts( array( |
|
| 221 | + 'post_type' => 'attachment', |
|
| 222 | + 'posts_per_page' => 1, |
|
| 223 | + 'post_status' => 'any', |
|
| 224 | + 'post_parent' => $parent_post_id, |
|
| 225 | + 'meta_key' => 'wl_source_url', |
|
| 226 | + 'meta_value' => $source_url, |
|
| 227 | + ) ); |
|
| 228 | 228 | |
| 229 | - // Return the found post. |
|
| 230 | - if ( 1 === count( $posts ) ) { |
|
| 231 | - return $posts[0]; |
|
| 232 | - } |
|
| 229 | + // Return the found post. |
|
| 230 | + if ( 1 === count( $posts ) ) { |
|
| 231 | + return $posts[0]; |
|
| 232 | + } |
|
| 233 | 233 | |
| 234 | - // Return null. |
|
| 235 | - return null; |
|
| 234 | + // Return null. |
|
| 235 | + return null; |
|
| 236 | 236 | } |
| 237 | 237 | |
| 238 | 238 | /** |
@@ -243,8 +243,8 @@ discard block |
||
| 243 | 243 | */ |
| 244 | 244 | function wl_set_source_url( $post_id, $source_url ) { |
| 245 | 245 | |
| 246 | - delete_post_meta( $post_id, 'wl_source_url' ); |
|
| 247 | - add_post_meta( $post_id, 'wl_source_url', $source_url ); |
|
| 246 | + delete_post_meta( $post_id, 'wl_source_url' ); |
|
| 247 | + add_post_meta( $post_id, 'wl_source_url', $source_url ); |
|
| 248 | 248 | } |
| 249 | 249 | |
| 250 | 250 | /** |
@@ -261,7 +261,7 @@ discard block |
||
| 261 | 261 | */ |
| 262 | 262 | function wl_sanitize_uri_path( $path, $char = '_' ) { |
| 263 | 263 | |
| 264 | - return Wordlift_Uri_Service::get_instance()->sanitize_path( $path, $char ); |
|
| 264 | + return Wordlift_Uri_Service::get_instance()->sanitize_path( $path, $char ); |
|
| 265 | 265 | } |
| 266 | 266 | |
| 267 | 267 | /** |
@@ -273,47 +273,47 @@ discard block |
||
| 273 | 273 | */ |
| 274 | 274 | function wl_replace_item_id_with_uri( $content ) { |
| 275 | 275 | |
| 276 | - $log = Wordlift_Log_Service::get_logger( 'wl_replace_item_id_with_uri' ); |
|
| 277 | - $log->trace( 'Replacing item IDs with URIs...' ); |
|
| 276 | + $log = Wordlift_Log_Service::get_logger( 'wl_replace_item_id_with_uri' ); |
|
| 277 | + $log->trace( 'Replacing item IDs with URIs...' ); |
|
| 278 | 278 | |
| 279 | - // Strip slashes, see https://core.trac.wordpress.org/ticket/21767 |
|
| 280 | - $content = stripslashes( $content ); |
|
| 279 | + // Strip slashes, see https://core.trac.wordpress.org/ticket/21767 |
|
| 280 | + $content = stripslashes( $content ); |
|
| 281 | 281 | |
| 282 | - // If any match are found. |
|
| 283 | - $matches = array(); |
|
| 284 | - if ( 0 < preg_match_all( '/ itemid="([^"]+)"/i', $content, $matches, PREG_SET_ORDER ) ) { |
|
| 282 | + // If any match are found. |
|
| 283 | + $matches = array(); |
|
| 284 | + if ( 0 < preg_match_all( '/ itemid="([^"]+)"/i', $content, $matches, PREG_SET_ORDER ) ) { |
|
| 285 | 285 | |
| 286 | - foreach ( $matches as $match ) { |
|
| 286 | + foreach ( $matches as $match ) { |
|
| 287 | 287 | |
| 288 | - // Get the item ID. |
|
| 289 | - $item_id = $match[1]; |
|
| 288 | + // Get the item ID. |
|
| 289 | + $item_id = $match[1]; |
|
| 290 | 290 | |
| 291 | - // Get the post bound to that item ID (looking both in the 'official' URI and in the 'same-as' . |
|
| 292 | - $post = Wordlift_Entity_Service::get_instance() |
|
| 293 | - ->get_entity_post_by_uri( $item_id ); |
|
| 291 | + // Get the post bound to that item ID (looking both in the 'official' URI and in the 'same-as' . |
|
| 292 | + $post = Wordlift_Entity_Service::get_instance() |
|
| 293 | + ->get_entity_post_by_uri( $item_id ); |
|
| 294 | 294 | |
| 295 | - // If no entity is found, continue to the next one. |
|
| 296 | - if ( null === $post ) { |
|
| 297 | - continue; |
|
| 298 | - } |
|
| 295 | + // If no entity is found, continue to the next one. |
|
| 296 | + if ( null === $post ) { |
|
| 297 | + continue; |
|
| 298 | + } |
|
| 299 | 299 | |
| 300 | - // Get the URI for that post. |
|
| 301 | - $uri = wl_get_entity_uri( $post->ID ); |
|
| 300 | + // Get the URI for that post. |
|
| 301 | + $uri = wl_get_entity_uri( $post->ID ); |
|
| 302 | 302 | |
| 303 | - // wl_write_log( "wl_replace_item_id_with_uri [ item id :: $item_id ][ uri :: $uri ]" ); |
|
| 303 | + // wl_write_log( "wl_replace_item_id_with_uri [ item id :: $item_id ][ uri :: $uri ]" ); |
|
| 304 | 304 | |
| 305 | - // If the item ID and the URI differ, replace the item ID with the URI saved in WordPress. |
|
| 306 | - if ( ! empty( $uri ) && $item_id !== $uri ) { |
|
| 307 | - $uri_e = esc_html( $uri ); |
|
| 308 | - $content = str_replace( " itemid=\"$item_id\"", " itemid=\"$uri_e\"", $content ); |
|
| 309 | - } |
|
| 310 | - } |
|
| 311 | - } |
|
| 305 | + // If the item ID and the URI differ, replace the item ID with the URI saved in WordPress. |
|
| 306 | + if ( ! empty( $uri ) && $item_id !== $uri ) { |
|
| 307 | + $uri_e = esc_html( $uri ); |
|
| 308 | + $content = str_replace( " itemid=\"$item_id\"", " itemid=\"$uri_e\"", $content ); |
|
| 309 | + } |
|
| 310 | + } |
|
| 311 | + } |
|
| 312 | 312 | |
| 313 | - // Reapply slashes. |
|
| 314 | - $content = addslashes( $content ); |
|
| 313 | + // Reapply slashes. |
|
| 314 | + $content = addslashes( $content ); |
|
| 315 | 315 | |
| 316 | - return $content; |
|
| 316 | + return $content; |
|
| 317 | 317 | } |
| 318 | 318 | |
| 319 | 319 | add_filter( 'content_save_pre', 'wl_replace_item_id_with_uri', 1, 1 ); |
@@ -376,29 +376,29 @@ discard block |
||
| 376 | 376 | */ |
| 377 | 377 | function activate_wordlift() { |
| 378 | 378 | |
| 379 | - $log = Wordlift_Log_Service::get_logger( 'activate_wordlift' ); |
|
| 380 | - |
|
| 381 | - $log->info( 'Activating WordLift...' ); |
|
| 382 | - |
|
| 383 | - require_once plugin_dir_path( __FILE__ ) . 'includes/class-wordlift-activator.php'; |
|
| 384 | - Wordlift_Activator::activate(); |
|
| 385 | - |
|
| 386 | - /** |
|
| 387 | - * Tell the {@link Wordlift_Http_Api} class that we're activating, to let it run activation tasks. |
|
| 388 | - * |
|
| 389 | - * @see https://github.com/insideout10/wordlift-plugin/issues/820 related issue. |
|
| 390 | - * @since 3.19.2 |
|
| 391 | - */ |
|
| 392 | - Wordlift_Http_Api::activate(); |
|
| 393 | - |
|
| 394 | - // Ensure the post type is registered before flushing the rewrite rules. |
|
| 395 | - Wordlift_Entity_Post_Type_Service::get_instance()->register(); |
|
| 396 | - flush_rewrite_rules(); |
|
| 397 | - /** |
|
| 398 | - * @since 3.27.7 |
|
| 399 | - * @see https://github.com/insideout10/wordlift-plugin/issues/1214 |
|
| 400 | - */ |
|
| 401 | - Top_Entities::activate(); |
|
| 379 | + $log = Wordlift_Log_Service::get_logger( 'activate_wordlift' ); |
|
| 380 | + |
|
| 381 | + $log->info( 'Activating WordLift...' ); |
|
| 382 | + |
|
| 383 | + require_once plugin_dir_path( __FILE__ ) . 'includes/class-wordlift-activator.php'; |
|
| 384 | + Wordlift_Activator::activate(); |
|
| 385 | + |
|
| 386 | + /** |
|
| 387 | + * Tell the {@link Wordlift_Http_Api} class that we're activating, to let it run activation tasks. |
|
| 388 | + * |
|
| 389 | + * @see https://github.com/insideout10/wordlift-plugin/issues/820 related issue. |
|
| 390 | + * @since 3.19.2 |
|
| 391 | + */ |
|
| 392 | + Wordlift_Http_Api::activate(); |
|
| 393 | + |
|
| 394 | + // Ensure the post type is registered before flushing the rewrite rules. |
|
| 395 | + Wordlift_Entity_Post_Type_Service::get_instance()->register(); |
|
| 396 | + flush_rewrite_rules(); |
|
| 397 | + /** |
|
| 398 | + * @since 3.27.7 |
|
| 399 | + * @see https://github.com/insideout10/wordlift-plugin/issues/1214 |
|
| 400 | + */ |
|
| 401 | + Top_Entities::activate(); |
|
| 402 | 402 | } |
| 403 | 403 | |
| 404 | 404 | /** |
@@ -407,21 +407,21 @@ discard block |
||
| 407 | 407 | */ |
| 408 | 408 | function deactivate_wordlift() { |
| 409 | 409 | |
| 410 | - require_once plugin_dir_path( __FILE__ ) . 'includes/class-wordlift-deactivator.php'; |
|
| 411 | - Wordlift_Deactivator::deactivate(); |
|
| 412 | - Wordlift_Http_Api::deactivate(); |
|
| 413 | - Ttl_Cache_Cleaner::deactivate(); |
|
| 414 | - /** |
|
| 415 | - * @since 3.27.7 |
|
| 416 | - * @see https://github.com/insideout10/wordlift-plugin/issues/1214 |
|
| 417 | - */ |
|
| 418 | - Top_Entities::deactivate(); |
|
| 419 | - /** |
|
| 420 | - * @since 3.27.8 |
|
| 421 | - * Remove notification flag on deactivation. |
|
| 422 | - */ |
|
| 423 | - Key_Validation_Notice::remove_notification_flag(); |
|
| 424 | - flush_rewrite_rules(); |
|
| 410 | + require_once plugin_dir_path( __FILE__ ) . 'includes/class-wordlift-deactivator.php'; |
|
| 411 | + Wordlift_Deactivator::deactivate(); |
|
| 412 | + Wordlift_Http_Api::deactivate(); |
|
| 413 | + Ttl_Cache_Cleaner::deactivate(); |
|
| 414 | + /** |
|
| 415 | + * @since 3.27.7 |
|
| 416 | + * @see https://github.com/insideout10/wordlift-plugin/issues/1214 |
|
| 417 | + */ |
|
| 418 | + Top_Entities::deactivate(); |
|
| 419 | + /** |
|
| 420 | + * @since 3.27.8 |
|
| 421 | + * Remove notification flag on deactivation. |
|
| 422 | + */ |
|
| 423 | + Key_Validation_Notice::remove_notification_flag(); |
|
| 424 | + flush_rewrite_rules(); |
|
| 425 | 425 | |
| 426 | 426 | } |
| 427 | 427 | |
@@ -444,104 +444,104 @@ discard block |
||
| 444 | 444 | * @since 1.0.0 |
| 445 | 445 | */ |
| 446 | 446 | function run_wordlift() { |
| 447 | - /** |
|
| 448 | - * Filter: wl_feature__enable__widgets. |
|
| 449 | - * |
|
| 450 | - * @param bool whether the widgets needed to be registered, defaults to true. |
|
| 451 | - * |
|
| 452 | - * @return bool |
|
| 453 | - * @since 3.27.6 |
|
| 454 | - */ |
|
| 455 | - if ( apply_filters( 'wl_feature__enable__widgets', true ) ) { |
|
| 456 | - add_action( 'widgets_init', 'wl_register_chord_widget' ); |
|
| 457 | - add_action( 'widgets_init', 'wl_register_geo_widget' ); |
|
| 458 | - add_action( 'widgets_init', 'wl_register_timeline_widget' ); |
|
| 459 | - } |
|
| 460 | - add_filter( 'widget_text', 'do_shortcode' ); |
|
| 461 | - |
|
| 462 | - |
|
| 463 | - /** |
|
| 464 | - * Filter: wl_feature__enable__analysis |
|
| 465 | - * |
|
| 466 | - * @param bool Whether to send api request to analysis or not |
|
| 467 | - * |
|
| 468 | - * @return bool |
|
| 469 | - * @since 3.27.6 |
|
| 470 | - */ |
|
| 471 | - if ( apply_filters( 'wl_feature__enable__analysis', true ) ) { |
|
| 472 | - add_action( 'wp_ajax_wl_analyze', 'wl_ajax_analyze_action' ); |
|
| 473 | - } else { |
|
| 474 | - add_action( 'wp_ajax_wl_analyze', 'wl_ajax_analyze_disabled_action' ); |
|
| 475 | - } |
|
| 476 | - |
|
| 477 | - /* |
|
| 447 | + /** |
|
| 448 | + * Filter: wl_feature__enable__widgets. |
|
| 449 | + * |
|
| 450 | + * @param bool whether the widgets needed to be registered, defaults to true. |
|
| 451 | + * |
|
| 452 | + * @return bool |
|
| 453 | + * @since 3.27.6 |
|
| 454 | + */ |
|
| 455 | + if ( apply_filters( 'wl_feature__enable__widgets', true ) ) { |
|
| 456 | + add_action( 'widgets_init', 'wl_register_chord_widget' ); |
|
| 457 | + add_action( 'widgets_init', 'wl_register_geo_widget' ); |
|
| 458 | + add_action( 'widgets_init', 'wl_register_timeline_widget' ); |
|
| 459 | + } |
|
| 460 | + add_filter( 'widget_text', 'do_shortcode' ); |
|
| 461 | + |
|
| 462 | + |
|
| 463 | + /** |
|
| 464 | + * Filter: wl_feature__enable__analysis |
|
| 465 | + * |
|
| 466 | + * @param bool Whether to send api request to analysis or not |
|
| 467 | + * |
|
| 468 | + * @return bool |
|
| 469 | + * @since 3.27.6 |
|
| 470 | + */ |
|
| 471 | + if ( apply_filters( 'wl_feature__enable__analysis', true ) ) { |
|
| 472 | + add_action( 'wp_ajax_wl_analyze', 'wl_ajax_analyze_action' ); |
|
| 473 | + } else { |
|
| 474 | + add_action( 'wp_ajax_wl_analyze', 'wl_ajax_analyze_disabled_action' ); |
|
| 475 | + } |
|
| 476 | + |
|
| 477 | + /* |
|
| 478 | 478 | * We introduce the WordLift autoloader, since we start using classes in namespaces, i.e. Wordlift\Http. |
| 479 | 479 | * |
| 480 | 480 | * @since 3.21.2 |
| 481 | 481 | */ |
| 482 | - wordlift_plugin_autoload_register(); |
|
| 482 | + wordlift_plugin_autoload_register(); |
|
| 483 | 483 | |
| 484 | - $plugin = new Wordlift(); |
|
| 485 | - $plugin->run(); |
|
| 484 | + $plugin = new Wordlift(); |
|
| 485 | + $plugin->run(); |
|
| 486 | 486 | |
| 487 | - // Initialize the TTL Cache Cleaner. |
|
| 488 | - new Ttl_Cache_Cleaner(); |
|
| 487 | + // Initialize the TTL Cache Cleaner. |
|
| 488 | + new Ttl_Cache_Cleaner(); |
|
| 489 | 489 | |
| 490 | - // Load the new Post Adapter. |
|
| 491 | - new Post_Adapter(); |
|
| 490 | + // Load the new Post Adapter. |
|
| 491 | + new Post_Adapter(); |
|
| 492 | 492 | |
| 493 | - // Load the API Data Hooks. |
|
| 494 | - new Api_Data_Hooks(); |
|
| 493 | + // Load the API Data Hooks. |
|
| 494 | + new Api_Data_Hooks(); |
|
| 495 | 495 | |
| 496 | - add_action( 'plugins_loaded', function () { |
|
| 497 | - // Load early. **PLEASE NOTE** that features are applied only to calls that happen **AFTER** the `plugins_loaded` |
|
| 498 | - // action. |
|
| 499 | - require_once plugin_dir_path( __FILE__ ) . 'wordlift/features/index.php'; |
|
| 500 | - }, 1 ); |
|
| 496 | + add_action( 'plugins_loaded', function () { |
|
| 497 | + // Load early. **PLEASE NOTE** that features are applied only to calls that happen **AFTER** the `plugins_loaded` |
|
| 498 | + // action. |
|
| 499 | + require_once plugin_dir_path( __FILE__ ) . 'wordlift/features/index.php'; |
|
| 500 | + }, 1 ); |
|
| 501 | 501 | |
| 502 | - add_action( 'plugins_loaded', function () use ( $plugin ) { |
|
| 503 | - // Licenses Images. |
|
| 504 | - $user_agent = User_Agent::get_user_agent(); |
|
| 505 | - $wordlift_key = Wordlift_Configuration_Service::get_instance()->get_key(); |
|
| 506 | - $api_service = new Default_Api_Service( apply_filters( 'wl_api_base_url', WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE ), 60, $user_agent, $wordlift_key ); |
|
| 507 | - $image_license_factory = new Image_License_Factory(); |
|
| 508 | - $image_license_service = new Image_License_Service( $api_service, $image_license_factory ); |
|
| 509 | - $image_license_cache = new Ttl_Cache( 'image-license', 86400 * 30 ); // 30 days. |
|
| 510 | - $cached_image_license_service = new Cached_Image_License_Service( $image_license_service, $image_license_cache ); |
|
| 502 | + add_action( 'plugins_loaded', function () use ( $plugin ) { |
|
| 503 | + // Licenses Images. |
|
| 504 | + $user_agent = User_Agent::get_user_agent(); |
|
| 505 | + $wordlift_key = Wordlift_Configuration_Service::get_instance()->get_key(); |
|
| 506 | + $api_service = new Default_Api_Service( apply_filters( 'wl_api_base_url', WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE ), 60, $user_agent, $wordlift_key ); |
|
| 507 | + $image_license_factory = new Image_License_Factory(); |
|
| 508 | + $image_license_service = new Image_License_Service( $api_service, $image_license_factory ); |
|
| 509 | + $image_license_cache = new Ttl_Cache( 'image-license', 86400 * 30 ); // 30 days. |
|
| 510 | + $cached_image_license_service = new Cached_Image_License_Service( $image_license_service, $image_license_cache ); |
|
| 511 | 511 | |
| 512 | - $image_license_scheduler = new Image_License_Scheduler( $image_license_service, $image_license_cache ); |
|
| 513 | - $image_license_cleanup_service = new Image_License_Cleanup_Service(); |
|
| 512 | + $image_license_scheduler = new Image_License_Scheduler( $image_license_service, $image_license_cache ); |
|
| 513 | + $image_license_cleanup_service = new Image_License_Cleanup_Service(); |
|
| 514 | 514 | |
| 515 | - // Get the cached data. If we have cached data, we load the notifier. |
|
| 516 | - $image_license_data = $image_license_cache->get( Cached_Image_License_Service::GET_NON_PUBLIC_DOMAIN_IMAGES ); |
|
| 517 | - if ( null !== $image_license_data ) { |
|
| 518 | - $image_license_page = new Image_License_Page( $image_license_data, Wordlift::get_instance()->get_version() ); |
|
| 519 | - new Image_License_Notifier( $image_license_data, $image_license_page ); |
|
| 520 | - } |
|
| 515 | + // Get the cached data. If we have cached data, we load the notifier. |
|
| 516 | + $image_license_data = $image_license_cache->get( Cached_Image_License_Service::GET_NON_PUBLIC_DOMAIN_IMAGES ); |
|
| 517 | + if ( null !== $image_license_data ) { |
|
| 518 | + $image_license_page = new Image_License_Page( $image_license_data, Wordlift::get_instance()->get_version() ); |
|
| 519 | + new Image_License_Notifier( $image_license_data, $image_license_page ); |
|
| 520 | + } |
|
| 521 | 521 | |
| 522 | - $remove_all_images_task = new Remove_All_Images_Task( $cached_image_license_service ); |
|
| 523 | - $remove_all_images_task_adapter = new Task_Ajax_Adapter( $remove_all_images_task ); |
|
| 522 | + $remove_all_images_task = new Remove_All_Images_Task( $cached_image_license_service ); |
|
| 523 | + $remove_all_images_task_adapter = new Task_Ajax_Adapter( $remove_all_images_task ); |
|
| 524 | 524 | |
| 525 | - $reload_data_task = new Reload_Data_Task(); |
|
| 526 | - $reload_data_task_adapter = new Task_Ajax_Adapter( $reload_data_task ); |
|
| 525 | + $reload_data_task = new Reload_Data_Task(); |
|
| 526 | + $reload_data_task_adapter = new Task_Ajax_Adapter( $reload_data_task ); |
|
| 527 | 527 | |
| 528 | - $add_license_caption_or_remove_task = new Add_License_Caption_Or_Remove_Task( $cached_image_license_service ); |
|
| 529 | - $add_license_caption_or_remove_task_adapter = new Task_Ajax_Adapter( $add_license_caption_or_remove_task ); |
|
| 528 | + $add_license_caption_or_remove_task = new Add_License_Caption_Or_Remove_Task( $cached_image_license_service ); |
|
| 529 | + $add_license_caption_or_remove_task_adapter = new Task_Ajax_Adapter( $add_license_caption_or_remove_task ); |
|
| 530 | 530 | |
| 531 | - $remove_all_images_task_page = new Remove_All_Images_Page( new Task_Ajax_Adapters_Registry( $remove_all_images_task_adapter ), $plugin->get_version() ); |
|
| 532 | - $reload_data_task_page = new Reload_Data_Page( new Task_Ajax_Adapters_Registry( $reload_data_task_adapter ), $plugin->get_version() ); |
|
| 533 | - $add_license_caption_or_remove_task_page = new Add_License_Caption_Or_Remove_Page( new Task_Ajax_Adapters_Registry( $add_license_caption_or_remove_task_adapter ), $plugin->get_version() ); |
|
| 531 | + $remove_all_images_task_page = new Remove_All_Images_Page( new Task_Ajax_Adapters_Registry( $remove_all_images_task_adapter ), $plugin->get_version() ); |
|
| 532 | + $reload_data_task_page = new Reload_Data_Page( new Task_Ajax_Adapters_Registry( $reload_data_task_adapter ), $plugin->get_version() ); |
|
| 533 | + $add_license_caption_or_remove_task_page = new Add_License_Caption_Or_Remove_Page( new Task_Ajax_Adapters_Registry( $add_license_caption_or_remove_task_adapter ), $plugin->get_version() ); |
|
| 534 | 534 | |
| 535 | - new Wordlift_Products_Navigator_Shortcode_REST(); |
|
| 535 | + new Wordlift_Products_Navigator_Shortcode_REST(); |
|
| 536 | 536 | |
| 537 | - if ( apply_filters( 'wl_feature__enable__article-wrapper', false ) ) { |
|
| 538 | - new Jsonld_Article_Wrapper( Wordlift_Post_To_Jsonld_Converter::get_instance() ); |
|
| 539 | - } |
|
| 537 | + if ( apply_filters( 'wl_feature__enable__article-wrapper', false ) ) { |
|
| 538 | + new Jsonld_Article_Wrapper( Wordlift_Post_To_Jsonld_Converter::get_instance() ); |
|
| 539 | + } |
|
| 540 | 540 | |
| 541 | - // Register the Dataset module, requires `$api_service`. |
|
| 542 | - require_once plugin_dir_path( __FILE__ ) . 'wordlift/dataset/index.php'; |
|
| 541 | + // Register the Dataset module, requires `$api_service`. |
|
| 542 | + require_once plugin_dir_path( __FILE__ ) . 'wordlift/dataset/index.php'; |
|
| 543 | 543 | |
| 544 | - } ); |
|
| 544 | + } ); |
|
| 545 | 545 | |
| 546 | 546 | } |
| 547 | 547 | |
@@ -555,45 +555,45 @@ discard block |
||
| 555 | 555 | */ |
| 556 | 556 | function wordlift_plugin_autoload_register() { |
| 557 | 557 | |
| 558 | - spl_autoload_register( function ( $class_name ) { |
|
| 558 | + spl_autoload_register( function ( $class_name ) { |
|
| 559 | 559 | |
| 560 | - // Bail out if these are not our classes. |
|
| 561 | - if ( 0 !== strpos( $class_name, 'Wordlift\\' ) ) { |
|
| 562 | - return false; |
|
| 563 | - } |
|
| 560 | + // Bail out if these are not our classes. |
|
| 561 | + if ( 0 !== strpos( $class_name, 'Wordlift\\' ) ) { |
|
| 562 | + return false; |
|
| 563 | + } |
|
| 564 | 564 | |
| 565 | - $class_name_lc = strtolower( str_replace( '_', '-', $class_name ) ); |
|
| 565 | + $class_name_lc = strtolower( str_replace( '_', '-', $class_name ) ); |
|
| 566 | 566 | |
| 567 | - preg_match( '|^(?:(.*)\\\\)?(.+?)$|', $class_name_lc, $matches ); |
|
| 567 | + preg_match( '|^(?:(.*)\\\\)?(.+?)$|', $class_name_lc, $matches ); |
|
| 568 | 568 | |
| 569 | - $path = str_replace( '\\', DIRECTORY_SEPARATOR, $matches[1] ); |
|
| 570 | - $file = 'class-' . $matches[2] . '.php'; |
|
| 569 | + $path = str_replace( '\\', DIRECTORY_SEPARATOR, $matches[1] ); |
|
| 570 | + $file = 'class-' . $matches[2] . '.php'; |
|
| 571 | 571 | |
| 572 | - $full_path = plugin_dir_path( __FILE__ ) . $path . DIRECTORY_SEPARATOR . $file; |
|
| 572 | + $full_path = plugin_dir_path( __FILE__ ) . $path . DIRECTORY_SEPARATOR . $file; |
|
| 573 | 573 | |
| 574 | - if ( ! file_exists( $full_path ) ) { |
|
| 575 | - echo( "Class $class_name not found at $full_path." ); |
|
| 574 | + if ( ! file_exists( $full_path ) ) { |
|
| 575 | + echo( "Class $class_name not found at $full_path." ); |
|
| 576 | 576 | |
| 577 | - return false; |
|
| 578 | - } |
|
| 577 | + return false; |
|
| 578 | + } |
|
| 579 | 579 | |
| 580 | - require_once $full_path; |
|
| 580 | + require_once $full_path; |
|
| 581 | 581 | |
| 582 | - return true; |
|
| 583 | - } ); |
|
| 582 | + return true; |
|
| 583 | + } ); |
|
| 584 | 584 | |
| 585 | 585 | } |
| 586 | 586 | |
| 587 | 587 | function wl_block_categories( $categories, $post ) { |
| 588 | - return array_merge( |
|
| 589 | - $categories, |
|
| 590 | - array( |
|
| 591 | - array( |
|
| 592 | - 'slug' => 'wordlift', |
|
| 593 | - 'title' => __( 'WordLift', 'wordlift' ), |
|
| 594 | - ), |
|
| 595 | - ) |
|
| 596 | - ); |
|
| 588 | + return array_merge( |
|
| 589 | + $categories, |
|
| 590 | + array( |
|
| 591 | + array( |
|
| 592 | + 'slug' => 'wordlift', |
|
| 593 | + 'title' => __( 'WordLift', 'wordlift' ), |
|
| 594 | + ), |
|
| 595 | + ) |
|
| 596 | + ); |
|
| 597 | 597 | } |
| 598 | 598 | |
| 599 | 599 | add_filter( 'block_categories', 'wl_block_categories', 10, 2 ); |
@@ -50,17 +50,17 @@ discard block |
||
| 50 | 50 | use Wordlift\Tasks\Task_Ajax_Adapters_Registry; |
| 51 | 51 | |
| 52 | 52 | // If this file is called directly, abort. |
| 53 | -if ( ! defined( 'WPINC' ) ) { |
|
| 53 | +if ( ! defined('WPINC')) { |
|
| 54 | 54 | die; |
| 55 | 55 | } |
| 56 | 56 | |
| 57 | -require_once plugin_dir_path( __FILE__ ) . 'vendor/autoload.php'; |
|
| 57 | +require_once plugin_dir_path(__FILE__).'vendor/autoload.php'; |
|
| 58 | 58 | |
| 59 | 59 | // Include WordLift constants. |
| 60 | -require_once( 'wordlift_constants.php' ); |
|
| 60 | +require_once('wordlift_constants.php'); |
|
| 61 | 61 | |
| 62 | 62 | // Load modules. |
| 63 | -require_once( 'modules/core/wordlift_core.php' ); |
|
| 63 | +require_once('modules/core/wordlift_core.php'); |
|
| 64 | 64 | |
| 65 | 65 | /** |
| 66 | 66 | * Log to the debug.log file. |
@@ -72,9 +72,9 @@ discard block |
||
| 72 | 72 | * @deprecated use Wordlift_Log_Service::get_instance()->info( $log ); |
| 73 | 73 | * |
| 74 | 74 | */ |
| 75 | -function wl_write_log( $log ) { |
|
| 75 | +function wl_write_log($log) { |
|
| 76 | 76 | |
| 77 | - Wordlift_Log_Service::get_instance()->debug( $log ); |
|
| 77 | + Wordlift_Log_Service::get_instance()->debug($log); |
|
| 78 | 78 | |
| 79 | 79 | } |
| 80 | 80 | |
@@ -89,9 +89,9 @@ discard block |
||
| 89 | 89 | * @since 3.0.0 |
| 90 | 90 | * |
| 91 | 91 | */ |
| 92 | -function wl_write_log_hide_key( $text ) { |
|
| 92 | +function wl_write_log_hide_key($text) { |
|
| 93 | 93 | |
| 94 | - return str_ireplace( wl_configuration_get_key(), '<hidden>', $text ); |
|
| 94 | + return str_ireplace(wl_configuration_get_key(), '<hidden>', $text); |
|
| 95 | 95 | } |
| 96 | 96 | |
| 97 | 97 | /** |
@@ -101,7 +101,7 @@ discard block |
||
| 101 | 101 | function wordlift_allowed_post_tags() { |
| 102 | 102 | global $allowedposttags; |
| 103 | 103 | |
| 104 | - $tags = array( 'span' ); |
|
| 104 | + $tags = array('span'); |
|
| 105 | 105 | $new_attributes = array( |
| 106 | 106 | 'itemscope' => array(), |
| 107 | 107 | 'itemtype' => array(), |
@@ -109,15 +109,15 @@ discard block |
||
| 109 | 109 | 'itemid' => array(), |
| 110 | 110 | ); |
| 111 | 111 | |
| 112 | - foreach ( $tags as $tag ) { |
|
| 113 | - if ( isset( $allowedposttags[ $tag ] ) && is_array( $allowedposttags[ $tag ] ) ) { |
|
| 114 | - $allowedposttags[ $tag ] = array_merge( $allowedposttags[ $tag ], $new_attributes ); |
|
| 112 | + foreach ($tags as $tag) { |
|
| 113 | + if (isset($allowedposttags[$tag]) && is_array($allowedposttags[$tag])) { |
|
| 114 | + $allowedposttags[$tag] = array_merge($allowedposttags[$tag], $new_attributes); |
|
| 115 | 115 | } |
| 116 | 116 | } |
| 117 | 117 | } |
| 118 | 118 | |
| 119 | 119 | // add allowed post tags. |
| 120 | -add_action( 'init', 'wordlift_allowed_post_tags' ); |
|
| 120 | +add_action('init', 'wordlift_allowed_post_tags'); |
|
| 121 | 121 | |
| 122 | 122 | /** |
| 123 | 123 | * Register additional scripts for the admin UI. |
@@ -125,21 +125,21 @@ discard block |
||
| 125 | 125 | function wordlift_admin_enqueue_scripts() { |
| 126 | 126 | |
| 127 | 127 | // Added for compatibility with WordPress 3.9 (see http://make.wordpress.org/core/2014/04/16/jquery-ui-and-wpdialogs-in-wordpress-3-9/) |
| 128 | - wp_enqueue_script( 'wpdialogs' ); |
|
| 129 | - wp_enqueue_style( 'wp-jquery-ui-dialog' ); |
|
| 128 | + wp_enqueue_script('wpdialogs'); |
|
| 129 | + wp_enqueue_style('wp-jquery-ui-dialog'); |
|
| 130 | 130 | |
| 131 | - wp_enqueue_style( 'wordlift-reloaded', plugin_dir_url( __FILE__ ) . 'css/wordlift-reloaded.min.css' ); |
|
| 131 | + wp_enqueue_style('wordlift-reloaded', plugin_dir_url(__FILE__).'css/wordlift-reloaded.min.css'); |
|
| 132 | 132 | |
| 133 | - wp_enqueue_script( 'jquery-ui-autocomplete' ); |
|
| 133 | + wp_enqueue_script('jquery-ui-autocomplete'); |
|
| 134 | 134 | |
| 135 | 135 | // Disable auto-save for custom entity posts only |
| 136 | - if ( Wordlift_Entity_Service::TYPE_NAME === get_post_type() ) { |
|
| 137 | - wp_dequeue_script( 'autosave' ); |
|
| 136 | + if (Wordlift_Entity_Service::TYPE_NAME === get_post_type()) { |
|
| 137 | + wp_dequeue_script('autosave'); |
|
| 138 | 138 | } |
| 139 | 139 | |
| 140 | 140 | } |
| 141 | 141 | |
| 142 | -add_action( 'admin_enqueue_scripts', 'wordlift_admin_enqueue_scripts' ); |
|
| 142 | +add_action('admin_enqueue_scripts', 'wordlift_admin_enqueue_scripts'); |
|
| 143 | 143 | |
| 144 | 144 | /** |
| 145 | 145 | * Hooked to *wp_kses_allowed_html* filter, adds microdata attributes. |
@@ -149,23 +149,23 @@ discard block |
||
| 149 | 149 | * |
| 150 | 150 | * @return array An array which contains allowed microdata attributes. |
| 151 | 151 | */ |
| 152 | -function wordlift_allowed_html( $allowedtags, $context ) { |
|
| 152 | +function wordlift_allowed_html($allowedtags, $context) { |
|
| 153 | 153 | |
| 154 | - if ( 'post' !== $context ) { |
|
| 154 | + if ('post' !== $context) { |
|
| 155 | 155 | return $allowedtags; |
| 156 | 156 | } |
| 157 | 157 | |
| 158 | - return array_merge_recursive( $allowedtags, array( |
|
| 158 | + return array_merge_recursive($allowedtags, array( |
|
| 159 | 159 | 'span' => array( |
| 160 | 160 | 'itemscope' => true, |
| 161 | 161 | 'itemtype' => true, |
| 162 | 162 | 'itemid' => true, |
| 163 | 163 | 'itemprop' => true, |
| 164 | 164 | ), |
| 165 | - ) ); |
|
| 165 | + )); |
|
| 166 | 166 | } |
| 167 | 167 | |
| 168 | -add_filter( 'wp_kses_allowed_html', 'wordlift_allowed_html', 10, 2 ); |
|
| 168 | +add_filter('wp_kses_allowed_html', 'wordlift_allowed_html', 10, 2); |
|
| 169 | 169 | |
| 170 | 170 | /** |
| 171 | 171 | * Get the coordinates for the specified post ID. |
@@ -174,17 +174,17 @@ discard block |
||
| 174 | 174 | * |
| 175 | 175 | * @return array|null An array of coordinates or null. |
| 176 | 176 | */ |
| 177 | -function wl_get_coordinates( $post_id ) { |
|
| 177 | +function wl_get_coordinates($post_id) { |
|
| 178 | 178 | |
| 179 | - $latitude = wl_schema_get_value( $post_id, 'latitude' ); |
|
| 180 | - $longitude = wl_schema_get_value( $post_id, 'longitude' ); |
|
| 179 | + $latitude = wl_schema_get_value($post_id, 'latitude'); |
|
| 180 | + $longitude = wl_schema_get_value($post_id, 'longitude'); |
|
| 181 | 181 | |
| 182 | 182 | // DO NOT set latitude/longitude to 0/0 as default values. It's a specific |
| 183 | 183 | // place on the globe:"The zero/zero point of this system is located in the |
| 184 | 184 | // Gulf of Guinea about 625 km (390 mi) south of Tema, Ghana." |
| 185 | 185 | return array( |
| 186 | - 'latitude' => isset( $latitude[0] ) && is_numeric( $latitude[0] ) ? $latitude[0] : '', |
|
| 187 | - 'longitude' => isset( $longitude[0] ) && is_numeric( $longitude[0] ) ? $longitude[0] : '', |
|
| 186 | + 'latitude' => isset($latitude[0]) && is_numeric($latitude[0]) ? $latitude[0] : '', |
|
| 187 | + 'longitude' => isset($longitude[0]) && is_numeric($longitude[0]) ? $longitude[0] : '', |
|
| 188 | 188 | ); |
| 189 | 189 | } |
| 190 | 190 | |
@@ -197,11 +197,11 @@ discard block |
||
| 197 | 197 | * @deprecated use Wordlift_Storage_Factory::get_instance()->post_images()->get( $post_id ) |
| 198 | 198 | * |
| 199 | 199 | */ |
| 200 | -function wl_get_image_urls( $post_id ) { |
|
| 200 | +function wl_get_image_urls($post_id) { |
|
| 201 | 201 | |
| 202 | 202 | return Wordlift_Storage_Factory::get_instance() |
| 203 | 203 | ->post_images() |
| 204 | - ->get( $post_id ); |
|
| 204 | + ->get($post_id); |
|
| 205 | 205 | |
| 206 | 206 | } |
| 207 | 207 | |
@@ -213,21 +213,21 @@ discard block |
||
| 213 | 213 | * |
| 214 | 214 | * @return WP_Post|null A post instance or null if not found. |
| 215 | 215 | */ |
| 216 | -function wl_get_attachment_for_source_url( $parent_post_id, $source_url ) { |
|
| 216 | +function wl_get_attachment_for_source_url($parent_post_id, $source_url) { |
|
| 217 | 217 | |
| 218 | 218 | // wl_write_log( "wl_get_attachment_for_source_url [ parent post id :: $parent_post_id ][ source url :: $source_url ]" ); |
| 219 | 219 | |
| 220 | - $posts = get_posts( array( |
|
| 220 | + $posts = get_posts(array( |
|
| 221 | 221 | 'post_type' => 'attachment', |
| 222 | 222 | 'posts_per_page' => 1, |
| 223 | 223 | 'post_status' => 'any', |
| 224 | 224 | 'post_parent' => $parent_post_id, |
| 225 | 225 | 'meta_key' => 'wl_source_url', |
| 226 | 226 | 'meta_value' => $source_url, |
| 227 | - ) ); |
|
| 227 | + )); |
|
| 228 | 228 | |
| 229 | 229 | // Return the found post. |
| 230 | - if ( 1 === count( $posts ) ) { |
|
| 230 | + if (1 === count($posts)) { |
|
| 231 | 231 | return $posts[0]; |
| 232 | 232 | } |
| 233 | 233 | |
@@ -241,10 +241,10 @@ discard block |
||
| 241 | 241 | * @param int $post_id The post ID. |
| 242 | 242 | * @param string $source_url The source URL. |
| 243 | 243 | */ |
| 244 | -function wl_set_source_url( $post_id, $source_url ) { |
|
| 244 | +function wl_set_source_url($post_id, $source_url) { |
|
| 245 | 245 | |
| 246 | - delete_post_meta( $post_id, 'wl_source_url' ); |
|
| 247 | - add_post_meta( $post_id, 'wl_source_url', $source_url ); |
|
| 246 | + delete_post_meta($post_id, 'wl_source_url'); |
|
| 247 | + add_post_meta($post_id, 'wl_source_url', $source_url); |
|
| 248 | 248 | } |
| 249 | 249 | |
| 250 | 250 | /** |
@@ -259,9 +259,9 @@ discard block |
||
| 259 | 259 | * @see https://codex.wordpress.org/Function_Reference/sanitize_title |
| 260 | 260 | * |
| 261 | 261 | */ |
| 262 | -function wl_sanitize_uri_path( $path, $char = '_' ) { |
|
| 262 | +function wl_sanitize_uri_path($path, $char = '_') { |
|
| 263 | 263 | |
| 264 | - return Wordlift_Uri_Service::get_instance()->sanitize_path( $path, $char ); |
|
| 264 | + return Wordlift_Uri_Service::get_instance()->sanitize_path($path, $char); |
|
| 265 | 265 | } |
| 266 | 266 | |
| 267 | 267 | /** |
@@ -271,104 +271,104 @@ discard block |
||
| 271 | 271 | * |
| 272 | 272 | * @return string The updated post content. |
| 273 | 273 | */ |
| 274 | -function wl_replace_item_id_with_uri( $content ) { |
|
| 274 | +function wl_replace_item_id_with_uri($content) { |
|
| 275 | 275 | |
| 276 | - $log = Wordlift_Log_Service::get_logger( 'wl_replace_item_id_with_uri' ); |
|
| 277 | - $log->trace( 'Replacing item IDs with URIs...' ); |
|
| 276 | + $log = Wordlift_Log_Service::get_logger('wl_replace_item_id_with_uri'); |
|
| 277 | + $log->trace('Replacing item IDs with URIs...'); |
|
| 278 | 278 | |
| 279 | 279 | // Strip slashes, see https://core.trac.wordpress.org/ticket/21767 |
| 280 | - $content = stripslashes( $content ); |
|
| 280 | + $content = stripslashes($content); |
|
| 281 | 281 | |
| 282 | 282 | // If any match are found. |
| 283 | 283 | $matches = array(); |
| 284 | - if ( 0 < preg_match_all( '/ itemid="([^"]+)"/i', $content, $matches, PREG_SET_ORDER ) ) { |
|
| 284 | + if (0 < preg_match_all('/ itemid="([^"]+)"/i', $content, $matches, PREG_SET_ORDER)) { |
|
| 285 | 285 | |
| 286 | - foreach ( $matches as $match ) { |
|
| 286 | + foreach ($matches as $match) { |
|
| 287 | 287 | |
| 288 | 288 | // Get the item ID. |
| 289 | 289 | $item_id = $match[1]; |
| 290 | 290 | |
| 291 | 291 | // Get the post bound to that item ID (looking both in the 'official' URI and in the 'same-as' . |
| 292 | 292 | $post = Wordlift_Entity_Service::get_instance() |
| 293 | - ->get_entity_post_by_uri( $item_id ); |
|
| 293 | + ->get_entity_post_by_uri($item_id); |
|
| 294 | 294 | |
| 295 | 295 | // If no entity is found, continue to the next one. |
| 296 | - if ( null === $post ) { |
|
| 296 | + if (null === $post) { |
|
| 297 | 297 | continue; |
| 298 | 298 | } |
| 299 | 299 | |
| 300 | 300 | // Get the URI for that post. |
| 301 | - $uri = wl_get_entity_uri( $post->ID ); |
|
| 301 | + $uri = wl_get_entity_uri($post->ID); |
|
| 302 | 302 | |
| 303 | 303 | // wl_write_log( "wl_replace_item_id_with_uri [ item id :: $item_id ][ uri :: $uri ]" ); |
| 304 | 304 | |
| 305 | 305 | // If the item ID and the URI differ, replace the item ID with the URI saved in WordPress. |
| 306 | - if ( ! empty( $uri ) && $item_id !== $uri ) { |
|
| 307 | - $uri_e = esc_html( $uri ); |
|
| 308 | - $content = str_replace( " itemid=\"$item_id\"", " itemid=\"$uri_e\"", $content ); |
|
| 306 | + if ( ! empty($uri) && $item_id !== $uri) { |
|
| 307 | + $uri_e = esc_html($uri); |
|
| 308 | + $content = str_replace(" itemid=\"$item_id\"", " itemid=\"$uri_e\"", $content); |
|
| 309 | 309 | } |
| 310 | 310 | } |
| 311 | 311 | } |
| 312 | 312 | |
| 313 | 313 | // Reapply slashes. |
| 314 | - $content = addslashes( $content ); |
|
| 314 | + $content = addslashes($content); |
|
| 315 | 315 | |
| 316 | 316 | return $content; |
| 317 | 317 | } |
| 318 | 318 | |
| 319 | -add_filter( 'content_save_pre', 'wl_replace_item_id_with_uri', 1, 1 ); |
|
| 319 | +add_filter('content_save_pre', 'wl_replace_item_id_with_uri', 1, 1); |
|
| 320 | 320 | |
| 321 | -require_once( 'wordlift_entity_functions.php' ); |
|
| 321 | +require_once('wordlift_entity_functions.php'); |
|
| 322 | 322 | |
| 323 | 323 | // add editor related methods. |
| 324 | -require_once( 'wordlift_editor.php' ); |
|
| 324 | +require_once('wordlift_editor.php'); |
|
| 325 | 325 | |
| 326 | 326 | // add the WordLift entity custom type. |
| 327 | -require_once( 'wordlift_entity_type.php' ); |
|
| 327 | +require_once('wordlift_entity_type.php'); |
|
| 328 | 328 | |
| 329 | 329 | // add callbacks on post save to notify data changes from wp to redlink triple store |
| 330 | -require_once( 'wordlift_to_redlink_data_push_callbacks.php' ); |
|
| 330 | +require_once('wordlift_to_redlink_data_push_callbacks.php'); |
|
| 331 | 331 | |
| 332 | -require_once( 'modules/configuration/wordlift_configuration_settings.php' ); |
|
| 332 | +require_once('modules/configuration/wordlift_configuration_settings.php'); |
|
| 333 | 333 | |
| 334 | 334 | // Load modules |
| 335 | -require_once( 'modules/analyzer/wordlift_analyzer.php' ); |
|
| 336 | -require_once( 'modules/linked_data/wordlift_linked_data.php' ); |
|
| 337 | -require_once( 'modules/prefixes/wordlift_prefixes.php' ); |
|
| 335 | +require_once('modules/analyzer/wordlift_analyzer.php'); |
|
| 336 | +require_once('modules/linked_data/wordlift_linked_data.php'); |
|
| 337 | +require_once('modules/prefixes/wordlift_prefixes.php'); |
|
| 338 | 338 | |
| 339 | 339 | // Shortcodes |
| 340 | 340 | |
| 341 | -require_once( 'modules/geo_widget/wordlift_geo_widget.php' ); |
|
| 342 | -require_once( 'shortcodes/class-wordlift-shortcode-rest.php' ); |
|
| 343 | -require_once( 'shortcodes/wordlift_shortcode_chord.php' ); |
|
| 344 | -require_once( 'shortcodes/wordlift_shortcode_geomap.php' ); |
|
| 345 | -require_once( 'shortcodes/wordlift_shortcode_field.php' ); |
|
| 346 | -require_once( 'shortcodes/wordlift_shortcode_faceted_search.php' ); |
|
| 347 | -require_once( 'shortcodes/wordlift_shortcode_navigator.php' ); |
|
| 348 | -require_once( 'shortcodes/class-wordlift-products-navigator-shortcode-rest.php' ); |
|
| 341 | +require_once('modules/geo_widget/wordlift_geo_widget.php'); |
|
| 342 | +require_once('shortcodes/class-wordlift-shortcode-rest.php'); |
|
| 343 | +require_once('shortcodes/wordlift_shortcode_chord.php'); |
|
| 344 | +require_once('shortcodes/wordlift_shortcode_geomap.php'); |
|
| 345 | +require_once('shortcodes/wordlift_shortcode_field.php'); |
|
| 346 | +require_once('shortcodes/wordlift_shortcode_faceted_search.php'); |
|
| 347 | +require_once('shortcodes/wordlift_shortcode_navigator.php'); |
|
| 348 | +require_once('shortcodes/class-wordlift-products-navigator-shortcode-rest.php'); |
|
| 349 | 349 | |
| 350 | -require_once( 'widgets/wordlift_widget_geo.php' ); |
|
| 351 | -require_once( 'widgets/class-wordlift-chord-widget.php' ); |
|
| 352 | -require_once( 'widgets/wordlift_widget_timeline.php' ); |
|
| 350 | +require_once('widgets/wordlift_widget_geo.php'); |
|
| 351 | +require_once('widgets/class-wordlift-chord-widget.php'); |
|
| 352 | +require_once('widgets/wordlift_widget_timeline.php'); |
|
| 353 | 353 | |
| 354 | -require_once( 'wordlift_redlink.php' ); |
|
| 354 | +require_once('wordlift_redlink.php'); |
|
| 355 | 355 | |
| 356 | 356 | // Add admin functions. |
| 357 | 357 | // TODO: find a way to make 'admin' UI tests work. |
| 358 | 358 | //if ( is_admin() ) { |
| 359 | 359 | |
| 360 | -require_once( 'admin/wordlift_admin.php' ); |
|
| 361 | -require_once( 'admin/wordlift_admin_edit_post.php' ); |
|
| 362 | -require_once( 'admin/wordlift_admin_save_post.php' ); |
|
| 360 | +require_once('admin/wordlift_admin.php'); |
|
| 361 | +require_once('admin/wordlift_admin_edit_post.php'); |
|
| 362 | +require_once('admin/wordlift_admin_save_post.php'); |
|
| 363 | 363 | |
| 364 | 364 | // add the entities meta box. |
| 365 | -require_once( 'admin/wordlift_admin_meta_box_entities.php' ); |
|
| 365 | +require_once('admin/wordlift_admin_meta_box_entities.php'); |
|
| 366 | 366 | |
| 367 | 367 | // add the entity creation AJAX. |
| 368 | -require_once( 'admin/wordlift_admin_ajax_related_posts.php' ); |
|
| 368 | +require_once('admin/wordlift_admin_ajax_related_posts.php'); |
|
| 369 | 369 | |
| 370 | 370 | // Load the wl_chord TinyMCE button and configuration dialog. |
| 371 | -require_once( 'admin/wordlift_admin_shortcodes.php' ); |
|
| 371 | +require_once('admin/wordlift_admin_shortcodes.php'); |
|
| 372 | 372 | |
| 373 | 373 | /** |
| 374 | 374 | * The code that runs during plugin activation. |
@@ -376,11 +376,11 @@ discard block |
||
| 376 | 376 | */ |
| 377 | 377 | function activate_wordlift() { |
| 378 | 378 | |
| 379 | - $log = Wordlift_Log_Service::get_logger( 'activate_wordlift' ); |
|
| 379 | + $log = Wordlift_Log_Service::get_logger('activate_wordlift'); |
|
| 380 | 380 | |
| 381 | - $log->info( 'Activating WordLift...' ); |
|
| 381 | + $log->info('Activating WordLift...'); |
|
| 382 | 382 | |
| 383 | - require_once plugin_dir_path( __FILE__ ) . 'includes/class-wordlift-activator.php'; |
|
| 383 | + require_once plugin_dir_path(__FILE__).'includes/class-wordlift-activator.php'; |
|
| 384 | 384 | Wordlift_Activator::activate(); |
| 385 | 385 | |
| 386 | 386 | /** |
@@ -407,7 +407,7 @@ discard block |
||
| 407 | 407 | */ |
| 408 | 408 | function deactivate_wordlift() { |
| 409 | 409 | |
| 410 | - require_once plugin_dir_path( __FILE__ ) . 'includes/class-wordlift-deactivator.php'; |
|
| 410 | + require_once plugin_dir_path(__FILE__).'includes/class-wordlift-deactivator.php'; |
|
| 411 | 411 | Wordlift_Deactivator::deactivate(); |
| 412 | 412 | Wordlift_Http_Api::deactivate(); |
| 413 | 413 | Ttl_Cache_Cleaner::deactivate(); |
@@ -425,14 +425,14 @@ discard block |
||
| 425 | 425 | |
| 426 | 426 | } |
| 427 | 427 | |
| 428 | -register_activation_hook( __FILE__, 'activate_wordlift' ); |
|
| 429 | -register_deactivation_hook( __FILE__, 'deactivate_wordlift' ); |
|
| 428 | +register_activation_hook(__FILE__, 'activate_wordlift'); |
|
| 429 | +register_deactivation_hook(__FILE__, 'deactivate_wordlift'); |
|
| 430 | 430 | |
| 431 | 431 | /** |
| 432 | 432 | * The core plugin class that is used to define internationalization, |
| 433 | 433 | * admin-specific hooks, and public-facing site hooks. |
| 434 | 434 | */ |
| 435 | -require plugin_dir_path( __FILE__ ) . 'includes/class-wordlift.php'; |
|
| 435 | +require plugin_dir_path(__FILE__).'includes/class-wordlift.php'; |
|
| 436 | 436 | |
| 437 | 437 | /** |
| 438 | 438 | * Begins execution of the plugin. |
@@ -452,12 +452,12 @@ discard block |
||
| 452 | 452 | * @return bool |
| 453 | 453 | * @since 3.27.6 |
| 454 | 454 | */ |
| 455 | - if ( apply_filters( 'wl_feature__enable__widgets', true ) ) { |
|
| 456 | - add_action( 'widgets_init', 'wl_register_chord_widget' ); |
|
| 457 | - add_action( 'widgets_init', 'wl_register_geo_widget' ); |
|
| 458 | - add_action( 'widgets_init', 'wl_register_timeline_widget' ); |
|
| 455 | + if (apply_filters('wl_feature__enable__widgets', true)) { |
|
| 456 | + add_action('widgets_init', 'wl_register_chord_widget'); |
|
| 457 | + add_action('widgets_init', 'wl_register_geo_widget'); |
|
| 458 | + add_action('widgets_init', 'wl_register_timeline_widget'); |
|
| 459 | 459 | } |
| 460 | - add_filter( 'widget_text', 'do_shortcode' ); |
|
| 460 | + add_filter('widget_text', 'do_shortcode'); |
|
| 461 | 461 | |
| 462 | 462 | |
| 463 | 463 | /** |
@@ -468,10 +468,10 @@ discard block |
||
| 468 | 468 | * @return bool |
| 469 | 469 | * @since 3.27.6 |
| 470 | 470 | */ |
| 471 | - if ( apply_filters( 'wl_feature__enable__analysis', true ) ) { |
|
| 472 | - add_action( 'wp_ajax_wl_analyze', 'wl_ajax_analyze_action' ); |
|
| 471 | + if (apply_filters('wl_feature__enable__analysis', true)) { |
|
| 472 | + add_action('wp_ajax_wl_analyze', 'wl_ajax_analyze_action'); |
|
| 473 | 473 | } else { |
| 474 | - add_action( 'wp_ajax_wl_analyze', 'wl_ajax_analyze_disabled_action' ); |
|
| 474 | + add_action('wp_ajax_wl_analyze', 'wl_ajax_analyze_disabled_action'); |
|
| 475 | 475 | } |
| 476 | 476 | |
| 477 | 477 | /* |
@@ -493,53 +493,53 @@ discard block |
||
| 493 | 493 | // Load the API Data Hooks. |
| 494 | 494 | new Api_Data_Hooks(); |
| 495 | 495 | |
| 496 | - add_action( 'plugins_loaded', function () { |
|
| 496 | + add_action('plugins_loaded', function() { |
|
| 497 | 497 | // Load early. **PLEASE NOTE** that features are applied only to calls that happen **AFTER** the `plugins_loaded` |
| 498 | 498 | // action. |
| 499 | - require_once plugin_dir_path( __FILE__ ) . 'wordlift/features/index.php'; |
|
| 500 | - }, 1 ); |
|
| 499 | + require_once plugin_dir_path(__FILE__).'wordlift/features/index.php'; |
|
| 500 | + }, 1); |
|
| 501 | 501 | |
| 502 | - add_action( 'plugins_loaded', function () use ( $plugin ) { |
|
| 502 | + add_action('plugins_loaded', function() use ($plugin) { |
|
| 503 | 503 | // Licenses Images. |
| 504 | 504 | $user_agent = User_Agent::get_user_agent(); |
| 505 | 505 | $wordlift_key = Wordlift_Configuration_Service::get_instance()->get_key(); |
| 506 | - $api_service = new Default_Api_Service( apply_filters( 'wl_api_base_url', WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE ), 60, $user_agent, $wordlift_key ); |
|
| 506 | + $api_service = new Default_Api_Service(apply_filters('wl_api_base_url', WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE), 60, $user_agent, $wordlift_key); |
|
| 507 | 507 | $image_license_factory = new Image_License_Factory(); |
| 508 | - $image_license_service = new Image_License_Service( $api_service, $image_license_factory ); |
|
| 509 | - $image_license_cache = new Ttl_Cache( 'image-license', 86400 * 30 ); // 30 days. |
|
| 510 | - $cached_image_license_service = new Cached_Image_License_Service( $image_license_service, $image_license_cache ); |
|
| 508 | + $image_license_service = new Image_License_Service($api_service, $image_license_factory); |
|
| 509 | + $image_license_cache = new Ttl_Cache('image-license', 86400 * 30); // 30 days. |
|
| 510 | + $cached_image_license_service = new Cached_Image_License_Service($image_license_service, $image_license_cache); |
|
| 511 | 511 | |
| 512 | - $image_license_scheduler = new Image_License_Scheduler( $image_license_service, $image_license_cache ); |
|
| 512 | + $image_license_scheduler = new Image_License_Scheduler($image_license_service, $image_license_cache); |
|
| 513 | 513 | $image_license_cleanup_service = new Image_License_Cleanup_Service(); |
| 514 | 514 | |
| 515 | 515 | // Get the cached data. If we have cached data, we load the notifier. |
| 516 | - $image_license_data = $image_license_cache->get( Cached_Image_License_Service::GET_NON_PUBLIC_DOMAIN_IMAGES ); |
|
| 517 | - if ( null !== $image_license_data ) { |
|
| 518 | - $image_license_page = new Image_License_Page( $image_license_data, Wordlift::get_instance()->get_version() ); |
|
| 519 | - new Image_License_Notifier( $image_license_data, $image_license_page ); |
|
| 516 | + $image_license_data = $image_license_cache->get(Cached_Image_License_Service::GET_NON_PUBLIC_DOMAIN_IMAGES); |
|
| 517 | + if (null !== $image_license_data) { |
|
| 518 | + $image_license_page = new Image_License_Page($image_license_data, Wordlift::get_instance()->get_version()); |
|
| 519 | + new Image_License_Notifier($image_license_data, $image_license_page); |
|
| 520 | 520 | } |
| 521 | 521 | |
| 522 | - $remove_all_images_task = new Remove_All_Images_Task( $cached_image_license_service ); |
|
| 523 | - $remove_all_images_task_adapter = new Task_Ajax_Adapter( $remove_all_images_task ); |
|
| 522 | + $remove_all_images_task = new Remove_All_Images_Task($cached_image_license_service); |
|
| 523 | + $remove_all_images_task_adapter = new Task_Ajax_Adapter($remove_all_images_task); |
|
| 524 | 524 | |
| 525 | 525 | $reload_data_task = new Reload_Data_Task(); |
| 526 | - $reload_data_task_adapter = new Task_Ajax_Adapter( $reload_data_task ); |
|
| 526 | + $reload_data_task_adapter = new Task_Ajax_Adapter($reload_data_task); |
|
| 527 | 527 | |
| 528 | - $add_license_caption_or_remove_task = new Add_License_Caption_Or_Remove_Task( $cached_image_license_service ); |
|
| 529 | - $add_license_caption_or_remove_task_adapter = new Task_Ajax_Adapter( $add_license_caption_or_remove_task ); |
|
| 528 | + $add_license_caption_or_remove_task = new Add_License_Caption_Or_Remove_Task($cached_image_license_service); |
|
| 529 | + $add_license_caption_or_remove_task_adapter = new Task_Ajax_Adapter($add_license_caption_or_remove_task); |
|
| 530 | 530 | |
| 531 | - $remove_all_images_task_page = new Remove_All_Images_Page( new Task_Ajax_Adapters_Registry( $remove_all_images_task_adapter ), $plugin->get_version() ); |
|
| 532 | - $reload_data_task_page = new Reload_Data_Page( new Task_Ajax_Adapters_Registry( $reload_data_task_adapter ), $plugin->get_version() ); |
|
| 533 | - $add_license_caption_or_remove_task_page = new Add_License_Caption_Or_Remove_Page( new Task_Ajax_Adapters_Registry( $add_license_caption_or_remove_task_adapter ), $plugin->get_version() ); |
|
| 531 | + $remove_all_images_task_page = new Remove_All_Images_Page(new Task_Ajax_Adapters_Registry($remove_all_images_task_adapter), $plugin->get_version()); |
|
| 532 | + $reload_data_task_page = new Reload_Data_Page(new Task_Ajax_Adapters_Registry($reload_data_task_adapter), $plugin->get_version()); |
|
| 533 | + $add_license_caption_or_remove_task_page = new Add_License_Caption_Or_Remove_Page(new Task_Ajax_Adapters_Registry($add_license_caption_or_remove_task_adapter), $plugin->get_version()); |
|
| 534 | 534 | |
| 535 | 535 | new Wordlift_Products_Navigator_Shortcode_REST(); |
| 536 | 536 | |
| 537 | - if ( apply_filters( 'wl_feature__enable__article-wrapper', false ) ) { |
|
| 538 | - new Jsonld_Article_Wrapper( Wordlift_Post_To_Jsonld_Converter::get_instance() ); |
|
| 537 | + if (apply_filters('wl_feature__enable__article-wrapper', false)) { |
|
| 538 | + new Jsonld_Article_Wrapper(Wordlift_Post_To_Jsonld_Converter::get_instance()); |
|
| 539 | 539 | } |
| 540 | 540 | |
| 541 | 541 | // Register the Dataset module, requires `$api_service`. |
| 542 | - require_once plugin_dir_path( __FILE__ ) . 'wordlift/dataset/index.php'; |
|
| 542 | + require_once plugin_dir_path(__FILE__).'wordlift/dataset/index.php'; |
|
| 543 | 543 | |
| 544 | 544 | } ); |
| 545 | 545 | |
@@ -555,24 +555,24 @@ discard block |
||
| 555 | 555 | */ |
| 556 | 556 | function wordlift_plugin_autoload_register() { |
| 557 | 557 | |
| 558 | - spl_autoload_register( function ( $class_name ) { |
|
| 558 | + spl_autoload_register(function($class_name) { |
|
| 559 | 559 | |
| 560 | 560 | // Bail out if these are not our classes. |
| 561 | - if ( 0 !== strpos( $class_name, 'Wordlift\\' ) ) { |
|
| 561 | + if (0 !== strpos($class_name, 'Wordlift\\')) { |
|
| 562 | 562 | return false; |
| 563 | 563 | } |
| 564 | 564 | |
| 565 | - $class_name_lc = strtolower( str_replace( '_', '-', $class_name ) ); |
|
| 565 | + $class_name_lc = strtolower(str_replace('_', '-', $class_name)); |
|
| 566 | 566 | |
| 567 | - preg_match( '|^(?:(.*)\\\\)?(.+?)$|', $class_name_lc, $matches ); |
|
| 567 | + preg_match('|^(?:(.*)\\\\)?(.+?)$|', $class_name_lc, $matches); |
|
| 568 | 568 | |
| 569 | - $path = str_replace( '\\', DIRECTORY_SEPARATOR, $matches[1] ); |
|
| 570 | - $file = 'class-' . $matches[2] . '.php'; |
|
| 569 | + $path = str_replace('\\', DIRECTORY_SEPARATOR, $matches[1]); |
|
| 570 | + $file = 'class-'.$matches[2].'.php'; |
|
| 571 | 571 | |
| 572 | - $full_path = plugin_dir_path( __FILE__ ) . $path . DIRECTORY_SEPARATOR . $file; |
|
| 572 | + $full_path = plugin_dir_path(__FILE__).$path.DIRECTORY_SEPARATOR.$file; |
|
| 573 | 573 | |
| 574 | - if ( ! file_exists( $full_path ) ) { |
|
| 575 | - echo( "Class $class_name not found at $full_path." ); |
|
| 574 | + if ( ! file_exists($full_path)) { |
|
| 575 | + echo("Class $class_name not found at $full_path."); |
|
| 576 | 576 | |
| 577 | 577 | return false; |
| 578 | 578 | } |
@@ -584,16 +584,16 @@ discard block |
||
| 584 | 584 | |
| 585 | 585 | } |
| 586 | 586 | |
| 587 | -function wl_block_categories( $categories, $post ) { |
|
| 587 | +function wl_block_categories($categories, $post) { |
|
| 588 | 588 | return array_merge( |
| 589 | 589 | $categories, |
| 590 | 590 | array( |
| 591 | 591 | array( |
| 592 | 592 | 'slug' => 'wordlift', |
| 593 | - 'title' => __( 'WordLift', 'wordlift' ), |
|
| 593 | + 'title' => __('WordLift', 'wordlift'), |
|
| 594 | 594 | ), |
| 595 | 595 | ) |
| 596 | 596 | ); |
| 597 | 597 | } |
| 598 | 598 | |
| 599 | -add_filter( 'block_categories', 'wl_block_categories', 10, 2 ); |
|
| 599 | +add_filter('block_categories', 'wl_block_categories', 10, 2); |
|
@@ -15,237 +15,237 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class Wordlift_Jsonld_Service { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * A {@link Wordlift_Entity_Service} instance. |
|
| 20 | - * |
|
| 21 | - * @since 3.8.0 |
|
| 22 | - * @access private |
|
| 23 | - * @var Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 24 | - */ |
|
| 25 | - private $entity_service; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * A {@link Wordlift_Post_Converter} instance. |
|
| 29 | - * |
|
| 30 | - * @since 3.8.0 |
|
| 31 | - * @access private |
|
| 32 | - * @var \Wordlift_Post_Converter A {@link Wordlift_Post_Converter} instance. |
|
| 33 | - */ |
|
| 34 | - private $converter; |
|
| 35 | - |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * A {@link Wordlift_Website_Jsonld_Converter} instance. |
|
| 39 | - * |
|
| 40 | - * @since 3.14.0 |
|
| 41 | - * @access private |
|
| 42 | - * @var \Wordlift_Website_Jsonld_Converter A {@link Wordlift_Website_Jsonld_Converter} instance. |
|
| 43 | - */ |
|
| 44 | - private $website_converter; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * The singleton instance for the JSON-LD service. |
|
| 48 | - * |
|
| 49 | - * @since 3.15.1 |
|
| 50 | - * |
|
| 51 | - * @var \Wordlift_Jsonld_Service $instance The singleton instance for the JSON-LD service. |
|
| 52 | - */ |
|
| 53 | - private static $instance; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * Create a JSON-LD service. |
|
| 57 | - * |
|
| 58 | - * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 59 | - * @param \Wordlift_Post_Converter $converter A {@link Wordlift_Uri_To_Jsonld_Converter} instance. |
|
| 60 | - * @param \Wordlift_Website_Jsonld_Converter $website_converter A {@link Wordlift_Website_Jsonld_Converter} instance. |
|
| 61 | - * |
|
| 62 | - * @since 3.8.0 |
|
| 63 | - * |
|
| 64 | - */ |
|
| 65 | - public function __construct( $entity_service, $converter, $website_converter ) { |
|
| 66 | - |
|
| 67 | - $this->entity_service = $entity_service; |
|
| 68 | - $this->converter = $converter; |
|
| 69 | - $this->website_converter = $website_converter; |
|
| 70 | - |
|
| 71 | - self::$instance = $this; |
|
| 72 | - |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * Get the singleton instance for the JSON-LD service. |
|
| 77 | - * |
|
| 78 | - * @return \Wordlift_Jsonld_Service The singleton instance for the JSON-LD service. |
|
| 79 | - * @since 3.15.1 |
|
| 80 | - * |
|
| 81 | - */ |
|
| 82 | - public static function get_instance() { |
|
| 83 | - |
|
| 84 | - return self::$instance; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * Process calls to the AJAX 'wl_jsonld' endpoint. |
|
| 89 | - * |
|
| 90 | - * @since 3.8.0 |
|
| 91 | - */ |
|
| 92 | - public function get() { |
|
| 93 | - // Clear the buffer to be sure someone doesn't mess with our response. |
|
| 94 | - // |
|
| 95 | - // See https://github.com/insideout10/wordlift-plugin/issues/406. |
|
| 96 | - // See https://codex.wordpress.org/AJAX_in_Plugins. |
|
| 97 | - @ob_clean(); |
|
| 98 | - |
|
| 99 | - // Get the parameter from the request. |
|
| 100 | - $is_homepage = isset( $_REQUEST['homepage'] ); |
|
| 101 | - $post_id = isset( $_REQUEST['id'] ) && is_numeric( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : null; |
|
| 102 | - |
|
| 103 | - // Send the generated JSON-LD. |
|
| 104 | - $this->send_jsonld( $this->get_jsonld( $is_homepage, $post_id ) ); |
|
| 105 | - |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * A close of WP's own `wp_send_json` function which uses `application/ld+json` as content type. |
|
| 110 | - * |
|
| 111 | - * @param mixed $response Variable (usually an array or object) to encode as JSON, |
|
| 112 | - * then print and die. |
|
| 113 | - * @param int $status_code The HTTP status code to output. |
|
| 114 | - * |
|
| 115 | - * @since 3.18.5 |
|
| 116 | - * |
|
| 117 | - */ |
|
| 118 | - private function send_jsonld( $response, $status_code = null ) { |
|
| 119 | - @header( 'Content-Type: application/ld+json; charset=' . get_option( 'blog_charset' ) ); |
|
| 120 | - echo wp_json_encode( $response ); |
|
| 121 | - if ( apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { |
|
| 122 | - wp_die(); |
|
| 123 | - } else { |
|
| 124 | - die; |
|
| 125 | - } |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * Get the JSON-LD. |
|
| 130 | - * |
|
| 131 | - * @param bool $is_homepage Whether the JSON-LD for the homepage is being requested. |
|
| 132 | - * @param int|null $post_id The JSON-LD for the specified {@link WP_Post} id. |
|
| 133 | - * @param int $context A context for the JSON-LD generation, valid values in Jsonld_Context_Enum. |
|
| 134 | - * |
|
| 135 | - * @return array A JSON-LD structure. |
|
| 136 | - * @since 3.15.1 |
|
| 137 | - * |
|
| 138 | - */ |
|
| 139 | - public function get_jsonld( $is_homepage = false, $post_id = null, $context = Jsonld_Context_Enum::UNKNOWN ) { |
|
| 140 | - |
|
| 141 | - // Tell NewRelic to ignore us, otherwise NewRelic customers might receive |
|
| 142 | - // e-mails with a low apdex score. |
|
| 143 | - // |
|
| 144 | - // See https://github.com/insideout10/wordlift-plugin/issues/521 |
|
| 145 | - Wordlift_NewRelic_Adapter::ignore_apdex(); |
|
| 146 | - |
|
| 147 | - // Switch to Website converter if is home page. |
|
| 148 | - if ( $is_homepage ) { |
|
| 149 | - /** |
|
| 150 | - * Filter: 'wordlift_disable_website_json_ld' - Allow disabling of the json+ld output. |
|
| 151 | - * |
|
| 152 | - * @since 3.14.0 |
|
| 153 | - * @api bool $display_search Whether or not to display json+ld search on the frontend. |
|
| 154 | - */ |
|
| 155 | - if ( apply_filters( 'wordlift_disable_website_json_ld', false ) ) { |
|
| 156 | - return array(); |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - // Set a reference to the website_converter. |
|
| 160 | - $website_converter = $this->website_converter; |
|
| 161 | - |
|
| 162 | - // Send JSON-LD. |
|
| 163 | - return $website_converter->create_schema(); |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - // If no id has been provided return an empty array. |
|
| 167 | - if ( ! isset( $post_id ) ) { |
|
| 168 | - return array(); |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - // An array of references which is captured when converting an URI to a |
|
| 172 | - // json which we gather to further expand our json-ld. |
|
| 173 | - $references = array(); |
|
| 174 | - $references_infos = array(); |
|
| 175 | - |
|
| 176 | - // Set a reference to the entity_to_jsonld_converter to use in the closures. |
|
| 177 | - $entity_to_jsonld_converter = $this->converter; |
|
| 178 | - |
|
| 179 | - // Convert each URI to a JSON-LD array, while gathering referenced entities. |
|
| 180 | - // in the references array. |
|
| 181 | - $jsonld = array_merge( |
|
| 182 | - array( $entity_to_jsonld_converter->convert( $post_id, $references, $references_infos ) ), |
|
| 183 | - // Convert each URI in the references array to JSON-LD. We don't output |
|
| 184 | - // entities already output above (hence the array_diff). |
|
| 185 | - array_filter( array_map( function ( $item ) use ( $entity_to_jsonld_converter, &$references_infos ) { |
|
| 186 | - |
|
| 187 | - // "2nd level properties" may not output here, e.g. a post |
|
| 188 | - // mentioning an event, located in a place: the place is referenced |
|
| 189 | - // via the `@id` but no other properties are loaded. |
|
| 190 | - $ignored = array(); |
|
| 191 | - |
|
| 192 | - return $entity_to_jsonld_converter->convert( $item, $ignored, $references_infos ); |
|
| 193 | - }, array_unique( $references ) ) ) ); |
|
| 194 | - |
|
| 195 | - $required_references = array_filter( $references_infos, function ( $item ) use ( $references ) { |
|
| 196 | - return isset( $item['reference'] ) && |
|
| 197 | - // Check that the reference is required |
|
| 198 | - $item['reference']->get_required() && |
|
| 199 | - // Check that the reference isn't being output already. |
|
| 200 | - ! in_array( $item['reference']->get_id(), $references ); |
|
| 201 | - } ); |
|
| 202 | - |
|
| 203 | - $jsonld = array_merge( $jsonld, array_filter( array_map( function ( $item ) use ( $references, $entity_to_jsonld_converter ) { |
|
| 204 | - |
|
| 205 | - if ( ! isset( $item['reference'] ) ) { |
|
| 206 | - return null; |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - $post_id = $item['reference']->get_id(); |
|
| 210 | - if ( in_array( $post_id, $references ) ) { |
|
| 211 | - return null; |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - $references[] = $post_id; |
|
| 215 | - |
|
| 216 | - return $entity_to_jsonld_converter->convert( $post_id, $references ); |
|
| 217 | - }, $required_references ) ) ); |
|
| 218 | - |
|
| 219 | - /** |
|
| 220 | - * Filter name: wl_after_get_jsonld |
|
| 221 | - * @return array |
|
| 222 | - * @since 3.27.2 |
|
| 223 | - * @var $jsonld array The final jsonld before outputting to page. |
|
| 224 | - * @var $post_id int The post id for which the jsonld is generated. |
|
| 225 | - * |
|
| 226 | - */ |
|
| 227 | - $jsonld = apply_filters( 'wl_after_get_jsonld', $jsonld, $post_id, $context ); |
|
| 228 | - |
|
| 229 | - return $jsonld; |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - /** |
|
| 233 | - * Write the JSON-LD in the head. |
|
| 234 | - * |
|
| 235 | - * This function isn't actually used, but may be used to quickly enable writing the JSON-LD synchronously to the |
|
| 236 | - * document head, using the `wp_head` hook. |
|
| 237 | - * |
|
| 238 | - * @since 3.18.5 |
|
| 239 | - */ |
|
| 240 | - public function wp_head() { |
|
| 241 | - |
|
| 242 | - // Determine whether this is the home page or whether we're displaying a single post. |
|
| 243 | - $is_homepage = is_home() || is_front_page(); |
|
| 244 | - $post_id = is_singular() ? get_the_ID() : null; |
|
| 245 | - |
|
| 246 | - $jsonld = json_encode( $this->get_jsonld( $is_homepage, $post_id, Jsonld_Context_Enum::PAGE ) ); |
|
| 247 | - ?> |
|
| 18 | + /** |
|
| 19 | + * A {@link Wordlift_Entity_Service} instance. |
|
| 20 | + * |
|
| 21 | + * @since 3.8.0 |
|
| 22 | + * @access private |
|
| 23 | + * @var Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 24 | + */ |
|
| 25 | + private $entity_service; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * A {@link Wordlift_Post_Converter} instance. |
|
| 29 | + * |
|
| 30 | + * @since 3.8.0 |
|
| 31 | + * @access private |
|
| 32 | + * @var \Wordlift_Post_Converter A {@link Wordlift_Post_Converter} instance. |
|
| 33 | + */ |
|
| 34 | + private $converter; |
|
| 35 | + |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * A {@link Wordlift_Website_Jsonld_Converter} instance. |
|
| 39 | + * |
|
| 40 | + * @since 3.14.0 |
|
| 41 | + * @access private |
|
| 42 | + * @var \Wordlift_Website_Jsonld_Converter A {@link Wordlift_Website_Jsonld_Converter} instance. |
|
| 43 | + */ |
|
| 44 | + private $website_converter; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * The singleton instance for the JSON-LD service. |
|
| 48 | + * |
|
| 49 | + * @since 3.15.1 |
|
| 50 | + * |
|
| 51 | + * @var \Wordlift_Jsonld_Service $instance The singleton instance for the JSON-LD service. |
|
| 52 | + */ |
|
| 53 | + private static $instance; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * Create a JSON-LD service. |
|
| 57 | + * |
|
| 58 | + * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 59 | + * @param \Wordlift_Post_Converter $converter A {@link Wordlift_Uri_To_Jsonld_Converter} instance. |
|
| 60 | + * @param \Wordlift_Website_Jsonld_Converter $website_converter A {@link Wordlift_Website_Jsonld_Converter} instance. |
|
| 61 | + * |
|
| 62 | + * @since 3.8.0 |
|
| 63 | + * |
|
| 64 | + */ |
|
| 65 | + public function __construct( $entity_service, $converter, $website_converter ) { |
|
| 66 | + |
|
| 67 | + $this->entity_service = $entity_service; |
|
| 68 | + $this->converter = $converter; |
|
| 69 | + $this->website_converter = $website_converter; |
|
| 70 | + |
|
| 71 | + self::$instance = $this; |
|
| 72 | + |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * Get the singleton instance for the JSON-LD service. |
|
| 77 | + * |
|
| 78 | + * @return \Wordlift_Jsonld_Service The singleton instance for the JSON-LD service. |
|
| 79 | + * @since 3.15.1 |
|
| 80 | + * |
|
| 81 | + */ |
|
| 82 | + public static function get_instance() { |
|
| 83 | + |
|
| 84 | + return self::$instance; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * Process calls to the AJAX 'wl_jsonld' endpoint. |
|
| 89 | + * |
|
| 90 | + * @since 3.8.0 |
|
| 91 | + */ |
|
| 92 | + public function get() { |
|
| 93 | + // Clear the buffer to be sure someone doesn't mess with our response. |
|
| 94 | + // |
|
| 95 | + // See https://github.com/insideout10/wordlift-plugin/issues/406. |
|
| 96 | + // See https://codex.wordpress.org/AJAX_in_Plugins. |
|
| 97 | + @ob_clean(); |
|
| 98 | + |
|
| 99 | + // Get the parameter from the request. |
|
| 100 | + $is_homepage = isset( $_REQUEST['homepage'] ); |
|
| 101 | + $post_id = isset( $_REQUEST['id'] ) && is_numeric( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : null; |
|
| 102 | + |
|
| 103 | + // Send the generated JSON-LD. |
|
| 104 | + $this->send_jsonld( $this->get_jsonld( $is_homepage, $post_id ) ); |
|
| 105 | + |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * A close of WP's own `wp_send_json` function which uses `application/ld+json` as content type. |
|
| 110 | + * |
|
| 111 | + * @param mixed $response Variable (usually an array or object) to encode as JSON, |
|
| 112 | + * then print and die. |
|
| 113 | + * @param int $status_code The HTTP status code to output. |
|
| 114 | + * |
|
| 115 | + * @since 3.18.5 |
|
| 116 | + * |
|
| 117 | + */ |
|
| 118 | + private function send_jsonld( $response, $status_code = null ) { |
|
| 119 | + @header( 'Content-Type: application/ld+json; charset=' . get_option( 'blog_charset' ) ); |
|
| 120 | + echo wp_json_encode( $response ); |
|
| 121 | + if ( apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { |
|
| 122 | + wp_die(); |
|
| 123 | + } else { |
|
| 124 | + die; |
|
| 125 | + } |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * Get the JSON-LD. |
|
| 130 | + * |
|
| 131 | + * @param bool $is_homepage Whether the JSON-LD for the homepage is being requested. |
|
| 132 | + * @param int|null $post_id The JSON-LD for the specified {@link WP_Post} id. |
|
| 133 | + * @param int $context A context for the JSON-LD generation, valid values in Jsonld_Context_Enum. |
|
| 134 | + * |
|
| 135 | + * @return array A JSON-LD structure. |
|
| 136 | + * @since 3.15.1 |
|
| 137 | + * |
|
| 138 | + */ |
|
| 139 | + public function get_jsonld( $is_homepage = false, $post_id = null, $context = Jsonld_Context_Enum::UNKNOWN ) { |
|
| 140 | + |
|
| 141 | + // Tell NewRelic to ignore us, otherwise NewRelic customers might receive |
|
| 142 | + // e-mails with a low apdex score. |
|
| 143 | + // |
|
| 144 | + // See https://github.com/insideout10/wordlift-plugin/issues/521 |
|
| 145 | + Wordlift_NewRelic_Adapter::ignore_apdex(); |
|
| 146 | + |
|
| 147 | + // Switch to Website converter if is home page. |
|
| 148 | + if ( $is_homepage ) { |
|
| 149 | + /** |
|
| 150 | + * Filter: 'wordlift_disable_website_json_ld' - Allow disabling of the json+ld output. |
|
| 151 | + * |
|
| 152 | + * @since 3.14.0 |
|
| 153 | + * @api bool $display_search Whether or not to display json+ld search on the frontend. |
|
| 154 | + */ |
|
| 155 | + if ( apply_filters( 'wordlift_disable_website_json_ld', false ) ) { |
|
| 156 | + return array(); |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + // Set a reference to the website_converter. |
|
| 160 | + $website_converter = $this->website_converter; |
|
| 161 | + |
|
| 162 | + // Send JSON-LD. |
|
| 163 | + return $website_converter->create_schema(); |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + // If no id has been provided return an empty array. |
|
| 167 | + if ( ! isset( $post_id ) ) { |
|
| 168 | + return array(); |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + // An array of references which is captured when converting an URI to a |
|
| 172 | + // json which we gather to further expand our json-ld. |
|
| 173 | + $references = array(); |
|
| 174 | + $references_infos = array(); |
|
| 175 | + |
|
| 176 | + // Set a reference to the entity_to_jsonld_converter to use in the closures. |
|
| 177 | + $entity_to_jsonld_converter = $this->converter; |
|
| 178 | + |
|
| 179 | + // Convert each URI to a JSON-LD array, while gathering referenced entities. |
|
| 180 | + // in the references array. |
|
| 181 | + $jsonld = array_merge( |
|
| 182 | + array( $entity_to_jsonld_converter->convert( $post_id, $references, $references_infos ) ), |
|
| 183 | + // Convert each URI in the references array to JSON-LD. We don't output |
|
| 184 | + // entities already output above (hence the array_diff). |
|
| 185 | + array_filter( array_map( function ( $item ) use ( $entity_to_jsonld_converter, &$references_infos ) { |
|
| 186 | + |
|
| 187 | + // "2nd level properties" may not output here, e.g. a post |
|
| 188 | + // mentioning an event, located in a place: the place is referenced |
|
| 189 | + // via the `@id` but no other properties are loaded. |
|
| 190 | + $ignored = array(); |
|
| 191 | + |
|
| 192 | + return $entity_to_jsonld_converter->convert( $item, $ignored, $references_infos ); |
|
| 193 | + }, array_unique( $references ) ) ) ); |
|
| 194 | + |
|
| 195 | + $required_references = array_filter( $references_infos, function ( $item ) use ( $references ) { |
|
| 196 | + return isset( $item['reference'] ) && |
|
| 197 | + // Check that the reference is required |
|
| 198 | + $item['reference']->get_required() && |
|
| 199 | + // Check that the reference isn't being output already. |
|
| 200 | + ! in_array( $item['reference']->get_id(), $references ); |
|
| 201 | + } ); |
|
| 202 | + |
|
| 203 | + $jsonld = array_merge( $jsonld, array_filter( array_map( function ( $item ) use ( $references, $entity_to_jsonld_converter ) { |
|
| 204 | + |
|
| 205 | + if ( ! isset( $item['reference'] ) ) { |
|
| 206 | + return null; |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + $post_id = $item['reference']->get_id(); |
|
| 210 | + if ( in_array( $post_id, $references ) ) { |
|
| 211 | + return null; |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + $references[] = $post_id; |
|
| 215 | + |
|
| 216 | + return $entity_to_jsonld_converter->convert( $post_id, $references ); |
|
| 217 | + }, $required_references ) ) ); |
|
| 218 | + |
|
| 219 | + /** |
|
| 220 | + * Filter name: wl_after_get_jsonld |
|
| 221 | + * @return array |
|
| 222 | + * @since 3.27.2 |
|
| 223 | + * @var $jsonld array The final jsonld before outputting to page. |
|
| 224 | + * @var $post_id int The post id for which the jsonld is generated. |
|
| 225 | + * |
|
| 226 | + */ |
|
| 227 | + $jsonld = apply_filters( 'wl_after_get_jsonld', $jsonld, $post_id, $context ); |
|
| 228 | + |
|
| 229 | + return $jsonld; |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + /** |
|
| 233 | + * Write the JSON-LD in the head. |
|
| 234 | + * |
|
| 235 | + * This function isn't actually used, but may be used to quickly enable writing the JSON-LD synchronously to the |
|
| 236 | + * document head, using the `wp_head` hook. |
|
| 237 | + * |
|
| 238 | + * @since 3.18.5 |
|
| 239 | + */ |
|
| 240 | + public function wp_head() { |
|
| 241 | + |
|
| 242 | + // Determine whether this is the home page or whether we're displaying a single post. |
|
| 243 | + $is_homepage = is_home() || is_front_page(); |
|
| 244 | + $post_id = is_singular() ? get_the_ID() : null; |
|
| 245 | + |
|
| 246 | + $jsonld = json_encode( $this->get_jsonld( $is_homepage, $post_id, Jsonld_Context_Enum::PAGE ) ); |
|
| 247 | + ?> |
|
| 248 | 248 | <script type="application/ld+json"><?php echo $jsonld; ?></script><?php |
| 249 | - } |
|
| 249 | + } |
|
| 250 | 250 | |
| 251 | 251 | } |
@@ -62,7 +62,7 @@ discard block |
||
| 62 | 62 | * @since 3.8.0 |
| 63 | 63 | * |
| 64 | 64 | */ |
| 65 | - public function __construct( $entity_service, $converter, $website_converter ) { |
|
| 65 | + public function __construct($entity_service, $converter, $website_converter) { |
|
| 66 | 66 | |
| 67 | 67 | $this->entity_service = $entity_service; |
| 68 | 68 | $this->converter = $converter; |
@@ -97,11 +97,11 @@ discard block |
||
| 97 | 97 | @ob_clean(); |
| 98 | 98 | |
| 99 | 99 | // Get the parameter from the request. |
| 100 | - $is_homepage = isset( $_REQUEST['homepage'] ); |
|
| 101 | - $post_id = isset( $_REQUEST['id'] ) && is_numeric( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : null; |
|
| 100 | + $is_homepage = isset($_REQUEST['homepage']); |
|
| 101 | + $post_id = isset($_REQUEST['id']) && is_numeric($_REQUEST['id']) ? intval($_REQUEST['id']) : null; |
|
| 102 | 102 | |
| 103 | 103 | // Send the generated JSON-LD. |
| 104 | - $this->send_jsonld( $this->get_jsonld( $is_homepage, $post_id ) ); |
|
| 104 | + $this->send_jsonld($this->get_jsonld($is_homepage, $post_id)); |
|
| 105 | 105 | |
| 106 | 106 | } |
| 107 | 107 | |
@@ -115,10 +115,10 @@ discard block |
||
| 115 | 115 | * @since 3.18.5 |
| 116 | 116 | * |
| 117 | 117 | */ |
| 118 | - private function send_jsonld( $response, $status_code = null ) { |
|
| 119 | - @header( 'Content-Type: application/ld+json; charset=' . get_option( 'blog_charset' ) ); |
|
| 120 | - echo wp_json_encode( $response ); |
|
| 121 | - if ( apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { |
|
| 118 | + private function send_jsonld($response, $status_code = null) { |
|
| 119 | + @header('Content-Type: application/ld+json; charset='.get_option('blog_charset')); |
|
| 120 | + echo wp_json_encode($response); |
|
| 121 | + if (apply_filters('wp_doing_ajax', defined('DOING_AJAX') && DOING_AJAX)) { |
|
| 122 | 122 | wp_die(); |
| 123 | 123 | } else { |
| 124 | 124 | die; |
@@ -136,7 +136,7 @@ discard block |
||
| 136 | 136 | * @since 3.15.1 |
| 137 | 137 | * |
| 138 | 138 | */ |
| 139 | - public function get_jsonld( $is_homepage = false, $post_id = null, $context = Jsonld_Context_Enum::UNKNOWN ) { |
|
| 139 | + public function get_jsonld($is_homepage = false, $post_id = null, $context = Jsonld_Context_Enum::UNKNOWN) { |
|
| 140 | 140 | |
| 141 | 141 | // Tell NewRelic to ignore us, otherwise NewRelic customers might receive |
| 142 | 142 | // e-mails with a low apdex score. |
@@ -145,14 +145,14 @@ discard block |
||
| 145 | 145 | Wordlift_NewRelic_Adapter::ignore_apdex(); |
| 146 | 146 | |
| 147 | 147 | // Switch to Website converter if is home page. |
| 148 | - if ( $is_homepage ) { |
|
| 148 | + if ($is_homepage) { |
|
| 149 | 149 | /** |
| 150 | 150 | * Filter: 'wordlift_disable_website_json_ld' - Allow disabling of the json+ld output. |
| 151 | 151 | * |
| 152 | 152 | * @since 3.14.0 |
| 153 | 153 | * @api bool $display_search Whether or not to display json+ld search on the frontend. |
| 154 | 154 | */ |
| 155 | - if ( apply_filters( 'wordlift_disable_website_json_ld', false ) ) { |
|
| 155 | + if (apply_filters('wordlift_disable_website_json_ld', false)) { |
|
| 156 | 156 | return array(); |
| 157 | 157 | } |
| 158 | 158 | |
@@ -164,7 +164,7 @@ discard block |
||
| 164 | 164 | } |
| 165 | 165 | |
| 166 | 166 | // If no id has been provided return an empty array. |
| 167 | - if ( ! isset( $post_id ) ) { |
|
| 167 | + if ( ! isset($post_id)) { |
|
| 168 | 168 | return array(); |
| 169 | 169 | } |
| 170 | 170 | |
@@ -179,42 +179,42 @@ discard block |
||
| 179 | 179 | // Convert each URI to a JSON-LD array, while gathering referenced entities. |
| 180 | 180 | // in the references array. |
| 181 | 181 | $jsonld = array_merge( |
| 182 | - array( $entity_to_jsonld_converter->convert( $post_id, $references, $references_infos ) ), |
|
| 182 | + array($entity_to_jsonld_converter->convert($post_id, $references, $references_infos)), |
|
| 183 | 183 | // Convert each URI in the references array to JSON-LD. We don't output |
| 184 | 184 | // entities already output above (hence the array_diff). |
| 185 | - array_filter( array_map( function ( $item ) use ( $entity_to_jsonld_converter, &$references_infos ) { |
|
| 185 | + array_filter(array_map(function($item) use ($entity_to_jsonld_converter, &$references_infos) { |
|
| 186 | 186 | |
| 187 | 187 | // "2nd level properties" may not output here, e.g. a post |
| 188 | 188 | // mentioning an event, located in a place: the place is referenced |
| 189 | 189 | // via the `@id` but no other properties are loaded. |
| 190 | 190 | $ignored = array(); |
| 191 | 191 | |
| 192 | - return $entity_to_jsonld_converter->convert( $item, $ignored, $references_infos ); |
|
| 193 | - }, array_unique( $references ) ) ) ); |
|
| 192 | + return $entity_to_jsonld_converter->convert($item, $ignored, $references_infos); |
|
| 193 | + }, array_unique($references))) ); |
|
| 194 | 194 | |
| 195 | - $required_references = array_filter( $references_infos, function ( $item ) use ( $references ) { |
|
| 196 | - return isset( $item['reference'] ) && |
|
| 195 | + $required_references = array_filter($references_infos, function($item) use ($references) { |
|
| 196 | + return isset($item['reference']) && |
|
| 197 | 197 | // Check that the reference is required |
| 198 | 198 | $item['reference']->get_required() && |
| 199 | 199 | // Check that the reference isn't being output already. |
| 200 | - ! in_array( $item['reference']->get_id(), $references ); |
|
| 200 | + ! in_array($item['reference']->get_id(), $references); |
|
| 201 | 201 | } ); |
| 202 | 202 | |
| 203 | - $jsonld = array_merge( $jsonld, array_filter( array_map( function ( $item ) use ( $references, $entity_to_jsonld_converter ) { |
|
| 203 | + $jsonld = array_merge($jsonld, array_filter(array_map(function($item) use ($references, $entity_to_jsonld_converter) { |
|
| 204 | 204 | |
| 205 | - if ( ! isset( $item['reference'] ) ) { |
|
| 205 | + if ( ! isset($item['reference'])) { |
|
| 206 | 206 | return null; |
| 207 | 207 | } |
| 208 | 208 | |
| 209 | 209 | $post_id = $item['reference']->get_id(); |
| 210 | - if ( in_array( $post_id, $references ) ) { |
|
| 210 | + if (in_array($post_id, $references)) { |
|
| 211 | 211 | return null; |
| 212 | 212 | } |
| 213 | 213 | |
| 214 | 214 | $references[] = $post_id; |
| 215 | 215 | |
| 216 | - return $entity_to_jsonld_converter->convert( $post_id, $references ); |
|
| 217 | - }, $required_references ) ) ); |
|
| 216 | + return $entity_to_jsonld_converter->convert($post_id, $references); |
|
| 217 | + }, $required_references))); |
|
| 218 | 218 | |
| 219 | 219 | /** |
| 220 | 220 | * Filter name: wl_after_get_jsonld |
@@ -224,7 +224,7 @@ discard block |
||
| 224 | 224 | * @var $post_id int The post id for which the jsonld is generated. |
| 225 | 225 | * |
| 226 | 226 | */ |
| 227 | - $jsonld = apply_filters( 'wl_after_get_jsonld', $jsonld, $post_id, $context ); |
|
| 227 | + $jsonld = apply_filters('wl_after_get_jsonld', $jsonld, $post_id, $context); |
|
| 228 | 228 | |
| 229 | 229 | return $jsonld; |
| 230 | 230 | } |
@@ -243,7 +243,7 @@ discard block |
||
| 243 | 243 | $is_homepage = is_home() || is_front_page(); |
| 244 | 244 | $post_id = is_singular() ? get_the_ID() : null; |
| 245 | 245 | |
| 246 | - $jsonld = json_encode( $this->get_jsonld( $is_homepage, $post_id, Jsonld_Context_Enum::PAGE ) ); |
|
| 246 | + $jsonld = json_encode($this->get_jsonld($is_homepage, $post_id, Jsonld_Context_Enum::PAGE)); |
|
| 247 | 247 | ?> |
| 248 | 248 | <script type="application/ld+json"><?php echo $jsonld; ?></script><?php |
| 249 | 249 | } |
@@ -15,452 +15,452 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class Wordlift_Post_To_Jsonld_Converter extends Wordlift_Abstract_Post_To_Jsonld_Converter { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * @var Wordlift_Post_To_Jsonld_Converter |
|
| 20 | - */ |
|
| 21 | - private static $instance; |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * A {@link Wordlift_Configuration_Service} instance. |
|
| 25 | - * |
|
| 26 | - * @since 3.10.0 |
|
| 27 | - * @access private |
|
| 28 | - * @var \Wordlift_Configuration_Service $configuration_service A {@link Wordlift_Configuration_Service} instance. |
|
| 29 | - */ |
|
| 30 | - private $configuration_service; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * A {@link Wordlift_Log_Service} instance. |
|
| 34 | - * |
|
| 35 | - * @since 3.10.0 |
|
| 36 | - * @access private |
|
| 37 | - * @var Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 38 | - */ |
|
| 39 | - private $log; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * Wordlift_Post_To_Jsonld_Converter constructor. |
|
| 43 | - * |
|
| 44 | - * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
| 45 | - * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 46 | - * @param \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance. |
|
| 47 | - * @param \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance. |
|
| 48 | - * @param \Wordlift_Configuration_Service $configuration_service A {@link Wordlift_Configuration_Service} instance. |
|
| 49 | - * |
|
| 50 | - * @since 3.10.0 |
|
| 51 | - * |
|
| 52 | - */ |
|
| 53 | - public function __construct( $entity_type_service, $entity_service, $user_service, $attachment_service, $configuration_service ) { |
|
| 54 | - parent::__construct( $entity_type_service, $entity_service, $user_service, $attachment_service ); |
|
| 55 | - |
|
| 56 | - $this->configuration_service = $configuration_service; |
|
| 57 | - |
|
| 58 | - // Set a reference to the logger. |
|
| 59 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Post_To_Jsonld_Converter' ); |
|
| 60 | - |
|
| 61 | - self::$instance = $this; |
|
| 62 | - |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - public static function get_instance() { |
|
| 66 | - |
|
| 67 | - return self::$instance; |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * Convert the provided {@link WP_Post} to a JSON-LD array. Any entity reference |
|
| 72 | - * found while processing the post is set in the $references array. |
|
| 73 | - * |
|
| 74 | - * @param int $post_id The post id. |
|
| 75 | - * @param array $references An array of entity references. |
|
| 76 | - * @param array $references_infos |
|
| 77 | - * |
|
| 78 | - * @return array A JSON-LD array. |
|
| 79 | - * @since 3.10.0 |
|
| 80 | - */ |
|
| 81 | - public function convert( $post_id, &$references = array(), &$references_infos = array() ) { |
|
| 82 | - |
|
| 83 | - // Get the post instance. |
|
| 84 | - if ( null === $post = get_post( $post_id ) ) { |
|
| 85 | - // Post not found. |
|
| 86 | - return null; |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - // Get the base JSON-LD and the list of entities referenced by this entity. |
|
| 90 | - $jsonld = parent::convert( $post_id, $references, $references_infos ); |
|
| 91 | - |
|
| 92 | - // Set WebPage by default. |
|
| 93 | - if ( empty( $jsonld['@type'] ) ) { |
|
| 94 | - $jsonld['@type'] = 'WebPage'; |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - // Get the entity name. |
|
| 98 | - $jsonld['headline'] = $post->post_title; |
|
| 99 | - |
|
| 100 | - // Set the published and modified dates. |
|
| 101 | - /* |
|
| 18 | + /** |
|
| 19 | + * @var Wordlift_Post_To_Jsonld_Converter |
|
| 20 | + */ |
|
| 21 | + private static $instance; |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * A {@link Wordlift_Configuration_Service} instance. |
|
| 25 | + * |
|
| 26 | + * @since 3.10.0 |
|
| 27 | + * @access private |
|
| 28 | + * @var \Wordlift_Configuration_Service $configuration_service A {@link Wordlift_Configuration_Service} instance. |
|
| 29 | + */ |
|
| 30 | + private $configuration_service; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * A {@link Wordlift_Log_Service} instance. |
|
| 34 | + * |
|
| 35 | + * @since 3.10.0 |
|
| 36 | + * @access private |
|
| 37 | + * @var Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 38 | + */ |
|
| 39 | + private $log; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * Wordlift_Post_To_Jsonld_Converter constructor. |
|
| 43 | + * |
|
| 44 | + * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
| 45 | + * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 46 | + * @param \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance. |
|
| 47 | + * @param \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance. |
|
| 48 | + * @param \Wordlift_Configuration_Service $configuration_service A {@link Wordlift_Configuration_Service} instance. |
|
| 49 | + * |
|
| 50 | + * @since 3.10.0 |
|
| 51 | + * |
|
| 52 | + */ |
|
| 53 | + public function __construct( $entity_type_service, $entity_service, $user_service, $attachment_service, $configuration_service ) { |
|
| 54 | + parent::__construct( $entity_type_service, $entity_service, $user_service, $attachment_service ); |
|
| 55 | + |
|
| 56 | + $this->configuration_service = $configuration_service; |
|
| 57 | + |
|
| 58 | + // Set a reference to the logger. |
|
| 59 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Post_To_Jsonld_Converter' ); |
|
| 60 | + |
|
| 61 | + self::$instance = $this; |
|
| 62 | + |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + public static function get_instance() { |
|
| 66 | + |
|
| 67 | + return self::$instance; |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * Convert the provided {@link WP_Post} to a JSON-LD array. Any entity reference |
|
| 72 | + * found while processing the post is set in the $references array. |
|
| 73 | + * |
|
| 74 | + * @param int $post_id The post id. |
|
| 75 | + * @param array $references An array of entity references. |
|
| 76 | + * @param array $references_infos |
|
| 77 | + * |
|
| 78 | + * @return array A JSON-LD array. |
|
| 79 | + * @since 3.10.0 |
|
| 80 | + */ |
|
| 81 | + public function convert( $post_id, &$references = array(), &$references_infos = array() ) { |
|
| 82 | + |
|
| 83 | + // Get the post instance. |
|
| 84 | + if ( null === $post = get_post( $post_id ) ) { |
|
| 85 | + // Post not found. |
|
| 86 | + return null; |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + // Get the base JSON-LD and the list of entities referenced by this entity. |
|
| 90 | + $jsonld = parent::convert( $post_id, $references, $references_infos ); |
|
| 91 | + |
|
| 92 | + // Set WebPage by default. |
|
| 93 | + if ( empty( $jsonld['@type'] ) ) { |
|
| 94 | + $jsonld['@type'] = 'WebPage'; |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + // Get the entity name. |
|
| 98 | + $jsonld['headline'] = $post->post_title; |
|
| 99 | + |
|
| 100 | + // Set the published and modified dates. |
|
| 101 | + /* |
|
| 102 | 102 | * Set the `datePublished` and `dateModified` using the local timezone. |
| 103 | 103 | * |
| 104 | 104 | * @see https://github.com/insideout10/wordlift-plugin/issues/887 |
| 105 | 105 | * |
| 106 | 106 | * @since 3.20.0 |
| 107 | 107 | */ |
| 108 | - try { |
|
| 109 | - $default_timezone = date_default_timezone_get(); |
|
| 110 | - $timezone = get_option( 'timezone_string' ); |
|
| 111 | - if ( ! empty( $timezone ) ) { |
|
| 112 | - date_default_timezone_set( $timezone ); |
|
| 113 | - $jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i:sP', false, $post ); |
|
| 114 | - $jsonld['dateModified'] = get_post_modified_time( 'Y-m-d\TH:i:sP', false, $post ); |
|
| 115 | - date_default_timezone_set( $default_timezone ); |
|
| 116 | - } else { |
|
| 117 | - $jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 118 | - $jsonld['dateModified'] = get_post_modified_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 119 | - } |
|
| 120 | - } catch ( Exception $e ) { |
|
| 121 | - $jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 122 | - $jsonld['dateModified'] = get_post_modified_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - // Get the word count for the post. |
|
| 126 | - /* |
|
| 108 | + try { |
|
| 109 | + $default_timezone = date_default_timezone_get(); |
|
| 110 | + $timezone = get_option( 'timezone_string' ); |
|
| 111 | + if ( ! empty( $timezone ) ) { |
|
| 112 | + date_default_timezone_set( $timezone ); |
|
| 113 | + $jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i:sP', false, $post ); |
|
| 114 | + $jsonld['dateModified'] = get_post_modified_time( 'Y-m-d\TH:i:sP', false, $post ); |
|
| 115 | + date_default_timezone_set( $default_timezone ); |
|
| 116 | + } else { |
|
| 117 | + $jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 118 | + $jsonld['dateModified'] = get_post_modified_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 119 | + } |
|
| 120 | + } catch ( Exception $e ) { |
|
| 121 | + $jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 122 | + $jsonld['dateModified'] = get_post_modified_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + // Get the word count for the post. |
|
| 126 | + /* |
|
| 127 | 127 | * Do not display the `wordCount` on a `WebPage`. |
| 128 | 128 | * |
| 129 | 129 | * @see https://github.com/insideout10/wordlift-plugin/issues/888 |
| 130 | 130 | * |
| 131 | 131 | * @since 3.20.0 |
| 132 | 132 | */ |
| 133 | - if ( ! empty( $jsonld['@type'] ) && 'WebPage' !== $jsonld['@type'] ) { |
|
| 134 | - $post_adapter = new Wordlift_Post_Adapter( $post_id ); |
|
| 135 | - $jsonld['wordCount'] = $post_adapter->word_count(); |
|
| 136 | - } |
|
| 133 | + if ( ! empty( $jsonld['@type'] ) && 'WebPage' !== $jsonld['@type'] ) { |
|
| 134 | + $post_adapter = new Wordlift_Post_Adapter( $post_id ); |
|
| 135 | + $jsonld['wordCount'] = $post_adapter->word_count(); |
|
| 136 | + } |
|
| 137 | 137 | |
| 138 | - /* |
|
| 138 | + /* |
|
| 139 | 139 | * Add keywords, articleSection, commentCount and inLanguage properties to `Article` JSON-LD |
| 140 | 140 | * |
| 141 | 141 | * @see https://github.com/insideout10/wordlift-plugin/issues/1140 |
| 142 | 142 | * |
| 143 | 143 | * @since 3.27.2 |
| 144 | 144 | */ |
| 145 | - if ( ! empty( $jsonld['@type'] ) && 'WebPage' !== $jsonld['@type'] ) { |
|
| 146 | - $post_adapter = new Wordlift_Post_Adapter( $post_id ); |
|
| 147 | - $keywords = $post_adapter->keywords(); |
|
| 148 | - $article_section = $post_adapter->article_section(); |
|
| 149 | - $comment_count = $post_adapter->comment_count(); |
|
| 150 | - $locale = $post_adapter->locale(); |
|
| 151 | - |
|
| 152 | - if ( isset( $keywords ) ) { |
|
| 153 | - $jsonld['keywords'] = $keywords; |
|
| 154 | - } |
|
| 155 | - if ( isset( $article_section ) ) { |
|
| 156 | - $jsonld['articleSection'] = $article_section; |
|
| 157 | - } |
|
| 158 | - $jsonld['commentCount'] = $comment_count; |
|
| 159 | - $jsonld['inLanguage'] = $locale; |
|
| 160 | - $post_adapter->add_mentions( $post_id, $references ); |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - // Set the publisher. |
|
| 164 | - $this->set_publisher( $jsonld ); |
|
| 165 | - |
|
| 166 | - // Process the references if any. |
|
| 167 | - if ( 0 < count( $references ) ) { |
|
| 168 | - |
|
| 169 | - // Prepare the `about` and `mentions` array. |
|
| 170 | - $about = $mentions = array(); |
|
| 171 | - |
|
| 172 | - // If the entity is in the title, then it should be an `about`. |
|
| 173 | - foreach ( $references as $reference ) { |
|
| 174 | - |
|
| 175 | - // Get the entity labels. |
|
| 176 | - $labels = $this->entity_service->get_labels( $reference ); |
|
| 177 | - |
|
| 178 | - // Get the entity URI. |
|
| 179 | - $item = array( |
|
| 180 | - '@id' => $this->entity_service->get_uri( $reference ), |
|
| 181 | - ); |
|
| 182 | - |
|
| 183 | - $escaped_labels = array_map( function ( $value ) { |
|
| 184 | - return preg_quote( $value, '/' ); |
|
| 185 | - }, $labels ); |
|
| 186 | - |
|
| 187 | - // Check if the labels match any part of the title. |
|
| 188 | - $matches = 1 === preg_match( '/' . implode( '|', $escaped_labels ) . '/', $post->post_title ); |
|
| 189 | - |
|
| 190 | - // If the title matches, assign the entity to the about, otherwise to the mentions. |
|
| 191 | - if ( $matches ) { |
|
| 192 | - $about[] = $item; |
|
| 193 | - } else { |
|
| 194 | - $mentions[] = $item; |
|
| 195 | - } |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - // If we have abouts, assign them to the JSON-LD. |
|
| 199 | - if ( 0 < count( $about ) ) { |
|
| 200 | - $jsonld['about'] = $about; |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - // If we have mentions, assign them to the JSON-LD. |
|
| 204 | - if ( 0 < count( $mentions ) ) { |
|
| 205 | - $jsonld['mentions'] = $mentions; |
|
| 206 | - } |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - // Finally set the author. |
|
| 210 | - $jsonld['author'] = $this->get_author( $post->post_author, $references ); |
|
| 211 | - |
|
| 212 | - /** |
|
| 213 | - * Call the `wl_post_jsonld_array` filter. This filter allows 3rd parties to also modify the references. |
|
| 214 | - * |
|
| 215 | - * @param array $value { |
|
| 216 | - * |
|
| 217 | - * @type array $jsonld The JSON-LD structure. |
|
| 218 | - * @type int[] $references An array of post IDs. |
|
| 219 | - * } |
|
| 220 | - * @since 3.25.0 |
|
| 221 | - * |
|
| 222 | - * @see https://www.geeklab.info/2010/04/wordpress-pass-variables-by-reference-with-apply_filter/ |
|
| 223 | - * |
|
| 224 | - * @api |
|
| 225 | - */ |
|
| 226 | - $ret_val = apply_filters( 'wl_post_jsonld_array', array( |
|
| 227 | - 'jsonld' => $jsonld, |
|
| 228 | - 'references' => $references, |
|
| 229 | - ), $post_id ); |
|
| 230 | - $jsonld = $ret_val['jsonld']; |
|
| 231 | - $references = $ret_val['references']; |
|
| 232 | - |
|
| 233 | - /** |
|
| 234 | - * Call the `wl_post_jsonld` filter. |
|
| 235 | - * |
|
| 236 | - * @param array $jsonld The JSON-LD structure. |
|
| 237 | - * @param int $post_id The {@link WP_Post} `id`. |
|
| 238 | - * @param array $references The array of referenced entities. |
|
| 239 | - * |
|
| 240 | - * @since 3.14.0 |
|
| 241 | - * |
|
| 242 | - * @api |
|
| 243 | - */ |
|
| 244 | - return apply_filters( 'wl_post_jsonld', $jsonld, $post_id, $references ); |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - /** |
|
| 248 | - * Get the author's JSON-LD fragment. |
|
| 249 | - * |
|
| 250 | - * The JSON-LD fragment is generated using the {@link WP_User}'s data or |
|
| 251 | - * the referenced entity if configured for the {@link WP_User}. |
|
| 252 | - * |
|
| 253 | - * @param int $author_id The author {@link WP_User}'s `id`. |
|
| 254 | - * @param array $references An array of referenced entities. |
|
| 255 | - * |
|
| 256 | - * @return string|array A JSON-LD structure. |
|
| 257 | - * @since 3.14.0 |
|
| 258 | - * |
|
| 259 | - */ |
|
| 260 | - private function get_author( $author_id, &$references ) { |
|
| 261 | - |
|
| 262 | - // Get the entity bound to this user. |
|
| 263 | - $entity_id = $this->user_service->get_entity( $author_id ); |
|
| 264 | - |
|
| 265 | - // If there's no entity bound return a simple author structure. |
|
| 266 | - if ( empty( $entity_id ) || 'publish' !== get_post_status( $entity_id ) ) { |
|
| 267 | - |
|
| 268 | - $author = get_the_author_meta( 'display_name', $author_id ); |
|
| 269 | - $author_first_name = get_the_author_meta( 'first_name', $author_id ); |
|
| 270 | - $author_last_name = get_the_author_meta( 'last_name', $author_id ); |
|
| 271 | - $author_uri = $this->user_service->get_uri( $author_id ); |
|
| 272 | - |
|
| 273 | - return array( |
|
| 274 | - '@type' => 'Person', |
|
| 275 | - '@id' => $author_uri, |
|
| 276 | - 'name' => $author, |
|
| 277 | - 'givenName' => $author_first_name, |
|
| 278 | - 'familyName' => $author_last_name |
|
| 279 | - ); |
|
| 280 | - } |
|
| 281 | - |
|
| 282 | - // Add the author to the references. |
|
| 283 | - $author_uri = $this->entity_service->get_uri( $entity_id ); |
|
| 284 | - $references[] = $entity_id; |
|
| 285 | - |
|
| 286 | - // Return the JSON-LD for the referenced entity. |
|
| 287 | - return array( |
|
| 288 | - '@id' => $author_uri, |
|
| 289 | - ); |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - /** |
|
| 293 | - * Enrich the provided params array with publisher data, if available. |
|
| 294 | - * |
|
| 295 | - * @param array $params The parameters array. |
|
| 296 | - * |
|
| 297 | - * @since 3.10.0 |
|
| 298 | - * |
|
| 299 | - */ |
|
| 300 | - protected function set_publisher( &$params ) { |
|
| 301 | - |
|
| 302 | - // If the publisher id isn't set don't do anything. |
|
| 303 | - if ( null === $publisher_id = $this->configuration_service->get_publisher_id() ) { |
|
| 304 | - return; |
|
| 305 | - } |
|
| 306 | - |
|
| 307 | - // Get the post instance. |
|
| 308 | - if ( null === $post = get_post( $publisher_id ) ) { |
|
| 309 | - // Publisher not found. |
|
| 310 | - return; |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - // Get the item id. |
|
| 314 | - $id = $this->entity_service->get_uri( $publisher_id ); |
|
| 315 | - |
|
| 316 | - // Get the type. |
|
| 317 | - $type = $this->entity_type_service->get( $publisher_id ); |
|
| 318 | - |
|
| 319 | - // Get the name. |
|
| 320 | - $name = $post->post_title; |
|
| 321 | - |
|
| 322 | - // Set the publisher data. |
|
| 323 | - $params['publisher'] = array( |
|
| 324 | - '@type' => $this->relative_to_context( $type['uri'] ), |
|
| 325 | - '@id' => $id, |
|
| 326 | - 'name' => $name, |
|
| 327 | - ); |
|
| 328 | - |
|
| 329 | - // Add the sameAs values associated with the publisher. |
|
| 330 | - $storage_factory = Wordlift_Storage_Factory::get_instance(); |
|
| 331 | - $sameas = $storage_factory->post_meta( Wordlift_Schema_Service::FIELD_SAME_AS )->get( $publisher_id ); |
|
| 332 | - if ( ! empty( $sameas ) ) { |
|
| 333 | - $params['publisher']['sameAs'] = $sameas; |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - // Set the logo, only for http://schema.org/Organization as Person doesn't |
|
| 337 | - // support the logo property. |
|
| 338 | - // |
|
| 339 | - // See http://schema.org/logo. |
|
| 340 | - if ( 1 !== preg_match( '~Organization$~', $type['uri'] ) ) { |
|
| 341 | - return; |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - // Get the publisher logo. |
|
| 345 | - $publisher_logo = $this->get_publisher_logo( $post->ID ); |
|
| 346 | - |
|
| 347 | - // Bail out if the publisher logo isn't set. |
|
| 348 | - if ( false === $publisher_logo ) { |
|
| 349 | - return; |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - // Copy over some useful properties. |
|
| 353 | - // |
|
| 354 | - // See https://developers.google.com/search/docs/data-types/articles. |
|
| 355 | - $params['publisher']['logo']['@type'] = 'ImageObject'; |
|
| 356 | - $params['publisher']['logo']['url'] = $publisher_logo['url']; |
|
| 357 | - |
|
| 358 | - // If you specify a "width" or "height" value you should leave out |
|
| 359 | - // 'px'. For example: "width":"4608px" should be "width":"4608". |
|
| 360 | - // |
|
| 361 | - // See https://github.com/insideout10/wordlift-plugin/issues/451. |
|
| 362 | - $params['publisher']['logo']['width'] = $publisher_logo['width']; |
|
| 363 | - $params['publisher']['logo']['height'] = $publisher_logo['height']; |
|
| 364 | - |
|
| 365 | - } |
|
| 366 | - |
|
| 367 | - /** |
|
| 368 | - * Get the publisher logo structure. |
|
| 369 | - * |
|
| 370 | - * The function returns false when the publisher logo cannot be determined, i.e.: |
|
| 371 | - * - the post has no featured image. |
|
| 372 | - * - the featured image has no file. |
|
| 373 | - * - a wp_image_editor instance cannot be instantiated on the original file or on the publisher logo file. |
|
| 374 | - * |
|
| 375 | - * @param int $post_id The post id. |
|
| 376 | - * |
|
| 377 | - * @return array|false Returns an array with the `url`, `width` and `height` for the publisher logo or false in case |
|
| 378 | - * of errors. |
|
| 379 | - * @since 3.19.2 |
|
| 380 | - * @see https://github.com/insideout10/wordlift-plugin/issues/823 related issue. |
|
| 381 | - * |
|
| 382 | - */ |
|
| 383 | - private function get_publisher_logo( $post_id ) { |
|
| 384 | - |
|
| 385 | - // Get the featured image for the post. |
|
| 386 | - $thumbnail_id = get_post_thumbnail_id( $post_id ); |
|
| 387 | - |
|
| 388 | - // Bail out if thumbnail not available. |
|
| 389 | - if ( empty( $thumbnail_id ) || 0 === $thumbnail_id ) { |
|
| 390 | - $this->log->info( "Featured image not set for post $post_id." ); |
|
| 391 | - |
|
| 392 | - return false; |
|
| 393 | - } |
|
| 394 | - |
|
| 395 | - // Get the uploads base URL. |
|
| 396 | - $uploads_dir = wp_upload_dir(); |
|
| 397 | - |
|
| 398 | - // Get the attachment metadata. |
|
| 399 | - $metadata = wp_get_attachment_metadata( $thumbnail_id ); |
|
| 400 | - |
|
| 401 | - // Bail out if the file isn't set. |
|
| 402 | - if ( ! isset( $metadata['file'] ) ) { |
|
| 403 | - $this->log->warn( "Featured image file not found for post $post_id." ); |
|
| 404 | - |
|
| 405 | - return false; |
|
| 406 | - } |
|
| 407 | - |
|
| 408 | - // Retrieve the relative filename, e.g. "2018/05/logo_publisher.png" |
|
| 409 | - $path = $uploads_dir['basedir'] . DIRECTORY_SEPARATOR . $metadata['file']; |
|
| 410 | - |
|
| 411 | - // Use image src, if local file does not exist. @see https://github.com/insideout10/wordlift-plugin/issues/1149 |
|
| 412 | - if ( ! file_exists( $path ) ) { |
|
| 413 | - $this->log->warn( "Featured image file $path doesn't exist for post $post_id." ); |
|
| 414 | - |
|
| 415 | - $attachment_image_src = wp_get_attachment_image_src( $thumbnail_id, '' ); |
|
| 416 | - if ( $attachment_image_src ) { |
|
| 417 | - return array( |
|
| 418 | - 'url' => $attachment_image_src[0], |
|
| 419 | - 'width' => $attachment_image_src[1], |
|
| 420 | - 'height' => $attachment_image_src[2], |
|
| 421 | - ); |
|
| 422 | - } |
|
| 423 | - |
|
| 424 | - // Bail out if we cant fetch wp_get_attachment_image_src |
|
| 425 | - return false; |
|
| 426 | - |
|
| 427 | - } |
|
| 428 | - |
|
| 429 | - // Try to get the image editor and bail out if the editor cannot be instantiated. |
|
| 430 | - $original_file_editor = wp_get_image_editor( $path ); |
|
| 431 | - if ( is_wp_error( $original_file_editor ) ) { |
|
| 432 | - $this->log->warn( "Cannot instantiate WP Image Editor on file $path for post $post_id." ); |
|
| 433 | - |
|
| 434 | - return false; |
|
| 435 | - } |
|
| 436 | - |
|
| 437 | - // Generate the publisher logo filename, we cannot use the `width` and `height` because we're scaling |
|
| 438 | - // and we don't actually know the end values. |
|
| 439 | - $publisher_logo_path = $original_file_editor->generate_filename( '-publisher-logo' ); |
|
| 440 | - |
|
| 441 | - // If the file doesn't exist yet, create it. |
|
| 442 | - if ( ! file_exists( $publisher_logo_path ) ) { |
|
| 443 | - $original_file_editor->resize( 600, 60 ); |
|
| 444 | - $original_file_editor->save( $publisher_logo_path ); |
|
| 445 | - } |
|
| 446 | - |
|
| 447 | - // Try to get the image editor and bail out if the editor cannot be instantiated. |
|
| 448 | - $publisher_logo_editor = wp_get_image_editor( $publisher_logo_path ); |
|
| 449 | - if ( is_wp_error( $publisher_logo_editor ) ) { |
|
| 450 | - $this->log->warn( "Cannot instantiate WP Image Editor on file $publisher_logo_path for post $post_id." ); |
|
| 451 | - |
|
| 452 | - return false; |
|
| 453 | - } |
|
| 454 | - |
|
| 455 | - // Get the actual size. |
|
| 456 | - $size = $publisher_logo_editor->get_size(); |
|
| 457 | - |
|
| 458 | - // Finally return the array with data. |
|
| 459 | - return array( |
|
| 460 | - 'url' => $uploads_dir['baseurl'] . substr( $publisher_logo_path, strlen( $uploads_dir['basedir'] ) ), |
|
| 461 | - 'width' => $size['width'], |
|
| 462 | - 'height' => $size['height'], |
|
| 463 | - ); |
|
| 464 | - } |
|
| 145 | + if ( ! empty( $jsonld['@type'] ) && 'WebPage' !== $jsonld['@type'] ) { |
|
| 146 | + $post_adapter = new Wordlift_Post_Adapter( $post_id ); |
|
| 147 | + $keywords = $post_adapter->keywords(); |
|
| 148 | + $article_section = $post_adapter->article_section(); |
|
| 149 | + $comment_count = $post_adapter->comment_count(); |
|
| 150 | + $locale = $post_adapter->locale(); |
|
| 151 | + |
|
| 152 | + if ( isset( $keywords ) ) { |
|
| 153 | + $jsonld['keywords'] = $keywords; |
|
| 154 | + } |
|
| 155 | + if ( isset( $article_section ) ) { |
|
| 156 | + $jsonld['articleSection'] = $article_section; |
|
| 157 | + } |
|
| 158 | + $jsonld['commentCount'] = $comment_count; |
|
| 159 | + $jsonld['inLanguage'] = $locale; |
|
| 160 | + $post_adapter->add_mentions( $post_id, $references ); |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + // Set the publisher. |
|
| 164 | + $this->set_publisher( $jsonld ); |
|
| 165 | + |
|
| 166 | + // Process the references if any. |
|
| 167 | + if ( 0 < count( $references ) ) { |
|
| 168 | + |
|
| 169 | + // Prepare the `about` and `mentions` array. |
|
| 170 | + $about = $mentions = array(); |
|
| 171 | + |
|
| 172 | + // If the entity is in the title, then it should be an `about`. |
|
| 173 | + foreach ( $references as $reference ) { |
|
| 174 | + |
|
| 175 | + // Get the entity labels. |
|
| 176 | + $labels = $this->entity_service->get_labels( $reference ); |
|
| 177 | + |
|
| 178 | + // Get the entity URI. |
|
| 179 | + $item = array( |
|
| 180 | + '@id' => $this->entity_service->get_uri( $reference ), |
|
| 181 | + ); |
|
| 182 | + |
|
| 183 | + $escaped_labels = array_map( function ( $value ) { |
|
| 184 | + return preg_quote( $value, '/' ); |
|
| 185 | + }, $labels ); |
|
| 186 | + |
|
| 187 | + // Check if the labels match any part of the title. |
|
| 188 | + $matches = 1 === preg_match( '/' . implode( '|', $escaped_labels ) . '/', $post->post_title ); |
|
| 189 | + |
|
| 190 | + // If the title matches, assign the entity to the about, otherwise to the mentions. |
|
| 191 | + if ( $matches ) { |
|
| 192 | + $about[] = $item; |
|
| 193 | + } else { |
|
| 194 | + $mentions[] = $item; |
|
| 195 | + } |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + // If we have abouts, assign them to the JSON-LD. |
|
| 199 | + if ( 0 < count( $about ) ) { |
|
| 200 | + $jsonld['about'] = $about; |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + // If we have mentions, assign them to the JSON-LD. |
|
| 204 | + if ( 0 < count( $mentions ) ) { |
|
| 205 | + $jsonld['mentions'] = $mentions; |
|
| 206 | + } |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + // Finally set the author. |
|
| 210 | + $jsonld['author'] = $this->get_author( $post->post_author, $references ); |
|
| 211 | + |
|
| 212 | + /** |
|
| 213 | + * Call the `wl_post_jsonld_array` filter. This filter allows 3rd parties to also modify the references. |
|
| 214 | + * |
|
| 215 | + * @param array $value { |
|
| 216 | + * |
|
| 217 | + * @type array $jsonld The JSON-LD structure. |
|
| 218 | + * @type int[] $references An array of post IDs. |
|
| 219 | + * } |
|
| 220 | + * @since 3.25.0 |
|
| 221 | + * |
|
| 222 | + * @see https://www.geeklab.info/2010/04/wordpress-pass-variables-by-reference-with-apply_filter/ |
|
| 223 | + * |
|
| 224 | + * @api |
|
| 225 | + */ |
|
| 226 | + $ret_val = apply_filters( 'wl_post_jsonld_array', array( |
|
| 227 | + 'jsonld' => $jsonld, |
|
| 228 | + 'references' => $references, |
|
| 229 | + ), $post_id ); |
|
| 230 | + $jsonld = $ret_val['jsonld']; |
|
| 231 | + $references = $ret_val['references']; |
|
| 232 | + |
|
| 233 | + /** |
|
| 234 | + * Call the `wl_post_jsonld` filter. |
|
| 235 | + * |
|
| 236 | + * @param array $jsonld The JSON-LD structure. |
|
| 237 | + * @param int $post_id The {@link WP_Post} `id`. |
|
| 238 | + * @param array $references The array of referenced entities. |
|
| 239 | + * |
|
| 240 | + * @since 3.14.0 |
|
| 241 | + * |
|
| 242 | + * @api |
|
| 243 | + */ |
|
| 244 | + return apply_filters( 'wl_post_jsonld', $jsonld, $post_id, $references ); |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + /** |
|
| 248 | + * Get the author's JSON-LD fragment. |
|
| 249 | + * |
|
| 250 | + * The JSON-LD fragment is generated using the {@link WP_User}'s data or |
|
| 251 | + * the referenced entity if configured for the {@link WP_User}. |
|
| 252 | + * |
|
| 253 | + * @param int $author_id The author {@link WP_User}'s `id`. |
|
| 254 | + * @param array $references An array of referenced entities. |
|
| 255 | + * |
|
| 256 | + * @return string|array A JSON-LD structure. |
|
| 257 | + * @since 3.14.0 |
|
| 258 | + * |
|
| 259 | + */ |
|
| 260 | + private function get_author( $author_id, &$references ) { |
|
| 261 | + |
|
| 262 | + // Get the entity bound to this user. |
|
| 263 | + $entity_id = $this->user_service->get_entity( $author_id ); |
|
| 264 | + |
|
| 265 | + // If there's no entity bound return a simple author structure. |
|
| 266 | + if ( empty( $entity_id ) || 'publish' !== get_post_status( $entity_id ) ) { |
|
| 267 | + |
|
| 268 | + $author = get_the_author_meta( 'display_name', $author_id ); |
|
| 269 | + $author_first_name = get_the_author_meta( 'first_name', $author_id ); |
|
| 270 | + $author_last_name = get_the_author_meta( 'last_name', $author_id ); |
|
| 271 | + $author_uri = $this->user_service->get_uri( $author_id ); |
|
| 272 | + |
|
| 273 | + return array( |
|
| 274 | + '@type' => 'Person', |
|
| 275 | + '@id' => $author_uri, |
|
| 276 | + 'name' => $author, |
|
| 277 | + 'givenName' => $author_first_name, |
|
| 278 | + 'familyName' => $author_last_name |
|
| 279 | + ); |
|
| 280 | + } |
|
| 281 | + |
|
| 282 | + // Add the author to the references. |
|
| 283 | + $author_uri = $this->entity_service->get_uri( $entity_id ); |
|
| 284 | + $references[] = $entity_id; |
|
| 285 | + |
|
| 286 | + // Return the JSON-LD for the referenced entity. |
|
| 287 | + return array( |
|
| 288 | + '@id' => $author_uri, |
|
| 289 | + ); |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + /** |
|
| 293 | + * Enrich the provided params array with publisher data, if available. |
|
| 294 | + * |
|
| 295 | + * @param array $params The parameters array. |
|
| 296 | + * |
|
| 297 | + * @since 3.10.0 |
|
| 298 | + * |
|
| 299 | + */ |
|
| 300 | + protected function set_publisher( &$params ) { |
|
| 301 | + |
|
| 302 | + // If the publisher id isn't set don't do anything. |
|
| 303 | + if ( null === $publisher_id = $this->configuration_service->get_publisher_id() ) { |
|
| 304 | + return; |
|
| 305 | + } |
|
| 306 | + |
|
| 307 | + // Get the post instance. |
|
| 308 | + if ( null === $post = get_post( $publisher_id ) ) { |
|
| 309 | + // Publisher not found. |
|
| 310 | + return; |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + // Get the item id. |
|
| 314 | + $id = $this->entity_service->get_uri( $publisher_id ); |
|
| 315 | + |
|
| 316 | + // Get the type. |
|
| 317 | + $type = $this->entity_type_service->get( $publisher_id ); |
|
| 318 | + |
|
| 319 | + // Get the name. |
|
| 320 | + $name = $post->post_title; |
|
| 321 | + |
|
| 322 | + // Set the publisher data. |
|
| 323 | + $params['publisher'] = array( |
|
| 324 | + '@type' => $this->relative_to_context( $type['uri'] ), |
|
| 325 | + '@id' => $id, |
|
| 326 | + 'name' => $name, |
|
| 327 | + ); |
|
| 328 | + |
|
| 329 | + // Add the sameAs values associated with the publisher. |
|
| 330 | + $storage_factory = Wordlift_Storage_Factory::get_instance(); |
|
| 331 | + $sameas = $storage_factory->post_meta( Wordlift_Schema_Service::FIELD_SAME_AS )->get( $publisher_id ); |
|
| 332 | + if ( ! empty( $sameas ) ) { |
|
| 333 | + $params['publisher']['sameAs'] = $sameas; |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + // Set the logo, only for http://schema.org/Organization as Person doesn't |
|
| 337 | + // support the logo property. |
|
| 338 | + // |
|
| 339 | + // See http://schema.org/logo. |
|
| 340 | + if ( 1 !== preg_match( '~Organization$~', $type['uri'] ) ) { |
|
| 341 | + return; |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + // Get the publisher logo. |
|
| 345 | + $publisher_logo = $this->get_publisher_logo( $post->ID ); |
|
| 346 | + |
|
| 347 | + // Bail out if the publisher logo isn't set. |
|
| 348 | + if ( false === $publisher_logo ) { |
|
| 349 | + return; |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + // Copy over some useful properties. |
|
| 353 | + // |
|
| 354 | + // See https://developers.google.com/search/docs/data-types/articles. |
|
| 355 | + $params['publisher']['logo']['@type'] = 'ImageObject'; |
|
| 356 | + $params['publisher']['logo']['url'] = $publisher_logo['url']; |
|
| 357 | + |
|
| 358 | + // If you specify a "width" or "height" value you should leave out |
|
| 359 | + // 'px'. For example: "width":"4608px" should be "width":"4608". |
|
| 360 | + // |
|
| 361 | + // See https://github.com/insideout10/wordlift-plugin/issues/451. |
|
| 362 | + $params['publisher']['logo']['width'] = $publisher_logo['width']; |
|
| 363 | + $params['publisher']['logo']['height'] = $publisher_logo['height']; |
|
| 364 | + |
|
| 365 | + } |
|
| 366 | + |
|
| 367 | + /** |
|
| 368 | + * Get the publisher logo structure. |
|
| 369 | + * |
|
| 370 | + * The function returns false when the publisher logo cannot be determined, i.e.: |
|
| 371 | + * - the post has no featured image. |
|
| 372 | + * - the featured image has no file. |
|
| 373 | + * - a wp_image_editor instance cannot be instantiated on the original file or on the publisher logo file. |
|
| 374 | + * |
|
| 375 | + * @param int $post_id The post id. |
|
| 376 | + * |
|
| 377 | + * @return array|false Returns an array with the `url`, `width` and `height` for the publisher logo or false in case |
|
| 378 | + * of errors. |
|
| 379 | + * @since 3.19.2 |
|
| 380 | + * @see https://github.com/insideout10/wordlift-plugin/issues/823 related issue. |
|
| 381 | + * |
|
| 382 | + */ |
|
| 383 | + private function get_publisher_logo( $post_id ) { |
|
| 384 | + |
|
| 385 | + // Get the featured image for the post. |
|
| 386 | + $thumbnail_id = get_post_thumbnail_id( $post_id ); |
|
| 387 | + |
|
| 388 | + // Bail out if thumbnail not available. |
|
| 389 | + if ( empty( $thumbnail_id ) || 0 === $thumbnail_id ) { |
|
| 390 | + $this->log->info( "Featured image not set for post $post_id." ); |
|
| 391 | + |
|
| 392 | + return false; |
|
| 393 | + } |
|
| 394 | + |
|
| 395 | + // Get the uploads base URL. |
|
| 396 | + $uploads_dir = wp_upload_dir(); |
|
| 397 | + |
|
| 398 | + // Get the attachment metadata. |
|
| 399 | + $metadata = wp_get_attachment_metadata( $thumbnail_id ); |
|
| 400 | + |
|
| 401 | + // Bail out if the file isn't set. |
|
| 402 | + if ( ! isset( $metadata['file'] ) ) { |
|
| 403 | + $this->log->warn( "Featured image file not found for post $post_id." ); |
|
| 404 | + |
|
| 405 | + return false; |
|
| 406 | + } |
|
| 407 | + |
|
| 408 | + // Retrieve the relative filename, e.g. "2018/05/logo_publisher.png" |
|
| 409 | + $path = $uploads_dir['basedir'] . DIRECTORY_SEPARATOR . $metadata['file']; |
|
| 410 | + |
|
| 411 | + // Use image src, if local file does not exist. @see https://github.com/insideout10/wordlift-plugin/issues/1149 |
|
| 412 | + if ( ! file_exists( $path ) ) { |
|
| 413 | + $this->log->warn( "Featured image file $path doesn't exist for post $post_id." ); |
|
| 414 | + |
|
| 415 | + $attachment_image_src = wp_get_attachment_image_src( $thumbnail_id, '' ); |
|
| 416 | + if ( $attachment_image_src ) { |
|
| 417 | + return array( |
|
| 418 | + 'url' => $attachment_image_src[0], |
|
| 419 | + 'width' => $attachment_image_src[1], |
|
| 420 | + 'height' => $attachment_image_src[2], |
|
| 421 | + ); |
|
| 422 | + } |
|
| 423 | + |
|
| 424 | + // Bail out if we cant fetch wp_get_attachment_image_src |
|
| 425 | + return false; |
|
| 426 | + |
|
| 427 | + } |
|
| 428 | + |
|
| 429 | + // Try to get the image editor and bail out if the editor cannot be instantiated. |
|
| 430 | + $original_file_editor = wp_get_image_editor( $path ); |
|
| 431 | + if ( is_wp_error( $original_file_editor ) ) { |
|
| 432 | + $this->log->warn( "Cannot instantiate WP Image Editor on file $path for post $post_id." ); |
|
| 433 | + |
|
| 434 | + return false; |
|
| 435 | + } |
|
| 436 | + |
|
| 437 | + // Generate the publisher logo filename, we cannot use the `width` and `height` because we're scaling |
|
| 438 | + // and we don't actually know the end values. |
|
| 439 | + $publisher_logo_path = $original_file_editor->generate_filename( '-publisher-logo' ); |
|
| 440 | + |
|
| 441 | + // If the file doesn't exist yet, create it. |
|
| 442 | + if ( ! file_exists( $publisher_logo_path ) ) { |
|
| 443 | + $original_file_editor->resize( 600, 60 ); |
|
| 444 | + $original_file_editor->save( $publisher_logo_path ); |
|
| 445 | + } |
|
| 446 | + |
|
| 447 | + // Try to get the image editor and bail out if the editor cannot be instantiated. |
|
| 448 | + $publisher_logo_editor = wp_get_image_editor( $publisher_logo_path ); |
|
| 449 | + if ( is_wp_error( $publisher_logo_editor ) ) { |
|
| 450 | + $this->log->warn( "Cannot instantiate WP Image Editor on file $publisher_logo_path for post $post_id." ); |
|
| 451 | + |
|
| 452 | + return false; |
|
| 453 | + } |
|
| 454 | + |
|
| 455 | + // Get the actual size. |
|
| 456 | + $size = $publisher_logo_editor->get_size(); |
|
| 457 | + |
|
| 458 | + // Finally return the array with data. |
|
| 459 | + return array( |
|
| 460 | + 'url' => $uploads_dir['baseurl'] . substr( $publisher_logo_path, strlen( $uploads_dir['basedir'] ) ), |
|
| 461 | + 'width' => $size['width'], |
|
| 462 | + 'height' => $size['height'], |
|
| 463 | + ); |
|
| 464 | + } |
|
| 465 | 465 | |
| 466 | 466 | } |
@@ -50,13 +50,13 @@ discard block |
||
| 50 | 50 | * @since 3.10.0 |
| 51 | 51 | * |
| 52 | 52 | */ |
| 53 | - public function __construct( $entity_type_service, $entity_service, $user_service, $attachment_service, $configuration_service ) { |
|
| 54 | - parent::__construct( $entity_type_service, $entity_service, $user_service, $attachment_service ); |
|
| 53 | + public function __construct($entity_type_service, $entity_service, $user_service, $attachment_service, $configuration_service) { |
|
| 54 | + parent::__construct($entity_type_service, $entity_service, $user_service, $attachment_service); |
|
| 55 | 55 | |
| 56 | 56 | $this->configuration_service = $configuration_service; |
| 57 | 57 | |
| 58 | 58 | // Set a reference to the logger. |
| 59 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Post_To_Jsonld_Converter' ); |
|
| 59 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Post_To_Jsonld_Converter'); |
|
| 60 | 60 | |
| 61 | 61 | self::$instance = $this; |
| 62 | 62 | |
@@ -78,19 +78,19 @@ discard block |
||
| 78 | 78 | * @return array A JSON-LD array. |
| 79 | 79 | * @since 3.10.0 |
| 80 | 80 | */ |
| 81 | - public function convert( $post_id, &$references = array(), &$references_infos = array() ) { |
|
| 81 | + public function convert($post_id, &$references = array(), &$references_infos = array()) { |
|
| 82 | 82 | |
| 83 | 83 | // Get the post instance. |
| 84 | - if ( null === $post = get_post( $post_id ) ) { |
|
| 84 | + if (null === $post = get_post($post_id)) { |
|
| 85 | 85 | // Post not found. |
| 86 | 86 | return null; |
| 87 | 87 | } |
| 88 | 88 | |
| 89 | 89 | // Get the base JSON-LD and the list of entities referenced by this entity. |
| 90 | - $jsonld = parent::convert( $post_id, $references, $references_infos ); |
|
| 90 | + $jsonld = parent::convert($post_id, $references, $references_infos); |
|
| 91 | 91 | |
| 92 | 92 | // Set WebPage by default. |
| 93 | - if ( empty( $jsonld['@type'] ) ) { |
|
| 93 | + if (empty($jsonld['@type'])) { |
|
| 94 | 94 | $jsonld['@type'] = 'WebPage'; |
| 95 | 95 | } |
| 96 | 96 | |
@@ -107,19 +107,19 @@ discard block |
||
| 107 | 107 | */ |
| 108 | 108 | try { |
| 109 | 109 | $default_timezone = date_default_timezone_get(); |
| 110 | - $timezone = get_option( 'timezone_string' ); |
|
| 111 | - if ( ! empty( $timezone ) ) { |
|
| 112 | - date_default_timezone_set( $timezone ); |
|
| 113 | - $jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i:sP', false, $post ); |
|
| 114 | - $jsonld['dateModified'] = get_post_modified_time( 'Y-m-d\TH:i:sP', false, $post ); |
|
| 115 | - date_default_timezone_set( $default_timezone ); |
|
| 110 | + $timezone = get_option('timezone_string'); |
|
| 111 | + if ( ! empty($timezone)) { |
|
| 112 | + date_default_timezone_set($timezone); |
|
| 113 | + $jsonld['datePublished'] = get_post_time('Y-m-d\TH:i:sP', false, $post); |
|
| 114 | + $jsonld['dateModified'] = get_post_modified_time('Y-m-d\TH:i:sP', false, $post); |
|
| 115 | + date_default_timezone_set($default_timezone); |
|
| 116 | 116 | } else { |
| 117 | - $jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 118 | - $jsonld['dateModified'] = get_post_modified_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 117 | + $jsonld['datePublished'] = get_post_time('Y-m-d\TH:i', true, $post, false); |
|
| 118 | + $jsonld['dateModified'] = get_post_modified_time('Y-m-d\TH:i', true, $post, false); |
|
| 119 | 119 | } |
| 120 | - } catch ( Exception $e ) { |
|
| 121 | - $jsonld['datePublished'] = get_post_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 122 | - $jsonld['dateModified'] = get_post_modified_time( 'Y-m-d\TH:i', true, $post, false ); |
|
| 120 | + } catch (Exception $e) { |
|
| 121 | + $jsonld['datePublished'] = get_post_time('Y-m-d\TH:i', true, $post, false); |
|
| 122 | + $jsonld['dateModified'] = get_post_modified_time('Y-m-d\TH:i', true, $post, false); |
|
| 123 | 123 | } |
| 124 | 124 | |
| 125 | 125 | // Get the word count for the post. |
@@ -130,8 +130,8 @@ discard block |
||
| 130 | 130 | * |
| 131 | 131 | * @since 3.20.0 |
| 132 | 132 | */ |
| 133 | - if ( ! empty( $jsonld['@type'] ) && 'WebPage' !== $jsonld['@type'] ) { |
|
| 134 | - $post_adapter = new Wordlift_Post_Adapter( $post_id ); |
|
| 133 | + if ( ! empty($jsonld['@type']) && 'WebPage' !== $jsonld['@type']) { |
|
| 134 | + $post_adapter = new Wordlift_Post_Adapter($post_id); |
|
| 135 | 135 | $jsonld['wordCount'] = $post_adapter->word_count(); |
| 136 | 136 | } |
| 137 | 137 | |
@@ -142,53 +142,53 @@ discard block |
||
| 142 | 142 | * |
| 143 | 143 | * @since 3.27.2 |
| 144 | 144 | */ |
| 145 | - if ( ! empty( $jsonld['@type'] ) && 'WebPage' !== $jsonld['@type'] ) { |
|
| 146 | - $post_adapter = new Wordlift_Post_Adapter( $post_id ); |
|
| 145 | + if ( ! empty($jsonld['@type']) && 'WebPage' !== $jsonld['@type']) { |
|
| 146 | + $post_adapter = new Wordlift_Post_Adapter($post_id); |
|
| 147 | 147 | $keywords = $post_adapter->keywords(); |
| 148 | 148 | $article_section = $post_adapter->article_section(); |
| 149 | 149 | $comment_count = $post_adapter->comment_count(); |
| 150 | 150 | $locale = $post_adapter->locale(); |
| 151 | 151 | |
| 152 | - if ( isset( $keywords ) ) { |
|
| 152 | + if (isset($keywords)) { |
|
| 153 | 153 | $jsonld['keywords'] = $keywords; |
| 154 | 154 | } |
| 155 | - if ( isset( $article_section ) ) { |
|
| 155 | + if (isset($article_section)) { |
|
| 156 | 156 | $jsonld['articleSection'] = $article_section; |
| 157 | 157 | } |
| 158 | 158 | $jsonld['commentCount'] = $comment_count; |
| 159 | 159 | $jsonld['inLanguage'] = $locale; |
| 160 | - $post_adapter->add_mentions( $post_id, $references ); |
|
| 160 | + $post_adapter->add_mentions($post_id, $references); |
|
| 161 | 161 | } |
| 162 | 162 | |
| 163 | 163 | // Set the publisher. |
| 164 | - $this->set_publisher( $jsonld ); |
|
| 164 | + $this->set_publisher($jsonld); |
|
| 165 | 165 | |
| 166 | 166 | // Process the references if any. |
| 167 | - if ( 0 < count( $references ) ) { |
|
| 167 | + if (0 < count($references)) { |
|
| 168 | 168 | |
| 169 | 169 | // Prepare the `about` and `mentions` array. |
| 170 | 170 | $about = $mentions = array(); |
| 171 | 171 | |
| 172 | 172 | // If the entity is in the title, then it should be an `about`. |
| 173 | - foreach ( $references as $reference ) { |
|
| 173 | + foreach ($references as $reference) { |
|
| 174 | 174 | |
| 175 | 175 | // Get the entity labels. |
| 176 | - $labels = $this->entity_service->get_labels( $reference ); |
|
| 176 | + $labels = $this->entity_service->get_labels($reference); |
|
| 177 | 177 | |
| 178 | 178 | // Get the entity URI. |
| 179 | 179 | $item = array( |
| 180 | - '@id' => $this->entity_service->get_uri( $reference ), |
|
| 180 | + '@id' => $this->entity_service->get_uri($reference), |
|
| 181 | 181 | ); |
| 182 | 182 | |
| 183 | - $escaped_labels = array_map( function ( $value ) { |
|
| 184 | - return preg_quote( $value, '/' ); |
|
| 185 | - }, $labels ); |
|
| 183 | + $escaped_labels = array_map(function($value) { |
|
| 184 | + return preg_quote($value, '/'); |
|
| 185 | + }, $labels); |
|
| 186 | 186 | |
| 187 | 187 | // Check if the labels match any part of the title. |
| 188 | - $matches = 1 === preg_match( '/' . implode( '|', $escaped_labels ) . '/', $post->post_title ); |
|
| 188 | + $matches = 1 === preg_match('/'.implode('|', $escaped_labels).'/', $post->post_title); |
|
| 189 | 189 | |
| 190 | 190 | // If the title matches, assign the entity to the about, otherwise to the mentions. |
| 191 | - if ( $matches ) { |
|
| 191 | + if ($matches) { |
|
| 192 | 192 | $about[] = $item; |
| 193 | 193 | } else { |
| 194 | 194 | $mentions[] = $item; |
@@ -196,18 +196,18 @@ discard block |
||
| 196 | 196 | } |
| 197 | 197 | |
| 198 | 198 | // If we have abouts, assign them to the JSON-LD. |
| 199 | - if ( 0 < count( $about ) ) { |
|
| 199 | + if (0 < count($about)) { |
|
| 200 | 200 | $jsonld['about'] = $about; |
| 201 | 201 | } |
| 202 | 202 | |
| 203 | 203 | // If we have mentions, assign them to the JSON-LD. |
| 204 | - if ( 0 < count( $mentions ) ) { |
|
| 204 | + if (0 < count($mentions)) { |
|
| 205 | 205 | $jsonld['mentions'] = $mentions; |
| 206 | 206 | } |
| 207 | 207 | } |
| 208 | 208 | |
| 209 | 209 | // Finally set the author. |
| 210 | - $jsonld['author'] = $this->get_author( $post->post_author, $references ); |
|
| 210 | + $jsonld['author'] = $this->get_author($post->post_author, $references); |
|
| 211 | 211 | |
| 212 | 212 | /** |
| 213 | 213 | * Call the `wl_post_jsonld_array` filter. This filter allows 3rd parties to also modify the references. |
@@ -223,10 +223,10 @@ discard block |
||
| 223 | 223 | * |
| 224 | 224 | * @api |
| 225 | 225 | */ |
| 226 | - $ret_val = apply_filters( 'wl_post_jsonld_array', array( |
|
| 226 | + $ret_val = apply_filters('wl_post_jsonld_array', array( |
|
| 227 | 227 | 'jsonld' => $jsonld, |
| 228 | 228 | 'references' => $references, |
| 229 | - ), $post_id ); |
|
| 229 | + ), $post_id); |
|
| 230 | 230 | $jsonld = $ret_val['jsonld']; |
| 231 | 231 | $references = $ret_val['references']; |
| 232 | 232 | |
@@ -241,7 +241,7 @@ discard block |
||
| 241 | 241 | * |
| 242 | 242 | * @api |
| 243 | 243 | */ |
| 244 | - return apply_filters( 'wl_post_jsonld', $jsonld, $post_id, $references ); |
|
| 244 | + return apply_filters('wl_post_jsonld', $jsonld, $post_id, $references); |
|
| 245 | 245 | } |
| 246 | 246 | |
| 247 | 247 | /** |
@@ -257,18 +257,18 @@ discard block |
||
| 257 | 257 | * @since 3.14.0 |
| 258 | 258 | * |
| 259 | 259 | */ |
| 260 | - private function get_author( $author_id, &$references ) { |
|
| 260 | + private function get_author($author_id, &$references) { |
|
| 261 | 261 | |
| 262 | 262 | // Get the entity bound to this user. |
| 263 | - $entity_id = $this->user_service->get_entity( $author_id ); |
|
| 263 | + $entity_id = $this->user_service->get_entity($author_id); |
|
| 264 | 264 | |
| 265 | 265 | // If there's no entity bound return a simple author structure. |
| 266 | - if ( empty( $entity_id ) || 'publish' !== get_post_status( $entity_id ) ) { |
|
| 266 | + if (empty($entity_id) || 'publish' !== get_post_status($entity_id)) { |
|
| 267 | 267 | |
| 268 | - $author = get_the_author_meta( 'display_name', $author_id ); |
|
| 269 | - $author_first_name = get_the_author_meta( 'first_name', $author_id ); |
|
| 270 | - $author_last_name = get_the_author_meta( 'last_name', $author_id ); |
|
| 271 | - $author_uri = $this->user_service->get_uri( $author_id ); |
|
| 268 | + $author = get_the_author_meta('display_name', $author_id); |
|
| 269 | + $author_first_name = get_the_author_meta('first_name', $author_id); |
|
| 270 | + $author_last_name = get_the_author_meta('last_name', $author_id); |
|
| 271 | + $author_uri = $this->user_service->get_uri($author_id); |
|
| 272 | 272 | |
| 273 | 273 | return array( |
| 274 | 274 | '@type' => 'Person', |
@@ -280,7 +280,7 @@ discard block |
||
| 280 | 280 | } |
| 281 | 281 | |
| 282 | 282 | // Add the author to the references. |
| 283 | - $author_uri = $this->entity_service->get_uri( $entity_id ); |
|
| 283 | + $author_uri = $this->entity_service->get_uri($entity_id); |
|
| 284 | 284 | $references[] = $entity_id; |
| 285 | 285 | |
| 286 | 286 | // Return the JSON-LD for the referenced entity. |
@@ -297,39 +297,39 @@ discard block |
||
| 297 | 297 | * @since 3.10.0 |
| 298 | 298 | * |
| 299 | 299 | */ |
| 300 | - protected function set_publisher( &$params ) { |
|
| 300 | + protected function set_publisher(&$params) { |
|
| 301 | 301 | |
| 302 | 302 | // If the publisher id isn't set don't do anything. |
| 303 | - if ( null === $publisher_id = $this->configuration_service->get_publisher_id() ) { |
|
| 303 | + if (null === $publisher_id = $this->configuration_service->get_publisher_id()) { |
|
| 304 | 304 | return; |
| 305 | 305 | } |
| 306 | 306 | |
| 307 | 307 | // Get the post instance. |
| 308 | - if ( null === $post = get_post( $publisher_id ) ) { |
|
| 308 | + if (null === $post = get_post($publisher_id)) { |
|
| 309 | 309 | // Publisher not found. |
| 310 | 310 | return; |
| 311 | 311 | } |
| 312 | 312 | |
| 313 | 313 | // Get the item id. |
| 314 | - $id = $this->entity_service->get_uri( $publisher_id ); |
|
| 314 | + $id = $this->entity_service->get_uri($publisher_id); |
|
| 315 | 315 | |
| 316 | 316 | // Get the type. |
| 317 | - $type = $this->entity_type_service->get( $publisher_id ); |
|
| 317 | + $type = $this->entity_type_service->get($publisher_id); |
|
| 318 | 318 | |
| 319 | 319 | // Get the name. |
| 320 | 320 | $name = $post->post_title; |
| 321 | 321 | |
| 322 | 322 | // Set the publisher data. |
| 323 | 323 | $params['publisher'] = array( |
| 324 | - '@type' => $this->relative_to_context( $type['uri'] ), |
|
| 324 | + '@type' => $this->relative_to_context($type['uri']), |
|
| 325 | 325 | '@id' => $id, |
| 326 | 326 | 'name' => $name, |
| 327 | 327 | ); |
| 328 | 328 | |
| 329 | 329 | // Add the sameAs values associated with the publisher. |
| 330 | 330 | $storage_factory = Wordlift_Storage_Factory::get_instance(); |
| 331 | - $sameas = $storage_factory->post_meta( Wordlift_Schema_Service::FIELD_SAME_AS )->get( $publisher_id ); |
|
| 332 | - if ( ! empty( $sameas ) ) { |
|
| 331 | + $sameas = $storage_factory->post_meta(Wordlift_Schema_Service::FIELD_SAME_AS)->get($publisher_id); |
|
| 332 | + if ( ! empty($sameas)) { |
|
| 333 | 333 | $params['publisher']['sameAs'] = $sameas; |
| 334 | 334 | } |
| 335 | 335 | |
@@ -337,15 +337,15 @@ discard block |
||
| 337 | 337 | // support the logo property. |
| 338 | 338 | // |
| 339 | 339 | // See http://schema.org/logo. |
| 340 | - if ( 1 !== preg_match( '~Organization$~', $type['uri'] ) ) { |
|
| 340 | + if (1 !== preg_match('~Organization$~', $type['uri'])) { |
|
| 341 | 341 | return; |
| 342 | 342 | } |
| 343 | 343 | |
| 344 | 344 | // Get the publisher logo. |
| 345 | - $publisher_logo = $this->get_publisher_logo( $post->ID ); |
|
| 345 | + $publisher_logo = $this->get_publisher_logo($post->ID); |
|
| 346 | 346 | |
| 347 | 347 | // Bail out if the publisher logo isn't set. |
| 348 | - if ( false === $publisher_logo ) { |
|
| 348 | + if (false === $publisher_logo) { |
|
| 349 | 349 | return; |
| 350 | 350 | } |
| 351 | 351 | |
@@ -380,14 +380,14 @@ discard block |
||
| 380 | 380 | * @see https://github.com/insideout10/wordlift-plugin/issues/823 related issue. |
| 381 | 381 | * |
| 382 | 382 | */ |
| 383 | - private function get_publisher_logo( $post_id ) { |
|
| 383 | + private function get_publisher_logo($post_id) { |
|
| 384 | 384 | |
| 385 | 385 | // Get the featured image for the post. |
| 386 | - $thumbnail_id = get_post_thumbnail_id( $post_id ); |
|
| 386 | + $thumbnail_id = get_post_thumbnail_id($post_id); |
|
| 387 | 387 | |
| 388 | 388 | // Bail out if thumbnail not available. |
| 389 | - if ( empty( $thumbnail_id ) || 0 === $thumbnail_id ) { |
|
| 390 | - $this->log->info( "Featured image not set for post $post_id." ); |
|
| 389 | + if (empty($thumbnail_id) || 0 === $thumbnail_id) { |
|
| 390 | + $this->log->info("Featured image not set for post $post_id."); |
|
| 391 | 391 | |
| 392 | 392 | return false; |
| 393 | 393 | } |
@@ -396,24 +396,24 @@ discard block |
||
| 396 | 396 | $uploads_dir = wp_upload_dir(); |
| 397 | 397 | |
| 398 | 398 | // Get the attachment metadata. |
| 399 | - $metadata = wp_get_attachment_metadata( $thumbnail_id ); |
|
| 399 | + $metadata = wp_get_attachment_metadata($thumbnail_id); |
|
| 400 | 400 | |
| 401 | 401 | // Bail out if the file isn't set. |
| 402 | - if ( ! isset( $metadata['file'] ) ) { |
|
| 403 | - $this->log->warn( "Featured image file not found for post $post_id." ); |
|
| 402 | + if ( ! isset($metadata['file'])) { |
|
| 403 | + $this->log->warn("Featured image file not found for post $post_id."); |
|
| 404 | 404 | |
| 405 | 405 | return false; |
| 406 | 406 | } |
| 407 | 407 | |
| 408 | 408 | // Retrieve the relative filename, e.g. "2018/05/logo_publisher.png" |
| 409 | - $path = $uploads_dir['basedir'] . DIRECTORY_SEPARATOR . $metadata['file']; |
|
| 409 | + $path = $uploads_dir['basedir'].DIRECTORY_SEPARATOR.$metadata['file']; |
|
| 410 | 410 | |
| 411 | 411 | // Use image src, if local file does not exist. @see https://github.com/insideout10/wordlift-plugin/issues/1149 |
| 412 | - if ( ! file_exists( $path ) ) { |
|
| 413 | - $this->log->warn( "Featured image file $path doesn't exist for post $post_id." ); |
|
| 412 | + if ( ! file_exists($path)) { |
|
| 413 | + $this->log->warn("Featured image file $path doesn't exist for post $post_id."); |
|
| 414 | 414 | |
| 415 | - $attachment_image_src = wp_get_attachment_image_src( $thumbnail_id, '' ); |
|
| 416 | - if ( $attachment_image_src ) { |
|
| 415 | + $attachment_image_src = wp_get_attachment_image_src($thumbnail_id, ''); |
|
| 416 | + if ($attachment_image_src) { |
|
| 417 | 417 | return array( |
| 418 | 418 | 'url' => $attachment_image_src[0], |
| 419 | 419 | 'width' => $attachment_image_src[1], |
@@ -427,27 +427,27 @@ discard block |
||
| 427 | 427 | } |
| 428 | 428 | |
| 429 | 429 | // Try to get the image editor and bail out if the editor cannot be instantiated. |
| 430 | - $original_file_editor = wp_get_image_editor( $path ); |
|
| 431 | - if ( is_wp_error( $original_file_editor ) ) { |
|
| 432 | - $this->log->warn( "Cannot instantiate WP Image Editor on file $path for post $post_id." ); |
|
| 430 | + $original_file_editor = wp_get_image_editor($path); |
|
| 431 | + if (is_wp_error($original_file_editor)) { |
|
| 432 | + $this->log->warn("Cannot instantiate WP Image Editor on file $path for post $post_id."); |
|
| 433 | 433 | |
| 434 | 434 | return false; |
| 435 | 435 | } |
| 436 | 436 | |
| 437 | 437 | // Generate the publisher logo filename, we cannot use the `width` and `height` because we're scaling |
| 438 | 438 | // and we don't actually know the end values. |
| 439 | - $publisher_logo_path = $original_file_editor->generate_filename( '-publisher-logo' ); |
|
| 439 | + $publisher_logo_path = $original_file_editor->generate_filename('-publisher-logo'); |
|
| 440 | 440 | |
| 441 | 441 | // If the file doesn't exist yet, create it. |
| 442 | - if ( ! file_exists( $publisher_logo_path ) ) { |
|
| 443 | - $original_file_editor->resize( 600, 60 ); |
|
| 444 | - $original_file_editor->save( $publisher_logo_path ); |
|
| 442 | + if ( ! file_exists($publisher_logo_path)) { |
|
| 443 | + $original_file_editor->resize(600, 60); |
|
| 444 | + $original_file_editor->save($publisher_logo_path); |
|
| 445 | 445 | } |
| 446 | 446 | |
| 447 | 447 | // Try to get the image editor and bail out if the editor cannot be instantiated. |
| 448 | - $publisher_logo_editor = wp_get_image_editor( $publisher_logo_path ); |
|
| 449 | - if ( is_wp_error( $publisher_logo_editor ) ) { |
|
| 450 | - $this->log->warn( "Cannot instantiate WP Image Editor on file $publisher_logo_path for post $post_id." ); |
|
| 448 | + $publisher_logo_editor = wp_get_image_editor($publisher_logo_path); |
|
| 449 | + if (is_wp_error($publisher_logo_editor)) { |
|
| 450 | + $this->log->warn("Cannot instantiate WP Image Editor on file $publisher_logo_path for post $post_id."); |
|
| 451 | 451 | |
| 452 | 452 | return false; |
| 453 | 453 | } |
@@ -457,7 +457,7 @@ discard block |
||
| 457 | 457 | |
| 458 | 458 | // Finally return the array with data. |
| 459 | 459 | return array( |
| 460 | - 'url' => $uploads_dir['baseurl'] . substr( $publisher_logo_path, strlen( $uploads_dir['basedir'] ) ), |
|
| 460 | + 'url' => $uploads_dir['baseurl'].substr($publisher_logo_path, strlen($uploads_dir['basedir'])), |
|
| 461 | 461 | 'width' => $size['width'], |
| 462 | 462 | 'height' => $size['height'], |
| 463 | 463 | ); |