PluginProbe
Hostinger Tools / 2.1.1
Hostinger Tools v2.1.1
3.0.77 3.0.76 3.0.75 3.0.74 3.0.73 3.0.72 3.0.71 3.0.70 3.0.69 3.0.68 3.0.67 3.0.66 1.8.1 1.8.2 1.8.3 1.9.1 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0.0 2.0.1 2.0.4 All 108 releases
hostinger / includes / admin / onboarding / class-hostinger-autocomplete-steps.php

class-hostinger-autocomplete-steps.php in Hostinger Tools 2.1.1, at includes/admin/onboarding/class-hostinger-autocomplete-steps.php

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