@@ -9,15 +9,15 @@ discard block |
||
| 9 | 9 | * @since 3.27.6 |
| 10 | 10 | */ |
| 11 | 11 | function wl_ajax_analyze_disabled_action() { |
| 12 | - // adding the below header for debugging purpose. |
|
| 13 | - if ( ! headers_sent() ) { |
|
| 14 | - header( 'X-WordLift-Analysis: OFF' ); |
|
| 15 | - } |
|
| 16 | - wp_send_json_success( array( |
|
| 17 | - 'entities' => array(), |
|
| 18 | - 'annotations' => array(), |
|
| 19 | - 'topics' => array() |
|
| 20 | - ) ); |
|
| 12 | + // adding the below header for debugging purpose. |
|
| 13 | + if ( ! headers_sent() ) { |
|
| 14 | + header( 'X-WordLift-Analysis: OFF' ); |
|
| 15 | + } |
|
| 16 | + wp_send_json_success( array( |
|
| 17 | + 'entities' => array(), |
|
| 18 | + 'annotations' => array(), |
|
| 19 | + 'topics' => array() |
|
| 20 | + ) ); |
|
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | /** |
@@ -31,13 +31,13 @@ discard block |
||
| 31 | 31 | */ |
| 32 | 32 | function wl_ajax_analyze_action() { |
| 33 | 33 | |
| 34 | - check_admin_referer( 'wl_analyze' ); |
|
| 34 | + check_admin_referer( 'wl_analyze' ); |
|
| 35 | 35 | |
| 36 | - // If you use `filter_input` here, `Ajax_Content_Analysis_Test` would fail because `filter_input` doesn't use |
|
| 37 | - // `$_POST`. |
|
| 38 | - $data = $_POST['data']; |
|
| 36 | + // If you use `filter_input` here, `Ajax_Content_Analysis_Test` would fail because `filter_input` doesn't use |
|
| 37 | + // `$_POST`. |
|
| 38 | + $data = $_POST['data']; |
|
| 39 | 39 | |
| 40 | - wp_send_json_success( wl_analyze_content( $data, 'application/json; charset=' . strtolower( get_bloginfo( 'charset' ) ) ) ); |
|
| 40 | + wp_send_json_success( wl_analyze_content( $data, 'application/json; charset=' . strtolower( get_bloginfo( 'charset' ) ) ) ); |
|
| 41 | 41 | |
| 42 | 42 | } |
| 43 | 43 | |
@@ -59,72 +59,72 @@ discard block |
||
| 59 | 59 | */ |
| 60 | 60 | function wl_analyze_content( $data, $content_type ) { |
| 61 | 61 | |
| 62 | - $default_response = json_decode( '{ "entities": {}, "annotations": {}, "topics": {} }' ); |
|
| 63 | - $request_body = json_decode( $data, true ); |
|
| 64 | - if ( $request_body === null ) { |
|
| 65 | - /** |
|
| 66 | - * @since 3.27.7 |
|
| 67 | - * |
|
| 68 | - * Conditionally try using stripslashes as $_POST['data'] could be escaped |
|
| 69 | - */ |
|
| 70 | - $request_body = json_decode( stripslashes( $data ), true ); |
|
| 71 | - } |
|
| 72 | - $request_body['contentLanguage'] = Wordlift_Configuration_Service::get_instance()->get_language_code(); |
|
| 73 | - $excluded_uris = array_key_exists( 'exclude', $request_body ) ? (array) $request_body['exclude'] : array(); |
|
| 74 | - $data = wp_json_encode( $request_body ); |
|
| 75 | - |
|
| 76 | - // If dataset is not enabled, return a locally prepared response without analysis API. |
|
| 77 | - if ( ! apply_filters( 'wl_features__enable__dataset', true ) ) { |
|
| 78 | - |
|
| 79 | - return Analysis_Response_Ops_Factory::get_instance() |
|
| 80 | - ->create( $default_response ) |
|
| 81 | - ->make_entities_local() |
|
| 82 | - ->add_occurrences( $request_body['content'] ) |
|
| 83 | - ->add_local_entities() |
|
| 84 | - ->get_json(); |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - add_filter( 'wl_api_service_api_url_path', 'wl_use_analysis_on_api_wordlift_io' ); |
|
| 88 | - $json = Wordlift_Api_Service::get_instance() |
|
| 89 | - ->post_custom_content_type( 'analysis/single', $data, $content_type ); |
|
| 90 | - remove_filter( 'wl_api_service_api_url_path', 'wl_use_analysis_on_api_wordlift_io' ); |
|
| 91 | - |
|
| 92 | - // If it's an error log it. |
|
| 93 | - if ( is_wp_error( $json ) ) { |
|
| 94 | - |
|
| 95 | - return Analysis_Response_Ops_Factory::get_instance() |
|
| 96 | - ->create( $default_response ) |
|
| 97 | - ->make_entities_local() |
|
| 98 | - ->add_occurrences( $request_body['content'] ) |
|
| 99 | - ->get_json(); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /* |
|
| 62 | + $default_response = json_decode( '{ "entities": {}, "annotations": {}, "topics": {} }' ); |
|
| 63 | + $request_body = json_decode( $data, true ); |
|
| 64 | + if ( $request_body === null ) { |
|
| 65 | + /** |
|
| 66 | + * @since 3.27.7 |
|
| 67 | + * |
|
| 68 | + * Conditionally try using stripslashes as $_POST['data'] could be escaped |
|
| 69 | + */ |
|
| 70 | + $request_body = json_decode( stripslashes( $data ), true ); |
|
| 71 | + } |
|
| 72 | + $request_body['contentLanguage'] = Wordlift_Configuration_Service::get_instance()->get_language_code(); |
|
| 73 | + $excluded_uris = array_key_exists( 'exclude', $request_body ) ? (array) $request_body['exclude'] : array(); |
|
| 74 | + $data = wp_json_encode( $request_body ); |
|
| 75 | + |
|
| 76 | + // If dataset is not enabled, return a locally prepared response without analysis API. |
|
| 77 | + if ( ! apply_filters( 'wl_features__enable__dataset', true ) ) { |
|
| 78 | + |
|
| 79 | + return Analysis_Response_Ops_Factory::get_instance() |
|
| 80 | + ->create( $default_response ) |
|
| 81 | + ->make_entities_local() |
|
| 82 | + ->add_occurrences( $request_body['content'] ) |
|
| 83 | + ->add_local_entities() |
|
| 84 | + ->get_json(); |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + add_filter( 'wl_api_service_api_url_path', 'wl_use_analysis_on_api_wordlift_io' ); |
|
| 88 | + $json = Wordlift_Api_Service::get_instance() |
|
| 89 | + ->post_custom_content_type( 'analysis/single', $data, $content_type ); |
|
| 90 | + remove_filter( 'wl_api_service_api_url_path', 'wl_use_analysis_on_api_wordlift_io' ); |
|
| 91 | + |
|
| 92 | + // If it's an error log it. |
|
| 93 | + if ( is_wp_error( $json ) ) { |
|
| 94 | + |
|
| 95 | + return Analysis_Response_Ops_Factory::get_instance() |
|
| 96 | + ->create( $default_response ) |
|
| 97 | + ->make_entities_local() |
|
| 98 | + ->add_occurrences( $request_body['content'] ) |
|
| 99 | + ->get_json(); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /* |
|
| 103 | 103 | * We pass the response to the Analysis_Response_Ops to ensure that we make remote entities local. |
| 104 | 104 | * |
| 105 | 105 | * @see https://github.com/insideout10/wordlift-plugin/issues/944 |
| 106 | 106 | * @since 3.21.5 |
| 107 | 107 | */ |
| 108 | 108 | |
| 109 | - // Get the actual content sent to the analysis, so that we can pass it to the Analysis_Response_Ops to populate |
|
| 110 | - // the occurrences for the local entities. |
|
| 111 | - if ( 0 === strpos( $content_type, 'application/json' ) ) { |
|
| 112 | - $request_content = $request_body['content']; |
|
| 113 | - } else { |
|
| 114 | - $request_content = $data; |
|
| 115 | - } |
|
| 109 | + // Get the actual content sent to the analysis, so that we can pass it to the Analysis_Response_Ops to populate |
|
| 110 | + // the occurrences for the local entities. |
|
| 111 | + if ( 0 === strpos( $content_type, 'application/json' ) ) { |
|
| 112 | + $request_content = $request_body['content']; |
|
| 113 | + } else { |
|
| 114 | + $request_content = $data; |
|
| 115 | + } |
|
| 116 | 116 | |
| 117 | 117 | |
| 118 | - return Analysis_Response_Ops_Factory::get_instance() |
|
| 119 | - ->create( $json ) |
|
| 120 | - ->make_entities_local() |
|
| 121 | - ->remove_excluded_entities( $excluded_uris ) |
|
| 122 | - ->add_occurrences( $request_content ) |
|
| 123 | - ->get_json(); |
|
| 118 | + return Analysis_Response_Ops_Factory::get_instance() |
|
| 119 | + ->create( $json ) |
|
| 120 | + ->make_entities_local() |
|
| 121 | + ->remove_excluded_entities( $excluded_uris ) |
|
| 122 | + ->add_occurrences( $request_content ) |
|
| 123 | + ->get_json(); |
|
| 124 | 124 | |
| 125 | 125 | } |
| 126 | 126 | |
| 127 | 127 | function wl_use_analysis_on_api_wordlift_io( $value ) { |
| 128 | 128 | |
| 129 | - return preg_replace( '|https://api\.wordlift\.it/|', apply_filters( 'wl_api_base_url', 'https://api.wordlift.io' ) . '/', $value ); |
|
| 129 | + return preg_replace( '|https://api\.wordlift\.it/|', apply_filters( 'wl_api_base_url', 'https://api.wordlift.io' ) . '/', $value ); |
|
| 130 | 130 | } |
@@ -10,14 +10,14 @@ discard block |
||
| 10 | 10 | */ |
| 11 | 11 | function wl_ajax_analyze_disabled_action() { |
| 12 | 12 | // adding the below header for debugging purpose. |
| 13 | - if ( ! headers_sent() ) { |
|
| 14 | - header( 'X-WordLift-Analysis: OFF' ); |
|
| 13 | + if ( ! headers_sent()) { |
|
| 14 | + header('X-WordLift-Analysis: OFF'); |
|
| 15 | 15 | } |
| 16 | - wp_send_json_success( array( |
|
| 16 | + wp_send_json_success(array( |
|
| 17 | 17 | 'entities' => array(), |
| 18 | 18 | 'annotations' => array(), |
| 19 | 19 | 'topics' => array() |
| 20 | - ) ); |
|
| 20 | + )); |
|
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | /** |
@@ -31,13 +31,13 @@ discard block |
||
| 31 | 31 | */ |
| 32 | 32 | function wl_ajax_analyze_action() { |
| 33 | 33 | |
| 34 | - check_admin_referer( 'wl_analyze' ); |
|
| 34 | + check_admin_referer('wl_analyze'); |
|
| 35 | 35 | |
| 36 | 36 | // If you use `filter_input` here, `Ajax_Content_Analysis_Test` would fail because `filter_input` doesn't use |
| 37 | 37 | // `$_POST`. |
| 38 | 38 | $data = $_POST['data']; |
| 39 | 39 | |
| 40 | - wp_send_json_success( wl_analyze_content( $data, 'application/json; charset=' . strtolower( get_bloginfo( 'charset' ) ) ) ); |
|
| 40 | + wp_send_json_success(wl_analyze_content($data, 'application/json; charset='.strtolower(get_bloginfo('charset')))); |
|
| 41 | 41 | |
| 42 | 42 | } |
| 43 | 43 | |
@@ -57,45 +57,45 @@ discard block |
||
| 57 | 57 | * @since 3.24.2 We don't return an error anymore, but an empty analysis response. This is required to allow the editor |
| 58 | 58 | * to manage entities or to manually add them even when analysis isn't available. |
| 59 | 59 | */ |
| 60 | -function wl_analyze_content( $data, $content_type ) { |
|
| 60 | +function wl_analyze_content($data, $content_type) { |
|
| 61 | 61 | |
| 62 | - $default_response = json_decode( '{ "entities": {}, "annotations": {}, "topics": {} }' ); |
|
| 63 | - $request_body = json_decode( $data, true ); |
|
| 64 | - if ( $request_body === null ) { |
|
| 62 | + $default_response = json_decode('{ "entities": {}, "annotations": {}, "topics": {} }'); |
|
| 63 | + $request_body = json_decode($data, true); |
|
| 64 | + if ($request_body === null) { |
|
| 65 | 65 | /** |
| 66 | 66 | * @since 3.27.7 |
| 67 | 67 | * |
| 68 | 68 | * Conditionally try using stripslashes as $_POST['data'] could be escaped |
| 69 | 69 | */ |
| 70 | - $request_body = json_decode( stripslashes( $data ), true ); |
|
| 70 | + $request_body = json_decode(stripslashes($data), true); |
|
| 71 | 71 | } |
| 72 | 72 | $request_body['contentLanguage'] = Wordlift_Configuration_Service::get_instance()->get_language_code(); |
| 73 | - $excluded_uris = array_key_exists( 'exclude', $request_body ) ? (array) $request_body['exclude'] : array(); |
|
| 74 | - $data = wp_json_encode( $request_body ); |
|
| 73 | + $excluded_uris = array_key_exists('exclude', $request_body) ? (array) $request_body['exclude'] : array(); |
|
| 74 | + $data = wp_json_encode($request_body); |
|
| 75 | 75 | |
| 76 | 76 | // If dataset is not enabled, return a locally prepared response without analysis API. |
| 77 | - if ( ! apply_filters( 'wl_features__enable__dataset', true ) ) { |
|
| 77 | + if ( ! apply_filters('wl_features__enable__dataset', true)) { |
|
| 78 | 78 | |
| 79 | 79 | return Analysis_Response_Ops_Factory::get_instance() |
| 80 | - ->create( $default_response ) |
|
| 80 | + ->create($default_response) |
|
| 81 | 81 | ->make_entities_local() |
| 82 | - ->add_occurrences( $request_body['content'] ) |
|
| 82 | + ->add_occurrences($request_body['content']) |
|
| 83 | 83 | ->add_local_entities() |
| 84 | 84 | ->get_json(); |
| 85 | 85 | } |
| 86 | 86 | |
| 87 | - add_filter( 'wl_api_service_api_url_path', 'wl_use_analysis_on_api_wordlift_io' ); |
|
| 87 | + add_filter('wl_api_service_api_url_path', 'wl_use_analysis_on_api_wordlift_io'); |
|
| 88 | 88 | $json = Wordlift_Api_Service::get_instance() |
| 89 | - ->post_custom_content_type( 'analysis/single', $data, $content_type ); |
|
| 90 | - remove_filter( 'wl_api_service_api_url_path', 'wl_use_analysis_on_api_wordlift_io' ); |
|
| 89 | + ->post_custom_content_type('analysis/single', $data, $content_type); |
|
| 90 | + remove_filter('wl_api_service_api_url_path', 'wl_use_analysis_on_api_wordlift_io'); |
|
| 91 | 91 | |
| 92 | 92 | // If it's an error log it. |
| 93 | - if ( is_wp_error( $json ) ) { |
|
| 93 | + if (is_wp_error($json)) { |
|
| 94 | 94 | |
| 95 | 95 | return Analysis_Response_Ops_Factory::get_instance() |
| 96 | - ->create( $default_response ) |
|
| 96 | + ->create($default_response) |
|
| 97 | 97 | ->make_entities_local() |
| 98 | - ->add_occurrences( $request_body['content'] ) |
|
| 98 | + ->add_occurrences($request_body['content']) |
|
| 99 | 99 | ->get_json(); |
| 100 | 100 | } |
| 101 | 101 | |
@@ -108,7 +108,7 @@ discard block |
||
| 108 | 108 | |
| 109 | 109 | // Get the actual content sent to the analysis, so that we can pass it to the Analysis_Response_Ops to populate |
| 110 | 110 | // the occurrences for the local entities. |
| 111 | - if ( 0 === strpos( $content_type, 'application/json' ) ) { |
|
| 111 | + if (0 === strpos($content_type, 'application/json')) { |
|
| 112 | 112 | $request_content = $request_body['content']; |
| 113 | 113 | } else { |
| 114 | 114 | $request_content = $data; |
@@ -116,15 +116,15 @@ discard block |
||
| 116 | 116 | |
| 117 | 117 | |
| 118 | 118 | return Analysis_Response_Ops_Factory::get_instance() |
| 119 | - ->create( $json ) |
|
| 119 | + ->create($json) |
|
| 120 | 120 | ->make_entities_local() |
| 121 | - ->remove_excluded_entities( $excluded_uris ) |
|
| 122 | - ->add_occurrences( $request_content ) |
|
| 121 | + ->remove_excluded_entities($excluded_uris) |
|
| 122 | + ->add_occurrences($request_content) |
|
| 123 | 123 | ->get_json(); |
| 124 | 124 | |
| 125 | 125 | } |
| 126 | 126 | |
| 127 | -function wl_use_analysis_on_api_wordlift_io( $value ) { |
|
| 127 | +function wl_use_analysis_on_api_wordlift_io($value) { |
|
| 128 | 128 | |
| 129 | - return preg_replace( '|https://api\.wordlift\.it/|', apply_filters( 'wl_api_base_url', 'https://api.wordlift.io' ) . '/', $value ); |
|
| 129 | + return preg_replace('|https://api\.wordlift\.it/|', apply_filters('wl_api_base_url', 'https://api.wordlift.io').'/', $value); |
|
| 130 | 130 | } |
@@ -15,355 +15,355 @@ |
||
| 15 | 15 | |
| 16 | 16 | class Analysis_Response_Ops { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * The analysis response json. |
|
| 20 | - * |
|
| 21 | - * @since 3.21.5 |
|
| 22 | - * @access private |
|
| 23 | - * @var mixed $json Holds the analysis response json. |
|
| 24 | - */ |
|
| 25 | - private $json; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * Holds the {@link Wordlift_Entity_Uri_Service}. |
|
| 29 | - * |
|
| 30 | - * @since 3.21.5 |
|
| 31 | - * @access private |
|
| 32 | - * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 33 | - */ |
|
| 34 | - private $entity_uri_service; |
|
| 35 | - |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * @var Entity_Helper |
|
| 39 | - */ |
|
| 40 | - private $entity_helper; |
|
| 41 | - /** |
|
| 42 | - * @var Entity_Provider_Registry |
|
| 43 | - */ |
|
| 44 | - private $entity_provider_registry; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * Analysis_Response_Ops constructor. |
|
| 48 | - * |
|
| 49 | - * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service}. |
|
| 50 | - * @param Entity_Helper $entity_helper The {@link Entity_Helper}. |
|
| 51 | - * @param Entity_Provider_Registry $entity_provider_registry Entity Provider registry. |
|
| 52 | - * @param mixed $json The analysis response json. |
|
| 53 | - * |
|
| 54 | - * @since 3.21.5 |
|
| 55 | - */ |
|
| 56 | - public function __construct( $entity_uri_service, $entity_helper, $entity_provider_registry, $json ) { |
|
| 57 | - |
|
| 58 | - $this->json = $json; |
|
| 59 | - $this->entity_uri_service = $entity_uri_service; |
|
| 60 | - $this->entity_helper = $entity_helper; |
|
| 61 | - $this->entity_provider_registry = $entity_provider_registry; |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Switches remote entities, i.e. entities with id outside the local dataset, to local entities. |
|
| 66 | - * |
|
| 67 | - * The function takes all the entities that have an id which is not local. For each remote entity, a list of URIs |
|
| 68 | - * is built comprising the entity id and the sameAs. Then a query is issued in the local database to find potential |
|
| 69 | - * matches from the local vocabulary. |
|
| 70 | - * |
|
| 71 | - * If found, the entity id is swapped with the local id and the remote id is added to the sameAs. |
|
| 72 | - * |
|
| 73 | - * |
|
| 74 | - * @return Analysis_Response_Ops The current Analysis_Response_Ops instance. |
|
| 75 | - */ |
|
| 76 | - public function make_entities_local() { |
|
| 77 | - |
|
| 78 | - if ( ! isset( $this->json->entities ) ) { |
|
| 79 | - return $this; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - // Get the URIs. |
|
| 83 | - $uris = array_keys( get_object_vars( $this->json->entities ) ); |
|
| 84 | - $mappings = $this->entity_helper->map_many_to_local( $uris ); |
|
| 85 | - |
|
| 86 | - foreach ( $mappings as $external_uri => $internal_uri ) { |
|
| 87 | - |
|
| 88 | - // Move the data from the external URI to the internal URI. |
|
| 89 | - if ( ! isset( $this->json->entities->{$internal_uri} ) ) { |
|
| 90 | - $this->json->entities->{$internal_uri} = $this->json->entities->{$external_uri}; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - // Ensure sameAs is an array. |
|
| 94 | - if ( ! isset( $this->json->entities->{$internal_uri}->sameAs ) |
|
| 95 | - || ! is_array( $this->json->entities->{$internal_uri}->sameAs ) ) { |
|
| 96 | - $this->json->entities->{$internal_uri}->sameAs = array(); |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - // Add the external URI as sameAs. |
|
| 100 | - $this->json->entities->{$internal_uri}->sameAs[] = $external_uri; |
|
| 101 | - |
|
| 102 | - // Finally remove the external URI. |
|
| 103 | - unset( $this->json->entities->{$external_uri} ); |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - // Set the internal uri in the annotation for the entityMatch in annotations. |
|
| 107 | - if ( isset( $this->json->annotations ) ) { |
|
| 108 | - foreach ( $this->json->annotations as $key => $annotation ) { |
|
| 109 | - if ( isset( $annotation->entityMatches ) ) { |
|
| 110 | - foreach ( $annotation->entityMatches as $match ) { |
|
| 111 | - if ( isset( $match->entityId ) && isset( $mappings[ $match->entityId ] ) ) { |
|
| 112 | - $match->entityId = $mappings[ $match->entityId ]; |
|
| 113 | - } |
|
| 114 | - } |
|
| 115 | - } |
|
| 116 | - } |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - return $this; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * Add occurrences by parsing the provided html content. |
|
| 124 | - * |
|
| 125 | - * @param string $content The html content with annotations. |
|
| 126 | - * |
|
| 127 | - * @return Analysis_Response_Ops The {@link Analysis_Response_Ops} instance. |
|
| 128 | - * |
|
| 129 | - * @since 3.23.7 refactor the regex pattern to take into account that there might be css classes between textannotation |
|
| 130 | - * and disambiguated. |
|
| 131 | - * |
|
| 132 | - * @link https://github.com/insideout10/wordlift-plugin/issues/1001 |
|
| 133 | - */ |
|
| 134 | - public function add_occurrences( $content ) { |
|
| 135 | - |
|
| 136 | - // Try to get all the disambiguated annotations and bail out if an error occurs. |
|
| 137 | - if ( false === preg_match_all( |
|
| 138 | - '|<span\s+id="([^"]+)"\s+class="textannotation\s+(?:\S+\s+)?disambiguated(?=[\s"])[^"]*"\s+itemid="([^"]*)">(.*?)</span>|', |
|
| 139 | - $content, |
|
| 140 | - $matches, |
|
| 141 | - PREG_OFFSET_CAPTURE |
|
| 142 | - ) ) { |
|
| 143 | - return $this; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - if ( empty( $matches ) ) { |
|
| 147 | - return $this; |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - $parse_data = array_reduce( range( 0, count( $matches[1] ) - 1 ), function ( $carry, $i ) use ( $matches ) { |
|
| 151 | - if ( empty( $matches[0] ) ) { |
|
| 152 | - return $carry; |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - $start = $matches[0][ $i ][1]; |
|
| 156 | - $end = $start + strlen( $matches[0][ $i ][0] ); |
|
| 157 | - $annotation_id = $matches[1][ $i ][0]; |
|
| 158 | - $item_id = $matches[2][ $i ][0]; |
|
| 159 | - $text = $matches[3][ $i ][0]; |
|
| 160 | - |
|
| 161 | - $annotation = new StdClass; |
|
| 162 | - $annotation->annotationId = $annotation_id; |
|
| 163 | - $annotation->start = $start; |
|
| 164 | - $annotation->end = $end; |
|
| 165 | - $annotation->text = $text; |
|
| 166 | - |
|
| 167 | - $entity_match = new StdClass; |
|
| 168 | - $entity_match->confidence = 100; |
|
| 169 | - $entity_match->entityId = $item_id; |
|
| 170 | - $annotation->entityMatches[] = $entity_match; |
|
| 171 | - |
|
| 172 | - $carry['annotations'][ $annotation_id ] = $annotation; |
|
| 173 | - $carry['occurrences'][ $item_id ][] = $annotation_id; |
|
| 174 | - |
|
| 175 | - return $carry; |
|
| 176 | - }, array( 'annotations' => array(), 'occurrences' => array(), ) ); |
|
| 177 | - |
|
| 178 | - $annotations = $parse_data['annotations']; |
|
| 179 | - $occurrences = $parse_data['occurrences']; |
|
| 180 | - |
|
| 181 | - foreach ( array_keys( $occurrences ) as $item_id ) { |
|
| 182 | - |
|
| 183 | - // If the entity isn't there, add it. |
|
| 184 | - if ( ! is_bool( $this->json ) && ! isset( $this->json->entities->{$item_id} ) ) { |
|
| 185 | - $entity = $this->entity_provider_registry->get_local_entity( $item_id ); |
|
| 186 | - |
|
| 187 | - // Entity not found in the local vocabulary, continue to the next one. |
|
| 188 | - if ( false === $entity ) { |
|
| 189 | - continue; |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - $this->json->entities->{$item_id} = $entity; |
|
| 193 | - } |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - // Here we're adding back some data structures required by the client-side code. |
|
| 197 | - // |
|
| 198 | - // We're adding: |
|
| 199 | - // 1. the .entities[entity_id].occurrences array with the annotations' ids. |
|
| 200 | - // 2. the .entities[entity_id].annotations[annotation_id] = { id: annotation_id } map. |
|
| 201 | - // |
|
| 202 | - // Before 3.23.0 this was done by the client-side code located in src/coffee/editpost-widget/app.services.AnalysisService.coffee |
|
| 203 | - // function `preselect`, which was called by src/coffee/editpost-widget/app.services.EditorService.coffee in |
|
| 204 | - // `embedAnalysis`. |
|
| 205 | - |
|
| 206 | - if ( ! is_bool( $this->json ) && isset( $this->json->entities ) ) { |
|
| 207 | - foreach ( $this->json->entities as $id => $entity ) { |
|
| 208 | - $this->json->entities->{$id}->occurrences = isset( $occurrences[ $id ] ) ? $occurrences[ $id ] : array();; |
|
| 209 | - |
|
| 210 | - foreach ( $this->json->entities->{$id}->occurrences as $annotation_id ) { |
|
| 211 | - $this->json->entities->{$id}->annotations[ $annotation_id ] = array( |
|
| 212 | - 'id' => $annotation_id, |
|
| 213 | - ); |
|
| 214 | - } |
|
| 215 | - } |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - // Add the missing annotations. This allows the analysis response to work also if we didn't receive results |
|
| 219 | - // from the analysis API. |
|
| 220 | - foreach ( $annotations as $annotation_id => $annotation ) { |
|
| 221 | - |
|
| 222 | - if ( ! is_bool( $this->json ) && ! isset( $this->json->annotations->{$annotation_id} ) ) { |
|
| 223 | - $this->json->annotations->{$annotation_id} = $annotation; |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - return $this; |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - /** |
|
| 232 | - * Add local entities |
|
| 233 | - * |
|
| 234 | - * @return Analysis_Response_Ops The {@link Analysis_Response_Ops} instance. |
|
| 235 | - * |
|
| 236 | - * @since 3.27.6 |
|
| 237 | - * |
|
| 238 | - * @link https://github.com/insideout10/wordlift-plugin/issues/1178 |
|
| 239 | - */ |
|
| 240 | - public function add_local_entities() { |
|
| 241 | - |
|
| 242 | - // Populating the local entities object |
|
| 243 | - $entities = array(); |
|
| 244 | - foreach ( $this->json->annotations as $annotation ) { |
|
| 245 | - foreach ( $annotation->entityMatches as $entity_matches ) { |
|
| 246 | - |
|
| 247 | - $entity_id = $this->entity_uri_service->get_post_id_from_url( $entity_matches->entityId ); |
|
| 248 | - $serialized_entity = wl_serialize_entity( $entity_id ); |
|
| 249 | - |
|
| 250 | - if ( $serialized_entity ) { |
|
| 251 | - $serialized_entity['entityId'] = $serialized_entity['id']; |
|
| 252 | - unset( $serialized_entity['id'] ); |
|
| 253 | - |
|
| 254 | - $entities[ $entity_matches->entityId ] = $serialized_entity; |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - } |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - // Adding occurrences and annotations data structures required by the client-side code. |
|
| 261 | - foreach ( $entities as $entity_id => $entity ) { |
|
| 262 | - foreach ( $this->json->annotations as $annotation ) { |
|
| 263 | - if ( $annotation->entityMatches[0]->entityId === $entity_id ) { |
|
| 264 | - $entities[ $entity_id ]['occurrences'][] = $annotation->annotationId; |
|
| 265 | - $entities[ $entity_id ]['annotations'][ $annotation->annotationId ]['id'] = $annotation->annotationId; |
|
| 266 | - } |
|
| 267 | - } |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - $this->json->entities = $entities; |
|
| 271 | - |
|
| 272 | - return $this; |
|
| 273 | - |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - |
|
| 277 | - /** |
|
| 278 | - * Return the JSON response. |
|
| 279 | - * |
|
| 280 | - * @return mixed The JSON response. |
|
| 281 | - * @since 3.24.2 |
|
| 282 | - */ |
|
| 283 | - public function get_json() { |
|
| 284 | - |
|
| 285 | - return $this->json; |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - /** |
|
| 289 | - * This function should be invoked after `make_entities_local` after this |
|
| 290 | - * method. |
|
| 291 | - * |
|
| 292 | - * @param $excluded_uris array An array of entity URIs to be excluded. |
|
| 293 | - * |
|
| 294 | - * @return $this |
|
| 295 | - * @since 3.32.3.1 |
|
| 296 | - */ |
|
| 297 | - public function remove_excluded_entities( $excluded_uris ) { |
|
| 298 | - |
|
| 299 | - // If we didnt receive array, return early. |
|
| 300 | - if ( ! is_array( $excluded_uris ) ) { |
|
| 301 | - return $this; |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - // We may also receive an array of null, make sure to filter uris when receiving. |
|
| 305 | - $excluded_uris = array_filter( $excluded_uris, 'is_string' ); |
|
| 306 | - |
|
| 307 | - $this->remove_entities_with_excluded_uris( $excluded_uris ); |
|
| 308 | - |
|
| 309 | - if ( isset( $this->json->annotations ) ) { |
|
| 310 | - foreach ( $this->json->annotations as $annotation_key => &$annotation_data ) { |
|
| 311 | - |
|
| 312 | - if ( ! isset( $annotation_data->entityMatches ) ) { |
|
| 313 | - continue; |
|
| 314 | - } |
|
| 315 | - |
|
| 316 | - foreach ( $annotation_data->entityMatches as $entity_match_key => $entity_match_data ) { |
|
| 18 | + /** |
|
| 19 | + * The analysis response json. |
|
| 20 | + * |
|
| 21 | + * @since 3.21.5 |
|
| 22 | + * @access private |
|
| 23 | + * @var mixed $json Holds the analysis response json. |
|
| 24 | + */ |
|
| 25 | + private $json; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * Holds the {@link Wordlift_Entity_Uri_Service}. |
|
| 29 | + * |
|
| 30 | + * @since 3.21.5 |
|
| 31 | + * @access private |
|
| 32 | + * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 33 | + */ |
|
| 34 | + private $entity_uri_service; |
|
| 35 | + |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * @var Entity_Helper |
|
| 39 | + */ |
|
| 40 | + private $entity_helper; |
|
| 41 | + /** |
|
| 42 | + * @var Entity_Provider_Registry |
|
| 43 | + */ |
|
| 44 | + private $entity_provider_registry; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * Analysis_Response_Ops constructor. |
|
| 48 | + * |
|
| 49 | + * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service}. |
|
| 50 | + * @param Entity_Helper $entity_helper The {@link Entity_Helper}. |
|
| 51 | + * @param Entity_Provider_Registry $entity_provider_registry Entity Provider registry. |
|
| 52 | + * @param mixed $json The analysis response json. |
|
| 53 | + * |
|
| 54 | + * @since 3.21.5 |
|
| 55 | + */ |
|
| 56 | + public function __construct( $entity_uri_service, $entity_helper, $entity_provider_registry, $json ) { |
|
| 57 | + |
|
| 58 | + $this->json = $json; |
|
| 59 | + $this->entity_uri_service = $entity_uri_service; |
|
| 60 | + $this->entity_helper = $entity_helper; |
|
| 61 | + $this->entity_provider_registry = $entity_provider_registry; |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Switches remote entities, i.e. entities with id outside the local dataset, to local entities. |
|
| 66 | + * |
|
| 67 | + * The function takes all the entities that have an id which is not local. For each remote entity, a list of URIs |
|
| 68 | + * is built comprising the entity id and the sameAs. Then a query is issued in the local database to find potential |
|
| 69 | + * matches from the local vocabulary. |
|
| 70 | + * |
|
| 71 | + * If found, the entity id is swapped with the local id and the remote id is added to the sameAs. |
|
| 72 | + * |
|
| 73 | + * |
|
| 74 | + * @return Analysis_Response_Ops The current Analysis_Response_Ops instance. |
|
| 75 | + */ |
|
| 76 | + public function make_entities_local() { |
|
| 77 | + |
|
| 78 | + if ( ! isset( $this->json->entities ) ) { |
|
| 79 | + return $this; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + // Get the URIs. |
|
| 83 | + $uris = array_keys( get_object_vars( $this->json->entities ) ); |
|
| 84 | + $mappings = $this->entity_helper->map_many_to_local( $uris ); |
|
| 85 | + |
|
| 86 | + foreach ( $mappings as $external_uri => $internal_uri ) { |
|
| 87 | + |
|
| 88 | + // Move the data from the external URI to the internal URI. |
|
| 89 | + if ( ! isset( $this->json->entities->{$internal_uri} ) ) { |
|
| 90 | + $this->json->entities->{$internal_uri} = $this->json->entities->{$external_uri}; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + // Ensure sameAs is an array. |
|
| 94 | + if ( ! isset( $this->json->entities->{$internal_uri}->sameAs ) |
|
| 95 | + || ! is_array( $this->json->entities->{$internal_uri}->sameAs ) ) { |
|
| 96 | + $this->json->entities->{$internal_uri}->sameAs = array(); |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + // Add the external URI as sameAs. |
|
| 100 | + $this->json->entities->{$internal_uri}->sameAs[] = $external_uri; |
|
| 101 | + |
|
| 102 | + // Finally remove the external URI. |
|
| 103 | + unset( $this->json->entities->{$external_uri} ); |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + // Set the internal uri in the annotation for the entityMatch in annotations. |
|
| 107 | + if ( isset( $this->json->annotations ) ) { |
|
| 108 | + foreach ( $this->json->annotations as $key => $annotation ) { |
|
| 109 | + if ( isset( $annotation->entityMatches ) ) { |
|
| 110 | + foreach ( $annotation->entityMatches as $match ) { |
|
| 111 | + if ( isset( $match->entityId ) && isset( $mappings[ $match->entityId ] ) ) { |
|
| 112 | + $match->entityId = $mappings[ $match->entityId ]; |
|
| 113 | + } |
|
| 114 | + } |
|
| 115 | + } |
|
| 116 | + } |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + return $this; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * Add occurrences by parsing the provided html content. |
|
| 124 | + * |
|
| 125 | + * @param string $content The html content with annotations. |
|
| 126 | + * |
|
| 127 | + * @return Analysis_Response_Ops The {@link Analysis_Response_Ops} instance. |
|
| 128 | + * |
|
| 129 | + * @since 3.23.7 refactor the regex pattern to take into account that there might be css classes between textannotation |
|
| 130 | + * and disambiguated. |
|
| 131 | + * |
|
| 132 | + * @link https://github.com/insideout10/wordlift-plugin/issues/1001 |
|
| 133 | + */ |
|
| 134 | + public function add_occurrences( $content ) { |
|
| 135 | + |
|
| 136 | + // Try to get all the disambiguated annotations and bail out if an error occurs. |
|
| 137 | + if ( false === preg_match_all( |
|
| 138 | + '|<span\s+id="([^"]+)"\s+class="textannotation\s+(?:\S+\s+)?disambiguated(?=[\s"])[^"]*"\s+itemid="([^"]*)">(.*?)</span>|', |
|
| 139 | + $content, |
|
| 140 | + $matches, |
|
| 141 | + PREG_OFFSET_CAPTURE |
|
| 142 | + ) ) { |
|
| 143 | + return $this; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + if ( empty( $matches ) ) { |
|
| 147 | + return $this; |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + $parse_data = array_reduce( range( 0, count( $matches[1] ) - 1 ), function ( $carry, $i ) use ( $matches ) { |
|
| 151 | + if ( empty( $matches[0] ) ) { |
|
| 152 | + return $carry; |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + $start = $matches[0][ $i ][1]; |
|
| 156 | + $end = $start + strlen( $matches[0][ $i ][0] ); |
|
| 157 | + $annotation_id = $matches[1][ $i ][0]; |
|
| 158 | + $item_id = $matches[2][ $i ][0]; |
|
| 159 | + $text = $matches[3][ $i ][0]; |
|
| 160 | + |
|
| 161 | + $annotation = new StdClass; |
|
| 162 | + $annotation->annotationId = $annotation_id; |
|
| 163 | + $annotation->start = $start; |
|
| 164 | + $annotation->end = $end; |
|
| 165 | + $annotation->text = $text; |
|
| 166 | + |
|
| 167 | + $entity_match = new StdClass; |
|
| 168 | + $entity_match->confidence = 100; |
|
| 169 | + $entity_match->entityId = $item_id; |
|
| 170 | + $annotation->entityMatches[] = $entity_match; |
|
| 171 | + |
|
| 172 | + $carry['annotations'][ $annotation_id ] = $annotation; |
|
| 173 | + $carry['occurrences'][ $item_id ][] = $annotation_id; |
|
| 174 | + |
|
| 175 | + return $carry; |
|
| 176 | + }, array( 'annotations' => array(), 'occurrences' => array(), ) ); |
|
| 177 | + |
|
| 178 | + $annotations = $parse_data['annotations']; |
|
| 179 | + $occurrences = $parse_data['occurrences']; |
|
| 180 | + |
|
| 181 | + foreach ( array_keys( $occurrences ) as $item_id ) { |
|
| 182 | + |
|
| 183 | + // If the entity isn't there, add it. |
|
| 184 | + if ( ! is_bool( $this->json ) && ! isset( $this->json->entities->{$item_id} ) ) { |
|
| 185 | + $entity = $this->entity_provider_registry->get_local_entity( $item_id ); |
|
| 186 | + |
|
| 187 | + // Entity not found in the local vocabulary, continue to the next one. |
|
| 188 | + if ( false === $entity ) { |
|
| 189 | + continue; |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + $this->json->entities->{$item_id} = $entity; |
|
| 193 | + } |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + // Here we're adding back some data structures required by the client-side code. |
|
| 197 | + // |
|
| 198 | + // We're adding: |
|
| 199 | + // 1. the .entities[entity_id].occurrences array with the annotations' ids. |
|
| 200 | + // 2. the .entities[entity_id].annotations[annotation_id] = { id: annotation_id } map. |
|
| 201 | + // |
|
| 202 | + // Before 3.23.0 this was done by the client-side code located in src/coffee/editpost-widget/app.services.AnalysisService.coffee |
|
| 203 | + // function `preselect`, which was called by src/coffee/editpost-widget/app.services.EditorService.coffee in |
|
| 204 | + // `embedAnalysis`. |
|
| 205 | + |
|
| 206 | + if ( ! is_bool( $this->json ) && isset( $this->json->entities ) ) { |
|
| 207 | + foreach ( $this->json->entities as $id => $entity ) { |
|
| 208 | + $this->json->entities->{$id}->occurrences = isset( $occurrences[ $id ] ) ? $occurrences[ $id ] : array();; |
|
| 209 | + |
|
| 210 | + foreach ( $this->json->entities->{$id}->occurrences as $annotation_id ) { |
|
| 211 | + $this->json->entities->{$id}->annotations[ $annotation_id ] = array( |
|
| 212 | + 'id' => $annotation_id, |
|
| 213 | + ); |
|
| 214 | + } |
|
| 215 | + } |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + // Add the missing annotations. This allows the analysis response to work also if we didn't receive results |
|
| 219 | + // from the analysis API. |
|
| 220 | + foreach ( $annotations as $annotation_id => $annotation ) { |
|
| 221 | + |
|
| 222 | + if ( ! is_bool( $this->json ) && ! isset( $this->json->annotations->{$annotation_id} ) ) { |
|
| 223 | + $this->json->annotations->{$annotation_id} = $annotation; |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + return $this; |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + /** |
|
| 232 | + * Add local entities |
|
| 233 | + * |
|
| 234 | + * @return Analysis_Response_Ops The {@link Analysis_Response_Ops} instance. |
|
| 235 | + * |
|
| 236 | + * @since 3.27.6 |
|
| 237 | + * |
|
| 238 | + * @link https://github.com/insideout10/wordlift-plugin/issues/1178 |
|
| 239 | + */ |
|
| 240 | + public function add_local_entities() { |
|
| 241 | + |
|
| 242 | + // Populating the local entities object |
|
| 243 | + $entities = array(); |
|
| 244 | + foreach ( $this->json->annotations as $annotation ) { |
|
| 245 | + foreach ( $annotation->entityMatches as $entity_matches ) { |
|
| 246 | + |
|
| 247 | + $entity_id = $this->entity_uri_service->get_post_id_from_url( $entity_matches->entityId ); |
|
| 248 | + $serialized_entity = wl_serialize_entity( $entity_id ); |
|
| 249 | + |
|
| 250 | + if ( $serialized_entity ) { |
|
| 251 | + $serialized_entity['entityId'] = $serialized_entity['id']; |
|
| 252 | + unset( $serialized_entity['id'] ); |
|
| 253 | + |
|
| 254 | + $entities[ $entity_matches->entityId ] = $serialized_entity; |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + } |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + // Adding occurrences and annotations data structures required by the client-side code. |
|
| 261 | + foreach ( $entities as $entity_id => $entity ) { |
|
| 262 | + foreach ( $this->json->annotations as $annotation ) { |
|
| 263 | + if ( $annotation->entityMatches[0]->entityId === $entity_id ) { |
|
| 264 | + $entities[ $entity_id ]['occurrences'][] = $annotation->annotationId; |
|
| 265 | + $entities[ $entity_id ]['annotations'][ $annotation->annotationId ]['id'] = $annotation->annotationId; |
|
| 266 | + } |
|
| 267 | + } |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + $this->json->entities = $entities; |
|
| 271 | + |
|
| 272 | + return $this; |
|
| 273 | + |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + |
|
| 277 | + /** |
|
| 278 | + * Return the JSON response. |
|
| 279 | + * |
|
| 280 | + * @return mixed The JSON response. |
|
| 281 | + * @since 3.24.2 |
|
| 282 | + */ |
|
| 283 | + public function get_json() { |
|
| 284 | + |
|
| 285 | + return $this->json; |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + /** |
|
| 289 | + * This function should be invoked after `make_entities_local` after this |
|
| 290 | + * method. |
|
| 291 | + * |
|
| 292 | + * @param $excluded_uris array An array of entity URIs to be excluded. |
|
| 293 | + * |
|
| 294 | + * @return $this |
|
| 295 | + * @since 3.32.3.1 |
|
| 296 | + */ |
|
| 297 | + public function remove_excluded_entities( $excluded_uris ) { |
|
| 298 | + |
|
| 299 | + // If we didnt receive array, return early. |
|
| 300 | + if ( ! is_array( $excluded_uris ) ) { |
|
| 301 | + return $this; |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + // We may also receive an array of null, make sure to filter uris when receiving. |
|
| 305 | + $excluded_uris = array_filter( $excluded_uris, 'is_string' ); |
|
| 306 | + |
|
| 307 | + $this->remove_entities_with_excluded_uris( $excluded_uris ); |
|
| 308 | + |
|
| 309 | + if ( isset( $this->json->annotations ) ) { |
|
| 310 | + foreach ( $this->json->annotations as $annotation_key => &$annotation_data ) { |
|
| 311 | + |
|
| 312 | + if ( ! isset( $annotation_data->entityMatches ) ) { |
|
| 313 | + continue; |
|
| 314 | + } |
|
| 315 | + |
|
| 316 | + foreach ( $annotation_data->entityMatches as $entity_match_key => $entity_match_data ) { |
|
| 317 | 317 | |
| 318 | - $entity_uri = $entity_match_data->entityId; |
|
| 319 | - |
|
| 320 | - if ( ! in_array( $entity_uri, $excluded_uris ) ) { |
|
| 321 | - continue; |
|
| 322 | - } |
|
| 323 | - unset( $annotation_data->entityMatches[ $entity_match_key ] ); |
|
| 324 | - } |
|
| 325 | - |
|
| 326 | - if ( count( $annotation_data->entityMatches ) === 0 ) { |
|
| 327 | - // Remove the annotation if we have zero empty annotation matches. |
|
| 328 | - unset( $this->json->annotations->{$annotation_key} ); |
|
| 329 | - } |
|
| 330 | - |
|
| 331 | - } |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - return $this; |
|
| 335 | - } |
|
| 318 | + $entity_uri = $entity_match_data->entityId; |
|
| 319 | + |
|
| 320 | + if ( ! in_array( $entity_uri, $excluded_uris ) ) { |
|
| 321 | + continue; |
|
| 322 | + } |
|
| 323 | + unset( $annotation_data->entityMatches[ $entity_match_key ] ); |
|
| 324 | + } |
|
| 325 | + |
|
| 326 | + if ( count( $annotation_data->entityMatches ) === 0 ) { |
|
| 327 | + // Remove the annotation if we have zero empty annotation matches. |
|
| 328 | + unset( $this->json->annotations->{$annotation_key} ); |
|
| 329 | + } |
|
| 330 | + |
|
| 331 | + } |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + return $this; |
|
| 335 | + } |
|
| 336 | 336 | |
| 337 | 337 | |
| 338 | - /** |
|
| 339 | - * Get the string representation of the JSON. |
|
| 340 | - * |
|
| 341 | - * @return false|string The string representation or false in case of error. |
|
| 342 | - */ |
|
| 343 | - public function to_string() { |
|
| 338 | + /** |
|
| 339 | + * Get the string representation of the JSON. |
|
| 340 | + * |
|
| 341 | + * @return false|string The string representation or false in case of error. |
|
| 342 | + */ |
|
| 343 | + public function to_string() { |
|
| 344 | 344 | |
| 345 | - // Add the `JSON_UNESCAPED_UNICODE` only for PHP 5.4+. |
|
| 346 | - $options = ( version_compare( PHP_VERSION, '5.4', '>=' ) |
|
| 347 | - ? 256 : 0 ); |
|
| 345 | + // Add the `JSON_UNESCAPED_UNICODE` only for PHP 5.4+. |
|
| 346 | + $options = ( version_compare( PHP_VERSION, '5.4', '>=' ) |
|
| 347 | + ? 256 : 0 ); |
|
| 348 | 348 | |
| 349 | - return wp_json_encode( $this->json, $options ); |
|
| 350 | - } |
|
| 349 | + return wp_json_encode( $this->json, $options ); |
|
| 350 | + } |
|
| 351 | 351 | |
| 352 | - /** |
|
| 353 | - * @param array $excluded_uris |
|
| 354 | - */ |
|
| 355 | - private function remove_entities_with_excluded_uris( array $excluded_uris ) { |
|
| 352 | + /** |
|
| 353 | + * @param array $excluded_uris |
|
| 354 | + */ |
|
| 355 | + private function remove_entities_with_excluded_uris( array $excluded_uris ) { |
|
| 356 | 356 | // Remove the excluded entity uris. |
| 357 | - if ( isset( $this->json->entities ) ) { |
|
| 358 | - foreach ( $excluded_uris as $excluded_uri ) { |
|
| 359 | - |
|
| 360 | - if ( isset( $this->json->entities->{$excluded_uri} ) ) { |
|
| 361 | - // Remove this entity. |
|
| 362 | - unset( $this->json->entities->{$excluded_uri} ); |
|
| 363 | - // Also remove the annotations. |
|
| 364 | - } |
|
| 365 | - } |
|
| 366 | - } |
|
| 367 | - } |
|
| 357 | + if ( isset( $this->json->entities ) ) { |
|
| 358 | + foreach ( $excluded_uris as $excluded_uri ) { |
|
| 359 | + |
|
| 360 | + if ( isset( $this->json->entities->{$excluded_uri} ) ) { |
|
| 361 | + // Remove this entity. |
|
| 362 | + unset( $this->json->entities->{$excluded_uri} ); |
|
| 363 | + // Also remove the annotations. |
|
| 364 | + } |
|
| 365 | + } |
|
| 366 | + } |
|
| 367 | + } |
|
| 368 | 368 | |
| 369 | 369 | } |
@@ -53,7 +53,7 @@ discard block |
||
| 53 | 53 | * |
| 54 | 54 | * @since 3.21.5 |
| 55 | 55 | */ |
| 56 | - public function __construct( $entity_uri_service, $entity_helper, $entity_provider_registry, $json ) { |
|
| 56 | + public function __construct($entity_uri_service, $entity_helper, $entity_provider_registry, $json) { |
|
| 57 | 57 | |
| 58 | 58 | $this->json = $json; |
| 59 | 59 | $this->entity_uri_service = $entity_uri_service; |
@@ -75,24 +75,24 @@ discard block |
||
| 75 | 75 | */ |
| 76 | 76 | public function make_entities_local() { |
| 77 | 77 | |
| 78 | - if ( ! isset( $this->json->entities ) ) { |
|
| 78 | + if ( ! isset($this->json->entities)) { |
|
| 79 | 79 | return $this; |
| 80 | 80 | } |
| 81 | 81 | |
| 82 | 82 | // Get the URIs. |
| 83 | - $uris = array_keys( get_object_vars( $this->json->entities ) ); |
|
| 84 | - $mappings = $this->entity_helper->map_many_to_local( $uris ); |
|
| 83 | + $uris = array_keys(get_object_vars($this->json->entities)); |
|
| 84 | + $mappings = $this->entity_helper->map_many_to_local($uris); |
|
| 85 | 85 | |
| 86 | - foreach ( $mappings as $external_uri => $internal_uri ) { |
|
| 86 | + foreach ($mappings as $external_uri => $internal_uri) { |
|
| 87 | 87 | |
| 88 | 88 | // Move the data from the external URI to the internal URI. |
| 89 | - if ( ! isset( $this->json->entities->{$internal_uri} ) ) { |
|
| 89 | + if ( ! isset($this->json->entities->{$internal_uri} )) { |
|
| 90 | 90 | $this->json->entities->{$internal_uri} = $this->json->entities->{$external_uri}; |
| 91 | 91 | } |
| 92 | 92 | |
| 93 | 93 | // Ensure sameAs is an array. |
| 94 | - if ( ! isset( $this->json->entities->{$internal_uri}->sameAs ) |
|
| 95 | - || ! is_array( $this->json->entities->{$internal_uri}->sameAs ) ) { |
|
| 94 | + if ( ! isset($this->json->entities->{$internal_uri}->sameAs) |
|
| 95 | + || ! is_array($this->json->entities->{$internal_uri}->sameAs)) { |
|
| 96 | 96 | $this->json->entities->{$internal_uri}->sameAs = array(); |
| 97 | 97 | } |
| 98 | 98 | |
@@ -100,16 +100,16 @@ discard block |
||
| 100 | 100 | $this->json->entities->{$internal_uri}->sameAs[] = $external_uri; |
| 101 | 101 | |
| 102 | 102 | // Finally remove the external URI. |
| 103 | - unset( $this->json->entities->{$external_uri} ); |
|
| 103 | + unset($this->json->entities->{$external_uri} ); |
|
| 104 | 104 | } |
| 105 | 105 | |
| 106 | 106 | // Set the internal uri in the annotation for the entityMatch in annotations. |
| 107 | - if ( isset( $this->json->annotations ) ) { |
|
| 108 | - foreach ( $this->json->annotations as $key => $annotation ) { |
|
| 109 | - if ( isset( $annotation->entityMatches ) ) { |
|
| 110 | - foreach ( $annotation->entityMatches as $match ) { |
|
| 111 | - if ( isset( $match->entityId ) && isset( $mappings[ $match->entityId ] ) ) { |
|
| 112 | - $match->entityId = $mappings[ $match->entityId ]; |
|
| 107 | + if (isset($this->json->annotations)) { |
|
| 108 | + foreach ($this->json->annotations as $key => $annotation) { |
|
| 109 | + if (isset($annotation->entityMatches)) { |
|
| 110 | + foreach ($annotation->entityMatches as $match) { |
|
| 111 | + if (isset($match->entityId) && isset($mappings[$match->entityId])) { |
|
| 112 | + $match->entityId = $mappings[$match->entityId]; |
|
| 113 | 113 | } |
| 114 | 114 | } |
| 115 | 115 | } |
@@ -131,32 +131,32 @@ discard block |
||
| 131 | 131 | * |
| 132 | 132 | * @link https://github.com/insideout10/wordlift-plugin/issues/1001 |
| 133 | 133 | */ |
| 134 | - public function add_occurrences( $content ) { |
|
| 134 | + public function add_occurrences($content) { |
|
| 135 | 135 | |
| 136 | 136 | // Try to get all the disambiguated annotations and bail out if an error occurs. |
| 137 | - if ( false === preg_match_all( |
|
| 137 | + if (false === preg_match_all( |
|
| 138 | 138 | '|<span\s+id="([^"]+)"\s+class="textannotation\s+(?:\S+\s+)?disambiguated(?=[\s"])[^"]*"\s+itemid="([^"]*)">(.*?)</span>|', |
| 139 | 139 | $content, |
| 140 | 140 | $matches, |
| 141 | 141 | PREG_OFFSET_CAPTURE |
| 142 | - ) ) { |
|
| 142 | + )) { |
|
| 143 | 143 | return $this; |
| 144 | 144 | } |
| 145 | 145 | |
| 146 | - if ( empty( $matches ) ) { |
|
| 146 | + if (empty($matches)) { |
|
| 147 | 147 | return $this; |
| 148 | 148 | } |
| 149 | 149 | |
| 150 | - $parse_data = array_reduce( range( 0, count( $matches[1] ) - 1 ), function ( $carry, $i ) use ( $matches ) { |
|
| 151 | - if ( empty( $matches[0] ) ) { |
|
| 150 | + $parse_data = array_reduce(range(0, count($matches[1]) - 1), function($carry, $i) use ($matches) { |
|
| 151 | + if (empty($matches[0])) { |
|
| 152 | 152 | return $carry; |
| 153 | 153 | } |
| 154 | 154 | |
| 155 | - $start = $matches[0][ $i ][1]; |
|
| 156 | - $end = $start + strlen( $matches[0][ $i ][0] ); |
|
| 157 | - $annotation_id = $matches[1][ $i ][0]; |
|
| 158 | - $item_id = $matches[2][ $i ][0]; |
|
| 159 | - $text = $matches[3][ $i ][0]; |
|
| 155 | + $start = $matches[0][$i][1]; |
|
| 156 | + $end = $start + strlen($matches[0][$i][0]); |
|
| 157 | + $annotation_id = $matches[1][$i][0]; |
|
| 158 | + $item_id = $matches[2][$i][0]; |
|
| 159 | + $text = $matches[3][$i][0]; |
|
| 160 | 160 | |
| 161 | 161 | $annotation = new StdClass; |
| 162 | 162 | $annotation->annotationId = $annotation_id; |
@@ -169,23 +169,23 @@ discard block |
||
| 169 | 169 | $entity_match->entityId = $item_id; |
| 170 | 170 | $annotation->entityMatches[] = $entity_match; |
| 171 | 171 | |
| 172 | - $carry['annotations'][ $annotation_id ] = $annotation; |
|
| 173 | - $carry['occurrences'][ $item_id ][] = $annotation_id; |
|
| 172 | + $carry['annotations'][$annotation_id] = $annotation; |
|
| 173 | + $carry['occurrences'][$item_id][] = $annotation_id; |
|
| 174 | 174 | |
| 175 | 175 | return $carry; |
| 176 | - }, array( 'annotations' => array(), 'occurrences' => array(), ) ); |
|
| 176 | + }, array('annotations' => array(), 'occurrences' => array(),)); |
|
| 177 | 177 | |
| 178 | 178 | $annotations = $parse_data['annotations']; |
| 179 | 179 | $occurrences = $parse_data['occurrences']; |
| 180 | 180 | |
| 181 | - foreach ( array_keys( $occurrences ) as $item_id ) { |
|
| 181 | + foreach (array_keys($occurrences) as $item_id) { |
|
| 182 | 182 | |
| 183 | 183 | // If the entity isn't there, add it. |
| 184 | - if ( ! is_bool( $this->json ) && ! isset( $this->json->entities->{$item_id} ) ) { |
|
| 185 | - $entity = $this->entity_provider_registry->get_local_entity( $item_id ); |
|
| 184 | + if ( ! is_bool($this->json) && ! isset($this->json->entities->{$item_id} )) { |
|
| 185 | + $entity = $this->entity_provider_registry->get_local_entity($item_id); |
|
| 186 | 186 | |
| 187 | 187 | // Entity not found in the local vocabulary, continue to the next one. |
| 188 | - if ( false === $entity ) { |
|
| 188 | + if (false === $entity) { |
|
| 189 | 189 | continue; |
| 190 | 190 | } |
| 191 | 191 | |
@@ -203,12 +203,12 @@ discard block |
||
| 203 | 203 | // function `preselect`, which was called by src/coffee/editpost-widget/app.services.EditorService.coffee in |
| 204 | 204 | // `embedAnalysis`. |
| 205 | 205 | |
| 206 | - if ( ! is_bool( $this->json ) && isset( $this->json->entities ) ) { |
|
| 207 | - foreach ( $this->json->entities as $id => $entity ) { |
|
| 208 | - $this->json->entities->{$id}->occurrences = isset( $occurrences[ $id ] ) ? $occurrences[ $id ] : array();; |
|
| 206 | + if ( ! is_bool($this->json) && isset($this->json->entities)) { |
|
| 207 | + foreach ($this->json->entities as $id => $entity) { |
|
| 208 | + $this->json->entities->{$id}->occurrences = isset($occurrences[$id]) ? $occurrences[$id] : array(); ; |
|
| 209 | 209 | |
| 210 | - foreach ( $this->json->entities->{$id}->occurrences as $annotation_id ) { |
|
| 211 | - $this->json->entities->{$id}->annotations[ $annotation_id ] = array( |
|
| 210 | + foreach ($this->json->entities->{$id}->occurrences as $annotation_id) { |
|
| 211 | + $this->json->entities->{$id}->annotations[$annotation_id] = array( |
|
| 212 | 212 | 'id' => $annotation_id, |
| 213 | 213 | ); |
| 214 | 214 | } |
@@ -217,9 +217,9 @@ discard block |
||
| 217 | 217 | |
| 218 | 218 | // Add the missing annotations. This allows the analysis response to work also if we didn't receive results |
| 219 | 219 | // from the analysis API. |
| 220 | - foreach ( $annotations as $annotation_id => $annotation ) { |
|
| 220 | + foreach ($annotations as $annotation_id => $annotation) { |
|
| 221 | 221 | |
| 222 | - if ( ! is_bool( $this->json ) && ! isset( $this->json->annotations->{$annotation_id} ) ) { |
|
| 222 | + if ( ! is_bool($this->json) && ! isset($this->json->annotations->{$annotation_id} )) { |
|
| 223 | 223 | $this->json->annotations->{$annotation_id} = $annotation; |
| 224 | 224 | } |
| 225 | 225 | |
@@ -241,28 +241,28 @@ discard block |
||
| 241 | 241 | |
| 242 | 242 | // Populating the local entities object |
| 243 | 243 | $entities = array(); |
| 244 | - foreach ( $this->json->annotations as $annotation ) { |
|
| 245 | - foreach ( $annotation->entityMatches as $entity_matches ) { |
|
| 244 | + foreach ($this->json->annotations as $annotation) { |
|
| 245 | + foreach ($annotation->entityMatches as $entity_matches) { |
|
| 246 | 246 | |
| 247 | - $entity_id = $this->entity_uri_service->get_post_id_from_url( $entity_matches->entityId ); |
|
| 248 | - $serialized_entity = wl_serialize_entity( $entity_id ); |
|
| 247 | + $entity_id = $this->entity_uri_service->get_post_id_from_url($entity_matches->entityId); |
|
| 248 | + $serialized_entity = wl_serialize_entity($entity_id); |
|
| 249 | 249 | |
| 250 | - if ( $serialized_entity ) { |
|
| 250 | + if ($serialized_entity) { |
|
| 251 | 251 | $serialized_entity['entityId'] = $serialized_entity['id']; |
| 252 | - unset( $serialized_entity['id'] ); |
|
| 252 | + unset($serialized_entity['id']); |
|
| 253 | 253 | |
| 254 | - $entities[ $entity_matches->entityId ] = $serialized_entity; |
|
| 254 | + $entities[$entity_matches->entityId] = $serialized_entity; |
|
| 255 | 255 | } |
| 256 | 256 | |
| 257 | 257 | } |
| 258 | 258 | } |
| 259 | 259 | |
| 260 | 260 | // Adding occurrences and annotations data structures required by the client-side code. |
| 261 | - foreach ( $entities as $entity_id => $entity ) { |
|
| 262 | - foreach ( $this->json->annotations as $annotation ) { |
|
| 263 | - if ( $annotation->entityMatches[0]->entityId === $entity_id ) { |
|
| 264 | - $entities[ $entity_id ]['occurrences'][] = $annotation->annotationId; |
|
| 265 | - $entities[ $entity_id ]['annotations'][ $annotation->annotationId ]['id'] = $annotation->annotationId; |
|
| 261 | + foreach ($entities as $entity_id => $entity) { |
|
| 262 | + foreach ($this->json->annotations as $annotation) { |
|
| 263 | + if ($annotation->entityMatches[0]->entityId === $entity_id) { |
|
| 264 | + $entities[$entity_id]['occurrences'][] = $annotation->annotationId; |
|
| 265 | + $entities[$entity_id]['annotations'][$annotation->annotationId]['id'] = $annotation->annotationId; |
|
| 266 | 266 | } |
| 267 | 267 | } |
| 268 | 268 | } |
@@ -294,38 +294,38 @@ discard block |
||
| 294 | 294 | * @return $this |
| 295 | 295 | * @since 3.32.3.1 |
| 296 | 296 | */ |
| 297 | - public function remove_excluded_entities( $excluded_uris ) { |
|
| 297 | + public function remove_excluded_entities($excluded_uris) { |
|
| 298 | 298 | |
| 299 | 299 | // If we didnt receive array, return early. |
| 300 | - if ( ! is_array( $excluded_uris ) ) { |
|
| 300 | + if ( ! is_array($excluded_uris)) { |
|
| 301 | 301 | return $this; |
| 302 | 302 | } |
| 303 | 303 | |
| 304 | 304 | // We may also receive an array of null, make sure to filter uris when receiving. |
| 305 | - $excluded_uris = array_filter( $excluded_uris, 'is_string' ); |
|
| 305 | + $excluded_uris = array_filter($excluded_uris, 'is_string'); |
|
| 306 | 306 | |
| 307 | - $this->remove_entities_with_excluded_uris( $excluded_uris ); |
|
| 307 | + $this->remove_entities_with_excluded_uris($excluded_uris); |
|
| 308 | 308 | |
| 309 | - if ( isset( $this->json->annotations ) ) { |
|
| 310 | - foreach ( $this->json->annotations as $annotation_key => &$annotation_data ) { |
|
| 309 | + if (isset($this->json->annotations)) { |
|
| 310 | + foreach ($this->json->annotations as $annotation_key => &$annotation_data) { |
|
| 311 | 311 | |
| 312 | - if ( ! isset( $annotation_data->entityMatches ) ) { |
|
| 312 | + if ( ! isset($annotation_data->entityMatches)) { |
|
| 313 | 313 | continue; |
| 314 | 314 | } |
| 315 | 315 | |
| 316 | - foreach ( $annotation_data->entityMatches as $entity_match_key => $entity_match_data ) { |
|
| 316 | + foreach ($annotation_data->entityMatches as $entity_match_key => $entity_match_data) { |
|
| 317 | 317 | |
| 318 | 318 | $entity_uri = $entity_match_data->entityId; |
| 319 | 319 | |
| 320 | - if ( ! in_array( $entity_uri, $excluded_uris ) ) { |
|
| 320 | + if ( ! in_array($entity_uri, $excluded_uris)) { |
|
| 321 | 321 | continue; |
| 322 | 322 | } |
| 323 | - unset( $annotation_data->entityMatches[ $entity_match_key ] ); |
|
| 323 | + unset($annotation_data->entityMatches[$entity_match_key]); |
|
| 324 | 324 | } |
| 325 | 325 | |
| 326 | - if ( count( $annotation_data->entityMatches ) === 0 ) { |
|
| 326 | + if (count($annotation_data->entityMatches) === 0) { |
|
| 327 | 327 | // Remove the annotation if we have zero empty annotation matches. |
| 328 | - unset( $this->json->annotations->{$annotation_key} ); |
|
| 328 | + unset($this->json->annotations->{$annotation_key} ); |
|
| 329 | 329 | } |
| 330 | 330 | |
| 331 | 331 | } |
@@ -343,23 +343,23 @@ discard block |
||
| 343 | 343 | public function to_string() { |
| 344 | 344 | |
| 345 | 345 | // Add the `JSON_UNESCAPED_UNICODE` only for PHP 5.4+. |
| 346 | - $options = ( version_compare( PHP_VERSION, '5.4', '>=' ) |
|
| 347 | - ? 256 : 0 ); |
|
| 346 | + $options = (version_compare(PHP_VERSION, '5.4', '>=') |
|
| 347 | + ? 256 : 0); |
|
| 348 | 348 | |
| 349 | - return wp_json_encode( $this->json, $options ); |
|
| 349 | + return wp_json_encode($this->json, $options); |
|
| 350 | 350 | } |
| 351 | 351 | |
| 352 | 352 | /** |
| 353 | 353 | * @param array $excluded_uris |
| 354 | 354 | */ |
| 355 | - private function remove_entities_with_excluded_uris( array $excluded_uris ) { |
|
| 355 | + private function remove_entities_with_excluded_uris(array $excluded_uris) { |
|
| 356 | 356 | // Remove the excluded entity uris. |
| 357 | - if ( isset( $this->json->entities ) ) { |
|
| 358 | - foreach ( $excluded_uris as $excluded_uri ) { |
|
| 357 | + if (isset($this->json->entities)) { |
|
| 358 | + foreach ($excluded_uris as $excluded_uri) { |
|
| 359 | 359 | |
| 360 | - if ( isset( $this->json->entities->{$excluded_uri} ) ) { |
|
| 360 | + if (isset($this->json->entities->{$excluded_uri} )) { |
|
| 361 | 361 | // Remove this entity. |
| 362 | - unset( $this->json->entities->{$excluded_uri} ); |
|
| 362 | + unset($this->json->entities->{$excluded_uri} ); |
|
| 363 | 363 | // Also remove the annotations. |
| 364 | 364 | } |
| 365 | 365 | } |