| 1 |
<?php |
| 2 |
|
| 3 |
namespace ABlocks\Ajax; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
use ABlocks\Classes\AbstractAjaxHandler; |
| 10 |
use ABlocks\Helper; |
| 11 |
use ABlocks\Classes\Sanitizer; |
| 12 |
|
| 13 |
use ABlocks\Blocks\FormBuilder\ValidateFormData; |
| 14 |
use ABlocks\Blocks\FormBuilder\Actions\SaveFormData; |
| 15 |
use ABlocks\Blocks\FormBuilder\Actions\SendEmail; |
| 16 |
use ABlocks\Blocks\FormBuilder\Actions\SendEmails; |
| 17 |
use ABlocks\Blocks\FormBuilder\Actions\Subscribe; |
| 18 |
|
| 19 |
use ABlocks\Blocks\FormBuilder\Subscribe\Mailchimp; |
| 20 |
use Exception; |
| 21 |
|
| 22 |
class FormBuilder extends AbstractAjaxHandler { |
| 23 |
public function __construct() { |
| 24 |
$this->actions = array( |
| 25 |
'form_builder_action_setting_data' => array( |
| 26 |
'callback' => array( $this, 'action_setting_data' ), |
| 27 |
'capability' => 'edit_posts', |
| 28 |
'fields' => array( |
| 29 |
'type' => 'string', |
| 30 |
'api' => 'string', |
| 31 |
'list_id' => 'string', |
| 32 |
) |
| 33 |
), |
| 34 |
'registration_form_setting_data' => array( |
| 35 |
'callback' => array( $this, 'registration_form_setting_data' ), |
| 36 |
'capability' => 'edit_posts' |
| 37 |
), |
| 38 |
); |
| 39 |
|
| 40 |
} |
| 41 |
|
| 42 |
public function prepare_res( array $data, array $block_data, string $redirect_url ) : array { |
| 43 |
$formType = $block_data['parentAttributes']['formType'] ?? ''; |
| 44 |
|
| 45 |
// confirmationType based on formType |
| 46 |
if ( $formType == 'login' ) { |
| 47 |
$confirmationType = ( $block_data['parentAttributes']['loginRedirect'] ?? false ) ? 'redirect' : 'success'; |
| 48 |
} elseif ( $formType == 'registration' ) { |
| 49 |
$confirmationType = ( $block_data['parentAttributes']['registerRedirect'] ?? false ) ? 'redirect' : 'success'; |
| 50 |
} else { |
| 51 |
$confirmationType = $block_data['parentAttributes']['confirmationType'] ?? 'success'; |
| 52 |
} |
| 53 |
|
| 54 |
return array_merge( |
| 55 |
$data, |
| 56 |
[ |
| 57 |
'afterFormSubmission' => $block_data['parentAttributes']['afterFormSubmission'] ?? 'reset', |
| 58 |
'confirmationType' => $confirmationType, |
| 59 |
'confirmationNotice' => $block_data['parentAttributes']['confirmationNotice'] ?? $data['message'] ?? __( 'Form successfully submitted!', 'ablocks' ), |
| 60 |
'redirect_url' => esc_url( $redirect_url ), |
| 61 |
'no_follow' => $block_data['parentAttributes']['link']['noFollow'] ?? '', |
| 62 |
'link_target' => $block_data['parentAttributes']['link']['linkTarget'] ?? '', |
| 63 |
'formType' => $formType, |
| 64 |
] |
| 65 |
); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public function registration_form_setting_data( $payload ) { |
| 71 |
$roles = [ |
| 72 |
'default' => __( 'Default', 'ablocks' ) |
| 73 |
]; |
| 74 |
|
| 75 |
if ( current_user_can( 'manage_options' ) ) { |
| 76 |
foreach ( wp_roles()->roles ?? [] as $slug => ['name' => $name] ) { |
| 77 |
$roles[ $slug ] = $name; |
| 78 |
} |
| 79 |
} |
| 80 |
wp_send_json_success( $roles ); |
| 81 |
} |
| 82 |
|
| 83 |
private function validate_registration_data( $payload ) { |
| 84 |
if ( empty( $payload['username'] ) || ! validate_username( $payload['username'] ) ) { |
| 85 |
return 'Invalid username.'; |
| 86 |
} elseif ( username_exists( $payload['username'] ) ) { |
| 87 |
return 'Username already exists.'; |
| 88 |
} |
| 89 |
|
| 90 |
if ( empty( $payload['email'] ) || ! is_email( $payload['email'] ) ) { |
| 91 |
return 'Invalid email address.'; |
| 92 |
} elseif ( email_exists( $payload['email'] ) ) { |
| 93 |
return 'Email already exists.'; |
| 94 |
} |
| 95 |
|
| 96 |
if ( empty( $payload['password'] ) || strlen( $payload['password'] ) < 6 ) { |
| 97 |
return 'Password must be at least 6 characters.'; |
| 98 |
} elseif ( $payload['password'] !== $payload['confirm_password'] ) { |
| 99 |
return 'Passwords do not match.'; |
| 100 |
} |
| 101 |
|
| 102 |
return null; |
| 103 |
} |
| 104 |
|
| 105 |
private function get_custom_fields( $form_data ) { |
| 106 |
$reserved_keys = [ 'username', 'email', 'password', 'confirm_password', 'current_post_id', 'block_id', 'security', 'action' ]; |
| 107 |
return array_diff_key( $form_data, array_flip( $reserved_keys ) ); |
| 108 |
} |
| 109 |
|
| 110 |
public function action_setting_data( $payload ) { |
| 111 |
$type = $payload['type'] ?? ''; |
| 112 |
$settings_key = $type . '_api_key'; |
| 113 |
$api = $payload['api'] ?? $GLOBALS['ablocks_settings']->{$settings_key} ?? ''; |
| 114 |
$list_id = $payload['list_id'] ?? ''; |
| 115 |
|
| 116 |
$api_key = $api; |
| 117 |
|
| 118 |
if ( empty( $api_key ) ) { |
| 119 |
wp_send_json_error( [ 'message' => __( 'api key is required', 'ablocks' ) ] ); |
| 120 |
} |
| 121 |
|
| 122 |
$action_config = apply_filters('ablocks/form_builder/action_config', [ |
| 123 |
'mailchimp' => function( $api_key ) use ( $list_id ) { |
| 124 |
if ( ! class_exists( Mailchimp::class ) ) { |
| 125 |
return; |
| 126 |
} |
| 127 |
|
| 128 |
if ( empty( $list_id ) ) { |
| 129 |
$data = ( new Mailchimp( |
| 130 |
$api_key, |
| 131 |
'', |
| 132 |
'' |
| 133 |
) ) |
| 134 |
->get_lists(); |
| 135 |
wp_send_json_success( $data ); |
| 136 |
} else { |
| 137 |
|
| 138 |
$data = ( new Mailchimp( |
| 139 |
$api_key, |
| 140 |
'', |
| 141 |
'' |
| 142 |
) ) |
| 143 |
->get_groups( $list_id ); |
| 144 |
} |
| 145 |
|
| 146 |
wp_send_json_success( $data ); |
| 147 |
} |
| 148 |
]); |
| 149 |
|
| 150 |
if ( ! array_key_exists( $type, $action_config ) ) { |
| 151 |
wp_send_json_error( [ 'message' => __( 'Not a valid type', 'ablocks' ) ] ); |
| 152 |
} |
| 153 |
|
| 154 |
try { |
| 155 |
if ( |
| 156 |
array_key_exists( $type, $action_config ) && |
| 157 |
is_callable( $action_config[ $type ] ) |
| 158 |
) { |
| 159 |
$action_config[ $type ]( $api_key, $payload ); |
| 160 |
} |
| 161 |
} catch ( Exception $e ) { |
| 162 |
wp_send_json_error( [ 'message' => esc_html( $e->getMessage() ) ], 400 ); |
| 163 |
} |
| 164 |
|
| 165 |
wp_send_json_error( [ 'message' => __( 'Unspecified service type', 'ablocks' ) ], 400 ); |
| 166 |
} |
| 167 |
} |
| 168 |
|