PluginProbe
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 0.0.13
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v0.0.13
2.12.7 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 All 97 releases
sureforms / inc / admin-ajax.php

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

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