Last active
May 20, 2021 17:52
-
-
Save Canvilled/2772158c8e43fddce63f8f8a3edfea57 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Settings_Admin { | |
public function __construct() { | |
add_action( 'wp_head', array($this, 'add_script_to_fe') ); | |
add_action( 'admin_enqueue_scripts', array($this, 'add_custom_style_script') ); | |
add_filter( 'script_loader_tag', array($this,'defer_js'), 10,2 ); | |
} | |
public function add_custom_style_script(){ | |
wp_register_style('wa_email_marketing', PLUGIN_URL . '/dist/css/main.css'); | |
wp_enqueue_style('wa_email_marketing'); | |
wp_register_script('wa_email_marketing', PLUGIN_URL . '/dist/js/root.js') | |
} | |
public function defer_js($tag, $handle){ | |
$scripts_to_defer = array('wa_email_marketing'); | |
foreach($scripts_to_defer as $defer_script) { | |
if ($defer_script === $handle) { | |
return str_replace(' src', ' defer src', $tag); | |
} | |
} | |
return $tag; | |
} | |
public function add_script_to_fe(){ | |
$script = 'var apiObject = "' . wp_json_encode( | |
array( | |
'homeUrl' => home_url(), | |
'rootapiurl' => rest_url(), | |
'nonce' => wp_create_nonce('wp_rest'), | |
'plugin_url' => PLUGIN_URL, | |
'user_id' => get_current_user_id(), | |
)) . '";'; | |
if ( ! empty( $data ) ) { | |
$script = "$data\n$script"; | |
} | |
_e("\n<script type=\"text/javascript\">\n $script \n</script>\n",'_themename'); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment