allowed_screens, $template_file_name); // $current_screen = $template_file_name; // } // } // } if (in_array($current_screen, $this->allowed_screens, true)) { Assets\enqueue_asset( ADVICO_PLUGIN_DIR . '/assets/admin/dist', self::DEV_SCRIPT, $this->get_config() ); wp_localize_script(self::HANDLE, self::OBJ_NAME, $this->get_data()); } } /** * Get the script configuration. * * @return array The script configuration. */ public function get_config() { return array( 'dependencies' => array('react', 'react-dom'), 'handle' => self::HANDLE, 'in-footer' => true, ); } /** * Get data for script localization. * * @return array The localized script data. */ public function get_data() { return array( 'pluginName' => ADVICO_PLUGIN_NAME, 'isAdmin' => is_admin(), 'apiUrl' => rest_url(), 'pluginApiUrl' => rest_url() . ADVICO_ROUTE_PREFIX, 'userInfo' => $this->get_user_data(), 'logo' => ADVICO_PLUGIN_ASSETS_URL, 'nonce' => wp_create_nonce('wp_rest'), ); } /** * Get user data for script localization. * * @return array The user data. */ private function get_user_data() { $username = ''; $avatar_url = ''; $user_role = ''; if (is_user_logged_in()) { $current_user = wp_get_current_user(); // Get username. $username = $current_user->user_login; // Get avatar URL. $avatar_url = get_avatar_url($current_user->ID); // Get the first role (if available). if (! empty($current_user->roles) && is_array($current_user->roles)) { $user_role = $current_user->roles[0]; } } return array( 'username' => $username, 'avatar' => $avatar_url, 'user_role' => $user_role, ); } }