@@ -21,309 +21,309 @@ |
||
| 21 | 21 | */ |
| 22 | 22 | class Wordlift_Term_JsonLd_Adapter { |
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 26 | - * |
|
| 27 | - * @since 3.20.0 |
|
| 28 | - * @access private |
|
| 29 | - * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 30 | - */ |
|
| 31 | - private $entity_uri_service; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * Instance. |
|
| 35 | - * |
|
| 36 | - * @var Wordlift_Term_JsonLd_Adapter |
|
| 37 | - */ |
|
| 38 | - private static $instance; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * @var Wordlift_Post_Converter |
|
| 42 | - */ |
|
| 43 | - private $post_id_to_jsonld_converter; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Wordlift_Term_JsonLd_Adapter constructor. |
|
| 47 | - * |
|
| 48 | - * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 49 | - * @param \Wordlift_Post_Converter $post_id_to_jsonld_converter The {@link Wordlift_Post_Converter} instance. |
|
| 50 | - * |
|
| 51 | - * @since 3.20.0 |
|
| 52 | - */ |
|
| 53 | - public function __construct( $entity_uri_service, $post_id_to_jsonld_converter ) { |
|
| 54 | - |
|
| 55 | - add_action( 'wp_head', array( $this, 'wp_head' ) ); |
|
| 56 | - |
|
| 57 | - $this->entity_uri_service = $entity_uri_service; |
|
| 58 | - $this->post_id_to_jsonld_converter = $post_id_to_jsonld_converter; |
|
| 59 | - |
|
| 60 | - self::$instance = $this; |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Get instance. |
|
| 65 | - * |
|
| 66 | - * @return Wordlift_Term_JsonLd_Adapter|static |
|
| 67 | - */ |
|
| 68 | - public static function get_instance() { |
|
| 69 | - return self::$instance; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * Adds carousel json ld data to term page if the conditions match |
|
| 74 | - * |
|
| 75 | - * @return array|boolean |
|
| 76 | - */ |
|
| 77 | - public function get_carousel_jsonld( $id = null ) { |
|
| 78 | - $posts = $this->get_posts( $id ); |
|
| 79 | - $post_jsonld = array(); |
|
| 80 | - if ( ! is_array( $posts ) || count( $posts ) < 2 ) { |
|
| 81 | - // Bail out if no posts are present. |
|
| 82 | - return false; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - if ( $id !== null ) { |
|
| 86 | - $term = get_term( $id ); |
|
| 87 | - $post_jsonld['description'] = wp_strip_all_tags( strip_shortcodes( $term->description ) ); |
|
| 88 | - $thumbnail_id = get_term_meta( $id, 'thumbnail_id', true ); |
|
| 89 | - if ( ! empty( $thumbnail_id ) ) { |
|
| 90 | - $post_jsonld['image'] = wp_get_attachment_url( $thumbnail_id ); |
|
| 91 | - } |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - // More than 2 items are present, so construct the post_jsonld data |
|
| 95 | - $post_jsonld['@context'] = 'https://schema.org'; |
|
| 96 | - $post_jsonld['@type'] = 'ItemList'; |
|
| 97 | - $post_jsonld['url'] = $this->get_term_url( $id ); |
|
| 98 | - $post_jsonld['itemListElement'] = array(); |
|
| 99 | - $position = 1; |
|
| 100 | - |
|
| 101 | - foreach ( $posts as $post_id ) { |
|
| 102 | - $result = array( |
|
| 103 | - '@type' => 'ListItem', |
|
| 104 | - 'position' => $position, |
|
| 105 | - /** |
|
| 106 | - * We can't use `item` here unless we change the URL for the item to point to the current page. |
|
| 107 | - * |
|
| 108 | - * See https://developers.google.com/search/docs/data-types/carousel |
|
| 109 | - */ |
|
| 110 | - 'url' => apply_filters( 'wl_carousel_post_list_item_url', get_permalink( $post_id ), $post_id ), |
|
| 111 | - ); |
|
| 112 | - array_push( $post_jsonld['itemListElement'], $result ); |
|
| 113 | - ++ $position; |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - return $post_jsonld; |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * Get posts. |
|
| 121 | - * |
|
| 122 | - * @param $id |
|
| 123 | - * |
|
| 124 | - * @return array|int[]|string[]|WP_Error|null |
|
| 125 | - */ |
|
| 126 | - private function get_posts( $id ) { |
|
| 127 | - global $wp_query; |
|
| 128 | - |
|
| 129 | - if ( $wp_query->posts !== null ) { |
|
| 130 | - return array_map( |
|
| 131 | - function ( $post ) { |
|
| 132 | - return $post->ID; |
|
| 133 | - }, |
|
| 134 | - $wp_query->posts |
|
| 135 | - ); |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - if ( $id === null ) { |
|
| 139 | - return null; |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - $term = get_term( $id ); |
|
| 143 | - |
|
| 144 | - return get_objects_in_term( $id, $term->taxonomy ); |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * Hook to `wp_head` to print the JSON-LD. |
|
| 149 | - * |
|
| 150 | - * @since 3.20.0 |
|
| 151 | - */ |
|
| 152 | - public function wp_head() { |
|
| 153 | - $query_object = get_queried_object(); |
|
| 154 | - |
|
| 155 | - // Check if it is a term page. |
|
| 156 | - if ( ! $query_object instanceof WP_Term ) { |
|
| 157 | - return; |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - // Bail out if `wl_jsonld_enabled` isn't enabled. |
|
| 161 | - if ( ! apply_filters( 'wl_jsonld_enabled', true ) ) { |
|
| 162 | - return; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - $term_id = $query_object->term_id; |
|
| 166 | - |
|
| 167 | - $jsonld = $this->get( $term_id, Jsonld_Context_Enum::PAGE ); |
|
| 168 | - |
|
| 169 | - // Bail out if the JSON-LD is empty. |
|
| 170 | - if ( empty( $jsonld ) ) { |
|
| 171 | - return; |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - $jsonld_string = wp_json_encode( $jsonld ); |
|
| 175 | - |
|
| 176 | - $jsonld_term_html_output = '<script type="application/ld+json" id="wl-jsonld-term">' . $jsonld_string . '</script>'; |
|
| 177 | - $jsonld_term_html_output = apply_filters( 'wl_jsonld_term_html_output', $jsonld_term_html_output, $term_id ); |
|
| 178 | - |
|
| 179 | - echo $jsonld_term_html_output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- It's an application/ld+json output. |
|
| 180 | - |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - /** |
|
| 184 | - * Get. |
|
| 185 | - * |
|
| 186 | - * @param $id |
|
| 187 | - * @param $context |
|
| 188 | - * @param $is_recursive_call |
|
| 189 | - * |
|
| 190 | - * @return array |
|
| 191 | - */ |
|
| 192 | - public function get( $id, $context, $is_recursive_call = false ) { |
|
| 193 | - /** |
|
| 194 | - * Support for carousel rich snippet, get jsonld data present |
|
| 195 | - * for all the posts shown in the term page, and add the jsonld data |
|
| 196 | - * to list |
|
| 197 | - * |
|
| 198 | - * see here: https://developers.google.com/search/docs/data-types/carousel |
|
| 199 | - * |
|
| 200 | - * @since 3.26.0 |
|
| 201 | - */ |
|
| 202 | - $jsonld_array = array(); |
|
| 203 | - |
|
| 204 | - if ( Jsonld_Context_Enum::PAGE === $context ) { |
|
| 205 | - $carousel_data = $this->get_carousel_jsonld( $id ); |
|
| 206 | - if ( $carousel_data ) { |
|
| 207 | - $jsonld_array[] = $carousel_data; |
|
| 208 | - } |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - $entities_jsonld_array = $this->get_entity_jsonld( $id, $context ); |
|
| 212 | - |
|
| 213 | - $result = array( |
|
| 214 | - 'jsonld' => array_merge( $jsonld_array, $entities_jsonld_array ), |
|
| 215 | - 'references' => array(), |
|
| 216 | - ); |
|
| 217 | - |
|
| 218 | - /** |
|
| 219 | - * @since 3.26.3 |
|
| 220 | - * Filter: wl_term_jsonld_array |
|
| 221 | - * @var $id int Term id |
|
| 222 | - * @var $jsonld_array array An array containing jsonld for term and entities. |
|
| 223 | - * @var $context int A context for the JSON-LD generation, valid values in Jsonld_Context_Enum |
|
| 224 | - */ |
|
| 225 | - $arr = apply_filters( 'wl_term_jsonld_array', $result, $id, $context ); |
|
| 226 | - |
|
| 227 | - $references = array(); |
|
| 228 | - |
|
| 229 | - // Don't expand nested references, it will lead to an infinite loop. |
|
| 230 | - if ( ! $is_recursive_call ) { |
|
| 231 | - /** |
|
| 232 | - * @since 3.32.0 |
|
| 233 | - * Expand the references returned by this filter. |
|
| 234 | - */ |
|
| 235 | - $references = $this->expand_references( $arr['references'] ); |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - $jsonld_array = array_merge( $arr['jsonld'], $references ); |
|
| 239 | - |
|
| 240 | - return $jsonld_array; |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - /** |
|
| 244 | - * Get term url. |
|
| 245 | - * |
|
| 246 | - * @param $id |
|
| 247 | - * |
|
| 248 | - * @return array|false|int|mixed|string|WP_Error|WP_Term|null |
|
| 249 | - */ |
|
| 250 | - private function get_term_url( $id ) { |
|
| 251 | - if ( null === $id ) { |
|
| 252 | - return isset( $_SERVER['REQUEST_URI'] ) ? filter_var( wp_unslash( $_SERVER['REQUEST_URI'] ), FILTER_SANITIZE_URL ) : ''; |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - $maybe_url = get_term_meta( $id, Wordlift_Url_Property_Service::META_KEY, true ); |
|
| 256 | - if ( ! empty( $maybe_url ) ) { |
|
| 257 | - return $maybe_url; |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - return get_term_link( $id ); |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - /** |
|
| 264 | - * Return jsonld for entities bound to terms. |
|
| 265 | - * |
|
| 266 | - * @param int $term_id Term ID. |
|
| 267 | - * @param int $context A context for the JSON-LD generation, valid values in Jsonld_Context_Enum. |
|
| 268 | - * |
|
| 269 | - * @return array |
|
| 270 | - */ |
|
| 271 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 272 | - private function get_entity_jsonld( $term_id, $context ) { |
|
| 273 | - |
|
| 274 | - // The `_wl_entity_id` are URIs. |
|
| 275 | - $entity_ids = get_term_meta( $term_id, '_wl_entity_id' ); |
|
| 276 | - $entity_uri_service = $this->entity_uri_service; |
|
| 277 | - |
|
| 278 | - $wordlift_jsonld_service = Wordlift_Jsonld_Service::get_instance(); |
|
| 279 | - |
|
| 280 | - $local_entity_ids = array_filter( |
|
| 281 | - $entity_ids, |
|
| 282 | - function ( $uri ) use ( $entity_uri_service ) { |
|
| 283 | - return $entity_uri_service->is_internal( $uri ); |
|
| 284 | - } |
|
| 285 | - ); |
|
| 286 | - |
|
| 287 | - // Bail out if there are no entities. |
|
| 288 | - if ( empty( $local_entity_ids ) ) { |
|
| 289 | - return array(); |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - $post = $this->entity_uri_service->get_entity( array_shift( $local_entity_ids ) ); |
|
| 293 | - $entities_jsonld = $wordlift_jsonld_service->get_jsonld( false, $post->ID ); |
|
| 294 | - // Reset the `url` to the term page. |
|
| 295 | - $entities_jsonld[0]['url'] = get_term_link( $term_id ); |
|
| 296 | - |
|
| 297 | - return $entities_jsonld; |
|
| 298 | - } |
|
| 299 | - |
|
| 300 | - /** |
|
| 301 | - * @param $references |
|
| 302 | - * |
|
| 303 | - * @return array |
|
| 304 | - */ |
|
| 305 | - private function expand_references( $references ) { |
|
| 306 | - if ( ! is_array( $references ) ) { |
|
| 307 | - return array(); |
|
| 308 | - } |
|
| 309 | - $references_jsonld = array(); |
|
| 310 | - // Expand the references. |
|
| 311 | - foreach ( $references as $reference ) { |
|
| 312 | - if ( $reference instanceof Term_Reference ) { |
|
| 313 | - // Second level references won't be expanded. |
|
| 314 | - $references_jsonld[] = current( $this->get( $reference->get_id(), Jsonld_Context_Enum::UNKNOWN, true ) ); |
|
| 315 | - } elseif ( is_numeric( $reference ) ) { |
|
| 316 | - $ref_2 = array(); |
|
| 317 | - $ref_info_2 = array(); |
|
| 318 | - $references_jsonld[] = $this->post_id_to_jsonld_converter->convert( $reference, $ref_2, $ref_info_2, new Relations() ); |
|
| 319 | - } elseif ( $reference instanceof Post_Reference ) { |
|
| 320 | - $ref_2 = array(); |
|
| 321 | - $ref_info_2 = array(); |
|
| 322 | - $references_jsonld[] = $this->post_id_to_jsonld_converter->convert( $reference->get_id(), $ref_2, $ref_info_2, new Relations() ); |
|
| 323 | - } |
|
| 324 | - } |
|
| 325 | - |
|
| 326 | - return $references_jsonld; |
|
| 327 | - } |
|
| 24 | + /** |
|
| 25 | + * The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 26 | + * |
|
| 27 | + * @since 3.20.0 |
|
| 28 | + * @access private |
|
| 29 | + * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 30 | + */ |
|
| 31 | + private $entity_uri_service; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * Instance. |
|
| 35 | + * |
|
| 36 | + * @var Wordlift_Term_JsonLd_Adapter |
|
| 37 | + */ |
|
| 38 | + private static $instance; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * @var Wordlift_Post_Converter |
|
| 42 | + */ |
|
| 43 | + private $post_id_to_jsonld_converter; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Wordlift_Term_JsonLd_Adapter constructor. |
|
| 47 | + * |
|
| 48 | + * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 49 | + * @param \Wordlift_Post_Converter $post_id_to_jsonld_converter The {@link Wordlift_Post_Converter} instance. |
|
| 50 | + * |
|
| 51 | + * @since 3.20.0 |
|
| 52 | + */ |
|
| 53 | + public function __construct( $entity_uri_service, $post_id_to_jsonld_converter ) { |
|
| 54 | + |
|
| 55 | + add_action( 'wp_head', array( $this, 'wp_head' ) ); |
|
| 56 | + |
|
| 57 | + $this->entity_uri_service = $entity_uri_service; |
|
| 58 | + $this->post_id_to_jsonld_converter = $post_id_to_jsonld_converter; |
|
| 59 | + |
|
| 60 | + self::$instance = $this; |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Get instance. |
|
| 65 | + * |
|
| 66 | + * @return Wordlift_Term_JsonLd_Adapter|static |
|
| 67 | + */ |
|
| 68 | + public static function get_instance() { |
|
| 69 | + return self::$instance; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * Adds carousel json ld data to term page if the conditions match |
|
| 74 | + * |
|
| 75 | + * @return array|boolean |
|
| 76 | + */ |
|
| 77 | + public function get_carousel_jsonld( $id = null ) { |
|
| 78 | + $posts = $this->get_posts( $id ); |
|
| 79 | + $post_jsonld = array(); |
|
| 80 | + if ( ! is_array( $posts ) || count( $posts ) < 2 ) { |
|
| 81 | + // Bail out if no posts are present. |
|
| 82 | + return false; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + if ( $id !== null ) { |
|
| 86 | + $term = get_term( $id ); |
|
| 87 | + $post_jsonld['description'] = wp_strip_all_tags( strip_shortcodes( $term->description ) ); |
|
| 88 | + $thumbnail_id = get_term_meta( $id, 'thumbnail_id', true ); |
|
| 89 | + if ( ! empty( $thumbnail_id ) ) { |
|
| 90 | + $post_jsonld['image'] = wp_get_attachment_url( $thumbnail_id ); |
|
| 91 | + } |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + // More than 2 items are present, so construct the post_jsonld data |
|
| 95 | + $post_jsonld['@context'] = 'https://schema.org'; |
|
| 96 | + $post_jsonld['@type'] = 'ItemList'; |
|
| 97 | + $post_jsonld['url'] = $this->get_term_url( $id ); |
|
| 98 | + $post_jsonld['itemListElement'] = array(); |
|
| 99 | + $position = 1; |
|
| 100 | + |
|
| 101 | + foreach ( $posts as $post_id ) { |
|
| 102 | + $result = array( |
|
| 103 | + '@type' => 'ListItem', |
|
| 104 | + 'position' => $position, |
|
| 105 | + /** |
|
| 106 | + * We can't use `item` here unless we change the URL for the item to point to the current page. |
|
| 107 | + * |
|
| 108 | + * See https://developers.google.com/search/docs/data-types/carousel |
|
| 109 | + */ |
|
| 110 | + 'url' => apply_filters( 'wl_carousel_post_list_item_url', get_permalink( $post_id ), $post_id ), |
|
| 111 | + ); |
|
| 112 | + array_push( $post_jsonld['itemListElement'], $result ); |
|
| 113 | + ++ $position; |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + return $post_jsonld; |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * Get posts. |
|
| 121 | + * |
|
| 122 | + * @param $id |
|
| 123 | + * |
|
| 124 | + * @return array|int[]|string[]|WP_Error|null |
|
| 125 | + */ |
|
| 126 | + private function get_posts( $id ) { |
|
| 127 | + global $wp_query; |
|
| 128 | + |
|
| 129 | + if ( $wp_query->posts !== null ) { |
|
| 130 | + return array_map( |
|
| 131 | + function ( $post ) { |
|
| 132 | + return $post->ID; |
|
| 133 | + }, |
|
| 134 | + $wp_query->posts |
|
| 135 | + ); |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + if ( $id === null ) { |
|
| 139 | + return null; |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + $term = get_term( $id ); |
|
| 143 | + |
|
| 144 | + return get_objects_in_term( $id, $term->taxonomy ); |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * Hook to `wp_head` to print the JSON-LD. |
|
| 149 | + * |
|
| 150 | + * @since 3.20.0 |
|
| 151 | + */ |
|
| 152 | + public function wp_head() { |
|
| 153 | + $query_object = get_queried_object(); |
|
| 154 | + |
|
| 155 | + // Check if it is a term page. |
|
| 156 | + if ( ! $query_object instanceof WP_Term ) { |
|
| 157 | + return; |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + // Bail out if `wl_jsonld_enabled` isn't enabled. |
|
| 161 | + if ( ! apply_filters( 'wl_jsonld_enabled', true ) ) { |
|
| 162 | + return; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + $term_id = $query_object->term_id; |
|
| 166 | + |
|
| 167 | + $jsonld = $this->get( $term_id, Jsonld_Context_Enum::PAGE ); |
|
| 168 | + |
|
| 169 | + // Bail out if the JSON-LD is empty. |
|
| 170 | + if ( empty( $jsonld ) ) { |
|
| 171 | + return; |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + $jsonld_string = wp_json_encode( $jsonld ); |
|
| 175 | + |
|
| 176 | + $jsonld_term_html_output = '<script type="application/ld+json" id="wl-jsonld-term">' . $jsonld_string . '</script>'; |
|
| 177 | + $jsonld_term_html_output = apply_filters( 'wl_jsonld_term_html_output', $jsonld_term_html_output, $term_id ); |
|
| 178 | + |
|
| 179 | + echo $jsonld_term_html_output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- It's an application/ld+json output. |
|
| 180 | + |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + /** |
|
| 184 | + * Get. |
|
| 185 | + * |
|
| 186 | + * @param $id |
|
| 187 | + * @param $context |
|
| 188 | + * @param $is_recursive_call |
|
| 189 | + * |
|
| 190 | + * @return array |
|
| 191 | + */ |
|
| 192 | + public function get( $id, $context, $is_recursive_call = false ) { |
|
| 193 | + /** |
|
| 194 | + * Support for carousel rich snippet, get jsonld data present |
|
| 195 | + * for all the posts shown in the term page, and add the jsonld data |
|
| 196 | + * to list |
|
| 197 | + * |
|
| 198 | + * see here: https://developers.google.com/search/docs/data-types/carousel |
|
| 199 | + * |
|
| 200 | + * @since 3.26.0 |
|
| 201 | + */ |
|
| 202 | + $jsonld_array = array(); |
|
| 203 | + |
|
| 204 | + if ( Jsonld_Context_Enum::PAGE === $context ) { |
|
| 205 | + $carousel_data = $this->get_carousel_jsonld( $id ); |
|
| 206 | + if ( $carousel_data ) { |
|
| 207 | + $jsonld_array[] = $carousel_data; |
|
| 208 | + } |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + $entities_jsonld_array = $this->get_entity_jsonld( $id, $context ); |
|
| 212 | + |
|
| 213 | + $result = array( |
|
| 214 | + 'jsonld' => array_merge( $jsonld_array, $entities_jsonld_array ), |
|
| 215 | + 'references' => array(), |
|
| 216 | + ); |
|
| 217 | + |
|
| 218 | + /** |
|
| 219 | + * @since 3.26.3 |
|
| 220 | + * Filter: wl_term_jsonld_array |
|
| 221 | + * @var $id int Term id |
|
| 222 | + * @var $jsonld_array array An array containing jsonld for term and entities. |
|
| 223 | + * @var $context int A context for the JSON-LD generation, valid values in Jsonld_Context_Enum |
|
| 224 | + */ |
|
| 225 | + $arr = apply_filters( 'wl_term_jsonld_array', $result, $id, $context ); |
|
| 226 | + |
|
| 227 | + $references = array(); |
|
| 228 | + |
|
| 229 | + // Don't expand nested references, it will lead to an infinite loop. |
|
| 230 | + if ( ! $is_recursive_call ) { |
|
| 231 | + /** |
|
| 232 | + * @since 3.32.0 |
|
| 233 | + * Expand the references returned by this filter. |
|
| 234 | + */ |
|
| 235 | + $references = $this->expand_references( $arr['references'] ); |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + $jsonld_array = array_merge( $arr['jsonld'], $references ); |
|
| 239 | + |
|
| 240 | + return $jsonld_array; |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + /** |
|
| 244 | + * Get term url. |
|
| 245 | + * |
|
| 246 | + * @param $id |
|
| 247 | + * |
|
| 248 | + * @return array|false|int|mixed|string|WP_Error|WP_Term|null |
|
| 249 | + */ |
|
| 250 | + private function get_term_url( $id ) { |
|
| 251 | + if ( null === $id ) { |
|
| 252 | + return isset( $_SERVER['REQUEST_URI'] ) ? filter_var( wp_unslash( $_SERVER['REQUEST_URI'] ), FILTER_SANITIZE_URL ) : ''; |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + $maybe_url = get_term_meta( $id, Wordlift_Url_Property_Service::META_KEY, true ); |
|
| 256 | + if ( ! empty( $maybe_url ) ) { |
|
| 257 | + return $maybe_url; |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + return get_term_link( $id ); |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + /** |
|
| 264 | + * Return jsonld for entities bound to terms. |
|
| 265 | + * |
|
| 266 | + * @param int $term_id Term ID. |
|
| 267 | + * @param int $context A context for the JSON-LD generation, valid values in Jsonld_Context_Enum. |
|
| 268 | + * |
|
| 269 | + * @return array |
|
| 270 | + */ |
|
| 271 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 272 | + private function get_entity_jsonld( $term_id, $context ) { |
|
| 273 | + |
|
| 274 | + // The `_wl_entity_id` are URIs. |
|
| 275 | + $entity_ids = get_term_meta( $term_id, '_wl_entity_id' ); |
|
| 276 | + $entity_uri_service = $this->entity_uri_service; |
|
| 277 | + |
|
| 278 | + $wordlift_jsonld_service = Wordlift_Jsonld_Service::get_instance(); |
|
| 279 | + |
|
| 280 | + $local_entity_ids = array_filter( |
|
| 281 | + $entity_ids, |
|
| 282 | + function ( $uri ) use ( $entity_uri_service ) { |
|
| 283 | + return $entity_uri_service->is_internal( $uri ); |
|
| 284 | + } |
|
| 285 | + ); |
|
| 286 | + |
|
| 287 | + // Bail out if there are no entities. |
|
| 288 | + if ( empty( $local_entity_ids ) ) { |
|
| 289 | + return array(); |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + $post = $this->entity_uri_service->get_entity( array_shift( $local_entity_ids ) ); |
|
| 293 | + $entities_jsonld = $wordlift_jsonld_service->get_jsonld( false, $post->ID ); |
|
| 294 | + // Reset the `url` to the term page. |
|
| 295 | + $entities_jsonld[0]['url'] = get_term_link( $term_id ); |
|
| 296 | + |
|
| 297 | + return $entities_jsonld; |
|
| 298 | + } |
|
| 299 | + |
|
| 300 | + /** |
|
| 301 | + * @param $references |
|
| 302 | + * |
|
| 303 | + * @return array |
|
| 304 | + */ |
|
| 305 | + private function expand_references( $references ) { |
|
| 306 | + if ( ! is_array( $references ) ) { |
|
| 307 | + return array(); |
|
| 308 | + } |
|
| 309 | + $references_jsonld = array(); |
|
| 310 | + // Expand the references. |
|
| 311 | + foreach ( $references as $reference ) { |
|
| 312 | + if ( $reference instanceof Term_Reference ) { |
|
| 313 | + // Second level references won't be expanded. |
|
| 314 | + $references_jsonld[] = current( $this->get( $reference->get_id(), Jsonld_Context_Enum::UNKNOWN, true ) ); |
|
| 315 | + } elseif ( is_numeric( $reference ) ) { |
|
| 316 | + $ref_2 = array(); |
|
| 317 | + $ref_info_2 = array(); |
|
| 318 | + $references_jsonld[] = $this->post_id_to_jsonld_converter->convert( $reference, $ref_2, $ref_info_2, new Relations() ); |
|
| 319 | + } elseif ( $reference instanceof Post_Reference ) { |
|
| 320 | + $ref_2 = array(); |
|
| 321 | + $ref_info_2 = array(); |
|
| 322 | + $references_jsonld[] = $this->post_id_to_jsonld_converter->convert( $reference->get_id(), $ref_2, $ref_info_2, new Relations() ); |
|
| 323 | + } |
|
| 324 | + } |
|
| 325 | + |
|
| 326 | + return $references_jsonld; |
|
| 327 | + } |
|
| 328 | 328 | |
| 329 | 329 | } |
@@ -21,469 +21,469 @@ |
||
| 21 | 21 | */ |
| 22 | 22 | class Wordlift_Jsonld_Service { |
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * Creative work types. |
|
| 26 | - * |
|
| 27 | - * @var string[] |
|
| 28 | - */ |
|
| 29 | - private static $creative_work_types = array( |
|
| 30 | - 'AmpStory', |
|
| 31 | - 'ArchiveComponent', |
|
| 32 | - 'Article', |
|
| 33 | - 'Atlas', |
|
| 34 | - 'Blog', |
|
| 35 | - 'Book', |
|
| 36 | - 'Chapter', |
|
| 37 | - 'Claim', |
|
| 38 | - 'Clip', |
|
| 39 | - 'Code', |
|
| 40 | - 'Collection', |
|
| 41 | - 'ComicStory', |
|
| 42 | - 'Comment', |
|
| 43 | - 'Conversation', |
|
| 44 | - 'Course', |
|
| 45 | - 'CreativeWork', |
|
| 46 | - 'CreativeWorkSeason', |
|
| 47 | - 'CreativeWorkSeries', |
|
| 48 | - 'DataCatalog', |
|
| 49 | - 'Dataset', |
|
| 50 | - 'DefinedTermSet', |
|
| 51 | - 'Diet', |
|
| 52 | - 'DigitalDocument', |
|
| 53 | - 'Drawing', |
|
| 54 | - 'EducationalOccupationalCredential', |
|
| 55 | - 'Episode', |
|
| 56 | - 'ExercisePlan', |
|
| 57 | - 'Game', |
|
| 58 | - 'Guide', |
|
| 59 | - 'HowTo', |
|
| 60 | - 'HowToDirection', |
|
| 61 | - 'HowToSection', |
|
| 62 | - 'HowToStep', |
|
| 63 | - 'HowToTip', |
|
| 64 | - 'HyperToc', |
|
| 65 | - 'HyperTocEntry', |
|
| 66 | - 'LearningResource', |
|
| 67 | - 'Legislation', |
|
| 68 | - 'Manuscript', |
|
| 69 | - 'Map', |
|
| 70 | - 'MathSolver', |
|
| 71 | - 'MediaObject', |
|
| 72 | - 'Menu', |
|
| 73 | - 'MenuSection', |
|
| 74 | - 'Message', |
|
| 75 | - 'Movie', |
|
| 76 | - 'MusicComposition', |
|
| 77 | - 'MusicPlaylist', |
|
| 78 | - 'MusicRecording', |
|
| 79 | - 'Painting', |
|
| 80 | - 'Photograph', |
|
| 81 | - 'Play', |
|
| 82 | - 'Poster', |
|
| 83 | - 'PublicationIssue', |
|
| 84 | - 'PublicationVolume', |
|
| 85 | - 'Quotation', |
|
| 86 | - 'Review', |
|
| 87 | - 'Sculpture', |
|
| 88 | - 'Season', |
|
| 89 | - 'SheetMusic', |
|
| 90 | - 'ShortStory', |
|
| 91 | - 'SoftwareApplication', |
|
| 92 | - 'SoftwareSourceCode', |
|
| 93 | - 'SpecialAnnouncement', |
|
| 94 | - 'Thesis', |
|
| 95 | - 'TvSeason', |
|
| 96 | - 'TvSeries', |
|
| 97 | - 'VisualArtwork', |
|
| 98 | - 'WebContent', |
|
| 99 | - 'WebPage', |
|
| 100 | - 'WebPageElement', |
|
| 101 | - 'WebSite', |
|
| 102 | - 'AdvertiserContentArticle', |
|
| 103 | - 'NewsArticle', |
|
| 104 | - 'Report', |
|
| 105 | - 'SatiricalArticle', |
|
| 106 | - 'ScholarlyArticle', |
|
| 107 | - 'SocialMediaPosting', |
|
| 108 | - 'TechArticle', |
|
| 109 | - 'AnalysisNewsArticle', |
|
| 110 | - 'AskPublicNewsArticle', |
|
| 111 | - 'BackgroundNewsArticle', |
|
| 112 | - 'OpinionNewsArticle', |
|
| 113 | - 'ReportageNewsArticle', |
|
| 114 | - 'ReviewNewsArticle', |
|
| 115 | - 'MedicalScholarlyArticle', |
|
| 116 | - 'BlogPosting', |
|
| 117 | - 'DiscussionForumPosting', |
|
| 118 | - 'LiveBlogPosting', |
|
| 119 | - 'ApiReference', |
|
| 120 | - 'Audiobook', |
|
| 121 | - 'MovieClip', |
|
| 122 | - 'RadioClip', |
|
| 123 | - 'TvClip', |
|
| 124 | - 'VideoGameClip', |
|
| 125 | - 'ProductCollection', |
|
| 126 | - 'ComicCoverArt', |
|
| 127 | - 'Answer', |
|
| 128 | - 'CorrectionComment', |
|
| 129 | - 'Question', |
|
| 130 | - 'PodcastSeason', |
|
| 131 | - 'RadioSeason', |
|
| 132 | - 'TvSeason', |
|
| 133 | - 'BookSeries', |
|
| 134 | - 'MovieSeries', |
|
| 135 | - 'Periodical', |
|
| 136 | - 'PodcastSeries', |
|
| 137 | - 'RadioSeries', |
|
| 138 | - 'TvSeries', |
|
| 139 | - 'VideoGameSeries', |
|
| 140 | - 'ComicSeries', |
|
| 141 | - 'Newspaper', |
|
| 142 | - 'DataFeed', |
|
| 143 | - 'CompleteDataFeed', |
|
| 144 | - 'CategoryCodeSet', |
|
| 145 | - 'NoteDigitalDocument', |
|
| 146 | - 'PresentationDigitalDocument', |
|
| 147 | - 'SpreadsheetDigitalDocument', |
|
| 148 | - 'TextDigitalDocument', |
|
| 149 | - 'PodcastEpisode', |
|
| 150 | - 'RadioEpisode', |
|
| 151 | - 'TvEpisode', |
|
| 152 | - 'VideoGame', |
|
| 153 | - 'Recipe', |
|
| 154 | - 'Course', |
|
| 155 | - 'Quiz', |
|
| 156 | - 'LegislationObject', |
|
| 157 | - 'AudioObject', |
|
| 158 | - 'DModel', |
|
| 159 | - 'DataDownload', |
|
| 160 | - 'ImageObject', |
|
| 161 | - 'LegislationObject', |
|
| 162 | - 'MusicVideoObject', |
|
| 163 | - 'VideoObject', |
|
| 164 | - 'Audiobook', |
|
| 165 | - 'Barcode', |
|
| 166 | - 'EmailMessage', |
|
| 167 | - 'MusicAlbum', |
|
| 168 | - 'MusicRelease', |
|
| 169 | - 'ComicIssue', |
|
| 170 | - 'ClaimReview', |
|
| 171 | - 'CriticReview', |
|
| 172 | - 'EmployerReview', |
|
| 173 | - 'MediaReview', |
|
| 174 | - 'Recommendation', |
|
| 175 | - 'UserReview', |
|
| 176 | - 'ReviewNewsArticle', |
|
| 177 | - 'MobileApplication', |
|
| 178 | - 'VideoGame', |
|
| 179 | - 'WebApplication', |
|
| 180 | - 'CoverArt', |
|
| 181 | - 'ComicCoverArt', |
|
| 182 | - 'HealthTopicContent', |
|
| 183 | - 'AboutPage', |
|
| 184 | - 'CheckoutPage', |
|
| 185 | - 'CollectionPage', |
|
| 186 | - 'ContactPage', |
|
| 187 | - 'FaqPage', |
|
| 188 | - 'ItemPage', |
|
| 189 | - 'MedicalWebPage', |
|
| 190 | - 'ProfilePage', |
|
| 191 | - 'QaPage', |
|
| 192 | - 'RealEstateListing', |
|
| 193 | - 'SearchResultsPage', |
|
| 194 | - 'MediaGallery', |
|
| 195 | - 'ImageGallery', |
|
| 196 | - 'VideoGallery', |
|
| 197 | - 'SiteNavigationElement', |
|
| 198 | - 'Table', |
|
| 199 | - 'WpAdBlock', |
|
| 200 | - 'WpFooter', |
|
| 201 | - 'WpHeader', |
|
| 202 | - 'WpSideBar', |
|
| 203 | - ); |
|
| 204 | - |
|
| 205 | - /** |
|
| 206 | - * The singleton instance for the JSON-LD service. |
|
| 207 | - * |
|
| 208 | - * @since 3.15.1 |
|
| 209 | - * |
|
| 210 | - * @var \Wordlift_Jsonld_Service $instance The singleton instance for the JSON-LD service. |
|
| 211 | - */ |
|
| 212 | - private static $instance; |
|
| 213 | - |
|
| 214 | - /** |
|
| 215 | - * A {@link Wordlift_Entity_Service} instance. |
|
| 216 | - * |
|
| 217 | - * @since 3.8.0 |
|
| 218 | - * @access private |
|
| 219 | - * @var Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 220 | - */ |
|
| 221 | - private $entity_service; |
|
| 222 | - |
|
| 223 | - /** |
|
| 224 | - * A {@link Wordlift_Term_JsonLd_Adapter} instance. |
|
| 225 | - * |
|
| 226 | - * @since 3.32.0 |
|
| 227 | - * @access private |
|
| 228 | - * @var Wordlift_Term_JsonLd_Adapter $entity_service A {@link Wordlift_Term_JsonLd_Adapter} instance. |
|
| 229 | - */ |
|
| 230 | - private $term_jsonld_adapter; |
|
| 231 | - |
|
| 232 | - /** |
|
| 233 | - * A {@link Wordlift_Post_Converter} instance. |
|
| 234 | - * |
|
| 235 | - * @since 3.8.0 |
|
| 236 | - * @access private |
|
| 237 | - * @var \Wordlift_Post_Converter A {@link Wordlift_Post_Converter} instance. |
|
| 238 | - */ |
|
| 239 | - private $converter; |
|
| 240 | - |
|
| 241 | - /** |
|
| 242 | - * A {@link Wordlift_Website_Jsonld_Converter} instance. |
|
| 243 | - * |
|
| 244 | - * @since 3.14.0 |
|
| 245 | - * @access private |
|
| 246 | - * @var \Wordlift_Website_Jsonld_Converter A {@link Wordlift_Website_Jsonld_Converter} instance. |
|
| 247 | - */ |
|
| 248 | - private $website_converter; |
|
| 249 | - |
|
| 250 | - /** |
|
| 251 | - * Create a JSON-LD service. |
|
| 252 | - * |
|
| 253 | - * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 254 | - * @param \Wordlift_Post_Converter $converter A {@link Wordlift_Uri_To_Jsonld_Converter} instance. |
|
| 255 | - * @param \Wordlift_Website_Jsonld_Converter $website_converter A {@link Wordlift_Website_Jsonld_Converter} instance. |
|
| 256 | - * @param \Wordlift_Term_JsonLd_Adapter $term_jsonld_adapter |
|
| 257 | - * |
|
| 258 | - * @since 3.8.0 |
|
| 259 | - */ |
|
| 260 | - public function __construct( $entity_service, $converter, $website_converter, $term_jsonld_adapter ) { |
|
| 261 | - $this->entity_service = $entity_service; |
|
| 262 | - $this->converter = $converter; |
|
| 263 | - $this->website_converter = $website_converter; |
|
| 264 | - $this->term_jsonld_adapter = $term_jsonld_adapter; |
|
| 265 | - self::$instance = $this; |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - /** |
|
| 269 | - * Get the singleton instance for the JSON-LD service. |
|
| 270 | - * |
|
| 271 | - * @return \Wordlift_Jsonld_Service The singleton instance for the JSON-LD service. |
|
| 272 | - * @since 3.15.1 |
|
| 273 | - */ |
|
| 274 | - public static function get_instance() { |
|
| 275 | - |
|
| 276 | - return self::$instance; |
|
| 277 | - } |
|
| 278 | - |
|
| 279 | - /** |
|
| 280 | - * Process calls to the AJAX 'wl_jsonld' endpoint. |
|
| 281 | - * |
|
| 282 | - * @since 3.8.0 |
|
| 283 | - */ |
|
| 284 | - public function get() { |
|
| 285 | - // Clear the buffer to be sure someone doesn't mess with our response. |
|
| 286 | - // |
|
| 287 | - // See https://github.com/insideout10/wordlift-plugin/issues/406. |
|
| 288 | - // See https://codex.wordpress.org/AJAX_in_Plugins. |
|
| 289 | - // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
| 290 | - @ob_clean(); |
|
| 291 | - |
|
| 292 | - // Get the parameter from the request. |
|
| 293 | - $is_homepage = isset( $_REQUEST['homepage'] ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 294 | - $post_id = isset( $_REQUEST['id'] ) && is_numeric( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : null; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 295 | - |
|
| 296 | - // Send the generated JSON-LD. |
|
| 297 | - $this->send_jsonld( $this->get_jsonld( $is_homepage, $post_id ) ); |
|
| 298 | - |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - /** |
|
| 302 | - * A close of WP's own `wp_send_json` function which uses `application/ld+json` as content type. |
|
| 303 | - * |
|
| 304 | - * @param mixed $response Variable (usually an array or object) to encode as JSON, |
|
| 305 | - * then print and die. |
|
| 306 | - * |
|
| 307 | - * @since 3.18.5 |
|
| 308 | - */ |
|
| 309 | - private function send_jsonld( $response ) { |
|
| 310 | - // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
| 311 | - @header( 'Content-Type: application/ld+json; charset=' . get_option( 'blog_charset' ) ); |
|
| 312 | - echo wp_json_encode( $response ); |
|
| 313 | - if ( apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { |
|
| 314 | - wp_die(); |
|
| 315 | - } else { |
|
| 316 | - die; |
|
| 317 | - } |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - /** |
|
| 321 | - * Get the JSON-LD. |
|
| 322 | - * |
|
| 323 | - * @param bool $is_homepage Whether the JSON-LD for the homepage is being requested. |
|
| 324 | - * @param int|null $post_id The JSON-LD for the specified {@link WP_Post} id. |
|
| 325 | - * @param int $context A context for the JSON-LD generation, valid values in Jsonld_Context_Enum. |
|
| 326 | - * |
|
| 327 | - * @return array A JSON-LD structure. |
|
| 328 | - * @since 3.15.1 |
|
| 329 | - */ |
|
| 330 | - public function get_jsonld( $is_homepage = false, $post_id = null, $context = Jsonld_Context_Enum::UNKNOWN ) { |
|
| 331 | - // Tell NewRelic to ignore us, otherwise NewRelic customers might receive |
|
| 332 | - // e-mails with a low apdex score. |
|
| 333 | - // |
|
| 334 | - // See https://github.com/insideout10/wordlift-plugin/issues/521 |
|
| 335 | - Wordlift_NewRelic_Adapter::ignore_apdex(); |
|
| 336 | - |
|
| 337 | - // Switch to Website converter if is home page. |
|
| 338 | - if ( $is_homepage ) { |
|
| 339 | - /** |
|
| 340 | - * Filter: 'wordlift_disable_website_json_ld' - Allow disabling of the json+ld output. |
|
| 341 | - * |
|
| 342 | - * @since 3.14.0 |
|
| 343 | - * @api bool $display_search Whether or not to display json+ld search on the frontend. |
|
| 344 | - */ |
|
| 345 | - if ( apply_filters( 'wordlift_disable_website_json_ld', false ) ) { |
|
| 346 | - return array(); |
|
| 347 | - } |
|
| 348 | - |
|
| 349 | - // Set a reference to the website_converter. |
|
| 350 | - $website_converter = $this->website_converter; |
|
| 351 | - |
|
| 352 | - // Send JSON-LD. |
|
| 353 | - return $website_converter->create_schema(); |
|
| 354 | - } |
|
| 355 | - |
|
| 356 | - // If no id has been provided return an empty array. |
|
| 357 | - if ( ! isset( $post_id ) ) { |
|
| 358 | - return array(); |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - // An array of references which is captured when converting an URI to a |
|
| 362 | - // json which we gather to further expand our json-ld. |
|
| 363 | - $references = array(); |
|
| 364 | - $references_infos = array(); |
|
| 365 | - |
|
| 366 | - // Set a reference to the entity_to_jsonld_converter to use in the closures. |
|
| 367 | - $entity_to_jsonld_converter = $this->converter; |
|
| 368 | - |
|
| 369 | - $relations = new Relations(); |
|
| 370 | - $jsonld = $entity_to_jsonld_converter->convert( $post_id, $references, $references_infos, $relations ); |
|
| 371 | - |
|
| 372 | - $graph = new Graph( $jsonld, $entity_to_jsonld_converter, Wordlift_Term_JsonLd_Adapter::get_instance() ); |
|
| 373 | - |
|
| 374 | - $schema_type = is_array( $jsonld['@type'] ) ? $jsonld['@type'] : array( $jsonld['@type'] ); |
|
| 375 | - |
|
| 376 | - // Add `about`/`mentions` only for `CreativeWork` and descendants. |
|
| 377 | - if ( array_intersect( $schema_type, self::$creative_work_types ) ) { |
|
| 378 | - |
|
| 379 | - foreach ( $relations->toArray() as $relation ) { |
|
| 380 | - |
|
| 381 | - // Setting about or mentions by label match is currently supported only for posts |
|
| 382 | - if ( Object_Type_Enum::POST !== $relation->get_object()->get_type() ) { |
|
| 383 | - continue; |
|
| 384 | - } |
|
| 385 | - |
|
| 386 | - // Add the `mentions`/`about` prop. |
|
| 387 | - $this->add_mention_or_about( $jsonld, $post_id, $relation ); |
|
| 388 | - } |
|
| 389 | - $graph->set_main_jsonld( $jsonld ); |
|
| 390 | - } |
|
| 391 | - |
|
| 392 | - $jsonld_arr = $graph->add_references( $references ) |
|
| 393 | - ->add_relations( $relations ) |
|
| 394 | - ->add_required_reference_infos( $references_infos ) |
|
| 395 | - ->render( $context ); |
|
| 396 | - |
|
| 397 | - /** |
|
| 398 | - * Filter name: wl_after_get_jsonld |
|
| 399 | - * |
|
| 400 | - * @return array |
|
| 401 | - * @since 3.27.2 |
|
| 402 | - * @var $jsonld_arr array The final jsonld before outputting to page. |
|
| 403 | - * @var $post_id int The post id for which the jsonld is generated. |
|
| 404 | - */ |
|
| 405 | - $jsonld_arr = apply_filters( 'wl_after_get_jsonld', $jsonld_arr, $post_id, $context ); |
|
| 406 | - |
|
| 407 | - return $jsonld_arr; |
|
| 408 | - } |
|
| 409 | - |
|
| 410 | - /** |
|
| 411 | - * Write the JSON-LD in the head. |
|
| 412 | - * |
|
| 413 | - * This function isn't actually used, but may be used to quickly enable writing the JSON-LD synchronously to the |
|
| 414 | - * document head, using the `wp_head` hook. |
|
| 415 | - * |
|
| 416 | - * @since 3.18.5 |
|
| 417 | - */ |
|
| 418 | - public function wp_head() { |
|
| 419 | - |
|
| 420 | - // Determine whether this is the home page or whether we're displaying a single post. |
|
| 421 | - $is_homepage = is_home() || is_front_page(); |
|
| 422 | - $post_id = is_singular() ? get_the_ID() : null; |
|
| 423 | - |
|
| 424 | - $jsonld = wp_json_encode( $this->get_jsonld( $is_homepage, $post_id, Jsonld_Context_Enum::PAGE ) ); |
|
| 425 | - ?> |
|
| 24 | + /** |
|
| 25 | + * Creative work types. |
|
| 26 | + * |
|
| 27 | + * @var string[] |
|
| 28 | + */ |
|
| 29 | + private static $creative_work_types = array( |
|
| 30 | + 'AmpStory', |
|
| 31 | + 'ArchiveComponent', |
|
| 32 | + 'Article', |
|
| 33 | + 'Atlas', |
|
| 34 | + 'Blog', |
|
| 35 | + 'Book', |
|
| 36 | + 'Chapter', |
|
| 37 | + 'Claim', |
|
| 38 | + 'Clip', |
|
| 39 | + 'Code', |
|
| 40 | + 'Collection', |
|
| 41 | + 'ComicStory', |
|
| 42 | + 'Comment', |
|
| 43 | + 'Conversation', |
|
| 44 | + 'Course', |
|
| 45 | + 'CreativeWork', |
|
| 46 | + 'CreativeWorkSeason', |
|
| 47 | + 'CreativeWorkSeries', |
|
| 48 | + 'DataCatalog', |
|
| 49 | + 'Dataset', |
|
| 50 | + 'DefinedTermSet', |
|
| 51 | + 'Diet', |
|
| 52 | + 'DigitalDocument', |
|
| 53 | + 'Drawing', |
|
| 54 | + 'EducationalOccupationalCredential', |
|
| 55 | + 'Episode', |
|
| 56 | + 'ExercisePlan', |
|
| 57 | + 'Game', |
|
| 58 | + 'Guide', |
|
| 59 | + 'HowTo', |
|
| 60 | + 'HowToDirection', |
|
| 61 | + 'HowToSection', |
|
| 62 | + 'HowToStep', |
|
| 63 | + 'HowToTip', |
|
| 64 | + 'HyperToc', |
|
| 65 | + 'HyperTocEntry', |
|
| 66 | + 'LearningResource', |
|
| 67 | + 'Legislation', |
|
| 68 | + 'Manuscript', |
|
| 69 | + 'Map', |
|
| 70 | + 'MathSolver', |
|
| 71 | + 'MediaObject', |
|
| 72 | + 'Menu', |
|
| 73 | + 'MenuSection', |
|
| 74 | + 'Message', |
|
| 75 | + 'Movie', |
|
| 76 | + 'MusicComposition', |
|
| 77 | + 'MusicPlaylist', |
|
| 78 | + 'MusicRecording', |
|
| 79 | + 'Painting', |
|
| 80 | + 'Photograph', |
|
| 81 | + 'Play', |
|
| 82 | + 'Poster', |
|
| 83 | + 'PublicationIssue', |
|
| 84 | + 'PublicationVolume', |
|
| 85 | + 'Quotation', |
|
| 86 | + 'Review', |
|
| 87 | + 'Sculpture', |
|
| 88 | + 'Season', |
|
| 89 | + 'SheetMusic', |
|
| 90 | + 'ShortStory', |
|
| 91 | + 'SoftwareApplication', |
|
| 92 | + 'SoftwareSourceCode', |
|
| 93 | + 'SpecialAnnouncement', |
|
| 94 | + 'Thesis', |
|
| 95 | + 'TvSeason', |
|
| 96 | + 'TvSeries', |
|
| 97 | + 'VisualArtwork', |
|
| 98 | + 'WebContent', |
|
| 99 | + 'WebPage', |
|
| 100 | + 'WebPageElement', |
|
| 101 | + 'WebSite', |
|
| 102 | + 'AdvertiserContentArticle', |
|
| 103 | + 'NewsArticle', |
|
| 104 | + 'Report', |
|
| 105 | + 'SatiricalArticle', |
|
| 106 | + 'ScholarlyArticle', |
|
| 107 | + 'SocialMediaPosting', |
|
| 108 | + 'TechArticle', |
|
| 109 | + 'AnalysisNewsArticle', |
|
| 110 | + 'AskPublicNewsArticle', |
|
| 111 | + 'BackgroundNewsArticle', |
|
| 112 | + 'OpinionNewsArticle', |
|
| 113 | + 'ReportageNewsArticle', |
|
| 114 | + 'ReviewNewsArticle', |
|
| 115 | + 'MedicalScholarlyArticle', |
|
| 116 | + 'BlogPosting', |
|
| 117 | + 'DiscussionForumPosting', |
|
| 118 | + 'LiveBlogPosting', |
|
| 119 | + 'ApiReference', |
|
| 120 | + 'Audiobook', |
|
| 121 | + 'MovieClip', |
|
| 122 | + 'RadioClip', |
|
| 123 | + 'TvClip', |
|
| 124 | + 'VideoGameClip', |
|
| 125 | + 'ProductCollection', |
|
| 126 | + 'ComicCoverArt', |
|
| 127 | + 'Answer', |
|
| 128 | + 'CorrectionComment', |
|
| 129 | + 'Question', |
|
| 130 | + 'PodcastSeason', |
|
| 131 | + 'RadioSeason', |
|
| 132 | + 'TvSeason', |
|
| 133 | + 'BookSeries', |
|
| 134 | + 'MovieSeries', |
|
| 135 | + 'Periodical', |
|
| 136 | + 'PodcastSeries', |
|
| 137 | + 'RadioSeries', |
|
| 138 | + 'TvSeries', |
|
| 139 | + 'VideoGameSeries', |
|
| 140 | + 'ComicSeries', |
|
| 141 | + 'Newspaper', |
|
| 142 | + 'DataFeed', |
|
| 143 | + 'CompleteDataFeed', |
|
| 144 | + 'CategoryCodeSet', |
|
| 145 | + 'NoteDigitalDocument', |
|
| 146 | + 'PresentationDigitalDocument', |
|
| 147 | + 'SpreadsheetDigitalDocument', |
|
| 148 | + 'TextDigitalDocument', |
|
| 149 | + 'PodcastEpisode', |
|
| 150 | + 'RadioEpisode', |
|
| 151 | + 'TvEpisode', |
|
| 152 | + 'VideoGame', |
|
| 153 | + 'Recipe', |
|
| 154 | + 'Course', |
|
| 155 | + 'Quiz', |
|
| 156 | + 'LegislationObject', |
|
| 157 | + 'AudioObject', |
|
| 158 | + 'DModel', |
|
| 159 | + 'DataDownload', |
|
| 160 | + 'ImageObject', |
|
| 161 | + 'LegislationObject', |
|
| 162 | + 'MusicVideoObject', |
|
| 163 | + 'VideoObject', |
|
| 164 | + 'Audiobook', |
|
| 165 | + 'Barcode', |
|
| 166 | + 'EmailMessage', |
|
| 167 | + 'MusicAlbum', |
|
| 168 | + 'MusicRelease', |
|
| 169 | + 'ComicIssue', |
|
| 170 | + 'ClaimReview', |
|
| 171 | + 'CriticReview', |
|
| 172 | + 'EmployerReview', |
|
| 173 | + 'MediaReview', |
|
| 174 | + 'Recommendation', |
|
| 175 | + 'UserReview', |
|
| 176 | + 'ReviewNewsArticle', |
|
| 177 | + 'MobileApplication', |
|
| 178 | + 'VideoGame', |
|
| 179 | + 'WebApplication', |
|
| 180 | + 'CoverArt', |
|
| 181 | + 'ComicCoverArt', |
|
| 182 | + 'HealthTopicContent', |
|
| 183 | + 'AboutPage', |
|
| 184 | + 'CheckoutPage', |
|
| 185 | + 'CollectionPage', |
|
| 186 | + 'ContactPage', |
|
| 187 | + 'FaqPage', |
|
| 188 | + 'ItemPage', |
|
| 189 | + 'MedicalWebPage', |
|
| 190 | + 'ProfilePage', |
|
| 191 | + 'QaPage', |
|
| 192 | + 'RealEstateListing', |
|
| 193 | + 'SearchResultsPage', |
|
| 194 | + 'MediaGallery', |
|
| 195 | + 'ImageGallery', |
|
| 196 | + 'VideoGallery', |
|
| 197 | + 'SiteNavigationElement', |
|
| 198 | + 'Table', |
|
| 199 | + 'WpAdBlock', |
|
| 200 | + 'WpFooter', |
|
| 201 | + 'WpHeader', |
|
| 202 | + 'WpSideBar', |
|
| 203 | + ); |
|
| 204 | + |
|
| 205 | + /** |
|
| 206 | + * The singleton instance for the JSON-LD service. |
|
| 207 | + * |
|
| 208 | + * @since 3.15.1 |
|
| 209 | + * |
|
| 210 | + * @var \Wordlift_Jsonld_Service $instance The singleton instance for the JSON-LD service. |
|
| 211 | + */ |
|
| 212 | + private static $instance; |
|
| 213 | + |
|
| 214 | + /** |
|
| 215 | + * A {@link Wordlift_Entity_Service} instance. |
|
| 216 | + * |
|
| 217 | + * @since 3.8.0 |
|
| 218 | + * @access private |
|
| 219 | + * @var Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 220 | + */ |
|
| 221 | + private $entity_service; |
|
| 222 | + |
|
| 223 | + /** |
|
| 224 | + * A {@link Wordlift_Term_JsonLd_Adapter} instance. |
|
| 225 | + * |
|
| 226 | + * @since 3.32.0 |
|
| 227 | + * @access private |
|
| 228 | + * @var Wordlift_Term_JsonLd_Adapter $entity_service A {@link Wordlift_Term_JsonLd_Adapter} instance. |
|
| 229 | + */ |
|
| 230 | + private $term_jsonld_adapter; |
|
| 231 | + |
|
| 232 | + /** |
|
| 233 | + * A {@link Wordlift_Post_Converter} instance. |
|
| 234 | + * |
|
| 235 | + * @since 3.8.0 |
|
| 236 | + * @access private |
|
| 237 | + * @var \Wordlift_Post_Converter A {@link Wordlift_Post_Converter} instance. |
|
| 238 | + */ |
|
| 239 | + private $converter; |
|
| 240 | + |
|
| 241 | + /** |
|
| 242 | + * A {@link Wordlift_Website_Jsonld_Converter} instance. |
|
| 243 | + * |
|
| 244 | + * @since 3.14.0 |
|
| 245 | + * @access private |
|
| 246 | + * @var \Wordlift_Website_Jsonld_Converter A {@link Wordlift_Website_Jsonld_Converter} instance. |
|
| 247 | + */ |
|
| 248 | + private $website_converter; |
|
| 249 | + |
|
| 250 | + /** |
|
| 251 | + * Create a JSON-LD service. |
|
| 252 | + * |
|
| 253 | + * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 254 | + * @param \Wordlift_Post_Converter $converter A {@link Wordlift_Uri_To_Jsonld_Converter} instance. |
|
| 255 | + * @param \Wordlift_Website_Jsonld_Converter $website_converter A {@link Wordlift_Website_Jsonld_Converter} instance. |
|
| 256 | + * @param \Wordlift_Term_JsonLd_Adapter $term_jsonld_adapter |
|
| 257 | + * |
|
| 258 | + * @since 3.8.0 |
|
| 259 | + */ |
|
| 260 | + public function __construct( $entity_service, $converter, $website_converter, $term_jsonld_adapter ) { |
|
| 261 | + $this->entity_service = $entity_service; |
|
| 262 | + $this->converter = $converter; |
|
| 263 | + $this->website_converter = $website_converter; |
|
| 264 | + $this->term_jsonld_adapter = $term_jsonld_adapter; |
|
| 265 | + self::$instance = $this; |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + /** |
|
| 269 | + * Get the singleton instance for the JSON-LD service. |
|
| 270 | + * |
|
| 271 | + * @return \Wordlift_Jsonld_Service The singleton instance for the JSON-LD service. |
|
| 272 | + * @since 3.15.1 |
|
| 273 | + */ |
|
| 274 | + public static function get_instance() { |
|
| 275 | + |
|
| 276 | + return self::$instance; |
|
| 277 | + } |
|
| 278 | + |
|
| 279 | + /** |
|
| 280 | + * Process calls to the AJAX 'wl_jsonld' endpoint. |
|
| 281 | + * |
|
| 282 | + * @since 3.8.0 |
|
| 283 | + */ |
|
| 284 | + public function get() { |
|
| 285 | + // Clear the buffer to be sure someone doesn't mess with our response. |
|
| 286 | + // |
|
| 287 | + // See https://github.com/insideout10/wordlift-plugin/issues/406. |
|
| 288 | + // See https://codex.wordpress.org/AJAX_in_Plugins. |
|
| 289 | + // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
| 290 | + @ob_clean(); |
|
| 291 | + |
|
| 292 | + // Get the parameter from the request. |
|
| 293 | + $is_homepage = isset( $_REQUEST['homepage'] ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 294 | + $post_id = isset( $_REQUEST['id'] ) && is_numeric( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : null; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 295 | + |
|
| 296 | + // Send the generated JSON-LD. |
|
| 297 | + $this->send_jsonld( $this->get_jsonld( $is_homepage, $post_id ) ); |
|
| 298 | + |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + /** |
|
| 302 | + * A close of WP's own `wp_send_json` function which uses `application/ld+json` as content type. |
|
| 303 | + * |
|
| 304 | + * @param mixed $response Variable (usually an array or object) to encode as JSON, |
|
| 305 | + * then print and die. |
|
| 306 | + * |
|
| 307 | + * @since 3.18.5 |
|
| 308 | + */ |
|
| 309 | + private function send_jsonld( $response ) { |
|
| 310 | + // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
| 311 | + @header( 'Content-Type: application/ld+json; charset=' . get_option( 'blog_charset' ) ); |
|
| 312 | + echo wp_json_encode( $response ); |
|
| 313 | + if ( apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { |
|
| 314 | + wp_die(); |
|
| 315 | + } else { |
|
| 316 | + die; |
|
| 317 | + } |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + /** |
|
| 321 | + * Get the JSON-LD. |
|
| 322 | + * |
|
| 323 | + * @param bool $is_homepage Whether the JSON-LD for the homepage is being requested. |
|
| 324 | + * @param int|null $post_id The JSON-LD for the specified {@link WP_Post} id. |
|
| 325 | + * @param int $context A context for the JSON-LD generation, valid values in Jsonld_Context_Enum. |
|
| 326 | + * |
|
| 327 | + * @return array A JSON-LD structure. |
|
| 328 | + * @since 3.15.1 |
|
| 329 | + */ |
|
| 330 | + public function get_jsonld( $is_homepage = false, $post_id = null, $context = Jsonld_Context_Enum::UNKNOWN ) { |
|
| 331 | + // Tell NewRelic to ignore us, otherwise NewRelic customers might receive |
|
| 332 | + // e-mails with a low apdex score. |
|
| 333 | + // |
|
| 334 | + // See https://github.com/insideout10/wordlift-plugin/issues/521 |
|
| 335 | + Wordlift_NewRelic_Adapter::ignore_apdex(); |
|
| 336 | + |
|
| 337 | + // Switch to Website converter if is home page. |
|
| 338 | + if ( $is_homepage ) { |
|
| 339 | + /** |
|
| 340 | + * Filter: 'wordlift_disable_website_json_ld' - Allow disabling of the json+ld output. |
|
| 341 | + * |
|
| 342 | + * @since 3.14.0 |
|
| 343 | + * @api bool $display_search Whether or not to display json+ld search on the frontend. |
|
| 344 | + */ |
|
| 345 | + if ( apply_filters( 'wordlift_disable_website_json_ld', false ) ) { |
|
| 346 | + return array(); |
|
| 347 | + } |
|
| 348 | + |
|
| 349 | + // Set a reference to the website_converter. |
|
| 350 | + $website_converter = $this->website_converter; |
|
| 351 | + |
|
| 352 | + // Send JSON-LD. |
|
| 353 | + return $website_converter->create_schema(); |
|
| 354 | + } |
|
| 355 | + |
|
| 356 | + // If no id has been provided return an empty array. |
|
| 357 | + if ( ! isset( $post_id ) ) { |
|
| 358 | + return array(); |
|
| 359 | + } |
|
| 360 | + |
|
| 361 | + // An array of references which is captured when converting an URI to a |
|
| 362 | + // json which we gather to further expand our json-ld. |
|
| 363 | + $references = array(); |
|
| 364 | + $references_infos = array(); |
|
| 365 | + |
|
| 366 | + // Set a reference to the entity_to_jsonld_converter to use in the closures. |
|
| 367 | + $entity_to_jsonld_converter = $this->converter; |
|
| 368 | + |
|
| 369 | + $relations = new Relations(); |
|
| 370 | + $jsonld = $entity_to_jsonld_converter->convert( $post_id, $references, $references_infos, $relations ); |
|
| 371 | + |
|
| 372 | + $graph = new Graph( $jsonld, $entity_to_jsonld_converter, Wordlift_Term_JsonLd_Adapter::get_instance() ); |
|
| 373 | + |
|
| 374 | + $schema_type = is_array( $jsonld['@type'] ) ? $jsonld['@type'] : array( $jsonld['@type'] ); |
|
| 375 | + |
|
| 376 | + // Add `about`/`mentions` only for `CreativeWork` and descendants. |
|
| 377 | + if ( array_intersect( $schema_type, self::$creative_work_types ) ) { |
|
| 378 | + |
|
| 379 | + foreach ( $relations->toArray() as $relation ) { |
|
| 380 | + |
|
| 381 | + // Setting about or mentions by label match is currently supported only for posts |
|
| 382 | + if ( Object_Type_Enum::POST !== $relation->get_object()->get_type() ) { |
|
| 383 | + continue; |
|
| 384 | + } |
|
| 385 | + |
|
| 386 | + // Add the `mentions`/`about` prop. |
|
| 387 | + $this->add_mention_or_about( $jsonld, $post_id, $relation ); |
|
| 388 | + } |
|
| 389 | + $graph->set_main_jsonld( $jsonld ); |
|
| 390 | + } |
|
| 391 | + |
|
| 392 | + $jsonld_arr = $graph->add_references( $references ) |
|
| 393 | + ->add_relations( $relations ) |
|
| 394 | + ->add_required_reference_infos( $references_infos ) |
|
| 395 | + ->render( $context ); |
|
| 396 | + |
|
| 397 | + /** |
|
| 398 | + * Filter name: wl_after_get_jsonld |
|
| 399 | + * |
|
| 400 | + * @return array |
|
| 401 | + * @since 3.27.2 |
|
| 402 | + * @var $jsonld_arr array The final jsonld before outputting to page. |
|
| 403 | + * @var $post_id int The post id for which the jsonld is generated. |
|
| 404 | + */ |
|
| 405 | + $jsonld_arr = apply_filters( 'wl_after_get_jsonld', $jsonld_arr, $post_id, $context ); |
|
| 406 | + |
|
| 407 | + return $jsonld_arr; |
|
| 408 | + } |
|
| 409 | + |
|
| 410 | + /** |
|
| 411 | + * Write the JSON-LD in the head. |
|
| 412 | + * |
|
| 413 | + * This function isn't actually used, but may be used to quickly enable writing the JSON-LD synchronously to the |
|
| 414 | + * document head, using the `wp_head` hook. |
|
| 415 | + * |
|
| 416 | + * @since 3.18.5 |
|
| 417 | + */ |
|
| 418 | + public function wp_head() { |
|
| 419 | + |
|
| 420 | + // Determine whether this is the home page or whether we're displaying a single post. |
|
| 421 | + $is_homepage = is_home() || is_front_page(); |
|
| 422 | + $post_id = is_singular() ? get_the_ID() : null; |
|
| 423 | + |
|
| 424 | + $jsonld = wp_json_encode( $this->get_jsonld( $is_homepage, $post_id, Jsonld_Context_Enum::PAGE ) ); |
|
| 425 | + ?> |
|
| 426 | 426 | <script type="application/ld+json"><?php echo esc_html( $jsonld ); ?></script> |
| 427 | 427 | <?php |
| 428 | - } |
|
| 429 | - |
|
| 430 | - /** |
|
| 431 | - * @param array $jsonld |
|
| 432 | - * @param Relation $relation |
|
| 433 | - * |
|
| 434 | - * @return void |
|
| 435 | - */ |
|
| 436 | - private function add_mention_or_about( &$jsonld, $post_id, $relation ) { |
|
| 437 | - $content_service = Wordpress_Content_Service::get_instance(); |
|
| 438 | - $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 439 | - |
|
| 440 | - $object = $relation->get_object(); |
|
| 441 | - $entity_uri = $content_service->get_entity_id( $object ); |
|
| 442 | - $labels = $entity_service->get_labels( $object->get_id(), $object->get_type() ); |
|
| 443 | - |
|
| 444 | - $escaped_labels = array_map( |
|
| 445 | - function ( $value ) { |
|
| 446 | - return preg_quote( $value, '/' ); |
|
| 447 | - }, |
|
| 448 | - $labels |
|
| 449 | - ); |
|
| 450 | - |
|
| 451 | - $matches = false; |
|
| 452 | - |
|
| 453 | - // When the title is empty, then we shouldn't yield a match to about section. |
|
| 454 | - if ( array_filter( $escaped_labels ) ) { |
|
| 455 | - // Check if the labels match any part of the title. |
|
| 456 | - $post = get_post( $post_id ); |
|
| 457 | - $matches = $this->check_title_match( $escaped_labels, $post->post_title ); |
|
| 458 | - } |
|
| 459 | - |
|
| 460 | - if ( $entity_uri ) { |
|
| 461 | - // If the title matches, assign the entity to the about, otherwise to the mentions. |
|
| 462 | - $property_name = $matches ? 'about' : 'mentions'; |
|
| 463 | - $jsonld[ $property_name ] = isset( $jsonld[ $property_name ] ) ? (array) $jsonld[ $property_name ] : array(); |
|
| 464 | - $jsonld[ $property_name ][] = array( '@id' => $entity_uri ); |
|
| 465 | - } |
|
| 466 | - |
|
| 467 | - } |
|
| 468 | - |
|
| 469 | - /** |
|
| 470 | - * Check if the labels match any part of the title. |
|
| 471 | - * |
|
| 472 | - * @param $labels array The labels to check. |
|
| 473 | - * @param $title string The title to check. |
|
| 474 | - * |
|
| 475 | - * @return boolean |
|
| 476 | - */ |
|
| 477 | - public function check_title_match( $labels, $title ) { |
|
| 478 | - |
|
| 479 | - // If the title is empty, then we shouldn't yield a match to about section. |
|
| 480 | - if ( empty( $title ) ) { |
|
| 481 | - return false; |
|
| 482 | - } |
|
| 483 | - |
|
| 484 | - // Check if the labels match any part of the title. |
|
| 485 | - return 1 === preg_match( '/\b(' . implode( '|', $labels ) . ')\b/iu', $title ); |
|
| 486 | - |
|
| 487 | - } |
|
| 428 | + } |
|
| 429 | + |
|
| 430 | + /** |
|
| 431 | + * @param array $jsonld |
|
| 432 | + * @param Relation $relation |
|
| 433 | + * |
|
| 434 | + * @return void |
|
| 435 | + */ |
|
| 436 | + private function add_mention_or_about( &$jsonld, $post_id, $relation ) { |
|
| 437 | + $content_service = Wordpress_Content_Service::get_instance(); |
|
| 438 | + $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 439 | + |
|
| 440 | + $object = $relation->get_object(); |
|
| 441 | + $entity_uri = $content_service->get_entity_id( $object ); |
|
| 442 | + $labels = $entity_service->get_labels( $object->get_id(), $object->get_type() ); |
|
| 443 | + |
|
| 444 | + $escaped_labels = array_map( |
|
| 445 | + function ( $value ) { |
|
| 446 | + return preg_quote( $value, '/' ); |
|
| 447 | + }, |
|
| 448 | + $labels |
|
| 449 | + ); |
|
| 450 | + |
|
| 451 | + $matches = false; |
|
| 452 | + |
|
| 453 | + // When the title is empty, then we shouldn't yield a match to about section. |
|
| 454 | + if ( array_filter( $escaped_labels ) ) { |
|
| 455 | + // Check if the labels match any part of the title. |
|
| 456 | + $post = get_post( $post_id ); |
|
| 457 | + $matches = $this->check_title_match( $escaped_labels, $post->post_title ); |
|
| 458 | + } |
|
| 459 | + |
|
| 460 | + if ( $entity_uri ) { |
|
| 461 | + // If the title matches, assign the entity to the about, otherwise to the mentions. |
|
| 462 | + $property_name = $matches ? 'about' : 'mentions'; |
|
| 463 | + $jsonld[ $property_name ] = isset( $jsonld[ $property_name ] ) ? (array) $jsonld[ $property_name ] : array(); |
|
| 464 | + $jsonld[ $property_name ][] = array( '@id' => $entity_uri ); |
|
| 465 | + } |
|
| 466 | + |
|
| 467 | + } |
|
| 468 | + |
|
| 469 | + /** |
|
| 470 | + * Check if the labels match any part of the title. |
|
| 471 | + * |
|
| 472 | + * @param $labels array The labels to check. |
|
| 473 | + * @param $title string The title to check. |
|
| 474 | + * |
|
| 475 | + * @return boolean |
|
| 476 | + */ |
|
| 477 | + public function check_title_match( $labels, $title ) { |
|
| 478 | + |
|
| 479 | + // If the title is empty, then we shouldn't yield a match to about section. |
|
| 480 | + if ( empty( $title ) ) { |
|
| 481 | + return false; |
|
| 482 | + } |
|
| 483 | + |
|
| 484 | + // Check if the labels match any part of the title. |
|
| 485 | + return 1 === preg_match( '/\b(' . implode( '|', $labels ) . ')\b/iu', $title ); |
|
| 486 | + |
|
| 487 | + } |
|
| 488 | 488 | |
| 489 | 489 | } |
@@ -14,43 +14,43 @@ |
||
| 14 | 14 | * @package Wordlift\Vocabulary\Jsonld |
| 15 | 15 | */ |
| 16 | 16 | class Term_Jsonld { |
| 17 | - /** |
|
| 18 | - * Init. |
|
| 19 | - */ |
|
| 20 | - public function init() { |
|
| 21 | - add_filter( 'wl_term_jsonld_array', array( $this, 'wl_term_jsonld_array' ), 10, 2 ); |
|
| 22 | - } |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * Wl term jsonld array. |
|
| 26 | - * |
|
| 27 | - * @param $jsonld_array |
|
| 28 | - * @param $term_id |
|
| 29 | - * |
|
| 30 | - * @return array|mixed |
|
| 31 | - */ |
|
| 32 | - public function wl_term_jsonld_array( $jsonld_array, $term_id ) { |
|
| 33 | - |
|
| 34 | - $entities = Jsonld_Utils::get_matched_entities_for_term( $term_id ); |
|
| 35 | - |
|
| 36 | - if ( count( $entities ) > 0 ) { |
|
| 37 | - $entity = array_shift( $entities ); |
|
| 38 | - $entity['@context'] = 'http://schema.org'; |
|
| 39 | - |
|
| 40 | - $term_link = get_term_link( $term_id ); |
|
| 41 | - if ( is_wp_error( $term_link ) ) { |
|
| 42 | - Wordlift_Log_Service::get_logger( get_class() ) |
|
| 43 | - ->error( "Term $term_id returned an error: " . $term_link->get_error_message() ); |
|
| 44 | - |
|
| 45 | - return $jsonld_array; |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - $entity['@id'] = $term_link . '/#id'; |
|
| 49 | - $entity['url'] = $term_link; |
|
| 50 | - $entity['mainEntityOfPage'] = $term_link; |
|
| 51 | - $jsonld_array['jsonld'][] = $entity; |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - return $jsonld_array; |
|
| 55 | - } |
|
| 17 | + /** |
|
| 18 | + * Init. |
|
| 19 | + */ |
|
| 20 | + public function init() { |
|
| 21 | + add_filter( 'wl_term_jsonld_array', array( $this, 'wl_term_jsonld_array' ), 10, 2 ); |
|
| 22 | + } |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * Wl term jsonld array. |
|
| 26 | + * |
|
| 27 | + * @param $jsonld_array |
|
| 28 | + * @param $term_id |
|
| 29 | + * |
|
| 30 | + * @return array|mixed |
|
| 31 | + */ |
|
| 32 | + public function wl_term_jsonld_array( $jsonld_array, $term_id ) { |
|
| 33 | + |
|
| 34 | + $entities = Jsonld_Utils::get_matched_entities_for_term( $term_id ); |
|
| 35 | + |
|
| 36 | + if ( count( $entities ) > 0 ) { |
|
| 37 | + $entity = array_shift( $entities ); |
|
| 38 | + $entity['@context'] = 'http://schema.org'; |
|
| 39 | + |
|
| 40 | + $term_link = get_term_link( $term_id ); |
|
| 41 | + if ( is_wp_error( $term_link ) ) { |
|
| 42 | + Wordlift_Log_Service::get_logger( get_class() ) |
|
| 43 | + ->error( "Term $term_id returned an error: " . $term_link->get_error_message() ); |
|
| 44 | + |
|
| 45 | + return $jsonld_array; |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + $entity['@id'] = $term_link . '/#id'; |
|
| 49 | + $entity['url'] = $term_link; |
|
| 50 | + $entity['mainEntityOfPage'] = $term_link; |
|
| 51 | + $jsonld_array['jsonld'][] = $entity; |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + return $jsonld_array; |
|
| 55 | + } |
|
| 56 | 56 | } |
@@ -15,131 +15,131 @@ |
||
| 15 | 15 | * @package Wordlift\Vocabulary\Jsonld |
| 16 | 16 | */ |
| 17 | 17 | class Post_Jsonld { |
| 18 | - /** |
|
| 19 | - * Enhance post jsonld. |
|
| 20 | - */ |
|
| 21 | - public function enhance_post_jsonld() { |
|
| 22 | - add_filter( 'wl_post_jsonld_array', array( $this, 'wl_post_jsonld_array' ), 11, 2 ); |
|
| 23 | - add_filter( 'wl_after_get_jsonld', array( $this, 'wl_after_get_jsonld' ), 11 ); |
|
| 24 | - } |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * Wl post jsonld array. |
|
| 28 | - * |
|
| 29 | - * @param $arr |
|
| 30 | - * @param $post_id |
|
| 31 | - * |
|
| 32 | - * @return array |
|
| 33 | - */ |
|
| 34 | - public function wl_post_jsonld_array( $arr, $post_id ) { |
|
| 35 | - $jsonld = $arr['jsonld']; |
|
| 36 | - $references = $arr['references']; |
|
| 37 | - |
|
| 38 | - $this->add_mentions( $post_id, $jsonld ); |
|
| 39 | - |
|
| 40 | - return array( |
|
| 41 | - 'jsonld' => $jsonld, |
|
| 42 | - 'references' => $references, |
|
| 43 | - ); |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * Add mentions. |
|
| 48 | - * |
|
| 49 | - * @param $post_id |
|
| 50 | - * @param $jsonld |
|
| 51 | - */ |
|
| 52 | - public function add_mentions( $post_id, &$jsonld ) { |
|
| 53 | - |
|
| 54 | - $taxonomies = Terms_Compat::get_public_taxonomies(); |
|
| 55 | - $terms = array(); |
|
| 56 | - |
|
| 57 | - foreach ( $taxonomies as $taxonomy ) { |
|
| 58 | - $taxonomy_terms = get_the_terms( $post_id, $taxonomy ); |
|
| 59 | - if ( ! $taxonomy_terms ) { |
|
| 60 | - continue; |
|
| 61 | - } |
|
| 62 | - $terms = array_merge( $taxonomy_terms, $terms ); |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - if ( ! $terms ) { |
|
| 66 | - return; |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - if ( ! array_key_exists( 'mentions', $jsonld ) && count( $terms ) > 0 ) { |
|
| 70 | - $jsonld['mentions'] = array(); |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - foreach ( $terms as $term ) { |
|
| 74 | - |
|
| 75 | - $is_matched = intval( get_term_meta( $term->term_id, Entity_Rest_Endpoint::IGNORE_TAG_FROM_LISTING, true ) ) === 1; |
|
| 76 | - |
|
| 77 | - if ( ! $is_matched ) { |
|
| 78 | - continue; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - $entities = Jsonld_Utils::get_matched_entities_for_term( $term->term_id ); |
|
| 82 | - |
|
| 83 | - if ( count( $entities ) === 0 ) { |
|
| 84 | - continue; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - $add_additional_attrs = self::add_additional_attrs( $term, $entities ); |
|
| 88 | - |
|
| 89 | - $jsonld['mentions'] = array_merge( $jsonld['mentions'], $add_additional_attrs ); |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * @param $term \WP_Term |
|
| 96 | - * @param $entities |
|
| 97 | - * |
|
| 98 | - * @return array |
|
| 99 | - */ |
|
| 100 | - public static function add_additional_attrs( $term, $entities ) { |
|
| 101 | - |
|
| 102 | - return array_map( |
|
| 103 | - function ( $entity ) use ( $term ) { |
|
| 104 | - $entity['@id'] = get_term_link( $term->term_id ) . '#id'; |
|
| 105 | - if ( ! empty( $term->description ) ) { |
|
| 106 | - $entity['description'] = $term->description; |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - return $entity; |
|
| 110 | - |
|
| 111 | - }, |
|
| 112 | - $entities |
|
| 113 | - ); |
|
| 114 | - |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * Wl after get jsonld. |
|
| 119 | - * |
|
| 120 | - * @param $jsonld |
|
| 121 | - * |
|
| 122 | - * @return array|mixed |
|
| 123 | - */ |
|
| 124 | - public function wl_after_get_jsonld( $jsonld ) { |
|
| 125 | - |
|
| 126 | - if ( ! is_array( $jsonld ) || count( $jsonld ) === 0 ) { |
|
| 127 | - return $jsonld; |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - foreach ( $jsonld as $key => $value ) { |
|
| 131 | - if ( 'Article' === $value['@type'] && isset( $value['image'] ) ) { |
|
| 132 | - $image = $value['image']; |
|
| 133 | - } |
|
| 134 | - if ( 'Recipe' === $value['@type'] && ! isset( $value['image'] ) ) { |
|
| 135 | - $index = $key; |
|
| 136 | - } |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - if ( isset( $index ) && ! empty( $image ) ) { |
|
| 140 | - $jsonld[ $index ]['image'] = $image; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - return $jsonld; |
|
| 144 | - } |
|
| 18 | + /** |
|
| 19 | + * Enhance post jsonld. |
|
| 20 | + */ |
|
| 21 | + public function enhance_post_jsonld() { |
|
| 22 | + add_filter( 'wl_post_jsonld_array', array( $this, 'wl_post_jsonld_array' ), 11, 2 ); |
|
| 23 | + add_filter( 'wl_after_get_jsonld', array( $this, 'wl_after_get_jsonld' ), 11 ); |
|
| 24 | + } |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * Wl post jsonld array. |
|
| 28 | + * |
|
| 29 | + * @param $arr |
|
| 30 | + * @param $post_id |
|
| 31 | + * |
|
| 32 | + * @return array |
|
| 33 | + */ |
|
| 34 | + public function wl_post_jsonld_array( $arr, $post_id ) { |
|
| 35 | + $jsonld = $arr['jsonld']; |
|
| 36 | + $references = $arr['references']; |
|
| 37 | + |
|
| 38 | + $this->add_mentions( $post_id, $jsonld ); |
|
| 39 | + |
|
| 40 | + return array( |
|
| 41 | + 'jsonld' => $jsonld, |
|
| 42 | + 'references' => $references, |
|
| 43 | + ); |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * Add mentions. |
|
| 48 | + * |
|
| 49 | + * @param $post_id |
|
| 50 | + * @param $jsonld |
|
| 51 | + */ |
|
| 52 | + public function add_mentions( $post_id, &$jsonld ) { |
|
| 53 | + |
|
| 54 | + $taxonomies = Terms_Compat::get_public_taxonomies(); |
|
| 55 | + $terms = array(); |
|
| 56 | + |
|
| 57 | + foreach ( $taxonomies as $taxonomy ) { |
|
| 58 | + $taxonomy_terms = get_the_terms( $post_id, $taxonomy ); |
|
| 59 | + if ( ! $taxonomy_terms ) { |
|
| 60 | + continue; |
|
| 61 | + } |
|
| 62 | + $terms = array_merge( $taxonomy_terms, $terms ); |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + if ( ! $terms ) { |
|
| 66 | + return; |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + if ( ! array_key_exists( 'mentions', $jsonld ) && count( $terms ) > 0 ) { |
|
| 70 | + $jsonld['mentions'] = array(); |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + foreach ( $terms as $term ) { |
|
| 74 | + |
|
| 75 | + $is_matched = intval( get_term_meta( $term->term_id, Entity_Rest_Endpoint::IGNORE_TAG_FROM_LISTING, true ) ) === 1; |
|
| 76 | + |
|
| 77 | + if ( ! $is_matched ) { |
|
| 78 | + continue; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + $entities = Jsonld_Utils::get_matched_entities_for_term( $term->term_id ); |
|
| 82 | + |
|
| 83 | + if ( count( $entities ) === 0 ) { |
|
| 84 | + continue; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + $add_additional_attrs = self::add_additional_attrs( $term, $entities ); |
|
| 88 | + |
|
| 89 | + $jsonld['mentions'] = array_merge( $jsonld['mentions'], $add_additional_attrs ); |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * @param $term \WP_Term |
|
| 96 | + * @param $entities |
|
| 97 | + * |
|
| 98 | + * @return array |
|
| 99 | + */ |
|
| 100 | + public static function add_additional_attrs( $term, $entities ) { |
|
| 101 | + |
|
| 102 | + return array_map( |
|
| 103 | + function ( $entity ) use ( $term ) { |
|
| 104 | + $entity['@id'] = get_term_link( $term->term_id ) . '#id'; |
|
| 105 | + if ( ! empty( $term->description ) ) { |
|
| 106 | + $entity['description'] = $term->description; |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + return $entity; |
|
| 110 | + |
|
| 111 | + }, |
|
| 112 | + $entities |
|
| 113 | + ); |
|
| 114 | + |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * Wl after get jsonld. |
|
| 119 | + * |
|
| 120 | + * @param $jsonld |
|
| 121 | + * |
|
| 122 | + * @return array|mixed |
|
| 123 | + */ |
|
| 124 | + public function wl_after_get_jsonld( $jsonld ) { |
|
| 125 | + |
|
| 126 | + if ( ! is_array( $jsonld ) || count( $jsonld ) === 0 ) { |
|
| 127 | + return $jsonld; |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + foreach ( $jsonld as $key => $value ) { |
|
| 131 | + if ( 'Article' === $value['@type'] && isset( $value['image'] ) ) { |
|
| 132 | + $image = $value['image']; |
|
| 133 | + } |
|
| 134 | + if ( 'Recipe' === $value['@type'] && ! isset( $value['image'] ) ) { |
|
| 135 | + $index = $key; |
|
| 136 | + } |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + if ( isset( $index ) && ! empty( $image ) ) { |
|
| 140 | + $jsonld[ $index ]['image'] = $image; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + return $jsonld; |
|
| 144 | + } |
|
| 145 | 145 | } |
@@ -6,29 +6,29 @@ |
||
| 6 | 6 | |
| 7 | 7 | class Include_Exclude_API { |
| 8 | 8 | |
| 9 | - /** |
|
| 10 | - * @var Api_Service |
|
| 11 | - */ |
|
| 12 | - private $api_service; |
|
| 9 | + /** |
|
| 10 | + * @var Api_Service |
|
| 11 | + */ |
|
| 12 | + private $api_service; |
|
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * @param Api_Service $api_service |
|
| 16 | - */ |
|
| 17 | - public function __construct( Api_Service $api_service ) { |
|
| 18 | - $this->api_service = $api_service; |
|
| 19 | - } |
|
| 14 | + /** |
|
| 15 | + * @param Api_Service $api_service |
|
| 16 | + */ |
|
| 17 | + public function __construct( Api_Service $api_service ) { |
|
| 18 | + $this->api_service = $api_service; |
|
| 19 | + } |
|
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * Update. |
|
| 23 | - * |
|
| 24 | - * @param $payload |
|
| 25 | - */ |
|
| 26 | - public function update( $payload ) { |
|
| 27 | - $this->api_service->request( |
|
| 28 | - 'PUT', |
|
| 29 | - '/accounts/me/include-excludes', |
|
| 30 | - array( 'content-type' => 'application/json' ), |
|
| 31 | - wp_json_encode( $payload ) |
|
| 32 | - ); |
|
| 33 | - } |
|
| 21 | + /** |
|
| 22 | + * Update. |
|
| 23 | + * |
|
| 24 | + * @param $payload |
|
| 25 | + */ |
|
| 26 | + public function update( $payload ) { |
|
| 27 | + $this->api_service->request( |
|
| 28 | + 'PUT', |
|
| 29 | + '/accounts/me/include-excludes', |
|
| 30 | + array( 'content-type' => 'application/json' ), |
|
| 31 | + wp_json_encode( $payload ) |
|
| 32 | + ); |
|
| 33 | + } |
|
| 34 | 34 | } |
@@ -6,60 +6,60 @@ |
||
| 6 | 6 | use Wordlift\Modules\Include_Exclude_Push_Config\Include_Exclude_API; |
| 7 | 7 | |
| 8 | 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | - exit; |
|
| 9 | + exit; |
|
| 10 | 10 | } |
| 11 | 11 | |
| 12 | 12 | // Bail out if the feature isn't enabled. |
| 13 | 13 | if ( ! apply_filters( 'wl_feature__enable__include-exclude', false ) ) { // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 14 | - return; |
|
| 14 | + return; |
|
| 15 | 15 | } |
| 16 | 16 | |
| 17 | 17 | // Autoloader for dependencies. |
| 18 | 18 | if ( file_exists( __DIR__ . '/third-party/vendor/scoper-autoload.php' ) ) { |
| 19 | - require __DIR__ . '/third-party/vendor/scoper-autoload.php'; |
|
| 19 | + require __DIR__ . '/third-party/vendor/scoper-autoload.php'; |
|
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | // Autoloader for plugin itself. |
| 23 | 23 | if ( file_exists( __DIR__ . '/includes/vendor/autoload.php' ) ) { |
| 24 | - require __DIR__ . '/includes/vendor/autoload.php'; |
|
| 24 | + require __DIR__ . '/includes/vendor/autoload.php'; |
|
| 25 | 25 | } |
| 26 | 26 | |
| 27 | 27 | function __wl_include_exclude_push_config() { |
| 28 | - // Get the configuration. |
|
| 29 | - $config = get_option( 'wl_exclude_include_urls_settings', array() ); |
|
| 28 | + // Get the configuration. |
|
| 29 | + $config = get_option( 'wl_exclude_include_urls_settings', array() ); |
|
| 30 | 30 | |
| 31 | - // Set the default data. |
|
| 32 | - if ( ! is_array( $config ) || empty( $config ) || ! isset( $config['include_exclude'] ) || ! isset( $config['urls'] ) ) { |
|
| 33 | - $config = get_option( |
|
| 34 | - 'wl_exclude_include_urls_settings', |
|
| 35 | - array( |
|
| 36 | - 'include_exclude' => 'exclude', |
|
| 37 | - 'urls' => '', |
|
| 38 | - ) |
|
| 39 | - ); |
|
| 40 | - } |
|
| 31 | + // Set the default data. |
|
| 32 | + if ( ! is_array( $config ) || empty( $config ) || ! isset( $config['include_exclude'] ) || ! isset( $config['urls'] ) ) { |
|
| 33 | + $config = get_option( |
|
| 34 | + 'wl_exclude_include_urls_settings', |
|
| 35 | + array( |
|
| 36 | + 'include_exclude' => 'exclude', |
|
| 37 | + 'urls' => '', |
|
| 38 | + ) |
|
| 39 | + ); |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | - // Map the configuration to the payload. |
|
| 43 | - $payload = array_map( |
|
| 44 | - function ( $item ) use ( $config ) { |
|
| 45 | - return array( |
|
| 46 | - 'url' => |
|
| 47 | - ( 1 === preg_match( '@^https?://.*$@', $item ) ? $item : get_home_url( null, $item ) ), |
|
| 48 | - 'flag' => strtoupper( $config['include_exclude'] ), |
|
| 49 | - ); |
|
| 50 | - }, |
|
| 51 | - array_filter( preg_split( '/[\r\n]+/', $config['urls'] ) ) |
|
| 52 | - ); |
|
| 42 | + // Map the configuration to the payload. |
|
| 43 | + $payload = array_map( |
|
| 44 | + function ( $item ) use ( $config ) { |
|
| 45 | + return array( |
|
| 46 | + 'url' => |
|
| 47 | + ( 1 === preg_match( '@^https?://.*$@', $item ) ? $item : get_home_url( null, $item ) ), |
|
| 48 | + 'flag' => strtoupper( $config['include_exclude'] ), |
|
| 49 | + ); |
|
| 50 | + }, |
|
| 51 | + array_filter( preg_split( '/[\r\n]+/', $config['urls'] ) ) |
|
| 52 | + ); |
|
| 53 | 53 | |
| 54 | - // Load the service. |
|
| 55 | - $container_builder = new ContainerBuilder(); |
|
| 56 | - $loader = new YamlFileLoader( $container_builder, new FileLocator( __DIR__ ) ); |
|
| 57 | - $loader->load( 'services.yml' ); |
|
| 58 | - $container_builder->compile(); |
|
| 54 | + // Load the service. |
|
| 55 | + $container_builder = new ContainerBuilder(); |
|
| 56 | + $loader = new YamlFileLoader( $container_builder, new FileLocator( __DIR__ ) ); |
|
| 57 | + $loader->load( 'services.yml' ); |
|
| 58 | + $container_builder->compile(); |
|
| 59 | 59 | |
| 60 | - /** @var Include_Exclude_API $api */ |
|
| 61 | - $api = $container_builder->get( 'Wordlift\Modules\Include_Exclude_Push_Config\Include_Exclude_API' ); |
|
| 62 | - $api->update( $payload ); |
|
| 60 | + /** @var Include_Exclude_API $api */ |
|
| 61 | + $api = $container_builder->get( 'Wordlift\Modules\Include_Exclude_Push_Config\Include_Exclude_API' ); |
|
| 62 | + $api->update( $payload ); |
|
| 63 | 63 | } |
| 64 | 64 | |
| 65 | 65 | /** |
@@ -47,7 +47,7 @@ discard block |
||
| 47 | 47 | * @since 3.33.6 |
| 48 | 48 | */ |
| 49 | 49 | if ( ! apply_filters( 'wl_is_enabled', true ) ) { |
| 50 | - return; |
|
| 50 | + return; |
|
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | require_once plugin_dir_path( __FILE__ ) . 'vendor/autoload.php'; |
@@ -76,33 +76,33 @@ discard block |
||
| 76 | 76 | */ |
| 77 | 77 | function activate_wordlift() { |
| 78 | 78 | |
| 79 | - $log = Wordlift_Log_Service::get_logger( 'activate_wordlift' ); |
|
| 79 | + $log = Wordlift_Log_Service::get_logger( 'activate_wordlift' ); |
|
| 80 | 80 | |
| 81 | - $log->info( 'Activating WordLift...' ); |
|
| 81 | + $log->info( 'Activating WordLift...' ); |
|
| 82 | 82 | |
| 83 | - require_once plugin_dir_path( __FILE__ ) . 'includes/class-wordlift-activator.php'; |
|
| 84 | - Wordlift_Activator::activate(); |
|
| 83 | + require_once plugin_dir_path( __FILE__ ) . 'includes/class-wordlift-activator.php'; |
|
| 84 | + Wordlift_Activator::activate(); |
|
| 85 | 85 | |
| 86 | - /** |
|
| 87 | - * Tell the {@link Wordlift_Http_Api} class that we're activating, to let it run activation tasks. |
|
| 88 | - * |
|
| 89 | - * @see https://github.com/insideout10/wordlift-plugin/issues/820 related issue. |
|
| 90 | - * @since 3.19.2 |
|
| 91 | - */ |
|
| 92 | - Wordlift_Http_Api::activate(); |
|
| 86 | + /** |
|
| 87 | + * Tell the {@link Wordlift_Http_Api} class that we're activating, to let it run activation tasks. |
|
| 88 | + * |
|
| 89 | + * @see https://github.com/insideout10/wordlift-plugin/issues/820 related issue. |
|
| 90 | + * @since 3.19.2 |
|
| 91 | + */ |
|
| 92 | + Wordlift_Http_Api::activate(); |
|
| 93 | 93 | |
| 94 | - // Ensure the post type is registered before flushing the rewrite rules. |
|
| 95 | - Wordlift_Entity_Post_Type_Service::get_instance()->register(); |
|
| 96 | - flush_rewrite_rules(); |
|
| 97 | - /** |
|
| 98 | - * @since 3.27.7 |
|
| 99 | - * @see https://github.com/insideout10/wordlift-plugin/issues/1214 |
|
| 100 | - */ |
|
| 101 | - Top_Entities::activate(); |
|
| 94 | + // Ensure the post type is registered before flushing the rewrite rules. |
|
| 95 | + Wordlift_Entity_Post_Type_Service::get_instance()->register(); |
|
| 96 | + flush_rewrite_rules(); |
|
| 97 | + /** |
|
| 98 | + * @since 3.27.7 |
|
| 99 | + * @see https://github.com/insideout10/wordlift-plugin/issues/1214 |
|
| 100 | + */ |
|
| 101 | + Top_Entities::activate(); |
|
| 102 | 102 | |
| 103 | - if ( ! wp_next_scheduled( 'wl_daily_cron' ) ) { |
|
| 104 | - wp_schedule_event( time(), 'daily', 'wl_daily_cron' ); |
|
| 105 | - } |
|
| 103 | + if ( ! wp_next_scheduled( 'wl_daily_cron' ) ) { |
|
| 104 | + wp_schedule_event( time(), 'daily', 'wl_daily_cron' ); |
|
| 105 | + } |
|
| 106 | 106 | |
| 107 | 107 | } |
| 108 | 108 | |
@@ -112,23 +112,23 @@ discard block |
||
| 112 | 112 | */ |
| 113 | 113 | function deactivate_wordlift() { |
| 114 | 114 | |
| 115 | - require_once plugin_dir_path( __FILE__ ) . 'includes/class-wordlift-deactivator.php'; |
|
| 116 | - Wordlift_Deactivator::deactivate(); |
|
| 117 | - Wordlift_Http_Api::deactivate(); |
|
| 118 | - Ttl_Cache_Cleaner::deactivate(); |
|
| 119 | - /** |
|
| 120 | - * @since 3.27.7 |
|
| 121 | - * @see https://github.com/insideout10/wordlift-plugin/issues/1214 |
|
| 122 | - */ |
|
| 123 | - Top_Entities::deactivate(); |
|
| 124 | - /** |
|
| 125 | - * @since 3.27.8 |
|
| 126 | - * Remove notification flag on deactivation. |
|
| 127 | - */ |
|
| 128 | - Key_Validation_Notice::remove_notification_flag(); |
|
| 129 | - flush_rewrite_rules(); |
|
| 130 | - |
|
| 131 | - wp_clear_scheduled_hook( 'wl_daily_cron' ); |
|
| 115 | + require_once plugin_dir_path( __FILE__ ) . 'includes/class-wordlift-deactivator.php'; |
|
| 116 | + Wordlift_Deactivator::deactivate(); |
|
| 117 | + Wordlift_Http_Api::deactivate(); |
|
| 118 | + Ttl_Cache_Cleaner::deactivate(); |
|
| 119 | + /** |
|
| 120 | + * @since 3.27.7 |
|
| 121 | + * @see https://github.com/insideout10/wordlift-plugin/issues/1214 |
|
| 122 | + */ |
|
| 123 | + Top_Entities::deactivate(); |
|
| 124 | + /** |
|
| 125 | + * @since 3.27.8 |
|
| 126 | + * Remove notification flag on deactivation. |
|
| 127 | + */ |
|
| 128 | + Key_Validation_Notice::remove_notification_flag(); |
|
| 129 | + flush_rewrite_rules(); |
|
| 130 | + |
|
| 131 | + wp_clear_scheduled_hook( 'wl_daily_cron' ); |
|
| 132 | 132 | |
| 133 | 133 | } |
| 134 | 134 | |
@@ -151,84 +151,84 @@ discard block |
||
| 151 | 151 | * @since 1.0.0 |
| 152 | 152 | */ |
| 153 | 153 | function run_wordlift() { |
| 154 | - /** |
|
| 155 | - * Filter: wl_feature__enable__widgets. |
|
| 156 | - * |
|
| 157 | - * @param bool whether the widgets needed to be registered, defaults to true. |
|
| 158 | - * |
|
| 159 | - * @return bool |
|
| 160 | - * @since 3.27.6 |
|
| 161 | - */ |
|
| 162 | - if ( apply_filters( 'wl_feature__enable__widgets', true ) ) { |
|
| 163 | - add_action( 'widgets_init', 'wl_register_chord_widget' ); |
|
| 164 | - add_action( 'widgets_init', 'wl_register_geo_widget' ); |
|
| 165 | - add_action( 'widgets_init', 'wl_register_timeline_widget' ); |
|
| 166 | - } |
|
| 167 | - add_filter( 'widget_text', 'do_shortcode' ); |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * Filter: wl_feature__enable__analysis |
|
| 171 | - * |
|
| 172 | - * @param bool Whether to send api request to analysis or not |
|
| 173 | - * |
|
| 174 | - * @return bool |
|
| 175 | - * @since 3.27.6 |
|
| 176 | - */ |
|
| 177 | - if ( apply_filters( 'wl_feature__enable__analysis', true ) ) { |
|
| 178 | - add_action( 'wp_ajax_wl_analyze', 'wl_ajax_analyze_action' ); |
|
| 179 | - } else { |
|
| 180 | - add_action( 'wp_ajax_wl_analyze', 'wl_ajax_analyze_disabled_action' ); |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - $plugin = new Wordlift(); |
|
| 184 | - $plugin->run(); |
|
| 185 | - |
|
| 186 | - // Initialize the TTL Cache Cleaner. |
|
| 187 | - new Ttl_Cache_Cleaner(); |
|
| 188 | - |
|
| 189 | - // Load the new Post Adapter. |
|
| 190 | - new Post_Adapter(); |
|
| 191 | - |
|
| 192 | - // Load the API Data Hooks. |
|
| 193 | - new Api_Data_Hooks(); |
|
| 194 | - |
|
| 195 | - add_action( |
|
| 196 | - 'plugins_loaded', |
|
| 197 | - function () { |
|
| 198 | - // All features from registry should be initialized here. |
|
| 199 | - $features_registry = Features_Registry::get_instance(); |
|
| 200 | - $features_registry->initialize_all_features(); |
|
| 201 | - }, |
|
| 202 | - 5 |
|
| 203 | - ); |
|
| 204 | - |
|
| 205 | - add_action( |
|
| 206 | - 'plugins_loaded', |
|
| 207 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 208 | - function () use ( $plugin ) { |
|
| 209 | - |
|
| 210 | - new Wordlift_Products_Navigator_Shortcode_REST(); |
|
| 211 | - |
|
| 212 | - // Register the Dataset module, requires `$api_service`. |
|
| 213 | - require_once plugin_dir_path( __FILE__ ) . 'classes/dataset/index.php'; |
|
| 214 | - require_once plugin_dir_path( __FILE__ ) . 'classes/shipping-data/index.php'; |
|
| 215 | - |
|
| 216 | - /* |
|
| 154 | + /** |
|
| 155 | + * Filter: wl_feature__enable__widgets. |
|
| 156 | + * |
|
| 157 | + * @param bool whether the widgets needed to be registered, defaults to true. |
|
| 158 | + * |
|
| 159 | + * @return bool |
|
| 160 | + * @since 3.27.6 |
|
| 161 | + */ |
|
| 162 | + if ( apply_filters( 'wl_feature__enable__widgets', true ) ) { |
|
| 163 | + add_action( 'widgets_init', 'wl_register_chord_widget' ); |
|
| 164 | + add_action( 'widgets_init', 'wl_register_geo_widget' ); |
|
| 165 | + add_action( 'widgets_init', 'wl_register_timeline_widget' ); |
|
| 166 | + } |
|
| 167 | + add_filter( 'widget_text', 'do_shortcode' ); |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * Filter: wl_feature__enable__analysis |
|
| 171 | + * |
|
| 172 | + * @param bool Whether to send api request to analysis or not |
|
| 173 | + * |
|
| 174 | + * @return bool |
|
| 175 | + * @since 3.27.6 |
|
| 176 | + */ |
|
| 177 | + if ( apply_filters( 'wl_feature__enable__analysis', true ) ) { |
|
| 178 | + add_action( 'wp_ajax_wl_analyze', 'wl_ajax_analyze_action' ); |
|
| 179 | + } else { |
|
| 180 | + add_action( 'wp_ajax_wl_analyze', 'wl_ajax_analyze_disabled_action' ); |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + $plugin = new Wordlift(); |
|
| 184 | + $plugin->run(); |
|
| 185 | + |
|
| 186 | + // Initialize the TTL Cache Cleaner. |
|
| 187 | + new Ttl_Cache_Cleaner(); |
|
| 188 | + |
|
| 189 | + // Load the new Post Adapter. |
|
| 190 | + new Post_Adapter(); |
|
| 191 | + |
|
| 192 | + // Load the API Data Hooks. |
|
| 193 | + new Api_Data_Hooks(); |
|
| 194 | + |
|
| 195 | + add_action( |
|
| 196 | + 'plugins_loaded', |
|
| 197 | + function () { |
|
| 198 | + // All features from registry should be initialized here. |
|
| 199 | + $features_registry = Features_Registry::get_instance(); |
|
| 200 | + $features_registry->initialize_all_features(); |
|
| 201 | + }, |
|
| 202 | + 5 |
|
| 203 | + ); |
|
| 204 | + |
|
| 205 | + add_action( |
|
| 206 | + 'plugins_loaded', |
|
| 207 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 208 | + function () use ( $plugin ) { |
|
| 209 | + |
|
| 210 | + new Wordlift_Products_Navigator_Shortcode_REST(); |
|
| 211 | + |
|
| 212 | + // Register the Dataset module, requires `$api_service`. |
|
| 213 | + require_once plugin_dir_path( __FILE__ ) . 'classes/dataset/index.php'; |
|
| 214 | + require_once plugin_dir_path( __FILE__ ) . 'classes/shipping-data/index.php'; |
|
| 215 | + |
|
| 216 | + /* |
|
| 217 | 217 | * Require the Entity annotation cleanup module. |
| 218 | 218 | * |
| 219 | 219 | * @since 3.34.6 |
| 220 | 220 | */ |
| 221 | - require_once plugin_dir_path( __FILE__ ) . 'classes/cleanup/index.php'; |
|
| 221 | + require_once plugin_dir_path( __FILE__ ) . 'classes/cleanup/index.php'; |
|
| 222 | 222 | |
| 223 | - /* |
|
| 223 | + /* |
|
| 224 | 224 | * Import LOD entities. |
| 225 | 225 | * |
| 226 | 226 | * @since 3.35.0 |
| 227 | 227 | */ |
| 228 | - require_once plugin_dir_path( __FILE__ ) . 'classes/lod-import/index.php'; |
|
| 228 | + require_once plugin_dir_path( __FILE__ ) . 'classes/lod-import/index.php'; |
|
| 229 | 229 | |
| 230 | - } |
|
| 231 | - ); |
|
| 230 | + } |
|
| 231 | + ); |
|
| 232 | 232 | |
| 233 | 233 | } |
| 234 | 234 | |
@@ -242,50 +242,50 @@ discard block |
||
| 242 | 242 | */ |
| 243 | 243 | function wordlift_plugin_autoload_register() { |
| 244 | 244 | |
| 245 | - spl_autoload_register( |
|
| 246 | - function ( $class_name ) { |
|
| 245 | + spl_autoload_register( |
|
| 246 | + function ( $class_name ) { |
|
| 247 | 247 | |
| 248 | - // Bail out if these are not our classes. |
|
| 249 | - if ( 0 !== strpos( $class_name, 'Wordlift\\' ) ) { |
|
| 250 | - return false; |
|
| 251 | - } |
|
| 248 | + // Bail out if these are not our classes. |
|
| 249 | + if ( 0 !== strpos( $class_name, 'Wordlift\\' ) ) { |
|
| 250 | + return false; |
|
| 251 | + } |
|
| 252 | 252 | |
| 253 | - $class_name_lc = strtolower( str_replace( '_', '-', $class_name ) ); |
|
| 253 | + $class_name_lc = strtolower( str_replace( '_', '-', $class_name ) ); |
|
| 254 | 254 | |
| 255 | - preg_match( '|^wordlift\\\\(?:(.*)\\\\)?(.+?)$|', $class_name_lc, $matches ); |
|
| 255 | + preg_match( '|^wordlift\\\\(?:(.*)\\\\)?(.+?)$|', $class_name_lc, $matches ); |
|
| 256 | 256 | |
| 257 | - $path = str_replace( '\\', DIRECTORY_SEPARATOR, $matches[1] ); |
|
| 258 | - $file = 'class-' . $matches[2] . '.php'; |
|
| 257 | + $path = str_replace( '\\', DIRECTORY_SEPARATOR, $matches[1] ); |
|
| 258 | + $file = 'class-' . $matches[2] . '.php'; |
|
| 259 | 259 | |
| 260 | - $full_path = plugin_dir_path( __FILE__ ) . 'classes' . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . $file; |
|
| 260 | + $full_path = plugin_dir_path( __FILE__ ) . 'classes' . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . $file; |
|
| 261 | 261 | |
| 262 | - if ( ! file_exists( $full_path ) ) { |
|
| 263 | - return false; |
|
| 264 | - } |
|
| 262 | + if ( ! file_exists( $full_path ) ) { |
|
| 263 | + return false; |
|
| 264 | + } |
|
| 265 | 265 | |
| 266 | - try { |
|
| 267 | - require_once $full_path; |
|
| 268 | - } catch ( Exception $e ) { |
|
| 269 | - // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log |
|
| 270 | - error_log( "[WordLift] $full_path not found, cannot include." ); |
|
| 271 | - } |
|
| 266 | + try { |
|
| 267 | + require_once $full_path; |
|
| 268 | + } catch ( Exception $e ) { |
|
| 269 | + // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log |
|
| 270 | + error_log( "[WordLift] $full_path not found, cannot include." ); |
|
| 271 | + } |
|
| 272 | 272 | |
| 273 | - return true; |
|
| 274 | - } |
|
| 275 | - ); |
|
| 273 | + return true; |
|
| 274 | + } |
|
| 275 | + ); |
|
| 276 | 276 | |
| 277 | 277 | } |
| 278 | 278 | |
| 279 | 279 | function wl_block_categories( $categories ) { |
| 280 | - return array_merge( |
|
| 281 | - $categories, |
|
| 282 | - array( |
|
| 283 | - array( |
|
| 284 | - 'slug' => 'wordlift', |
|
| 285 | - 'title' => __( 'WordLift', 'wordlift' ), |
|
| 286 | - ), |
|
| 287 | - ) |
|
| 288 | - ); |
|
| 280 | + return array_merge( |
|
| 281 | + $categories, |
|
| 282 | + array( |
|
| 283 | + array( |
|
| 284 | + 'slug' => 'wordlift', |
|
| 285 | + 'title' => __( 'WordLift', 'wordlift' ), |
|
| 286 | + ), |
|
| 287 | + ) |
|
| 288 | + ); |
|
| 289 | 289 | } |
| 290 | 290 | |
| 291 | 291 | /** |
@@ -293,19 +293,19 @@ discard block |
||
| 293 | 293 | * this has to be removed when removing the legacy fields from the ui. |
| 294 | 294 | */ |
| 295 | 295 | function wl_enqueue_leaflet( $in_footer = false ) { |
| 296 | - // Leaflet. |
|
| 297 | - wp_enqueue_style( 'wl-leaflet', plugin_dir_url( __FILE__ ) . 'js/leaflet/leaflet.css', array(), '1.6.0' ); |
|
| 298 | - wp_enqueue_script( 'wl-leaflet', plugin_dir_url( __FILE__ ) . 'js/leaflet/leaflet.js', array(), '1.6.0', $in_footer ); |
|
| 296 | + // Leaflet. |
|
| 297 | + wp_enqueue_style( 'wl-leaflet', plugin_dir_url( __FILE__ ) . 'js/leaflet/leaflet.css', array(), '1.6.0' ); |
|
| 298 | + wp_enqueue_script( 'wl-leaflet', plugin_dir_url( __FILE__ ) . 'js/leaflet/leaflet.js', array(), '1.6.0', $in_footer ); |
|
| 299 | 299 | } |
| 300 | 300 | |
| 301 | 301 | add_filter( 'block_categories', 'wl_block_categories', 10 ); |
| 302 | 302 | |
| 303 | 303 | // Temporary fix for a typo in WooCommerce Extension. |
| 304 | 304 | add_filter( |
| 305 | - 'wl_feature__enable__dataset', |
|
| 306 | - function ( $value ) { |
|
| 307 | - return apply_filters( 'wl_features__enable__dataset', $value ); |
|
| 308 | - } |
|
| 305 | + 'wl_feature__enable__dataset', |
|
| 306 | + function ( $value ) { |
|
| 307 | + return apply_filters( 'wl_features__enable__dataset', $value ); |
|
| 308 | + } |
|
| 309 | 309 | ); |
| 310 | 310 | |
| 311 | 311 | require_once __DIR__ . '/modules/food-kg/load.php'; |
@@ -320,35 +320,35 @@ discard block |
||
| 320 | 320 | require_once __DIR__ . '/modules/events/load.php'; |
| 321 | 321 | |
| 322 | 322 | function _wl_update_plugins_raptive_domain( $update, $plugin_data, $plugin_file ) { |
| 323 | - // Bail out if it's not our plugin. |
|
| 324 | - $update_uri = $plugin_data['UpdateURI']; |
|
| 325 | - if ( 'wordlift/wordlift.php' !== $plugin_file || ! isset( $update_uri ) ) { |
|
| 326 | - return $update; |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - $response = wp_remote_get( "$update_uri?nocache=" . time() ); |
|
| 330 | - |
|
| 331 | - if ( is_wp_error( $response ) ) { |
|
| 332 | - return $update; |
|
| 333 | - } |
|
| 334 | - |
|
| 335 | - try { |
|
| 336 | - return json_decode( wp_remote_retrieve_body( $response ) ); |
|
| 337 | - } catch ( Exception $e ) { |
|
| 338 | - return $update; |
|
| 339 | - } |
|
| 323 | + // Bail out if it's not our plugin. |
|
| 324 | + $update_uri = $plugin_data['UpdateURI']; |
|
| 325 | + if ( 'wordlift/wordlift.php' !== $plugin_file || ! isset( $update_uri ) ) { |
|
| 326 | + return $update; |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + $response = wp_remote_get( "$update_uri?nocache=" . time() ); |
|
| 330 | + |
|
| 331 | + if ( is_wp_error( $response ) ) { |
|
| 332 | + return $update; |
|
| 333 | + } |
|
| 334 | + |
|
| 335 | + try { |
|
| 336 | + return json_decode( wp_remote_retrieve_body( $response ) ); |
|
| 337 | + } catch ( Exception $e ) { |
|
| 338 | + return $update; |
|
| 339 | + } |
|
| 340 | 340 | } |
| 341 | 341 | |
| 342 | 342 | add_action( |
| 343 | - 'update_plugins_adthrive.wordlift.io', |
|
| 344 | - '_wl_update_plugins_raptive_domain', |
|
| 345 | - 10, |
|
| 346 | - 3 |
|
| 343 | + 'update_plugins_adthrive.wordlift.io', |
|
| 344 | + '_wl_update_plugins_raptive_domain', |
|
| 345 | + 10, |
|
| 346 | + 3 |
|
| 347 | 347 | ); |
| 348 | 348 | |
| 349 | 349 | add_action( |
| 350 | - 'update_plugins_raptive.wordlift.io', |
|
| 351 | - '_wl_update_plugins_raptive_domain', |
|
| 352 | - 10, |
|
| 353 | - 3 |
|
| 350 | + 'update_plugins_raptive.wordlift.io', |
|
| 351 | + '_wl_update_plugins_raptive_domain', |
|
| 352 | + 10, |
|
| 353 | + 3 |
|
| 354 | 354 | ); |
@@ -12,115 +12,115 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class Events_Post_Entity_Jsonld { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * The {@link Api_Service} used to communicate with the remote APIs. |
|
| 17 | - * |
|
| 18 | - * @access private |
|
| 19 | - * @var Api_Service |
|
| 20 | - */ |
|
| 21 | - private $api_service; |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * @param Api_Service $api_service |
|
| 25 | - */ |
|
| 26 | - public function __construct( Api_Service $api_service ) { |
|
| 27 | - $this->api_service = $api_service; |
|
| 28 | - } |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * Register hooks. |
|
| 32 | - */ |
|
| 33 | - public function register_hooks() { |
|
| 34 | - add_filter( 'wl_after_get_jsonld', array( $this, 'set_events_request' ), 90, 3 ); |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * Set events request. |
|
| 39 | - * |
|
| 40 | - * @param $jsonld_arr array The final jsonld before outputting to page. |
|
| 41 | - * @param $post_id int The post id for which the jsonld is generated. |
|
| 42 | - * @param $context int A context for the JSON-LD generation, valid values in Jsonld_Context_Enum |
|
| 43 | - * |
|
| 44 | - * @return array |
|
| 45 | - */ |
|
| 46 | - public function set_events_request( $jsonld_arr, $post_id, $context ) { |
|
| 47 | - // If context is not PAGE, return early. |
|
| 48 | - if ( Jsonld_Context_Enum::PAGE !== $context ) { |
|
| 49 | - return $jsonld_arr; |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - // Flag to indicate if we should make an API request. |
|
| 53 | - $change_status = false; |
|
| 54 | - |
|
| 55 | - // Fetch the initial 'about' and 'mentions' counts from post meta. |
|
| 56 | - $counts = array( |
|
| 57 | - 'about' => get_post_meta( $post_id, 'wl_about_count', true ) ? (int) get_post_meta( $post_id, 'wl_about_count', true ) : 0, |
|
| 58 | - 'mentions' => get_post_meta( $post_id, 'wl_mentions_count', true ) |
|
| 59 | - ? (int) get_post_meta( $post_id, 'wl_mentions_count', true ) |
|
| 60 | - : 0, |
|
| 61 | - ); |
|
| 62 | - |
|
| 63 | - // Check if $jsonld_arr is not empty |
|
| 64 | - if ( ! empty( $jsonld_arr[0] ) ) { |
|
| 65 | - // Get data from the array. |
|
| 66 | - $data = $jsonld_arr[0]; |
|
| 67 | - |
|
| 68 | - // Iterate over the counts array. |
|
| 69 | - foreach ( $counts as $type => $type_count ) { |
|
| 70 | - // Check if data has 'about' or 'mentions' and the count is different from the existing meta value. |
|
| 71 | - if ( isset( $data[ $type ] ) ) { |
|
| 72 | - $new_count = count( $data[ $type ] ); |
|
| 73 | - |
|
| 74 | - if ( $type_count !== $new_count ) { |
|
| 75 | - // Set flag to true if counts have changed. |
|
| 76 | - $change_status = true; |
|
| 77 | - |
|
| 78 | - // Update the counts array with new count. |
|
| 79 | - $counts[ $type ] = $new_count; |
|
| 80 | - |
|
| 81 | - // Update post meta with new count. |
|
| 82 | - update_post_meta( $post_id, 'wl_' . $type . '_count', $new_count ); |
|
| 83 | - } |
|
| 84 | - } elseif ( $type_count > 0 ) { |
|
| 85 | - // If the 'about' or 'mentions' has become empty, set it to 0. |
|
| 86 | - $change_status = true; |
|
| 87 | - $counts[ $type ] = 0; |
|
| 88 | - update_post_meta( $post_id, 'wl_' . $type . '_count', 0 ); |
|
| 89 | - } |
|
| 90 | - } |
|
| 91 | - } else { |
|
| 92 | - // If the $jsonld_arr is empty but the counts were previously more than 0. |
|
| 93 | - foreach ( $counts as $type => $type_count ) { |
|
| 94 | - if ( $type_count > 0 ) { |
|
| 95 | - $change_status = true; |
|
| 96 | - $counts[ $type ] = 0; |
|
| 97 | - update_post_meta( $post_id, 'wl_' . $type . '_count', 0 ); |
|
| 98 | - } |
|
| 99 | - } |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - // If the count has changed, make the API request. |
|
| 103 | - if ( $change_status ) { |
|
| 104 | - $this->api_service->request( |
|
| 105 | - 'POST', |
|
| 106 | - '/plugin/events', |
|
| 107 | - array( 'Content-Type' => 'application/json' ), |
|
| 108 | - wp_json_encode( |
|
| 109 | - array( |
|
| 110 | - 'source' => 'jsonld', |
|
| 111 | - 'args' => array( |
|
| 112 | - array( 'about_count' => $counts['about'] ), |
|
| 113 | - array( 'mentions_count' => $counts['mentions'] ), |
|
| 114 | - ), |
|
| 115 | - 'url' => get_permalink( $post_id ), |
|
| 116 | - ) |
|
| 117 | - ), |
|
| 118 | - 0.001, |
|
| 119 | - null, |
|
| 120 | - array( 'blocking' => false ) |
|
| 121 | - ); |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - return $jsonld_arr; |
|
| 125 | - } |
|
| 15 | + /** |
|
| 16 | + * The {@link Api_Service} used to communicate with the remote APIs. |
|
| 17 | + * |
|
| 18 | + * @access private |
|
| 19 | + * @var Api_Service |
|
| 20 | + */ |
|
| 21 | + private $api_service; |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * @param Api_Service $api_service |
|
| 25 | + */ |
|
| 26 | + public function __construct( Api_Service $api_service ) { |
|
| 27 | + $this->api_service = $api_service; |
|
| 28 | + } |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * Register hooks. |
|
| 32 | + */ |
|
| 33 | + public function register_hooks() { |
|
| 34 | + add_filter( 'wl_after_get_jsonld', array( $this, 'set_events_request' ), 90, 3 ); |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * Set events request. |
|
| 39 | + * |
|
| 40 | + * @param $jsonld_arr array The final jsonld before outputting to page. |
|
| 41 | + * @param $post_id int The post id for which the jsonld is generated. |
|
| 42 | + * @param $context int A context for the JSON-LD generation, valid values in Jsonld_Context_Enum |
|
| 43 | + * |
|
| 44 | + * @return array |
|
| 45 | + */ |
|
| 46 | + public function set_events_request( $jsonld_arr, $post_id, $context ) { |
|
| 47 | + // If context is not PAGE, return early. |
|
| 48 | + if ( Jsonld_Context_Enum::PAGE !== $context ) { |
|
| 49 | + return $jsonld_arr; |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + // Flag to indicate if we should make an API request. |
|
| 53 | + $change_status = false; |
|
| 54 | + |
|
| 55 | + // Fetch the initial 'about' and 'mentions' counts from post meta. |
|
| 56 | + $counts = array( |
|
| 57 | + 'about' => get_post_meta( $post_id, 'wl_about_count', true ) ? (int) get_post_meta( $post_id, 'wl_about_count', true ) : 0, |
|
| 58 | + 'mentions' => get_post_meta( $post_id, 'wl_mentions_count', true ) |
|
| 59 | + ? (int) get_post_meta( $post_id, 'wl_mentions_count', true ) |
|
| 60 | + : 0, |
|
| 61 | + ); |
|
| 62 | + |
|
| 63 | + // Check if $jsonld_arr is not empty |
|
| 64 | + if ( ! empty( $jsonld_arr[0] ) ) { |
|
| 65 | + // Get data from the array. |
|
| 66 | + $data = $jsonld_arr[0]; |
|
| 67 | + |
|
| 68 | + // Iterate over the counts array. |
|
| 69 | + foreach ( $counts as $type => $type_count ) { |
|
| 70 | + // Check if data has 'about' or 'mentions' and the count is different from the existing meta value. |
|
| 71 | + if ( isset( $data[ $type ] ) ) { |
|
| 72 | + $new_count = count( $data[ $type ] ); |
|
| 73 | + |
|
| 74 | + if ( $type_count !== $new_count ) { |
|
| 75 | + // Set flag to true if counts have changed. |
|
| 76 | + $change_status = true; |
|
| 77 | + |
|
| 78 | + // Update the counts array with new count. |
|
| 79 | + $counts[ $type ] = $new_count; |
|
| 80 | + |
|
| 81 | + // Update post meta with new count. |
|
| 82 | + update_post_meta( $post_id, 'wl_' . $type . '_count', $new_count ); |
|
| 83 | + } |
|
| 84 | + } elseif ( $type_count > 0 ) { |
|
| 85 | + // If the 'about' or 'mentions' has become empty, set it to 0. |
|
| 86 | + $change_status = true; |
|
| 87 | + $counts[ $type ] = 0; |
|
| 88 | + update_post_meta( $post_id, 'wl_' . $type . '_count', 0 ); |
|
| 89 | + } |
|
| 90 | + } |
|
| 91 | + } else { |
|
| 92 | + // If the $jsonld_arr is empty but the counts were previously more than 0. |
|
| 93 | + foreach ( $counts as $type => $type_count ) { |
|
| 94 | + if ( $type_count > 0 ) { |
|
| 95 | + $change_status = true; |
|
| 96 | + $counts[ $type ] = 0; |
|
| 97 | + update_post_meta( $post_id, 'wl_' . $type . '_count', 0 ); |
|
| 98 | + } |
|
| 99 | + } |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + // If the count has changed, make the API request. |
|
| 103 | + if ( $change_status ) { |
|
| 104 | + $this->api_service->request( |
|
| 105 | + 'POST', |
|
| 106 | + '/plugin/events', |
|
| 107 | + array( 'Content-Type' => 'application/json' ), |
|
| 108 | + wp_json_encode( |
|
| 109 | + array( |
|
| 110 | + 'source' => 'jsonld', |
|
| 111 | + 'args' => array( |
|
| 112 | + array( 'about_count' => $counts['about'] ), |
|
| 113 | + array( 'mentions_count' => $counts['mentions'] ), |
|
| 114 | + ), |
|
| 115 | + 'url' => get_permalink( $post_id ), |
|
| 116 | + ) |
|
| 117 | + ), |
|
| 118 | + 0.001, |
|
| 119 | + null, |
|
| 120 | + array( 'blocking' => false ) |
|
| 121 | + ); |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + return $jsonld_arr; |
|
| 125 | + } |
|
| 126 | 126 | } |
@@ -11,161 +11,161 @@ |
||
| 11 | 11 | * @package Wordlift\Modules\Events\Options_Entity |
| 12 | 12 | */ |
| 13 | 13 | class Events_Options_Entity_Include_Exclude { |
| 14 | - /** |
|
| 15 | - * The {@link Api_Service} used to communicate with the remote APIs. |
|
| 16 | - * |
|
| 17 | - * @access private |
|
| 18 | - * @var Api_Service |
|
| 19 | - */ |
|
| 20 | - private $api_service; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * @param Api_Service $api_service |
|
| 24 | - */ |
|
| 25 | - public function __construct( Api_Service $api_service ) { |
|
| 26 | - $this->api_service = $api_service; |
|
| 27 | - } |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * Register hooks. |
|
| 31 | - */ |
|
| 32 | - public function register_hooks() { |
|
| 33 | - add_action( 'update_option_wl_exclude_include_urls_settings', array( $this, 'event_update' ), 15, 0 ); |
|
| 34 | - add_action( 'update_option_wl_exclude_include_urls_settings', array( $this, 'save_old_config' ), 99, 0 ); |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * Include exclude event update. |
|
| 39 | - * |
|
| 40 | - * @throws Exception If the application fails to load the services configuration file or if the URL cannot be processed. |
|
| 41 | - */ |
|
| 42 | - public function event_update() { |
|
| 43 | - // Get the configurations. |
|
| 44 | - $config = get_option( 'wl_exclude_include_urls_settings', array() ); |
|
| 45 | - $old_config = get_option( 'wl_exclude_include_urls_settings_old', array() ); |
|
| 46 | - |
|
| 47 | - // Get included and excluded URLs. |
|
| 48 | - $urls = $this->get_urls( $config, $old_config ); |
|
| 49 | - |
|
| 50 | - // Call API method for each URL. |
|
| 51 | - foreach ( $urls['included'] as $url ) { |
|
| 52 | - $this->send_event( $url, 'include' ); |
|
| 53 | - } |
|
| 54 | - foreach ( $urls['excluded'] as $url ) { |
|
| 55 | - $this->send_event( $url, 'exclude' ); |
|
| 56 | - } |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * Save old config. |
|
| 61 | - */ |
|
| 62 | - public function save_old_config() { |
|
| 63 | - // Get the current configuration. |
|
| 64 | - $config = get_option( 'wl_exclude_include_urls_settings', array() ); |
|
| 65 | - |
|
| 66 | - // Save the current configuration to another option. |
|
| 67 | - update_option( 'wl_exclude_include_urls_settings_old', $config ); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * Get included and excluded urls. |
|
| 72 | - * |
|
| 73 | - * @param $config |
|
| 74 | - * @param $old_config |
|
| 75 | - * |
|
| 76 | - * @return array |
|
| 77 | - */ |
|
| 78 | - private function get_urls( $config, $old_config ) { |
|
| 79 | - // Get the payload for both new and old values: |
|
| 80 | - $payload_new = $this->get_payload( $config ); |
|
| 81 | - $payload_old = $this->get_payload( $old_config ); |
|
| 82 | - |
|
| 83 | - // Extract URLs from payloads. |
|
| 84 | - $urls_new = array_column( $payload_new, 'url' ); |
|
| 85 | - $urls_old = array_column( $payload_old, 'url' ); |
|
| 86 | - |
|
| 87 | - // If both $urls_new and $urls_old are empty, there is no URL to process. |
|
| 88 | - if ( empty( $urls_new ) && empty( $urls_old ) ) { |
|
| 89 | - return array( |
|
| 90 | - 'included' => array(), |
|
| 91 | - 'excluded' => array(), |
|
| 92 | - ); |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - // Find added and removed URLs. |
|
| 96 | - $urls_added = array_diff( $urls_new, $urls_old ); |
|
| 97 | - $urls_removed = array_diff( $urls_old, $urls_new ); |
|
| 98 | - |
|
| 99 | - // Determine included and excluded URLs. |
|
| 100 | - $included = ( 'include' === strtolower( $config['include_exclude'] ) ) ? $urls_added : $urls_removed; |
|
| 101 | - $excluded = ( 'include' === strtolower( $config['include_exclude'] ) ) ? $urls_removed : $urls_added; |
|
| 102 | - |
|
| 103 | - // Check if filter type has changed. |
|
| 104 | - $filter_changed = strtolower( $config['include_exclude'] ) !== strtolower( $old_config['include_exclude'] ); |
|
| 105 | - |
|
| 106 | - if ( $filter_changed ) { |
|
| 107 | - // Filter type changed, so we reverse the logic of adding URLs to included and excluded arrays. |
|
| 108 | - $included = ( 'include' === strtolower( $config['include_exclude'] ) ) ? $urls_new : $urls_removed; |
|
| 109 | - $excluded = ( 'include' === strtolower( $config['include_exclude'] ) ) ? $urls_removed : $urls_new; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - return array( |
|
| 113 | - 'included' => $included, |
|
| 114 | - 'excluded' => $excluded, |
|
| 115 | - ); |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * Get payload. |
|
| 120 | - * |
|
| 121 | - * @param $config |
|
| 122 | - * |
|
| 123 | - * @return array|array[] |
|
| 124 | - */ |
|
| 125 | - private function get_payload( $config ) { |
|
| 126 | - // Set the default data. |
|
| 127 | - if ( ! is_array( $config ) || empty( $config ) || ! isset( $config['include_exclude'] ) || ! isset( $config['urls'] ) ) { |
|
| 128 | - $config = array( |
|
| 129 | - 'include_exclude' => 'exclude', |
|
| 130 | - 'urls' => '', |
|
| 131 | - ); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - // Map the configuration to the payload. |
|
| 135 | - return array_map( |
|
| 136 | - function ( $item ) use ( $config ) { |
|
| 137 | - return array( |
|
| 138 | - 'url' => ( 1 === preg_match( '@^https?://.*$@', $item ) ? $item : get_home_url( null, $item ) ), |
|
| 139 | - 'flag' => strtoupper( $config['include_exclude'] ), |
|
| 140 | - ); |
|
| 141 | - }, |
|
| 142 | - array_filter( preg_split( '/[\r\n]+/', $config['urls'] ) ) |
|
| 143 | - ); |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * Send event. |
|
| 148 | - * |
|
| 149 | - * @param $url |
|
| 150 | - * @param $value |
|
| 151 | - */ |
|
| 152 | - private function send_event( $url, $value ) { |
|
| 153 | - $this->api_service->request( |
|
| 154 | - 'POST', |
|
| 155 | - '/plugin/events', |
|
| 156 | - array( 'content-type' => 'application/json' ), |
|
| 157 | - wp_json_encode( |
|
| 158 | - array( |
|
| 159 | - 'source' => 'include-exclude', |
|
| 160 | - 'args' => array( |
|
| 161 | - array( 'value' => $value ), |
|
| 162 | - ), |
|
| 163 | - 'url' => $url, |
|
| 164 | - ) |
|
| 165 | - ), |
|
| 166 | - 0.001, |
|
| 167 | - null, |
|
| 168 | - array( 'blocking' => false ) |
|
| 169 | - ); |
|
| 170 | - } |
|
| 14 | + /** |
|
| 15 | + * The {@link Api_Service} used to communicate with the remote APIs. |
|
| 16 | + * |
|
| 17 | + * @access private |
|
| 18 | + * @var Api_Service |
|
| 19 | + */ |
|
| 20 | + private $api_service; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * @param Api_Service $api_service |
|
| 24 | + */ |
|
| 25 | + public function __construct( Api_Service $api_service ) { |
|
| 26 | + $this->api_service = $api_service; |
|
| 27 | + } |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * Register hooks. |
|
| 31 | + */ |
|
| 32 | + public function register_hooks() { |
|
| 33 | + add_action( 'update_option_wl_exclude_include_urls_settings', array( $this, 'event_update' ), 15, 0 ); |
|
| 34 | + add_action( 'update_option_wl_exclude_include_urls_settings', array( $this, 'save_old_config' ), 99, 0 ); |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * Include exclude event update. |
|
| 39 | + * |
|
| 40 | + * @throws Exception If the application fails to load the services configuration file or if the URL cannot be processed. |
|
| 41 | + */ |
|
| 42 | + public function event_update() { |
|
| 43 | + // Get the configurations. |
|
| 44 | + $config = get_option( 'wl_exclude_include_urls_settings', array() ); |
|
| 45 | + $old_config = get_option( 'wl_exclude_include_urls_settings_old', array() ); |
|
| 46 | + |
|
| 47 | + // Get included and excluded URLs. |
|
| 48 | + $urls = $this->get_urls( $config, $old_config ); |
|
| 49 | + |
|
| 50 | + // Call API method for each URL. |
|
| 51 | + foreach ( $urls['included'] as $url ) { |
|
| 52 | + $this->send_event( $url, 'include' ); |
|
| 53 | + } |
|
| 54 | + foreach ( $urls['excluded'] as $url ) { |
|
| 55 | + $this->send_event( $url, 'exclude' ); |
|
| 56 | + } |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * Save old config. |
|
| 61 | + */ |
|
| 62 | + public function save_old_config() { |
|
| 63 | + // Get the current configuration. |
|
| 64 | + $config = get_option( 'wl_exclude_include_urls_settings', array() ); |
|
| 65 | + |
|
| 66 | + // Save the current configuration to another option. |
|
| 67 | + update_option( 'wl_exclude_include_urls_settings_old', $config ); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * Get included and excluded urls. |
|
| 72 | + * |
|
| 73 | + * @param $config |
|
| 74 | + * @param $old_config |
|
| 75 | + * |
|
| 76 | + * @return array |
|
| 77 | + */ |
|
| 78 | + private function get_urls( $config, $old_config ) { |
|
| 79 | + // Get the payload for both new and old values: |
|
| 80 | + $payload_new = $this->get_payload( $config ); |
|
| 81 | + $payload_old = $this->get_payload( $old_config ); |
|
| 82 | + |
|
| 83 | + // Extract URLs from payloads. |
|
| 84 | + $urls_new = array_column( $payload_new, 'url' ); |
|
| 85 | + $urls_old = array_column( $payload_old, 'url' ); |
|
| 86 | + |
|
| 87 | + // If both $urls_new and $urls_old are empty, there is no URL to process. |
|
| 88 | + if ( empty( $urls_new ) && empty( $urls_old ) ) { |
|
| 89 | + return array( |
|
| 90 | + 'included' => array(), |
|
| 91 | + 'excluded' => array(), |
|
| 92 | + ); |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + // Find added and removed URLs. |
|
| 96 | + $urls_added = array_diff( $urls_new, $urls_old ); |
|
| 97 | + $urls_removed = array_diff( $urls_old, $urls_new ); |
|
| 98 | + |
|
| 99 | + // Determine included and excluded URLs. |
|
| 100 | + $included = ( 'include' === strtolower( $config['include_exclude'] ) ) ? $urls_added : $urls_removed; |
|
| 101 | + $excluded = ( 'include' === strtolower( $config['include_exclude'] ) ) ? $urls_removed : $urls_added; |
|
| 102 | + |
|
| 103 | + // Check if filter type has changed. |
|
| 104 | + $filter_changed = strtolower( $config['include_exclude'] ) !== strtolower( $old_config['include_exclude'] ); |
|
| 105 | + |
|
| 106 | + if ( $filter_changed ) { |
|
| 107 | + // Filter type changed, so we reverse the logic of adding URLs to included and excluded arrays. |
|
| 108 | + $included = ( 'include' === strtolower( $config['include_exclude'] ) ) ? $urls_new : $urls_removed; |
|
| 109 | + $excluded = ( 'include' === strtolower( $config['include_exclude'] ) ) ? $urls_removed : $urls_new; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + return array( |
|
| 113 | + 'included' => $included, |
|
| 114 | + 'excluded' => $excluded, |
|
| 115 | + ); |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * Get payload. |
|
| 120 | + * |
|
| 121 | + * @param $config |
|
| 122 | + * |
|
| 123 | + * @return array|array[] |
|
| 124 | + */ |
|
| 125 | + private function get_payload( $config ) { |
|
| 126 | + // Set the default data. |
|
| 127 | + if ( ! is_array( $config ) || empty( $config ) || ! isset( $config['include_exclude'] ) || ! isset( $config['urls'] ) ) { |
|
| 128 | + $config = array( |
|
| 129 | + 'include_exclude' => 'exclude', |
|
| 130 | + 'urls' => '', |
|
| 131 | + ); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + // Map the configuration to the payload. |
|
| 135 | + return array_map( |
|
| 136 | + function ( $item ) use ( $config ) { |
|
| 137 | + return array( |
|
| 138 | + 'url' => ( 1 === preg_match( '@^https?://.*$@', $item ) ? $item : get_home_url( null, $item ) ), |
|
| 139 | + 'flag' => strtoupper( $config['include_exclude'] ), |
|
| 140 | + ); |
|
| 141 | + }, |
|
| 142 | + array_filter( preg_split( '/[\r\n]+/', $config['urls'] ) ) |
|
| 143 | + ); |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * Send event. |
|
| 148 | + * |
|
| 149 | + * @param $url |
|
| 150 | + * @param $value |
|
| 151 | + */ |
|
| 152 | + private function send_event( $url, $value ) { |
|
| 153 | + $this->api_service->request( |
|
| 154 | + 'POST', |
|
| 155 | + '/plugin/events', |
|
| 156 | + array( 'content-type' => 'application/json' ), |
|
| 157 | + wp_json_encode( |
|
| 158 | + array( |
|
| 159 | + 'source' => 'include-exclude', |
|
| 160 | + 'args' => array( |
|
| 161 | + array( 'value' => $value ), |
|
| 162 | + ), |
|
| 163 | + 'url' => $url, |
|
| 164 | + ) |
|
| 165 | + ), |
|
| 166 | + 0.001, |
|
| 167 | + null, |
|
| 168 | + array( 'blocking' => false ) |
|
| 169 | + ); |
|
| 170 | + } |
|
| 171 | 171 | } |