class-ajax-functions.php
2 days ago
class-define-constant.php
2 days ago
class-functions.php
2 days ago
do-it.php
2 days ago
inject-script.php
2 days ago
class-ajax-functions.php
104 lines
| 1 | <?php |
| 2 | if (! defined('ABSPATH')) { |
| 3 | exit; // Exit if accessed directly |
| 4 | } |
| 5 | function avcf_deactivate_collab() { |
| 6 | $function = new AVCF_Functions(); |
| 7 | if (! $function->avcf_validate_nonce() || ! is_user_logged_in()) { |
| 8 | wp_send_json_error(['message' => 'Unauthorized access.'], 403); |
| 9 | exit; |
| 10 | } |
| 11 | |
| 12 | if ( ! current_user_can('manage_options') ) { |
| 13 | wp_send_json_error(['message' => 'Forbidden.'], 403); |
| 14 | } |
| 15 | |
| 16 | $function->avcf_update_settings('avc_collab_active', 'no'); |
| 17 | exit; |
| 18 | } |
| 19 | add_action('wp_ajax_avcf_deactivate_collab', 'avcf_deactivate_collab'); |
| 20 | |
| 21 | function avcf_user_consent() { |
| 22 | $function = new AVCF_Functions(); |
| 23 | if (! $function->avcf_validate_nonce() || ! is_user_logged_in()) { |
| 24 | wp_send_json_error(['message' => 'Unauthorized access.'], 403); |
| 25 | exit; |
| 26 | } |
| 27 | |
| 28 | $site_id = $function->avcf_get_setting_data('avc_site_id'); |
| 29 | $email = $function->avcf_get_user_detail('email'); |
| 30 | $fname = $function->avcf_get_user_detail('first_name'); |
| 31 | $lname = $function->avcf_get_user_detail('last_name'); |
| 32 | |
| 33 | if (is_wp_error($email)) { |
| 34 | wp_send_json_error(['message' => $email->get_error_message()], 400); |
| 35 | exit; |
| 36 | } |
| 37 | |
| 38 | $payload = [ |
| 39 | 'site_id' => $site_id, |
| 40 | 'email' => $email, |
| 41 | 'name' => $fname . ' ' . $lname, |
| 42 | 'source' => 'wordpress', |
| 43 | 'apikey' => 'ab497511-9293-4e36-8e8b-fe3fdf0c4086', |
| 44 | 'apiurl' => AVCF_CRM_API . 'wp-api/user/auth', |
| 45 | ]; |
| 46 | |
| 47 | wp_send_json_success($payload); |
| 48 | exit; |
| 49 | } |
| 50 | add_action('wp_ajax_avcf_user_consent', 'avcf_user_consent'); |
| 51 | |
| 52 | function avcf_set_user_consent_status() { |
| 53 | $function = new AVCF_Functions(); |
| 54 | if (! $function->avcf_validate_nonce() || ! is_user_logged_in()) { |
| 55 | wp_send_json_error(['message' => 'Unauthorized access.'], 403); |
| 56 | exit; |
| 57 | } |
| 58 | |
| 59 | $user_id = $function->avcf_get_user_detail('id'); |
| 60 | update_user_meta($user_id, 'avc_consent_status', true); |
| 61 | |
| 62 | wp_send_json_success(['message' => 'Consent status updated.']); |
| 63 | } |
| 64 | add_action('wp_ajax_avcf_set_user_consent_status', 'avcf_set_user_consent_status'); |
| 65 | |
| 66 | function avcf_save_avcf_settings() { |
| 67 | if ( ! is_user_logged_in() ) { |
| 68 | wp_send_json_error( [ 'message' => 'Authentication required.' ], 401 ); |
| 69 | } |
| 70 | |
| 71 | if ( ! current_user_can( 'manage_options' ) ) { |
| 72 | wp_send_json_error( [ 'message' => 'Unauthorized.' ], 403 ); |
| 73 | } |
| 74 | |
| 75 | $nonce = ''; |
| 76 | if ( isset($_SERVER['HTTP_X_AVC_NONCE']) ) { |
| 77 | $nonce = sanitize_text_field( wp_unslash($_SERVER['HTTP_X_AVC_NONCE']) ); |
| 78 | } |
| 79 | |
| 80 | if ( ! wp_verify_nonce( $nonce, 'avc-script-nonce' ) ) { |
| 81 | wp_send_json_error(['message' => 'Invalid nonce.'], 403); |
| 82 | } |
| 83 | |
| 84 | $function = new AVCF_Functions(); |
| 85 | $data = json_decode(file_get_contents('php://input'), true); |
| 86 | |
| 87 | $allowed_fields = ['avc_selected_role', 'avc_website_developer', 'avc_enable_doit']; |
| 88 | |
| 89 | foreach ($data as $key => $value) { |
| 90 | if (! in_array($key, $allowed_fields, true)) { |
| 91 | $key = sanitize_text_field($key); |
| 92 | wp_send_json_error(['message' => 'Invalid setting field: ' . esc_html($key)]); |
| 93 | } |
| 94 | |
| 95 | if ($key === 'avc_enable_doit') { |
| 96 | $value = ! empty($value) ? '1' : '0'; |
| 97 | } |
| 98 | |
| 99 | $function->avcf_update_settings($key, $value); |
| 100 | } |
| 101 | |
| 102 | wp_send_json_success(['message' => 'Settings saved']); |
| 103 | } |
| 104 | add_action('wp_ajax_avcf_save_settings', 'avcf_save_avcf_settings'); |