admin-ajax.php
| 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.', 'sureforms' ) ] ); |
| 215 | } |
| 216 | |
| 217 | if ( ! check_ajax_referer( 'suretriggers_nonce', 'security', false ) ) { |
| 218 | wp_send_json_error( [ 'message' => __( 'Invalid nonce.', 'sureforms' ) ] ); |
| 219 | } |
| 220 | |
| 221 | if ( empty( $_POST['formId'] ) ) { |
| 222 | wp_send_json_error( [ 'message' => __( 'Form ID is required.', 'sureforms' ) ] ); |
| 223 | } |
| 224 | |
| 225 | if ( ! Helper::is_suretriggers_ready() ) { |
| 226 | wp_send_json_error( |
| 227 | [ |
| 228 | 'code' => 'invalid_secret_key', |
| 229 | 'message' => __( 'SureTriggers is not configured properly.', 'sureforms' ), |
| 230 | ] |
| 231 | ); |
| 232 | } |
| 233 | |
| 234 | $form_id = Helper::get_integer_value( sanitize_text_field( wp_unslash( $_POST['formId'] ) ) ); |
| 235 | $form = get_post( $form_id ); |
| 236 | |
| 237 | if ( is_null( $form ) || SRFM_FORMS_POST_TYPE !== $form->post_type ) { |
| 238 | wp_send_json_error( [ 'message' => __( 'Invalid form ID.', 'sureforms' ) ] ); |
| 239 | } |
| 240 | |
| 241 | $form_name = ! empty( $form->post_title ) ? $form->post_title : 'SureForms id: ' . $form_id; |
| 242 | $api_url = apply_filters( 'suretriggers_get_iframe_url', SRFM_SURETRIGGERS_INTEGRATION_BASE_URL ); |
| 243 | |
| 244 | // This is the format of data required by SureTriggers for adding iframe in target id. |
| 245 | $body = [ |
| 246 | 'client_id' => 'SureForms', |
| 247 | 'st_embed_url' => $api_url, |
| 248 | 'embedded_identifier' => $form_id, |
| 249 | 'target' => 'suretriggers-iframe-wrapper', // div where we want SureTriggers to add iframe should have this target id. |
| 250 | 'event' => [ |
| 251 | 'label' => 'Form Submitted', |
| 252 | 'value' => 'sureforms_form_submitted', |
| 253 | 'description' => 'Runs when a form is submitted', |
| 254 | ], |
| 255 | 'summary' => $form_name, |
| 256 | 'selected_options' => [ |
| 257 | 'form_id' => [ |
| 258 | 'value' => $form_id, |
| 259 | 'label' => $form_name, |
| 260 | ], |
| 261 | ], |
| 262 | 'integration' => 'SureForms', |
| 263 | 'sample_response' => [ |
| 264 | 'form_id' => $form_id, |
| 265 | 'to_emails' => [ |
| 266 | 'dev-email@wpengine.local', |
| 267 | ], |
| 268 | 'form_name' => $form_name, |
| 269 | 'data' => $this->get_form_fields( $form_id ), |
| 270 | ], |
| 271 | ]; |
| 272 | |
| 273 | wp_send_json_success( |
| 274 | [ |
| 275 | 'message' => 'success', |
| 276 | 'data' => apply_filters( 'srfm_suretriggers_integration_data_filter', $body, $form_id ), |
| 277 | ] |
| 278 | ); |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * This function populates data for particular form. |
| 283 | * |
| 284 | * @param int $form_id Form ID. |
| 285 | * @since 0.0.8 |
| 286 | * @return array<mixed> |
| 287 | */ |
| 288 | public function get_form_fields( $form_id ) { |
| 289 | if ( empty( $form_id ) || ! is_int( $form_id ) ) { |
| 290 | return []; |
| 291 | } |
| 292 | |
| 293 | if ( SRFM_FORMS_POST_TYPE !== get_post_type( $form_id ) ) { |
| 294 | return []; |
| 295 | } |
| 296 | |
| 297 | $post = get_post( $form_id ); |
| 298 | |
| 299 | if ( is_null( $post ) ) { |
| 300 | return []; |
| 301 | } |
| 302 | |
| 303 | $blocks = parse_blocks( $post->post_content ); |
| 304 | |
| 305 | if ( empty( $blocks ) ) { |
| 306 | return []; |
| 307 | } |
| 308 | |
| 309 | $data = []; |
| 310 | |
| 311 | foreach ( $blocks as $block ) { |
| 312 | if ( ! empty( $block['blockName'] ) && 0 === strpos( $block['blockName'], 'srfm/' ) ) { |
| 313 | if ( ! empty( $block['attrs']['slug'] ) ) { |
| 314 | $data[ $block['attrs']['slug'] ] = $this->get_sample_data( $block['blockName'] ); |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | if ( empty( $data ) ) { |
| 320 | return []; |
| 321 | } |
| 322 | |
| 323 | return $data; |
| 324 | |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Returns sample data for a block. |
| 329 | * |
| 330 | * @param string $block_name Block name. |
| 331 | * @since 0.0.8 |
| 332 | * @return mixed |
| 333 | */ |
| 334 | public function get_sample_data( $block_name ) { |
| 335 | if ( empty( $block_name ) ) { |
| 336 | return 'Sample data'; |
| 337 | } |
| 338 | |
| 339 | $dummy_data = [ |
| 340 | 'srfm/input' => 'Sample input data', |
| 341 | 'srfm/email' => 'noreply@sureforms.com', |
| 342 | 'srfm/textarea' => 'Sample textarea data', |
| 343 | 'srfm/number' => 123, |
| 344 | 'srfm/checkbox' => 'checkbox value', |
| 345 | 'srfm/gdpr' => 'GDPR value', |
| 346 | 'srfm/phone' => '1234567890', |
| 347 | 'srfm/address' => 'Address data', |
| 348 | 'srfm/address-compact' => 'Address data', |
| 349 | 'srfm/dropdown' => 'Selected dropdown option', |
| 350 | 'srfm/multi-choice' => 'Selected Multichoice option', |
| 351 | 'srfm/radio' => 'Selected radio option', |
| 352 | 'srfm/submit' => 'Submit', |
| 353 | 'srfm/url' => 'https://example.com', |
| 354 | 'srfm/date-time-picker' => '2022-01-01 12:00:00', |
| 355 | 'srfm/hidden' => 'Hidden Value', |
| 356 | 'srfm/slider' => 50, |
| 357 | 'srfm/password' => 'DummyPassword123', |
| 358 | 'srfm/rating' => 4, |
| 359 | 'srfm/upload' => 'https://example.com/uploads/file.pdf', |
| 360 | ]; |
| 361 | |
| 362 | if ( ! empty( $dummy_data[ $block_name ] ) ) { |
| 363 | return $dummy_data[ $block_name ]; |
| 364 | } else { |
| 365 | return 'Sample data'; |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | |
| 370 |