class-field-environment.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\ConfigurationUI
  6. */
  7. /**
  8. * Class WPSEO_Config_Field_Environment
  9. */
  10. class WPSEO_Config_Field_Environment extends WPSEO_Config_Field_Choice {
  11. /**
  12. * WPSEO_Config_Field_Environment constructor.
  13. */
  14. public function __construct() {
  15. parent::__construct( 'environment_type' );
  16. $this->set_property( 'label', __( 'Please specify if your site is under construction or already active.', 'wordpress-seo' ) );
  17. $this->set_property( 'description', __( 'Choose under construction if you want to keep the site out of the index
  18. of search engines. Don\'t forget to activate it once you\'re ready to
  19. publish your site.', 'wordpress-seo' ) );
  20. $this->add_choice( 'production', __( 'Option A: My site is live and ready to be indexed', 'wordpress-seo' ) );
  21. $this->add_choice( 'staging', __( 'Option B: My site is under construction and should not be indexed', 'wordpress-seo' ) );
  22. }
  23. /**
  24. * Set adapter
  25. *
  26. * @param WPSEO_Configuration_Options_Adapter $adapter Adapter to register lookup on.
  27. */
  28. public function set_adapter( WPSEO_Configuration_Options_Adapter $adapter ) {
  29. $adapter->add_custom_lookup(
  30. $this->get_identifier(),
  31. array( $this, 'get_data' ),
  32. array( $this, 'set_data' )
  33. );
  34. }
  35. /**
  36. * Gets the option that is set for this field.
  37. *
  38. * @return string The value for the environment_type wpseo option.
  39. */
  40. public function get_data() {
  41. return WPSEO_Options::get( 'environment_type' );
  42. }
  43. /**
  44. * Set new data.
  45. *
  46. * @param string $environment_type The site's environment type.
  47. *
  48. * @return bool Returns whether the value is successfully set.
  49. */
  50. public function set_data( $environment_type ) {
  51. $return = true;
  52. if ( $this->get_data() !== $environment_type ) {
  53. $return = WPSEO_Options::set( 'environment_type', $environment_type );
  54. if ( ! $this->set_indexation( $environment_type ) ) {
  55. return false;
  56. }
  57. }
  58. return $return;
  59. }
  60. /**
  61. * Set the WordPress Search Engine Visibility option based on the environment type.
  62. *
  63. * @param string $environment_type The environment the site is running in.
  64. *
  65. * @return bool Returns if the options is set successfully.
  66. */
  67. protected function set_indexation( $environment_type ) {
  68. $new_blog_public_value = 0;
  69. $current_blog_public_value = get_option( 'blog_public' );
  70. if ( $environment_type === 'production' ) {
  71. $new_blog_public_value = 1;
  72. }
  73. if ( $current_blog_public_value !== $new_blog_public_value ) {
  74. update_option( 'blog_public', $new_blog_public_value );
  75. return true;
  76. }
  77. return ( get_option( 'blog_public' ) === $new_blog_public_value );
  78. }
  79. }