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