PluginProbe
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 0.0.12
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v0.0.12
2.12.8 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 All 98 releases
sureforms / inc / admin-ajax.php

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

371 lines 10.5 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 $suretrigger_connected = apply_filters( 'suretriggers_is_user_connected', '' );
157 return apply_filters(
158 'srfm_integrated_plugins',
159 [
160 [
161 'title' => __( 'SureTriggers', 'sureforms' ),
162 'subtitle' => __( 'Connect SureForms to hundreds of apps, CRMs and tools such as Slack, Mailchimp, etc.', 'sureforms' ),
163 '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' ),
164 'status' => self::get_plugin_status( 'suretriggers/suretriggers.php' ),
165 'slug' => 'suretriggers',
166 'path' => 'suretriggers/suretriggers.php',
167 'redirection' => admin_url( 'admin.php?page=suretriggers' ),
168 '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' ) : '' ),
169 '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' ) : '' ),
170 'connected' => $suretrigger_connected,
171 ],
172 ]
173 );
174 }
175
176 /**
177 * Encodes the given string with base64.
178 *
179 * @param string $logo contains svg's.
180 * @return string
181 */
182 public function encode_svg( $logo ) {
183 return 'data:image/svg+xml;base64,' . base64_encode( $logo ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
184 }
185 /**
186 * Get plugin status
187 *
188 * @since 0.0.1
189 *
190 * @param string $plugin_init_file Plugin init file.
191 * @return string
192 */
193 public static function get_plugin_status( $plugin_init_file ) {
194
195 $installed_plugins = get_plugins();
196
197 if ( ! isset( $installed_plugins[ $plugin_init_file ] ) ) {
198 return 'Install';
199 } elseif ( is_plugin_active( $plugin_init_file ) ) {
200 return 'Activated';
201 } else {
202 return 'Installed';
203 }
204 }
205
206 /**
207 * Generates data required for suretriggers integration
208 *
209 * @since 0.0.8
210 * @return void
211 */
212 public function generate_data_for_suretriggers_integration() {
213 if ( ! current_user_can( 'manage_options' ) ) {
214 wp_send_json_error( [ 'message' => 'You do not have permission to access this page.' ] );
215 }
216
217 if ( ! check_ajax_referer( 'suretriggers_nonce', 'security', false ) ) {
218 wp_send_json_error( [ 'message' => 'Invalid nonce.' ] );
219 }
220
221 if ( empty( $_POST['formId'] ) ) {
222 wp_send_json_error( [ 'message' => 'Form ID is required.' ] );
223 }
224
225 $suretriggers_data = get_option( 'suretrigger_options', [] );
226 if ( ! is_array( $suretriggers_data ) || empty( $suretriggers_data['secret_key'] ) || ! is_string( $suretriggers_data['secret_key'] ) ) {
227 wp_send_json_error(
228 [
229 'code' => 'invalid_secret_key',
230 'message' => 'SureTriggers is not configured properly.',
231 ]
232 );
233 }
234
235 $form_id = Helper::get_integer_value( sanitize_text_field( wp_unslash( $_POST['formId'] ) ) );
236 $form = get_post( $form_id );
237
238 if ( is_null( $form ) || SRFM_FORMS_POST_TYPE !== $form->post_type ) {
239 wp_send_json_error( [ 'message' => __( 'Invalid form ID.', 'sureforms' ) ] );
240 }
241
242 $form_name = ! empty( $form->post_title ) ? $form->post_title : 'SureForms id: ' . $form_id;
243 $api_url = apply_filters( 'suretriggers_get_iframe_url', SRFM_SURETRIGGERS_INTERGATION_BASE_URL );
244
245 // This is the format of data required by SureTriggers for adding iframe in target id.
246 $body = [
247 'client_id' => 'SureForms',
248 'st_embed_url' => $api_url,
249 'embedded_identifier' => $form_id,
250 'target' => 'suretriggers-iframe-wrapper', // div where we want SureTriggers to add iframe should have this target id.
251 'event' => [
252 'label' => 'Form Submitted',
253 'value' => 'sureforms_form_submitted',
254 'description' => 'Runs when a form is submitted',
255 ],
256 'summary' => $form_name,
257 'selected_options' => [
258 'form_id' => [
259 'value' => $form_id,
260 'label' => $form_name,
261 ],
262 ],
263 'integration' => 'SureForms',
264 'sample_response' => [
265 'form_id' => $form_id,
266 'to_emails' => [
267 '[email protected]',
268 ],
269 'form_name' => $form_name,
270 'data' => $this->get_form_fields( $form_id ),
271 ],
272 ];
273
274 wp_send_json_success(
275 [
276 'message' => 'success',
277 'data' => $body,
278 ]
279 );
280 }
281
282 /**
283 * This function populates data for particular form.
284 *
285 * @param int $form_id Form ID.
286 * @since 0.0.8
287 * @return array<mixed>
288 */
289 public function get_form_fields( $form_id ) {
290 if ( empty( $form_id ) || ! is_int( $form_id ) ) {
291 return [];
292 }
293
294 if ( SRFM_FORMS_POST_TYPE !== get_post_type( $form_id ) ) {
295 return [];
296 }
297
298 $post = get_post( $form_id );
299
300 if ( is_null( $post ) ) {
301 return [];
302 }
303
304 $blocks = parse_blocks( $post->post_content );
305
306 if ( empty( $blocks ) ) {
307 return [];
308 }
309
310 $data = [];
311
312 foreach ( $blocks as $block ) {
313 if ( ! empty( $block['blockName'] ) && 0 === strpos( $block['blockName'], 'srfm/' ) ) {
314 if ( ! empty( $block['attrs']['slug'] ) ) {
315 $data[ $block['attrs']['slug'] ] = $this->get_sample_data( $block['blockName'] );
316 }
317 }
318 }
319
320 if ( empty( $data ) ) {
321 return [];
322 }
323
324 return $data;
325
326 }
327
328 /**
329 * Returns sample data for a block.
330 *
331 * @param string $block_name Block name.
332 * @since 0.0.8
333 * @return mixed
334 */
335 public function get_sample_data( $block_name ) {
336 if ( empty( $block_name ) ) {
337 return 'Sample data';
338 }
339
340 $dummy_data = [
341 'srfm/input' => 'Sample input data',
342 'srfm/email' => '[email protected]',
343 'srfm/textarea' => 'Sample textarea data',
344 'srfm/number' => 123,
345 'srfm/checkbox' => 'checkbox value',
346 'srfm/gdpr' => 'GDPR value',
347 'srfm/phone' => '1234567890',
348 'srfm/address' => 'Address data',
349 'srfm/address-compact' => 'Address data',
350 'srfm/dropdown' => 'Selected dropdown option',
351 'srfm/multi-choice' => 'Selected Multichoice option',
352 'srfm/radio' => 'Selected radio option',
353 'srfm/submit' => 'Submit',
354 'srfm/url' => 'https://example.com',
355 'srfm/date-time-picker' => '2022-01-01 12:00:00',
356 'srfm/hidden' => 'Hidden Value',
357 'srfm/slider' => 50,
358 'srfm/password' => 'DummyPassword123',
359 'srfm/rating' => 4,
360 'srfm/upload' => 'https://example.com/uploads/file.pdf',
361 ];
362
363 if ( ! empty( $dummy_data[ $block_name ] ) ) {
364 return $dummy_data[ $block_name ];
365 } else {
366 return 'Sample data';
367 }
368 }
369 }
370
371