Completed
Pull Request — master (#1595)
by Naveen
01:06
created
src/includes/class-wordlift-key-validation-service.php 1 patch
Indentation   +193 added lines, -193 removed lines patch added patch discarded remove patch
@@ -20,201 +20,201 @@
 block discarded – undo
20 20
  */
21 21
 class Wordlift_Key_Validation_Service {
22 22
 
23
-	/**
24
-	 * A {@link Wordlift_Log_Service} instance.
25
-	 *
26
-	 * @since  3.14.0
27
-	 * @access private
28
-	 * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
29
-	 */
30
-	private $log;
31
-
32
-	/**
33
-	 * @var Ttl_Cache
34
-	 */
35
-	private $ttl_cache_service;
36
-
37
-	/**
38
-	 * Create a {@link Wordlift_Key_Validation_Service} instance.
39
-	 *
40
-	 * @since 3.14.0
41
-	 */
42
-	public function __construct() {
43
-
44
-		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Key_Validation_Service' );
45
-
46
-		add_action( 'admin_init', array( $this, 'wl_load_plugin' ) );
47
-		/**
48
-		 * Filter: wl_feature__enable__notices.
49
-		 *
50
-		 * @param bool whether notices need to be enabled or not.
51
-		 *
52
-		 * @return bool
53
-		 * @since 3.27.6
54
-		 */
55
-		if ( apply_filters( 'wl_feature__enable__notices', true ) ) {
56
-			add_action( 'admin_notices', array( $this, 'wl_key_update_notice' ) );
57
-		}
58
-
59
-		$this->ttl_cache_service = new Ttl_Cache( 'key-validation-notification' );
60
-
61
-	}
62
-
63
-	/**
64
-	 * Validate the provided key.
65
-	 *
66
-	 * @param string $key WordLift's key to validate.
67
-	 *
68
-	 * @return WP_Error|array The response or WP_Error on failure.
69
-	 * @since 3.9.0
70
-	 */
71
-	public function get_account_info( $key ) {
72
-
73
-		$this->log->debug( 'Validating key...' );
74
-
75
-		$response = Default_Api_Service::get_instance()->get(
76
-			'/accounts/info',
77
-			array(
78
-				'Authorization' => "Key $key",
79
-			)
80
-		);
81
-
82
-		/**
83
-		 * @since 3.38.6
84
-		 * This action is fired when the key is validated.
85
-		 * @param $response \Wordlift\Api\Response
86
-		 */
87
-		do_action( 'wl_key_validation_response', $response );
88
-
89
-		return $response->get_response();
90
-	}
91
-
92
-	/**
93
-	 * Check if key is valid
94
-	 *
95
-	 * @param $key string
96
-	 *
97
-	 * @return bool
98
-	 */
99
-	public function is_key_valid( $key ) {
100
-
101
-		$response = $this->get_account_info( $key );
102
-
103
-		if ( is_wp_error( $response ) || 2 !== (int) $response['response']['code'] / 100 ) {
104
-			return false;
105
-		}
106
-		$res_body = json_decode( wp_remote_retrieve_body( $response ), true );
107
-
108
-		$url = $res_body['url'];
109
-
110
-		// Considering that production URL may be filtered.
111
-		$home_url = get_option( 'home' );
112
-		$site_url = apply_filters( 'wl_production_site_url', untrailingslashit( $home_url ) );
113
-		if ( $url === null || $url === $site_url ) {
114
-			return true;
115
-		}
116
-
117
-		return false;
118
-	}
119
-
120
-	/**
121
-	 * This function is hooked to the `wl_validate_key` AJAX call.
122
-	 *
123
-	 * @since 3.9.0
124
-	 */
125
-	public function validate_key() {
126
-
127
-		// Ensure we don't have garbage before us.
128
-		ob_clean();
129
-
130
-		// Check if we have a key.
131
-		if ( ! isset( $_POST['key'] ) ) {  //phpcs:ignore WordPress.Security.NonceVerification.Missing
132
-			wp_send_json_error( 'The key parameter is required.' );
133
-		}
134
-
135
-		$response = $this->get_account_info( sanitize_text_field( wp_unslash( (string) $_POST['key'] ) ) );  //phpcs:ignore WordPress.Security.NonceVerification.Missing
136
-
137
-		// If we got an error, return invalid.
138
-		if ( is_wp_error( $response ) || 2 !== (int) $response['response']['code'] / 100 ) {
139
-			wp_send_json_success(
140
-				array(
141
-					'valid'    => false,
142
-					'message'  => '',
143
-					'response' => $response,
144
-					'api_url'  => Default_Api_Service::get_instance()->get_base_url(),
145
-				)
146
-			);
147
-		}
148
-
149
-		$res_body = json_decode( wp_remote_retrieve_body( $response ), true );
150
-
151
-		// The URL stored in WLS. If this is the initial install the URL may be null.
152
-		$url = $res_body['url'];
153
-
154
-		// Considering that production URL may be filtered.
155
-		$home_url = get_option( 'home' );
156
-		$site_url = apply_filters( 'wl_production_site_url', untrailingslashit( $home_url ) );
157
-
158
-		// If the URL isn't set or matches, then it's valid.
159
-		if ( $url === null || $url === $site_url ) {
160
-			// Invalidate the cache key
161
-			$this->ttl_cache_service->delete( 'is_key_valid' );
162
-			wp_send_json_success(
163
-				array(
164
-					'valid'   => true,
165
-					'message' => '',
166
-				)
167
-			);
168
-		}
169
-
170
-		// If the URL doesn't match it means that this key has been configured elsewhere already.
171
-		if ( $url !== $site_url ) {
172
-			Wordlift_Configuration_Service::get_instance()->set_key( '' );
173
-			wp_send_json_success(
174
-				array(
175
-					'valid'   => false,
176
-					'message' => __( 'The key is already used on another site, please contact us at [email protected] to move the key to another site.', 'wordlift' ),
177
-				)
178
-			);
179
-		}
180
-
181
-		// Set a response with valid set to true or false according to the key validity with message.
182
-		wp_send_json_success(
183
-			array(
184
-				'valid'   => false,
185
-				'message' => __( 'An error occurred, please contact us at [email protected]', 'wordlift' ),
186
-			)
187
-		);
188
-	}
189
-
190
-	/**
191
-	 * This function is hooked `admin_init` to check _wl_blog_url.
192
-	 */
193
-	public function wl_load_plugin() {
194
-
195
-		$wl_blog_url = get_option( '_wl_blog_url' );
196
-		$home_url    = get_option( 'home' );
197
-
198
-		if ( ! $wl_blog_url ) {
199
-			update_option( '_wl_blog_url', $home_url, true );
200
-		} elseif ( $wl_blog_url !== $home_url ) {
201
-			update_option( '_wl_blog_url', $home_url, true );
202
-			Wordlift_Configuration_Service::get_instance()->set_key( '' );
203
-			set_transient( 'wl-key-error-msg', __( "Your web site URL has changed. To avoid data corruption, WordLift's key has been removed. Please provide a new key in WordLift Settings. If you believe this to be an error, please contact us at [email protected]", 'wordlift' ), 10 );
204
-		}
205
-
206
-	}
207
-
208
-	/**
209
-	 * This function is hooked to the `admin_notices` to show admin notification.
210
-	 */
211
-	public function wl_key_update_notice() {
212
-		if ( get_transient( 'wl-key-error-msg' ) ) {
213
-			?>
23
+    /**
24
+     * A {@link Wordlift_Log_Service} instance.
25
+     *
26
+     * @since  3.14.0
27
+     * @access private
28
+     * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
29
+     */
30
+    private $log;
31
+
32
+    /**
33
+     * @var Ttl_Cache
34
+     */
35
+    private $ttl_cache_service;
36
+
37
+    /**
38
+     * Create a {@link Wordlift_Key_Validation_Service} instance.
39
+     *
40
+     * @since 3.14.0
41
+     */
42
+    public function __construct() {
43
+
44
+        $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Key_Validation_Service' );
45
+
46
+        add_action( 'admin_init', array( $this, 'wl_load_plugin' ) );
47
+        /**
48
+         * Filter: wl_feature__enable__notices.
49
+         *
50
+         * @param bool whether notices need to be enabled or not.
51
+         *
52
+         * @return bool
53
+         * @since 3.27.6
54
+         */
55
+        if ( apply_filters( 'wl_feature__enable__notices', true ) ) {
56
+            add_action( 'admin_notices', array( $this, 'wl_key_update_notice' ) );
57
+        }
58
+
59
+        $this->ttl_cache_service = new Ttl_Cache( 'key-validation-notification' );
60
+
61
+    }
62
+
63
+    /**
64
+     * Validate the provided key.
65
+     *
66
+     * @param string $key WordLift's key to validate.
67
+     *
68
+     * @return WP_Error|array The response or WP_Error on failure.
69
+     * @since 3.9.0
70
+     */
71
+    public function get_account_info( $key ) {
72
+
73
+        $this->log->debug( 'Validating key...' );
74
+
75
+        $response = Default_Api_Service::get_instance()->get(
76
+            '/accounts/info',
77
+            array(
78
+                'Authorization' => "Key $key",
79
+            )
80
+        );
81
+
82
+        /**
83
+         * @since 3.38.6
84
+         * This action is fired when the key is validated.
85
+         * @param $response \Wordlift\Api\Response
86
+         */
87
+        do_action( 'wl_key_validation_response', $response );
88
+
89
+        return $response->get_response();
90
+    }
91
+
92
+    /**
93
+     * Check if key is valid
94
+     *
95
+     * @param $key string
96
+     *
97
+     * @return bool
98
+     */
99
+    public function is_key_valid( $key ) {
100
+
101
+        $response = $this->get_account_info( $key );
102
+
103
+        if ( is_wp_error( $response ) || 2 !== (int) $response['response']['code'] / 100 ) {
104
+            return false;
105
+        }
106
+        $res_body = json_decode( wp_remote_retrieve_body( $response ), true );
107
+
108
+        $url = $res_body['url'];
109
+
110
+        // Considering that production URL may be filtered.
111
+        $home_url = get_option( 'home' );
112
+        $site_url = apply_filters( 'wl_production_site_url', untrailingslashit( $home_url ) );
113
+        if ( $url === null || $url === $site_url ) {
114
+            return true;
115
+        }
116
+
117
+        return false;
118
+    }
119
+
120
+    /**
121
+     * This function is hooked to the `wl_validate_key` AJAX call.
122
+     *
123
+     * @since 3.9.0
124
+     */
125
+    public function validate_key() {
126
+
127
+        // Ensure we don't have garbage before us.
128
+        ob_clean();
129
+
130
+        // Check if we have a key.
131
+        if ( ! isset( $_POST['key'] ) ) {  //phpcs:ignore WordPress.Security.NonceVerification.Missing
132
+            wp_send_json_error( 'The key parameter is required.' );
133
+        }
134
+
135
+        $response = $this->get_account_info( sanitize_text_field( wp_unslash( (string) $_POST['key'] ) ) );  //phpcs:ignore WordPress.Security.NonceVerification.Missing
136
+
137
+        // If we got an error, return invalid.
138
+        if ( is_wp_error( $response ) || 2 !== (int) $response['response']['code'] / 100 ) {
139
+            wp_send_json_success(
140
+                array(
141
+                    'valid'    => false,
142
+                    'message'  => '',
143
+                    'response' => $response,
144
+                    'api_url'  => Default_Api_Service::get_instance()->get_base_url(),
145
+                )
146
+            );
147
+        }
148
+
149
+        $res_body = json_decode( wp_remote_retrieve_body( $response ), true );
150
+
151
+        // The URL stored in WLS. If this is the initial install the URL may be null.
152
+        $url = $res_body['url'];
153
+
154
+        // Considering that production URL may be filtered.
155
+        $home_url = get_option( 'home' );
156
+        $site_url = apply_filters( 'wl_production_site_url', untrailingslashit( $home_url ) );
157
+
158
+        // If the URL isn't set or matches, then it's valid.
159
+        if ( $url === null || $url === $site_url ) {
160
+            // Invalidate the cache key
161
+            $this->ttl_cache_service->delete( 'is_key_valid' );
162
+            wp_send_json_success(
163
+                array(
164
+                    'valid'   => true,
165
+                    'message' => '',
166
+                )
167
+            );
168
+        }
169
+
170
+        // If the URL doesn't match it means that this key has been configured elsewhere already.
171
+        if ( $url !== $site_url ) {
172
+            Wordlift_Configuration_Service::get_instance()->set_key( '' );
173
+            wp_send_json_success(
174
+                array(
175
+                    'valid'   => false,
176
+                    'message' => __( 'The key is already used on another site, please contact us at [email protected] to move the key to another site.', 'wordlift' ),
177
+                )
178
+            );
179
+        }
180
+
181
+        // Set a response with valid set to true or false according to the key validity with message.
182
+        wp_send_json_success(
183
+            array(
184
+                'valid'   => false,
185
+                'message' => __( 'An error occurred, please contact us at [email protected]', 'wordlift' ),
186
+            )
187
+        );
188
+    }
189
+
190
+    /**
191
+     * This function is hooked `admin_init` to check _wl_blog_url.
192
+     */
193
+    public function wl_load_plugin() {
194
+
195
+        $wl_blog_url = get_option( '_wl_blog_url' );
196
+        $home_url    = get_option( 'home' );
197
+
198
+        if ( ! $wl_blog_url ) {
199
+            update_option( '_wl_blog_url', $home_url, true );
200
+        } elseif ( $wl_blog_url !== $home_url ) {
201
+            update_option( '_wl_blog_url', $home_url, true );
202
+            Wordlift_Configuration_Service::get_instance()->set_key( '' );
203
+            set_transient( 'wl-key-error-msg', __( "Your web site URL has changed. To avoid data corruption, WordLift's key has been removed. Please provide a new key in WordLift Settings. If you believe this to be an error, please contact us at [email protected]", 'wordlift' ), 10 );
204
+        }
205
+
206
+    }
207
+
208
+    /**
209
+     * This function is hooked to the `admin_notices` to show admin notification.
210
+     */
211
+    public function wl_key_update_notice() {
212
+        if ( get_transient( 'wl-key-error-msg' ) ) {
213
+            ?>
214 214
 			<div class="updated notice is-dismissible error">
215 215
 				<p><?php esc_html( get_transient( 'wl-key-error-msg' ) ); ?></p>
216 216
 			</div>
217 217
 			<?php
218
-		}
219
-	}
218
+        }
219
+    }
220 220
 }
Please login to merge, or discard this patch.
src/includes/class-wordlift-configuration-service.php 1 patch
Indentation   +728 added lines, -728 removed lines patch added patch discarded remove patch
@@ -13,7 +13,7 @@  discard block
 block discarded – undo
13 13
 use Wordlift\Api\Default_Api_Service;
14 14
 
15 15
 if ( ! defined( 'ABSPATH' ) ) {
16
-	exit;
16
+    exit;
17 17
 }
18 18
 
19 19
 /**
@@ -23,736 +23,736 @@  discard block
 block discarded – undo
23 23
  */
24 24
 class Wordlift_Configuration_Service {
25 25
 
26
-	/**
27
-	 * The entity base path option name.
28
-	 *
29
-	 * @since 3.6.0
30
-	 */
31
-	const ENTITY_BASE_PATH_KEY = 'wl_entity_base_path';
32
-
33
-	/**
34
-	 * The skip wizard (admin installation wizard) option name.
35
-	 *
36
-	 * @since 3.9.0
37
-	 */
38
-	const SKIP_WIZARD = 'wl_skip_wizard';
39
-
40
-	/**
41
-	 * WordLift's key option name.
42
-	 *
43
-	 * @since 3.9.0
44
-	 */
45
-	const KEY = 'key';
46
-
47
-	/**
48
-	 * WordLift's configured language option name.
49
-	 *
50
-	 * @since 3.9.0
51
-	 */
52
-	const LANGUAGE = 'site_language';
53
-
54
-	/**
55
-	 * WordLift's configured country code.
56
-	 *
57
-	 * @since 3.18.0
58
-	 */
59
-	const COUNTRY_CODE = 'country_code';
60
-
61
-	/**
62
-	 * The publisher entity post ID option name.
63
-	 *
64
-	 * @since 3.9.0
65
-	 */
66
-	const PUBLISHER_ID = 'publisher_id';
67
-
68
-	/**
69
-	 * The dataset URI option name
70
-	 *
71
-	 * @since 3.10.0
72
-	 */
73
-	const DATASET_URI = 'redlink_dataset_uri';
74
-
75
-	/**
76
-	 * The link by default option name.
77
-	 *
78
-	 * @since 3.11.0
79
-	 */
80
-	const LINK_BY_DEFAULT = 'link_by_default';
81
-
82
-	/**
83
-	 * The analytics enable option.
84
-	 *
85
-	 * @since 3.21.0
86
-	 */
87
-	const ANALYTICS_ENABLE = 'analytics_enable';
88
-
89
-	/**
90
-	 * The analytics entity uri dimension option.
91
-	 *
92
-	 * @since 3.21.0
93
-	 */
94
-	const ANALYTICS_ENTITY_URI_DIMENSION = 'analytics_entity_uri_dimension';
95
-
96
-	/**
97
-	 * The analytics entity type dimension option.
98
-	 *
99
-	 * @since 3.21.0
100
-	 */
101
-	const ANALYTICS_ENTITY_TYPE_DIMENSION = 'analytics_entity_type_dimension';
102
-
103
-	/**
104
-	 * The user preferences about sharing data option.
105
-	 *
106
-	 * @since 3.19.0
107
-	 */
108
-	const SEND_DIAGNOSTIC = 'send_diagnostic';
109
-
110
-	/**
111
-	 * The package type configuration key.
112
-	 *
113
-	 * @since 3.20.0
114
-	 */
115
-	const PACKAGE_TYPE = 'package_type';
116
-	/**
117
-	 * The dataset ids connected to the current key
118
-	 *
119
-	 * @since 3.38.6
120
-	 */
121
-	const NETWORK_DATASET_IDS = 'network_dataset_ids';
122
-
123
-	/**
124
-	 * The {@link Wordlift_Log_Service} instance.
125
-	 *
126
-	 * @since 3.16.0
127
-	 *
128
-	 * @var \Wordlift_Log_Service $log The {@link Wordlift_Log_Service} instance.
129
-	 */
130
-	private $log;
131
-
132
-	/**
133
-	 * Create a Wordlift_Configuration_Service's instance.
134
-	 *
135
-	 * @since 3.6.0
136
-	 */
137
-	protected function __construct() {
138
-
139
-		$this->log = Wordlift_Log_Service::get_logger( get_class() );
140
-
141
-		// Sync some configuration properties when key is validated.
142
-		add_action( 'wl_key_validation_response', array( $this, 'sync' ) );
143
-
144
-	}
145
-
146
-	/**
147
-	 * @param $response \Wordlift\Api\Response
148
-	 *
149
-	 * @return void
150
-	 */
151
-	public function sync( $response ) {
152
-		if ( ! $response->is_success() ) {
153
-			return;
154
-		}
155
-		$data = json_decode( $response->get_body(), true );
156
-		if ( ! is_array( $data ) || ! array_key_exists( 'networkDatasetId', $data ) ) {
157
-			return;
158
-		}
159
-		$this->set_network_dataset_ids( $data['networkDatasetId'] );
160
-	}
161
-
162
-	/**
163
-	 * The Wordlift_Configuration_Service's singleton instance.
164
-	 *
165
-	 * @since  3.6.0
166
-	 *
167
-	 * @access private
168
-	 * @var \Wordlift_Configuration_Service $instance Wordlift_Configuration_Service's singleton instance.
169
-	 */
170
-	private static $instance = null;
171
-
172
-	/**
173
-	 * Get the singleton instance.
174
-	 *
175
-	 * @return \Wordlift_Configuration_Service
176
-	 * @since 3.6.0
177
-	 */
178
-	public static function get_instance() {
179
-
180
-		if ( ! isset( self::$instance ) ) {
181
-			self::$instance = new self();
182
-		}
183
-
184
-		return self::$instance;
185
-	}
186
-
187
-	/**
188
-	 * Get a configuration given the option name and a key. The option value is
189
-	 * expected to be an array.
190
-	 *
191
-	 * @param string $option The option name.
192
-	 * @param string $key A key in the option value array.
193
-	 * @param string $default The default value in case the key is not found (by default an empty string).
194
-	 *
195
-	 * @return mixed The configuration value or the default value if not found.
196
-	 * @since 3.6.0
197
-	 */
198
-	private function get( $option, $key, $default = '' ) {
199
-
200
-		$options = get_option( $option, array() );
201
-
202
-		return isset( $options[ $key ] ) ? $options[ $key ] : $default;
203
-	}
204
-
205
-	/**
206
-	 * Set a configuration parameter.
207
-	 *
208
-	 * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
209
-	 * @param string $key The value key.
210
-	 * @param mixed  $value The value.
211
-	 *
212
-	 * @since 3.9.0
213
-	 */
214
-	private function set( $option, $key, $value ) {
215
-
216
-		$values         = get_option( $option );
217
-		$values         = isset( $values ) ? $values : array();
218
-		$values[ $key ] = $value;
219
-		update_option( $option, $values );
220
-
221
-	}
222
-
223
-	/**
224
-	 * Get the entity base path, by default 'entity'.
225
-	 *
226
-	 * @return string The entity base path.
227
-	 * @since 3.6.0
228
-	 */
229
-	public function get_entity_base_path() {
230
-
231
-		return $this->get( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, 'entity' );
232
-	}
233
-
234
-	/**
235
-	 * Get the entity base path.
236
-	 *
237
-	 * @param string $value The entity base path.
238
-	 *
239
-	 * @since 3.9.0
240
-	 */
241
-	public function set_entity_base_path( $value ) {
242
-
243
-		$this->set( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, $value );
244
-
245
-	}
246
-
247
-	/**
248
-	 * Whether the installation skip wizard should be skipped.
249
-	 *
250
-	 * @return bool True if it should be skipped otherwise false.
251
-	 * @since 3.9.0
252
-	 */
253
-	public function is_skip_wizard() {
254
-
255
-		return $this->get( 'wl_general_settings', self::SKIP_WIZARD, false );
256
-	}
257
-
258
-	/**
259
-	 * Set the skip wizard parameter.
260
-	 *
261
-	 * @param bool $value True to skip the wizard. We expect a boolean value.
262
-	 *
263
-	 * @since 3.9.0
264
-	 */
265
-	public function set_skip_wizard( $value ) {
266
-
267
-		$this->set( 'wl_general_settings', self::SKIP_WIZARD, true === $value );
268
-
269
-	}
270
-
271
-	/**
272
-	 * Get WordLift's key.
273
-	 *
274
-	 * @return string WordLift's key or an empty string if not set.
275
-	 * @since 3.9.0
276
-	 */
277
-	public function get_key() {
278
-
279
-		return $this->get( 'wl_general_settings', self::KEY, '' );
280
-	}
281
-
282
-	/**
283
-	 * Set WordLift's key.
284
-	 *
285
-	 * @param string $value WordLift's key.
286
-	 *
287
-	 * @since 3.9.0
288
-	 */
289
-	public function set_key( $value ) {
290
-
291
-		$this->set( 'wl_general_settings', self::KEY, $value );
292
-	}
293
-
294
-	/**
295
-	 * Get WordLift's configured language, by default 'en'.
296
-	 *
297
-	 * Note that WordLift's language is used when writing strings to the Linked Data dataset, not for the analysis.
298
-	 *
299
-	 * @return string WordLift's configured language code ('en' by default).
300
-	 * @since 3.9.0
301
-	 */
302
-	public function get_language_code() {
303
-
304
-		$language = get_locale();
305
-		if ( ! $language ) {
306
-			return 'en';
307
-		}
308
-
309
-		return substr( $language, 0, 2 );
310
-	}
311
-
312
-	/**
313
-	 * @param string $value WordLift's language code.
314
-	 *
315
-	 * @see https://github.com/insideout10/wordlift-plugin/issues/1466
316
-	 *
317
-	 * Set WordLift's language code, used when storing strings to the Linked Data dataset.
318
-	 *
319
-	 * @deprecated As of 3.32.7 this below method has no effect on setting the language, we use the
320
-	 * language code form WordPress directly.
321
-	 *
322
-	 * @since 3.9.0
323
-	 */
324
-	public function set_language_code( $value ) {
325
-
326
-		$this->set( 'wl_general_settings', self::LANGUAGE, $value );
327
-
328
-	}
329
-
330
-	/**
331
-	 * Set the user preferences about sharing diagnostic with us.
332
-	 *
333
-	 * @param string $value The user preferences(yes/no).
334
-	 *
335
-	 * @since 3.19.0
336
-	 */
337
-	public function set_diagnostic_preferences( $value ) {
338
-
339
-		$this->set( 'wl_general_settings', self::SEND_DIAGNOSTIC, $value );
340
-
341
-	}
342
-
343
-	/**
344
-	 * Get the user preferences about sharing diagnostic.
345
-	 *
346
-	 * @since 3.19.0
347
-	 */
348
-	public function get_diagnostic_preferences() {
349
-
350
-		return $this->get( 'wl_general_settings', self::SEND_DIAGNOSTIC, 'no' );
351
-	}
352
-
353
-	/**
354
-	 * Get WordLift's configured country code, by default 'us'.
355
-	 *
356
-	 * @return string WordLift's configured country code ('us' by default).
357
-	 * @since 3.18.0
358
-	 */
359
-	public function get_country_code() {
360
-
361
-		return $this->get( 'wl_general_settings', self::COUNTRY_CODE, 'us' );
362
-	}
363
-
364
-	/**
365
-	 * Set WordLift's country code.
366
-	 *
367
-	 * @param string $value WordLift's country code.
368
-	 *
369
-	 * @since 3.18.0
370
-	 */
371
-	public function set_country_code( $value ) {
372
-
373
-		$this->set( 'wl_general_settings', self::COUNTRY_CODE, $value );
374
-
375
-	}
376
-
377
-	/**
378
-	 * Get the publisher entity post id.
379
-	 *
380
-	 * The publisher entity post id points to an entity post which contains the data for the publisher used in schema.org
381
-	 * Article markup.
382
-	 *
383
-	 * @return int|NULL The publisher entity post id or NULL if not set.
384
-	 * @since 3.9.0
385
-	 */
386
-	public function get_publisher_id() {
387
-
388
-		return $this->get( 'wl_general_settings', self::PUBLISHER_ID, null );
389
-	}
390
-
391
-	/**
392
-	 * Set the publisher entity post id.
393
-	 *
394
-	 * @param int $value The publisher entity post id.
395
-	 *
396
-	 * @since 3.9.0
397
-	 */
398
-	public function set_publisher_id( $value ) {
399
-
400
-		$this->set( 'wl_general_settings', self::PUBLISHER_ID, $value );
401
-
402
-	}
403
-
404
-	/**
405
-	 * Get the dataset URI.
406
-	 *
407
-	 * @return string The dataset URI or an empty string if not set.
408
-	 * @since 3.10.0
409
-	 * @since 3.27.7 Always return null if `wl_features__enable__dataset` is disabled.
410
-	 */
411
-	public function get_dataset_uri() {
412
-
413
-		if ( apply_filters( 'wl_feature__enable__dataset', true ) ) {
414
-			return $this->get( 'wl_advanced_settings', self::DATASET_URI, null );
415
-		} else {
416
-			return null;
417
-		}
418
-	}
419
-
420
-	/**
421
-	 * Set the dataset URI.
422
-	 *
423
-	 * @param string $value The dataset URI.
424
-	 *
425
-	 * @since 3.10.0
426
-	 */
427
-	public function set_dataset_uri( $value ) {
428
-
429
-		$this->set( 'wl_advanced_settings', self::DATASET_URI, $value );
430
-	}
431
-
432
-	/**
433
-	 * Get the package type.
434
-	 *
435
-	 * @return string The package type or an empty string if not set.
436
-	 * @since 3.20.0
437
-	 */
438
-	public function get_package_type() {
439
-
440
-		return $this->get( 'wl_advanced_settings', self::PACKAGE_TYPE, null );
441
-	}
442
-
443
-	/**
444
-	 * Set the package type.
445
-	 *
446
-	 * @param string $value The package type.
447
-	 *
448
-	 * @since 3.20.0
449
-	 */
450
-	public function set_package_type( $value ) {
451
-		$this->set( 'wl_advanced_settings', self::PACKAGE_TYPE, $value );
452
-	}
453
-
454
-	/**
455
-	 * Intercept the change of the WordLift key in order to set the dataset URI.
456
-	 *
457
-	 * @since 3.20.0 as of #761, we save settings every time a key is set, not only when the key changes, so to
458
-	 *               store the configuration parameters such as country or language.
459
-	 * @since 3.11.0
460
-	 *
461
-	 * @see https://github.com/insideout10/wordlift-plugin/issues/761
462
-	 *
463
-	 * @param array $old_value The old settings.
464
-	 * @param array $new_value The new settings.
465
-	 */
466
-	public function update_key( $old_value, $new_value ) {
467
-
468
-		// Check the old key value and the new one. We're going to ask for the dataset URI only if the key has changed.
469
-		// $old_key = isset( $old_value['key'] ) ? $old_value['key'] : '';
470
-		$new_key = isset( $new_value['key'] ) ? $new_value['key'] : '';
471
-
472
-		// If the key hasn't changed, don't do anything.
473
-		// WARN The 'update_option' hook is fired only if the new and old value are not equal.
474
-		// if ( $old_key === $new_key ) {
475
-		// return;
476
-		// }
477
-
478
-		// If the key is empty, empty the dataset URI.
479
-		if ( '' === $new_key ) {
480
-			$this->set_dataset_uri( '' );
481
-		}
482
-
483
-		// make the request to the remote server.
484
-		$this->get_remote_dataset_uri( $new_key );
485
-
486
-		do_action( 'wl_key_updated' );
487
-
488
-	}
489
-
490
-	/**
491
-	 * Handle retrieving the dataset uri from the remote server.
492
-	 *
493
-	 * If a valid dataset uri is returned it is stored in the appropriate option,
494
-	 * otherwise the option is set to empty string.
495
-	 *
496
-	 * @param string $key The key to be used.
497
-	 *
498
-	 * @since 3.12.0
499
-	 *
500
-	 * @since 3.17.0 send the site URL and get the dataset URI.
501
-	 */
502
-	public function get_remote_dataset_uri( $key ) {
503
-
504
-		$this->log->trace( 'Getting the remote dataset URI and package type...' );
505
-
506
-		if ( empty( $key ) ) {
507
-			$this->log->warn( 'Key set to empty value.' );
508
-
509
-			$this->set_dataset_uri( '' );
510
-			$this->set_package_type( null );
511
-
512
-			return;
513
-		}
514
-
515
-		/**
516
-		 * Allow 3rd parties to change the site_url.
517
-		 *
518
-		 * @param string $site_url The site url.
519
-		 *
520
-		 * @see https://github.com/insideout10/wordlift-plugin/issues/850
521
-		 *
522
-		 * @since 3.20.0
523
-		 */
524
-		$home_url = get_option( 'home' );
525
-		$site_url = apply_filters( 'wl_production_site_url', untrailingslashit( $home_url ) );
526
-
527
-		// Build the URL.
528
-		$url = '/accounts'
529
-			   . '?key=' . rawurlencode( $key )
530
-			   . '&url=' . rawurlencode( $site_url )
531
-			   . '&country=' . $this->get_country_code()
532
-			   . '&language=' . $this->get_language_code();
533
-
534
-		$api_service = Default_Api_Service::get_instance();
535
-		/**
536
-		 * @since 3.27.7.1
537
-		 * The Key should be passed to headers, otherwise api would return null.
538
-		 */
539
-		$headers  = array(
540
-			'Authorization' => "Key $key",
541
-		);
542
-		$response = $api_service->request( 'PUT', $url, $headers )->get_response();
543
-
544
-		// The response is an error.
545
-		if ( is_wp_error( $response ) ) {
546
-			$this->log->error( 'An error occurred setting the dataset URI: ' . $response->get_error_message() );
547
-
548
-			$this->set_dataset_uri( '' );
549
-			$this->set_package_type( null );
550
-
551
-			return;
552
-		}
553
-
554
-		// The response is not OK.
555
-		if ( ! is_array( $response ) || 200 !== (int) $response['response']['code'] ) {
556
-			$base_url = $api_service->get_base_url();
557
-
558
-			if ( ! is_array( $response ) ) {
559
-				// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export
560
-				$this->log->error( "Unexpected response when opening URL $base_url$url: " . var_export( $response, true ) );
561
-			} else {
562
-				// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export
563
-				$this->log->error( "Unexpected status code when opening URL $base_url$url: " . $response['response']['code'] . "\n" . var_export( $response, true ) );
564
-			}
565
-
566
-			$this->set_dataset_uri( '' );
567
-			$this->set_package_type( null );
568
-
569
-			return;
570
-		}
571
-
572
-		/*
26
+    /**
27
+     * The entity base path option name.
28
+     *
29
+     * @since 3.6.0
30
+     */
31
+    const ENTITY_BASE_PATH_KEY = 'wl_entity_base_path';
32
+
33
+    /**
34
+     * The skip wizard (admin installation wizard) option name.
35
+     *
36
+     * @since 3.9.0
37
+     */
38
+    const SKIP_WIZARD = 'wl_skip_wizard';
39
+
40
+    /**
41
+     * WordLift's key option name.
42
+     *
43
+     * @since 3.9.0
44
+     */
45
+    const KEY = 'key';
46
+
47
+    /**
48
+     * WordLift's configured language option name.
49
+     *
50
+     * @since 3.9.0
51
+     */
52
+    const LANGUAGE = 'site_language';
53
+
54
+    /**
55
+     * WordLift's configured country code.
56
+     *
57
+     * @since 3.18.0
58
+     */
59
+    const COUNTRY_CODE = 'country_code';
60
+
61
+    /**
62
+     * The publisher entity post ID option name.
63
+     *
64
+     * @since 3.9.0
65
+     */
66
+    const PUBLISHER_ID = 'publisher_id';
67
+
68
+    /**
69
+     * The dataset URI option name
70
+     *
71
+     * @since 3.10.0
72
+     */
73
+    const DATASET_URI = 'redlink_dataset_uri';
74
+
75
+    /**
76
+     * The link by default option name.
77
+     *
78
+     * @since 3.11.0
79
+     */
80
+    const LINK_BY_DEFAULT = 'link_by_default';
81
+
82
+    /**
83
+     * The analytics enable option.
84
+     *
85
+     * @since 3.21.0
86
+     */
87
+    const ANALYTICS_ENABLE = 'analytics_enable';
88
+
89
+    /**
90
+     * The analytics entity uri dimension option.
91
+     *
92
+     * @since 3.21.0
93
+     */
94
+    const ANALYTICS_ENTITY_URI_DIMENSION = 'analytics_entity_uri_dimension';
95
+
96
+    /**
97
+     * The analytics entity type dimension option.
98
+     *
99
+     * @since 3.21.0
100
+     */
101
+    const ANALYTICS_ENTITY_TYPE_DIMENSION = 'analytics_entity_type_dimension';
102
+
103
+    /**
104
+     * The user preferences about sharing data option.
105
+     *
106
+     * @since 3.19.0
107
+     */
108
+    const SEND_DIAGNOSTIC = 'send_diagnostic';
109
+
110
+    /**
111
+     * The package type configuration key.
112
+     *
113
+     * @since 3.20.0
114
+     */
115
+    const PACKAGE_TYPE = 'package_type';
116
+    /**
117
+     * The dataset ids connected to the current key
118
+     *
119
+     * @since 3.38.6
120
+     */
121
+    const NETWORK_DATASET_IDS = 'network_dataset_ids';
122
+
123
+    /**
124
+     * The {@link Wordlift_Log_Service} instance.
125
+     *
126
+     * @since 3.16.0
127
+     *
128
+     * @var \Wordlift_Log_Service $log The {@link Wordlift_Log_Service} instance.
129
+     */
130
+    private $log;
131
+
132
+    /**
133
+     * Create a Wordlift_Configuration_Service's instance.
134
+     *
135
+     * @since 3.6.0
136
+     */
137
+    protected function __construct() {
138
+
139
+        $this->log = Wordlift_Log_Service::get_logger( get_class() );
140
+
141
+        // Sync some configuration properties when key is validated.
142
+        add_action( 'wl_key_validation_response', array( $this, 'sync' ) );
143
+
144
+    }
145
+
146
+    /**
147
+     * @param $response \Wordlift\Api\Response
148
+     *
149
+     * @return void
150
+     */
151
+    public function sync( $response ) {
152
+        if ( ! $response->is_success() ) {
153
+            return;
154
+        }
155
+        $data = json_decode( $response->get_body(), true );
156
+        if ( ! is_array( $data ) || ! array_key_exists( 'networkDatasetId', $data ) ) {
157
+            return;
158
+        }
159
+        $this->set_network_dataset_ids( $data['networkDatasetId'] );
160
+    }
161
+
162
+    /**
163
+     * The Wordlift_Configuration_Service's singleton instance.
164
+     *
165
+     * @since  3.6.0
166
+     *
167
+     * @access private
168
+     * @var \Wordlift_Configuration_Service $instance Wordlift_Configuration_Service's singleton instance.
169
+     */
170
+    private static $instance = null;
171
+
172
+    /**
173
+     * Get the singleton instance.
174
+     *
175
+     * @return \Wordlift_Configuration_Service
176
+     * @since 3.6.0
177
+     */
178
+    public static function get_instance() {
179
+
180
+        if ( ! isset( self::$instance ) ) {
181
+            self::$instance = new self();
182
+        }
183
+
184
+        return self::$instance;
185
+    }
186
+
187
+    /**
188
+     * Get a configuration given the option name and a key. The option value is
189
+     * expected to be an array.
190
+     *
191
+     * @param string $option The option name.
192
+     * @param string $key A key in the option value array.
193
+     * @param string $default The default value in case the key is not found (by default an empty string).
194
+     *
195
+     * @return mixed The configuration value or the default value if not found.
196
+     * @since 3.6.0
197
+     */
198
+    private function get( $option, $key, $default = '' ) {
199
+
200
+        $options = get_option( $option, array() );
201
+
202
+        return isset( $options[ $key ] ) ? $options[ $key ] : $default;
203
+    }
204
+
205
+    /**
206
+     * Set a configuration parameter.
207
+     *
208
+     * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
209
+     * @param string $key The value key.
210
+     * @param mixed  $value The value.
211
+     *
212
+     * @since 3.9.0
213
+     */
214
+    private function set( $option, $key, $value ) {
215
+
216
+        $values         = get_option( $option );
217
+        $values         = isset( $values ) ? $values : array();
218
+        $values[ $key ] = $value;
219
+        update_option( $option, $values );
220
+
221
+    }
222
+
223
+    /**
224
+     * Get the entity base path, by default 'entity'.
225
+     *
226
+     * @return string The entity base path.
227
+     * @since 3.6.0
228
+     */
229
+    public function get_entity_base_path() {
230
+
231
+        return $this->get( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, 'entity' );
232
+    }
233
+
234
+    /**
235
+     * Get the entity base path.
236
+     *
237
+     * @param string $value The entity base path.
238
+     *
239
+     * @since 3.9.0
240
+     */
241
+    public function set_entity_base_path( $value ) {
242
+
243
+        $this->set( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, $value );
244
+
245
+    }
246
+
247
+    /**
248
+     * Whether the installation skip wizard should be skipped.
249
+     *
250
+     * @return bool True if it should be skipped otherwise false.
251
+     * @since 3.9.0
252
+     */
253
+    public function is_skip_wizard() {
254
+
255
+        return $this->get( 'wl_general_settings', self::SKIP_WIZARD, false );
256
+    }
257
+
258
+    /**
259
+     * Set the skip wizard parameter.
260
+     *
261
+     * @param bool $value True to skip the wizard. We expect a boolean value.
262
+     *
263
+     * @since 3.9.0
264
+     */
265
+    public function set_skip_wizard( $value ) {
266
+
267
+        $this->set( 'wl_general_settings', self::SKIP_WIZARD, true === $value );
268
+
269
+    }
270
+
271
+    /**
272
+     * Get WordLift's key.
273
+     *
274
+     * @return string WordLift's key or an empty string if not set.
275
+     * @since 3.9.0
276
+     */
277
+    public function get_key() {
278
+
279
+        return $this->get( 'wl_general_settings', self::KEY, '' );
280
+    }
281
+
282
+    /**
283
+     * Set WordLift's key.
284
+     *
285
+     * @param string $value WordLift's key.
286
+     *
287
+     * @since 3.9.0
288
+     */
289
+    public function set_key( $value ) {
290
+
291
+        $this->set( 'wl_general_settings', self::KEY, $value );
292
+    }
293
+
294
+    /**
295
+     * Get WordLift's configured language, by default 'en'.
296
+     *
297
+     * Note that WordLift's language is used when writing strings to the Linked Data dataset, not for the analysis.
298
+     *
299
+     * @return string WordLift's configured language code ('en' by default).
300
+     * @since 3.9.0
301
+     */
302
+    public function get_language_code() {
303
+
304
+        $language = get_locale();
305
+        if ( ! $language ) {
306
+            return 'en';
307
+        }
308
+
309
+        return substr( $language, 0, 2 );
310
+    }
311
+
312
+    /**
313
+     * @param string $value WordLift's language code.
314
+     *
315
+     * @see https://github.com/insideout10/wordlift-plugin/issues/1466
316
+     *
317
+     * Set WordLift's language code, used when storing strings to the Linked Data dataset.
318
+     *
319
+     * @deprecated As of 3.32.7 this below method has no effect on setting the language, we use the
320
+     * language code form WordPress directly.
321
+     *
322
+     * @since 3.9.0
323
+     */
324
+    public function set_language_code( $value ) {
325
+
326
+        $this->set( 'wl_general_settings', self::LANGUAGE, $value );
327
+
328
+    }
329
+
330
+    /**
331
+     * Set the user preferences about sharing diagnostic with us.
332
+     *
333
+     * @param string $value The user preferences(yes/no).
334
+     *
335
+     * @since 3.19.0
336
+     */
337
+    public function set_diagnostic_preferences( $value ) {
338
+
339
+        $this->set( 'wl_general_settings', self::SEND_DIAGNOSTIC, $value );
340
+
341
+    }
342
+
343
+    /**
344
+     * Get the user preferences about sharing diagnostic.
345
+     *
346
+     * @since 3.19.0
347
+     */
348
+    public function get_diagnostic_preferences() {
349
+
350
+        return $this->get( 'wl_general_settings', self::SEND_DIAGNOSTIC, 'no' );
351
+    }
352
+
353
+    /**
354
+     * Get WordLift's configured country code, by default 'us'.
355
+     *
356
+     * @return string WordLift's configured country code ('us' by default).
357
+     * @since 3.18.0
358
+     */
359
+    public function get_country_code() {
360
+
361
+        return $this->get( 'wl_general_settings', self::COUNTRY_CODE, 'us' );
362
+    }
363
+
364
+    /**
365
+     * Set WordLift's country code.
366
+     *
367
+     * @param string $value WordLift's country code.
368
+     *
369
+     * @since 3.18.0
370
+     */
371
+    public function set_country_code( $value ) {
372
+
373
+        $this->set( 'wl_general_settings', self::COUNTRY_CODE, $value );
374
+
375
+    }
376
+
377
+    /**
378
+     * Get the publisher entity post id.
379
+     *
380
+     * The publisher entity post id points to an entity post which contains the data for the publisher used in schema.org
381
+     * Article markup.
382
+     *
383
+     * @return int|NULL The publisher entity post id or NULL if not set.
384
+     * @since 3.9.0
385
+     */
386
+    public function get_publisher_id() {
387
+
388
+        return $this->get( 'wl_general_settings', self::PUBLISHER_ID, null );
389
+    }
390
+
391
+    /**
392
+     * Set the publisher entity post id.
393
+     *
394
+     * @param int $value The publisher entity post id.
395
+     *
396
+     * @since 3.9.0
397
+     */
398
+    public function set_publisher_id( $value ) {
399
+
400
+        $this->set( 'wl_general_settings', self::PUBLISHER_ID, $value );
401
+
402
+    }
403
+
404
+    /**
405
+     * Get the dataset URI.
406
+     *
407
+     * @return string The dataset URI or an empty string if not set.
408
+     * @since 3.10.0
409
+     * @since 3.27.7 Always return null if `wl_features__enable__dataset` is disabled.
410
+     */
411
+    public function get_dataset_uri() {
412
+
413
+        if ( apply_filters( 'wl_feature__enable__dataset', true ) ) {
414
+            return $this->get( 'wl_advanced_settings', self::DATASET_URI, null );
415
+        } else {
416
+            return null;
417
+        }
418
+    }
419
+
420
+    /**
421
+     * Set the dataset URI.
422
+     *
423
+     * @param string $value The dataset URI.
424
+     *
425
+     * @since 3.10.0
426
+     */
427
+    public function set_dataset_uri( $value ) {
428
+
429
+        $this->set( 'wl_advanced_settings', self::DATASET_URI, $value );
430
+    }
431
+
432
+    /**
433
+     * Get the package type.
434
+     *
435
+     * @return string The package type or an empty string if not set.
436
+     * @since 3.20.0
437
+     */
438
+    public function get_package_type() {
439
+
440
+        return $this->get( 'wl_advanced_settings', self::PACKAGE_TYPE, null );
441
+    }
442
+
443
+    /**
444
+     * Set the package type.
445
+     *
446
+     * @param string $value The package type.
447
+     *
448
+     * @since 3.20.0
449
+     */
450
+    public function set_package_type( $value ) {
451
+        $this->set( 'wl_advanced_settings', self::PACKAGE_TYPE, $value );
452
+    }
453
+
454
+    /**
455
+     * Intercept the change of the WordLift key in order to set the dataset URI.
456
+     *
457
+     * @since 3.20.0 as of #761, we save settings every time a key is set, not only when the key changes, so to
458
+     *               store the configuration parameters such as country or language.
459
+     * @since 3.11.0
460
+     *
461
+     * @see https://github.com/insideout10/wordlift-plugin/issues/761
462
+     *
463
+     * @param array $old_value The old settings.
464
+     * @param array $new_value The new settings.
465
+     */
466
+    public function update_key( $old_value, $new_value ) {
467
+
468
+        // Check the old key value and the new one. We're going to ask for the dataset URI only if the key has changed.
469
+        // $old_key = isset( $old_value['key'] ) ? $old_value['key'] : '';
470
+        $new_key = isset( $new_value['key'] ) ? $new_value['key'] : '';
471
+
472
+        // If the key hasn't changed, don't do anything.
473
+        // WARN The 'update_option' hook is fired only if the new and old value are not equal.
474
+        // if ( $old_key === $new_key ) {
475
+        // return;
476
+        // }
477
+
478
+        // If the key is empty, empty the dataset URI.
479
+        if ( '' === $new_key ) {
480
+            $this->set_dataset_uri( '' );
481
+        }
482
+
483
+        // make the request to the remote server.
484
+        $this->get_remote_dataset_uri( $new_key );
485
+
486
+        do_action( 'wl_key_updated' );
487
+
488
+    }
489
+
490
+    /**
491
+     * Handle retrieving the dataset uri from the remote server.
492
+     *
493
+     * If a valid dataset uri is returned it is stored in the appropriate option,
494
+     * otherwise the option is set to empty string.
495
+     *
496
+     * @param string $key The key to be used.
497
+     *
498
+     * @since 3.12.0
499
+     *
500
+     * @since 3.17.0 send the site URL and get the dataset URI.
501
+     */
502
+    public function get_remote_dataset_uri( $key ) {
503
+
504
+        $this->log->trace( 'Getting the remote dataset URI and package type...' );
505
+
506
+        if ( empty( $key ) ) {
507
+            $this->log->warn( 'Key set to empty value.' );
508
+
509
+            $this->set_dataset_uri( '' );
510
+            $this->set_package_type( null );
511
+
512
+            return;
513
+        }
514
+
515
+        /**
516
+         * Allow 3rd parties to change the site_url.
517
+         *
518
+         * @param string $site_url The site url.
519
+         *
520
+         * @see https://github.com/insideout10/wordlift-plugin/issues/850
521
+         *
522
+         * @since 3.20.0
523
+         */
524
+        $home_url = get_option( 'home' );
525
+        $site_url = apply_filters( 'wl_production_site_url', untrailingslashit( $home_url ) );
526
+
527
+        // Build the URL.
528
+        $url = '/accounts'
529
+                . '?key=' . rawurlencode( $key )
530
+                . '&url=' . rawurlencode( $site_url )
531
+                . '&country=' . $this->get_country_code()
532
+                . '&language=' . $this->get_language_code();
533
+
534
+        $api_service = Default_Api_Service::get_instance();
535
+        /**
536
+         * @since 3.27.7.1
537
+         * The Key should be passed to headers, otherwise api would return null.
538
+         */
539
+        $headers  = array(
540
+            'Authorization' => "Key $key",
541
+        );
542
+        $response = $api_service->request( 'PUT', $url, $headers )->get_response();
543
+
544
+        // The response is an error.
545
+        if ( is_wp_error( $response ) ) {
546
+            $this->log->error( 'An error occurred setting the dataset URI: ' . $response->get_error_message() );
547
+
548
+            $this->set_dataset_uri( '' );
549
+            $this->set_package_type( null );
550
+
551
+            return;
552
+        }
553
+
554
+        // The response is not OK.
555
+        if ( ! is_array( $response ) || 200 !== (int) $response['response']['code'] ) {
556
+            $base_url = $api_service->get_base_url();
557
+
558
+            if ( ! is_array( $response ) ) {
559
+                // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export
560
+                $this->log->error( "Unexpected response when opening URL $base_url$url: " . var_export( $response, true ) );
561
+            } else {
562
+                // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export
563
+                $this->log->error( "Unexpected status code when opening URL $base_url$url: " . $response['response']['code'] . "\n" . var_export( $response, true ) );
564
+            }
565
+
566
+            $this->set_dataset_uri( '' );
567
+            $this->set_package_type( null );
568
+
569
+            return;
570
+        }
571
+
572
+        /*
573 573
 		 * We also store the package type.
574 574
 		 *
575 575
 		 * @since 3.20.0
576 576
 		 */
577
-		$json = json_decode( $response['body'] );
578
-		/**
579
-		 * @since 3.27.7
580
-		 * Remove the trailing slash returned from the new platform api.
581
-		 */
582
-		// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
583
-		$dataset_uri = untrailingslashit( $json->datasetURI );
584
-		// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
585
-		$package_type = isset( $json->packageType ) ? $json->packageType : null;
586
-
587
-		$this->log->info( "Updating [ dataset uri :: $dataset_uri ][ package type :: $package_type ]..." );
588
-
589
-		$this->set_dataset_uri( $dataset_uri );
590
-		$this->set_package_type( $package_type );
591
-	}
592
-
593
-	/**
594
-	 * Handle the edge case where a user submits the same key again
595
-	 * when he does not have the dataset uri to regain it.
596
-	 *
597
-	 * This can not be handled in the normal option update hook because
598
-	 * it is not being triggered when the save value equals to the one already
599
-	 * in the DB.
600
-	 *
601
-	 * @param mixed $value The new, unserialized option value.
602
-	 * @param mixed $old_value The old option value.
603
-	 *
604
-	 * @return mixed The same value in the $value parameter
605
-	 * @since 3.12.0
606
-	 */
607
-	public function maybe_update_dataset_uri( $value, $old_value ) {
608
-
609
-		// Check the old key value and the new one. Here we're only handling the
610
-		// case where the key hasn't changed and the dataset URI isn't set. The
611
-		// other case, i.e. a new key is inserted, is handled at `update_key`.
612
-		$old_key = isset( $old_value['key'] ) ? $old_value['key'] : '';
613
-		$new_key = isset( $value['key'] ) ? $value['key'] : '';
614
-
615
-		$dataset_uri = $this->get_dataset_uri();
616
-
617
-		if ( ! empty( $new_key ) && $new_key === $old_key && empty( $dataset_uri ) ) {
618
-
619
-			// make the request to the remote server to try to get the dataset uri.
620
-			$this->get_remote_dataset_uri( $new_key );
621
-		}
622
-
623
-		return $value;
624
-	}
625
-
626
-	/**
627
-	 * Get the API URI to retrieve the dataset URI using the WordLift Key.
628
-	 *
629
-	 * @param string $key The WordLift key to use.
630
-	 *
631
-	 * @return string The API URI.
632
-	 * @since 3.11.0
633
-	 */
634
-	public function get_accounts_by_key_dataset_uri( $key ) {
635
-
636
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . "accounts/key=$key/dataset_uri";
637
-	}
638
-
639
-	/**
640
-	 * Get the `accounts` end point.
641
-	 *
642
-	 * @return string The `accounts` end point.
643
-	 * @since 3.16.0
644
-	 */
645
-	public function get_accounts() {
646
-
647
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'accounts';
648
-	}
649
-
650
-	/**
651
-	 * Get the `link by default` option.
652
-	 *
653
-	 * @return bool True if entities must be linked by default otherwise false.
654
-	 * @since 3.13.0
655
-	 */
656
-	public function is_link_by_default() {
657
-
658
-		return 'yes' === $this->get( 'wl_general_settings', self::LINK_BY_DEFAULT, 'yes' );
659
-	}
660
-
661
-	/**
662
-	 * Set the `link by default` option.
663
-	 *
664
-	 * @param bool $value True to enabling linking by default, otherwise false.
665
-	 *
666
-	 * @since 3.13.0
667
-	 */
668
-	public function set_link_by_default( $value ) {
669
-
670
-		$this->set( 'wl_general_settings', self::LINK_BY_DEFAULT, true === $value ? 'yes' : 'no' );
671
-	}
672
-
673
-	/**
674
-	 * Get the 'analytics-enable' option.
675
-	 *
676
-	 * @return string 'no' or 'yes' representing bool.
677
-	 * @since 3.21.0
678
-	 */
679
-	public function is_analytics_enable() {
680
-		return 'yes' === $this->get( 'wl_analytics_settings', self::ANALYTICS_ENABLE, 'no' );
681
-	}
682
-
683
-	/**
684
-	 * Set the `analytics-enable` option.
685
-	 *
686
-	 * @param bool $value True to enabling analytics, otherwise false.
687
-	 *
688
-	 * @since 3.21.0
689
-	 */
690
-	public function set_is_analytics_enable( $value ) {
691
-
692
-		$this->set( 'wl_general_settings', self::ANALYTICS_ENABLE, true === $value ? 'yes' : 'no' );
693
-	}
694
-
695
-	/**
696
-	 * Get the 'analytics-entity-uri-dimention' option.
697
-	 *
698
-	 * @return int
699
-	 * @since 3.21.0
700
-	 */
701
-	public function get_analytics_entity_uri_dimension() {
702
-		return (int) $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_URI_DIMENSION, 1 );
703
-	}
704
-
705
-	/**
706
-	 * Get the 'analytics-entity-type-dimension' option.
707
-	 *
708
-	 * @return int
709
-	 * @since 3.21.0
710
-	 */
711
-	public function get_analytics_entity_type_dimension() {
712
-		return $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_TYPE_DIMENSION, 2 );
713
-	}
714
-
715
-	/**
716
-	 * Get the URL to perform autocomplete request.
717
-	 *
718
-	 * @return string The URL to call to perform the autocomplete request.
719
-	 * @since 3.15.0
720
-	 */
721
-	public function get_autocomplete_url() {
722
-
723
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'autocomplete';
724
-
725
-	}
726
-
727
-	/**
728
-	 * Get the URL to perform feedback deactivation request.
729
-	 *
730
-	 * @return string The URL to call to perform the feedback deactivation request.
731
-	 * @since 3.19.0
732
-	 */
733
-	public function get_deactivation_feedback_url() {
734
-
735
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'feedbacks';
736
-
737
-	}
738
-
739
-	/**
740
-	 * Get the base API URL.
741
-	 *
742
-	 * @return string The base API URL.
743
-	 * @since 3.20.0
744
-	 */
745
-	public function get_api_url() {
746
-
747
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE;
748
-	}
749
-
750
-	public function get_network_dataset_ids() {
751
-		return $this->get( 'wl_advanced_settings', self::NETWORK_DATASET_IDS, array() );
752
-	}
753
-
754
-	public function set_network_dataset_ids( $network_dataset_ids ) {
755
-		$this->set( 'wl_advanced_settings', self::NETWORK_DATASET_IDS, $network_dataset_ids );
756
-	}
577
+        $json = json_decode( $response['body'] );
578
+        /**
579
+         * @since 3.27.7
580
+         * Remove the trailing slash returned from the new platform api.
581
+         */
582
+        // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
583
+        $dataset_uri = untrailingslashit( $json->datasetURI );
584
+        // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
585
+        $package_type = isset( $json->packageType ) ? $json->packageType : null;
586
+
587
+        $this->log->info( "Updating [ dataset uri :: $dataset_uri ][ package type :: $package_type ]..." );
588
+
589
+        $this->set_dataset_uri( $dataset_uri );
590
+        $this->set_package_type( $package_type );
591
+    }
592
+
593
+    /**
594
+     * Handle the edge case where a user submits the same key again
595
+     * when he does not have the dataset uri to regain it.
596
+     *
597
+     * This can not be handled in the normal option update hook because
598
+     * it is not being triggered when the save value equals to the one already
599
+     * in the DB.
600
+     *
601
+     * @param mixed $value The new, unserialized option value.
602
+     * @param mixed $old_value The old option value.
603
+     *
604
+     * @return mixed The same value in the $value parameter
605
+     * @since 3.12.0
606
+     */
607
+    public function maybe_update_dataset_uri( $value, $old_value ) {
608
+
609
+        // Check the old key value and the new one. Here we're only handling the
610
+        // case where the key hasn't changed and the dataset URI isn't set. The
611
+        // other case, i.e. a new key is inserted, is handled at `update_key`.
612
+        $old_key = isset( $old_value['key'] ) ? $old_value['key'] : '';
613
+        $new_key = isset( $value['key'] ) ? $value['key'] : '';
614
+
615
+        $dataset_uri = $this->get_dataset_uri();
616
+
617
+        if ( ! empty( $new_key ) && $new_key === $old_key && empty( $dataset_uri ) ) {
618
+
619
+            // make the request to the remote server to try to get the dataset uri.
620
+            $this->get_remote_dataset_uri( $new_key );
621
+        }
622
+
623
+        return $value;
624
+    }
625
+
626
+    /**
627
+     * Get the API URI to retrieve the dataset URI using the WordLift Key.
628
+     *
629
+     * @param string $key The WordLift key to use.
630
+     *
631
+     * @return string The API URI.
632
+     * @since 3.11.0
633
+     */
634
+    public function get_accounts_by_key_dataset_uri( $key ) {
635
+
636
+        return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . "accounts/key=$key/dataset_uri";
637
+    }
638
+
639
+    /**
640
+     * Get the `accounts` end point.
641
+     *
642
+     * @return string The `accounts` end point.
643
+     * @since 3.16.0
644
+     */
645
+    public function get_accounts() {
646
+
647
+        return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'accounts';
648
+    }
649
+
650
+    /**
651
+     * Get the `link by default` option.
652
+     *
653
+     * @return bool True if entities must be linked by default otherwise false.
654
+     * @since 3.13.0
655
+     */
656
+    public function is_link_by_default() {
657
+
658
+        return 'yes' === $this->get( 'wl_general_settings', self::LINK_BY_DEFAULT, 'yes' );
659
+    }
660
+
661
+    /**
662
+     * Set the `link by default` option.
663
+     *
664
+     * @param bool $value True to enabling linking by default, otherwise false.
665
+     *
666
+     * @since 3.13.0
667
+     */
668
+    public function set_link_by_default( $value ) {
669
+
670
+        $this->set( 'wl_general_settings', self::LINK_BY_DEFAULT, true === $value ? 'yes' : 'no' );
671
+    }
672
+
673
+    /**
674
+     * Get the 'analytics-enable' option.
675
+     *
676
+     * @return string 'no' or 'yes' representing bool.
677
+     * @since 3.21.0
678
+     */
679
+    public function is_analytics_enable() {
680
+        return 'yes' === $this->get( 'wl_analytics_settings', self::ANALYTICS_ENABLE, 'no' );
681
+    }
682
+
683
+    /**
684
+     * Set the `analytics-enable` option.
685
+     *
686
+     * @param bool $value True to enabling analytics, otherwise false.
687
+     *
688
+     * @since 3.21.0
689
+     */
690
+    public function set_is_analytics_enable( $value ) {
691
+
692
+        $this->set( 'wl_general_settings', self::ANALYTICS_ENABLE, true === $value ? 'yes' : 'no' );
693
+    }
694
+
695
+    /**
696
+     * Get the 'analytics-entity-uri-dimention' option.
697
+     *
698
+     * @return int
699
+     * @since 3.21.0
700
+     */
701
+    public function get_analytics_entity_uri_dimension() {
702
+        return (int) $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_URI_DIMENSION, 1 );
703
+    }
704
+
705
+    /**
706
+     * Get the 'analytics-entity-type-dimension' option.
707
+     *
708
+     * @return int
709
+     * @since 3.21.0
710
+     */
711
+    public function get_analytics_entity_type_dimension() {
712
+        return $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_TYPE_DIMENSION, 2 );
713
+    }
714
+
715
+    /**
716
+     * Get the URL to perform autocomplete request.
717
+     *
718
+     * @return string The URL to call to perform the autocomplete request.
719
+     * @since 3.15.0
720
+     */
721
+    public function get_autocomplete_url() {
722
+
723
+        return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'autocomplete';
724
+
725
+    }
726
+
727
+    /**
728
+     * Get the URL to perform feedback deactivation request.
729
+     *
730
+     * @return string The URL to call to perform the feedback deactivation request.
731
+     * @since 3.19.0
732
+     */
733
+    public function get_deactivation_feedback_url() {
734
+
735
+        return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'feedbacks';
736
+
737
+    }
738
+
739
+    /**
740
+     * Get the base API URL.
741
+     *
742
+     * @return string The base API URL.
743
+     * @since 3.20.0
744
+     */
745
+    public function get_api_url() {
746
+
747
+        return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE;
748
+    }
749
+
750
+    public function get_network_dataset_ids() {
751
+        return $this->get( 'wl_advanced_settings', self::NETWORK_DATASET_IDS, array() );
752
+    }
753
+
754
+    public function set_network_dataset_ids( $network_dataset_ids ) {
755
+        $this->set( 'wl_advanced_settings', self::NETWORK_DATASET_IDS, $network_dataset_ids );
756
+    }
757 757
 
758 758
 }
Please login to merge, or discard this patch.
src/wordlift/vocabulary/class-analysis-service.php 1 patch
Indentation   +171 added lines, -171 removed lines patch added patch discarded remove patch
@@ -12,177 +12,177 @@
 block discarded – undo
12 12
  */
13 13
 class Analysis_Service {
14 14
 
15
-	/**
16
-	 * @var Default_Api_Service
17
-	 */
18
-	private $api_service;
19
-	/**
20
-	 * @var Cache
21
-	 */
22
-	private $cache_service;
23
-	/**
24
-	 * @var \Wordlift_Log_Service
25
-	 */
26
-	private $log;
27
-
28
-	/**
29
-	 * Tag_Rest_Endpoint constructor.
30
-	 *
31
-	 * @param Default_Api_Service $api_service
32
-	 * @param Cache               $cache_service
33
-	 */
34
-	public function __construct( $api_service, $cache_service ) {
35
-
36
-		$this->api_service = $api_service;
37
-
38
-		$this->cache_service = $cache_service;
39
-
40
-		$this->log = \Wordlift_Log_Service::get_logger( get_class() );
41
-
42
-	}
43
-
44
-	/**
45
-	 * Check if entities are in cache, if not return the results from
46
-	 * cache service.
47
-	 *
48
-	 * @param $tag \WP_Term
49
-	 */
50
-	public function get_entities( $tag ) {
51
-
52
-		$cache_key    = $tag->term_id;
53
-		$cache_result = $this->cache_service->get( $cache_key );
54
-		if ( false !== $cache_result ) {
55
-			return $cache_result;
56
-		}
57
-
58
-		// send the request.
59
-		$tag_name = $tag->name;
60
-
61
-		$entities = $this->get_entities_by_search_query( $tag_name );
62
-
63
-		if ( ! $entities ) {
64
-			return false;
65
-		}
66
-
67
-		$this->cache_service->put( $cache_key, $entities );
68
-
69
-		return $entities;
70
-
71
-	}
72
-
73
-	/**
74
-	 * @param $entity_url string
75
-	 * Formats the entity url from https://foo.com/some/path to
76
-	 * https/foo.com/some/path
77
-	 *
78
-	 * @return bool|string
79
-	 */
80
-	public static function format_entity_url( $entity_url ) {
81
-		$result = wp_parse_url( $entity_url );
82
-		if ( ! $result ) {
83
-			return false;
84
-		}
85
-		if ( ! array_key_exists( 'scheme', $result )
86
-			 || ! array_key_exists( 'host', $result )
87
-			 || ! array_key_exists( 'path', $result ) ) {
88
-			return false;
89
-		}
90
-
91
-		return $result['scheme'] . '/' . $result['host'] . $result['path'];
92
-	}
93
-
94
-	private function get_meta( $entity_url ) {
95
-
96
-		$cache_results = $this->cache_service->get( $entity_url );
97
-
98
-		if ( false !== $cache_results ) {
99
-			return $cache_results;
100
-		}
101
-
102
-		$formatted_url = self::format_entity_url( $entity_url );
103
-
104
-		if ( ! $formatted_url ) {
105
-			return array();
106
-		}
107
-
108
-		$meta_url = 'https://api.wordlift.io/id/' . $formatted_url;
109
-
110
-		$response = wp_remote_get( $meta_url );
111
-
112
-		if ( is_wp_error( $response )
113
-			 || ! isset( $response['response']['code'] )
114
-			 || 2 !== (int) $response['response']['code'] / 100 ) {
115
-			return false;
116
-		}
117
-
118
-		if ( ! is_wp_error( $response ) ) {
119
-			$meta = json_decode( wp_remote_retrieve_body( $response ), true );
120
-			$this->cache_service->put( $entity_url, $meta );
121
-
122
-			return $meta;
123
-		}
124
-
125
-		return array();
126
-
127
-	}
128
-
129
-	private function get_meta_for_entities( $entities ) {
130
-
131
-		$filtered_entities = array();
132
-		foreach ( $entities as $entity ) {
133
-			$entity['meta'] = array();
134
-			$meta           = $this->get_meta( $entity['entityId'] );
135
-			if ( $meta ) {
136
-				$meta                = Default_Entity_List::compact_jsonld( $meta );
137
-				$entity['meta']      = $meta;
138
-				$filtered_entities[] = $entity;
139
-			}
140
-		}
141
-
142
-		return $filtered_entities;
143
-
144
-	}
145
-
146
-	/**
147
-	 * @param $tag_name
148
-	 *
149
-	 * @return array
150
-	 */
151
-	public function get_entities_by_search_query( $tag_name ) {
152
-		$response = $this->api_service->request(
153
-			'POST',
154
-			'/analysis/single',
155
-			array( 'Content-Type' => 'application/json' ),
156
-			wp_json_encode(
157
-				array(
158
-					'content'         => $tag_name,
159
-					'contentType'     => 'text/plain',
160
-					'version'         => '1.0.0',
161
-					'contentLanguage' => 'en',
162
-					'scope'           => $this->get_scope(),
163
-				)
164
-			)
165
-		);
166
-
167
-		if ( ! $response->is_success() ) {
168
-			return false;
169
-		}
170
-
171
-		$response = json_decode( $response->get_body(), true );
172
-
173
-		if ( ! array_key_exists( 'entities', $response ) ) {
174
-			return false;
175
-		}
176
-
177
-		$entities = $this->get_meta_for_entities( $response['entities'] );
178
-
179
-		return $entities;
180
-	}
181
-
182
-	public function get_scope() {
183
-		$service = \Wordlift_Configuration_Service::get_instance();
15
+    /**
16
+     * @var Default_Api_Service
17
+     */
18
+    private $api_service;
19
+    /**
20
+     * @var Cache
21
+     */
22
+    private $cache_service;
23
+    /**
24
+     * @var \Wordlift_Log_Service
25
+     */
26
+    private $log;
27
+
28
+    /**
29
+     * Tag_Rest_Endpoint constructor.
30
+     *
31
+     * @param Default_Api_Service $api_service
32
+     * @param Cache               $cache_service
33
+     */
34
+    public function __construct( $api_service, $cache_service ) {
35
+
36
+        $this->api_service = $api_service;
37
+
38
+        $this->cache_service = $cache_service;
39
+
40
+        $this->log = \Wordlift_Log_Service::get_logger( get_class() );
41
+
42
+    }
43
+
44
+    /**
45
+     * Check if entities are in cache, if not return the results from
46
+     * cache service.
47
+     *
48
+     * @param $tag \WP_Term
49
+     */
50
+    public function get_entities( $tag ) {
51
+
52
+        $cache_key    = $tag->term_id;
53
+        $cache_result = $this->cache_service->get( $cache_key );
54
+        if ( false !== $cache_result ) {
55
+            return $cache_result;
56
+        }
57
+
58
+        // send the request.
59
+        $tag_name = $tag->name;
60
+
61
+        $entities = $this->get_entities_by_search_query( $tag_name );
62
+
63
+        if ( ! $entities ) {
64
+            return false;
65
+        }
66
+
67
+        $this->cache_service->put( $cache_key, $entities );
68
+
69
+        return $entities;
70
+
71
+    }
72
+
73
+    /**
74
+     * @param $entity_url string
75
+     * Formats the entity url from https://foo.com/some/path to
76
+     * https/foo.com/some/path
77
+     *
78
+     * @return bool|string
79
+     */
80
+    public static function format_entity_url( $entity_url ) {
81
+        $result = wp_parse_url( $entity_url );
82
+        if ( ! $result ) {
83
+            return false;
84
+        }
85
+        if ( ! array_key_exists( 'scheme', $result )
86
+             || ! array_key_exists( 'host', $result )
87
+             || ! array_key_exists( 'path', $result ) ) {
88
+            return false;
89
+        }
90
+
91
+        return $result['scheme'] . '/' . $result['host'] . $result['path'];
92
+    }
93
+
94
+    private function get_meta( $entity_url ) {
95
+
96
+        $cache_results = $this->cache_service->get( $entity_url );
97
+
98
+        if ( false !== $cache_results ) {
99
+            return $cache_results;
100
+        }
101
+
102
+        $formatted_url = self::format_entity_url( $entity_url );
103
+
104
+        if ( ! $formatted_url ) {
105
+            return array();
106
+        }
107
+
108
+        $meta_url = 'https://api.wordlift.io/id/' . $formatted_url;
109
+
110
+        $response = wp_remote_get( $meta_url );
111
+
112
+        if ( is_wp_error( $response )
113
+             || ! isset( $response['response']['code'] )
114
+             || 2 !== (int) $response['response']['code'] / 100 ) {
115
+            return false;
116
+        }
117
+
118
+        if ( ! is_wp_error( $response ) ) {
119
+            $meta = json_decode( wp_remote_retrieve_body( $response ), true );
120
+            $this->cache_service->put( $entity_url, $meta );
121
+
122
+            return $meta;
123
+        }
124
+
125
+        return array();
126
+
127
+    }
128
+
129
+    private function get_meta_for_entities( $entities ) {
130
+
131
+        $filtered_entities = array();
132
+        foreach ( $entities as $entity ) {
133
+            $entity['meta'] = array();
134
+            $meta           = $this->get_meta( $entity['entityId'] );
135
+            if ( $meta ) {
136
+                $meta                = Default_Entity_List::compact_jsonld( $meta );
137
+                $entity['meta']      = $meta;
138
+                $filtered_entities[] = $entity;
139
+            }
140
+        }
141
+
142
+        return $filtered_entities;
143
+
144
+    }
145
+
146
+    /**
147
+     * @param $tag_name
148
+     *
149
+     * @return array
150
+     */
151
+    public function get_entities_by_search_query( $tag_name ) {
152
+        $response = $this->api_service->request(
153
+            'POST',
154
+            '/analysis/single',
155
+            array( 'Content-Type' => 'application/json' ),
156
+            wp_json_encode(
157
+                array(
158
+                    'content'         => $tag_name,
159
+                    'contentType'     => 'text/plain',
160
+                    'version'         => '1.0.0',
161
+                    'contentLanguage' => 'en',
162
+                    'scope'           => $this->get_scope(),
163
+                )
164
+            )
165
+        );
166
+
167
+        if ( ! $response->is_success() ) {
168
+            return false;
169
+        }
170
+
171
+        $response = json_decode( $response->get_body(), true );
172
+
173
+        if ( ! array_key_exists( 'entities', $response ) ) {
174
+            return false;
175
+        }
176
+
177
+        $entities = $this->get_meta_for_entities( $response['entities'] );
178
+
179
+        return $entities;
180
+    }
181
+
182
+    public function get_scope() {
183
+        $service = \Wordlift_Configuration_Service::get_instance();
184 184
 
185
-		return count( $service->get_network_dataset_ids() ) > 0 ? 'network-only' : 'cloud';
186
-	}
185
+        return count( $service->get_network_dataset_ids() ) > 0 ? 'network-only' : 'cloud';
186
+    }
187 187
 
188 188
 }
Please login to merge, or discard this patch.