PluginProbe
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 0.0.9
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v0.0.9
2.12.6 2.12.5 2.12.4 2.12.3 2.12.2 2.12.1 2.12.0 2.11.1 2.11.0 2.10.1 2.10.0 2.9.1 2.9.0 2.8.2 2.8.1 2.7.0 2.7.1 2.8.0 trunk 0.0.10 0.0.11 0.0.12 0.0.13 0.0.2 0.0.3 All 96 releases
sureforms / inc / admin-ajax.php

admin-ajax.php in SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz 0.0.9, at inc/admin-ajax.php

385 lines 11.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Sureforms Admin Ajax Class.
4 *
5 * Class file for public functions.
6 *
7 * @package sureforms
8 */
9
10 namespace SRFM\Inc;
11
12 use SRFM\Inc\Traits\Get_Instance;
13 use SRFM\Inc\Helper;
14
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit; // Exit if accessed directly.
17 }
18
19 if ( ! function_exists( 'get_plugins' ) ) {
20 require_once ABSPATH . 'wp-admin/includes/plugin.php';
21 }
22
23 /**
24 * Public Class
25 *
26 * @since 0.0.1
27 */
28 class Admin_Ajax {
29
30 use Get_Instance;
31
32 /**
33 * Constructor
34 *
35 * @since 0.0.1
36 */
37 public function __construct() {
38 add_action( 'wp_ajax_sureforms_recommended_plugin_activate', [ $this, 'required_plugin_activate' ] );
39 add_action( 'wp_ajax_sureforms_recommended_plugin_install', 'wp_ajax_install_plugin' );
40 add_action( 'wp_ajax_sureforms_integration', [ $this, 'generate_data_for_suretriggers_integration' ] );
41
42 add_filter( SRFM_SLUG . '_admin_filter', [ $this, 'localize_script_integration' ] );
43 }
44
45
46 /**
47 * Required Plugin Activate
48 *
49 * @return void
50 * @since 0.0.1
51 */
52 public function required_plugin_activate() {
53
54 $response_data = [ 'message' => $this->get_error_msg( 'permission' ) ];
55
56 if ( ! current_user_can( 'manage_options' ) ) {
57 wp_send_json_error( $response_data );
58 }
59
60 if ( empty( $_POST ) ) {
61 $response_data = [ 'message' => $this->get_error_msg( 'invalid' ) ];
62 wp_send_json_error( $response_data );
63 }
64
65 /**
66 * Nonce verification.
67 */
68 if ( ! check_ajax_referer( 'sf_plugin_manager_nonce', 'security', false ) ) {
69 $response_data = [ 'message' => $this->get_error_msg( 'nonce' ) ];
70 wp_send_json_error( $response_data );
71 }
72
73 if ( ! current_user_can( 'install_plugins' ) || ! isset( $_POST['init'] ) || ! sanitize_text_field( wp_unslash( $_POST['init'] ) ) ) {
74 wp_send_json_error(
75 [
76 'success' => false,
77 'message' => __( 'No plugin specified', 'sureforms' ),
78 ]
79 );
80 }
81
82 $plugin_init = ( isset( $_POST['init'] ) ) ? sanitize_text_field( wp_unslash( $_POST['init'] ) ) : '';
83
84 $activate = activate_plugin( $plugin_init, '', false, true );
85
86 if ( is_wp_error( $activate ) ) {
87 wp_send_json_error(
88 [
89 'success' => false,
90 'message' => $activate->get_error_message(),
91 ]
92 );
93 }
94
95 wp_send_json_success(
96 [
97 'success' => true,
98 'message' => __( 'Plugin Successfully Activated', 'sureforms' ),
99 ]
100 );
101 }
102
103 /**
104 * Get ajax error message.
105 *
106 * @param string $type Message type.
107 * @return string
108 * @since 0.0.2
109 */
110 public function get_error_msg( $type ) {
111
112 if ( ! isset( $this->errors[ $type ] ) ) {
113 $type = 'default';
114 }
115 if ( ! isset( $this->errors ) ) {
116 return '';
117 }
118 return $this->errors[ $type ];
119 }
120
121 /**
122 * Localize the variables required for integration plugins.
123 *
124 * @param array<mixed> $values localized values.
125 * @return array<mixed>
126 * @since 0.0.1
127 */
128 public function localize_script_integration( $values ) {
129 return array_merge(
130 $values,
131 [
132 'ajax_url' => admin_url( 'admin-ajax.php' ),
133 'sfPluginManagerNonce' => wp_create_nonce( 'sf_plugin_manager_nonce' ),
134 'plugin_installer_nonce' => wp_create_nonce( 'updates' ),
135 'plugin_activating_text' => __( 'Activating...', 'sureforms' ),
136 'plugin_activated_text' => __( 'Activated', 'sureforms' ),
137 'plugin_activate_text' => __( 'Activate', 'sureforms' ),
138 'integrations' => self::sureforms_get_integration(),
139 'plugin_installing_text' => __( 'Installing...', 'sureforms' ),
140 'plugin_installed_text' => __( 'Installed', 'sureforms' ),
141 'isRTL' => is_rtl(),
142 'current_screen_id' => get_current_screen() ? get_current_screen()->id : '',
143 'form_id' => get_post() ? get_post()->ID : '',
144 'suretriggers_nonce' => wp_create_nonce( 'suretriggers_nonce' ),
145 ]
146 );
147 }
148
149 /**
150 * Get sureforms recommended integrations.
151 *
152 * @since 0.0.1
153 * @return array<mixed>
154 */
155 public function sureforms_get_integration() {
156 $sc_api_token = get_option( 'sc_api_token', '' );
157 $suretriggers_data = get_option( 'suretrigger_options', [] );
158 $suretrigger_connected = ( ! is_array( $suretriggers_data ) || empty( $suretriggers_data['secret_key'] ) || ! is_string( $suretriggers_data['secret_key'] ) ) ? false : true;
159
160 return apply_filters(
161 'srfm_integrated_plugins',
162 [
163 [
164 'title' => __( 'SureTriggers', 'sureforms' ),
165 'subtitle' => __( 'Connect SureForms to over 600 apps, CRMs and tools such as Slack, Mailchimp, etc.', 'sureforms' ),
166 'description' => __( 'SureTriggers is a powerful automation platform that helps you connect your various plugins and apps together. It allows you to automate repetitive tasks, so you can focus on more important work.', 'sureforms' ),
167 'status' => self::get_plugin_status( 'suretriggers/suretriggers.php' ),
168 'slug' => 'suretriggers',
169 'path' => 'suretriggers/suretriggers.php',
170 'redirection' => admin_url( 'admin.php?page=suretriggers' ),
171 'logo' => self::encode_svg( is_string( file_get_contents( plugin_dir_path( SRFM_FILE ) . 'images/suretriggers.svg' ) ) ? file_get_contents( plugin_dir_path( SRFM_FILE ) . 'images/suretriggers.svg' ) : '' ),
172 'logo_full' => self::encode_svg( is_string( file_get_contents( plugin_dir_path( SRFM_FILE ) . 'images/suretriggers_full.svg' ) ) ? file_get_contents( plugin_dir_path( SRFM_FILE ) . 'images/suretriggers_full.svg' ) : '' ),
173 'connected' => $suretrigger_connected,
174 ],
175 ]
176 );
177 }
178
179 /**
180 * Encodes the given string with base64.
181 *
182 * @param string $logo contains svg's.
183 * @return string
184 */
185 public function encode_svg( $logo ) {
186 return 'data:image/svg+xml;base64,' . base64_encode( $logo ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
187 }
188 /**
189 * Get plugin status
190 *
191 * @since 0.0.1
192 *
193 * @param string $plugin_init_file Plugin init file.
194 * @return string
195 */
196 public static function get_plugin_status( $plugin_init_file ) {
197
198 $installed_plugins = get_plugins();
199
200 if ( ! isset( $installed_plugins[ $plugin_init_file ] ) ) {
201 return 'Install';
202 } elseif ( is_plugin_active( $plugin_init_file ) ) {
203 return 'Activated';
204 } else {
205 return 'Installed';
206 }
207 }
208
209 /**
210 * Generates data required for suretriggers integration
211 *
212 * @since 0.0.8
213 * @return void
214 */
215 public function generate_data_for_suretriggers_integration() {
216 if ( ! current_user_can( 'manage_options' ) ) {
217 wp_send_json_error( [ 'message' => 'You do not have permission to access this page.' ] );
218 }
219
220 if ( ! check_ajax_referer( 'suretriggers_nonce', 'security', false ) ) {
221 wp_send_json_error( [ 'message' => 'Invalid nonce.' ] );
222 }
223
224 if ( empty( $_POST['formId'] ) ) {
225 wp_send_json_error( [ 'message' => 'Form ID is required.' ] );
226 }
227
228 $suretriggers_data = get_option( 'suretrigger_options', [] );
229 if ( ! is_array( $suretriggers_data ) || empty( $suretriggers_data['secret_key'] ) || ! is_string( $suretriggers_data['secret_key'] ) ) {
230 wp_send_json_error(
231 [
232 'code' => 'invalid_secret_key',
233 'message' => 'SureTriggers is not configured properly.',
234 ]
235 );
236 }
237
238 $form_id = Helper::get_integer_value( sanitize_text_field( wp_unslash( $_POST['formId'] ) ) );
239 $form = get_post( $form_id );
240
241 if ( is_null( $form ) || SRFM_FORMS_POST_TYPE !== $form->post_type ) {
242 wp_send_json_error( [ 'message' => __( 'Invalid form ID.', 'sureforms' ) ] );
243 }
244
245 $secret_key = $suretriggers_data['secret_key'];
246 $base_url = get_site_url();
247 $form_name = ! empty( $form->post_title ) ? $form->post_title : 'SureForms id: ' . $form_id;
248
249 $api_url = add_query_arg(
250 [
251 'redirect_url' => SRFM_SURETRIGGERS_INTERGATION_BASE_URL . 'embed-login',
252 'st-code' => $secret_key,
253 'base_url' => $base_url,
254 'reset_url' => rtrim( base64_encode( wp_nonce_url( admin_url( 'admin.php?st-reset=true' ), 'st-reset-action' ) ), '=' ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode, required as we need to encode base url for suretriggers.
255 ],
256 SRFM_SURETRIGGERS_INTERGATION_BASE_URL . 'wp-login'
257 );
258
259 // This is the format of data required by SureTriggers for adding iframe in target id.
260 $body = [
261 'client_id' => 'SureForms',
262 'st_embed_url' => $api_url,
263 'embedded_identifier' => $form_id,
264 'target' => 'suretriggers-iframe-wrapper', // div where we want SureTriggers to add iframe should have this target id.
265 'event' => [
266 'label' => 'Form Submitted',
267 'value' => 'sureforms_form_submitted',
268 'description' => 'Runs when a form is submitted',
269 ],
270 'summary' => $form_name,
271 'selected_options' => [
272 'form_id' => [
273 'value' => $form_id,
274 'label' => $form_name,
275 ],
276 ],
277 'integration' => 'SureForms',
278 'sample_response' => [
279 'form_id' => $form_id,
280 'to_emails' => [
281 'dev-email@wpengine.local',
282 ],
283 'form_name' => $form_name,
284 'data' => $this->get_form_fields( $form_id ),
285 ],
286 ];
287
288 wp_send_json_success(
289 [
290 'message' => 'success',
291 'data' => $body,
292 ]
293 );
294 }
295
296 /**
297 * This function populates data for particular form.
298 *
299 * @param int $form_id Form ID.
300 * @since 0.0.8
301 * @return array<mixed>
302 */
303 public function get_form_fields( $form_id ) {
304 if ( empty( $form_id ) || ! is_int( $form_id ) ) {
305 return [];
306 }
307
308 if ( SRFM_FORMS_POST_TYPE !== get_post_type( $form_id ) ) {
309 return [];
310 }
311
312 $post = get_post( $form_id );
313
314 if ( is_null( $post ) ) {
315 return [];
316 }
317
318 $blocks = parse_blocks( $post->post_content );
319
320 if ( empty( $blocks ) ) {
321 return [];
322 }
323
324 $data = [];
325
326 foreach ( $blocks as $block ) {
327 if ( ! empty( $block['blockName'] ) && 0 === strpos( $block['blockName'], 'srfm/' ) ) {
328 if ( ! empty( $block['attrs']['slug'] ) ) {
329 $data[ $block['attrs']['slug'] ] = $this->get_sample_data( $block['blockName'] );
330 }
331 }
332 }
333
334 if ( empty( $data ) ) {
335 return [];
336 }
337
338 return $data;
339
340 }
341
342 /**
343 * Returns sample data for a block.
344 *
345 * @param string $block_name Block name.
346 * @since 0.0.8
347 * @return mixed
348 */
349 public function get_sample_data( $block_name ) {
350 if ( empty( $block_name ) ) {
351 return 'Sample data';
352 }
353
354 $dummy_data = [
355 'srfm/input' => 'Sample input data',
356 'srfm/email' => 'noreply@sureforms.com',
357 'srfm/textarea' => 'Sample textarea data',
358 'srfm/number' => 123,
359 'srfm/checkbox' => 'checkbox value',
360 'srfm/gdpr' => 'GDPR value',
361 'srfm/phone' => '1234567890',
362 'srfm/address' => 'Address data',
363 'srfm/address-compact' => 'Address data',
364 'srfm/dropdown' => 'Selected dropdown option',
365 'srfm/multi-choice' => 'Selected Multichoice option',
366 'srfm/radio' => 'Selected radio option',
367 'srfm/submit' => 'Submit',
368 'srfm/url' => 'https://example.com',
369 'srfm/date-time-picker' => '2022-01-01 12:00:00',
370 'srfm/hidden' => 'Hidden Value',
371 'srfm/number-slider' => 50,
372 'srfm/password' => 'DummyPassword123',
373 'srfm/rating' => 4,
374 'srfm/upload' => 'https://example.com/uploads/file.pdf',
375 ];
376
377 if ( ! empty( $dummy_data[ $block_name ] ) ) {
378 return $dummy_data[ $block_name ];
379 } else {
380 return 'Sample data';
381 }
382 }
383 }
384
385