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

Scripts and styles enqueued before wp_enqueue_scripts fail to get sources identified #4133

Closed
westonruter opened this issue Jan 18, 2020 · 2 comments · Fixed by #4135
Closed
Assignees
Labels
Bug Something isn't working CSS
Milestone

Comments

@westonruter
Copy link
Member

westonruter commented Jan 18, 2020

Bug Description

Scripts and stylesheets enqueued at init do not get sourcing information. The same goes for assets registered at wp_default_styles and wp_scripts.

See demo plugin which demonstrates the issue.

(Additionally, inline styles added via wp_add_inline_style() do not get their source attributed.)

The wp-includes source is erroneous for 2 scripts and 3 stylesheets shown on the page.

Related: #4134

Expected Behaviour

The source column should indicate the theme/plugin that is actually responsible for the style or script being added to the page.

The case of the inline stylesheet is a tricky one, since it is one stylesheet with multiple sources. At the very least it should have the same source information as the stylesheet it is being added inline for. Best case is for it to include all themes/plugins that added inline styles to it.

Steps to reproduce

  1. Enable Standard or Transitional mode in the AMP plugin.
  2. Activate the above plugin
  3. Validate a page.
  4. Notice the wp-includes mentioned unexpectedly.

Screenshots

When active (and #4026 checked out) the validated URL screen looks like the following, with the red entries erroneously saying they are from wp-includes when they should actually be from “Add Assets before wp_enqueue_scripts”.

72671921-d66e7780-3a06-11ea-8135-d2f12e3dc6b5

Additional context

  • WordPress version:
  • Plugin version:
  • Gutenberg plugin version (if applicable):
  • AMP plugin template mode:
  • PHP version:
  • OS:
  • Browser: [e.g. chrome, safari]
  • Device: [e.g. iPhone6]

Do not alter or remove anything below. The following sections will be managed by moderators only.

Acceptance criteria

Implementation brief

  • This may be resolved simply by adding the validation hooks earlier. Origination is also a solution, perhaps long-term.

QA testing instructions

Demo

Changelog entry

@westonruter westonruter added Bug Something isn't working CSS labels Jan 18, 2020
@westonruter westonruter changed the title Scripts and styles enqueued before wp_enqueue_scripts (and inline styles) fail to get sources identified Scripts and styles enqueued before wp_enqueue_scripts fail to get sources identified Jan 19, 2020
@westonruter westonruter added the P2 Low priority label Jan 21, 2020
@westonruter westonruter removed the P2 Low priority label Jan 23, 2020
@westonruter westonruter self-assigned this Jan 23, 2020
@westonruter westonruter added this to the v1.5 milestone Jan 24, 2020
@csossi
Copy link

csossi commented Feb 28, 2020

@kienstra , can you do a dev QA check on this one (or shoot instructions my way?)

@kienstra
Copy link
Contributor

kienstra commented Mar 2, 2020

Works as expected

Now, scripts enqueued on the init action get their source.

Similar to the test in #4134 (comment):

// Enqueues a script and stylesheet.
add_action(
	'init',
	function() {
		$script_slug = 'amp-scratchpad-script';
		wp_enqueue_script(
			$script_slug,
			plugins_url( 'script.js', __FILE__ ),
			[ 'jquery' ],
			'0.1',
			true
		);

		wp_localize_script(
			$script_slug,
			'ampScratchpad',
			wp_json_encode( [ 'foo' => 'baz' ] )
		);

		wp_add_inline_script(
			$script_slug,
			'doSomethingHere()'
		);

		$style_slug = 'amp-scratchpad-style';
		wp_enqueue_style(
			$style_slug,
			plugins_url( 'style.css', __FILE__ ),
			[ 'wp-color-picker', 'site-icon' ],
			'0.1'
		);

		wp_add_inline_style(
			$style_slug,
			'body { background-color: #ffffff; }'
		);
	}
);

...the source now appears:

sources-there

Before, it was wp-includes, as Weston mentioned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working CSS
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants