@@ -11,7 +11,7 @@ discard block |
||
| 11 | 11 | */ |
| 12 | 12 | |
| 13 | 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | - exit; |
|
| 14 | + exit; |
|
| 15 | 15 | } |
| 16 | 16 | |
| 17 | 17 | /** |
@@ -21,716 +21,716 @@ discard block |
||
| 21 | 21 | */ |
| 22 | 22 | class Wordlift_Configuration_Service { |
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * The entity base path option name. |
|
| 26 | - * |
|
| 27 | - * @since 3.6.0 |
|
| 28 | - */ |
|
| 29 | - const ENTITY_BASE_PATH_KEY = 'wl_entity_base_path'; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * The skip wizard (admin installation wizard) option name. |
|
| 33 | - * |
|
| 34 | - * @since 3.9.0 |
|
| 35 | - */ |
|
| 36 | - const SKIP_WIZARD = 'wl_skip_wizard'; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * WordLift's key option name. |
|
| 40 | - * |
|
| 41 | - * @since 3.9.0 |
|
| 42 | - */ |
|
| 43 | - const KEY = 'key'; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * WordLift's configured language option name. |
|
| 47 | - * |
|
| 48 | - * @since 3.9.0 |
|
| 49 | - */ |
|
| 50 | - const LANGUAGE = 'site_language'; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * WordLift's configured country code. |
|
| 54 | - * |
|
| 55 | - * @since 3.18.0 |
|
| 56 | - */ |
|
| 57 | - const COUNTRY_CODE = 'country_code'; |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * The publisher entity post ID option name. |
|
| 61 | - * |
|
| 62 | - * @since 3.9.0 |
|
| 63 | - */ |
|
| 64 | - const PUBLISHER_ID = 'publisher_id'; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * The dataset URI option name |
|
| 68 | - * |
|
| 69 | - * @since 3.10.0 |
|
| 70 | - */ |
|
| 71 | - const DATASET_URI = 'redlink_dataset_uri'; |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * The link by default option name. |
|
| 75 | - * |
|
| 76 | - * @since 3.11.0 |
|
| 77 | - */ |
|
| 78 | - const LINK_BY_DEFAULT = 'link_by_default'; |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * The analytics enable option. |
|
| 82 | - * |
|
| 83 | - * @since 3.21.0 |
|
| 84 | - */ |
|
| 85 | - const ANALYTICS_ENABLE = 'analytics_enable'; |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * The analytics entity uri dimension option. |
|
| 89 | - * |
|
| 90 | - * @since 3.21.0 |
|
| 91 | - */ |
|
| 92 | - const ANALYTICS_ENTITY_URI_DIMENSION = 'analytics_entity_uri_dimension'; |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * The analytics entity type dimension option. |
|
| 96 | - * |
|
| 97 | - * @since 3.21.0 |
|
| 98 | - */ |
|
| 99 | - const ANALYTICS_ENTITY_TYPE_DIMENSION = 'analytics_entity_type_dimension'; |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * The user preferences about sharing data option. |
|
| 103 | - * |
|
| 104 | - * @since 3.19.0 |
|
| 105 | - */ |
|
| 106 | - const SEND_DIAGNOSTIC = 'send_diagnostic'; |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * The package type configuration key. |
|
| 110 | - * |
|
| 111 | - * @since 3.20.0 |
|
| 112 | - */ |
|
| 113 | - const PACKAGE_TYPE = 'package_type'; |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * The {@link Wordlift_Log_Service} instance. |
|
| 117 | - * |
|
| 118 | - * @since 3.16.0 |
|
| 119 | - * |
|
| 120 | - * @var \Wordlift_Log_Service $log The {@link Wordlift_Log_Service} instance. |
|
| 121 | - */ |
|
| 122 | - private $log; |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * The Wordlift_Configuration_Service's singleton instance. |
|
| 126 | - * |
|
| 127 | - * @since 3.6.0 |
|
| 128 | - * |
|
| 129 | - * @access private |
|
| 130 | - * @var \Wordlift_Configuration_Service $instance Wordlift_Configuration_Service's singleton instance. |
|
| 131 | - */ |
|
| 132 | - private static $instance; |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * Create a Wordlift_Configuration_Service's instance. |
|
| 136 | - * |
|
| 137 | - * @since 3.6.0 |
|
| 138 | - */ |
|
| 139 | - public function __construct() { |
|
| 140 | - |
|
| 141 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 142 | - |
|
| 143 | - self::$instance = $this; |
|
| 144 | - |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * Get the singleton instance. |
|
| 149 | - * |
|
| 150 | - * @return \Wordlift_Configuration_Service |
|
| 151 | - * @since 3.6.0 |
|
| 152 | - * |
|
| 153 | - */ |
|
| 154 | - public static function get_instance() { |
|
| 155 | - |
|
| 156 | - return self::$instance; |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * Get a configuration given the option name and a key. The option value is |
|
| 161 | - * expected to be an array. |
|
| 162 | - * |
|
| 163 | - * @param string $option The option name. |
|
| 164 | - * @param string $key A key in the option value array. |
|
| 165 | - * @param string $default The default value in case the key is not found (by default an empty string). |
|
| 166 | - * |
|
| 167 | - * @return mixed The configuration value or the default value if not found. |
|
| 168 | - * @since 3.6.0 |
|
| 169 | - * |
|
| 170 | - */ |
|
| 171 | - private function get( $option, $key, $default = '' ) { |
|
| 172 | - |
|
| 173 | - $options = get_option( $option, array() ); |
|
| 174 | - |
|
| 175 | - return isset( $options[ $key ] ) ? $options[ $key ] : $default; |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * Set a configuration parameter. |
|
| 180 | - * |
|
| 181 | - * @param string $option Name of option to retrieve. Expected to not be SQL-escaped. |
|
| 182 | - * @param string $key The value key. |
|
| 183 | - * @param mixed $value The value. |
|
| 184 | - * |
|
| 185 | - * @since 3.9.0 |
|
| 186 | - * |
|
| 187 | - */ |
|
| 188 | - private function set( $option, $key, $value ) { |
|
| 189 | - |
|
| 190 | - $values = get_option( $option ); |
|
| 191 | - $values = isset( $values ) ? $values : array(); |
|
| 192 | - $values[ $key ] = $value; |
|
| 193 | - update_option( $option, $values ); |
|
| 194 | - |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * Get the entity base path, by default 'entity'. |
|
| 199 | - * |
|
| 200 | - * @return string The entity base path. |
|
| 201 | - * @since 3.6.0 |
|
| 202 | - * |
|
| 203 | - */ |
|
| 204 | - public function get_entity_base_path() { |
|
| 205 | - |
|
| 206 | - return $this->get( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, 'entity' ); |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - /** |
|
| 210 | - * Get the entity base path. |
|
| 211 | - * |
|
| 212 | - * @param string $value The entity base path. |
|
| 213 | - * |
|
| 214 | - * @since 3.9.0 |
|
| 215 | - * |
|
| 216 | - */ |
|
| 217 | - public function set_entity_base_path( $value ) { |
|
| 218 | - |
|
| 219 | - $this->set( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, $value ); |
|
| 220 | - |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - /** |
|
| 224 | - * Whether the installation skip wizard should be skipped. |
|
| 225 | - * |
|
| 226 | - * @return bool True if it should be skipped otherwise false. |
|
| 227 | - * @since 3.9.0 |
|
| 228 | - * |
|
| 229 | - */ |
|
| 230 | - public function is_skip_wizard() { |
|
| 231 | - |
|
| 232 | - return $this->get( 'wl_general_settings', self::SKIP_WIZARD, false ); |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - /** |
|
| 236 | - * Set the skip wizard parameter. |
|
| 237 | - * |
|
| 238 | - * @param bool $value True to skip the wizard. We expect a boolean value. |
|
| 239 | - * |
|
| 240 | - * @since 3.9.0 |
|
| 241 | - * |
|
| 242 | - */ |
|
| 243 | - public function set_skip_wizard( $value ) { |
|
| 244 | - |
|
| 245 | - $this->set( 'wl_general_settings', self::SKIP_WIZARD, true === $value ); |
|
| 246 | - |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - /** |
|
| 250 | - * Get WordLift's key. |
|
| 251 | - * |
|
| 252 | - * @return string WordLift's key or an empty string if not set. |
|
| 253 | - * @since 3.9.0 |
|
| 254 | - * |
|
| 255 | - */ |
|
| 256 | - public function get_key() { |
|
| 257 | - |
|
| 258 | - return $this->get( 'wl_general_settings', self::KEY, '' ); |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - /** |
|
| 262 | - * Set WordLift's key. |
|
| 263 | - * |
|
| 264 | - * @param string $value WordLift's key. |
|
| 265 | - * |
|
| 266 | - * @since 3.9.0 |
|
| 267 | - * |
|
| 268 | - */ |
|
| 269 | - public function set_key( $value ) { |
|
| 270 | - |
|
| 271 | - $this->set( 'wl_general_settings', self::KEY, $value ); |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - /** |
|
| 275 | - * Get WordLift's configured language, by default 'en'. |
|
| 276 | - * |
|
| 277 | - * Note that WordLift's language is used when writing strings to the Linked Data dataset, not for the analysis. |
|
| 278 | - * |
|
| 279 | - * @return string WordLift's configured language code ('en' by default). |
|
| 280 | - * @since 3.9.0 |
|
| 281 | - * |
|
| 282 | - */ |
|
| 283 | - public function get_language_code() { |
|
| 284 | - |
|
| 285 | - return $this->get( 'wl_general_settings', self::LANGUAGE, 'en' ); |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - /** |
|
| 289 | - * Set WordLift's language code, used when storing strings to the Linked Data dataset. |
|
| 290 | - * |
|
| 291 | - * @param string $value WordLift's language code. |
|
| 292 | - * |
|
| 293 | - * @since 3.9.0 |
|
| 294 | - * |
|
| 295 | - */ |
|
| 296 | - public function set_language_code( $value ) { |
|
| 297 | - |
|
| 298 | - $this->set( 'wl_general_settings', self::LANGUAGE, $value ); |
|
| 299 | - |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - /** |
|
| 303 | - * Set the user preferences about sharing diagnostic with us. |
|
| 304 | - * |
|
| 305 | - * @param string $value The user preferences(yes/no). |
|
| 306 | - * |
|
| 307 | - * @since 3.19.0 |
|
| 308 | - * |
|
| 309 | - */ |
|
| 310 | - public function set_diagnostic_preferences( $value ) { |
|
| 311 | - |
|
| 312 | - $this->set( 'wl_general_settings', self::SEND_DIAGNOSTIC, $value ); |
|
| 313 | - |
|
| 314 | - } |
|
| 315 | - |
|
| 316 | - /** |
|
| 317 | - * Get the user preferences about sharing diagnostic. |
|
| 318 | - * |
|
| 319 | - * @since 3.19.0 |
|
| 320 | - */ |
|
| 321 | - public function get_diagnostic_preferences() { |
|
| 322 | - |
|
| 323 | - return $this->get( 'wl_general_settings', self::SEND_DIAGNOSTIC, 'no' ); |
|
| 324 | - } |
|
| 325 | - |
|
| 326 | - /** |
|
| 327 | - * Get WordLift's configured country code, by default 'us'. |
|
| 328 | - * |
|
| 329 | - * @return string WordLift's configured country code ('us' by default). |
|
| 330 | - * @since 3.18.0 |
|
| 331 | - * |
|
| 332 | - */ |
|
| 333 | - public function get_country_code() { |
|
| 334 | - |
|
| 335 | - return $this->get( 'wl_general_settings', self::COUNTRY_CODE, 'us' ); |
|
| 336 | - } |
|
| 337 | - |
|
| 338 | - /** |
|
| 339 | - * Set WordLift's country code. |
|
| 340 | - * |
|
| 341 | - * @param string $value WordLift's country code. |
|
| 342 | - * |
|
| 343 | - * @since 3.18.0 |
|
| 344 | - * |
|
| 345 | - */ |
|
| 346 | - public function set_country_code( $value ) { |
|
| 347 | - |
|
| 348 | - $this->set( 'wl_general_settings', self::COUNTRY_CODE, $value ); |
|
| 349 | - |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - /** |
|
| 353 | - * Get the publisher entity post id. |
|
| 354 | - * |
|
| 355 | - * The publisher entity post id points to an entity post which contains the data for the publisher used in schema.org |
|
| 356 | - * Article markup. |
|
| 357 | - * |
|
| 358 | - * @return int|NULL The publisher entity post id or NULL if not set. |
|
| 359 | - * @since 3.9.0 |
|
| 360 | - * |
|
| 361 | - */ |
|
| 362 | - public function get_publisher_id() { |
|
| 363 | - |
|
| 364 | - return $this->get( 'wl_general_settings', self::PUBLISHER_ID, null ); |
|
| 365 | - } |
|
| 366 | - |
|
| 367 | - /** |
|
| 368 | - * Set the publisher entity post id. |
|
| 369 | - * |
|
| 370 | - * @param int $value The publisher entity post id. |
|
| 371 | - * |
|
| 372 | - * @since 3.9.0 |
|
| 373 | - * |
|
| 374 | - */ |
|
| 375 | - public function set_publisher_id( $value ) { |
|
| 376 | - |
|
| 377 | - $this->set( 'wl_general_settings', self::PUBLISHER_ID, $value ); |
|
| 378 | - |
|
| 379 | - } |
|
| 380 | - |
|
| 381 | - /** |
|
| 382 | - * Get the dataset URI. |
|
| 383 | - * |
|
| 384 | - * @return string The dataset URI or an empty string if not set. |
|
| 385 | - * @since 3.10.0 |
|
| 386 | - * |
|
| 387 | - */ |
|
| 388 | - public function get_dataset_uri() { |
|
| 389 | - |
|
| 390 | - return $this->get( 'wl_advanced_settings', self::DATASET_URI, null ); |
|
| 391 | - } |
|
| 392 | - |
|
| 393 | - /** |
|
| 394 | - * Set the dataset URI. |
|
| 395 | - * |
|
| 396 | - * @param string $value The dataset URI. |
|
| 397 | - * |
|
| 398 | - * @since 3.10.0 |
|
| 399 | - * |
|
| 400 | - */ |
|
| 401 | - public function set_dataset_uri( $value ) { |
|
| 402 | - |
|
| 403 | - $this->set( 'wl_advanced_settings', self::DATASET_URI, $value ); |
|
| 404 | - } |
|
| 405 | - |
|
| 406 | - /** |
|
| 407 | - * Get the package type. |
|
| 408 | - * |
|
| 409 | - * @return string The package type or an empty string if not set. |
|
| 410 | - * @since 3.20.0 |
|
| 411 | - * |
|
| 412 | - */ |
|
| 413 | - public function get_package_type() { |
|
| 414 | - |
|
| 415 | - return $this->get( 'wl_advanced_settings', self::PACKAGE_TYPE, null ); |
|
| 416 | - } |
|
| 417 | - |
|
| 418 | - /** |
|
| 419 | - * Set the package type. |
|
| 420 | - * |
|
| 421 | - * @param string $value The package type. |
|
| 422 | - * |
|
| 423 | - * @since 3.20.0 |
|
| 424 | - * |
|
| 425 | - */ |
|
| 426 | - public function set_package_type( $value ) { |
|
| 427 | - |
|
| 428 | - $this->set( 'wl_advanced_settings', self::PACKAGE_TYPE, $value ); |
|
| 429 | - } |
|
| 430 | - |
|
| 431 | - /** |
|
| 432 | - * Intercept the change of the WordLift key in order to set the dataset URI. |
|
| 433 | - * |
|
| 434 | - * |
|
| 435 | - * @since 3.20.0 as of #761, we save settings every time a key is set, not only when the key changes, so to |
|
| 436 | - * store the configuration parameters such as country or language. |
|
| 437 | - * @since 3.11.0 |
|
| 438 | - * |
|
| 439 | - * @see https://github.com/insideout10/wordlift-plugin/issues/761 |
|
| 440 | - * |
|
| 441 | - * @param array $old_value The old settings. |
|
| 442 | - * @param array $new_value The new settings. |
|
| 443 | - */ |
|
| 444 | - public function update_key( $old_value, $new_value ) { |
|
| 445 | - |
|
| 446 | - // Check the old key value and the new one. We're going to ask for the dataset URI only if the key has changed. |
|
| 447 | - // $old_key = isset( $old_value['key'] ) ? $old_value['key'] : ''; |
|
| 448 | - $new_key = isset( $new_value['key'] ) ? $new_value['key'] : ''; |
|
| 449 | - |
|
| 450 | - // If the key hasn't changed, don't do anything. |
|
| 451 | - // WARN The 'update_option' hook is fired only if the new and old value are not equal. |
|
| 452 | - // if ( $old_key === $new_key ) { |
|
| 453 | - // return; |
|
| 454 | - // } |
|
| 455 | - |
|
| 456 | - // If the key is empty, empty the dataset URI. |
|
| 457 | - if ( '' === $new_key ) { |
|
| 458 | - $this->set_dataset_uri( '' ); |
|
| 459 | - } |
|
| 460 | - |
|
| 461 | - // make the request to the remote server. |
|
| 462 | - $this->get_remote_dataset_uri( $new_key ); |
|
| 463 | - |
|
| 464 | - } |
|
| 465 | - |
|
| 466 | - /** |
|
| 467 | - * Handle retrieving the dataset uri from the remote server. |
|
| 468 | - * |
|
| 469 | - * If a valid dataset uri is returned it is stored in the appropriate option, |
|
| 470 | - * otherwise the option is set to empty string. |
|
| 471 | - * |
|
| 472 | - * @param string $key The key to be used. |
|
| 473 | - * |
|
| 474 | - * @since 3.12.0 |
|
| 475 | - * |
|
| 476 | - * @since 3.17.0 send the site URL and get the dataset URI. |
|
| 477 | - */ |
|
| 478 | - public function get_remote_dataset_uri( $key ) { |
|
| 479 | - |
|
| 480 | - $this->log->trace( 'Getting the remote dataset URI and package type...' ); |
|
| 481 | - |
|
| 482 | - if ( empty( $key ) ) { |
|
| 483 | - $this->log->warn( 'Key set to empty value.' ); |
|
| 484 | - |
|
| 485 | - $this->set_dataset_uri( '' ); |
|
| 486 | - $this->set_package_type( null ); |
|
| 487 | - |
|
| 488 | - return; |
|
| 489 | - } |
|
| 490 | - |
|
| 491 | - /** |
|
| 492 | - * Allow 3rd parties to change the site_url. |
|
| 493 | - * |
|
| 494 | - * @param string $site_url The site url. |
|
| 495 | - * |
|
| 496 | - * @see https://github.com/insideout10/wordlift-plugin/issues/850 |
|
| 497 | - * |
|
| 498 | - * @since 3.20.0 |
|
| 499 | - * |
|
| 500 | - */ |
|
| 501 | - $home_url = defined( 'WP_HOME' ) ? WP_HOME : get_option( 'home' ); |
|
| 502 | - $site_url = apply_filters( 'wl_production_site_url', $home_url ); |
|
| 503 | - |
|
| 504 | - // Build the URL. |
|
| 505 | - $url = $this->get_accounts() |
|
| 506 | - . '?key=' . rawurlencode( $key ) |
|
| 507 | - . '&url=' . rawurlencode( $site_url ) |
|
| 508 | - . '&country=' . $this->get_country_code() |
|
| 509 | - . '&language=' . $this->get_language_code(); |
|
| 510 | - |
|
| 511 | - $args = wp_parse_args( unserialize( WL_REDLINK_API_HTTP_OPTIONS ), array( |
|
| 512 | - 'method' => 'PUT', |
|
| 513 | - ) ); |
|
| 514 | - |
|
| 515 | - $response = wp_remote_request( $url, $args ); |
|
| 516 | - |
|
| 517 | - // The response is an error. |
|
| 518 | - if ( is_wp_error( $response ) ) { |
|
| 519 | - $this->log->error( 'An error occurred setting the dataset URI: ' . $response->get_error_message() ); |
|
| 520 | - |
|
| 521 | - $this->set_dataset_uri( '' ); |
|
| 522 | - $this->set_package_type( null ); |
|
| 523 | - |
|
| 524 | - return; |
|
| 525 | - } |
|
| 526 | - |
|
| 527 | - // The response is not OK. |
|
| 528 | - if ( 200 !== (int) $response['response']['code'] ) { |
|
| 529 | - $this->log->error( "Unexpected status code when opening URL $url: " . $response['response']['code'] ); |
|
| 530 | - |
|
| 531 | - $this->set_dataset_uri( '' ); |
|
| 532 | - $this->set_package_type( null ); |
|
| 533 | - |
|
| 534 | - return; |
|
| 535 | - } |
|
| 536 | - |
|
| 537 | - /* |
|
| 24 | + /** |
|
| 25 | + * The entity base path option name. |
|
| 26 | + * |
|
| 27 | + * @since 3.6.0 |
|
| 28 | + */ |
|
| 29 | + const ENTITY_BASE_PATH_KEY = 'wl_entity_base_path'; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * The skip wizard (admin installation wizard) option name. |
|
| 33 | + * |
|
| 34 | + * @since 3.9.0 |
|
| 35 | + */ |
|
| 36 | + const SKIP_WIZARD = 'wl_skip_wizard'; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * WordLift's key option name. |
|
| 40 | + * |
|
| 41 | + * @since 3.9.0 |
|
| 42 | + */ |
|
| 43 | + const KEY = 'key'; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * WordLift's configured language option name. |
|
| 47 | + * |
|
| 48 | + * @since 3.9.0 |
|
| 49 | + */ |
|
| 50 | + const LANGUAGE = 'site_language'; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * WordLift's configured country code. |
|
| 54 | + * |
|
| 55 | + * @since 3.18.0 |
|
| 56 | + */ |
|
| 57 | + const COUNTRY_CODE = 'country_code'; |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * The publisher entity post ID option name. |
|
| 61 | + * |
|
| 62 | + * @since 3.9.0 |
|
| 63 | + */ |
|
| 64 | + const PUBLISHER_ID = 'publisher_id'; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * The dataset URI option name |
|
| 68 | + * |
|
| 69 | + * @since 3.10.0 |
|
| 70 | + */ |
|
| 71 | + const DATASET_URI = 'redlink_dataset_uri'; |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * The link by default option name. |
|
| 75 | + * |
|
| 76 | + * @since 3.11.0 |
|
| 77 | + */ |
|
| 78 | + const LINK_BY_DEFAULT = 'link_by_default'; |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * The analytics enable option. |
|
| 82 | + * |
|
| 83 | + * @since 3.21.0 |
|
| 84 | + */ |
|
| 85 | + const ANALYTICS_ENABLE = 'analytics_enable'; |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * The analytics entity uri dimension option. |
|
| 89 | + * |
|
| 90 | + * @since 3.21.0 |
|
| 91 | + */ |
|
| 92 | + const ANALYTICS_ENTITY_URI_DIMENSION = 'analytics_entity_uri_dimension'; |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * The analytics entity type dimension option. |
|
| 96 | + * |
|
| 97 | + * @since 3.21.0 |
|
| 98 | + */ |
|
| 99 | + const ANALYTICS_ENTITY_TYPE_DIMENSION = 'analytics_entity_type_dimension'; |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * The user preferences about sharing data option. |
|
| 103 | + * |
|
| 104 | + * @since 3.19.0 |
|
| 105 | + */ |
|
| 106 | + const SEND_DIAGNOSTIC = 'send_diagnostic'; |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * The package type configuration key. |
|
| 110 | + * |
|
| 111 | + * @since 3.20.0 |
|
| 112 | + */ |
|
| 113 | + const PACKAGE_TYPE = 'package_type'; |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * The {@link Wordlift_Log_Service} instance. |
|
| 117 | + * |
|
| 118 | + * @since 3.16.0 |
|
| 119 | + * |
|
| 120 | + * @var \Wordlift_Log_Service $log The {@link Wordlift_Log_Service} instance. |
|
| 121 | + */ |
|
| 122 | + private $log; |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * The Wordlift_Configuration_Service's singleton instance. |
|
| 126 | + * |
|
| 127 | + * @since 3.6.0 |
|
| 128 | + * |
|
| 129 | + * @access private |
|
| 130 | + * @var \Wordlift_Configuration_Service $instance Wordlift_Configuration_Service's singleton instance. |
|
| 131 | + */ |
|
| 132 | + private static $instance; |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * Create a Wordlift_Configuration_Service's instance. |
|
| 136 | + * |
|
| 137 | + * @since 3.6.0 |
|
| 138 | + */ |
|
| 139 | + public function __construct() { |
|
| 140 | + |
|
| 141 | + $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 142 | + |
|
| 143 | + self::$instance = $this; |
|
| 144 | + |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * Get the singleton instance. |
|
| 149 | + * |
|
| 150 | + * @return \Wordlift_Configuration_Service |
|
| 151 | + * @since 3.6.0 |
|
| 152 | + * |
|
| 153 | + */ |
|
| 154 | + public static function get_instance() { |
|
| 155 | + |
|
| 156 | + return self::$instance; |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * Get a configuration given the option name and a key. The option value is |
|
| 161 | + * expected to be an array. |
|
| 162 | + * |
|
| 163 | + * @param string $option The option name. |
|
| 164 | + * @param string $key A key in the option value array. |
|
| 165 | + * @param string $default The default value in case the key is not found (by default an empty string). |
|
| 166 | + * |
|
| 167 | + * @return mixed The configuration value or the default value if not found. |
|
| 168 | + * @since 3.6.0 |
|
| 169 | + * |
|
| 170 | + */ |
|
| 171 | + private function get( $option, $key, $default = '' ) { |
|
| 172 | + |
|
| 173 | + $options = get_option( $option, array() ); |
|
| 174 | + |
|
| 175 | + return isset( $options[ $key ] ) ? $options[ $key ] : $default; |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * Set a configuration parameter. |
|
| 180 | + * |
|
| 181 | + * @param string $option Name of option to retrieve. Expected to not be SQL-escaped. |
|
| 182 | + * @param string $key The value key. |
|
| 183 | + * @param mixed $value The value. |
|
| 184 | + * |
|
| 185 | + * @since 3.9.0 |
|
| 186 | + * |
|
| 187 | + */ |
|
| 188 | + private function set( $option, $key, $value ) { |
|
| 189 | + |
|
| 190 | + $values = get_option( $option ); |
|
| 191 | + $values = isset( $values ) ? $values : array(); |
|
| 192 | + $values[ $key ] = $value; |
|
| 193 | + update_option( $option, $values ); |
|
| 194 | + |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * Get the entity base path, by default 'entity'. |
|
| 199 | + * |
|
| 200 | + * @return string The entity base path. |
|
| 201 | + * @since 3.6.0 |
|
| 202 | + * |
|
| 203 | + */ |
|
| 204 | + public function get_entity_base_path() { |
|
| 205 | + |
|
| 206 | + return $this->get( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, 'entity' ); |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + /** |
|
| 210 | + * Get the entity base path. |
|
| 211 | + * |
|
| 212 | + * @param string $value The entity base path. |
|
| 213 | + * |
|
| 214 | + * @since 3.9.0 |
|
| 215 | + * |
|
| 216 | + */ |
|
| 217 | + public function set_entity_base_path( $value ) { |
|
| 218 | + |
|
| 219 | + $this->set( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, $value ); |
|
| 220 | + |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + /** |
|
| 224 | + * Whether the installation skip wizard should be skipped. |
|
| 225 | + * |
|
| 226 | + * @return bool True if it should be skipped otherwise false. |
|
| 227 | + * @since 3.9.0 |
|
| 228 | + * |
|
| 229 | + */ |
|
| 230 | + public function is_skip_wizard() { |
|
| 231 | + |
|
| 232 | + return $this->get( 'wl_general_settings', self::SKIP_WIZARD, false ); |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + /** |
|
| 236 | + * Set the skip wizard parameter. |
|
| 237 | + * |
|
| 238 | + * @param bool $value True to skip the wizard. We expect a boolean value. |
|
| 239 | + * |
|
| 240 | + * @since 3.9.0 |
|
| 241 | + * |
|
| 242 | + */ |
|
| 243 | + public function set_skip_wizard( $value ) { |
|
| 244 | + |
|
| 245 | + $this->set( 'wl_general_settings', self::SKIP_WIZARD, true === $value ); |
|
| 246 | + |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + /** |
|
| 250 | + * Get WordLift's key. |
|
| 251 | + * |
|
| 252 | + * @return string WordLift's key or an empty string if not set. |
|
| 253 | + * @since 3.9.0 |
|
| 254 | + * |
|
| 255 | + */ |
|
| 256 | + public function get_key() { |
|
| 257 | + |
|
| 258 | + return $this->get( 'wl_general_settings', self::KEY, '' ); |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + /** |
|
| 262 | + * Set WordLift's key. |
|
| 263 | + * |
|
| 264 | + * @param string $value WordLift's key. |
|
| 265 | + * |
|
| 266 | + * @since 3.9.0 |
|
| 267 | + * |
|
| 268 | + */ |
|
| 269 | + public function set_key( $value ) { |
|
| 270 | + |
|
| 271 | + $this->set( 'wl_general_settings', self::KEY, $value ); |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + /** |
|
| 275 | + * Get WordLift's configured language, by default 'en'. |
|
| 276 | + * |
|
| 277 | + * Note that WordLift's language is used when writing strings to the Linked Data dataset, not for the analysis. |
|
| 278 | + * |
|
| 279 | + * @return string WordLift's configured language code ('en' by default). |
|
| 280 | + * @since 3.9.0 |
|
| 281 | + * |
|
| 282 | + */ |
|
| 283 | + public function get_language_code() { |
|
| 284 | + |
|
| 285 | + return $this->get( 'wl_general_settings', self::LANGUAGE, 'en' ); |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + /** |
|
| 289 | + * Set WordLift's language code, used when storing strings to the Linked Data dataset. |
|
| 290 | + * |
|
| 291 | + * @param string $value WordLift's language code. |
|
| 292 | + * |
|
| 293 | + * @since 3.9.0 |
|
| 294 | + * |
|
| 295 | + */ |
|
| 296 | + public function set_language_code( $value ) { |
|
| 297 | + |
|
| 298 | + $this->set( 'wl_general_settings', self::LANGUAGE, $value ); |
|
| 299 | + |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + /** |
|
| 303 | + * Set the user preferences about sharing diagnostic with us. |
|
| 304 | + * |
|
| 305 | + * @param string $value The user preferences(yes/no). |
|
| 306 | + * |
|
| 307 | + * @since 3.19.0 |
|
| 308 | + * |
|
| 309 | + */ |
|
| 310 | + public function set_diagnostic_preferences( $value ) { |
|
| 311 | + |
|
| 312 | + $this->set( 'wl_general_settings', self::SEND_DIAGNOSTIC, $value ); |
|
| 313 | + |
|
| 314 | + } |
|
| 315 | + |
|
| 316 | + /** |
|
| 317 | + * Get the user preferences about sharing diagnostic. |
|
| 318 | + * |
|
| 319 | + * @since 3.19.0 |
|
| 320 | + */ |
|
| 321 | + public function get_diagnostic_preferences() { |
|
| 322 | + |
|
| 323 | + return $this->get( 'wl_general_settings', self::SEND_DIAGNOSTIC, 'no' ); |
|
| 324 | + } |
|
| 325 | + |
|
| 326 | + /** |
|
| 327 | + * Get WordLift's configured country code, by default 'us'. |
|
| 328 | + * |
|
| 329 | + * @return string WordLift's configured country code ('us' by default). |
|
| 330 | + * @since 3.18.0 |
|
| 331 | + * |
|
| 332 | + */ |
|
| 333 | + public function get_country_code() { |
|
| 334 | + |
|
| 335 | + return $this->get( 'wl_general_settings', self::COUNTRY_CODE, 'us' ); |
|
| 336 | + } |
|
| 337 | + |
|
| 338 | + /** |
|
| 339 | + * Set WordLift's country code. |
|
| 340 | + * |
|
| 341 | + * @param string $value WordLift's country code. |
|
| 342 | + * |
|
| 343 | + * @since 3.18.0 |
|
| 344 | + * |
|
| 345 | + */ |
|
| 346 | + public function set_country_code( $value ) { |
|
| 347 | + |
|
| 348 | + $this->set( 'wl_general_settings', self::COUNTRY_CODE, $value ); |
|
| 349 | + |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + /** |
|
| 353 | + * Get the publisher entity post id. |
|
| 354 | + * |
|
| 355 | + * The publisher entity post id points to an entity post which contains the data for the publisher used in schema.org |
|
| 356 | + * Article markup. |
|
| 357 | + * |
|
| 358 | + * @return int|NULL The publisher entity post id or NULL if not set. |
|
| 359 | + * @since 3.9.0 |
|
| 360 | + * |
|
| 361 | + */ |
|
| 362 | + public function get_publisher_id() { |
|
| 363 | + |
|
| 364 | + return $this->get( 'wl_general_settings', self::PUBLISHER_ID, null ); |
|
| 365 | + } |
|
| 366 | + |
|
| 367 | + /** |
|
| 368 | + * Set the publisher entity post id. |
|
| 369 | + * |
|
| 370 | + * @param int $value The publisher entity post id. |
|
| 371 | + * |
|
| 372 | + * @since 3.9.0 |
|
| 373 | + * |
|
| 374 | + */ |
|
| 375 | + public function set_publisher_id( $value ) { |
|
| 376 | + |
|
| 377 | + $this->set( 'wl_general_settings', self::PUBLISHER_ID, $value ); |
|
| 378 | + |
|
| 379 | + } |
|
| 380 | + |
|
| 381 | + /** |
|
| 382 | + * Get the dataset URI. |
|
| 383 | + * |
|
| 384 | + * @return string The dataset URI or an empty string if not set. |
|
| 385 | + * @since 3.10.0 |
|
| 386 | + * |
|
| 387 | + */ |
|
| 388 | + public function get_dataset_uri() { |
|
| 389 | + |
|
| 390 | + return $this->get( 'wl_advanced_settings', self::DATASET_URI, null ); |
|
| 391 | + } |
|
| 392 | + |
|
| 393 | + /** |
|
| 394 | + * Set the dataset URI. |
|
| 395 | + * |
|
| 396 | + * @param string $value The dataset URI. |
|
| 397 | + * |
|
| 398 | + * @since 3.10.0 |
|
| 399 | + * |
|
| 400 | + */ |
|
| 401 | + public function set_dataset_uri( $value ) { |
|
| 402 | + |
|
| 403 | + $this->set( 'wl_advanced_settings', self::DATASET_URI, $value ); |
|
| 404 | + } |
|
| 405 | + |
|
| 406 | + /** |
|
| 407 | + * Get the package type. |
|
| 408 | + * |
|
| 409 | + * @return string The package type or an empty string if not set. |
|
| 410 | + * @since 3.20.0 |
|
| 411 | + * |
|
| 412 | + */ |
|
| 413 | + public function get_package_type() { |
|
| 414 | + |
|
| 415 | + return $this->get( 'wl_advanced_settings', self::PACKAGE_TYPE, null ); |
|
| 416 | + } |
|
| 417 | + |
|
| 418 | + /** |
|
| 419 | + * Set the package type. |
|
| 420 | + * |
|
| 421 | + * @param string $value The package type. |
|
| 422 | + * |
|
| 423 | + * @since 3.20.0 |
|
| 424 | + * |
|
| 425 | + */ |
|
| 426 | + public function set_package_type( $value ) { |
|
| 427 | + |
|
| 428 | + $this->set( 'wl_advanced_settings', self::PACKAGE_TYPE, $value ); |
|
| 429 | + } |
|
| 430 | + |
|
| 431 | + /** |
|
| 432 | + * Intercept the change of the WordLift key in order to set the dataset URI. |
|
| 433 | + * |
|
| 434 | + * |
|
| 435 | + * @since 3.20.0 as of #761, we save settings every time a key is set, not only when the key changes, so to |
|
| 436 | + * store the configuration parameters such as country or language. |
|
| 437 | + * @since 3.11.0 |
|
| 438 | + * |
|
| 439 | + * @see https://github.com/insideout10/wordlift-plugin/issues/761 |
|
| 440 | + * |
|
| 441 | + * @param array $old_value The old settings. |
|
| 442 | + * @param array $new_value The new settings. |
|
| 443 | + */ |
|
| 444 | + public function update_key( $old_value, $new_value ) { |
|
| 445 | + |
|
| 446 | + // Check the old key value and the new one. We're going to ask for the dataset URI only if the key has changed. |
|
| 447 | + // $old_key = isset( $old_value['key'] ) ? $old_value['key'] : ''; |
|
| 448 | + $new_key = isset( $new_value['key'] ) ? $new_value['key'] : ''; |
|
| 449 | + |
|
| 450 | + // If the key hasn't changed, don't do anything. |
|
| 451 | + // WARN The 'update_option' hook is fired only if the new and old value are not equal. |
|
| 452 | + // if ( $old_key === $new_key ) { |
|
| 453 | + // return; |
|
| 454 | + // } |
|
| 455 | + |
|
| 456 | + // If the key is empty, empty the dataset URI. |
|
| 457 | + if ( '' === $new_key ) { |
|
| 458 | + $this->set_dataset_uri( '' ); |
|
| 459 | + } |
|
| 460 | + |
|
| 461 | + // make the request to the remote server. |
|
| 462 | + $this->get_remote_dataset_uri( $new_key ); |
|
| 463 | + |
|
| 464 | + } |
|
| 465 | + |
|
| 466 | + /** |
|
| 467 | + * Handle retrieving the dataset uri from the remote server. |
|
| 468 | + * |
|
| 469 | + * If a valid dataset uri is returned it is stored in the appropriate option, |
|
| 470 | + * otherwise the option is set to empty string. |
|
| 471 | + * |
|
| 472 | + * @param string $key The key to be used. |
|
| 473 | + * |
|
| 474 | + * @since 3.12.0 |
|
| 475 | + * |
|
| 476 | + * @since 3.17.0 send the site URL and get the dataset URI. |
|
| 477 | + */ |
|
| 478 | + public function get_remote_dataset_uri( $key ) { |
|
| 479 | + |
|
| 480 | + $this->log->trace( 'Getting the remote dataset URI and package type...' ); |
|
| 481 | + |
|
| 482 | + if ( empty( $key ) ) { |
|
| 483 | + $this->log->warn( 'Key set to empty value.' ); |
|
| 484 | + |
|
| 485 | + $this->set_dataset_uri( '' ); |
|
| 486 | + $this->set_package_type( null ); |
|
| 487 | + |
|
| 488 | + return; |
|
| 489 | + } |
|
| 490 | + |
|
| 491 | + /** |
|
| 492 | + * Allow 3rd parties to change the site_url. |
|
| 493 | + * |
|
| 494 | + * @param string $site_url The site url. |
|
| 495 | + * |
|
| 496 | + * @see https://github.com/insideout10/wordlift-plugin/issues/850 |
|
| 497 | + * |
|
| 498 | + * @since 3.20.0 |
|
| 499 | + * |
|
| 500 | + */ |
|
| 501 | + $home_url = defined( 'WP_HOME' ) ? WP_HOME : get_option( 'home' ); |
|
| 502 | + $site_url = apply_filters( 'wl_production_site_url', $home_url ); |
|
| 503 | + |
|
| 504 | + // Build the URL. |
|
| 505 | + $url = $this->get_accounts() |
|
| 506 | + . '?key=' . rawurlencode( $key ) |
|
| 507 | + . '&url=' . rawurlencode( $site_url ) |
|
| 508 | + . '&country=' . $this->get_country_code() |
|
| 509 | + . '&language=' . $this->get_language_code(); |
|
| 510 | + |
|
| 511 | + $args = wp_parse_args( unserialize( WL_REDLINK_API_HTTP_OPTIONS ), array( |
|
| 512 | + 'method' => 'PUT', |
|
| 513 | + ) ); |
|
| 514 | + |
|
| 515 | + $response = wp_remote_request( $url, $args ); |
|
| 516 | + |
|
| 517 | + // The response is an error. |
|
| 518 | + if ( is_wp_error( $response ) ) { |
|
| 519 | + $this->log->error( 'An error occurred setting the dataset URI: ' . $response->get_error_message() ); |
|
| 520 | + |
|
| 521 | + $this->set_dataset_uri( '' ); |
|
| 522 | + $this->set_package_type( null ); |
|
| 523 | + |
|
| 524 | + return; |
|
| 525 | + } |
|
| 526 | + |
|
| 527 | + // The response is not OK. |
|
| 528 | + if ( 200 !== (int) $response['response']['code'] ) { |
|
| 529 | + $this->log->error( "Unexpected status code when opening URL $url: " . $response['response']['code'] ); |
|
| 530 | + |
|
| 531 | + $this->set_dataset_uri( '' ); |
|
| 532 | + $this->set_package_type( null ); |
|
| 533 | + |
|
| 534 | + return; |
|
| 535 | + } |
|
| 536 | + |
|
| 537 | + /* |
|
| 538 | 538 | * We also store the package type. |
| 539 | 539 | * |
| 540 | 540 | * @since 3.20.0 |
| 541 | 541 | */ |
| 542 | - $json = json_decode( $response['body'] ); |
|
| 543 | - $dataset_uri = $json->datasetURI; |
|
| 544 | - $package_type = isset( $json->packageType ) ? $json->packageType : null; |
|
| 545 | - |
|
| 546 | - $this->log->info( "Updating [ dataset uri :: $dataset_uri ][ package type :: $package_type ]..." ); |
|
| 547 | - |
|
| 548 | - $this->set_dataset_uri( $dataset_uri ); |
|
| 549 | - $this->set_package_type( $package_type ); |
|
| 550 | - |
|
| 551 | - } |
|
| 552 | - |
|
| 553 | - /** |
|
| 554 | - * Handle the edge case where a user submits the same key again |
|
| 555 | - * when he does not have the dataset uri to regain it. |
|
| 556 | - * |
|
| 557 | - * This can not be handled in the normal option update hook because |
|
| 558 | - * it is not being triggered when the save value equals to the one already |
|
| 559 | - * in the DB. |
|
| 560 | - * |
|
| 561 | - * @param mixed $value The new, unserialized option value. |
|
| 562 | - * @param mixed $old_value The old option value. |
|
| 563 | - * |
|
| 564 | - * @return mixed The same value in the $value parameter |
|
| 565 | - * @since 3.12.0 |
|
| 566 | - * |
|
| 567 | - */ |
|
| 568 | - function maybe_update_dataset_uri( $value, $old_value ) { |
|
| 569 | - |
|
| 570 | - // Check the old key value and the new one. Here we're only handling the |
|
| 571 | - // case where the key hasn't changed and the dataset URI isn't set. The |
|
| 572 | - // other case, i.e. a new key is inserted, is handled at `update_key`. |
|
| 573 | - $old_key = isset( $old_value['key'] ) ? $old_value['key'] : ''; |
|
| 574 | - $new_key = isset( $value['key'] ) ? $value['key'] : ''; |
|
| 575 | - |
|
| 576 | - $dataset_uri = $this->get_dataset_uri(); |
|
| 577 | - |
|
| 578 | - if ( ! empty( $new_key ) && $new_key === $old_key && empty( $dataset_uri ) ) { |
|
| 579 | - |
|
| 580 | - // make the request to the remote server to try to get the dataset uri. |
|
| 581 | - $this->get_remote_dataset_uri( $new_key ); |
|
| 582 | - } |
|
| 583 | - |
|
| 584 | - return $value; |
|
| 585 | - } |
|
| 586 | - |
|
| 587 | - /** |
|
| 588 | - * Get the API URI to retrieve the dataset URI using the WordLift Key. |
|
| 589 | - * |
|
| 590 | - * @param string $key The WordLift key to use. |
|
| 591 | - * |
|
| 592 | - * @return string The API URI. |
|
| 593 | - * @since 3.11.0 |
|
| 594 | - * |
|
| 595 | - */ |
|
| 596 | - public function get_accounts_by_key_dataset_uri( $key ) { |
|
| 597 | - |
|
| 598 | - return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . "accounts/key=$key/dataset_uri"; |
|
| 599 | - } |
|
| 600 | - |
|
| 601 | - /** |
|
| 602 | - * Get the API URI to retrieve the account info using the WordLift Key. |
|
| 603 | - * |
|
| 604 | - * @param string $key The WordLift key to use. |
|
| 605 | - * |
|
| 606 | - * @return string The API URI. |
|
| 607 | - * @since 3.26.0 |
|
| 608 | - * |
|
| 609 | - */ |
|
| 610 | - public function get_accounts_info_by_key( $key ) { |
|
| 611 | - |
|
| 612 | - return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . "accounts/info"; |
|
| 613 | - } |
|
| 614 | - |
|
| 615 | - /** |
|
| 616 | - * Get the `accounts` end point. |
|
| 617 | - * |
|
| 618 | - * @return string The `accounts` end point. |
|
| 619 | - * @since 3.16.0 |
|
| 620 | - * |
|
| 621 | - */ |
|
| 622 | - public function get_accounts() { |
|
| 623 | - |
|
| 624 | - return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'accounts'; |
|
| 625 | - } |
|
| 626 | - |
|
| 627 | - /** |
|
| 628 | - * Get the `link by default` option. |
|
| 629 | - * |
|
| 630 | - * @return bool True if entities must be linked by default otherwise false. |
|
| 631 | - * @since 3.13.0 |
|
| 632 | - * |
|
| 633 | - */ |
|
| 634 | - public function is_link_by_default() { |
|
| 635 | - |
|
| 636 | - return 'yes' === $this->get( 'wl_general_settings', self::LINK_BY_DEFAULT, 'yes' ); |
|
| 637 | - } |
|
| 638 | - |
|
| 639 | - /** |
|
| 640 | - * Set the `link by default` option. |
|
| 641 | - * |
|
| 642 | - * @param bool $value True to enabling linking by default, otherwise false. |
|
| 643 | - * |
|
| 644 | - * @since 3.13.0 |
|
| 645 | - * |
|
| 646 | - */ |
|
| 647 | - public function set_link_by_default( $value ) { |
|
| 648 | - |
|
| 649 | - $this->set( 'wl_general_settings', self::LINK_BY_DEFAULT, true === $value ? 'yes' : 'no' ); |
|
| 650 | - } |
|
| 651 | - |
|
| 652 | - /** |
|
| 653 | - * Get the 'analytics-enable' option. |
|
| 654 | - * |
|
| 655 | - * @return string 'no' or 'yes' representing bool. |
|
| 656 | - * @since 3.21.0 |
|
| 657 | - * |
|
| 658 | - */ |
|
| 659 | - public function is_analytics_enable() { |
|
| 660 | - return 'yes' === $this->get( 'wl_analytics_settings', self::ANALYTICS_ENABLE, 'no' ); |
|
| 661 | - } |
|
| 662 | - |
|
| 663 | - /** |
|
| 664 | - * Set the `analytics-enable` option. |
|
| 665 | - * |
|
| 666 | - * @param bool $value True to enabling analytics, otherwise false. |
|
| 667 | - * |
|
| 668 | - * @since 3.21.0 |
|
| 669 | - * |
|
| 670 | - */ |
|
| 671 | - public function set_is_analytics_enable( $value ) { |
|
| 672 | - |
|
| 673 | - $this->set( 'wl_general_settings', self::ANALYTICS_ENABLE, true === $value ? 'yes' : 'no' ); |
|
| 674 | - } |
|
| 675 | - |
|
| 676 | - /** |
|
| 677 | - * Get the 'analytics-entity-uri-dimention' option. |
|
| 678 | - * |
|
| 679 | - * @return int |
|
| 680 | - * @since 3.21.0 |
|
| 681 | - * |
|
| 682 | - */ |
|
| 683 | - public function get_analytics_entity_uri_dimension() { |
|
| 684 | - return (int) $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_URI_DIMENSION, 1 ); |
|
| 685 | - } |
|
| 686 | - |
|
| 687 | - /** |
|
| 688 | - * Get the 'analytics-entity-type-dimension' option. |
|
| 689 | - * |
|
| 690 | - * @return int |
|
| 691 | - * @since 3.21.0 |
|
| 692 | - * |
|
| 693 | - */ |
|
| 694 | - public function get_analytics_entity_type_dimension() { |
|
| 695 | - return $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_TYPE_DIMENSION, 2 ); |
|
| 696 | - } |
|
| 697 | - |
|
| 698 | - /** |
|
| 699 | - * Get the URL to perform autocomplete request. |
|
| 700 | - * |
|
| 701 | - * @return string The URL to call to perform the autocomplete request. |
|
| 702 | - * @since 3.15.0 |
|
| 703 | - * |
|
| 704 | - */ |
|
| 705 | - public function get_autocomplete_url() { |
|
| 706 | - |
|
| 707 | - return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'autocomplete'; |
|
| 708 | - |
|
| 709 | - } |
|
| 710 | - |
|
| 711 | - /** |
|
| 712 | - * Get the URL to perform feedback deactivation request. |
|
| 713 | - * |
|
| 714 | - * @return string The URL to call to perform the feedback deactivation request. |
|
| 715 | - * @since 3.19.0 |
|
| 716 | - * |
|
| 717 | - */ |
|
| 718 | - public function get_deactivation_feedback_url() { |
|
| 719 | - |
|
| 720 | - return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'feedbacks'; |
|
| 721 | - |
|
| 722 | - } |
|
| 723 | - |
|
| 724 | - /** |
|
| 725 | - * Get the base API URL. |
|
| 726 | - * |
|
| 727 | - * @return string The base API URL. |
|
| 728 | - * @since 3.20.0 |
|
| 729 | - * |
|
| 730 | - */ |
|
| 731 | - public function get_api_url() { |
|
| 732 | - |
|
| 733 | - return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE; |
|
| 734 | - } |
|
| 542 | + $json = json_decode( $response['body'] ); |
|
| 543 | + $dataset_uri = $json->datasetURI; |
|
| 544 | + $package_type = isset( $json->packageType ) ? $json->packageType : null; |
|
| 545 | + |
|
| 546 | + $this->log->info( "Updating [ dataset uri :: $dataset_uri ][ package type :: $package_type ]..." ); |
|
| 547 | + |
|
| 548 | + $this->set_dataset_uri( $dataset_uri ); |
|
| 549 | + $this->set_package_type( $package_type ); |
|
| 550 | + |
|
| 551 | + } |
|
| 552 | + |
|
| 553 | + /** |
|
| 554 | + * Handle the edge case where a user submits the same key again |
|
| 555 | + * when he does not have the dataset uri to regain it. |
|
| 556 | + * |
|
| 557 | + * This can not be handled in the normal option update hook because |
|
| 558 | + * it is not being triggered when the save value equals to the one already |
|
| 559 | + * in the DB. |
|
| 560 | + * |
|
| 561 | + * @param mixed $value The new, unserialized option value. |
|
| 562 | + * @param mixed $old_value The old option value. |
|
| 563 | + * |
|
| 564 | + * @return mixed The same value in the $value parameter |
|
| 565 | + * @since 3.12.0 |
|
| 566 | + * |
|
| 567 | + */ |
|
| 568 | + function maybe_update_dataset_uri( $value, $old_value ) { |
|
| 569 | + |
|
| 570 | + // Check the old key value and the new one. Here we're only handling the |
|
| 571 | + // case where the key hasn't changed and the dataset URI isn't set. The |
|
| 572 | + // other case, i.e. a new key is inserted, is handled at `update_key`. |
|
| 573 | + $old_key = isset( $old_value['key'] ) ? $old_value['key'] : ''; |
|
| 574 | + $new_key = isset( $value['key'] ) ? $value['key'] : ''; |
|
| 575 | + |
|
| 576 | + $dataset_uri = $this->get_dataset_uri(); |
|
| 577 | + |
|
| 578 | + if ( ! empty( $new_key ) && $new_key === $old_key && empty( $dataset_uri ) ) { |
|
| 579 | + |
|
| 580 | + // make the request to the remote server to try to get the dataset uri. |
|
| 581 | + $this->get_remote_dataset_uri( $new_key ); |
|
| 582 | + } |
|
| 583 | + |
|
| 584 | + return $value; |
|
| 585 | + } |
|
| 586 | + |
|
| 587 | + /** |
|
| 588 | + * Get the API URI to retrieve the dataset URI using the WordLift Key. |
|
| 589 | + * |
|
| 590 | + * @param string $key The WordLift key to use. |
|
| 591 | + * |
|
| 592 | + * @return string The API URI. |
|
| 593 | + * @since 3.11.0 |
|
| 594 | + * |
|
| 595 | + */ |
|
| 596 | + public function get_accounts_by_key_dataset_uri( $key ) { |
|
| 597 | + |
|
| 598 | + return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . "accounts/key=$key/dataset_uri"; |
|
| 599 | + } |
|
| 600 | + |
|
| 601 | + /** |
|
| 602 | + * Get the API URI to retrieve the account info using the WordLift Key. |
|
| 603 | + * |
|
| 604 | + * @param string $key The WordLift key to use. |
|
| 605 | + * |
|
| 606 | + * @return string The API URI. |
|
| 607 | + * @since 3.26.0 |
|
| 608 | + * |
|
| 609 | + */ |
|
| 610 | + public function get_accounts_info_by_key( $key ) { |
|
| 611 | + |
|
| 612 | + return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . "accounts/info"; |
|
| 613 | + } |
|
| 614 | + |
|
| 615 | + /** |
|
| 616 | + * Get the `accounts` end point. |
|
| 617 | + * |
|
| 618 | + * @return string The `accounts` end point. |
|
| 619 | + * @since 3.16.0 |
|
| 620 | + * |
|
| 621 | + */ |
|
| 622 | + public function get_accounts() { |
|
| 623 | + |
|
| 624 | + return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'accounts'; |
|
| 625 | + } |
|
| 626 | + |
|
| 627 | + /** |
|
| 628 | + * Get the `link by default` option. |
|
| 629 | + * |
|
| 630 | + * @return bool True if entities must be linked by default otherwise false. |
|
| 631 | + * @since 3.13.0 |
|
| 632 | + * |
|
| 633 | + */ |
|
| 634 | + public function is_link_by_default() { |
|
| 635 | + |
|
| 636 | + return 'yes' === $this->get( 'wl_general_settings', self::LINK_BY_DEFAULT, 'yes' ); |
|
| 637 | + } |
|
| 638 | + |
|
| 639 | + /** |
|
| 640 | + * Set the `link by default` option. |
|
| 641 | + * |
|
| 642 | + * @param bool $value True to enabling linking by default, otherwise false. |
|
| 643 | + * |
|
| 644 | + * @since 3.13.0 |
|
| 645 | + * |
|
| 646 | + */ |
|
| 647 | + public function set_link_by_default( $value ) { |
|
| 648 | + |
|
| 649 | + $this->set( 'wl_general_settings', self::LINK_BY_DEFAULT, true === $value ? 'yes' : 'no' ); |
|
| 650 | + } |
|
| 651 | + |
|
| 652 | + /** |
|
| 653 | + * Get the 'analytics-enable' option. |
|
| 654 | + * |
|
| 655 | + * @return string 'no' or 'yes' representing bool. |
|
| 656 | + * @since 3.21.0 |
|
| 657 | + * |
|
| 658 | + */ |
|
| 659 | + public function is_analytics_enable() { |
|
| 660 | + return 'yes' === $this->get( 'wl_analytics_settings', self::ANALYTICS_ENABLE, 'no' ); |
|
| 661 | + } |
|
| 662 | + |
|
| 663 | + /** |
|
| 664 | + * Set the `analytics-enable` option. |
|
| 665 | + * |
|
| 666 | + * @param bool $value True to enabling analytics, otherwise false. |
|
| 667 | + * |
|
| 668 | + * @since 3.21.0 |
|
| 669 | + * |
|
| 670 | + */ |
|
| 671 | + public function set_is_analytics_enable( $value ) { |
|
| 672 | + |
|
| 673 | + $this->set( 'wl_general_settings', self::ANALYTICS_ENABLE, true === $value ? 'yes' : 'no' ); |
|
| 674 | + } |
|
| 675 | + |
|
| 676 | + /** |
|
| 677 | + * Get the 'analytics-entity-uri-dimention' option. |
|
| 678 | + * |
|
| 679 | + * @return int |
|
| 680 | + * @since 3.21.0 |
|
| 681 | + * |
|
| 682 | + */ |
|
| 683 | + public function get_analytics_entity_uri_dimension() { |
|
| 684 | + return (int) $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_URI_DIMENSION, 1 ); |
|
| 685 | + } |
|
| 686 | + |
|
| 687 | + /** |
|
| 688 | + * Get the 'analytics-entity-type-dimension' option. |
|
| 689 | + * |
|
| 690 | + * @return int |
|
| 691 | + * @since 3.21.0 |
|
| 692 | + * |
|
| 693 | + */ |
|
| 694 | + public function get_analytics_entity_type_dimension() { |
|
| 695 | + return $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_TYPE_DIMENSION, 2 ); |
|
| 696 | + } |
|
| 697 | + |
|
| 698 | + /** |
|
| 699 | + * Get the URL to perform autocomplete request. |
|
| 700 | + * |
|
| 701 | + * @return string The URL to call to perform the autocomplete request. |
|
| 702 | + * @since 3.15.0 |
|
| 703 | + * |
|
| 704 | + */ |
|
| 705 | + public function get_autocomplete_url() { |
|
| 706 | + |
|
| 707 | + return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'autocomplete'; |
|
| 708 | + |
|
| 709 | + } |
|
| 710 | + |
|
| 711 | + /** |
|
| 712 | + * Get the URL to perform feedback deactivation request. |
|
| 713 | + * |
|
| 714 | + * @return string The URL to call to perform the feedback deactivation request. |
|
| 715 | + * @since 3.19.0 |
|
| 716 | + * |
|
| 717 | + */ |
|
| 718 | + public function get_deactivation_feedback_url() { |
|
| 719 | + |
|
| 720 | + return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'feedbacks'; |
|
| 721 | + |
|
| 722 | + } |
|
| 723 | + |
|
| 724 | + /** |
|
| 725 | + * Get the base API URL. |
|
| 726 | + * |
|
| 727 | + * @return string The base API URL. |
|
| 728 | + * @since 3.20.0 |
|
| 729 | + * |
|
| 730 | + */ |
|
| 731 | + public function get_api_url() { |
|
| 732 | + |
|
| 733 | + return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE; |
|
| 734 | + } |
|
| 735 | 735 | |
| 736 | 736 | } |
@@ -10,7 +10,7 @@ discard block |
||
| 10 | 10 | * @since 3.6.0 |
| 11 | 11 | */ |
| 12 | 12 | |
| 13 | -if ( ! defined( 'ABSPATH' ) ) { |
|
| 13 | +if ( ! defined('ABSPATH')) { |
|
| 14 | 14 | exit; |
| 15 | 15 | } |
| 16 | 16 | |
@@ -138,7 +138,7 @@ discard block |
||
| 138 | 138 | */ |
| 139 | 139 | public function __construct() { |
| 140 | 140 | |
| 141 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 141 | + $this->log = Wordlift_Log_Service::get_logger(get_class()); |
|
| 142 | 142 | |
| 143 | 143 | self::$instance = $this; |
| 144 | 144 | |
@@ -168,11 +168,11 @@ discard block |
||
| 168 | 168 | * @since 3.6.0 |
| 169 | 169 | * |
| 170 | 170 | */ |
| 171 | - private function get( $option, $key, $default = '' ) { |
|
| 171 | + private function get($option, $key, $default = '') { |
|
| 172 | 172 | |
| 173 | - $options = get_option( $option, array() ); |
|
| 173 | + $options = get_option($option, array()); |
|
| 174 | 174 | |
| 175 | - return isset( $options[ $key ] ) ? $options[ $key ] : $default; |
|
| 175 | + return isset($options[$key]) ? $options[$key] : $default; |
|
| 176 | 176 | } |
| 177 | 177 | |
| 178 | 178 | /** |
@@ -185,12 +185,12 @@ discard block |
||
| 185 | 185 | * @since 3.9.0 |
| 186 | 186 | * |
| 187 | 187 | */ |
| 188 | - private function set( $option, $key, $value ) { |
|
| 188 | + private function set($option, $key, $value) { |
|
| 189 | 189 | |
| 190 | - $values = get_option( $option ); |
|
| 191 | - $values = isset( $values ) ? $values : array(); |
|
| 192 | - $values[ $key ] = $value; |
|
| 193 | - update_option( $option, $values ); |
|
| 190 | + $values = get_option($option); |
|
| 191 | + $values = isset($values) ? $values : array(); |
|
| 192 | + $values[$key] = $value; |
|
| 193 | + update_option($option, $values); |
|
| 194 | 194 | |
| 195 | 195 | } |
| 196 | 196 | |
@@ -203,7 +203,7 @@ discard block |
||
| 203 | 203 | */ |
| 204 | 204 | public function get_entity_base_path() { |
| 205 | 205 | |
| 206 | - return $this->get( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, 'entity' ); |
|
| 206 | + return $this->get('wl_general_settings', self::ENTITY_BASE_PATH_KEY, 'entity'); |
|
| 207 | 207 | } |
| 208 | 208 | |
| 209 | 209 | /** |
@@ -214,9 +214,9 @@ discard block |
||
| 214 | 214 | * @since 3.9.0 |
| 215 | 215 | * |
| 216 | 216 | */ |
| 217 | - public function set_entity_base_path( $value ) { |
|
| 217 | + public function set_entity_base_path($value) { |
|
| 218 | 218 | |
| 219 | - $this->set( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, $value ); |
|
| 219 | + $this->set('wl_general_settings', self::ENTITY_BASE_PATH_KEY, $value); |
|
| 220 | 220 | |
| 221 | 221 | } |
| 222 | 222 | |
@@ -229,7 +229,7 @@ discard block |
||
| 229 | 229 | */ |
| 230 | 230 | public function is_skip_wizard() { |
| 231 | 231 | |
| 232 | - return $this->get( 'wl_general_settings', self::SKIP_WIZARD, false ); |
|
| 232 | + return $this->get('wl_general_settings', self::SKIP_WIZARD, false); |
|
| 233 | 233 | } |
| 234 | 234 | |
| 235 | 235 | /** |
@@ -240,9 +240,9 @@ discard block |
||
| 240 | 240 | * @since 3.9.0 |
| 241 | 241 | * |
| 242 | 242 | */ |
| 243 | - public function set_skip_wizard( $value ) { |
|
| 243 | + public function set_skip_wizard($value) { |
|
| 244 | 244 | |
| 245 | - $this->set( 'wl_general_settings', self::SKIP_WIZARD, true === $value ); |
|
| 245 | + $this->set('wl_general_settings', self::SKIP_WIZARD, true === $value); |
|
| 246 | 246 | |
| 247 | 247 | } |
| 248 | 248 | |
@@ -255,7 +255,7 @@ discard block |
||
| 255 | 255 | */ |
| 256 | 256 | public function get_key() { |
| 257 | 257 | |
| 258 | - return $this->get( 'wl_general_settings', self::KEY, '' ); |
|
| 258 | + return $this->get('wl_general_settings', self::KEY, ''); |
|
| 259 | 259 | } |
| 260 | 260 | |
| 261 | 261 | /** |
@@ -266,9 +266,9 @@ discard block |
||
| 266 | 266 | * @since 3.9.0 |
| 267 | 267 | * |
| 268 | 268 | */ |
| 269 | - public function set_key( $value ) { |
|
| 269 | + public function set_key($value) { |
|
| 270 | 270 | |
| 271 | - $this->set( 'wl_general_settings', self::KEY, $value ); |
|
| 271 | + $this->set('wl_general_settings', self::KEY, $value); |
|
| 272 | 272 | } |
| 273 | 273 | |
| 274 | 274 | /** |
@@ -282,7 +282,7 @@ discard block |
||
| 282 | 282 | */ |
| 283 | 283 | public function get_language_code() { |
| 284 | 284 | |
| 285 | - return $this->get( 'wl_general_settings', self::LANGUAGE, 'en' ); |
|
| 285 | + return $this->get('wl_general_settings', self::LANGUAGE, 'en'); |
|
| 286 | 286 | } |
| 287 | 287 | |
| 288 | 288 | /** |
@@ -293,9 +293,9 @@ discard block |
||
| 293 | 293 | * @since 3.9.0 |
| 294 | 294 | * |
| 295 | 295 | */ |
| 296 | - public function set_language_code( $value ) { |
|
| 296 | + public function set_language_code($value) { |
|
| 297 | 297 | |
| 298 | - $this->set( 'wl_general_settings', self::LANGUAGE, $value ); |
|
| 298 | + $this->set('wl_general_settings', self::LANGUAGE, $value); |
|
| 299 | 299 | |
| 300 | 300 | } |
| 301 | 301 | |
@@ -307,9 +307,9 @@ discard block |
||
| 307 | 307 | * @since 3.19.0 |
| 308 | 308 | * |
| 309 | 309 | */ |
| 310 | - public function set_diagnostic_preferences( $value ) { |
|
| 310 | + public function set_diagnostic_preferences($value) { |
|
| 311 | 311 | |
| 312 | - $this->set( 'wl_general_settings', self::SEND_DIAGNOSTIC, $value ); |
|
| 312 | + $this->set('wl_general_settings', self::SEND_DIAGNOSTIC, $value); |
|
| 313 | 313 | |
| 314 | 314 | } |
| 315 | 315 | |
@@ -320,7 +320,7 @@ discard block |
||
| 320 | 320 | */ |
| 321 | 321 | public function get_diagnostic_preferences() { |
| 322 | 322 | |
| 323 | - return $this->get( 'wl_general_settings', self::SEND_DIAGNOSTIC, 'no' ); |
|
| 323 | + return $this->get('wl_general_settings', self::SEND_DIAGNOSTIC, 'no'); |
|
| 324 | 324 | } |
| 325 | 325 | |
| 326 | 326 | /** |
@@ -332,7 +332,7 @@ discard block |
||
| 332 | 332 | */ |
| 333 | 333 | public function get_country_code() { |
| 334 | 334 | |
| 335 | - return $this->get( 'wl_general_settings', self::COUNTRY_CODE, 'us' ); |
|
| 335 | + return $this->get('wl_general_settings', self::COUNTRY_CODE, 'us'); |
|
| 336 | 336 | } |
| 337 | 337 | |
| 338 | 338 | /** |
@@ -343,9 +343,9 @@ discard block |
||
| 343 | 343 | * @since 3.18.0 |
| 344 | 344 | * |
| 345 | 345 | */ |
| 346 | - public function set_country_code( $value ) { |
|
| 346 | + public function set_country_code($value) { |
|
| 347 | 347 | |
| 348 | - $this->set( 'wl_general_settings', self::COUNTRY_CODE, $value ); |
|
| 348 | + $this->set('wl_general_settings', self::COUNTRY_CODE, $value); |
|
| 349 | 349 | |
| 350 | 350 | } |
| 351 | 351 | |
@@ -361,7 +361,7 @@ discard block |
||
| 361 | 361 | */ |
| 362 | 362 | public function get_publisher_id() { |
| 363 | 363 | |
| 364 | - return $this->get( 'wl_general_settings', self::PUBLISHER_ID, null ); |
|
| 364 | + return $this->get('wl_general_settings', self::PUBLISHER_ID, null); |
|
| 365 | 365 | } |
| 366 | 366 | |
| 367 | 367 | /** |
@@ -372,9 +372,9 @@ discard block |
||
| 372 | 372 | * @since 3.9.0 |
| 373 | 373 | * |
| 374 | 374 | */ |
| 375 | - public function set_publisher_id( $value ) { |
|
| 375 | + public function set_publisher_id($value) { |
|
| 376 | 376 | |
| 377 | - $this->set( 'wl_general_settings', self::PUBLISHER_ID, $value ); |
|
| 377 | + $this->set('wl_general_settings', self::PUBLISHER_ID, $value); |
|
| 378 | 378 | |
| 379 | 379 | } |
| 380 | 380 | |
@@ -387,7 +387,7 @@ discard block |
||
| 387 | 387 | */ |
| 388 | 388 | public function get_dataset_uri() { |
| 389 | 389 | |
| 390 | - return $this->get( 'wl_advanced_settings', self::DATASET_URI, null ); |
|
| 390 | + return $this->get('wl_advanced_settings', self::DATASET_URI, null); |
|
| 391 | 391 | } |
| 392 | 392 | |
| 393 | 393 | /** |
@@ -398,9 +398,9 @@ discard block |
||
| 398 | 398 | * @since 3.10.0 |
| 399 | 399 | * |
| 400 | 400 | */ |
| 401 | - public function set_dataset_uri( $value ) { |
|
| 401 | + public function set_dataset_uri($value) { |
|
| 402 | 402 | |
| 403 | - $this->set( 'wl_advanced_settings', self::DATASET_URI, $value ); |
|
| 403 | + $this->set('wl_advanced_settings', self::DATASET_URI, $value); |
|
| 404 | 404 | } |
| 405 | 405 | |
| 406 | 406 | /** |
@@ -412,7 +412,7 @@ discard block |
||
| 412 | 412 | */ |
| 413 | 413 | public function get_package_type() { |
| 414 | 414 | |
| 415 | - return $this->get( 'wl_advanced_settings', self::PACKAGE_TYPE, null ); |
|
| 415 | + return $this->get('wl_advanced_settings', self::PACKAGE_TYPE, null); |
|
| 416 | 416 | } |
| 417 | 417 | |
| 418 | 418 | /** |
@@ -423,9 +423,9 @@ discard block |
||
| 423 | 423 | * @since 3.20.0 |
| 424 | 424 | * |
| 425 | 425 | */ |
| 426 | - public function set_package_type( $value ) { |
|
| 426 | + public function set_package_type($value) { |
|
| 427 | 427 | |
| 428 | - $this->set( 'wl_advanced_settings', self::PACKAGE_TYPE, $value ); |
|
| 428 | + $this->set('wl_advanced_settings', self::PACKAGE_TYPE, $value); |
|
| 429 | 429 | } |
| 430 | 430 | |
| 431 | 431 | /** |
@@ -441,11 +441,11 @@ discard block |
||
| 441 | 441 | * @param array $old_value The old settings. |
| 442 | 442 | * @param array $new_value The new settings. |
| 443 | 443 | */ |
| 444 | - public function update_key( $old_value, $new_value ) { |
|
| 444 | + public function update_key($old_value, $new_value) { |
|
| 445 | 445 | |
| 446 | 446 | // Check the old key value and the new one. We're going to ask for the dataset URI only if the key has changed. |
| 447 | 447 | // $old_key = isset( $old_value['key'] ) ? $old_value['key'] : ''; |
| 448 | - $new_key = isset( $new_value['key'] ) ? $new_value['key'] : ''; |
|
| 448 | + $new_key = isset($new_value['key']) ? $new_value['key'] : ''; |
|
| 449 | 449 | |
| 450 | 450 | // If the key hasn't changed, don't do anything. |
| 451 | 451 | // WARN The 'update_option' hook is fired only if the new and old value are not equal. |
@@ -454,12 +454,12 @@ discard block |
||
| 454 | 454 | // } |
| 455 | 455 | |
| 456 | 456 | // If the key is empty, empty the dataset URI. |
| 457 | - if ( '' === $new_key ) { |
|
| 458 | - $this->set_dataset_uri( '' ); |
|
| 457 | + if ('' === $new_key) { |
|
| 458 | + $this->set_dataset_uri(''); |
|
| 459 | 459 | } |
| 460 | 460 | |
| 461 | 461 | // make the request to the remote server. |
| 462 | - $this->get_remote_dataset_uri( $new_key ); |
|
| 462 | + $this->get_remote_dataset_uri($new_key); |
|
| 463 | 463 | |
| 464 | 464 | } |
| 465 | 465 | |
@@ -475,15 +475,15 @@ discard block |
||
| 475 | 475 | * |
| 476 | 476 | * @since 3.17.0 send the site URL and get the dataset URI. |
| 477 | 477 | */ |
| 478 | - public function get_remote_dataset_uri( $key ) { |
|
| 478 | + public function get_remote_dataset_uri($key) { |
|
| 479 | 479 | |
| 480 | - $this->log->trace( 'Getting the remote dataset URI and package type...' ); |
|
| 480 | + $this->log->trace('Getting the remote dataset URI and package type...'); |
|
| 481 | 481 | |
| 482 | - if ( empty( $key ) ) { |
|
| 483 | - $this->log->warn( 'Key set to empty value.' ); |
|
| 482 | + if (empty($key)) { |
|
| 483 | + $this->log->warn('Key set to empty value.'); |
|
| 484 | 484 | |
| 485 | - $this->set_dataset_uri( '' ); |
|
| 486 | - $this->set_package_type( null ); |
|
| 485 | + $this->set_dataset_uri(''); |
|
| 486 | + $this->set_package_type(null); |
|
| 487 | 487 | |
| 488 | 488 | return; |
| 489 | 489 | } |
@@ -498,38 +498,38 @@ discard block |
||
| 498 | 498 | * @since 3.20.0 |
| 499 | 499 | * |
| 500 | 500 | */ |
| 501 | - $home_url = defined( 'WP_HOME' ) ? WP_HOME : get_option( 'home' ); |
|
| 502 | - $site_url = apply_filters( 'wl_production_site_url', $home_url ); |
|
| 501 | + $home_url = defined('WP_HOME') ? WP_HOME : get_option('home'); |
|
| 502 | + $site_url = apply_filters('wl_production_site_url', $home_url); |
|
| 503 | 503 | |
| 504 | 504 | // Build the URL. |
| 505 | 505 | $url = $this->get_accounts() |
| 506 | - . '?key=' . rawurlencode( $key ) |
|
| 507 | - . '&url=' . rawurlencode( $site_url ) |
|
| 508 | - . '&country=' . $this->get_country_code() |
|
| 509 | - . '&language=' . $this->get_language_code(); |
|
| 506 | + . '?key='.rawurlencode($key) |
|
| 507 | + . '&url='.rawurlencode($site_url) |
|
| 508 | + . '&country='.$this->get_country_code() |
|
| 509 | + . '&language='.$this->get_language_code(); |
|
| 510 | 510 | |
| 511 | - $args = wp_parse_args( unserialize( WL_REDLINK_API_HTTP_OPTIONS ), array( |
|
| 511 | + $args = wp_parse_args(unserialize(WL_REDLINK_API_HTTP_OPTIONS), array( |
|
| 512 | 512 | 'method' => 'PUT', |
| 513 | - ) ); |
|
| 513 | + )); |
|
| 514 | 514 | |
| 515 | - $response = wp_remote_request( $url, $args ); |
|
| 515 | + $response = wp_remote_request($url, $args); |
|
| 516 | 516 | |
| 517 | 517 | // The response is an error. |
| 518 | - if ( is_wp_error( $response ) ) { |
|
| 519 | - $this->log->error( 'An error occurred setting the dataset URI: ' . $response->get_error_message() ); |
|
| 518 | + if (is_wp_error($response)) { |
|
| 519 | + $this->log->error('An error occurred setting the dataset URI: '.$response->get_error_message()); |
|
| 520 | 520 | |
| 521 | - $this->set_dataset_uri( '' ); |
|
| 522 | - $this->set_package_type( null ); |
|
| 521 | + $this->set_dataset_uri(''); |
|
| 522 | + $this->set_package_type(null); |
|
| 523 | 523 | |
| 524 | 524 | return; |
| 525 | 525 | } |
| 526 | 526 | |
| 527 | 527 | // The response is not OK. |
| 528 | - if ( 200 !== (int) $response['response']['code'] ) { |
|
| 529 | - $this->log->error( "Unexpected status code when opening URL $url: " . $response['response']['code'] ); |
|
| 528 | + if (200 !== (int) $response['response']['code']) { |
|
| 529 | + $this->log->error("Unexpected status code when opening URL $url: ".$response['response']['code']); |
|
| 530 | 530 | |
| 531 | - $this->set_dataset_uri( '' ); |
|
| 532 | - $this->set_package_type( null ); |
|
| 531 | + $this->set_dataset_uri(''); |
|
| 532 | + $this->set_package_type(null); |
|
| 533 | 533 | |
| 534 | 534 | return; |
| 535 | 535 | } |
@@ -539,14 +539,14 @@ discard block |
||
| 539 | 539 | * |
| 540 | 540 | * @since 3.20.0 |
| 541 | 541 | */ |
| 542 | - $json = json_decode( $response['body'] ); |
|
| 542 | + $json = json_decode($response['body']); |
|
| 543 | 543 | $dataset_uri = $json->datasetURI; |
| 544 | - $package_type = isset( $json->packageType ) ? $json->packageType : null; |
|
| 544 | + $package_type = isset($json->packageType) ? $json->packageType : null; |
|
| 545 | 545 | |
| 546 | - $this->log->info( "Updating [ dataset uri :: $dataset_uri ][ package type :: $package_type ]..." ); |
|
| 546 | + $this->log->info("Updating [ dataset uri :: $dataset_uri ][ package type :: $package_type ]..."); |
|
| 547 | 547 | |
| 548 | - $this->set_dataset_uri( $dataset_uri ); |
|
| 549 | - $this->set_package_type( $package_type ); |
|
| 548 | + $this->set_dataset_uri($dataset_uri); |
|
| 549 | + $this->set_package_type($package_type); |
|
| 550 | 550 | |
| 551 | 551 | } |
| 552 | 552 | |
@@ -565,20 +565,20 @@ discard block |
||
| 565 | 565 | * @since 3.12.0 |
| 566 | 566 | * |
| 567 | 567 | */ |
| 568 | - function maybe_update_dataset_uri( $value, $old_value ) { |
|
| 568 | + function maybe_update_dataset_uri($value, $old_value) { |
|
| 569 | 569 | |
| 570 | 570 | // Check the old key value and the new one. Here we're only handling the |
| 571 | 571 | // case where the key hasn't changed and the dataset URI isn't set. The |
| 572 | 572 | // other case, i.e. a new key is inserted, is handled at `update_key`. |
| 573 | - $old_key = isset( $old_value['key'] ) ? $old_value['key'] : ''; |
|
| 574 | - $new_key = isset( $value['key'] ) ? $value['key'] : ''; |
|
| 573 | + $old_key = isset($old_value['key']) ? $old_value['key'] : ''; |
|
| 574 | + $new_key = isset($value['key']) ? $value['key'] : ''; |
|
| 575 | 575 | |
| 576 | 576 | $dataset_uri = $this->get_dataset_uri(); |
| 577 | 577 | |
| 578 | - if ( ! empty( $new_key ) && $new_key === $old_key && empty( $dataset_uri ) ) { |
|
| 578 | + if ( ! empty($new_key) && $new_key === $old_key && empty($dataset_uri)) { |
|
| 579 | 579 | |
| 580 | 580 | // make the request to the remote server to try to get the dataset uri. |
| 581 | - $this->get_remote_dataset_uri( $new_key ); |
|
| 581 | + $this->get_remote_dataset_uri($new_key); |
|
| 582 | 582 | } |
| 583 | 583 | |
| 584 | 584 | return $value; |
@@ -593,9 +593,9 @@ discard block |
||
| 593 | 593 | * @since 3.11.0 |
| 594 | 594 | * |
| 595 | 595 | */ |
| 596 | - public function get_accounts_by_key_dataset_uri( $key ) { |
|
| 596 | + public function get_accounts_by_key_dataset_uri($key) { |
|
| 597 | 597 | |
| 598 | - return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . "accounts/key=$key/dataset_uri"; |
|
| 598 | + return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE."accounts/key=$key/dataset_uri"; |
|
| 599 | 599 | } |
| 600 | 600 | |
| 601 | 601 | /** |
@@ -607,9 +607,9 @@ discard block |
||
| 607 | 607 | * @since 3.26.0 |
| 608 | 608 | * |
| 609 | 609 | */ |
| 610 | - public function get_accounts_info_by_key( $key ) { |
|
| 610 | + public function get_accounts_info_by_key($key) { |
|
| 611 | 611 | |
| 612 | - return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . "accounts/info"; |
|
| 612 | + return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE."accounts/info"; |
|
| 613 | 613 | } |
| 614 | 614 | |
| 615 | 615 | /** |
@@ -621,7 +621,7 @@ discard block |
||
| 621 | 621 | */ |
| 622 | 622 | public function get_accounts() { |
| 623 | 623 | |
| 624 | - return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'accounts'; |
|
| 624 | + return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE.'accounts'; |
|
| 625 | 625 | } |
| 626 | 626 | |
| 627 | 627 | /** |
@@ -633,7 +633,7 @@ discard block |
||
| 633 | 633 | */ |
| 634 | 634 | public function is_link_by_default() { |
| 635 | 635 | |
| 636 | - return 'yes' === $this->get( 'wl_general_settings', self::LINK_BY_DEFAULT, 'yes' ); |
|
| 636 | + return 'yes' === $this->get('wl_general_settings', self::LINK_BY_DEFAULT, 'yes'); |
|
| 637 | 637 | } |
| 638 | 638 | |
| 639 | 639 | /** |
@@ -644,9 +644,9 @@ discard block |
||
| 644 | 644 | * @since 3.13.0 |
| 645 | 645 | * |
| 646 | 646 | */ |
| 647 | - public function set_link_by_default( $value ) { |
|
| 647 | + public function set_link_by_default($value) { |
|
| 648 | 648 | |
| 649 | - $this->set( 'wl_general_settings', self::LINK_BY_DEFAULT, true === $value ? 'yes' : 'no' ); |
|
| 649 | + $this->set('wl_general_settings', self::LINK_BY_DEFAULT, true === $value ? 'yes' : 'no'); |
|
| 650 | 650 | } |
| 651 | 651 | |
| 652 | 652 | /** |
@@ -657,7 +657,7 @@ discard block |
||
| 657 | 657 | * |
| 658 | 658 | */ |
| 659 | 659 | public function is_analytics_enable() { |
| 660 | - return 'yes' === $this->get( 'wl_analytics_settings', self::ANALYTICS_ENABLE, 'no' ); |
|
| 660 | + return 'yes' === $this->get('wl_analytics_settings', self::ANALYTICS_ENABLE, 'no'); |
|
| 661 | 661 | } |
| 662 | 662 | |
| 663 | 663 | /** |
@@ -668,9 +668,9 @@ discard block |
||
| 668 | 668 | * @since 3.21.0 |
| 669 | 669 | * |
| 670 | 670 | */ |
| 671 | - public function set_is_analytics_enable( $value ) { |
|
| 671 | + public function set_is_analytics_enable($value) { |
|
| 672 | 672 | |
| 673 | - $this->set( 'wl_general_settings', self::ANALYTICS_ENABLE, true === $value ? 'yes' : 'no' ); |
|
| 673 | + $this->set('wl_general_settings', self::ANALYTICS_ENABLE, true === $value ? 'yes' : 'no'); |
|
| 674 | 674 | } |
| 675 | 675 | |
| 676 | 676 | /** |
@@ -681,7 +681,7 @@ discard block |
||
| 681 | 681 | * |
| 682 | 682 | */ |
| 683 | 683 | public function get_analytics_entity_uri_dimension() { |
| 684 | - return (int) $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_URI_DIMENSION, 1 ); |
|
| 684 | + return (int) $this->get('wl_analytics_settings', self::ANALYTICS_ENTITY_URI_DIMENSION, 1); |
|
| 685 | 685 | } |
| 686 | 686 | |
| 687 | 687 | /** |
@@ -692,7 +692,7 @@ discard block |
||
| 692 | 692 | * |
| 693 | 693 | */ |
| 694 | 694 | public function get_analytics_entity_type_dimension() { |
| 695 | - return $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_TYPE_DIMENSION, 2 ); |
|
| 695 | + return $this->get('wl_analytics_settings', self::ANALYTICS_ENTITY_TYPE_DIMENSION, 2); |
|
| 696 | 696 | } |
| 697 | 697 | |
| 698 | 698 | /** |
@@ -704,7 +704,7 @@ discard block |
||
| 704 | 704 | */ |
| 705 | 705 | public function get_autocomplete_url() { |
| 706 | 706 | |
| 707 | - return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'autocomplete'; |
|
| 707 | + return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE.'autocomplete'; |
|
| 708 | 708 | |
| 709 | 709 | } |
| 710 | 710 | |
@@ -717,7 +717,7 @@ discard block |
||
| 717 | 717 | */ |
| 718 | 718 | public function get_deactivation_feedback_url() { |
| 719 | 719 | |
| 720 | - return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'feedbacks'; |
|
| 720 | + return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE.'feedbacks'; |
|
| 721 | 721 | |
| 722 | 722 | } |
| 723 | 723 | |
@@ -18,432 +18,432 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class Wordlift_Admin_Settings_Page extends Wordlift_Admin_Page { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * A singleton instance of the Notice service. |
|
| 23 | - * |
|
| 24 | - * @since 3.2.0 |
|
| 25 | - * @access private |
|
| 26 | - * @var \Wordlift_Notice_Service $instance A singleton instance of the Notice service. |
|
| 27 | - */ |
|
| 28 | - private static $instance; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * A {@link Wordlift_Entity_Service} instance. |
|
| 32 | - * |
|
| 33 | - * @since 3.11.0 |
|
| 34 | - * @access private |
|
| 35 | - * @var \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 36 | - */ |
|
| 37 | - private $entity_service; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * A {@link Wordlift_Configuration_Service} instance. |
|
| 41 | - * |
|
| 42 | - * @since 3.11.0 |
|
| 43 | - * @access private |
|
| 44 | - * @var \Wordlift_Configuration_Service $configuration_service A {@link Wordlift_Configuration_Service} instance. |
|
| 45 | - */ |
|
| 46 | - private $configuration_service; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * A {@link Wordlift_Admin_Input_Element} element renderer. |
|
| 50 | - * |
|
| 51 | - * @since 3.11.0 |
|
| 52 | - * @access private |
|
| 53 | - * @var \Wordlift_Admin_Input_Element $input_element An {@link Wordlift_Admin_Input_Element} element renderer. |
|
| 54 | - */ |
|
| 55 | - private $input_element; |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * A {@link Wordlift_Admin_Radio_Input_Element} element renderer. |
|
| 59 | - * |
|
| 60 | - * @since 3.13.0 |
|
| 61 | - * @access protected |
|
| 62 | - * @var \Wordlift_Admin_Radio_Input_Element $radio_input_element A {@link Wordlift_Admin_Radio_Input_Element} element renderer. |
|
| 63 | - */ |
|
| 64 | - private $radio_input_element; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * A {@link Wordlift_Admin_Language_Select_Element} element renderer. |
|
| 68 | - * |
|
| 69 | - * @since 3.11.0 |
|
| 70 | - * @access private |
|
| 71 | - * @var \Wordlift_Admin_Language_Select_Element $language_select_element A {@link Wordlift_Admin_Language_Select_Element} element renderer. |
|
| 72 | - */ |
|
| 73 | - private $language_select_element; |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * A {@link Wordlift_Admin_Country_Select_Element} element renderer. |
|
| 77 | - * |
|
| 78 | - * @since 3.18.0 |
|
| 79 | - * @access private |
|
| 80 | - * @var \Wordlift_Admin_Country_Select_Element $country_select_element A {@link Wordlift_Admin_Country_Select_Element} element renderer. |
|
| 81 | - */ |
|
| 82 | - private $country_select_element; |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * A {@link Wordlift_Admin_Publisher_Element} element renderer. |
|
| 86 | - * |
|
| 87 | - * @since 3.11.0 |
|
| 88 | - * @access private |
|
| 89 | - * @var \Wordlift_Admin_Publisher_Element $publisher_element A {@link Wordlift_Admin_Publisher_Element} element renderer. |
|
| 90 | - */ |
|
| 91 | - private $publisher_element; |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Create a {@link Wordlift_Admin_Settings_Page} instance. |
|
| 95 | - * |
|
| 96 | - * @since 3.11.0 |
|
| 97 | - * |
|
| 98 | - * @param \Wordlift_Configuration_Service $configuration_service A {@link Wordlift_Configuration_Service} instance. |
|
| 99 | - * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 100 | - * @param \Wordlift_Admin_Input_Element $input_element A {@link Wordlift_Admin_Input_Element} element renderer. |
|
| 101 | - * @param \Wordlift_Admin_Language_Select_Element $language_select_element A {@link Wordlift_Admin_Language_Select_Element} element renderer. |
|
| 102 | - * @param \Wordlift_Admin_Country_Select_Element $country_select_element A {@link Wordlift_Admin_Country_Select_Element} element renderer. |
|
| 103 | - * @param \Wordlift_Admin_Publisher_Element $publisher_element A {@link Wordlift_Admin_Publisher_Element} element renderer. |
|
| 104 | - * @param \Wordlift_Admin_Radio_Input_Element $radio_input_element A {@link Wordlift_Admin_Radio_Input_Element} element renderer. |
|
| 105 | - */ |
|
| 106 | - function __construct( $configuration_service, $entity_service, $input_element, $language_select_element, $country_select_element, $publisher_element, $radio_input_element ) { |
|
| 107 | - |
|
| 108 | - $this->configuration_service = $configuration_service; |
|
| 109 | - $this->entity_service = $entity_service; |
|
| 110 | - |
|
| 111 | - // Set a reference to the UI elements. |
|
| 112 | - $this->input_element = $input_element; |
|
| 113 | - $this->radio_input_element = $radio_input_element; |
|
| 114 | - $this->language_select_element = $language_select_element; |
|
| 115 | - $this->country_select_element = $country_select_element; |
|
| 116 | - $this->publisher_element = $publisher_element; |
|
| 117 | - |
|
| 118 | - self::$instance = $this; |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * Get the singleton instance of the Notice service. |
|
| 123 | - * |
|
| 124 | - * @since 3.14.0 |
|
| 125 | - * @return \Wordlift_Admin_Settings_Page The singleton instance of the settings page service. |
|
| 126 | - */ |
|
| 127 | - public static function get_instance() { |
|
| 128 | - |
|
| 129 | - return self::$instance; |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * @inheritdoc |
|
| 134 | - */ |
|
| 135 | - function get_parent_slug() { |
|
| 136 | - |
|
| 137 | - return 'wl_admin_menu'; |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * @inheritdoc |
|
| 142 | - */ |
|
| 143 | - function get_capability() { |
|
| 144 | - |
|
| 145 | - return 'manage_options'; |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * @inheritdoc |
|
| 150 | - */ |
|
| 151 | - function get_page_title() { |
|
| 152 | - |
|
| 153 | - return 'WordLift Settings'; |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * @inheritdoc |
|
| 158 | - */ |
|
| 159 | - function get_menu_title() { |
|
| 160 | - |
|
| 161 | - return 'Settings'; |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * @inheritdoc |
|
| 166 | - */ |
|
| 167 | - function get_menu_slug() { |
|
| 168 | - |
|
| 169 | - return 'wl_configuration_admin_menu'; |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - /** |
|
| 173 | - * @inheritdoc |
|
| 174 | - */ |
|
| 175 | - function get_partial_name() { |
|
| 176 | - |
|
| 177 | - return 'wordlift-admin-settings-page.php'; |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * @inheritdoc |
|
| 182 | - */ |
|
| 183 | - public function enqueue_scripts() { |
|
| 184 | - |
|
| 185 | - // Enqueue the media scripts to be used for the publisher's logo selection. |
|
| 186 | - wp_enqueue_media(); |
|
| 187 | - |
|
| 188 | - // JavaScript required for the settings page. |
|
| 189 | - // @todo: try to move to the `wordlift-admin.bundle.js`. |
|
| 190 | - wp_enqueue_script( 'wordlift-admin-settings-page', plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/1/settings.js', array( 'wp-util' ) ); |
|
| 191 | - wp_enqueue_style( 'wordlift-admin-settings-page', plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/1/settings.css' ); |
|
| 192 | - |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - /** |
|
| 196 | - * Configure all the configuration parameters. |
|
| 197 | - * |
|
| 198 | - * Called by the *admin_init* hook. |
|
| 199 | - * |
|
| 200 | - * @since 3.11.0 |
|
| 201 | - */ |
|
| 202 | - function admin_init() { |
|
| 203 | - |
|
| 204 | - // Register WordLift's general settings, providing our own sanitize callback |
|
| 205 | - // which will also check whether the user filled the WL Publisher form. |
|
| 206 | - register_setting( |
|
| 207 | - 'wl_general_settings', |
|
| 208 | - 'wl_general_settings', |
|
| 209 | - array( $this, 'sanitize_callback' ) |
|
| 210 | - ); |
|
| 211 | - |
|
| 212 | - // Add the general settings section. |
|
| 213 | - add_settings_section( |
|
| 214 | - 'wl_general_settings_section', // ID used to identify this section and with which to register options. |
|
| 215 | - '', // Section header. |
|
| 216 | - '', // Callback used to render the description of the section. |
|
| 217 | - 'wl_general_settings' // Page on which to add this section of options. |
|
| 218 | - ); |
|
| 219 | - |
|
| 220 | - $key_args = array( |
|
| 221 | - 'id' => 'wl-key', |
|
| 222 | - 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::KEY . ']', |
|
| 223 | - 'value' => $this->configuration_service->get_key(), |
|
| 224 | - 'description' => __( 'Insert the <a href="https://www.wordlift.io/blogger">WordLift Key</a> you received via email.', 'wordlift' ) |
|
| 225 | - . ' [' . get_option( 'home' ) . ']', |
|
| 226 | - ); |
|
| 227 | - |
|
| 228 | - // Before we were used to validate the key beforehand, but this means |
|
| 229 | - // an http call whenever a page is opened in the admin area. Therefore |
|
| 230 | - // we now leave the input `untouched`, leaving to the client to update |
|
| 231 | - // the `css_class`. |
|
| 232 | - // |
|
| 233 | - // See https://github.com/insideout10/wordlift-plugin/issues/669. |
|
| 234 | - $key_args['css_class'] = 'untouched'; |
|
| 235 | - |
|
| 236 | - // Add the `key` field. |
|
| 237 | - add_settings_field( |
|
| 238 | - 'wl-key', // Element id used to identify the field throughout the theme. |
|
| 239 | - __( 'WordLift Key', 'wordlift' ), // The label to the left of the option interface element. |
|
| 240 | - // The name of the function responsible for rendering the option interface. |
|
| 241 | - array( $this->input_element, 'render' ), |
|
| 242 | - 'wl_general_settings', // The page on which this option will be displayed. |
|
| 243 | - 'wl_general_settings_section', // The name of the section to which this field belongs. |
|
| 244 | - $key_args // The array of arguments to pass to the callback. In this case, just a description. |
|
| 245 | - ); |
|
| 246 | - |
|
| 247 | - // Entity Base Path input. |
|
| 248 | - $entity_base_path_args = array( |
|
| 249 | - // The array of arguments to pass to the callback. In this case, just a description. |
|
| 250 | - 'id' => 'wl-entity-base-path', |
|
| 251 | - 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::ENTITY_BASE_PATH_KEY . ']', |
|
| 252 | - 'value' => $this->configuration_service->get_entity_base_path(), |
|
| 253 | - /* translators: Placeholders: %s - a link to FAQ's page. */ |
|
| 254 | - 'description' => sprintf( __( 'All new pages created with WordLift, will be stored inside your internal vocabulary. You can customize the url pattern of these pages in the field above. Check our <a href="%s">FAQs</a> if you need more info.', 'wordlift' ), 'https://wordlift.io/wordlift-user-faqs/#10-why-and-how-should-i-customize-the-url-of-the-entity-pages-created-in-my-vocabulary' ), |
|
| 255 | - ); |
|
| 256 | - |
|
| 257 | - // The following call is very heavy on large web sites and is always run |
|
| 258 | - // also when not needed: |
|
| 259 | - // $entity_base_path_args['readonly'] = 0 < $this->entity_service->count(); |
|
| 260 | - // |
|
| 261 | - // It is now replaced by a filter to add the `readonly` flag to the |
|
| 262 | - // input element when this is actually rendered. |
|
| 263 | - add_filter( 'wl_admin_input_element_params', array( |
|
| 264 | - $this, |
|
| 265 | - 'entity_path_input_element_params', |
|
| 266 | - ) ); |
|
| 267 | - |
|
| 268 | - // Add the `wl_entity_base_path` field. |
|
| 269 | - add_settings_field( |
|
| 270 | - 'wl-entity-base-path', // ID used to identify the field throughout the theme |
|
| 271 | - __( 'Entity Base Path', 'wordlift' ), // The label to the left of the option interface element |
|
| 272 | - // The name of the function responsible for rendering the option interface |
|
| 273 | - array( $this->input_element, 'render', ), |
|
| 274 | - 'wl_general_settings', // The page on which this option will be displayed |
|
| 275 | - 'wl_general_settings_section', // The name of the section to which this field belongs |
|
| 276 | - $entity_base_path_args |
|
| 277 | - ); |
|
| 278 | - |
|
| 279 | - // Add the `language_name` field. |
|
| 280 | - add_settings_field( |
|
| 281 | - 'wl-site-language', |
|
| 282 | - __( 'Site Language', 'wordlift' ), |
|
| 283 | - array( $this->language_select_element, 'render' ), |
|
| 284 | - 'wl_general_settings', |
|
| 285 | - 'wl_general_settings_section', |
|
| 286 | - array( |
|
| 287 | - // The array of arguments to pass to the callback. In this case, just a description. |
|
| 288 | - 'id' => 'wl-site-language', |
|
| 289 | - 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::LANGUAGE . ']', |
|
| 290 | - 'value' => $this->configuration_service->get_language_code(), |
|
| 291 | - 'description' => __( 'Each WordLift Key can be used only in one language. Pick yours.', 'wordlift' ), |
|
| 292 | - ) |
|
| 293 | - ); |
|
| 294 | - |
|
| 295 | - // Add the `country_code` field. |
|
| 296 | - add_settings_field( |
|
| 297 | - 'wl-country-code', |
|
| 298 | - _x( 'Country', 'wordlift' ), |
|
| 299 | - array( $this->country_select_element, 'render' ), |
|
| 300 | - 'wl_general_settings', |
|
| 301 | - 'wl_general_settings_section', |
|
| 302 | - array( |
|
| 303 | - 'id' => 'wl-country-code', |
|
| 304 | - 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::COUNTRY_CODE . ']', |
|
| 305 | - 'value' => $this->configuration_service->get_country_code(), |
|
| 306 | - 'description' => __( 'Please select a country.', 'wordlift' ), |
|
| 307 | - 'notice' => __( 'The selected language is not supported in this country.</br>Please choose another country or language.', 'wordlift' ), |
|
| 308 | - ) |
|
| 309 | - ); |
|
| 310 | - |
|
| 311 | - // Add the `publisher` field. |
|
| 312 | - add_settings_field( |
|
| 313 | - 'wl-publisher-id', |
|
| 314 | - __( 'Publisher', 'wordlift' ), |
|
| 315 | - array( $this->publisher_element, 'render' ), |
|
| 316 | - 'wl_general_settings', |
|
| 317 | - 'wl_general_settings_section', |
|
| 318 | - array( |
|
| 319 | - 'id' => 'wl-publisher-id', |
|
| 320 | - 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::PUBLISHER_ID . ']', |
|
| 321 | - ) |
|
| 322 | - ); |
|
| 323 | - |
|
| 324 | - // Add the `link by default` field. |
|
| 325 | - add_settings_field( |
|
| 326 | - 'wl-link-by-default', |
|
| 327 | - __( 'Link by Default', 'wordlift' ), |
|
| 328 | - array( $this->radio_input_element, 'render' ), |
|
| 329 | - 'wl_general_settings', |
|
| 330 | - 'wl_general_settings_section', |
|
| 331 | - array( |
|
| 332 | - 'id' => 'wl-link-by-default', |
|
| 333 | - 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::LINK_BY_DEFAULT . ']', |
|
| 334 | - 'value' => $this->configuration_service->is_link_by_default() ? 'yes' : 'no', |
|
| 335 | - 'description' => __( 'Whether to link entities by default or not. This setting applies to all the entities.', 'wordlift' ), |
|
| 336 | - ) |
|
| 337 | - ); |
|
| 338 | - |
|
| 339 | - // Add the `diagnostic data` field. |
|
| 340 | - add_settings_field( |
|
| 341 | - 'wl-send-diagnostic', |
|
| 342 | - __( 'Send Diagnostic Data', 'wordlift' ), |
|
| 343 | - array( $this->radio_input_element, 'render' ), |
|
| 344 | - 'wl_general_settings', |
|
| 345 | - 'wl_general_settings_section', |
|
| 346 | - array( |
|
| 347 | - 'id' => 'wl-send-diagnostic', |
|
| 348 | - 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::SEND_DIAGNOSTIC . ']', |
|
| 349 | - 'value' => 'yes' === $this->configuration_service->get_diagnostic_preferences() ? 'yes' : 'no', |
|
| 350 | - 'description' => __( 'Whether to send diagnostic data or not.', 'wordlift' ), |
|
| 351 | - ) |
|
| 352 | - ); |
|
| 353 | - |
|
| 354 | - } |
|
| 355 | - |
|
| 356 | - /** |
|
| 357 | - * Filter the {@link Wordlift_Admin_Input_Element} in order to add the |
|
| 358 | - * `readonly` flag to the `wl-entity-base-path` input. |
|
| 359 | - * |
|
| 360 | - * @since 3.17.0 |
|
| 361 | - * |
|
| 362 | - * @param array $args An array of {@link Wordlift_Admin_Input_Element} parameters. |
|
| 363 | - * |
|
| 364 | - * @return array The updated array. |
|
| 365 | - */ |
|
| 366 | - public function entity_path_input_element_params( $args ) { |
|
| 367 | - |
|
| 368 | - // Bail out if it's not the `wl-entity-base-path`). |
|
| 369 | - if ( 'wl-entity-base-path' !== $args['id'] ) { |
|
| 370 | - return $args; |
|
| 371 | - } |
|
| 372 | - |
|
| 373 | - // Set the readonly flag according to the entities count. |
|
| 374 | - $args['readonly'] = 0 < $this->entity_service->count(); |
|
| 375 | - |
|
| 376 | - // Return the updated args. |
|
| 377 | - return $args; |
|
| 378 | - } |
|
| 379 | - |
|
| 380 | - /** |
|
| 381 | - * Sanitize the configuration settings to be stored. |
|
| 382 | - * |
|
| 383 | - * If a new entity is being created for the publisher, create it and set The |
|
| 384 | - * publisher setting. |
|
| 385 | - * |
|
| 386 | - * @since 3.11.0 |
|
| 387 | - * |
|
| 388 | - * @param array $input The configuration settings array. |
|
| 389 | - * |
|
| 390 | - * @return array The sanitized input array. |
|
| 391 | - */ |
|
| 392 | - function sanitize_callback( $input ) { |
|
| 393 | - |
|
| 394 | - // Validate the selected country. |
|
| 395 | - $this->validate_country(); |
|
| 396 | - |
|
| 397 | - // Check whether a publisher name has been set. |
|
| 398 | - if ( isset( $_POST['wl_publisher'] ) && ! empty( $_POST['wl_publisher']['name'] ) ) { // WPCS: CSRF, input var, sanitization ok. |
|
| 399 | - $name = $_POST['wl_publisher']['name']; // WPCS: CSRF, input var, sanitization ok. |
|
| 400 | - $type = $_POST['wl_publisher']['type']; // WPCS: CSRF, input var, sanitization ok. |
|
| 401 | - $thumbnail_id = $_POST['wl_publisher']['thumbnail_id'] ?: null; // WPCS: CSRF, input var, sanitization ok. |
|
| 402 | - |
|
| 403 | - // Set the type URI, either http://schema.org/Person or http://schema.org/Organization. |
|
| 404 | - $type_uri = sprintf( 'http://schema.org/%s', 'organization' === $type ? 'Organization' : 'Person' ); |
|
| 405 | - |
|
| 406 | - // Create an entity for the publisher and assign it to the input |
|
| 407 | - // parameter which WordPress automatically saves into the settings. |
|
| 408 | - $input['publisher_id'] = $this->entity_service->create( $name, $type_uri, $thumbnail_id, 'publish' ); |
|
| 409 | - } |
|
| 410 | - |
|
| 411 | - return $input; |
|
| 412 | - } |
|
| 413 | - |
|
| 414 | - /** |
|
| 415 | - * Check whether the currently selected country supports the site language. |
|
| 416 | - * |
|
| 417 | - * @since 3.18.0 |
|
| 418 | - */ |
|
| 419 | - private function validate_country() { |
|
| 420 | - |
|
| 421 | - // Bail out if for some reason the country and language are not set. |
|
| 422 | - if ( |
|
| 423 | - empty( $_POST['wl_general_settings']['site_language'] ) && // WPCS: CSRF, input var, sanitization ok. |
|
| 424 | - empty( $_POST['wl_general_settings']['country_code'] ) // WPCS: CSRF, input var, sanitization ok. |
|
| 425 | - ) { |
|
| 426 | - return; |
|
| 427 | - } |
|
| 428 | - |
|
| 429 | - // Get the values. |
|
| 430 | - $language = $_POST['wl_general_settings']['site_language']; // WPCS: CSRF, input var, sanitization ok. |
|
| 431 | - $country = $_POST['wl_general_settings']['country_code']; // WPCS: CSRF, input var, sanitization ok. |
|
| 432 | - $codes = Wordlift_Countries::get_codes(); |
|
| 433 | - |
|
| 434 | - // Check whether the chosen country has language limitations |
|
| 435 | - // and whether the chosen language is supported for that country. |
|
| 436 | - if ( |
|
| 437 | - ! empty( $codes[ $country ] ) && |
|
| 438 | - ! in_array( $language, $codes[ $country ] ) |
|
| 439 | - ) { |
|
| 440 | - // Otherwise add an error. |
|
| 441 | - add_settings_error( |
|
| 442 | - 'wl-country-code', |
|
| 443 | - esc_attr( 'settings_updated' ), |
|
| 444 | - _x( 'The selected language is not supported for the currently chosen country. Please choose another country or language.', 'wordlift' ) |
|
| 445 | - ); |
|
| 446 | - } |
|
| 447 | - } |
|
| 21 | + /** |
|
| 22 | + * A singleton instance of the Notice service. |
|
| 23 | + * |
|
| 24 | + * @since 3.2.0 |
|
| 25 | + * @access private |
|
| 26 | + * @var \Wordlift_Notice_Service $instance A singleton instance of the Notice service. |
|
| 27 | + */ |
|
| 28 | + private static $instance; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * A {@link Wordlift_Entity_Service} instance. |
|
| 32 | + * |
|
| 33 | + * @since 3.11.0 |
|
| 34 | + * @access private |
|
| 35 | + * @var \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 36 | + */ |
|
| 37 | + private $entity_service; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * A {@link Wordlift_Configuration_Service} instance. |
|
| 41 | + * |
|
| 42 | + * @since 3.11.0 |
|
| 43 | + * @access private |
|
| 44 | + * @var \Wordlift_Configuration_Service $configuration_service A {@link Wordlift_Configuration_Service} instance. |
|
| 45 | + */ |
|
| 46 | + private $configuration_service; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * A {@link Wordlift_Admin_Input_Element} element renderer. |
|
| 50 | + * |
|
| 51 | + * @since 3.11.0 |
|
| 52 | + * @access private |
|
| 53 | + * @var \Wordlift_Admin_Input_Element $input_element An {@link Wordlift_Admin_Input_Element} element renderer. |
|
| 54 | + */ |
|
| 55 | + private $input_element; |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * A {@link Wordlift_Admin_Radio_Input_Element} element renderer. |
|
| 59 | + * |
|
| 60 | + * @since 3.13.0 |
|
| 61 | + * @access protected |
|
| 62 | + * @var \Wordlift_Admin_Radio_Input_Element $radio_input_element A {@link Wordlift_Admin_Radio_Input_Element} element renderer. |
|
| 63 | + */ |
|
| 64 | + private $radio_input_element; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * A {@link Wordlift_Admin_Language_Select_Element} element renderer. |
|
| 68 | + * |
|
| 69 | + * @since 3.11.0 |
|
| 70 | + * @access private |
|
| 71 | + * @var \Wordlift_Admin_Language_Select_Element $language_select_element A {@link Wordlift_Admin_Language_Select_Element} element renderer. |
|
| 72 | + */ |
|
| 73 | + private $language_select_element; |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * A {@link Wordlift_Admin_Country_Select_Element} element renderer. |
|
| 77 | + * |
|
| 78 | + * @since 3.18.0 |
|
| 79 | + * @access private |
|
| 80 | + * @var \Wordlift_Admin_Country_Select_Element $country_select_element A {@link Wordlift_Admin_Country_Select_Element} element renderer. |
|
| 81 | + */ |
|
| 82 | + private $country_select_element; |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * A {@link Wordlift_Admin_Publisher_Element} element renderer. |
|
| 86 | + * |
|
| 87 | + * @since 3.11.0 |
|
| 88 | + * @access private |
|
| 89 | + * @var \Wordlift_Admin_Publisher_Element $publisher_element A {@link Wordlift_Admin_Publisher_Element} element renderer. |
|
| 90 | + */ |
|
| 91 | + private $publisher_element; |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Create a {@link Wordlift_Admin_Settings_Page} instance. |
|
| 95 | + * |
|
| 96 | + * @since 3.11.0 |
|
| 97 | + * |
|
| 98 | + * @param \Wordlift_Configuration_Service $configuration_service A {@link Wordlift_Configuration_Service} instance. |
|
| 99 | + * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 100 | + * @param \Wordlift_Admin_Input_Element $input_element A {@link Wordlift_Admin_Input_Element} element renderer. |
|
| 101 | + * @param \Wordlift_Admin_Language_Select_Element $language_select_element A {@link Wordlift_Admin_Language_Select_Element} element renderer. |
|
| 102 | + * @param \Wordlift_Admin_Country_Select_Element $country_select_element A {@link Wordlift_Admin_Country_Select_Element} element renderer. |
|
| 103 | + * @param \Wordlift_Admin_Publisher_Element $publisher_element A {@link Wordlift_Admin_Publisher_Element} element renderer. |
|
| 104 | + * @param \Wordlift_Admin_Radio_Input_Element $radio_input_element A {@link Wordlift_Admin_Radio_Input_Element} element renderer. |
|
| 105 | + */ |
|
| 106 | + function __construct( $configuration_service, $entity_service, $input_element, $language_select_element, $country_select_element, $publisher_element, $radio_input_element ) { |
|
| 107 | + |
|
| 108 | + $this->configuration_service = $configuration_service; |
|
| 109 | + $this->entity_service = $entity_service; |
|
| 110 | + |
|
| 111 | + // Set a reference to the UI elements. |
|
| 112 | + $this->input_element = $input_element; |
|
| 113 | + $this->radio_input_element = $radio_input_element; |
|
| 114 | + $this->language_select_element = $language_select_element; |
|
| 115 | + $this->country_select_element = $country_select_element; |
|
| 116 | + $this->publisher_element = $publisher_element; |
|
| 117 | + |
|
| 118 | + self::$instance = $this; |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * Get the singleton instance of the Notice service. |
|
| 123 | + * |
|
| 124 | + * @since 3.14.0 |
|
| 125 | + * @return \Wordlift_Admin_Settings_Page The singleton instance of the settings page service. |
|
| 126 | + */ |
|
| 127 | + public static function get_instance() { |
|
| 128 | + |
|
| 129 | + return self::$instance; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * @inheritdoc |
|
| 134 | + */ |
|
| 135 | + function get_parent_slug() { |
|
| 136 | + |
|
| 137 | + return 'wl_admin_menu'; |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * @inheritdoc |
|
| 142 | + */ |
|
| 143 | + function get_capability() { |
|
| 144 | + |
|
| 145 | + return 'manage_options'; |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * @inheritdoc |
|
| 150 | + */ |
|
| 151 | + function get_page_title() { |
|
| 152 | + |
|
| 153 | + return 'WordLift Settings'; |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * @inheritdoc |
|
| 158 | + */ |
|
| 159 | + function get_menu_title() { |
|
| 160 | + |
|
| 161 | + return 'Settings'; |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * @inheritdoc |
|
| 166 | + */ |
|
| 167 | + function get_menu_slug() { |
|
| 168 | + |
|
| 169 | + return 'wl_configuration_admin_menu'; |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + /** |
|
| 173 | + * @inheritdoc |
|
| 174 | + */ |
|
| 175 | + function get_partial_name() { |
|
| 176 | + |
|
| 177 | + return 'wordlift-admin-settings-page.php'; |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * @inheritdoc |
|
| 182 | + */ |
|
| 183 | + public function enqueue_scripts() { |
|
| 184 | + |
|
| 185 | + // Enqueue the media scripts to be used for the publisher's logo selection. |
|
| 186 | + wp_enqueue_media(); |
|
| 187 | + |
|
| 188 | + // JavaScript required for the settings page. |
|
| 189 | + // @todo: try to move to the `wordlift-admin.bundle.js`. |
|
| 190 | + wp_enqueue_script( 'wordlift-admin-settings-page', plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/1/settings.js', array( 'wp-util' ) ); |
|
| 191 | + wp_enqueue_style( 'wordlift-admin-settings-page', plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/1/settings.css' ); |
|
| 192 | + |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + /** |
|
| 196 | + * Configure all the configuration parameters. |
|
| 197 | + * |
|
| 198 | + * Called by the *admin_init* hook. |
|
| 199 | + * |
|
| 200 | + * @since 3.11.0 |
|
| 201 | + */ |
|
| 202 | + function admin_init() { |
|
| 203 | + |
|
| 204 | + // Register WordLift's general settings, providing our own sanitize callback |
|
| 205 | + // which will also check whether the user filled the WL Publisher form. |
|
| 206 | + register_setting( |
|
| 207 | + 'wl_general_settings', |
|
| 208 | + 'wl_general_settings', |
|
| 209 | + array( $this, 'sanitize_callback' ) |
|
| 210 | + ); |
|
| 211 | + |
|
| 212 | + // Add the general settings section. |
|
| 213 | + add_settings_section( |
|
| 214 | + 'wl_general_settings_section', // ID used to identify this section and with which to register options. |
|
| 215 | + '', // Section header. |
|
| 216 | + '', // Callback used to render the description of the section. |
|
| 217 | + 'wl_general_settings' // Page on which to add this section of options. |
|
| 218 | + ); |
|
| 219 | + |
|
| 220 | + $key_args = array( |
|
| 221 | + 'id' => 'wl-key', |
|
| 222 | + 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::KEY . ']', |
|
| 223 | + 'value' => $this->configuration_service->get_key(), |
|
| 224 | + 'description' => __( 'Insert the <a href="https://www.wordlift.io/blogger">WordLift Key</a> you received via email.', 'wordlift' ) |
|
| 225 | + . ' [' . get_option( 'home' ) . ']', |
|
| 226 | + ); |
|
| 227 | + |
|
| 228 | + // Before we were used to validate the key beforehand, but this means |
|
| 229 | + // an http call whenever a page is opened in the admin area. Therefore |
|
| 230 | + // we now leave the input `untouched`, leaving to the client to update |
|
| 231 | + // the `css_class`. |
|
| 232 | + // |
|
| 233 | + // See https://github.com/insideout10/wordlift-plugin/issues/669. |
|
| 234 | + $key_args['css_class'] = 'untouched'; |
|
| 235 | + |
|
| 236 | + // Add the `key` field. |
|
| 237 | + add_settings_field( |
|
| 238 | + 'wl-key', // Element id used to identify the field throughout the theme. |
|
| 239 | + __( 'WordLift Key', 'wordlift' ), // The label to the left of the option interface element. |
|
| 240 | + // The name of the function responsible for rendering the option interface. |
|
| 241 | + array( $this->input_element, 'render' ), |
|
| 242 | + 'wl_general_settings', // The page on which this option will be displayed. |
|
| 243 | + 'wl_general_settings_section', // The name of the section to which this field belongs. |
|
| 244 | + $key_args // The array of arguments to pass to the callback. In this case, just a description. |
|
| 245 | + ); |
|
| 246 | + |
|
| 247 | + // Entity Base Path input. |
|
| 248 | + $entity_base_path_args = array( |
|
| 249 | + // The array of arguments to pass to the callback. In this case, just a description. |
|
| 250 | + 'id' => 'wl-entity-base-path', |
|
| 251 | + 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::ENTITY_BASE_PATH_KEY . ']', |
|
| 252 | + 'value' => $this->configuration_service->get_entity_base_path(), |
|
| 253 | + /* translators: Placeholders: %s - a link to FAQ's page. */ |
|
| 254 | + 'description' => sprintf( __( 'All new pages created with WordLift, will be stored inside your internal vocabulary. You can customize the url pattern of these pages in the field above. Check our <a href="%s">FAQs</a> if you need more info.', 'wordlift' ), 'https://wordlift.io/wordlift-user-faqs/#10-why-and-how-should-i-customize-the-url-of-the-entity-pages-created-in-my-vocabulary' ), |
|
| 255 | + ); |
|
| 256 | + |
|
| 257 | + // The following call is very heavy on large web sites and is always run |
|
| 258 | + // also when not needed: |
|
| 259 | + // $entity_base_path_args['readonly'] = 0 < $this->entity_service->count(); |
|
| 260 | + // |
|
| 261 | + // It is now replaced by a filter to add the `readonly` flag to the |
|
| 262 | + // input element when this is actually rendered. |
|
| 263 | + add_filter( 'wl_admin_input_element_params', array( |
|
| 264 | + $this, |
|
| 265 | + 'entity_path_input_element_params', |
|
| 266 | + ) ); |
|
| 267 | + |
|
| 268 | + // Add the `wl_entity_base_path` field. |
|
| 269 | + add_settings_field( |
|
| 270 | + 'wl-entity-base-path', // ID used to identify the field throughout the theme |
|
| 271 | + __( 'Entity Base Path', 'wordlift' ), // The label to the left of the option interface element |
|
| 272 | + // The name of the function responsible for rendering the option interface |
|
| 273 | + array( $this->input_element, 'render', ), |
|
| 274 | + 'wl_general_settings', // The page on which this option will be displayed |
|
| 275 | + 'wl_general_settings_section', // The name of the section to which this field belongs |
|
| 276 | + $entity_base_path_args |
|
| 277 | + ); |
|
| 278 | + |
|
| 279 | + // Add the `language_name` field. |
|
| 280 | + add_settings_field( |
|
| 281 | + 'wl-site-language', |
|
| 282 | + __( 'Site Language', 'wordlift' ), |
|
| 283 | + array( $this->language_select_element, 'render' ), |
|
| 284 | + 'wl_general_settings', |
|
| 285 | + 'wl_general_settings_section', |
|
| 286 | + array( |
|
| 287 | + // The array of arguments to pass to the callback. In this case, just a description. |
|
| 288 | + 'id' => 'wl-site-language', |
|
| 289 | + 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::LANGUAGE . ']', |
|
| 290 | + 'value' => $this->configuration_service->get_language_code(), |
|
| 291 | + 'description' => __( 'Each WordLift Key can be used only in one language. Pick yours.', 'wordlift' ), |
|
| 292 | + ) |
|
| 293 | + ); |
|
| 294 | + |
|
| 295 | + // Add the `country_code` field. |
|
| 296 | + add_settings_field( |
|
| 297 | + 'wl-country-code', |
|
| 298 | + _x( 'Country', 'wordlift' ), |
|
| 299 | + array( $this->country_select_element, 'render' ), |
|
| 300 | + 'wl_general_settings', |
|
| 301 | + 'wl_general_settings_section', |
|
| 302 | + array( |
|
| 303 | + 'id' => 'wl-country-code', |
|
| 304 | + 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::COUNTRY_CODE . ']', |
|
| 305 | + 'value' => $this->configuration_service->get_country_code(), |
|
| 306 | + 'description' => __( 'Please select a country.', 'wordlift' ), |
|
| 307 | + 'notice' => __( 'The selected language is not supported in this country.</br>Please choose another country or language.', 'wordlift' ), |
|
| 308 | + ) |
|
| 309 | + ); |
|
| 310 | + |
|
| 311 | + // Add the `publisher` field. |
|
| 312 | + add_settings_field( |
|
| 313 | + 'wl-publisher-id', |
|
| 314 | + __( 'Publisher', 'wordlift' ), |
|
| 315 | + array( $this->publisher_element, 'render' ), |
|
| 316 | + 'wl_general_settings', |
|
| 317 | + 'wl_general_settings_section', |
|
| 318 | + array( |
|
| 319 | + 'id' => 'wl-publisher-id', |
|
| 320 | + 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::PUBLISHER_ID . ']', |
|
| 321 | + ) |
|
| 322 | + ); |
|
| 323 | + |
|
| 324 | + // Add the `link by default` field. |
|
| 325 | + add_settings_field( |
|
| 326 | + 'wl-link-by-default', |
|
| 327 | + __( 'Link by Default', 'wordlift' ), |
|
| 328 | + array( $this->radio_input_element, 'render' ), |
|
| 329 | + 'wl_general_settings', |
|
| 330 | + 'wl_general_settings_section', |
|
| 331 | + array( |
|
| 332 | + 'id' => 'wl-link-by-default', |
|
| 333 | + 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::LINK_BY_DEFAULT . ']', |
|
| 334 | + 'value' => $this->configuration_service->is_link_by_default() ? 'yes' : 'no', |
|
| 335 | + 'description' => __( 'Whether to link entities by default or not. This setting applies to all the entities.', 'wordlift' ), |
|
| 336 | + ) |
|
| 337 | + ); |
|
| 338 | + |
|
| 339 | + // Add the `diagnostic data` field. |
|
| 340 | + add_settings_field( |
|
| 341 | + 'wl-send-diagnostic', |
|
| 342 | + __( 'Send Diagnostic Data', 'wordlift' ), |
|
| 343 | + array( $this->radio_input_element, 'render' ), |
|
| 344 | + 'wl_general_settings', |
|
| 345 | + 'wl_general_settings_section', |
|
| 346 | + array( |
|
| 347 | + 'id' => 'wl-send-diagnostic', |
|
| 348 | + 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::SEND_DIAGNOSTIC . ']', |
|
| 349 | + 'value' => 'yes' === $this->configuration_service->get_diagnostic_preferences() ? 'yes' : 'no', |
|
| 350 | + 'description' => __( 'Whether to send diagnostic data or not.', 'wordlift' ), |
|
| 351 | + ) |
|
| 352 | + ); |
|
| 353 | + |
|
| 354 | + } |
|
| 355 | + |
|
| 356 | + /** |
|
| 357 | + * Filter the {@link Wordlift_Admin_Input_Element} in order to add the |
|
| 358 | + * `readonly` flag to the `wl-entity-base-path` input. |
|
| 359 | + * |
|
| 360 | + * @since 3.17.0 |
|
| 361 | + * |
|
| 362 | + * @param array $args An array of {@link Wordlift_Admin_Input_Element} parameters. |
|
| 363 | + * |
|
| 364 | + * @return array The updated array. |
|
| 365 | + */ |
|
| 366 | + public function entity_path_input_element_params( $args ) { |
|
| 367 | + |
|
| 368 | + // Bail out if it's not the `wl-entity-base-path`). |
|
| 369 | + if ( 'wl-entity-base-path' !== $args['id'] ) { |
|
| 370 | + return $args; |
|
| 371 | + } |
|
| 372 | + |
|
| 373 | + // Set the readonly flag according to the entities count. |
|
| 374 | + $args['readonly'] = 0 < $this->entity_service->count(); |
|
| 375 | + |
|
| 376 | + // Return the updated args. |
|
| 377 | + return $args; |
|
| 378 | + } |
|
| 379 | + |
|
| 380 | + /** |
|
| 381 | + * Sanitize the configuration settings to be stored. |
|
| 382 | + * |
|
| 383 | + * If a new entity is being created for the publisher, create it and set The |
|
| 384 | + * publisher setting. |
|
| 385 | + * |
|
| 386 | + * @since 3.11.0 |
|
| 387 | + * |
|
| 388 | + * @param array $input The configuration settings array. |
|
| 389 | + * |
|
| 390 | + * @return array The sanitized input array. |
|
| 391 | + */ |
|
| 392 | + function sanitize_callback( $input ) { |
|
| 393 | + |
|
| 394 | + // Validate the selected country. |
|
| 395 | + $this->validate_country(); |
|
| 396 | + |
|
| 397 | + // Check whether a publisher name has been set. |
|
| 398 | + if ( isset( $_POST['wl_publisher'] ) && ! empty( $_POST['wl_publisher']['name'] ) ) { // WPCS: CSRF, input var, sanitization ok. |
|
| 399 | + $name = $_POST['wl_publisher']['name']; // WPCS: CSRF, input var, sanitization ok. |
|
| 400 | + $type = $_POST['wl_publisher']['type']; // WPCS: CSRF, input var, sanitization ok. |
|
| 401 | + $thumbnail_id = $_POST['wl_publisher']['thumbnail_id'] ?: null; // WPCS: CSRF, input var, sanitization ok. |
|
| 402 | + |
|
| 403 | + // Set the type URI, either http://schema.org/Person or http://schema.org/Organization. |
|
| 404 | + $type_uri = sprintf( 'http://schema.org/%s', 'organization' === $type ? 'Organization' : 'Person' ); |
|
| 405 | + |
|
| 406 | + // Create an entity for the publisher and assign it to the input |
|
| 407 | + // parameter which WordPress automatically saves into the settings. |
|
| 408 | + $input['publisher_id'] = $this->entity_service->create( $name, $type_uri, $thumbnail_id, 'publish' ); |
|
| 409 | + } |
|
| 410 | + |
|
| 411 | + return $input; |
|
| 412 | + } |
|
| 413 | + |
|
| 414 | + /** |
|
| 415 | + * Check whether the currently selected country supports the site language. |
|
| 416 | + * |
|
| 417 | + * @since 3.18.0 |
|
| 418 | + */ |
|
| 419 | + private function validate_country() { |
|
| 420 | + |
|
| 421 | + // Bail out if for some reason the country and language are not set. |
|
| 422 | + if ( |
|
| 423 | + empty( $_POST['wl_general_settings']['site_language'] ) && // WPCS: CSRF, input var, sanitization ok. |
|
| 424 | + empty( $_POST['wl_general_settings']['country_code'] ) // WPCS: CSRF, input var, sanitization ok. |
|
| 425 | + ) { |
|
| 426 | + return; |
|
| 427 | + } |
|
| 428 | + |
|
| 429 | + // Get the values. |
|
| 430 | + $language = $_POST['wl_general_settings']['site_language']; // WPCS: CSRF, input var, sanitization ok. |
|
| 431 | + $country = $_POST['wl_general_settings']['country_code']; // WPCS: CSRF, input var, sanitization ok. |
|
| 432 | + $codes = Wordlift_Countries::get_codes(); |
|
| 433 | + |
|
| 434 | + // Check whether the chosen country has language limitations |
|
| 435 | + // and whether the chosen language is supported for that country. |
|
| 436 | + if ( |
|
| 437 | + ! empty( $codes[ $country ] ) && |
|
| 438 | + ! in_array( $language, $codes[ $country ] ) |
|
| 439 | + ) { |
|
| 440 | + // Otherwise add an error. |
|
| 441 | + add_settings_error( |
|
| 442 | + 'wl-country-code', |
|
| 443 | + esc_attr( 'settings_updated' ), |
|
| 444 | + _x( 'The selected language is not supported for the currently chosen country. Please choose another country or language.', 'wordlift' ) |
|
| 445 | + ); |
|
| 446 | + } |
|
| 447 | + } |
|
| 448 | 448 | |
| 449 | 449 | } |
@@ -103,7 +103,7 @@ discard block |
||
| 103 | 103 | * @param \Wordlift_Admin_Publisher_Element $publisher_element A {@link Wordlift_Admin_Publisher_Element} element renderer. |
| 104 | 104 | * @param \Wordlift_Admin_Radio_Input_Element $radio_input_element A {@link Wordlift_Admin_Radio_Input_Element} element renderer. |
| 105 | 105 | */ |
| 106 | - function __construct( $configuration_service, $entity_service, $input_element, $language_select_element, $country_select_element, $publisher_element, $radio_input_element ) { |
|
| 106 | + function __construct($configuration_service, $entity_service, $input_element, $language_select_element, $country_select_element, $publisher_element, $radio_input_element) { |
|
| 107 | 107 | |
| 108 | 108 | $this->configuration_service = $configuration_service; |
| 109 | 109 | $this->entity_service = $entity_service; |
@@ -187,8 +187,8 @@ discard block |
||
| 187 | 187 | |
| 188 | 188 | // JavaScript required for the settings page. |
| 189 | 189 | // @todo: try to move to the `wordlift-admin.bundle.js`. |
| 190 | - wp_enqueue_script( 'wordlift-admin-settings-page', plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/1/settings.js', array( 'wp-util' ) ); |
|
| 191 | - wp_enqueue_style( 'wordlift-admin-settings-page', plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/1/settings.css' ); |
|
| 190 | + wp_enqueue_script('wordlift-admin-settings-page', plugin_dir_url(dirname(__FILE__)).'admin/js/1/settings.js', array('wp-util')); |
|
| 191 | + wp_enqueue_style('wordlift-admin-settings-page', plugin_dir_url(dirname(__FILE__)).'admin/js/1/settings.css'); |
|
| 192 | 192 | |
| 193 | 193 | } |
| 194 | 194 | |
@@ -206,23 +206,23 @@ discard block |
||
| 206 | 206 | register_setting( |
| 207 | 207 | 'wl_general_settings', |
| 208 | 208 | 'wl_general_settings', |
| 209 | - array( $this, 'sanitize_callback' ) |
|
| 209 | + array($this, 'sanitize_callback') |
|
| 210 | 210 | ); |
| 211 | 211 | |
| 212 | 212 | // Add the general settings section. |
| 213 | 213 | add_settings_section( |
| 214 | 214 | 'wl_general_settings_section', // ID used to identify this section and with which to register options. |
| 215 | - '', // Section header. |
|
| 216 | - '', // Callback used to render the description of the section. |
|
| 215 | + '', // Section header. |
|
| 216 | + '', // Callback used to render the description of the section. |
|
| 217 | 217 | 'wl_general_settings' // Page on which to add this section of options. |
| 218 | 218 | ); |
| 219 | 219 | |
| 220 | 220 | $key_args = array( |
| 221 | 221 | 'id' => 'wl-key', |
| 222 | - 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::KEY . ']', |
|
| 222 | + 'name' => 'wl_general_settings['.Wordlift_Configuration_Service::KEY.']', |
|
| 223 | 223 | 'value' => $this->configuration_service->get_key(), |
| 224 | - 'description' => __( 'Insert the <a href="https://www.wordlift.io/blogger">WordLift Key</a> you received via email.', 'wordlift' ) |
|
| 225 | - . ' [' . get_option( 'home' ) . ']', |
|
| 224 | + 'description' => __('Insert the <a href="https://www.wordlift.io/blogger">WordLift Key</a> you received via email.', 'wordlift') |
|
| 225 | + . ' ['.get_option('home').']', |
|
| 226 | 226 | ); |
| 227 | 227 | |
| 228 | 228 | // Before we were used to validate the key beforehand, but this means |
@@ -235,12 +235,12 @@ discard block |
||
| 235 | 235 | |
| 236 | 236 | // Add the `key` field. |
| 237 | 237 | add_settings_field( |
| 238 | - 'wl-key', // Element id used to identify the field throughout the theme. |
|
| 239 | - __( 'WordLift Key', 'wordlift' ), // The label to the left of the option interface element. |
|
| 238 | + 'wl-key', // Element id used to identify the field throughout the theme. |
|
| 239 | + __('WordLift Key', 'wordlift'), // The label to the left of the option interface element. |
|
| 240 | 240 | // The name of the function responsible for rendering the option interface. |
| 241 | - array( $this->input_element, 'render' ), |
|
| 242 | - 'wl_general_settings', // The page on which this option will be displayed. |
|
| 243 | - 'wl_general_settings_section', // The name of the section to which this field belongs. |
|
| 241 | + array($this->input_element, 'render'), |
|
| 242 | + 'wl_general_settings', // The page on which this option will be displayed. |
|
| 243 | + 'wl_general_settings_section', // The name of the section to which this field belongs. |
|
| 244 | 244 | $key_args // The array of arguments to pass to the callback. In this case, just a description. |
| 245 | 245 | ); |
| 246 | 246 | |
@@ -248,10 +248,10 @@ discard block |
||
| 248 | 248 | $entity_base_path_args = array( |
| 249 | 249 | // The array of arguments to pass to the callback. In this case, just a description. |
| 250 | 250 | 'id' => 'wl-entity-base-path', |
| 251 | - 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::ENTITY_BASE_PATH_KEY . ']', |
|
| 251 | + 'name' => 'wl_general_settings['.Wordlift_Configuration_Service::ENTITY_BASE_PATH_KEY.']', |
|
| 252 | 252 | 'value' => $this->configuration_service->get_entity_base_path(), |
| 253 | 253 | /* translators: Placeholders: %s - a link to FAQ's page. */ |
| 254 | - 'description' => sprintf( __( 'All new pages created with WordLift, will be stored inside your internal vocabulary. You can customize the url pattern of these pages in the field above. Check our <a href="%s">FAQs</a> if you need more info.', 'wordlift' ), 'https://wordlift.io/wordlift-user-faqs/#10-why-and-how-should-i-customize-the-url-of-the-entity-pages-created-in-my-vocabulary' ), |
|
| 254 | + 'description' => sprintf(__('All new pages created with WordLift, will be stored inside your internal vocabulary. You can customize the url pattern of these pages in the field above. Check our <a href="%s">FAQs</a> if you need more info.', 'wordlift'), 'https://wordlift.io/wordlift-user-faqs/#10-why-and-how-should-i-customize-the-url-of-the-entity-pages-created-in-my-vocabulary'), |
|
| 255 | 255 | ); |
| 256 | 256 | |
| 257 | 257 | // The following call is very heavy on large web sites and is always run |
@@ -260,94 +260,94 @@ discard block |
||
| 260 | 260 | // |
| 261 | 261 | // It is now replaced by a filter to add the `readonly` flag to the |
| 262 | 262 | // input element when this is actually rendered. |
| 263 | - add_filter( 'wl_admin_input_element_params', array( |
|
| 263 | + add_filter('wl_admin_input_element_params', array( |
|
| 264 | 264 | $this, |
| 265 | 265 | 'entity_path_input_element_params', |
| 266 | - ) ); |
|
| 266 | + )); |
|
| 267 | 267 | |
| 268 | 268 | // Add the `wl_entity_base_path` field. |
| 269 | 269 | add_settings_field( |
| 270 | - 'wl-entity-base-path', // ID used to identify the field throughout the theme |
|
| 271 | - __( 'Entity Base Path', 'wordlift' ), // The label to the left of the option interface element |
|
| 270 | + 'wl-entity-base-path', // ID used to identify the field throughout the theme |
|
| 271 | + __('Entity Base Path', 'wordlift'), // The label to the left of the option interface element |
|
| 272 | 272 | // The name of the function responsible for rendering the option interface |
| 273 | - array( $this->input_element, 'render', ), |
|
| 274 | - 'wl_general_settings', // The page on which this option will be displayed |
|
| 275 | - 'wl_general_settings_section', // The name of the section to which this field belongs |
|
| 273 | + array($this->input_element, 'render',), |
|
| 274 | + 'wl_general_settings', // The page on which this option will be displayed |
|
| 275 | + 'wl_general_settings_section', // The name of the section to which this field belongs |
|
| 276 | 276 | $entity_base_path_args |
| 277 | 277 | ); |
| 278 | 278 | |
| 279 | 279 | // Add the `language_name` field. |
| 280 | 280 | add_settings_field( |
| 281 | 281 | 'wl-site-language', |
| 282 | - __( 'Site Language', 'wordlift' ), |
|
| 283 | - array( $this->language_select_element, 'render' ), |
|
| 282 | + __('Site Language', 'wordlift'), |
|
| 283 | + array($this->language_select_element, 'render'), |
|
| 284 | 284 | 'wl_general_settings', |
| 285 | 285 | 'wl_general_settings_section', |
| 286 | 286 | array( |
| 287 | 287 | // The array of arguments to pass to the callback. In this case, just a description. |
| 288 | 288 | 'id' => 'wl-site-language', |
| 289 | - 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::LANGUAGE . ']', |
|
| 289 | + 'name' => 'wl_general_settings['.Wordlift_Configuration_Service::LANGUAGE.']', |
|
| 290 | 290 | 'value' => $this->configuration_service->get_language_code(), |
| 291 | - 'description' => __( 'Each WordLift Key can be used only in one language. Pick yours.', 'wordlift' ), |
|
| 291 | + 'description' => __('Each WordLift Key can be used only in one language. Pick yours.', 'wordlift'), |
|
| 292 | 292 | ) |
| 293 | 293 | ); |
| 294 | 294 | |
| 295 | 295 | // Add the `country_code` field. |
| 296 | 296 | add_settings_field( |
| 297 | 297 | 'wl-country-code', |
| 298 | - _x( 'Country', 'wordlift' ), |
|
| 299 | - array( $this->country_select_element, 'render' ), |
|
| 298 | + _x('Country', 'wordlift'), |
|
| 299 | + array($this->country_select_element, 'render'), |
|
| 300 | 300 | 'wl_general_settings', |
| 301 | 301 | 'wl_general_settings_section', |
| 302 | 302 | array( |
| 303 | 303 | 'id' => 'wl-country-code', |
| 304 | - 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::COUNTRY_CODE . ']', |
|
| 304 | + 'name' => 'wl_general_settings['.Wordlift_Configuration_Service::COUNTRY_CODE.']', |
|
| 305 | 305 | 'value' => $this->configuration_service->get_country_code(), |
| 306 | - 'description' => __( 'Please select a country.', 'wordlift' ), |
|
| 307 | - 'notice' => __( 'The selected language is not supported in this country.</br>Please choose another country or language.', 'wordlift' ), |
|
| 306 | + 'description' => __('Please select a country.', 'wordlift'), |
|
| 307 | + 'notice' => __('The selected language is not supported in this country.</br>Please choose another country or language.', 'wordlift'), |
|
| 308 | 308 | ) |
| 309 | 309 | ); |
| 310 | 310 | |
| 311 | 311 | // Add the `publisher` field. |
| 312 | 312 | add_settings_field( |
| 313 | 313 | 'wl-publisher-id', |
| 314 | - __( 'Publisher', 'wordlift' ), |
|
| 315 | - array( $this->publisher_element, 'render' ), |
|
| 314 | + __('Publisher', 'wordlift'), |
|
| 315 | + array($this->publisher_element, 'render'), |
|
| 316 | 316 | 'wl_general_settings', |
| 317 | 317 | 'wl_general_settings_section', |
| 318 | 318 | array( |
| 319 | 319 | 'id' => 'wl-publisher-id', |
| 320 | - 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::PUBLISHER_ID . ']', |
|
| 320 | + 'name' => 'wl_general_settings['.Wordlift_Configuration_Service::PUBLISHER_ID.']', |
|
| 321 | 321 | ) |
| 322 | 322 | ); |
| 323 | 323 | |
| 324 | 324 | // Add the `link by default` field. |
| 325 | 325 | add_settings_field( |
| 326 | 326 | 'wl-link-by-default', |
| 327 | - __( 'Link by Default', 'wordlift' ), |
|
| 328 | - array( $this->radio_input_element, 'render' ), |
|
| 327 | + __('Link by Default', 'wordlift'), |
|
| 328 | + array($this->radio_input_element, 'render'), |
|
| 329 | 329 | 'wl_general_settings', |
| 330 | 330 | 'wl_general_settings_section', |
| 331 | 331 | array( |
| 332 | 332 | 'id' => 'wl-link-by-default', |
| 333 | - 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::LINK_BY_DEFAULT . ']', |
|
| 333 | + 'name' => 'wl_general_settings['.Wordlift_Configuration_Service::LINK_BY_DEFAULT.']', |
|
| 334 | 334 | 'value' => $this->configuration_service->is_link_by_default() ? 'yes' : 'no', |
| 335 | - 'description' => __( 'Whether to link entities by default or not. This setting applies to all the entities.', 'wordlift' ), |
|
| 335 | + 'description' => __('Whether to link entities by default or not. This setting applies to all the entities.', 'wordlift'), |
|
| 336 | 336 | ) |
| 337 | 337 | ); |
| 338 | 338 | |
| 339 | 339 | // Add the `diagnostic data` field. |
| 340 | 340 | add_settings_field( |
| 341 | 341 | 'wl-send-diagnostic', |
| 342 | - __( 'Send Diagnostic Data', 'wordlift' ), |
|
| 343 | - array( $this->radio_input_element, 'render' ), |
|
| 342 | + __('Send Diagnostic Data', 'wordlift'), |
|
| 343 | + array($this->radio_input_element, 'render'), |
|
| 344 | 344 | 'wl_general_settings', |
| 345 | 345 | 'wl_general_settings_section', |
| 346 | 346 | array( |
| 347 | 347 | 'id' => 'wl-send-diagnostic', |
| 348 | - 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::SEND_DIAGNOSTIC . ']', |
|
| 348 | + 'name' => 'wl_general_settings['.Wordlift_Configuration_Service::SEND_DIAGNOSTIC.']', |
|
| 349 | 349 | 'value' => 'yes' === $this->configuration_service->get_diagnostic_preferences() ? 'yes' : 'no', |
| 350 | - 'description' => __( 'Whether to send diagnostic data or not.', 'wordlift' ), |
|
| 350 | + 'description' => __('Whether to send diagnostic data or not.', 'wordlift'), |
|
| 351 | 351 | ) |
| 352 | 352 | ); |
| 353 | 353 | |
@@ -363,10 +363,10 @@ discard block |
||
| 363 | 363 | * |
| 364 | 364 | * @return array The updated array. |
| 365 | 365 | */ |
| 366 | - public function entity_path_input_element_params( $args ) { |
|
| 366 | + public function entity_path_input_element_params($args) { |
|
| 367 | 367 | |
| 368 | 368 | // Bail out if it's not the `wl-entity-base-path`). |
| 369 | - if ( 'wl-entity-base-path' !== $args['id'] ) { |
|
| 369 | + if ('wl-entity-base-path' !== $args['id']) { |
|
| 370 | 370 | return $args; |
| 371 | 371 | } |
| 372 | 372 | |
@@ -389,23 +389,23 @@ discard block |
||
| 389 | 389 | * |
| 390 | 390 | * @return array The sanitized input array. |
| 391 | 391 | */ |
| 392 | - function sanitize_callback( $input ) { |
|
| 392 | + function sanitize_callback($input) { |
|
| 393 | 393 | |
| 394 | 394 | // Validate the selected country. |
| 395 | 395 | $this->validate_country(); |
| 396 | 396 | |
| 397 | 397 | // Check whether a publisher name has been set. |
| 398 | - if ( isset( $_POST['wl_publisher'] ) && ! empty( $_POST['wl_publisher']['name'] ) ) { // WPCS: CSRF, input var, sanitization ok. |
|
| 398 | + if (isset($_POST['wl_publisher']) && ! empty($_POST['wl_publisher']['name'])) { // WPCS: CSRF, input var, sanitization ok. |
|
| 399 | 399 | $name = $_POST['wl_publisher']['name']; // WPCS: CSRF, input var, sanitization ok. |
| 400 | 400 | $type = $_POST['wl_publisher']['type']; // WPCS: CSRF, input var, sanitization ok. |
| 401 | 401 | $thumbnail_id = $_POST['wl_publisher']['thumbnail_id'] ?: null; // WPCS: CSRF, input var, sanitization ok. |
| 402 | 402 | |
| 403 | 403 | // Set the type URI, either http://schema.org/Person or http://schema.org/Organization. |
| 404 | - $type_uri = sprintf( 'http://schema.org/%s', 'organization' === $type ? 'Organization' : 'Person' ); |
|
| 404 | + $type_uri = sprintf('http://schema.org/%s', 'organization' === $type ? 'Organization' : 'Person'); |
|
| 405 | 405 | |
| 406 | 406 | // Create an entity for the publisher and assign it to the input |
| 407 | 407 | // parameter which WordPress automatically saves into the settings. |
| 408 | - $input['publisher_id'] = $this->entity_service->create( $name, $type_uri, $thumbnail_id, 'publish' ); |
|
| 408 | + $input['publisher_id'] = $this->entity_service->create($name, $type_uri, $thumbnail_id, 'publish'); |
|
| 409 | 409 | } |
| 410 | 410 | |
| 411 | 411 | return $input; |
@@ -420,8 +420,8 @@ discard block |
||
| 420 | 420 | |
| 421 | 421 | // Bail out if for some reason the country and language are not set. |
| 422 | 422 | if ( |
| 423 | - empty( $_POST['wl_general_settings']['site_language'] ) && // WPCS: CSRF, input var, sanitization ok. |
|
| 424 | - empty( $_POST['wl_general_settings']['country_code'] ) // WPCS: CSRF, input var, sanitization ok. |
|
| 423 | + empty($_POST['wl_general_settings']['site_language']) && // WPCS: CSRF, input var, sanitization ok. |
|
| 424 | + empty($_POST['wl_general_settings']['country_code']) // WPCS: CSRF, input var, sanitization ok. |
|
| 425 | 425 | ) { |
| 426 | 426 | return; |
| 427 | 427 | } |
@@ -434,14 +434,14 @@ discard block |
||
| 434 | 434 | // Check whether the chosen country has language limitations |
| 435 | 435 | // and whether the chosen language is supported for that country. |
| 436 | 436 | if ( |
| 437 | - ! empty( $codes[ $country ] ) && |
|
| 438 | - ! in_array( $language, $codes[ $country ] ) |
|
| 437 | + ! empty($codes[$country]) && |
|
| 438 | + ! in_array($language, $codes[$country]) |
|
| 439 | 439 | ) { |
| 440 | 440 | // Otherwise add an error. |
| 441 | 441 | add_settings_error( |
| 442 | 442 | 'wl-country-code', |
| 443 | - esc_attr( 'settings_updated' ), |
|
| 444 | - _x( 'The selected language is not supported for the currently chosen country. Please choose another country or language.', 'wordlift' ) |
|
| 443 | + esc_attr('settings_updated'), |
|
| 444 | + _x('The selected language is not supported for the currently chosen country. Please choose another country or language.', 'wordlift') |
|
| 445 | 445 | ); |
| 446 | 446 | } |
| 447 | 447 | } |