| @@ -1,54 +1,115 @@ | ||
| 1 | -<?php | |
| 2 | -namespace Booktics\Assets; | |
| 3 | - | |
| 4 | -/** | |
| 5 | - * Manage all localize data | |
| 6 | - */ | |
| 7 | -class Localize { | |
| 8 | - | |
| 9 | - /** | |
| 10 | - * Get admin localize data | |
| 11 | - * | |
| 12 | - * @return array Collection localize data | |
| 13 | - */ | |
| 14 | - public static function get_admin() { | |
| 15 | - $current_user = wp_get_current_user(); | |
| 16 | - | |
| 17 | - $data = [ | |
| 18 | - 'site_url' => site_url(), | |
| 19 | - 'admin_url' => admin_url(), | |
| 20 | - 'nonce' => wp_create_nonce( 'wp_rest' ), | |
| 21 | - 'date_format' => get_option( 'date_format' ), | |
| 22 | - 'date_format_string' => date_i18n( get_option( 'date_format' ) ), | |
| 23 | - 'time_format' => get_option( 'time_format' ), | |
| 24 | - 'time_format_string' => date_i18n( get_option( 'time_format' ) ), | |
| 25 | - 'start_of_week' => get_option( 'start_of_week', 0 ), | |
| 26 | - 'current_user_id' => get_current_user_id(), | |
| 27 | - 'currency_list' => booktics_get_currencies(), | |
| 28 | - 'bookticsPro' => class_exists('BookticsPro'), | |
| 29 | - 'user_role' => $current_user->roles, | |
| 30 | - ]; | |
| 31 | - | |
| 32 | - return apply_filters( 'booktics_admin_localize', $data ); | |
| 33 | - } | |
| 34 | - | |
| 35 | - /** | |
| 36 | - * Get frontend localize data | |
| 37 | - * | |
| 38 | - * @return array Collection localize data | |
| 39 | - */ | |
| 40 | - public static function get_frontend() { | |
| 41 | - $data = [ | |
| 42 | - 'site_url' => site_url(), | |
| 43 | - 'nonce' => wp_create_nonce( 'wp_rest' ), | |
| 44 | - 'date_format' => get_option( 'date_format' ), | |
| 45 | - 'time_format' => get_option( 'time_format' ), | |
| 46 | - 'current_user_id' => get_current_user_id(), | |
| 47 | - 'start_of_week' => get_option( 'start_of_week', 0 ), | |
| 48 | - 'locale_name' => strtolower( str_replace( '_', '-', get_locale() ) ), | |
| 49 | - 'currency_list' => booktics_get_currencies(), | |
| 50 | - ]; | |
| 51 | - | |
| 52 | - return apply_filters( 'booktics_frontend_localize', $data ); | |
| 53 | - } | |
| 1 | +<?php | |
| 2 | +namespace Booktics\Assets; | |
| 3 | + | |
| 4 | +use Booktics\Admin\Theme_Mode; | |
| 5 | + | |
| 6 | +if ( ! defined( 'ABSPATH' ) ) exit; | |
| 7 | + | |
| 8 | +/** | |
| 9 | + * Manage all localize data | |
| 10 | + */ | |
| 11 | +class Localize { | |
| 12 | + | |
| 13 | + /** | |
| 14 | + * Get admin localize data | |
| 15 | + * | |
| 16 | + * @return array Collection localize data | |
| 17 | + */ | |
| 18 | + public static function get_admin() { | |
| 19 | + $current_user = wp_get_current_user(); | |
| 20 | + $user_roles = []; | |
| 21 | + if ( isset( $current_user->roles ) && is_array( $current_user->roles) ) { | |
| 22 | + foreach( $current_user->roles as $role) { | |
| 23 | + $user_roles[] = $role; | |
| 24 | + } | |
| 25 | + } | |
| 26 | + | |
| 27 | + $data = [ | |
| 28 | + 'site_url' => site_url(), | |
| 29 | + 'admin_url' => admin_url(), | |
| 30 | + 'nonce' => wp_create_nonce( 'wp_rest' ), | |
| 31 | + 'date_format' => get_option( 'date_format' ), | |
| 32 | + 'date_format_string' => date_i18n( get_option( 'date_format' ) ), | |
| 33 | + 'time_format' => get_option( 'time_format' ), | |
| 34 | + 'time_format_string' => date_i18n( get_option( 'time_format' ) ), | |
| 35 | + 'start_of_week' => get_option( 'start_of_week', 0 ), | |
| 36 | + 'current_user_id' => get_current_user_id(), | |
| 37 | + 'current_user_email' => $current_user->user_email, | |
| 38 | + 'current_user_name' => $current_user->display_name, | |
| 39 | + 'currency_list' => booktics_get_currencies(), | |
| 40 | + 'bookticsPro' => class_exists('BookticsPro'), | |
| 41 | + 'user_role' => (array) $user_roles, | |
| 42 | + 'doc_links' => self::get_doc_links(), | |
| 43 | + 'aisentic' => self::get_aisentic_state(), | |
| 44 | + 'theme_mode' => Theme_Mode::get_for_user(), | |
| 45 | + 'dark_mode_enabled' => Theme_Mode::is_enabled(), | |
| 46 | + ]; | |
| 47 | + | |
| 48 | + return apply_filters( 'booktics_admin_localize', $data ); | |
| 49 | + } | |
| 50 | + | |
| 51 | + /** | |
| 52 | + * Build a slug => doc_link map from the module registry. | |
| 53 | + * | |
| 54 | + * Single source of truth for documentation URLs: both the addon card | |
| 55 | + * (PHP `doc_link`) and the React configure modals read from this list, | |
| 56 | + * so a doc URL is only ever defined once in booktics_modules_list(). | |
| 57 | + * | |
| 58 | + * @return array<string, string> | |
| 59 | + */ | |
| 60 | + private static function get_doc_links() { | |
| 61 | + $doc_links = []; | |
| 62 | + | |
| 63 | + foreach ( booktics_modules_list() as $module ) { | |
| 64 | + if ( ! empty( $module['slug'] ) && ! empty( $module['doc_link'] ) ) { | |
| 65 | + $doc_links[ $module['slug'] ] = $module['doc_link']; | |
| 66 | + } | |
| 67 | + } | |
| 68 | + | |
| 69 | + return $doc_links; | |
| 70 | + } | |
| 71 | + | |
| 72 | + /** | |
| 73 | + * Aisentic connection state for the admin UI. | |
| 74 | + * | |
| 75 | + * The identity comes from booktics_aisentic_identity() so the email rendered on | |
| 76 | + * the consent copy is the same one the register action would send. | |
| 77 | + * | |
| 78 | + * @return array<string, mixed> | |
| 79 | + */ | |
| 80 | + private static function get_aisentic_state() { | |
| 81 | + $identity = booktics_aisentic_identity(); | |
| 82 | + $integration = get_option( 'aisentic_integration_settings', [] ); | |
| 83 | + | |
| 84 | + return [ | |
| 85 | + 'name' => $identity['name'], | |
| 86 | + 'email' => $identity['email'], | |
| 87 | + 'site_url' => $identity['site_url'], | |
| 88 | + 'active' => class_exists( 'Aisentic\Init' ), | |
| 89 | + 'registered' => booktics_aisentic_is_registered(), | |
| 90 | + 'supports_registration' => class_exists( 'Aisentic\Api\Services\Registration_Service' ), | |
| 91 | + 'connected' => isset( $integration['booktics']['status'] ) | |
| 92 | + && 'connected' === $integration['booktics']['status'], | |
| 93 | + ]; | |
| 94 | + } | |
| 95 | + | |
| 96 | + /** | |
| 97 | + * Get frontend localize data | |
| 98 | + * | |
| 99 | + * @return array Collection localize data | |
| 100 | + */ | |
| 101 | + public static function get_frontend() { | |
| 102 | + $data = [ | |
| 103 | + 'site_url' => site_url(), | |
| 104 | + 'nonce' => wp_create_nonce( 'wp_rest' ), | |
| 105 | + 'date_format' => get_option( 'date_format' ), | |
| 106 | + 'time_format' => get_option( 'time_format' ), | |
| 107 | + 'current_user_id' => get_current_user_id(), | |
| 108 | + 'start_of_week' => get_option( 'start_of_week', 0 ), | |
| 109 | + 'locale_name' => strtolower( str_replace( '_', '-', get_locale() ) ), | |
| 110 | + 'currency_list' => booktics_get_currencies(), | |
| 111 | + ]; | |
| 112 | + | |
| 113 | + return apply_filters( 'booktics_frontend_localize', $data ); | |
| 114 | + } | |
| 54 | 115 | } |