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

Introduce register_asset() and enqueue_asset() functions; refactor & test plugin #22

Merged
merged 40 commits into from
Jul 21, 2020
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
152349f
Use a static $manifests cache to avoid repeat file reads in one cycle
kadamwhite Jul 8, 2020
2cc9705
Use the new PHPCS ruleset in CI
kadamwhite Jul 8, 2020
9d3ba7b
Install PHPUnit
kadamwhite Jul 8, 2020
5d4d240
Begin moving admin script warning to new namespace, with tests
kadamwhite Jul 8, 2020
314c05f
Expand test coverage of admin script load error notice handling
kadamwhite Jul 8, 2020
b57c4b4
Include tests in travis, and add editorconfig
kadamwhite Jul 8, 2020
d77b114
Add Manifest\get_manifest_resource method
kadamwhite Jul 8, 2020
fa62dc4
Add unit tests for Paths\plugin_or_theme_file_uri
kadamwhite Jul 9, 2020
fe13729
Rename path_or_theme_file_uri to get_file_uri
kadamwhite Jul 9, 2020
5e766a6
Test is_css and adjust admin namespace test function comments
kadamwhite Jul 9, 2020
32324f4
Introduce single-resource register_asset and enqueue_asset functions
kadamwhite Jul 9, 2020
3e6a7a9
Disable side-effects rule in tests bootstrap
kadamwhite Jul 9, 2020
b66dfa1
Complete moving ssl cert error logic to Admin namespace
kadamwhite Jul 9, 2020
2220e57
Name each test case in data providers
kadamwhite Jul 9, 2020
51715ce
Add _doing_it_wrong to autoenqueue/autoregister
kadamwhite Jul 9, 2020
f45f962
Consistently apply SSL cert error notification in all main logic paths
kadamwhite Jul 9, 2020
5c0dcbb
Add CHANGELOG.md covering through upcoming 0.4
kadamwhite Jul 9, 2020
3b5c0c6
Use resource URI as script or style handle if no handle provided
kadamwhite Jul 9, 2020
213b4dd
Update README
kadamwhite Jul 9, 2020
2c2fab8
Introduce Manifest\get_version() helper to set asset version strings
kadamwhite Jul 20, 2020
1cf2d0b
Use new Manifest\get_version() function when registering assets
kadamwhite Jul 20, 2020
e2327a4
Move mocks of trailingslashit to base test class
kadamwhite Jul 20, 2020
ebf9d1f
Determine hash-ness with 16 or more chars, not 25
kadamwhite Jul 20, 2020
729305c
Add overly-complex unit test setup for register_asset method
kadamwhite Jul 21, 2020
e5716bf
Add test for complex fallback logic for CSS-only dev assets
kadamwhite Jul 21, 2020
8b9e72c
Eliminate trailing comma causing error in PHP 7.2
kadamwhite Jul 21, 2020
28ace0a
Update inc/manifest.php
kadamwhite Jul 21, 2020
d881e63
Use terse forms of composer script commands
kadamwhite Jul 21, 2020
cd9611c
Remove superfluous is_admin check
kadamwhite Jul 21, 2020
6f545d6
Rename phpcs file to remove need to manually specify --standard
kadamwhite Jul 21, 2020
de59196
Use null coalescing to obviate isset check
kadamwhite Jul 21, 2020
93e3782
Use is_readable over file_exists
kadamwhite Jul 21, 2020
6d070f5
Remove unnecessary namespace & "use"
kadamwhite Jul 21, 2020
01936ab
Remove superfluous blank line.
tfrommen Jul 21, 2020
e9f6453
Unset DOM loaded event after execution.
kadamwhite Jul 21, 2020
faf56db
Use rtrim in trailingslashit mock implementation
kadamwhite Jul 21, 2020
31fa16b
Remove admin flag from tests for function which no longer cares
kadamwhite Jul 21, 2020
6cf6f1b
Use strict_types=1 throughout codebase
kadamwhite Jul 21, 2020
0609d7e
Update CHANGELOG to reflect versioning logic changes
kadamwhite Jul 21, 2020
fd70cf2
Clarify usage of the new methods in CHANGELOG
kadamwhite Jul 21, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# EditorConfig is awesome: http://EditorConfig.org

# top-most EditorConfig file
root = true

# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true

[*.{js,ts,jsx,tsx,scss,php,xml}]
# Avoid trailing whitespace
trim_trailing_whitespace = true
# Set default charset for JS files
charset = utf-8
# Tab indentation
indent_style = tab
tab_width = 2
kadamwhite marked this conversation as resolved.
Show resolved Hide resolved

# Use 4-space indentation on composer.json
[{composer.json}]
indent_style = space
indent_size = 4

# Matches the exact files either package.json or .travis.yml
# and overrides them to use 2-space indentation
[{package.json,.travis.yml}]
indent_style = space
indent_size = 2
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ php:
install: composer install

script:
- vendor/bin/phpcs --standard=vendor/humanmade/coding-standards .
- composer run lint
- composer run test
kadamwhite marked this conversation as resolved.
Show resolved Hide resolved
34 changes: 34 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Changelog

## v0.4.0

- **Breaking**: Remove undocumented `Asset_Loader\is_development` method.
- **Breaking**: Remove undocumented `Asset_Loader\enqueue_assets` method.
- **New**: Introduce new `Asset_Loader\register_asset()` and `Asset_Loader\enqueue_asset()` public API.
- Refactor how SSL warning notice behavior gets triggered during asset registration.

## v0.3.4

- Added `composer/installers` as a dependency to permit custom installation paths when installing this package.

## v0.3.3

- Display admin notification about accepting Webpack's SSL certificate if `https://localhost` scripts encounter errors when loading.
- Derive script & style version string from file hash, not `filemtime`.

## v0.3.2

- Do not require plugin files if plugin is already active elsewhere in the project.

## v0.3.1

- Transfer plugin to `humanmade` GitHub organization

## v0.3.0

- Fix bug when loading plugin assets outside of `wp-content/plugins`
- Permit installation with `composer`.

## v0.2.0

- Initial release: introduce `autoregister()` and `autoenqueue()` public API.
47 changes: 43 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,47 @@ This plugin exposes functions which may be used within other WordPress themes or

## Usage

Full documentation to come. See [react-wp-scripts](https://github.com/humanmade/react-wp-scripts) for directional inspiration about how the methods in [plugin.php](plugin.php) may be used.
This library is designed to work in conjunction with a Webpack configuration (such as those created with the presets in [@humanmade/webpack-helpers](https://github.com/humanmade/webpack-helpers)) which generate an asset manifest file. This manifest associates asset bundle names with either URIs pointing to asset bundles on a running DevServer instance, or else local file paths on disk.

Assuming a Webpack configuration (such as those created with the presets in [@humanmade/webpack-helpers](https://github.com/humanmade/webpack-helpers)) which sets up a Webpack DevServer instance for an entrypoint named `editor.js`, and assuming the asset manifest and the build, production version of the `editor.js` file would both end up in a `build/` folder within a theme or plugin, you may load the bundle by using the Asset Loader's autoloader method:
`Asset_Loader` provides a set of methods for reading in this manifest file and registering a specific resource within it to load within your WordPress website. The primary public interface provided by this plugin is a pair of methods, `Asset_Loader\register_asset()` and `Asset_Loader\enqueue_asset()`. To register a manifest asset call one of these methods inside actions like `wp_enqueue_scripts` or `enqueue_block_editor_assets`, in the same manner you would have called the standard WordPress `wp_register_script` or `wp_enqueue_style` functions.

```php
<?php
namespace My_Plugin_Or_Theme;

use Asset_Loader;

add_action( 'enqueue_block_editor_assets', __NAMESPACE__ . '\\enqueue_block_editor_assets' );

/**
* Enqueue the JS and CSS for blocks in the editor.
*
* @return void
*/
function enqueue_block_editor_assets() {
Asset_Loader\enqueue_asset(
// In a plugin, this would be `plugin_dir_path( __FILE__ )` or similar.
get_stylesheet_directory() . '/build/asset-manifest.json',
// The handle of a resource within the manifest. For static file fallbacks,
// this should also match the filename on disk of a build production asset.
'editor.js',
[
'handle' => 'optional-custom-script-handle',
'dependencies' => [ 'wp-element', 'wp-editor' ],
]
);
}
```

To register an asset to be manually enqueued later, use `Asset_Loader\register_asset()` instead of `enqueue_asset()`. Both methods take the same arguments.

If a manifest is not present then `Asset_Loader` will attempt to load the specified resource from the same directory containing the manifest file.

## Migrating from v0.3

Prior to v0.4, the main public interface exposed by this package was a pair of methods named `autoenqueue` and `autoregister`. Internally these methods used a somewhat "magical" and inefficient method of filtering through asset resources. They are deprecated as of v0.4, and will be removed in v0.5.

The following snippet of v0.3-compatible code using `autoenqueue` can be replaced with the `Asset_Loader\enqueue_asset()` example above.

```php
<?php
Expand Down Expand Up @@ -40,13 +78,14 @@ function enqueue_block_editor_assets() {

## Local Development

Before submitting a pull request, ensure that your PHP code passes our [coding standards](https://github.com/humanmade/coding-standards) by running [PHPCS](https://pear.php.net/package/PHP_CodeSniffer):
Before submitting a pull request, ensure that your PHP code passes all existing unit tests and conforms to our [coding standards](https://github.com/humanmade/coding-standards) by running these commands:

```sh
composer run-script lint
composer run-script test
kadamwhite marked this conversation as resolved.
Show resolved Hide resolved
```

If the above command does not work, ensure you have [Composer](https://getcomposer.org/) installed on your machine & run `composer install` from the project root.
If the above commands do not work, ensure you have [Composer](https://getcomposer.org/) installed on your machine & run `composer install` from the project root.

## License

Expand Down
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
"composer/installers": "~1.0"
},
"require-dev": {
"humanmade/coding-standards": "dev-master"
"humanmade/coding-standards": "dev-master",
"10up/wp_mock": "^0.4.2",
"phpunit/phpunit": "^7.5"
},
"scripts": {
"lint": "phpcs --standard=phpcs.ruleset.xml ."
"lint": "phpcs --standard=phpcs.ruleset.xml .",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is PHP_CodeSniffer expected as global dependency? Why is PHPUnit a local one (which I absolutely prefer)?

Since the config file has a default name, it will automagically get picked up. No need to specify:

Suggested change
"lint": "phpcs --standard=phpcs.ruleset.xml .",
"lint": "phpcs .",

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not the same for me. If I do not manually specify the ruleset, it uses a completely different set of standards. Am I possibly using an invalid ruleset name?

Copy link
Contributor

@tfrommen tfrommen Jul 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, right. Sorry, my bad. The supported name would be phpcs.xml(.dist). (Or others.)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahah! Thanks!

"test": "phpunit"
}
}
Loading