class-field-environment.php 2.84 KB
Newer Older
imac's avatar
imac committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
<?php
/**
 * @package WPSEO\Admin\ConfigurationUI
 */

/**
 * Class WPSEO_Config_Field_Environment
 */
class WPSEO_Config_Field_Environment extends WPSEO_Config_Field_Choice {
	/**
	 * WPSEO_Config_Field_Environment constructor.
	 */
	public function __construct() {
		parent::__construct( 'environment_type' );

		/* Translators: %1$s resolves to the home_url of the blog. */
		$this->set_property( 'label', sprintf( __( 'Please specify the environment in which this site - %1$s - is running.', 'wordpress-seo' ), get_home_url() ) );

		$this->add_choice( 'production', __( 'Production (this is a live site with real traffic)', 'wordpress-seo' ) );
		$this->add_choice( 'staging', __( 'Staging (this is a copy of a live site used for testing purposes only)', 'wordpress-seo' ) );
		$this->add_choice( 'development', __( 'Development (this site is running locally for development purposes)', 'wordpress-seo' ) );
	}

	/**
	 * Set adapter
	 *
	 * @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(),
			array( $this, 'get_data' ),
			array( $this, 'set_data' )
		);
	}

	/**
	 * Gets the option that is set for this field.
	 *
	 * @return string The value for the environment_type wpseo option.
	 */
	public function get_data() {
		$option = WPSEO_Options::get_option( 'wpseo' );

		return $option['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 ) {
		$option = WPSEO_Options::get_option( 'wpseo' );

		if ( $option['environment_type'] !== $environment_type ) {
			$option['environment_type'] = $environment_type;
			update_option( 'wpseo', $option );
			if ( ! $this->set_indexation( $environment_type ) ) {
				return false;
			}
		}

		$saved_environment_option = WPSEO_Options::get_option( 'wpseo' );

		return ( $saved_environment_option['environment_type'] === $option['environment_type'] );
	}

	/**
	 * 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;
		}
		$saved_blog_public_value = get_option( 'blog_public' );

		return ( $saved_blog_public_value === $new_blog_public_value );
	}
}