| 1 |
<?php |
| 2 |
|
| 3 |
add_action('wp_ajax_linguise_update_config_iframe', function () { |
| 4 |
// nonce |
| 5 |
if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'linguise_update_config_iframe')) { |
| 6 |
// response with error |
| 7 |
wp_send_json_error(array( |
| 8 |
'message' => __('Nonce verification failed', 'linguise') |
| 9 |
)); |
| 10 |
return; |
| 11 |
} |
| 12 |
|
| 13 |
// check if user is logged in |
| 14 |
if (!is_user_logged_in()) { |
| 15 |
// response with error |
| 16 |
wp_send_json_error(array( |
| 17 |
'message' => __('User is not logged in', 'linguise') |
| 18 |
)); |
| 19 |
return; |
| 20 |
} |
| 21 |
// check if user has permission to manage options |
| 22 |
if (!current_user_can('manage_options')) { |
| 23 |
// response with error |
| 24 |
wp_send_json_error(array( |
| 25 |
'message' => __('User does not have permission to manage options', 'linguise') |
| 26 |
)); |
| 27 |
return; |
| 28 |
} |
| 29 |
|
| 30 |
// time to update the data, data is nested in $_POST['data'] |
| 31 |
if (!isset($_POST['config'])) { |
| 32 |
// response with error |
| 33 |
wp_send_json_error(array( |
| 34 |
'message' => __('No data to update', 'linguise') |
| 35 |
)); |
| 36 |
return; |
| 37 |
} |
| 38 |
|
| 39 |
$data = $_POST['config']; |
| 40 |
$options = linguiseGetOptions(); |
| 41 |
|
| 42 |
// Check if all required fields are present |
| 43 |
$required_fields = [ |
| 44 |
'token', |
| 45 |
'language', |
| 46 |
'allowed_languages', |
| 47 |
]; |
| 48 |
|
| 49 |
foreach ($required_fields as $field) { |
| 50 |
if (!isset($data[$field])) { |
| 51 |
// response with error |
| 52 |
wp_send_json_error(array( |
| 53 |
'message' => sprintf( |
| 54 |
/* translators: %s: Missing required field name */ |
| 55 |
__('Missing required field: %s', 'linguise'), |
| 56 |
esc_html($field) |
| 57 |
) |
| 58 |
)); |
| 59 |
return; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
$options['token'] = $data['token']; |
| 64 |
$options['default_language'] = $data['language']; |
| 65 |
$options['enabled_languages'] = $data['allowed_languages']; |
| 66 |
|
| 67 |
$dynamic_translations = $options['dynamic_translations']; |
| 68 |
if (isset($data['dynamic_translations'])) { |
| 69 |
$dynamic_translations['enabled'] = $data['dynamic_translations'] === true ? 1 : 0; |
| 70 |
} |
| 71 |
if (isset($data['public_key'])) { |
| 72 |
$dynamic_translations['public_key'] = $data['public_key']; |
| 73 |
} |
| 74 |
$expert_mode = isset($data['expert_mode']) ? $data['expert_mode'] : []; |
| 75 |
if (isset($data['api_host'])) { |
| 76 |
$expert_mode['api_host'] = $data['api_host']; |
| 77 |
// Extract the port from the host if it is set |
| 78 |
if (strpos($data['api_host'], ':') !== false) { |
| 79 |
$parts = explode(':', $data['api_host']); |
| 80 |
$expert_mode['api_host'] = $parts[0]; |
| 81 |
if (isset($parts[1])) { |
| 82 |
$expert_mode['api_port'] = (int)$parts[1]; |
| 83 |
} |
| 84 |
} else { |
| 85 |
$expert_mode['api_port'] = 443; // Default port |
| 86 |
} |
| 87 |
|
| 88 |
$options['expert_mode'] = $expert_mode; |
| 89 |
} |
| 90 |
$options['dynamic_translations'] = $dynamic_translations; |
| 91 |
|
| 92 |
// save the options |
| 93 |
linguiseSwitchMainSite(); |
| 94 |
update_option('linguise_options', $options); |
| 95 |
linguiseRestoreMultisite(); |
| 96 |
wp_send_json_success(array( |
| 97 |
'message' => __('Linguise settings saved!', 'linguise') |
| 98 |
)); |
| 99 |
}); |
| 100 |
|
| 101 |
add_action('wp_ajax_linguise_get_headers', function () { |
| 102 |
// nonce |
| 103 |
if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'linguise_update_config_iframe')) { |
| 104 |
// set header |
| 105 |
wp_die(esc_html__('Nonce verification failed', 'linguise'), esc_html__('Unauthorized', 'linguise'), array( |
| 106 |
'response' => 403, |
| 107 |
'back_link' => true, |
| 108 |
)); |
| 109 |
return; |
| 110 |
} |
| 111 |
|
| 112 |
// check if user is logged in |
| 113 |
if (!is_user_logged_in()) { |
| 114 |
// response with error |
| 115 |
wp_die(esc_html__('User is not logged in', 'linguise'), esc_html__('Unauthorized', 'linguise'), array( |
| 116 |
'response' => 403, |
| 117 |
'back_link' => true, |
| 118 |
)); |
| 119 |
return; |
| 120 |
} |
| 121 |
// check if user has permission to manage options |
| 122 |
if (!current_user_can('manage_options')) { |
| 123 |
// response with error |
| 124 |
wp_die(esc_html__('User does not have permission to manage options', 'linguise'), esc_html__('Unauthorized', 'linguise'), array( |
| 125 |
'response' => 403, |
| 126 |
'back_link' => true, |
| 127 |
)); |
| 128 |
return; |
| 129 |
} |
| 130 |
|
| 131 |
// Dump request headers |
| 132 |
$headers = getallheaders(); |
| 133 |
if ($headers === false) { |
| 134 |
$headers = []; |
| 135 |
} |
| 136 |
|
| 137 |
$headers_line_by_line = []; |
| 138 |
foreach ($headers as $key => $value) { |
| 139 |
$headers_line_by_line[] = sprintf('%s: %s', $key, $value); |
| 140 |
} |
| 141 |
$headers_line_by_line = implode("\n", $headers_line_by_line); |
| 142 |
wp_die(esc_html($headers_line_by_line), esc_html__('Request Headers', 'linguise'), array( |
| 143 |
'response' => 200, |
| 144 |
'back_link' => false, |
| 145 |
)); |
| 146 |
}); |
| 147 |
|