Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit enqueue to specific pages #711

Closed
cfoellmann opened this issue Aug 22, 2018 · 3 comments
Closed

Limit enqueue to specific pages #711

cfoellmann opened this issue Aug 22, 2018 · 3 comments

Comments

@cfoellmann
Copy link

cfoellmann commented Aug 22, 2018

Currently the ghu-install.js is loaded on all is_admin() pages.
This could be limited to the GH Updater Settings pages, right?
I would suggest the following code in src/GitHub_Updater/Install.php:

/**
 * Let's set up the Install tabs.
 * Need class-wp-upgrader.php for upgrade classes.
 *
 * @return void
 */
public function run() {
	add_action( 'admin_enqueue_scripts', array( $this, 'load_js' ) );
	$this->add_settings_tabs();
	require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
}

/**
 * Load javascript for Install.
 *
 * @return void
 */
public function load_js( $hook ) {
	if ( ! ( $hook == 'settings_page_github-updater' ) ) {
		return;
	}

	wp_register_script( 'ghu-install', plugins_url( basename( dirname( dirname( __DIR__ ) ) ) . '/js/ghu-install.js' ), [ 'jquery' ], false, true );
	wp_enqueue_script( 'ghu-install' );
}

Or if there is/will be more than that one page hook

/**
 * Load javascript for Install.
 *
 * @return void
 */
public function load_js( $hook ) {
	$pages = array( 'settings_page_github-updater', );

	if ( ! in_array( $hook, $pages )  ) {
		return;
	}

	wp_register_script( 'ghu-install', plugins_url( basename( dirname( dirname( __DIR__ ) ) ) . '/js/ghu-install.js' ), [ 'jquery' ], false, true );
	wp_enqueue_script( 'ghu-install' );
}
@afragen
Copy link
Owner

afragen commented Aug 22, 2018

@cfoellmann I believe I already limit the loading to specific pages. Let’s follow the loading.

The Install page loads from Settings->run() from Base->load() but just afterwards is the following which checks for specific pages and exits.

https://github.com/afragen/github-updater/blob/769d6c6f6ca7bab57e846832282a0492ea3646fd/src/GitHub_Updater/Base.php#L134-L136

Yes, the JS loads on all pages that GHU loads but it’s not on all admin pages. Because the JS loads from the Install->run() it runs when the Install class runs.

Also, this only effects users who have the proper privileges.

https://github.com/afragen/github-updater/blob/769d6c6f6ca7bab57e846832282a0492ea3646fd/src/GitHub_Updater/Init.php#L118

@afragen
Copy link
Owner

afragen commented Aug 22, 2018

This question brings up a larger issue that I need to step through.

Perhaps I can limit simply limit the loading of Settings to the proper page in the dashboard, obviously checking for single and multisite.

@afragen
Copy link
Owner

afragen commented Aug 22, 2018

I think 6b4e1b7 should fix this issue. LMK if it caused any issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants