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