@@ -15,105 +15,105 @@ |
||
| 15 | 15 | |
| 16 | 16 | class Assertions { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * Asserts that the provided value is of the specified type. |
|
| 20 | - * |
|
| 21 | - * @param mixed $value The value to test. |
|
| 22 | - * @param string $type The expected type. |
|
| 23 | - * |
|
| 24 | - * @throws Exception when an error occurs. |
|
| 25 | - */ |
|
| 26 | - public static function assert_of_type( $value, $type ) { |
|
| 27 | - |
|
| 28 | - // Check for nulls. |
|
| 29 | - if ( null === $value ) { |
|
| 30 | - throw new Exception( 'Value can`t be null.' ); |
|
| 31 | - } |
|
| 32 | - |
|
| 33 | - // Check for type. |
|
| 34 | - if ( get_class( $value ) !== $type ) { |
|
| 35 | - throw new Exception( "Value must be a $type." ); |
|
| 36 | - } |
|
| 37 | - |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * @throws Exception when the argument is not a string. |
|
| 42 | - */ |
|
| 43 | - public static function is_string( $arg, $message = 'Value must be a string' ) { |
|
| 44 | - if ( ! is_string( $arg ) ) { |
|
| 45 | - throw new Exception( $message ); |
|
| 46 | - } |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * @throws Exception when actual doesn't match expected. |
|
| 51 | - */ |
|
| 52 | - public static function equals( $actual, $expected, $message = 'Values do not match' ) { |
|
| 53 | - if ( $actual !== $expected ) { |
|
| 54 | - throw new Exception( $message ); |
|
| 55 | - } |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * @throws Exception when actual doesn't match expected. |
|
| 60 | - */ |
|
| 61 | - public static function array_key_exists( $arr, $key ) { |
|
| 62 | - if ( ! array_key_exists( $key, $arr ) ) { |
|
| 63 | - throw new Exception( "The key {$key} doesn't exist in array" ); |
|
| 64 | - } |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * @throws Exception when the value doesn't match the pattern. |
|
| 69 | - */ |
|
| 70 | - public static function matches( $value, $pattern, $message = "Value doesn't match" ) { |
|
| 71 | - if ( 1 !== preg_match( $pattern, $value ) ) { |
|
| 72 | - throw new Exception( $message ); |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * @throws Exception when the value doesn't start with the provided scope. |
|
| 79 | - */ |
|
| 80 | - public static function starts_with( $value, $scope, $message = "Value doesn't start with provided scope" ) { |
|
| 81 | - if ( 0 !== strpos( $value, $scope ) ) { |
|
| 82 | - throw new Exception( $message ); |
|
| 83 | - } |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * @throws Exception when the value is not of the specified type. |
|
| 88 | - */ |
|
| 89 | - public static function is_a( $value, $type, $message = 'Value is not of the required type' ) { |
|
| 90 | - if ( ! is_a( $value, $type ) ) { |
|
| 91 | - throw new Exception( $message ); |
|
| 92 | - } |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - public static function is_set( $value, $message = 'Value is not set' ) { |
|
| 96 | - if ( ! isset( $value ) ) { |
|
| 97 | - throw new Exception( $message ); |
|
| 98 | - } |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - public static function not_empty( $value, $message = "Value can't be empty" ) { |
|
| 102 | - if ( empty( $value ) ) { |
|
| 103 | - throw new Exception( $message ); |
|
| 104 | - } |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - public static function is_array( $value, $message = 'Value should be array' ) { |
|
| 108 | - if ( ! is_array( $value ) ) { |
|
| 109 | - throw new Exception( $message ); |
|
| 110 | - } |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - public static function is_numeric( $value, $message = 'Value should be numeric' ) { |
|
| 114 | - if ( ! is_numeric( $value ) ) { |
|
| 115 | - throw new Exception( $message ); |
|
| 116 | - } |
|
| 117 | - } |
|
| 18 | + /** |
|
| 19 | + * Asserts that the provided value is of the specified type. |
|
| 20 | + * |
|
| 21 | + * @param mixed $value The value to test. |
|
| 22 | + * @param string $type The expected type. |
|
| 23 | + * |
|
| 24 | + * @throws Exception when an error occurs. |
|
| 25 | + */ |
|
| 26 | + public static function assert_of_type( $value, $type ) { |
|
| 27 | + |
|
| 28 | + // Check for nulls. |
|
| 29 | + if ( null === $value ) { |
|
| 30 | + throw new Exception( 'Value can`t be null.' ); |
|
| 31 | + } |
|
| 32 | + |
|
| 33 | + // Check for type. |
|
| 34 | + if ( get_class( $value ) !== $type ) { |
|
| 35 | + throw new Exception( "Value must be a $type." ); |
|
| 36 | + } |
|
| 37 | + |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * @throws Exception when the argument is not a string. |
|
| 42 | + */ |
|
| 43 | + public static function is_string( $arg, $message = 'Value must be a string' ) { |
|
| 44 | + if ( ! is_string( $arg ) ) { |
|
| 45 | + throw new Exception( $message ); |
|
| 46 | + } |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * @throws Exception when actual doesn't match expected. |
|
| 51 | + */ |
|
| 52 | + public static function equals( $actual, $expected, $message = 'Values do not match' ) { |
|
| 53 | + if ( $actual !== $expected ) { |
|
| 54 | + throw new Exception( $message ); |
|
| 55 | + } |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * @throws Exception when actual doesn't match expected. |
|
| 60 | + */ |
|
| 61 | + public static function array_key_exists( $arr, $key ) { |
|
| 62 | + if ( ! array_key_exists( $key, $arr ) ) { |
|
| 63 | + throw new Exception( "The key {$key} doesn't exist in array" ); |
|
| 64 | + } |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * @throws Exception when the value doesn't match the pattern. |
|
| 69 | + */ |
|
| 70 | + public static function matches( $value, $pattern, $message = "Value doesn't match" ) { |
|
| 71 | + if ( 1 !== preg_match( $pattern, $value ) ) { |
|
| 72 | + throw new Exception( $message ); |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * @throws Exception when the value doesn't start with the provided scope. |
|
| 79 | + */ |
|
| 80 | + public static function starts_with( $value, $scope, $message = "Value doesn't start with provided scope" ) { |
|
| 81 | + if ( 0 !== strpos( $value, $scope ) ) { |
|
| 82 | + throw new Exception( $message ); |
|
| 83 | + } |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * @throws Exception when the value is not of the specified type. |
|
| 88 | + */ |
|
| 89 | + public static function is_a( $value, $type, $message = 'Value is not of the required type' ) { |
|
| 90 | + if ( ! is_a( $value, $type ) ) { |
|
| 91 | + throw new Exception( $message ); |
|
| 92 | + } |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + public static function is_set( $value, $message = 'Value is not set' ) { |
|
| 96 | + if ( ! isset( $value ) ) { |
|
| 97 | + throw new Exception( $message ); |
|
| 98 | + } |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + public static function not_empty( $value, $message = "Value can't be empty" ) { |
|
| 102 | + if ( empty( $value ) ) { |
|
| 103 | + throw new Exception( $message ); |
|
| 104 | + } |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + public static function is_array( $value, $message = 'Value should be array' ) { |
|
| 108 | + if ( ! is_array( $value ) ) { |
|
| 109 | + throw new Exception( $message ); |
|
| 110 | + } |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + public static function is_numeric( $value, $message = 'Value should be numeric' ) { |
|
| 114 | + if ( ! is_numeric( $value ) ) { |
|
| 115 | + throw new Exception( $message ); |
|
| 116 | + } |
|
| 117 | + } |
|
| 118 | 118 | |
| 119 | 119 | } |
@@ -23,16 +23,16 @@ discard block |
||
| 23 | 23 | * |
| 24 | 24 | * @throws Exception when an error occurs. |
| 25 | 25 | */ |
| 26 | - public static function assert_of_type( $value, $type ) { |
|
| 26 | + public static function assert_of_type($value, $type) { |
|
| 27 | 27 | |
| 28 | 28 | // Check for nulls. |
| 29 | - if ( null === $value ) { |
|
| 30 | - throw new Exception( 'Value can`t be null.' ); |
|
| 29 | + if (null === $value) { |
|
| 30 | + throw new Exception('Value can`t be null.'); |
|
| 31 | 31 | } |
| 32 | 32 | |
| 33 | 33 | // Check for type. |
| 34 | - if ( get_class( $value ) !== $type ) { |
|
| 35 | - throw new Exception( "Value must be a $type." ); |
|
| 34 | + if (get_class($value) !== $type) { |
|
| 35 | + throw new Exception("Value must be a $type."); |
|
| 36 | 36 | } |
| 37 | 37 | |
| 38 | 38 | } |
@@ -40,36 +40,36 @@ discard block |
||
| 40 | 40 | /** |
| 41 | 41 | * @throws Exception when the argument is not a string. |
| 42 | 42 | */ |
| 43 | - public static function is_string( $arg, $message = 'Value must be a string' ) { |
|
| 44 | - if ( ! is_string( $arg ) ) { |
|
| 45 | - throw new Exception( $message ); |
|
| 43 | + public static function is_string($arg, $message = 'Value must be a string') { |
|
| 44 | + if ( ! is_string($arg)) { |
|
| 45 | + throw new Exception($message); |
|
| 46 | 46 | } |
| 47 | 47 | } |
| 48 | 48 | |
| 49 | 49 | /** |
| 50 | 50 | * @throws Exception when actual doesn't match expected. |
| 51 | 51 | */ |
| 52 | - public static function equals( $actual, $expected, $message = 'Values do not match' ) { |
|
| 53 | - if ( $actual !== $expected ) { |
|
| 54 | - throw new Exception( $message ); |
|
| 52 | + public static function equals($actual, $expected, $message = 'Values do not match') { |
|
| 53 | + if ($actual !== $expected) { |
|
| 54 | + throw new Exception($message); |
|
| 55 | 55 | } |
| 56 | 56 | } |
| 57 | 57 | |
| 58 | 58 | /** |
| 59 | 59 | * @throws Exception when actual doesn't match expected. |
| 60 | 60 | */ |
| 61 | - public static function array_key_exists( $arr, $key ) { |
|
| 62 | - if ( ! array_key_exists( $key, $arr ) ) { |
|
| 63 | - throw new Exception( "The key {$key} doesn't exist in array" ); |
|
| 61 | + public static function array_key_exists($arr, $key) { |
|
| 62 | + if ( ! array_key_exists($key, $arr)) { |
|
| 63 | + throw new Exception("The key {$key} doesn't exist in array"); |
|
| 64 | 64 | } |
| 65 | 65 | } |
| 66 | 66 | |
| 67 | 67 | /** |
| 68 | 68 | * @throws Exception when the value doesn't match the pattern. |
| 69 | 69 | */ |
| 70 | - public static function matches( $value, $pattern, $message = "Value doesn't match" ) { |
|
| 71 | - if ( 1 !== preg_match( $pattern, $value ) ) { |
|
| 72 | - throw new Exception( $message ); |
|
| 70 | + public static function matches($value, $pattern, $message = "Value doesn't match") { |
|
| 71 | + if (1 !== preg_match($pattern, $value)) { |
|
| 72 | + throw new Exception($message); |
|
| 73 | 73 | } |
| 74 | 74 | |
| 75 | 75 | } |
@@ -77,42 +77,42 @@ discard block |
||
| 77 | 77 | /** |
| 78 | 78 | * @throws Exception when the value doesn't start with the provided scope. |
| 79 | 79 | */ |
| 80 | - public static function starts_with( $value, $scope, $message = "Value doesn't start with provided scope" ) { |
|
| 81 | - if ( 0 !== strpos( $value, $scope ) ) { |
|
| 82 | - throw new Exception( $message ); |
|
| 80 | + public static function starts_with($value, $scope, $message = "Value doesn't start with provided scope") { |
|
| 81 | + if (0 !== strpos($value, $scope)) { |
|
| 82 | + throw new Exception($message); |
|
| 83 | 83 | } |
| 84 | 84 | } |
| 85 | 85 | |
| 86 | 86 | /** |
| 87 | 87 | * @throws Exception when the value is not of the specified type. |
| 88 | 88 | */ |
| 89 | - public static function is_a( $value, $type, $message = 'Value is not of the required type' ) { |
|
| 90 | - if ( ! is_a( $value, $type ) ) { |
|
| 91 | - throw new Exception( $message ); |
|
| 89 | + public static function is_a($value, $type, $message = 'Value is not of the required type') { |
|
| 90 | + if ( ! is_a($value, $type)) { |
|
| 91 | + throw new Exception($message); |
|
| 92 | 92 | } |
| 93 | 93 | } |
| 94 | 94 | |
| 95 | - public static function is_set( $value, $message = 'Value is not set' ) { |
|
| 96 | - if ( ! isset( $value ) ) { |
|
| 97 | - throw new Exception( $message ); |
|
| 95 | + public static function is_set($value, $message = 'Value is not set') { |
|
| 96 | + if ( ! isset($value)) { |
|
| 97 | + throw new Exception($message); |
|
| 98 | 98 | } |
| 99 | 99 | } |
| 100 | 100 | |
| 101 | - public static function not_empty( $value, $message = "Value can't be empty" ) { |
|
| 102 | - if ( empty( $value ) ) { |
|
| 103 | - throw new Exception( $message ); |
|
| 101 | + public static function not_empty($value, $message = "Value can't be empty") { |
|
| 102 | + if (empty($value)) { |
|
| 103 | + throw new Exception($message); |
|
| 104 | 104 | } |
| 105 | 105 | } |
| 106 | 106 | |
| 107 | - public static function is_array( $value, $message = 'Value should be array' ) { |
|
| 108 | - if ( ! is_array( $value ) ) { |
|
| 109 | - throw new Exception( $message ); |
|
| 107 | + public static function is_array($value, $message = 'Value should be array') { |
|
| 108 | + if ( ! is_array($value)) { |
|
| 109 | + throw new Exception($message); |
|
| 110 | 110 | } |
| 111 | 111 | } |
| 112 | 112 | |
| 113 | - public static function is_numeric( $value, $message = 'Value should be numeric' ) { |
|
| 114 | - if ( ! is_numeric( $value ) ) { |
|
| 115 | - throw new Exception( $message ); |
|
| 113 | + public static function is_numeric($value, $message = 'Value should be numeric') { |
|
| 114 | + if ( ! is_numeric($value)) { |
|
| 115 | + throw new Exception($message); |
|
| 116 | 116 | } |
| 117 | 117 | } |
| 118 | 118 | |
@@ -7,167 +7,167 @@ |
||
| 7 | 7 | |
| 8 | 8 | class Jsonld_Article_Wrapper { |
| 9 | 9 | |
| 10 | - public static $article_types = array( |
|
| 11 | - 'Article', |
|
| 12 | - 'AdvertiserContentArticle', |
|
| 13 | - 'NewsArticle', |
|
| 14 | - 'AnalysisNewsArticle', |
|
| 15 | - 'AskPublicNewsArticle', |
|
| 16 | - 'BackgroundNewsArticle', |
|
| 17 | - 'OpinionNewsArticle', |
|
| 18 | - 'ReportageNewsArticle', |
|
| 19 | - 'ReviewNewsArticle', |
|
| 20 | - 'Report', |
|
| 21 | - 'SatiricalArticle', |
|
| 22 | - 'ScholarlyArticle', |
|
| 23 | - 'MedicalScholarlyArticle', |
|
| 24 | - 'SocialMediaPosting', |
|
| 25 | - 'BlogPosting', |
|
| 26 | - 'LiveBlogPosting', |
|
| 27 | - 'DiscussionForumPosting', |
|
| 28 | - 'TechArticle', |
|
| 29 | - 'APIReference', |
|
| 30 | - ); |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * @var Wordlift_Post_To_Jsonld_Converter |
|
| 34 | - */ |
|
| 35 | - private $post_to_jsonld_converter; |
|
| 36 | - /** |
|
| 37 | - * @var \Wordlift_Cached_Post_Converter |
|
| 38 | - */ |
|
| 39 | - private $cached_postid_to_jsonld_converter; |
|
| 40 | - /** |
|
| 41 | - * @var \Wordlift_Entity_Uri_Service |
|
| 42 | - */ |
|
| 43 | - private $entity_uri_service; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Jsonld_Article_Wrapper constructor. |
|
| 47 | - * |
|
| 48 | - * @param Wordlift_Post_To_Jsonld_Converter $post_to_jsonld_converter |
|
| 49 | - * @param $cached_postid_to_jsonld_converter |
|
| 50 | - */ |
|
| 51 | - public function __construct( $post_to_jsonld_converter, $cached_postid_to_jsonld_converter ) { |
|
| 52 | - |
|
| 53 | - $this->post_to_jsonld_converter = $post_to_jsonld_converter->new_instance_with_filters_disabled(); |
|
| 54 | - |
|
| 55 | - add_filter( |
|
| 56 | - 'wl_after_get_jsonld', |
|
| 57 | - array( |
|
| 58 | - $this, |
|
| 59 | - 'after_get_jsonld', |
|
| 60 | - ), |
|
| 61 | - PHP_INT_MAX - 100, |
|
| 62 | - 3 |
|
| 63 | - ); |
|
| 64 | - |
|
| 65 | - $this->cached_postid_to_jsonld_converter = $cached_postid_to_jsonld_converter; |
|
| 66 | - |
|
| 67 | - $this->entity_uri_service = \Wordlift_Entity_Uri_Service::get_instance(); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - public function after_get_jsonld( $jsonld, $post_id, $context ) { |
|
| 71 | - |
|
| 72 | - // Invalid data structure |
|
| 73 | - if ( ! is_array( $jsonld ) || ! isset( $jsonld[0] ) || ! is_array( $jsonld[0] ) ) { |
|
| 74 | - return $jsonld; |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - if ( Jsonld_Context_Enum::PAGE !== $context |
|
| 78 | - // Returns true for "1", "true", "on" and "yes". Returns false otherwise. |
|
| 79 | - && ! filter_input( INPUT_GET, 'article_wrapper', FILTER_VALIDATE_BOOLEAN ) ) { |
|
| 80 | - return $jsonld; |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - // Copy the 1st array element |
|
| 84 | - $post_jsonld = $jsonld[0]; |
|
| 85 | - |
|
| 86 | - // Don't wrap in article if the json-ld is already about an Article (or its descendants). `@type` must be |
|
| 87 | - // in the schema.org context, i.e. `Article`, not `http://schema.org/Article`. |
|
| 88 | - if ( ! isset( $post_jsonld['@id'] ) || ! isset( $post_jsonld['@type'] ) || $this->is_article( $post_jsonld['@type'] ) ) { |
|
| 89 | - return $jsonld; |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - $references = array(); |
|
| 93 | - $reference_infos = array(); |
|
| 94 | - |
|
| 95 | - // Convert the post as Article. |
|
| 96 | - $article_jsonld = $this->post_to_jsonld_converter->convert( $post_id, $references, $reference_infos, new Relations() ); |
|
| 97 | - |
|
| 98 | - $article_jsonld['@id'] = $post_jsonld['@id'] . '#article'; |
|
| 99 | - // Reset the type, since by default the type assigned via the Entity Type taxonomy is used. |
|
| 100 | - $article_jsonld['@type'] = 'Article'; |
|
| 101 | - $article_jsonld['about'] = array( '@id' => $post_jsonld['@id'] ); |
|
| 102 | - |
|
| 103 | - // Copy over the URLs. |
|
| 104 | - if ( isset( $post_jsonld['url'] ) ) { |
|
| 105 | - $article_jsonld['url'] = $post_jsonld['url']; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - array_unshift( $jsonld, $article_jsonld ); |
|
| 109 | - |
|
| 110 | - $author_jsonld = $this->get_author_linked_entity( $article_jsonld ); |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * The author entities can be present in graph for some entity types |
|
| 114 | - * for Person and Organization, so check before we add it to graph. |
|
| 115 | - * reference : https://schema.org/author |
|
| 116 | - */ |
|
| 117 | - if ( $author_jsonld && ! $this->is_author_entity_present_in_graph( $jsonld, $article_jsonld['author']['@id'] ) ) { |
|
| 118 | - $jsonld[] = $author_jsonld; |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - return $jsonld; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - private function is_article( $schema_types ) { |
|
| 125 | - |
|
| 126 | - $array_intersect = array_intersect( self::$article_types, (array) $schema_types ); |
|
| 127 | - |
|
| 128 | - return ! empty( $array_intersect ); |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - private function get_author_linked_entity( $article_jsonld ) { |
|
| 132 | - if ( ! array_key_exists( 'author', $article_jsonld ) ) { |
|
| 133 | - return false; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - $author = $article_jsonld['author']; |
|
| 10 | + public static $article_types = array( |
|
| 11 | + 'Article', |
|
| 12 | + 'AdvertiserContentArticle', |
|
| 13 | + 'NewsArticle', |
|
| 14 | + 'AnalysisNewsArticle', |
|
| 15 | + 'AskPublicNewsArticle', |
|
| 16 | + 'BackgroundNewsArticle', |
|
| 17 | + 'OpinionNewsArticle', |
|
| 18 | + 'ReportageNewsArticle', |
|
| 19 | + 'ReviewNewsArticle', |
|
| 20 | + 'Report', |
|
| 21 | + 'SatiricalArticle', |
|
| 22 | + 'ScholarlyArticle', |
|
| 23 | + 'MedicalScholarlyArticle', |
|
| 24 | + 'SocialMediaPosting', |
|
| 25 | + 'BlogPosting', |
|
| 26 | + 'LiveBlogPosting', |
|
| 27 | + 'DiscussionForumPosting', |
|
| 28 | + 'TechArticle', |
|
| 29 | + 'APIReference', |
|
| 30 | + ); |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * @var Wordlift_Post_To_Jsonld_Converter |
|
| 34 | + */ |
|
| 35 | + private $post_to_jsonld_converter; |
|
| 36 | + /** |
|
| 37 | + * @var \Wordlift_Cached_Post_Converter |
|
| 38 | + */ |
|
| 39 | + private $cached_postid_to_jsonld_converter; |
|
| 40 | + /** |
|
| 41 | + * @var \Wordlift_Entity_Uri_Service |
|
| 42 | + */ |
|
| 43 | + private $entity_uri_service; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Jsonld_Article_Wrapper constructor. |
|
| 47 | + * |
|
| 48 | + * @param Wordlift_Post_To_Jsonld_Converter $post_to_jsonld_converter |
|
| 49 | + * @param $cached_postid_to_jsonld_converter |
|
| 50 | + */ |
|
| 51 | + public function __construct( $post_to_jsonld_converter, $cached_postid_to_jsonld_converter ) { |
|
| 52 | + |
|
| 53 | + $this->post_to_jsonld_converter = $post_to_jsonld_converter->new_instance_with_filters_disabled(); |
|
| 54 | + |
|
| 55 | + add_filter( |
|
| 56 | + 'wl_after_get_jsonld', |
|
| 57 | + array( |
|
| 58 | + $this, |
|
| 59 | + 'after_get_jsonld', |
|
| 60 | + ), |
|
| 61 | + PHP_INT_MAX - 100, |
|
| 62 | + 3 |
|
| 63 | + ); |
|
| 64 | + |
|
| 65 | + $this->cached_postid_to_jsonld_converter = $cached_postid_to_jsonld_converter; |
|
| 66 | + |
|
| 67 | + $this->entity_uri_service = \Wordlift_Entity_Uri_Service::get_instance(); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + public function after_get_jsonld( $jsonld, $post_id, $context ) { |
|
| 71 | + |
|
| 72 | + // Invalid data structure |
|
| 73 | + if ( ! is_array( $jsonld ) || ! isset( $jsonld[0] ) || ! is_array( $jsonld[0] ) ) { |
|
| 74 | + return $jsonld; |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + if ( Jsonld_Context_Enum::PAGE !== $context |
|
| 78 | + // Returns true for "1", "true", "on" and "yes". Returns false otherwise. |
|
| 79 | + && ! filter_input( INPUT_GET, 'article_wrapper', FILTER_VALIDATE_BOOLEAN ) ) { |
|
| 80 | + return $jsonld; |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + // Copy the 1st array element |
|
| 84 | + $post_jsonld = $jsonld[0]; |
|
| 85 | + |
|
| 86 | + // Don't wrap in article if the json-ld is already about an Article (or its descendants). `@type` must be |
|
| 87 | + // in the schema.org context, i.e. `Article`, not `http://schema.org/Article`. |
|
| 88 | + if ( ! isset( $post_jsonld['@id'] ) || ! isset( $post_jsonld['@type'] ) || $this->is_article( $post_jsonld['@type'] ) ) { |
|
| 89 | + return $jsonld; |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + $references = array(); |
|
| 93 | + $reference_infos = array(); |
|
| 94 | + |
|
| 95 | + // Convert the post as Article. |
|
| 96 | + $article_jsonld = $this->post_to_jsonld_converter->convert( $post_id, $references, $reference_infos, new Relations() ); |
|
| 97 | + |
|
| 98 | + $article_jsonld['@id'] = $post_jsonld['@id'] . '#article'; |
|
| 99 | + // Reset the type, since by default the type assigned via the Entity Type taxonomy is used. |
|
| 100 | + $article_jsonld['@type'] = 'Article'; |
|
| 101 | + $article_jsonld['about'] = array( '@id' => $post_jsonld['@id'] ); |
|
| 102 | + |
|
| 103 | + // Copy over the URLs. |
|
| 104 | + if ( isset( $post_jsonld['url'] ) ) { |
|
| 105 | + $article_jsonld['url'] = $post_jsonld['url']; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + array_unshift( $jsonld, $article_jsonld ); |
|
| 109 | + |
|
| 110 | + $author_jsonld = $this->get_author_linked_entity( $article_jsonld ); |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * The author entities can be present in graph for some entity types |
|
| 114 | + * for Person and Organization, so check before we add it to graph. |
|
| 115 | + * reference : https://schema.org/author |
|
| 116 | + */ |
|
| 117 | + if ( $author_jsonld && ! $this->is_author_entity_present_in_graph( $jsonld, $article_jsonld['author']['@id'] ) ) { |
|
| 118 | + $jsonld[] = $author_jsonld; |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + return $jsonld; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + private function is_article( $schema_types ) { |
|
| 125 | + |
|
| 126 | + $array_intersect = array_intersect( self::$article_types, (array) $schema_types ); |
|
| 127 | + |
|
| 128 | + return ! empty( $array_intersect ); |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + private function get_author_linked_entity( $article_jsonld ) { |
|
| 132 | + if ( ! array_key_exists( 'author', $article_jsonld ) ) { |
|
| 133 | + return false; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + $author = $article_jsonld['author']; |
|
| 137 | 137 | |
| 138 | - if ( count( array_keys( $author ) ) !== 1 || ! array_key_exists( '@id', $author ) ) { |
|
| 139 | - return false; |
|
| 140 | - } |
|
| 138 | + if ( count( array_keys( $author ) ) !== 1 || ! array_key_exists( '@id', $author ) ) { |
|
| 139 | + return false; |
|
| 140 | + } |
|
| 141 | 141 | |
| 142 | - $author_linked_entity_id = $author['@id']; |
|
| 142 | + $author_linked_entity_id = $author['@id']; |
|
| 143 | 143 | |
| 144 | - $author_entity_post = $this->entity_uri_service->get_entity( $author_linked_entity_id ); |
|
| 144 | + $author_entity_post = $this->entity_uri_service->get_entity( $author_linked_entity_id ); |
|
| 145 | 145 | |
| 146 | - if ( ! $author_entity_post instanceof \WP_Post ) { |
|
| 147 | - return false; |
|
| 148 | - } |
|
| 146 | + if ( ! $author_entity_post instanceof \WP_Post ) { |
|
| 147 | + return false; |
|
| 148 | + } |
|
| 149 | 149 | |
| 150 | - $references = array(); |
|
| 151 | - $reference_infos = array(); |
|
| 150 | + $references = array(); |
|
| 151 | + $reference_infos = array(); |
|
| 152 | 152 | |
| 153 | - return $this->cached_postid_to_jsonld_converter->convert( |
|
| 154 | - $author_entity_post->ID, |
|
| 155 | - $references, |
|
| 156 | - $reference_infos, |
|
| 157 | - new Relations() |
|
| 158 | - ); |
|
| 153 | + return $this->cached_postid_to_jsonld_converter->convert( |
|
| 154 | + $author_entity_post->ID, |
|
| 155 | + $references, |
|
| 156 | + $reference_infos, |
|
| 157 | + new Relations() |
|
| 158 | + ); |
|
| 159 | 159 | |
| 160 | - } |
|
| 160 | + } |
|
| 161 | 161 | |
| 162 | - private function is_author_entity_present_in_graph( $jsonld, $author_entity_id ) { |
|
| 162 | + private function is_author_entity_present_in_graph( $jsonld, $author_entity_id ) { |
|
| 163 | 163 | |
| 164 | - foreach ( $jsonld as $item ) { |
|
| 165 | - if ( $item && array_key_exists( '@id', $item ) && $item['@id'] === $author_entity_id ) { |
|
| 166 | - return true; |
|
| 167 | - } |
|
| 168 | - } |
|
| 164 | + foreach ( $jsonld as $item ) { |
|
| 165 | + if ( $item && array_key_exists( '@id', $item ) && $item['@id'] === $author_entity_id ) { |
|
| 166 | + return true; |
|
| 167 | + } |
|
| 168 | + } |
|
| 169 | 169 | |
| 170 | - return false; |
|
| 171 | - } |
|
| 170 | + return false; |
|
| 171 | + } |
|
| 172 | 172 | |
| 173 | 173 | } |
@@ -48,7 +48,7 @@ discard block |
||
| 48 | 48 | * @param Wordlift_Post_To_Jsonld_Converter $post_to_jsonld_converter |
| 49 | 49 | * @param $cached_postid_to_jsonld_converter |
| 50 | 50 | */ |
| 51 | - public function __construct( $post_to_jsonld_converter, $cached_postid_to_jsonld_converter ) { |
|
| 51 | + public function __construct($post_to_jsonld_converter, $cached_postid_to_jsonld_converter) { |
|
| 52 | 52 | |
| 53 | 53 | $this->post_to_jsonld_converter = $post_to_jsonld_converter->new_instance_with_filters_disabled(); |
| 54 | 54 | |
@@ -67,16 +67,16 @@ discard block |
||
| 67 | 67 | $this->entity_uri_service = \Wordlift_Entity_Uri_Service::get_instance(); |
| 68 | 68 | } |
| 69 | 69 | |
| 70 | - public function after_get_jsonld( $jsonld, $post_id, $context ) { |
|
| 70 | + public function after_get_jsonld($jsonld, $post_id, $context) { |
|
| 71 | 71 | |
| 72 | 72 | // Invalid data structure |
| 73 | - if ( ! is_array( $jsonld ) || ! isset( $jsonld[0] ) || ! is_array( $jsonld[0] ) ) { |
|
| 73 | + if ( ! is_array($jsonld) || ! isset($jsonld[0]) || ! is_array($jsonld[0])) { |
|
| 74 | 74 | return $jsonld; |
| 75 | 75 | } |
| 76 | 76 | |
| 77 | - if ( Jsonld_Context_Enum::PAGE !== $context |
|
| 77 | + if (Jsonld_Context_Enum::PAGE !== $context |
|
| 78 | 78 | // Returns true for "1", "true", "on" and "yes". Returns false otherwise. |
| 79 | - && ! filter_input( INPUT_GET, 'article_wrapper', FILTER_VALIDATE_BOOLEAN ) ) { |
|
| 79 | + && ! filter_input(INPUT_GET, 'article_wrapper', FILTER_VALIDATE_BOOLEAN)) { |
|
| 80 | 80 | return $jsonld; |
| 81 | 81 | } |
| 82 | 82 | |
@@ -85,7 +85,7 @@ discard block |
||
| 85 | 85 | |
| 86 | 86 | // Don't wrap in article if the json-ld is already about an Article (or its descendants). `@type` must be |
| 87 | 87 | // in the schema.org context, i.e. `Article`, not `http://schema.org/Article`. |
| 88 | - if ( ! isset( $post_jsonld['@id'] ) || ! isset( $post_jsonld['@type'] ) || $this->is_article( $post_jsonld['@type'] ) ) { |
|
| 88 | + if ( ! isset($post_jsonld['@id']) || ! isset($post_jsonld['@type']) || $this->is_article($post_jsonld['@type'])) { |
|
| 89 | 89 | return $jsonld; |
| 90 | 90 | } |
| 91 | 91 | |
@@ -93,57 +93,57 @@ discard block |
||
| 93 | 93 | $reference_infos = array(); |
| 94 | 94 | |
| 95 | 95 | // Convert the post as Article. |
| 96 | - $article_jsonld = $this->post_to_jsonld_converter->convert( $post_id, $references, $reference_infos, new Relations() ); |
|
| 96 | + $article_jsonld = $this->post_to_jsonld_converter->convert($post_id, $references, $reference_infos, new Relations()); |
|
| 97 | 97 | |
| 98 | - $article_jsonld['@id'] = $post_jsonld['@id'] . '#article'; |
|
| 98 | + $article_jsonld['@id'] = $post_jsonld['@id'].'#article'; |
|
| 99 | 99 | // Reset the type, since by default the type assigned via the Entity Type taxonomy is used. |
| 100 | 100 | $article_jsonld['@type'] = 'Article'; |
| 101 | - $article_jsonld['about'] = array( '@id' => $post_jsonld['@id'] ); |
|
| 101 | + $article_jsonld['about'] = array('@id' => $post_jsonld['@id']); |
|
| 102 | 102 | |
| 103 | 103 | // Copy over the URLs. |
| 104 | - if ( isset( $post_jsonld['url'] ) ) { |
|
| 104 | + if (isset($post_jsonld['url'])) { |
|
| 105 | 105 | $article_jsonld['url'] = $post_jsonld['url']; |
| 106 | 106 | } |
| 107 | 107 | |
| 108 | - array_unshift( $jsonld, $article_jsonld ); |
|
| 108 | + array_unshift($jsonld, $article_jsonld); |
|
| 109 | 109 | |
| 110 | - $author_jsonld = $this->get_author_linked_entity( $article_jsonld ); |
|
| 110 | + $author_jsonld = $this->get_author_linked_entity($article_jsonld); |
|
| 111 | 111 | |
| 112 | 112 | /** |
| 113 | 113 | * The author entities can be present in graph for some entity types |
| 114 | 114 | * for Person and Organization, so check before we add it to graph. |
| 115 | 115 | * reference : https://schema.org/author |
| 116 | 116 | */ |
| 117 | - if ( $author_jsonld && ! $this->is_author_entity_present_in_graph( $jsonld, $article_jsonld['author']['@id'] ) ) { |
|
| 117 | + if ($author_jsonld && ! $this->is_author_entity_present_in_graph($jsonld, $article_jsonld['author']['@id'])) { |
|
| 118 | 118 | $jsonld[] = $author_jsonld; |
| 119 | 119 | } |
| 120 | 120 | |
| 121 | 121 | return $jsonld; |
| 122 | 122 | } |
| 123 | 123 | |
| 124 | - private function is_article( $schema_types ) { |
|
| 124 | + private function is_article($schema_types) { |
|
| 125 | 125 | |
| 126 | - $array_intersect = array_intersect( self::$article_types, (array) $schema_types ); |
|
| 126 | + $array_intersect = array_intersect(self::$article_types, (array) $schema_types); |
|
| 127 | 127 | |
| 128 | - return ! empty( $array_intersect ); |
|
| 128 | + return ! empty($array_intersect); |
|
| 129 | 129 | } |
| 130 | 130 | |
| 131 | - private function get_author_linked_entity( $article_jsonld ) { |
|
| 132 | - if ( ! array_key_exists( 'author', $article_jsonld ) ) { |
|
| 131 | + private function get_author_linked_entity($article_jsonld) { |
|
| 132 | + if ( ! array_key_exists('author', $article_jsonld)) { |
|
| 133 | 133 | return false; |
| 134 | 134 | } |
| 135 | 135 | |
| 136 | 136 | $author = $article_jsonld['author']; |
| 137 | 137 | |
| 138 | - if ( count( array_keys( $author ) ) !== 1 || ! array_key_exists( '@id', $author ) ) { |
|
| 138 | + if (count(array_keys($author)) !== 1 || ! array_key_exists('@id', $author)) { |
|
| 139 | 139 | return false; |
| 140 | 140 | } |
| 141 | 141 | |
| 142 | 142 | $author_linked_entity_id = $author['@id']; |
| 143 | 143 | |
| 144 | - $author_entity_post = $this->entity_uri_service->get_entity( $author_linked_entity_id ); |
|
| 144 | + $author_entity_post = $this->entity_uri_service->get_entity($author_linked_entity_id); |
|
| 145 | 145 | |
| 146 | - if ( ! $author_entity_post instanceof \WP_Post ) { |
|
| 146 | + if ( ! $author_entity_post instanceof \WP_Post) { |
|
| 147 | 147 | return false; |
| 148 | 148 | } |
| 149 | 149 | |
@@ -159,10 +159,10 @@ discard block |
||
| 159 | 159 | |
| 160 | 160 | } |
| 161 | 161 | |
| 162 | - private function is_author_entity_present_in_graph( $jsonld, $author_entity_id ) { |
|
| 162 | + private function is_author_entity_present_in_graph($jsonld, $author_entity_id) { |
|
| 163 | 163 | |
| 164 | - foreach ( $jsonld as $item ) { |
|
| 165 | - if ( $item && array_key_exists( '@id', $item ) && $item['@id'] === $author_entity_id ) { |
|
| 164 | + foreach ($jsonld as $item) { |
|
| 165 | + if ($item && array_key_exists('@id', $item) && $item['@id'] === $author_entity_id) { |
|
| 166 | 166 | return true; |
| 167 | 167 | } |
| 168 | 168 | } |
@@ -10,9 +10,9 @@ |
||
| 10 | 10 | |
| 11 | 11 | interface Occurrences { |
| 12 | 12 | |
| 13 | - /** |
|
| 14 | - * @return array Return json data structure. |
|
| 15 | - */ |
|
| 16 | - public function add_occurrences_to_entities( $occurrences, $json, $post_id ); |
|
| 13 | + /** |
|
| 14 | + * @return array Return json data structure. |
|
| 15 | + */ |
|
| 16 | + public function add_occurrences_to_entities( $occurrences, $json, $post_id ); |
|
| 17 | 17 | |
| 18 | 18 | } |
@@ -13,6 +13,6 @@ |
||
| 13 | 13 | /** |
| 14 | 14 | * @return array Return json data structure. |
| 15 | 15 | */ |
| 16 | - public function add_occurrences_to_entities( $occurrences, $json, $post_id ); |
|
| 16 | + public function add_occurrences_to_entities($occurrences, $json, $post_id); |
|
| 17 | 17 | |
| 18 | 18 | } |
@@ -11,20 +11,20 @@ |
||
| 11 | 11 | |
| 12 | 12 | class Default_Strategy extends Singleton implements Occurrences { |
| 13 | 13 | |
| 14 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 15 | - public function add_occurrences_to_entities( $occurrences, $json, $post_id ) { |
|
| 14 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 15 | + public function add_occurrences_to_entities( $occurrences, $json, $post_id ) { |
|
| 16 | 16 | |
| 17 | - foreach ( $json->entities as $id => $entity ) { |
|
| 17 | + foreach ( $json->entities as $id => $entity ) { |
|
| 18 | 18 | |
| 19 | - $json->entities->{$id}->occurrences = isset( $occurrences[ $id ] ) ? $occurrences[ $id ] : array(); |
|
| 19 | + $json->entities->{$id}->occurrences = isset( $occurrences[ $id ] ) ? $occurrences[ $id ] : array(); |
|
| 20 | 20 | |
| 21 | - foreach ( $json->entities->{$id}->occurrences as $annotation_id ) { |
|
| 22 | - $json->entities->{$id}->annotations[ $annotation_id ] = array( |
|
| 23 | - 'id' => $annotation_id, |
|
| 24 | - ); |
|
| 25 | - } |
|
| 26 | - } |
|
| 21 | + foreach ( $json->entities->{$id}->occurrences as $annotation_id ) { |
|
| 22 | + $json->entities->{$id}->annotations[ $annotation_id ] = array( |
|
| 23 | + 'id' => $annotation_id, |
|
| 24 | + ); |
|
| 25 | + } |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | - return $json; |
|
| 29 | - } |
|
| 28 | + return $json; |
|
| 29 | + } |
|
| 30 | 30 | } |
@@ -12,14 +12,14 @@ |
||
| 12 | 12 | class Default_Strategy extends Singleton implements Occurrences { |
| 13 | 13 | |
| 14 | 14 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 15 | - public function add_occurrences_to_entities( $occurrences, $json, $post_id ) { |
|
| 15 | + public function add_occurrences_to_entities($occurrences, $json, $post_id) { |
|
| 16 | 16 | |
| 17 | - foreach ( $json->entities as $id => $entity ) { |
|
| 17 | + foreach ($json->entities as $id => $entity) { |
|
| 18 | 18 | |
| 19 | - $json->entities->{$id}->occurrences = isset( $occurrences[ $id ] ) ? $occurrences[ $id ] : array(); |
|
| 19 | + $json->entities->{$id}->occurrences = isset($occurrences[$id]) ? $occurrences[$id] : array(); |
|
| 20 | 20 | |
| 21 | - foreach ( $json->entities->{$id}->occurrences as $annotation_id ) { |
|
| 22 | - $json->entities->{$id}->annotations[ $annotation_id ] = array( |
|
| 21 | + foreach ($json->entities->{$id}->occurrences as $annotation_id) { |
|
| 22 | + $json->entities->{$id}->annotations[$annotation_id] = array( |
|
| 23 | 23 | 'id' => $annotation_id, |
| 24 | 24 | ); |
| 25 | 25 | } |
@@ -16,382 +16,382 @@ |
||
| 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 | - * @var Entity_Helper |
|
| 39 | - */ |
|
| 40 | - private $entity_helper; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * @var int $post_id |
|
| 44 | - */ |
|
| 45 | - private $post_id; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * Analysis_Response_Ops constructor. |
|
| 49 | - * |
|
| 50 | - * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service}. |
|
| 51 | - * @param Entity_Helper $entity_helper The {@link Entity_Helper}. |
|
| 52 | - * @param mixed $json The analysis response json. |
|
| 53 | - * |
|
| 54 | - * @since 3.21.5 |
|
| 55 | - */ |
|
| 56 | - public function __construct( $entity_uri_service, $entity_helper, $json, $post_id ) { |
|
| 57 | - $this->json = $json; |
|
| 58 | - $this->entity_uri_service = $entity_uri_service; |
|
| 59 | - $this->entity_helper = $entity_helper; |
|
| 60 | - $this->post_id = $post_id; |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Switches remote entities, i.e. entities with id outside the local dataset, to local entities. |
|
| 65 | - * |
|
| 66 | - * The function takes all the entities that have an id which is not local. For each remote entity, a list of URIs |
|
| 67 | - * is built comprising the entity id and the sameAs. Then a query is issued in the local database to find potential |
|
| 68 | - * matches from the local vocabulary. |
|
| 69 | - * |
|
| 70 | - * If found, the entity id is swapped with the local id and the remote id is added to the sameAs. |
|
| 71 | - * |
|
| 72 | - * @return Analysis_Response_Ops The current Analysis_Response_Ops instance. |
|
| 73 | - */ |
|
| 74 | - public function make_entities_local() { |
|
| 75 | - |
|
| 76 | - if ( ! isset( $this->json->entities ) ) { |
|
| 77 | - return $this; |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - // Get the URIs. |
|
| 81 | - $uris = array_keys( get_object_vars( $this->json->entities ) ); |
|
| 82 | - $mappings = $this->entity_helper->map_many_to_local( $uris ); |
|
| 83 | - |
|
| 84 | - foreach ( $mappings as $external_uri => $internal_uri ) { |
|
| 85 | - |
|
| 86 | - // Move the data from the external URI to the internal URI. |
|
| 87 | - if ( ! isset( $this->json->entities->{$internal_uri} ) ) { |
|
| 88 | - $this->json->entities->{$internal_uri} = $this->json->entities->{$external_uri}; |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - // Ensure sameAs is an array. |
|
| 92 | - if ( ! isset( $this->json->entities->{$internal_uri}->sameAs ) |
|
| 93 | - || ! is_array( $this->json->entities->{$internal_uri}->sameAs ) ) { |
|
| 94 | - $this->json->entities->{$internal_uri}->sameAs = array(); |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - // Add the external URI as sameAs. |
|
| 98 | - $this->json->entities->{$internal_uri}->sameAs[] = $external_uri; |
|
| 99 | - |
|
| 100 | - // Finally remove the external URI. |
|
| 101 | - unset( $this->json->entities->{$external_uri} ); |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - // Set the internal uri in the annotation for the entityMatch in annotations. |
|
| 105 | - if ( isset( $this->json->annotations ) ) { |
|
| 106 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 107 | - foreach ( $this->json->annotations as $key => $annotation ) { |
|
| 108 | - // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
|
| 109 | - if ( isset( $annotation->entityMatches ) ) { |
|
| 110 | - // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
|
| 111 | - foreach ( $annotation->entityMatches as $match ) { |
|
| 112 | - // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
|
| 113 | - if ( isset( $match->entityId ) && isset( $mappings[ $match->entityId ] ) ) { |
|
| 114 | - // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
|
| 115 | - $match->entityId = $mappings[ $match->entityId ]; |
|
| 116 | - } |
|
| 117 | - } |
|
| 118 | - } |
|
| 119 | - } |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - return $this; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * Add occurrences by parsing the provided html content. |
|
| 127 | - * |
|
| 128 | - * @param string $content The html content with annotations. |
|
| 129 | - * |
|
| 130 | - * @return Analysis_Response_Ops The {@link Analysis_Response_Ops} instance. |
|
| 131 | - * |
|
| 132 | - * @since 3.23.7 refactor the regex pattern to take into account that there might be css classes between textannotation |
|
| 133 | - * and disambiguated. |
|
| 134 | - * |
|
| 135 | - * @link https://github.com/insideout10/wordlift-plugin/issues/1001 |
|
| 136 | - */ |
|
| 137 | - public function add_occurrences( $content ) { |
|
| 138 | - |
|
| 139 | - // Try to get all the disambiguated annotations and bail out if an error occurs. |
|
| 140 | - if ( false === preg_match_all( |
|
| 141 | - '|<span\s+id="([^"]+)"\s+class="textannotation\s+(?:\S+\s+)?disambiguated(?=[\s"])[^"]*"\s+itemid="([^"]*)">(.*?)</span>|', |
|
| 142 | - $content, |
|
| 143 | - $matches, |
|
| 144 | - PREG_OFFSET_CAPTURE |
|
| 145 | - ) ) { |
|
| 146 | - return $this; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - if ( empty( $matches ) ) { |
|
| 150 | - return $this; |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - $parse_data = array_reduce( |
|
| 154 | - range( 0, count( $matches[1] ) - 1 ), |
|
| 155 | - function ( $carry, $i ) use ( $matches ) { |
|
| 156 | - if ( empty( $matches[0] ) ) { |
|
| 157 | - return $carry; |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - $start = $matches[0][ $i ][1]; |
|
| 161 | - $end = $start + strlen( $matches[0][ $i ][0] ); |
|
| 162 | - $annotation_id = $matches[1][ $i ][0]; |
|
| 163 | - $item_id = $matches[2][ $i ][0]; |
|
| 164 | - $text = $matches[3][ $i ][0]; |
|
| 165 | - |
|
| 166 | - $annotation = new StdClass(); |
|
| 167 | - // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
|
| 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 | - // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
|
| 176 | - $entity_match->entityId = $item_id; |
|
| 177 | - // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
|
| 178 | - $annotation->entityMatches[] = $entity_match; |
|
| 179 | - |
|
| 180 | - $carry['annotations'][ $annotation_id ] = $annotation; |
|
| 181 | - $carry['occurrences'][ $item_id ][] = $annotation_id; |
|
| 182 | - |
|
| 183 | - return $carry; |
|
| 184 | - }, |
|
| 185 | - array( |
|
| 186 | - 'annotations' => array(), |
|
| 187 | - 'occurrences' => array(), |
|
| 188 | - ) |
|
| 189 | - ); |
|
| 190 | - |
|
| 191 | - $annotations = $parse_data['annotations']; |
|
| 192 | - $occurrences = $parse_data['occurrences']; |
|
| 193 | - |
|
| 194 | - $entity_provider_registry = Entity_Provider_Registry::get_instance(); |
|
| 195 | - |
|
| 196 | - foreach ( array_keys( $occurrences ) as $item_id ) { |
|
| 197 | - |
|
| 198 | - // If the entity isn't there, add it. |
|
| 199 | - if ( ! is_bool( $this->json ) && ! isset( $this->json->entities->{$item_id} ) ) { |
|
| 200 | - $entity = $entity_provider_registry->get_local_entity( $item_id ); |
|
| 201 | - |
|
| 202 | - // Entity not found in the local vocabulary, continue to the next one. |
|
| 203 | - if ( false === $entity ) { |
|
| 204 | - continue; |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - $this->json->entities->{$item_id} = $entity; |
|
| 208 | - } |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - // Here we're adding back some data structures required by the client-side code. |
|
| 212 | - // |
|
| 213 | - // We're adding: |
|
| 214 | - // 1. the .entities[entity_id].occurrences array with the annotations' ids. |
|
| 215 | - // 2. the .entities[entity_id].annotations[annotation_id] = { id: annotation_id } map. |
|
| 216 | - // |
|
| 217 | - // Before 3.23.0 this was done by the client-side code located in src/coffee/editpost-widget/app.services.AnalysisService.coffee |
|
| 218 | - // function `preselect`, which was called by src/coffee/editpost-widget/app.services.EditorService.coffee in |
|
| 219 | - // `embedAnalysis`. |
|
| 220 | - |
|
| 221 | - if ( ! is_bool( $this->json ) && isset( $this->json->entities ) ) { |
|
| 222 | - $occurrences_processor = Occurrences_Factory::get_instance( $this->post_id ); |
|
| 223 | - $this->json = $occurrences_processor->add_occurrences_to_entities( $occurrences, $this->json, $this->post_id ); |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - // Add the missing annotations. This allows the analysis response to work also if we didn't receive results |
|
| 227 | - // from the analysis API. |
|
| 228 | - foreach ( $annotations as $annotation_id => $annotation ) { |
|
| 229 | - |
|
| 230 | - if ( ! is_bool( $this->json ) && ! isset( $this->json->annotations->{$annotation_id} ) ) { |
|
| 231 | - $this->json->annotations->{$annotation_id} = $annotation; |
|
| 232 | - } |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - return $this; |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - /** |
|
| 239 | - * Add local entities |
|
| 240 | - * |
|
| 241 | - * @return Analysis_Response_Ops The {@link Analysis_Response_Ops} instance. |
|
| 242 | - * |
|
| 243 | - * @since 3.27.6 |
|
| 244 | - * |
|
| 245 | - * @link https://github.com/insideout10/wordlift-plugin/issues/1178 |
|
| 246 | - */ |
|
| 247 | - public function add_local_entities() { |
|
| 248 | - |
|
| 249 | - // Populating the local entities object |
|
| 250 | - $entities = array(); |
|
| 251 | - foreach ( $this->json->annotations as $annotation ) { |
|
| 252 | - // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
|
| 253 | - foreach ( $annotation->entityMatches as $entity_matches ) { |
|
| 254 | - |
|
| 255 | - // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
|
| 256 | - $entity_id = $this->entity_uri_service->get_post_id_from_url( $entity_matches->entityId ); |
|
| 257 | - $serialized_entity = wl_serialize_entity( $entity_id ); |
|
| 258 | - |
|
| 259 | - if ( $serialized_entity ) { |
|
| 260 | - $serialized_entity['entityId'] = $serialized_entity['id']; |
|
| 261 | - unset( $serialized_entity['id'] ); |
|
| 262 | - |
|
| 263 | - // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
|
| 264 | - $entities[ $entity_matches->entityId ] = $serialized_entity; |
|
| 265 | - } |
|
| 266 | - } |
|
| 267 | - } |
|
| 268 | - |
|
| 269 | - // Adding occurrences and annotations data structures required by the client-side code. |
|
| 270 | - foreach ( $entities as $entity_id => $entity ) { |
|
| 271 | - foreach ( $this->json->annotations as $annotation ) { |
|
| 272 | - // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
|
| 273 | - if ( $annotation->entityMatches[0]->entityId === $entity_id ) { |
|
| 274 | - // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
|
| 275 | - $entities[ $entity_id ]['occurrences'][] = $annotation->annotationId; |
|
| 276 | - // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
|
| 277 | - $entities[ $entity_id ]['annotations'][ $annotation->annotationId ]['id'] = $annotation->annotationId; |
|
| 278 | - } |
|
| 279 | - } |
|
| 280 | - } |
|
| 281 | - |
|
| 282 | - $this->json->entities = $entities; |
|
| 283 | - |
|
| 284 | - return $this; |
|
| 285 | - |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - /** |
|
| 289 | - * Return the JSON response. |
|
| 290 | - * |
|
| 291 | - * @return mixed The JSON response. |
|
| 292 | - * @since 3.24.2 |
|
| 293 | - */ |
|
| 294 | - public function get_json() { |
|
| 295 | - |
|
| 296 | - return $this->json; |
|
| 297 | - } |
|
| 298 | - |
|
| 299 | - /** |
|
| 300 | - * This function should be invoked after `make_entities_local` after this |
|
| 301 | - * method. |
|
| 302 | - * |
|
| 303 | - * @param $excluded_uris array An array of entity URIs to be excluded. |
|
| 304 | - * |
|
| 305 | - * @return $this |
|
| 306 | - * @since 3.32.3.1 |
|
| 307 | - */ |
|
| 308 | - public function remove_excluded_entities( $excluded_uris ) { |
|
| 309 | - |
|
| 310 | - // If we didnt receive array, return early. |
|
| 311 | - if ( ! is_array( $excluded_uris ) ) { |
|
| 312 | - return $this; |
|
| 313 | - } |
|
| 314 | - |
|
| 315 | - // We may also receive an array of null, make sure to filter uris when receiving. |
|
| 316 | - $excluded_uris = array_filter( $excluded_uris, 'is_string' ); |
|
| 317 | - |
|
| 318 | - $this->remove_entities_with_excluded_uris( $excluded_uris ); |
|
| 319 | - |
|
| 320 | - $this->remove_annotations_with_excluded_uris( $excluded_uris ); |
|
| 321 | - |
|
| 322 | - return $this; |
|
| 323 | - } |
|
| 324 | - |
|
| 325 | - /** |
|
| 326 | - * Get the string representation of the JSON. |
|
| 327 | - * |
|
| 328 | - * @return false|string The string representation or false in case of error. |
|
| 329 | - */ |
|
| 330 | - public function to_string() { |
|
| 331 | - |
|
| 332 | - // Add the `JSON_UNESCAPED_UNICODE` only for PHP 5.4+. |
|
| 333 | - $options = ( version_compare( PHP_VERSION, '5.4', '>=' ) |
|
| 334 | - ? 256 : 0 ); |
|
| 335 | - |
|
| 336 | - return wp_json_encode( $this->json, $options ); |
|
| 337 | - } |
|
| 338 | - |
|
| 339 | - /** |
|
| 340 | - * Remove all the entities with the excluded URIs. |
|
| 341 | - * |
|
| 342 | - * @param array $excluded_uris The array of URIs to be excluded. |
|
| 343 | - */ |
|
| 344 | - private function remove_entities_with_excluded_uris( array $excluded_uris ) { |
|
| 345 | - // Remove the excluded entity uris. |
|
| 346 | - if ( isset( $this->json->entities ) ) { |
|
| 347 | - foreach ( $excluded_uris as $excluded_uri ) { |
|
| 348 | - |
|
| 349 | - if ( isset( $this->json->entities->{$excluded_uri} ) ) { |
|
| 350 | - // Remove this entity. |
|
| 351 | - unset( $this->json->entities->{$excluded_uri} ); |
|
| 352 | - // Also remove the annotations. |
|
| 353 | - } |
|
| 354 | - } |
|
| 355 | - } |
|
| 356 | - } |
|
| 357 | - |
|
| 358 | - /** |
|
| 359 | - * Remove all the annotations with the excluded entity URIs. |
|
| 360 | - * |
|
| 361 | - * @param array $excluded_uris The array of URIs to be excluded. |
|
| 362 | - * |
|
| 363 | - * @return void |
|
| 364 | - */ |
|
| 365 | - private function remove_annotations_with_excluded_uris( array $excluded_uris ) { |
|
| 366 | - if ( isset( $this->json->annotations ) ) { |
|
| 367 | - foreach ( $this->json->annotations as $annotation_key => &$annotation_data ) { |
|
| 368 | - |
|
| 369 | - // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
|
| 370 | - if ( ! isset( $annotation_data->entityMatches ) ) { |
|
| 371 | - continue; |
|
| 372 | - } |
|
| 373 | - |
|
| 374 | - // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
|
| 375 | - foreach ( $annotation_data->entityMatches as $entity_match_key => $entity_match_data ) { |
|
| 376 | - |
|
| 377 | - // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
|
| 378 | - $entity_uri = $entity_match_data->entityId; |
|
| 379 | - |
|
| 380 | - if ( ! in_array( $entity_uri, $excluded_uris, true ) ) { |
|
| 381 | - continue; |
|
| 382 | - } |
|
| 383 | - // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
|
| 384 | - unset( $annotation_data->entityMatches[ $entity_match_key ] ); |
|
| 385 | - } |
|
| 386 | - |
|
| 387 | - // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
|
| 388 | - if ( count( $annotation_data->entityMatches ) === 0 ) { |
|
| 389 | - // Remove the annotation if we have zero empty annotation matches. |
|
| 390 | - unset( $this->json->annotations->{$annotation_key} ); |
|
| 391 | - } |
|
| 392 | - } |
|
| 393 | - } |
|
| 394 | - |
|
| 395 | - } |
|
| 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 | + * @var Entity_Helper |
|
| 39 | + */ |
|
| 40 | + private $entity_helper; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * @var int $post_id |
|
| 44 | + */ |
|
| 45 | + private $post_id; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * Analysis_Response_Ops constructor. |
|
| 49 | + * |
|
| 50 | + * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service}. |
|
| 51 | + * @param Entity_Helper $entity_helper The {@link Entity_Helper}. |
|
| 52 | + * @param mixed $json The analysis response json. |
|
| 53 | + * |
|
| 54 | + * @since 3.21.5 |
|
| 55 | + */ |
|
| 56 | + public function __construct( $entity_uri_service, $entity_helper, $json, $post_id ) { |
|
| 57 | + $this->json = $json; |
|
| 58 | + $this->entity_uri_service = $entity_uri_service; |
|
| 59 | + $this->entity_helper = $entity_helper; |
|
| 60 | + $this->post_id = $post_id; |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Switches remote entities, i.e. entities with id outside the local dataset, to local entities. |
|
| 65 | + * |
|
| 66 | + * The function takes all the entities that have an id which is not local. For each remote entity, a list of URIs |
|
| 67 | + * is built comprising the entity id and the sameAs. Then a query is issued in the local database to find potential |
|
| 68 | + * matches from the local vocabulary. |
|
| 69 | + * |
|
| 70 | + * If found, the entity id is swapped with the local id and the remote id is added to the sameAs. |
|
| 71 | + * |
|
| 72 | + * @return Analysis_Response_Ops The current Analysis_Response_Ops instance. |
|
| 73 | + */ |
|
| 74 | + public function make_entities_local() { |
|
| 75 | + |
|
| 76 | + if ( ! isset( $this->json->entities ) ) { |
|
| 77 | + return $this; |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + // Get the URIs. |
|
| 81 | + $uris = array_keys( get_object_vars( $this->json->entities ) ); |
|
| 82 | + $mappings = $this->entity_helper->map_many_to_local( $uris ); |
|
| 83 | + |
|
| 84 | + foreach ( $mappings as $external_uri => $internal_uri ) { |
|
| 85 | + |
|
| 86 | + // Move the data from the external URI to the internal URI. |
|
| 87 | + if ( ! isset( $this->json->entities->{$internal_uri} ) ) { |
|
| 88 | + $this->json->entities->{$internal_uri} = $this->json->entities->{$external_uri}; |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + // Ensure sameAs is an array. |
|
| 92 | + if ( ! isset( $this->json->entities->{$internal_uri}->sameAs ) |
|
| 93 | + || ! is_array( $this->json->entities->{$internal_uri}->sameAs ) ) { |
|
| 94 | + $this->json->entities->{$internal_uri}->sameAs = array(); |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + // Add the external URI as sameAs. |
|
| 98 | + $this->json->entities->{$internal_uri}->sameAs[] = $external_uri; |
|
| 99 | + |
|
| 100 | + // Finally remove the external URI. |
|
| 101 | + unset( $this->json->entities->{$external_uri} ); |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + // Set the internal uri in the annotation for the entityMatch in annotations. |
|
| 105 | + if ( isset( $this->json->annotations ) ) { |
|
| 106 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 107 | + foreach ( $this->json->annotations as $key => $annotation ) { |
|
| 108 | + // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
|
| 109 | + if ( isset( $annotation->entityMatches ) ) { |
|
| 110 | + // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
|
| 111 | + foreach ( $annotation->entityMatches as $match ) { |
|
| 112 | + // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
|
| 113 | + if ( isset( $match->entityId ) && isset( $mappings[ $match->entityId ] ) ) { |
|
| 114 | + // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
|
| 115 | + $match->entityId = $mappings[ $match->entityId ]; |
|
| 116 | + } |
|
| 117 | + } |
|
| 118 | + } |
|
| 119 | + } |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + return $this; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * Add occurrences by parsing the provided html content. |
|
| 127 | + * |
|
| 128 | + * @param string $content The html content with annotations. |
|
| 129 | + * |
|
| 130 | + * @return Analysis_Response_Ops The {@link Analysis_Response_Ops} instance. |
|
| 131 | + * |
|
| 132 | + * @since 3.23.7 refactor the regex pattern to take into account that there might be css classes between textannotation |
|
| 133 | + * and disambiguated. |
|
| 134 | + * |
|
| 135 | + * @link https://github.com/insideout10/wordlift-plugin/issues/1001 |
|
| 136 | + */ |
|
| 137 | + public function add_occurrences( $content ) { |
|
| 138 | + |
|
| 139 | + // Try to get all the disambiguated annotations and bail out if an error occurs. |
|
| 140 | + if ( false === preg_match_all( |
|
| 141 | + '|<span\s+id="([^"]+)"\s+class="textannotation\s+(?:\S+\s+)?disambiguated(?=[\s"])[^"]*"\s+itemid="([^"]*)">(.*?)</span>|', |
|
| 142 | + $content, |
|
| 143 | + $matches, |
|
| 144 | + PREG_OFFSET_CAPTURE |
|
| 145 | + ) ) { |
|
| 146 | + return $this; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + if ( empty( $matches ) ) { |
|
| 150 | + return $this; |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + $parse_data = array_reduce( |
|
| 154 | + range( 0, count( $matches[1] ) - 1 ), |
|
| 155 | + function ( $carry, $i ) use ( $matches ) { |
|
| 156 | + if ( empty( $matches[0] ) ) { |
|
| 157 | + return $carry; |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + $start = $matches[0][ $i ][1]; |
|
| 161 | + $end = $start + strlen( $matches[0][ $i ][0] ); |
|
| 162 | + $annotation_id = $matches[1][ $i ][0]; |
|
| 163 | + $item_id = $matches[2][ $i ][0]; |
|
| 164 | + $text = $matches[3][ $i ][0]; |
|
| 165 | + |
|
| 166 | + $annotation = new StdClass(); |
|
| 167 | + // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
|
| 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 | + // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
|
| 176 | + $entity_match->entityId = $item_id; |
|
| 177 | + // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
|
| 178 | + $annotation->entityMatches[] = $entity_match; |
|
| 179 | + |
|
| 180 | + $carry['annotations'][ $annotation_id ] = $annotation; |
|
| 181 | + $carry['occurrences'][ $item_id ][] = $annotation_id; |
|
| 182 | + |
|
| 183 | + return $carry; |
|
| 184 | + }, |
|
| 185 | + array( |
|
| 186 | + 'annotations' => array(), |
|
| 187 | + 'occurrences' => array(), |
|
| 188 | + ) |
|
| 189 | + ); |
|
| 190 | + |
|
| 191 | + $annotations = $parse_data['annotations']; |
|
| 192 | + $occurrences = $parse_data['occurrences']; |
|
| 193 | + |
|
| 194 | + $entity_provider_registry = Entity_Provider_Registry::get_instance(); |
|
| 195 | + |
|
| 196 | + foreach ( array_keys( $occurrences ) as $item_id ) { |
|
| 197 | + |
|
| 198 | + // If the entity isn't there, add it. |
|
| 199 | + if ( ! is_bool( $this->json ) && ! isset( $this->json->entities->{$item_id} ) ) { |
|
| 200 | + $entity = $entity_provider_registry->get_local_entity( $item_id ); |
|
| 201 | + |
|
| 202 | + // Entity not found in the local vocabulary, continue to the next one. |
|
| 203 | + if ( false === $entity ) { |
|
| 204 | + continue; |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + $this->json->entities->{$item_id} = $entity; |
|
| 208 | + } |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + // Here we're adding back some data structures required by the client-side code. |
|
| 212 | + // |
|
| 213 | + // We're adding: |
|
| 214 | + // 1. the .entities[entity_id].occurrences array with the annotations' ids. |
|
| 215 | + // 2. the .entities[entity_id].annotations[annotation_id] = { id: annotation_id } map. |
|
| 216 | + // |
|
| 217 | + // Before 3.23.0 this was done by the client-side code located in src/coffee/editpost-widget/app.services.AnalysisService.coffee |
|
| 218 | + // function `preselect`, which was called by src/coffee/editpost-widget/app.services.EditorService.coffee in |
|
| 219 | + // `embedAnalysis`. |
|
| 220 | + |
|
| 221 | + if ( ! is_bool( $this->json ) && isset( $this->json->entities ) ) { |
|
| 222 | + $occurrences_processor = Occurrences_Factory::get_instance( $this->post_id ); |
|
| 223 | + $this->json = $occurrences_processor->add_occurrences_to_entities( $occurrences, $this->json, $this->post_id ); |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + // Add the missing annotations. This allows the analysis response to work also if we didn't receive results |
|
| 227 | + // from the analysis API. |
|
| 228 | + foreach ( $annotations as $annotation_id => $annotation ) { |
|
| 229 | + |
|
| 230 | + if ( ! is_bool( $this->json ) && ! isset( $this->json->annotations->{$annotation_id} ) ) { |
|
| 231 | + $this->json->annotations->{$annotation_id} = $annotation; |
|
| 232 | + } |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + return $this; |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + /** |
|
| 239 | + * Add local entities |
|
| 240 | + * |
|
| 241 | + * @return Analysis_Response_Ops The {@link Analysis_Response_Ops} instance. |
|
| 242 | + * |
|
| 243 | + * @since 3.27.6 |
|
| 244 | + * |
|
| 245 | + * @link https://github.com/insideout10/wordlift-plugin/issues/1178 |
|
| 246 | + */ |
|
| 247 | + public function add_local_entities() { |
|
| 248 | + |
|
| 249 | + // Populating the local entities object |
|
| 250 | + $entities = array(); |
|
| 251 | + foreach ( $this->json->annotations as $annotation ) { |
|
| 252 | + // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
|
| 253 | + foreach ( $annotation->entityMatches as $entity_matches ) { |
|
| 254 | + |
|
| 255 | + // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
|
| 256 | + $entity_id = $this->entity_uri_service->get_post_id_from_url( $entity_matches->entityId ); |
|
| 257 | + $serialized_entity = wl_serialize_entity( $entity_id ); |
|
| 258 | + |
|
| 259 | + if ( $serialized_entity ) { |
|
| 260 | + $serialized_entity['entityId'] = $serialized_entity['id']; |
|
| 261 | + unset( $serialized_entity['id'] ); |
|
| 262 | + |
|
| 263 | + // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
|
| 264 | + $entities[ $entity_matches->entityId ] = $serialized_entity; |
|
| 265 | + } |
|
| 266 | + } |
|
| 267 | + } |
|
| 268 | + |
|
| 269 | + // Adding occurrences and annotations data structures required by the client-side code. |
|
| 270 | + foreach ( $entities as $entity_id => $entity ) { |
|
| 271 | + foreach ( $this->json->annotations as $annotation ) { |
|
| 272 | + // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
|
| 273 | + if ( $annotation->entityMatches[0]->entityId === $entity_id ) { |
|
| 274 | + // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
|
| 275 | + $entities[ $entity_id ]['occurrences'][] = $annotation->annotationId; |
|
| 276 | + // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
|
| 277 | + $entities[ $entity_id ]['annotations'][ $annotation->annotationId ]['id'] = $annotation->annotationId; |
|
| 278 | + } |
|
| 279 | + } |
|
| 280 | + } |
|
| 281 | + |
|
| 282 | + $this->json->entities = $entities; |
|
| 283 | + |
|
| 284 | + return $this; |
|
| 285 | + |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + /** |
|
| 289 | + * Return the JSON response. |
|
| 290 | + * |
|
| 291 | + * @return mixed The JSON response. |
|
| 292 | + * @since 3.24.2 |
|
| 293 | + */ |
|
| 294 | + public function get_json() { |
|
| 295 | + |
|
| 296 | + return $this->json; |
|
| 297 | + } |
|
| 298 | + |
|
| 299 | + /** |
|
| 300 | + * This function should be invoked after `make_entities_local` after this |
|
| 301 | + * method. |
|
| 302 | + * |
|
| 303 | + * @param $excluded_uris array An array of entity URIs to be excluded. |
|
| 304 | + * |
|
| 305 | + * @return $this |
|
| 306 | + * @since 3.32.3.1 |
|
| 307 | + */ |
|
| 308 | + public function remove_excluded_entities( $excluded_uris ) { |
|
| 309 | + |
|
| 310 | + // If we didnt receive array, return early. |
|
| 311 | + if ( ! is_array( $excluded_uris ) ) { |
|
| 312 | + return $this; |
|
| 313 | + } |
|
| 314 | + |
|
| 315 | + // We may also receive an array of null, make sure to filter uris when receiving. |
|
| 316 | + $excluded_uris = array_filter( $excluded_uris, 'is_string' ); |
|
| 317 | + |
|
| 318 | + $this->remove_entities_with_excluded_uris( $excluded_uris ); |
|
| 319 | + |
|
| 320 | + $this->remove_annotations_with_excluded_uris( $excluded_uris ); |
|
| 321 | + |
|
| 322 | + return $this; |
|
| 323 | + } |
|
| 324 | + |
|
| 325 | + /** |
|
| 326 | + * Get the string representation of the JSON. |
|
| 327 | + * |
|
| 328 | + * @return false|string The string representation or false in case of error. |
|
| 329 | + */ |
|
| 330 | + public function to_string() { |
|
| 331 | + |
|
| 332 | + // Add the `JSON_UNESCAPED_UNICODE` only for PHP 5.4+. |
|
| 333 | + $options = ( version_compare( PHP_VERSION, '5.4', '>=' ) |
|
| 334 | + ? 256 : 0 ); |
|
| 335 | + |
|
| 336 | + return wp_json_encode( $this->json, $options ); |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + /** |
|
| 340 | + * Remove all the entities with the excluded URIs. |
|
| 341 | + * |
|
| 342 | + * @param array $excluded_uris The array of URIs to be excluded. |
|
| 343 | + */ |
|
| 344 | + private function remove_entities_with_excluded_uris( array $excluded_uris ) { |
|
| 345 | + // Remove the excluded entity uris. |
|
| 346 | + if ( isset( $this->json->entities ) ) { |
|
| 347 | + foreach ( $excluded_uris as $excluded_uri ) { |
|
| 348 | + |
|
| 349 | + if ( isset( $this->json->entities->{$excluded_uri} ) ) { |
|
| 350 | + // Remove this entity. |
|
| 351 | + unset( $this->json->entities->{$excluded_uri} ); |
|
| 352 | + // Also remove the annotations. |
|
| 353 | + } |
|
| 354 | + } |
|
| 355 | + } |
|
| 356 | + } |
|
| 357 | + |
|
| 358 | + /** |
|
| 359 | + * Remove all the annotations with the excluded entity URIs. |
|
| 360 | + * |
|
| 361 | + * @param array $excluded_uris The array of URIs to be excluded. |
|
| 362 | + * |
|
| 363 | + * @return void |
|
| 364 | + */ |
|
| 365 | + private function remove_annotations_with_excluded_uris( array $excluded_uris ) { |
|
| 366 | + if ( isset( $this->json->annotations ) ) { |
|
| 367 | + foreach ( $this->json->annotations as $annotation_key => &$annotation_data ) { |
|
| 368 | + |
|
| 369 | + // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
|
| 370 | + if ( ! isset( $annotation_data->entityMatches ) ) { |
|
| 371 | + continue; |
|
| 372 | + } |
|
| 373 | + |
|
| 374 | + // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
|
| 375 | + foreach ( $annotation_data->entityMatches as $entity_match_key => $entity_match_data ) { |
|
| 376 | + |
|
| 377 | + // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
|
| 378 | + $entity_uri = $entity_match_data->entityId; |
|
| 379 | + |
|
| 380 | + if ( ! in_array( $entity_uri, $excluded_uris, true ) ) { |
|
| 381 | + continue; |
|
| 382 | + } |
|
| 383 | + // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
|
| 384 | + unset( $annotation_data->entityMatches[ $entity_match_key ] ); |
|
| 385 | + } |
|
| 386 | + |
|
| 387 | + // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
|
| 388 | + if ( count( $annotation_data->entityMatches ) === 0 ) { |
|
| 389 | + // Remove the annotation if we have zero empty annotation matches. |
|
| 390 | + unset( $this->json->annotations->{$annotation_key} ); |
|
| 391 | + } |
|
| 392 | + } |
|
| 393 | + } |
|
| 394 | + |
|
| 395 | + } |
|
| 396 | 396 | |
| 397 | 397 | } |
@@ -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, $json, $post_id ) { |
|
| 56 | + public function __construct($entity_uri_service, $entity_helper, $json, $post_id) { |
|
| 57 | 57 | $this->json = $json; |
| 58 | 58 | $this->entity_uri_service = $entity_uri_service; |
| 59 | 59 | $this->entity_helper = $entity_helper; |
@@ -73,24 +73,24 @@ discard block |
||
| 73 | 73 | */ |
| 74 | 74 | public function make_entities_local() { |
| 75 | 75 | |
| 76 | - if ( ! isset( $this->json->entities ) ) { |
|
| 76 | + if ( ! isset($this->json->entities)) { |
|
| 77 | 77 | return $this; |
| 78 | 78 | } |
| 79 | 79 | |
| 80 | 80 | // Get the URIs. |
| 81 | - $uris = array_keys( get_object_vars( $this->json->entities ) ); |
|
| 82 | - $mappings = $this->entity_helper->map_many_to_local( $uris ); |
|
| 81 | + $uris = array_keys(get_object_vars($this->json->entities)); |
|
| 82 | + $mappings = $this->entity_helper->map_many_to_local($uris); |
|
| 83 | 83 | |
| 84 | - foreach ( $mappings as $external_uri => $internal_uri ) { |
|
| 84 | + foreach ($mappings as $external_uri => $internal_uri) { |
|
| 85 | 85 | |
| 86 | 86 | // Move the data from the external URI to the internal URI. |
| 87 | - if ( ! isset( $this->json->entities->{$internal_uri} ) ) { |
|
| 87 | + if ( ! isset($this->json->entities->{$internal_uri} )) { |
|
| 88 | 88 | $this->json->entities->{$internal_uri} = $this->json->entities->{$external_uri}; |
| 89 | 89 | } |
| 90 | 90 | |
| 91 | 91 | // Ensure sameAs is an array. |
| 92 | - if ( ! isset( $this->json->entities->{$internal_uri}->sameAs ) |
|
| 93 | - || ! is_array( $this->json->entities->{$internal_uri}->sameAs ) ) { |
|
| 92 | + if ( ! isset($this->json->entities->{$internal_uri}->sameAs) |
|
| 93 | + || ! is_array($this->json->entities->{$internal_uri}->sameAs)) { |
|
| 94 | 94 | $this->json->entities->{$internal_uri}->sameAs = array(); |
| 95 | 95 | } |
| 96 | 96 | |
@@ -98,21 +98,21 @@ discard block |
||
| 98 | 98 | $this->json->entities->{$internal_uri}->sameAs[] = $external_uri; |
| 99 | 99 | |
| 100 | 100 | // Finally remove the external URI. |
| 101 | - unset( $this->json->entities->{$external_uri} ); |
|
| 101 | + unset($this->json->entities->{$external_uri} ); |
|
| 102 | 102 | } |
| 103 | 103 | |
| 104 | 104 | // Set the internal uri in the annotation for the entityMatch in annotations. |
| 105 | - if ( isset( $this->json->annotations ) ) { |
|
| 105 | + if (isset($this->json->annotations)) { |
|
| 106 | 106 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 107 | - foreach ( $this->json->annotations as $key => $annotation ) { |
|
| 107 | + foreach ($this->json->annotations as $key => $annotation) { |
|
| 108 | 108 | // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 109 | - if ( isset( $annotation->entityMatches ) ) { |
|
| 109 | + if (isset($annotation->entityMatches)) { |
|
| 110 | 110 | // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 111 | - foreach ( $annotation->entityMatches as $match ) { |
|
| 111 | + foreach ($annotation->entityMatches as $match) { |
|
| 112 | 112 | // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 113 | - if ( isset( $match->entityId ) && isset( $mappings[ $match->entityId ] ) ) { |
|
| 113 | + if (isset($match->entityId) && isset($mappings[$match->entityId])) { |
|
| 114 | 114 | // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 115 | - $match->entityId = $mappings[ $match->entityId ]; |
|
| 115 | + $match->entityId = $mappings[$match->entityId]; |
|
| 116 | 116 | } |
| 117 | 117 | } |
| 118 | 118 | } |
@@ -134,34 +134,34 @@ discard block |
||
| 134 | 134 | * |
| 135 | 135 | * @link https://github.com/insideout10/wordlift-plugin/issues/1001 |
| 136 | 136 | */ |
| 137 | - public function add_occurrences( $content ) { |
|
| 137 | + public function add_occurrences($content) { |
|
| 138 | 138 | |
| 139 | 139 | // Try to get all the disambiguated annotations and bail out if an error occurs. |
| 140 | - if ( false === preg_match_all( |
|
| 140 | + if (false === preg_match_all( |
|
| 141 | 141 | '|<span\s+id="([^"]+)"\s+class="textannotation\s+(?:\S+\s+)?disambiguated(?=[\s"])[^"]*"\s+itemid="([^"]*)">(.*?)</span>|', |
| 142 | 142 | $content, |
| 143 | 143 | $matches, |
| 144 | 144 | PREG_OFFSET_CAPTURE |
| 145 | - ) ) { |
|
| 145 | + )) { |
|
| 146 | 146 | return $this; |
| 147 | 147 | } |
| 148 | 148 | |
| 149 | - if ( empty( $matches ) ) { |
|
| 149 | + if (empty($matches)) { |
|
| 150 | 150 | return $this; |
| 151 | 151 | } |
| 152 | 152 | |
| 153 | 153 | $parse_data = array_reduce( |
| 154 | - range( 0, count( $matches[1] ) - 1 ), |
|
| 155 | - function ( $carry, $i ) use ( $matches ) { |
|
| 156 | - if ( empty( $matches[0] ) ) { |
|
| 154 | + range(0, count($matches[1]) - 1), |
|
| 155 | + function($carry, $i) use ($matches) { |
|
| 156 | + if (empty($matches[0])) { |
|
| 157 | 157 | return $carry; |
| 158 | 158 | } |
| 159 | 159 | |
| 160 | - $start = $matches[0][ $i ][1]; |
|
| 161 | - $end = $start + strlen( $matches[0][ $i ][0] ); |
|
| 162 | - $annotation_id = $matches[1][ $i ][0]; |
|
| 163 | - $item_id = $matches[2][ $i ][0]; |
|
| 164 | - $text = $matches[3][ $i ][0]; |
|
| 160 | + $start = $matches[0][$i][1]; |
|
| 161 | + $end = $start + strlen($matches[0][$i][0]); |
|
| 162 | + $annotation_id = $matches[1][$i][0]; |
|
| 163 | + $item_id = $matches[2][$i][0]; |
|
| 164 | + $text = $matches[3][$i][0]; |
|
| 165 | 165 | |
| 166 | 166 | $annotation = new StdClass(); |
| 167 | 167 | // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
@@ -177,8 +177,8 @@ discard block |
||
| 177 | 177 | // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 178 | 178 | $annotation->entityMatches[] = $entity_match; |
| 179 | 179 | |
| 180 | - $carry['annotations'][ $annotation_id ] = $annotation; |
|
| 181 | - $carry['occurrences'][ $item_id ][] = $annotation_id; |
|
| 180 | + $carry['annotations'][$annotation_id] = $annotation; |
|
| 181 | + $carry['occurrences'][$item_id][] = $annotation_id; |
|
| 182 | 182 | |
| 183 | 183 | return $carry; |
| 184 | 184 | }, |
@@ -193,14 +193,14 @@ discard block |
||
| 193 | 193 | |
| 194 | 194 | $entity_provider_registry = Entity_Provider_Registry::get_instance(); |
| 195 | 195 | |
| 196 | - foreach ( array_keys( $occurrences ) as $item_id ) { |
|
| 196 | + foreach (array_keys($occurrences) as $item_id) { |
|
| 197 | 197 | |
| 198 | 198 | // If the entity isn't there, add it. |
| 199 | - if ( ! is_bool( $this->json ) && ! isset( $this->json->entities->{$item_id} ) ) { |
|
| 200 | - $entity = $entity_provider_registry->get_local_entity( $item_id ); |
|
| 199 | + if ( ! is_bool($this->json) && ! isset($this->json->entities->{$item_id} )) { |
|
| 200 | + $entity = $entity_provider_registry->get_local_entity($item_id); |
|
| 201 | 201 | |
| 202 | 202 | // Entity not found in the local vocabulary, continue to the next one. |
| 203 | - if ( false === $entity ) { |
|
| 203 | + if (false === $entity) { |
|
| 204 | 204 | continue; |
| 205 | 205 | } |
| 206 | 206 | |
@@ -218,16 +218,16 @@ discard block |
||
| 218 | 218 | // function `preselect`, which was called by src/coffee/editpost-widget/app.services.EditorService.coffee in |
| 219 | 219 | // `embedAnalysis`. |
| 220 | 220 | |
| 221 | - if ( ! is_bool( $this->json ) && isset( $this->json->entities ) ) { |
|
| 222 | - $occurrences_processor = Occurrences_Factory::get_instance( $this->post_id ); |
|
| 223 | - $this->json = $occurrences_processor->add_occurrences_to_entities( $occurrences, $this->json, $this->post_id ); |
|
| 221 | + if ( ! is_bool($this->json) && isset($this->json->entities)) { |
|
| 222 | + $occurrences_processor = Occurrences_Factory::get_instance($this->post_id); |
|
| 223 | + $this->json = $occurrences_processor->add_occurrences_to_entities($occurrences, $this->json, $this->post_id); |
|
| 224 | 224 | } |
| 225 | 225 | |
| 226 | 226 | // Add the missing annotations. This allows the analysis response to work also if we didn't receive results |
| 227 | 227 | // from the analysis API. |
| 228 | - foreach ( $annotations as $annotation_id => $annotation ) { |
|
| 228 | + foreach ($annotations as $annotation_id => $annotation) { |
|
| 229 | 229 | |
| 230 | - if ( ! is_bool( $this->json ) && ! isset( $this->json->annotations->{$annotation_id} ) ) { |
|
| 230 | + if ( ! is_bool($this->json) && ! isset($this->json->annotations->{$annotation_id} )) { |
|
| 231 | 231 | $this->json->annotations->{$annotation_id} = $annotation; |
| 232 | 232 | } |
| 233 | 233 | } |
@@ -248,33 +248,33 @@ discard block |
||
| 248 | 248 | |
| 249 | 249 | // Populating the local entities object |
| 250 | 250 | $entities = array(); |
| 251 | - foreach ( $this->json->annotations as $annotation ) { |
|
| 251 | + foreach ($this->json->annotations as $annotation) { |
|
| 252 | 252 | // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 253 | - foreach ( $annotation->entityMatches as $entity_matches ) { |
|
| 253 | + foreach ($annotation->entityMatches as $entity_matches) { |
|
| 254 | 254 | |
| 255 | 255 | // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 256 | - $entity_id = $this->entity_uri_service->get_post_id_from_url( $entity_matches->entityId ); |
|
| 257 | - $serialized_entity = wl_serialize_entity( $entity_id ); |
|
| 256 | + $entity_id = $this->entity_uri_service->get_post_id_from_url($entity_matches->entityId); |
|
| 257 | + $serialized_entity = wl_serialize_entity($entity_id); |
|
| 258 | 258 | |
| 259 | - if ( $serialized_entity ) { |
|
| 259 | + if ($serialized_entity) { |
|
| 260 | 260 | $serialized_entity['entityId'] = $serialized_entity['id']; |
| 261 | - unset( $serialized_entity['id'] ); |
|
| 261 | + unset($serialized_entity['id']); |
|
| 262 | 262 | |
| 263 | 263 | // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 264 | - $entities[ $entity_matches->entityId ] = $serialized_entity; |
|
| 264 | + $entities[$entity_matches->entityId] = $serialized_entity; |
|
| 265 | 265 | } |
| 266 | 266 | } |
| 267 | 267 | } |
| 268 | 268 | |
| 269 | 269 | // Adding occurrences and annotations data structures required by the client-side code. |
| 270 | - foreach ( $entities as $entity_id => $entity ) { |
|
| 271 | - foreach ( $this->json->annotations as $annotation ) { |
|
| 270 | + foreach ($entities as $entity_id => $entity) { |
|
| 271 | + foreach ($this->json->annotations as $annotation) { |
|
| 272 | 272 | // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 273 | - if ( $annotation->entityMatches[0]->entityId === $entity_id ) { |
|
| 273 | + if ($annotation->entityMatches[0]->entityId === $entity_id) { |
|
| 274 | 274 | // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 275 | - $entities[ $entity_id ]['occurrences'][] = $annotation->annotationId; |
|
| 275 | + $entities[$entity_id]['occurrences'][] = $annotation->annotationId; |
|
| 276 | 276 | // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 277 | - $entities[ $entity_id ]['annotations'][ $annotation->annotationId ]['id'] = $annotation->annotationId; |
|
| 277 | + $entities[$entity_id]['annotations'][$annotation->annotationId]['id'] = $annotation->annotationId; |
|
| 278 | 278 | } |
| 279 | 279 | } |
| 280 | 280 | } |
@@ -305,19 +305,19 @@ discard block |
||
| 305 | 305 | * @return $this |
| 306 | 306 | * @since 3.32.3.1 |
| 307 | 307 | */ |
| 308 | - public function remove_excluded_entities( $excluded_uris ) { |
|
| 308 | + public function remove_excluded_entities($excluded_uris) { |
|
| 309 | 309 | |
| 310 | 310 | // If we didnt receive array, return early. |
| 311 | - if ( ! is_array( $excluded_uris ) ) { |
|
| 311 | + if ( ! is_array($excluded_uris)) { |
|
| 312 | 312 | return $this; |
| 313 | 313 | } |
| 314 | 314 | |
| 315 | 315 | // We may also receive an array of null, make sure to filter uris when receiving. |
| 316 | - $excluded_uris = array_filter( $excluded_uris, 'is_string' ); |
|
| 316 | + $excluded_uris = array_filter($excluded_uris, 'is_string'); |
|
| 317 | 317 | |
| 318 | - $this->remove_entities_with_excluded_uris( $excluded_uris ); |
|
| 318 | + $this->remove_entities_with_excluded_uris($excluded_uris); |
|
| 319 | 319 | |
| 320 | - $this->remove_annotations_with_excluded_uris( $excluded_uris ); |
|
| 320 | + $this->remove_annotations_with_excluded_uris($excluded_uris); |
|
| 321 | 321 | |
| 322 | 322 | return $this; |
| 323 | 323 | } |
@@ -330,10 +330,10 @@ discard block |
||
| 330 | 330 | public function to_string() { |
| 331 | 331 | |
| 332 | 332 | // Add the `JSON_UNESCAPED_UNICODE` only for PHP 5.4+. |
| 333 | - $options = ( version_compare( PHP_VERSION, '5.4', '>=' ) |
|
| 334 | - ? 256 : 0 ); |
|
| 333 | + $options = (version_compare(PHP_VERSION, '5.4', '>=') |
|
| 334 | + ? 256 : 0); |
|
| 335 | 335 | |
| 336 | - return wp_json_encode( $this->json, $options ); |
|
| 336 | + return wp_json_encode($this->json, $options); |
|
| 337 | 337 | } |
| 338 | 338 | |
| 339 | 339 | /** |
@@ -341,14 +341,14 @@ discard block |
||
| 341 | 341 | * |
| 342 | 342 | * @param array $excluded_uris The array of URIs to be excluded. |
| 343 | 343 | */ |
| 344 | - private function remove_entities_with_excluded_uris( array $excluded_uris ) { |
|
| 344 | + private function remove_entities_with_excluded_uris(array $excluded_uris) { |
|
| 345 | 345 | // Remove the excluded entity uris. |
| 346 | - if ( isset( $this->json->entities ) ) { |
|
| 347 | - foreach ( $excluded_uris as $excluded_uri ) { |
|
| 346 | + if (isset($this->json->entities)) { |
|
| 347 | + foreach ($excluded_uris as $excluded_uri) { |
|
| 348 | 348 | |
| 349 | - if ( isset( $this->json->entities->{$excluded_uri} ) ) { |
|
| 349 | + if (isset($this->json->entities->{$excluded_uri} )) { |
|
| 350 | 350 | // Remove this entity. |
| 351 | - unset( $this->json->entities->{$excluded_uri} ); |
|
| 351 | + unset($this->json->entities->{$excluded_uri} ); |
|
| 352 | 352 | // Also remove the annotations. |
| 353 | 353 | } |
| 354 | 354 | } |
@@ -362,32 +362,32 @@ discard block |
||
| 362 | 362 | * |
| 363 | 363 | * @return void |
| 364 | 364 | */ |
| 365 | - private function remove_annotations_with_excluded_uris( array $excluded_uris ) { |
|
| 366 | - if ( isset( $this->json->annotations ) ) { |
|
| 367 | - foreach ( $this->json->annotations as $annotation_key => &$annotation_data ) { |
|
| 365 | + private function remove_annotations_with_excluded_uris(array $excluded_uris) { |
|
| 366 | + if (isset($this->json->annotations)) { |
|
| 367 | + foreach ($this->json->annotations as $annotation_key => &$annotation_data) { |
|
| 368 | 368 | |
| 369 | 369 | // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 370 | - if ( ! isset( $annotation_data->entityMatches ) ) { |
|
| 370 | + if ( ! isset($annotation_data->entityMatches)) { |
|
| 371 | 371 | continue; |
| 372 | 372 | } |
| 373 | 373 | |
| 374 | 374 | // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 375 | - foreach ( $annotation_data->entityMatches as $entity_match_key => $entity_match_data ) { |
|
| 375 | + foreach ($annotation_data->entityMatches as $entity_match_key => $entity_match_data) { |
|
| 376 | 376 | |
| 377 | 377 | // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 378 | 378 | $entity_uri = $entity_match_data->entityId; |
| 379 | 379 | |
| 380 | - if ( ! in_array( $entity_uri, $excluded_uris, true ) ) { |
|
| 380 | + if ( ! in_array($entity_uri, $excluded_uris, true)) { |
|
| 381 | 381 | continue; |
| 382 | 382 | } |
| 383 | 383 | // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 384 | - unset( $annotation_data->entityMatches[ $entity_match_key ] ); |
|
| 384 | + unset($annotation_data->entityMatches[$entity_match_key]); |
|
| 385 | 385 | } |
| 386 | 386 | |
| 387 | 387 | // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 388 | - if ( count( $annotation_data->entityMatches ) === 0 ) { |
|
| 388 | + if (count($annotation_data->entityMatches) === 0) { |
|
| 389 | 389 | // Remove the annotation if we have zero empty annotation matches. |
| 390 | - unset( $this->json->annotations->{$annotation_key} ); |
|
| 390 | + unset($this->json->annotations->{$annotation_key} ); |
|
| 391 | 391 | } |
| 392 | 392 | } |
| 393 | 393 | } |
@@ -14,61 +14,61 @@ |
||
| 14 | 14 | // phpcs:ignore WordPress.WP.CapitalPDangit.MisspelledClassName |
| 15 | 15 | class Wordpress_Content_Id implements Content_Id, JsonSerializable { |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * @var int $id The post/page/term/user ID. |
|
| 19 | - */ |
|
| 20 | - private $id; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * @var Object_Type_Enum $type The content type, post/page/term/user. |
|
| 24 | - */ |
|
| 25 | - private $type; |
|
| 26 | - |
|
| 27 | - public static function create_post( $id ) { |
|
| 28 | - return new self( $id, Object_Type_Enum::POST ); |
|
| 29 | - } |
|
| 30 | - |
|
| 31 | - public static function create_term( $id ) { |
|
| 32 | - return new self( $id, Object_Type_Enum::TERM ); |
|
| 33 | - } |
|
| 34 | - |
|
| 35 | - public static function create_user( $id ) { |
|
| 36 | - return new self( $id, Object_Type_Enum::USER ); |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * @param int $id The post/page/term/user ID. |
|
| 41 | - * @param int $type The content type, post/page/term/user. |
|
| 42 | - */ |
|
| 43 | - public function __construct( $id, $type ) { |
|
| 44 | - Assertions::is_numeric( $id ); |
|
| 45 | - Assertions::is_numeric( $type ); |
|
| 46 | - |
|
| 47 | - $this->id = (int) $id; |
|
| 48 | - $this->type = (int) $type; |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - public static function from_json( $json ) { |
|
| 52 | - return new self( $json['id'], $json['type'] ); |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - public function get_id() { |
|
| 56 | - return $this->id; |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - public function get_type() { |
|
| 60 | - return $this->type; |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - public function jsonSerialize() { |
|
| 64 | - return array( |
|
| 65 | - 'id' => $this->id, |
|
| 66 | - 'type' => $this->type, |
|
| 67 | - ); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - public function __toString() { |
|
| 71 | - return sprintf( '%d_%d', $this->type, $this->id ); |
|
| 72 | - } |
|
| 17 | + /** |
|
| 18 | + * @var int $id The post/page/term/user ID. |
|
| 19 | + */ |
|
| 20 | + private $id; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * @var Object_Type_Enum $type The content type, post/page/term/user. |
|
| 24 | + */ |
|
| 25 | + private $type; |
|
| 26 | + |
|
| 27 | + public static function create_post( $id ) { |
|
| 28 | + return new self( $id, Object_Type_Enum::POST ); |
|
| 29 | + } |
|
| 30 | + |
|
| 31 | + public static function create_term( $id ) { |
|
| 32 | + return new self( $id, Object_Type_Enum::TERM ); |
|
| 33 | + } |
|
| 34 | + |
|
| 35 | + public static function create_user( $id ) { |
|
| 36 | + return new self( $id, Object_Type_Enum::USER ); |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * @param int $id The post/page/term/user ID. |
|
| 41 | + * @param int $type The content type, post/page/term/user. |
|
| 42 | + */ |
|
| 43 | + public function __construct( $id, $type ) { |
|
| 44 | + Assertions::is_numeric( $id ); |
|
| 45 | + Assertions::is_numeric( $type ); |
|
| 46 | + |
|
| 47 | + $this->id = (int) $id; |
|
| 48 | + $this->type = (int) $type; |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + public static function from_json( $json ) { |
|
| 52 | + return new self( $json['id'], $json['type'] ); |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + public function get_id() { |
|
| 56 | + return $this->id; |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + public function get_type() { |
|
| 60 | + return $this->type; |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + public function jsonSerialize() { |
|
| 64 | + return array( |
|
| 65 | + 'id' => $this->id, |
|
| 66 | + 'type' => $this->type, |
|
| 67 | + ); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + public function __toString() { |
|
| 71 | + return sprintf( '%d_%d', $this->type, $this->id ); |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | 74 | } |
@@ -24,32 +24,32 @@ discard block |
||
| 24 | 24 | */ |
| 25 | 25 | private $type; |
| 26 | 26 | |
| 27 | - public static function create_post( $id ) { |
|
| 28 | - return new self( $id, Object_Type_Enum::POST ); |
|
| 27 | + public static function create_post($id) { |
|
| 28 | + return new self($id, Object_Type_Enum::POST); |
|
| 29 | 29 | } |
| 30 | 30 | |
| 31 | - public static function create_term( $id ) { |
|
| 32 | - return new self( $id, Object_Type_Enum::TERM ); |
|
| 31 | + public static function create_term($id) { |
|
| 32 | + return new self($id, Object_Type_Enum::TERM); |
|
| 33 | 33 | } |
| 34 | 34 | |
| 35 | - public static function create_user( $id ) { |
|
| 36 | - return new self( $id, Object_Type_Enum::USER ); |
|
| 35 | + public static function create_user($id) { |
|
| 36 | + return new self($id, Object_Type_Enum::USER); |
|
| 37 | 37 | } |
| 38 | 38 | |
| 39 | 39 | /** |
| 40 | 40 | * @param int $id The post/page/term/user ID. |
| 41 | 41 | * @param int $type The content type, post/page/term/user. |
| 42 | 42 | */ |
| 43 | - public function __construct( $id, $type ) { |
|
| 44 | - Assertions::is_numeric( $id ); |
|
| 45 | - Assertions::is_numeric( $type ); |
|
| 43 | + public function __construct($id, $type) { |
|
| 44 | + Assertions::is_numeric($id); |
|
| 45 | + Assertions::is_numeric($type); |
|
| 46 | 46 | |
| 47 | 47 | $this->id = (int) $id; |
| 48 | 48 | $this->type = (int) $type; |
| 49 | 49 | } |
| 50 | 50 | |
| 51 | - public static function from_json( $json ) { |
|
| 52 | - return new self( $json['id'], $json['type'] ); |
|
| 51 | + public static function from_json($json) { |
|
| 52 | + return new self($json['id'], $json['type']); |
|
| 53 | 53 | } |
| 54 | 54 | |
| 55 | 55 | public function get_id() { |
@@ -68,7 +68,7 @@ discard block |
||
| 68 | 68 | } |
| 69 | 69 | |
| 70 | 70 | public function __toString() { |
| 71 | - return sprintf( '%d_%d', $this->type, $this->id ); |
|
| 71 | + return sprintf('%d_%d', $this->type, $this->id); |
|
| 72 | 72 | } |
| 73 | 73 | |
| 74 | 74 | } |
@@ -16,69 +16,69 @@ |
||
| 16 | 16 | |
| 17 | 17 | class Post_Terms_Relation_Service extends Abstract_Relation_Service { |
| 18 | 18 | |
| 19 | - private static $instance = null; |
|
| 19 | + private static $instance = null; |
|
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * The singleton instance. |
|
| 23 | - * |
|
| 24 | - * @return Relation_Service_Interface |
|
| 25 | - */ |
|
| 26 | - public static function get_instance() { |
|
| 27 | - if ( ! isset( self::$instance ) ) { |
|
| 28 | - self::$instance = new self(); |
|
| 29 | - } |
|
| 21 | + /** |
|
| 22 | + * The singleton instance. |
|
| 23 | + * |
|
| 24 | + * @return Relation_Service_Interface |
|
| 25 | + */ |
|
| 26 | + public static function get_instance() { |
|
| 27 | + if ( ! isset( self::$instance ) ) { |
|
| 28 | + self::$instance = new self(); |
|
| 29 | + } |
|
| 30 | 30 | |
| 31 | - return self::$instance; |
|
| 32 | - } |
|
| 31 | + return self::$instance; |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - // ** |
|
| 35 | - // * @param $term_id int Term id. |
|
| 36 | - // */ |
|
| 37 | - // public function get_relation_type( $term_id ) { |
|
| 38 | - // $schema = $this->term_entity_type_service->get_schema( |
|
| 39 | - // $term_id |
|
| 40 | - // ); |
|
| 34 | + // ** |
|
| 35 | + // * @param $term_id int Term id. |
|
| 36 | + // */ |
|
| 37 | + // public function get_relation_type( $term_id ) { |
|
| 38 | + // $schema = $this->term_entity_type_service->get_schema( |
|
| 39 | + // $term_id |
|
| 40 | + // ); |
|
| 41 | 41 | // // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_unserialize |
| 42 | - // $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
| 43 | - // |
|
| 44 | - // $entity_type = str_replace( 'wl-', '', $schema['css_class'] ); |
|
| 45 | - // foreach ( $classification_boxes as $cb ) { |
|
| 46 | - // if ( in_array( $entity_type, $cb['registeredTypes'], true ) ) { |
|
| 47 | - // return $cb['id']; |
|
| 48 | - // } |
|
| 49 | - // } |
|
| 50 | - // |
|
| 51 | - // return WL_WHAT_RELATION; |
|
| 52 | - // } |
|
| 42 | + // $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
| 43 | + // |
|
| 44 | + // $entity_type = str_replace( 'wl-', '', $schema['css_class'] ); |
|
| 45 | + // foreach ( $classification_boxes as $cb ) { |
|
| 46 | + // if ( in_array( $entity_type, $cb['registeredTypes'], true ) ) { |
|
| 47 | + // return $cb['id']; |
|
| 48 | + // } |
|
| 49 | + // } |
|
| 50 | + // |
|
| 51 | + // return WL_WHAT_RELATION; |
|
| 52 | + // } |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * @param $content_id |
|
| 56 | - * @param $relations |
|
| 57 | - * |
|
| 58 | - * @return void |
|
| 59 | - */ |
|
| 60 | - public function add_relations( $content_id, $relations ) { |
|
| 61 | - if ( $content_id->get_type() !== Object_Type_Enum::POST ) { |
|
| 62 | - return; |
|
| 63 | - } |
|
| 54 | + /** |
|
| 55 | + * @param $content_id |
|
| 56 | + * @param $relations |
|
| 57 | + * |
|
| 58 | + * @return void |
|
| 59 | + */ |
|
| 60 | + public function add_relations( $content_id, $relations ) { |
|
| 61 | + if ( $content_id->get_type() !== Object_Type_Enum::POST ) { |
|
| 62 | + return; |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - $post_id = $content_id->get_id(); |
|
| 66 | - // @@todo, add a filter here? |
|
| 67 | - $taxonomies = array( 'category', 'post_tag' ); |
|
| 68 | - $term_ids = wp_get_object_terms( $post_id, $taxonomies, array( 'fields' => 'ids' ) ); |
|
| 65 | + $post_id = $content_id->get_id(); |
|
| 66 | + // @@todo, add a filter here? |
|
| 67 | + $taxonomies = array( 'category', 'post_tag' ); |
|
| 68 | + $term_ids = wp_get_object_terms( $post_id, $taxonomies, array( 'fields' => 'ids' ) ); |
|
| 69 | 69 | |
| 70 | - $new_relations = array_map( |
|
| 71 | - function ( $term_id ) use ( $content_id ) { |
|
| 72 | - $object_id = Wordpress_Content_Id::create_term( $term_id ); |
|
| 70 | + $new_relations = array_map( |
|
| 71 | + function ( $term_id ) use ( $content_id ) { |
|
| 72 | + $object_id = Wordpress_Content_Id::create_term( $term_id ); |
|
| 73 | 73 | |
| 74 | - // @@todo Entity Service works only for posts, we default to `WHAT` as predicate |
|
| 75 | - // for terms. |
|
| 76 | - return new Relation( $content_id, $object_id, WL_WHAT_RELATION ); |
|
| 77 | - }, |
|
| 78 | - $term_ids |
|
| 79 | - ); |
|
| 74 | + // @@todo Entity Service works only for posts, we default to `WHAT` as predicate |
|
| 75 | + // for terms. |
|
| 76 | + return new Relation( $content_id, $object_id, WL_WHAT_RELATION ); |
|
| 77 | + }, |
|
| 78 | + $term_ids |
|
| 79 | + ); |
|
| 80 | 80 | |
| 81 | - $relations->add( ...$new_relations ); |
|
| 82 | - } |
|
| 81 | + $relations->add( ...$new_relations ); |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | 84 | } |
@@ -24,7 +24,7 @@ discard block |
||
| 24 | 24 | * @return Relation_Service_Interface |
| 25 | 25 | */ |
| 26 | 26 | public static function get_instance() { |
| 27 | - if ( ! isset( self::$instance ) ) { |
|
| 27 | + if ( ! isset(self::$instance)) { |
|
| 28 | 28 | self::$instance = new self(); |
| 29 | 29 | } |
| 30 | 30 | |
@@ -57,28 +57,28 @@ discard block |
||
| 57 | 57 | * |
| 58 | 58 | * @return void |
| 59 | 59 | */ |
| 60 | - public function add_relations( $content_id, $relations ) { |
|
| 61 | - if ( $content_id->get_type() !== Object_Type_Enum::POST ) { |
|
| 60 | + public function add_relations($content_id, $relations) { |
|
| 61 | + if ($content_id->get_type() !== Object_Type_Enum::POST) { |
|
| 62 | 62 | return; |
| 63 | 63 | } |
| 64 | 64 | |
| 65 | 65 | $post_id = $content_id->get_id(); |
| 66 | 66 | // @@todo, add a filter here? |
| 67 | - $taxonomies = array( 'category', 'post_tag' ); |
|
| 68 | - $term_ids = wp_get_object_terms( $post_id, $taxonomies, array( 'fields' => 'ids' ) ); |
|
| 67 | + $taxonomies = array('category', 'post_tag'); |
|
| 68 | + $term_ids = wp_get_object_terms($post_id, $taxonomies, array('fields' => 'ids')); |
|
| 69 | 69 | |
| 70 | 70 | $new_relations = array_map( |
| 71 | - function ( $term_id ) use ( $content_id ) { |
|
| 72 | - $object_id = Wordpress_Content_Id::create_term( $term_id ); |
|
| 71 | + function($term_id) use ($content_id) { |
|
| 72 | + $object_id = Wordpress_Content_Id::create_term($term_id); |
|
| 73 | 73 | |
| 74 | 74 | // @@todo Entity Service works only for posts, we default to `WHAT` as predicate |
| 75 | 75 | // for terms. |
| 76 | - return new Relation( $content_id, $object_id, WL_WHAT_RELATION ); |
|
| 76 | + return new Relation($content_id, $object_id, WL_WHAT_RELATION); |
|
| 77 | 77 | }, |
| 78 | 78 | $term_ids |
| 79 | 79 | ); |
| 80 | 80 | |
| 81 | - $relations->add( ...$new_relations ); |
|
| 81 | + $relations->add(...$new_relations); |
|
| 82 | 82 | } |
| 83 | 83 | |
| 84 | 84 | } |
@@ -15,46 +15,46 @@ |
||
| 15 | 15 | |
| 16 | 16 | class Relation_Instances_Relation_Service extends Abstract_Relation_Service { |
| 17 | 17 | |
| 18 | - private static $instance = null; |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * The singleton instance. |
|
| 22 | - * |
|
| 23 | - * @return Relation_Service_Interface |
|
| 24 | - */ |
|
| 25 | - public static function get_instance() { |
|
| 26 | - if ( ! isset( self::$instance ) ) { |
|
| 27 | - self::$instance = new self(); |
|
| 28 | - } |
|
| 29 | - |
|
| 30 | - return self::$instance; |
|
| 31 | - } |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * @param Wordpress_Content_Id $content_id |
|
| 35 | - * @param Relations_Interface $relations |
|
| 36 | - */ |
|
| 37 | - public function add_relations( $content_id, $relations ) { |
|
| 38 | - global $wpdb; |
|
| 39 | - |
|
| 40 | - $results = $wpdb->get_results( |
|
| 41 | - $wpdb->prepare( |
|
| 42 | - " |
|
| 18 | + private static $instance = null; |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * The singleton instance. |
|
| 22 | + * |
|
| 23 | + * @return Relation_Service_Interface |
|
| 24 | + */ |
|
| 25 | + public static function get_instance() { |
|
| 26 | + if ( ! isset( self::$instance ) ) { |
|
| 27 | + self::$instance = new self(); |
|
| 28 | + } |
|
| 29 | + |
|
| 30 | + return self::$instance; |
|
| 31 | + } |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * @param Wordpress_Content_Id $content_id |
|
| 35 | + * @param Relations_Interface $relations |
|
| 36 | + */ |
|
| 37 | + public function add_relations( $content_id, $relations ) { |
|
| 38 | + global $wpdb; |
|
| 39 | + |
|
| 40 | + $results = $wpdb->get_results( |
|
| 41 | + $wpdb->prepare( |
|
| 42 | + " |
|
| 43 | 43 | SELECT subject_id, subject_type, predicate, object_id, object_type |
| 44 | 44 | FROM {$wpdb->prefix}wl_relation_instances |
| 45 | 45 | WHERE subject_id = %d AND subject_type = %s |
| 46 | 46 | ", |
| 47 | - $content_id->get_id(), |
|
| 48 | - $content_id->get_type() |
|
| 49 | - ) |
|
| 50 | - ); |
|
| 51 | - |
|
| 52 | - $new_relations = array_map( |
|
| 53 | - 'Wordlift\Relation\Relation::from_relation_instances', |
|
| 54 | - $results |
|
| 55 | - ); |
|
| 56 | - |
|
| 57 | - $relations->add( ...$new_relations ); |
|
| 58 | - } |
|
| 47 | + $content_id->get_id(), |
|
| 48 | + $content_id->get_type() |
|
| 49 | + ) |
|
| 50 | + ); |
|
| 51 | + |
|
| 52 | + $new_relations = array_map( |
|
| 53 | + 'Wordlift\Relation\Relation::from_relation_instances', |
|
| 54 | + $results |
|
| 55 | + ); |
|
| 56 | + |
|
| 57 | + $relations->add( ...$new_relations ); |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | 60 | } |
@@ -23,7 +23,7 @@ discard block |
||
| 23 | 23 | * @return Relation_Service_Interface |
| 24 | 24 | */ |
| 25 | 25 | public static function get_instance() { |
| 26 | - if ( ! isset( self::$instance ) ) { |
|
| 26 | + if ( ! isset(self::$instance)) { |
|
| 27 | 27 | self::$instance = new self(); |
| 28 | 28 | } |
| 29 | 29 | |
@@ -34,7 +34,7 @@ discard block |
||
| 34 | 34 | * @param Wordpress_Content_Id $content_id |
| 35 | 35 | * @param Relations_Interface $relations |
| 36 | 36 | */ |
| 37 | - public function add_relations( $content_id, $relations ) { |
|
| 37 | + public function add_relations($content_id, $relations) { |
|
| 38 | 38 | global $wpdb; |
| 39 | 39 | |
| 40 | 40 | $results = $wpdb->get_results( |
@@ -54,7 +54,7 @@ discard block |
||
| 54 | 54 | $results |
| 55 | 55 | ); |
| 56 | 56 | |
| 57 | - $relations->add( ...$new_relations ); |
|
| 57 | + $relations->add(...$new_relations); |
|
| 58 | 58 | } |
| 59 | 59 | |
| 60 | 60 | } |
@@ -6,24 +6,24 @@ |
||
| 6 | 6 | |
| 7 | 7 | abstract class Abstract_Relation_Service implements Relation_Service_Interface { |
| 8 | 8 | |
| 9 | - protected function __construct() { |
|
| 9 | + protected function __construct() { |
|
| 10 | 10 | |
| 11 | - } |
|
| 11 | + } |
|
| 12 | 12 | |
| 13 | - /** |
|
| 14 | - * A default implementation for the `get_relations` method that creates `Relations` instance |
|
| 15 | - * and populates it with `Relation`s. |
|
| 16 | - * |
|
| 17 | - * @param Wordpress_Content_Id $content_id The WordPress content id. |
|
| 18 | - * |
|
| 19 | - * @return Relations_Interface A `Relations` instance. |
|
| 20 | - */ |
|
| 21 | - public function get_relations( $content_id ) { |
|
| 22 | - $relations = new Relations(); |
|
| 13 | + /** |
|
| 14 | + * A default implementation for the `get_relations` method that creates `Relations` instance |
|
| 15 | + * and populates it with `Relation`s. |
|
| 16 | + * |
|
| 17 | + * @param Wordpress_Content_Id $content_id The WordPress content id. |
|
| 18 | + * |
|
| 19 | + * @return Relations_Interface A `Relations` instance. |
|
| 20 | + */ |
|
| 21 | + public function get_relations( $content_id ) { |
|
| 22 | + $relations = new Relations(); |
|
| 23 | 23 | |
| 24 | - $this->add_relations( $content_id, $relations ); |
|
| 24 | + $this->add_relations( $content_id, $relations ); |
|
| 25 | 25 | |
| 26 | - return $relations; |
|
| 27 | - } |
|
| 26 | + return $relations; |
|
| 27 | + } |
|
| 28 | 28 | |
| 29 | 29 | } |
@@ -18,10 +18,10 @@ |
||
| 18 | 18 | * |
| 19 | 19 | * @return Relations_Interface A `Relations` instance. |
| 20 | 20 | */ |
| 21 | - public function get_relations( $content_id ) { |
|
| 21 | + public function get_relations($content_id) { |
|
| 22 | 22 | $relations = new Relations(); |
| 23 | 23 | |
| 24 | - $this->add_relations( $content_id, $relations ); |
|
| 24 | + $this->add_relations($content_id, $relations); |
|
| 25 | 25 | |
| 26 | 26 | return $relations; |
| 27 | 27 | } |