Skip to content

Commit

Permalink
Enable 'Yoast AI' by default
Browse files Browse the repository at this point in the history
  • Loading branch information
wpalani committed Sep 11, 2023
1 parent 8de988c commit e242ed1
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ function () {
require BLUEHOST_PLUGIN_DIR . '/inc/RestApi/rest-api.php';
require BLUEHOST_PLUGIN_DIR . '/inc/settings.php';
require BLUEHOST_PLUGIN_DIR . '/inc/updates.php';
require BLUEHOST_PLUGIN_DIR . '/inc/YoastAI.php';

/* WordPress Admin Page & Features */
if ( is_admin() ) {
Expand Down
65 changes: 65 additions & 0 deletions inc/YoastAI.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Bluehost;

/**
* Class YoastAI
*
* @package Bluehost
*/
class YoastAI
{
/*
* Initialize the Yoast AI class.
*/
public static function init()
{
add_action( 'init', array( __CLASS__, 'on_yoast_plugin_activation' ) );
add_filter( 'http_request_args', array( __CLASS__,'modify_http_request_args' ), 10, 2 );
}

/*
* Register Yoast plugin activation hook to enable the Yoast AI generator on new activations.
*/
public static function on_yoast_plugin_activation()
{
register_activation_hook( 'wordpress-seo/wp-seo.php', array( __CLASS__, 'enable_on_new_activations' ) );
}

/*
* Filter Yoast SEO default settings to enable AI on new installations/activations
*/
public static function enable_on_new_activations()
{
add_filter(
'wpseo_option_wpseo_defaults',
function ( $defaults ) {
if ( ! is_array( $defaults ) || ! isset( $defaults['enable_ai_generator'] ) ) {
return $defaults;
}
$defaults['enable_ai_generator'] = true;
return $defaults;
}
);
}

/*
* Add a custom header to outbound Yoast AI requests for Bluehost customers.
*/
public static function modify_http_request_args( $parsed_args, $url )
{
$parsed_url = parse_url( $url );
if ( ! is_array( $parsed_url ) ) {
return $parsed_args;
}
if ( $parsed_url['host'] !== 'ai.yoa.st' ) {
return $parsed_args;
}
if ( is_array( $parsed_args['headers'] ) ) {
$parsed_args['headers']['X-Yst-Cohort'] = 'Bluehost';
}
return $parsed_args;
}
}

YoastAI::init();
14 changes: 14 additions & 0 deletions inc/upgrades/3.1.1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
/**
* Handle updates for version 3.1.1
*
* @package Bluehost
*/

// Enable Yoast AI on existing Yoast SEO installs
if ( function_exists( 'yoastseo' ) ) {
// Only enable the enable_ai_generator setting if we have a stored value.
if ( yoastseo()->helpers->options->get( 'enable_ai_generator', null ) !== null ) {
yoastseo()->helpers->options->set( 'enable_ai_generator', true );
}
}

0 comments on commit e242ed1

Please sign in to comment.