WPSEO_Config_Field_Environment

Class WPSEO_Config_Field_Environment.

Defined (1)

The class is defined in the following location(s).

/admin/config-ui/fields/class-field-environment.php  
  1. class WPSEO_Config_Field_Environment extends WPSEO_Config_Field_Choice { 
  2. /** 
  3. * WPSEO_Config_Field_Environment constructor. 
  4. */ 
  5. public function __construct() { 
  6. parent::__construct( 'environment_type' ); 
  7.  
  8. /** Translators: %1$s resolves to the home_url of the blog. */ 
  9. $this->set_property( 'label', sprintf( __( 'Please specify the environment in which this site - %1$s - is running.', 'wordpress-seo' ), get_home_url() ) ); 
  10.  
  11. $this->add_choice( 'production', __( 'Production (this is a live site with real traffic)', 'wordpress-seo' ) ); 
  12. $this->add_choice( 'staging', __( 'Staging (this is a copy of a live site used for testing purposes only)', 'wordpress-seo' ) ); 
  13. $this->add_choice( 'development', __( 'Development (this site is running locally for development purposes)', 'wordpress-seo' ) ); 
  14.  
  15. /** 
  16. * Set adapter 
  17. * @param WPSEO_Configuration_Options_Adapter $adapter Adapter to register lookup on. 
  18. */ 
  19. public function set_adapter( WPSEO_Configuration_Options_Adapter $adapter ) { 
  20. $adapter->add_custom_lookup( 
  21. $this->get_identifier(),  
  22. array( $this, 'get_data' ),  
  23. array( $this, 'set_data' ) 
  24. ); 
  25.  
  26. /** 
  27. * Gets the option that is set for this field. 
  28. * @return string The value for the environment_type wpseo option. 
  29. */ 
  30. public function get_data() { 
  31. $option = WPSEO_Options::get_option( 'wpseo' ); 
  32.  
  33. return $option['environment_type']; 
  34.  
  35. /** 
  36. * Set new data. 
  37. * @param string $environment_type The site's environment type. 
  38. * @return bool Returns whether the value is successfully set. 
  39. */ 
  40. public function set_data( $environment_type ) { 
  41. $option = WPSEO_Options::get_option( 'wpseo' ); 
  42.  
  43. if ( $option['environment_type'] !== $environment_type ) { 
  44. $option['environment_type'] = $environment_type; 
  45. update_option( 'wpseo', $option ); 
  46. if ( ! $this->set_indexation( $environment_type ) ) { 
  47. return false; 
  48.  
  49. $saved_environment_option = WPSEO_Options::get_option( 'wpseo' ); 
  50.  
  51. return ( $saved_environment_option['environment_type'] === $option['environment_type'] ); 
  52.  
  53. /** 
  54. * Set the WordPress Search Engine Visibility option based on the environment type. 
  55. * @param string $environment_type The environment the site is running in. 
  56. * @return bool Returns if the options is set successfully. 
  57. */ 
  58. protected function set_indexation( $environment_type ) { 
  59. $new_blog_public_value = 0; 
  60. $current_blog_public_value = get_option( 'blog_public' ); 
  61.  
  62. if ( $environment_type === 'production' ) { 
  63. $new_blog_public_value = 1; 
  64.  
  65. if ( $current_blog_public_value !== $new_blog_public_value ) { 
  66. update_option( 'blog_public', $new_blog_public_value ); 
  67.  
  68. return true; 
  69. $saved_blog_public_value = get_option( 'blog_public' ); 
  70.  
  71. return ( $saved_blog_public_value === $new_blog_public_value );