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