' . PHP_EOL . PHP_EOL;
}
/**
* Registers the hook to catch option change.
*
* @codeCoverageIgnore Reason: because it calls a WordPress function.
*
* @return void
*/
public function register_hooks() {
add_action( 'update_option_wpseo', array( $this, 'update_option' ), 10, 2 );
}
/**
* Compares the logic between old and new option value and send the request.
*
* @param mixed $old_value The old option value.
* @param mixed $new_value The new option value.
*
* @return void
*/
public function update_option( $old_value, $new_value ) {
$old_option_value = false;
if ( isset( $old_value[ $this->option_name ] ) ) {
$old_option_value = $old_value[ $this->option_name ];
}
$new_option_value = false;
if ( isset( $new_value[ $this->option_name ] ) ) {
$new_option_value = $new_value[ $this->option_name ];
}
if ( $old_option_value === $new_option_value ) {
return;
}
if ( $new_option_value === true ) {
$this->subscribe_newsletter();
}
}
/**
* Checks if the recalibration beta has been enabled.
*
* @codeCoverageIgnore Reason: It calls a dependency.
*
* @return bool True whether the beta has been enabled.
*/
public static function is_enabled() {
return WPSEO_Options::get( 'recalibration_beta' );
}
/**
* Checks if the user has a mailinglist subscription.
*
* @codeCoverageIgnore Reason: because it calls a WordPress function.
*
* The mailinglist subscription value will set to true when the beta is set
* to enabled. This value stays true, so it's a good indicator that the user
* tried the beta.
*
* @return bool True whether the user has a subscription.
*/
public function has_mailinglist_subscription() {
return (bool) get_option( 'wpseo_recalibration_beta_mailinglist_subscription', false );
}
/**
* Retrieves the option value based on the current setting.
*
* @param bool $is_enabled Is the option enabled.
*
* @return string On when is enabled, off when not.
*/
protected function get_option_value( $is_enabled ) {
if ( $is_enabled === true ) {
return 'on';
}
return 'off';
}
/**
* Subscribes to the newsletter.
*
* @return void
*/
protected function subscribe_newsletter() {
if ( $this->has_mailinglist_subscription() ) {
return;
}
try {
$this->do_request(
'https://my.yoast.com/api/customers/newsletter/recalibration/subscribe',
array(
'email' => get_option( 'admin_email' ),
'firstName' => get_option( 'blogname' ),
'lastName' => '',
)
);
$this->set_mailinglist_subscription();
}
catch ( Requests_Exception_HTTP $e ) {
// Intentionally left blank. @todo We should offer this to a logger.
return;
}
}
/**
* Performs a request to the given url.
*
* @codeCoverageIgnore Reason: because it contains WordPress functions.
*
* @param string $url The request url.
* @param array $body The request body.
*
* @return void
*
* @throws Requests_Exception_HTTP When request has failed.
*/
protected function do_request( $url, $body ) {
$response = wp_remote_post(
$url,
array(
'body' => $body,
)
);
if ( is_wp_error( $response ) ) {
throw new Requests_Exception_HTTP( $response->get_error_message() );
}
}
/**
* Sets the mailing list subscription value to true.
*
* @codeCoverageIgnore Reason: because it calls a WordPress function.
*
* @return void
*/
protected function set_mailinglist_subscription() {
update_option( 'wpseo_recalibration_beta_mailinglist_subscription', true );
}
}