PluginProbe
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 1.4.2
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v1.4.2
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
394 lines 11.6 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
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit; // Exit if accessed directly.
16 }
17
18 if ( ! function_exists( 'get_plugins' ) ) {
19 require_once ABSPATH . 'wp-admin/includes/plugin.php';
20 }
21
22 /**
23 * Public Class
24 *
25 * @since 0.0.1
26 */
27 class Admin_Ajax {
28 use Get_Instance;
29
30 /**
31 * Constructor
32 *
33 * @since 0.0.1
34 */
35 public function __construct() {
36 add_action( 'wp_ajax_sureforms_recommended_plugin_activate', [ $this, 'required_plugin_activate' ] );
37 add_action( 'wp_ajax_sureforms_recommended_plugin_install', 'wp_ajax_install_plugin' );
38 add_action( 'wp_ajax_sureforms_integration', [ $this, 'generate_data_for_suretriggers_integration' ] );
39
40 add_filter( SRFM_SLUG . '_admin_filter', [ $this, 'localize_script_integration' ] );
41
42 // adding support for rest-nonce endpoint. To regenerate latest nonce incase of form submission failure.
43 add_action( 'wp_ajax_rest-nonce', [ $this, 'print_rest_nonce' ], 999 );
44 add_action( 'wp_ajax_nopriv_rest-nonce', [ $this, 'print_rest_nonce' ], 999 );
45 }
46
47 /**
48 * Required Plugin Activate
49 *
50 * @return void
51 * @since 0.0.1
52 */
53 public function required_plugin_activate() {
54
55 $response_data = [ 'message' => $this->get_error_msg( 'permission' ) ];
56
57 if ( ! current_user_can( 'manage_options' ) ) {
58 wp_send_json_error( $response_data );
59 }
60
61 if ( empty( $_POST ) ) {
62 $response_data = [ 'message' => $this->get_error_msg( 'invalid' ) ];
63 wp_send_json_error( $response_data );
64 }
65
66 /**
67 * Nonce verification.
68 */
69 if ( ! check_ajax_referer( 'sf_plugin_manager_nonce', 'security', false ) ) {
70 $response_data = [ 'message' => $this->get_error_msg( 'nonce' ) ];
71 wp_send_json_error( $response_data );
72 }
73
74 if ( ! current_user_can( 'install_plugins' ) || ! isset( $_POST['init'] ) || ! sanitize_text_field( wp_unslash( $_POST['init'] ) ) ) {
75 wp_send_json_error(
76 [
77 'success' => false,
78 'message' => __( 'No plugin specified', 'sureforms' ),
79 ]
80 );
81 }
82
83 $plugin_init = isset( $_POST['init'] ) ? sanitize_text_field( wp_unslash( $_POST['init'] ) ) : '';
84
85 $activate = activate_plugin( $plugin_init, '', false, true );
86
87 if ( is_wp_error( $activate ) ) {
88 wp_send_json_error(
89 [
90 'success' => false,
91 'message' => $activate->get_error_message(),
92 ]
93 );
94 }
95
96 wp_send_json_success(
97 [
98 'success' => true,
99 'message' => __( 'Plugin Successfully Activated', 'sureforms' ),
100 ]
101 );
102 }
103
104 /**
105 * Get ajax error message.
106 *
107 * @param string $type Message type.
108 * @return string
109 * @since 0.0.2
110 */
111 public function get_error_msg( $type ) {
112
113 if ( ! isset( $this->errors[ $type ] ) ) {
114 $type = 'default';
115 }
116 if ( ! isset( $this->errors ) ) {
117 return '';
118 }
119 return $this->errors[ $type ];
120 }
121
122 /**
123 * Localize the variables required for integration plugins.
124 *
125 * @param array<mixed> $values localized values.
126 * @return array<mixed>
127 * @since 0.0.1
128 */
129 public function localize_script_integration( $values ) {
130 $is_screen_sureforms_menu = Helper::validate_request_context( 'sureforms_menu', 'page' );
131 return array_merge(
132 $values,
133 [
134 'ajax_url' => admin_url( 'admin-ajax.php' ),
135 'sfPluginManagerNonce' => wp_create_nonce( 'sf_plugin_manager_nonce' ),
136 'plugin_installer_nonce' => wp_create_nonce( 'updates' ),
137 'plugin_activating_text' => __( 'Activating...', 'sureforms' ),
138 'plugin_activated_text' => __( 'Activated', 'sureforms' ),
139 'plugin_activate_text' => __( 'Activate', 'sureforms' ),
140 'integrations' => self::sureforms_get_integration(),
141 'plugin_installing_text' => __( 'Installing...', 'sureforms' ),
142 'plugin_installed_text' => __( 'Installed', 'sureforms' ),
143 'isRTL' => is_rtl(),
144 'current_screen_id' => $is_screen_sureforms_menu ? 'sureforms_menu' : '',
145 'form_id' => get_post() ? get_post()->ID : '',
146 'suretriggers_nonce' => wp_create_nonce( 'suretriggers_nonce' ),
147 ]
148 );
149 }
150
151 /**
152 * Get sureforms recommended integrations.
153 *
154 * @since 0.0.1
155 * @return array<mixed>
156 */
157 public function sureforms_get_integration() {
158 $suretrigger_connected = apply_filters( 'suretriggers_is_user_connected', '' );
159 $logo = file_get_contents( plugin_dir_path( SRFM_FILE ) . 'images/suretriggers.svg' );
160 $logo_full = file_get_contents( plugin_dir_path( SRFM_FILE ) . 'images/suretriggers_full.svg' );
161 return apply_filters(
162 'srfm_integrated_plugins',
163 [
164 [
165 'title' => __( 'SureTriggers', 'sureforms' ),
166 'subtitle' => __( 'Connect SureForms to hundreds of apps, CRMs and tools such as Slack, Mailchimp, etc.', 'sureforms' ),
167 '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' ),
168 'status' => self::get_plugin_status( 'suretriggers/suretriggers.php' ),
169 'slug' => 'suretriggers',
170 'path' => 'suretriggers/suretriggers.php',
171 'redirection' => admin_url( 'admin.php?page=suretriggers' ),
172 'logo' => self::encode_svg( is_string( $logo ) ? $logo : '' ),
173 'logo_full' => self::encode_svg( is_string( $logo_full ) ? $logo_full : '' ),
174 'connected' => $suretrigger_connected,
175 ],
176 ]
177 );
178 }
179
180 /**
181 * Encodes the given string with base64.
182 *
183 * @param string $logo contains svg's.
184 * @return string
185 */
186 public function encode_svg( $logo ) {
187 return 'data:image/svg+xml;base64,' . base64_encode( $logo ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
188 }
189 /**
190 * Get plugin status
191 *
192 * @since 0.0.1
193 *
194 * @param string $plugin_init_file Plugin init file.
195 * @return string
196 */
197 public static function get_plugin_status( $plugin_init_file ) {
198
199 $installed_plugins = get_plugins();
200
201 if ( ! isset( $installed_plugins[ $plugin_init_file ] ) ) {
202 return 'Install';
203 }
204 if ( is_plugin_active( $plugin_init_file ) ) {
205 return 'Activated';
206 }
207 return 'Installed';
208 }
209
210 /**
211 * Generates data required for suretriggers integration
212 *
213 * @since 0.0.8
214 * @return void
215 */
216 public function generate_data_for_suretriggers_integration() {
217 if ( ! current_user_can( 'manage_options' ) ) {
218 wp_send_json_error( [ 'message' => __( 'You do not have permission to access this page.', 'sureforms' ) ] );
219 }
220
221 if ( ! check_ajax_referer( 'suretriggers_nonce', 'security', false ) ) {
222 wp_send_json_error( [ 'message' => __( 'Invalid nonce.', 'sureforms' ) ] );
223 }
224
225 if ( empty( $_POST['formId'] ) ) {
226 wp_send_json_error( [ 'message' => __( 'Form ID is required.', 'sureforms' ) ] );
227 }
228
229 if ( ! Helper::is_suretriggers_ready() ) {
230 wp_send_json_error(
231 [
232 'code' => 'invalid_secret_key',
233 'message' => __( 'SureTriggers is not configured properly.', 'sureforms' ),
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 // Translators: %s: Form ID.
246 $form_name = ! empty( $form->post_title ) ? $form->post_title : sprintf( __( 'SureForms id: %s', 'sureforms' ), $form_id );
247 $api_url = apply_filters( 'suretriggers_get_iframe_url', SRFM_SURETRIGGERS_INTEGRATION_BASE_URL );
248
249 // This is the format of data required by SureTriggers for adding iframe in target id.
250 $body = [
251 'client_id' => 'SureForms',
252 'st_embed_url' => $api_url,
253 'embedded_identifier' => $form_id,
254 'target' => 'suretriggers-iframe-wrapper', // div where we want SureTriggers to add iframe should have this target id.
255 'event' => [
256 'label' => __( 'Form Submitted', 'sureforms' ),
257 'value' => 'sureforms_form_submitted',
258 'description' => __( 'Runs when a form is submitted', 'sureforms' ),
259 ],
260 'summary' => $form_name,
261 'selected_options' => [
262 'form_id' => [
263 'value' => $form_id,
264 'label' => $form_name,
265 ],
266 ],
267 'integration' => 'SureForms',
268 'sample_response' => [
269 'form_id' => $form_id,
270 'to_emails' => [
271 'dev-email@wpengine.local',
272 ],
273 'form_name' => $form_name,
274 'data' => $this->get_form_fields( $form_id ),
275 ],
276 ];
277
278 // Adding entry_id in body sample response if do_not_store_entries is not enabled.
279 $compliance = get_post_meta( $form_id, '_srfm_compliance', true );
280 $do_not_store_entries = is_array( $compliance ) && isset( $compliance[0]['do_not_store_entries'] )
281 ? $compliance[0]['do_not_store_entries']
282 : null;
283
284 if ( ! $do_not_store_entries ) {
285 $body['sample_response']['entry_id'] = 12;
286 }
287
288 wp_send_json_success(
289 [
290 'message' => 'success',
291 'data' => apply_filters( 'srfm_suretriggers_integration_data_filter', $body, $form_id ),
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 * Returns sample data for a block.
343 *
344 * @param string $block_name Block name.
345 * @since 0.0.8
346 * @return mixed
347 */
348 public function get_sample_data( $block_name ) {
349 if ( empty( $block_name ) ) {
350 return __( 'Sample data', 'sureforms' );
351 }
352
353 $dummy_data = [
354 'srfm/input' => __( 'Sample input data', 'sureforms' ),
355 'srfm/email' => 'noreply@sureforms.com',
356 'srfm/textarea' => __( 'Sample textarea data', 'sureforms' ),
357 'srfm/number' => 123,
358 'srfm/checkbox' => 'checkbox value',
359 'srfm/gdpr' => 'GDPR value',
360 'srfm/phone' => '1234567890',
361 'srfm/address' => __( 'Address data', 'sureforms' ),
362 'srfm/address-compact' => __( 'Address data', 'sureforms' ),
363 'srfm/dropdown' => __( 'Selected dropdown option', 'sureforms' ),
364 'srfm/multi-choice' => __( 'Selected Multichoice option', 'sureforms' ),
365 'srfm/radio' => __( 'Selected radio option', 'sureforms' ),
366 'srfm/submit' => __( 'Submit', 'sureforms' ),
367 'srfm/url' => 'https://example.com',
368 'srfm/date-time-picker' => '2022-01-01 12:00:00',
369 'srfm/hidden' => __( 'Hidden Value', 'sureforms' ),
370 'srfm/slider' => 50,
371 'srfm/password' => 'DummyPassword123',
372 'srfm/rating' => 4,
373 'srfm/upload' => 'https://example.com/uploads/file.pdf',
374 ];
375
376 if ( ! empty( $dummy_data[ $block_name ] ) ) {
377 return $dummy_data[ $block_name ];
378 }
379 return __( 'Sample data', 'sureforms' );
380 }
381
382 /**
383 * This function will echo wp_rest nonce
384 * was required to provide nonce for fallback of wp.apiFetch
385 *
386 * @since 1.2.4
387 * @return void
388 */
389 public function print_rest_nonce() {
390 echo esc_js( wp_create_nonce( 'wp_rest' ) );
391 exit;
392 }
393 }
394