@@ -3,20 +3,20 @@ |
||
| 3 | 3 | |
| 4 | 4 | class Terms_Compat { |
| 5 | 5 | |
| 6 | - public static function get_terms( $taxonomy, $args ) { |
|
| 7 | - global $wp_version; |
|
| 6 | + public static function get_terms( $taxonomy, $args ) { |
|
| 7 | + global $wp_version; |
|
| 8 | 8 | |
| 9 | - if ( version_compare( $wp_version, '4.5', '<' ) ) { |
|
| 10 | - return get_terms( $taxonomy, $args ); |
|
| 11 | - } else { |
|
| 12 | - $args['taxonomy'] = $taxonomy; |
|
| 13 | - return get_terms( $args ); |
|
| 14 | - } |
|
| 15 | - } |
|
| 9 | + if ( version_compare( $wp_version, '4.5', '<' ) ) { |
|
| 10 | + return get_terms( $taxonomy, $args ); |
|
| 11 | + } else { |
|
| 12 | + $args['taxonomy'] = $taxonomy; |
|
| 13 | + return get_terms( $args ); |
|
| 14 | + } |
|
| 15 | + } |
|
| 16 | 16 | |
| 17 | - public static function get_public_taxonomies() { |
|
| 18 | - return get_taxonomies( array( 'public' => true ) ); |
|
| 19 | - } |
|
| 17 | + public static function get_public_taxonomies() { |
|
| 18 | + return get_taxonomies( array( 'public' => true ) ); |
|
| 19 | + } |
|
| 20 | 20 | |
| 21 | 21 | |
| 22 | 22 | } |
| 23 | 23 | \ No newline at end of file |
@@ -3,19 +3,19 @@ |
||
| 3 | 3 | |
| 4 | 4 | class Terms_Compat { |
| 5 | 5 | |
| 6 | - public static function get_terms( $taxonomy, $args ) { |
|
| 6 | + public static function get_terms($taxonomy, $args) { |
|
| 7 | 7 | global $wp_version; |
| 8 | 8 | |
| 9 | - if ( version_compare( $wp_version, '4.5', '<' ) ) { |
|
| 10 | - return get_terms( $taxonomy, $args ); |
|
| 9 | + if (version_compare($wp_version, '4.5', '<')) { |
|
| 10 | + return get_terms($taxonomy, $args); |
|
| 11 | 11 | } else { |
| 12 | 12 | $args['taxonomy'] = $taxonomy; |
| 13 | - return get_terms( $args ); |
|
| 13 | + return get_terms($args); |
|
| 14 | 14 | } |
| 15 | 15 | } |
| 16 | 16 | |
| 17 | 17 | public static function get_public_taxonomies() { |
| 18 | - return get_taxonomies( array( 'public' => true ) ); |
|
| 18 | + return get_taxonomies(array('public' => true)); |
|
| 19 | 19 | } |
| 20 | 20 | |
| 21 | 21 | |
@@ -13,7 +13,7 @@ discard block |
||
| 13 | 13 | use Wordlift\Api\Default_Api_Service; |
| 14 | 14 | |
| 15 | 15 | if ( ! defined( 'ABSPATH' ) ) { |
| 16 | - exit; |
|
| 16 | + exit; |
|
| 17 | 17 | } |
| 18 | 18 | |
| 19 | 19 | /** |
@@ -23,720 +23,720 @@ discard block |
||
| 23 | 23 | */ |
| 24 | 24 | class Wordlift_Configuration_Service { |
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * The entity base path option name. |
|
| 28 | - * |
|
| 29 | - * @since 3.6.0 |
|
| 30 | - */ |
|
| 31 | - const ENTITY_BASE_PATH_KEY = 'wl_entity_base_path'; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * The skip wizard (admin installation wizard) option name. |
|
| 35 | - * |
|
| 36 | - * @since 3.9.0 |
|
| 37 | - */ |
|
| 38 | - const SKIP_WIZARD = 'wl_skip_wizard'; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * WordLift's key option name. |
|
| 42 | - * |
|
| 43 | - * @since 3.9.0 |
|
| 44 | - */ |
|
| 45 | - const KEY = 'key'; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * WordLift's configured language option name. |
|
| 49 | - * |
|
| 50 | - * @since 3.9.0 |
|
| 51 | - */ |
|
| 52 | - const LANGUAGE = 'site_language'; |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * WordLift's configured country code. |
|
| 56 | - * |
|
| 57 | - * @since 3.18.0 |
|
| 58 | - */ |
|
| 59 | - const COUNTRY_CODE = 'country_code'; |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * The publisher entity post ID option name. |
|
| 63 | - * |
|
| 64 | - * @since 3.9.0 |
|
| 65 | - */ |
|
| 66 | - const PUBLISHER_ID = 'publisher_id'; |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * The dataset URI option name |
|
| 70 | - * |
|
| 71 | - * @since 3.10.0 |
|
| 72 | - */ |
|
| 73 | - const DATASET_URI = 'redlink_dataset_uri'; |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * The link by default option name. |
|
| 77 | - * |
|
| 78 | - * @since 3.11.0 |
|
| 79 | - */ |
|
| 80 | - const LINK_BY_DEFAULT = 'link_by_default'; |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * The analytics enable option. |
|
| 84 | - * |
|
| 85 | - * @since 3.21.0 |
|
| 86 | - */ |
|
| 87 | - const ANALYTICS_ENABLE = 'analytics_enable'; |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * The analytics entity uri dimension option. |
|
| 91 | - * |
|
| 92 | - * @since 3.21.0 |
|
| 93 | - */ |
|
| 94 | - const ANALYTICS_ENTITY_URI_DIMENSION = 'analytics_entity_uri_dimension'; |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * The analytics entity type dimension option. |
|
| 98 | - * |
|
| 99 | - * @since 3.21.0 |
|
| 100 | - */ |
|
| 101 | - const ANALYTICS_ENTITY_TYPE_DIMENSION = 'analytics_entity_type_dimension'; |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * The user preferences about sharing data option. |
|
| 105 | - * |
|
| 106 | - * @since 3.19.0 |
|
| 107 | - */ |
|
| 108 | - const SEND_DIAGNOSTIC = 'send_diagnostic'; |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * The package type configuration key. |
|
| 112 | - * |
|
| 113 | - * @since 3.20.0 |
|
| 114 | - */ |
|
| 115 | - const PACKAGE_TYPE = 'package_type'; |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * The {@link Wordlift_Log_Service} instance. |
|
| 119 | - * |
|
| 120 | - * @since 3.16.0 |
|
| 121 | - * |
|
| 122 | - * @var \Wordlift_Log_Service $log The {@link Wordlift_Log_Service} instance. |
|
| 123 | - */ |
|
| 124 | - private $log; |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * The Wordlift_Configuration_Service's singleton instance. |
|
| 128 | - * |
|
| 129 | - * @since 3.6.0 |
|
| 130 | - * |
|
| 131 | - * @access private |
|
| 132 | - * @var \Wordlift_Configuration_Service $instance Wordlift_Configuration_Service's singleton instance. |
|
| 133 | - */ |
|
| 134 | - private static $instance; |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Create a Wordlift_Configuration_Service's instance. |
|
| 138 | - * |
|
| 139 | - * @since 3.6.0 |
|
| 140 | - */ |
|
| 141 | - public function __construct() { |
|
| 142 | - |
|
| 143 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 144 | - |
|
| 145 | - self::$instance = $this; |
|
| 146 | - |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * Get the singleton instance. |
|
| 151 | - * |
|
| 152 | - * @return \Wordlift_Configuration_Service |
|
| 153 | - * @since 3.6.0 |
|
| 154 | - * |
|
| 155 | - */ |
|
| 156 | - public static function get_instance() { |
|
| 157 | - |
|
| 158 | - return self::$instance; |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * Get a configuration given the option name and a key. The option value is |
|
| 163 | - * expected to be an array. |
|
| 164 | - * |
|
| 165 | - * @param string $option The option name. |
|
| 166 | - * @param string $key A key in the option value array. |
|
| 167 | - * @param string $default The default value in case the key is not found (by default an empty string). |
|
| 168 | - * |
|
| 169 | - * @return mixed The configuration value or the default value if not found. |
|
| 170 | - * @since 3.6.0 |
|
| 171 | - * |
|
| 172 | - */ |
|
| 173 | - private function get( $option, $key, $default = '' ) { |
|
| 174 | - |
|
| 175 | - $options = get_option( $option, array() ); |
|
| 176 | - |
|
| 177 | - return isset( $options[ $key ] ) ? $options[ $key ] : $default; |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * Set a configuration parameter. |
|
| 182 | - * |
|
| 183 | - * @param string $option Name of option to retrieve. Expected to not be SQL-escaped. |
|
| 184 | - * @param string $key The value key. |
|
| 185 | - * @param mixed $value The value. |
|
| 186 | - * |
|
| 187 | - * @since 3.9.0 |
|
| 188 | - * |
|
| 189 | - */ |
|
| 190 | - private function set( $option, $key, $value ) { |
|
| 191 | - |
|
| 192 | - $values = get_option( $option ); |
|
| 193 | - $values = isset( $values ) ? $values : array(); |
|
| 194 | - $values[ $key ] = $value; |
|
| 195 | - update_option( $option, $values ); |
|
| 196 | - |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - /** |
|
| 200 | - * Get the entity base path, by default 'entity'. |
|
| 201 | - * |
|
| 202 | - * @return string The entity base path. |
|
| 203 | - * @since 3.6.0 |
|
| 204 | - * |
|
| 205 | - */ |
|
| 206 | - public function get_entity_base_path() { |
|
| 207 | - |
|
| 208 | - return $this->get( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, 'entity' ); |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - /** |
|
| 212 | - * Get the entity base path. |
|
| 213 | - * |
|
| 214 | - * @param string $value The entity base path. |
|
| 215 | - * |
|
| 216 | - * @since 3.9.0 |
|
| 217 | - * |
|
| 218 | - */ |
|
| 219 | - public function set_entity_base_path( $value ) { |
|
| 220 | - |
|
| 221 | - $this->set( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, $value ); |
|
| 222 | - |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - /** |
|
| 226 | - * Whether the installation skip wizard should be skipped. |
|
| 227 | - * |
|
| 228 | - * @return bool True if it should be skipped otherwise false. |
|
| 229 | - * @since 3.9.0 |
|
| 230 | - * |
|
| 231 | - */ |
|
| 232 | - public function is_skip_wizard() { |
|
| 233 | - |
|
| 234 | - return $this->get( 'wl_general_settings', self::SKIP_WIZARD, false ); |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - /** |
|
| 238 | - * Set the skip wizard parameter. |
|
| 239 | - * |
|
| 240 | - * @param bool $value True to skip the wizard. We expect a boolean value. |
|
| 241 | - * |
|
| 242 | - * @since 3.9.0 |
|
| 243 | - * |
|
| 244 | - */ |
|
| 245 | - public function set_skip_wizard( $value ) { |
|
| 246 | - |
|
| 247 | - $this->set( 'wl_general_settings', self::SKIP_WIZARD, true === $value ); |
|
| 248 | - |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - /** |
|
| 252 | - * Get WordLift's key. |
|
| 253 | - * |
|
| 254 | - * @return string WordLift's key or an empty string if not set. |
|
| 255 | - * @since 3.9.0 |
|
| 256 | - * |
|
| 257 | - */ |
|
| 258 | - public function get_key() { |
|
| 259 | - |
|
| 260 | - return $this->get( 'wl_general_settings', self::KEY, '' ); |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - /** |
|
| 264 | - * Set WordLift's key. |
|
| 265 | - * |
|
| 266 | - * @param string $value WordLift's key. |
|
| 267 | - * |
|
| 268 | - * @since 3.9.0 |
|
| 269 | - * |
|
| 270 | - */ |
|
| 271 | - public function set_key( $value ) { |
|
| 272 | - |
|
| 273 | - $this->set( 'wl_general_settings', self::KEY, $value ); |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - /** |
|
| 277 | - * Get WordLift's configured language, by default 'en'. |
|
| 278 | - * |
|
| 279 | - * Note that WordLift's language is used when writing strings to the Linked Data dataset, not for the analysis. |
|
| 280 | - * |
|
| 281 | - * @return string WordLift's configured language code ('en' by default). |
|
| 282 | - * @since 3.9.0 |
|
| 283 | - * |
|
| 284 | - */ |
|
| 285 | - public function get_language_code() { |
|
| 286 | - |
|
| 287 | - return $this->get( 'wl_general_settings', self::LANGUAGE, 'en' ); |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * Set WordLift's language code, used when storing strings to the Linked Data dataset. |
|
| 292 | - * |
|
| 293 | - * @param string $value WordLift's language code. |
|
| 294 | - * |
|
| 295 | - * @since 3.9.0 |
|
| 296 | - * |
|
| 297 | - */ |
|
| 298 | - public function set_language_code( $value ) { |
|
| 299 | - |
|
| 300 | - $this->set( 'wl_general_settings', self::LANGUAGE, $value ); |
|
| 301 | - |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - /** |
|
| 305 | - * Set the user preferences about sharing diagnostic with us. |
|
| 306 | - * |
|
| 307 | - * @param string $value The user preferences(yes/no). |
|
| 308 | - * |
|
| 309 | - * @since 3.19.0 |
|
| 310 | - * |
|
| 311 | - */ |
|
| 312 | - public function set_diagnostic_preferences( $value ) { |
|
| 313 | - |
|
| 314 | - $this->set( 'wl_general_settings', self::SEND_DIAGNOSTIC, $value ); |
|
| 315 | - |
|
| 316 | - } |
|
| 317 | - |
|
| 318 | - /** |
|
| 319 | - * Get the user preferences about sharing diagnostic. |
|
| 320 | - * |
|
| 321 | - * @since 3.19.0 |
|
| 322 | - */ |
|
| 323 | - public function get_diagnostic_preferences() { |
|
| 324 | - |
|
| 325 | - return $this->get( 'wl_general_settings', self::SEND_DIAGNOSTIC, 'no' ); |
|
| 326 | - } |
|
| 327 | - |
|
| 328 | - /** |
|
| 329 | - * Get WordLift's configured country code, by default 'us'. |
|
| 330 | - * |
|
| 331 | - * @return string WordLift's configured country code ('us' by default). |
|
| 332 | - * @since 3.18.0 |
|
| 333 | - * |
|
| 334 | - */ |
|
| 335 | - public function get_country_code() { |
|
| 336 | - |
|
| 337 | - return $this->get( 'wl_general_settings', self::COUNTRY_CODE, 'us' ); |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - /** |
|
| 341 | - * Set WordLift's country code. |
|
| 342 | - * |
|
| 343 | - * @param string $value WordLift's country code. |
|
| 344 | - * |
|
| 345 | - * @since 3.18.0 |
|
| 346 | - * |
|
| 347 | - */ |
|
| 348 | - public function set_country_code( $value ) { |
|
| 349 | - |
|
| 350 | - $this->set( 'wl_general_settings', self::COUNTRY_CODE, $value ); |
|
| 351 | - |
|
| 352 | - } |
|
| 353 | - |
|
| 354 | - /** |
|
| 355 | - * Get the publisher entity post id. |
|
| 356 | - * |
|
| 357 | - * The publisher entity post id points to an entity post which contains the data for the publisher used in schema.org |
|
| 358 | - * Article markup. |
|
| 359 | - * |
|
| 360 | - * @return int|NULL The publisher entity post id or NULL if not set. |
|
| 361 | - * @since 3.9.0 |
|
| 362 | - * |
|
| 363 | - */ |
|
| 364 | - public function get_publisher_id() { |
|
| 365 | - |
|
| 366 | - return $this->get( 'wl_general_settings', self::PUBLISHER_ID, null ); |
|
| 367 | - } |
|
| 368 | - |
|
| 369 | - /** |
|
| 370 | - * Set the publisher entity post id. |
|
| 371 | - * |
|
| 372 | - * @param int $value The publisher entity post id. |
|
| 373 | - * |
|
| 374 | - * @since 3.9.0 |
|
| 375 | - * |
|
| 376 | - */ |
|
| 377 | - public function set_publisher_id( $value ) { |
|
| 378 | - |
|
| 379 | - $this->set( 'wl_general_settings', self::PUBLISHER_ID, $value ); |
|
| 380 | - |
|
| 381 | - } |
|
| 382 | - |
|
| 383 | - /** |
|
| 384 | - * Get the dataset URI. |
|
| 385 | - * |
|
| 386 | - * @return string The dataset URI or an empty string if not set. |
|
| 387 | - * @since 3.10.0 |
|
| 388 | - * @since 3.27.7 Always return null if `wl_features__enable__dataset` is disabled. |
|
| 389 | - * |
|
| 390 | - */ |
|
| 391 | - public function get_dataset_uri() { |
|
| 392 | - |
|
| 393 | - if ( apply_filters( 'wl_features__enable__dataset', true ) ) { |
|
| 394 | - return $this->get( 'wl_advanced_settings', self::DATASET_URI, null ); |
|
| 395 | - } else { |
|
| 396 | - return null; |
|
| 397 | - } |
|
| 398 | - } |
|
| 399 | - |
|
| 400 | - /** |
|
| 401 | - * Set the dataset URI. |
|
| 402 | - * |
|
| 403 | - * @param string $value The dataset URI. |
|
| 404 | - * |
|
| 405 | - * @since 3.10.0 |
|
| 406 | - * |
|
| 407 | - */ |
|
| 408 | - public function set_dataset_uri( $value ) { |
|
| 409 | - |
|
| 410 | - $this->set( 'wl_advanced_settings', self::DATASET_URI, $value ); |
|
| 411 | - } |
|
| 412 | - |
|
| 413 | - /** |
|
| 414 | - * Get the package type. |
|
| 415 | - * |
|
| 416 | - * @return string The package type or an empty string if not set. |
|
| 417 | - * @since 3.20.0 |
|
| 418 | - * |
|
| 419 | - */ |
|
| 420 | - public function get_package_type() { |
|
| 421 | - |
|
| 422 | - return $this->get( 'wl_advanced_settings', self::PACKAGE_TYPE, null ); |
|
| 423 | - } |
|
| 424 | - |
|
| 425 | - /** |
|
| 426 | - * Set the package type. |
|
| 427 | - * |
|
| 428 | - * @param string $value The package type. |
|
| 429 | - * |
|
| 430 | - * @since 3.20.0 |
|
| 431 | - * |
|
| 432 | - */ |
|
| 433 | - public function set_package_type( $value ) { |
|
| 434 | - $this->set( 'wl_advanced_settings', self::PACKAGE_TYPE, $value ); |
|
| 435 | - } |
|
| 436 | - |
|
| 437 | - /** |
|
| 438 | - * Intercept the change of the WordLift key in order to set the dataset URI. |
|
| 439 | - * |
|
| 440 | - * |
|
| 441 | - * @since 3.20.0 as of #761, we save settings every time a key is set, not only when the key changes, so to |
|
| 442 | - * store the configuration parameters such as country or language. |
|
| 443 | - * @since 3.11.0 |
|
| 444 | - * |
|
| 445 | - * @see https://github.com/insideout10/wordlift-plugin/issues/761 |
|
| 446 | - * |
|
| 447 | - * @param array $old_value The old settings. |
|
| 448 | - * @param array $new_value The new settings. |
|
| 449 | - */ |
|
| 450 | - public function update_key( $old_value, $new_value ) { |
|
| 451 | - |
|
| 452 | - // Check the old key value and the new one. We're going to ask for the dataset URI only if the key has changed. |
|
| 453 | - // $old_key = isset( $old_value['key'] ) ? $old_value['key'] : ''; |
|
| 454 | - $new_key = isset( $new_value['key'] ) ? $new_value['key'] : ''; |
|
| 455 | - |
|
| 456 | - // If the key hasn't changed, don't do anything. |
|
| 457 | - // WARN The 'update_option' hook is fired only if the new and old value are not equal. |
|
| 458 | - // if ( $old_key === $new_key ) { |
|
| 459 | - // return; |
|
| 460 | - // } |
|
| 461 | - |
|
| 462 | - // If the key is empty, empty the dataset URI. |
|
| 463 | - if ( '' === $new_key ) { |
|
| 464 | - $this->set_dataset_uri( '' ); |
|
| 465 | - } |
|
| 466 | - |
|
| 467 | - // make the request to the remote server. |
|
| 468 | - $this->get_remote_dataset_uri( $new_key ); |
|
| 469 | - |
|
| 470 | - } |
|
| 471 | - |
|
| 472 | - /** |
|
| 473 | - * Handle retrieving the dataset uri from the remote server. |
|
| 474 | - * |
|
| 475 | - * If a valid dataset uri is returned it is stored in the appropriate option, |
|
| 476 | - * otherwise the option is set to empty string. |
|
| 477 | - * |
|
| 478 | - * @param string $key The key to be used. |
|
| 479 | - * |
|
| 480 | - * @since 3.12.0 |
|
| 481 | - * |
|
| 482 | - * @since 3.17.0 send the site URL and get the dataset URI. |
|
| 483 | - */ |
|
| 484 | - public function get_remote_dataset_uri( $key ) { |
|
| 485 | - |
|
| 486 | - $this->log->trace( 'Getting the remote dataset URI and package type...' ); |
|
| 487 | - |
|
| 488 | - if ( empty( $key ) ) { |
|
| 489 | - $this->log->warn( 'Key set to empty value.' ); |
|
| 490 | - |
|
| 491 | - $this->set_dataset_uri( '' ); |
|
| 492 | - $this->set_package_type( null ); |
|
| 493 | - |
|
| 494 | - return; |
|
| 495 | - } |
|
| 496 | - |
|
| 497 | - /** |
|
| 498 | - * Allow 3rd parties to change the site_url. |
|
| 499 | - * |
|
| 500 | - * @param string $site_url The site url. |
|
| 501 | - * |
|
| 502 | - * @see https://github.com/insideout10/wordlift-plugin/issues/850 |
|
| 503 | - * |
|
| 504 | - * @since 3.20.0 |
|
| 505 | - * |
|
| 506 | - */ |
|
| 507 | - $home_url = get_option( 'home' ); |
|
| 508 | - $site_url = apply_filters( 'wl_production_site_url', untrailingslashit( $home_url ) ); |
|
| 509 | - |
|
| 510 | - // Build the URL. |
|
| 511 | - $url = '/accounts' |
|
| 512 | - . '?key=' . rawurlencode( $key ) |
|
| 513 | - . '&url=' . rawurlencode( $site_url ) |
|
| 514 | - . '&country=' . $this->get_country_code() |
|
| 515 | - . '&language=' . $this->get_language_code(); |
|
| 516 | - |
|
| 517 | - $api_service = Default_Api_Service::get_instance(); |
|
| 518 | - /** |
|
| 519 | - * @since 3.27.7.1 |
|
| 520 | - * The Key should be passed to headers, otherwise api would return null. |
|
| 521 | - */ |
|
| 522 | - $headers = array( |
|
| 523 | - 'Authorization' => "Key $key", |
|
| 524 | - ); |
|
| 525 | - $response = $api_service->request( 'PUT', $url, $headers )->get_response(); |
|
| 526 | - |
|
| 527 | - // The response is an error. |
|
| 528 | - if ( is_wp_error( $response ) ) { |
|
| 529 | - $this->log->error( 'An error occurred setting the dataset URI: ' . $response->get_error_message() ); |
|
| 530 | - |
|
| 531 | - $this->set_dataset_uri( '' ); |
|
| 532 | - $this->set_package_type( null ); |
|
| 533 | - |
|
| 534 | - return; |
|
| 535 | - } |
|
| 536 | - |
|
| 537 | - // The response is not OK. |
|
| 538 | - if ( ! is_array( $response ) || 200 !== (int) $response['response']['code'] ) { |
|
| 539 | - $base_url = $api_service->get_base_url(); |
|
| 540 | - |
|
| 541 | - if ( ! is_array( $response ) ) { |
|
| 542 | - $this->log->error( "Unexpected response when opening URL $base_url$url: " . var_export( $response, true ) ); |
|
| 543 | - } else { |
|
| 544 | - $this->log->error( "Unexpected status code when opening URL $base_url$url: " . $response['response']['code'] . "\n" . var_export( $response, true ) ); |
|
| 545 | - } |
|
| 546 | - |
|
| 547 | - |
|
| 548 | - $this->set_dataset_uri( '' ); |
|
| 549 | - $this->set_package_type( null ); |
|
| 550 | - |
|
| 551 | - return; |
|
| 552 | - } |
|
| 553 | - |
|
| 554 | - /* |
|
| 26 | + /** |
|
| 27 | + * The entity base path option name. |
|
| 28 | + * |
|
| 29 | + * @since 3.6.0 |
|
| 30 | + */ |
|
| 31 | + const ENTITY_BASE_PATH_KEY = 'wl_entity_base_path'; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * The skip wizard (admin installation wizard) option name. |
|
| 35 | + * |
|
| 36 | + * @since 3.9.0 |
|
| 37 | + */ |
|
| 38 | + const SKIP_WIZARD = 'wl_skip_wizard'; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * WordLift's key option name. |
|
| 42 | + * |
|
| 43 | + * @since 3.9.0 |
|
| 44 | + */ |
|
| 45 | + const KEY = 'key'; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * WordLift's configured language option name. |
|
| 49 | + * |
|
| 50 | + * @since 3.9.0 |
|
| 51 | + */ |
|
| 52 | + const LANGUAGE = 'site_language'; |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * WordLift's configured country code. |
|
| 56 | + * |
|
| 57 | + * @since 3.18.0 |
|
| 58 | + */ |
|
| 59 | + const COUNTRY_CODE = 'country_code'; |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * The publisher entity post ID option name. |
|
| 63 | + * |
|
| 64 | + * @since 3.9.0 |
|
| 65 | + */ |
|
| 66 | + const PUBLISHER_ID = 'publisher_id'; |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * The dataset URI option name |
|
| 70 | + * |
|
| 71 | + * @since 3.10.0 |
|
| 72 | + */ |
|
| 73 | + const DATASET_URI = 'redlink_dataset_uri'; |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * The link by default option name. |
|
| 77 | + * |
|
| 78 | + * @since 3.11.0 |
|
| 79 | + */ |
|
| 80 | + const LINK_BY_DEFAULT = 'link_by_default'; |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * The analytics enable option. |
|
| 84 | + * |
|
| 85 | + * @since 3.21.0 |
|
| 86 | + */ |
|
| 87 | + const ANALYTICS_ENABLE = 'analytics_enable'; |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * The analytics entity uri dimension option. |
|
| 91 | + * |
|
| 92 | + * @since 3.21.0 |
|
| 93 | + */ |
|
| 94 | + const ANALYTICS_ENTITY_URI_DIMENSION = 'analytics_entity_uri_dimension'; |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * The analytics entity type dimension option. |
|
| 98 | + * |
|
| 99 | + * @since 3.21.0 |
|
| 100 | + */ |
|
| 101 | + const ANALYTICS_ENTITY_TYPE_DIMENSION = 'analytics_entity_type_dimension'; |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * The user preferences about sharing data option. |
|
| 105 | + * |
|
| 106 | + * @since 3.19.0 |
|
| 107 | + */ |
|
| 108 | + const SEND_DIAGNOSTIC = 'send_diagnostic'; |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * The package type configuration key. |
|
| 112 | + * |
|
| 113 | + * @since 3.20.0 |
|
| 114 | + */ |
|
| 115 | + const PACKAGE_TYPE = 'package_type'; |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * The {@link Wordlift_Log_Service} instance. |
|
| 119 | + * |
|
| 120 | + * @since 3.16.0 |
|
| 121 | + * |
|
| 122 | + * @var \Wordlift_Log_Service $log The {@link Wordlift_Log_Service} instance. |
|
| 123 | + */ |
|
| 124 | + private $log; |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * The Wordlift_Configuration_Service's singleton instance. |
|
| 128 | + * |
|
| 129 | + * @since 3.6.0 |
|
| 130 | + * |
|
| 131 | + * @access private |
|
| 132 | + * @var \Wordlift_Configuration_Service $instance Wordlift_Configuration_Service's singleton instance. |
|
| 133 | + */ |
|
| 134 | + private static $instance; |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Create a Wordlift_Configuration_Service's instance. |
|
| 138 | + * |
|
| 139 | + * @since 3.6.0 |
|
| 140 | + */ |
|
| 141 | + public function __construct() { |
|
| 142 | + |
|
| 143 | + $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 144 | + |
|
| 145 | + self::$instance = $this; |
|
| 146 | + |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * Get the singleton instance. |
|
| 151 | + * |
|
| 152 | + * @return \Wordlift_Configuration_Service |
|
| 153 | + * @since 3.6.0 |
|
| 154 | + * |
|
| 155 | + */ |
|
| 156 | + public static function get_instance() { |
|
| 157 | + |
|
| 158 | + return self::$instance; |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * Get a configuration given the option name and a key. The option value is |
|
| 163 | + * expected to be an array. |
|
| 164 | + * |
|
| 165 | + * @param string $option The option name. |
|
| 166 | + * @param string $key A key in the option value array. |
|
| 167 | + * @param string $default The default value in case the key is not found (by default an empty string). |
|
| 168 | + * |
|
| 169 | + * @return mixed The configuration value or the default value if not found. |
|
| 170 | + * @since 3.6.0 |
|
| 171 | + * |
|
| 172 | + */ |
|
| 173 | + private function get( $option, $key, $default = '' ) { |
|
| 174 | + |
|
| 175 | + $options = get_option( $option, array() ); |
|
| 176 | + |
|
| 177 | + return isset( $options[ $key ] ) ? $options[ $key ] : $default; |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * Set a configuration parameter. |
|
| 182 | + * |
|
| 183 | + * @param string $option Name of option to retrieve. Expected to not be SQL-escaped. |
|
| 184 | + * @param string $key The value key. |
|
| 185 | + * @param mixed $value The value. |
|
| 186 | + * |
|
| 187 | + * @since 3.9.0 |
|
| 188 | + * |
|
| 189 | + */ |
|
| 190 | + private function set( $option, $key, $value ) { |
|
| 191 | + |
|
| 192 | + $values = get_option( $option ); |
|
| 193 | + $values = isset( $values ) ? $values : array(); |
|
| 194 | + $values[ $key ] = $value; |
|
| 195 | + update_option( $option, $values ); |
|
| 196 | + |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + /** |
|
| 200 | + * Get the entity base path, by default 'entity'. |
|
| 201 | + * |
|
| 202 | + * @return string The entity base path. |
|
| 203 | + * @since 3.6.0 |
|
| 204 | + * |
|
| 205 | + */ |
|
| 206 | + public function get_entity_base_path() { |
|
| 207 | + |
|
| 208 | + return $this->get( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, 'entity' ); |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + /** |
|
| 212 | + * Get the entity base path. |
|
| 213 | + * |
|
| 214 | + * @param string $value The entity base path. |
|
| 215 | + * |
|
| 216 | + * @since 3.9.0 |
|
| 217 | + * |
|
| 218 | + */ |
|
| 219 | + public function set_entity_base_path( $value ) { |
|
| 220 | + |
|
| 221 | + $this->set( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, $value ); |
|
| 222 | + |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + /** |
|
| 226 | + * Whether the installation skip wizard should be skipped. |
|
| 227 | + * |
|
| 228 | + * @return bool True if it should be skipped otherwise false. |
|
| 229 | + * @since 3.9.0 |
|
| 230 | + * |
|
| 231 | + */ |
|
| 232 | + public function is_skip_wizard() { |
|
| 233 | + |
|
| 234 | + return $this->get( 'wl_general_settings', self::SKIP_WIZARD, false ); |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + /** |
|
| 238 | + * Set the skip wizard parameter. |
|
| 239 | + * |
|
| 240 | + * @param bool $value True to skip the wizard. We expect a boolean value. |
|
| 241 | + * |
|
| 242 | + * @since 3.9.0 |
|
| 243 | + * |
|
| 244 | + */ |
|
| 245 | + public function set_skip_wizard( $value ) { |
|
| 246 | + |
|
| 247 | + $this->set( 'wl_general_settings', self::SKIP_WIZARD, true === $value ); |
|
| 248 | + |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + /** |
|
| 252 | + * Get WordLift's key. |
|
| 253 | + * |
|
| 254 | + * @return string WordLift's key or an empty string if not set. |
|
| 255 | + * @since 3.9.0 |
|
| 256 | + * |
|
| 257 | + */ |
|
| 258 | + public function get_key() { |
|
| 259 | + |
|
| 260 | + return $this->get( 'wl_general_settings', self::KEY, '' ); |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + /** |
|
| 264 | + * Set WordLift's key. |
|
| 265 | + * |
|
| 266 | + * @param string $value WordLift's key. |
|
| 267 | + * |
|
| 268 | + * @since 3.9.0 |
|
| 269 | + * |
|
| 270 | + */ |
|
| 271 | + public function set_key( $value ) { |
|
| 272 | + |
|
| 273 | + $this->set( 'wl_general_settings', self::KEY, $value ); |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + /** |
|
| 277 | + * Get WordLift's configured language, by default 'en'. |
|
| 278 | + * |
|
| 279 | + * Note that WordLift's language is used when writing strings to the Linked Data dataset, not for the analysis. |
|
| 280 | + * |
|
| 281 | + * @return string WordLift's configured language code ('en' by default). |
|
| 282 | + * @since 3.9.0 |
|
| 283 | + * |
|
| 284 | + */ |
|
| 285 | + public function get_language_code() { |
|
| 286 | + |
|
| 287 | + return $this->get( 'wl_general_settings', self::LANGUAGE, 'en' ); |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + /** |
|
| 291 | + * Set WordLift's language code, used when storing strings to the Linked Data dataset. |
|
| 292 | + * |
|
| 293 | + * @param string $value WordLift's language code. |
|
| 294 | + * |
|
| 295 | + * @since 3.9.0 |
|
| 296 | + * |
|
| 297 | + */ |
|
| 298 | + public function set_language_code( $value ) { |
|
| 299 | + |
|
| 300 | + $this->set( 'wl_general_settings', self::LANGUAGE, $value ); |
|
| 301 | + |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + /** |
|
| 305 | + * Set the user preferences about sharing diagnostic with us. |
|
| 306 | + * |
|
| 307 | + * @param string $value The user preferences(yes/no). |
|
| 308 | + * |
|
| 309 | + * @since 3.19.0 |
|
| 310 | + * |
|
| 311 | + */ |
|
| 312 | + public function set_diagnostic_preferences( $value ) { |
|
| 313 | + |
|
| 314 | + $this->set( 'wl_general_settings', self::SEND_DIAGNOSTIC, $value ); |
|
| 315 | + |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + /** |
|
| 319 | + * Get the user preferences about sharing diagnostic. |
|
| 320 | + * |
|
| 321 | + * @since 3.19.0 |
|
| 322 | + */ |
|
| 323 | + public function get_diagnostic_preferences() { |
|
| 324 | + |
|
| 325 | + return $this->get( 'wl_general_settings', self::SEND_DIAGNOSTIC, 'no' ); |
|
| 326 | + } |
|
| 327 | + |
|
| 328 | + /** |
|
| 329 | + * Get WordLift's configured country code, by default 'us'. |
|
| 330 | + * |
|
| 331 | + * @return string WordLift's configured country code ('us' by default). |
|
| 332 | + * @since 3.18.0 |
|
| 333 | + * |
|
| 334 | + */ |
|
| 335 | + public function get_country_code() { |
|
| 336 | + |
|
| 337 | + return $this->get( 'wl_general_settings', self::COUNTRY_CODE, 'us' ); |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + /** |
|
| 341 | + * Set WordLift's country code. |
|
| 342 | + * |
|
| 343 | + * @param string $value WordLift's country code. |
|
| 344 | + * |
|
| 345 | + * @since 3.18.0 |
|
| 346 | + * |
|
| 347 | + */ |
|
| 348 | + public function set_country_code( $value ) { |
|
| 349 | + |
|
| 350 | + $this->set( 'wl_general_settings', self::COUNTRY_CODE, $value ); |
|
| 351 | + |
|
| 352 | + } |
|
| 353 | + |
|
| 354 | + /** |
|
| 355 | + * Get the publisher entity post id. |
|
| 356 | + * |
|
| 357 | + * The publisher entity post id points to an entity post which contains the data for the publisher used in schema.org |
|
| 358 | + * Article markup. |
|
| 359 | + * |
|
| 360 | + * @return int|NULL The publisher entity post id or NULL if not set. |
|
| 361 | + * @since 3.9.0 |
|
| 362 | + * |
|
| 363 | + */ |
|
| 364 | + public function get_publisher_id() { |
|
| 365 | + |
|
| 366 | + return $this->get( 'wl_general_settings', self::PUBLISHER_ID, null ); |
|
| 367 | + } |
|
| 368 | + |
|
| 369 | + /** |
|
| 370 | + * Set the publisher entity post id. |
|
| 371 | + * |
|
| 372 | + * @param int $value The publisher entity post id. |
|
| 373 | + * |
|
| 374 | + * @since 3.9.0 |
|
| 375 | + * |
|
| 376 | + */ |
|
| 377 | + public function set_publisher_id( $value ) { |
|
| 378 | + |
|
| 379 | + $this->set( 'wl_general_settings', self::PUBLISHER_ID, $value ); |
|
| 380 | + |
|
| 381 | + } |
|
| 382 | + |
|
| 383 | + /** |
|
| 384 | + * Get the dataset URI. |
|
| 385 | + * |
|
| 386 | + * @return string The dataset URI or an empty string if not set. |
|
| 387 | + * @since 3.10.0 |
|
| 388 | + * @since 3.27.7 Always return null if `wl_features__enable__dataset` is disabled. |
|
| 389 | + * |
|
| 390 | + */ |
|
| 391 | + public function get_dataset_uri() { |
|
| 392 | + |
|
| 393 | + if ( apply_filters( 'wl_features__enable__dataset', true ) ) { |
|
| 394 | + return $this->get( 'wl_advanced_settings', self::DATASET_URI, null ); |
|
| 395 | + } else { |
|
| 396 | + return null; |
|
| 397 | + } |
|
| 398 | + } |
|
| 399 | + |
|
| 400 | + /** |
|
| 401 | + * Set the dataset URI. |
|
| 402 | + * |
|
| 403 | + * @param string $value The dataset URI. |
|
| 404 | + * |
|
| 405 | + * @since 3.10.0 |
|
| 406 | + * |
|
| 407 | + */ |
|
| 408 | + public function set_dataset_uri( $value ) { |
|
| 409 | + |
|
| 410 | + $this->set( 'wl_advanced_settings', self::DATASET_URI, $value ); |
|
| 411 | + } |
|
| 412 | + |
|
| 413 | + /** |
|
| 414 | + * Get the package type. |
|
| 415 | + * |
|
| 416 | + * @return string The package type or an empty string if not set. |
|
| 417 | + * @since 3.20.0 |
|
| 418 | + * |
|
| 419 | + */ |
|
| 420 | + public function get_package_type() { |
|
| 421 | + |
|
| 422 | + return $this->get( 'wl_advanced_settings', self::PACKAGE_TYPE, null ); |
|
| 423 | + } |
|
| 424 | + |
|
| 425 | + /** |
|
| 426 | + * Set the package type. |
|
| 427 | + * |
|
| 428 | + * @param string $value The package type. |
|
| 429 | + * |
|
| 430 | + * @since 3.20.0 |
|
| 431 | + * |
|
| 432 | + */ |
|
| 433 | + public function set_package_type( $value ) { |
|
| 434 | + $this->set( 'wl_advanced_settings', self::PACKAGE_TYPE, $value ); |
|
| 435 | + } |
|
| 436 | + |
|
| 437 | + /** |
|
| 438 | + * Intercept the change of the WordLift key in order to set the dataset URI. |
|
| 439 | + * |
|
| 440 | + * |
|
| 441 | + * @since 3.20.0 as of #761, we save settings every time a key is set, not only when the key changes, so to |
|
| 442 | + * store the configuration parameters such as country or language. |
|
| 443 | + * @since 3.11.0 |
|
| 444 | + * |
|
| 445 | + * @see https://github.com/insideout10/wordlift-plugin/issues/761 |
|
| 446 | + * |
|
| 447 | + * @param array $old_value The old settings. |
|
| 448 | + * @param array $new_value The new settings. |
|
| 449 | + */ |
|
| 450 | + public function update_key( $old_value, $new_value ) { |
|
| 451 | + |
|
| 452 | + // Check the old key value and the new one. We're going to ask for the dataset URI only if the key has changed. |
|
| 453 | + // $old_key = isset( $old_value['key'] ) ? $old_value['key'] : ''; |
|
| 454 | + $new_key = isset( $new_value['key'] ) ? $new_value['key'] : ''; |
|
| 455 | + |
|
| 456 | + // If the key hasn't changed, don't do anything. |
|
| 457 | + // WARN The 'update_option' hook is fired only if the new and old value are not equal. |
|
| 458 | + // if ( $old_key === $new_key ) { |
|
| 459 | + // return; |
|
| 460 | + // } |
|
| 461 | + |
|
| 462 | + // If the key is empty, empty the dataset URI. |
|
| 463 | + if ( '' === $new_key ) { |
|
| 464 | + $this->set_dataset_uri( '' ); |
|
| 465 | + } |
|
| 466 | + |
|
| 467 | + // make the request to the remote server. |
|
| 468 | + $this->get_remote_dataset_uri( $new_key ); |
|
| 469 | + |
|
| 470 | + } |
|
| 471 | + |
|
| 472 | + /** |
|
| 473 | + * Handle retrieving the dataset uri from the remote server. |
|
| 474 | + * |
|
| 475 | + * If a valid dataset uri is returned it is stored in the appropriate option, |
|
| 476 | + * otherwise the option is set to empty string. |
|
| 477 | + * |
|
| 478 | + * @param string $key The key to be used. |
|
| 479 | + * |
|
| 480 | + * @since 3.12.0 |
|
| 481 | + * |
|
| 482 | + * @since 3.17.0 send the site URL and get the dataset URI. |
|
| 483 | + */ |
|
| 484 | + public function get_remote_dataset_uri( $key ) { |
|
| 485 | + |
|
| 486 | + $this->log->trace( 'Getting the remote dataset URI and package type...' ); |
|
| 487 | + |
|
| 488 | + if ( empty( $key ) ) { |
|
| 489 | + $this->log->warn( 'Key set to empty value.' ); |
|
| 490 | + |
|
| 491 | + $this->set_dataset_uri( '' ); |
|
| 492 | + $this->set_package_type( null ); |
|
| 493 | + |
|
| 494 | + return; |
|
| 495 | + } |
|
| 496 | + |
|
| 497 | + /** |
|
| 498 | + * Allow 3rd parties to change the site_url. |
|
| 499 | + * |
|
| 500 | + * @param string $site_url The site url. |
|
| 501 | + * |
|
| 502 | + * @see https://github.com/insideout10/wordlift-plugin/issues/850 |
|
| 503 | + * |
|
| 504 | + * @since 3.20.0 |
|
| 505 | + * |
|
| 506 | + */ |
|
| 507 | + $home_url = get_option( 'home' ); |
|
| 508 | + $site_url = apply_filters( 'wl_production_site_url', untrailingslashit( $home_url ) ); |
|
| 509 | + |
|
| 510 | + // Build the URL. |
|
| 511 | + $url = '/accounts' |
|
| 512 | + . '?key=' . rawurlencode( $key ) |
|
| 513 | + . '&url=' . rawurlencode( $site_url ) |
|
| 514 | + . '&country=' . $this->get_country_code() |
|
| 515 | + . '&language=' . $this->get_language_code(); |
|
| 516 | + |
|
| 517 | + $api_service = Default_Api_Service::get_instance(); |
|
| 518 | + /** |
|
| 519 | + * @since 3.27.7.1 |
|
| 520 | + * The Key should be passed to headers, otherwise api would return null. |
|
| 521 | + */ |
|
| 522 | + $headers = array( |
|
| 523 | + 'Authorization' => "Key $key", |
|
| 524 | + ); |
|
| 525 | + $response = $api_service->request( 'PUT', $url, $headers )->get_response(); |
|
| 526 | + |
|
| 527 | + // The response is an error. |
|
| 528 | + if ( is_wp_error( $response ) ) { |
|
| 529 | + $this->log->error( 'An error occurred setting the dataset URI: ' . $response->get_error_message() ); |
|
| 530 | + |
|
| 531 | + $this->set_dataset_uri( '' ); |
|
| 532 | + $this->set_package_type( null ); |
|
| 533 | + |
|
| 534 | + return; |
|
| 535 | + } |
|
| 536 | + |
|
| 537 | + // The response is not OK. |
|
| 538 | + if ( ! is_array( $response ) || 200 !== (int) $response['response']['code'] ) { |
|
| 539 | + $base_url = $api_service->get_base_url(); |
|
| 540 | + |
|
| 541 | + if ( ! is_array( $response ) ) { |
|
| 542 | + $this->log->error( "Unexpected response when opening URL $base_url$url: " . var_export( $response, true ) ); |
|
| 543 | + } else { |
|
| 544 | + $this->log->error( "Unexpected status code when opening URL $base_url$url: " . $response['response']['code'] . "\n" . var_export( $response, true ) ); |
|
| 545 | + } |
|
| 546 | + |
|
| 547 | + |
|
| 548 | + $this->set_dataset_uri( '' ); |
|
| 549 | + $this->set_package_type( null ); |
|
| 550 | + |
|
| 551 | + return; |
|
| 552 | + } |
|
| 553 | + |
|
| 554 | + /* |
|
| 555 | 555 | * We also store the package type. |
| 556 | 556 | * |
| 557 | 557 | * @since 3.20.0 |
| 558 | 558 | */ |
| 559 | - $json = json_decode( $response['body'] ); |
|
| 560 | - /** |
|
| 561 | - * @since 3.27.7 |
|
| 562 | - * Remove the trailing slash returned from the new platform api. |
|
| 563 | - */ |
|
| 564 | - $dataset_uri = untrailingslashit( $json->datasetURI ); |
|
| 565 | - $package_type = isset( $json->packageType ) ? $json->packageType : null; |
|
| 566 | - |
|
| 567 | - $this->log->info( "Updating [ dataset uri :: $dataset_uri ][ package type :: $package_type ]..." ); |
|
| 568 | - |
|
| 569 | - $this->set_dataset_uri( $dataset_uri ); |
|
| 570 | - $this->set_package_type( $package_type ); |
|
| 571 | - } |
|
| 572 | - |
|
| 573 | - /** |
|
| 574 | - * Handle the edge case where a user submits the same key again |
|
| 575 | - * when he does not have the dataset uri to regain it. |
|
| 576 | - * |
|
| 577 | - * This can not be handled in the normal option update hook because |
|
| 578 | - * it is not being triggered when the save value equals to the one already |
|
| 579 | - * in the DB. |
|
| 580 | - * |
|
| 581 | - * @param mixed $value The new, unserialized option value. |
|
| 582 | - * @param mixed $old_value The old option value. |
|
| 583 | - * |
|
| 584 | - * @return mixed The same value in the $value parameter |
|
| 585 | - * @since 3.12.0 |
|
| 586 | - * |
|
| 587 | - */ |
|
| 588 | - function maybe_update_dataset_uri( $value, $old_value ) { |
|
| 589 | - |
|
| 590 | - // Check the old key value and the new one. Here we're only handling the |
|
| 591 | - // case where the key hasn't changed and the dataset URI isn't set. The |
|
| 592 | - // other case, i.e. a new key is inserted, is handled at `update_key`. |
|
| 593 | - $old_key = isset( $old_value['key'] ) ? $old_value['key'] : ''; |
|
| 594 | - $new_key = isset( $value['key'] ) ? $value['key'] : ''; |
|
| 595 | - |
|
| 596 | - $dataset_uri = $this->get_dataset_uri(); |
|
| 597 | - |
|
| 598 | - if ( ! empty( $new_key ) && $new_key === $old_key && empty( $dataset_uri ) ) { |
|
| 599 | - |
|
| 600 | - // make the request to the remote server to try to get the dataset uri. |
|
| 601 | - $this->get_remote_dataset_uri( $new_key ); |
|
| 602 | - } |
|
| 603 | - |
|
| 604 | - return $value; |
|
| 605 | - } |
|
| 606 | - |
|
| 607 | - /** |
|
| 608 | - * Get the API URI to retrieve the dataset URI using the WordLift Key. |
|
| 609 | - * |
|
| 610 | - * @param string $key The WordLift key to use. |
|
| 611 | - * |
|
| 612 | - * @return string The API URI. |
|
| 613 | - * @since 3.11.0 |
|
| 614 | - * |
|
| 615 | - */ |
|
| 616 | - public function get_accounts_by_key_dataset_uri( $key ) { |
|
| 617 | - |
|
| 618 | - return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . "accounts/key=$key/dataset_uri"; |
|
| 619 | - } |
|
| 620 | - |
|
| 621 | - /** |
|
| 622 | - * Get the `accounts` end point. |
|
| 623 | - * |
|
| 624 | - * @return string The `accounts` end point. |
|
| 625 | - * @since 3.16.0 |
|
| 626 | - * |
|
| 627 | - */ |
|
| 628 | - public function get_accounts() { |
|
| 629 | - |
|
| 630 | - return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'accounts'; |
|
| 631 | - } |
|
| 632 | - |
|
| 633 | - /** |
|
| 634 | - * Get the `link by default` option. |
|
| 635 | - * |
|
| 636 | - * @return bool True if entities must be linked by default otherwise false. |
|
| 637 | - * @since 3.13.0 |
|
| 638 | - * |
|
| 639 | - */ |
|
| 640 | - public function is_link_by_default() { |
|
| 641 | - |
|
| 642 | - return 'yes' === $this->get( 'wl_general_settings', self::LINK_BY_DEFAULT, 'yes' ); |
|
| 643 | - } |
|
| 644 | - |
|
| 645 | - /** |
|
| 646 | - * Set the `link by default` option. |
|
| 647 | - * |
|
| 648 | - * @param bool $value True to enabling linking by default, otherwise false. |
|
| 649 | - * |
|
| 650 | - * @since 3.13.0 |
|
| 651 | - * |
|
| 652 | - */ |
|
| 653 | - public function set_link_by_default( $value ) { |
|
| 654 | - |
|
| 655 | - $this->set( 'wl_general_settings', self::LINK_BY_DEFAULT, true === $value ? 'yes' : 'no' ); |
|
| 656 | - } |
|
| 657 | - |
|
| 658 | - /** |
|
| 659 | - * Get the 'analytics-enable' option. |
|
| 660 | - * |
|
| 661 | - * @return string 'no' or 'yes' representing bool. |
|
| 662 | - * @since 3.21.0 |
|
| 663 | - * |
|
| 664 | - */ |
|
| 665 | - public function is_analytics_enable() { |
|
| 666 | - return 'yes' === $this->get( 'wl_analytics_settings', self::ANALYTICS_ENABLE, 'no' ); |
|
| 667 | - } |
|
| 668 | - |
|
| 669 | - /** |
|
| 670 | - * Set the `analytics-enable` option. |
|
| 671 | - * |
|
| 672 | - * @param bool $value True to enabling analytics, otherwise false. |
|
| 673 | - * |
|
| 674 | - * @since 3.21.0 |
|
| 675 | - * |
|
| 676 | - */ |
|
| 677 | - public function set_is_analytics_enable( $value ) { |
|
| 678 | - |
|
| 679 | - $this->set( 'wl_general_settings', self::ANALYTICS_ENABLE, true === $value ? 'yes' : 'no' ); |
|
| 680 | - } |
|
| 681 | - |
|
| 682 | - /** |
|
| 683 | - * Get the 'analytics-entity-uri-dimention' option. |
|
| 684 | - * |
|
| 685 | - * @return int |
|
| 686 | - * @since 3.21.0 |
|
| 687 | - * |
|
| 688 | - */ |
|
| 689 | - public function get_analytics_entity_uri_dimension() { |
|
| 690 | - return (int) $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_URI_DIMENSION, 1 ); |
|
| 691 | - } |
|
| 692 | - |
|
| 693 | - /** |
|
| 694 | - * Get the 'analytics-entity-type-dimension' option. |
|
| 695 | - * |
|
| 696 | - * @return int |
|
| 697 | - * @since 3.21.0 |
|
| 698 | - * |
|
| 699 | - */ |
|
| 700 | - public function get_analytics_entity_type_dimension() { |
|
| 701 | - return $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_TYPE_DIMENSION, 2 ); |
|
| 702 | - } |
|
| 703 | - |
|
| 704 | - /** |
|
| 705 | - * Get the URL to perform autocomplete request. |
|
| 706 | - * |
|
| 707 | - * @return string The URL to call to perform the autocomplete request. |
|
| 708 | - * @since 3.15.0 |
|
| 709 | - * |
|
| 710 | - */ |
|
| 711 | - public function get_autocomplete_url() { |
|
| 712 | - |
|
| 713 | - return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'autocomplete'; |
|
| 714 | - |
|
| 715 | - } |
|
| 716 | - |
|
| 717 | - /** |
|
| 718 | - * Get the URL to perform feedback deactivation request. |
|
| 719 | - * |
|
| 720 | - * @return string The URL to call to perform the feedback deactivation request. |
|
| 721 | - * @since 3.19.0 |
|
| 722 | - * |
|
| 723 | - */ |
|
| 724 | - public function get_deactivation_feedback_url() { |
|
| 725 | - |
|
| 726 | - return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'feedbacks'; |
|
| 727 | - |
|
| 728 | - } |
|
| 729 | - |
|
| 730 | - /** |
|
| 731 | - * Get the base API URL. |
|
| 732 | - * |
|
| 733 | - * @return string The base API URL. |
|
| 734 | - * @since 3.20.0 |
|
| 735 | - * |
|
| 736 | - */ |
|
| 737 | - public function get_api_url() { |
|
| 738 | - |
|
| 739 | - return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE; |
|
| 740 | - } |
|
| 559 | + $json = json_decode( $response['body'] ); |
|
| 560 | + /** |
|
| 561 | + * @since 3.27.7 |
|
| 562 | + * Remove the trailing slash returned from the new platform api. |
|
| 563 | + */ |
|
| 564 | + $dataset_uri = untrailingslashit( $json->datasetURI ); |
|
| 565 | + $package_type = isset( $json->packageType ) ? $json->packageType : null; |
|
| 566 | + |
|
| 567 | + $this->log->info( "Updating [ dataset uri :: $dataset_uri ][ package type :: $package_type ]..." ); |
|
| 568 | + |
|
| 569 | + $this->set_dataset_uri( $dataset_uri ); |
|
| 570 | + $this->set_package_type( $package_type ); |
|
| 571 | + } |
|
| 572 | + |
|
| 573 | + /** |
|
| 574 | + * Handle the edge case where a user submits the same key again |
|
| 575 | + * when he does not have the dataset uri to regain it. |
|
| 576 | + * |
|
| 577 | + * This can not be handled in the normal option update hook because |
|
| 578 | + * it is not being triggered when the save value equals to the one already |
|
| 579 | + * in the DB. |
|
| 580 | + * |
|
| 581 | + * @param mixed $value The new, unserialized option value. |
|
| 582 | + * @param mixed $old_value The old option value. |
|
| 583 | + * |
|
| 584 | + * @return mixed The same value in the $value parameter |
|
| 585 | + * @since 3.12.0 |
|
| 586 | + * |
|
| 587 | + */ |
|
| 588 | + function maybe_update_dataset_uri( $value, $old_value ) { |
|
| 589 | + |
|
| 590 | + // Check the old key value and the new one. Here we're only handling the |
|
| 591 | + // case where the key hasn't changed and the dataset URI isn't set. The |
|
| 592 | + // other case, i.e. a new key is inserted, is handled at `update_key`. |
|
| 593 | + $old_key = isset( $old_value['key'] ) ? $old_value['key'] : ''; |
|
| 594 | + $new_key = isset( $value['key'] ) ? $value['key'] : ''; |
|
| 595 | + |
|
| 596 | + $dataset_uri = $this->get_dataset_uri(); |
|
| 597 | + |
|
| 598 | + if ( ! empty( $new_key ) && $new_key === $old_key && empty( $dataset_uri ) ) { |
|
| 599 | + |
|
| 600 | + // make the request to the remote server to try to get the dataset uri. |
|
| 601 | + $this->get_remote_dataset_uri( $new_key ); |
|
| 602 | + } |
|
| 603 | + |
|
| 604 | + return $value; |
|
| 605 | + } |
|
| 606 | + |
|
| 607 | + /** |
|
| 608 | + * Get the API URI to retrieve the dataset URI using the WordLift Key. |
|
| 609 | + * |
|
| 610 | + * @param string $key The WordLift key to use. |
|
| 611 | + * |
|
| 612 | + * @return string The API URI. |
|
| 613 | + * @since 3.11.0 |
|
| 614 | + * |
|
| 615 | + */ |
|
| 616 | + public function get_accounts_by_key_dataset_uri( $key ) { |
|
| 617 | + |
|
| 618 | + return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . "accounts/key=$key/dataset_uri"; |
|
| 619 | + } |
|
| 620 | + |
|
| 621 | + /** |
|
| 622 | + * Get the `accounts` end point. |
|
| 623 | + * |
|
| 624 | + * @return string The `accounts` end point. |
|
| 625 | + * @since 3.16.0 |
|
| 626 | + * |
|
| 627 | + */ |
|
| 628 | + public function get_accounts() { |
|
| 629 | + |
|
| 630 | + return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'accounts'; |
|
| 631 | + } |
|
| 632 | + |
|
| 633 | + /** |
|
| 634 | + * Get the `link by default` option. |
|
| 635 | + * |
|
| 636 | + * @return bool True if entities must be linked by default otherwise false. |
|
| 637 | + * @since 3.13.0 |
|
| 638 | + * |
|
| 639 | + */ |
|
| 640 | + public function is_link_by_default() { |
|
| 641 | + |
|
| 642 | + return 'yes' === $this->get( 'wl_general_settings', self::LINK_BY_DEFAULT, 'yes' ); |
|
| 643 | + } |
|
| 644 | + |
|
| 645 | + /** |
|
| 646 | + * Set the `link by default` option. |
|
| 647 | + * |
|
| 648 | + * @param bool $value True to enabling linking by default, otherwise false. |
|
| 649 | + * |
|
| 650 | + * @since 3.13.0 |
|
| 651 | + * |
|
| 652 | + */ |
|
| 653 | + public function set_link_by_default( $value ) { |
|
| 654 | + |
|
| 655 | + $this->set( 'wl_general_settings', self::LINK_BY_DEFAULT, true === $value ? 'yes' : 'no' ); |
|
| 656 | + } |
|
| 657 | + |
|
| 658 | + /** |
|
| 659 | + * Get the 'analytics-enable' option. |
|
| 660 | + * |
|
| 661 | + * @return string 'no' or 'yes' representing bool. |
|
| 662 | + * @since 3.21.0 |
|
| 663 | + * |
|
| 664 | + */ |
|
| 665 | + public function is_analytics_enable() { |
|
| 666 | + return 'yes' === $this->get( 'wl_analytics_settings', self::ANALYTICS_ENABLE, 'no' ); |
|
| 667 | + } |
|
| 668 | + |
|
| 669 | + /** |
|
| 670 | + * Set the `analytics-enable` option. |
|
| 671 | + * |
|
| 672 | + * @param bool $value True to enabling analytics, otherwise false. |
|
| 673 | + * |
|
| 674 | + * @since 3.21.0 |
|
| 675 | + * |
|
| 676 | + */ |
|
| 677 | + public function set_is_analytics_enable( $value ) { |
|
| 678 | + |
|
| 679 | + $this->set( 'wl_general_settings', self::ANALYTICS_ENABLE, true === $value ? 'yes' : 'no' ); |
|
| 680 | + } |
|
| 681 | + |
|
| 682 | + /** |
|
| 683 | + * Get the 'analytics-entity-uri-dimention' option. |
|
| 684 | + * |
|
| 685 | + * @return int |
|
| 686 | + * @since 3.21.0 |
|
| 687 | + * |
|
| 688 | + */ |
|
| 689 | + public function get_analytics_entity_uri_dimension() { |
|
| 690 | + return (int) $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_URI_DIMENSION, 1 ); |
|
| 691 | + } |
|
| 692 | + |
|
| 693 | + /** |
|
| 694 | + * Get the 'analytics-entity-type-dimension' option. |
|
| 695 | + * |
|
| 696 | + * @return int |
|
| 697 | + * @since 3.21.0 |
|
| 698 | + * |
|
| 699 | + */ |
|
| 700 | + public function get_analytics_entity_type_dimension() { |
|
| 701 | + return $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_TYPE_DIMENSION, 2 ); |
|
| 702 | + } |
|
| 703 | + |
|
| 704 | + /** |
|
| 705 | + * Get the URL to perform autocomplete request. |
|
| 706 | + * |
|
| 707 | + * @return string The URL to call to perform the autocomplete request. |
|
| 708 | + * @since 3.15.0 |
|
| 709 | + * |
|
| 710 | + */ |
|
| 711 | + public function get_autocomplete_url() { |
|
| 712 | + |
|
| 713 | + return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'autocomplete'; |
|
| 714 | + |
|
| 715 | + } |
|
| 716 | + |
|
| 717 | + /** |
|
| 718 | + * Get the URL to perform feedback deactivation request. |
|
| 719 | + * |
|
| 720 | + * @return string The URL to call to perform the feedback deactivation request. |
|
| 721 | + * @since 3.19.0 |
|
| 722 | + * |
|
| 723 | + */ |
|
| 724 | + public function get_deactivation_feedback_url() { |
|
| 725 | + |
|
| 726 | + return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'feedbacks'; |
|
| 727 | + |
|
| 728 | + } |
|
| 729 | + |
|
| 730 | + /** |
|
| 731 | + * Get the base API URL. |
|
| 732 | + * |
|
| 733 | + * @return string The base API URL. |
|
| 734 | + * @since 3.20.0 |
|
| 735 | + * |
|
| 736 | + */ |
|
| 737 | + public function get_api_url() { |
|
| 738 | + |
|
| 739 | + return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE; |
|
| 740 | + } |
|
| 741 | 741 | |
| 742 | 742 | } |
@@ -12,7 +12,7 @@ discard block |
||
| 12 | 12 | |
| 13 | 13 | use Wordlift\Api\Default_Api_Service; |
| 14 | 14 | |
| 15 | -if ( ! defined( 'ABSPATH' ) ) { |
|
| 15 | +if ( ! defined('ABSPATH')) { |
|
| 16 | 16 | exit; |
| 17 | 17 | } |
| 18 | 18 | |
@@ -140,7 +140,7 @@ discard block |
||
| 140 | 140 | */ |
| 141 | 141 | public function __construct() { |
| 142 | 142 | |
| 143 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 143 | + $this->log = Wordlift_Log_Service::get_logger(get_class()); |
|
| 144 | 144 | |
| 145 | 145 | self::$instance = $this; |
| 146 | 146 | |
@@ -170,11 +170,11 @@ discard block |
||
| 170 | 170 | * @since 3.6.0 |
| 171 | 171 | * |
| 172 | 172 | */ |
| 173 | - private function get( $option, $key, $default = '' ) { |
|
| 173 | + private function get($option, $key, $default = '') { |
|
| 174 | 174 | |
| 175 | - $options = get_option( $option, array() ); |
|
| 175 | + $options = get_option($option, array()); |
|
| 176 | 176 | |
| 177 | - return isset( $options[ $key ] ) ? $options[ $key ] : $default; |
|
| 177 | + return isset($options[$key]) ? $options[$key] : $default; |
|
| 178 | 178 | } |
| 179 | 179 | |
| 180 | 180 | /** |
@@ -187,12 +187,12 @@ discard block |
||
| 187 | 187 | * @since 3.9.0 |
| 188 | 188 | * |
| 189 | 189 | */ |
| 190 | - private function set( $option, $key, $value ) { |
|
| 190 | + private function set($option, $key, $value) { |
|
| 191 | 191 | |
| 192 | - $values = get_option( $option ); |
|
| 193 | - $values = isset( $values ) ? $values : array(); |
|
| 194 | - $values[ $key ] = $value; |
|
| 195 | - update_option( $option, $values ); |
|
| 192 | + $values = get_option($option); |
|
| 193 | + $values = isset($values) ? $values : array(); |
|
| 194 | + $values[$key] = $value; |
|
| 195 | + update_option($option, $values); |
|
| 196 | 196 | |
| 197 | 197 | } |
| 198 | 198 | |
@@ -205,7 +205,7 @@ discard block |
||
| 205 | 205 | */ |
| 206 | 206 | public function get_entity_base_path() { |
| 207 | 207 | |
| 208 | - return $this->get( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, 'entity' ); |
|
| 208 | + return $this->get('wl_general_settings', self::ENTITY_BASE_PATH_KEY, 'entity'); |
|
| 209 | 209 | } |
| 210 | 210 | |
| 211 | 211 | /** |
@@ -216,9 +216,9 @@ discard block |
||
| 216 | 216 | * @since 3.9.0 |
| 217 | 217 | * |
| 218 | 218 | */ |
| 219 | - public function set_entity_base_path( $value ) { |
|
| 219 | + public function set_entity_base_path($value) { |
|
| 220 | 220 | |
| 221 | - $this->set( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, $value ); |
|
| 221 | + $this->set('wl_general_settings', self::ENTITY_BASE_PATH_KEY, $value); |
|
| 222 | 222 | |
| 223 | 223 | } |
| 224 | 224 | |
@@ -231,7 +231,7 @@ discard block |
||
| 231 | 231 | */ |
| 232 | 232 | public function is_skip_wizard() { |
| 233 | 233 | |
| 234 | - return $this->get( 'wl_general_settings', self::SKIP_WIZARD, false ); |
|
| 234 | + return $this->get('wl_general_settings', self::SKIP_WIZARD, false); |
|
| 235 | 235 | } |
| 236 | 236 | |
| 237 | 237 | /** |
@@ -242,9 +242,9 @@ discard block |
||
| 242 | 242 | * @since 3.9.0 |
| 243 | 243 | * |
| 244 | 244 | */ |
| 245 | - public function set_skip_wizard( $value ) { |
|
| 245 | + public function set_skip_wizard($value) { |
|
| 246 | 246 | |
| 247 | - $this->set( 'wl_general_settings', self::SKIP_WIZARD, true === $value ); |
|
| 247 | + $this->set('wl_general_settings', self::SKIP_WIZARD, true === $value); |
|
| 248 | 248 | |
| 249 | 249 | } |
| 250 | 250 | |
@@ -257,7 +257,7 @@ discard block |
||
| 257 | 257 | */ |
| 258 | 258 | public function get_key() { |
| 259 | 259 | |
| 260 | - return $this->get( 'wl_general_settings', self::KEY, '' ); |
|
| 260 | + return $this->get('wl_general_settings', self::KEY, ''); |
|
| 261 | 261 | } |
| 262 | 262 | |
| 263 | 263 | /** |
@@ -268,9 +268,9 @@ discard block |
||
| 268 | 268 | * @since 3.9.0 |
| 269 | 269 | * |
| 270 | 270 | */ |
| 271 | - public function set_key( $value ) { |
|
| 271 | + public function set_key($value) { |
|
| 272 | 272 | |
| 273 | - $this->set( 'wl_general_settings', self::KEY, $value ); |
|
| 273 | + $this->set('wl_general_settings', self::KEY, $value); |
|
| 274 | 274 | } |
| 275 | 275 | |
| 276 | 276 | /** |
@@ -284,7 +284,7 @@ discard block |
||
| 284 | 284 | */ |
| 285 | 285 | public function get_language_code() { |
| 286 | 286 | |
| 287 | - return $this->get( 'wl_general_settings', self::LANGUAGE, 'en' ); |
|
| 287 | + return $this->get('wl_general_settings', self::LANGUAGE, 'en'); |
|
| 288 | 288 | } |
| 289 | 289 | |
| 290 | 290 | /** |
@@ -295,9 +295,9 @@ discard block |
||
| 295 | 295 | * @since 3.9.0 |
| 296 | 296 | * |
| 297 | 297 | */ |
| 298 | - public function set_language_code( $value ) { |
|
| 298 | + public function set_language_code($value) { |
|
| 299 | 299 | |
| 300 | - $this->set( 'wl_general_settings', self::LANGUAGE, $value ); |
|
| 300 | + $this->set('wl_general_settings', self::LANGUAGE, $value); |
|
| 301 | 301 | |
| 302 | 302 | } |
| 303 | 303 | |
@@ -309,9 +309,9 @@ discard block |
||
| 309 | 309 | * @since 3.19.0 |
| 310 | 310 | * |
| 311 | 311 | */ |
| 312 | - public function set_diagnostic_preferences( $value ) { |
|
| 312 | + public function set_diagnostic_preferences($value) { |
|
| 313 | 313 | |
| 314 | - $this->set( 'wl_general_settings', self::SEND_DIAGNOSTIC, $value ); |
|
| 314 | + $this->set('wl_general_settings', self::SEND_DIAGNOSTIC, $value); |
|
| 315 | 315 | |
| 316 | 316 | } |
| 317 | 317 | |
@@ -322,7 +322,7 @@ discard block |
||
| 322 | 322 | */ |
| 323 | 323 | public function get_diagnostic_preferences() { |
| 324 | 324 | |
| 325 | - return $this->get( 'wl_general_settings', self::SEND_DIAGNOSTIC, 'no' ); |
|
| 325 | + return $this->get('wl_general_settings', self::SEND_DIAGNOSTIC, 'no'); |
|
| 326 | 326 | } |
| 327 | 327 | |
| 328 | 328 | /** |
@@ -334,7 +334,7 @@ discard block |
||
| 334 | 334 | */ |
| 335 | 335 | public function get_country_code() { |
| 336 | 336 | |
| 337 | - return $this->get( 'wl_general_settings', self::COUNTRY_CODE, 'us' ); |
|
| 337 | + return $this->get('wl_general_settings', self::COUNTRY_CODE, 'us'); |
|
| 338 | 338 | } |
| 339 | 339 | |
| 340 | 340 | /** |
@@ -345,9 +345,9 @@ discard block |
||
| 345 | 345 | * @since 3.18.0 |
| 346 | 346 | * |
| 347 | 347 | */ |
| 348 | - public function set_country_code( $value ) { |
|
| 348 | + public function set_country_code($value) { |
|
| 349 | 349 | |
| 350 | - $this->set( 'wl_general_settings', self::COUNTRY_CODE, $value ); |
|
| 350 | + $this->set('wl_general_settings', self::COUNTRY_CODE, $value); |
|
| 351 | 351 | |
| 352 | 352 | } |
| 353 | 353 | |
@@ -363,7 +363,7 @@ discard block |
||
| 363 | 363 | */ |
| 364 | 364 | public function get_publisher_id() { |
| 365 | 365 | |
| 366 | - return $this->get( 'wl_general_settings', self::PUBLISHER_ID, null ); |
|
| 366 | + return $this->get('wl_general_settings', self::PUBLISHER_ID, null); |
|
| 367 | 367 | } |
| 368 | 368 | |
| 369 | 369 | /** |
@@ -374,9 +374,9 @@ discard block |
||
| 374 | 374 | * @since 3.9.0 |
| 375 | 375 | * |
| 376 | 376 | */ |
| 377 | - public function set_publisher_id( $value ) { |
|
| 377 | + public function set_publisher_id($value) { |
|
| 378 | 378 | |
| 379 | - $this->set( 'wl_general_settings', self::PUBLISHER_ID, $value ); |
|
| 379 | + $this->set('wl_general_settings', self::PUBLISHER_ID, $value); |
|
| 380 | 380 | |
| 381 | 381 | } |
| 382 | 382 | |
@@ -390,8 +390,8 @@ discard block |
||
| 390 | 390 | */ |
| 391 | 391 | public function get_dataset_uri() { |
| 392 | 392 | |
| 393 | - if ( apply_filters( 'wl_features__enable__dataset', true ) ) { |
|
| 394 | - return $this->get( 'wl_advanced_settings', self::DATASET_URI, null ); |
|
| 393 | + if (apply_filters('wl_features__enable__dataset', true)) { |
|
| 394 | + return $this->get('wl_advanced_settings', self::DATASET_URI, null); |
|
| 395 | 395 | } else { |
| 396 | 396 | return null; |
| 397 | 397 | } |
@@ -405,9 +405,9 @@ discard block |
||
| 405 | 405 | * @since 3.10.0 |
| 406 | 406 | * |
| 407 | 407 | */ |
| 408 | - public function set_dataset_uri( $value ) { |
|
| 408 | + public function set_dataset_uri($value) { |
|
| 409 | 409 | |
| 410 | - $this->set( 'wl_advanced_settings', self::DATASET_URI, $value ); |
|
| 410 | + $this->set('wl_advanced_settings', self::DATASET_URI, $value); |
|
| 411 | 411 | } |
| 412 | 412 | |
| 413 | 413 | /** |
@@ -419,7 +419,7 @@ discard block |
||
| 419 | 419 | */ |
| 420 | 420 | public function get_package_type() { |
| 421 | 421 | |
| 422 | - return $this->get( 'wl_advanced_settings', self::PACKAGE_TYPE, null ); |
|
| 422 | + return $this->get('wl_advanced_settings', self::PACKAGE_TYPE, null); |
|
| 423 | 423 | } |
| 424 | 424 | |
| 425 | 425 | /** |
@@ -430,8 +430,8 @@ discard block |
||
| 430 | 430 | * @since 3.20.0 |
| 431 | 431 | * |
| 432 | 432 | */ |
| 433 | - public function set_package_type( $value ) { |
|
| 434 | - $this->set( 'wl_advanced_settings', self::PACKAGE_TYPE, $value ); |
|
| 433 | + public function set_package_type($value) { |
|
| 434 | + $this->set('wl_advanced_settings', self::PACKAGE_TYPE, $value); |
|
| 435 | 435 | } |
| 436 | 436 | |
| 437 | 437 | /** |
@@ -447,11 +447,11 @@ discard block |
||
| 447 | 447 | * @param array $old_value The old settings. |
| 448 | 448 | * @param array $new_value The new settings. |
| 449 | 449 | */ |
| 450 | - public function update_key( $old_value, $new_value ) { |
|
| 450 | + public function update_key($old_value, $new_value) { |
|
| 451 | 451 | |
| 452 | 452 | // Check the old key value and the new one. We're going to ask for the dataset URI only if the key has changed. |
| 453 | 453 | // $old_key = isset( $old_value['key'] ) ? $old_value['key'] : ''; |
| 454 | - $new_key = isset( $new_value['key'] ) ? $new_value['key'] : ''; |
|
| 454 | + $new_key = isset($new_value['key']) ? $new_value['key'] : ''; |
|
| 455 | 455 | |
| 456 | 456 | // If the key hasn't changed, don't do anything. |
| 457 | 457 | // WARN The 'update_option' hook is fired only if the new and old value are not equal. |
@@ -460,12 +460,12 @@ discard block |
||
| 460 | 460 | // } |
| 461 | 461 | |
| 462 | 462 | // If the key is empty, empty the dataset URI. |
| 463 | - if ( '' === $new_key ) { |
|
| 464 | - $this->set_dataset_uri( '' ); |
|
| 463 | + if ('' === $new_key) { |
|
| 464 | + $this->set_dataset_uri(''); |
|
| 465 | 465 | } |
| 466 | 466 | |
| 467 | 467 | // make the request to the remote server. |
| 468 | - $this->get_remote_dataset_uri( $new_key ); |
|
| 468 | + $this->get_remote_dataset_uri($new_key); |
|
| 469 | 469 | |
| 470 | 470 | } |
| 471 | 471 | |
@@ -481,15 +481,15 @@ discard block |
||
| 481 | 481 | * |
| 482 | 482 | * @since 3.17.0 send the site URL and get the dataset URI. |
| 483 | 483 | */ |
| 484 | - public function get_remote_dataset_uri( $key ) { |
|
| 484 | + public function get_remote_dataset_uri($key) { |
|
| 485 | 485 | |
| 486 | - $this->log->trace( 'Getting the remote dataset URI and package type...' ); |
|
| 486 | + $this->log->trace('Getting the remote dataset URI and package type...'); |
|
| 487 | 487 | |
| 488 | - if ( empty( $key ) ) { |
|
| 489 | - $this->log->warn( 'Key set to empty value.' ); |
|
| 488 | + if (empty($key)) { |
|
| 489 | + $this->log->warn('Key set to empty value.'); |
|
| 490 | 490 | |
| 491 | - $this->set_dataset_uri( '' ); |
|
| 492 | - $this->set_package_type( null ); |
|
| 491 | + $this->set_dataset_uri(''); |
|
| 492 | + $this->set_package_type(null); |
|
| 493 | 493 | |
| 494 | 494 | return; |
| 495 | 495 | } |
@@ -504,15 +504,15 @@ discard block |
||
| 504 | 504 | * @since 3.20.0 |
| 505 | 505 | * |
| 506 | 506 | */ |
| 507 | - $home_url = get_option( 'home' ); |
|
| 508 | - $site_url = apply_filters( 'wl_production_site_url', untrailingslashit( $home_url ) ); |
|
| 507 | + $home_url = get_option('home'); |
|
| 508 | + $site_url = apply_filters('wl_production_site_url', untrailingslashit($home_url)); |
|
| 509 | 509 | |
| 510 | 510 | // Build the URL. |
| 511 | 511 | $url = '/accounts' |
| 512 | - . '?key=' . rawurlencode( $key ) |
|
| 513 | - . '&url=' . rawurlencode( $site_url ) |
|
| 514 | - . '&country=' . $this->get_country_code() |
|
| 515 | - . '&language=' . $this->get_language_code(); |
|
| 512 | + . '?key='.rawurlencode($key) |
|
| 513 | + . '&url='.rawurlencode($site_url) |
|
| 514 | + . '&country='.$this->get_country_code() |
|
| 515 | + . '&language='.$this->get_language_code(); |
|
| 516 | 516 | |
| 517 | 517 | $api_service = Default_Api_Service::get_instance(); |
| 518 | 518 | /** |
@@ -522,31 +522,31 @@ discard block |
||
| 522 | 522 | $headers = array( |
| 523 | 523 | 'Authorization' => "Key $key", |
| 524 | 524 | ); |
| 525 | - $response = $api_service->request( 'PUT', $url, $headers )->get_response(); |
|
| 525 | + $response = $api_service->request('PUT', $url, $headers)->get_response(); |
|
| 526 | 526 | |
| 527 | 527 | // The response is an error. |
| 528 | - if ( is_wp_error( $response ) ) { |
|
| 529 | - $this->log->error( 'An error occurred setting the dataset URI: ' . $response->get_error_message() ); |
|
| 528 | + if (is_wp_error($response)) { |
|
| 529 | + $this->log->error('An error occurred setting the dataset URI: '.$response->get_error_message()); |
|
| 530 | 530 | |
| 531 | - $this->set_dataset_uri( '' ); |
|
| 532 | - $this->set_package_type( null ); |
|
| 531 | + $this->set_dataset_uri(''); |
|
| 532 | + $this->set_package_type(null); |
|
| 533 | 533 | |
| 534 | 534 | return; |
| 535 | 535 | } |
| 536 | 536 | |
| 537 | 537 | // The response is not OK. |
| 538 | - if ( ! is_array( $response ) || 200 !== (int) $response['response']['code'] ) { |
|
| 538 | + if ( ! is_array($response) || 200 !== (int) $response['response']['code']) { |
|
| 539 | 539 | $base_url = $api_service->get_base_url(); |
| 540 | 540 | |
| 541 | - if ( ! is_array( $response ) ) { |
|
| 542 | - $this->log->error( "Unexpected response when opening URL $base_url$url: " . var_export( $response, true ) ); |
|
| 541 | + if ( ! is_array($response)) { |
|
| 542 | + $this->log->error("Unexpected response when opening URL $base_url$url: ".var_export($response, true)); |
|
| 543 | 543 | } else { |
| 544 | - $this->log->error( "Unexpected status code when opening URL $base_url$url: " . $response['response']['code'] . "\n" . var_export( $response, true ) ); |
|
| 544 | + $this->log->error("Unexpected status code when opening URL $base_url$url: ".$response['response']['code']."\n".var_export($response, true)); |
|
| 545 | 545 | } |
| 546 | 546 | |
| 547 | 547 | |
| 548 | - $this->set_dataset_uri( '' ); |
|
| 549 | - $this->set_package_type( null ); |
|
| 548 | + $this->set_dataset_uri(''); |
|
| 549 | + $this->set_package_type(null); |
|
| 550 | 550 | |
| 551 | 551 | return; |
| 552 | 552 | } |
@@ -556,18 +556,18 @@ discard block |
||
| 556 | 556 | * |
| 557 | 557 | * @since 3.20.0 |
| 558 | 558 | */ |
| 559 | - $json = json_decode( $response['body'] ); |
|
| 559 | + $json = json_decode($response['body']); |
|
| 560 | 560 | /** |
| 561 | 561 | * @since 3.27.7 |
| 562 | 562 | * Remove the trailing slash returned from the new platform api. |
| 563 | 563 | */ |
| 564 | - $dataset_uri = untrailingslashit( $json->datasetURI ); |
|
| 565 | - $package_type = isset( $json->packageType ) ? $json->packageType : null; |
|
| 564 | + $dataset_uri = untrailingslashit($json->datasetURI); |
|
| 565 | + $package_type = isset($json->packageType) ? $json->packageType : null; |
|
| 566 | 566 | |
| 567 | - $this->log->info( "Updating [ dataset uri :: $dataset_uri ][ package type :: $package_type ]..." ); |
|
| 567 | + $this->log->info("Updating [ dataset uri :: $dataset_uri ][ package type :: $package_type ]..."); |
|
| 568 | 568 | |
| 569 | - $this->set_dataset_uri( $dataset_uri ); |
|
| 570 | - $this->set_package_type( $package_type ); |
|
| 569 | + $this->set_dataset_uri($dataset_uri); |
|
| 570 | + $this->set_package_type($package_type); |
|
| 571 | 571 | } |
| 572 | 572 | |
| 573 | 573 | /** |
@@ -585,20 +585,20 @@ discard block |
||
| 585 | 585 | * @since 3.12.0 |
| 586 | 586 | * |
| 587 | 587 | */ |
| 588 | - function maybe_update_dataset_uri( $value, $old_value ) { |
|
| 588 | + function maybe_update_dataset_uri($value, $old_value) { |
|
| 589 | 589 | |
| 590 | 590 | // Check the old key value and the new one. Here we're only handling the |
| 591 | 591 | // case where the key hasn't changed and the dataset URI isn't set. The |
| 592 | 592 | // other case, i.e. a new key is inserted, is handled at `update_key`. |
| 593 | - $old_key = isset( $old_value['key'] ) ? $old_value['key'] : ''; |
|
| 594 | - $new_key = isset( $value['key'] ) ? $value['key'] : ''; |
|
| 593 | + $old_key = isset($old_value['key']) ? $old_value['key'] : ''; |
|
| 594 | + $new_key = isset($value['key']) ? $value['key'] : ''; |
|
| 595 | 595 | |
| 596 | 596 | $dataset_uri = $this->get_dataset_uri(); |
| 597 | 597 | |
| 598 | - if ( ! empty( $new_key ) && $new_key === $old_key && empty( $dataset_uri ) ) { |
|
| 598 | + if ( ! empty($new_key) && $new_key === $old_key && empty($dataset_uri)) { |
|
| 599 | 599 | |
| 600 | 600 | // make the request to the remote server to try to get the dataset uri. |
| 601 | - $this->get_remote_dataset_uri( $new_key ); |
|
| 601 | + $this->get_remote_dataset_uri($new_key); |
|
| 602 | 602 | } |
| 603 | 603 | |
| 604 | 604 | return $value; |
@@ -613,9 +613,9 @@ discard block |
||
| 613 | 613 | * @since 3.11.0 |
| 614 | 614 | * |
| 615 | 615 | */ |
| 616 | - public function get_accounts_by_key_dataset_uri( $key ) { |
|
| 616 | + public function get_accounts_by_key_dataset_uri($key) { |
|
| 617 | 617 | |
| 618 | - return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . "accounts/key=$key/dataset_uri"; |
|
| 618 | + return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE."accounts/key=$key/dataset_uri"; |
|
| 619 | 619 | } |
| 620 | 620 | |
| 621 | 621 | /** |
@@ -627,7 +627,7 @@ discard block |
||
| 627 | 627 | */ |
| 628 | 628 | public function get_accounts() { |
| 629 | 629 | |
| 630 | - return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'accounts'; |
|
| 630 | + return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE.'accounts'; |
|
| 631 | 631 | } |
| 632 | 632 | |
| 633 | 633 | /** |
@@ -639,7 +639,7 @@ discard block |
||
| 639 | 639 | */ |
| 640 | 640 | public function is_link_by_default() { |
| 641 | 641 | |
| 642 | - return 'yes' === $this->get( 'wl_general_settings', self::LINK_BY_DEFAULT, 'yes' ); |
|
| 642 | + return 'yes' === $this->get('wl_general_settings', self::LINK_BY_DEFAULT, 'yes'); |
|
| 643 | 643 | } |
| 644 | 644 | |
| 645 | 645 | /** |
@@ -650,9 +650,9 @@ discard block |
||
| 650 | 650 | * @since 3.13.0 |
| 651 | 651 | * |
| 652 | 652 | */ |
| 653 | - public function set_link_by_default( $value ) { |
|
| 653 | + public function set_link_by_default($value) { |
|
| 654 | 654 | |
| 655 | - $this->set( 'wl_general_settings', self::LINK_BY_DEFAULT, true === $value ? 'yes' : 'no' ); |
|
| 655 | + $this->set('wl_general_settings', self::LINK_BY_DEFAULT, true === $value ? 'yes' : 'no'); |
|
| 656 | 656 | } |
| 657 | 657 | |
| 658 | 658 | /** |
@@ -663,7 +663,7 @@ discard block |
||
| 663 | 663 | * |
| 664 | 664 | */ |
| 665 | 665 | public function is_analytics_enable() { |
| 666 | - return 'yes' === $this->get( 'wl_analytics_settings', self::ANALYTICS_ENABLE, 'no' ); |
|
| 666 | + return 'yes' === $this->get('wl_analytics_settings', self::ANALYTICS_ENABLE, 'no'); |
|
| 667 | 667 | } |
| 668 | 668 | |
| 669 | 669 | /** |
@@ -674,9 +674,9 @@ discard block |
||
| 674 | 674 | * @since 3.21.0 |
| 675 | 675 | * |
| 676 | 676 | */ |
| 677 | - public function set_is_analytics_enable( $value ) { |
|
| 677 | + public function set_is_analytics_enable($value) { |
|
| 678 | 678 | |
| 679 | - $this->set( 'wl_general_settings', self::ANALYTICS_ENABLE, true === $value ? 'yes' : 'no' ); |
|
| 679 | + $this->set('wl_general_settings', self::ANALYTICS_ENABLE, true === $value ? 'yes' : 'no'); |
|
| 680 | 680 | } |
| 681 | 681 | |
| 682 | 682 | /** |
@@ -687,7 +687,7 @@ discard block |
||
| 687 | 687 | * |
| 688 | 688 | */ |
| 689 | 689 | public function get_analytics_entity_uri_dimension() { |
| 690 | - return (int) $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_URI_DIMENSION, 1 ); |
|
| 690 | + return (int) $this->get('wl_analytics_settings', self::ANALYTICS_ENTITY_URI_DIMENSION, 1); |
|
| 691 | 691 | } |
| 692 | 692 | |
| 693 | 693 | /** |
@@ -698,7 +698,7 @@ discard block |
||
| 698 | 698 | * |
| 699 | 699 | */ |
| 700 | 700 | public function get_analytics_entity_type_dimension() { |
| 701 | - return $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_TYPE_DIMENSION, 2 ); |
|
| 701 | + return $this->get('wl_analytics_settings', self::ANALYTICS_ENTITY_TYPE_DIMENSION, 2); |
|
| 702 | 702 | } |
| 703 | 703 | |
| 704 | 704 | /** |
@@ -710,7 +710,7 @@ discard block |
||
| 710 | 710 | */ |
| 711 | 711 | public function get_autocomplete_url() { |
| 712 | 712 | |
| 713 | - return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'autocomplete'; |
|
| 713 | + return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE.'autocomplete'; |
|
| 714 | 714 | |
| 715 | 715 | } |
| 716 | 716 | |
@@ -723,7 +723,7 @@ discard block |
||
| 723 | 723 | */ |
| 724 | 724 | public function get_deactivation_feedback_url() { |
| 725 | 725 | |
| 726 | - return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'feedbacks'; |
|
| 726 | + return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE.'feedbacks'; |
|
| 727 | 727 | |
| 728 | 728 | } |
| 729 | 729 | |
@@ -18,433 +18,433 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class Wordlift_Admin_Settings_Page extends Wordlift_Admin_Page { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * A singleton instance of the Notice service. |
|
| 23 | - * |
|
| 24 | - * @since 3.2.0 |
|
| 25 | - * @access private |
|
| 26 | - * @var \Wordlift_Notice_Service $instance A singleton instance of the Notice service. |
|
| 27 | - */ |
|
| 28 | - private static $instance; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * A {@link Wordlift_Entity_Service} instance. |
|
| 32 | - * |
|
| 33 | - * @since 3.11.0 |
|
| 34 | - * @access private |
|
| 35 | - * @var \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 36 | - */ |
|
| 37 | - private $entity_service; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * A {@link Wordlift_Configuration_Service} instance. |
|
| 41 | - * |
|
| 42 | - * @since 3.11.0 |
|
| 43 | - * @access private |
|
| 44 | - * @var \Wordlift_Configuration_Service $configuration_service A {@link Wordlift_Configuration_Service} instance. |
|
| 45 | - */ |
|
| 46 | - private $configuration_service; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * A {@link Wordlift_Admin_Input_Element} element renderer. |
|
| 50 | - * |
|
| 51 | - * @since 3.11.0 |
|
| 52 | - * @access private |
|
| 53 | - * @var \Wordlift_Admin_Input_Element $input_element An {@link Wordlift_Admin_Input_Element} element renderer. |
|
| 54 | - */ |
|
| 55 | - private $input_element; |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * A {@link Wordlift_Admin_Radio_Input_Element} element renderer. |
|
| 59 | - * |
|
| 60 | - * @since 3.13.0 |
|
| 61 | - * @access protected |
|
| 62 | - * @var \Wordlift_Admin_Radio_Input_Element $radio_input_element A {@link Wordlift_Admin_Radio_Input_Element} element renderer. |
|
| 63 | - */ |
|
| 64 | - private $radio_input_element; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * A {@link Wordlift_Admin_Language_Select_Element} element renderer. |
|
| 68 | - * |
|
| 69 | - * @since 3.11.0 |
|
| 70 | - * @access private |
|
| 71 | - * @var \Wordlift_Admin_Language_Select_Element $language_select_element A {@link Wordlift_Admin_Language_Select_Element} element renderer. |
|
| 72 | - */ |
|
| 73 | - private $language_select_element; |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * A {@link Wordlift_Admin_Country_Select_Element} element renderer. |
|
| 77 | - * |
|
| 78 | - * @since 3.18.0 |
|
| 79 | - * @access private |
|
| 80 | - * @var \Wordlift_Admin_Country_Select_Element $country_select_element A {@link Wordlift_Admin_Country_Select_Element} element renderer. |
|
| 81 | - */ |
|
| 82 | - private $country_select_element; |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * A {@link Wordlift_Admin_Publisher_Element} element renderer. |
|
| 86 | - * |
|
| 87 | - * @since 3.11.0 |
|
| 88 | - * @access private |
|
| 89 | - * @var \Wordlift_Admin_Publisher_Element $publisher_element A {@link Wordlift_Admin_Publisher_Element} element renderer. |
|
| 90 | - */ |
|
| 91 | - private $publisher_element; |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Create a {@link Wordlift_Admin_Settings_Page} instance. |
|
| 95 | - * |
|
| 96 | - * @param \Wordlift_Configuration_Service $configuration_service A {@link Wordlift_Configuration_Service} instance. |
|
| 97 | - * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 98 | - * @param \Wordlift_Admin_Input_Element $input_element A {@link Wordlift_Admin_Input_Element} element renderer. |
|
| 99 | - * @param \Wordlift_Admin_Language_Select_Element $language_select_element A {@link Wordlift_Admin_Language_Select_Element} element renderer. |
|
| 100 | - * @param \Wordlift_Admin_Country_Select_Element $country_select_element A {@link Wordlift_Admin_Country_Select_Element} element renderer. |
|
| 101 | - * @param \Wordlift_Admin_Publisher_Element $publisher_element A {@link Wordlift_Admin_Publisher_Element} element renderer. |
|
| 102 | - * @param \Wordlift_Admin_Radio_Input_Element $radio_input_element A {@link Wordlift_Admin_Radio_Input_Element} element renderer. |
|
| 103 | - * |
|
| 104 | - * @since 3.11.0 |
|
| 105 | - * |
|
| 106 | - */ |
|
| 107 | - function __construct( $configuration_service, $entity_service, $input_element, $language_select_element, $country_select_element, $publisher_element, $radio_input_element ) { |
|
| 108 | - |
|
| 109 | - $this->configuration_service = $configuration_service; |
|
| 110 | - $this->entity_service = $entity_service; |
|
| 111 | - |
|
| 112 | - // Set a reference to the UI elements. |
|
| 113 | - $this->input_element = $input_element; |
|
| 114 | - $this->radio_input_element = $radio_input_element; |
|
| 115 | - $this->language_select_element = $language_select_element; |
|
| 116 | - $this->country_select_element = $country_select_element; |
|
| 117 | - $this->publisher_element = $publisher_element; |
|
| 118 | - |
|
| 119 | - self::$instance = $this; |
|
| 120 | - |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Get the singleton instance of the Notice service. |
|
| 125 | - * |
|
| 126 | - * @return \Wordlift_Admin_Settings_Page The singleton instance of the settings page service. |
|
| 127 | - * @since 3.14.0 |
|
| 128 | - */ |
|
| 129 | - public static function get_instance() { |
|
| 130 | - |
|
| 131 | - return self::$instance; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * @inheritdoc |
|
| 136 | - */ |
|
| 137 | - function get_parent_slug() { |
|
| 138 | - |
|
| 139 | - return 'wl_admin_menu'; |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * @inheritdoc |
|
| 144 | - */ |
|
| 145 | - function get_capability() { |
|
| 146 | - |
|
| 147 | - return 'manage_options'; |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * @inheritdoc |
|
| 152 | - */ |
|
| 153 | - function get_page_title() { |
|
| 154 | - |
|
| 155 | - return 'WordLift Settings'; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * @inheritdoc |
|
| 160 | - */ |
|
| 161 | - function get_menu_title() { |
|
| 162 | - |
|
| 163 | - return 'Settings'; |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * @inheritdoc |
|
| 168 | - */ |
|
| 169 | - function get_menu_slug() { |
|
| 170 | - |
|
| 171 | - return 'wl_configuration_admin_menu'; |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * @inheritdoc |
|
| 176 | - */ |
|
| 177 | - function get_partial_name() { |
|
| 178 | - |
|
| 179 | - return 'wordlift-admin-settings-page.php'; |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - /** |
|
| 183 | - * @inheritdoc |
|
| 184 | - */ |
|
| 185 | - public function enqueue_scripts() { |
|
| 186 | - |
|
| 187 | - // Enqueue the media scripts to be used for the publisher's logo selection. |
|
| 188 | - wp_enqueue_media(); |
|
| 189 | - |
|
| 190 | - // JavaScript required for the settings page. |
|
| 191 | - // @todo: try to move to the `wordlift-admin.bundle.js`. |
|
| 192 | - wp_enqueue_script( 'wordlift-admin-settings-page', plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/1/settings.js', array( 'wp-util' ) ); |
|
| 193 | - wp_enqueue_style( 'wordlift-admin-settings-page', plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/1/settings.css' ); |
|
| 194 | - |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * Configure all the configuration parameters. |
|
| 199 | - * |
|
| 200 | - * Called by the *admin_init* hook. |
|
| 201 | - * |
|
| 202 | - * @since 3.11.0 |
|
| 203 | - */ |
|
| 204 | - function admin_init() { |
|
| 205 | - // Register WordLift's general settings, providing our own sanitize callback |
|
| 206 | - // which will also check whether the user filled the WL Publisher form. |
|
| 207 | - register_setting( |
|
| 208 | - 'wl_general_settings', |
|
| 209 | - 'wl_general_settings', |
|
| 210 | - array( $this, 'sanitize_callback' ) |
|
| 211 | - ); |
|
| 212 | - |
|
| 213 | - // Add the general settings section. |
|
| 214 | - add_settings_section( |
|
| 215 | - 'wl_general_settings_section', // ID used to identify this section and with which to register options. |
|
| 216 | - '', // Section header. |
|
| 217 | - '', // Callback used to render the description of the section. |
|
| 218 | - 'wl_general_settings' // Page on which to add this section of options. |
|
| 219 | - ); |
|
| 220 | - |
|
| 221 | - $key_args = array( |
|
| 222 | - 'id' => 'wl-key', |
|
| 223 | - 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::KEY . ']', |
|
| 224 | - 'value' => $this->configuration_service->get_key(), |
|
| 225 | - 'description' => __( 'Insert the <a href="https://www.wordlift.io/blogger">WordLift Key</a> you received via email.', 'wordlift' ) |
|
| 226 | - . ' [' . get_option( 'home' ) . ']', |
|
| 227 | - ); |
|
| 228 | - |
|
| 229 | - // Before we were used to validate the key beforehand, but this means |
|
| 230 | - // an http call whenever a page is opened in the admin area. Therefore |
|
| 231 | - // we now leave the input `untouched`, leaving to the client to update |
|
| 232 | - // the `css_class`. |
|
| 233 | - // |
|
| 234 | - // See https://github.com/insideout10/wordlift-plugin/issues/669. |
|
| 235 | - $key_args['css_class'] = 'untouched'; |
|
| 236 | - |
|
| 237 | - // Add the `key` field. |
|
| 238 | - add_settings_field( |
|
| 239 | - 'wl-key', // Element id used to identify the field throughout the theme. |
|
| 240 | - __( 'WordLift Key', 'wordlift' ), // The label to the left of the option interface element. |
|
| 241 | - // The name of the function responsible for rendering the option interface. |
|
| 242 | - array( $this->input_element, 'render' ), |
|
| 243 | - 'wl_general_settings', // The page on which this option will be displayed. |
|
| 244 | - 'wl_general_settings_section', // The name of the section to which this field belongs. |
|
| 245 | - $key_args // The array of arguments to pass to the callback. In this case, just a description. |
|
| 246 | - ); |
|
| 247 | - |
|
| 248 | - // Entity Base Path input. |
|
| 249 | - $entity_base_path_args = array( |
|
| 250 | - // The array of arguments to pass to the callback. In this case, just a description. |
|
| 251 | - 'id' => 'wl-entity-base-path', |
|
| 252 | - 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::ENTITY_BASE_PATH_KEY . ']', |
|
| 253 | - 'value' => $this->configuration_service->get_entity_base_path(), |
|
| 254 | - /* translators: Placeholders: %s - a link to FAQ's page. */ |
|
| 255 | - 'description' => sprintf( __( 'All new pages created with WordLift, will be stored inside your internal vocabulary. You can customize the url pattern of these pages in the field above. Check our <a href="%s">FAQs</a> if you need more info.', 'wordlift' ), 'https://wordlift.io/wordlift-user-faqs/#10-why-and-how-should-i-customize-the-url-of-the-entity-pages-created-in-my-vocabulary' ), |
|
| 256 | - ); |
|
| 257 | - |
|
| 258 | - // The following call is very heavy on large web sites and is always run |
|
| 259 | - // also when not needed: |
|
| 260 | - // $entity_base_path_args['readonly'] = 0 < $this->entity_service->count(); |
|
| 261 | - // |
|
| 262 | - // It is now replaced by a filter to add the `readonly` flag to the |
|
| 263 | - // input element when this is actually rendered. |
|
| 264 | - add_filter( 'wl_admin_input_element_params', array( |
|
| 265 | - $this, |
|
| 266 | - 'entity_path_input_element_params', |
|
| 267 | - ) ); |
|
| 268 | - |
|
| 269 | - // Add the `wl_entity_base_path` field. |
|
| 270 | - add_settings_field( |
|
| 271 | - 'wl-entity-base-path', // ID used to identify the field throughout the theme |
|
| 272 | - __( 'Entity Base Path', 'wordlift' ), // The label to the left of the option interface element |
|
| 273 | - // The name of the function responsible for rendering the option interface |
|
| 274 | - array( $this->input_element, 'render', ), |
|
| 275 | - 'wl_general_settings', // The page on which this option will be displayed |
|
| 276 | - 'wl_general_settings_section', // The name of the section to which this field belongs |
|
| 277 | - $entity_base_path_args |
|
| 278 | - ); |
|
| 279 | - |
|
| 280 | - // Add the `language_name` field. |
|
| 281 | - add_settings_field( |
|
| 282 | - 'wl-site-language', |
|
| 283 | - __( 'Site Language', 'wordlift' ), |
|
| 284 | - array( $this->language_select_element, 'render' ), |
|
| 285 | - 'wl_general_settings', |
|
| 286 | - 'wl_general_settings_section', |
|
| 287 | - array( |
|
| 288 | - // The array of arguments to pass to the callback. In this case, just a description. |
|
| 289 | - 'id' => 'wl-site-language', |
|
| 290 | - 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::LANGUAGE . ']', |
|
| 291 | - 'value' => $this->configuration_service->get_language_code(), |
|
| 292 | - 'description' => __( 'Each WordLift Key can be used only in one language. Pick yours.', 'wordlift' ), |
|
| 293 | - ) |
|
| 294 | - ); |
|
| 295 | - |
|
| 296 | - // Add the `country_code` field. |
|
| 297 | - add_settings_field( |
|
| 298 | - 'wl-country-code', |
|
| 299 | - _x( 'Country', 'wordlift' ), |
|
| 300 | - array( $this->country_select_element, 'render' ), |
|
| 301 | - 'wl_general_settings', |
|
| 302 | - 'wl_general_settings_section', |
|
| 303 | - array( |
|
| 304 | - 'id' => 'wl-country-code', |
|
| 305 | - 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::COUNTRY_CODE . ']', |
|
| 306 | - 'value' => $this->configuration_service->get_country_code(), |
|
| 307 | - 'description' => __( 'Please select a country.', 'wordlift' ), |
|
| 308 | - 'notice' => __( 'The selected language is not supported in this country.</br>Please choose another country or language.', 'wordlift' ), |
|
| 309 | - ) |
|
| 310 | - ); |
|
| 311 | - |
|
| 312 | - // Add the `publisher` field. |
|
| 313 | - add_settings_field( |
|
| 314 | - 'wl-publisher-id', |
|
| 315 | - __( 'Publisher', 'wordlift' ), |
|
| 316 | - array( $this->publisher_element, 'render' ), |
|
| 317 | - 'wl_general_settings', |
|
| 318 | - 'wl_general_settings_section', |
|
| 319 | - array( |
|
| 320 | - 'id' => 'wl-publisher-id', |
|
| 321 | - 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::PUBLISHER_ID . ']', |
|
| 322 | - ) |
|
| 323 | - ); |
|
| 324 | - |
|
| 325 | - // Add the `link by default` field. |
|
| 326 | - add_settings_field( |
|
| 327 | - 'wl-link-by-default', |
|
| 328 | - __( 'Link by Default', 'wordlift' ), |
|
| 329 | - array( $this->radio_input_element, 'render' ), |
|
| 330 | - 'wl_general_settings', |
|
| 331 | - 'wl_general_settings_section', |
|
| 332 | - array( |
|
| 333 | - 'id' => 'wl-link-by-default', |
|
| 334 | - 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::LINK_BY_DEFAULT . ']', |
|
| 335 | - 'value' => $this->configuration_service->is_link_by_default() ? 'yes' : 'no', |
|
| 336 | - 'description' => __( 'Whether to link entities by default or not. This setting applies to all the entities.', 'wordlift' ), |
|
| 337 | - ) |
|
| 338 | - ); |
|
| 339 | - |
|
| 340 | - // Add the `diagnostic data` field. |
|
| 341 | - add_settings_field( |
|
| 342 | - 'wl-send-diagnostic', |
|
| 343 | - __( 'Send Diagnostic Data', 'wordlift' ), |
|
| 344 | - array( $this->radio_input_element, 'render' ), |
|
| 345 | - 'wl_general_settings', |
|
| 346 | - 'wl_general_settings_section', |
|
| 347 | - array( |
|
| 348 | - 'id' => 'wl-send-diagnostic', |
|
| 349 | - 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::SEND_DIAGNOSTIC . ']', |
|
| 350 | - 'value' => 'yes' === $this->configuration_service->get_diagnostic_preferences() ? 'yes' : 'no', |
|
| 351 | - 'description' => __( 'Whether to send diagnostic data or not.', 'wordlift' ), |
|
| 352 | - ) |
|
| 353 | - ); |
|
| 354 | - |
|
| 355 | - } |
|
| 356 | - |
|
| 357 | - /** |
|
| 358 | - * Filter the {@link Wordlift_Admin_Input_Element} in order to add the |
|
| 359 | - * `readonly` flag to the `wl-entity-base-path` input. |
|
| 360 | - * |
|
| 361 | - * @param array $args An array of {@link Wordlift_Admin_Input_Element} parameters. |
|
| 362 | - * |
|
| 363 | - * @return array The updated array. |
|
| 364 | - * @since 3.17.0 |
|
| 365 | - * |
|
| 366 | - */ |
|
| 367 | - public function entity_path_input_element_params( $args ) { |
|
| 368 | - |
|
| 369 | - // Bail out if it's not the `wl-entity-base-path`). |
|
| 370 | - if ( 'wl-entity-base-path' !== $args['id'] ) { |
|
| 371 | - return $args; |
|
| 372 | - } |
|
| 373 | - |
|
| 374 | - // Set the readonly flag according to the entities count. |
|
| 375 | - $args['readonly'] = 0 < $this->entity_service->count(); |
|
| 376 | - |
|
| 377 | - // Return the updated args. |
|
| 378 | - return $args; |
|
| 379 | - } |
|
| 380 | - |
|
| 381 | - /** |
|
| 382 | - * Sanitize the configuration settings to be stored. |
|
| 383 | - * |
|
| 384 | - * If a new entity is being created for the publisher, create it and set The |
|
| 385 | - * publisher setting. |
|
| 386 | - * |
|
| 387 | - * @param array $input The configuration settings array. |
|
| 388 | - * |
|
| 389 | - * @return array The sanitized input array. |
|
| 390 | - * @since 3.11.0 |
|
| 391 | - * |
|
| 392 | - */ |
|
| 393 | - function sanitize_callback( $input ) { |
|
| 394 | - |
|
| 395 | - // Validate the selected country. |
|
| 396 | - $this->validate_country(); |
|
| 397 | - |
|
| 398 | - // Check whether a publisher name has been set. |
|
| 399 | - if ( isset( $_POST['wl_publisher'] ) && ! empty( $_POST['wl_publisher']['name'] ) ) { // WPCS: CSRF, input var, sanitization ok. |
|
| 400 | - $name = $_POST['wl_publisher']['name']; // WPCS: CSRF, input var, sanitization ok. |
|
| 401 | - $type = $_POST['wl_publisher']['type']; // WPCS: CSRF, input var, sanitization ok. |
|
| 402 | - $thumbnail_id = $_POST['wl_publisher']['thumbnail_id'] ?: null; // WPCS: CSRF, input var, sanitization ok. |
|
| 403 | - |
|
| 404 | - // Set the type URI, either http://schema.org/Person or http://schema.org/Organization. |
|
| 405 | - $type_uri = sprintf( 'http://schema.org/%s', 'organization' === $type ? 'Organization' : 'Person' ); |
|
| 406 | - |
|
| 407 | - // Create an entity for the publisher and assign it to the input |
|
| 408 | - // parameter which WordPress automatically saves into the settings. |
|
| 409 | - $input['publisher_id'] = $this->entity_service->create( $name, $type_uri, $thumbnail_id, 'publish' ); |
|
| 410 | - } |
|
| 411 | - |
|
| 412 | - return $input; |
|
| 413 | - } |
|
| 414 | - |
|
| 415 | - /** |
|
| 416 | - * Check whether the currently selected country supports the site language. |
|
| 417 | - * |
|
| 418 | - * @since 3.18.0 |
|
| 419 | - */ |
|
| 420 | - private function validate_country() { |
|
| 421 | - |
|
| 422 | - // Bail out if for some reason the country and language are not set. |
|
| 423 | - if ( |
|
| 424 | - empty( $_POST['wl_general_settings']['site_language'] ) && // WPCS: CSRF, input var, sanitization ok. |
|
| 425 | - empty( $_POST['wl_general_settings']['country_code'] ) // WPCS: CSRF, input var, sanitization ok. |
|
| 426 | - ) { |
|
| 427 | - return; |
|
| 428 | - } |
|
| 429 | - |
|
| 430 | - // Get the values. |
|
| 431 | - $language = $_POST['wl_general_settings']['site_language']; // WPCS: CSRF, input var, sanitization ok. |
|
| 432 | - $country = $_POST['wl_general_settings']['country_code']; // WPCS: CSRF, input var, sanitization ok. |
|
| 433 | - $codes = Wordlift_Countries::get_codes(); |
|
| 434 | - |
|
| 435 | - // Check whether the chosen country has language limitations |
|
| 436 | - // and whether the chosen language is supported for that country. |
|
| 437 | - if ( |
|
| 438 | - ! empty( $codes[ $country ] ) && |
|
| 439 | - ! in_array( $language, $codes[ $country ] ) |
|
| 440 | - ) { |
|
| 441 | - // Otherwise add an error. |
|
| 442 | - add_settings_error( |
|
| 443 | - 'wl-country-code', |
|
| 444 | - esc_attr( 'settings_updated' ), |
|
| 445 | - _x( 'The selected language is not supported for the currently chosen country. Please choose another country or language.', 'wordlift' ) |
|
| 446 | - ); |
|
| 447 | - } |
|
| 448 | - } |
|
| 21 | + /** |
|
| 22 | + * A singleton instance of the Notice service. |
|
| 23 | + * |
|
| 24 | + * @since 3.2.0 |
|
| 25 | + * @access private |
|
| 26 | + * @var \Wordlift_Notice_Service $instance A singleton instance of the Notice service. |
|
| 27 | + */ |
|
| 28 | + private static $instance; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * A {@link Wordlift_Entity_Service} instance. |
|
| 32 | + * |
|
| 33 | + * @since 3.11.0 |
|
| 34 | + * @access private |
|
| 35 | + * @var \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 36 | + */ |
|
| 37 | + private $entity_service; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * A {@link Wordlift_Configuration_Service} instance. |
|
| 41 | + * |
|
| 42 | + * @since 3.11.0 |
|
| 43 | + * @access private |
|
| 44 | + * @var \Wordlift_Configuration_Service $configuration_service A {@link Wordlift_Configuration_Service} instance. |
|
| 45 | + */ |
|
| 46 | + private $configuration_service; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * A {@link Wordlift_Admin_Input_Element} element renderer. |
|
| 50 | + * |
|
| 51 | + * @since 3.11.0 |
|
| 52 | + * @access private |
|
| 53 | + * @var \Wordlift_Admin_Input_Element $input_element An {@link Wordlift_Admin_Input_Element} element renderer. |
|
| 54 | + */ |
|
| 55 | + private $input_element; |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * A {@link Wordlift_Admin_Radio_Input_Element} element renderer. |
|
| 59 | + * |
|
| 60 | + * @since 3.13.0 |
|
| 61 | + * @access protected |
|
| 62 | + * @var \Wordlift_Admin_Radio_Input_Element $radio_input_element A {@link Wordlift_Admin_Radio_Input_Element} element renderer. |
|
| 63 | + */ |
|
| 64 | + private $radio_input_element; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * A {@link Wordlift_Admin_Language_Select_Element} element renderer. |
|
| 68 | + * |
|
| 69 | + * @since 3.11.0 |
|
| 70 | + * @access private |
|
| 71 | + * @var \Wordlift_Admin_Language_Select_Element $language_select_element A {@link Wordlift_Admin_Language_Select_Element} element renderer. |
|
| 72 | + */ |
|
| 73 | + private $language_select_element; |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * A {@link Wordlift_Admin_Country_Select_Element} element renderer. |
|
| 77 | + * |
|
| 78 | + * @since 3.18.0 |
|
| 79 | + * @access private |
|
| 80 | + * @var \Wordlift_Admin_Country_Select_Element $country_select_element A {@link Wordlift_Admin_Country_Select_Element} element renderer. |
|
| 81 | + */ |
|
| 82 | + private $country_select_element; |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * A {@link Wordlift_Admin_Publisher_Element} element renderer. |
|
| 86 | + * |
|
| 87 | + * @since 3.11.0 |
|
| 88 | + * @access private |
|
| 89 | + * @var \Wordlift_Admin_Publisher_Element $publisher_element A {@link Wordlift_Admin_Publisher_Element} element renderer. |
|
| 90 | + */ |
|
| 91 | + private $publisher_element; |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Create a {@link Wordlift_Admin_Settings_Page} instance. |
|
| 95 | + * |
|
| 96 | + * @param \Wordlift_Configuration_Service $configuration_service A {@link Wordlift_Configuration_Service} instance. |
|
| 97 | + * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 98 | + * @param \Wordlift_Admin_Input_Element $input_element A {@link Wordlift_Admin_Input_Element} element renderer. |
|
| 99 | + * @param \Wordlift_Admin_Language_Select_Element $language_select_element A {@link Wordlift_Admin_Language_Select_Element} element renderer. |
|
| 100 | + * @param \Wordlift_Admin_Country_Select_Element $country_select_element A {@link Wordlift_Admin_Country_Select_Element} element renderer. |
|
| 101 | + * @param \Wordlift_Admin_Publisher_Element $publisher_element A {@link Wordlift_Admin_Publisher_Element} element renderer. |
|
| 102 | + * @param \Wordlift_Admin_Radio_Input_Element $radio_input_element A {@link Wordlift_Admin_Radio_Input_Element} element renderer. |
|
| 103 | + * |
|
| 104 | + * @since 3.11.0 |
|
| 105 | + * |
|
| 106 | + */ |
|
| 107 | + function __construct( $configuration_service, $entity_service, $input_element, $language_select_element, $country_select_element, $publisher_element, $radio_input_element ) { |
|
| 108 | + |
|
| 109 | + $this->configuration_service = $configuration_service; |
|
| 110 | + $this->entity_service = $entity_service; |
|
| 111 | + |
|
| 112 | + // Set a reference to the UI elements. |
|
| 113 | + $this->input_element = $input_element; |
|
| 114 | + $this->radio_input_element = $radio_input_element; |
|
| 115 | + $this->language_select_element = $language_select_element; |
|
| 116 | + $this->country_select_element = $country_select_element; |
|
| 117 | + $this->publisher_element = $publisher_element; |
|
| 118 | + |
|
| 119 | + self::$instance = $this; |
|
| 120 | + |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Get the singleton instance of the Notice service. |
|
| 125 | + * |
|
| 126 | + * @return \Wordlift_Admin_Settings_Page The singleton instance of the settings page service. |
|
| 127 | + * @since 3.14.0 |
|
| 128 | + */ |
|
| 129 | + public static function get_instance() { |
|
| 130 | + |
|
| 131 | + return self::$instance; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * @inheritdoc |
|
| 136 | + */ |
|
| 137 | + function get_parent_slug() { |
|
| 138 | + |
|
| 139 | + return 'wl_admin_menu'; |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * @inheritdoc |
|
| 144 | + */ |
|
| 145 | + function get_capability() { |
|
| 146 | + |
|
| 147 | + return 'manage_options'; |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * @inheritdoc |
|
| 152 | + */ |
|
| 153 | + function get_page_title() { |
|
| 154 | + |
|
| 155 | + return 'WordLift Settings'; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * @inheritdoc |
|
| 160 | + */ |
|
| 161 | + function get_menu_title() { |
|
| 162 | + |
|
| 163 | + return 'Settings'; |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * @inheritdoc |
|
| 168 | + */ |
|
| 169 | + function get_menu_slug() { |
|
| 170 | + |
|
| 171 | + return 'wl_configuration_admin_menu'; |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * @inheritdoc |
|
| 176 | + */ |
|
| 177 | + function get_partial_name() { |
|
| 178 | + |
|
| 179 | + return 'wordlift-admin-settings-page.php'; |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + /** |
|
| 183 | + * @inheritdoc |
|
| 184 | + */ |
|
| 185 | + public function enqueue_scripts() { |
|
| 186 | + |
|
| 187 | + // Enqueue the media scripts to be used for the publisher's logo selection. |
|
| 188 | + wp_enqueue_media(); |
|
| 189 | + |
|
| 190 | + // JavaScript required for the settings page. |
|
| 191 | + // @todo: try to move to the `wordlift-admin.bundle.js`. |
|
| 192 | + wp_enqueue_script( 'wordlift-admin-settings-page', plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/1/settings.js', array( 'wp-util' ) ); |
|
| 193 | + wp_enqueue_style( 'wordlift-admin-settings-page', plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/1/settings.css' ); |
|
| 194 | + |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * Configure all the configuration parameters. |
|
| 199 | + * |
|
| 200 | + * Called by the *admin_init* hook. |
|
| 201 | + * |
|
| 202 | + * @since 3.11.0 |
|
| 203 | + */ |
|
| 204 | + function admin_init() { |
|
| 205 | + // Register WordLift's general settings, providing our own sanitize callback |
|
| 206 | + // which will also check whether the user filled the WL Publisher form. |
|
| 207 | + register_setting( |
|
| 208 | + 'wl_general_settings', |
|
| 209 | + 'wl_general_settings', |
|
| 210 | + array( $this, 'sanitize_callback' ) |
|
| 211 | + ); |
|
| 212 | + |
|
| 213 | + // Add the general settings section. |
|
| 214 | + add_settings_section( |
|
| 215 | + 'wl_general_settings_section', // ID used to identify this section and with which to register options. |
|
| 216 | + '', // Section header. |
|
| 217 | + '', // Callback used to render the description of the section. |
|
| 218 | + 'wl_general_settings' // Page on which to add this section of options. |
|
| 219 | + ); |
|
| 220 | + |
|
| 221 | + $key_args = array( |
|
| 222 | + 'id' => 'wl-key', |
|
| 223 | + 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::KEY . ']', |
|
| 224 | + 'value' => $this->configuration_service->get_key(), |
|
| 225 | + 'description' => __( 'Insert the <a href="https://www.wordlift.io/blogger">WordLift Key</a> you received via email.', 'wordlift' ) |
|
| 226 | + . ' [' . get_option( 'home' ) . ']', |
|
| 227 | + ); |
|
| 228 | + |
|
| 229 | + // Before we were used to validate the key beforehand, but this means |
|
| 230 | + // an http call whenever a page is opened in the admin area. Therefore |
|
| 231 | + // we now leave the input `untouched`, leaving to the client to update |
|
| 232 | + // the `css_class`. |
|
| 233 | + // |
|
| 234 | + // See https://github.com/insideout10/wordlift-plugin/issues/669. |
|
| 235 | + $key_args['css_class'] = 'untouched'; |
|
| 236 | + |
|
| 237 | + // Add the `key` field. |
|
| 238 | + add_settings_field( |
|
| 239 | + 'wl-key', // Element id used to identify the field throughout the theme. |
|
| 240 | + __( 'WordLift Key', 'wordlift' ), // The label to the left of the option interface element. |
|
| 241 | + // The name of the function responsible for rendering the option interface. |
|
| 242 | + array( $this->input_element, 'render' ), |
|
| 243 | + 'wl_general_settings', // The page on which this option will be displayed. |
|
| 244 | + 'wl_general_settings_section', // The name of the section to which this field belongs. |
|
| 245 | + $key_args // The array of arguments to pass to the callback. In this case, just a description. |
|
| 246 | + ); |
|
| 247 | + |
|
| 248 | + // Entity Base Path input. |
|
| 249 | + $entity_base_path_args = array( |
|
| 250 | + // The array of arguments to pass to the callback. In this case, just a description. |
|
| 251 | + 'id' => 'wl-entity-base-path', |
|
| 252 | + 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::ENTITY_BASE_PATH_KEY . ']', |
|
| 253 | + 'value' => $this->configuration_service->get_entity_base_path(), |
|
| 254 | + /* translators: Placeholders: %s - a link to FAQ's page. */ |
|
| 255 | + 'description' => sprintf( __( 'All new pages created with WordLift, will be stored inside your internal vocabulary. You can customize the url pattern of these pages in the field above. Check our <a href="%s">FAQs</a> if you need more info.', 'wordlift' ), 'https://wordlift.io/wordlift-user-faqs/#10-why-and-how-should-i-customize-the-url-of-the-entity-pages-created-in-my-vocabulary' ), |
|
| 256 | + ); |
|
| 257 | + |
|
| 258 | + // The following call is very heavy on large web sites and is always run |
|
| 259 | + // also when not needed: |
|
| 260 | + // $entity_base_path_args['readonly'] = 0 < $this->entity_service->count(); |
|
| 261 | + // |
|
| 262 | + // It is now replaced by a filter to add the `readonly` flag to the |
|
| 263 | + // input element when this is actually rendered. |
|
| 264 | + add_filter( 'wl_admin_input_element_params', array( |
|
| 265 | + $this, |
|
| 266 | + 'entity_path_input_element_params', |
|
| 267 | + ) ); |
|
| 268 | + |
|
| 269 | + // Add the `wl_entity_base_path` field. |
|
| 270 | + add_settings_field( |
|
| 271 | + 'wl-entity-base-path', // ID used to identify the field throughout the theme |
|
| 272 | + __( 'Entity Base Path', 'wordlift' ), // The label to the left of the option interface element |
|
| 273 | + // The name of the function responsible for rendering the option interface |
|
| 274 | + array( $this->input_element, 'render', ), |
|
| 275 | + 'wl_general_settings', // The page on which this option will be displayed |
|
| 276 | + 'wl_general_settings_section', // The name of the section to which this field belongs |
|
| 277 | + $entity_base_path_args |
|
| 278 | + ); |
|
| 279 | + |
|
| 280 | + // Add the `language_name` field. |
|
| 281 | + add_settings_field( |
|
| 282 | + 'wl-site-language', |
|
| 283 | + __( 'Site Language', 'wordlift' ), |
|
| 284 | + array( $this->language_select_element, 'render' ), |
|
| 285 | + 'wl_general_settings', |
|
| 286 | + 'wl_general_settings_section', |
|
| 287 | + array( |
|
| 288 | + // The array of arguments to pass to the callback. In this case, just a description. |
|
| 289 | + 'id' => 'wl-site-language', |
|
| 290 | + 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::LANGUAGE . ']', |
|
| 291 | + 'value' => $this->configuration_service->get_language_code(), |
|
| 292 | + 'description' => __( 'Each WordLift Key can be used only in one language. Pick yours.', 'wordlift' ), |
|
| 293 | + ) |
|
| 294 | + ); |
|
| 295 | + |
|
| 296 | + // Add the `country_code` field. |
|
| 297 | + add_settings_field( |
|
| 298 | + 'wl-country-code', |
|
| 299 | + _x( 'Country', 'wordlift' ), |
|
| 300 | + array( $this->country_select_element, 'render' ), |
|
| 301 | + 'wl_general_settings', |
|
| 302 | + 'wl_general_settings_section', |
|
| 303 | + array( |
|
| 304 | + 'id' => 'wl-country-code', |
|
| 305 | + 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::COUNTRY_CODE . ']', |
|
| 306 | + 'value' => $this->configuration_service->get_country_code(), |
|
| 307 | + 'description' => __( 'Please select a country.', 'wordlift' ), |
|
| 308 | + 'notice' => __( 'The selected language is not supported in this country.</br>Please choose another country or language.', 'wordlift' ), |
|
| 309 | + ) |
|
| 310 | + ); |
|
| 311 | + |
|
| 312 | + // Add the `publisher` field. |
|
| 313 | + add_settings_field( |
|
| 314 | + 'wl-publisher-id', |
|
| 315 | + __( 'Publisher', 'wordlift' ), |
|
| 316 | + array( $this->publisher_element, 'render' ), |
|
| 317 | + 'wl_general_settings', |
|
| 318 | + 'wl_general_settings_section', |
|
| 319 | + array( |
|
| 320 | + 'id' => 'wl-publisher-id', |
|
| 321 | + 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::PUBLISHER_ID . ']', |
|
| 322 | + ) |
|
| 323 | + ); |
|
| 324 | + |
|
| 325 | + // Add the `link by default` field. |
|
| 326 | + add_settings_field( |
|
| 327 | + 'wl-link-by-default', |
|
| 328 | + __( 'Link by Default', 'wordlift' ), |
|
| 329 | + array( $this->radio_input_element, 'render' ), |
|
| 330 | + 'wl_general_settings', |
|
| 331 | + 'wl_general_settings_section', |
|
| 332 | + array( |
|
| 333 | + 'id' => 'wl-link-by-default', |
|
| 334 | + 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::LINK_BY_DEFAULT . ']', |
|
| 335 | + 'value' => $this->configuration_service->is_link_by_default() ? 'yes' : 'no', |
|
| 336 | + 'description' => __( 'Whether to link entities by default or not. This setting applies to all the entities.', 'wordlift' ), |
|
| 337 | + ) |
|
| 338 | + ); |
|
| 339 | + |
|
| 340 | + // Add the `diagnostic data` field. |
|
| 341 | + add_settings_field( |
|
| 342 | + 'wl-send-diagnostic', |
|
| 343 | + __( 'Send Diagnostic Data', 'wordlift' ), |
|
| 344 | + array( $this->radio_input_element, 'render' ), |
|
| 345 | + 'wl_general_settings', |
|
| 346 | + 'wl_general_settings_section', |
|
| 347 | + array( |
|
| 348 | + 'id' => 'wl-send-diagnostic', |
|
| 349 | + 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::SEND_DIAGNOSTIC . ']', |
|
| 350 | + 'value' => 'yes' === $this->configuration_service->get_diagnostic_preferences() ? 'yes' : 'no', |
|
| 351 | + 'description' => __( 'Whether to send diagnostic data or not.', 'wordlift' ), |
|
| 352 | + ) |
|
| 353 | + ); |
|
| 354 | + |
|
| 355 | + } |
|
| 356 | + |
|
| 357 | + /** |
|
| 358 | + * Filter the {@link Wordlift_Admin_Input_Element} in order to add the |
|
| 359 | + * `readonly` flag to the `wl-entity-base-path` input. |
|
| 360 | + * |
|
| 361 | + * @param array $args An array of {@link Wordlift_Admin_Input_Element} parameters. |
|
| 362 | + * |
|
| 363 | + * @return array The updated array. |
|
| 364 | + * @since 3.17.0 |
|
| 365 | + * |
|
| 366 | + */ |
|
| 367 | + public function entity_path_input_element_params( $args ) { |
|
| 368 | + |
|
| 369 | + // Bail out if it's not the `wl-entity-base-path`). |
|
| 370 | + if ( 'wl-entity-base-path' !== $args['id'] ) { |
|
| 371 | + return $args; |
|
| 372 | + } |
|
| 373 | + |
|
| 374 | + // Set the readonly flag according to the entities count. |
|
| 375 | + $args['readonly'] = 0 < $this->entity_service->count(); |
|
| 376 | + |
|
| 377 | + // Return the updated args. |
|
| 378 | + return $args; |
|
| 379 | + } |
|
| 380 | + |
|
| 381 | + /** |
|
| 382 | + * Sanitize the configuration settings to be stored. |
|
| 383 | + * |
|
| 384 | + * If a new entity is being created for the publisher, create it and set The |
|
| 385 | + * publisher setting. |
|
| 386 | + * |
|
| 387 | + * @param array $input The configuration settings array. |
|
| 388 | + * |
|
| 389 | + * @return array The sanitized input array. |
|
| 390 | + * @since 3.11.0 |
|
| 391 | + * |
|
| 392 | + */ |
|
| 393 | + function sanitize_callback( $input ) { |
|
| 394 | + |
|
| 395 | + // Validate the selected country. |
|
| 396 | + $this->validate_country(); |
|
| 397 | + |
|
| 398 | + // Check whether a publisher name has been set. |
|
| 399 | + if ( isset( $_POST['wl_publisher'] ) && ! empty( $_POST['wl_publisher']['name'] ) ) { // WPCS: CSRF, input var, sanitization ok. |
|
| 400 | + $name = $_POST['wl_publisher']['name']; // WPCS: CSRF, input var, sanitization ok. |
|
| 401 | + $type = $_POST['wl_publisher']['type']; // WPCS: CSRF, input var, sanitization ok. |
|
| 402 | + $thumbnail_id = $_POST['wl_publisher']['thumbnail_id'] ?: null; // WPCS: CSRF, input var, sanitization ok. |
|
| 403 | + |
|
| 404 | + // Set the type URI, either http://schema.org/Person or http://schema.org/Organization. |
|
| 405 | + $type_uri = sprintf( 'http://schema.org/%s', 'organization' === $type ? 'Organization' : 'Person' ); |
|
| 406 | + |
|
| 407 | + // Create an entity for the publisher and assign it to the input |
|
| 408 | + // parameter which WordPress automatically saves into the settings. |
|
| 409 | + $input['publisher_id'] = $this->entity_service->create( $name, $type_uri, $thumbnail_id, 'publish' ); |
|
| 410 | + } |
|
| 411 | + |
|
| 412 | + return $input; |
|
| 413 | + } |
|
| 414 | + |
|
| 415 | + /** |
|
| 416 | + * Check whether the currently selected country supports the site language. |
|
| 417 | + * |
|
| 418 | + * @since 3.18.0 |
|
| 419 | + */ |
|
| 420 | + private function validate_country() { |
|
| 421 | + |
|
| 422 | + // Bail out if for some reason the country and language are not set. |
|
| 423 | + if ( |
|
| 424 | + empty( $_POST['wl_general_settings']['site_language'] ) && // WPCS: CSRF, input var, sanitization ok. |
|
| 425 | + empty( $_POST['wl_general_settings']['country_code'] ) // WPCS: CSRF, input var, sanitization ok. |
|
| 426 | + ) { |
|
| 427 | + return; |
|
| 428 | + } |
|
| 429 | + |
|
| 430 | + // Get the values. |
|
| 431 | + $language = $_POST['wl_general_settings']['site_language']; // WPCS: CSRF, input var, sanitization ok. |
|
| 432 | + $country = $_POST['wl_general_settings']['country_code']; // WPCS: CSRF, input var, sanitization ok. |
|
| 433 | + $codes = Wordlift_Countries::get_codes(); |
|
| 434 | + |
|
| 435 | + // Check whether the chosen country has language limitations |
|
| 436 | + // and whether the chosen language is supported for that country. |
|
| 437 | + if ( |
|
| 438 | + ! empty( $codes[ $country ] ) && |
|
| 439 | + ! in_array( $language, $codes[ $country ] ) |
|
| 440 | + ) { |
|
| 441 | + // Otherwise add an error. |
|
| 442 | + add_settings_error( |
|
| 443 | + 'wl-country-code', |
|
| 444 | + esc_attr( 'settings_updated' ), |
|
| 445 | + _x( 'The selected language is not supported for the currently chosen country. Please choose another country or language.', 'wordlift' ) |
|
| 446 | + ); |
|
| 447 | + } |
|
| 448 | + } |
|
| 449 | 449 | |
| 450 | 450 | } |
@@ -104,7 +104,7 @@ discard block |
||
| 104 | 104 | * @since 3.11.0 |
| 105 | 105 | * |
| 106 | 106 | */ |
| 107 | - function __construct( $configuration_service, $entity_service, $input_element, $language_select_element, $country_select_element, $publisher_element, $radio_input_element ) { |
|
| 107 | + function __construct($configuration_service, $entity_service, $input_element, $language_select_element, $country_select_element, $publisher_element, $radio_input_element) { |
|
| 108 | 108 | |
| 109 | 109 | $this->configuration_service = $configuration_service; |
| 110 | 110 | $this->entity_service = $entity_service; |
@@ -189,8 +189,8 @@ discard block |
||
| 189 | 189 | |
| 190 | 190 | // JavaScript required for the settings page. |
| 191 | 191 | // @todo: try to move to the `wordlift-admin.bundle.js`. |
| 192 | - wp_enqueue_script( 'wordlift-admin-settings-page', plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/1/settings.js', array( 'wp-util' ) ); |
|
| 193 | - wp_enqueue_style( 'wordlift-admin-settings-page', plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/1/settings.css' ); |
|
| 192 | + wp_enqueue_script('wordlift-admin-settings-page', plugin_dir_url(dirname(__FILE__)).'admin/js/1/settings.js', array('wp-util')); |
|
| 193 | + wp_enqueue_style('wordlift-admin-settings-page', plugin_dir_url(dirname(__FILE__)).'admin/js/1/settings.css'); |
|
| 194 | 194 | |
| 195 | 195 | } |
| 196 | 196 | |
@@ -207,23 +207,23 @@ discard block |
||
| 207 | 207 | register_setting( |
| 208 | 208 | 'wl_general_settings', |
| 209 | 209 | 'wl_general_settings', |
| 210 | - array( $this, 'sanitize_callback' ) |
|
| 210 | + array($this, 'sanitize_callback') |
|
| 211 | 211 | ); |
| 212 | 212 | |
| 213 | 213 | // Add the general settings section. |
| 214 | 214 | add_settings_section( |
| 215 | 215 | 'wl_general_settings_section', // ID used to identify this section and with which to register options. |
| 216 | - '', // Section header. |
|
| 217 | - '', // Callback used to render the description of the section. |
|
| 216 | + '', // Section header. |
|
| 217 | + '', // Callback used to render the description of the section. |
|
| 218 | 218 | 'wl_general_settings' // Page on which to add this section of options. |
| 219 | 219 | ); |
| 220 | 220 | |
| 221 | 221 | $key_args = array( |
| 222 | 222 | 'id' => 'wl-key', |
| 223 | - 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::KEY . ']', |
|
| 223 | + 'name' => 'wl_general_settings['.Wordlift_Configuration_Service::KEY.']', |
|
| 224 | 224 | 'value' => $this->configuration_service->get_key(), |
| 225 | - 'description' => __( 'Insert the <a href="https://www.wordlift.io/blogger">WordLift Key</a> you received via email.', 'wordlift' ) |
|
| 226 | - . ' [' . get_option( 'home' ) . ']', |
|
| 225 | + 'description' => __('Insert the <a href="https://www.wordlift.io/blogger">WordLift Key</a> you received via email.', 'wordlift') |
|
| 226 | + . ' ['.get_option('home').']', |
|
| 227 | 227 | ); |
| 228 | 228 | |
| 229 | 229 | // Before we were used to validate the key beforehand, but this means |
@@ -236,12 +236,12 @@ discard block |
||
| 236 | 236 | |
| 237 | 237 | // Add the `key` field. |
| 238 | 238 | add_settings_field( |
| 239 | - 'wl-key', // Element id used to identify the field throughout the theme. |
|
| 240 | - __( 'WordLift Key', 'wordlift' ), // The label to the left of the option interface element. |
|
| 239 | + 'wl-key', // Element id used to identify the field throughout the theme. |
|
| 240 | + __('WordLift Key', 'wordlift'), // The label to the left of the option interface element. |
|
| 241 | 241 | // The name of the function responsible for rendering the option interface. |
| 242 | - array( $this->input_element, 'render' ), |
|
| 243 | - 'wl_general_settings', // The page on which this option will be displayed. |
|
| 244 | - 'wl_general_settings_section', // The name of the section to which this field belongs. |
|
| 242 | + array($this->input_element, 'render'), |
|
| 243 | + 'wl_general_settings', // The page on which this option will be displayed. |
|
| 244 | + 'wl_general_settings_section', // The name of the section to which this field belongs. |
|
| 245 | 245 | $key_args // The array of arguments to pass to the callback. In this case, just a description. |
| 246 | 246 | ); |
| 247 | 247 | |
@@ -249,10 +249,10 @@ discard block |
||
| 249 | 249 | $entity_base_path_args = array( |
| 250 | 250 | // The array of arguments to pass to the callback. In this case, just a description. |
| 251 | 251 | 'id' => 'wl-entity-base-path', |
| 252 | - 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::ENTITY_BASE_PATH_KEY . ']', |
|
| 252 | + 'name' => 'wl_general_settings['.Wordlift_Configuration_Service::ENTITY_BASE_PATH_KEY.']', |
|
| 253 | 253 | 'value' => $this->configuration_service->get_entity_base_path(), |
| 254 | 254 | /* translators: Placeholders: %s - a link to FAQ's page. */ |
| 255 | - 'description' => sprintf( __( 'All new pages created with WordLift, will be stored inside your internal vocabulary. You can customize the url pattern of these pages in the field above. Check our <a href="%s">FAQs</a> if you need more info.', 'wordlift' ), 'https://wordlift.io/wordlift-user-faqs/#10-why-and-how-should-i-customize-the-url-of-the-entity-pages-created-in-my-vocabulary' ), |
|
| 255 | + 'description' => sprintf(__('All new pages created with WordLift, will be stored inside your internal vocabulary. You can customize the url pattern of these pages in the field above. Check our <a href="%s">FAQs</a> if you need more info.', 'wordlift'), 'https://wordlift.io/wordlift-user-faqs/#10-why-and-how-should-i-customize-the-url-of-the-entity-pages-created-in-my-vocabulary'), |
|
| 256 | 256 | ); |
| 257 | 257 | |
| 258 | 258 | // The following call is very heavy on large web sites and is always run |
@@ -261,94 +261,94 @@ discard block |
||
| 261 | 261 | // |
| 262 | 262 | // It is now replaced by a filter to add the `readonly` flag to the |
| 263 | 263 | // input element when this is actually rendered. |
| 264 | - add_filter( 'wl_admin_input_element_params', array( |
|
| 264 | + add_filter('wl_admin_input_element_params', array( |
|
| 265 | 265 | $this, |
| 266 | 266 | 'entity_path_input_element_params', |
| 267 | - ) ); |
|
| 267 | + )); |
|
| 268 | 268 | |
| 269 | 269 | // Add the `wl_entity_base_path` field. |
| 270 | 270 | add_settings_field( |
| 271 | - 'wl-entity-base-path', // ID used to identify the field throughout the theme |
|
| 272 | - __( 'Entity Base Path', 'wordlift' ), // The label to the left of the option interface element |
|
| 271 | + 'wl-entity-base-path', // ID used to identify the field throughout the theme |
|
| 272 | + __('Entity Base Path', 'wordlift'), // The label to the left of the option interface element |
|
| 273 | 273 | // The name of the function responsible for rendering the option interface |
| 274 | - array( $this->input_element, 'render', ), |
|
| 275 | - 'wl_general_settings', // The page on which this option will be displayed |
|
| 276 | - 'wl_general_settings_section', // The name of the section to which this field belongs |
|
| 274 | + array($this->input_element, 'render',), |
|
| 275 | + 'wl_general_settings', // The page on which this option will be displayed |
|
| 276 | + 'wl_general_settings_section', // The name of the section to which this field belongs |
|
| 277 | 277 | $entity_base_path_args |
| 278 | 278 | ); |
| 279 | 279 | |
| 280 | 280 | // Add the `language_name` field. |
| 281 | 281 | add_settings_field( |
| 282 | 282 | 'wl-site-language', |
| 283 | - __( 'Site Language', 'wordlift' ), |
|
| 284 | - array( $this->language_select_element, 'render' ), |
|
| 283 | + __('Site Language', 'wordlift'), |
|
| 284 | + array($this->language_select_element, 'render'), |
|
| 285 | 285 | 'wl_general_settings', |
| 286 | 286 | 'wl_general_settings_section', |
| 287 | 287 | array( |
| 288 | 288 | // The array of arguments to pass to the callback. In this case, just a description. |
| 289 | 289 | 'id' => 'wl-site-language', |
| 290 | - 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::LANGUAGE . ']', |
|
| 290 | + 'name' => 'wl_general_settings['.Wordlift_Configuration_Service::LANGUAGE.']', |
|
| 291 | 291 | 'value' => $this->configuration_service->get_language_code(), |
| 292 | - 'description' => __( 'Each WordLift Key can be used only in one language. Pick yours.', 'wordlift' ), |
|
| 292 | + 'description' => __('Each WordLift Key can be used only in one language. Pick yours.', 'wordlift'), |
|
| 293 | 293 | ) |
| 294 | 294 | ); |
| 295 | 295 | |
| 296 | 296 | // Add the `country_code` field. |
| 297 | 297 | add_settings_field( |
| 298 | 298 | 'wl-country-code', |
| 299 | - _x( 'Country', 'wordlift' ), |
|
| 300 | - array( $this->country_select_element, 'render' ), |
|
| 299 | + _x('Country', 'wordlift'), |
|
| 300 | + array($this->country_select_element, 'render'), |
|
| 301 | 301 | 'wl_general_settings', |
| 302 | 302 | 'wl_general_settings_section', |
| 303 | 303 | array( |
| 304 | 304 | 'id' => 'wl-country-code', |
| 305 | - 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::COUNTRY_CODE . ']', |
|
| 305 | + 'name' => 'wl_general_settings['.Wordlift_Configuration_Service::COUNTRY_CODE.']', |
|
| 306 | 306 | 'value' => $this->configuration_service->get_country_code(), |
| 307 | - 'description' => __( 'Please select a country.', 'wordlift' ), |
|
| 308 | - 'notice' => __( 'The selected language is not supported in this country.</br>Please choose another country or language.', 'wordlift' ), |
|
| 307 | + 'description' => __('Please select a country.', 'wordlift'), |
|
| 308 | + 'notice' => __('The selected language is not supported in this country.</br>Please choose another country or language.', 'wordlift'), |
|
| 309 | 309 | ) |
| 310 | 310 | ); |
| 311 | 311 | |
| 312 | 312 | // Add the `publisher` field. |
| 313 | 313 | add_settings_field( |
| 314 | 314 | 'wl-publisher-id', |
| 315 | - __( 'Publisher', 'wordlift' ), |
|
| 316 | - array( $this->publisher_element, 'render' ), |
|
| 315 | + __('Publisher', 'wordlift'), |
|
| 316 | + array($this->publisher_element, 'render'), |
|
| 317 | 317 | 'wl_general_settings', |
| 318 | 318 | 'wl_general_settings_section', |
| 319 | 319 | array( |
| 320 | 320 | 'id' => 'wl-publisher-id', |
| 321 | - 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::PUBLISHER_ID . ']', |
|
| 321 | + 'name' => 'wl_general_settings['.Wordlift_Configuration_Service::PUBLISHER_ID.']', |
|
| 322 | 322 | ) |
| 323 | 323 | ); |
| 324 | 324 | |
| 325 | 325 | // Add the `link by default` field. |
| 326 | 326 | add_settings_field( |
| 327 | 327 | 'wl-link-by-default', |
| 328 | - __( 'Link by Default', 'wordlift' ), |
|
| 329 | - array( $this->radio_input_element, 'render' ), |
|
| 328 | + __('Link by Default', 'wordlift'), |
|
| 329 | + array($this->radio_input_element, 'render'), |
|
| 330 | 330 | 'wl_general_settings', |
| 331 | 331 | 'wl_general_settings_section', |
| 332 | 332 | array( |
| 333 | 333 | 'id' => 'wl-link-by-default', |
| 334 | - 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::LINK_BY_DEFAULT . ']', |
|
| 334 | + 'name' => 'wl_general_settings['.Wordlift_Configuration_Service::LINK_BY_DEFAULT.']', |
|
| 335 | 335 | 'value' => $this->configuration_service->is_link_by_default() ? 'yes' : 'no', |
| 336 | - 'description' => __( 'Whether to link entities by default or not. This setting applies to all the entities.', 'wordlift' ), |
|
| 336 | + 'description' => __('Whether to link entities by default or not. This setting applies to all the entities.', 'wordlift'), |
|
| 337 | 337 | ) |
| 338 | 338 | ); |
| 339 | 339 | |
| 340 | 340 | // Add the `diagnostic data` field. |
| 341 | 341 | add_settings_field( |
| 342 | 342 | 'wl-send-diagnostic', |
| 343 | - __( 'Send Diagnostic Data', 'wordlift' ), |
|
| 344 | - array( $this->radio_input_element, 'render' ), |
|
| 343 | + __('Send Diagnostic Data', 'wordlift'), |
|
| 344 | + array($this->radio_input_element, 'render'), |
|
| 345 | 345 | 'wl_general_settings', |
| 346 | 346 | 'wl_general_settings_section', |
| 347 | 347 | array( |
| 348 | 348 | 'id' => 'wl-send-diagnostic', |
| 349 | - 'name' => 'wl_general_settings[' . Wordlift_Configuration_Service::SEND_DIAGNOSTIC . ']', |
|
| 349 | + 'name' => 'wl_general_settings['.Wordlift_Configuration_Service::SEND_DIAGNOSTIC.']', |
|
| 350 | 350 | 'value' => 'yes' === $this->configuration_service->get_diagnostic_preferences() ? 'yes' : 'no', |
| 351 | - 'description' => __( 'Whether to send diagnostic data or not.', 'wordlift' ), |
|
| 351 | + 'description' => __('Whether to send diagnostic data or not.', 'wordlift'), |
|
| 352 | 352 | ) |
| 353 | 353 | ); |
| 354 | 354 | |
@@ -364,10 +364,10 @@ discard block |
||
| 364 | 364 | * @since 3.17.0 |
| 365 | 365 | * |
| 366 | 366 | */ |
| 367 | - public function entity_path_input_element_params( $args ) { |
|
| 367 | + public function entity_path_input_element_params($args) { |
|
| 368 | 368 | |
| 369 | 369 | // Bail out if it's not the `wl-entity-base-path`). |
| 370 | - if ( 'wl-entity-base-path' !== $args['id'] ) { |
|
| 370 | + if ('wl-entity-base-path' !== $args['id']) { |
|
| 371 | 371 | return $args; |
| 372 | 372 | } |
| 373 | 373 | |
@@ -390,23 +390,23 @@ discard block |
||
| 390 | 390 | * @since 3.11.0 |
| 391 | 391 | * |
| 392 | 392 | */ |
| 393 | - function sanitize_callback( $input ) { |
|
| 393 | + function sanitize_callback($input) { |
|
| 394 | 394 | |
| 395 | 395 | // Validate the selected country. |
| 396 | 396 | $this->validate_country(); |
| 397 | 397 | |
| 398 | 398 | // Check whether a publisher name has been set. |
| 399 | - if ( isset( $_POST['wl_publisher'] ) && ! empty( $_POST['wl_publisher']['name'] ) ) { // WPCS: CSRF, input var, sanitization ok. |
|
| 399 | + if (isset($_POST['wl_publisher']) && ! empty($_POST['wl_publisher']['name'])) { // WPCS: CSRF, input var, sanitization ok. |
|
| 400 | 400 | $name = $_POST['wl_publisher']['name']; // WPCS: CSRF, input var, sanitization ok. |
| 401 | 401 | $type = $_POST['wl_publisher']['type']; // WPCS: CSRF, input var, sanitization ok. |
| 402 | 402 | $thumbnail_id = $_POST['wl_publisher']['thumbnail_id'] ?: null; // WPCS: CSRF, input var, sanitization ok. |
| 403 | 403 | |
| 404 | 404 | // Set the type URI, either http://schema.org/Person or http://schema.org/Organization. |
| 405 | - $type_uri = sprintf( 'http://schema.org/%s', 'organization' === $type ? 'Organization' : 'Person' ); |
|
| 405 | + $type_uri = sprintf('http://schema.org/%s', 'organization' === $type ? 'Organization' : 'Person'); |
|
| 406 | 406 | |
| 407 | 407 | // Create an entity for the publisher and assign it to the input |
| 408 | 408 | // parameter which WordPress automatically saves into the settings. |
| 409 | - $input['publisher_id'] = $this->entity_service->create( $name, $type_uri, $thumbnail_id, 'publish' ); |
|
| 409 | + $input['publisher_id'] = $this->entity_service->create($name, $type_uri, $thumbnail_id, 'publish'); |
|
| 410 | 410 | } |
| 411 | 411 | |
| 412 | 412 | return $input; |
@@ -421,8 +421,8 @@ discard block |
||
| 421 | 421 | |
| 422 | 422 | // Bail out if for some reason the country and language are not set. |
| 423 | 423 | if ( |
| 424 | - empty( $_POST['wl_general_settings']['site_language'] ) && // WPCS: CSRF, input var, sanitization ok. |
|
| 425 | - empty( $_POST['wl_general_settings']['country_code'] ) // WPCS: CSRF, input var, sanitization ok. |
|
| 424 | + empty($_POST['wl_general_settings']['site_language']) && // WPCS: CSRF, input var, sanitization ok. |
|
| 425 | + empty($_POST['wl_general_settings']['country_code']) // WPCS: CSRF, input var, sanitization ok. |
|
| 426 | 426 | ) { |
| 427 | 427 | return; |
| 428 | 428 | } |
@@ -435,14 +435,14 @@ discard block |
||
| 435 | 435 | // Check whether the chosen country has language limitations |
| 436 | 436 | // and whether the chosen language is supported for that country. |
| 437 | 437 | if ( |
| 438 | - ! empty( $codes[ $country ] ) && |
|
| 439 | - ! in_array( $language, $codes[ $country ] ) |
|
| 438 | + ! empty($codes[$country]) && |
|
| 439 | + ! in_array($language, $codes[$country]) |
|
| 440 | 440 | ) { |
| 441 | 441 | // Otherwise add an error. |
| 442 | 442 | add_settings_error( |
| 443 | 443 | 'wl-country-code', |
| 444 | - esc_attr( 'settings_updated' ), |
|
| 445 | - _x( 'The selected language is not supported for the currently chosen country. Please choose another country or language.', 'wordlift' ) |
|
| 444 | + esc_attr('settings_updated'), |
|
| 445 | + _x('The selected language is not supported for the currently chosen country. Please choose another country or language.', 'wordlift') |
|
| 446 | 446 | ); |
| 447 | 447 | } |
| 448 | 448 | } |
@@ -13,7 +13,7 @@ discard block |
||
| 13 | 13 | */ |
| 14 | 14 | |
| 15 | 15 | if ( ! defined( 'ABSPATH' ) ) { |
| 16 | - exit; |
|
| 16 | + exit; |
|
| 17 | 17 | } |
| 18 | 18 | |
| 19 | 19 | /** |
@@ -28,140 +28,140 @@ discard block |
||
| 28 | 28 | */ |
| 29 | 29 | class Wordlift_Admin_Setup { |
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * A {@link Wordlift_Configuration_Service} instance. |
|
| 33 | - * |
|
| 34 | - * @since 3.9.0 |
|
| 35 | - * @access private |
|
| 36 | - * @var Wordlift_Configuration_Service A {@link Wordlift_Configuration_Service} instance. |
|
| 37 | - */ |
|
| 38 | - private $configuration_service; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * A {@link Wordlift_Key_Validation_Service} instance. |
|
| 42 | - * |
|
| 43 | - * @since 3.9.0 |
|
| 44 | - * @access private |
|
| 45 | - * @var Wordlift_Key_Validation_Service A {@link Wordlift_Key_Validation_Service} instance. |
|
| 46 | - */ |
|
| 47 | - private $key_validation_service; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * A {@link Wordlift_Entity_Service} instance. |
|
| 51 | - * |
|
| 52 | - * @since 3.9.0 |
|
| 53 | - * @access private |
|
| 54 | - * @var Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 55 | - */ |
|
| 56 | - private $entity_service; |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * A {@link Wordlift_Admin_Language_Select_Element} element renderer. |
|
| 60 | - * |
|
| 61 | - * @since 3.20.0 |
|
| 62 | - * @access private |
|
| 63 | - * @var \Wordlift_Admin_Language_Select_Element $language_select_element A {@link Wordlift_Admin_Language_Select_Element} element renderer. |
|
| 64 | - */ |
|
| 65 | - private $language_select_element; |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * A {@link Wordlift_Admin_Country_Select_Element} element renderer. |
|
| 69 | - * |
|
| 70 | - * @since 3.20.0 |
|
| 71 | - * @access private |
|
| 72 | - * @var \Wordlift_Admin_Country_Select_Element $country_select_element A {@link Wordlift_Admin_Country_Select_Element} element renderer. |
|
| 73 | - */ |
|
| 74 | - private $country_select_element; |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * Initialize the class and set its properties. |
|
| 78 | - * |
|
| 79 | - * @param \Wordlift_Configuration_Service $configuration_service A {@link Wordlift_Configuration_Service} instance. |
|
| 80 | - * @param \Wordlift_Key_Validation_Service $key_validation_service A {@link Wordlift_Key_Validation_Service} instance. |
|
| 81 | - * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 82 | - * @param \Wordlift_Admin_Language_Select_Element $language_select_element A {@link Wordlift_Admin_Language_Select_Element} element renderer. |
|
| 83 | - * @param \Wordlift_Admin_Country_Select_Element $country_select_element A {@link Wordlift_Admin_Country_Select_Element} element renderer. |
|
| 84 | - * |
|
| 85 | - * @since 3.9.0 |
|
| 86 | - * |
|
| 87 | - */ |
|
| 88 | - public function __construct( $configuration_service, $key_validation_service, $entity_service, $language_select_element, $country_select_element ) { |
|
| 89 | - |
|
| 90 | - // Set a reference to the configuration service. |
|
| 91 | - $this->configuration_service = $configuration_service; |
|
| 92 | - |
|
| 93 | - // Set a reference to the key validation service. |
|
| 94 | - $this->key_validation_service = $key_validation_service; |
|
| 95 | - |
|
| 96 | - // Set a reference to the entity service. |
|
| 97 | - $this->entity_service = $entity_service; |
|
| 98 | - |
|
| 99 | - // Set a reference to the UI elements language and country. |
|
| 100 | - $this->language_select_element = $language_select_element; |
|
| 101 | - $this->country_select_element = $country_select_element; |
|
| 102 | - |
|
| 103 | - // Hook to some WP's events: |
|
| 104 | - // When WP is loaded check whether the user decided to skip the set-up, i.e. don't show us even if WL is not set up. |
|
| 105 | - add_action( 'wp_loaded', array( $this, 'hide_notices' ) ); |
|
| 106 | - |
|
| 107 | - // Hook to `admin_menu` in order to add our own setup wizard page. |
|
| 108 | - add_action( 'admin_menu', array( $this, 'admin_menu' ) ); |
|
| 109 | - |
|
| 110 | - // Triggered when the user accesses the admin area, we decide whether to show our own wizard. |
|
| 111 | - add_action( 'admin_init', array( $this, 'show_page' ) ); |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * Filter: wl_feature__enable__notices. |
|
| 115 | - * |
|
| 116 | - * @param bool whether the notices needs to be enabled or not. |
|
| 117 | - * |
|
| 118 | - * @return bool |
|
| 119 | - * @since 3.27.6 |
|
| 120 | - */ |
|
| 121 | - if ( apply_filters( 'wl_feature__enable__notices', true ) ) { |
|
| 122 | - // Hook to `admin_notices` to display our notices. |
|
| 123 | - add_action( 'admin_notices', array( $this, 'admin_notices' ) ); |
|
| 124 | - } |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - /** |
|
| 128 | - * Hook to `admin_init` and redirect to WordLift's setup page if the `_wl_activation_redirect` transient flag is set. |
|
| 129 | - * |
|
| 130 | - * @since 3.9.0 |
|
| 131 | - */ |
|
| 132 | - public function admin_init() { |
|
| 133 | - |
|
| 134 | - // If the `_wl_activation_redirect` is set, the redirect to the setup page. |
|
| 135 | - if ( get_transient( '_wl_activation_redirect' ) ) { |
|
| 136 | - delete_transient( '_wl_activation_redirect' ); |
|
| 137 | - |
|
| 138 | - // If the user asked to skip the wizard then comply. |
|
| 139 | - if ( $this->configuration_service->is_skip_wizard() ) { |
|
| 140 | - return; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - // If we're already on the page or the user doesn't have permissions, return. |
|
| 144 | - if ( ( ! empty( $_GET['page'] ) && 'wl-setup' === $_GET['page'] ) || is_network_admin() || isset( $_GET['activate-multi'] ) || ! current_user_can( 'manage_options' ) ) { |
|
| 145 | - return; |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - // Finally redirect to the setup page. |
|
| 149 | - wp_safe_redirect( admin_url( 'index.php?page=wl-setup' ) ); |
|
| 150 | - |
|
| 151 | - exit; |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * Generate an admin notice suggesting to start the wizard if there is no configuration. |
|
| 158 | - * |
|
| 159 | - * @since 3.9.0 |
|
| 160 | - */ |
|
| 161 | - public function admin_notices() { |
|
| 162 | - |
|
| 163 | - // Use `wl_configuration_get_key` to check whether WL's key is set and that the user didn't disable the wizard. |
|
| 164 | - if ( '' === $this->configuration_service->get_key() && ! $this->configuration_service->is_skip_wizard() ) { ?> |
|
| 31 | + /** |
|
| 32 | + * A {@link Wordlift_Configuration_Service} instance. |
|
| 33 | + * |
|
| 34 | + * @since 3.9.0 |
|
| 35 | + * @access private |
|
| 36 | + * @var Wordlift_Configuration_Service A {@link Wordlift_Configuration_Service} instance. |
|
| 37 | + */ |
|
| 38 | + private $configuration_service; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * A {@link Wordlift_Key_Validation_Service} instance. |
|
| 42 | + * |
|
| 43 | + * @since 3.9.0 |
|
| 44 | + * @access private |
|
| 45 | + * @var Wordlift_Key_Validation_Service A {@link Wordlift_Key_Validation_Service} instance. |
|
| 46 | + */ |
|
| 47 | + private $key_validation_service; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * A {@link Wordlift_Entity_Service} instance. |
|
| 51 | + * |
|
| 52 | + * @since 3.9.0 |
|
| 53 | + * @access private |
|
| 54 | + * @var Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 55 | + */ |
|
| 56 | + private $entity_service; |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * A {@link Wordlift_Admin_Language_Select_Element} element renderer. |
|
| 60 | + * |
|
| 61 | + * @since 3.20.0 |
|
| 62 | + * @access private |
|
| 63 | + * @var \Wordlift_Admin_Language_Select_Element $language_select_element A {@link Wordlift_Admin_Language_Select_Element} element renderer. |
|
| 64 | + */ |
|
| 65 | + private $language_select_element; |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * A {@link Wordlift_Admin_Country_Select_Element} element renderer. |
|
| 69 | + * |
|
| 70 | + * @since 3.20.0 |
|
| 71 | + * @access private |
|
| 72 | + * @var \Wordlift_Admin_Country_Select_Element $country_select_element A {@link Wordlift_Admin_Country_Select_Element} element renderer. |
|
| 73 | + */ |
|
| 74 | + private $country_select_element; |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * Initialize the class and set its properties. |
|
| 78 | + * |
|
| 79 | + * @param \Wordlift_Configuration_Service $configuration_service A {@link Wordlift_Configuration_Service} instance. |
|
| 80 | + * @param \Wordlift_Key_Validation_Service $key_validation_service A {@link Wordlift_Key_Validation_Service} instance. |
|
| 81 | + * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 82 | + * @param \Wordlift_Admin_Language_Select_Element $language_select_element A {@link Wordlift_Admin_Language_Select_Element} element renderer. |
|
| 83 | + * @param \Wordlift_Admin_Country_Select_Element $country_select_element A {@link Wordlift_Admin_Country_Select_Element} element renderer. |
|
| 84 | + * |
|
| 85 | + * @since 3.9.0 |
|
| 86 | + * |
|
| 87 | + */ |
|
| 88 | + public function __construct( $configuration_service, $key_validation_service, $entity_service, $language_select_element, $country_select_element ) { |
|
| 89 | + |
|
| 90 | + // Set a reference to the configuration service. |
|
| 91 | + $this->configuration_service = $configuration_service; |
|
| 92 | + |
|
| 93 | + // Set a reference to the key validation service. |
|
| 94 | + $this->key_validation_service = $key_validation_service; |
|
| 95 | + |
|
| 96 | + // Set a reference to the entity service. |
|
| 97 | + $this->entity_service = $entity_service; |
|
| 98 | + |
|
| 99 | + // Set a reference to the UI elements language and country. |
|
| 100 | + $this->language_select_element = $language_select_element; |
|
| 101 | + $this->country_select_element = $country_select_element; |
|
| 102 | + |
|
| 103 | + // Hook to some WP's events: |
|
| 104 | + // When WP is loaded check whether the user decided to skip the set-up, i.e. don't show us even if WL is not set up. |
|
| 105 | + add_action( 'wp_loaded', array( $this, 'hide_notices' ) ); |
|
| 106 | + |
|
| 107 | + // Hook to `admin_menu` in order to add our own setup wizard page. |
|
| 108 | + add_action( 'admin_menu', array( $this, 'admin_menu' ) ); |
|
| 109 | + |
|
| 110 | + // Triggered when the user accesses the admin area, we decide whether to show our own wizard. |
|
| 111 | + add_action( 'admin_init', array( $this, 'show_page' ) ); |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * Filter: wl_feature__enable__notices. |
|
| 115 | + * |
|
| 116 | + * @param bool whether the notices needs to be enabled or not. |
|
| 117 | + * |
|
| 118 | + * @return bool |
|
| 119 | + * @since 3.27.6 |
|
| 120 | + */ |
|
| 121 | + if ( apply_filters( 'wl_feature__enable__notices', true ) ) { |
|
| 122 | + // Hook to `admin_notices` to display our notices. |
|
| 123 | + add_action( 'admin_notices', array( $this, 'admin_notices' ) ); |
|
| 124 | + } |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + /** |
|
| 128 | + * Hook to `admin_init` and redirect to WordLift's setup page if the `_wl_activation_redirect` transient flag is set. |
|
| 129 | + * |
|
| 130 | + * @since 3.9.0 |
|
| 131 | + */ |
|
| 132 | + public function admin_init() { |
|
| 133 | + |
|
| 134 | + // If the `_wl_activation_redirect` is set, the redirect to the setup page. |
|
| 135 | + if ( get_transient( '_wl_activation_redirect' ) ) { |
|
| 136 | + delete_transient( '_wl_activation_redirect' ); |
|
| 137 | + |
|
| 138 | + // If the user asked to skip the wizard then comply. |
|
| 139 | + if ( $this->configuration_service->is_skip_wizard() ) { |
|
| 140 | + return; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + // If we're already on the page or the user doesn't have permissions, return. |
|
| 144 | + if ( ( ! empty( $_GET['page'] ) && 'wl-setup' === $_GET['page'] ) || is_network_admin() || isset( $_GET['activate-multi'] ) || ! current_user_can( 'manage_options' ) ) { |
|
| 145 | + return; |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + // Finally redirect to the setup page. |
|
| 149 | + wp_safe_redirect( admin_url( 'index.php?page=wl-setup' ) ); |
|
| 150 | + |
|
| 151 | + exit; |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * Generate an admin notice suggesting to start the wizard if there is no configuration. |
|
| 158 | + * |
|
| 159 | + * @since 3.9.0 |
|
| 160 | + */ |
|
| 161 | + public function admin_notices() { |
|
| 162 | + |
|
| 163 | + // Use `wl_configuration_get_key` to check whether WL's key is set and that the user didn't disable the wizard. |
|
| 164 | + if ( '' === $this->configuration_service->get_key() && ! $this->configuration_service->is_skip_wizard() ) { ?> |
|
| 165 | 165 | <div id="wl-message" class="updated"> |
| 166 | 166 | <p><?php esc_html_e( 'Welcome to WordLift – You‘re almost ready to start', 'wordlift' ); ?></p> |
| 167 | 167 | <p class="submit"> |
@@ -173,140 +173,140 @@ discard block |
||
| 173 | 173 | </div> |
| 174 | 174 | <?php } |
| 175 | 175 | |
| 176 | - } |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * Handle hiding the wizard notices by user request. |
|
| 180 | - * |
|
| 181 | - * @since 3.9.0 |
|
| 182 | - */ |
|
| 183 | - public function hide_notices() { |
|
| 184 | - |
|
| 185 | - // If it's not a `wl-hide-notice` or the nonce is not set, return. |
|
| 186 | - if ( ! isset( $_GET['wl-hide-notice'], $_GET['_wl_notice_nonce'] ) ) { |
|
| 187 | - return; |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - // If the nonce is invalid, return an error. |
|
| 191 | - if ( ! wp_verify_nonce( $_GET['_wl_notice_nonce'], 'wordlift_hide_notices_nonce' ) ) { |
|
| 192 | - wp_die( __( 'Action failed. Please refresh the page and retry.', 'wordlift' ) ); |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - // If the user doesn't have the right privileges, return an error. |
|
| 196 | - if ( ! current_user_can( 'manage_options' ) ) { |
|
| 197 | - wp_die( __( 'Cheatin’ huh?', 'wordlift' ) ); |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - // Store a flag telling to skip the wizard. |
|
| 201 | - $this->configuration_service->set_skip_wizard( true ); |
|
| 202 | - |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - /** |
|
| 206 | - * Register the wizard page to be able to access it. |
|
| 207 | - * |
|
| 208 | - * @since 3.9.0 |
|
| 209 | - */ |
|
| 210 | - public function admin_menu() { |
|
| 211 | - /** |
|
| 212 | - * |
|
| 213 | - * @see https://github.com/insideout10/wordlift-plugin/issues/1209 |
|
| 214 | - * @since 3.27.7 |
|
| 215 | - */ |
|
| 216 | - // @todo: find another way to do this, since this is adding an empty space in WP's dashboard menu. |
|
| 217 | - add_dashboard_page( '', '', 'manage_options', 'wl-setup', '' ); |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - /** |
|
| 221 | - * Displays the wizard page. |
|
| 222 | - * |
|
| 223 | - * @since 3.9.0 |
|
| 224 | - */ |
|
| 225 | - public function show_page() { |
|
| 226 | - |
|
| 227 | - // First check if we are in the wizard page at all, if not do nothing. |
|
| 228 | - if ( empty( $_GET['page'] ) || 'wl-setup' !== $_GET['page'] ) { |
|
| 229 | - return; |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - // If it's a POST and the `wl-save-configuration` action is set, save the configuration. |
|
| 233 | - if ( isset( $_POST['action'] ) && 'wl-save-configuration' === $_POST['action'] ) { |
|
| 234 | - |
|
| 235 | - // Check the nonce and the user capabilities. |
|
| 236 | - check_admin_referer( 'wl-save-configuration' ); |
|
| 237 | - |
|
| 238 | - // Check if the user has the right privileges. |
|
| 239 | - if ( ! current_user_can( 'manage_options' ) ) { |
|
| 240 | - wp_die( __( 'Sorry, you do not have a permission to save the settings', 'wordlift' ) ); |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - // Save the configuration. |
|
| 244 | - $this->save_configuration( $_POST ); |
|
| 245 | - |
|
| 246 | - // Redirect to the admin's page. |
|
| 247 | - wp_redirect( admin_url() ); |
|
| 248 | - |
|
| 249 | - exit; |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - $language_select = $this->language_select_element; |
|
| 253 | - $country_select = $this->country_select_element; |
|
| 254 | - |
|
| 255 | - include plugin_dir_path( dirname( __FILE__ ) ) . 'admin/partials/wordlift-admin-setup.php'; |
|
| 256 | - |
|
| 257 | - exit; |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - /** |
|
| 261 | - * Save WordLift's configuration using the provided parameters. |
|
| 262 | - * |
|
| 263 | - * @param array $params An array of configuration parameters. |
|
| 264 | - * |
|
| 265 | - * @since 3.9.0 |
|
| 266 | - * |
|
| 267 | - */ |
|
| 268 | - public function save_configuration( $params ) { |
|
| 269 | - |
|
| 270 | - // We have the following parameters: |
|
| 271 | - // `key`, holding WL's key, |
|
| 272 | - // `vocabulary`, holding the vocabulary path, |
|
| 273 | - // `wl-site-language`, with the language code (e.g. `en`), |
|
| 274 | - // `wl-country-code` with the country code (e.g. `us`), |
|
| 275 | - // `share-diagnostic`, the user preferences about sharing data with us. |
|
| 276 | - // `user_type`, the user type either `personal` or `company`, |
|
| 277 | - // `name`, with the `personal` or `company`'s name, |
|
| 278 | - // `logo`, the attachment id for the `personal` or `company` entity. |
|
| 279 | - |
|
| 280 | - // Store the key: |
|
| 281 | - $this->configuration_service->set_key( $params['key'] ); |
|
| 282 | - |
|
| 283 | - // Store the vocabulary path: |
|
| 284 | - $this->configuration_service->set_entity_base_path( $params['vocabulary'] ); |
|
| 285 | - |
|
| 286 | - // Store the site's language: |
|
| 287 | - $this->configuration_service->set_language_code( $params['wl-site-language'] ); |
|
| 288 | - |
|
| 289 | - // Store the site's country: |
|
| 290 | - $this->configuration_service->set_country_code( $params['wl-country-code'] ); |
|
| 291 | - |
|
| 292 | - // Store the preferences in variable, because if the checkbox is not checked |
|
| 293 | - // the `share-diagnostic` will not exists in `$params` array. |
|
| 294 | - $share_diagnostic_preferences = empty( $params['share-diagnostic'] ) ? 'no' : 'yes'; |
|
| 295 | - |
|
| 296 | - // Store the diagnostic preferences: |
|
| 297 | - $this->configuration_service->set_diagnostic_preferences( $share_diagnostic_preferences ); |
|
| 298 | - |
|
| 299 | - // Set the type URI, either http://schema.org/Person or http://schema.org/Organization. |
|
| 300 | - $type_uri = sprintf( 'http://schema.org/%s', 'organization' === $params['user_type'] ? 'Organization' : 'Person' ); |
|
| 301 | - |
|
| 302 | - // Create an entity for the publisher. |
|
| 303 | - $publisher_post_id = $this->entity_service->create( $params['name'], $type_uri, $params['logo'], 'publish' ); |
|
| 304 | - |
|
| 305 | - // Store the publisher entity post id in the configuration. |
|
| 306 | - $this->configuration_service->set_publisher_id( $publisher_post_id ); |
|
| 307 | - |
|
| 308 | - flush_rewrite_rules(); // Needed because of possible change to the entity base path. |
|
| 309 | - |
|
| 310 | - } |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * Handle hiding the wizard notices by user request. |
|
| 180 | + * |
|
| 181 | + * @since 3.9.0 |
|
| 182 | + */ |
|
| 183 | + public function hide_notices() { |
|
| 184 | + |
|
| 185 | + // If it's not a `wl-hide-notice` or the nonce is not set, return. |
|
| 186 | + if ( ! isset( $_GET['wl-hide-notice'], $_GET['_wl_notice_nonce'] ) ) { |
|
| 187 | + return; |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + // If the nonce is invalid, return an error. |
|
| 191 | + if ( ! wp_verify_nonce( $_GET['_wl_notice_nonce'], 'wordlift_hide_notices_nonce' ) ) { |
|
| 192 | + wp_die( __( 'Action failed. Please refresh the page and retry.', 'wordlift' ) ); |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + // If the user doesn't have the right privileges, return an error. |
|
| 196 | + if ( ! current_user_can( 'manage_options' ) ) { |
|
| 197 | + wp_die( __( 'Cheatin’ huh?', 'wordlift' ) ); |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + // Store a flag telling to skip the wizard. |
|
| 201 | + $this->configuration_service->set_skip_wizard( true ); |
|
| 202 | + |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + /** |
|
| 206 | + * Register the wizard page to be able to access it. |
|
| 207 | + * |
|
| 208 | + * @since 3.9.0 |
|
| 209 | + */ |
|
| 210 | + public function admin_menu() { |
|
| 211 | + /** |
|
| 212 | + * |
|
| 213 | + * @see https://github.com/insideout10/wordlift-plugin/issues/1209 |
|
| 214 | + * @since 3.27.7 |
|
| 215 | + */ |
|
| 216 | + // @todo: find another way to do this, since this is adding an empty space in WP's dashboard menu. |
|
| 217 | + add_dashboard_page( '', '', 'manage_options', 'wl-setup', '' ); |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + /** |
|
| 221 | + * Displays the wizard page. |
|
| 222 | + * |
|
| 223 | + * @since 3.9.0 |
|
| 224 | + */ |
|
| 225 | + public function show_page() { |
|
| 226 | + |
|
| 227 | + // First check if we are in the wizard page at all, if not do nothing. |
|
| 228 | + if ( empty( $_GET['page'] ) || 'wl-setup' !== $_GET['page'] ) { |
|
| 229 | + return; |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + // If it's a POST and the `wl-save-configuration` action is set, save the configuration. |
|
| 233 | + if ( isset( $_POST['action'] ) && 'wl-save-configuration' === $_POST['action'] ) { |
|
| 234 | + |
|
| 235 | + // Check the nonce and the user capabilities. |
|
| 236 | + check_admin_referer( 'wl-save-configuration' ); |
|
| 237 | + |
|
| 238 | + // Check if the user has the right privileges. |
|
| 239 | + if ( ! current_user_can( 'manage_options' ) ) { |
|
| 240 | + wp_die( __( 'Sorry, you do not have a permission to save the settings', 'wordlift' ) ); |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + // Save the configuration. |
|
| 244 | + $this->save_configuration( $_POST ); |
|
| 245 | + |
|
| 246 | + // Redirect to the admin's page. |
|
| 247 | + wp_redirect( admin_url() ); |
|
| 248 | + |
|
| 249 | + exit; |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + $language_select = $this->language_select_element; |
|
| 253 | + $country_select = $this->country_select_element; |
|
| 254 | + |
|
| 255 | + include plugin_dir_path( dirname( __FILE__ ) ) . 'admin/partials/wordlift-admin-setup.php'; |
|
| 256 | + |
|
| 257 | + exit; |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + /** |
|
| 261 | + * Save WordLift's configuration using the provided parameters. |
|
| 262 | + * |
|
| 263 | + * @param array $params An array of configuration parameters. |
|
| 264 | + * |
|
| 265 | + * @since 3.9.0 |
|
| 266 | + * |
|
| 267 | + */ |
|
| 268 | + public function save_configuration( $params ) { |
|
| 269 | + |
|
| 270 | + // We have the following parameters: |
|
| 271 | + // `key`, holding WL's key, |
|
| 272 | + // `vocabulary`, holding the vocabulary path, |
|
| 273 | + // `wl-site-language`, with the language code (e.g. `en`), |
|
| 274 | + // `wl-country-code` with the country code (e.g. `us`), |
|
| 275 | + // `share-diagnostic`, the user preferences about sharing data with us. |
|
| 276 | + // `user_type`, the user type either `personal` or `company`, |
|
| 277 | + // `name`, with the `personal` or `company`'s name, |
|
| 278 | + // `logo`, the attachment id for the `personal` or `company` entity. |
|
| 279 | + |
|
| 280 | + // Store the key: |
|
| 281 | + $this->configuration_service->set_key( $params['key'] ); |
|
| 282 | + |
|
| 283 | + // Store the vocabulary path: |
|
| 284 | + $this->configuration_service->set_entity_base_path( $params['vocabulary'] ); |
|
| 285 | + |
|
| 286 | + // Store the site's language: |
|
| 287 | + $this->configuration_service->set_language_code( $params['wl-site-language'] ); |
|
| 288 | + |
|
| 289 | + // Store the site's country: |
|
| 290 | + $this->configuration_service->set_country_code( $params['wl-country-code'] ); |
|
| 291 | + |
|
| 292 | + // Store the preferences in variable, because if the checkbox is not checked |
|
| 293 | + // the `share-diagnostic` will not exists in `$params` array. |
|
| 294 | + $share_diagnostic_preferences = empty( $params['share-diagnostic'] ) ? 'no' : 'yes'; |
|
| 295 | + |
|
| 296 | + // Store the diagnostic preferences: |
|
| 297 | + $this->configuration_service->set_diagnostic_preferences( $share_diagnostic_preferences ); |
|
| 298 | + |
|
| 299 | + // Set the type URI, either http://schema.org/Person or http://schema.org/Organization. |
|
| 300 | + $type_uri = sprintf( 'http://schema.org/%s', 'organization' === $params['user_type'] ? 'Organization' : 'Person' ); |
|
| 301 | + |
|
| 302 | + // Create an entity for the publisher. |
|
| 303 | + $publisher_post_id = $this->entity_service->create( $params['name'], $type_uri, $params['logo'], 'publish' ); |
|
| 304 | + |
|
| 305 | + // Store the publisher entity post id in the configuration. |
|
| 306 | + $this->configuration_service->set_publisher_id( $publisher_post_id ); |
|
| 307 | + |
|
| 308 | + flush_rewrite_rules(); // Needed because of possible change to the entity base path. |
|
| 309 | + |
|
| 310 | + } |
|
| 311 | 311 | |
| 312 | 312 | } |
@@ -4,54 +4,54 @@ |
||
| 4 | 4 | |
| 5 | 5 | class Features_Registry { |
| 6 | 6 | |
| 7 | - /** |
|
| 8 | - * @var array<Feature> |
|
| 9 | - */ |
|
| 10 | - private $features = array(); |
|
| 11 | - |
|
| 12 | - private static $instance = null; |
|
| 13 | - |
|
| 14 | - public static function get_instance() { |
|
| 15 | - if ( self::$instance === null ) { |
|
| 16 | - self::$instance = new Features_Registry(); |
|
| 17 | - } |
|
| 18 | - |
|
| 19 | - return self::$instance; |
|
| 20 | - } |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * @param $feature Feature |
|
| 24 | - */ |
|
| 25 | - public function register_feature( $feature ) { |
|
| 26 | - $this->features[] = $feature; |
|
| 27 | - } |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * @param $feature_slug string |
|
| 31 | - * @param $default_value bool |
|
| 32 | - * @param $callback callable |
|
| 33 | - */ |
|
| 34 | - public function register_feature_from_slug( $feature_slug, $default_value, $callback ) { |
|
| 35 | - $this->features[] = new Feature( |
|
| 36 | - $feature_slug, |
|
| 37 | - $default_value, |
|
| 38 | - $callback |
|
| 39 | - ); |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - public function initialize_all_features() { |
|
| 43 | - foreach ( $this->features as $feature ) { |
|
| 44 | - /** |
|
| 45 | - * @var $feature Feature |
|
| 46 | - */ |
|
| 47 | - $feature_slug = $feature->feature_slug; |
|
| 48 | - if ( apply_filters( "wl_feature__enable__${feature_slug}", $feature->default_value ) ) { |
|
| 49 | - call_user_func( $feature->callback ); |
|
| 50 | - } |
|
| 51 | - } |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - public function clear_all() { |
|
| 55 | - $this->features = array(); |
|
| 56 | - } |
|
| 7 | + /** |
|
| 8 | + * @var array<Feature> |
|
| 9 | + */ |
|
| 10 | + private $features = array(); |
|
| 11 | + |
|
| 12 | + private static $instance = null; |
|
| 13 | + |
|
| 14 | + public static function get_instance() { |
|
| 15 | + if ( self::$instance === null ) { |
|
| 16 | + self::$instance = new Features_Registry(); |
|
| 17 | + } |
|
| 18 | + |
|
| 19 | + return self::$instance; |
|
| 20 | + } |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * @param $feature Feature |
|
| 24 | + */ |
|
| 25 | + public function register_feature( $feature ) { |
|
| 26 | + $this->features[] = $feature; |
|
| 27 | + } |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * @param $feature_slug string |
|
| 31 | + * @param $default_value bool |
|
| 32 | + * @param $callback callable |
|
| 33 | + */ |
|
| 34 | + public function register_feature_from_slug( $feature_slug, $default_value, $callback ) { |
|
| 35 | + $this->features[] = new Feature( |
|
| 36 | + $feature_slug, |
|
| 37 | + $default_value, |
|
| 38 | + $callback |
|
| 39 | + ); |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + public function initialize_all_features() { |
|
| 43 | + foreach ( $this->features as $feature ) { |
|
| 44 | + /** |
|
| 45 | + * @var $feature Feature |
|
| 46 | + */ |
|
| 47 | + $feature_slug = $feature->feature_slug; |
|
| 48 | + if ( apply_filters( "wl_feature__enable__${feature_slug}", $feature->default_value ) ) { |
|
| 49 | + call_user_func( $feature->callback ); |
|
| 50 | + } |
|
| 51 | + } |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + public function clear_all() { |
|
| 55 | + $this->features = array(); |
|
| 56 | + } |
|
| 57 | 57 | } |
| 58 | 58 | \ No newline at end of file |
@@ -3,115 +3,115 @@ |
||
| 3 | 3 | namespace Wordlift\Features; |
| 4 | 4 | |
| 5 | 5 | class Response_Adapter { |
| 6 | - const WL_FEATURES = '_wl_features'; |
|
| 7 | - const WL_1 = 'wl1'; |
|
| 6 | + const WL_FEATURES = '_wl_features'; |
|
| 7 | + const WL_1 = 'wl1'; |
|
| 8 | 8 | |
| 9 | - /** |
|
| 10 | - * @var \Wordlift_Log_Service |
|
| 11 | - */ |
|
| 12 | - private $log; |
|
| 9 | + /** |
|
| 10 | + * @var \Wordlift_Log_Service |
|
| 11 | + */ |
|
| 12 | + private $log; |
|
| 13 | 13 | |
| 14 | - function __construct() { |
|
| 14 | + function __construct() { |
|
| 15 | 15 | |
| 16 | - $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
| 16 | + $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
| 17 | 17 | |
| 18 | - // Filter responses from the API calls to update the enabled features. |
|
| 19 | - add_filter( 'wl_api_service__response', array( $this, 'response' ), 10, 1 ); |
|
| 18 | + // Filter responses from the API calls to update the enabled features. |
|
| 19 | + add_filter( 'wl_api_service__response', array( $this, 'response' ), 10, 1 ); |
|
| 20 | 20 | |
| 21 | - // Initialize from `$_ENV`: this is currently required for Unit Tests, since `tests/bootstrap.php` loads WordLift |
|
| 22 | - // before it can actually query the enabled features via HTTP (mock), which would prevent files from being included. |
|
| 21 | + // Initialize from `$_ENV`: this is currently required for Unit Tests, since `tests/bootstrap.php` loads WordLift |
|
| 22 | + // before it can actually query the enabled features via HTTP (mock), which would prevent files from being included. |
|
| 23 | 23 | // $this->init_from_env(); |
| 24 | 24 | |
| 25 | - // Register the `wl_features__enable__{feature-name}` filters. |
|
| 26 | - $this->register_filters(); |
|
| 25 | + // Register the `wl_features__enable__{feature-name}` filters. |
|
| 26 | + $this->register_filters(); |
|
| 27 | 27 | |
| 28 | - // Hook to updates to the features setting to refresh the features' filters. |
|
| 29 | - add_action( 'update_option_' . self::WL_FEATURES, array( $this, 'register_filters' ), 10, 0 ); |
|
| 28 | + // Hook to updates to the features setting to refresh the features' filters. |
|
| 29 | + add_action( 'update_option_' . self::WL_FEATURES, array( $this, 'register_filters' ), 10, 0 ); |
|
| 30 | 30 | |
| 31 | - } |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | - function response( $response ) { |
|
| 33 | + function response( $response ) { |
|
| 34 | 34 | |
| 35 | - $headers = wp_remote_retrieve_headers( $response ); |
|
| 35 | + $headers = wp_remote_retrieve_headers( $response ); |
|
| 36 | 36 | |
| 37 | - // Bail out if the `wl1` header isn't defined. |
|
| 38 | - if ( ! isset( $headers[ self::WL_1 ] ) ) { |
|
| 39 | - return $response; |
|
| 40 | - } |
|
| 41 | - $wl1_as_base64_string = $headers[ self::WL_1 ]; |
|
| 42 | - $wl1 = json_decode( base64_decode( $wl1_as_base64_string ), true ); |
|
| 37 | + // Bail out if the `wl1` header isn't defined. |
|
| 38 | + if ( ! isset( $headers[ self::WL_1 ] ) ) { |
|
| 39 | + return $response; |
|
| 40 | + } |
|
| 41 | + $wl1_as_base64_string = $headers[ self::WL_1 ]; |
|
| 42 | + $wl1 = json_decode( base64_decode( $wl1_as_base64_string ), true ); |
|
| 43 | 43 | |
| 44 | - $this->log->debug( "WL1 [ encoded :: $wl1_as_base64_string ] " . var_export( $wl1, true ) ); |
|
| 44 | + $this->log->debug( "WL1 [ encoded :: $wl1_as_base64_string ] " . var_export( $wl1, true ) ); |
|
| 45 | 45 | |
| 46 | 46 | |
| 47 | - $updated_features = $wl1['features']; |
|
| 47 | + $updated_features = $wl1['features']; |
|
| 48 | 48 | |
| 49 | - $existing_features = get_option( self::WL_FEATURES, array() ); |
|
| 49 | + $existing_features = get_option( self::WL_FEATURES, array() ); |
|
| 50 | 50 | |
| 51 | - // Loop through updated features. |
|
| 52 | - foreach ( $updated_features as $feature_slug => $new_value ) { |
|
| 51 | + // Loop through updated features. |
|
| 52 | + foreach ( $updated_features as $feature_slug => $new_value ) { |
|
| 53 | 53 | |
| 54 | - // We cant pass false because that indicates if the feature is active or not, null is used to represent the features which are |
|
| 55 | - // not set before. |
|
| 56 | - $old_value = array_key_exists( $feature_slug, $existing_features ) ? $existing_features[ $feature_slug ] : null; |
|
| 54 | + // We cant pass false because that indicates if the feature is active or not, null is used to represent the features which are |
|
| 55 | + // not set before. |
|
| 56 | + $old_value = array_key_exists( $feature_slug, $existing_features ) ? $existing_features[ $feature_slug ] : null; |
|
| 57 | 57 | |
| 58 | - if ( $old_value !== $new_value ) { |
|
| 59 | - /** |
|
| 60 | - * @param $feature_slug string The feature slug. |
|
| 61 | - * @param $old_value null | boolean Null represents the feature flag was not set before. |
|
| 62 | - * @param $new_value boolean True or false. |
|
| 63 | - * |
|
| 64 | - * @since 3.32.1 |
|
| 65 | - * Hook : `wl_feature__change__{feature_slug}` |
|
| 66 | - * Action hook to be fired when there is a change in feature state. |
|
| 67 | - */ |
|
| 68 | - do_action( "wl_feature__change__${feature_slug}", $new_value, $old_value, $feature_slug ); |
|
| 69 | - } |
|
| 58 | + if ( $old_value !== $new_value ) { |
|
| 59 | + /** |
|
| 60 | + * @param $feature_slug string The feature slug. |
|
| 61 | + * @param $old_value null | boolean Null represents the feature flag was not set before. |
|
| 62 | + * @param $new_value boolean True or false. |
|
| 63 | + * |
|
| 64 | + * @since 3.32.1 |
|
| 65 | + * Hook : `wl_feature__change__{feature_slug}` |
|
| 66 | + * Action hook to be fired when there is a change in feature state. |
|
| 67 | + */ |
|
| 68 | + do_action( "wl_feature__change__${feature_slug}", $new_value, $old_value, $feature_slug ); |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - } |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | 73 | |
| 74 | - if ( isset( $updated_features ) ) { |
|
| 74 | + if ( isset( $updated_features ) ) { |
|
| 75 | 75 | |
| 76 | - if ( update_option( self::WL_FEATURES, (array) $updated_features, true ) ) { |
|
| 77 | - $this->register_filters(); |
|
| 78 | - } |
|
| 79 | - } |
|
| 76 | + if ( update_option( self::WL_FEATURES, (array) $updated_features, true ) ) { |
|
| 77 | + $this->register_filters(); |
|
| 78 | + } |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | - return $response; |
|
| 82 | - } |
|
| 81 | + return $response; |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - /** |
|
| 85 | - * Registers the feature filters. |
|
| 86 | - */ |
|
| 87 | - function register_filters() { |
|
| 84 | + /** |
|
| 85 | + * Registers the feature filters. |
|
| 86 | + */ |
|
| 87 | + function register_filters() { |
|
| 88 | 88 | |
| 89 | - $this->log->debug( 'Registering feature filters...' ); |
|
| 89 | + $this->log->debug( 'Registering feature filters...' ); |
|
| 90 | 90 | |
| 91 | - foreach ( (array) get_option( self::WL_FEATURES, array() ) as $name => $enabled ) { |
|
| 92 | - // Remove previous filters. |
|
| 93 | - remove_filter( "wl_feature__enable__${name}", '__return_true' ); |
|
| 94 | - remove_filter( "wl_feature__enable__${name}", '__return_false' ); |
|
| 91 | + foreach ( (array) get_option( self::WL_FEATURES, array() ) as $name => $enabled ) { |
|
| 92 | + // Remove previous filters. |
|
| 93 | + remove_filter( "wl_feature__enable__${name}", '__return_true' ); |
|
| 94 | + remove_filter( "wl_feature__enable__${name}", '__return_false' ); |
|
| 95 | 95 | |
| 96 | - $callback = ( $enabled ? '__return_true' : '__return_false' ); |
|
| 97 | - add_filter( "wl_feature__enable__${name}", $callback ); |
|
| 98 | - } |
|
| 96 | + $callback = ( $enabled ? '__return_true' : '__return_false' ); |
|
| 97 | + add_filter( "wl_feature__enable__${name}", $callback ); |
|
| 98 | + } |
|
| 99 | 99 | |
| 100 | - } |
|
| 100 | + } |
|
| 101 | 101 | |
| 102 | - private function init_from_env() { |
|
| 103 | - $features = array_reduce( array_filter( array_keys( $_ENV ), function ( $key ) { |
|
| 104 | - return preg_match( '~^WL_FEATURES__.*~', $key ); |
|
| 105 | - } ), function ( $features, $env ) { |
|
| 106 | - $name = strtolower( str_replace( '_', '-', substr( $env, strlen( 'WL_FEATURES__' ) ) ) ); |
|
| 107 | - $value = wp_validate_boolean( getenv( $env ) ); |
|
| 108 | - $features[ $name ] = $value; |
|
| 102 | + private function init_from_env() { |
|
| 103 | + $features = array_reduce( array_filter( array_keys( $_ENV ), function ( $key ) { |
|
| 104 | + return preg_match( '~^WL_FEATURES__.*~', $key ); |
|
| 105 | + } ), function ( $features, $env ) { |
|
| 106 | + $name = strtolower( str_replace( '_', '-', substr( $env, strlen( 'WL_FEATURES__' ) ) ) ); |
|
| 107 | + $value = wp_validate_boolean( getenv( $env ) ); |
|
| 108 | + $features[ $name ] = $value; |
|
| 109 | 109 | |
| 110 | - return $features; |
|
| 111 | - }, array() ); |
|
| 110 | + return $features; |
|
| 111 | + }, array() ); |
|
| 112 | 112 | |
| 113 | - update_option( self::WL_FEATURES, (array) $features, true ); |
|
| 113 | + update_option( self::WL_FEATURES, (array) $features, true ); |
|
| 114 | 114 | |
| 115 | - } |
|
| 115 | + } |
|
| 116 | 116 | |
| 117 | 117 | } |
@@ -13,10 +13,10 @@ discard block |
||
| 13 | 13 | |
| 14 | 14 | function __construct() { |
| 15 | 15 | |
| 16 | - $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
| 16 | + $this->log = \Wordlift_Log_Service::get_logger(get_class()); |
|
| 17 | 17 | |
| 18 | 18 | // Filter responses from the API calls to update the enabled features. |
| 19 | - add_filter( 'wl_api_service__response', array( $this, 'response' ), 10, 1 ); |
|
| 19 | + add_filter('wl_api_service__response', array($this, 'response'), 10, 1); |
|
| 20 | 20 | |
| 21 | 21 | // Initialize from `$_ENV`: this is currently required for Unit Tests, since `tests/bootstrap.php` loads WordLift |
| 22 | 22 | // before it can actually query the enabled features via HTTP (mock), which would prevent files from being included. |
@@ -26,36 +26,36 @@ discard block |
||
| 26 | 26 | $this->register_filters(); |
| 27 | 27 | |
| 28 | 28 | // Hook to updates to the features setting to refresh the features' filters. |
| 29 | - add_action( 'update_option_' . self::WL_FEATURES, array( $this, 'register_filters' ), 10, 0 ); |
|
| 29 | + add_action('update_option_'.self::WL_FEATURES, array($this, 'register_filters'), 10, 0); |
|
| 30 | 30 | |
| 31 | 31 | } |
| 32 | 32 | |
| 33 | - function response( $response ) { |
|
| 33 | + function response($response) { |
|
| 34 | 34 | |
| 35 | - $headers = wp_remote_retrieve_headers( $response ); |
|
| 35 | + $headers = wp_remote_retrieve_headers($response); |
|
| 36 | 36 | |
| 37 | 37 | // Bail out if the `wl1` header isn't defined. |
| 38 | - if ( ! isset( $headers[ self::WL_1 ] ) ) { |
|
| 38 | + if ( ! isset($headers[self::WL_1])) { |
|
| 39 | 39 | return $response; |
| 40 | 40 | } |
| 41 | - $wl1_as_base64_string = $headers[ self::WL_1 ]; |
|
| 42 | - $wl1 = json_decode( base64_decode( $wl1_as_base64_string ), true ); |
|
| 41 | + $wl1_as_base64_string = $headers[self::WL_1]; |
|
| 42 | + $wl1 = json_decode(base64_decode($wl1_as_base64_string), true); |
|
| 43 | 43 | |
| 44 | - $this->log->debug( "WL1 [ encoded :: $wl1_as_base64_string ] " . var_export( $wl1, true ) ); |
|
| 44 | + $this->log->debug("WL1 [ encoded :: $wl1_as_base64_string ] ".var_export($wl1, true)); |
|
| 45 | 45 | |
| 46 | 46 | |
| 47 | 47 | $updated_features = $wl1['features']; |
| 48 | 48 | |
| 49 | - $existing_features = get_option( self::WL_FEATURES, array() ); |
|
| 49 | + $existing_features = get_option(self::WL_FEATURES, array()); |
|
| 50 | 50 | |
| 51 | 51 | // Loop through updated features. |
| 52 | - foreach ( $updated_features as $feature_slug => $new_value ) { |
|
| 52 | + foreach ($updated_features as $feature_slug => $new_value) { |
|
| 53 | 53 | |
| 54 | 54 | // We cant pass false because that indicates if the feature is active or not, null is used to represent the features which are |
| 55 | 55 | // not set before. |
| 56 | - $old_value = array_key_exists( $feature_slug, $existing_features ) ? $existing_features[ $feature_slug ] : null; |
|
| 56 | + $old_value = array_key_exists($feature_slug, $existing_features) ? $existing_features[$feature_slug] : null; |
|
| 57 | 57 | |
| 58 | - if ( $old_value !== $new_value ) { |
|
| 58 | + if ($old_value !== $new_value) { |
|
| 59 | 59 | /** |
| 60 | 60 | * @param $feature_slug string The feature slug. |
| 61 | 61 | * @param $old_value null | boolean Null represents the feature flag was not set before. |
@@ -65,15 +65,15 @@ discard block |
||
| 65 | 65 | * Hook : `wl_feature__change__{feature_slug}` |
| 66 | 66 | * Action hook to be fired when there is a change in feature state. |
| 67 | 67 | */ |
| 68 | - do_action( "wl_feature__change__${feature_slug}", $new_value, $old_value, $feature_slug ); |
|
| 68 | + do_action("wl_feature__change__${feature_slug}", $new_value, $old_value, $feature_slug); |
|
| 69 | 69 | } |
| 70 | 70 | |
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | |
| 74 | - if ( isset( $updated_features ) ) { |
|
| 74 | + if (isset($updated_features)) { |
|
| 75 | 75 | |
| 76 | - if ( update_option( self::WL_FEATURES, (array) $updated_features, true ) ) { |
|
| 76 | + if (update_option(self::WL_FEATURES, (array) $updated_features, true)) { |
|
| 77 | 77 | $this->register_filters(); |
| 78 | 78 | } |
| 79 | 79 | } |
@@ -86,31 +86,31 @@ discard block |
||
| 86 | 86 | */ |
| 87 | 87 | function register_filters() { |
| 88 | 88 | |
| 89 | - $this->log->debug( 'Registering feature filters...' ); |
|
| 89 | + $this->log->debug('Registering feature filters...'); |
|
| 90 | 90 | |
| 91 | - foreach ( (array) get_option( self::WL_FEATURES, array() ) as $name => $enabled ) { |
|
| 91 | + foreach ((array) get_option(self::WL_FEATURES, array()) as $name => $enabled) { |
|
| 92 | 92 | // Remove previous filters. |
| 93 | - remove_filter( "wl_feature__enable__${name}", '__return_true' ); |
|
| 94 | - remove_filter( "wl_feature__enable__${name}", '__return_false' ); |
|
| 93 | + remove_filter("wl_feature__enable__${name}", '__return_true'); |
|
| 94 | + remove_filter("wl_feature__enable__${name}", '__return_false'); |
|
| 95 | 95 | |
| 96 | - $callback = ( $enabled ? '__return_true' : '__return_false' ); |
|
| 97 | - add_filter( "wl_feature__enable__${name}", $callback ); |
|
| 96 | + $callback = ($enabled ? '__return_true' : '__return_false'); |
|
| 97 | + add_filter("wl_feature__enable__${name}", $callback); |
|
| 98 | 98 | } |
| 99 | 99 | |
| 100 | 100 | } |
| 101 | 101 | |
| 102 | 102 | private function init_from_env() { |
| 103 | - $features = array_reduce( array_filter( array_keys( $_ENV ), function ( $key ) { |
|
| 104 | - return preg_match( '~^WL_FEATURES__.*~', $key ); |
|
| 105 | - } ), function ( $features, $env ) { |
|
| 106 | - $name = strtolower( str_replace( '_', '-', substr( $env, strlen( 'WL_FEATURES__' ) ) ) ); |
|
| 107 | - $value = wp_validate_boolean( getenv( $env ) ); |
|
| 108 | - $features[ $name ] = $value; |
|
| 103 | + $features = array_reduce(array_filter(array_keys($_ENV), function($key) { |
|
| 104 | + return preg_match('~^WL_FEATURES__.*~', $key); |
|
| 105 | + } ), function($features, $env) { |
|
| 106 | + $name = strtolower(str_replace('_', '-', substr($env, strlen('WL_FEATURES__')))); |
|
| 107 | + $value = wp_validate_boolean(getenv($env)); |
|
| 108 | + $features[$name] = $value; |
|
| 109 | 109 | |
| 110 | 110 | return $features; |
| 111 | - }, array() ); |
|
| 111 | + }, array()); |
|
| 112 | 112 | |
| 113 | - update_option( self::WL_FEATURES, (array) $features, true ); |
|
| 113 | + update_option(self::WL_FEATURES, (array) $features, true); |
|
| 114 | 114 | |
| 115 | 115 | } |
| 116 | 116 | |
@@ -11,191 +11,191 @@ |
||
| 11 | 11 | class Entity_Type_Setter { |
| 12 | 12 | |
| 13 | 13 | |
| 14 | - const STARTER_PLAN = 'entity-types-starter'; |
|
| 15 | - |
|
| 16 | - const PROFESSIONAL_PLAN = 'entity-types-professional'; |
|
| 17 | - |
|
| 18 | - const BUSINESS_PLAN = 'entity-types-business'; |
|
| 19 | - |
|
| 20 | - public function __construct() { |
|
| 21 | - add_action( "wl_feature__change__entity-types-starter", array( $this, 'wl_entity_types_feature_changed' ), 10, 3 ); |
|
| 22 | - add_action( "wl_feature__change__entity-types-professional", array( $this, 'wl_entity_types_feature_changed' ), 10, 3 ); |
|
| 23 | - add_action( "wl_feature__change__entity-types-business", array( $this, 'wl_entity_types_feature_changed' ), 10, 3 ); |
|
| 24 | - } |
|
| 25 | - |
|
| 26 | - |
|
| 27 | - public function wl_entity_types_feature_changed( $new_value, $old_value, $feature_slug ) { |
|
| 28 | - |
|
| 29 | - // If the entity types is not set by server, then return early. |
|
| 30 | - if ( ! $new_value ) { |
|
| 31 | - return; |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - |
|
| 35 | - $entity_types_data = self::get_entity_types_by_feature_flag( $feature_slug ); |
|
| 36 | - |
|
| 37 | - // If we dont have entity types returned, then dont reset the entity types, return early. |
|
| 38 | - if ( ! $entity_types_data ) { |
|
| 39 | - return; |
|
| 40 | - } |
|
| 41 | - // Repopulate the ones returned by package type. |
|
| 42 | - foreach ( $entity_types_data as $entity_type_data ) { |
|
| 43 | - |
|
| 44 | - $schema_label = $entity_type_data['label']; |
|
| 45 | - |
|
| 46 | - $term_exists = get_term_by( 'name', $schema_label, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ) instanceof \WP_Term; |
|
| 47 | - |
|
| 48 | - if ( $term_exists ) { |
|
| 49 | - // Dont create term if it already exists. |
|
| 50 | - continue; |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - |
|
| 54 | - $term_data = wp_insert_term( |
|
| 55 | - $schema_label, |
|
| 56 | - Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 57 | - array( |
|
| 58 | - 'description' => $entity_type_data['description'], |
|
| 59 | - 'slug' => $entity_type_data['slug'] |
|
| 60 | - ) |
|
| 61 | - ); |
|
| 62 | - |
|
| 63 | - $term_id = $term_data['term_id']; |
|
| 64 | - |
|
| 65 | - update_term_meta( $term_id, '_wl_uri', 'http://schema.org/' . $schema_label ); |
|
| 66 | - update_term_meta( $term_id, '_wl_name', $schema_label ); |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - |
|
| 72 | - public static function get_starter_entity_types() { |
|
| 73 | - return array( |
|
| 74 | - array( |
|
| 75 | - 'label' => 'Person', |
|
| 76 | - 'description' => 'A person (or a music artist).', |
|
| 77 | - 'slug' => 'person' |
|
| 78 | - ), |
|
| 79 | - array( |
|
| 80 | - 'label' => 'Thing', |
|
| 81 | - 'description' => 'A generic thing (something that doesn\'t fit in the previous definitions.', |
|
| 82 | - 'slug' => 'thing' |
|
| 83 | - ), |
|
| 84 | - |
|
| 85 | - array( |
|
| 86 | - 'label' => 'Place', |
|
| 87 | - 'description' => 'A place.', |
|
| 88 | - 'slug' => 'place' |
|
| 89 | - ), |
|
| 90 | - |
|
| 91 | - array( |
|
| 92 | - 'label' => 'CreativeWork', |
|
| 93 | - 'description' => 'A creative work (or a Music Album).', |
|
| 94 | - 'slug' => 'creative-work' |
|
| 95 | - ), |
|
| 96 | - array( |
|
| 97 | - 'label' => 'Organization', |
|
| 98 | - 'description' => 'An organization, including a government or a newspaper.', |
|
| 99 | - 'slug' => 'organization' |
|
| 100 | - ), |
|
| 101 | - |
|
| 102 | - array( |
|
| 103 | - 'label' => 'Article', |
|
| 104 | - 'description' => 'An article, such as a news article or piece of investigative report. Newspapers and magazines have articles of many different types and this is intended to cover them all.', |
|
| 105 | - 'slug' => 'article' |
|
| 106 | - ), |
|
| 107 | - |
|
| 108 | - array( |
|
| 109 | - 'label' => 'WebSite', |
|
| 110 | - 'description' => 'A WebSite is a set of related web pages and other items typically served from a single web domain and accessible via URLs.', |
|
| 111 | - 'slug' => 'web-site' |
|
| 112 | - ), |
|
| 113 | - |
|
| 114 | - array( |
|
| 115 | - 'label' => 'NewsArticle', |
|
| 116 | - 'description' => 'A NewsArticle is an article whose content reports news, or provides background context and supporting materials for understanding the news.', |
|
| 117 | - 'slug' => 'news-article' |
|
| 118 | - ), |
|
| 119 | - |
|
| 120 | - array( |
|
| 121 | - 'label' => 'AboutPage', |
|
| 122 | - 'description' => 'An About page.', |
|
| 123 | - 'slug' => 'about-page' |
|
| 124 | - ), |
|
| 125 | - |
|
| 126 | - array( |
|
| 127 | - 'label' => 'ContactPage', |
|
| 128 | - 'description' => 'A Contact Page.', |
|
| 129 | - 'slug' => 'contact-page' |
|
| 130 | - ) |
|
| 131 | - |
|
| 132 | - ); |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - |
|
| 136 | - public static function get_professional_entity_types() { |
|
| 137 | - return array( |
|
| 138 | - |
|
| 139 | - array( |
|
| 140 | - 'label' => 'FAQPage', |
|
| 141 | - 'description' => 'A FAQPage is a WebPage presenting one or more "Frequently asked questions".', |
|
| 142 | - 'slug' => 'faq-page' |
|
| 143 | - ), |
|
| 144 | - array( |
|
| 145 | - 'label' => 'LocalBusiness', |
|
| 146 | - 'description' => 'A particular physical business or branch of an organization. Examples of LocalBusiness include a restaurant, a particular branch of a restaurant chain, a branch of a bank, a medical practice, a club, a bowling alley, etc.', |
|
| 147 | - 'slug' => 'local-business' |
|
| 148 | - |
|
| 149 | - ), |
|
| 150 | - array( |
|
| 151 | - 'label' => 'Recipe', |
|
| 152 | - 'description' => 'A recipe', |
|
| 153 | - 'slug' => 'recipe' |
|
| 154 | - ), |
|
| 155 | - array( |
|
| 156 | - 'label' => 'PodcastEpisode', |
|
| 157 | - 'description' => 'A single episode of a podcast series.', |
|
| 158 | - 'slug' => 'podcast-episode' |
|
| 159 | - ), |
|
| 160 | - array( |
|
| 161 | - 'label' => 'Course', |
|
| 162 | - 'description' => 'A description of an educational course which may be offered as distinct instances at which take place at different times or take place at different locations, or be offered through different media or modes of study.', |
|
| 163 | - 'slug' => 'course' |
|
| 164 | - ), |
|
| 165 | - array( |
|
| 166 | - 'label' => 'Event', |
|
| 167 | - 'description' => 'An event happening at a certain time and location, such as a concert, lecture, or festival.', |
|
| 168 | - 'slug' => 'event' |
|
| 169 | - ), |
|
| 170 | - array( |
|
| 171 | - 'label' => 'Review', |
|
| 172 | - 'description' => 'A review of an item - for example, of a restaurant, movie, or store.', |
|
| 173 | - 'slug' => 'review' |
|
| 174 | - ), |
|
| 175 | - |
|
| 176 | - ); |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - |
|
| 180 | - private static function get_entity_types_by_feature_flag( $package_type ) { |
|
| 181 | - |
|
| 182 | - switch ( $package_type ) { |
|
| 183 | - case self::STARTER_PLAN: |
|
| 184 | - return self::get_starter_entity_types(); |
|
| 185 | - case self::BUSINESS_PLAN: |
|
| 186 | - case self::PROFESSIONAL_PLAN: |
|
| 187 | - // We return same entity types for professional and business plans. |
|
| 188 | - // Business plan should have sync schema ui feature enabled, to sync all the entity types. |
|
| 189 | - return array_merge( |
|
| 190 | - self::get_starter_entity_types(), |
|
| 191 | - self::get_professional_entity_types() |
|
| 192 | - ); |
|
| 193 | - default: |
|
| 194 | - return array(); |
|
| 195 | - |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - } |
|
| 14 | + const STARTER_PLAN = 'entity-types-starter'; |
|
| 15 | + |
|
| 16 | + const PROFESSIONAL_PLAN = 'entity-types-professional'; |
|
| 17 | + |
|
| 18 | + const BUSINESS_PLAN = 'entity-types-business'; |
|
| 19 | + |
|
| 20 | + public function __construct() { |
|
| 21 | + add_action( "wl_feature__change__entity-types-starter", array( $this, 'wl_entity_types_feature_changed' ), 10, 3 ); |
|
| 22 | + add_action( "wl_feature__change__entity-types-professional", array( $this, 'wl_entity_types_feature_changed' ), 10, 3 ); |
|
| 23 | + add_action( "wl_feature__change__entity-types-business", array( $this, 'wl_entity_types_feature_changed' ), 10, 3 ); |
|
| 24 | + } |
|
| 25 | + |
|
| 26 | + |
|
| 27 | + public function wl_entity_types_feature_changed( $new_value, $old_value, $feature_slug ) { |
|
| 28 | + |
|
| 29 | + // If the entity types is not set by server, then return early. |
|
| 30 | + if ( ! $new_value ) { |
|
| 31 | + return; |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + |
|
| 35 | + $entity_types_data = self::get_entity_types_by_feature_flag( $feature_slug ); |
|
| 36 | + |
|
| 37 | + // If we dont have entity types returned, then dont reset the entity types, return early. |
|
| 38 | + if ( ! $entity_types_data ) { |
|
| 39 | + return; |
|
| 40 | + } |
|
| 41 | + // Repopulate the ones returned by package type. |
|
| 42 | + foreach ( $entity_types_data as $entity_type_data ) { |
|
| 43 | + |
|
| 44 | + $schema_label = $entity_type_data['label']; |
|
| 45 | + |
|
| 46 | + $term_exists = get_term_by( 'name', $schema_label, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ) instanceof \WP_Term; |
|
| 47 | + |
|
| 48 | + if ( $term_exists ) { |
|
| 49 | + // Dont create term if it already exists. |
|
| 50 | + continue; |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + |
|
| 54 | + $term_data = wp_insert_term( |
|
| 55 | + $schema_label, |
|
| 56 | + Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 57 | + array( |
|
| 58 | + 'description' => $entity_type_data['description'], |
|
| 59 | + 'slug' => $entity_type_data['slug'] |
|
| 60 | + ) |
|
| 61 | + ); |
|
| 62 | + |
|
| 63 | + $term_id = $term_data['term_id']; |
|
| 64 | + |
|
| 65 | + update_term_meta( $term_id, '_wl_uri', 'http://schema.org/' . $schema_label ); |
|
| 66 | + update_term_meta( $term_id, '_wl_name', $schema_label ); |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + |
|
| 72 | + public static function get_starter_entity_types() { |
|
| 73 | + return array( |
|
| 74 | + array( |
|
| 75 | + 'label' => 'Person', |
|
| 76 | + 'description' => 'A person (or a music artist).', |
|
| 77 | + 'slug' => 'person' |
|
| 78 | + ), |
|
| 79 | + array( |
|
| 80 | + 'label' => 'Thing', |
|
| 81 | + 'description' => 'A generic thing (something that doesn\'t fit in the previous definitions.', |
|
| 82 | + 'slug' => 'thing' |
|
| 83 | + ), |
|
| 84 | + |
|
| 85 | + array( |
|
| 86 | + 'label' => 'Place', |
|
| 87 | + 'description' => 'A place.', |
|
| 88 | + 'slug' => 'place' |
|
| 89 | + ), |
|
| 90 | + |
|
| 91 | + array( |
|
| 92 | + 'label' => 'CreativeWork', |
|
| 93 | + 'description' => 'A creative work (or a Music Album).', |
|
| 94 | + 'slug' => 'creative-work' |
|
| 95 | + ), |
|
| 96 | + array( |
|
| 97 | + 'label' => 'Organization', |
|
| 98 | + 'description' => 'An organization, including a government or a newspaper.', |
|
| 99 | + 'slug' => 'organization' |
|
| 100 | + ), |
|
| 101 | + |
|
| 102 | + array( |
|
| 103 | + 'label' => 'Article', |
|
| 104 | + 'description' => 'An article, such as a news article or piece of investigative report. Newspapers and magazines have articles of many different types and this is intended to cover them all.', |
|
| 105 | + 'slug' => 'article' |
|
| 106 | + ), |
|
| 107 | + |
|
| 108 | + array( |
|
| 109 | + 'label' => 'WebSite', |
|
| 110 | + 'description' => 'A WebSite is a set of related web pages and other items typically served from a single web domain and accessible via URLs.', |
|
| 111 | + 'slug' => 'web-site' |
|
| 112 | + ), |
|
| 113 | + |
|
| 114 | + array( |
|
| 115 | + 'label' => 'NewsArticle', |
|
| 116 | + 'description' => 'A NewsArticle is an article whose content reports news, or provides background context and supporting materials for understanding the news.', |
|
| 117 | + 'slug' => 'news-article' |
|
| 118 | + ), |
|
| 119 | + |
|
| 120 | + array( |
|
| 121 | + 'label' => 'AboutPage', |
|
| 122 | + 'description' => 'An About page.', |
|
| 123 | + 'slug' => 'about-page' |
|
| 124 | + ), |
|
| 125 | + |
|
| 126 | + array( |
|
| 127 | + 'label' => 'ContactPage', |
|
| 128 | + 'description' => 'A Contact Page.', |
|
| 129 | + 'slug' => 'contact-page' |
|
| 130 | + ) |
|
| 131 | + |
|
| 132 | + ); |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + |
|
| 136 | + public static function get_professional_entity_types() { |
|
| 137 | + return array( |
|
| 138 | + |
|
| 139 | + array( |
|
| 140 | + 'label' => 'FAQPage', |
|
| 141 | + 'description' => 'A FAQPage is a WebPage presenting one or more "Frequently asked questions".', |
|
| 142 | + 'slug' => 'faq-page' |
|
| 143 | + ), |
|
| 144 | + array( |
|
| 145 | + 'label' => 'LocalBusiness', |
|
| 146 | + 'description' => 'A particular physical business or branch of an organization. Examples of LocalBusiness include a restaurant, a particular branch of a restaurant chain, a branch of a bank, a medical practice, a club, a bowling alley, etc.', |
|
| 147 | + 'slug' => 'local-business' |
|
| 148 | + |
|
| 149 | + ), |
|
| 150 | + array( |
|
| 151 | + 'label' => 'Recipe', |
|
| 152 | + 'description' => 'A recipe', |
|
| 153 | + 'slug' => 'recipe' |
|
| 154 | + ), |
|
| 155 | + array( |
|
| 156 | + 'label' => 'PodcastEpisode', |
|
| 157 | + 'description' => 'A single episode of a podcast series.', |
|
| 158 | + 'slug' => 'podcast-episode' |
|
| 159 | + ), |
|
| 160 | + array( |
|
| 161 | + 'label' => 'Course', |
|
| 162 | + 'description' => 'A description of an educational course which may be offered as distinct instances at which take place at different times or take place at different locations, or be offered through different media or modes of study.', |
|
| 163 | + 'slug' => 'course' |
|
| 164 | + ), |
|
| 165 | + array( |
|
| 166 | + 'label' => 'Event', |
|
| 167 | + 'description' => 'An event happening at a certain time and location, such as a concert, lecture, or festival.', |
|
| 168 | + 'slug' => 'event' |
|
| 169 | + ), |
|
| 170 | + array( |
|
| 171 | + 'label' => 'Review', |
|
| 172 | + 'description' => 'A review of an item - for example, of a restaurant, movie, or store.', |
|
| 173 | + 'slug' => 'review' |
|
| 174 | + ), |
|
| 175 | + |
|
| 176 | + ); |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + |
|
| 180 | + private static function get_entity_types_by_feature_flag( $package_type ) { |
|
| 181 | + |
|
| 182 | + switch ( $package_type ) { |
|
| 183 | + case self::STARTER_PLAN: |
|
| 184 | + return self::get_starter_entity_types(); |
|
| 185 | + case self::BUSINESS_PLAN: |
|
| 186 | + case self::PROFESSIONAL_PLAN: |
|
| 187 | + // We return same entity types for professional and business plans. |
|
| 188 | + // Business plan should have sync schema ui feature enabled, to sync all the entity types. |
|
| 189 | + return array_merge( |
|
| 190 | + self::get_starter_entity_types(), |
|
| 191 | + self::get_professional_entity_types() |
|
| 192 | + ); |
|
| 193 | + default: |
|
| 194 | + return array(); |
|
| 195 | + |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + } |
|
| 199 | 199 | |
| 200 | 200 | |
| 201 | 201 | } |
| 202 | 202 | \ No newline at end of file |
@@ -18,34 +18,34 @@ discard block |
||
| 18 | 18 | const BUSINESS_PLAN = 'entity-types-business'; |
| 19 | 19 | |
| 20 | 20 | public function __construct() { |
| 21 | - add_action( "wl_feature__change__entity-types-starter", array( $this, 'wl_entity_types_feature_changed' ), 10, 3 ); |
|
| 22 | - add_action( "wl_feature__change__entity-types-professional", array( $this, 'wl_entity_types_feature_changed' ), 10, 3 ); |
|
| 23 | - add_action( "wl_feature__change__entity-types-business", array( $this, 'wl_entity_types_feature_changed' ), 10, 3 ); |
|
| 21 | + add_action("wl_feature__change__entity-types-starter", array($this, 'wl_entity_types_feature_changed'), 10, 3); |
|
| 22 | + add_action("wl_feature__change__entity-types-professional", array($this, 'wl_entity_types_feature_changed'), 10, 3); |
|
| 23 | + add_action("wl_feature__change__entity-types-business", array($this, 'wl_entity_types_feature_changed'), 10, 3); |
|
| 24 | 24 | } |
| 25 | 25 | |
| 26 | 26 | |
| 27 | - public function wl_entity_types_feature_changed( $new_value, $old_value, $feature_slug ) { |
|
| 27 | + public function wl_entity_types_feature_changed($new_value, $old_value, $feature_slug) { |
|
| 28 | 28 | |
| 29 | 29 | // If the entity types is not set by server, then return early. |
| 30 | - if ( ! $new_value ) { |
|
| 30 | + if ( ! $new_value) { |
|
| 31 | 31 | return; |
| 32 | 32 | } |
| 33 | 33 | |
| 34 | 34 | |
| 35 | - $entity_types_data = self::get_entity_types_by_feature_flag( $feature_slug ); |
|
| 35 | + $entity_types_data = self::get_entity_types_by_feature_flag($feature_slug); |
|
| 36 | 36 | |
| 37 | 37 | // If we dont have entity types returned, then dont reset the entity types, return early. |
| 38 | - if ( ! $entity_types_data ) { |
|
| 38 | + if ( ! $entity_types_data) { |
|
| 39 | 39 | return; |
| 40 | 40 | } |
| 41 | 41 | // Repopulate the ones returned by package type. |
| 42 | - foreach ( $entity_types_data as $entity_type_data ) { |
|
| 42 | + foreach ($entity_types_data as $entity_type_data) { |
|
| 43 | 43 | |
| 44 | 44 | $schema_label = $entity_type_data['label']; |
| 45 | 45 | |
| 46 | - $term_exists = get_term_by( 'name', $schema_label, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ) instanceof \WP_Term; |
|
| 46 | + $term_exists = get_term_by('name', $schema_label, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME) instanceof \WP_Term; |
|
| 47 | 47 | |
| 48 | - if ( $term_exists ) { |
|
| 48 | + if ($term_exists) { |
|
| 49 | 49 | // Dont create term if it already exists. |
| 50 | 50 | continue; |
| 51 | 51 | } |
@@ -62,8 +62,8 @@ discard block |
||
| 62 | 62 | |
| 63 | 63 | $term_id = $term_data['term_id']; |
| 64 | 64 | |
| 65 | - update_term_meta( $term_id, '_wl_uri', 'http://schema.org/' . $schema_label ); |
|
| 66 | - update_term_meta( $term_id, '_wl_name', $schema_label ); |
|
| 65 | + update_term_meta($term_id, '_wl_uri', 'http://schema.org/'.$schema_label); |
|
| 66 | + update_term_meta($term_id, '_wl_name', $schema_label); |
|
| 67 | 67 | } |
| 68 | 68 | |
| 69 | 69 | } |
@@ -177,9 +177,9 @@ discard block |
||
| 177 | 177 | } |
| 178 | 178 | |
| 179 | 179 | |
| 180 | - private static function get_entity_types_by_feature_flag( $package_type ) { |
|
| 180 | + private static function get_entity_types_by_feature_flag($package_type) { |
|
| 181 | 181 | |
| 182 | - switch ( $package_type ) { |
|
| 182 | + switch ($package_type) { |
|
| 183 | 183 | case self::STARTER_PLAN: |
| 184 | 184 | return self::get_starter_entity_types(); |
| 185 | 185 | case self::BUSINESS_PLAN: |
@@ -18,323 +18,323 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class Wordlift_Admin_Settings_Analytics_Page extends Wordlift_Admin_Page { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * A singleton instance of the Notice service. |
|
| 23 | - * |
|
| 24 | - * @since 3.21.0 |
|
| 25 | - * @access private |
|
| 26 | - * @var \Wordlift_Admin_Settings_Analytics_Page $instance A singleton instance of a {@link Wordlift_Admin_Settings_Analytics_Page} class. |
|
| 27 | - */ |
|
| 28 | - private static $instance; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * A {@link Wordlift_Configuration_Service} instance. |
|
| 32 | - * |
|
| 33 | - * @since 3.21.0 |
|
| 34 | - * @access private |
|
| 35 | - * @var \Wordlift_Configuration_Service $configuration_service A {@link Wordlift_Configuration_Service} instance. |
|
| 36 | - */ |
|
| 37 | - private $configuration_service; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * A {@link Wordlift_Admin_Input_Element} element renderer. |
|
| 41 | - * |
|
| 42 | - * @since 3.21.0 |
|
| 43 | - * @access private |
|
| 44 | - * @var \Wordlift_Admin_Input_Element $input_element An {@link Wordlift_Admin_Input_Element} element renderer. |
|
| 45 | - */ |
|
| 46 | - private $input_element; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * A {@link Wordlift_Admin_Radio_Input_Element} element renderer. |
|
| 50 | - * |
|
| 51 | - * @since 3.21.0 |
|
| 52 | - * @access protected |
|
| 53 | - * @var \Wordlift_Admin_Radio_Input_Element $radio_input_element A {@link Wordlift_Admin_Radio_Input_Element} element renderer. |
|
| 54 | - */ |
|
| 55 | - private $radio_input_element; |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * Create a {@link Wordlift_Admin_Settings_Page} instance. |
|
| 59 | - * |
|
| 60 | - * @param \Wordlift_Configuration_Service $configuration_service The wordlift configuration service. |
|
| 61 | - * @param \Wordlift_Admin_Input_Element $input_element An input element class to output input boxes in a settings form. |
|
| 62 | - * @param \Wordlift_Admin_Radio_Input_Element $radio_input_element A radio element input class for use in a settings form. |
|
| 63 | - * |
|
| 64 | - * @since 3.21.0 |
|
| 65 | - * |
|
| 66 | - */ |
|
| 67 | - public function __construct( $configuration_service, $input_element, $radio_input_element ) { |
|
| 68 | - |
|
| 69 | - $this->configuration_service = $configuration_service; |
|
| 70 | - |
|
| 71 | - // Set a reference to the UI elements. |
|
| 72 | - $this->input_element = $input_element; |
|
| 73 | - $this->radio_input_element = $radio_input_element; |
|
| 74 | - |
|
| 75 | - // Adds this page to the main admin settings tabbed menu. |
|
| 76 | - add_filter( 'wl_admin_page_tabs', array( $this, 'add_admin_page_tab' ) ); |
|
| 77 | - |
|
| 78 | - self::$instance = $this; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * Get the singleton instance of the Notice service. |
|
| 83 | - * |
|
| 84 | - * @return \Wordlift_Admin_Settings_Page The singleton instance of the settings page service. |
|
| 85 | - * @since 3.21.0 |
|
| 86 | - */ |
|
| 87 | - public static function get_instance() { |
|
| 88 | - |
|
| 89 | - return self::$instance; |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * @inheritdoc |
|
| 94 | - */ |
|
| 95 | - protected function get_parent_slug() { |
|
| 96 | - |
|
| 97 | - return 'wl_admin_menu'; |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * @inheritdoc |
|
| 102 | - */ |
|
| 103 | - protected function get_capability() { |
|
| 104 | - |
|
| 105 | - return 'manage_options'; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * @inheritdoc |
|
| 110 | - */ |
|
| 111 | - public function get_page_title() { |
|
| 112 | - |
|
| 113 | - return __( 'WorldLift Analytics Settings', 'wordlift' ); |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * @inheritdoc |
|
| 118 | - */ |
|
| 119 | - public function get_menu_title() { |
|
| 120 | - |
|
| 121 | - return __( 'Analytics Settings', 'wordlift' ); |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * @inheritdoc |
|
| 126 | - */ |
|
| 127 | - public function get_menu_slug() { |
|
| 128 | - |
|
| 129 | - return 'wl_configuration_admin_analytics_menu'; |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * @inheritdoc |
|
| 134 | - */ |
|
| 135 | - public function get_partial_name() { |
|
| 136 | - |
|
| 137 | - return 'wordlift-admin-settings-analytics-page.php'; |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * Returns the string to use for the tab slug on main admin settings page. |
|
| 142 | - * |
|
| 143 | - * @method get_admin_page_tab_slug |
|
| 144 | - * @return string |
|
| 145 | - * @since 3.21.0 |
|
| 146 | - */ |
|
| 147 | - public function get_admin_page_tab_slug() { |
|
| 148 | - return 'analytics'; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * Adds pages to the tabs menu on the main admin settings page. |
|
| 153 | - * |
|
| 154 | - * @method add_admin_page_tab |
|
| 155 | - * @param array $tabs an array of tab titles and slugs to use when building a tabbed menu on option pages. |
|
| 156 | - * |
|
| 157 | - * @since 3.21.0 |
|
| 158 | - */ |
|
| 159 | - public function add_admin_page_tab( $tabs ) { |
|
| 160 | - if ( apply_filters( 'wl_feature__enable__analytics', true ) ) { |
|
| 161 | - $tabs[] = array( |
|
| 162 | - 'title' => $this->get_menu_title(), |
|
| 163 | - 'slug' => $this->get_admin_page_tab_slug(), |
|
| 164 | - ); |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - return $tabs; |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * Configure all the configuration parameters. |
|
| 172 | - * |
|
| 173 | - * Called by the *admin_init* hook. |
|
| 174 | - * |
|
| 175 | - * @since 3.21.0 |
|
| 176 | - */ |
|
| 177 | - public function admin_init() { |
|
| 178 | - |
|
| 179 | - // Register WordLift's analytics settings with our in class sanitizer. |
|
| 180 | - register_setting( |
|
| 181 | - 'wl_analytics_settings', |
|
| 182 | - 'wl_analytics_settings', |
|
| 183 | - array( $this, 'sanitize_callback' ) |
|
| 184 | - ); |
|
| 185 | - |
|
| 186 | - // Add the analytics settings setction. |
|
| 187 | - add_settings_section( |
|
| 188 | - 'wl_analytics_settings_section', |
|
| 189 | - '', |
|
| 190 | - '', |
|
| 191 | - 'wl_analytics_settings' |
|
| 192 | - ); |
|
| 193 | - |
|
| 194 | - // Add a toggle to determine if analytics functions are enabled or not. |
|
| 195 | - // NOTE: this uses yes/no rather than true/false. |
|
| 196 | - add_settings_field( |
|
| 197 | - 'wl-analytics-enabled', |
|
| 198 | - __( 'Enable Analytics', 'wordlift' ), |
|
| 199 | - array( $this->radio_input_element, 'render' ), |
|
| 200 | - 'wl_analytics_settings', |
|
| 201 | - 'wl_analytics_settings_section', |
|
| 202 | - array( |
|
| 203 | - 'id' => 'wl-analytics-enable', |
|
| 204 | - 'name' => 'wl_analytics_settings[' . Wordlift_Configuration_Service::ANALYTICS_ENABLE . ']', |
|
| 205 | - 'value' => $this->configuration_service->is_analytics_enable() ? 'yes' : 'no', |
|
| 206 | - 'description' => __( 'Toggle on/off the default values.', 'wordlift' ), |
|
| 207 | - ) |
|
| 208 | - ); |
|
| 209 | - |
|
| 210 | - /** |
|
| 211 | - * A basic number field that will accept anything from 1 to 20. |
|
| 212 | - * |
|
| 213 | - * Represents the custom dim number for the uri. |
|
| 214 | - */ |
|
| 215 | - add_settings_field( |
|
| 216 | - 'wl-analytics-entity-uri-dimension', |
|
| 217 | - __( 'Entity URI dimension', 'wordlift' ), |
|
| 218 | - array( $this->input_element, 'render' ), |
|
| 219 | - 'wl_analytics_settings', |
|
| 220 | - 'wl_analytics_settings_section', |
|
| 221 | - array( |
|
| 222 | - 'id' => 'wl-analytics-entity-uri-dimension', |
|
| 223 | - 'name' => 'wl_analytics_settings[' . Wordlift_Configuration_Service::ANALYTICS_ENTITY_URI_DIMENSION . ']', |
|
| 224 | - 'type' => 'number', |
|
| 225 | - 'value' => $this->configuration_service->get_analytics_entity_uri_dimension(), |
|
| 226 | - 'description' => __( 'Entity URI dimesion', 'wordlift' ), |
|
| 227 | - ) |
|
| 228 | - ); |
|
| 229 | - |
|
| 230 | - /** |
|
| 231 | - * A basic number field that will accept anything from 1 to 20. |
|
| 232 | - * |
|
| 233 | - * Represents the custom dim number for the type. |
|
| 234 | - */ |
|
| 235 | - add_settings_field( |
|
| 236 | - 'wl-analytics-entity-type-dimension', |
|
| 237 | - __( 'Entity Type dimension', 'wordlift' ), |
|
| 238 | - array( $this->input_element, 'render' ), |
|
| 239 | - 'wl_analytics_settings', |
|
| 240 | - 'wl_analytics_settings_section', |
|
| 241 | - array( |
|
| 242 | - 'id' => 'wl-analytics-entity-type-dimension', |
|
| 243 | - 'name' => 'wl_analytics_settings[' . Wordlift_Configuration_Service::ANALYTICS_ENTITY_TYPE_DIMENSION . ']', |
|
| 244 | - 'type' => 'number', |
|
| 245 | - 'value' => $this->configuration_service->get_analytics_entity_type_dimension(), |
|
| 246 | - 'description' => __( 'Entity Type dimension', 'wordlift' ), |
|
| 247 | - ) |
|
| 248 | - ); |
|
| 249 | - |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - /** |
|
| 253 | - * Validates an entity uri based on an integer passed. |
|
| 254 | - * |
|
| 255 | - * TODO: Needs a feedback method to pass back error messages. |
|
| 256 | - * |
|
| 257 | - * @method validate_entity_uri |
|
| 258 | - * @param string $uri a sting representing an entity ID that can be converted to a uri. |
|
| 259 | - * |
|
| 260 | - * @return int |
|
| 261 | - * @since 3.21.0 |
|
| 262 | - */ |
|
| 263 | - public function validate_entity_uri( $uri ) { |
|
| 264 | - // Basic validation is to ensure number is between 1 and 20. |
|
| 265 | - // NOTE: certain analytics accounts have a much higher value - as many |
|
| 266 | - // as 200 are allowed. |
|
| 267 | - if ( (int) $uri < 1 || (int) $uri > 20 ) { |
|
| 268 | - // if we are out of range then pass the default value. |
|
| 269 | - $uri = $this->configuration_service->get_analytics_entity_uri_dimension(); |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - return absint( $uri ); |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - /** |
|
| 276 | - * Validates an entity type. |
|
| 277 | - * |
|
| 278 | - * TODO: Needs a feedback method to pass back error messages. |
|
| 279 | - * |
|
| 280 | - * @method validate_entity_type |
|
| 281 | - * @param string $type This is an entity type ID in string form - really a number. |
|
| 282 | - * |
|
| 283 | - * @return int |
|
| 284 | - * @since 3.21.0 |
|
| 285 | - */ |
|
| 286 | - public function validate_entity_type( $type ) { |
|
| 287 | - // Basic validation is to ensure number is between 1 and 20. |
|
| 288 | - // NOTE: certain analytics accounts have a much higher value - as many |
|
| 289 | - // as 200 are allowed. |
|
| 290 | - if ( (int) $type < 1 || (int) $type > 20 ) { |
|
| 291 | - // if we are out of range then pass the default value. |
|
| 292 | - $type = $this->configuration_service->get_analytics_entity_type_dimension(); |
|
| 293 | - } |
|
| 294 | - |
|
| 295 | - return absint( $type ); |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - /** |
|
| 299 | - * Sanitize the configuration settings to be stored. |
|
| 300 | - * |
|
| 301 | - * If a new entity is being created for the publisher, create it and set The |
|
| 302 | - * publisher setting. |
|
| 303 | - * |
|
| 304 | - * @param array $input The configuration settings array. |
|
| 305 | - * |
|
| 306 | - * @return array The sanitized input array. |
|
| 307 | - * @since 3.21.0 |
|
| 308 | - * |
|
| 309 | - */ |
|
| 310 | - public function sanitize_callback( $input ) { |
|
| 311 | - if ( ! check_admin_referer( 'wl_analytics_settings-options' ) ) { |
|
| 312 | - // Any failing nonce checks already die(). |
|
| 313 | - return; |
|
| 314 | - } |
|
| 315 | - |
|
| 316 | - /** |
|
| 317 | - * Validate and sanitize the $inputs and store them in $output saved. |
|
| 318 | - */ |
|
| 319 | - $output = array(); |
|
| 320 | - if ( isset( $input['analytics_enable'] ) ) { |
|
| 321 | - $output['analytics_enable'] = ( 'yes' === $input['analytics_enable'] ) ? 'yes' : 'no'; |
|
| 322 | - } |
|
| 323 | - if ( isset( $input['analytics_entity_uri_dimension'] ) ) { |
|
| 324 | - $output['analytics_entity_uri_dimension'] = (int) $this->validate_entity_uri( $input['analytics_entity_uri_dimension'] ); |
|
| 325 | - } |
|
| 326 | - if ( isset( $input['analytics_entity_type_dimension'] ) ) { |
|
| 327 | - // This dimension cannot be the same as the one set above. If it is |
|
| 328 | - // then zero it out and it will fail validation. |
|
| 329 | - if ( isset( $output['analytics_entity_uri_dimension'] ) && $output['analytics_entity_uri_dimension'] === (int) $input['analytics_entity_type_dimension'] ) { |
|
| 330 | - $input['analytics_entity_type_dimension'] = 0; |
|
| 331 | - } |
|
| 332 | - $output['analytics_entity_type_dimension'] = (int) $this->validate_entity_type( $input['analytics_entity_type_dimension'] ); |
|
| 333 | - } |
|
| 334 | - |
|
| 335 | - // return items added to the output for saving. |
|
| 336 | - return $output; |
|
| 337 | - } |
|
| 21 | + /** |
|
| 22 | + * A singleton instance of the Notice service. |
|
| 23 | + * |
|
| 24 | + * @since 3.21.0 |
|
| 25 | + * @access private |
|
| 26 | + * @var \Wordlift_Admin_Settings_Analytics_Page $instance A singleton instance of a {@link Wordlift_Admin_Settings_Analytics_Page} class. |
|
| 27 | + */ |
|
| 28 | + private static $instance; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * A {@link Wordlift_Configuration_Service} instance. |
|
| 32 | + * |
|
| 33 | + * @since 3.21.0 |
|
| 34 | + * @access private |
|
| 35 | + * @var \Wordlift_Configuration_Service $configuration_service A {@link Wordlift_Configuration_Service} instance. |
|
| 36 | + */ |
|
| 37 | + private $configuration_service; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * A {@link Wordlift_Admin_Input_Element} element renderer. |
|
| 41 | + * |
|
| 42 | + * @since 3.21.0 |
|
| 43 | + * @access private |
|
| 44 | + * @var \Wordlift_Admin_Input_Element $input_element An {@link Wordlift_Admin_Input_Element} element renderer. |
|
| 45 | + */ |
|
| 46 | + private $input_element; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * A {@link Wordlift_Admin_Radio_Input_Element} element renderer. |
|
| 50 | + * |
|
| 51 | + * @since 3.21.0 |
|
| 52 | + * @access protected |
|
| 53 | + * @var \Wordlift_Admin_Radio_Input_Element $radio_input_element A {@link Wordlift_Admin_Radio_Input_Element} element renderer. |
|
| 54 | + */ |
|
| 55 | + private $radio_input_element; |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * Create a {@link Wordlift_Admin_Settings_Page} instance. |
|
| 59 | + * |
|
| 60 | + * @param \Wordlift_Configuration_Service $configuration_service The wordlift configuration service. |
|
| 61 | + * @param \Wordlift_Admin_Input_Element $input_element An input element class to output input boxes in a settings form. |
|
| 62 | + * @param \Wordlift_Admin_Radio_Input_Element $radio_input_element A radio element input class for use in a settings form. |
|
| 63 | + * |
|
| 64 | + * @since 3.21.0 |
|
| 65 | + * |
|
| 66 | + */ |
|
| 67 | + public function __construct( $configuration_service, $input_element, $radio_input_element ) { |
|
| 68 | + |
|
| 69 | + $this->configuration_service = $configuration_service; |
|
| 70 | + |
|
| 71 | + // Set a reference to the UI elements. |
|
| 72 | + $this->input_element = $input_element; |
|
| 73 | + $this->radio_input_element = $radio_input_element; |
|
| 74 | + |
|
| 75 | + // Adds this page to the main admin settings tabbed menu. |
|
| 76 | + add_filter( 'wl_admin_page_tabs', array( $this, 'add_admin_page_tab' ) ); |
|
| 77 | + |
|
| 78 | + self::$instance = $this; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * Get the singleton instance of the Notice service. |
|
| 83 | + * |
|
| 84 | + * @return \Wordlift_Admin_Settings_Page The singleton instance of the settings page service. |
|
| 85 | + * @since 3.21.0 |
|
| 86 | + */ |
|
| 87 | + public static function get_instance() { |
|
| 88 | + |
|
| 89 | + return self::$instance; |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * @inheritdoc |
|
| 94 | + */ |
|
| 95 | + protected function get_parent_slug() { |
|
| 96 | + |
|
| 97 | + return 'wl_admin_menu'; |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * @inheritdoc |
|
| 102 | + */ |
|
| 103 | + protected function get_capability() { |
|
| 104 | + |
|
| 105 | + return 'manage_options'; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * @inheritdoc |
|
| 110 | + */ |
|
| 111 | + public function get_page_title() { |
|
| 112 | + |
|
| 113 | + return __( 'WorldLift Analytics Settings', 'wordlift' ); |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * @inheritdoc |
|
| 118 | + */ |
|
| 119 | + public function get_menu_title() { |
|
| 120 | + |
|
| 121 | + return __( 'Analytics Settings', 'wordlift' ); |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * @inheritdoc |
|
| 126 | + */ |
|
| 127 | + public function get_menu_slug() { |
|
| 128 | + |
|
| 129 | + return 'wl_configuration_admin_analytics_menu'; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * @inheritdoc |
|
| 134 | + */ |
|
| 135 | + public function get_partial_name() { |
|
| 136 | + |
|
| 137 | + return 'wordlift-admin-settings-analytics-page.php'; |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * Returns the string to use for the tab slug on main admin settings page. |
|
| 142 | + * |
|
| 143 | + * @method get_admin_page_tab_slug |
|
| 144 | + * @return string |
|
| 145 | + * @since 3.21.0 |
|
| 146 | + */ |
|
| 147 | + public function get_admin_page_tab_slug() { |
|
| 148 | + return 'analytics'; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * Adds pages to the tabs menu on the main admin settings page. |
|
| 153 | + * |
|
| 154 | + * @method add_admin_page_tab |
|
| 155 | + * @param array $tabs an array of tab titles and slugs to use when building a tabbed menu on option pages. |
|
| 156 | + * |
|
| 157 | + * @since 3.21.0 |
|
| 158 | + */ |
|
| 159 | + public function add_admin_page_tab( $tabs ) { |
|
| 160 | + if ( apply_filters( 'wl_feature__enable__analytics', true ) ) { |
|
| 161 | + $tabs[] = array( |
|
| 162 | + 'title' => $this->get_menu_title(), |
|
| 163 | + 'slug' => $this->get_admin_page_tab_slug(), |
|
| 164 | + ); |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + return $tabs; |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * Configure all the configuration parameters. |
|
| 172 | + * |
|
| 173 | + * Called by the *admin_init* hook. |
|
| 174 | + * |
|
| 175 | + * @since 3.21.0 |
|
| 176 | + */ |
|
| 177 | + public function admin_init() { |
|
| 178 | + |
|
| 179 | + // Register WordLift's analytics settings with our in class sanitizer. |
|
| 180 | + register_setting( |
|
| 181 | + 'wl_analytics_settings', |
|
| 182 | + 'wl_analytics_settings', |
|
| 183 | + array( $this, 'sanitize_callback' ) |
|
| 184 | + ); |
|
| 185 | + |
|
| 186 | + // Add the analytics settings setction. |
|
| 187 | + add_settings_section( |
|
| 188 | + 'wl_analytics_settings_section', |
|
| 189 | + '', |
|
| 190 | + '', |
|
| 191 | + 'wl_analytics_settings' |
|
| 192 | + ); |
|
| 193 | + |
|
| 194 | + // Add a toggle to determine if analytics functions are enabled or not. |
|
| 195 | + // NOTE: this uses yes/no rather than true/false. |
|
| 196 | + add_settings_field( |
|
| 197 | + 'wl-analytics-enabled', |
|
| 198 | + __( 'Enable Analytics', 'wordlift' ), |
|
| 199 | + array( $this->radio_input_element, 'render' ), |
|
| 200 | + 'wl_analytics_settings', |
|
| 201 | + 'wl_analytics_settings_section', |
|
| 202 | + array( |
|
| 203 | + 'id' => 'wl-analytics-enable', |
|
| 204 | + 'name' => 'wl_analytics_settings[' . Wordlift_Configuration_Service::ANALYTICS_ENABLE . ']', |
|
| 205 | + 'value' => $this->configuration_service->is_analytics_enable() ? 'yes' : 'no', |
|
| 206 | + 'description' => __( 'Toggle on/off the default values.', 'wordlift' ), |
|
| 207 | + ) |
|
| 208 | + ); |
|
| 209 | + |
|
| 210 | + /** |
|
| 211 | + * A basic number field that will accept anything from 1 to 20. |
|
| 212 | + * |
|
| 213 | + * Represents the custom dim number for the uri. |
|
| 214 | + */ |
|
| 215 | + add_settings_field( |
|
| 216 | + 'wl-analytics-entity-uri-dimension', |
|
| 217 | + __( 'Entity URI dimension', 'wordlift' ), |
|
| 218 | + array( $this->input_element, 'render' ), |
|
| 219 | + 'wl_analytics_settings', |
|
| 220 | + 'wl_analytics_settings_section', |
|
| 221 | + array( |
|
| 222 | + 'id' => 'wl-analytics-entity-uri-dimension', |
|
| 223 | + 'name' => 'wl_analytics_settings[' . Wordlift_Configuration_Service::ANALYTICS_ENTITY_URI_DIMENSION . ']', |
|
| 224 | + 'type' => 'number', |
|
| 225 | + 'value' => $this->configuration_service->get_analytics_entity_uri_dimension(), |
|
| 226 | + 'description' => __( 'Entity URI dimesion', 'wordlift' ), |
|
| 227 | + ) |
|
| 228 | + ); |
|
| 229 | + |
|
| 230 | + /** |
|
| 231 | + * A basic number field that will accept anything from 1 to 20. |
|
| 232 | + * |
|
| 233 | + * Represents the custom dim number for the type. |
|
| 234 | + */ |
|
| 235 | + add_settings_field( |
|
| 236 | + 'wl-analytics-entity-type-dimension', |
|
| 237 | + __( 'Entity Type dimension', 'wordlift' ), |
|
| 238 | + array( $this->input_element, 'render' ), |
|
| 239 | + 'wl_analytics_settings', |
|
| 240 | + 'wl_analytics_settings_section', |
|
| 241 | + array( |
|
| 242 | + 'id' => 'wl-analytics-entity-type-dimension', |
|
| 243 | + 'name' => 'wl_analytics_settings[' . Wordlift_Configuration_Service::ANALYTICS_ENTITY_TYPE_DIMENSION . ']', |
|
| 244 | + 'type' => 'number', |
|
| 245 | + 'value' => $this->configuration_service->get_analytics_entity_type_dimension(), |
|
| 246 | + 'description' => __( 'Entity Type dimension', 'wordlift' ), |
|
| 247 | + ) |
|
| 248 | + ); |
|
| 249 | + |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + /** |
|
| 253 | + * Validates an entity uri based on an integer passed. |
|
| 254 | + * |
|
| 255 | + * TODO: Needs a feedback method to pass back error messages. |
|
| 256 | + * |
|
| 257 | + * @method validate_entity_uri |
|
| 258 | + * @param string $uri a sting representing an entity ID that can be converted to a uri. |
|
| 259 | + * |
|
| 260 | + * @return int |
|
| 261 | + * @since 3.21.0 |
|
| 262 | + */ |
|
| 263 | + public function validate_entity_uri( $uri ) { |
|
| 264 | + // Basic validation is to ensure number is between 1 and 20. |
|
| 265 | + // NOTE: certain analytics accounts have a much higher value - as many |
|
| 266 | + // as 200 are allowed. |
|
| 267 | + if ( (int) $uri < 1 || (int) $uri > 20 ) { |
|
| 268 | + // if we are out of range then pass the default value. |
|
| 269 | + $uri = $this->configuration_service->get_analytics_entity_uri_dimension(); |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + return absint( $uri ); |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + /** |
|
| 276 | + * Validates an entity type. |
|
| 277 | + * |
|
| 278 | + * TODO: Needs a feedback method to pass back error messages. |
|
| 279 | + * |
|
| 280 | + * @method validate_entity_type |
|
| 281 | + * @param string $type This is an entity type ID in string form - really a number. |
|
| 282 | + * |
|
| 283 | + * @return int |
|
| 284 | + * @since 3.21.0 |
|
| 285 | + */ |
|
| 286 | + public function validate_entity_type( $type ) { |
|
| 287 | + // Basic validation is to ensure number is between 1 and 20. |
|
| 288 | + // NOTE: certain analytics accounts have a much higher value - as many |
|
| 289 | + // as 200 are allowed. |
|
| 290 | + if ( (int) $type < 1 || (int) $type > 20 ) { |
|
| 291 | + // if we are out of range then pass the default value. |
|
| 292 | + $type = $this->configuration_service->get_analytics_entity_type_dimension(); |
|
| 293 | + } |
|
| 294 | + |
|
| 295 | + return absint( $type ); |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + /** |
|
| 299 | + * Sanitize the configuration settings to be stored. |
|
| 300 | + * |
|
| 301 | + * If a new entity is being created for the publisher, create it and set The |
|
| 302 | + * publisher setting. |
|
| 303 | + * |
|
| 304 | + * @param array $input The configuration settings array. |
|
| 305 | + * |
|
| 306 | + * @return array The sanitized input array. |
|
| 307 | + * @since 3.21.0 |
|
| 308 | + * |
|
| 309 | + */ |
|
| 310 | + public function sanitize_callback( $input ) { |
|
| 311 | + if ( ! check_admin_referer( 'wl_analytics_settings-options' ) ) { |
|
| 312 | + // Any failing nonce checks already die(). |
|
| 313 | + return; |
|
| 314 | + } |
|
| 315 | + |
|
| 316 | + /** |
|
| 317 | + * Validate and sanitize the $inputs and store them in $output saved. |
|
| 318 | + */ |
|
| 319 | + $output = array(); |
|
| 320 | + if ( isset( $input['analytics_enable'] ) ) { |
|
| 321 | + $output['analytics_enable'] = ( 'yes' === $input['analytics_enable'] ) ? 'yes' : 'no'; |
|
| 322 | + } |
|
| 323 | + if ( isset( $input['analytics_entity_uri_dimension'] ) ) { |
|
| 324 | + $output['analytics_entity_uri_dimension'] = (int) $this->validate_entity_uri( $input['analytics_entity_uri_dimension'] ); |
|
| 325 | + } |
|
| 326 | + if ( isset( $input['analytics_entity_type_dimension'] ) ) { |
|
| 327 | + // This dimension cannot be the same as the one set above. If it is |
|
| 328 | + // then zero it out and it will fail validation. |
|
| 329 | + if ( isset( $output['analytics_entity_uri_dimension'] ) && $output['analytics_entity_uri_dimension'] === (int) $input['analytics_entity_type_dimension'] ) { |
|
| 330 | + $input['analytics_entity_type_dimension'] = 0; |
|
| 331 | + } |
|
| 332 | + $output['analytics_entity_type_dimension'] = (int) $this->validate_entity_type( $input['analytics_entity_type_dimension'] ); |
|
| 333 | + } |
|
| 334 | + |
|
| 335 | + // return items added to the output for saving. |
|
| 336 | + return $output; |
|
| 337 | + } |
|
| 338 | 338 | |
| 339 | 339 | |
| 340 | 340 | } |
@@ -64,7 +64,7 @@ discard block |
||
| 64 | 64 | * @since 3.21.0 |
| 65 | 65 | * |
| 66 | 66 | */ |
| 67 | - public function __construct( $configuration_service, $input_element, $radio_input_element ) { |
|
| 67 | + public function __construct($configuration_service, $input_element, $radio_input_element) { |
|
| 68 | 68 | |
| 69 | 69 | $this->configuration_service = $configuration_service; |
| 70 | 70 | |
@@ -73,7 +73,7 @@ discard block |
||
| 73 | 73 | $this->radio_input_element = $radio_input_element; |
| 74 | 74 | |
| 75 | 75 | // Adds this page to the main admin settings tabbed menu. |
| 76 | - add_filter( 'wl_admin_page_tabs', array( $this, 'add_admin_page_tab' ) ); |
|
| 76 | + add_filter('wl_admin_page_tabs', array($this, 'add_admin_page_tab')); |
|
| 77 | 77 | |
| 78 | 78 | self::$instance = $this; |
| 79 | 79 | } |
@@ -110,7 +110,7 @@ discard block |
||
| 110 | 110 | */ |
| 111 | 111 | public function get_page_title() { |
| 112 | 112 | |
| 113 | - return __( 'WorldLift Analytics Settings', 'wordlift' ); |
|
| 113 | + return __('WorldLift Analytics Settings', 'wordlift'); |
|
| 114 | 114 | } |
| 115 | 115 | |
| 116 | 116 | /** |
@@ -118,7 +118,7 @@ discard block |
||
| 118 | 118 | */ |
| 119 | 119 | public function get_menu_title() { |
| 120 | 120 | |
| 121 | - return __( 'Analytics Settings', 'wordlift' ); |
|
| 121 | + return __('Analytics Settings', 'wordlift'); |
|
| 122 | 122 | } |
| 123 | 123 | |
| 124 | 124 | /** |
@@ -156,8 +156,8 @@ discard block |
||
| 156 | 156 | * |
| 157 | 157 | * @since 3.21.0 |
| 158 | 158 | */ |
| 159 | - public function add_admin_page_tab( $tabs ) { |
|
| 160 | - if ( apply_filters( 'wl_feature__enable__analytics', true ) ) { |
|
| 159 | + public function add_admin_page_tab($tabs) { |
|
| 160 | + if (apply_filters('wl_feature__enable__analytics', true)) { |
|
| 161 | 161 | $tabs[] = array( |
| 162 | 162 | 'title' => $this->get_menu_title(), |
| 163 | 163 | 'slug' => $this->get_admin_page_tab_slug(), |
@@ -180,7 +180,7 @@ discard block |
||
| 180 | 180 | register_setting( |
| 181 | 181 | 'wl_analytics_settings', |
| 182 | 182 | 'wl_analytics_settings', |
| 183 | - array( $this, 'sanitize_callback' ) |
|
| 183 | + array($this, 'sanitize_callback') |
|
| 184 | 184 | ); |
| 185 | 185 | |
| 186 | 186 | // Add the analytics settings setction. |
@@ -195,15 +195,15 @@ discard block |
||
| 195 | 195 | // NOTE: this uses yes/no rather than true/false. |
| 196 | 196 | add_settings_field( |
| 197 | 197 | 'wl-analytics-enabled', |
| 198 | - __( 'Enable Analytics', 'wordlift' ), |
|
| 199 | - array( $this->radio_input_element, 'render' ), |
|
| 198 | + __('Enable Analytics', 'wordlift'), |
|
| 199 | + array($this->radio_input_element, 'render'), |
|
| 200 | 200 | 'wl_analytics_settings', |
| 201 | 201 | 'wl_analytics_settings_section', |
| 202 | 202 | array( |
| 203 | 203 | 'id' => 'wl-analytics-enable', |
| 204 | - 'name' => 'wl_analytics_settings[' . Wordlift_Configuration_Service::ANALYTICS_ENABLE . ']', |
|
| 204 | + 'name' => 'wl_analytics_settings['.Wordlift_Configuration_Service::ANALYTICS_ENABLE.']', |
|
| 205 | 205 | 'value' => $this->configuration_service->is_analytics_enable() ? 'yes' : 'no', |
| 206 | - 'description' => __( 'Toggle on/off the default values.', 'wordlift' ), |
|
| 206 | + 'description' => __('Toggle on/off the default values.', 'wordlift'), |
|
| 207 | 207 | ) |
| 208 | 208 | ); |
| 209 | 209 | |
@@ -214,16 +214,16 @@ discard block |
||
| 214 | 214 | */ |
| 215 | 215 | add_settings_field( |
| 216 | 216 | 'wl-analytics-entity-uri-dimension', |
| 217 | - __( 'Entity URI dimension', 'wordlift' ), |
|
| 218 | - array( $this->input_element, 'render' ), |
|
| 217 | + __('Entity URI dimension', 'wordlift'), |
|
| 218 | + array($this->input_element, 'render'), |
|
| 219 | 219 | 'wl_analytics_settings', |
| 220 | 220 | 'wl_analytics_settings_section', |
| 221 | 221 | array( |
| 222 | 222 | 'id' => 'wl-analytics-entity-uri-dimension', |
| 223 | - 'name' => 'wl_analytics_settings[' . Wordlift_Configuration_Service::ANALYTICS_ENTITY_URI_DIMENSION . ']', |
|
| 223 | + 'name' => 'wl_analytics_settings['.Wordlift_Configuration_Service::ANALYTICS_ENTITY_URI_DIMENSION.']', |
|
| 224 | 224 | 'type' => 'number', |
| 225 | 225 | 'value' => $this->configuration_service->get_analytics_entity_uri_dimension(), |
| 226 | - 'description' => __( 'Entity URI dimesion', 'wordlift' ), |
|
| 226 | + 'description' => __('Entity URI dimesion', 'wordlift'), |
|
| 227 | 227 | ) |
| 228 | 228 | ); |
| 229 | 229 | |
@@ -234,16 +234,16 @@ discard block |
||
| 234 | 234 | */ |
| 235 | 235 | add_settings_field( |
| 236 | 236 | 'wl-analytics-entity-type-dimension', |
| 237 | - __( 'Entity Type dimension', 'wordlift' ), |
|
| 238 | - array( $this->input_element, 'render' ), |
|
| 237 | + __('Entity Type dimension', 'wordlift'), |
|
| 238 | + array($this->input_element, 'render'), |
|
| 239 | 239 | 'wl_analytics_settings', |
| 240 | 240 | 'wl_analytics_settings_section', |
| 241 | 241 | array( |
| 242 | 242 | 'id' => 'wl-analytics-entity-type-dimension', |
| 243 | - 'name' => 'wl_analytics_settings[' . Wordlift_Configuration_Service::ANALYTICS_ENTITY_TYPE_DIMENSION . ']', |
|
| 243 | + 'name' => 'wl_analytics_settings['.Wordlift_Configuration_Service::ANALYTICS_ENTITY_TYPE_DIMENSION.']', |
|
| 244 | 244 | 'type' => 'number', |
| 245 | 245 | 'value' => $this->configuration_service->get_analytics_entity_type_dimension(), |
| 246 | - 'description' => __( 'Entity Type dimension', 'wordlift' ), |
|
| 246 | + 'description' => __('Entity Type dimension', 'wordlift'), |
|
| 247 | 247 | ) |
| 248 | 248 | ); |
| 249 | 249 | |
@@ -260,16 +260,16 @@ discard block |
||
| 260 | 260 | * @return int |
| 261 | 261 | * @since 3.21.0 |
| 262 | 262 | */ |
| 263 | - public function validate_entity_uri( $uri ) { |
|
| 263 | + public function validate_entity_uri($uri) { |
|
| 264 | 264 | // Basic validation is to ensure number is between 1 and 20. |
| 265 | 265 | // NOTE: certain analytics accounts have a much higher value - as many |
| 266 | 266 | // as 200 are allowed. |
| 267 | - if ( (int) $uri < 1 || (int) $uri > 20 ) { |
|
| 267 | + if ((int) $uri < 1 || (int) $uri > 20) { |
|
| 268 | 268 | // if we are out of range then pass the default value. |
| 269 | 269 | $uri = $this->configuration_service->get_analytics_entity_uri_dimension(); |
| 270 | 270 | } |
| 271 | 271 | |
| 272 | - return absint( $uri ); |
|
| 272 | + return absint($uri); |
|
| 273 | 273 | } |
| 274 | 274 | |
| 275 | 275 | /** |
@@ -283,16 +283,16 @@ discard block |
||
| 283 | 283 | * @return int |
| 284 | 284 | * @since 3.21.0 |
| 285 | 285 | */ |
| 286 | - public function validate_entity_type( $type ) { |
|
| 286 | + public function validate_entity_type($type) { |
|
| 287 | 287 | // Basic validation is to ensure number is between 1 and 20. |
| 288 | 288 | // NOTE: certain analytics accounts have a much higher value - as many |
| 289 | 289 | // as 200 are allowed. |
| 290 | - if ( (int) $type < 1 || (int) $type > 20 ) { |
|
| 290 | + if ((int) $type < 1 || (int) $type > 20) { |
|
| 291 | 291 | // if we are out of range then pass the default value. |
| 292 | 292 | $type = $this->configuration_service->get_analytics_entity_type_dimension(); |
| 293 | 293 | } |
| 294 | 294 | |
| 295 | - return absint( $type ); |
|
| 295 | + return absint($type); |
|
| 296 | 296 | } |
| 297 | 297 | |
| 298 | 298 | /** |
@@ -307,8 +307,8 @@ discard block |
||
| 307 | 307 | * @since 3.21.0 |
| 308 | 308 | * |
| 309 | 309 | */ |
| 310 | - public function sanitize_callback( $input ) { |
|
| 311 | - if ( ! check_admin_referer( 'wl_analytics_settings-options' ) ) { |
|
| 310 | + public function sanitize_callback($input) { |
|
| 311 | + if ( ! check_admin_referer('wl_analytics_settings-options')) { |
|
| 312 | 312 | // Any failing nonce checks already die(). |
| 313 | 313 | return; |
| 314 | 314 | } |
@@ -317,19 +317,19 @@ discard block |
||
| 317 | 317 | * Validate and sanitize the $inputs and store them in $output saved. |
| 318 | 318 | */ |
| 319 | 319 | $output = array(); |
| 320 | - if ( isset( $input['analytics_enable'] ) ) { |
|
| 321 | - $output['analytics_enable'] = ( 'yes' === $input['analytics_enable'] ) ? 'yes' : 'no'; |
|
| 320 | + if (isset($input['analytics_enable'])) { |
|
| 321 | + $output['analytics_enable'] = ('yes' === $input['analytics_enable']) ? 'yes' : 'no'; |
|
| 322 | 322 | } |
| 323 | - if ( isset( $input['analytics_entity_uri_dimension'] ) ) { |
|
| 324 | - $output['analytics_entity_uri_dimension'] = (int) $this->validate_entity_uri( $input['analytics_entity_uri_dimension'] ); |
|
| 323 | + if (isset($input['analytics_entity_uri_dimension'])) { |
|
| 324 | + $output['analytics_entity_uri_dimension'] = (int) $this->validate_entity_uri($input['analytics_entity_uri_dimension']); |
|
| 325 | 325 | } |
| 326 | - if ( isset( $input['analytics_entity_type_dimension'] ) ) { |
|
| 326 | + if (isset($input['analytics_entity_type_dimension'])) { |
|
| 327 | 327 | // This dimension cannot be the same as the one set above. If it is |
| 328 | 328 | // then zero it out and it will fail validation. |
| 329 | - if ( isset( $output['analytics_entity_uri_dimension'] ) && $output['analytics_entity_uri_dimension'] === (int) $input['analytics_entity_type_dimension'] ) { |
|
| 329 | + if (isset($output['analytics_entity_uri_dimension']) && $output['analytics_entity_uri_dimension'] === (int) $input['analytics_entity_type_dimension']) { |
|
| 330 | 330 | $input['analytics_entity_type_dimension'] = 0; |
| 331 | 331 | } |
| 332 | - $output['analytics_entity_type_dimension'] = (int) $this->validate_entity_type( $input['analytics_entity_type_dimension'] ); |
|
| 332 | + $output['analytics_entity_type_dimension'] = (int) $this->validate_entity_type($input['analytics_entity_type_dimension']); |
|
| 333 | 333 | } |
| 334 | 334 | |
| 335 | 335 | // return items added to the output for saving. |
@@ -18,202 +18,202 @@ |
||
| 18 | 18 | |
| 19 | 19 | class Post_Excerpt_Rest_Controller { |
| 20 | 20 | |
| 21 | - const POST_EXCERPT_NAMESPACE = 'post-excerpt'; |
|
| 22 | - /** |
|
| 23 | - * Key for storing the meta data for the wordlift post excerpt. |
|
| 24 | - */ |
|
| 25 | - const POST_EXCERPT_META_KEY = '_wl_post_excerpt_meta'; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * Url for getting the post excerpt data from wordlift api. |
|
| 29 | - */ |
|
| 30 | - const WORDLIFT_POST_EXCERPT_ENDPOINT = '/summarize'; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * Wordlift returns excerpt in response using this key.. |
|
| 34 | - */ |
|
| 35 | - const WORDLIFT_POST_EXCERPT_RESPONSE_KEY = 'summary'; |
|
| 36 | - |
|
| 37 | - public static function register_routes() { |
|
| 38 | - add_action( 'rest_api_init', 'Wordlift\Post_Excerpt\Post_Excerpt_Rest_Controller::register_route_callback' ); |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * Determines whether we need to get the excerpt from wordlift api, |
|
| 43 | - * or just use the one we already obtained by generating the hash and comparing it |
|
| 44 | - * with the previous one. |
|
| 45 | - * |
|
| 46 | - * @param $request WP_REST_Request $request {@link WP_REST_Request instance}. |
|
| 47 | - * |
|
| 48 | - * @return array Post excerpt data. |
|
| 49 | - */ |
|
| 50 | - public static function get_post_excerpt( $request ) { |
|
| 51 | - $data = $request->get_params(); |
|
| 52 | - $post_id = $data['post_id']; |
|
| 53 | - $post_body = strip_shortcodes( $data['post_body'] ); |
|
| 54 | - $current_hash = md5( $post_body ); |
|
| 55 | - $server_response = self::get_post_excerpt_conditionally( $post_id, $post_body, $current_hash ); |
|
| 56 | - if ( empty( $server_response ) || ! array_key_exists( 'post_excerpt', $server_response ) ) { |
|
| 57 | - return array( |
|
| 58 | - 'status' => 'error', |
|
| 59 | - 'message' => __( 'Unable to contact WordLift API', 'wordlift' ) |
|
| 60 | - ); |
|
| 61 | - } else { |
|
| 62 | - return array( |
|
| 63 | - 'status' => 'success', |
|
| 64 | - 'post_excerpt' => $server_response['post_excerpt'], |
|
| 65 | - 'from_cache' => $server_response['from_cache'], |
|
| 66 | - 'message' => __( 'Excerpt successfully generated.', 'wordlift' ) |
|
| 67 | - ); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * This function determines whether to get the excerpt from the server or from the meta cache. |
|
| 74 | - * |
|
| 75 | - * @param $post_id int The Post id. |
|
| 76 | - * @param $post_body string The post content |
|
| 77 | - * @param $current_hash string md5 hash of the current post body. |
|
| 78 | - * |
|
| 79 | - * @return array|bool|null |
|
| 80 | - */ |
|
| 81 | - public static function get_post_excerpt_conditionally( $post_id, $post_body, $current_hash ) { |
|
| 82 | - $previous_data = get_post_meta( $post_id, self::POST_EXCERPT_META_KEY, true ); |
|
| 83 | - $server_response = null; |
|
| 84 | - if ( $previous_data === "" ) { |
|
| 85 | - // There is no data in meta, so just fetch the data from remote server. |
|
| 86 | - $server_response = self::get_post_excerpt_from_remote_server( $post_id, $post_body ); |
|
| 87 | - } else { |
|
| 88 | - // If there is data in meta, get the previous hash and compare. |
|
| 89 | - $previous_hash = $previous_data['post_body_hash']; |
|
| 90 | - |
|
| 91 | - if ( $current_hash === $previous_hash ) { |
|
| 92 | - // then return the previous value. |
|
| 93 | - $server_response = array( |
|
| 94 | - 'post_excerpt' => $previous_data['post_excerpt'], |
|
| 95 | - 'from_cache' => true |
|
| 96 | - ); |
|
| 97 | - } else { |
|
| 98 | - // send the request to external API and then send the response. |
|
| 99 | - $server_response = self::get_post_excerpt_from_remote_server( $post_id, $post_body ); |
|
| 100 | - } |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - return $server_response; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * Sends the remote request to the wordlift API and saves the response in meta for |
|
| 108 | - * future use. |
|
| 109 | - * |
|
| 110 | - * @param $post_id int Post id which the post excerpt belongs to |
|
| 111 | - * @param $post_body string Total text content of the post body. |
|
| 112 | - * |
|
| 113 | - * @return array|bool |
|
| 114 | - */ |
|
| 115 | - public static function get_post_excerpt_from_remote_server( $post_id, $post_body ) { |
|
| 116 | - // The configuration is constant for now, it might be changing in future. |
|
| 117 | - $configuration = array( |
|
| 118 | - 'ratio' => 0.0005, |
|
| 119 | - 'min_length' => 60 |
|
| 120 | - ); |
|
| 121 | - // Construct the url with the configuration |
|
| 122 | - $endpoint = add_query_arg( $configuration, self::WORDLIFT_POST_EXCERPT_ENDPOINT ); |
|
| 123 | - $api_service = Default_Api_Service::get_instance(); |
|
| 124 | - $response = $api_service->request( |
|
| 125 | - 'POST', |
|
| 126 | - $endpoint, |
|
| 127 | - array( 'Content-Type' => 'text/plain' ), |
|
| 128 | - $post_body, |
|
| 129 | - null, |
|
| 130 | - null, |
|
| 131 | - array( 'data_format' => 'body' ) |
|
| 132 | - ); |
|
| 133 | - |
|
| 134 | - return self::save_response_to_meta_on_success( $post_id, $post_body, $response ); |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * Save the post excerpt to meta if the response is successful. |
|
| 139 | - * |
|
| 140 | - * @param $post_id int The post id |
|
| 141 | - * @param $post_body string Full text content of the post. |
|
| 142 | - * @param $response Response instance |
|
| 143 | - * |
|
| 144 | - * @return array|bool |
|
| 145 | - */ |
|
| 146 | - public static function save_response_to_meta_on_success( $post_id, $post_body, $response ) { |
|
| 147 | - // If body exists then decode the body. |
|
| 148 | - $body = json_decode( $response->get_body(), true ); |
|
| 149 | - if ( empty( $body ) || ! array_key_exists( self::WORDLIFT_POST_EXCERPT_RESPONSE_KEY, $body ) ) { |
|
| 150 | - // Bail out if we get an incorrect response |
|
| 151 | - return false; |
|
| 152 | - } else { |
|
| 153 | - $post_excerpt = (string) $body[ self::WORDLIFT_POST_EXCERPT_RESPONSE_KEY ]; |
|
| 154 | - // Save it to meta. |
|
| 155 | - self::save_post_excerpt_in_meta( $post_id, $post_excerpt, $post_body ); |
|
| 156 | - |
|
| 157 | - return array( |
|
| 158 | - 'post_excerpt' => $post_excerpt, |
|
| 159 | - 'from_cache' => false |
|
| 160 | - ); |
|
| 161 | - } |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * Saves the excerpt in the post meta. |
|
| 166 | - * |
|
| 167 | - * @param $post_id int Post id which the post excerpt belongs to |
|
| 168 | - * @param $post_excerpt string Post excerpt returned by the server |
|
| 169 | - * @param $post_body string Total text content of the post body. |
|
| 170 | - * |
|
| 171 | - * @return void |
|
| 172 | - */ |
|
| 173 | - public static function save_post_excerpt_in_meta( $post_id, $post_excerpt, $post_body ) { |
|
| 174 | - // hash the post body and save it. |
|
| 175 | - $data = array( |
|
| 176 | - 'post_body_hash' => md5( $post_body ), |
|
| 177 | - 'post_excerpt' => $post_excerpt |
|
| 178 | - ); |
|
| 179 | - update_post_meta( $post_id, self::POST_EXCERPT_META_KEY, $data ); |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - /** |
|
| 183 | - * This call back is invoked by the Rest api action. |
|
| 184 | - */ |
|
| 185 | - public static function register_route_callback() { |
|
| 186 | - /** @var $post_id_validation_settings array Settings used to validate post id */ |
|
| 187 | - $post_id_validation_settings = array( |
|
| 188 | - 'required' => true, |
|
| 189 | - 'validate_callback' => function ( $param, $request, $key ) { |
|
| 190 | - return is_numeric( $param ); |
|
| 191 | - } |
|
| 192 | - ); |
|
| 193 | - $post_body_validation_settings = array( |
|
| 194 | - 'required' => true, |
|
| 195 | - 'validate_callback' => function ( $param, $request, $key ) { |
|
| 196 | - return is_string( $param ); |
|
| 197 | - } |
|
| 198 | - ); |
|
| 199 | - /** |
|
| 200 | - * Rest route for getting the excerpt from wordlift api. |
|
| 201 | - */ |
|
| 202 | - register_rest_route( |
|
| 203 | - WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
| 204 | - '/' . self::POST_EXCERPT_NAMESPACE . '/(?P<post_id>\d+)', |
|
| 205 | - array( |
|
| 206 | - 'methods' => \WP_REST_Server::CREATABLE, |
|
| 207 | - 'callback' => 'Wordlift\Post_Excerpt\Post_Excerpt_Rest_Controller::get_post_excerpt', |
|
| 208 | - 'permission_callback' => function () { |
|
| 209 | - return current_user_can( 'publish_posts' ); |
|
| 210 | - }, |
|
| 211 | - 'args' => array( |
|
| 212 | - 'post_id' => $post_id_validation_settings, |
|
| 213 | - 'post_body' => $post_body_validation_settings |
|
| 214 | - ) |
|
| 215 | - ) |
|
| 216 | - ); |
|
| 217 | - } |
|
| 21 | + const POST_EXCERPT_NAMESPACE = 'post-excerpt'; |
|
| 22 | + /** |
|
| 23 | + * Key for storing the meta data for the wordlift post excerpt. |
|
| 24 | + */ |
|
| 25 | + const POST_EXCERPT_META_KEY = '_wl_post_excerpt_meta'; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * Url for getting the post excerpt data from wordlift api. |
|
| 29 | + */ |
|
| 30 | + const WORDLIFT_POST_EXCERPT_ENDPOINT = '/summarize'; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * Wordlift returns excerpt in response using this key.. |
|
| 34 | + */ |
|
| 35 | + const WORDLIFT_POST_EXCERPT_RESPONSE_KEY = 'summary'; |
|
| 36 | + |
|
| 37 | + public static function register_routes() { |
|
| 38 | + add_action( 'rest_api_init', 'Wordlift\Post_Excerpt\Post_Excerpt_Rest_Controller::register_route_callback' ); |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * Determines whether we need to get the excerpt from wordlift api, |
|
| 43 | + * or just use the one we already obtained by generating the hash and comparing it |
|
| 44 | + * with the previous one. |
|
| 45 | + * |
|
| 46 | + * @param $request WP_REST_Request $request {@link WP_REST_Request instance}. |
|
| 47 | + * |
|
| 48 | + * @return array Post excerpt data. |
|
| 49 | + */ |
|
| 50 | + public static function get_post_excerpt( $request ) { |
|
| 51 | + $data = $request->get_params(); |
|
| 52 | + $post_id = $data['post_id']; |
|
| 53 | + $post_body = strip_shortcodes( $data['post_body'] ); |
|
| 54 | + $current_hash = md5( $post_body ); |
|
| 55 | + $server_response = self::get_post_excerpt_conditionally( $post_id, $post_body, $current_hash ); |
|
| 56 | + if ( empty( $server_response ) || ! array_key_exists( 'post_excerpt', $server_response ) ) { |
|
| 57 | + return array( |
|
| 58 | + 'status' => 'error', |
|
| 59 | + 'message' => __( 'Unable to contact WordLift API', 'wordlift' ) |
|
| 60 | + ); |
|
| 61 | + } else { |
|
| 62 | + return array( |
|
| 63 | + 'status' => 'success', |
|
| 64 | + 'post_excerpt' => $server_response['post_excerpt'], |
|
| 65 | + 'from_cache' => $server_response['from_cache'], |
|
| 66 | + 'message' => __( 'Excerpt successfully generated.', 'wordlift' ) |
|
| 67 | + ); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * This function determines whether to get the excerpt from the server or from the meta cache. |
|
| 74 | + * |
|
| 75 | + * @param $post_id int The Post id. |
|
| 76 | + * @param $post_body string The post content |
|
| 77 | + * @param $current_hash string md5 hash of the current post body. |
|
| 78 | + * |
|
| 79 | + * @return array|bool|null |
|
| 80 | + */ |
|
| 81 | + public static function get_post_excerpt_conditionally( $post_id, $post_body, $current_hash ) { |
|
| 82 | + $previous_data = get_post_meta( $post_id, self::POST_EXCERPT_META_KEY, true ); |
|
| 83 | + $server_response = null; |
|
| 84 | + if ( $previous_data === "" ) { |
|
| 85 | + // There is no data in meta, so just fetch the data from remote server. |
|
| 86 | + $server_response = self::get_post_excerpt_from_remote_server( $post_id, $post_body ); |
|
| 87 | + } else { |
|
| 88 | + // If there is data in meta, get the previous hash and compare. |
|
| 89 | + $previous_hash = $previous_data['post_body_hash']; |
|
| 90 | + |
|
| 91 | + if ( $current_hash === $previous_hash ) { |
|
| 92 | + // then return the previous value. |
|
| 93 | + $server_response = array( |
|
| 94 | + 'post_excerpt' => $previous_data['post_excerpt'], |
|
| 95 | + 'from_cache' => true |
|
| 96 | + ); |
|
| 97 | + } else { |
|
| 98 | + // send the request to external API and then send the response. |
|
| 99 | + $server_response = self::get_post_excerpt_from_remote_server( $post_id, $post_body ); |
|
| 100 | + } |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + return $server_response; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * Sends the remote request to the wordlift API and saves the response in meta for |
|
| 108 | + * future use. |
|
| 109 | + * |
|
| 110 | + * @param $post_id int Post id which the post excerpt belongs to |
|
| 111 | + * @param $post_body string Total text content of the post body. |
|
| 112 | + * |
|
| 113 | + * @return array|bool |
|
| 114 | + */ |
|
| 115 | + public static function get_post_excerpt_from_remote_server( $post_id, $post_body ) { |
|
| 116 | + // The configuration is constant for now, it might be changing in future. |
|
| 117 | + $configuration = array( |
|
| 118 | + 'ratio' => 0.0005, |
|
| 119 | + 'min_length' => 60 |
|
| 120 | + ); |
|
| 121 | + // Construct the url with the configuration |
|
| 122 | + $endpoint = add_query_arg( $configuration, self::WORDLIFT_POST_EXCERPT_ENDPOINT ); |
|
| 123 | + $api_service = Default_Api_Service::get_instance(); |
|
| 124 | + $response = $api_service->request( |
|
| 125 | + 'POST', |
|
| 126 | + $endpoint, |
|
| 127 | + array( 'Content-Type' => 'text/plain' ), |
|
| 128 | + $post_body, |
|
| 129 | + null, |
|
| 130 | + null, |
|
| 131 | + array( 'data_format' => 'body' ) |
|
| 132 | + ); |
|
| 133 | + |
|
| 134 | + return self::save_response_to_meta_on_success( $post_id, $post_body, $response ); |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * Save the post excerpt to meta if the response is successful. |
|
| 139 | + * |
|
| 140 | + * @param $post_id int The post id |
|
| 141 | + * @param $post_body string Full text content of the post. |
|
| 142 | + * @param $response Response instance |
|
| 143 | + * |
|
| 144 | + * @return array|bool |
|
| 145 | + */ |
|
| 146 | + public static function save_response_to_meta_on_success( $post_id, $post_body, $response ) { |
|
| 147 | + // If body exists then decode the body. |
|
| 148 | + $body = json_decode( $response->get_body(), true ); |
|
| 149 | + if ( empty( $body ) || ! array_key_exists( self::WORDLIFT_POST_EXCERPT_RESPONSE_KEY, $body ) ) { |
|
| 150 | + // Bail out if we get an incorrect response |
|
| 151 | + return false; |
|
| 152 | + } else { |
|
| 153 | + $post_excerpt = (string) $body[ self::WORDLIFT_POST_EXCERPT_RESPONSE_KEY ]; |
|
| 154 | + // Save it to meta. |
|
| 155 | + self::save_post_excerpt_in_meta( $post_id, $post_excerpt, $post_body ); |
|
| 156 | + |
|
| 157 | + return array( |
|
| 158 | + 'post_excerpt' => $post_excerpt, |
|
| 159 | + 'from_cache' => false |
|
| 160 | + ); |
|
| 161 | + } |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * Saves the excerpt in the post meta. |
|
| 166 | + * |
|
| 167 | + * @param $post_id int Post id which the post excerpt belongs to |
|
| 168 | + * @param $post_excerpt string Post excerpt returned by the server |
|
| 169 | + * @param $post_body string Total text content of the post body. |
|
| 170 | + * |
|
| 171 | + * @return void |
|
| 172 | + */ |
|
| 173 | + public static function save_post_excerpt_in_meta( $post_id, $post_excerpt, $post_body ) { |
|
| 174 | + // hash the post body and save it. |
|
| 175 | + $data = array( |
|
| 176 | + 'post_body_hash' => md5( $post_body ), |
|
| 177 | + 'post_excerpt' => $post_excerpt |
|
| 178 | + ); |
|
| 179 | + update_post_meta( $post_id, self::POST_EXCERPT_META_KEY, $data ); |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + /** |
|
| 183 | + * This call back is invoked by the Rest api action. |
|
| 184 | + */ |
|
| 185 | + public static function register_route_callback() { |
|
| 186 | + /** @var $post_id_validation_settings array Settings used to validate post id */ |
|
| 187 | + $post_id_validation_settings = array( |
|
| 188 | + 'required' => true, |
|
| 189 | + 'validate_callback' => function ( $param, $request, $key ) { |
|
| 190 | + return is_numeric( $param ); |
|
| 191 | + } |
|
| 192 | + ); |
|
| 193 | + $post_body_validation_settings = array( |
|
| 194 | + 'required' => true, |
|
| 195 | + 'validate_callback' => function ( $param, $request, $key ) { |
|
| 196 | + return is_string( $param ); |
|
| 197 | + } |
|
| 198 | + ); |
|
| 199 | + /** |
|
| 200 | + * Rest route for getting the excerpt from wordlift api. |
|
| 201 | + */ |
|
| 202 | + register_rest_route( |
|
| 203 | + WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
| 204 | + '/' . self::POST_EXCERPT_NAMESPACE . '/(?P<post_id>\d+)', |
|
| 205 | + array( |
|
| 206 | + 'methods' => \WP_REST_Server::CREATABLE, |
|
| 207 | + 'callback' => 'Wordlift\Post_Excerpt\Post_Excerpt_Rest_Controller::get_post_excerpt', |
|
| 208 | + 'permission_callback' => function () { |
|
| 209 | + return current_user_can( 'publish_posts' ); |
|
| 210 | + }, |
|
| 211 | + 'args' => array( |
|
| 212 | + 'post_id' => $post_id_validation_settings, |
|
| 213 | + 'post_body' => $post_body_validation_settings |
|
| 214 | + ) |
|
| 215 | + ) |
|
| 216 | + ); |
|
| 217 | + } |
|
| 218 | 218 | |
| 219 | 219 | } |
@@ -35,7 +35,7 @@ discard block |
||
| 35 | 35 | const WORDLIFT_POST_EXCERPT_RESPONSE_KEY = 'summary'; |
| 36 | 36 | |
| 37 | 37 | public static function register_routes() { |
| 38 | - add_action( 'rest_api_init', 'Wordlift\Post_Excerpt\Post_Excerpt_Rest_Controller::register_route_callback' ); |
|
| 38 | + add_action('rest_api_init', 'Wordlift\Post_Excerpt\Post_Excerpt_Rest_Controller::register_route_callback'); |
|
| 39 | 39 | } |
| 40 | 40 | |
| 41 | 41 | /** |
@@ -47,23 +47,23 @@ discard block |
||
| 47 | 47 | * |
| 48 | 48 | * @return array Post excerpt data. |
| 49 | 49 | */ |
| 50 | - public static function get_post_excerpt( $request ) { |
|
| 50 | + public static function get_post_excerpt($request) { |
|
| 51 | 51 | $data = $request->get_params(); |
| 52 | 52 | $post_id = $data['post_id']; |
| 53 | - $post_body = strip_shortcodes( $data['post_body'] ); |
|
| 54 | - $current_hash = md5( $post_body ); |
|
| 55 | - $server_response = self::get_post_excerpt_conditionally( $post_id, $post_body, $current_hash ); |
|
| 56 | - if ( empty( $server_response ) || ! array_key_exists( 'post_excerpt', $server_response ) ) { |
|
| 53 | + $post_body = strip_shortcodes($data['post_body']); |
|
| 54 | + $current_hash = md5($post_body); |
|
| 55 | + $server_response = self::get_post_excerpt_conditionally($post_id, $post_body, $current_hash); |
|
| 56 | + if (empty($server_response) || ! array_key_exists('post_excerpt', $server_response)) { |
|
| 57 | 57 | return array( |
| 58 | 58 | 'status' => 'error', |
| 59 | - 'message' => __( 'Unable to contact WordLift API', 'wordlift' ) |
|
| 59 | + 'message' => __('Unable to contact WordLift API', 'wordlift') |
|
| 60 | 60 | ); |
| 61 | 61 | } else { |
| 62 | 62 | return array( |
| 63 | 63 | 'status' => 'success', |
| 64 | 64 | 'post_excerpt' => $server_response['post_excerpt'], |
| 65 | 65 | 'from_cache' => $server_response['from_cache'], |
| 66 | - 'message' => __( 'Excerpt successfully generated.', 'wordlift' ) |
|
| 66 | + 'message' => __('Excerpt successfully generated.', 'wordlift') |
|
| 67 | 67 | ); |
| 68 | 68 | } |
| 69 | 69 | |
@@ -78,17 +78,17 @@ discard block |
||
| 78 | 78 | * |
| 79 | 79 | * @return array|bool|null |
| 80 | 80 | */ |
| 81 | - public static function get_post_excerpt_conditionally( $post_id, $post_body, $current_hash ) { |
|
| 82 | - $previous_data = get_post_meta( $post_id, self::POST_EXCERPT_META_KEY, true ); |
|
| 81 | + public static function get_post_excerpt_conditionally($post_id, $post_body, $current_hash) { |
|
| 82 | + $previous_data = get_post_meta($post_id, self::POST_EXCERPT_META_KEY, true); |
|
| 83 | 83 | $server_response = null; |
| 84 | - if ( $previous_data === "" ) { |
|
| 84 | + if ($previous_data === "") { |
|
| 85 | 85 | // There is no data in meta, so just fetch the data from remote server. |
| 86 | - $server_response = self::get_post_excerpt_from_remote_server( $post_id, $post_body ); |
|
| 86 | + $server_response = self::get_post_excerpt_from_remote_server($post_id, $post_body); |
|
| 87 | 87 | } else { |
| 88 | 88 | // If there is data in meta, get the previous hash and compare. |
| 89 | 89 | $previous_hash = $previous_data['post_body_hash']; |
| 90 | 90 | |
| 91 | - if ( $current_hash === $previous_hash ) { |
|
| 91 | + if ($current_hash === $previous_hash) { |
|
| 92 | 92 | // then return the previous value. |
| 93 | 93 | $server_response = array( |
| 94 | 94 | 'post_excerpt' => $previous_data['post_excerpt'], |
@@ -96,7 +96,7 @@ discard block |
||
| 96 | 96 | ); |
| 97 | 97 | } else { |
| 98 | 98 | // send the request to external API and then send the response. |
| 99 | - $server_response = self::get_post_excerpt_from_remote_server( $post_id, $post_body ); |
|
| 99 | + $server_response = self::get_post_excerpt_from_remote_server($post_id, $post_body); |
|
| 100 | 100 | } |
| 101 | 101 | } |
| 102 | 102 | |
@@ -112,26 +112,26 @@ discard block |
||
| 112 | 112 | * |
| 113 | 113 | * @return array|bool |
| 114 | 114 | */ |
| 115 | - public static function get_post_excerpt_from_remote_server( $post_id, $post_body ) { |
|
| 115 | + public static function get_post_excerpt_from_remote_server($post_id, $post_body) { |
|
| 116 | 116 | // The configuration is constant for now, it might be changing in future. |
| 117 | 117 | $configuration = array( |
| 118 | 118 | 'ratio' => 0.0005, |
| 119 | 119 | 'min_length' => 60 |
| 120 | 120 | ); |
| 121 | 121 | // Construct the url with the configuration |
| 122 | - $endpoint = add_query_arg( $configuration, self::WORDLIFT_POST_EXCERPT_ENDPOINT ); |
|
| 122 | + $endpoint = add_query_arg($configuration, self::WORDLIFT_POST_EXCERPT_ENDPOINT); |
|
| 123 | 123 | $api_service = Default_Api_Service::get_instance(); |
| 124 | 124 | $response = $api_service->request( |
| 125 | 125 | 'POST', |
| 126 | 126 | $endpoint, |
| 127 | - array( 'Content-Type' => 'text/plain' ), |
|
| 127 | + array('Content-Type' => 'text/plain'), |
|
| 128 | 128 | $post_body, |
| 129 | 129 | null, |
| 130 | 130 | null, |
| 131 | - array( 'data_format' => 'body' ) |
|
| 131 | + array('data_format' => 'body') |
|
| 132 | 132 | ); |
| 133 | 133 | |
| 134 | - return self::save_response_to_meta_on_success( $post_id, $post_body, $response ); |
|
| 134 | + return self::save_response_to_meta_on_success($post_id, $post_body, $response); |
|
| 135 | 135 | } |
| 136 | 136 | |
| 137 | 137 | /** |
@@ -143,16 +143,16 @@ discard block |
||
| 143 | 143 | * |
| 144 | 144 | * @return array|bool |
| 145 | 145 | */ |
| 146 | - public static function save_response_to_meta_on_success( $post_id, $post_body, $response ) { |
|
| 146 | + public static function save_response_to_meta_on_success($post_id, $post_body, $response) { |
|
| 147 | 147 | // If body exists then decode the body. |
| 148 | - $body = json_decode( $response->get_body(), true ); |
|
| 149 | - if ( empty( $body ) || ! array_key_exists( self::WORDLIFT_POST_EXCERPT_RESPONSE_KEY, $body ) ) { |
|
| 148 | + $body = json_decode($response->get_body(), true); |
|
| 149 | + if (empty($body) || ! array_key_exists(self::WORDLIFT_POST_EXCERPT_RESPONSE_KEY, $body)) { |
|
| 150 | 150 | // Bail out if we get an incorrect response |
| 151 | 151 | return false; |
| 152 | 152 | } else { |
| 153 | - $post_excerpt = (string) $body[ self::WORDLIFT_POST_EXCERPT_RESPONSE_KEY ]; |
|
| 153 | + $post_excerpt = (string) $body[self::WORDLIFT_POST_EXCERPT_RESPONSE_KEY]; |
|
| 154 | 154 | // Save it to meta. |
| 155 | - self::save_post_excerpt_in_meta( $post_id, $post_excerpt, $post_body ); |
|
| 155 | + self::save_post_excerpt_in_meta($post_id, $post_excerpt, $post_body); |
|
| 156 | 156 | |
| 157 | 157 | return array( |
| 158 | 158 | 'post_excerpt' => $post_excerpt, |
@@ -170,13 +170,13 @@ discard block |
||
| 170 | 170 | * |
| 171 | 171 | * @return void |
| 172 | 172 | */ |
| 173 | - public static function save_post_excerpt_in_meta( $post_id, $post_excerpt, $post_body ) { |
|
| 173 | + public static function save_post_excerpt_in_meta($post_id, $post_excerpt, $post_body) { |
|
| 174 | 174 | // hash the post body and save it. |
| 175 | 175 | $data = array( |
| 176 | - 'post_body_hash' => md5( $post_body ), |
|
| 176 | + 'post_body_hash' => md5($post_body), |
|
| 177 | 177 | 'post_excerpt' => $post_excerpt |
| 178 | 178 | ); |
| 179 | - update_post_meta( $post_id, self::POST_EXCERPT_META_KEY, $data ); |
|
| 179 | + update_post_meta($post_id, self::POST_EXCERPT_META_KEY, $data); |
|
| 180 | 180 | } |
| 181 | 181 | |
| 182 | 182 | /** |
@@ -184,16 +184,16 @@ discard block |
||
| 184 | 184 | */ |
| 185 | 185 | public static function register_route_callback() { |
| 186 | 186 | /** @var $post_id_validation_settings array Settings used to validate post id */ |
| 187 | - $post_id_validation_settings = array( |
|
| 187 | + $post_id_validation_settings = array( |
|
| 188 | 188 | 'required' => true, |
| 189 | - 'validate_callback' => function ( $param, $request, $key ) { |
|
| 190 | - return is_numeric( $param ); |
|
| 189 | + 'validate_callback' => function($param, $request, $key) { |
|
| 190 | + return is_numeric($param); |
|
| 191 | 191 | } |
| 192 | 192 | ); |
| 193 | 193 | $post_body_validation_settings = array( |
| 194 | 194 | 'required' => true, |
| 195 | - 'validate_callback' => function ( $param, $request, $key ) { |
|
| 196 | - return is_string( $param ); |
|
| 195 | + 'validate_callback' => function($param, $request, $key) { |
|
| 196 | + return is_string($param); |
|
| 197 | 197 | } |
| 198 | 198 | ); |
| 199 | 199 | /** |
@@ -201,12 +201,12 @@ discard block |
||
| 201 | 201 | */ |
| 202 | 202 | register_rest_route( |
| 203 | 203 | WL_REST_ROUTE_DEFAULT_NAMESPACE, |
| 204 | - '/' . self::POST_EXCERPT_NAMESPACE . '/(?P<post_id>\d+)', |
|
| 204 | + '/'.self::POST_EXCERPT_NAMESPACE.'/(?P<post_id>\d+)', |
|
| 205 | 205 | array( |
| 206 | 206 | 'methods' => \WP_REST_Server::CREATABLE, |
| 207 | 207 | 'callback' => 'Wordlift\Post_Excerpt\Post_Excerpt_Rest_Controller::get_post_excerpt', |
| 208 | - 'permission_callback' => function () { |
|
| 209 | - return current_user_can( 'publish_posts' ); |
|
| 208 | + 'permission_callback' => function() { |
|
| 209 | + return current_user_can('publish_posts'); |
|
| 210 | 210 | }, |
| 211 | 211 | 'args' => array( |
| 212 | 212 | 'post_id' => $post_id_validation_settings, |