PluginProbe
Hello Plus / 1.2.0
Hello Plus v1.2.0
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.2.1 1.3.0 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.5.3 1.6.0 1.6.1 1.6.2 1.7.0 1.7.1 1.7.2 1.7.3 All 30 releases
hello-plus / modules / admin / classes / rest / onboarding-settings.php

onboarding-settings.php in Hello Plus 1.2.0, at modules/admin/classes/rest/onboarding-settings.php

131 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 use WP_REST_Server;
13
14 class Onboarding_Settings {
15 const KITS_TRANSIENT = 'helloplus_elementor_kits';
16
17 public function rest_api_init() {
18 register_rest_route(
19 'elementor-hello-plus/v1',
20 '/onboarding-settings',
21 [
22 'methods' => WP_REST_Server::READABLE,
23 'callback' => [ $this, 'get_onboarding_settings' ],
24 'permission_callback' => function () {
25 return current_user_can( 'manage_options' );
26 },
27 ]
28 );
29 }
30
31 public function get_kits() {
32 $kits = get_transient( self::KITS_TRANSIENT );
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 foreach ( $kits as $index => $kit ) {
59 $kits[ $index ]['manifest'] = $this->call_and_check(
60 Kit_Library::DEFAULT_BASE_ENDPOINT . '/kits/' . $kit['_id'] . '/manifest'
61 );
62 }
63
64 set_transient( self::KITS_TRANSIENT, $kits, 24 * HOUR_IN_SECONDS );
65 } catch ( \Exception $e ) {
66 return [];
67 }
68
69 return $kits;
70 }
71
72 /**
73 * @param string $url
74 *
75 * @return mixed
76 * @throws \Exception
77 */
78 public function call_and_check( string $url ) {
79 $response = wp_remote_get( $url );
80
81 if ( is_wp_error( $response ) ) {
82 if ( 0 === strpos( $url, Kit_Library::DEFAULT_BASE_ENDPOINT ) ) {
83 return $this->call_and_check(
84 str_replace( Kit_Library::DEFAULT_BASE_ENDPOINT, Kit_Library::FALLBACK_BASE_ENDPOINT, $url )
85 );
86 }
87
88 throw new \Exception( esc_html( "Error when calling $url: message {$response->get_error_message()}" ) );
89 }
90
91 $response_code = wp_remote_retrieve_response_code( $response );
92
93 if ( 200 !== $response_code ) {
94 if ( 0 === strpos( $url, Kit_Library::DEFAULT_BASE_ENDPOINT ) ) {
95 return $this->call_and_check(
96 str_replace( Kit_Library::DEFAULT_BASE_ENDPOINT, Kit_Library::FALLBACK_BASE_ENDPOINT, $url )
97 );
98 }
99
100 throw new \Exception( esc_html( "Error when calling $url: response code $response_code" ) );
101 }
102
103 $response_body = wp_remote_retrieve_body( $response );
104
105 return json_decode( $response_body, true );
106 }
107
108 public function get_onboarding_settings() {
109 $nonce = wp_create_nonce( 'updates' );
110
111 return rest_ensure_response(
112 [
113 'settings' => [
114 'nonce' => $nonce,
115 'elementorInstalled' => Utils::is_elementor_installed(),
116 'elementorActive' => Utils::is_elementor_active(),
117 'modalCloseRedirectUrl' => self_admin_url( 'admin.php?page=' . Utils::get_theme_slug() ),
118 'kits' => $this->get_kits(),
119 'applyKitBaseUrl' => self_admin_url( 'admin.php?page=elementor-app' ),
120 'wizardCompleted' => Setup_Wizard::has_site_wizard_been_completed(),
121 'returnUrl' => self_admin_url( 'admin.php?page=hello-plus-setup-wizard' ),
122 ],
123 ]
124 );
125 }
126
127 public function __construct() {
128 add_action( 'rest_api_init', [ $this, 'rest_api_init' ] );
129 }
130 }
131