1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
<?php
namespace WPForms\Lite\Admin;
use WP_Error; use WPForms\Helpers\PluginSilentUpgrader;
/** * WPForms Connect. * * WPForms Connect is our service that makes it easy for non-techy users to * upgrade to WPForms Pro without having to manually install WPForms Pro plugin. * * @since 1.5.5 */ class Connect {
/** * Constructor. * * @since 1.5.5 */ public function __construct() {
$this->hooks(); }
/** * Hooks. * * @since 1.5.5 */ public function hooks() {
\add_action( 'wpforms_settings_enqueue', array( $this, 'settings_enqueues' ) );
\add_action( 'wp_ajax_wpforms_connect_url', array( $this, 'generate_url' ) );
\add_action( 'wp_ajax_nopriv_wpforms_connect_process', array( $this, 'process' ) ); }
/** * Settings page enqueues. * * @since 1.5.5 */ public function settings_enqueues() {
$min = \wpforms_get_min_suffix();
\wp_enqueue_script( 'wpforms-connect', \WPFORMS_PLUGIN_URL . "lite/assets/js/admin/connect{$min}.js", array( 'jquery' ), \WPFORMS_VERSION, true ); }
/** * Generate and return WPForms Connect URL. * * @since 1.5.5 */ public function generate_url() {
// Run a security check. \check_ajax_referer( 'wpforms-admin', 'nonce' );
// Check for permissions. if ( ! \current_user_can( 'install_plugins' ) ) { \wp_send_json_error( [ 'message' => \esc_html__( 'You are not allowed to install plugins.', 'wpforms-lite' ) ] ); }
$key = ! empty( $_POST['key'] ) ? \sanitize_text_field( \wp_unslash( $_POST['key'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification
if ( empty( $key ) ) { \wp_send_json_error( [ 'message' => \esc_html__( 'Please enter your license key to connect.', 'wpforms-lite' ) ] ); }
if ( wpforms()->pro ) { \wp_send_json_error( [ 'message' => \esc_html__( 'Only the Lite version can be upgraded.', 'wpforms-lite' ) ] ); }
// Verify pro version is not installed. $active = \activate_plugin( 'wpforms/wpforms.php', false, false, true );
if ( ! \is_wp_error( $active ) ) {
// Deactivate Lite. $plugin = \plugin_basename( WPFORMS_PLUGIN_FILE );
\deactivate_plugins( $plugin );
do_action( 'wpforms_plugin_deactivated', $plugin );
\wp_send_json_success( [ 'message' => \esc_html__( 'WPForms Pro is installed but not activated.', 'wpforms-lite' ), 'reload' => true, ] ); }
// Generate URL. $oth = hash( 'sha512', \wp_rand() );
\update_option( 'wpforms_connect_token', $oth ); \update_option( 'wpforms_connect', $key );
$version = WPFORMS_VERSION; $endpoint = \admin_url( 'admin-ajax.php' ); $redirect = \admin_url( 'admin.php?page=wpforms-settings' ); $url = \add_query_arg( [ 'key' => $key, 'oth' => $oth, 'endpoint' => $endpoint, 'version' => $version, 'siteurl' => \admin_url(), 'homeurl' => \home_url(), 'redirect' => rawurldecode( base64_encode( $redirect ) ), // phpcs:ignore 'v' => 2, ], 'https://upgrade.wpforms.com' );
\wp_send_json_success( [ 'url' => $url, 'back_url' => \add_query_arg( [ 'action' => 'wpforms_connect', 'oth' => $oth, ], $endpoint ), ] ); }
/** * Process WPForms Connect. * * @since 1.5.5 */ public function process() {
$error = esc_html__( 'There was an error while installing an upgrade. Please download the plugin from wpforms.com and install it manually.', 'wpforms-lite' );
// Verify params present (oth & download link). $post_oth = ! empty( $_REQUEST['oth'] ) ? \sanitize_text_field( \wp_unslash( $_REQUEST['oth'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification $post_url = ! empty( $_REQUEST['file'] ) ? \esc_url_raw( \wp_unslash( $_REQUEST['file'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification
if ( empty( $post_oth ) || empty( $post_url ) ) { \wp_send_json_error( $error ); }
// Verify oth. $oth = \get_option( 'wpforms_connect_token' );
if ( empty( $oth ) || ! hash_equals( $oth, $post_oth ) ) { \wp_send_json_error( $error ); }
// Delete so cannot replay. \delete_option( 'wpforms_connect_token' );
// Set the current screen to avoid undefined notices. \set_current_screen( 'wpforms_page_wpforms-settings' );
// Prepare variables. $url = \esc_url_raw( \add_query_arg( [ 'page' => 'wpforms-settings' ], \admin_url( 'admin.php' ) ) );
// Verify pro not activated. if ( wpforms()->pro ) { \wp_send_json_success( \esc_html__( 'Plugin installed & activated.', 'wpforms-lite' ) ); }
// Verify pro not installed. $active = \activate_plugin( 'wpforms/wpforms.php', $url, false, true );
if ( ! \is_wp_error( $active ) ) { $plugin = plugin_basename( WPFORMS_PLUGIN_FILE );
\deactivate_plugins( $plugin );
do_action( 'wpforms_plugin_deactivated', $plugin );
\wp_send_json_success( esc_html__( 'Plugin installed & activated.', 'wpforms-lite' ) ); }
$creds = \request_filesystem_credentials( $url, '', false, false, null );
// Check for file system permissions. if ( false === $creds || ! \WP_Filesystem( $creds ) ) { \wp_send_json_error( \esc_html__( 'There was an error while installing an upgrade. Please check file system permissions and try again. Also, you can download the plugin from wpforms.com and install it manually.', 'wpforms-lite' ) ); }
/* * We do not need any extra credentials if we have gotten this far, so let's install the plugin. */
// Do not allow WordPress to search/download translations, as this will break JS output. \remove_action( 'upgrader_process_complete', [ 'Language_Pack_Upgrader', 'async_upgrade' ], 20 );
// Create the plugin upgrader with our custom skin. $installer = new PluginSilentUpgrader( new ConnectSkin() );
// Error check. if ( ! method_exists( $installer, 'install' ) ) { \wp_send_json_error( $error ); }
// Check license key. $key = \get_option( 'wpforms_connect', false );
if ( empty( $key ) ) { \wp_send_json_error( new WP_Error( '403', \esc_html__( 'No key provided.', 'wpforms-lite' ) ) ); }
$installer->install( $post_url ); // phpcs:ignore
// Flush the cache and return the newly installed plugin basename. \wp_cache_flush();
$plugin_basename = $installer->plugin_info();
if ( $plugin_basename ) {
// Deactivate the lite version first. $plugin = \plugin_basename( WPFORMS_PLUGIN_FILE );
\deactivate_plugins( $plugin );
do_action( 'wpforms_plugin_deactivated', $plugin );
// Activate the plugin silently. $activated = \activate_plugin( $plugin_basename, '', false, true );
if ( ! \is_wp_error( $activated ) ) { \add_option( 'wpforms_install', 1 ); \wp_send_json_success( \esc_html__( 'Plugin installed & activated.', 'wpforms-lite' ) ); } else { // Reactivate the lite plugin if pro activation failed. \activate_plugin( \plugin_basename( WPFORMS_PLUGIN_FILE ), '', false, true ); \wp_send_json_error( \esc_html__( 'Pro version installed but needs to be activated on the Plugins page inside your WordPress admin.', 'wpforms-lite' ) ); } }
\wp_send_json_error( $error ); } }
|