|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Wordlift\Vocabulary; |
|
4
|
|
|
|
|
5
|
|
|
use Wordlift\Api\Default_Api_Service; |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @since 1.0.0 |
|
10
|
|
|
* @author Naveen Muthusamy <[email protected]> |
|
11
|
|
|
*/ |
|
12
|
|
|
class Analysis_Service { |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var Default_Api_Service |
|
16
|
|
|
*/ |
|
17
|
|
|
private $analysis_service; |
|
18
|
|
|
/** |
|
19
|
|
|
* @var Options_Cache |
|
20
|
|
|
*/ |
|
21
|
|
|
private $cache_service; |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Tag_Rest_Endpoint constructor. |
|
26
|
|
|
* |
|
27
|
|
|
* @param Default_Api_Service $analysis_service |
|
28
|
|
|
* @param Options_Cache $cache_service |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct( $analysis_service, $cache_service ) { |
|
31
|
|
|
|
|
32
|
|
|
$this->analysis_service = $analysis_service; |
|
33
|
|
|
|
|
34
|
|
|
$this->cache_service = $cache_service; |
|
35
|
|
|
|
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Check if entities are in cache, if not return the results from |
|
41
|
|
|
* cache service. |
|
42
|
|
|
* |
|
43
|
|
|
* @param $tag \WP_Term |
|
44
|
|
|
*/ |
|
45
|
|
|
public function get_entities( $tag ) { |
|
46
|
|
|
|
|
47
|
|
|
$cache_key = $tag->term_id; |
|
48
|
|
|
$cache_result = $this->cache_service->get( $cache_key ); |
|
49
|
|
|
if ( $cache_result !== false ) { |
|
50
|
|
|
return $cache_result; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
// send the request. |
|
54
|
|
|
$response = $this->analysis_service->request( |
|
55
|
|
|
'POST', |
|
56
|
|
|
"/analysis/single", |
|
57
|
|
|
array( 'Content-Type' => 'application/json' ), |
|
58
|
|
|
wp_json_encode( array( |
|
59
|
|
|
"content" => $tag->name, |
|
60
|
|
|
"contentType" => "text/plain", |
|
61
|
|
|
"version" => "1.0.0", |
|
62
|
|
|
"contentLanguage" => "en", |
|
63
|
|
|
"scope" => "network", |
|
64
|
|
|
) ) |
|
65
|
|
|
); |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
if ( ! $response->is_success() ) { |
|
69
|
|
|
return false; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$response = json_decode( $response->get_body(), true ); |
|
73
|
|
|
|
|
74
|
|
|
if ( ! array_key_exists( 'entities', $response ) ) { |
|
75
|
|
|
return false; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
$entities = $this->get_meta_for_entities( $response['entities'] ); |
|
80
|
|
|
|
|
81
|
|
|
$this->cache_service->put( $cache_key, $entities ); |
|
82
|
|
|
|
|
83
|
|
|
return $entities; |
|
84
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
private function get_meta( $entity_url ) { |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
$cache_results = $this->cache_service->get( $entity_url ); |
|
92
|
|
|
|
|
93
|
|
|
if ( $cache_results !== false ) { |
|
94
|
|
|
return $cache_results; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$response = wp_remote_get( "https://app.wordlift.io/knowledge-cafemedia-com-food/wp-json/wordlift/v1/jsonld/meta/entity_url?meta_value=" . urlencode($entity_url) ); |
|
98
|
|
|
|
|
99
|
|
|
if ( ! is_wp_error( $response ) ) { |
|
100
|
|
|
$meta = json_decode( wp_remote_retrieve_body( $response ), true ); |
|
101
|
|
|
$this->cache_service->put( $entity_url, $meta ); |
|
102
|
|
|
return $meta; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return array(); |
|
106
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
private function get_meta_for_entities( $entities ) { |
|
110
|
|
|
|
|
111
|
|
|
$filtered_entities = array(); |
|
112
|
|
|
foreach ( $entities as $entity ) { |
|
113
|
|
|
$entity['meta'] = array(); |
|
114
|
|
|
$meta = $this->get_meta( $entity['entityId'] ); |
|
115
|
|
|
if ( $meta && count( $meta ) > 0 ) { |
|
116
|
|
|
$entity['meta'] = $meta[0]; |
|
117
|
|
|
} |
|
118
|
|
|
$filtered_entities[] = $entity; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return $filtered_entities; |
|
122
|
|
|
|
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
|
|
126
|
|
|
} |
|
127
|
|
|
|