| 1 |
<?php |
| 2 |
|
| 3 |
namespace HelloPlus\Modules\Admin\Classes\Rest; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; // Exit if accessed directly. |
| 7 |
} |
| 8 |
|
| 9 |
use Elementor\App\Modules\KitLibrary\Connect\Kit_Library; |
| 10 |
use HelloPlus\Includes\Utils; |
| 11 |
use HelloPlus\Modules\Admin\Classes\Menu\Pages\Setup_Wizard; |
| 12 |
|
| 13 |
class Onboarding_Settings { |
| 14 |
const KITS_TRANSIENT = 'helloplus_elementor_kits'; |
| 15 |
|
| 16 |
public function rest_api_init() { |
| 17 |
register_rest_route( |
| 18 |
'elementor-hello-plus/v1', |
| 19 |
'/onboarding-settings', |
| 20 |
[ |
| 21 |
'methods' => \WP_REST_Server::READABLE, |
| 22 |
'callback' => [ $this, 'get_onboarding_settings' ], |
| 23 |
'permission_callback' => function () { |
| 24 |
return current_user_can( 'manage_options' ); |
| 25 |
}, |
| 26 |
] |
| 27 |
); |
| 28 |
} |
| 29 |
|
| 30 |
public function get_kits() { |
| 31 |
$kits = get_transient( self::KITS_TRANSIENT ); |
| 32 |
|
| 33 |
if ( ! empty( $kits ) ) { |
| 34 |
return $kits; |
| 35 |
} |
| 36 |
|
| 37 |
if ( ! class_exists( 'Elementor\App\Modules\KitLibrary\Connect\Kit_Library' ) ) { |
| 38 |
return []; |
| 39 |
} |
| 40 |
|
| 41 |
$args = [ |
| 42 |
'products' => 'ehp', |
| 43 |
'visibility' => 'restricted', |
| 44 |
'editor_layout_type' => 'container_flexbox', |
| 45 |
]; |
| 46 |
|
| 47 |
/** |
| 48 |
* Filter the arguments used to fetch the Hello+ kits. |
| 49 |
* |
| 50 |
* @param array $args default arguments. |
| 51 |
*/ |
| 52 |
$args = apply_filters( 'hello-plus/onboarding/kits-args', $args ); |
| 53 |
|
| 54 |
$endpoint_url = add_query_arg( $args, Kit_Library::DEFAULT_BASE_ENDPOINT . '/kits' ); |
| 55 |
try { |
| 56 |
$kits = $this->call_and_check( $endpoint_url ); |
| 57 |
|
| 58 |
$sorted_kits = $this->sort_kits_by_index( $kits, 'updated_at' ); |
| 59 |
|
| 60 |
foreach ( $sorted_kits as $index => $kit ) { |
| 61 |
$sorted_kits[ $index ]['manifest'] = $this->call_and_check( |
| 62 |
Kit_Library::DEFAULT_BASE_ENDPOINT . '/kits/' . $kit['_id'] . '/manifest' |
| 63 |
); |
| 64 |
} |
| 65 |
|
| 66 |
set_transient( self::KITS_TRANSIENT, $sorted_kits, HOUR_IN_SECONDS ); |
| 67 |
} catch ( \Exception $e ) { |
| 68 |
return []; |
| 69 |
} |
| 70 |
|
| 71 |
return $sorted_kits; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* @param string $url |
| 76 |
* |
| 77 |
* @return mixed |
| 78 |
* @throws \Exception |
| 79 |
*/ |
| 80 |
public function call_and_check( string $url ) { |
| 81 |
$response = wp_remote_get( $url ); |
| 82 |
|
| 83 |
if ( is_wp_error( $response ) ) { |
| 84 |
if ( 0 === strpos( $url, Kit_Library::DEFAULT_BASE_ENDPOINT ) ) { |
| 85 |
return $this->call_and_check( |
| 86 |
str_replace( Kit_Library::DEFAULT_BASE_ENDPOINT, Kit_Library::FALLBACK_BASE_ENDPOINT, $url ) |
| 87 |
); |
| 88 |
} |
| 89 |
|
| 90 |
throw new \Exception( esc_html( "Error when calling $url: message {$response->get_error_message()}" ) ); |
| 91 |
} |
| 92 |
|
| 93 |
$response_code = wp_remote_retrieve_response_code( $response ); |
| 94 |
|
| 95 |
if ( 200 !== $response_code ) { |
| 96 |
if ( 0 === strpos( $url, Kit_Library::DEFAULT_BASE_ENDPOINT ) ) { |
| 97 |
return $this->call_and_check( |
| 98 |
str_replace( Kit_Library::DEFAULT_BASE_ENDPOINT, Kit_Library::FALLBACK_BASE_ENDPOINT, $url ) |
| 99 |
); |
| 100 |
} |
| 101 |
|
| 102 |
throw new \Exception( esc_html( "Error when calling $url: response code $response_code" ) ); |
| 103 |
} |
| 104 |
|
| 105 |
$response_body = wp_remote_retrieve_body( $response ); |
| 106 |
|
| 107 |
return json_decode( $response_body, true ); |
| 108 |
} |
| 109 |
|
| 110 |
public function get_onboarding_settings() { |
| 111 |
$nonce = wp_create_nonce( 'updates' ); |
| 112 |
|
| 113 |
/** |
| 114 |
* Filters the labels for the Get Started page in the Setup Wizard. |
| 115 |
* |
| 116 |
* @since 1.6.2 |
| 117 |
* |
| 118 |
* @param array $labels The labels and buttons text for the Get Started page in the Setup Wizard. |
| 119 |
*/ |
| 120 |
$get_started_text = apply_filters( |
| 121 |
'hello-plus/onboarding/get-started-text', |
| 122 |
[ |
| 123 |
'title' => __( 'Welcome! Let\'s create your website.', 'hello-plus' ), |
| 124 |
'description' => __( 'Thanks for installing the Hello Biz theme by Elementor. This setup wizard will help you create a website in moments.', 'hello-plus' ), |
| 125 |
'disclaimer' => __( 'By clicking "Start building my website", I agree to install & activate the Elementor plugin. I accept the Elementor', 'hello-plus' ), |
| 126 |
'termsUrl' => 'https://elementor.com/terms/', |
| 127 |
'termsText' => __( 'Terms and Conditions', 'hello-plus' ), |
| 128 |
'buttonText' => __( 'Start building my website', 'hello-plus' ), |
| 129 |
] |
| 130 |
); |
| 131 |
|
| 132 |
/** |
| 133 |
* Filters the labels for the Ready to Go page in the Setup Wizard. |
| 134 |
* |
| 135 |
* @since 1.6.2 |
| 136 |
* |
| 137 |
* @param array $labels The labels and buttons text for the Ready to Go page in the Setup Wizard. |
| 138 |
*/ |
| 139 |
$ready_to_go_text = apply_filters( |
| 140 |
'hello-plus/onboarding/ready-to-go-text', |
| 141 |
[ |
| 142 |
'title' => __( 'Congratulations, you have created your website!', 'hello-plus' ), |
| 143 |
'description' => __( 'It\'s time to make it yours—add your content, style, and personal touch.', 'hello-plus' ), |
| 144 |
'viewSite' => __( 'View my site', 'hello-plus' ), |
| 145 |
'customizeSite' => __( 'Customize my site', 'hello-plus' ), |
| 146 |
] |
| 147 |
); |
| 148 |
|
| 149 |
/** |
| 150 |
* Filters the labels for the Install Kit page in the Setup Wizard. |
| 151 |
* |
| 152 |
* @since 1.6.2 |
| 153 |
* |
| 154 |
* @param array $labels The labels and buttons text for the Install Kit page in the Setup Wizard. |
| 155 |
*/ |
| 156 |
$install_kit_text = apply_filters( |
| 157 |
'hello-plus/onboarding/install-kit-text', |
| 158 |
[ |
| 159 |
'title' => __( 'Choose your website template kit', 'hello-plus' ), |
| 160 |
'description' => __( 'Explore our versatile website kits to find one that fits your style or project.', 'hello-plus' ), |
| 161 |
] |
| 162 |
); |
| 163 |
|
| 164 |
return rest_ensure_response( |
| 165 |
[ |
| 166 |
'settings' => [ |
| 167 |
'nonce' => $nonce, |
| 168 |
'elementorInstalled' => Utils::is_elementor_installed(), |
| 169 |
'elementorActive' => Utils::is_elementor_active(), |
| 170 |
'modalCloseRedirectUrl' => self_admin_url( 'admin.php?page=' . Utils::get_theme_slug() ), |
| 171 |
'kits' => array_values( $this->filter_kits_by_theme( $this->get_kits() ) ), |
| 172 |
'applyKitBaseUrl' => self_admin_url( 'admin.php?page=elementor-app' ), |
| 173 |
'wizardCompleted' => Setup_Wizard::has_site_wizard_been_completed(), |
| 174 |
'returnUrl' => self_admin_url( 'admin.php?page=hello-plus-setup-wizard' ), |
| 175 |
'getStartedText' => $get_started_text, |
| 176 |
'readyToGoText' => $ready_to_go_text, |
| 177 |
'installKitText' => $install_kit_text, |
| 178 |
], |
| 179 |
] |
| 180 |
); |
| 181 |
} |
| 182 |
|
| 183 |
public function filter_kits_by_theme( array $kits ): array { |
| 184 |
$theme_slug = Utils::get_theme_slug(); |
| 185 |
|
| 186 |
return array_filter( $kits, function ( $kit ) use ( $theme_slug ) { |
| 187 |
if ( empty( $kit['taxonomies'] ) || ! is_array( $kit['taxonomies'] ) ) { |
| 188 |
return false; |
| 189 |
} |
| 190 |
|
| 191 |
foreach ( $kit['taxonomies'] as $category ) { |
| 192 |
$cat_name = $category['name'] ?? ''; |
| 193 |
$cat_type = $category['type'] ?? ''; |
| 194 |
|
| 195 |
if ( $cat_name === $theme_slug && 'third_category' === $cat_type ) { |
| 196 |
return true; |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
return false; |
| 201 |
} ); |
| 202 |
} |
| 203 |
|
| 204 |
private function sort_kits_by_index( array $kits, string $sort_by = 'featured_index' ): array { |
| 205 |
if ( empty( $kits[0][ $sort_by ] ) ) { |
| 206 |
$sort_by = 'featured_index'; |
| 207 |
} |
| 208 |
|
| 209 |
$sort_by_time = false; |
| 210 |
|
| 211 |
if ( in_array( $sort_by, [ 'created_at', 'updated_at', 'created' ], true ) ) { |
| 212 |
$sort_by_time = true; |
| 213 |
} |
| 214 |
|
| 215 |
usort($kits, function ( $kit_1, $kit_2 ) use ( $sort_by, $sort_by_time ) { |
| 216 |
if ( $sort_by_time ) { |
| 217 |
return \strtotime( $kit_2[ $sort_by ] ) <=> \strtotime( $kit_1[ $sort_by ] ); |
| 218 |
} |
| 219 |
|
| 220 |
return $kit_1[ $sort_by ] <=> $kit_2[ $sort_by ]; |
| 221 |
} ); |
| 222 |
|
| 223 |
return $kits; |
| 224 |
} |
| 225 |
|
| 226 |
public function __construct() { |
| 227 |
add_action( 'rest_api_init', [ $this, 'rest_api_init' ] ); |
| 228 |
} |
| 229 |
} |
| 230 |
|