Skip to content

Commit 2f9f7ea

Browse files
committed
👌 IMPROVE: Moved the enqueue scripts to their own file
1 parent 612bee6 commit 2f9f7ea

2 files changed

Lines changed: 59 additions & 1 deletion

File tree

admin/enqueue.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
// If this file is called directly, abort.
4+
if ( ! defined( 'WPINC' ) ) {
5+
die;
6+
}
7+
8+
/**
9+
* Enqueue BenchPress plugin assets.
10+
*
11+
* @since 1.0.0
12+
* @return void
13+
*/
14+
function benchpress_enqueue_assets( $hook ) {
15+
// Bail early if we're not on BenchPress pages.
16+
if ( ! in_array( $hook, [
17+
'toplevel_page_benchpress',
18+
'benchpress_page_benchpress-snapshots',
19+
'benchpress_page_benchpress-settings'
20+
], true ) ) {
21+
return;
22+
}
23+
24+
wp_enqueue_style( 'benchpress-styles', plugin_dir_url( __FILE__ ) . '../assets/css/style.css', [], BENCHPRESS_VERSION );
25+
wp_enqueue_style( 'select2-css', plugin_dir_url( __FILE__ ) . '../assets/css/select2.min.css', [], BENCHPRESS_VERSION );
26+
wp_enqueue_script( 'select2-js', plugin_dir_url( __FILE__ ) . '../assets/js/select2.min.js', [ 'jquery' ], BENCHPRESS_VERSION, true );
27+
wp_enqueue_script( 'benchpress-admin-js', plugin_dir_url( __FILE__ ) . '../assets/js/benchpress-admin.js', [ 'jquery', 'select2' ], BENCHPRESS_VERSION, true );
28+
wp_enqueue_script( 'benchpress-ajax', plugin_dir_url( __FILE__ ) . '../assets/js/benchpress-ajax.js', ['jquery'], BENCHPRESS_VERSION, true );
29+
30+
wp_localize_script( 'benchpress-ajax', 'benchpress_ajax', [
31+
'ajax_url' => admin_url( 'admin-ajax.php' ),
32+
'nonce' => wp_create_nonce( 'benchpress_nonce' ),
33+
'site_name' => sanitize_title( get_bloginfo( 'name' ) ),
34+
'datetime' => date( 'Y-m-d_H-i-s' ),
35+
] );
36+
}
37+
add_action( 'admin_enqueue_scripts', 'benchpress_enqueue_assets' );
38+
39+
/**
40+
* Enqueue custom admin styles for BenchPress plugin.
41+
*
42+
* This function loads the custom CSS file for the BenchPress plugin's
43+
* admin interface. It adds styling to the BenchPress menu icon in
44+
* the WordPress® admin sidebar, including hover effects.
45+
*
46+
* @since 1.0.0
47+
* @return void
48+
*/
49+
function benchpress_enqueue_admin_styles() {
50+
wp_enqueue_style(
51+
'benchpress-admin-style',
52+
plugin_dir_url( __FILE__ ) . '../assets/css/admin-style.css',
53+
[],
54+
BENCHPRESS_VERSION
55+
);
56+
}
57+
add_action( 'admin_enqueue_scripts', 'benchpress_enqueue_admin_styles' );

benchpress.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,9 @@ function benchpress_create_snapshots_table() {
119119
require_once BENCHPRESS_PLUGIN_DIR . 'classes/BenchPress_Table.php';
120120
require_once BENCHPRESS_PLUGIN_DIR . 'classes/BenchPress_Snapshots_Table.php';
121121
require_once BENCHPRESS_PLUGIN_DIR . 'includes/helper-functions.php';
122-
require_once BENCHPRESS_PLUGIN_DIR . 'admin/settings.php';
122+
require_once BENCHPRESS_PLUGIN_DIR . 'admin/enqueue.php';
123123
require_once BENCHPRESS_PLUGIN_DIR . 'admin/ajax.php';
124+
require_once BENCHPRESS_PLUGIN_DIR . 'admin/settings.php';
124125

125126
// Include PHP 8-specific functions if the server's PHP version is 8.0 or above.
126127
if ( version_compare( PHP_VERSION, '8.0', '>=' ) ) {

0 commit comments

Comments
 (0)