PluginProbe
Booking Calendar / 11.9
Booking Calendar v11.9
11.9 11.8.4 11.8.3 11.8.2 11.8.1 11.8 11.7 11.6.1 11.6 11.5 11.4.3 11.4.2 11.4.1 11.4 11.3 11.2.1 11.2 11.1 11.0 10.15.7 10.15.6 10.1.3 10.10 10.10.1 10.10.2 All 205 releases
booking / includes / page-setup-wizard / step-date-time-formats / class-wpbc-setup-wizard-date-time-formats-ajax.php

class-wpbc-setup-wizard-date-time-formats-ajax.php in Booking Calendar 11.9, at includes/page-setup-wizard/step-date-time-formats/class-wpbc-setup-wizard-date-time-formats-ajax.php

122 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Explicit Local translation mutation for Setup Wizard Step 3.
4 *
5 * @package Booking Calendar
6 */
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit;
10 }
11
12 /**
13 * Authorize and orchestrate the Step 3 Local translation installation.
14 */
15 final class WPBC_Setup_Wizard_Date_Time_Formats_Ajax {
16
17 const ACTION_INSTALL_LOCAL_TRANSLATION = 'WPBC_AJX_SETUP_WIZARD_INSTALL_LOCAL_TRANSLATION';
18
19 /**
20 * Register the authenticated mutation endpoint.
21 *
22 * @return void
23 */
24 public static function register() {
25 add_action( 'wp_ajax_' . self::ACTION_INSTALL_LOCAL_TRANSLATION, array( __CLASS__, 'install_local_translation' ) );
26 }
27
28 /**
29 * Update the complete Local archive and select Local as the preferred source.
30 *
31 * @return void Sends a WordPress JSON response and terminates the request.
32 */
33 public static function install_local_translation() {
34 self::authorize_request();
35
36 $request_id = self::get_request_id();
37 $service = new WPBC_Setup_Wizard_Date_Time_Formats();
38 $result = $service->install_site_local_translation();
39
40 if ( is_wp_error( $result ) ) {
41 $error_data = $result->get_error_data();
42 $error_logs = is_array( $error_data ) && isset( $error_data['logs'] ) && is_array( $error_data['logs'] )
43 ? $error_data['logs']
44 : array();
45
46 wp_send_json_error(
47 array(
48 'code' => $result->get_error_code(),
49 'message' => $result->get_error_message(),
50 'logs' => $error_logs,
51 'language' => self::get_language_response( $service->get_site_language_context() ),
52 'request_id' => $request_id,
53 ),
54 400
55 );
56 }
57
58 wp_send_json_success(
59 array(
60 'language' => self::get_language_response( $result ),
61 'message' => $result['message'],
62 'logs' => isset( $result['logs'] ) && is_array( $result['logs'] ) ? $result['logs'] : array(),
63 'reload_page' => true,
64 'request_id' => $request_id,
65 )
66 );
67 }
68
69 /**
70 * Normalize the authorized language presentation sent to the browser.
71 *
72 * @param array<string,mixed> $language_context Server-derived language context.
73 * @return array<string,mixed> Stable, data-only browser response.
74 */
75 private static function get_language_response( array $language_context ) {
76 return array(
77 'locale' => isset( $language_context['locale'] ) ? (string) $language_context['locale'] : '',
78 'label' => isset( $language_context['label'] ) ? (string) $language_context['label'] : '',
79 'status' => isset( $language_context['status'] ) ? (string) $language_context['status'] : '',
80 'status_label' => isset( $language_context['status_label'] ) ? (string) $language_context['status_label'] : '',
81 'help' => isset( $language_context['help'] ) ? (string) $language_context['help'] : '',
82 'translation_source' => isset( $language_context['translation_source'] ) ? (string) $language_context['translation_source'] : '',
83 'translation_source_label' => isset( $language_context['translation_source_label'] ) ? (string) $language_context['translation_source_label'] : '',
84 'is_local_source_recommended' => ! empty( $language_context['is_local_source_recommended'] ),
85 'local_source_recommendation' => isset( $language_context['local_source_recommendation'] ) ? (string) $language_context['local_source_recommendation'] : '',
86 'action_label' => isset( $language_context['action_label'] ) ? (string) $language_context['action_label'] : '',
87 );
88 }
89
90 /**
91 * Enforce the nonce, production wizard access, capability, and demo policy.
92 *
93 * @return void Sends an error response when authorization fails.
94 */
95 private static function authorize_request() {
96 if ( ! WPBC_Setup_Wizard_Ajax::verify_request_nonce() ) {
97 wp_send_json_error( array( 'message' => __( 'Your Setup Wizard session expired. Reload the page and try again.', 'booking' ) ), 403 );
98 }
99
100 if ( ! WPBC_Setup_Wizard_Access::current_user_can_access() || ! current_user_can( 'activate_plugins' ) ) {
101 wp_send_json_error( array( 'message' => __( 'You are not allowed to install Booking Calendar translations.', 'booking' ) ), 403 );
102 }
103
104 if ( function_exists( 'wpbc_is_this_demo' ) && wpbc_is_this_demo() ) {
105 wp_send_json_error( array( 'message' => __( 'Local translation downloads are unavailable on live demo sites.', 'booking' ) ), 403 );
106 }
107 }
108
109 /**
110 * Read a request correlation identifier without accepting domain input.
111 *
112 * @return string Sanitized request identifier.
113 */
114 private static function get_request_id() {
115 if ( ! isset( $_POST['request_id'] ) || ! is_scalar( $_POST['request_id'] ) ) {
116 return '';
117 }
118
119 return sanitize_key( wp_unslash( $_POST['request_id'] ) );
120 }
121 }
122