Skip to content

Commit

Permalink
Improve duplicate enqueue detection logic
Browse files Browse the repository at this point in the history
  • Loading branch information
schlessera committed Jan 11, 2020
1 parent 7d4916f commit b2ad12e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [0.3.5] - 2020-01-11
### Fixed
- Improve duplicate enqueue detection logic.

## [0.3.4] - 2020-01-11
### Fixed
- Fix abstract handler method access.
Expand Down Expand Up @@ -95,6 +99,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
### Added
- Initial release to GitHub.

[0.3.5]: https://github.com/brightnucleus/dependencies/compare/v0.3.4...v0.3.5
[0.3.4]: https://github.com/brightnucleus/dependencies/compare/v0.3.3...v0.3.4
[0.3.3]: https://github.com/brightnucleus/dependencies/compare/v0.3.2...v0.3.3
[0.3.2]: https://github.com/brightnucleus/dependencies/compare/v0.3.1...v0.3.2
Expand Down
9 changes: 9 additions & 0 deletions src/AbstractDependencyHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ abstract protected function get_register_function();
* called.
*/
public function enqueue( $args = null ) {
if ( array_key_exists( 'handle', $args )
&& $this->is_enqueued( $args['handle'] ) ) {
return;
}

$this->invokeFunction( $this->get_enqueue_function(), (array) $args );
}

Expand All @@ -81,6 +86,10 @@ abstract protected function get_enqueue_function();
* @return bool Whether the handle was found or not.
*/
public function maybe_enqueue( $handle ) {
if ( $this->is_enqueued( $handle ) ) {
return true;
}

if ( $this->is_registered( $handle ) ) {
$enqueue = $this->get_enqueue_function();
$enqueue( $handle );
Expand Down
12 changes: 7 additions & 5 deletions src/DependencyManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,6 @@ public function enqueue( $context = null ) {
* @return bool Returns whether the handle was found or not.
*/
public function enqueue_handle( $handle, $context = null, $fallback = false ) {
$handler = $this->handlers[ $context['dependency_type'] ];
if ( $handler->is_enqueued( $handle ) ) {
return true;
}

if ( ! $this->enqueue_internal_handle( $handle, $context ) ) {
return $this->enqueue_fallback_handle( $handle );
}
Expand All @@ -260,6 +255,13 @@ protected function enqueue_internal_handle( $handle, $context = null ) {
return false;
}

$handler = array_key_exists( $dependency_type, $this->handlers )
? $this->handlers[ $dependency_type ]
: null;
if ( $handler && $handler->is_enqueued( $handle ) ) {
return true;
}

$this->enqueue_dependency(
$dependency,
$handle,
Expand Down

0 comments on commit b2ad12e

Please sign in to comment.