@@ -651,7 +651,7 @@ |
||
| 651 | 651 | /** |
| 652 | 652 | * Get the 'analytics-enable' option. |
| 653 | 653 | * |
| 654 | - * @return string 'no' or 'yes' representing bool. |
|
| 654 | + * @return boolean 'no' or 'yes' representing bool. |
|
| 655 | 655 | * @since 3.21.0 |
| 656 | 656 | * |
| 657 | 657 | */ |
@@ -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,715 +21,715 @@ 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 | - $site_url = apply_filters( 'wl_production_site_url', site_url() ); |
|
| 502 | - |
|
| 503 | - // Build the URL. |
|
| 504 | - $url = $this->get_accounts() |
|
| 505 | - . '?key=' . rawurlencode( $key ) |
|
| 506 | - . '&url=' . rawurlencode( $site_url ) |
|
| 507 | - . '&country=' . $this->get_country_code() |
|
| 508 | - . '&language=' . $this->get_language_code(); |
|
| 509 | - |
|
| 510 | - $args = wp_parse_args( unserialize( WL_REDLINK_API_HTTP_OPTIONS ), array( |
|
| 511 | - 'method' => 'PUT', |
|
| 512 | - ) ); |
|
| 513 | - |
|
| 514 | - $response = wp_remote_request( $url, $args ); |
|
| 515 | - |
|
| 516 | - // The response is an error. |
|
| 517 | - if ( is_wp_error( $response ) ) { |
|
| 518 | - $this->log->error( 'An error occurred setting the dataset URI: ' . $response->get_error_message() ); |
|
| 519 | - |
|
| 520 | - $this->set_dataset_uri( '' ); |
|
| 521 | - $this->set_package_type( null ); |
|
| 522 | - |
|
| 523 | - return; |
|
| 524 | - } |
|
| 525 | - |
|
| 526 | - // The response is not OK. |
|
| 527 | - if ( 200 !== (int) $response['response']['code'] ) { |
|
| 528 | - $this->log->error( "Unexpected status code when opening URL $url: " . $response['response']['code'] ); |
|
| 529 | - |
|
| 530 | - $this->set_dataset_uri( '' ); |
|
| 531 | - $this->set_package_type( null ); |
|
| 532 | - |
|
| 533 | - return; |
|
| 534 | - } |
|
| 535 | - |
|
| 536 | - /* |
|
| 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 | + $site_url = apply_filters( 'wl_production_site_url', site_url() ); |
|
| 502 | + |
|
| 503 | + // Build the URL. |
|
| 504 | + $url = $this->get_accounts() |
|
| 505 | + . '?key=' . rawurlencode( $key ) |
|
| 506 | + . '&url=' . rawurlencode( $site_url ) |
|
| 507 | + . '&country=' . $this->get_country_code() |
|
| 508 | + . '&language=' . $this->get_language_code(); |
|
| 509 | + |
|
| 510 | + $args = wp_parse_args( unserialize( WL_REDLINK_API_HTTP_OPTIONS ), array( |
|
| 511 | + 'method' => 'PUT', |
|
| 512 | + ) ); |
|
| 513 | + |
|
| 514 | + $response = wp_remote_request( $url, $args ); |
|
| 515 | + |
|
| 516 | + // The response is an error. |
|
| 517 | + if ( is_wp_error( $response ) ) { |
|
| 518 | + $this->log->error( 'An error occurred setting the dataset URI: ' . $response->get_error_message() ); |
|
| 519 | + |
|
| 520 | + $this->set_dataset_uri( '' ); |
|
| 521 | + $this->set_package_type( null ); |
|
| 522 | + |
|
| 523 | + return; |
|
| 524 | + } |
|
| 525 | + |
|
| 526 | + // The response is not OK. |
|
| 527 | + if ( 200 !== (int) $response['response']['code'] ) { |
|
| 528 | + $this->log->error( "Unexpected status code when opening URL $url: " . $response['response']['code'] ); |
|
| 529 | + |
|
| 530 | + $this->set_dataset_uri( '' ); |
|
| 531 | + $this->set_package_type( null ); |
|
| 532 | + |
|
| 533 | + return; |
|
| 534 | + } |
|
| 535 | + |
|
| 536 | + /* |
|
| 537 | 537 | * We also store the package type. |
| 538 | 538 | * |
| 539 | 539 | * @since 3.20.0 |
| 540 | 540 | */ |
| 541 | - $json = json_decode( $response['body'] ); |
|
| 542 | - $dataset_uri = $json->datasetURI; |
|
| 543 | - $package_type = isset( $json->packageType ) ? $json->packageType : null; |
|
| 544 | - |
|
| 545 | - $this->log->info( "Updating [ dataset uri :: $dataset_uri ][ package type :: $package_type ]..." ); |
|
| 546 | - |
|
| 547 | - $this->set_dataset_uri( $dataset_uri ); |
|
| 548 | - $this->set_package_type( $package_type ); |
|
| 549 | - |
|
| 550 | - } |
|
| 551 | - |
|
| 552 | - /** |
|
| 553 | - * Handle the edge case where a user submits the same key again |
|
| 554 | - * when he does not have the dataset uri to regain it. |
|
| 555 | - * |
|
| 556 | - * This can not be handled in the normal option update hook because |
|
| 557 | - * it is not being triggered when the save value equals to the one already |
|
| 558 | - * in the DB. |
|
| 559 | - * |
|
| 560 | - * @param mixed $value The new, unserialized option value. |
|
| 561 | - * @param mixed $old_value The old option value. |
|
| 562 | - * |
|
| 563 | - * @return mixed The same value in the $value parameter |
|
| 564 | - * @since 3.12.0 |
|
| 565 | - * |
|
| 566 | - */ |
|
| 567 | - function maybe_update_dataset_uri( $value, $old_value ) { |
|
| 568 | - |
|
| 569 | - // Check the old key value and the new one. Here we're only handling the |
|
| 570 | - // case where the key hasn't changed and the dataset URI isn't set. The |
|
| 571 | - // other case, i.e. a new key is inserted, is handled at `update_key`. |
|
| 572 | - $old_key = isset( $old_value['key'] ) ? $old_value['key'] : ''; |
|
| 573 | - $new_key = isset( $value['key'] ) ? $value['key'] : ''; |
|
| 574 | - |
|
| 575 | - $dataset_uri = $this->get_dataset_uri(); |
|
| 576 | - |
|
| 577 | - if ( ! empty( $new_key ) && $new_key === $old_key && empty( $dataset_uri ) ) { |
|
| 578 | - |
|
| 579 | - // make the request to the remote server to try to get the dataset uri. |
|
| 580 | - $this->get_remote_dataset_uri( $new_key ); |
|
| 581 | - } |
|
| 582 | - |
|
| 583 | - return $value; |
|
| 584 | - } |
|
| 585 | - |
|
| 586 | - /** |
|
| 587 | - * Get the API URI to retrieve the dataset URI using the WordLift Key. |
|
| 588 | - * |
|
| 589 | - * @param string $key The WordLift key to use. |
|
| 590 | - * |
|
| 591 | - * @return string The API URI. |
|
| 592 | - * @since 3.11.0 |
|
| 593 | - * |
|
| 594 | - */ |
|
| 595 | - public function get_accounts_by_key_dataset_uri( $key ) { |
|
| 596 | - |
|
| 597 | - return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . "accounts/key=$key/dataset_uri"; |
|
| 598 | - } |
|
| 599 | - |
|
| 600 | - /** |
|
| 601 | - * Get the API URI to retrieve the account info using the WordLift Key. |
|
| 602 | - * |
|
| 603 | - * @param string $key The WordLift key to use. |
|
| 604 | - * |
|
| 605 | - * @return string The API URI. |
|
| 606 | - * @since 3.26.0 |
|
| 607 | - * |
|
| 608 | - */ |
|
| 609 | - public function get_accounts_info_by_key( $key ) { |
|
| 610 | - |
|
| 611 | - return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . "accounts/info"; |
|
| 612 | - } |
|
| 613 | - |
|
| 614 | - /** |
|
| 615 | - * Get the `accounts` end point. |
|
| 616 | - * |
|
| 617 | - * @return string The `accounts` end point. |
|
| 618 | - * @since 3.16.0 |
|
| 619 | - * |
|
| 620 | - */ |
|
| 621 | - public function get_accounts() { |
|
| 622 | - |
|
| 623 | - return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'accounts'; |
|
| 624 | - } |
|
| 625 | - |
|
| 626 | - /** |
|
| 627 | - * Get the `link by default` option. |
|
| 628 | - * |
|
| 629 | - * @return bool True if entities must be linked by default otherwise false. |
|
| 630 | - * @since 3.13.0 |
|
| 631 | - * |
|
| 632 | - */ |
|
| 633 | - public function is_link_by_default() { |
|
| 634 | - |
|
| 635 | - return 'yes' === $this->get( 'wl_general_settings', self::LINK_BY_DEFAULT, 'yes' ); |
|
| 636 | - } |
|
| 637 | - |
|
| 638 | - /** |
|
| 639 | - * Set the `link by default` option. |
|
| 640 | - * |
|
| 641 | - * @param bool $value True to enabling linking by default, otherwise false. |
|
| 642 | - * |
|
| 643 | - * @since 3.13.0 |
|
| 644 | - * |
|
| 645 | - */ |
|
| 646 | - public function set_link_by_default( $value ) { |
|
| 647 | - |
|
| 648 | - $this->set( 'wl_general_settings', self::LINK_BY_DEFAULT, true === $value ? 'yes' : 'no' ); |
|
| 649 | - } |
|
| 650 | - |
|
| 651 | - /** |
|
| 652 | - * Get the 'analytics-enable' option. |
|
| 653 | - * |
|
| 654 | - * @return string 'no' or 'yes' representing bool. |
|
| 655 | - * @since 3.21.0 |
|
| 656 | - * |
|
| 657 | - */ |
|
| 658 | - public function is_analytics_enable() { |
|
| 659 | - return 'yes' === $this->get( 'wl_analytics_settings', self::ANALYTICS_ENABLE, 'no' ); |
|
| 660 | - } |
|
| 661 | - |
|
| 662 | - /** |
|
| 663 | - * Set the `analytics-enable` option. |
|
| 664 | - * |
|
| 665 | - * @param bool $value True to enabling analytics, otherwise false. |
|
| 666 | - * |
|
| 667 | - * @since 3.21.0 |
|
| 668 | - * |
|
| 669 | - */ |
|
| 670 | - public function set_is_analytics_enable( $value ) { |
|
| 671 | - |
|
| 672 | - $this->set( 'wl_general_settings', self::ANALYTICS_ENABLE, true === $value ? 'yes' : 'no' ); |
|
| 673 | - } |
|
| 674 | - |
|
| 675 | - /** |
|
| 676 | - * Get the 'analytics-entity-uri-dimention' option. |
|
| 677 | - * |
|
| 678 | - * @return int |
|
| 679 | - * @since 3.21.0 |
|
| 680 | - * |
|
| 681 | - */ |
|
| 682 | - public function get_analytics_entity_uri_dimension() { |
|
| 683 | - return (int) $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_URI_DIMENSION, 1 ); |
|
| 684 | - } |
|
| 685 | - |
|
| 686 | - /** |
|
| 687 | - * Get the 'analytics-entity-type-dimension' option. |
|
| 688 | - * |
|
| 689 | - * @return int |
|
| 690 | - * @since 3.21.0 |
|
| 691 | - * |
|
| 692 | - */ |
|
| 693 | - public function get_analytics_entity_type_dimension() { |
|
| 694 | - return $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_TYPE_DIMENSION, 2 ); |
|
| 695 | - } |
|
| 696 | - |
|
| 697 | - /** |
|
| 698 | - * Get the URL to perform autocomplete request. |
|
| 699 | - * |
|
| 700 | - * @return string The URL to call to perform the autocomplete request. |
|
| 701 | - * @since 3.15.0 |
|
| 702 | - * |
|
| 703 | - */ |
|
| 704 | - public function get_autocomplete_url() { |
|
| 705 | - |
|
| 706 | - return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'autocomplete'; |
|
| 707 | - |
|
| 708 | - } |
|
| 709 | - |
|
| 710 | - /** |
|
| 711 | - * Get the URL to perform feedback deactivation request. |
|
| 712 | - * |
|
| 713 | - * @return string The URL to call to perform the feedback deactivation request. |
|
| 714 | - * @since 3.19.0 |
|
| 715 | - * |
|
| 716 | - */ |
|
| 717 | - public function get_deactivation_feedback_url() { |
|
| 718 | - |
|
| 719 | - return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'feedbacks'; |
|
| 720 | - |
|
| 721 | - } |
|
| 722 | - |
|
| 723 | - /** |
|
| 724 | - * Get the base API URL. |
|
| 725 | - * |
|
| 726 | - * @return string The base API URL. |
|
| 727 | - * @since 3.20.0 |
|
| 728 | - * |
|
| 729 | - */ |
|
| 730 | - public function get_api_url() { |
|
| 731 | - |
|
| 732 | - return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE; |
|
| 733 | - } |
|
| 541 | + $json = json_decode( $response['body'] ); |
|
| 542 | + $dataset_uri = $json->datasetURI; |
|
| 543 | + $package_type = isset( $json->packageType ) ? $json->packageType : null; |
|
| 544 | + |
|
| 545 | + $this->log->info( "Updating [ dataset uri :: $dataset_uri ][ package type :: $package_type ]..." ); |
|
| 546 | + |
|
| 547 | + $this->set_dataset_uri( $dataset_uri ); |
|
| 548 | + $this->set_package_type( $package_type ); |
|
| 549 | + |
|
| 550 | + } |
|
| 551 | + |
|
| 552 | + /** |
|
| 553 | + * Handle the edge case where a user submits the same key again |
|
| 554 | + * when he does not have the dataset uri to regain it. |
|
| 555 | + * |
|
| 556 | + * This can not be handled in the normal option update hook because |
|
| 557 | + * it is not being triggered when the save value equals to the one already |
|
| 558 | + * in the DB. |
|
| 559 | + * |
|
| 560 | + * @param mixed $value The new, unserialized option value. |
|
| 561 | + * @param mixed $old_value The old option value. |
|
| 562 | + * |
|
| 563 | + * @return mixed The same value in the $value parameter |
|
| 564 | + * @since 3.12.0 |
|
| 565 | + * |
|
| 566 | + */ |
|
| 567 | + function maybe_update_dataset_uri( $value, $old_value ) { |
|
| 568 | + |
|
| 569 | + // Check the old key value and the new one. Here we're only handling the |
|
| 570 | + // case where the key hasn't changed and the dataset URI isn't set. The |
|
| 571 | + // other case, i.e. a new key is inserted, is handled at `update_key`. |
|
| 572 | + $old_key = isset( $old_value['key'] ) ? $old_value['key'] : ''; |
|
| 573 | + $new_key = isset( $value['key'] ) ? $value['key'] : ''; |
|
| 574 | + |
|
| 575 | + $dataset_uri = $this->get_dataset_uri(); |
|
| 576 | + |
|
| 577 | + if ( ! empty( $new_key ) && $new_key === $old_key && empty( $dataset_uri ) ) { |
|
| 578 | + |
|
| 579 | + // make the request to the remote server to try to get the dataset uri. |
|
| 580 | + $this->get_remote_dataset_uri( $new_key ); |
|
| 581 | + } |
|
| 582 | + |
|
| 583 | + return $value; |
|
| 584 | + } |
|
| 585 | + |
|
| 586 | + /** |
|
| 587 | + * Get the API URI to retrieve the dataset URI using the WordLift Key. |
|
| 588 | + * |
|
| 589 | + * @param string $key The WordLift key to use. |
|
| 590 | + * |
|
| 591 | + * @return string The API URI. |
|
| 592 | + * @since 3.11.0 |
|
| 593 | + * |
|
| 594 | + */ |
|
| 595 | + public function get_accounts_by_key_dataset_uri( $key ) { |
|
| 596 | + |
|
| 597 | + return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . "accounts/key=$key/dataset_uri"; |
|
| 598 | + } |
|
| 599 | + |
|
| 600 | + /** |
|
| 601 | + * Get the API URI to retrieve the account info using the WordLift Key. |
|
| 602 | + * |
|
| 603 | + * @param string $key The WordLift key to use. |
|
| 604 | + * |
|
| 605 | + * @return string The API URI. |
|
| 606 | + * @since 3.26.0 |
|
| 607 | + * |
|
| 608 | + */ |
|
| 609 | + public function get_accounts_info_by_key( $key ) { |
|
| 610 | + |
|
| 611 | + return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . "accounts/info"; |
|
| 612 | + } |
|
| 613 | + |
|
| 614 | + /** |
|
| 615 | + * Get the `accounts` end point. |
|
| 616 | + * |
|
| 617 | + * @return string The `accounts` end point. |
|
| 618 | + * @since 3.16.0 |
|
| 619 | + * |
|
| 620 | + */ |
|
| 621 | + public function get_accounts() { |
|
| 622 | + |
|
| 623 | + return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'accounts'; |
|
| 624 | + } |
|
| 625 | + |
|
| 626 | + /** |
|
| 627 | + * Get the `link by default` option. |
|
| 628 | + * |
|
| 629 | + * @return bool True if entities must be linked by default otherwise false. |
|
| 630 | + * @since 3.13.0 |
|
| 631 | + * |
|
| 632 | + */ |
|
| 633 | + public function is_link_by_default() { |
|
| 634 | + |
|
| 635 | + return 'yes' === $this->get( 'wl_general_settings', self::LINK_BY_DEFAULT, 'yes' ); |
|
| 636 | + } |
|
| 637 | + |
|
| 638 | + /** |
|
| 639 | + * Set the `link by default` option. |
|
| 640 | + * |
|
| 641 | + * @param bool $value True to enabling linking by default, otherwise false. |
|
| 642 | + * |
|
| 643 | + * @since 3.13.0 |
|
| 644 | + * |
|
| 645 | + */ |
|
| 646 | + public function set_link_by_default( $value ) { |
|
| 647 | + |
|
| 648 | + $this->set( 'wl_general_settings', self::LINK_BY_DEFAULT, true === $value ? 'yes' : 'no' ); |
|
| 649 | + } |
|
| 650 | + |
|
| 651 | + /** |
|
| 652 | + * Get the 'analytics-enable' option. |
|
| 653 | + * |
|
| 654 | + * @return string 'no' or 'yes' representing bool. |
|
| 655 | + * @since 3.21.0 |
|
| 656 | + * |
|
| 657 | + */ |
|
| 658 | + public function is_analytics_enable() { |
|
| 659 | + return 'yes' === $this->get( 'wl_analytics_settings', self::ANALYTICS_ENABLE, 'no' ); |
|
| 660 | + } |
|
| 661 | + |
|
| 662 | + /** |
|
| 663 | + * Set the `analytics-enable` option. |
|
| 664 | + * |
|
| 665 | + * @param bool $value True to enabling analytics, otherwise false. |
|
| 666 | + * |
|
| 667 | + * @since 3.21.0 |
|
| 668 | + * |
|
| 669 | + */ |
|
| 670 | + public function set_is_analytics_enable( $value ) { |
|
| 671 | + |
|
| 672 | + $this->set( 'wl_general_settings', self::ANALYTICS_ENABLE, true === $value ? 'yes' : 'no' ); |
|
| 673 | + } |
|
| 674 | + |
|
| 675 | + /** |
|
| 676 | + * Get the 'analytics-entity-uri-dimention' option. |
|
| 677 | + * |
|
| 678 | + * @return int |
|
| 679 | + * @since 3.21.0 |
|
| 680 | + * |
|
| 681 | + */ |
|
| 682 | + public function get_analytics_entity_uri_dimension() { |
|
| 683 | + return (int) $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_URI_DIMENSION, 1 ); |
|
| 684 | + } |
|
| 685 | + |
|
| 686 | + /** |
|
| 687 | + * Get the 'analytics-entity-type-dimension' option. |
|
| 688 | + * |
|
| 689 | + * @return int |
|
| 690 | + * @since 3.21.0 |
|
| 691 | + * |
|
| 692 | + */ |
|
| 693 | + public function get_analytics_entity_type_dimension() { |
|
| 694 | + return $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_TYPE_DIMENSION, 2 ); |
|
| 695 | + } |
|
| 696 | + |
|
| 697 | + /** |
|
| 698 | + * Get the URL to perform autocomplete request. |
|
| 699 | + * |
|
| 700 | + * @return string The URL to call to perform the autocomplete request. |
|
| 701 | + * @since 3.15.0 |
|
| 702 | + * |
|
| 703 | + */ |
|
| 704 | + public function get_autocomplete_url() { |
|
| 705 | + |
|
| 706 | + return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'autocomplete'; |
|
| 707 | + |
|
| 708 | + } |
|
| 709 | + |
|
| 710 | + /** |
|
| 711 | + * Get the URL to perform feedback deactivation request. |
|
| 712 | + * |
|
| 713 | + * @return string The URL to call to perform the feedback deactivation request. |
|
| 714 | + * @since 3.19.0 |
|
| 715 | + * |
|
| 716 | + */ |
|
| 717 | + public function get_deactivation_feedback_url() { |
|
| 718 | + |
|
| 719 | + return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'feedbacks'; |
|
| 720 | + |
|
| 721 | + } |
|
| 722 | + |
|
| 723 | + /** |
|
| 724 | + * Get the base API URL. |
|
| 725 | + * |
|
| 726 | + * @return string The base API URL. |
|
| 727 | + * @since 3.20.0 |
|
| 728 | + * |
|
| 729 | + */ |
|
| 730 | + public function get_api_url() { |
|
| 731 | + |
|
| 732 | + return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE; |
|
| 733 | + } |
|
| 734 | 734 | |
| 735 | 735 | } |
@@ -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,37 +498,37 @@ discard block |
||
| 498 | 498 | * @since 3.20.0 |
| 499 | 499 | * |
| 500 | 500 | */ |
| 501 | - $site_url = apply_filters( 'wl_production_site_url', site_url() ); |
|
| 501 | + $site_url = apply_filters('wl_production_site_url', site_url()); |
|
| 502 | 502 | |
| 503 | 503 | // Build the URL. |
| 504 | 504 | $url = $this->get_accounts() |
| 505 | - . '?key=' . rawurlencode( $key ) |
|
| 506 | - . '&url=' . rawurlencode( $site_url ) |
|
| 507 | - . '&country=' . $this->get_country_code() |
|
| 508 | - . '&language=' . $this->get_language_code(); |
|
| 505 | + . '?key='.rawurlencode($key) |
|
| 506 | + . '&url='.rawurlencode($site_url) |
|
| 507 | + . '&country='.$this->get_country_code() |
|
| 508 | + . '&language='.$this->get_language_code(); |
|
| 509 | 509 | |
| 510 | - $args = wp_parse_args( unserialize( WL_REDLINK_API_HTTP_OPTIONS ), array( |
|
| 510 | + $args = wp_parse_args(unserialize(WL_REDLINK_API_HTTP_OPTIONS), array( |
|
| 511 | 511 | 'method' => 'PUT', |
| 512 | - ) ); |
|
| 512 | + )); |
|
| 513 | 513 | |
| 514 | - $response = wp_remote_request( $url, $args ); |
|
| 514 | + $response = wp_remote_request($url, $args); |
|
| 515 | 515 | |
| 516 | 516 | // The response is an error. |
| 517 | - if ( is_wp_error( $response ) ) { |
|
| 518 | - $this->log->error( 'An error occurred setting the dataset URI: ' . $response->get_error_message() ); |
|
| 517 | + if (is_wp_error($response)) { |
|
| 518 | + $this->log->error('An error occurred setting the dataset URI: '.$response->get_error_message()); |
|
| 519 | 519 | |
| 520 | - $this->set_dataset_uri( '' ); |
|
| 521 | - $this->set_package_type( null ); |
|
| 520 | + $this->set_dataset_uri(''); |
|
| 521 | + $this->set_package_type(null); |
|
| 522 | 522 | |
| 523 | 523 | return; |
| 524 | 524 | } |
| 525 | 525 | |
| 526 | 526 | // The response is not OK. |
| 527 | - if ( 200 !== (int) $response['response']['code'] ) { |
|
| 528 | - $this->log->error( "Unexpected status code when opening URL $url: " . $response['response']['code'] ); |
|
| 527 | + if (200 !== (int) $response['response']['code']) { |
|
| 528 | + $this->log->error("Unexpected status code when opening URL $url: ".$response['response']['code']); |
|
| 529 | 529 | |
| 530 | - $this->set_dataset_uri( '' ); |
|
| 531 | - $this->set_package_type( null ); |
|
| 530 | + $this->set_dataset_uri(''); |
|
| 531 | + $this->set_package_type(null); |
|
| 532 | 532 | |
| 533 | 533 | return; |
| 534 | 534 | } |
@@ -538,14 +538,14 @@ discard block |
||
| 538 | 538 | * |
| 539 | 539 | * @since 3.20.0 |
| 540 | 540 | */ |
| 541 | - $json = json_decode( $response['body'] ); |
|
| 541 | + $json = json_decode($response['body']); |
|
| 542 | 542 | $dataset_uri = $json->datasetURI; |
| 543 | - $package_type = isset( $json->packageType ) ? $json->packageType : null; |
|
| 543 | + $package_type = isset($json->packageType) ? $json->packageType : null; |
|
| 544 | 544 | |
| 545 | - $this->log->info( "Updating [ dataset uri :: $dataset_uri ][ package type :: $package_type ]..." ); |
|
| 545 | + $this->log->info("Updating [ dataset uri :: $dataset_uri ][ package type :: $package_type ]..."); |
|
| 546 | 546 | |
| 547 | - $this->set_dataset_uri( $dataset_uri ); |
|
| 548 | - $this->set_package_type( $package_type ); |
|
| 547 | + $this->set_dataset_uri($dataset_uri); |
|
| 548 | + $this->set_package_type($package_type); |
|
| 549 | 549 | |
| 550 | 550 | } |
| 551 | 551 | |
@@ -564,20 +564,20 @@ discard block |
||
| 564 | 564 | * @since 3.12.0 |
| 565 | 565 | * |
| 566 | 566 | */ |
| 567 | - function maybe_update_dataset_uri( $value, $old_value ) { |
|
| 567 | + function maybe_update_dataset_uri($value, $old_value) { |
|
| 568 | 568 | |
| 569 | 569 | // Check the old key value and the new one. Here we're only handling the |
| 570 | 570 | // case where the key hasn't changed and the dataset URI isn't set. The |
| 571 | 571 | // other case, i.e. a new key is inserted, is handled at `update_key`. |
| 572 | - $old_key = isset( $old_value['key'] ) ? $old_value['key'] : ''; |
|
| 573 | - $new_key = isset( $value['key'] ) ? $value['key'] : ''; |
|
| 572 | + $old_key = isset($old_value['key']) ? $old_value['key'] : ''; |
|
| 573 | + $new_key = isset($value['key']) ? $value['key'] : ''; |
|
| 574 | 574 | |
| 575 | 575 | $dataset_uri = $this->get_dataset_uri(); |
| 576 | 576 | |
| 577 | - if ( ! empty( $new_key ) && $new_key === $old_key && empty( $dataset_uri ) ) { |
|
| 577 | + if ( ! empty($new_key) && $new_key === $old_key && empty($dataset_uri)) { |
|
| 578 | 578 | |
| 579 | 579 | // make the request to the remote server to try to get the dataset uri. |
| 580 | - $this->get_remote_dataset_uri( $new_key ); |
|
| 580 | + $this->get_remote_dataset_uri($new_key); |
|
| 581 | 581 | } |
| 582 | 582 | |
| 583 | 583 | return $value; |
@@ -592,9 +592,9 @@ discard block |
||
| 592 | 592 | * @since 3.11.0 |
| 593 | 593 | * |
| 594 | 594 | */ |
| 595 | - public function get_accounts_by_key_dataset_uri( $key ) { |
|
| 595 | + public function get_accounts_by_key_dataset_uri($key) { |
|
| 596 | 596 | |
| 597 | - return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . "accounts/key=$key/dataset_uri"; |
|
| 597 | + return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE."accounts/key=$key/dataset_uri"; |
|
| 598 | 598 | } |
| 599 | 599 | |
| 600 | 600 | /** |
@@ -606,9 +606,9 @@ discard block |
||
| 606 | 606 | * @since 3.26.0 |
| 607 | 607 | * |
| 608 | 608 | */ |
| 609 | - public function get_accounts_info_by_key( $key ) { |
|
| 609 | + public function get_accounts_info_by_key($key) { |
|
| 610 | 610 | |
| 611 | - return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . "accounts/info"; |
|
| 611 | + return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE."accounts/info"; |
|
| 612 | 612 | } |
| 613 | 613 | |
| 614 | 614 | /** |
@@ -620,7 +620,7 @@ discard block |
||
| 620 | 620 | */ |
| 621 | 621 | public function get_accounts() { |
| 622 | 622 | |
| 623 | - return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'accounts'; |
|
| 623 | + return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE.'accounts'; |
|
| 624 | 624 | } |
| 625 | 625 | |
| 626 | 626 | /** |
@@ -632,7 +632,7 @@ discard block |
||
| 632 | 632 | */ |
| 633 | 633 | public function is_link_by_default() { |
| 634 | 634 | |
| 635 | - return 'yes' === $this->get( 'wl_general_settings', self::LINK_BY_DEFAULT, 'yes' ); |
|
| 635 | + return 'yes' === $this->get('wl_general_settings', self::LINK_BY_DEFAULT, 'yes'); |
|
| 636 | 636 | } |
| 637 | 637 | |
| 638 | 638 | /** |
@@ -643,9 +643,9 @@ discard block |
||
| 643 | 643 | * @since 3.13.0 |
| 644 | 644 | * |
| 645 | 645 | */ |
| 646 | - public function set_link_by_default( $value ) { |
|
| 646 | + public function set_link_by_default($value) { |
|
| 647 | 647 | |
| 648 | - $this->set( 'wl_general_settings', self::LINK_BY_DEFAULT, true === $value ? 'yes' : 'no' ); |
|
| 648 | + $this->set('wl_general_settings', self::LINK_BY_DEFAULT, true === $value ? 'yes' : 'no'); |
|
| 649 | 649 | } |
| 650 | 650 | |
| 651 | 651 | /** |
@@ -656,7 +656,7 @@ discard block |
||
| 656 | 656 | * |
| 657 | 657 | */ |
| 658 | 658 | public function is_analytics_enable() { |
| 659 | - return 'yes' === $this->get( 'wl_analytics_settings', self::ANALYTICS_ENABLE, 'no' ); |
|
| 659 | + return 'yes' === $this->get('wl_analytics_settings', self::ANALYTICS_ENABLE, 'no'); |
|
| 660 | 660 | } |
| 661 | 661 | |
| 662 | 662 | /** |
@@ -667,9 +667,9 @@ discard block |
||
| 667 | 667 | * @since 3.21.0 |
| 668 | 668 | * |
| 669 | 669 | */ |
| 670 | - public function set_is_analytics_enable( $value ) { |
|
| 670 | + public function set_is_analytics_enable($value) { |
|
| 671 | 671 | |
| 672 | - $this->set( 'wl_general_settings', self::ANALYTICS_ENABLE, true === $value ? 'yes' : 'no' ); |
|
| 672 | + $this->set('wl_general_settings', self::ANALYTICS_ENABLE, true === $value ? 'yes' : 'no'); |
|
| 673 | 673 | } |
| 674 | 674 | |
| 675 | 675 | /** |
@@ -680,7 +680,7 @@ discard block |
||
| 680 | 680 | * |
| 681 | 681 | */ |
| 682 | 682 | public function get_analytics_entity_uri_dimension() { |
| 683 | - return (int) $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_URI_DIMENSION, 1 ); |
|
| 683 | + return (int) $this->get('wl_analytics_settings', self::ANALYTICS_ENTITY_URI_DIMENSION, 1); |
|
| 684 | 684 | } |
| 685 | 685 | |
| 686 | 686 | /** |
@@ -691,7 +691,7 @@ discard block |
||
| 691 | 691 | * |
| 692 | 692 | */ |
| 693 | 693 | public function get_analytics_entity_type_dimension() { |
| 694 | - return $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_TYPE_DIMENSION, 2 ); |
|
| 694 | + return $this->get('wl_analytics_settings', self::ANALYTICS_ENTITY_TYPE_DIMENSION, 2); |
|
| 695 | 695 | } |
| 696 | 696 | |
| 697 | 697 | /** |
@@ -703,7 +703,7 @@ discard block |
||
| 703 | 703 | */ |
| 704 | 704 | public function get_autocomplete_url() { |
| 705 | 705 | |
| 706 | - return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'autocomplete'; |
|
| 706 | + return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE.'autocomplete'; |
|
| 707 | 707 | |
| 708 | 708 | } |
| 709 | 709 | |
@@ -716,7 +716,7 @@ discard block |
||
| 716 | 716 | */ |
| 717 | 717 | public function get_deactivation_feedback_url() { |
| 718 | 718 | |
| 719 | - return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'feedbacks'; |
|
| 719 | + return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE.'feedbacks'; |
|
| 720 | 720 | |
| 721 | 721 | } |
| 722 | 722 | |
@@ -18,320 +18,320 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | abstract class Wordlift_Abstract_Post_To_Jsonld_Converter implements Wordlift_Post_Converter { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * The JSON-LD context. |
|
| 23 | - * |
|
| 24 | - * @since 3.10.0 |
|
| 25 | - */ |
|
| 26 | - const CONTEXT = 'http://schema.org'; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * A {@link Wordlift_Entity_Type_Service} instance. |
|
| 30 | - * |
|
| 31 | - * @since 3.10.0 |
|
| 32 | - * @access protected |
|
| 33 | - * @var \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
| 34 | - */ |
|
| 35 | - protected $entity_type_service; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * A {@link Wordlift_Entity_Service} instance. |
|
| 39 | - * |
|
| 40 | - * @since 3.10.0 |
|
| 41 | - * @access protected |
|
| 42 | - * @var \Wordlift_Entity_Service $entity_type_service A {@link Wordlift_Entity_Service} instance. |
|
| 43 | - */ |
|
| 44 | - protected $entity_service; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * A {@link Wordlift_User_Service} instance. |
|
| 48 | - * |
|
| 49 | - * @since 3.10.0 |
|
| 50 | - * @access private |
|
| 51 | - * @var \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance. |
|
| 52 | - */ |
|
| 53 | - protected $user_service; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * A {@link Wordlift_Attachment_Service} instance. |
|
| 57 | - * |
|
| 58 | - * @since 3.10.0 |
|
| 59 | - * @access private |
|
| 60 | - * @var \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance. |
|
| 61 | - */ |
|
| 62 | - protected $attachment_service; |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Wordlift_Post_To_Jsonld_Converter constructor. |
|
| 66 | - * |
|
| 67 | - * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
| 68 | - * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 69 | - * @param \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance. |
|
| 70 | - * @param \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance. |
|
| 71 | - * |
|
| 72 | - * @since 3.10.0 |
|
| 73 | - * |
|
| 74 | - */ |
|
| 75 | - public function __construct( $entity_type_service, $entity_service, $user_service, $attachment_service ) { |
|
| 76 | - |
|
| 77 | - $this->entity_type_service = $entity_type_service; |
|
| 78 | - $this->entity_service = $entity_service; |
|
| 79 | - $this->user_service = $user_service; |
|
| 80 | - $this->attachment_service = $attachment_service; |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * Convert the provided {@link WP_Post} to a JSON-LD array. Any entity reference |
|
| 85 | - * found while processing the post is set in the $references array. |
|
| 86 | - * |
|
| 87 | - * @param int $post_id The post id. |
|
| 88 | - * @param array $references An array of entity references. |
|
| 89 | - * |
|
| 90 | - * @return array A JSON-LD array. |
|
| 91 | - * @since 3.10.0 |
|
| 92 | - * |
|
| 93 | - */ |
|
| 94 | - public function convert( $post_id, &$references = array() ) { |
|
| 95 | - |
|
| 96 | - // Get the post instance. |
|
| 97 | - $post = get_post( $post_id ); |
|
| 98 | - if ( null === $post ) { |
|
| 99 | - // Post not found. |
|
| 100 | - return null; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - // Get the post URI @id. |
|
| 104 | - $id = $this->entity_service->get_uri( $post->ID ); |
|
| 105 | - if ( is_null( $id ) ) { |
|
| 106 | - $id = 'get_uri returned null, dataset is ' . wl_configuration_get_redlink_dataset_uri(); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - /* |
|
| 21 | + /** |
|
| 22 | + * The JSON-LD context. |
|
| 23 | + * |
|
| 24 | + * @since 3.10.0 |
|
| 25 | + */ |
|
| 26 | + const CONTEXT = 'http://schema.org'; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * A {@link Wordlift_Entity_Type_Service} instance. |
|
| 30 | + * |
|
| 31 | + * @since 3.10.0 |
|
| 32 | + * @access protected |
|
| 33 | + * @var \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
| 34 | + */ |
|
| 35 | + protected $entity_type_service; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * A {@link Wordlift_Entity_Service} instance. |
|
| 39 | + * |
|
| 40 | + * @since 3.10.0 |
|
| 41 | + * @access protected |
|
| 42 | + * @var \Wordlift_Entity_Service $entity_type_service A {@link Wordlift_Entity_Service} instance. |
|
| 43 | + */ |
|
| 44 | + protected $entity_service; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * A {@link Wordlift_User_Service} instance. |
|
| 48 | + * |
|
| 49 | + * @since 3.10.0 |
|
| 50 | + * @access private |
|
| 51 | + * @var \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance. |
|
| 52 | + */ |
|
| 53 | + protected $user_service; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * A {@link Wordlift_Attachment_Service} instance. |
|
| 57 | + * |
|
| 58 | + * @since 3.10.0 |
|
| 59 | + * @access private |
|
| 60 | + * @var \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance. |
|
| 61 | + */ |
|
| 62 | + protected $attachment_service; |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Wordlift_Post_To_Jsonld_Converter constructor. |
|
| 66 | + * |
|
| 67 | + * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
| 68 | + * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 69 | + * @param \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance. |
|
| 70 | + * @param \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance. |
|
| 71 | + * |
|
| 72 | + * @since 3.10.0 |
|
| 73 | + * |
|
| 74 | + */ |
|
| 75 | + public function __construct( $entity_type_service, $entity_service, $user_service, $attachment_service ) { |
|
| 76 | + |
|
| 77 | + $this->entity_type_service = $entity_type_service; |
|
| 78 | + $this->entity_service = $entity_service; |
|
| 79 | + $this->user_service = $user_service; |
|
| 80 | + $this->attachment_service = $attachment_service; |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * Convert the provided {@link WP_Post} to a JSON-LD array. Any entity reference |
|
| 85 | + * found while processing the post is set in the $references array. |
|
| 86 | + * |
|
| 87 | + * @param int $post_id The post id. |
|
| 88 | + * @param array $references An array of entity references. |
|
| 89 | + * |
|
| 90 | + * @return array A JSON-LD array. |
|
| 91 | + * @since 3.10.0 |
|
| 92 | + * |
|
| 93 | + */ |
|
| 94 | + public function convert( $post_id, &$references = array() ) { |
|
| 95 | + |
|
| 96 | + // Get the post instance. |
|
| 97 | + $post = get_post( $post_id ); |
|
| 98 | + if ( null === $post ) { |
|
| 99 | + // Post not found. |
|
| 100 | + return null; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + // Get the post URI @id. |
|
| 104 | + $id = $this->entity_service->get_uri( $post->ID ); |
|
| 105 | + if ( is_null( $id ) ) { |
|
| 106 | + $id = 'get_uri returned null, dataset is ' . wl_configuration_get_redlink_dataset_uri(); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /* |
|
| 110 | 110 | * The `types` variable holds one or more entity types. The `type` variable isn't used anymore. |
| 111 | 111 | * |
| 112 | 112 | * @since 3.20.0 We support more than one entity type. |
| 113 | 113 | * |
| 114 | 114 | * @see https://github.com/insideout10/wordlift-plugin/issues/835 |
| 115 | 115 | */ |
| 116 | - // // Get the entity @type. We consider `post` BlogPostings. |
|
| 117 | - // $type = $this->entity_type_service->get( $post_id ); |
|
| 118 | - $types = $this->entity_type_service->get_names( $post_id ); |
|
| 119 | - |
|
| 120 | - // Prepare the response. |
|
| 121 | - $jsonld = array( |
|
| 122 | - '@context' => self::CONTEXT, |
|
| 123 | - '@id' => $id, |
|
| 124 | - '@type' => self::make_one( $types ), |
|
| 125 | - 'description' => Wordlift_Post_Excerpt_Helper::get_text_excerpt( $post ), |
|
| 126 | - ); |
|
| 127 | - |
|
| 128 | - // Set the `mainEntityOfPage` property if the post has some contents. |
|
| 129 | - /* |
|
| 116 | + // // Get the entity @type. We consider `post` BlogPostings. |
|
| 117 | + // $type = $this->entity_type_service->get( $post_id ); |
|
| 118 | + $types = $this->entity_type_service->get_names( $post_id ); |
|
| 119 | + |
|
| 120 | + // Prepare the response. |
|
| 121 | + $jsonld = array( |
|
| 122 | + '@context' => self::CONTEXT, |
|
| 123 | + '@id' => $id, |
|
| 124 | + '@type' => self::make_one( $types ), |
|
| 125 | + 'description' => Wordlift_Post_Excerpt_Helper::get_text_excerpt( $post ), |
|
| 126 | + ); |
|
| 127 | + |
|
| 128 | + // Set the `mainEntityOfPage` property if the post has some contents. |
|
| 129 | + /* |
|
| 130 | 130 | * Apply the `wl_post_content` filter, in case 3rd parties want to change the post content, e.g. |
| 131 | 131 | * because the content is written elsewhere. |
| 132 | 132 | * |
| 133 | 133 | * @since 3.20.0 |
| 134 | 134 | */ |
| 135 | - $post_content = apply_filters( 'wl_post_content', $post->post_content, $post ); |
|
| 136 | - if ( ! empty( $post_content ) ) { |
|
| 137 | - // We're setting the `mainEntityOfPage` to signal which one is the |
|
| 138 | - // main entity for the specified URL. It might be however that the |
|
| 139 | - // post/page is actually about another specific entity. How WL deals |
|
| 140 | - // with that hasn't been defined yet (see https://github.com/insideout10/wordlift-plugin/issues/451). |
|
| 141 | - // |
|
| 142 | - // See http://schema.org/mainEntityOfPage |
|
| 143 | - // |
|
| 144 | - // No need to specify `'@type' => 'WebPage'. |
|
| 145 | - // |
|
| 146 | - // See https://github.com/insideout10/wordlift-plugin/issues/451. |
|
| 147 | - $jsonld['mainEntityOfPage'] = get_the_permalink( $post->ID ); |
|
| 148 | - }; |
|
| 149 | - |
|
| 150 | - $this->set_images( $post, $jsonld ); |
|
| 151 | - |
|
| 152 | - // Get the entities referenced by this post and set it to the `references` |
|
| 153 | - // array so that the caller can do further processing, such as printing out |
|
| 154 | - // more of those references. |
|
| 155 | - $references_without_locations = $this->entity_service->get_related_entities( $post->ID ); |
|
| 156 | - |
|
| 157 | - /* |
|
| 135 | + $post_content = apply_filters( 'wl_post_content', $post->post_content, $post ); |
|
| 136 | + if ( ! empty( $post_content ) ) { |
|
| 137 | + // We're setting the `mainEntityOfPage` to signal which one is the |
|
| 138 | + // main entity for the specified URL. It might be however that the |
|
| 139 | + // post/page is actually about another specific entity. How WL deals |
|
| 140 | + // with that hasn't been defined yet (see https://github.com/insideout10/wordlift-plugin/issues/451). |
|
| 141 | + // |
|
| 142 | + // See http://schema.org/mainEntityOfPage |
|
| 143 | + // |
|
| 144 | + // No need to specify `'@type' => 'WebPage'. |
|
| 145 | + // |
|
| 146 | + // See https://github.com/insideout10/wordlift-plugin/issues/451. |
|
| 147 | + $jsonld['mainEntityOfPage'] = get_the_permalink( $post->ID ); |
|
| 148 | + }; |
|
| 149 | + |
|
| 150 | + $this->set_images( $post, $jsonld ); |
|
| 151 | + |
|
| 152 | + // Get the entities referenced by this post and set it to the `references` |
|
| 153 | + // array so that the caller can do further processing, such as printing out |
|
| 154 | + // more of those references. |
|
| 155 | + $references_without_locations = $this->entity_service->get_related_entities( $post->ID ); |
|
| 156 | + |
|
| 157 | + /* |
|
| 158 | 158 | * Add the locations to the references. |
| 159 | 159 | * |
| 160 | 160 | * @since 3.19.5 |
| 161 | 161 | * |
| 162 | 162 | * @see https://github.com/insideout10/wordlift-plugin/issues/858. |
| 163 | 163 | */ |
| 164 | - // A reference to use in closure. |
|
| 165 | - $entity_type_service = $this->entity_type_service; |
|
| 166 | - $locations = array_reduce( $references_without_locations, function ( $carry, $post_id ) use ( $entity_type_service ) { |
|
| 167 | - // @see https://schema.org/location for the schema.org types using the `location` property. |
|
| 168 | - if ( ! $entity_type_service->has_entity_type( $post_id, 'http://schema.org/Action' ) |
|
| 169 | - && ! $entity_type_service->has_entity_type( $post_id, 'http://schema.org/Event' ) |
|
| 170 | - && ! $entity_type_service->has_entity_type( $post_id, 'http://schema.org/Organization' ) ) { |
|
| 171 | - return $carry; |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - return array_merge( $carry, get_post_meta( $post_id, Wordlift_Schema_Service::FIELD_LOCATION ) ); |
|
| 175 | - }, array() ); |
|
| 176 | - |
|
| 177 | - // Merge the references with the referenced locations if any. |
|
| 178 | - $references = array_unique( array_merge( $references_without_locations, $locations ) ); |
|
| 179 | - |
|
| 180 | - return $jsonld; |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - /** |
|
| 184 | - * If the provided value starts with the schema.org context, we remove the schema.org |
|
| 185 | - * part since it is set with the '@context'. |
|
| 186 | - * |
|
| 187 | - * @param string $value The property value. |
|
| 188 | - * |
|
| 189 | - * @return string The property value without the context. |
|
| 190 | - * @since 3.10.0 |
|
| 191 | - * |
|
| 192 | - */ |
|
| 193 | - public function relative_to_context( $value ) { |
|
| 194 | - |
|
| 195 | - return 0 === strpos( $value, self::CONTEXT . '/' ) ? substr( $value, strlen( self::CONTEXT ) + 1 ) : $value; |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - /** |
|
| 199 | - * Set the images, by looking for embedded images, for images loaded via the |
|
| 200 | - * gallery and for the featured image. |
|
| 201 | - * |
|
| 202 | - * Uses the cache service to store the results of this function for a day. |
|
| 203 | - * |
|
| 204 | - * @param WP_Post $post The target {@link WP_Post}. |
|
| 205 | - * @param array $jsonld The JSON-LD array. |
|
| 206 | - * |
|
| 207 | - * @since 3.10.0 |
|
| 208 | - * |
|
| 209 | - */ |
|
| 210 | - protected function set_images( $post, &$jsonld ) { |
|
| 211 | - |
|
| 212 | - // Prepare the attachment ids array. |
|
| 213 | - $ids = array(); |
|
| 214 | - |
|
| 215 | - // Set the thumbnail id as first attachment id, if any. |
|
| 216 | - $thumbnail_id = get_post_thumbnail_id( $post->ID ); |
|
| 217 | - if ( '' !== $thumbnail_id ) { |
|
| 218 | - $ids[] = $thumbnail_id; |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - // For the time being the following is being removed since the query |
|
| 222 | - // initiated by `get_image_embeds` is consuming lots of CPU. |
|
| 223 | - // |
|
| 224 | - // See https://github.com/insideout10/wordlift-plugin/issues/689. |
|
| 225 | - // |
|
| 226 | - // Get the embeds, removing existing ids. |
|
| 227 | - // $embeds = array_diff( $this->attachment_service->get_image_embeds( $post->post_content ), $ids ); |
|
| 228 | - $embeds = array(); |
|
| 229 | - |
|
| 230 | - // Get the gallery, removing existing ids. |
|
| 231 | - $gallery = array_diff( $this->attachment_service->get_gallery( $post ), $ids, $embeds ); |
|
| 232 | - |
|
| 233 | - // Map the attachment ids to images' data structured for schema.org use. |
|
| 234 | - $images_with_sizes = array_filter( |
|
| 235 | - array_reduce( array_merge( $ids, $embeds, $gallery ), |
|
| 236 | - function ( $carry, $item ) { |
|
| 237 | - /* |
|
| 164 | + // A reference to use in closure. |
|
| 165 | + $entity_type_service = $this->entity_type_service; |
|
| 166 | + $locations = array_reduce( $references_without_locations, function ( $carry, $post_id ) use ( $entity_type_service ) { |
|
| 167 | + // @see https://schema.org/location for the schema.org types using the `location` property. |
|
| 168 | + if ( ! $entity_type_service->has_entity_type( $post_id, 'http://schema.org/Action' ) |
|
| 169 | + && ! $entity_type_service->has_entity_type( $post_id, 'http://schema.org/Event' ) |
|
| 170 | + && ! $entity_type_service->has_entity_type( $post_id, 'http://schema.org/Organization' ) ) { |
|
| 171 | + return $carry; |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + return array_merge( $carry, get_post_meta( $post_id, Wordlift_Schema_Service::FIELD_LOCATION ) ); |
|
| 175 | + }, array() ); |
|
| 176 | + |
|
| 177 | + // Merge the references with the referenced locations if any. |
|
| 178 | + $references = array_unique( array_merge( $references_without_locations, $locations ) ); |
|
| 179 | + |
|
| 180 | + return $jsonld; |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + /** |
|
| 184 | + * If the provided value starts with the schema.org context, we remove the schema.org |
|
| 185 | + * part since it is set with the '@context'. |
|
| 186 | + * |
|
| 187 | + * @param string $value The property value. |
|
| 188 | + * |
|
| 189 | + * @return string The property value without the context. |
|
| 190 | + * @since 3.10.0 |
|
| 191 | + * |
|
| 192 | + */ |
|
| 193 | + public function relative_to_context( $value ) { |
|
| 194 | + |
|
| 195 | + return 0 === strpos( $value, self::CONTEXT . '/' ) ? substr( $value, strlen( self::CONTEXT ) + 1 ) : $value; |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + /** |
|
| 199 | + * Set the images, by looking for embedded images, for images loaded via the |
|
| 200 | + * gallery and for the featured image. |
|
| 201 | + * |
|
| 202 | + * Uses the cache service to store the results of this function for a day. |
|
| 203 | + * |
|
| 204 | + * @param WP_Post $post The target {@link WP_Post}. |
|
| 205 | + * @param array $jsonld The JSON-LD array. |
|
| 206 | + * |
|
| 207 | + * @since 3.10.0 |
|
| 208 | + * |
|
| 209 | + */ |
|
| 210 | + protected function set_images( $post, &$jsonld ) { |
|
| 211 | + |
|
| 212 | + // Prepare the attachment ids array. |
|
| 213 | + $ids = array(); |
|
| 214 | + |
|
| 215 | + // Set the thumbnail id as first attachment id, if any. |
|
| 216 | + $thumbnail_id = get_post_thumbnail_id( $post->ID ); |
|
| 217 | + if ( '' !== $thumbnail_id ) { |
|
| 218 | + $ids[] = $thumbnail_id; |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + // For the time being the following is being removed since the query |
|
| 222 | + // initiated by `get_image_embeds` is consuming lots of CPU. |
|
| 223 | + // |
|
| 224 | + // See https://github.com/insideout10/wordlift-plugin/issues/689. |
|
| 225 | + // |
|
| 226 | + // Get the embeds, removing existing ids. |
|
| 227 | + // $embeds = array_diff( $this->attachment_service->get_image_embeds( $post->post_content ), $ids ); |
|
| 228 | + $embeds = array(); |
|
| 229 | + |
|
| 230 | + // Get the gallery, removing existing ids. |
|
| 231 | + $gallery = array_diff( $this->attachment_service->get_gallery( $post ), $ids, $embeds ); |
|
| 232 | + |
|
| 233 | + // Map the attachment ids to images' data structured for schema.org use. |
|
| 234 | + $images_with_sizes = array_filter( |
|
| 235 | + array_reduce( array_merge( $ids, $embeds, $gallery ), |
|
| 236 | + function ( $carry, $item ) { |
|
| 237 | + /* |
|
| 238 | 238 | * @todo: we're not sure that we're getting attachment data here, we |
| 239 | 239 | * should filter `false`s. |
| 240 | 240 | */ |
| 241 | 241 | |
| 242 | - $sources = array_merge( |
|
| 243 | - Wordlift_Image_Service::get_sources( $item ), |
|
| 244 | - array( wp_get_attachment_image_src( $item, 'full' ) ) |
|
| 245 | - ); |
|
| 246 | - |
|
| 247 | - $sources_with_image = array_filter( $sources, function ( $source ) { |
|
| 248 | - return ! empty( $source[0] ); |
|
| 249 | - } ); |
|
| 250 | - |
|
| 251 | - // Get the attachment data. |
|
| 252 | - // $attachment = wp_get_attachment_image_src( $item, 'full' ); |
|
| 253 | - |
|
| 254 | - // var_dump( array( "sources-$item" => Wordlift_Image_Service::get_sources( $item ) ) ); |
|
| 255 | - |
|
| 256 | - // Bail if image is not found. |
|
| 257 | - // In some cases, you can delete the image from the database |
|
| 258 | - // or from uploads dir, but the image id still exists as featured image |
|
| 259 | - // or in [gallery] shortcode. |
|
| 260 | - // if ( empty( $attachment[0] ) ) { |
|
| 261 | - if ( empty( $sources_with_image ) ) { |
|
| 262 | - return $carry; |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - // Merge the arrays. |
|
| 266 | - return array_merge( |
|
| 267 | - $carry, |
|
| 268 | - $sources_with_image |
|
| 269 | - ); |
|
| 270 | - } |
|
| 271 | - // Initial array. |
|
| 272 | - , array() ) |
|
| 273 | - ); |
|
| 274 | - |
|
| 275 | - // Refactor data as per schema.org specifications. |
|
| 276 | - $images = array_map( function ( $attachment ) { |
|
| 277 | - return Wordlift_Abstract_Post_To_Jsonld_Converter::set_image_size( |
|
| 278 | - array( |
|
| 279 | - '@type' => 'ImageObject', |
|
| 280 | - 'url' => $attachment[0], |
|
| 281 | - ), $attachment |
|
| 282 | - ); |
|
| 283 | - }, $images_with_sizes ); |
|
| 284 | - |
|
| 285 | - // Add images if present. |
|
| 286 | - if ( 0 < count( $images ) ) { |
|
| 287 | - $jsonld['image'] = $images; |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - /** |
|
| 293 | - * If the provided array of values contains only one value, then one single |
|
| 294 | - * value is returned, otherwise the original array is returned. |
|
| 295 | - * |
|
| 296 | - * @param array $value An array of values. |
|
| 297 | - * |
|
| 298 | - * @return mixed|array A single value or the original array. |
|
| 299 | - * @since 3.20.0 The function has been moved from {@link Wordlift_Entity_Post_To_Jsonld_Converter} to |
|
| 300 | - * {@link Wordlift_Abstract_Post_To_Jsonld_Converter}. |
|
| 301 | - * @since 3.8.0 |
|
| 302 | - * @access private |
|
| 303 | - * |
|
| 304 | - */ |
|
| 305 | - protected static function make_one( $value ) { |
|
| 306 | - |
|
| 307 | - return 1 === count( $value ) ? $value[0] : $value; |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - /** |
|
| 311 | - * Process the provided array by adding the width / height if the values |
|
| 312 | - * are available and are greater than 0. |
|
| 313 | - * |
|
| 314 | - * @param array $image The `ImageObject` array. |
|
| 315 | - * @param array $attachment The attachment array. |
|
| 316 | - * |
|
| 317 | - * @return array The enriched `ImageObject` array. |
|
| 318 | - * @since 3.14.0 |
|
| 319 | - * |
|
| 320 | - */ |
|
| 321 | - public static function set_image_size( $image, $attachment ) { |
|
| 322 | - |
|
| 323 | - // If you specify a "width" or "height" value you should leave out |
|
| 324 | - // 'px'. For example: "width":"4608px" should be "width":"4608". |
|
| 325 | - // |
|
| 326 | - // See https://github.com/insideout10/wordlift-plugin/issues/451. |
|
| 327 | - if ( isset( $attachment[1] ) && is_numeric( $attachment[1] ) && 0 < $attachment[1] ) { |
|
| 328 | - $image['width'] = $attachment[1]; |
|
| 329 | - } |
|
| 330 | - |
|
| 331 | - if ( isset( $attachment[2] ) && is_numeric( $attachment[2] ) && 0 < $attachment[2] ) { |
|
| 332 | - $image['height'] = $attachment[2]; |
|
| 333 | - } |
|
| 334 | - |
|
| 335 | - return $image; |
|
| 336 | - } |
|
| 242 | + $sources = array_merge( |
|
| 243 | + Wordlift_Image_Service::get_sources( $item ), |
|
| 244 | + array( wp_get_attachment_image_src( $item, 'full' ) ) |
|
| 245 | + ); |
|
| 246 | + |
|
| 247 | + $sources_with_image = array_filter( $sources, function ( $source ) { |
|
| 248 | + return ! empty( $source[0] ); |
|
| 249 | + } ); |
|
| 250 | + |
|
| 251 | + // Get the attachment data. |
|
| 252 | + // $attachment = wp_get_attachment_image_src( $item, 'full' ); |
|
| 253 | + |
|
| 254 | + // var_dump( array( "sources-$item" => Wordlift_Image_Service::get_sources( $item ) ) ); |
|
| 255 | + |
|
| 256 | + // Bail if image is not found. |
|
| 257 | + // In some cases, you can delete the image from the database |
|
| 258 | + // or from uploads dir, but the image id still exists as featured image |
|
| 259 | + // or in [gallery] shortcode. |
|
| 260 | + // if ( empty( $attachment[0] ) ) { |
|
| 261 | + if ( empty( $sources_with_image ) ) { |
|
| 262 | + return $carry; |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + // Merge the arrays. |
|
| 266 | + return array_merge( |
|
| 267 | + $carry, |
|
| 268 | + $sources_with_image |
|
| 269 | + ); |
|
| 270 | + } |
|
| 271 | + // Initial array. |
|
| 272 | + , array() ) |
|
| 273 | + ); |
|
| 274 | + |
|
| 275 | + // Refactor data as per schema.org specifications. |
|
| 276 | + $images = array_map( function ( $attachment ) { |
|
| 277 | + return Wordlift_Abstract_Post_To_Jsonld_Converter::set_image_size( |
|
| 278 | + array( |
|
| 279 | + '@type' => 'ImageObject', |
|
| 280 | + 'url' => $attachment[0], |
|
| 281 | + ), $attachment |
|
| 282 | + ); |
|
| 283 | + }, $images_with_sizes ); |
|
| 284 | + |
|
| 285 | + // Add images if present. |
|
| 286 | + if ( 0 < count( $images ) ) { |
|
| 287 | + $jsonld['image'] = $images; |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + /** |
|
| 293 | + * If the provided array of values contains only one value, then one single |
|
| 294 | + * value is returned, otherwise the original array is returned. |
|
| 295 | + * |
|
| 296 | + * @param array $value An array of values. |
|
| 297 | + * |
|
| 298 | + * @return mixed|array A single value or the original array. |
|
| 299 | + * @since 3.20.0 The function has been moved from {@link Wordlift_Entity_Post_To_Jsonld_Converter} to |
|
| 300 | + * {@link Wordlift_Abstract_Post_To_Jsonld_Converter}. |
|
| 301 | + * @since 3.8.0 |
|
| 302 | + * @access private |
|
| 303 | + * |
|
| 304 | + */ |
|
| 305 | + protected static function make_one( $value ) { |
|
| 306 | + |
|
| 307 | + return 1 === count( $value ) ? $value[0] : $value; |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + /** |
|
| 311 | + * Process the provided array by adding the width / height if the values |
|
| 312 | + * are available and are greater than 0. |
|
| 313 | + * |
|
| 314 | + * @param array $image The `ImageObject` array. |
|
| 315 | + * @param array $attachment The attachment array. |
|
| 316 | + * |
|
| 317 | + * @return array The enriched `ImageObject` array. |
|
| 318 | + * @since 3.14.0 |
|
| 319 | + * |
|
| 320 | + */ |
|
| 321 | + public static function set_image_size( $image, $attachment ) { |
|
| 322 | + |
|
| 323 | + // If you specify a "width" or "height" value you should leave out |
|
| 324 | + // 'px'. For example: "width":"4608px" should be "width":"4608". |
|
| 325 | + // |
|
| 326 | + // See https://github.com/insideout10/wordlift-plugin/issues/451. |
|
| 327 | + if ( isset( $attachment[1] ) && is_numeric( $attachment[1] ) && 0 < $attachment[1] ) { |
|
| 328 | + $image['width'] = $attachment[1]; |
|
| 329 | + } |
|
| 330 | + |
|
| 331 | + if ( isset( $attachment[2] ) && is_numeric( $attachment[2] ) && 0 < $attachment[2] ) { |
|
| 332 | + $image['height'] = $attachment[2]; |
|
| 333 | + } |
|
| 334 | + |
|
| 335 | + return $image; |
|
| 336 | + } |
|
| 337 | 337 | } |
@@ -72,7 +72,7 @@ discard block |
||
| 72 | 72 | * @since 3.10.0 |
| 73 | 73 | * |
| 74 | 74 | */ |
| 75 | - public function __construct( $entity_type_service, $entity_service, $user_service, $attachment_service ) { |
|
| 75 | + public function __construct($entity_type_service, $entity_service, $user_service, $attachment_service) { |
|
| 76 | 76 | |
| 77 | 77 | $this->entity_type_service = $entity_type_service; |
| 78 | 78 | $this->entity_service = $entity_service; |
@@ -91,19 +91,19 @@ discard block |
||
| 91 | 91 | * @since 3.10.0 |
| 92 | 92 | * |
| 93 | 93 | */ |
| 94 | - public function convert( $post_id, &$references = array() ) { |
|
| 94 | + public function convert($post_id, &$references = array()) { |
|
| 95 | 95 | |
| 96 | 96 | // Get the post instance. |
| 97 | - $post = get_post( $post_id ); |
|
| 98 | - if ( null === $post ) { |
|
| 97 | + $post = get_post($post_id); |
|
| 98 | + if (null === $post) { |
|
| 99 | 99 | // Post not found. |
| 100 | 100 | return null; |
| 101 | 101 | } |
| 102 | 102 | |
| 103 | 103 | // Get the post URI @id. |
| 104 | - $id = $this->entity_service->get_uri( $post->ID ); |
|
| 105 | - if ( is_null( $id ) ) { |
|
| 106 | - $id = 'get_uri returned null, dataset is ' . wl_configuration_get_redlink_dataset_uri(); |
|
| 104 | + $id = $this->entity_service->get_uri($post->ID); |
|
| 105 | + if (is_null($id)) { |
|
| 106 | + $id = 'get_uri returned null, dataset is '.wl_configuration_get_redlink_dataset_uri(); |
|
| 107 | 107 | } |
| 108 | 108 | |
| 109 | 109 | /* |
@@ -115,14 +115,14 @@ discard block |
||
| 115 | 115 | */ |
| 116 | 116 | // // Get the entity @type. We consider `post` BlogPostings. |
| 117 | 117 | // $type = $this->entity_type_service->get( $post_id ); |
| 118 | - $types = $this->entity_type_service->get_names( $post_id ); |
|
| 118 | + $types = $this->entity_type_service->get_names($post_id); |
|
| 119 | 119 | |
| 120 | 120 | // Prepare the response. |
| 121 | 121 | $jsonld = array( |
| 122 | 122 | '@context' => self::CONTEXT, |
| 123 | 123 | '@id' => $id, |
| 124 | - '@type' => self::make_one( $types ), |
|
| 125 | - 'description' => Wordlift_Post_Excerpt_Helper::get_text_excerpt( $post ), |
|
| 124 | + '@type' => self::make_one($types), |
|
| 125 | + 'description' => Wordlift_Post_Excerpt_Helper::get_text_excerpt($post), |
|
| 126 | 126 | ); |
| 127 | 127 | |
| 128 | 128 | // Set the `mainEntityOfPage` property if the post has some contents. |
@@ -132,8 +132,8 @@ discard block |
||
| 132 | 132 | * |
| 133 | 133 | * @since 3.20.0 |
| 134 | 134 | */ |
| 135 | - $post_content = apply_filters( 'wl_post_content', $post->post_content, $post ); |
|
| 136 | - if ( ! empty( $post_content ) ) { |
|
| 135 | + $post_content = apply_filters('wl_post_content', $post->post_content, $post); |
|
| 136 | + if ( ! empty($post_content)) { |
|
| 137 | 137 | // We're setting the `mainEntityOfPage` to signal which one is the |
| 138 | 138 | // main entity for the specified URL. It might be however that the |
| 139 | 139 | // post/page is actually about another specific entity. How WL deals |
@@ -144,15 +144,15 @@ discard block |
||
| 144 | 144 | // No need to specify `'@type' => 'WebPage'. |
| 145 | 145 | // |
| 146 | 146 | // See https://github.com/insideout10/wordlift-plugin/issues/451. |
| 147 | - $jsonld['mainEntityOfPage'] = get_the_permalink( $post->ID ); |
|
| 147 | + $jsonld['mainEntityOfPage'] = get_the_permalink($post->ID); |
|
| 148 | 148 | }; |
| 149 | 149 | |
| 150 | - $this->set_images( $post, $jsonld ); |
|
| 150 | + $this->set_images($post, $jsonld); |
|
| 151 | 151 | |
| 152 | 152 | // Get the entities referenced by this post and set it to the `references` |
| 153 | 153 | // array so that the caller can do further processing, such as printing out |
| 154 | 154 | // more of those references. |
| 155 | - $references_without_locations = $this->entity_service->get_related_entities( $post->ID ); |
|
| 155 | + $references_without_locations = $this->entity_service->get_related_entities($post->ID); |
|
| 156 | 156 | |
| 157 | 157 | /* |
| 158 | 158 | * Add the locations to the references. |
@@ -163,19 +163,19 @@ discard block |
||
| 163 | 163 | */ |
| 164 | 164 | // A reference to use in closure. |
| 165 | 165 | $entity_type_service = $this->entity_type_service; |
| 166 | - $locations = array_reduce( $references_without_locations, function ( $carry, $post_id ) use ( $entity_type_service ) { |
|
| 166 | + $locations = array_reduce($references_without_locations, function($carry, $post_id) use ($entity_type_service) { |
|
| 167 | 167 | // @see https://schema.org/location for the schema.org types using the `location` property. |
| 168 | - if ( ! $entity_type_service->has_entity_type( $post_id, 'http://schema.org/Action' ) |
|
| 169 | - && ! $entity_type_service->has_entity_type( $post_id, 'http://schema.org/Event' ) |
|
| 170 | - && ! $entity_type_service->has_entity_type( $post_id, 'http://schema.org/Organization' ) ) { |
|
| 168 | + if ( ! $entity_type_service->has_entity_type($post_id, 'http://schema.org/Action') |
|
| 169 | + && ! $entity_type_service->has_entity_type($post_id, 'http://schema.org/Event') |
|
| 170 | + && ! $entity_type_service->has_entity_type($post_id, 'http://schema.org/Organization')) { |
|
| 171 | 171 | return $carry; |
| 172 | 172 | } |
| 173 | 173 | |
| 174 | - return array_merge( $carry, get_post_meta( $post_id, Wordlift_Schema_Service::FIELD_LOCATION ) ); |
|
| 175 | - }, array() ); |
|
| 174 | + return array_merge($carry, get_post_meta($post_id, Wordlift_Schema_Service::FIELD_LOCATION)); |
|
| 175 | + }, array()); |
|
| 176 | 176 | |
| 177 | 177 | // Merge the references with the referenced locations if any. |
| 178 | - $references = array_unique( array_merge( $references_without_locations, $locations ) ); |
|
| 178 | + $references = array_unique(array_merge($references_without_locations, $locations)); |
|
| 179 | 179 | |
| 180 | 180 | return $jsonld; |
| 181 | 181 | } |
@@ -190,9 +190,9 @@ discard block |
||
| 190 | 190 | * @since 3.10.0 |
| 191 | 191 | * |
| 192 | 192 | */ |
| 193 | - public function relative_to_context( $value ) { |
|
| 193 | + public function relative_to_context($value) { |
|
| 194 | 194 | |
| 195 | - return 0 === strpos( $value, self::CONTEXT . '/' ) ? substr( $value, strlen( self::CONTEXT ) + 1 ) : $value; |
|
| 195 | + return 0 === strpos($value, self::CONTEXT.'/') ? substr($value, strlen(self::CONTEXT) + 1) : $value; |
|
| 196 | 196 | } |
| 197 | 197 | |
| 198 | 198 | /** |
@@ -207,14 +207,14 @@ discard block |
||
| 207 | 207 | * @since 3.10.0 |
| 208 | 208 | * |
| 209 | 209 | */ |
| 210 | - protected function set_images( $post, &$jsonld ) { |
|
| 210 | + protected function set_images($post, &$jsonld) { |
|
| 211 | 211 | |
| 212 | 212 | // Prepare the attachment ids array. |
| 213 | 213 | $ids = array(); |
| 214 | 214 | |
| 215 | 215 | // Set the thumbnail id as first attachment id, if any. |
| 216 | - $thumbnail_id = get_post_thumbnail_id( $post->ID ); |
|
| 217 | - if ( '' !== $thumbnail_id ) { |
|
| 216 | + $thumbnail_id = get_post_thumbnail_id($post->ID); |
|
| 217 | + if ('' !== $thumbnail_id) { |
|
| 218 | 218 | $ids[] = $thumbnail_id; |
| 219 | 219 | } |
| 220 | 220 | |
@@ -228,24 +228,24 @@ discard block |
||
| 228 | 228 | $embeds = array(); |
| 229 | 229 | |
| 230 | 230 | // Get the gallery, removing existing ids. |
| 231 | - $gallery = array_diff( $this->attachment_service->get_gallery( $post ), $ids, $embeds ); |
|
| 231 | + $gallery = array_diff($this->attachment_service->get_gallery($post), $ids, $embeds); |
|
| 232 | 232 | |
| 233 | 233 | // Map the attachment ids to images' data structured for schema.org use. |
| 234 | 234 | $images_with_sizes = array_filter( |
| 235 | - array_reduce( array_merge( $ids, $embeds, $gallery ), |
|
| 236 | - function ( $carry, $item ) { |
|
| 235 | + array_reduce(array_merge($ids, $embeds, $gallery), |
|
| 236 | + function($carry, $item) { |
|
| 237 | 237 | /* |
| 238 | 238 | * @todo: we're not sure that we're getting attachment data here, we |
| 239 | 239 | * should filter `false`s. |
| 240 | 240 | */ |
| 241 | 241 | |
| 242 | 242 | $sources = array_merge( |
| 243 | - Wordlift_Image_Service::get_sources( $item ), |
|
| 244 | - array( wp_get_attachment_image_src( $item, 'full' ) ) |
|
| 243 | + Wordlift_Image_Service::get_sources($item), |
|
| 244 | + array(wp_get_attachment_image_src($item, 'full')) |
|
| 245 | 245 | ); |
| 246 | 246 | |
| 247 | - $sources_with_image = array_filter( $sources, function ( $source ) { |
|
| 248 | - return ! empty( $source[0] ); |
|
| 247 | + $sources_with_image = array_filter($sources, function($source) { |
|
| 248 | + return ! empty($source[0]); |
|
| 249 | 249 | } ); |
| 250 | 250 | |
| 251 | 251 | // Get the attachment data. |
@@ -258,7 +258,7 @@ discard block |
||
| 258 | 258 | // or from uploads dir, but the image id still exists as featured image |
| 259 | 259 | // or in [gallery] shortcode. |
| 260 | 260 | // if ( empty( $attachment[0] ) ) { |
| 261 | - if ( empty( $sources_with_image ) ) { |
|
| 261 | + if (empty($sources_with_image)) { |
|
| 262 | 262 | return $carry; |
| 263 | 263 | } |
| 264 | 264 | |
@@ -269,21 +269,21 @@ discard block |
||
| 269 | 269 | ); |
| 270 | 270 | } |
| 271 | 271 | // Initial array. |
| 272 | - , array() ) |
|
| 272 | + , array()) |
|
| 273 | 273 | ); |
| 274 | 274 | |
| 275 | 275 | // Refactor data as per schema.org specifications. |
| 276 | - $images = array_map( function ( $attachment ) { |
|
| 276 | + $images = array_map(function($attachment) { |
|
| 277 | 277 | return Wordlift_Abstract_Post_To_Jsonld_Converter::set_image_size( |
| 278 | 278 | array( |
| 279 | 279 | '@type' => 'ImageObject', |
| 280 | 280 | 'url' => $attachment[0], |
| 281 | 281 | ), $attachment |
| 282 | 282 | ); |
| 283 | - }, $images_with_sizes ); |
|
| 283 | + }, $images_with_sizes); |
|
| 284 | 284 | |
| 285 | 285 | // Add images if present. |
| 286 | - if ( 0 < count( $images ) ) { |
|
| 286 | + if (0 < count($images)) { |
|
| 287 | 287 | $jsonld['image'] = $images; |
| 288 | 288 | } |
| 289 | 289 | |
@@ -302,9 +302,9 @@ discard block |
||
| 302 | 302 | * @access private |
| 303 | 303 | * |
| 304 | 304 | */ |
| 305 | - protected static function make_one( $value ) { |
|
| 305 | + protected static function make_one($value) { |
|
| 306 | 306 | |
| 307 | - return 1 === count( $value ) ? $value[0] : $value; |
|
| 307 | + return 1 === count($value) ? $value[0] : $value; |
|
| 308 | 308 | } |
| 309 | 309 | |
| 310 | 310 | /** |
@@ -318,17 +318,17 @@ discard block |
||
| 318 | 318 | * @since 3.14.0 |
| 319 | 319 | * |
| 320 | 320 | */ |
| 321 | - public static function set_image_size( $image, $attachment ) { |
|
| 321 | + public static function set_image_size($image, $attachment) { |
|
| 322 | 322 | |
| 323 | 323 | // If you specify a "width" or "height" value you should leave out |
| 324 | 324 | // 'px'. For example: "width":"4608px" should be "width":"4608". |
| 325 | 325 | // |
| 326 | 326 | // See https://github.com/insideout10/wordlift-plugin/issues/451. |
| 327 | - if ( isset( $attachment[1] ) && is_numeric( $attachment[1] ) && 0 < $attachment[1] ) { |
|
| 327 | + if (isset($attachment[1]) && is_numeric($attachment[1]) && 0 < $attachment[1]) { |
|
| 328 | 328 | $image['width'] = $attachment[1]; |
| 329 | 329 | } |
| 330 | 330 | |
| 331 | - if ( isset( $attachment[2] ) && is_numeric( $attachment[2] ) && 0 < $attachment[2] ) { |
|
| 331 | + if (isset($attachment[2]) && is_numeric($attachment[2]) && 0 < $attachment[2]) { |
|
| 332 | 332 | $image['height'] = $attachment[2]; |
| 333 | 333 | } |
| 334 | 334 | |
@@ -17,145 +17,145 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class Wordlift_Key_Validation_Service { |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * A {@link Wordlift_Log_Service} instance. |
|
| 22 | - * |
|
| 23 | - * @since 3.14.0 |
|
| 24 | - * @access private |
|
| 25 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 26 | - */ |
|
| 27 | - private $log; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * The {@link Wordlift_Configuration_Service} instance. |
|
| 31 | - * |
|
| 32 | - * @since 3.14.0 |
|
| 33 | - * @access private |
|
| 34 | - * @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 35 | - */ |
|
| 36 | - private $configuration_service; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * Create a {@link Wordlift_Key_Validation_Service} instance. |
|
| 40 | - * |
|
| 41 | - * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 42 | - * |
|
| 43 | - * @since 3.14.0 |
|
| 44 | - * |
|
| 45 | - */ |
|
| 46 | - public function __construct( $configuration_service ) { |
|
| 47 | - |
|
| 48 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Key_Validation_Service' ); |
|
| 49 | - |
|
| 50 | - $this->configuration_service = $configuration_service; |
|
| 51 | - add_action( 'admin_init', array( $this, 'wl_load_plugin' ) ); |
|
| 52 | - add_action( 'admin_notices', array( $this, 'wl_key_update_notice' ) ); |
|
| 53 | - |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * Validate the provided key. |
|
| 58 | - * |
|
| 59 | - * @param string $key WordLift's key to validate. |
|
| 60 | - * |
|
| 61 | - * @return WP_Error|array The response or WP_Error on failure. |
|
| 62 | - * @since 3.9.0 |
|
| 63 | - * |
|
| 64 | - */ |
|
| 65 | - public function get_account_info( $key ) { |
|
| 66 | - |
|
| 67 | - $this->log->debug( 'Validating key...' ); |
|
| 68 | - |
|
| 69 | - // Request the account info as a way to validate the key |
|
| 70 | - |
|
| 71 | - $args = array_merge_recursive( |
|
| 72 | - unserialize( WL_REDLINK_API_HTTP_OPTIONS ), |
|
| 73 | - array( |
|
| 74 | - 'headers' => array( |
|
| 75 | - 'Content-Type' => 'application/json; charset=utf-8', |
|
| 76 | - 'X-Authorization' => $key, |
|
| 77 | - ) |
|
| 78 | - ) |
|
| 79 | - ); |
|
| 80 | - |
|
| 81 | - return wp_remote_get( $this->configuration_service->get_accounts_info_by_key( $key ), $args ); |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * This function is hooked to the `wl_validate_key` AJAX call. |
|
| 86 | - * |
|
| 87 | - * @since 3.9.0 |
|
| 88 | - */ |
|
| 89 | - public function validate_key() { |
|
| 90 | - |
|
| 91 | - // Ensure we don't have garbage before us. |
|
| 92 | - ob_clean(); |
|
| 93 | - |
|
| 94 | - // Check if we have a key. |
|
| 95 | - if ( ! isset( $_POST['key'] ) ) { |
|
| 96 | - wp_send_json_error( 'The key parameter is required.' ); |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - $response = $this->get_account_info( $_POST['key'] ); |
|
| 100 | - $res_body = json_decode( wp_remote_retrieve_body( $response ), true ); |
|
| 101 | - |
|
| 102 | - // The URL stored in WLS. If this is the initial install the URL may be null. |
|
| 103 | - $url = $res_body['url']; |
|
| 104 | - |
|
| 105 | - // If we got an error, return invalid. |
|
| 106 | - if ( is_wp_error( $response ) || 2 !== (int) $response['response']['code'] / 100 ) { |
|
| 107 | - wp_send_json_success( array( 'valid' => false, 'message' => '' ) ); |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - // If the URL isn't set or matches, then it's valid. |
|
| 111 | - if ( is_null( $url ) || $url === site_url() ) { |
|
| 112 | - wp_send_json_success( array( 'valid' => true, 'message' => '' ) ); |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - // If the URL doesn't match it means that this key has been configured elsewhere already. |
|
| 116 | - if ( $url !== site_url() ) { |
|
| 117 | - Wordlift_Configuration_Service::get_instance()->set_key( '' ); |
|
| 118 | - wp_send_json_success( array( |
|
| 119 | - 'valid' => false, |
|
| 120 | - 'message' => __( 'The key is already used on another site, please contact us at [email protected] to move the key to another site.', 'wordlift' ), |
|
| 121 | - ) ); |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - // Set a response with valid set to true or false according to the key validity with message. |
|
| 125 | - wp_send_json_success( array( |
|
| 126 | - 'valid' => false, |
|
| 127 | - 'message' => __( 'An error occurred, please contact us at [email protected]', 'wordlift' ), |
|
| 128 | - ) ); |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * This function is hooked `admin_init` to check _wl_blog_url. |
|
| 133 | - * |
|
| 134 | - */ |
|
| 135 | - public function wl_load_plugin() { |
|
| 136 | - |
|
| 137 | - $wl_blog_url = get_option( '_wl_blog_url' ); |
|
| 138 | - |
|
| 139 | - if ( ! $wl_blog_url ) { |
|
| 140 | - update_option( '_wl_blog_url', site_url(), true ); |
|
| 141 | - } else if ( $wl_blog_url !== site_url() ) { |
|
| 142 | - Wordlift_Configuration_Service::get_instance()->set_key( '' ); |
|
| 143 | - set_transient( 'wl-key-error-msg', __( "Your web site URL has changed. To avoid data corruption, WordLift's key has been removed. Please provide a new key in WordLift Settings. If you believe this to be an error, please contact us at [email protected]", 'wordlift' ), 10 ); |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * This function is hooked to the `admin_notices` to show admin notification. |
|
| 150 | - * |
|
| 151 | - */ |
|
| 152 | - public function wl_key_update_notice() { |
|
| 153 | - if ( get_transient( 'wl-key-error-msg' ) ) { |
|
| 154 | - ?> |
|
| 20 | + /** |
|
| 21 | + * A {@link Wordlift_Log_Service} instance. |
|
| 22 | + * |
|
| 23 | + * @since 3.14.0 |
|
| 24 | + * @access private |
|
| 25 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 26 | + */ |
|
| 27 | + private $log; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * The {@link Wordlift_Configuration_Service} instance. |
|
| 31 | + * |
|
| 32 | + * @since 3.14.0 |
|
| 33 | + * @access private |
|
| 34 | + * @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 35 | + */ |
|
| 36 | + private $configuration_service; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * Create a {@link Wordlift_Key_Validation_Service} instance. |
|
| 40 | + * |
|
| 41 | + * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 42 | + * |
|
| 43 | + * @since 3.14.0 |
|
| 44 | + * |
|
| 45 | + */ |
|
| 46 | + public function __construct( $configuration_service ) { |
|
| 47 | + |
|
| 48 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Key_Validation_Service' ); |
|
| 49 | + |
|
| 50 | + $this->configuration_service = $configuration_service; |
|
| 51 | + add_action( 'admin_init', array( $this, 'wl_load_plugin' ) ); |
|
| 52 | + add_action( 'admin_notices', array( $this, 'wl_key_update_notice' ) ); |
|
| 53 | + |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * Validate the provided key. |
|
| 58 | + * |
|
| 59 | + * @param string $key WordLift's key to validate. |
|
| 60 | + * |
|
| 61 | + * @return WP_Error|array The response or WP_Error on failure. |
|
| 62 | + * @since 3.9.0 |
|
| 63 | + * |
|
| 64 | + */ |
|
| 65 | + public function get_account_info( $key ) { |
|
| 66 | + |
|
| 67 | + $this->log->debug( 'Validating key...' ); |
|
| 68 | + |
|
| 69 | + // Request the account info as a way to validate the key |
|
| 70 | + |
|
| 71 | + $args = array_merge_recursive( |
|
| 72 | + unserialize( WL_REDLINK_API_HTTP_OPTIONS ), |
|
| 73 | + array( |
|
| 74 | + 'headers' => array( |
|
| 75 | + 'Content-Type' => 'application/json; charset=utf-8', |
|
| 76 | + 'X-Authorization' => $key, |
|
| 77 | + ) |
|
| 78 | + ) |
|
| 79 | + ); |
|
| 80 | + |
|
| 81 | + return wp_remote_get( $this->configuration_service->get_accounts_info_by_key( $key ), $args ); |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * This function is hooked to the `wl_validate_key` AJAX call. |
|
| 86 | + * |
|
| 87 | + * @since 3.9.0 |
|
| 88 | + */ |
|
| 89 | + public function validate_key() { |
|
| 90 | + |
|
| 91 | + // Ensure we don't have garbage before us. |
|
| 92 | + ob_clean(); |
|
| 93 | + |
|
| 94 | + // Check if we have a key. |
|
| 95 | + if ( ! isset( $_POST['key'] ) ) { |
|
| 96 | + wp_send_json_error( 'The key parameter is required.' ); |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + $response = $this->get_account_info( $_POST['key'] ); |
|
| 100 | + $res_body = json_decode( wp_remote_retrieve_body( $response ), true ); |
|
| 101 | + |
|
| 102 | + // The URL stored in WLS. If this is the initial install the URL may be null. |
|
| 103 | + $url = $res_body['url']; |
|
| 104 | + |
|
| 105 | + // If we got an error, return invalid. |
|
| 106 | + if ( is_wp_error( $response ) || 2 !== (int) $response['response']['code'] / 100 ) { |
|
| 107 | + wp_send_json_success( array( 'valid' => false, 'message' => '' ) ); |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + // If the URL isn't set or matches, then it's valid. |
|
| 111 | + if ( is_null( $url ) || $url === site_url() ) { |
|
| 112 | + wp_send_json_success( array( 'valid' => true, 'message' => '' ) ); |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + // If the URL doesn't match it means that this key has been configured elsewhere already. |
|
| 116 | + if ( $url !== site_url() ) { |
|
| 117 | + Wordlift_Configuration_Service::get_instance()->set_key( '' ); |
|
| 118 | + wp_send_json_success( array( |
|
| 119 | + 'valid' => false, |
|
| 120 | + 'message' => __( 'The key is already used on another site, please contact us at [email protected] to move the key to another site.', 'wordlift' ), |
|
| 121 | + ) ); |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + // Set a response with valid set to true or false according to the key validity with message. |
|
| 125 | + wp_send_json_success( array( |
|
| 126 | + 'valid' => false, |
|
| 127 | + 'message' => __( 'An error occurred, please contact us at [email protected]', 'wordlift' ), |
|
| 128 | + ) ); |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * This function is hooked `admin_init` to check _wl_blog_url. |
|
| 133 | + * |
|
| 134 | + */ |
|
| 135 | + public function wl_load_plugin() { |
|
| 136 | + |
|
| 137 | + $wl_blog_url = get_option( '_wl_blog_url' ); |
|
| 138 | + |
|
| 139 | + if ( ! $wl_blog_url ) { |
|
| 140 | + update_option( '_wl_blog_url', site_url(), true ); |
|
| 141 | + } else if ( $wl_blog_url !== site_url() ) { |
|
| 142 | + Wordlift_Configuration_Service::get_instance()->set_key( '' ); |
|
| 143 | + set_transient( 'wl-key-error-msg', __( "Your web site URL has changed. To avoid data corruption, WordLift's key has been removed. Please provide a new key in WordLift Settings. If you believe this to be an error, please contact us at [email protected]", 'wordlift' ), 10 ); |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * This function is hooked to the `admin_notices` to show admin notification. |
|
| 150 | + * |
|
| 151 | + */ |
|
| 152 | + public function wl_key_update_notice() { |
|
| 153 | + if ( get_transient( 'wl-key-error-msg' ) ) { |
|
| 154 | + ?> |
|
| 155 | 155 | <div class="updated notice is-dismissible error"> |
| 156 | 156 | <p><?php _e( get_transient( 'wl-key-error-msg' ), 'wordlift' ); ?></p> |
| 157 | 157 | </div> |
| 158 | 158 | <?php |
| 159 | - } |
|
| 160 | - } |
|
| 159 | + } |
|
| 160 | + } |
|
| 161 | 161 | } |
@@ -43,13 +43,13 @@ discard block |
||
| 43 | 43 | * @since 3.14.0 |
| 44 | 44 | * |
| 45 | 45 | */ |
| 46 | - public function __construct( $configuration_service ) { |
|
| 46 | + public function __construct($configuration_service) { |
|
| 47 | 47 | |
| 48 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Key_Validation_Service' ); |
|
| 48 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Key_Validation_Service'); |
|
| 49 | 49 | |
| 50 | 50 | $this->configuration_service = $configuration_service; |
| 51 | - add_action( 'admin_init', array( $this, 'wl_load_plugin' ) ); |
|
| 52 | - add_action( 'admin_notices', array( $this, 'wl_key_update_notice' ) ); |
|
| 51 | + add_action('admin_init', array($this, 'wl_load_plugin')); |
|
| 52 | + add_action('admin_notices', array($this, 'wl_key_update_notice')); |
|
| 53 | 53 | |
| 54 | 54 | } |
| 55 | 55 | |
@@ -62,14 +62,14 @@ discard block |
||
| 62 | 62 | * @since 3.9.0 |
| 63 | 63 | * |
| 64 | 64 | */ |
| 65 | - public function get_account_info( $key ) { |
|
| 65 | + public function get_account_info($key) { |
|
| 66 | 66 | |
| 67 | - $this->log->debug( 'Validating key...' ); |
|
| 67 | + $this->log->debug('Validating key...'); |
|
| 68 | 68 | |
| 69 | 69 | // Request the account info as a way to validate the key |
| 70 | 70 | |
| 71 | 71 | $args = array_merge_recursive( |
| 72 | - unserialize( WL_REDLINK_API_HTTP_OPTIONS ), |
|
| 72 | + unserialize(WL_REDLINK_API_HTTP_OPTIONS), |
|
| 73 | 73 | array( |
| 74 | 74 | 'headers' => array( |
| 75 | 75 | 'Content-Type' => 'application/json; charset=utf-8', |
@@ -78,7 +78,7 @@ discard block |
||
| 78 | 78 | ) |
| 79 | 79 | ); |
| 80 | 80 | |
| 81 | - return wp_remote_get( $this->configuration_service->get_accounts_info_by_key( $key ), $args ); |
|
| 81 | + return wp_remote_get($this->configuration_service->get_accounts_info_by_key($key), $args); |
|
| 82 | 82 | } |
| 83 | 83 | |
| 84 | 84 | /** |
@@ -92,40 +92,40 @@ discard block |
||
| 92 | 92 | ob_clean(); |
| 93 | 93 | |
| 94 | 94 | // Check if we have a key. |
| 95 | - if ( ! isset( $_POST['key'] ) ) { |
|
| 96 | - wp_send_json_error( 'The key parameter is required.' ); |
|
| 95 | + if ( ! isset($_POST['key'])) { |
|
| 96 | + wp_send_json_error('The key parameter is required.'); |
|
| 97 | 97 | } |
| 98 | 98 | |
| 99 | - $response = $this->get_account_info( $_POST['key'] ); |
|
| 100 | - $res_body = json_decode( wp_remote_retrieve_body( $response ), true ); |
|
| 99 | + $response = $this->get_account_info($_POST['key']); |
|
| 100 | + $res_body = json_decode(wp_remote_retrieve_body($response), true); |
|
| 101 | 101 | |
| 102 | 102 | // The URL stored in WLS. If this is the initial install the URL may be null. |
| 103 | 103 | $url = $res_body['url']; |
| 104 | 104 | |
| 105 | 105 | // If we got an error, return invalid. |
| 106 | - if ( is_wp_error( $response ) || 2 !== (int) $response['response']['code'] / 100 ) { |
|
| 107 | - wp_send_json_success( array( 'valid' => false, 'message' => '' ) ); |
|
| 106 | + if (is_wp_error($response) || 2 !== (int) $response['response']['code'] / 100) { |
|
| 107 | + wp_send_json_success(array('valid' => false, 'message' => '')); |
|
| 108 | 108 | } |
| 109 | 109 | |
| 110 | 110 | // If the URL isn't set or matches, then it's valid. |
| 111 | - if ( is_null( $url ) || $url === site_url() ) { |
|
| 112 | - wp_send_json_success( array( 'valid' => true, 'message' => '' ) ); |
|
| 111 | + if (is_null($url) || $url === site_url()) { |
|
| 112 | + wp_send_json_success(array('valid' => true, 'message' => '')); |
|
| 113 | 113 | } |
| 114 | 114 | |
| 115 | 115 | // If the URL doesn't match it means that this key has been configured elsewhere already. |
| 116 | - if ( $url !== site_url() ) { |
|
| 117 | - Wordlift_Configuration_Service::get_instance()->set_key( '' ); |
|
| 118 | - wp_send_json_success( array( |
|
| 116 | + if ($url !== site_url()) { |
|
| 117 | + Wordlift_Configuration_Service::get_instance()->set_key(''); |
|
| 118 | + wp_send_json_success(array( |
|
| 119 | 119 | 'valid' => false, |
| 120 | - 'message' => __( 'The key is already used on another site, please contact us at [email protected] to move the key to another site.', 'wordlift' ), |
|
| 121 | - ) ); |
|
| 120 | + 'message' => __('The key is already used on another site, please contact us at [email protected] to move the key to another site.', 'wordlift'), |
|
| 121 | + )); |
|
| 122 | 122 | } |
| 123 | 123 | |
| 124 | 124 | // Set a response with valid set to true or false according to the key validity with message. |
| 125 | - wp_send_json_success( array( |
|
| 125 | + wp_send_json_success(array( |
|
| 126 | 126 | 'valid' => false, |
| 127 | - 'message' => __( 'An error occurred, please contact us at [email protected]', 'wordlift' ), |
|
| 128 | - ) ); |
|
| 127 | + 'message' => __('An error occurred, please contact us at [email protected]', 'wordlift'), |
|
| 128 | + )); |
|
| 129 | 129 | } |
| 130 | 130 | |
| 131 | 131 | /** |
@@ -134,13 +134,13 @@ discard block |
||
| 134 | 134 | */ |
| 135 | 135 | public function wl_load_plugin() { |
| 136 | 136 | |
| 137 | - $wl_blog_url = get_option( '_wl_blog_url' ); |
|
| 137 | + $wl_blog_url = get_option('_wl_blog_url'); |
|
| 138 | 138 | |
| 139 | - if ( ! $wl_blog_url ) { |
|
| 140 | - update_option( '_wl_blog_url', site_url(), true ); |
|
| 141 | - } else if ( $wl_blog_url !== site_url() ) { |
|
| 142 | - Wordlift_Configuration_Service::get_instance()->set_key( '' ); |
|
| 143 | - set_transient( 'wl-key-error-msg', __( "Your web site URL has changed. To avoid data corruption, WordLift's key has been removed. Please provide a new key in WordLift Settings. If you believe this to be an error, please contact us at [email protected]", 'wordlift' ), 10 ); |
|
| 139 | + if ( ! $wl_blog_url) { |
|
| 140 | + update_option('_wl_blog_url', site_url(), true); |
|
| 141 | + } else if ($wl_blog_url !== site_url()) { |
|
| 142 | + Wordlift_Configuration_Service::get_instance()->set_key(''); |
|
| 143 | + set_transient('wl-key-error-msg', __("Your web site URL has changed. To avoid data corruption, WordLift's key has been removed. Please provide a new key in WordLift Settings. If you believe this to be an error, please contact us at [email protected]", 'wordlift'), 10); |
|
| 144 | 144 | } |
| 145 | 145 | |
| 146 | 146 | } |
@@ -150,10 +150,10 @@ discard block |
||
| 150 | 150 | * |
| 151 | 151 | */ |
| 152 | 152 | public function wl_key_update_notice() { |
| 153 | - if ( get_transient( 'wl-key-error-msg' ) ) { |
|
| 153 | + if (get_transient('wl-key-error-msg')) { |
|
| 154 | 154 | ?> |
| 155 | 155 | <div class="updated notice is-dismissible error"> |
| 156 | - <p><?php _e( get_transient( 'wl-key-error-msg' ), 'wordlift' ); ?></p> |
|
| 156 | + <p><?php _e(get_transient('wl-key-error-msg'), 'wordlift'); ?></p> |
|
| 157 | 157 | </div> |
| 158 | 158 | <?php |
| 159 | 159 | } |
@@ -16,506 +16,506 @@ discard block |
||
| 16 | 16 | */ |
| 17 | 17 | class Wordlift_Entity_Service { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * The Log service. |
|
| 21 | - * |
|
| 22 | - * @since 3.2.0 |
|
| 23 | - * @access private |
|
| 24 | - * @var \Wordlift_Log_Service $log The Log service. |
|
| 25 | - */ |
|
| 26 | - private $log; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * The UI service. |
|
| 30 | - * |
|
| 31 | - * @since 3.2.0 |
|
| 32 | - * @access private |
|
| 33 | - * @var \Wordlift_UI_Service $ui_service The UI service. |
|
| 34 | - */ |
|
| 35 | - private $ui_service; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * The {@link Wordlift_Relation_Service} instance. |
|
| 39 | - * |
|
| 40 | - * @since 3.15.0 |
|
| 41 | - * @access private |
|
| 42 | - * @var \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 43 | - */ |
|
| 44 | - private $relation_service; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 48 | - * |
|
| 49 | - * @since 3.16.3 |
|
| 50 | - * @access private |
|
| 51 | - * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 52 | - */ |
|
| 53 | - private $entity_uri_service; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * The entity post type name. |
|
| 57 | - * |
|
| 58 | - * @since 3.1.0 |
|
| 59 | - */ |
|
| 60 | - const TYPE_NAME = 'entity'; |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * The alternative label meta key. |
|
| 64 | - * |
|
| 65 | - * @since 3.2.0 |
|
| 66 | - */ |
|
| 67 | - const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label'; |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * The alternative label input template. |
|
| 71 | - * |
|
| 72 | - * @since 3.2.0 |
|
| 73 | - */ |
|
| 74 | - // TODO: this should be moved to a class that deals with HTML code. |
|
| 75 | - const ALTERNATIVE_LABEL_INPUT_TEMPLATE = '<div class="wl-alternative-label"> |
|
| 19 | + /** |
|
| 20 | + * The Log service. |
|
| 21 | + * |
|
| 22 | + * @since 3.2.0 |
|
| 23 | + * @access private |
|
| 24 | + * @var \Wordlift_Log_Service $log The Log service. |
|
| 25 | + */ |
|
| 26 | + private $log; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * The UI service. |
|
| 30 | + * |
|
| 31 | + * @since 3.2.0 |
|
| 32 | + * @access private |
|
| 33 | + * @var \Wordlift_UI_Service $ui_service The UI service. |
|
| 34 | + */ |
|
| 35 | + private $ui_service; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * The {@link Wordlift_Relation_Service} instance. |
|
| 39 | + * |
|
| 40 | + * @since 3.15.0 |
|
| 41 | + * @access private |
|
| 42 | + * @var \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 43 | + */ |
|
| 44 | + private $relation_service; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 48 | + * |
|
| 49 | + * @since 3.16.3 |
|
| 50 | + * @access private |
|
| 51 | + * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 52 | + */ |
|
| 53 | + private $entity_uri_service; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * The entity post type name. |
|
| 57 | + * |
|
| 58 | + * @since 3.1.0 |
|
| 59 | + */ |
|
| 60 | + const TYPE_NAME = 'entity'; |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * The alternative label meta key. |
|
| 64 | + * |
|
| 65 | + * @since 3.2.0 |
|
| 66 | + */ |
|
| 67 | + const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label'; |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * The alternative label input template. |
|
| 71 | + * |
|
| 72 | + * @since 3.2.0 |
|
| 73 | + */ |
|
| 74 | + // TODO: this should be moved to a class that deals with HTML code. |
|
| 75 | + const ALTERNATIVE_LABEL_INPUT_TEMPLATE = '<div class="wl-alternative-label"> |
|
| 76 | 76 | <label class="screen-reader-text" id="wl-alternative-label-prompt-text" for="wl-alternative-label">Enter alternative label here</label> |
| 77 | 77 | <input name="wl_alternative_label[]" size="30" value="%s" id="wl-alternative-label" type="text"> |
| 78 | 78 | <button class="button wl-delete-button">%s</button> |
| 79 | 79 | </div>'; |
| 80 | 80 | |
| 81 | - /** |
|
| 82 | - * A singleton instance of the Entity service. |
|
| 83 | - * |
|
| 84 | - * @since 3.2.0 |
|
| 85 | - * @access private |
|
| 86 | - * @var \Wordlift_Entity_Service $instance A singleton instance of the Entity service. |
|
| 87 | - */ |
|
| 88 | - private static $instance; |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Create a Wordlift_Entity_Service instance. |
|
| 92 | - * |
|
| 93 | - * @param \Wordlift_UI_Service $ui_service The UI service. |
|
| 94 | - * @param \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 95 | - * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 96 | - * |
|
| 97 | - * @since 3.2.0 |
|
| 98 | - * |
|
| 99 | - */ |
|
| 100 | - public function __construct( $ui_service, $relation_service, $entity_uri_service ) { |
|
| 101 | - |
|
| 102 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
| 103 | - |
|
| 104 | - $this->ui_service = $ui_service; |
|
| 105 | - $this->relation_service = $relation_service; |
|
| 106 | - $this->entity_uri_service = $entity_uri_service; |
|
| 107 | - |
|
| 108 | - // Set the singleton instance. |
|
| 109 | - self::$instance = $this; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * Get the singleton instance of the Entity service. |
|
| 114 | - * |
|
| 115 | - * @return \Wordlift_Entity_Service The singleton instance of the Entity service. |
|
| 116 | - * @since 3.2.0 |
|
| 117 | - */ |
|
| 118 | - public static function get_instance() { |
|
| 119 | - |
|
| 120 | - return self::$instance; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Determines whether a post is an entity or not. Entity is in this context |
|
| 125 | - * something which is not an article. |
|
| 126 | - * |
|
| 127 | - * @param int $post_id A post id. |
|
| 128 | - * |
|
| 129 | - * @return bool Return true if the post is an entity otherwise false. |
|
| 130 | - * @since 3.1.0 |
|
| 131 | - * |
|
| 132 | - */ |
|
| 133 | - public function is_entity( $post_id ) { |
|
| 134 | - |
|
| 135 | - $terms = wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 136 | - |
|
| 137 | - if ( is_wp_error( $terms ) ) { |
|
| 138 | - $this->log->error( "Cannot get the terms for post $post_id: " . $terms->get_error_message() ); |
|
| 139 | - |
|
| 140 | - return false; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - if ( empty( $terms ) ) { |
|
| 144 | - return false; |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - /* |
|
| 81 | + /** |
|
| 82 | + * A singleton instance of the Entity service. |
|
| 83 | + * |
|
| 84 | + * @since 3.2.0 |
|
| 85 | + * @access private |
|
| 86 | + * @var \Wordlift_Entity_Service $instance A singleton instance of the Entity service. |
|
| 87 | + */ |
|
| 88 | + private static $instance; |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Create a Wordlift_Entity_Service instance. |
|
| 92 | + * |
|
| 93 | + * @param \Wordlift_UI_Service $ui_service The UI service. |
|
| 94 | + * @param \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 95 | + * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 96 | + * |
|
| 97 | + * @since 3.2.0 |
|
| 98 | + * |
|
| 99 | + */ |
|
| 100 | + public function __construct( $ui_service, $relation_service, $entity_uri_service ) { |
|
| 101 | + |
|
| 102 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
| 103 | + |
|
| 104 | + $this->ui_service = $ui_service; |
|
| 105 | + $this->relation_service = $relation_service; |
|
| 106 | + $this->entity_uri_service = $entity_uri_service; |
|
| 107 | + |
|
| 108 | + // Set the singleton instance. |
|
| 109 | + self::$instance = $this; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * Get the singleton instance of the Entity service. |
|
| 114 | + * |
|
| 115 | + * @return \Wordlift_Entity_Service The singleton instance of the Entity service. |
|
| 116 | + * @since 3.2.0 |
|
| 117 | + */ |
|
| 118 | + public static function get_instance() { |
|
| 119 | + |
|
| 120 | + return self::$instance; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Determines whether a post is an entity or not. Entity is in this context |
|
| 125 | + * something which is not an article. |
|
| 126 | + * |
|
| 127 | + * @param int $post_id A post id. |
|
| 128 | + * |
|
| 129 | + * @return bool Return true if the post is an entity otherwise false. |
|
| 130 | + * @since 3.1.0 |
|
| 131 | + * |
|
| 132 | + */ |
|
| 133 | + public function is_entity( $post_id ) { |
|
| 134 | + |
|
| 135 | + $terms = wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 136 | + |
|
| 137 | + if ( is_wp_error( $terms ) ) { |
|
| 138 | + $this->log->error( "Cannot get the terms for post $post_id: " . $terms->get_error_message() ); |
|
| 139 | + |
|
| 140 | + return false; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + if ( empty( $terms ) ) { |
|
| 144 | + return false; |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + /* |
|
| 148 | 148 | * We don't consider an `article` to be an entity. |
| 149 | 149 | * |
| 150 | 150 | * @since 3.20.0 At least one associated mustn't be an `article`. |
| 151 | 151 | * |
| 152 | 152 | * @see https://github.com/insideout10/wordlift-plugin/issues/835 |
| 153 | 153 | */ |
| 154 | - foreach ( $terms as $term ) { |
|
| 155 | - if ( 'article' !== $term->slug ) { |
|
| 156 | - return true; |
|
| 157 | - } |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - return false; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * Get the proper classification scope for a given entity post |
|
| 165 | - * |
|
| 166 | - * @param integer $post_id An entity post id. |
|
| 167 | - * |
|
| 168 | - * @param string $default The default classification scope, `what` if not |
|
| 169 | - * provided. |
|
| 170 | - * |
|
| 171 | - * @return string Returns a classification scope (e.g. 'what'). |
|
| 172 | - * @since 3.5.0 |
|
| 173 | - * |
|
| 174 | - */ |
|
| 175 | - public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) { |
|
| 176 | - |
|
| 177 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 178 | - return $default; |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - // Retrieve the entity type |
|
| 182 | - $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get( $post_id ); |
|
| 183 | - $entity_type = str_replace( 'wl-', '', $entity_type_arr['css_class'] ); |
|
| 184 | - // Retrieve classification boxes configuration |
|
| 185 | - $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
| 186 | - foreach ( $classification_boxes as $cb ) { |
|
| 187 | - if ( in_array( $entity_type, $cb['registeredTypes'] ) ) { |
|
| 188 | - return $cb['id']; |
|
| 189 | - } |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - return $default; |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - /** |
|
| 196 | - * Check whether a {@link WP_Post} is used. |
|
| 197 | - * |
|
| 198 | - * @param int $post_id The {@link WP_Post}'s id. |
|
| 199 | - * |
|
| 200 | - * @return bool|null Null if it's not an entity, otherwise true if it's used. |
|
| 201 | - */ |
|
| 202 | - public function is_used( $post_id ) { |
|
| 203 | - |
|
| 204 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 205 | - return null; |
|
| 206 | - } |
|
| 207 | - // Retrieve the post |
|
| 208 | - $entity = get_post( $post_id ); |
|
| 209 | - |
|
| 210 | - global $wpdb; |
|
| 211 | - // Retrieve Wordlift relation instances table name |
|
| 212 | - $table_name = wl_core_get_relation_instances_table_name(); |
|
| 213 | - |
|
| 214 | - // Check is it's referenced / related to another post / entity |
|
| 215 | - $stmt = $wpdb->prepare( |
|
| 216 | - "SELECT COUNT(*) FROM $table_name WHERE object_id = %d", |
|
| 217 | - $entity->ID |
|
| 218 | - ); |
|
| 219 | - |
|
| 220 | - // Perform the query |
|
| 221 | - $relation_instances = (int) $wpdb->get_var( $stmt ); |
|
| 222 | - // If there is at least one relation instance for the current entity, then it's used |
|
| 223 | - if ( 0 < $relation_instances ) { |
|
| 224 | - return true; |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - // Check if the entity uri is used as meta_value |
|
| 228 | - $stmt = $wpdb->prepare( |
|
| 229 | - "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s", |
|
| 230 | - $entity->ID, |
|
| 231 | - wl_get_entity_uri( $entity->ID ) |
|
| 232 | - ); |
|
| 233 | - // Perform the query |
|
| 234 | - $meta_instances = (int) $wpdb->get_var( $stmt ); |
|
| 235 | - |
|
| 236 | - // If there is at least one meta that refers the current entity uri, then current entity is used |
|
| 237 | - if ( 0 < $meta_instances ) { |
|
| 238 | - return true; |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - // If we are here, it means the current entity is not used at the moment |
|
| 242 | - return false; |
|
| 243 | - } |
|
| 244 | - |
|
| 245 | - /** |
|
| 246 | - * Find entity posts by the entity URI. Entity as searched by their entity URI or same as. |
|
| 247 | - * |
|
| 248 | - * @param string $uri The entity URI. |
|
| 249 | - * |
|
| 250 | - * @return WP_Post|null A WP_Post instance or null if not found. |
|
| 251 | - * @deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri ); |
|
| 252 | - * |
|
| 253 | - * @since 3.16.3 deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri ); |
|
| 254 | - * @since 3.2.0 |
|
| 255 | - * |
|
| 256 | - */ |
|
| 257 | - public function get_entity_post_by_uri( $uri ) { |
|
| 258 | - |
|
| 259 | - return $this->entity_uri_service->get_entity( $uri ); |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - /** |
|
| 263 | - * Fires once a post has been saved. This function uses the $_REQUEST, therefore |
|
| 264 | - * we check that the post we're saving is the current post. |
|
| 265 | - * |
|
| 266 | - * @see https://github.com/insideout10/wordlift-plugin/issues/363 |
|
| 267 | - * |
|
| 268 | - * @since 3.2.0 |
|
| 269 | - * |
|
| 270 | - * @param int $post_id Post ID. |
|
| 271 | - * @param WP_Post $post Post object. |
|
| 272 | - * @param bool $update Whether this is an existing post being updated or not. |
|
| 273 | - */ |
|
| 274 | - public function save_post( $post_id, $post, $update ) { |
|
| 275 | - |
|
| 276 | - // Avoid doing anything if post is autosave or a revision. |
|
| 277 | - if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { |
|
| 278 | - return; |
|
| 279 | - } |
|
| 280 | - |
|
| 281 | - // We're setting the alternative label that have been provided via the UI |
|
| 282 | - // (in fact we're using $_REQUEST), while save_post may be also called |
|
| 283 | - // programmatically by some other function: we need to check therefore if |
|
| 284 | - // the $post_id in the save_post call matches the post id set in the request. |
|
| 285 | - // |
|
| 286 | - // If this is not the current post being saved or if it's not an entity, return. |
|
| 287 | - if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) { |
|
| 288 | - return; |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - // Get the alt labels from the request (or empty array). |
|
| 292 | - $alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? $_REQUEST['wl_alternative_label'] : array(); |
|
| 293 | - |
|
| 294 | - if ( ( ! empty( $_POST['content'] ) && ! empty( $_POST['post_content'] ) ) || isset( $_REQUEST['wl_alternative_label'] ) ) { |
|
| 295 | - // This is via classic editor, so set the alternative labels. |
|
| 296 | - $this->set_alternative_labels( $post_id, $alt_labels ); |
|
| 297 | - } |
|
| 298 | - |
|
| 299 | - |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - /** |
|
| 303 | - * Set the alternative labels. |
|
| 304 | - * |
|
| 305 | - * @param int $post_id The post id. |
|
| 306 | - * @param array $alt_labels An array of labels. |
|
| 307 | - * |
|
| 308 | - * @since 3.2.0 |
|
| 309 | - * |
|
| 310 | - */ |
|
| 311 | - public function set_alternative_labels( $post_id, $alt_labels ) { |
|
| 312 | - |
|
| 313 | - // Bail out if post id is not numeric. We add this check as we found a WP install that was sending a WP_Error |
|
| 314 | - // instead of post id. |
|
| 315 | - if ( ! is_numeric( $post_id ) ) { |
|
| 316 | - return; |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - // Force $alt_labels to be an array |
|
| 320 | - if ( ! is_array( $alt_labels ) ) { |
|
| 321 | - $alt_labels = array( $alt_labels ); |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" ); |
|
| 325 | - |
|
| 326 | - // Delete all the existing alternate labels. |
|
| 327 | - delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 328 | - |
|
| 329 | - // Set the alternative labels. |
|
| 330 | - foreach ( $alt_labels as $alt_label ) { |
|
| 331 | - if ( ! empty( $alt_label ) ) { |
|
| 332 | - add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label ); |
|
| 333 | - } |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - } |
|
| 337 | - |
|
| 338 | - /** |
|
| 339 | - * Retrieve the alternate labels. |
|
| 340 | - * |
|
| 341 | - * @param int $post_id Post id. |
|
| 342 | - * |
|
| 343 | - * @return mixed An array of alternative labels. |
|
| 344 | - * @since 3.2.0 |
|
| 345 | - * |
|
| 346 | - */ |
|
| 347 | - public function get_alternative_labels( $post_id ) { |
|
| 348 | - |
|
| 349 | - return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - /** |
|
| 353 | - * Retrieve the labels for an entity, i.e. the title + the synonyms. |
|
| 354 | - * |
|
| 355 | - * @param int $post_id The entity {@link WP_Post} id. |
|
| 356 | - * |
|
| 357 | - * @return array An array with the entity title and labels. |
|
| 358 | - * @since 3.12.0 |
|
| 359 | - * |
|
| 360 | - */ |
|
| 361 | - public function get_labels( $post_id ) { |
|
| 362 | - |
|
| 363 | - return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) ); |
|
| 364 | - } |
|
| 365 | - |
|
| 366 | - /** |
|
| 367 | - * Fires before the permalink field in the edit form (this event is available in WP from 4.1.0). |
|
| 368 | - * |
|
| 369 | - * @param WP_Post $post Post object. |
|
| 370 | - * |
|
| 371 | - * @since 3.2.0 |
|
| 372 | - * |
|
| 373 | - */ |
|
| 374 | - public function edit_form_before_permalink( $post ) { |
|
| 375 | - |
|
| 376 | - // If it's not an entity, return. |
|
| 377 | - if ( ! $this->is_entity( $post->ID ) ) { |
|
| 378 | - return; |
|
| 379 | - } |
|
| 380 | - |
|
| 381 | - // Print the input template. |
|
| 382 | - $this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
| 383 | - |
|
| 384 | - // Print all the currently set alternative labels. |
|
| 385 | - foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) { |
|
| 386 | - |
|
| 387 | - echo $this->get_alternative_label_input( $alt_label ); |
|
| 388 | - |
|
| 389 | - }; |
|
| 390 | - |
|
| 391 | - // Print the button. |
|
| 392 | - $this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
| 393 | - |
|
| 394 | - } |
|
| 395 | - |
|
| 396 | - /** |
|
| 397 | - * Get the URI for the entity with the specified post id. |
|
| 398 | - * |
|
| 399 | - * @param int $post_id The entity post id. |
|
| 400 | - * |
|
| 401 | - * @return null|string The entity URI or NULL if not found or the dataset URI is not configured. |
|
| 402 | - * @since 3.6.0 |
|
| 403 | - * |
|
| 404 | - */ |
|
| 405 | - public function get_uri( $post_id ) { |
|
| 406 | - |
|
| 407 | - // If a null is given, nothing to do |
|
| 408 | - if ( is_null( $post_id ) ) { |
|
| 409 | - return null; |
|
| 410 | - } |
|
| 411 | - |
|
| 412 | - $dataset_uri = wl_configuration_get_redlink_dataset_uri(); |
|
| 413 | - |
|
| 414 | - // If the dataset uri is not properly configured, null is returned |
|
| 415 | - if ( empty( $dataset_uri ) ) { |
|
| 416 | - return null; |
|
| 417 | - } |
|
| 418 | - |
|
| 419 | - $uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true ); |
|
| 420 | - |
|
| 421 | - /* |
|
| 154 | + foreach ( $terms as $term ) { |
|
| 155 | + if ( 'article' !== $term->slug ) { |
|
| 156 | + return true; |
|
| 157 | + } |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + return false; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * Get the proper classification scope for a given entity post |
|
| 165 | + * |
|
| 166 | + * @param integer $post_id An entity post id. |
|
| 167 | + * |
|
| 168 | + * @param string $default The default classification scope, `what` if not |
|
| 169 | + * provided. |
|
| 170 | + * |
|
| 171 | + * @return string Returns a classification scope (e.g. 'what'). |
|
| 172 | + * @since 3.5.0 |
|
| 173 | + * |
|
| 174 | + */ |
|
| 175 | + public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) { |
|
| 176 | + |
|
| 177 | + if ( false === $this->is_entity( $post_id ) ) { |
|
| 178 | + return $default; |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + // Retrieve the entity type |
|
| 182 | + $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get( $post_id ); |
|
| 183 | + $entity_type = str_replace( 'wl-', '', $entity_type_arr['css_class'] ); |
|
| 184 | + // Retrieve classification boxes configuration |
|
| 185 | + $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
| 186 | + foreach ( $classification_boxes as $cb ) { |
|
| 187 | + if ( in_array( $entity_type, $cb['registeredTypes'] ) ) { |
|
| 188 | + return $cb['id']; |
|
| 189 | + } |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + return $default; |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + /** |
|
| 196 | + * Check whether a {@link WP_Post} is used. |
|
| 197 | + * |
|
| 198 | + * @param int $post_id The {@link WP_Post}'s id. |
|
| 199 | + * |
|
| 200 | + * @return bool|null Null if it's not an entity, otherwise true if it's used. |
|
| 201 | + */ |
|
| 202 | + public function is_used( $post_id ) { |
|
| 203 | + |
|
| 204 | + if ( false === $this->is_entity( $post_id ) ) { |
|
| 205 | + return null; |
|
| 206 | + } |
|
| 207 | + // Retrieve the post |
|
| 208 | + $entity = get_post( $post_id ); |
|
| 209 | + |
|
| 210 | + global $wpdb; |
|
| 211 | + // Retrieve Wordlift relation instances table name |
|
| 212 | + $table_name = wl_core_get_relation_instances_table_name(); |
|
| 213 | + |
|
| 214 | + // Check is it's referenced / related to another post / entity |
|
| 215 | + $stmt = $wpdb->prepare( |
|
| 216 | + "SELECT COUNT(*) FROM $table_name WHERE object_id = %d", |
|
| 217 | + $entity->ID |
|
| 218 | + ); |
|
| 219 | + |
|
| 220 | + // Perform the query |
|
| 221 | + $relation_instances = (int) $wpdb->get_var( $stmt ); |
|
| 222 | + // If there is at least one relation instance for the current entity, then it's used |
|
| 223 | + if ( 0 < $relation_instances ) { |
|
| 224 | + return true; |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + // Check if the entity uri is used as meta_value |
|
| 228 | + $stmt = $wpdb->prepare( |
|
| 229 | + "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s", |
|
| 230 | + $entity->ID, |
|
| 231 | + wl_get_entity_uri( $entity->ID ) |
|
| 232 | + ); |
|
| 233 | + // Perform the query |
|
| 234 | + $meta_instances = (int) $wpdb->get_var( $stmt ); |
|
| 235 | + |
|
| 236 | + // If there is at least one meta that refers the current entity uri, then current entity is used |
|
| 237 | + if ( 0 < $meta_instances ) { |
|
| 238 | + return true; |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + // If we are here, it means the current entity is not used at the moment |
|
| 242 | + return false; |
|
| 243 | + } |
|
| 244 | + |
|
| 245 | + /** |
|
| 246 | + * Find entity posts by the entity URI. Entity as searched by their entity URI or same as. |
|
| 247 | + * |
|
| 248 | + * @param string $uri The entity URI. |
|
| 249 | + * |
|
| 250 | + * @return WP_Post|null A WP_Post instance or null if not found. |
|
| 251 | + * @deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri ); |
|
| 252 | + * |
|
| 253 | + * @since 3.16.3 deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri ); |
|
| 254 | + * @since 3.2.0 |
|
| 255 | + * |
|
| 256 | + */ |
|
| 257 | + public function get_entity_post_by_uri( $uri ) { |
|
| 258 | + |
|
| 259 | + return $this->entity_uri_service->get_entity( $uri ); |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + /** |
|
| 263 | + * Fires once a post has been saved. This function uses the $_REQUEST, therefore |
|
| 264 | + * we check that the post we're saving is the current post. |
|
| 265 | + * |
|
| 266 | + * @see https://github.com/insideout10/wordlift-plugin/issues/363 |
|
| 267 | + * |
|
| 268 | + * @since 3.2.0 |
|
| 269 | + * |
|
| 270 | + * @param int $post_id Post ID. |
|
| 271 | + * @param WP_Post $post Post object. |
|
| 272 | + * @param bool $update Whether this is an existing post being updated or not. |
|
| 273 | + */ |
|
| 274 | + public function save_post( $post_id, $post, $update ) { |
|
| 275 | + |
|
| 276 | + // Avoid doing anything if post is autosave or a revision. |
|
| 277 | + if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { |
|
| 278 | + return; |
|
| 279 | + } |
|
| 280 | + |
|
| 281 | + // We're setting the alternative label that have been provided via the UI |
|
| 282 | + // (in fact we're using $_REQUEST), while save_post may be also called |
|
| 283 | + // programmatically by some other function: we need to check therefore if |
|
| 284 | + // the $post_id in the save_post call matches the post id set in the request. |
|
| 285 | + // |
|
| 286 | + // If this is not the current post being saved or if it's not an entity, return. |
|
| 287 | + if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) { |
|
| 288 | + return; |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + // Get the alt labels from the request (or empty array). |
|
| 292 | + $alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? $_REQUEST['wl_alternative_label'] : array(); |
|
| 293 | + |
|
| 294 | + if ( ( ! empty( $_POST['content'] ) && ! empty( $_POST['post_content'] ) ) || isset( $_REQUEST['wl_alternative_label'] ) ) { |
|
| 295 | + // This is via classic editor, so set the alternative labels. |
|
| 296 | + $this->set_alternative_labels( $post_id, $alt_labels ); |
|
| 297 | + } |
|
| 298 | + |
|
| 299 | + |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + /** |
|
| 303 | + * Set the alternative labels. |
|
| 304 | + * |
|
| 305 | + * @param int $post_id The post id. |
|
| 306 | + * @param array $alt_labels An array of labels. |
|
| 307 | + * |
|
| 308 | + * @since 3.2.0 |
|
| 309 | + * |
|
| 310 | + */ |
|
| 311 | + public function set_alternative_labels( $post_id, $alt_labels ) { |
|
| 312 | + |
|
| 313 | + // Bail out if post id is not numeric. We add this check as we found a WP install that was sending a WP_Error |
|
| 314 | + // instead of post id. |
|
| 315 | + if ( ! is_numeric( $post_id ) ) { |
|
| 316 | + return; |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + // Force $alt_labels to be an array |
|
| 320 | + if ( ! is_array( $alt_labels ) ) { |
|
| 321 | + $alt_labels = array( $alt_labels ); |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" ); |
|
| 325 | + |
|
| 326 | + // Delete all the existing alternate labels. |
|
| 327 | + delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 328 | + |
|
| 329 | + // Set the alternative labels. |
|
| 330 | + foreach ( $alt_labels as $alt_label ) { |
|
| 331 | + if ( ! empty( $alt_label ) ) { |
|
| 332 | + add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label ); |
|
| 333 | + } |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + } |
|
| 337 | + |
|
| 338 | + /** |
|
| 339 | + * Retrieve the alternate labels. |
|
| 340 | + * |
|
| 341 | + * @param int $post_id Post id. |
|
| 342 | + * |
|
| 343 | + * @return mixed An array of alternative labels. |
|
| 344 | + * @since 3.2.0 |
|
| 345 | + * |
|
| 346 | + */ |
|
| 347 | + public function get_alternative_labels( $post_id ) { |
|
| 348 | + |
|
| 349 | + return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + /** |
|
| 353 | + * Retrieve the labels for an entity, i.e. the title + the synonyms. |
|
| 354 | + * |
|
| 355 | + * @param int $post_id The entity {@link WP_Post} id. |
|
| 356 | + * |
|
| 357 | + * @return array An array with the entity title and labels. |
|
| 358 | + * @since 3.12.0 |
|
| 359 | + * |
|
| 360 | + */ |
|
| 361 | + public function get_labels( $post_id ) { |
|
| 362 | + |
|
| 363 | + return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) ); |
|
| 364 | + } |
|
| 365 | + |
|
| 366 | + /** |
|
| 367 | + * Fires before the permalink field in the edit form (this event is available in WP from 4.1.0). |
|
| 368 | + * |
|
| 369 | + * @param WP_Post $post Post object. |
|
| 370 | + * |
|
| 371 | + * @since 3.2.0 |
|
| 372 | + * |
|
| 373 | + */ |
|
| 374 | + public function edit_form_before_permalink( $post ) { |
|
| 375 | + |
|
| 376 | + // If it's not an entity, return. |
|
| 377 | + if ( ! $this->is_entity( $post->ID ) ) { |
|
| 378 | + return; |
|
| 379 | + } |
|
| 380 | + |
|
| 381 | + // Print the input template. |
|
| 382 | + $this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
| 383 | + |
|
| 384 | + // Print all the currently set alternative labels. |
|
| 385 | + foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) { |
|
| 386 | + |
|
| 387 | + echo $this->get_alternative_label_input( $alt_label ); |
|
| 388 | + |
|
| 389 | + }; |
|
| 390 | + |
|
| 391 | + // Print the button. |
|
| 392 | + $this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
| 393 | + |
|
| 394 | + } |
|
| 395 | + |
|
| 396 | + /** |
|
| 397 | + * Get the URI for the entity with the specified post id. |
|
| 398 | + * |
|
| 399 | + * @param int $post_id The entity post id. |
|
| 400 | + * |
|
| 401 | + * @return null|string The entity URI or NULL if not found or the dataset URI is not configured. |
|
| 402 | + * @since 3.6.0 |
|
| 403 | + * |
|
| 404 | + */ |
|
| 405 | + public function get_uri( $post_id ) { |
|
| 406 | + |
|
| 407 | + // If a null is given, nothing to do |
|
| 408 | + if ( is_null( $post_id ) ) { |
|
| 409 | + return null; |
|
| 410 | + } |
|
| 411 | + |
|
| 412 | + $dataset_uri = wl_configuration_get_redlink_dataset_uri(); |
|
| 413 | + |
|
| 414 | + // If the dataset uri is not properly configured, null is returned |
|
| 415 | + if ( empty( $dataset_uri ) ) { |
|
| 416 | + return null; |
|
| 417 | + } |
|
| 418 | + |
|
| 419 | + $uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true ); |
|
| 420 | + |
|
| 421 | + /* |
|
| 422 | 422 | * Consider the URI invalid if it doesn't start with the dataset URI. |
| 423 | 423 | * |
| 424 | 424 | * @see https://github.com/insideout10/wordlift-plugin/issues/996 |
| 425 | 425 | */ |
| 426 | - if ( 0 !== strpos( $uri, $dataset_uri ) ) { |
|
| 427 | - $uri = null; |
|
| 428 | - } |
|
| 429 | - |
|
| 430 | - // Set the URI if it isn't set yet. |
|
| 431 | - $post_status = get_post_status( $post_id ); |
|
| 432 | - if ( empty( $uri ) && 'auto-draft' !== $post_status && 'revision' !== $post_status ) { |
|
| 433 | - $uri = wl_build_entity_uri( $post_id ); |
|
| 434 | - wl_set_entity_uri( $post_id, $uri ); |
|
| 435 | - } |
|
| 436 | - |
|
| 437 | - return $uri; |
|
| 438 | - } |
|
| 439 | - |
|
| 440 | - |
|
| 441 | - /** |
|
| 442 | - * Get the alternative label input HTML code. |
|
| 443 | - * |
|
| 444 | - * @param string $value The input value. |
|
| 445 | - * |
|
| 446 | - * @return string The input HTML code. |
|
| 447 | - * @since 3.2.0 |
|
| 448 | - * |
|
| 449 | - */ |
|
| 450 | - private function get_alternative_label_input( $value = '' ) { |
|
| 451 | - |
|
| 452 | - return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) ); |
|
| 453 | - } |
|
| 454 | - |
|
| 455 | - /** |
|
| 456 | - * Get the number of entity posts published in this blog. |
|
| 457 | - * |
|
| 458 | - * @return int The number of published entity posts. |
|
| 459 | - * @since 3.6.0 |
|
| 460 | - * |
|
| 461 | - */ |
|
| 462 | - public function count() { |
|
| 463 | - global $wpdb; |
|
| 464 | - |
|
| 465 | - // Try to get the count from the transient. |
|
| 466 | - $count = get_transient( '_wl_entity_service__count' ); |
|
| 467 | - if ( false !== $count ) { |
|
| 468 | - return $count; |
|
| 469 | - } |
|
| 470 | - |
|
| 471 | - // Query the count. |
|
| 472 | - $count = $wpdb->get_var( $wpdb->prepare( |
|
| 473 | - "SELECT COUNT( DISTINCT( tr.object_id ) )" |
|
| 474 | - . " FROM {$wpdb->term_relationships} tr" |
|
| 475 | - . " INNER JOIN {$wpdb->term_taxonomy} tt" |
|
| 476 | - . " ON tt.taxonomy = %s AND tt.term_taxonomy_id = tr.term_taxonomy_id" |
|
| 477 | - . " INNER JOIN {$wpdb->terms} t" |
|
| 478 | - . " ON t.term_id = tt.term_id AND t.name != %s", |
|
| 479 | - Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 480 | - 'article' |
|
| 481 | - ) ); |
|
| 482 | - |
|
| 483 | - // Store the count in cache. |
|
| 484 | - set_transient( '_wl_entity_service__count', $count, 900 ); |
|
| 485 | - |
|
| 486 | - return $count; |
|
| 487 | - } |
|
| 488 | - |
|
| 489 | - /** |
|
| 490 | - * Add the entity filtering criterias to the arguments for a `get_posts` |
|
| 491 | - * call. |
|
| 492 | - * |
|
| 493 | - * @param array $args The arguments for a `get_posts` call. |
|
| 494 | - * |
|
| 495 | - * @return array The arguments for a `get_posts` call. |
|
| 496 | - * @since 3.15.0 |
|
| 497 | - * |
|
| 498 | - */ |
|
| 499 | - public static function add_criterias( $args ) { |
|
| 500 | - |
|
| 501 | - // Build an optimal tax-query. |
|
| 502 | - $tax_query = array( |
|
| 503 | - 'relation' => 'AND', |
|
| 504 | - array( |
|
| 505 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 506 | - 'operator' => 'EXISTS', |
|
| 507 | - ), |
|
| 508 | - array( |
|
| 509 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 510 | - 'field' => 'slug', |
|
| 511 | - 'terms' => 'article', |
|
| 512 | - 'operator' => 'NOT IN', |
|
| 513 | - ), |
|
| 514 | - ); |
|
| 515 | - |
|
| 516 | - return $args + array( |
|
| 517 | - 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 518 | - /* |
|
| 426 | + if ( 0 !== strpos( $uri, $dataset_uri ) ) { |
|
| 427 | + $uri = null; |
|
| 428 | + } |
|
| 429 | + |
|
| 430 | + // Set the URI if it isn't set yet. |
|
| 431 | + $post_status = get_post_status( $post_id ); |
|
| 432 | + if ( empty( $uri ) && 'auto-draft' !== $post_status && 'revision' !== $post_status ) { |
|
| 433 | + $uri = wl_build_entity_uri( $post_id ); |
|
| 434 | + wl_set_entity_uri( $post_id, $uri ); |
|
| 435 | + } |
|
| 436 | + |
|
| 437 | + return $uri; |
|
| 438 | + } |
|
| 439 | + |
|
| 440 | + |
|
| 441 | + /** |
|
| 442 | + * Get the alternative label input HTML code. |
|
| 443 | + * |
|
| 444 | + * @param string $value The input value. |
|
| 445 | + * |
|
| 446 | + * @return string The input HTML code. |
|
| 447 | + * @since 3.2.0 |
|
| 448 | + * |
|
| 449 | + */ |
|
| 450 | + private function get_alternative_label_input( $value = '' ) { |
|
| 451 | + |
|
| 452 | + return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) ); |
|
| 453 | + } |
|
| 454 | + |
|
| 455 | + /** |
|
| 456 | + * Get the number of entity posts published in this blog. |
|
| 457 | + * |
|
| 458 | + * @return int The number of published entity posts. |
|
| 459 | + * @since 3.6.0 |
|
| 460 | + * |
|
| 461 | + */ |
|
| 462 | + public function count() { |
|
| 463 | + global $wpdb; |
|
| 464 | + |
|
| 465 | + // Try to get the count from the transient. |
|
| 466 | + $count = get_transient( '_wl_entity_service__count' ); |
|
| 467 | + if ( false !== $count ) { |
|
| 468 | + return $count; |
|
| 469 | + } |
|
| 470 | + |
|
| 471 | + // Query the count. |
|
| 472 | + $count = $wpdb->get_var( $wpdb->prepare( |
|
| 473 | + "SELECT COUNT( DISTINCT( tr.object_id ) )" |
|
| 474 | + . " FROM {$wpdb->term_relationships} tr" |
|
| 475 | + . " INNER JOIN {$wpdb->term_taxonomy} tt" |
|
| 476 | + . " ON tt.taxonomy = %s AND tt.term_taxonomy_id = tr.term_taxonomy_id" |
|
| 477 | + . " INNER JOIN {$wpdb->terms} t" |
|
| 478 | + . " ON t.term_id = tt.term_id AND t.name != %s", |
|
| 479 | + Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 480 | + 'article' |
|
| 481 | + ) ); |
|
| 482 | + |
|
| 483 | + // Store the count in cache. |
|
| 484 | + set_transient( '_wl_entity_service__count', $count, 900 ); |
|
| 485 | + |
|
| 486 | + return $count; |
|
| 487 | + } |
|
| 488 | + |
|
| 489 | + /** |
|
| 490 | + * Add the entity filtering criterias to the arguments for a `get_posts` |
|
| 491 | + * call. |
|
| 492 | + * |
|
| 493 | + * @param array $args The arguments for a `get_posts` call. |
|
| 494 | + * |
|
| 495 | + * @return array The arguments for a `get_posts` call. |
|
| 496 | + * @since 3.15.0 |
|
| 497 | + * |
|
| 498 | + */ |
|
| 499 | + public static function add_criterias( $args ) { |
|
| 500 | + |
|
| 501 | + // Build an optimal tax-query. |
|
| 502 | + $tax_query = array( |
|
| 503 | + 'relation' => 'AND', |
|
| 504 | + array( |
|
| 505 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 506 | + 'operator' => 'EXISTS', |
|
| 507 | + ), |
|
| 508 | + array( |
|
| 509 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 510 | + 'field' => 'slug', |
|
| 511 | + 'terms' => 'article', |
|
| 512 | + 'operator' => 'NOT IN', |
|
| 513 | + ), |
|
| 514 | + ); |
|
| 515 | + |
|
| 516 | + return $args + array( |
|
| 517 | + 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 518 | + /* |
|
| 519 | 519 | * Ensure compatibility with Polylang. |
| 520 | 520 | * |
| 521 | 521 | * @see https://github.com/insideout10/wordlift-plugin/issues/855. |
@@ -523,102 +523,102 @@ discard block |
||
| 523 | 523 | * |
| 524 | 524 | * @since 3.19.5 |
| 525 | 525 | */ |
| 526 | - 'lang' => '', |
|
| 527 | - 'tax_query' => $tax_query, |
|
| 528 | - ); |
|
| 529 | - } |
|
| 530 | - |
|
| 531 | - /** |
|
| 532 | - * Create a new entity. |
|
| 533 | - * |
|
| 534 | - * @param string $name The entity name. |
|
| 535 | - * @param string $type_uri The entity's type URI. |
|
| 536 | - * @param null $logo The entity logo id (or NULL if none). |
|
| 537 | - * @param string $status The post status, by default 'publish'. |
|
| 538 | - * |
|
| 539 | - * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails. |
|
| 540 | - * @since 3.9.0 |
|
| 541 | - * |
|
| 542 | - */ |
|
| 543 | - public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
|
| 544 | - |
|
| 545 | - // Create an entity for the publisher. |
|
| 546 | - $post_id = @wp_insert_post( array( |
|
| 547 | - 'post_type' => self::TYPE_NAME, |
|
| 548 | - 'post_title' => $name, |
|
| 549 | - 'post_status' => $status, |
|
| 550 | - 'post_content' => '', |
|
| 551 | - ) ); |
|
| 552 | - |
|
| 553 | - // Return the error if any. |
|
| 554 | - if ( is_wp_error( $post_id ) ) { |
|
| 555 | - return $post_id; |
|
| 556 | - } |
|
| 557 | - |
|
| 558 | - // Set the entity logo. |
|
| 559 | - if ( $logo && is_numeric( $logo ) ) { |
|
| 560 | - set_post_thumbnail( $post_id, $logo ); |
|
| 561 | - } |
|
| 562 | - |
|
| 563 | - // Set the entity type. |
|
| 564 | - Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri ); |
|
| 565 | - |
|
| 566 | - return $post_id; |
|
| 567 | - } |
|
| 568 | - |
|
| 569 | - /** |
|
| 570 | - * Get the entities related to the one with the specified id. By default only |
|
| 571 | - * published entities will be returned. |
|
| 572 | - * |
|
| 573 | - * @param int $id The post id. |
|
| 574 | - * @param string $post_status The target post status (default = publish). |
|
| 575 | - * |
|
| 576 | - * @return array An array of post ids. |
|
| 577 | - * @since 3.10.0 |
|
| 578 | - * |
|
| 579 | - */ |
|
| 580 | - public function get_related_entities( $id, $post_status = 'publish' ) { |
|
| 581 | - |
|
| 582 | - return $this->relation_service->get_objects( $id, 'ids', null, $post_status ); |
|
| 583 | - } |
|
| 584 | - |
|
| 585 | - /** |
|
| 586 | - * Get the list of entities. |
|
| 587 | - * |
|
| 588 | - * @param array $params Custom parameters for WordPress' own {@link get_posts} function. |
|
| 589 | - * |
|
| 590 | - * @return array An array of entity posts. |
|
| 591 | - * @since 3.12.2 |
|
| 592 | - * |
|
| 593 | - */ |
|
| 594 | - public function get( $params = array() ) { |
|
| 595 | - |
|
| 596 | - // Set the defaults. |
|
| 597 | - $defaults = array( 'post_type' => 'entity' ); |
|
| 598 | - |
|
| 599 | - // Merge the defaults with the provided parameters. |
|
| 600 | - $args = wp_parse_args( $params, $defaults ); |
|
| 601 | - |
|
| 602 | - // Call the `get_posts` function. |
|
| 603 | - return get_posts( $args ); |
|
| 604 | - } |
|
| 605 | - |
|
| 606 | - /** |
|
| 607 | - * The list of post type names which can be used for entities |
|
| 608 | - * |
|
| 609 | - * Criteria is that the post type is public. The list of valid post types |
|
| 610 | - * can be overridden with a filter. |
|
| 611 | - * |
|
| 612 | - * @return array Array containing the names of the valid post types. |
|
| 613 | - * @since 3.15.0 |
|
| 614 | - * |
|
| 615 | - */ |
|
| 616 | - static function valid_entity_post_types() { |
|
| 617 | - |
|
| 618 | - // Ignore builtins in the call to avoid getting attachments. |
|
| 619 | - $post_types = array( 'post', 'page', self::TYPE_NAME ); |
|
| 620 | - |
|
| 621 | - return apply_filters( 'wl_valid_entity_post_types', $post_types ); |
|
| 622 | - } |
|
| 526 | + 'lang' => '', |
|
| 527 | + 'tax_query' => $tax_query, |
|
| 528 | + ); |
|
| 529 | + } |
|
| 530 | + |
|
| 531 | + /** |
|
| 532 | + * Create a new entity. |
|
| 533 | + * |
|
| 534 | + * @param string $name The entity name. |
|
| 535 | + * @param string $type_uri The entity's type URI. |
|
| 536 | + * @param null $logo The entity logo id (or NULL if none). |
|
| 537 | + * @param string $status The post status, by default 'publish'. |
|
| 538 | + * |
|
| 539 | + * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails. |
|
| 540 | + * @since 3.9.0 |
|
| 541 | + * |
|
| 542 | + */ |
|
| 543 | + public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
|
| 544 | + |
|
| 545 | + // Create an entity for the publisher. |
|
| 546 | + $post_id = @wp_insert_post( array( |
|
| 547 | + 'post_type' => self::TYPE_NAME, |
|
| 548 | + 'post_title' => $name, |
|
| 549 | + 'post_status' => $status, |
|
| 550 | + 'post_content' => '', |
|
| 551 | + ) ); |
|
| 552 | + |
|
| 553 | + // Return the error if any. |
|
| 554 | + if ( is_wp_error( $post_id ) ) { |
|
| 555 | + return $post_id; |
|
| 556 | + } |
|
| 557 | + |
|
| 558 | + // Set the entity logo. |
|
| 559 | + if ( $logo && is_numeric( $logo ) ) { |
|
| 560 | + set_post_thumbnail( $post_id, $logo ); |
|
| 561 | + } |
|
| 562 | + |
|
| 563 | + // Set the entity type. |
|
| 564 | + Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri ); |
|
| 565 | + |
|
| 566 | + return $post_id; |
|
| 567 | + } |
|
| 568 | + |
|
| 569 | + /** |
|
| 570 | + * Get the entities related to the one with the specified id. By default only |
|
| 571 | + * published entities will be returned. |
|
| 572 | + * |
|
| 573 | + * @param int $id The post id. |
|
| 574 | + * @param string $post_status The target post status (default = publish). |
|
| 575 | + * |
|
| 576 | + * @return array An array of post ids. |
|
| 577 | + * @since 3.10.0 |
|
| 578 | + * |
|
| 579 | + */ |
|
| 580 | + public function get_related_entities( $id, $post_status = 'publish' ) { |
|
| 581 | + |
|
| 582 | + return $this->relation_service->get_objects( $id, 'ids', null, $post_status ); |
|
| 583 | + } |
|
| 584 | + |
|
| 585 | + /** |
|
| 586 | + * Get the list of entities. |
|
| 587 | + * |
|
| 588 | + * @param array $params Custom parameters for WordPress' own {@link get_posts} function. |
|
| 589 | + * |
|
| 590 | + * @return array An array of entity posts. |
|
| 591 | + * @since 3.12.2 |
|
| 592 | + * |
|
| 593 | + */ |
|
| 594 | + public function get( $params = array() ) { |
|
| 595 | + |
|
| 596 | + // Set the defaults. |
|
| 597 | + $defaults = array( 'post_type' => 'entity' ); |
|
| 598 | + |
|
| 599 | + // Merge the defaults with the provided parameters. |
|
| 600 | + $args = wp_parse_args( $params, $defaults ); |
|
| 601 | + |
|
| 602 | + // Call the `get_posts` function. |
|
| 603 | + return get_posts( $args ); |
|
| 604 | + } |
|
| 605 | + |
|
| 606 | + /** |
|
| 607 | + * The list of post type names which can be used for entities |
|
| 608 | + * |
|
| 609 | + * Criteria is that the post type is public. The list of valid post types |
|
| 610 | + * can be overridden with a filter. |
|
| 611 | + * |
|
| 612 | + * @return array Array containing the names of the valid post types. |
|
| 613 | + * @since 3.15.0 |
|
| 614 | + * |
|
| 615 | + */ |
|
| 616 | + static function valid_entity_post_types() { |
|
| 617 | + |
|
| 618 | + // Ignore builtins in the call to avoid getting attachments. |
|
| 619 | + $post_types = array( 'post', 'page', self::TYPE_NAME ); |
|
| 620 | + |
|
| 621 | + return apply_filters( 'wl_valid_entity_post_types', $post_types ); |
|
| 622 | + } |
|
| 623 | 623 | |
| 624 | 624 | } |
@@ -97,9 +97,9 @@ discard block |
||
| 97 | 97 | * @since 3.2.0 |
| 98 | 98 | * |
| 99 | 99 | */ |
| 100 | - public function __construct( $ui_service, $relation_service, $entity_uri_service ) { |
|
| 100 | + public function __construct($ui_service, $relation_service, $entity_uri_service) { |
|
| 101 | 101 | |
| 102 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
| 102 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Entity_Service'); |
|
| 103 | 103 | |
| 104 | 104 | $this->ui_service = $ui_service; |
| 105 | 105 | $this->relation_service = $relation_service; |
@@ -130,17 +130,17 @@ discard block |
||
| 130 | 130 | * @since 3.1.0 |
| 131 | 131 | * |
| 132 | 132 | */ |
| 133 | - public function is_entity( $post_id ) { |
|
| 133 | + public function is_entity($post_id) { |
|
| 134 | 134 | |
| 135 | - $terms = wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 135 | + $terms = wp_get_object_terms($post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME); |
|
| 136 | 136 | |
| 137 | - if ( is_wp_error( $terms ) ) { |
|
| 138 | - $this->log->error( "Cannot get the terms for post $post_id: " . $terms->get_error_message() ); |
|
| 137 | + if (is_wp_error($terms)) { |
|
| 138 | + $this->log->error("Cannot get the terms for post $post_id: ".$terms->get_error_message()); |
|
| 139 | 139 | |
| 140 | 140 | return false; |
| 141 | 141 | } |
| 142 | 142 | |
| 143 | - if ( empty( $terms ) ) { |
|
| 143 | + if (empty($terms)) { |
|
| 144 | 144 | return false; |
| 145 | 145 | } |
| 146 | 146 | |
@@ -151,8 +151,8 @@ discard block |
||
| 151 | 151 | * |
| 152 | 152 | * @see https://github.com/insideout10/wordlift-plugin/issues/835 |
| 153 | 153 | */ |
| 154 | - foreach ( $terms as $term ) { |
|
| 155 | - if ( 'article' !== $term->slug ) { |
|
| 154 | + foreach ($terms as $term) { |
|
| 155 | + if ('article' !== $term->slug) { |
|
| 156 | 156 | return true; |
| 157 | 157 | } |
| 158 | 158 | } |
@@ -172,19 +172,19 @@ discard block |
||
| 172 | 172 | * @since 3.5.0 |
| 173 | 173 | * |
| 174 | 174 | */ |
| 175 | - public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) { |
|
| 175 | + public function get_classification_scope_for($post_id, $default = WL_WHAT_RELATION) { |
|
| 176 | 176 | |
| 177 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 177 | + if (false === $this->is_entity($post_id)) { |
|
| 178 | 178 | return $default; |
| 179 | 179 | } |
| 180 | 180 | |
| 181 | 181 | // Retrieve the entity type |
| 182 | - $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get( $post_id ); |
|
| 183 | - $entity_type = str_replace( 'wl-', '', $entity_type_arr['css_class'] ); |
|
| 182 | + $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get($post_id); |
|
| 183 | + $entity_type = str_replace('wl-', '', $entity_type_arr['css_class']); |
|
| 184 | 184 | // Retrieve classification boxes configuration |
| 185 | - $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
| 186 | - foreach ( $classification_boxes as $cb ) { |
|
| 187 | - if ( in_array( $entity_type, $cb['registeredTypes'] ) ) { |
|
| 185 | + $classification_boxes = unserialize(WL_CORE_POST_CLASSIFICATION_BOXES); |
|
| 186 | + foreach ($classification_boxes as $cb) { |
|
| 187 | + if (in_array($entity_type, $cb['registeredTypes'])) { |
|
| 188 | 188 | return $cb['id']; |
| 189 | 189 | } |
| 190 | 190 | } |
@@ -199,13 +199,13 @@ discard block |
||
| 199 | 199 | * |
| 200 | 200 | * @return bool|null Null if it's not an entity, otherwise true if it's used. |
| 201 | 201 | */ |
| 202 | - public function is_used( $post_id ) { |
|
| 202 | + public function is_used($post_id) { |
|
| 203 | 203 | |
| 204 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 204 | + if (false === $this->is_entity($post_id)) { |
|
| 205 | 205 | return null; |
| 206 | 206 | } |
| 207 | 207 | // Retrieve the post |
| 208 | - $entity = get_post( $post_id ); |
|
| 208 | + $entity = get_post($post_id); |
|
| 209 | 209 | |
| 210 | 210 | global $wpdb; |
| 211 | 211 | // Retrieve Wordlift relation instances table name |
@@ -218,9 +218,9 @@ discard block |
||
| 218 | 218 | ); |
| 219 | 219 | |
| 220 | 220 | // Perform the query |
| 221 | - $relation_instances = (int) $wpdb->get_var( $stmt ); |
|
| 221 | + $relation_instances = (int) $wpdb->get_var($stmt); |
|
| 222 | 222 | // If there is at least one relation instance for the current entity, then it's used |
| 223 | - if ( 0 < $relation_instances ) { |
|
| 223 | + if (0 < $relation_instances) { |
|
| 224 | 224 | return true; |
| 225 | 225 | } |
| 226 | 226 | |
@@ -228,13 +228,13 @@ discard block |
||
| 228 | 228 | $stmt = $wpdb->prepare( |
| 229 | 229 | "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s", |
| 230 | 230 | $entity->ID, |
| 231 | - wl_get_entity_uri( $entity->ID ) |
|
| 231 | + wl_get_entity_uri($entity->ID) |
|
| 232 | 232 | ); |
| 233 | 233 | // Perform the query |
| 234 | - $meta_instances = (int) $wpdb->get_var( $stmt ); |
|
| 234 | + $meta_instances = (int) $wpdb->get_var($stmt); |
|
| 235 | 235 | |
| 236 | 236 | // If there is at least one meta that refers the current entity uri, then current entity is used |
| 237 | - if ( 0 < $meta_instances ) { |
|
| 237 | + if (0 < $meta_instances) { |
|
| 238 | 238 | return true; |
| 239 | 239 | } |
| 240 | 240 | |
@@ -254,9 +254,9 @@ discard block |
||
| 254 | 254 | * @since 3.2.0 |
| 255 | 255 | * |
| 256 | 256 | */ |
| 257 | - public function get_entity_post_by_uri( $uri ) { |
|
| 257 | + public function get_entity_post_by_uri($uri) { |
|
| 258 | 258 | |
| 259 | - return $this->entity_uri_service->get_entity( $uri ); |
|
| 259 | + return $this->entity_uri_service->get_entity($uri); |
|
| 260 | 260 | } |
| 261 | 261 | |
| 262 | 262 | /** |
@@ -271,10 +271,10 @@ discard block |
||
| 271 | 271 | * @param WP_Post $post Post object. |
| 272 | 272 | * @param bool $update Whether this is an existing post being updated or not. |
| 273 | 273 | */ |
| 274 | - public function save_post( $post_id, $post, $update ) { |
|
| 274 | + public function save_post($post_id, $post, $update) { |
|
| 275 | 275 | |
| 276 | 276 | // Avoid doing anything if post is autosave or a revision. |
| 277 | - if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { |
|
| 277 | + if (wp_is_post_autosave($post) || wp_is_post_revision($post)) { |
|
| 278 | 278 | return; |
| 279 | 279 | } |
| 280 | 280 | |
@@ -284,16 +284,16 @@ discard block |
||
| 284 | 284 | // the $post_id in the save_post call matches the post id set in the request. |
| 285 | 285 | // |
| 286 | 286 | // If this is not the current post being saved or if it's not an entity, return. |
| 287 | - if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) { |
|
| 287 | + if ( ! isset($_REQUEST['post_ID']) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity($post_id)) { |
|
| 288 | 288 | return; |
| 289 | 289 | } |
| 290 | 290 | |
| 291 | 291 | // Get the alt labels from the request (or empty array). |
| 292 | - $alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? $_REQUEST['wl_alternative_label'] : array(); |
|
| 292 | + $alt_labels = isset($_REQUEST['wl_alternative_label']) ? $_REQUEST['wl_alternative_label'] : array(); |
|
| 293 | 293 | |
| 294 | - if ( ( ! empty( $_POST['content'] ) && ! empty( $_POST['post_content'] ) ) || isset( $_REQUEST['wl_alternative_label'] ) ) { |
|
| 294 | + if (( ! empty($_POST['content']) && ! empty($_POST['post_content'])) || isset($_REQUEST['wl_alternative_label'])) { |
|
| 295 | 295 | // This is via classic editor, so set the alternative labels. |
| 296 | - $this->set_alternative_labels( $post_id, $alt_labels ); |
|
| 296 | + $this->set_alternative_labels($post_id, $alt_labels); |
|
| 297 | 297 | } |
| 298 | 298 | |
| 299 | 299 | |
@@ -308,28 +308,28 @@ discard block |
||
| 308 | 308 | * @since 3.2.0 |
| 309 | 309 | * |
| 310 | 310 | */ |
| 311 | - public function set_alternative_labels( $post_id, $alt_labels ) { |
|
| 311 | + public function set_alternative_labels($post_id, $alt_labels) { |
|
| 312 | 312 | |
| 313 | 313 | // Bail out if post id is not numeric. We add this check as we found a WP install that was sending a WP_Error |
| 314 | 314 | // instead of post id. |
| 315 | - if ( ! is_numeric( $post_id ) ) { |
|
| 315 | + if ( ! is_numeric($post_id)) { |
|
| 316 | 316 | return; |
| 317 | 317 | } |
| 318 | 318 | |
| 319 | 319 | // Force $alt_labels to be an array |
| 320 | - if ( ! is_array( $alt_labels ) ) { |
|
| 321 | - $alt_labels = array( $alt_labels ); |
|
| 320 | + if ( ! is_array($alt_labels)) { |
|
| 321 | + $alt_labels = array($alt_labels); |
|
| 322 | 322 | } |
| 323 | 323 | |
| 324 | - $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" ); |
|
| 324 | + $this->log->debug("Setting alternative labels [ post id :: $post_id ][ alt labels :: ".implode(',', $alt_labels)." ]"); |
|
| 325 | 325 | |
| 326 | 326 | // Delete all the existing alternate labels. |
| 327 | - delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 327 | + delete_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY); |
|
| 328 | 328 | |
| 329 | 329 | // Set the alternative labels. |
| 330 | - foreach ( $alt_labels as $alt_label ) { |
|
| 331 | - if ( ! empty( $alt_label ) ) { |
|
| 332 | - add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label ); |
|
| 330 | + foreach ($alt_labels as $alt_label) { |
|
| 331 | + if ( ! empty($alt_label)) { |
|
| 332 | + add_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label); |
|
| 333 | 333 | } |
| 334 | 334 | } |
| 335 | 335 | |
@@ -344,9 +344,9 @@ discard block |
||
| 344 | 344 | * @since 3.2.0 |
| 345 | 345 | * |
| 346 | 346 | */ |
| 347 | - public function get_alternative_labels( $post_id ) { |
|
| 347 | + public function get_alternative_labels($post_id) { |
|
| 348 | 348 | |
| 349 | - return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 349 | + return get_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY); |
|
| 350 | 350 | } |
| 351 | 351 | |
| 352 | 352 | /** |
@@ -358,9 +358,9 @@ discard block |
||
| 358 | 358 | * @since 3.12.0 |
| 359 | 359 | * |
| 360 | 360 | */ |
| 361 | - public function get_labels( $post_id ) { |
|
| 361 | + public function get_labels($post_id) { |
|
| 362 | 362 | |
| 363 | - return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) ); |
|
| 363 | + return array_merge((array) get_the_title($post_id), $this->get_alternative_labels($post_id)); |
|
| 364 | 364 | } |
| 365 | 365 | |
| 366 | 366 | /** |
@@ -371,25 +371,25 @@ discard block |
||
| 371 | 371 | * @since 3.2.0 |
| 372 | 372 | * |
| 373 | 373 | */ |
| 374 | - public function edit_form_before_permalink( $post ) { |
|
| 374 | + public function edit_form_before_permalink($post) { |
|
| 375 | 375 | |
| 376 | 376 | // If it's not an entity, return. |
| 377 | - if ( ! $this->is_entity( $post->ID ) ) { |
|
| 377 | + if ( ! $this->is_entity($post->ID)) { |
|
| 378 | 378 | return; |
| 379 | 379 | } |
| 380 | 380 | |
| 381 | 381 | // Print the input template. |
| 382 | - $this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
| 382 | + $this->ui_service->print_template('wl-tmpl-alternative-label-input', $this->get_alternative_label_input()); |
|
| 383 | 383 | |
| 384 | 384 | // Print all the currently set alternative labels. |
| 385 | - foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) { |
|
| 385 | + foreach ($this->get_alternative_labels($post->ID) as $alt_label) { |
|
| 386 | 386 | |
| 387 | - echo $this->get_alternative_label_input( $alt_label ); |
|
| 387 | + echo $this->get_alternative_label_input($alt_label); |
|
| 388 | 388 | |
| 389 | 389 | }; |
| 390 | 390 | |
| 391 | 391 | // Print the button. |
| 392 | - $this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
| 392 | + $this->ui_service->print_button('wl-add-alternative-labels-button', __('Add more titles', 'wordlift')); |
|
| 393 | 393 | |
| 394 | 394 | } |
| 395 | 395 | |
@@ -402,36 +402,36 @@ discard block |
||
| 402 | 402 | * @since 3.6.0 |
| 403 | 403 | * |
| 404 | 404 | */ |
| 405 | - public function get_uri( $post_id ) { |
|
| 405 | + public function get_uri($post_id) { |
|
| 406 | 406 | |
| 407 | 407 | // If a null is given, nothing to do |
| 408 | - if ( is_null( $post_id ) ) { |
|
| 408 | + if (is_null($post_id)) { |
|
| 409 | 409 | return null; |
| 410 | 410 | } |
| 411 | 411 | |
| 412 | 412 | $dataset_uri = wl_configuration_get_redlink_dataset_uri(); |
| 413 | 413 | |
| 414 | 414 | // If the dataset uri is not properly configured, null is returned |
| 415 | - if ( empty( $dataset_uri ) ) { |
|
| 415 | + if (empty($dataset_uri)) { |
|
| 416 | 416 | return null; |
| 417 | 417 | } |
| 418 | 418 | |
| 419 | - $uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true ); |
|
| 419 | + $uri = get_post_meta($post_id, WL_ENTITY_URL_META_NAME, true); |
|
| 420 | 420 | |
| 421 | 421 | /* |
| 422 | 422 | * Consider the URI invalid if it doesn't start with the dataset URI. |
| 423 | 423 | * |
| 424 | 424 | * @see https://github.com/insideout10/wordlift-plugin/issues/996 |
| 425 | 425 | */ |
| 426 | - if ( 0 !== strpos( $uri, $dataset_uri ) ) { |
|
| 426 | + if (0 !== strpos($uri, $dataset_uri)) { |
|
| 427 | 427 | $uri = null; |
| 428 | 428 | } |
| 429 | 429 | |
| 430 | 430 | // Set the URI if it isn't set yet. |
| 431 | - $post_status = get_post_status( $post_id ); |
|
| 432 | - if ( empty( $uri ) && 'auto-draft' !== $post_status && 'revision' !== $post_status ) { |
|
| 433 | - $uri = wl_build_entity_uri( $post_id ); |
|
| 434 | - wl_set_entity_uri( $post_id, $uri ); |
|
| 431 | + $post_status = get_post_status($post_id); |
|
| 432 | + if (empty($uri) && 'auto-draft' !== $post_status && 'revision' !== $post_status) { |
|
| 433 | + $uri = wl_build_entity_uri($post_id); |
|
| 434 | + wl_set_entity_uri($post_id, $uri); |
|
| 435 | 435 | } |
| 436 | 436 | |
| 437 | 437 | return $uri; |
@@ -447,9 +447,9 @@ discard block |
||
| 447 | 447 | * @since 3.2.0 |
| 448 | 448 | * |
| 449 | 449 | */ |
| 450 | - private function get_alternative_label_input( $value = '' ) { |
|
| 450 | + private function get_alternative_label_input($value = '') { |
|
| 451 | 451 | |
| 452 | - return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) ); |
|
| 452 | + return sprintf(self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr($value), __('Delete', 'wordlift')); |
|
| 453 | 453 | } |
| 454 | 454 | |
| 455 | 455 | /** |
@@ -463,13 +463,13 @@ discard block |
||
| 463 | 463 | global $wpdb; |
| 464 | 464 | |
| 465 | 465 | // Try to get the count from the transient. |
| 466 | - $count = get_transient( '_wl_entity_service__count' ); |
|
| 467 | - if ( false !== $count ) { |
|
| 466 | + $count = get_transient('_wl_entity_service__count'); |
|
| 467 | + if (false !== $count) { |
|
| 468 | 468 | return $count; |
| 469 | 469 | } |
| 470 | 470 | |
| 471 | 471 | // Query the count. |
| 472 | - $count = $wpdb->get_var( $wpdb->prepare( |
|
| 472 | + $count = $wpdb->get_var($wpdb->prepare( |
|
| 473 | 473 | "SELECT COUNT( DISTINCT( tr.object_id ) )" |
| 474 | 474 | . " FROM {$wpdb->term_relationships} tr" |
| 475 | 475 | . " INNER JOIN {$wpdb->term_taxonomy} tt" |
@@ -478,10 +478,10 @@ discard block |
||
| 478 | 478 | . " ON t.term_id = tt.term_id AND t.name != %s", |
| 479 | 479 | Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
| 480 | 480 | 'article' |
| 481 | - ) ); |
|
| 481 | + )); |
|
| 482 | 482 | |
| 483 | 483 | // Store the count in cache. |
| 484 | - set_transient( '_wl_entity_service__count', $count, 900 ); |
|
| 484 | + set_transient('_wl_entity_service__count', $count, 900); |
|
| 485 | 485 | |
| 486 | 486 | return $count; |
| 487 | 487 | } |
@@ -496,7 +496,7 @@ discard block |
||
| 496 | 496 | * @since 3.15.0 |
| 497 | 497 | * |
| 498 | 498 | */ |
| 499 | - public static function add_criterias( $args ) { |
|
| 499 | + public static function add_criterias($args) { |
|
| 500 | 500 | |
| 501 | 501 | // Build an optimal tax-query. |
| 502 | 502 | $tax_query = array( |
@@ -540,28 +540,28 @@ discard block |
||
| 540 | 540 | * @since 3.9.0 |
| 541 | 541 | * |
| 542 | 542 | */ |
| 543 | - public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
|
| 543 | + public function create($name, $type_uri, $logo = null, $status = 'publish') { |
|
| 544 | 544 | |
| 545 | 545 | // Create an entity for the publisher. |
| 546 | - $post_id = @wp_insert_post( array( |
|
| 546 | + $post_id = @wp_insert_post(array( |
|
| 547 | 547 | 'post_type' => self::TYPE_NAME, |
| 548 | 548 | 'post_title' => $name, |
| 549 | 549 | 'post_status' => $status, |
| 550 | 550 | 'post_content' => '', |
| 551 | - ) ); |
|
| 551 | + )); |
|
| 552 | 552 | |
| 553 | 553 | // Return the error if any. |
| 554 | - if ( is_wp_error( $post_id ) ) { |
|
| 554 | + if (is_wp_error($post_id)) { |
|
| 555 | 555 | return $post_id; |
| 556 | 556 | } |
| 557 | 557 | |
| 558 | 558 | // Set the entity logo. |
| 559 | - if ( $logo && is_numeric( $logo ) ) { |
|
| 560 | - set_post_thumbnail( $post_id, $logo ); |
|
| 559 | + if ($logo && is_numeric($logo)) { |
|
| 560 | + set_post_thumbnail($post_id, $logo); |
|
| 561 | 561 | } |
| 562 | 562 | |
| 563 | 563 | // Set the entity type. |
| 564 | - Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri ); |
|
| 564 | + Wordlift_Entity_Type_Service::get_instance()->set($post_id, $type_uri); |
|
| 565 | 565 | |
| 566 | 566 | return $post_id; |
| 567 | 567 | } |
@@ -577,9 +577,9 @@ discard block |
||
| 577 | 577 | * @since 3.10.0 |
| 578 | 578 | * |
| 579 | 579 | */ |
| 580 | - public function get_related_entities( $id, $post_status = 'publish' ) { |
|
| 580 | + public function get_related_entities($id, $post_status = 'publish') { |
|
| 581 | 581 | |
| 582 | - return $this->relation_service->get_objects( $id, 'ids', null, $post_status ); |
|
| 582 | + return $this->relation_service->get_objects($id, 'ids', null, $post_status); |
|
| 583 | 583 | } |
| 584 | 584 | |
| 585 | 585 | /** |
@@ -591,16 +591,16 @@ discard block |
||
| 591 | 591 | * @since 3.12.2 |
| 592 | 592 | * |
| 593 | 593 | */ |
| 594 | - public function get( $params = array() ) { |
|
| 594 | + public function get($params = array()) { |
|
| 595 | 595 | |
| 596 | 596 | // Set the defaults. |
| 597 | - $defaults = array( 'post_type' => 'entity' ); |
|
| 597 | + $defaults = array('post_type' => 'entity'); |
|
| 598 | 598 | |
| 599 | 599 | // Merge the defaults with the provided parameters. |
| 600 | - $args = wp_parse_args( $params, $defaults ); |
|
| 600 | + $args = wp_parse_args($params, $defaults); |
|
| 601 | 601 | |
| 602 | 602 | // Call the `get_posts` function. |
| 603 | - return get_posts( $args ); |
|
| 603 | + return get_posts($args); |
|
| 604 | 604 | } |
| 605 | 605 | |
| 606 | 606 | /** |
@@ -616,9 +616,9 @@ discard block |
||
| 616 | 616 | static function valid_entity_post_types() { |
| 617 | 617 | |
| 618 | 618 | // Ignore builtins in the call to avoid getting attachments. |
| 619 | - $post_types = array( 'post', 'page', self::TYPE_NAME ); |
|
| 619 | + $post_types = array('post', 'page', self::TYPE_NAME); |
|
| 620 | 620 | |
| 621 | - return apply_filters( 'wl_valid_entity_post_types', $post_types ); |
|
| 621 | + return apply_filters('wl_valid_entity_post_types', $post_types); |
|
| 622 | 622 | } |
| 623 | 623 | |
| 624 | 624 | } |