2021-09-21 15:01:08 +02:00
< ? php
/**
* WPSEO plugin file .
*
* @ package WPSEO\Admin\ConfigurationUI
*/
/**
2021-09-21 15:05:30 +02:00
* Class WPSEO_Config_Field_Environment .
2021-09-21 15:01:08 +02:00
*/
class WPSEO_Config_Field_Environment extends WPSEO_Config_Field_Choice {
2021-09-21 15:05:30 +02:00
2021-09-21 15:01:08 +02:00
/**
* WPSEO_Config_Field_Environment constructor .
*/
public function __construct () {
parent :: __construct ( 'environment_type' );
$this -> set_property ( 'label' , __ ( 'Please specify if your site is under construction or already active.' , 'wordpress-seo' ) );
2021-09-21 15:05:30 +02:00
$this -> set_property ( 'description' , __ ( 'Choose under construction if you want to keep the site out of the index of search engines. Don\'t forget to activate it once you\'re ready to publish your site.' , 'wordpress-seo' ) );
2021-09-21 15:01:08 +02:00
$this -> add_choice ( 'production' , __ ( 'Option A: My site is live and ready to be indexed' , 'wordpress-seo' ) );
$this -> add_choice ( 'staging' , __ ( 'Option B: My site is under construction and should not be indexed' , 'wordpress-seo' ) );
}
/**
2021-09-21 15:05:30 +02:00
* Set adapter .
2021-09-21 15:01:08 +02:00
*
* @ param WPSEO_Configuration_Options_Adapter $adapter Adapter to register lookup on .
*/
public function set_adapter ( WPSEO_Configuration_Options_Adapter $adapter ) {
$adapter -> add_custom_lookup (
$this -> get_identifier (),
2021-09-21 15:05:30 +02:00
[ $this , 'get_data' ],
[ $this , 'set_data' ]
2021-09-21 15:01:08 +02:00
);
}
/**
* Gets the option that is set for this field .
*
* @ return string The value for the environment_type wpseo option .
*/
public function get_data () {
return WPSEO_Options :: get ( 'environment_type' );
}
/**
* Set new data .
*
* @ param string $environment_type The site ' s environment type .
*
* @ return bool Returns whether the value is successfully set .
*/
public function set_data ( $environment_type ) {
$return = true ;
if ( $this -> get_data () !== $environment_type ) {
$return = WPSEO_Options :: set ( 'environment_type' , $environment_type );
if ( ! $this -> set_indexation ( $environment_type ) ) {
return false ;
}
}
return $return ;
}
/**
* Set the WordPress Search Engine Visibility option based on the environment type .
*
* @ param string $environment_type The environment the site is running in .
*
* @ return bool Returns if the options is set successfully .
*/
protected function set_indexation ( $environment_type ) {
$new_blog_public_value = 0 ;
$current_blog_public_value = get_option ( 'blog_public' );
if ( $environment_type === 'production' ) {
$new_blog_public_value = 1 ;
}
if ( $current_blog_public_value !== $new_blog_public_value ) {
update_option ( 'blog_public' , $new_blog_public_value );
return true ;
}
return ( get_option ( 'blog_public' ) === $new_blog_public_value );
}
}