| 1 |
<?php |
| 2 |
/** |
| 3 |
* Core plugin functionality. |
| 4 |
* |
| 5 |
* @package MagicLogin |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace MagicLogin\Core; |
| 9 |
|
| 10 |
use function MagicLogin\Utils\is_magic_login_settings_screen; |
| 11 |
use \WP_Error as WP_Error; |
| 12 |
|
| 13 |
/** |
| 14 |
* Default setup routine |
| 15 |
* |
| 16 |
* @return void |
| 17 |
*/ |
| 18 |
function setup() { |
| 19 |
add_action( 'init', __NAMESPACE__ . '\\i18n' ); |
| 20 |
add_action( 'init', __NAMESPACE__ . '\\init' ); |
| 21 |
add_action( 'admin_enqueue_scripts', __NAMESPACE__ . '\\admin_scripts' ); |
| 22 |
add_action( 'admin_enqueue_scripts', __NAMESPACE__ . '\\admin_styles' ); |
| 23 |
|
| 24 |
do_action( 'magic_login_loaded' ); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Registers the default textdomain. |
| 29 |
* |
| 30 |
* @return void |
| 31 |
*/ |
| 32 |
function i18n() { |
| 33 |
$locale = apply_filters( 'plugin_locale', get_locale(), 'magic-login' ); |
| 34 |
load_textdomain( 'magic-login', WP_LANG_DIR . '/magic-login/magic-login-' . $locale . '.mo' ); |
| 35 |
load_plugin_textdomain( 'magic-login', false, plugin_basename( MAGIC_LOGIN_PATH ) . '/languages/' ); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Initializes the plugin and fires an action other plugins can hook into. |
| 40 |
* |
| 41 |
* @return void |
| 42 |
*/ |
| 43 |
function init() { |
| 44 |
do_action( 'magic_login_init' ); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* The list of knows contexts for enqueuing scripts/styles. |
| 49 |
* |
| 50 |
* @return array |
| 51 |
*/ |
| 52 |
function get_enqueue_contexts() { |
| 53 |
return [ 'admin', 'frontend', 'shared', 'shortcode', 'block-editor' ]; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Generate an URL to a script, taking into account whether SCRIPT_DEBUG is enabled. |
| 58 |
* |
| 59 |
* @param string $script Script file name (no .js extension) |
| 60 |
* @param string $context Context for the script ('admin', 'frontend', or 'shared') |
| 61 |
* |
| 62 |
* @return string|WP_Error URL |
| 63 |
*/ |
| 64 |
function script_url( $script, $context ) { |
| 65 |
|
| 66 |
if ( ! in_array( $context, get_enqueue_contexts(), true ) ) { |
| 67 |
return new WP_Error( 'invalid_enqueue_context', 'Invalid $context specified in MagicLogin script loader.' ); |
| 68 |
} |
| 69 |
|
| 70 |
return MAGIC_LOGIN_URL . "dist/js/{$script}.js"; |
| 71 |
|
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Generate an URL to a stylesheet, taking into account whether SCRIPT_DEBUG is enabled. |
| 76 |
* |
| 77 |
* @param string $stylesheet Stylesheet file name (no .css extension) |
| 78 |
* @param string $context Context for the script ('admin', 'frontend', or 'shared') |
| 79 |
* |
| 80 |
* @return string URL |
| 81 |
*/ |
| 82 |
function style_url( $stylesheet, $context ) { |
| 83 |
|
| 84 |
if ( ! in_array( $context, get_enqueue_contexts(), true ) ) { |
| 85 |
return new WP_Error( 'invalid_enqueue_context', 'Invalid $context specified in MagicLogin stylesheet loader.' ); |
| 86 |
} |
| 87 |
|
| 88 |
return MAGIC_LOGIN_URL . "dist/css/{$stylesheet}.css"; |
| 89 |
|
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
/** |
| 94 |
* Enqueue scripts for admin. |
| 95 |
* |
| 96 |
* @return void |
| 97 |
*/ |
| 98 |
function admin_scripts() { |
| 99 |
if ( ! is_magic_login_settings_screen() ) { |
| 100 |
return; |
| 101 |
} |
| 102 |
|
| 103 |
wp_enqueue_script( |
| 104 |
'magic_login_admin', |
| 105 |
script_url( 'admin', 'admin' ), |
| 106 |
[], |
| 107 |
MAGIC_LOGIN_VERSION, |
| 108 |
true |
| 109 |
); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Enqueue styles for admin. |
| 114 |
* |
| 115 |
* @return void |
| 116 |
*/ |
| 117 |
function admin_styles() { |
| 118 |
|
| 119 |
if ( ! is_magic_login_settings_screen() ) { |
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
wp_enqueue_style( |
| 124 |
'magic_login_admin', |
| 125 |
style_url( 'admin-style', 'admin' ), |
| 126 |
[], |
| 127 |
MAGIC_LOGIN_VERSION |
| 128 |
); |
| 129 |
|
| 130 |
} |
| 131 |
|