class-field-environment.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 of search engines. Don\'t forget to activate it once you\'re ready to publish your site.', 'wordpress-seo' ) );
  18. $this->add_choice( 'production', __( 'Option A: My site is live and ready to be indexed', 'wordpress-seo' ) );
  19. $this->add_choice( 'staging', __( 'Option B: My site is under construction and should not be indexed', 'wordpress-seo' ) );
  20. }
  21. /**
  22. * Set adapter.
  23. *
  24. * @param WPSEO_Configuration_Options_Adapter $adapter Adapter to register lookup on.
  25. */
  26. public function set_adapter( WPSEO_Configuration_Options_Adapter $adapter ) {
  27. $adapter->add_custom_lookup(
  28. $this->get_identifier(),
  29. [ $this, 'get_data' ],
  30. [ $this, 'set_data' ]
  31. );
  32. }
  33. /**
  34. * Gets the option that is set for this field.
  35. *
  36. * @return string The value for the environment_type wpseo option.
  37. */
  38. public function get_data() {
  39. return WPSEO_Options::get( 'environment_type' );
  40. }
  41. /**
  42. * Set new data.
  43. *
  44. * @param string $environment_type The site's environment type.
  45. *
  46. * @return bool Returns whether the value is successfully set.
  47. */
  48. public function set_data( $environment_type ) {
  49. $return = true;
  50. if ( $this->get_data() !== $environment_type ) {
  51. $return = WPSEO_Options::set( 'environment_type', $environment_type );
  52. if ( ! $this->set_indexation( $environment_type ) ) {
  53. return false;
  54. }
  55. }
  56. return $return;
  57. }
  58. /**
  59. * Set the WordPress Search Engine Visibility option based on the environment type.
  60. *
  61. * @param string $environment_type The environment the site is running in.
  62. *
  63. * @return bool Returns if the options is set successfully.
  64. */
  65. protected function set_indexation( $environment_type ) {
  66. $new_blog_public_value = 0;
  67. $current_blog_public_value = get_option( 'blog_public' );
  68. if ( $environment_type === 'production' ) {
  69. $new_blog_public_value = 1;
  70. }
  71. if ( $current_blog_public_value !== $new_blog_public_value ) {
  72. update_option( 'blog_public', $new_blog_public_value );
  73. return true;
  74. }
  75. return ( get_option( 'blog_public' ) === $new_blog_public_value );
  76. }
  77. }