File "Actions.php"
Full Path: /home/jovision/public_html/wp/wp-content/plugins/host-webfonts-local/src/Frontend/Actions.php
File size: 6.64 KB
MIME-type: text/x-php
Charset: utf-8
<?php
/* * * * * * * * * * * * * * * * * * * * *
*
* ██████╗ ███╗ ███╗ ██████╗ ███████╗
* ██╔═══██╗████╗ ████║██╔════╝ ██╔════╝
* ██║ ██║██╔████╔██║██║ ███╗█████╗
* ██║ ██║██║╚██╔╝██║██║ ██║██╔══╝
* ╚██████╔╝██║ ╚═╝ ██║╚██████╔╝██║
* ╚═════╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝
*
* @package : OMGF
* @author : Daan van den Bergh
* @copyright: © 2017 - 2025 Daan van den Bergh
* @url : https://daan.dev
* * * * * * * * * * * * * * * * * * * */
namespace OMGF\Frontend;
use OMGF\Admin\Dashboard;
use OMGF\Admin\Settings;
use OMGF\Helper as OMGF;
class Actions {
const FRONTEND_ASSET_HANDLE = "omgf-frontend";
/**
* Execute all classes required in the frontend.
*/
public function __construct() {
add_action( 'init', [ $this, 'init_frontend' ], 50 );
add_action( 'admin_bar_menu', [ $this, 'add_admin_bar_item' ], 1000 );
add_action( 'wp_enqueue_scripts', [ $this, 'maybe_add_admin_bar_css' ] );
add_action( 'wp_enqueue_scripts', [ $this, 'maybe_add_admin_bar_js' ] );
}
/**
* @param \WP_Admin_Bar $admin_bar
*
* @return void
*/
public function add_admin_bar_item( \WP_Admin_Bar $admin_bar ) {
if ( ! $this->should_display_menu() ) {
return;
}
$admin_bar->add_menu(
[
'id' => 'omgf',
'parent' => null,
'title' => apply_filters( 'omgf_settings_page_title', __( 'OMGF', 'host-webfonts-local' ) ),
'href' => admin_url( 'options-general.php?page=' . Settings::OMGF_ADMIN_PAGE ),
]
);
global $wp;
$permalink_structure = get_option( 'permalink_structure' );
$site_url = home_url( $wp->request );
if ( ! $permalink_structure ) {
foreach ( $wp->query_vars as $query_var_key => $query_var_value ) {
$site_url = add_query_arg( $query_var_key, $query_var_value, $site_url );
}
}
$admin_bar->add_menu(
[
'id' => 'omgf-optimize-this',
'parent' => 'omgf',
'title' => __( 'Optimize this page', 'host-webfonts-local' ),
'href' => OMGF::no_cache_optimize_url( $site_url ),
]
);
}
/**
* The top admin bar menu should be displayed when:
*
* - User is an administrator
* - This is not an admin screen i.e., we're in the frontend
* - Disable Quick Access is disabled.
*
* @return bool
*/
private function should_display_menu() {
$is_admin_user = current_user_can( 'manage_options' );
$is_admin_screen = is_admin();
$quick_access_disabled = ! empty( OMGF::get_option( Settings::OMGF_ADV_SETTING_DISABLE_ADMIN_BAR_MENU ) );
return $is_admin_user && ! $is_admin_screen && ! $quick_access_disabled;
}
/**
* Initializes everything required to process frontend optimization.
*/
public function init_frontend() {
new Process();
}
/**
* These stylesheets are only loaded for logged-in administrators unless:
*
* @return void
*/
public function maybe_add_admin_bar_css() {
// Even if the above filter forces the JS to load, we'll only need the CSS if the current user is an admin.
if ( apply_filters( 'omgf_do_not_load_frontend_css', ! current_user_can( 'manage_options' ) ) ) {
return;
}
$file_ext = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
$css_file = plugin_dir_url( OMGF_PLUGIN_FILE ) . "assets/css/" . self::FRONTEND_ASSET_HANDLE . "$file_ext.css";
$css_path = plugin_dir_path( OMGF_PLUGIN_FILE ) . "assets/css/" . self::FRONTEND_ASSET_HANDLE . "$file_ext.css";
wp_enqueue_style( self::FRONTEND_ASSET_HANDLE, $css_file, [], filemtime( $css_path ) );
}
/**
* These scripts are only loaded for logged-in administrators unless:
* - OMGF shouldn't run.
* - The current request directly points to a PHP file (some plugin's preview pages do that)
*
* @return void
*/
public function maybe_add_admin_bar_js() {
if ( apply_filters( 'omgf_do_not_load_frontend_js', ! current_user_can( 'manage_options' ) ) ) {
return;
}
if ( ( ! empty( $_SERVER['REQUEST_URI'] ) && str_contains( esc_url_raw( $_SERVER['REQUEST_URI'] ), '.php' ) ) || ! Process::should_start() ) {
return; // @codeCoverageIgnore
}
$file_ext = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
$js_file = plugin_dir_url( OMGF_PLUGIN_FILE ) . "assets/js/" . self::FRONTEND_ASSET_HANDLE . "$file_ext.js";
$js_path = plugin_dir_path( OMGF_PLUGIN_FILE ) . "assets/js/" . self::FRONTEND_ASSET_HANDLE . "$file_ext.js";
$subsets = OMGF::get_option( Settings::OMGF_ADV_SETTING_SUBSETS );
wp_register_script( self::FRONTEND_ASSET_HANDLE, $js_file, [], filemtime( $js_path ), [ 'strategy' => 'defer' ] );
wp_localize_script(
self::FRONTEND_ASSET_HANDLE,
'omgf_frontend_i18n',
[
'api_url' => get_rest_url( null, 'omgf/v1/adminbar-menu/status' ),
'first_run' => ! OMGF::optimize_succeeded(),
'info_box_alert_text' => __( 'Google Fonts were found on this page. Click here for more information.', 'host-webfonts-local' ),
'info_box_notice_text' => __( 'There are potential issues in your configuration that require your attention.', 'host-webfonts-local' ),
'info_box_preload_text' => __( 'Preloading %s fonts could save ~%sms on your LCP · %s impact', 'host-webfonts-local' ),
'info_box_unload_text' => __( '%s fonts loaded but unused · %s impact', 'host-webfonts-local' ),
'info_box_cls_text' => __( 'Google Fonts are causing layout shifts · CLS: %s · %s impact', 'host-webfonts-local' ),
'info_box_multilang_plugin_text' => __( '%s detected · %d subsets loading on every page · %s impact', 'host-webfonts-local' ),
'info_box_impact_high' => __( 'High', 'host-webfonts-local' ),
'info_box_impact_medium' => __( 'Medium', 'host-webfonts-local' ),
'info_box_impact_low' => __( 'Low', 'host-webfonts-local' ),
'info_box_admin_url' => admin_url( 'options-general.php?page=' . Settings::OMGF_ADMIN_PAGE ),
'multilang_plugin_used' => Dashboard::has_multilang_plugin(),
'multilang_plugin_name' => Dashboard::get_multilang_plugin(),
'nonce' => wp_create_nonce( 'wp_rest' ),
'subsets_count' => is_countable( $subsets ) ? count( $subsets ) : 0,
]
);
wp_localize_script(
self::FRONTEND_ASSET_HANDLE,
'omgf_frontend_results',
[ 'skip' => ! OMGF::optimize_succeeded() ]
);
wp_enqueue_script( self::FRONTEND_ASSET_HANDLE );
}
}