PluginProbe
Hello Plus / 1.4.0
Hello Plus v1.4.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.4.0, at modules/admin/classes/rest/onboarding-settings.php

154 lines 4.1 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
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 if ( ! empty( $kits ) ) {
33 return $kits;
34 }
35
36 if ( ! class_exists( 'Elementor\App\Modules\KitLibrary\Connect\Kit_Library' ) ) {
37 return [];
38 }
39
40 $args = [
41 'products' => 'ehp',
42 'visibility' => 'restricted',
43 'editor_layout_type' => 'container_flexbox',
44 ];
45
46 /**
47 * Filter the arguments used to fetch the Hello+ kits.
48 *
49 * @param array $args default arguments.
50 */
51 $args = apply_filters( 'hello-plus/onboarding/kits-args', $args );
52
53 $endpoint_url = add_query_arg( $args, Kit_Library::DEFAULT_BASE_ENDPOINT . '/kits' );
54 try {
55 $kits = $this->call_and_check( $endpoint_url );
56
57 $sorted_kits = $this->sort_kits_by_index( $kits, 'updated_at' );
58
59 foreach ( $sorted_kits as $index => $kit ) {
60 $sorted_kits[ $index ]['manifest'] = $this->call_and_check(
61 Kit_Library::DEFAULT_BASE_ENDPOINT . '/kits/' . $kit['_id'] . '/manifest'
62 );
63 }
64
65 set_transient( self::KITS_TRANSIENT, $sorted_kits, HOUR_IN_SECONDS );
66 } catch ( \Exception $e ) {
67 return [];
68 }
69
70 return $sorted_kits;
71 }
72
73 /**
74 * @param string $url
75 *
76 * @return mixed
77 * @throws \Exception
78 */
79 public function call_and_check( string $url ) {
80 $response = wp_remote_get( $url );
81
82 if ( is_wp_error( $response ) ) {
83 if ( 0 === strpos( $url, Kit_Library::DEFAULT_BASE_ENDPOINT ) ) {
84 return $this->call_and_check(
85 str_replace( Kit_Library::DEFAULT_BASE_ENDPOINT, Kit_Library::FALLBACK_BASE_ENDPOINT, $url )
86 );
87 }
88
89 throw new \Exception( esc_html( "Error when calling $url: message {$response->get_error_message()}" ) );
90 }
91
92 $response_code = wp_remote_retrieve_response_code( $response );
93
94 if ( 200 !== $response_code ) {
95 if ( 0 === strpos( $url, Kit_Library::DEFAULT_BASE_ENDPOINT ) ) {
96 return $this->call_and_check(
97 str_replace( Kit_Library::DEFAULT_BASE_ENDPOINT, Kit_Library::FALLBACK_BASE_ENDPOINT, $url )
98 );
99 }
100
101 throw new \Exception( esc_html( "Error when calling $url: response code $response_code" ) );
102 }
103
104 $response_body = wp_remote_retrieve_body( $response );
105
106 return json_decode( $response_body, true );
107 }
108
109 public function get_onboarding_settings() {
110 $nonce = wp_create_nonce( 'updates' );
111
112 return rest_ensure_response(
113 [
114 'settings' => [
115 'nonce' => $nonce,
116 'elementorInstalled' => Utils::is_elementor_installed(),
117 'elementorActive' => Utils::is_elementor_active(),
118 'modalCloseRedirectUrl' => self_admin_url( 'admin.php?page=' . Utils::get_theme_slug() ),
119 'kits' => $this->get_kits(),
120 'applyKitBaseUrl' => self_admin_url( 'admin.php?page=elementor-app' ),
121 'wizardCompleted' => Setup_Wizard::has_site_wizard_been_completed(),
122 'returnUrl' => self_admin_url( 'admin.php?page=hello-plus-setup-wizard' ),
123 ],
124 ]
125 );
126 }
127
128 private function sort_kits_by_index( array $kits, string $sort_by = 'featured_index' ): array {
129 if ( empty( $kits[0][ $sort_by ] ) ) {
130 $sort_by = 'featured_index';
131 }
132
133 $sort_by_time = false;
134
135 if ( in_array( $sort_by, [ 'created_at', 'updated_at', 'created' ], true ) ) {
136 $sort_by_time = true;
137 }
138
139 usort($kits, function ( $kit_1, $kit_2 ) use ( $sort_by, $sort_by_time ) {
140 if ( $sort_by_time ) {
141 return \strtotime( $kit_2[ $sort_by ] ) <=> \strtotime( $kit_1[ $sort_by ] );
142 }
143
144 return $kit_1[ $sort_by ] <=> $kit_2[ $sort_by ];
145 } );
146
147 return $kits;
148 }
149
150 public function __construct() {
151 add_action( 'rest_api_init', [ $this, 'rest_api_init' ] );
152 }
153 }
154