PluginProbe
StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More / 2.2.0
StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More v2.2.0
2.3.0 2.2.0 2.1.1 2.1.0 2.0.0 1.10.0 1.9.1 1.9.0 1.2.1 1.2.2 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 All 59 releases
storeengine / includes / ajax / setup.php

setup.php in StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More 2.2.0, at includes/ajax/setup.php

251 lines 6.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace StoreEngine\Ajax;
4
5 use StoreEngine\Admin\Settings\Base as BaseSettings;
6 use StoreEngine\Classes\AbstractAjaxHandler;
7 use StoreEngine\Utils\Helper;
8 use WP_Error;
9
10 class Setup extends AbstractAjaxHandler {
11
12 /**
13 * Request namespace.
14 *
15 * @var string
16 */
17 protected string $namespace = STOREENGINE_PLUGIN_SLUG . '/setup';
18
19 public function __construct() {
20 $this->actions = [
21 'generate_store_pages' => [
22 'callback' => [ $this, 'generate_store_pages' ],
23 'capability' => 'manage_options',
24 'fields' => [
25 'use_prefix' => 'boolean',
26 'use_woo_pages' => 'boolean',
27 ]
28 ],
29 'activate_plugin' => [
30 'callback' => [ $this, 'activate_plugin' ],
31 'capability' => 'manage_options',
32 'fields' => [
33 'slug' => 'string',
34 'name' => 'string',
35 'plugin' => 'string',
36 ]
37 ],
38 // One-click download + install + activate for the free companion
39 // plugins (Payments, Connectors, Bricks/Elementor addons). The
40 // package URL is resolved server-side from the plugin key — never
41 // client-supplied.
42 'install_satellite' => [
43 'callback' => [ $this, 'install_satellite' ],
44 'capability' => 'install_plugins',
45 'fields' => [
46 'plugin' => 'string',
47 ]
48 ]
49 ];
50 }
51
52 public static function install_satellite( array $payload ) {
53 $key = $payload['plugin'] ?? '';
54
55 if ( ! in_array( $key, \StoreEngine\Admin\SatellitePlugins::get_keys(), true ) ) {
56 return new WP_Error( 'storeengine_unknown_satellite', __( 'Unknown plugin.', 'storeengine' ), [ 'status' => 400 ] );
57 }
58
59 $result = \StoreEngine\Admin\SatellitePlugins::install_and_activate( $key );
60
61 if ( is_wp_error( $result ) ) {
62 return $result;
63 }
64
65 // Return the refreshed teaser state so the UI can react without a reload.
66 $result['satellite_plugins'] = \StoreEngine\Admin\SatellitePlugins::get_teaser_data();
67
68 return $result;
69 }
70
71 public static function activate_plugin( array $payload ) {
72 if ( empty( $payload['name'] ) || empty( $payload['slug'] ) || empty( $payload['plugin'] ) ) {
73 wp_send_json_error( __( 'No plugin specified.', 'storeengine' ) );
74 }
75
76 if ( ! current_user_can( 'activate_plugin', $payload['plugin'] ) ) {
77 wp_send_json_error( __( 'Sorry, you are not allowed to activate plugin.', 'storeengine' ) );
78 }
79
80 if ( Helper::is_plugin_active( $payload['plugin'] ) ) {
81 wp_send_json_error( sprintf(
82 /* translators: %s: Plugin name. */
83 __( '%s is already active.', 'storeengine' ),
84 $payload['name']
85 ) );
86 }
87
88 $activated = Helper::activate_plugin( $payload['plugin'], '', false, true );
89
90 if ( is_wp_error( $activated ) ) {
91 wp_send_json_error( $activated->get_error_message() );
92 }
93
94 wp_send_json_success( sprintf(
95 /* translators: %s: Plugin name. */
96 __( '%s has been activated.', 'storeengine' ),
97 $payload['name']
98 ) );
99 }
100
101 /**
102 * @param array $payload
103 *
104 * @return void|WP_Error
105 */
106 public function generate_store_pages( array $payload ) {
107 global $storeengine_settings;
108
109 $use_woo_pages = $payload['use_woo_pages'] ?? false;
110 $use_prefix = $payload['use_prefix'] ?? false;
111
112 if ( $use_woo_pages && Helper::is_plugin_installed( 'woocommerce/woocommerce.php' ) ) {
113 $wc_pages = self::get_woo_store_pages();
114
115 // Prepare settings data for update.
116 $settings = (array) $storeengine_settings;
117
118 foreach ( Helper::get_store_page_contents() as $key => $page ) {
119 if ( $wc_pages[ $key ] ) {
120 $updated = wp_update_post( [
121 'ID' => $wc_pages[ $key ],
122 'post_status' => 'publish',
123 'post_content' => $page[ $key ]['content'] ?? '',
124 'comment_status' => 'closed',
125 ], true );
126
127 if ( is_wp_error( $updated ) ) {
128 return $updated;
129 }
130
131 // Set template for handling block-theme compatibility.
132 update_post_meta( $wc_pages[ $key ], '_wp_page_template', 'storeengine-canvas.php' );
133
134 $settings[ $key ] = $wc_pages[ $key ];
135 } else {
136 $parent = ! empty( $page['parent'] ) ? Helper::get_settings( $page['parent'], 0 ) : 0;
137 $status = ! empty( $page['post_status'] ) ? $page['post_status'] : 'publish';
138 $post_id = Helper::create_page(
139 esc_sql( $page['slug'] ),
140 $key,
141 $page['title'],
142 $page['content'],
143 $parent,
144 $status
145 );
146
147 if ( ! $post_id ) {
148 return new WP_Error(
149 'failed-to-create-' . $key,
150 sprintf(
151 // translators: %s. Core page name.
152 __( 'Failed to create %s page', 'storeengine' ),
153 $page['title']
154 )
155 );
156 }
157
158 $settings[ $key ] = $post_id;
159 }
160 }
161
162 BaseSettings::save_settings( $settings );
163
164 wp_send_json_success( self::get_page_settings() );
165 }
166
167 if ( $use_prefix ) {
168 Helper::create_initial_pages( true );
169
170 wp_send_json_success( self::get_page_settings() );
171 }
172
173 if ( Helper::is_plugin_installed( 'woocommerce/woocommerce.php' ) ) {
174 if ( ! empty( self::get_woo_store_pages() ) ) {
175 wp_send_json_success( [ 'should_use_woo_pages' => true ] );
176 }
177 }
178
179 // Create pages without prefix if no wc pages are found.
180 Helper::create_initial_pages();
181 wp_send_json_success( self::get_page_settings() );
182 }
183
184 /**
185 * Get WooCommere store pages.
186 * WC store pages mapped with StoreEngine store page setting keys.
187 * WC doesn't have any separate thankyou page.
188 *
189 * @return array
190 * @see AssetDataRegistry::get_store_pages()
191 * @see WCAdminHelper::is_current_page_store_page()
192 */
193 protected static function get_woo_store_pages(): array {
194 if ( ! function_exists( 'wc_get_page_id' ) ) {
195 return [];
196 }
197
198 return array_filter(
199 [
200 'shop_page' => wc_get_page_id( 'shop' ),
201 'cart_page' => wc_get_page_id( 'cart' ),
202 'checkout_page' => wc_get_page_id( 'checkout' ),
203 'dashboard_page' => wc_get_page_id( 'myaccount' ),
204 ],
205 [ __CLASS__, 'filter_valid_page' ]
206 );
207 }
208
209 protected static function get_woo_other_pages(): array {
210 if ( ! function_exists( 'wc_get_page_id' ) ) {
211 return [];
212 }
213
214 return array_filter(
215 [
216 'coming_soon_page' => wc_get_page_id( 'coming_soon' ),
217 'privacy_page' => get_option( 'wp_page_for_privacy_policy', 0 ),
218 'terms_page' => wc_get_page_id( 'terms' ),
219 ],
220 [ __CLASS__, 'filter_valid_page' ]
221 );
222 }
223
224 public static function filter_valid_page( $page_id ): bool {
225 return (bool) get_post( $page_id );
226 }
227
228 protected static function get_page_settings(): array {
229 // Refresh settings.
230 \StoreEngine\Admin\Settings::load_settings();
231
232 $pages = [
233 'shop_page',
234 'cart_page',
235 'checkout_page',
236 'thankyou_page',
237 'dashboard_page',
238 'affiliate_registration_page',
239 'membership_pricing_page',
240 ];
241
242 $settings = [];
243
244 foreach ( $pages as $page ) {
245 $settings[ $page ] = Helper::get_settings( $page );
246 }
247
248 return array_filter( $settings, [ __CLASS__, 'filter_valid_page' ] );
249 }
250 }
251