should_add_notification() ) {
$this->add_notification();
}
if ( filter_input( INPUT_GET, 'page' ) !== self::PAGE_IDENTIFIER ) {
return;
}
// Register the page for the wizard.
add_action( 'admin_menu', array( $this, 'add_wizard_page' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
add_action( 'admin_init', array( $this, 'render_wizard_page' ) );
}
/**
* Check if the configuration is finished. If so, just remove the notification.
*/
public function catch_configuration_request() {
$configuration_page = filter_input( INPUT_GET, 'configuration' );
$page = filter_input( INPUT_GET, 'page' );
if ( ! ( $configuration_page === 'finished' && ( $page === WPSEO_Admin::PAGE_IDENTIFIER ) ) ) {
return;
}
$this->remove_notification();
$this->remove_notification_option();
wp_redirect( admin_url( 'admin.php?page=' . WPSEO_Admin::PAGE_IDENTIFIER ) );
exit;
}
/**
* Registers the page for the wizard.
*/
public function add_wizard_page() {
add_dashboard_page( '', '', 'manage_options', self::PAGE_IDENTIFIER, '' );
}
/**
* Renders the wizard page and exits to prevent the wordpress UI from loading.
*/
public function render_wizard_page() {
$this->show_wizard();
exit;
}
/**
* Enqueues the assets needed for the wizard.
*/
public function enqueue_assets() {
wp_enqueue_media();
/*
* Print the `forms.css` WP stylesheet before any Yoast style, this way
* it's easier to override selectors with the same specificity later.
*/
wp_enqueue_style( 'forms' );
$assetManager = new WPSEO_Admin_Asset_Manager();
$assetManager->register_assets();
$assetManager->enqueue_script( 'configuration-wizard' );
$assetManager->enqueue_style( 'yoast-components' );
$config = $this->get_config();
wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'configuration-wizard', 'yoastWizardConfig', $config );
}
/**
* Setup Wizard Header.
*/
public function show_wizard() {
$this->enqueue_assets();
$dashboard_url = admin_url( '/admin.php?page=wpseo_dashboard' );
?>
>
get_translations();
$service = new WPSEO_GSC_Service();
$config = array(
'namespace' => WPSEO_Configuration_Endpoint::REST_NAMESPACE,
'endpoint_retrieve' => WPSEO_Configuration_Endpoint::ENDPOINT_RETRIEVE,
'endpoint_store' => WPSEO_Configuration_Endpoint::ENDPOINT_STORE,
'nonce' => wp_create_nonce( 'wp_rest' ),
'root' => esc_url_raw( rest_url() ),
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'finishUrl' => admin_url( 'admin.php?page=wpseo_dashboard&configuration=finished' ),
'gscAuthURL' => $service->get_client()->createAuthUrl(),
'gscProfiles' => $service->get_sites(),
'gscNonce' => wp_create_nonce( 'wpseo-gsc-ajax-security' ),
'translations' => $translations,
);
return $config;
}
/**
* Returns the translations necessary for the configuration wizard.
*
* @returns array The translations for the configuration wizard.
*/
public function get_translations() {
$file = plugin_dir_path( WPSEO_FILE ) . 'languages/yoast-components-' . get_locale() . '.json';
if ( file_exists( $file ) && $file = file_get_contents( $file ) ) {
return json_decode( $file, true );
}
return array();
}
/**
* Adds a notification to the notification center.
*/
private function add_notification() {
$notification_center = Yoast_Notification_Center::get();
$notification_center->add_notification( self::get_notification() );
}
/**
* Removes the notification from the notification center.
*/
private function remove_notification() {
$notification_center = Yoast_Notification_Center::get();
$notification_center->remove_notification( self::get_notification() );
}
/**
* Gets the notification.
*
* @return Yoast_Notification
*/
private static function get_notification() {
$message = sprintf(
__( 'Since you are new to %1$s you can configure the %2$splugin%3$s', 'wordpress-seo' ),
'Yoast SEO',
'',
''
);
$notification = new Yoast_Notification(
$message,
array(
'type' => Yoast_Notification::WARNING,
'id' => 'wpseo-dismiss-onboarding-notice',
'capabilities' => 'manage_options',
'priority' => 0.8,
)
);
return $notification;
}
/**
* When the notice should be shown.
*
* @return bool
*/
private function should_add_notification() {
$options = $this->get_options();
return $options['show_onboarding_notice'] === true;
}
/**
* Remove the options that triggers the notice for the configuration wizard.
*/
private function remove_notification_option() {
$options = $this->get_options();
$options['show_onboarding_notice'] = false;
update_option( 'wpseo', $options );
}
/**
* Returns the set options
*
* @return mixed|void
*/
private function get_options() {
return get_option( 'wpseo' );
}
}