Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a home task for launching an Atomic site #7462

Draft
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 70 additions & 51 deletions includes/admin/home/tasks/class-sensei-home-tasks-provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,18 @@ class Sensei_Home_Tasks_Provider {
const COMPLETED_TASKS_OPTION_KEY = 'sensei_home_tasks_list_is_completed';

/**
* Tells if the WP hooks were attached or not.
* Tells if the tasks are initialized.
*
* @var bool
*/
private static $attached_hooks = false;
private static $initialized = false;

/**
* The task instances.
*
* @var Sensei_Home_Task[]
*/
private $tasks = [];

/**
* The option name that wp-calypso automatically fetches to use
Expand All @@ -36,7 +43,63 @@ class Sensei_Home_Tasks_Provider {
* Class constructor.
*/
public function __construct() {
$this->attach_tasks_statuses_hooks();
$this->init();
}

/**
* Attaches required hooks.
*/
private function init() {
// Run the initialization only once.
if ( self::$initialized ) {
return;
}

self::$initialized = true;

$this->init_tasks();

add_action( 'save_post_course', [ $this, 'log_course_completion_tasks' ], 10, 3 );

// Attach some hooks only for Atomic sites.
if ( ! Sensei_Utils::is_atomic_platform() ) {
return;
}

// Attach the update_tasks_statuses method to filters and actions
// that can affect the status of the Sensei Home tasks.
add_action( 'save_post_course', [ $this, 'update_tasks_statuses' ] );
add_action( 'wp_ajax_sensei_settings_section_visited', [ $this, 'update_tasks_statuses' ] );
}

/**
* Initializes the tasks.
*/
private function init_tasks() {
$this->tasks = [
new Sensei_Home_Task_Setup_Site(),
new Sensei_Home_Task_Create_First_Course(),
new Sensei_Home_Task_Configure_Learning_Mode(),
new Sensei_Home_Task_Publish_First_Course(),
];

if ( Sensei_Home_Task_Sell_Course_With_WooCommerce::is_active() ) {
$this->tasks[] = new Sensei_Home_Task_Sell_Course_With_WooCommerce();
}

if ( Sensei_Home_Task_Customize_Course_Theme::is_active() ) {
$this->tasks[] = new Sensei_Home_Task_Customize_Course_Theme();
}

if ( Sensei_Home_Task_Launch_Site::is_active() ) {
$this->tasks[] = new Sensei_Home_Task_Launch_Site();
}

foreach ( $this->tasks as $task ) {
if ( method_exists( $task, 'init' ) ) {
$task->init();
}
}
}

/**
Expand All @@ -59,30 +122,9 @@ public function get(): array {
* @return array[]
*/
private function get_tasks(): array {
// TODO Implement the logic for this.
$core_tasks = [
new Sensei_Home_Task_Setup_Site(),
new Sensei_Home_Task_Create_First_Course(),
new Sensei_Home_Task_Configure_Learning_Mode(),
new Sensei_Home_Task_Publish_First_Course(),
];

if ( Sensei_Home_Task_Sell_Course_With_WooCommerce::is_active() ) {
$core_tasks[] = new Sensei_Home_Task_Sell_Course_With_WooCommerce();
}

if ( Sensei_Home_Task_Customize_Course_Theme::is_active() ) {
$core_tasks[] = new Sensei_Home_Task_Customize_Course_Theme();
}

$tasks = [];
/**
* Each one of the core tasks.
*
* @var Sensei_Home_Task $core_task
*/
foreach ( $core_tasks as $core_task ) {
$tasks[ $core_task::get_id() ] = $this->map_task( $core_task );
foreach ( $this->tasks as $task ) {
$tasks[ $task::get_id() ] = $this->map_task( $task );
}

/**
Expand All @@ -105,7 +147,8 @@ private function get_tasks(): array {
}

/**
* Maps a specific Sensei_Home_Task to a basic array structure. Will execute the code in `is_completed` for all entries.
* Maps a specific Sensei_Home_Task to a basic array structure. Will execute the code in `is_completed` for all
* entries.
*
* @param Sensei_Home_Task $task The actual task to map.
* @return array
Expand Down Expand Up @@ -175,30 +218,6 @@ public function mark_as_completed( $completed = true ) {
update_option( self::COMPLETED_TASKS_OPTION_KEY, $completed, false );
}

/**
* Attaches required hooks.
*/
private function attach_tasks_statuses_hooks() {
// Attach the hooks only once.
if ( self::$attached_hooks ) {
return;
}

self::$attached_hooks = true;

add_action( 'save_post_course', [ $this, 'log_course_completion_tasks' ], 10, 3 );

// Attach some hooks only for Atomic sites.
if ( ! Sensei_Utils::is_atomic_platform() ) {
return;
}

// Attach the update_tasks_statuses method to filters and actions
// that can affect the status of the Sensei Home tasks.
add_action( 'save_post_course', [ $this, 'update_tasks_statuses' ] );
add_action( 'wp_ajax_sensei_settings_section_visited', [ $this, 'update_tasks_statuses' ] );
}

/**
* Updates the tasks_statuses in wp options
*/
Expand Down
113 changes: 113 additions & 0 deletions includes/admin/home/tasks/task/class-sensei-home-task-launch-site.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php
/**
* File containing the Sensei_Home_Task_Launch_Site class.
*
* @package sensei-lms
* @since $$next-version$$
*/

/**
* Sensei_Home_Task_Launch_Site class.
*
* @since $$next-version$$
*/
class Sensei_Home_Task_Launch_Site implements Sensei_Home_Task {
/**
* The action key.
*
* @var string
*/
const ACTION = 'sensei_home_task_launch_site';

/**
* Initialize the task hooks.
*/
public function init() {
add_action( 'admin_post_' . static::ACTION, [ $this, 'handle_action' ] );
}

/**
* Handle the task action.
*/
function handle_action() {
check_admin_referer( static::ACTION );

$blog_id = (int) Jetpack_Options::get_option( 'id' );
$response = \Automattic\Jetpack\Connection\Client::wpcom_json_api_request_as_user(
"/sites/$blog_id/launch",
'1.1',
[
'method' => 'POST',
],
null,
'rest'
);

print_r( $response );
}

/**
* The ID for the task.
*
* @return string
*/
public static function get_id(): string {
return 'launch-site';
}

/**
* Number used to sort in frontend.
*
* @return int
*/
public function get_priority(): int {
return 700;
}

/**
* Task title.
*
* @return string
*/
public function get_title(): string {
return __( 'Launch your site', 'sensei-lms' );
}

/**
* Task url.
*
* @return string
*/
public function get_url(): ?string {
return add_query_arg(
[
'action' => static::ACTION,
],
wp_nonce_url( admin_url( 'admin-post.php' ), static::ACTION )
);
}

/**
* Whether the task is completed or not.
*
* @return bool
*/
public function is_completed(): bool {
return false; // TODO: Remove hardcoded value.

$task_statuses = (array) get_option( 'launchpad_checklist_tasks_statuses' );

return isset( $task_statuses['site_launched'] ) && $task_statuses['site_launched'];
}

/**
* Test if this task should be active or not.
*
* @return bool Whether the task should be active or not.
*/
public static function is_active() {
return true; // TODO: Remove hardcoded value.

// return Sensei_Utils::is_atomic_platform();
}
}
Loading