@@ -20,201 +20,201 @@ |
||
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 | } |
@@ -41,9 +41,9 @@ discard block |
||
41 | 41 | */ |
42 | 42 | public function __construct() { |
43 | 43 | |
44 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Key_Validation_Service' ); |
|
44 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Key_Validation_Service'); |
|
45 | 45 | |
46 | - add_action( 'admin_init', array( $this, 'wl_load_plugin' ) ); |
|
46 | + add_action('admin_init', array($this, 'wl_load_plugin')); |
|
47 | 47 | /** |
48 | 48 | * Filter: wl_feature__enable__notices. |
49 | 49 | * |
@@ -52,11 +52,11 @@ discard block |
||
52 | 52 | * @return bool |
53 | 53 | * @since 3.27.6 |
54 | 54 | */ |
55 | - if ( apply_filters( 'wl_feature__enable__notices', true ) ) { |
|
56 | - add_action( 'admin_notices', array( $this, 'wl_key_update_notice' ) ); |
|
55 | + if (apply_filters('wl_feature__enable__notices', true)) { |
|
56 | + add_action('admin_notices', array($this, 'wl_key_update_notice')); |
|
57 | 57 | } |
58 | 58 | |
59 | - $this->ttl_cache_service = new Ttl_Cache( 'key-validation-notification' ); |
|
59 | + $this->ttl_cache_service = new Ttl_Cache('key-validation-notification'); |
|
60 | 60 | |
61 | 61 | } |
62 | 62 | |
@@ -68,9 +68,9 @@ discard block |
||
68 | 68 | * @return WP_Error|array The response or WP_Error on failure. |
69 | 69 | * @since 3.9.0 |
70 | 70 | */ |
71 | - public function get_account_info( $key ) { |
|
71 | + public function get_account_info($key) { |
|
72 | 72 | |
73 | - $this->log->debug( 'Validating key...' ); |
|
73 | + $this->log->debug('Validating key...'); |
|
74 | 74 | |
75 | 75 | $response = Default_Api_Service::get_instance()->get( |
76 | 76 | '/accounts/info', |
@@ -84,7 +84,7 @@ discard block |
||
84 | 84 | * This action is fired when the key is validated. |
85 | 85 | * @param $response \Wordlift\Api\Response |
86 | 86 | */ |
87 | - do_action( 'wl_key_validation_response', $response ); |
|
87 | + do_action('wl_key_validation_response', $response); |
|
88 | 88 | |
89 | 89 | return $response->get_response(); |
90 | 90 | } |
@@ -96,21 +96,21 @@ discard block |
||
96 | 96 | * |
97 | 97 | * @return bool |
98 | 98 | */ |
99 | - public function is_key_valid( $key ) { |
|
99 | + public function is_key_valid($key) { |
|
100 | 100 | |
101 | - $response = $this->get_account_info( $key ); |
|
101 | + $response = $this->get_account_info($key); |
|
102 | 102 | |
103 | - if ( is_wp_error( $response ) || 2 !== (int) $response['response']['code'] / 100 ) { |
|
103 | + if (is_wp_error($response) || 2 !== (int) $response['response']['code'] / 100) { |
|
104 | 104 | return false; |
105 | 105 | } |
106 | - $res_body = json_decode( wp_remote_retrieve_body( $response ), true ); |
|
106 | + $res_body = json_decode(wp_remote_retrieve_body($response), true); |
|
107 | 107 | |
108 | 108 | $url = $res_body['url']; |
109 | 109 | |
110 | 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 ) { |
|
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 | 114 | return true; |
115 | 115 | } |
116 | 116 | |
@@ -128,14 +128,14 @@ discard block |
||
128 | 128 | ob_clean(); |
129 | 129 | |
130 | 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.' ); |
|
131 | + if ( ! isset($_POST['key'])) { //phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
132 | + wp_send_json_error('The key parameter is required.'); |
|
133 | 133 | } |
134 | 134 | |
135 | - $response = $this->get_account_info( sanitize_text_field( wp_unslash( (string) $_POST['key'] ) ) ); //phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
135 | + $response = $this->get_account_info(sanitize_text_field(wp_unslash((string) $_POST['key']))); //phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
136 | 136 | |
137 | 137 | // If we got an error, return invalid. |
138 | - if ( is_wp_error( $response ) || 2 !== (int) $response['response']['code'] / 100 ) { |
|
138 | + if (is_wp_error($response) || 2 !== (int) $response['response']['code'] / 100) { |
|
139 | 139 | wp_send_json_success( |
140 | 140 | array( |
141 | 141 | 'valid' => false, |
@@ -146,19 +146,19 @@ discard block |
||
146 | 146 | ); |
147 | 147 | } |
148 | 148 | |
149 | - $res_body = json_decode( wp_remote_retrieve_body( $response ), true ); |
|
149 | + $res_body = json_decode(wp_remote_retrieve_body($response), true); |
|
150 | 150 | |
151 | 151 | // The URL stored in WLS. If this is the initial install the URL may be null. |
152 | 152 | $url = $res_body['url']; |
153 | 153 | |
154 | 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 ) ); |
|
155 | + $home_url = get_option('home'); |
|
156 | + $site_url = apply_filters('wl_production_site_url', untrailingslashit($home_url)); |
|
157 | 157 | |
158 | 158 | // If the URL isn't set or matches, then it's valid. |
159 | - if ( $url === null || $url === $site_url ) { |
|
159 | + if ($url === null || $url === $site_url) { |
|
160 | 160 | // Invalidate the cache key |
161 | - $this->ttl_cache_service->delete( 'is_key_valid' ); |
|
161 | + $this->ttl_cache_service->delete('is_key_valid'); |
|
162 | 162 | wp_send_json_success( |
163 | 163 | array( |
164 | 164 | 'valid' => true, |
@@ -168,12 +168,12 @@ discard block |
||
168 | 168 | } |
169 | 169 | |
170 | 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( '' ); |
|
171 | + if ($url !== $site_url) { |
|
172 | + Wordlift_Configuration_Service::get_instance()->set_key(''); |
|
173 | 173 | wp_send_json_success( |
174 | 174 | array( |
175 | 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' ), |
|
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 | 177 | ) |
178 | 178 | ); |
179 | 179 | } |
@@ -182,7 +182,7 @@ discard block |
||
182 | 182 | wp_send_json_success( |
183 | 183 | array( |
184 | 184 | 'valid' => false, |
185 | - 'message' => __( 'An error occurred, please contact us at [email protected]', 'wordlift' ), |
|
185 | + 'message' => __('An error occurred, please contact us at [email protected]', 'wordlift'), |
|
186 | 186 | ) |
187 | 187 | ); |
188 | 188 | } |
@@ -192,15 +192,15 @@ discard block |
||
192 | 192 | */ |
193 | 193 | public function wl_load_plugin() { |
194 | 194 | |
195 | - $wl_blog_url = get_option( '_wl_blog_url' ); |
|
196 | - $home_url = get_option( 'home' ); |
|
195 | + $wl_blog_url = get_option('_wl_blog_url'); |
|
196 | + $home_url = get_option('home'); |
|
197 | 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 ); |
|
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 | 204 | } |
205 | 205 | |
206 | 206 | } |
@@ -209,10 +209,10 @@ discard block |
||
209 | 209 | * This function is hooked to the `admin_notices` to show admin notification. |
210 | 210 | */ |
211 | 211 | public function wl_key_update_notice() { |
212 | - if ( get_transient( 'wl-key-error-msg' ) ) { |
|
212 | + if (get_transient('wl-key-error-msg')) { |
|
213 | 213 | ?> |
214 | 214 | <div class="updated notice is-dismissible error"> |
215 | - <p><?php esc_html( get_transient( 'wl-key-error-msg' ) ); ?></p> |
|
215 | + <p><?php esc_html(get_transient('wl-key-error-msg')); ?></p> |
|
216 | 216 | </div> |
217 | 217 | <?php |
218 | 218 | } |
@@ -13,7 +13,7 @@ discard block |
||
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 |
||
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 | } |
@@ -12,7 +12,7 @@ discard block |
||
12 | 12 | |
13 | 13 | use Wordlift\Api\Default_Api_Service; |
14 | 14 | |
15 | -if ( ! defined( 'ABSPATH' ) ) { |
|
15 | +if ( ! defined('ABSPATH')) { |
|
16 | 16 | exit; |
17 | 17 | } |
18 | 18 | |
@@ -136,10 +136,10 @@ discard block |
||
136 | 136 | */ |
137 | 137 | protected function __construct() { |
138 | 138 | |
139 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
139 | + $this->log = Wordlift_Log_Service::get_logger(get_class()); |
|
140 | 140 | |
141 | 141 | // Sync some configuration properties when key is validated. |
142 | - add_action( 'wl_key_validation_response', array( $this, 'sync' ) ); |
|
142 | + add_action('wl_key_validation_response', array($this, 'sync')); |
|
143 | 143 | |
144 | 144 | } |
145 | 145 | |
@@ -148,15 +148,15 @@ discard block |
||
148 | 148 | * |
149 | 149 | * @return void |
150 | 150 | */ |
151 | - public function sync( $response ) { |
|
152 | - if ( ! $response->is_success() ) { |
|
151 | + public function sync($response) { |
|
152 | + if ( ! $response->is_success()) { |
|
153 | 153 | return; |
154 | 154 | } |
155 | - $data = json_decode( $response->get_body(), true ); |
|
156 | - if ( ! is_array( $data ) || ! array_key_exists( 'networkDatasetId', $data ) ) { |
|
155 | + $data = json_decode($response->get_body(), true); |
|
156 | + if ( ! is_array($data) || ! array_key_exists('networkDatasetId', $data)) { |
|
157 | 157 | return; |
158 | 158 | } |
159 | - $this->set_network_dataset_ids( $data['networkDatasetId'] ); |
|
159 | + $this->set_network_dataset_ids($data['networkDatasetId']); |
|
160 | 160 | } |
161 | 161 | |
162 | 162 | /** |
@@ -177,7 +177,7 @@ discard block |
||
177 | 177 | */ |
178 | 178 | public static function get_instance() { |
179 | 179 | |
180 | - if ( ! isset( self::$instance ) ) { |
|
180 | + if ( ! isset(self::$instance)) { |
|
181 | 181 | self::$instance = new self(); |
182 | 182 | } |
183 | 183 | |
@@ -195,11 +195,11 @@ discard block |
||
195 | 195 | * @return mixed The configuration value or the default value if not found. |
196 | 196 | * @since 3.6.0 |
197 | 197 | */ |
198 | - private function get( $option, $key, $default = '' ) { |
|
198 | + private function get($option, $key, $default = '') { |
|
199 | 199 | |
200 | - $options = get_option( $option, array() ); |
|
200 | + $options = get_option($option, array()); |
|
201 | 201 | |
202 | - return isset( $options[ $key ] ) ? $options[ $key ] : $default; |
|
202 | + return isset($options[$key]) ? $options[$key] : $default; |
|
203 | 203 | } |
204 | 204 | |
205 | 205 | /** |
@@ -211,12 +211,12 @@ discard block |
||
211 | 211 | * |
212 | 212 | * @since 3.9.0 |
213 | 213 | */ |
214 | - private function set( $option, $key, $value ) { |
|
214 | + private function set($option, $key, $value) { |
|
215 | 215 | |
216 | - $values = get_option( $option ); |
|
217 | - $values = isset( $values ) ? $values : array(); |
|
218 | - $values[ $key ] = $value; |
|
219 | - update_option( $option, $values ); |
|
216 | + $values = get_option($option); |
|
217 | + $values = isset($values) ? $values : array(); |
|
218 | + $values[$key] = $value; |
|
219 | + update_option($option, $values); |
|
220 | 220 | |
221 | 221 | } |
222 | 222 | |
@@ -228,7 +228,7 @@ discard block |
||
228 | 228 | */ |
229 | 229 | public function get_entity_base_path() { |
230 | 230 | |
231 | - return $this->get( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, 'entity' ); |
|
231 | + return $this->get('wl_general_settings', self::ENTITY_BASE_PATH_KEY, 'entity'); |
|
232 | 232 | } |
233 | 233 | |
234 | 234 | /** |
@@ -238,9 +238,9 @@ discard block |
||
238 | 238 | * |
239 | 239 | * @since 3.9.0 |
240 | 240 | */ |
241 | - public function set_entity_base_path( $value ) { |
|
241 | + public function set_entity_base_path($value) { |
|
242 | 242 | |
243 | - $this->set( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, $value ); |
|
243 | + $this->set('wl_general_settings', self::ENTITY_BASE_PATH_KEY, $value); |
|
244 | 244 | |
245 | 245 | } |
246 | 246 | |
@@ -252,7 +252,7 @@ discard block |
||
252 | 252 | */ |
253 | 253 | public function is_skip_wizard() { |
254 | 254 | |
255 | - return $this->get( 'wl_general_settings', self::SKIP_WIZARD, false ); |
|
255 | + return $this->get('wl_general_settings', self::SKIP_WIZARD, false); |
|
256 | 256 | } |
257 | 257 | |
258 | 258 | /** |
@@ -262,9 +262,9 @@ discard block |
||
262 | 262 | * |
263 | 263 | * @since 3.9.0 |
264 | 264 | */ |
265 | - public function set_skip_wizard( $value ) { |
|
265 | + public function set_skip_wizard($value) { |
|
266 | 266 | |
267 | - $this->set( 'wl_general_settings', self::SKIP_WIZARD, true === $value ); |
|
267 | + $this->set('wl_general_settings', self::SKIP_WIZARD, true === $value); |
|
268 | 268 | |
269 | 269 | } |
270 | 270 | |
@@ -276,7 +276,7 @@ discard block |
||
276 | 276 | */ |
277 | 277 | public function get_key() { |
278 | 278 | |
279 | - return $this->get( 'wl_general_settings', self::KEY, '' ); |
|
279 | + return $this->get('wl_general_settings', self::KEY, ''); |
|
280 | 280 | } |
281 | 281 | |
282 | 282 | /** |
@@ -286,9 +286,9 @@ discard block |
||
286 | 286 | * |
287 | 287 | * @since 3.9.0 |
288 | 288 | */ |
289 | - public function set_key( $value ) { |
|
289 | + public function set_key($value) { |
|
290 | 290 | |
291 | - $this->set( 'wl_general_settings', self::KEY, $value ); |
|
291 | + $this->set('wl_general_settings', self::KEY, $value); |
|
292 | 292 | } |
293 | 293 | |
294 | 294 | /** |
@@ -302,11 +302,11 @@ discard block |
||
302 | 302 | public function get_language_code() { |
303 | 303 | |
304 | 304 | $language = get_locale(); |
305 | - if ( ! $language ) { |
|
305 | + if ( ! $language) { |
|
306 | 306 | return 'en'; |
307 | 307 | } |
308 | 308 | |
309 | - return substr( $language, 0, 2 ); |
|
309 | + return substr($language, 0, 2); |
|
310 | 310 | } |
311 | 311 | |
312 | 312 | /** |
@@ -321,9 +321,9 @@ discard block |
||
321 | 321 | * |
322 | 322 | * @since 3.9.0 |
323 | 323 | */ |
324 | - public function set_language_code( $value ) { |
|
324 | + public function set_language_code($value) { |
|
325 | 325 | |
326 | - $this->set( 'wl_general_settings', self::LANGUAGE, $value ); |
|
326 | + $this->set('wl_general_settings', self::LANGUAGE, $value); |
|
327 | 327 | |
328 | 328 | } |
329 | 329 | |
@@ -334,9 +334,9 @@ discard block |
||
334 | 334 | * |
335 | 335 | * @since 3.19.0 |
336 | 336 | */ |
337 | - public function set_diagnostic_preferences( $value ) { |
|
337 | + public function set_diagnostic_preferences($value) { |
|
338 | 338 | |
339 | - $this->set( 'wl_general_settings', self::SEND_DIAGNOSTIC, $value ); |
|
339 | + $this->set('wl_general_settings', self::SEND_DIAGNOSTIC, $value); |
|
340 | 340 | |
341 | 341 | } |
342 | 342 | |
@@ -347,7 +347,7 @@ discard block |
||
347 | 347 | */ |
348 | 348 | public function get_diagnostic_preferences() { |
349 | 349 | |
350 | - return $this->get( 'wl_general_settings', self::SEND_DIAGNOSTIC, 'no' ); |
|
350 | + return $this->get('wl_general_settings', self::SEND_DIAGNOSTIC, 'no'); |
|
351 | 351 | } |
352 | 352 | |
353 | 353 | /** |
@@ -358,7 +358,7 @@ discard block |
||
358 | 358 | */ |
359 | 359 | public function get_country_code() { |
360 | 360 | |
361 | - return $this->get( 'wl_general_settings', self::COUNTRY_CODE, 'us' ); |
|
361 | + return $this->get('wl_general_settings', self::COUNTRY_CODE, 'us'); |
|
362 | 362 | } |
363 | 363 | |
364 | 364 | /** |
@@ -368,9 +368,9 @@ discard block |
||
368 | 368 | * |
369 | 369 | * @since 3.18.0 |
370 | 370 | */ |
371 | - public function set_country_code( $value ) { |
|
371 | + public function set_country_code($value) { |
|
372 | 372 | |
373 | - $this->set( 'wl_general_settings', self::COUNTRY_CODE, $value ); |
|
373 | + $this->set('wl_general_settings', self::COUNTRY_CODE, $value); |
|
374 | 374 | |
375 | 375 | } |
376 | 376 | |
@@ -385,7 +385,7 @@ discard block |
||
385 | 385 | */ |
386 | 386 | public function get_publisher_id() { |
387 | 387 | |
388 | - return $this->get( 'wl_general_settings', self::PUBLISHER_ID, null ); |
|
388 | + return $this->get('wl_general_settings', self::PUBLISHER_ID, null); |
|
389 | 389 | } |
390 | 390 | |
391 | 391 | /** |
@@ -395,9 +395,9 @@ discard block |
||
395 | 395 | * |
396 | 396 | * @since 3.9.0 |
397 | 397 | */ |
398 | - public function set_publisher_id( $value ) { |
|
398 | + public function set_publisher_id($value) { |
|
399 | 399 | |
400 | - $this->set( 'wl_general_settings', self::PUBLISHER_ID, $value ); |
|
400 | + $this->set('wl_general_settings', self::PUBLISHER_ID, $value); |
|
401 | 401 | |
402 | 402 | } |
403 | 403 | |
@@ -410,8 +410,8 @@ discard block |
||
410 | 410 | */ |
411 | 411 | public function get_dataset_uri() { |
412 | 412 | |
413 | - if ( apply_filters( 'wl_feature__enable__dataset', true ) ) { |
|
414 | - return $this->get( 'wl_advanced_settings', self::DATASET_URI, null ); |
|
413 | + if (apply_filters('wl_feature__enable__dataset', true)) { |
|
414 | + return $this->get('wl_advanced_settings', self::DATASET_URI, null); |
|
415 | 415 | } else { |
416 | 416 | return null; |
417 | 417 | } |
@@ -424,9 +424,9 @@ discard block |
||
424 | 424 | * |
425 | 425 | * @since 3.10.0 |
426 | 426 | */ |
427 | - public function set_dataset_uri( $value ) { |
|
427 | + public function set_dataset_uri($value) { |
|
428 | 428 | |
429 | - $this->set( 'wl_advanced_settings', self::DATASET_URI, $value ); |
|
429 | + $this->set('wl_advanced_settings', self::DATASET_URI, $value); |
|
430 | 430 | } |
431 | 431 | |
432 | 432 | /** |
@@ -437,7 +437,7 @@ discard block |
||
437 | 437 | */ |
438 | 438 | public function get_package_type() { |
439 | 439 | |
440 | - return $this->get( 'wl_advanced_settings', self::PACKAGE_TYPE, null ); |
|
440 | + return $this->get('wl_advanced_settings', self::PACKAGE_TYPE, null); |
|
441 | 441 | } |
442 | 442 | |
443 | 443 | /** |
@@ -447,8 +447,8 @@ discard block |
||
447 | 447 | * |
448 | 448 | * @since 3.20.0 |
449 | 449 | */ |
450 | - public function set_package_type( $value ) { |
|
451 | - $this->set( 'wl_advanced_settings', self::PACKAGE_TYPE, $value ); |
|
450 | + public function set_package_type($value) { |
|
451 | + $this->set('wl_advanced_settings', self::PACKAGE_TYPE, $value); |
|
452 | 452 | } |
453 | 453 | |
454 | 454 | /** |
@@ -463,11 +463,11 @@ discard block |
||
463 | 463 | * @param array $old_value The old settings. |
464 | 464 | * @param array $new_value The new settings. |
465 | 465 | */ |
466 | - public function update_key( $old_value, $new_value ) { |
|
466 | + public function update_key($old_value, $new_value) { |
|
467 | 467 | |
468 | 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 | 469 | // $old_key = isset( $old_value['key'] ) ? $old_value['key'] : ''; |
470 | - $new_key = isset( $new_value['key'] ) ? $new_value['key'] : ''; |
|
470 | + $new_key = isset($new_value['key']) ? $new_value['key'] : ''; |
|
471 | 471 | |
472 | 472 | // If the key hasn't changed, don't do anything. |
473 | 473 | // WARN The 'update_option' hook is fired only if the new and old value are not equal. |
@@ -476,14 +476,14 @@ discard block |
||
476 | 476 | // } |
477 | 477 | |
478 | 478 | // If the key is empty, empty the dataset URI. |
479 | - if ( '' === $new_key ) { |
|
480 | - $this->set_dataset_uri( '' ); |
|
479 | + if ('' === $new_key) { |
|
480 | + $this->set_dataset_uri(''); |
|
481 | 481 | } |
482 | 482 | |
483 | 483 | // make the request to the remote server. |
484 | - $this->get_remote_dataset_uri( $new_key ); |
|
484 | + $this->get_remote_dataset_uri($new_key); |
|
485 | 485 | |
486 | - do_action( 'wl_key_updated' ); |
|
486 | + do_action('wl_key_updated'); |
|
487 | 487 | |
488 | 488 | } |
489 | 489 | |
@@ -499,15 +499,15 @@ discard block |
||
499 | 499 | * |
500 | 500 | * @since 3.17.0 send the site URL and get the dataset URI. |
501 | 501 | */ |
502 | - public function get_remote_dataset_uri( $key ) { |
|
502 | + public function get_remote_dataset_uri($key) { |
|
503 | 503 | |
504 | - $this->log->trace( 'Getting the remote dataset URI and package type...' ); |
|
504 | + $this->log->trace('Getting the remote dataset URI and package type...'); |
|
505 | 505 | |
506 | - if ( empty( $key ) ) { |
|
507 | - $this->log->warn( 'Key set to empty value.' ); |
|
506 | + if (empty($key)) { |
|
507 | + $this->log->warn('Key set to empty value.'); |
|
508 | 508 | |
509 | - $this->set_dataset_uri( '' ); |
|
510 | - $this->set_package_type( null ); |
|
509 | + $this->set_dataset_uri(''); |
|
510 | + $this->set_package_type(null); |
|
511 | 511 | |
512 | 512 | return; |
513 | 513 | } |
@@ -521,15 +521,15 @@ discard block |
||
521 | 521 | * |
522 | 522 | * @since 3.20.0 |
523 | 523 | */ |
524 | - $home_url = get_option( 'home' ); |
|
525 | - $site_url = apply_filters( 'wl_production_site_url', untrailingslashit( $home_url ) ); |
|
524 | + $home_url = get_option('home'); |
|
525 | + $site_url = apply_filters('wl_production_site_url', untrailingslashit($home_url)); |
|
526 | 526 | |
527 | 527 | // Build the URL. |
528 | 528 | $url = '/accounts' |
529 | - . '?key=' . rawurlencode( $key ) |
|
530 | - . '&url=' . rawurlencode( $site_url ) |
|
531 | - . '&country=' . $this->get_country_code() |
|
532 | - . '&language=' . $this->get_language_code(); |
|
529 | + . '?key='.rawurlencode($key) |
|
530 | + . '&url='.rawurlencode($site_url) |
|
531 | + . '&country='.$this->get_country_code() |
|
532 | + . '&language='.$this->get_language_code(); |
|
533 | 533 | |
534 | 534 | $api_service = Default_Api_Service::get_instance(); |
535 | 535 | /** |
@@ -539,32 +539,32 @@ discard block |
||
539 | 539 | $headers = array( |
540 | 540 | 'Authorization' => "Key $key", |
541 | 541 | ); |
542 | - $response = $api_service->request( 'PUT', $url, $headers )->get_response(); |
|
542 | + $response = $api_service->request('PUT', $url, $headers)->get_response(); |
|
543 | 543 | |
544 | 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() ); |
|
545 | + if (is_wp_error($response)) { |
|
546 | + $this->log->error('An error occurred setting the dataset URI: '.$response->get_error_message()); |
|
547 | 547 | |
548 | - $this->set_dataset_uri( '' ); |
|
549 | - $this->set_package_type( null ); |
|
548 | + $this->set_dataset_uri(''); |
|
549 | + $this->set_package_type(null); |
|
550 | 550 | |
551 | 551 | return; |
552 | 552 | } |
553 | 553 | |
554 | 554 | // The response is not OK. |
555 | - if ( ! is_array( $response ) || 200 !== (int) $response['response']['code'] ) { |
|
555 | + if ( ! is_array($response) || 200 !== (int) $response['response']['code']) { |
|
556 | 556 | $base_url = $api_service->get_base_url(); |
557 | 557 | |
558 | - if ( ! is_array( $response ) ) { |
|
558 | + if ( ! is_array($response)) { |
|
559 | 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 ) ); |
|
560 | + $this->log->error("Unexpected response when opening URL $base_url$url: ".var_export($response, true)); |
|
561 | 561 | } else { |
562 | 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 ) ); |
|
563 | + $this->log->error("Unexpected status code when opening URL $base_url$url: ".$response['response']['code']."\n".var_export($response, true)); |
|
564 | 564 | } |
565 | 565 | |
566 | - $this->set_dataset_uri( '' ); |
|
567 | - $this->set_package_type( null ); |
|
566 | + $this->set_dataset_uri(''); |
|
567 | + $this->set_package_type(null); |
|
568 | 568 | |
569 | 569 | return; |
570 | 570 | } |
@@ -574,20 +574,20 @@ discard block |
||
574 | 574 | * |
575 | 575 | * @since 3.20.0 |
576 | 576 | */ |
577 | - $json = json_decode( $response['body'] ); |
|
577 | + $json = json_decode($response['body']); |
|
578 | 578 | /** |
579 | 579 | * @since 3.27.7 |
580 | 580 | * Remove the trailing slash returned from the new platform api. |
581 | 581 | */ |
582 | 582 | // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
583 | - $dataset_uri = untrailingslashit( $json->datasetURI ); |
|
583 | + $dataset_uri = untrailingslashit($json->datasetURI); |
|
584 | 584 | // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
585 | - $package_type = isset( $json->packageType ) ? $json->packageType : null; |
|
585 | + $package_type = isset($json->packageType) ? $json->packageType : null; |
|
586 | 586 | |
587 | - $this->log->info( "Updating [ dataset uri :: $dataset_uri ][ package type :: $package_type ]..." ); |
|
587 | + $this->log->info("Updating [ dataset uri :: $dataset_uri ][ package type :: $package_type ]..."); |
|
588 | 588 | |
589 | - $this->set_dataset_uri( $dataset_uri ); |
|
590 | - $this->set_package_type( $package_type ); |
|
589 | + $this->set_dataset_uri($dataset_uri); |
|
590 | + $this->set_package_type($package_type); |
|
591 | 591 | } |
592 | 592 | |
593 | 593 | /** |
@@ -604,20 +604,20 @@ discard block |
||
604 | 604 | * @return mixed The same value in the $value parameter |
605 | 605 | * @since 3.12.0 |
606 | 606 | */ |
607 | - public function maybe_update_dataset_uri( $value, $old_value ) { |
|
607 | + public function maybe_update_dataset_uri($value, $old_value) { |
|
608 | 608 | |
609 | 609 | // Check the old key value and the new one. Here we're only handling the |
610 | 610 | // case where the key hasn't changed and the dataset URI isn't set. The |
611 | 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'] : ''; |
|
612 | + $old_key = isset($old_value['key']) ? $old_value['key'] : ''; |
|
613 | + $new_key = isset($value['key']) ? $value['key'] : ''; |
|
614 | 614 | |
615 | 615 | $dataset_uri = $this->get_dataset_uri(); |
616 | 616 | |
617 | - if ( ! empty( $new_key ) && $new_key === $old_key && empty( $dataset_uri ) ) { |
|
617 | + if ( ! empty($new_key) && $new_key === $old_key && empty($dataset_uri)) { |
|
618 | 618 | |
619 | 619 | // make the request to the remote server to try to get the dataset uri. |
620 | - $this->get_remote_dataset_uri( $new_key ); |
|
620 | + $this->get_remote_dataset_uri($new_key); |
|
621 | 621 | } |
622 | 622 | |
623 | 623 | return $value; |
@@ -631,9 +631,9 @@ discard block |
||
631 | 631 | * @return string The API URI. |
632 | 632 | * @since 3.11.0 |
633 | 633 | */ |
634 | - public function get_accounts_by_key_dataset_uri( $key ) { |
|
634 | + public function get_accounts_by_key_dataset_uri($key) { |
|
635 | 635 | |
636 | - return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . "accounts/key=$key/dataset_uri"; |
|
636 | + return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE."accounts/key=$key/dataset_uri"; |
|
637 | 637 | } |
638 | 638 | |
639 | 639 | /** |
@@ -644,7 +644,7 @@ discard block |
||
644 | 644 | */ |
645 | 645 | public function get_accounts() { |
646 | 646 | |
647 | - return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'accounts'; |
|
647 | + return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE.'accounts'; |
|
648 | 648 | } |
649 | 649 | |
650 | 650 | /** |
@@ -655,7 +655,7 @@ discard block |
||
655 | 655 | */ |
656 | 656 | public function is_link_by_default() { |
657 | 657 | |
658 | - return 'yes' === $this->get( 'wl_general_settings', self::LINK_BY_DEFAULT, 'yes' ); |
|
658 | + return 'yes' === $this->get('wl_general_settings', self::LINK_BY_DEFAULT, 'yes'); |
|
659 | 659 | } |
660 | 660 | |
661 | 661 | /** |
@@ -665,9 +665,9 @@ discard block |
||
665 | 665 | * |
666 | 666 | * @since 3.13.0 |
667 | 667 | */ |
668 | - public function set_link_by_default( $value ) { |
|
668 | + public function set_link_by_default($value) { |
|
669 | 669 | |
670 | - $this->set( 'wl_general_settings', self::LINK_BY_DEFAULT, true === $value ? 'yes' : 'no' ); |
|
670 | + $this->set('wl_general_settings', self::LINK_BY_DEFAULT, true === $value ? 'yes' : 'no'); |
|
671 | 671 | } |
672 | 672 | |
673 | 673 | /** |
@@ -677,7 +677,7 @@ discard block |
||
677 | 677 | * @since 3.21.0 |
678 | 678 | */ |
679 | 679 | public function is_analytics_enable() { |
680 | - return 'yes' === $this->get( 'wl_analytics_settings', self::ANALYTICS_ENABLE, 'no' ); |
|
680 | + return 'yes' === $this->get('wl_analytics_settings', self::ANALYTICS_ENABLE, 'no'); |
|
681 | 681 | } |
682 | 682 | |
683 | 683 | /** |
@@ -687,9 +687,9 @@ discard block |
||
687 | 687 | * |
688 | 688 | * @since 3.21.0 |
689 | 689 | */ |
690 | - public function set_is_analytics_enable( $value ) { |
|
690 | + public function set_is_analytics_enable($value) { |
|
691 | 691 | |
692 | - $this->set( 'wl_general_settings', self::ANALYTICS_ENABLE, true === $value ? 'yes' : 'no' ); |
|
692 | + $this->set('wl_general_settings', self::ANALYTICS_ENABLE, true === $value ? 'yes' : 'no'); |
|
693 | 693 | } |
694 | 694 | |
695 | 695 | /** |
@@ -699,7 +699,7 @@ discard block |
||
699 | 699 | * @since 3.21.0 |
700 | 700 | */ |
701 | 701 | public function get_analytics_entity_uri_dimension() { |
702 | - return (int) $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_URI_DIMENSION, 1 ); |
|
702 | + return (int) $this->get('wl_analytics_settings', self::ANALYTICS_ENTITY_URI_DIMENSION, 1); |
|
703 | 703 | } |
704 | 704 | |
705 | 705 | /** |
@@ -709,7 +709,7 @@ discard block |
||
709 | 709 | * @since 3.21.0 |
710 | 710 | */ |
711 | 711 | public function get_analytics_entity_type_dimension() { |
712 | - return $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_TYPE_DIMENSION, 2 ); |
|
712 | + return $this->get('wl_analytics_settings', self::ANALYTICS_ENTITY_TYPE_DIMENSION, 2); |
|
713 | 713 | } |
714 | 714 | |
715 | 715 | /** |
@@ -720,7 +720,7 @@ discard block |
||
720 | 720 | */ |
721 | 721 | public function get_autocomplete_url() { |
722 | 722 | |
723 | - return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'autocomplete'; |
|
723 | + return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE.'autocomplete'; |
|
724 | 724 | |
725 | 725 | } |
726 | 726 | |
@@ -732,7 +732,7 @@ discard block |
||
732 | 732 | */ |
733 | 733 | public function get_deactivation_feedback_url() { |
734 | 734 | |
735 | - return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'feedbacks'; |
|
735 | + return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE.'feedbacks'; |
|
736 | 736 | |
737 | 737 | } |
738 | 738 | |
@@ -748,11 +748,11 @@ discard block |
||
748 | 748 | } |
749 | 749 | |
750 | 750 | public function get_network_dataset_ids() { |
751 | - return $this->get( 'wl_advanced_settings', self::NETWORK_DATASET_IDS, array() ); |
|
751 | + return $this->get('wl_advanced_settings', self::NETWORK_DATASET_IDS, array()); |
|
752 | 752 | } |
753 | 753 | |
754 | - public function set_network_dataset_ids( $network_dataset_ids ) { |
|
755 | - $this->set( 'wl_advanced_settings', self::NETWORK_DATASET_IDS, $network_dataset_ids ); |
|
754 | + public function set_network_dataset_ids($network_dataset_ids) { |
|
755 | + $this->set('wl_advanced_settings', self::NETWORK_DATASET_IDS, $network_dataset_ids); |
|
756 | 756 | } |
757 | 757 | |
758 | 758 | } |
@@ -12,177 +12,177 @@ |
||
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 | } |
@@ -31,13 +31,13 @@ discard block |
||
31 | 31 | * @param Default_Api_Service $api_service |
32 | 32 | * @param Cache $cache_service |
33 | 33 | */ |
34 | - public function __construct( $api_service, $cache_service ) { |
|
34 | + public function __construct($api_service, $cache_service) { |
|
35 | 35 | |
36 | 36 | $this->api_service = $api_service; |
37 | 37 | |
38 | 38 | $this->cache_service = $cache_service; |
39 | 39 | |
40 | - $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
40 | + $this->log = \Wordlift_Log_Service::get_logger(get_class()); |
|
41 | 41 | |
42 | 42 | } |
43 | 43 | |
@@ -47,24 +47,24 @@ discard block |
||
47 | 47 | * |
48 | 48 | * @param $tag \WP_Term |
49 | 49 | */ |
50 | - public function get_entities( $tag ) { |
|
50 | + public function get_entities($tag) { |
|
51 | 51 | |
52 | 52 | $cache_key = $tag->term_id; |
53 | - $cache_result = $this->cache_service->get( $cache_key ); |
|
54 | - if ( false !== $cache_result ) { |
|
53 | + $cache_result = $this->cache_service->get($cache_key); |
|
54 | + if (false !== $cache_result) { |
|
55 | 55 | return $cache_result; |
56 | 56 | } |
57 | 57 | |
58 | 58 | // send the request. |
59 | 59 | $tag_name = $tag->name; |
60 | 60 | |
61 | - $entities = $this->get_entities_by_search_query( $tag_name ); |
|
61 | + $entities = $this->get_entities_by_search_query($tag_name); |
|
62 | 62 | |
63 | - if ( ! $entities ) { |
|
63 | + if ( ! $entities) { |
|
64 | 64 | return false; |
65 | 65 | } |
66 | 66 | |
67 | - $this->cache_service->put( $cache_key, $entities ); |
|
67 | + $this->cache_service->put($cache_key, $entities); |
|
68 | 68 | |
69 | 69 | return $entities; |
70 | 70 | |
@@ -77,47 +77,47 @@ discard block |
||
77 | 77 | * |
78 | 78 | * @return bool|string |
79 | 79 | */ |
80 | - public static function format_entity_url( $entity_url ) { |
|
81 | - $result = wp_parse_url( $entity_url ); |
|
82 | - if ( ! $result ) { |
|
80 | + public static function format_entity_url($entity_url) { |
|
81 | + $result = wp_parse_url($entity_url); |
|
82 | + if ( ! $result) { |
|
83 | 83 | return false; |
84 | 84 | } |
85 | - if ( ! array_key_exists( 'scheme', $result ) |
|
86 | - || ! array_key_exists( 'host', $result ) |
|
87 | - || ! array_key_exists( 'path', $result ) ) { |
|
85 | + if ( ! array_key_exists('scheme', $result) |
|
86 | + || ! array_key_exists('host', $result) |
|
87 | + || ! array_key_exists('path', $result)) { |
|
88 | 88 | return false; |
89 | 89 | } |
90 | 90 | |
91 | - return $result['scheme'] . '/' . $result['host'] . $result['path']; |
|
91 | + return $result['scheme'].'/'.$result['host'].$result['path']; |
|
92 | 92 | } |
93 | 93 | |
94 | - private function get_meta( $entity_url ) { |
|
94 | + private function get_meta($entity_url) { |
|
95 | 95 | |
96 | - $cache_results = $this->cache_service->get( $entity_url ); |
|
96 | + $cache_results = $this->cache_service->get($entity_url); |
|
97 | 97 | |
98 | - if ( false !== $cache_results ) { |
|
98 | + if (false !== $cache_results) { |
|
99 | 99 | return $cache_results; |
100 | 100 | } |
101 | 101 | |
102 | - $formatted_url = self::format_entity_url( $entity_url ); |
|
102 | + $formatted_url = self::format_entity_url($entity_url); |
|
103 | 103 | |
104 | - if ( ! $formatted_url ) { |
|
104 | + if ( ! $formatted_url) { |
|
105 | 105 | return array(); |
106 | 106 | } |
107 | 107 | |
108 | - $meta_url = 'https://api.wordlift.io/id/' . $formatted_url; |
|
108 | + $meta_url = 'https://api.wordlift.io/id/'.$formatted_url; |
|
109 | 109 | |
110 | - $response = wp_remote_get( $meta_url ); |
|
110 | + $response = wp_remote_get($meta_url); |
|
111 | 111 | |
112 | - if ( is_wp_error( $response ) |
|
113 | - || ! isset( $response['response']['code'] ) |
|
114 | - || 2 !== (int) $response['response']['code'] / 100 ) { |
|
112 | + if (is_wp_error($response) |
|
113 | + || ! isset($response['response']['code']) |
|
114 | + || 2 !== (int) $response['response']['code'] / 100) { |
|
115 | 115 | return false; |
116 | 116 | } |
117 | 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 ); |
|
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 | 121 | |
122 | 122 | return $meta; |
123 | 123 | } |
@@ -126,14 +126,14 @@ discard block |
||
126 | 126 | |
127 | 127 | } |
128 | 128 | |
129 | - private function get_meta_for_entities( $entities ) { |
|
129 | + private function get_meta_for_entities($entities) { |
|
130 | 130 | |
131 | 131 | $filtered_entities = array(); |
132 | - foreach ( $entities as $entity ) { |
|
132 | + foreach ($entities as $entity) { |
|
133 | 133 | $entity['meta'] = array(); |
134 | - $meta = $this->get_meta( $entity['entityId'] ); |
|
135 | - if ( $meta ) { |
|
136 | - $meta = Default_Entity_List::compact_jsonld( $meta ); |
|
134 | + $meta = $this->get_meta($entity['entityId']); |
|
135 | + if ($meta) { |
|
136 | + $meta = Default_Entity_List::compact_jsonld($meta); |
|
137 | 137 | $entity['meta'] = $meta; |
138 | 138 | $filtered_entities[] = $entity; |
139 | 139 | } |
@@ -148,11 +148,11 @@ discard block |
||
148 | 148 | * |
149 | 149 | * @return array |
150 | 150 | */ |
151 | - public function get_entities_by_search_query( $tag_name ) { |
|
151 | + public function get_entities_by_search_query($tag_name) { |
|
152 | 152 | $response = $this->api_service->request( |
153 | 153 | 'POST', |
154 | 154 | '/analysis/single', |
155 | - array( 'Content-Type' => 'application/json' ), |
|
155 | + array('Content-Type' => 'application/json'), |
|
156 | 156 | wp_json_encode( |
157 | 157 | array( |
158 | 158 | 'content' => $tag_name, |
@@ -164,17 +164,17 @@ discard block |
||
164 | 164 | ) |
165 | 165 | ); |
166 | 166 | |
167 | - if ( ! $response->is_success() ) { |
|
167 | + if ( ! $response->is_success()) { |
|
168 | 168 | return false; |
169 | 169 | } |
170 | 170 | |
171 | - $response = json_decode( $response->get_body(), true ); |
|
171 | + $response = json_decode($response->get_body(), true); |
|
172 | 172 | |
173 | - if ( ! array_key_exists( 'entities', $response ) ) { |
|
173 | + if ( ! array_key_exists('entities', $response)) { |
|
174 | 174 | return false; |
175 | 175 | } |
176 | 176 | |
177 | - $entities = $this->get_meta_for_entities( $response['entities'] ); |
|
177 | + $entities = $this->get_meta_for_entities($response['entities']); |
|
178 | 178 | |
179 | 179 | return $entities; |
180 | 180 | } |
@@ -182,7 +182,7 @@ discard block |
||
182 | 182 | public function get_scope() { |
183 | 183 | $service = \Wordlift_Configuration_Service::get_instance(); |
184 | 184 | |
185 | - return count( $service->get_network_dataset_ids() ) > 0 ? 'network-only' : 'cloud'; |
|
185 | + return count($service->get_network_dataset_ids()) > 0 ? 'network-only' : 'cloud'; |
|
186 | 186 | } |
187 | 187 | |
188 | 188 | } |
@@ -14,89 +14,89 @@ discard block |
||
14 | 14 | |
15 | 15 | class Meta_Box { |
16 | 16 | |
17 | - /** |
|
18 | - * @var Api_Service_Ext |
|
19 | - */ |
|
20 | - private $api_service; |
|
21 | - |
|
22 | - /** |
|
23 | - * @var Recipe_Lift_Strategy |
|
24 | - */ |
|
25 | - private $recipe_lift_strategy; |
|
26 | - |
|
27 | - /** |
|
28 | - * @param Api_Service_Ext $api_service |
|
29 | - * @param Recipe_Lift_Strategy $recipe_lift_strategy |
|
30 | - */ |
|
31 | - public function __construct( Api_Service_Ext $api_service, Recipe_Lift_Strategy $recipe_lift_strategy ) { |
|
32 | - $this->api_service = $api_service; |
|
33 | - $this->recipe_lift_strategy = $recipe_lift_strategy; |
|
34 | - } |
|
35 | - |
|
36 | - /** |
|
37 | - * Register Hooks. |
|
38 | - * |
|
39 | - * @return void |
|
40 | - */ |
|
41 | - public function register_hooks() { |
|
42 | - add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_editor_assets' ) ); |
|
43 | - add_action( 'wl_ingredient_metabox_html', array( $this, 'ingredients_html' ) ); |
|
44 | - add_action( 'wp_ajax_wl_update_ingredient_post_meta', array( $this, 'update_ingredient_post_meta' ) ); |
|
45 | - add_action( 'wp_ajax_wl_ingredient_autocomplete', array( $this, 'wl_ingredient_autocomplete' ) ); |
|
46 | - add_action( 'wl_metabox_html', array( $this, 'metabox_tab' ) ); |
|
47 | - } |
|
48 | - |
|
49 | - private function has_recipes( $post_id ) { |
|
50 | - |
|
51 | - $recipe_ids = \WPRM_Recipe_Manager::get_recipe_ids_from_post( $post_id ); |
|
52 | - |
|
53 | - return 0 < count( $recipe_ids ); |
|
54 | - } |
|
55 | - |
|
56 | - public function metabox_tab() { |
|
57 | - // Only display the Main Ingredient tab if the feature and WP Recipe Maker plugin enabled. |
|
58 | - if ( get_the_ID() && $this->has_recipes( get_the_ID() ) ) { // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
59 | - $recipe_ids = \WPRM_Recipe_Manager::get_recipe_ids_from_post( get_the_ID() ); |
|
60 | - ?> |
|
17 | + /** |
|
18 | + * @var Api_Service_Ext |
|
19 | + */ |
|
20 | + private $api_service; |
|
21 | + |
|
22 | + /** |
|
23 | + * @var Recipe_Lift_Strategy |
|
24 | + */ |
|
25 | + private $recipe_lift_strategy; |
|
26 | + |
|
27 | + /** |
|
28 | + * @param Api_Service_Ext $api_service |
|
29 | + * @param Recipe_Lift_Strategy $recipe_lift_strategy |
|
30 | + */ |
|
31 | + public function __construct( Api_Service_Ext $api_service, Recipe_Lift_Strategy $recipe_lift_strategy ) { |
|
32 | + $this->api_service = $api_service; |
|
33 | + $this->recipe_lift_strategy = $recipe_lift_strategy; |
|
34 | + } |
|
35 | + |
|
36 | + /** |
|
37 | + * Register Hooks. |
|
38 | + * |
|
39 | + * @return void |
|
40 | + */ |
|
41 | + public function register_hooks() { |
|
42 | + add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_editor_assets' ) ); |
|
43 | + add_action( 'wl_ingredient_metabox_html', array( $this, 'ingredients_html' ) ); |
|
44 | + add_action( 'wp_ajax_wl_update_ingredient_post_meta', array( $this, 'update_ingredient_post_meta' ) ); |
|
45 | + add_action( 'wp_ajax_wl_ingredient_autocomplete', array( $this, 'wl_ingredient_autocomplete' ) ); |
|
46 | + add_action( 'wl_metabox_html', array( $this, 'metabox_tab' ) ); |
|
47 | + } |
|
48 | + |
|
49 | + private function has_recipes( $post_id ) { |
|
50 | + |
|
51 | + $recipe_ids = \WPRM_Recipe_Manager::get_recipe_ids_from_post( $post_id ); |
|
52 | + |
|
53 | + return 0 < count( $recipe_ids ); |
|
54 | + } |
|
55 | + |
|
56 | + public function metabox_tab() { |
|
57 | + // Only display the Main Ingredient tab if the feature and WP Recipe Maker plugin enabled. |
|
58 | + if ( get_the_ID() && $this->has_recipes( get_the_ID() ) ) { // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
59 | + $recipe_ids = \WPRM_Recipe_Manager::get_recipe_ids_from_post( get_the_ID() ); |
|
60 | + ?> |
|
61 | 61 | <input id="wl-tab-main-ingredient" type="radio" name="wl-metabox-tabs"/><label |
62 | 62 | for="wl-tab-main-ingredient"><?php esc_html_e( 'Main Ingredient', 'wordlift' ); ?></label> |
63 | 63 | <div class="wl-tabs__tab"><?php $this->ingredients_html( $recipe_ids ); ?></div> |
64 | 64 | <?php |
65 | - } |
|
66 | - } |
|
65 | + } |
|
66 | + } |
|
67 | 67 | |
68 | - public function enqueue_block_editor_assets() { |
|
69 | - // Requires settings defined in $this->enqueue_scripts. |
|
70 | - wp_enqueue_script( 'wl-main-ingredient-select', WL_DIR_URL . 'js/dist/main-ingredient-select.js', array(), WORDLIFT_VERSION, true ); |
|
71 | - } |
|
68 | + public function enqueue_block_editor_assets() { |
|
69 | + // Requires settings defined in $this->enqueue_scripts. |
|
70 | + wp_enqueue_script( 'wl-main-ingredient-select', WL_DIR_URL . 'js/dist/main-ingredient-select.js', array(), WORDLIFT_VERSION, true ); |
|
71 | + } |
|
72 | 72 | |
73 | - /** |
|
74 | - * Ingredients HTML. |
|
75 | - * |
|
76 | - * @param array $recipe_ids Recipe IDs. |
|
77 | - */ |
|
78 | - public function ingredients_html( $recipe_ids ) { |
|
73 | + /** |
|
74 | + * Ingredients HTML. |
|
75 | + * |
|
76 | + * @param array $recipe_ids Recipe IDs. |
|
77 | + */ |
|
78 | + public function ingredients_html( $recipe_ids ) { |
|
79 | 79 | |
80 | - if ( empty( $recipe_ids ) ) { |
|
81 | - return; |
|
82 | - } |
|
80 | + if ( empty( $recipe_ids ) ) { |
|
81 | + return; |
|
82 | + } |
|
83 | 83 | |
84 | - // Enqueue scripts. |
|
85 | - $this->enqueue_scripts(); |
|
84 | + // Enqueue scripts. |
|
85 | + $this->enqueue_scripts(); |
|
86 | 86 | |
87 | - ?> |
|
87 | + ?> |
|
88 | 88 | <div class="wl-recipe-ingredient"> |
89 | 89 | <p> |
90 | 90 | <?php |
91 | - $count = count( $recipe_ids ); |
|
92 | - /* translators: 1: Number of recipes 2: Review notice */ |
|
93 | - echo sprintf( |
|
94 | - '%1$s %2$s', |
|
95 | - /* translators: %d: Number of recipes */ |
|
96 | - esc_html( sprintf( 1 < $count ? __( 'There are %d recipes embedded in this post.', 'wordlift' ) : __( 'There is %d recipe embedded in this post.', 'wordlift' ), $count ) ), |
|
97 | - esc_html__( 'Review the main ingredient for each recipe and change it if required.', 'wordlift' ) |
|
98 | - ); |
|
99 | - ?> |
|
91 | + $count = count( $recipe_ids ); |
|
92 | + /* translators: 1: Number of recipes 2: Review notice */ |
|
93 | + echo sprintf( |
|
94 | + '%1$s %2$s', |
|
95 | + /* translators: %d: Number of recipes */ |
|
96 | + esc_html( sprintf( 1 < $count ? __( 'There are %d recipes embedded in this post.', 'wordlift' ) : __( 'There is %d recipe embedded in this post.', 'wordlift' ), $count ) ), |
|
97 | + esc_html__( 'Review the main ingredient for each recipe and change it if required.', 'wordlift' ) |
|
98 | + ); |
|
99 | + ?> |
|
100 | 100 | </p> |
101 | 101 | </div> |
102 | 102 | <div class="wl-recipe-ingredient-form" id="wl-recipe-ingredient-form"> |
@@ -110,12 +110,12 @@ discard block |
||
110 | 110 | </thead> |
111 | 111 | <tbody> |
112 | 112 | <?php |
113 | - foreach ( $recipe_ids as $recipe_id ) { |
|
114 | - $recipe = \WPRM_Recipe_Manager::get_recipe( $recipe_id ); |
|
115 | - $json_ld = get_post_meta( $recipe_id, '_wl_main_ingredient_jsonld', true ); |
|
116 | - $obj = json_decode( $json_ld ); |
|
117 | - $main_ingredient = isset( $obj->name ) ? $obj->name : '<em>' . __( '(unset)', 'wordlift' ) . '</em>'; |
|
118 | - ?> |
|
113 | + foreach ( $recipe_ids as $recipe_id ) { |
|
114 | + $recipe = \WPRM_Recipe_Manager::get_recipe( $recipe_id ); |
|
115 | + $json_ld = get_post_meta( $recipe_id, '_wl_main_ingredient_jsonld', true ); |
|
116 | + $obj = json_decode( $json_ld ); |
|
117 | + $main_ingredient = isset( $obj->name ) ? $obj->name : '<em>' . __( '(unset)', 'wordlift' ) . '</em>'; |
|
118 | + ?> |
|
119 | 119 | <tr class="wl-table--main-ingredient__data"> |
120 | 120 | <td><?php echo esc_html( $recipe->name() ); ?></td> |
121 | 121 | <td><?php echo wp_kses( $main_ingredient, array( 'em' => array() ) ); ?></td> |
@@ -125,8 +125,8 @@ discard block |
||
125 | 125 | </td> |
126 | 126 | </tr> |
127 | 127 | <?php |
128 | - } |
|
129 | - ?> |
|
128 | + } |
|
129 | + ?> |
|
130 | 130 | </tbody> |
131 | 131 | </table> |
132 | 132 | <div class="wl-recipe-ingredient-form__submit"> |
@@ -137,140 +137,140 @@ discard block |
||
137 | 137 | </div> |
138 | 138 | </div> |
139 | 139 | <?php |
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * Ingredient Autocomplete. |
|
144 | - */ |
|
145 | - public function wl_ingredient_autocomplete() { |
|
146 | - |
|
147 | - check_ajax_referer( 'wl-ac-ingredient-nonce' ); |
|
148 | - |
|
149 | - // Return error if the query param is empty. |
|
150 | - if ( ! empty( $_REQUEST['query'] ) ) { // Input var okay. |
|
151 | - $query = sanitize_text_field( wp_unslash( $_REQUEST['query'] ) ); // Input var okay. |
|
152 | - } else { |
|
153 | - wp_send_json_error( |
|
154 | - array( |
|
155 | - 'message' => __( 'The query param is empty.', 'wordlift' ), |
|
156 | - ) |
|
157 | - ); |
|
158 | - } |
|
159 | - |
|
160 | - // Get new JSON LD Data. |
|
161 | - $new_json_ld = $this->recipe_lift_strategy->get_json_ld_data( $query ); |
|
162 | - |
|
163 | - $data = json_decode( $new_json_ld ); |
|
164 | - |
|
165 | - // Clear any buffer. |
|
166 | - ob_clean(); |
|
167 | - |
|
168 | - if ( $data ) { |
|
169 | - $results = array( |
|
170 | - array( |
|
171 | - 'label' => $data->name, |
|
172 | - 'value' => $new_json_ld, |
|
173 | - ), |
|
174 | - ); |
|
175 | - wp_send_json_success( $results ); |
|
176 | - } else { |
|
177 | - wp_send_json_error( |
|
178 | - array( |
|
179 | - 'message' => __( 'No results found.', 'wordlift' ), |
|
180 | - ) |
|
181 | - ); |
|
182 | - } |
|
183 | - } |
|
184 | - |
|
185 | - /** |
|
186 | - * Update Ingredient Post Meta. |
|
187 | - */ |
|
188 | - public function update_ingredient_post_meta() { |
|
189 | - check_ajax_referer( 'wl-ingredient-nonce' ); |
|
190 | - |
|
191 | - // Check if current user can edit posts. |
|
192 | - if ( ! current_user_can( 'edit_posts' ) ) { |
|
193 | - wp_send_json_error( |
|
194 | - array( |
|
195 | - 'message' => __( 'You are not allowed to edit posts.', 'wordlift' ), |
|
196 | - 'btnText' => __( 'Denied', 'wordlift' ), |
|
197 | - ) |
|
198 | - ); |
|
199 | - } |
|
200 | - // Return error if the recipe data is empty. |
|
201 | - if ( ! empty( $_REQUEST['data'] ) ) { |
|
202 | - $recipe_data = sanitize_text_field( wp_unslash( $_REQUEST['data'] ) ); |
|
203 | - } else { |
|
204 | - wp_send_json_error( |
|
205 | - array( |
|
206 | - 'message' => __( 'Recipe data is empty.', 'wordlift' ), |
|
207 | - 'btnText' => __( 'Failed', 'wordlift' ), |
|
208 | - ) |
|
209 | - ); |
|
210 | - } |
|
211 | - |
|
212 | - $recipes = json_decode( $recipe_data ); |
|
213 | - |
|
214 | - $updated = false; |
|
215 | - foreach ( $recipes as $recipe ) { |
|
216 | - $recipe_id = $recipe->recipe_id; |
|
217 | - $main_ingredient = $recipe->ingredient; |
|
218 | - if ( 'UNSET' === $main_ingredient ) { |
|
219 | - $updated = delete_post_meta( $recipe_id, '_wl_main_ingredient_jsonld' ); |
|
220 | - } elseif ( 'DONT_CHANGE' === $main_ingredient ) { |
|
221 | - $updated = true; |
|
222 | - } else { |
|
223 | - $main_ingredient = wp_json_encode( json_decode( $recipe->ingredient, true ) ); |
|
224 | - $updated = update_post_meta( $recipe_id, '_wl_main_ingredient_jsonld', $main_ingredient ); |
|
225 | - } |
|
226 | - } |
|
227 | - |
|
228 | - // Since we changed the main ingredients we want to flush all caches. |
|
229 | - Ttl_Cache::flush_all(); |
|
230 | - |
|
231 | - if ( $updated ) { |
|
232 | - wp_send_json_success( |
|
233 | - array( |
|
234 | - 'message' => __( 'The main ingredient has been updated.', 'wordlift' ), |
|
235 | - ) |
|
236 | - ); |
|
237 | - } else { |
|
238 | - wp_send_json_error( |
|
239 | - array( |
|
240 | - 'message' => __( 'The main ingredient could not be updated.', 'wordlift' ), |
|
241 | - ) |
|
242 | - ); |
|
243 | - } |
|
244 | - } |
|
245 | - |
|
246 | - /** |
|
247 | - * Enqueue Scripts. |
|
248 | - */ |
|
249 | - public function enqueue_scripts() { |
|
250 | - |
|
251 | - wp_enqueue_script( |
|
252 | - 'wl-meta-box-ingredient', |
|
253 | - WL_DIR_URL . 'js/dist/ingredients-meta-box.js', |
|
254 | - array( 'react', 'react-dom', 'wp-polyfill' ), |
|
255 | - WORDLIFT_VERSION, |
|
256 | - true |
|
257 | - ); |
|
258 | - |
|
259 | - wp_localize_script( |
|
260 | - 'wl-meta-box-ingredient', |
|
261 | - '_wlRecipeIngredientSettings', |
|
262 | - array( |
|
263 | - 'ajaxurl' => admin_url( 'admin-ajax.php' ), |
|
264 | - 'nonce' => wp_create_nonce( 'wl-ingredient-nonce' ), |
|
265 | - 'acNonce' => wp_create_nonce( 'wl-ac-ingredient-nonce' ), |
|
266 | - 'l10n' => array( |
|
267 | - 'Looking for main ingredients...' => _x( 'Looking for main ingredients...', 'Main Ingredient select', 'wordlift' ), |
|
268 | - 'Type at least 3 characters to search...' => _x( 'Type at least 3 characters to search...', 'Main Ingredient select', 'wordlift' ), |
|
269 | - 'No results found for your search.' => _x( 'No results found: try changing or removing some words.', 'Main Ingredient select', 'wordlift' ), |
|
270 | - "(don't change)" => _x( "(don't change)", 'Main Ingredient select', 'wordlift' ), |
|
271 | - '(unset)' => _x( '(unset)', 'Main Ingredient select', 'wordlift' ), |
|
272 | - ), |
|
273 | - ) |
|
274 | - ); |
|
275 | - } |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * Ingredient Autocomplete. |
|
144 | + */ |
|
145 | + public function wl_ingredient_autocomplete() { |
|
146 | + |
|
147 | + check_ajax_referer( 'wl-ac-ingredient-nonce' ); |
|
148 | + |
|
149 | + // Return error if the query param is empty. |
|
150 | + if ( ! empty( $_REQUEST['query'] ) ) { // Input var okay. |
|
151 | + $query = sanitize_text_field( wp_unslash( $_REQUEST['query'] ) ); // Input var okay. |
|
152 | + } else { |
|
153 | + wp_send_json_error( |
|
154 | + array( |
|
155 | + 'message' => __( 'The query param is empty.', 'wordlift' ), |
|
156 | + ) |
|
157 | + ); |
|
158 | + } |
|
159 | + |
|
160 | + // Get new JSON LD Data. |
|
161 | + $new_json_ld = $this->recipe_lift_strategy->get_json_ld_data( $query ); |
|
162 | + |
|
163 | + $data = json_decode( $new_json_ld ); |
|
164 | + |
|
165 | + // Clear any buffer. |
|
166 | + ob_clean(); |
|
167 | + |
|
168 | + if ( $data ) { |
|
169 | + $results = array( |
|
170 | + array( |
|
171 | + 'label' => $data->name, |
|
172 | + 'value' => $new_json_ld, |
|
173 | + ), |
|
174 | + ); |
|
175 | + wp_send_json_success( $results ); |
|
176 | + } else { |
|
177 | + wp_send_json_error( |
|
178 | + array( |
|
179 | + 'message' => __( 'No results found.', 'wordlift' ), |
|
180 | + ) |
|
181 | + ); |
|
182 | + } |
|
183 | + } |
|
184 | + |
|
185 | + /** |
|
186 | + * Update Ingredient Post Meta. |
|
187 | + */ |
|
188 | + public function update_ingredient_post_meta() { |
|
189 | + check_ajax_referer( 'wl-ingredient-nonce' ); |
|
190 | + |
|
191 | + // Check if current user can edit posts. |
|
192 | + if ( ! current_user_can( 'edit_posts' ) ) { |
|
193 | + wp_send_json_error( |
|
194 | + array( |
|
195 | + 'message' => __( 'You are not allowed to edit posts.', 'wordlift' ), |
|
196 | + 'btnText' => __( 'Denied', 'wordlift' ), |
|
197 | + ) |
|
198 | + ); |
|
199 | + } |
|
200 | + // Return error if the recipe data is empty. |
|
201 | + if ( ! empty( $_REQUEST['data'] ) ) { |
|
202 | + $recipe_data = sanitize_text_field( wp_unslash( $_REQUEST['data'] ) ); |
|
203 | + } else { |
|
204 | + wp_send_json_error( |
|
205 | + array( |
|
206 | + 'message' => __( 'Recipe data is empty.', 'wordlift' ), |
|
207 | + 'btnText' => __( 'Failed', 'wordlift' ), |
|
208 | + ) |
|
209 | + ); |
|
210 | + } |
|
211 | + |
|
212 | + $recipes = json_decode( $recipe_data ); |
|
213 | + |
|
214 | + $updated = false; |
|
215 | + foreach ( $recipes as $recipe ) { |
|
216 | + $recipe_id = $recipe->recipe_id; |
|
217 | + $main_ingredient = $recipe->ingredient; |
|
218 | + if ( 'UNSET' === $main_ingredient ) { |
|
219 | + $updated = delete_post_meta( $recipe_id, '_wl_main_ingredient_jsonld' ); |
|
220 | + } elseif ( 'DONT_CHANGE' === $main_ingredient ) { |
|
221 | + $updated = true; |
|
222 | + } else { |
|
223 | + $main_ingredient = wp_json_encode( json_decode( $recipe->ingredient, true ) ); |
|
224 | + $updated = update_post_meta( $recipe_id, '_wl_main_ingredient_jsonld', $main_ingredient ); |
|
225 | + } |
|
226 | + } |
|
227 | + |
|
228 | + // Since we changed the main ingredients we want to flush all caches. |
|
229 | + Ttl_Cache::flush_all(); |
|
230 | + |
|
231 | + if ( $updated ) { |
|
232 | + wp_send_json_success( |
|
233 | + array( |
|
234 | + 'message' => __( 'The main ingredient has been updated.', 'wordlift' ), |
|
235 | + ) |
|
236 | + ); |
|
237 | + } else { |
|
238 | + wp_send_json_error( |
|
239 | + array( |
|
240 | + 'message' => __( 'The main ingredient could not be updated.', 'wordlift' ), |
|
241 | + ) |
|
242 | + ); |
|
243 | + } |
|
244 | + } |
|
245 | + |
|
246 | + /** |
|
247 | + * Enqueue Scripts. |
|
248 | + */ |
|
249 | + public function enqueue_scripts() { |
|
250 | + |
|
251 | + wp_enqueue_script( |
|
252 | + 'wl-meta-box-ingredient', |
|
253 | + WL_DIR_URL . 'js/dist/ingredients-meta-box.js', |
|
254 | + array( 'react', 'react-dom', 'wp-polyfill' ), |
|
255 | + WORDLIFT_VERSION, |
|
256 | + true |
|
257 | + ); |
|
258 | + |
|
259 | + wp_localize_script( |
|
260 | + 'wl-meta-box-ingredient', |
|
261 | + '_wlRecipeIngredientSettings', |
|
262 | + array( |
|
263 | + 'ajaxurl' => admin_url( 'admin-ajax.php' ), |
|
264 | + 'nonce' => wp_create_nonce( 'wl-ingredient-nonce' ), |
|
265 | + 'acNonce' => wp_create_nonce( 'wl-ac-ingredient-nonce' ), |
|
266 | + 'l10n' => array( |
|
267 | + 'Looking for main ingredients...' => _x( 'Looking for main ingredients...', 'Main Ingredient select', 'wordlift' ), |
|
268 | + 'Type at least 3 characters to search...' => _x( 'Type at least 3 characters to search...', 'Main Ingredient select', 'wordlift' ), |
|
269 | + 'No results found for your search.' => _x( 'No results found: try changing or removing some words.', 'Main Ingredient select', 'wordlift' ), |
|
270 | + "(don't change)" => _x( "(don't change)", 'Main Ingredient select', 'wordlift' ), |
|
271 | + '(unset)' => _x( '(unset)', 'Main Ingredient select', 'wordlift' ), |
|
272 | + ), |
|
273 | + ) |
|
274 | + ); |
|
275 | + } |
|
276 | 276 | } |
@@ -28,7 +28,7 @@ discard block |
||
28 | 28 | * @param Api_Service_Ext $api_service |
29 | 29 | * @param Recipe_Lift_Strategy $recipe_lift_strategy |
30 | 30 | */ |
31 | - public function __construct( Api_Service_Ext $api_service, Recipe_Lift_Strategy $recipe_lift_strategy ) { |
|
31 | + public function __construct(Api_Service_Ext $api_service, Recipe_Lift_Strategy $recipe_lift_strategy) { |
|
32 | 32 | $this->api_service = $api_service; |
33 | 33 | $this->recipe_lift_strategy = $recipe_lift_strategy; |
34 | 34 | } |
@@ -39,35 +39,35 @@ discard block |
||
39 | 39 | * @return void |
40 | 40 | */ |
41 | 41 | public function register_hooks() { |
42 | - add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_editor_assets' ) ); |
|
43 | - add_action( 'wl_ingredient_metabox_html', array( $this, 'ingredients_html' ) ); |
|
44 | - add_action( 'wp_ajax_wl_update_ingredient_post_meta', array( $this, 'update_ingredient_post_meta' ) ); |
|
45 | - add_action( 'wp_ajax_wl_ingredient_autocomplete', array( $this, 'wl_ingredient_autocomplete' ) ); |
|
46 | - add_action( 'wl_metabox_html', array( $this, 'metabox_tab' ) ); |
|
42 | + add_action('enqueue_block_editor_assets', array($this, 'enqueue_block_editor_assets')); |
|
43 | + add_action('wl_ingredient_metabox_html', array($this, 'ingredients_html')); |
|
44 | + add_action('wp_ajax_wl_update_ingredient_post_meta', array($this, 'update_ingredient_post_meta')); |
|
45 | + add_action('wp_ajax_wl_ingredient_autocomplete', array($this, 'wl_ingredient_autocomplete')); |
|
46 | + add_action('wl_metabox_html', array($this, 'metabox_tab')); |
|
47 | 47 | } |
48 | 48 | |
49 | - private function has_recipes( $post_id ) { |
|
49 | + private function has_recipes($post_id) { |
|
50 | 50 | |
51 | - $recipe_ids = \WPRM_Recipe_Manager::get_recipe_ids_from_post( $post_id ); |
|
51 | + $recipe_ids = \WPRM_Recipe_Manager::get_recipe_ids_from_post($post_id); |
|
52 | 52 | |
53 | - return 0 < count( $recipe_ids ); |
|
53 | + return 0 < count($recipe_ids); |
|
54 | 54 | } |
55 | 55 | |
56 | 56 | public function metabox_tab() { |
57 | 57 | // Only display the Main Ingredient tab if the feature and WP Recipe Maker plugin enabled. |
58 | - if ( get_the_ID() && $this->has_recipes( get_the_ID() ) ) { // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
59 | - $recipe_ids = \WPRM_Recipe_Manager::get_recipe_ids_from_post( get_the_ID() ); |
|
58 | + if (get_the_ID() && $this->has_recipes(get_the_ID())) { // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
59 | + $recipe_ids = \WPRM_Recipe_Manager::get_recipe_ids_from_post(get_the_ID()); |
|
60 | 60 | ?> |
61 | 61 | <input id="wl-tab-main-ingredient" type="radio" name="wl-metabox-tabs"/><label |
62 | - for="wl-tab-main-ingredient"><?php esc_html_e( 'Main Ingredient', 'wordlift' ); ?></label> |
|
63 | - <div class="wl-tabs__tab"><?php $this->ingredients_html( $recipe_ids ); ?></div> |
|
62 | + for="wl-tab-main-ingredient"><?php esc_html_e('Main Ingredient', 'wordlift'); ?></label> |
|
63 | + <div class="wl-tabs__tab"><?php $this->ingredients_html($recipe_ids); ?></div> |
|
64 | 64 | <?php |
65 | 65 | } |
66 | 66 | } |
67 | 67 | |
68 | 68 | public function enqueue_block_editor_assets() { |
69 | 69 | // Requires settings defined in $this->enqueue_scripts. |
70 | - wp_enqueue_script( 'wl-main-ingredient-select', WL_DIR_URL . 'js/dist/main-ingredient-select.js', array(), WORDLIFT_VERSION, true ); |
|
70 | + wp_enqueue_script('wl-main-ingredient-select', WL_DIR_URL.'js/dist/main-ingredient-select.js', array(), WORDLIFT_VERSION, true); |
|
71 | 71 | } |
72 | 72 | |
73 | 73 | /** |
@@ -75,9 +75,9 @@ discard block |
||
75 | 75 | * |
76 | 76 | * @param array $recipe_ids Recipe IDs. |
77 | 77 | */ |
78 | - public function ingredients_html( $recipe_ids ) { |
|
78 | + public function ingredients_html($recipe_ids) { |
|
79 | 79 | |
80 | - if ( empty( $recipe_ids ) ) { |
|
80 | + if (empty($recipe_ids)) { |
|
81 | 81 | return; |
82 | 82 | } |
83 | 83 | |
@@ -88,13 +88,13 @@ discard block |
||
88 | 88 | <div class="wl-recipe-ingredient"> |
89 | 89 | <p> |
90 | 90 | <?php |
91 | - $count = count( $recipe_ids ); |
|
91 | + $count = count($recipe_ids); |
|
92 | 92 | /* translators: 1: Number of recipes 2: Review notice */ |
93 | 93 | echo sprintf( |
94 | 94 | '%1$s %2$s', |
95 | 95 | /* translators: %d: Number of recipes */ |
96 | - esc_html( sprintf( 1 < $count ? __( 'There are %d recipes embedded in this post.', 'wordlift' ) : __( 'There is %d recipe embedded in this post.', 'wordlift' ), $count ) ), |
|
97 | - esc_html__( 'Review the main ingredient for each recipe and change it if required.', 'wordlift' ) |
|
96 | + esc_html(sprintf(1 < $count ? __('There are %d recipes embedded in this post.', 'wordlift') : __('There is %d recipe embedded in this post.', 'wordlift'), $count)), |
|
97 | + esc_html__('Review the main ingredient for each recipe and change it if required.', 'wordlift') |
|
98 | 98 | ); |
99 | 99 | ?> |
100 | 100 | </p> |
@@ -103,25 +103,25 @@ discard block |
||
103 | 103 | <table class="wl-table wl-table--main-ingredient"> |
104 | 104 | <thead> |
105 | 105 | <tr> |
106 | - <th class="wl-table__th wl-table__th--recipe"><?php esc_html_e( 'Recipe', 'wordlift' ); ?></th> |
|
107 | - <th class="wl-table__th wl-table__th--main-ingredient"><?php esc_html_e( 'Main Ingredient', 'wordlift' ); ?></th> |
|
108 | - <th><?php esc_html_e( 'Action', 'wordlift' ); ?></th> |
|
106 | + <th class="wl-table__th wl-table__th--recipe"><?php esc_html_e('Recipe', 'wordlift'); ?></th> |
|
107 | + <th class="wl-table__th wl-table__th--main-ingredient"><?php esc_html_e('Main Ingredient', 'wordlift'); ?></th> |
|
108 | + <th><?php esc_html_e('Action', 'wordlift'); ?></th> |
|
109 | 109 | </tr> |
110 | 110 | </thead> |
111 | 111 | <tbody> |
112 | 112 | <?php |
113 | - foreach ( $recipe_ids as $recipe_id ) { |
|
114 | - $recipe = \WPRM_Recipe_Manager::get_recipe( $recipe_id ); |
|
115 | - $json_ld = get_post_meta( $recipe_id, '_wl_main_ingredient_jsonld', true ); |
|
116 | - $obj = json_decode( $json_ld ); |
|
117 | - $main_ingredient = isset( $obj->name ) ? $obj->name : '<em>' . __( '(unset)', 'wordlift' ) . '</em>'; |
|
113 | + foreach ($recipe_ids as $recipe_id) { |
|
114 | + $recipe = \WPRM_Recipe_Manager::get_recipe($recipe_id); |
|
115 | + $json_ld = get_post_meta($recipe_id, '_wl_main_ingredient_jsonld', true); |
|
116 | + $obj = json_decode($json_ld); |
|
117 | + $main_ingredient = isset($obj->name) ? $obj->name : '<em>'.__('(unset)', 'wordlift').'</em>'; |
|
118 | 118 | ?> |
119 | 119 | <tr class="wl-table--main-ingredient__data"> |
120 | - <td><?php echo esc_html( $recipe->name() ); ?></td> |
|
121 | - <td><?php echo wp_kses( $main_ingredient, array( 'em' => array() ) ); ?></td> |
|
120 | + <td><?php echo esc_html($recipe->name()); ?></td> |
|
121 | + <td><?php echo wp_kses($main_ingredient, array('em' => array())); ?></td> |
|
122 | 122 | <td class="wl-table__ingredients-data"> |
123 | 123 | <span class="wl-select-main-ingredient" |
124 | - data-recipe-id="<?php echo esc_attr( $recipe_id ); ?>"></span> |
|
124 | + data-recipe-id="<?php echo esc_attr($recipe_id); ?>"></span> |
|
125 | 125 | </td> |
126 | 126 | </tr> |
127 | 127 | <?php |
@@ -133,7 +133,7 @@ discard block |
||
133 | 133 | <div id="wl-recipe-ingredient-form__submit__message"></div> |
134 | 134 | <input type="submit" |
135 | 135 | class="button button-primary button-large pull-right" id="wl-recipe-ingredient-form__submit__btn" |
136 | - value="<?php echo esc_attr__( 'Save', 'wordlift' ); ?>"> |
|
136 | + value="<?php echo esc_attr__('Save', 'wordlift'); ?>"> |
|
137 | 137 | </div> |
138 | 138 | </div> |
139 | 139 | <?php |
@@ -144,39 +144,39 @@ discard block |
||
144 | 144 | */ |
145 | 145 | public function wl_ingredient_autocomplete() { |
146 | 146 | |
147 | - check_ajax_referer( 'wl-ac-ingredient-nonce' ); |
|
147 | + check_ajax_referer('wl-ac-ingredient-nonce'); |
|
148 | 148 | |
149 | 149 | // Return error if the query param is empty. |
150 | - if ( ! empty( $_REQUEST['query'] ) ) { // Input var okay. |
|
151 | - $query = sanitize_text_field( wp_unslash( $_REQUEST['query'] ) ); // Input var okay. |
|
150 | + if ( ! empty($_REQUEST['query'])) { // Input var okay. |
|
151 | + $query = sanitize_text_field(wp_unslash($_REQUEST['query'])); // Input var okay. |
|
152 | 152 | } else { |
153 | 153 | wp_send_json_error( |
154 | 154 | array( |
155 | - 'message' => __( 'The query param is empty.', 'wordlift' ), |
|
155 | + 'message' => __('The query param is empty.', 'wordlift'), |
|
156 | 156 | ) |
157 | 157 | ); |
158 | 158 | } |
159 | 159 | |
160 | 160 | // Get new JSON LD Data. |
161 | - $new_json_ld = $this->recipe_lift_strategy->get_json_ld_data( $query ); |
|
161 | + $new_json_ld = $this->recipe_lift_strategy->get_json_ld_data($query); |
|
162 | 162 | |
163 | - $data = json_decode( $new_json_ld ); |
|
163 | + $data = json_decode($new_json_ld); |
|
164 | 164 | |
165 | 165 | // Clear any buffer. |
166 | 166 | ob_clean(); |
167 | 167 | |
168 | - if ( $data ) { |
|
168 | + if ($data) { |
|
169 | 169 | $results = array( |
170 | 170 | array( |
171 | 171 | 'label' => $data->name, |
172 | 172 | 'value' => $new_json_ld, |
173 | 173 | ), |
174 | 174 | ); |
175 | - wp_send_json_success( $results ); |
|
175 | + wp_send_json_success($results); |
|
176 | 176 | } else { |
177 | 177 | wp_send_json_error( |
178 | 178 | array( |
179 | - 'message' => __( 'No results found.', 'wordlift' ), |
|
179 | + 'message' => __('No results found.', 'wordlift'), |
|
180 | 180 | ) |
181 | 181 | ); |
182 | 182 | } |
@@ -186,58 +186,58 @@ discard block |
||
186 | 186 | * Update Ingredient Post Meta. |
187 | 187 | */ |
188 | 188 | public function update_ingredient_post_meta() { |
189 | - check_ajax_referer( 'wl-ingredient-nonce' ); |
|
189 | + check_ajax_referer('wl-ingredient-nonce'); |
|
190 | 190 | |
191 | 191 | // Check if current user can edit posts. |
192 | - if ( ! current_user_can( 'edit_posts' ) ) { |
|
192 | + if ( ! current_user_can('edit_posts')) { |
|
193 | 193 | wp_send_json_error( |
194 | 194 | array( |
195 | - 'message' => __( 'You are not allowed to edit posts.', 'wordlift' ), |
|
196 | - 'btnText' => __( 'Denied', 'wordlift' ), |
|
195 | + 'message' => __('You are not allowed to edit posts.', 'wordlift'), |
|
196 | + 'btnText' => __('Denied', 'wordlift'), |
|
197 | 197 | ) |
198 | 198 | ); |
199 | 199 | } |
200 | 200 | // Return error if the recipe data is empty. |
201 | - if ( ! empty( $_REQUEST['data'] ) ) { |
|
202 | - $recipe_data = sanitize_text_field( wp_unslash( $_REQUEST['data'] ) ); |
|
201 | + if ( ! empty($_REQUEST['data'])) { |
|
202 | + $recipe_data = sanitize_text_field(wp_unslash($_REQUEST['data'])); |
|
203 | 203 | } else { |
204 | 204 | wp_send_json_error( |
205 | 205 | array( |
206 | - 'message' => __( 'Recipe data is empty.', 'wordlift' ), |
|
207 | - 'btnText' => __( 'Failed', 'wordlift' ), |
|
206 | + 'message' => __('Recipe data is empty.', 'wordlift'), |
|
207 | + 'btnText' => __('Failed', 'wordlift'), |
|
208 | 208 | ) |
209 | 209 | ); |
210 | 210 | } |
211 | 211 | |
212 | - $recipes = json_decode( $recipe_data ); |
|
212 | + $recipes = json_decode($recipe_data); |
|
213 | 213 | |
214 | 214 | $updated = false; |
215 | - foreach ( $recipes as $recipe ) { |
|
215 | + foreach ($recipes as $recipe) { |
|
216 | 216 | $recipe_id = $recipe->recipe_id; |
217 | 217 | $main_ingredient = $recipe->ingredient; |
218 | - if ( 'UNSET' === $main_ingredient ) { |
|
219 | - $updated = delete_post_meta( $recipe_id, '_wl_main_ingredient_jsonld' ); |
|
220 | - } elseif ( 'DONT_CHANGE' === $main_ingredient ) { |
|
218 | + if ('UNSET' === $main_ingredient) { |
|
219 | + $updated = delete_post_meta($recipe_id, '_wl_main_ingredient_jsonld'); |
|
220 | + } elseif ('DONT_CHANGE' === $main_ingredient) { |
|
221 | 221 | $updated = true; |
222 | 222 | } else { |
223 | - $main_ingredient = wp_json_encode( json_decode( $recipe->ingredient, true ) ); |
|
224 | - $updated = update_post_meta( $recipe_id, '_wl_main_ingredient_jsonld', $main_ingredient ); |
|
223 | + $main_ingredient = wp_json_encode(json_decode($recipe->ingredient, true)); |
|
224 | + $updated = update_post_meta($recipe_id, '_wl_main_ingredient_jsonld', $main_ingredient); |
|
225 | 225 | } |
226 | 226 | } |
227 | 227 | |
228 | 228 | // Since we changed the main ingredients we want to flush all caches. |
229 | 229 | Ttl_Cache::flush_all(); |
230 | 230 | |
231 | - if ( $updated ) { |
|
231 | + if ($updated) { |
|
232 | 232 | wp_send_json_success( |
233 | 233 | array( |
234 | - 'message' => __( 'The main ingredient has been updated.', 'wordlift' ), |
|
234 | + 'message' => __('The main ingredient has been updated.', 'wordlift'), |
|
235 | 235 | ) |
236 | 236 | ); |
237 | 237 | } else { |
238 | 238 | wp_send_json_error( |
239 | 239 | array( |
240 | - 'message' => __( 'The main ingredient could not be updated.', 'wordlift' ), |
|
240 | + 'message' => __('The main ingredient could not be updated.', 'wordlift'), |
|
241 | 241 | ) |
242 | 242 | ); |
243 | 243 | } |
@@ -250,8 +250,8 @@ discard block |
||
250 | 250 | |
251 | 251 | wp_enqueue_script( |
252 | 252 | 'wl-meta-box-ingredient', |
253 | - WL_DIR_URL . 'js/dist/ingredients-meta-box.js', |
|
254 | - array( 'react', 'react-dom', 'wp-polyfill' ), |
|
253 | + WL_DIR_URL.'js/dist/ingredients-meta-box.js', |
|
254 | + array('react', 'react-dom', 'wp-polyfill'), |
|
255 | 255 | WORDLIFT_VERSION, |
256 | 256 | true |
257 | 257 | ); |
@@ -260,15 +260,15 @@ discard block |
||
260 | 260 | 'wl-meta-box-ingredient', |
261 | 261 | '_wlRecipeIngredientSettings', |
262 | 262 | array( |
263 | - 'ajaxurl' => admin_url( 'admin-ajax.php' ), |
|
264 | - 'nonce' => wp_create_nonce( 'wl-ingredient-nonce' ), |
|
265 | - 'acNonce' => wp_create_nonce( 'wl-ac-ingredient-nonce' ), |
|
263 | + 'ajaxurl' => admin_url('admin-ajax.php'), |
|
264 | + 'nonce' => wp_create_nonce('wl-ingredient-nonce'), |
|
265 | + 'acNonce' => wp_create_nonce('wl-ac-ingredient-nonce'), |
|
266 | 266 | 'l10n' => array( |
267 | - 'Looking for main ingredients...' => _x( 'Looking for main ingredients...', 'Main Ingredient select', 'wordlift' ), |
|
268 | - 'Type at least 3 characters to search...' => _x( 'Type at least 3 characters to search...', 'Main Ingredient select', 'wordlift' ), |
|
269 | - 'No results found for your search.' => _x( 'No results found: try changing or removing some words.', 'Main Ingredient select', 'wordlift' ), |
|
270 | - "(don't change)" => _x( "(don't change)", 'Main Ingredient select', 'wordlift' ), |
|
271 | - '(unset)' => _x( '(unset)', 'Main Ingredient select', 'wordlift' ), |
|
267 | + 'Looking for main ingredients...' => _x('Looking for main ingredients...', 'Main Ingredient select', 'wordlift'), |
|
268 | + 'Type at least 3 characters to search...' => _x('Type at least 3 characters to search...', 'Main Ingredient select', 'wordlift'), |
|
269 | + 'No results found for your search.' => _x('No results found: try changing or removing some words.', 'Main Ingredient select', 'wordlift'), |
|
270 | + "(don't change)" => _x("(don't change)", 'Main Ingredient select', 'wordlift'), |
|
271 | + '(unset)' => _x('(unset)', 'Main Ingredient select', 'wordlift'), |
|
272 | 272 | ), |
273 | 273 | ) |
274 | 274 | ); |