| 1 |
<?php |
| 2 |
|
| 3 |
namespace Hostinger\Admin\Onboarding; |
| 4 |
|
| 5 |
use Hostinger\Helper; |
| 6 |
use Hostinger\Settings; |
| 7 |
use Hostinger\Admin\Actions as Admin_Actions; |
| 8 |
use WP_Post; |
| 9 |
|
| 10 |
defined( 'ABSPATH' ) || exit; |
| 11 |
|
| 12 |
class AutocompleteSteps { |
| 13 |
private array $completed_steps; |
| 14 |
private Helper $helper; |
| 15 |
|
| 16 |
public function __construct() { |
| 17 |
$this->helper = new Helper(); |
| 18 |
$this->completed_steps = get_option( 'hostinger_onboarding_steps', array() ); |
| 19 |
add_action( 'customize_save', array( $this, 'logo_upload' ) ); |
| 20 |
add_action( 'wp_handle_upload', array( $this, 'image_upload' ) ); |
| 21 |
add_action( 'post_updated', array( $this, 'post_content_change' ), 10, 3 ); |
| 22 |
add_action( 'customize_save', array( $this, 'edit_site_title' ) ); |
| 23 |
add_action( 'publish_page', array( $this, 'new_page_creation' ), 10, 3 ); |
| 24 |
add_action( 'publish_product', array( $this, 'new_product_creation' ), 10, 3 ); |
| 25 |
add_action( 'publish_post', array( $this, 'new_post_creation' ), 10, 3 ); |
| 26 |
add_action( 'updated_option', array( $this, 'check_option_change' ), 10, 3 ); |
| 27 |
|
| 28 |
if ( $this->helper->is_hostinger_admin_page() ) { |
| 29 |
add_action( 'admin_init', array( $this, 'domain_is_connected' ) ); |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
private function add_completed_step( string $action ): void { |
| 34 |
$this->completed_steps[] = array( |
| 35 |
'action' => $action, |
| 36 |
'date' => gmdate( 'Y-m-d H:i:s' ), |
| 37 |
); |
| 38 |
} |
| 39 |
|
| 40 |
private function is_step_completed( array $completed_steps, string $step_name ): array { |
| 41 |
$step_completed = array_filter( |
| 42 |
$completed_steps, |
| 43 |
function ( $item ) use ( $step_name ) { |
| 44 |
return isset( $item['action'] ) && $item['action'] === $step_name; |
| 45 |
} |
| 46 |
); |
| 47 |
|
| 48 |
return $step_completed; |
| 49 |
} |
| 50 |
|
| 51 |
public function domain_is_connected(): void { |
| 52 |
$action = Admin_Actions::DOMAIN_IS_CONNECTED; |
| 53 |
|
| 54 |
if ( $this->is_step_completed( $this->completed_steps, $action ) ) { |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
if ( ! $this->helper->is_free_subdomain() && ! $this->helper->is_preview_domain() ) { |
| 59 |
if ( ! did_action( 'hostinger_domain_connected' ) ) { |
| 60 |
$this->add_completed_step( $action ); |
| 61 |
Settings::update_setting( 'onboarding_steps', $this->completed_steps ); |
| 62 |
do_action( 'hostinger_domain_connected' ); |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
public function logo_upload( WP_Customize_Manager $data ): void { |
| 68 |
$action = Admin_Actions::LOGO_UPLOAD; |
| 69 |
|
| 70 |
$logo_updated = array_filter( |
| 71 |
$data->changeset_data(), |
| 72 |
function ( $key ) { |
| 73 |
return strpos( $key, 'custom_logo' ) !== false; |
| 74 |
}, |
| 75 |
ARRAY_FILTER_USE_KEY |
| 76 |
); |
| 77 |
|
| 78 |
$has_logo = reset( $logo_updated )['value'] ?? false; |
| 79 |
$cookie_value = isset( $_COOKIE[ $action ] ) ? sanitize_text_field( wp_unslash( $_COOKIE[ $action ] ) ) : ''; |
| 80 |
|
| 81 |
if ( $this->is_step_completed( $this->completed_steps, $action ) || $logo_updated && ! $has_logo ) { |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
if ( $logo_updated && $cookie_value === $action ) { |
| 86 |
$this->add_completed_step( $action ); |
| 87 |
Settings::update_setting( 'onboarding_steps', $this->completed_steps ); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
public function image_upload( array $data ): array { |
| 92 |
$action = Admin_Actions::IMAGE_UPLOAD; |
| 93 |
$file_type = $data['type'] ?? ''; |
| 94 |
$cookie_value = isset( $_COOKIE[ $action ] ) ? sanitize_text_field( wp_unslash( $_COOKIE[ $action ] ) ) : ''; |
| 95 |
|
| 96 |
if ( $this->is_step_completed( $this->completed_steps, $action ) || strpos( $file_type, 'image' ) !== 0 ) { |
| 97 |
return $data; |
| 98 |
} |
| 99 |
|
| 100 |
if ( $cookie_value === $action ) { |
| 101 |
$this->add_completed_step( $action ); |
| 102 |
Settings::update_setting( 'onboarding_steps', $this->completed_steps ); |
| 103 |
} |
| 104 |
|
| 105 |
return $data; |
| 106 |
} |
| 107 |
|
| 108 |
public function post_content_change( int $post_id, WP_Post $post_after, WP_Post $post_before ) { |
| 109 |
$action = Admin_Actions::EDIT_DESCRIPTION; |
| 110 |
$post_date = get_the_date( 'Y-m-d H:i:s', $post_id ); |
| 111 |
$modified_date = get_the_modified_date( 'Y-m-d H:i:s', $post_id ); |
| 112 |
$post_type = get_post_type( $post_id ); |
| 113 |
$cookie_value = isset( $_COOKIE[ $action ] ) ? sanitize_text_field( wp_unslash( $_COOKIE[ $action ] ) ) : ''; |
| 114 |
$content_before = $post_before->post_content; |
| 115 |
$content_after = $post_after->post_content; |
| 116 |
|
| 117 |
if ( $this->is_step_completed( $this->completed_steps, $action ) || $post_date === $modified_date ) { |
| 118 |
return; |
| 119 |
} |
| 120 |
|
| 121 |
if ( wp_is_post_revision( $post_id ) || wp_is_post_autosave( $post_id ) ) { |
| 122 |
return; |
| 123 |
} |
| 124 |
|
| 125 |
if ( $post_type === 'post' && $content_before !== $content_after && $cookie_value === $action ) { |
| 126 |
$this->add_completed_step( $action ); |
| 127 |
Settings::update_setting( 'onboarding_steps', $this->completed_steps ); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
public function edit_site_title( WP_Customize_Manager $data ): void { |
| 132 |
$action = Admin_Actions::EDIT_SITE_TITLE; |
| 133 |
$changed_title = $data->changeset_data()['blogname']['value'] ?? ''; |
| 134 |
$cookie_value = isset( $_COOKIE[ $action ] ) ? sanitize_text_field( wp_unslash( $_COOKIE[ $action ] ) ) : ''; |
| 135 |
|
| 136 |
if ( $this->is_step_completed( $this->completed_steps, $action ) ) { |
| 137 |
return; |
| 138 |
} |
| 139 |
|
| 140 |
if ( $cookie_value === $action && $changed_title !== '' && get_bloginfo( 'name' ) !== $changed_title ) { |
| 141 |
$this->add_completed_step( $action ); |
| 142 |
Settings::update_setting( 'onboarding_steps', $this->completed_steps ); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
public function new_post_item_creation( int $post_id, bool $update, string $action ): void { |
| 147 |
$cookie_value = isset( $_COOKIE[ $action ] ) ? sanitize_text_field( wp_unslash( $_COOKIE[ $action ] ) ) : ''; |
| 148 |
|
| 149 |
if ( $this->is_step_completed( $this->completed_steps, $action ) || wp_is_post_revision( $post_id ) || wp_is_post_autosave( $post_id ) ) { |
| 150 |
return; |
| 151 |
} |
| 152 |
|
| 153 |
if ( $update && $cookie_value === $action ) { |
| 154 |
$this->add_completed_step( $action ); |
| 155 |
Settings::update_setting( 'onboarding_steps', $this->completed_steps ); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
public function new_page_creation( int $post_id, WP_Post $post, bool $update ): void { |
| 160 |
$this->new_post_item_creation( $post_id, $update, Admin_Actions::ADD_PAGE ); |
| 161 |
} |
| 162 |
|
| 163 |
public function new_product_creation( int $post_id, WP_Post $post, bool $update ): void { |
| 164 |
$this->new_post_item_creation( $post_id, $update, Admin_Actions::ADD_PRODUCT ); |
| 165 |
} |
| 166 |
|
| 167 |
public function new_post_creation( int $post_id, WP_Post $post, bool $update ): void { |
| 168 |
$this->new_post_item_creation( $post_id, $update, Admin_Actions::ADD_POST ); |
| 169 |
} |
| 170 |
|
| 171 |
public function check_option_change( string $option_name, $old_value, $new_value ): void { |
| 172 |
$action = Admin_Actions::EDIT_SITE_TITLE; |
| 173 |
$cookie_value = isset( $_COOKIE[ $action ] ) ? sanitize_text_field( wp_unslash( $_COOKIE[ $action ] ) ) : ''; |
| 174 |
|
| 175 |
if ( $this->is_step_completed( $this->completed_steps, $action ) ) { |
| 176 |
return; |
| 177 |
} |
| 178 |
|
| 179 |
if ( $cookie_value === $action && $new_value !== '' && $option_name === Settings::SITE_TITLE_OPTION && $old_value !== $new_value ) { |
| 180 |
$this->add_completed_step( $action ); |
| 181 |
Settings::update_setting( 'onboarding_steps', $this->completed_steps ); |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
|