| @@ -1,7 +1,11 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | namespace Booktics\Assets; |
| 3 | 3 | |
| 4 | +use Booktics\Admin\Theme_Mode; | |
| 5 | + | |
| 6 | +if ( ! defined( 'ABSPATH' ) ) exit; | |
| 7 | + | |
| 4 | 8 | /** |
| 5 | 9 | * Manage all localize data |
| 6 | 10 | */ |
| 7 | 11 | class Localize { |
| @@ -12,8 +16,14 @@ | ||
| 12 | 16 | * @return array Collection localize data |
| 13 | 17 | */ |
| 14 | 18 | public static function get_admin() { |
| 15 | 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 | + } | |
| 16 | 26 | |
| 17 | 27 | $data = [ |
| 18 | 28 | 'site_url' => site_url(), |
| 19 | 29 | 'admin_url' => admin_url(), |
| @@ -23,14 +33,65 @@ | ||
| 23 | 33 | 'time_format' => get_option( 'time_format' ), |
| 24 | 34 | 'time_format_string' => date_i18n( get_option( 'time_format' ) ), |
| 25 | 35 | 'start_of_week' => get_option( 'start_of_week', 0 ), |
| 26 | 36 | 'current_user_id' => get_current_user_id(), |
| 37 | + 'current_user_email' => $current_user->user_email, | |
| 38 | + 'current_user_name' => $current_user->display_name, | |
| 27 | 39 | 'currency_list' => booktics_get_currencies(), |
| 28 | 40 | 'bookticsPro' => class_exists('BookticsPro'), |
| 29 | - 'user_role' => $current_user->roles, | |
| 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(), | |
| 30 | 46 | ]; |
| 31 | 47 | |
| 32 | 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 | + ]; | |
| 33 | 94 | } |
| 34 | 95 | |
| 35 | 96 | /** |
| 36 | 97 | * Get frontend localize data |