Skip to content

Commit

Permalink
fix: xWP compatibilty standardization
Browse files Browse the repository at this point in the history
  • Loading branch information
seebeen committed Sep 18, 2024
1 parent 16e3b4e commit 185c1ab
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 108 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ on:
push:
branches:
- master
- develop
- alpha
- 1.x
- beta

jobs:
Expand Down
9 changes: 1 addition & 8 deletions .releaserc
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
{
"branches": [
"master",
{
"name": "develop",
"prerelease": true
},
{
"name": "alpha",
"prerelease": true
},
"1.x",
{
"name": "beta",
"prerelease": true
Expand Down
10 changes: 6 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
"require": {
"php": ">= 8.0",
"automattic/jetpack-constants": "^2.0.1",
"oblak/wp-polyfills": "^1.4.2"
"x-wp/helper-functions": "^1.9"
},
"require-dev": {
"oblak/wordpress-coding-standard": "^1.1.1"
"oblak/wordpress-coding-standard": "^1"
},
"suggest": {
"automattic/jetpack-autoloader": "Allow for better interoperability with other plugins that use this package.",
Expand All @@ -36,8 +36,10 @@
"sort-packages": true,
"allow-plugins": {
"automattic/jetpack-autoloader": true,
"dealerdirect/phpcodesniffer-composer-installer": true,
"ergebnis/composer-normalize": true
"dealerdirect/phpcodesniffer-composer-installer": true
},
"platform": {
"php": "8.0"
}
}
}
111 changes: 61 additions & 50 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 8 additions & 39 deletions src/Traits/Hook_Processor_Trait.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php //phpcs:disable SlevomatCodingStandard.Classes.SuperfluousTraitNaming.SuperfluousSuffix
/**
* Base_Plugin class file.
*
Expand All @@ -9,7 +9,6 @@
namespace Oblak\WP\Traits;

use Oblak\WP\Decorators\Hookable;
use Oblak\WP\Traits\Singleton_Trait;

use function Oblak\WP\Utils\get_decorators;
use function Oblak\WP\Utils\invoke_class_hooks;
Expand All @@ -18,7 +17,6 @@
* Enables basic DI and hooking functionality for plugins / themes
*/
trait Hook_Processor_Trait {

/**
* Plugin textdomain
*
Expand All @@ -33,8 +31,8 @@ trait Hook_Processor_Trait {
* @param int $priority Hook priority.
*/
public function init( string $hook = 'plugins_loaded', int $priority = 10 ) {
add_action( $hook, array( $this, 'run_hooks' ), $priority );
add_action( $hook, array( $this, 'init_dependencies' ), $priority );
\add_action( $hook, array( $this, 'run_hooks' ), $priority );
\add_action( $hook, array( $this, 'init_dependencies' ), $priority );
}

/**
Expand All @@ -55,24 +53,14 @@ public function run_hooks() {
* Initializes the dependency dlasses
*/
public function init_dependencies() {
$di_data = array();

foreach ( $this->get_dependencies() as $dep_class ) {
$dep_data = $this->get_dependency_data( $dep_class );
$dep = $this->get_dependency_data( $dep_class );

if ( ! $dep_data ) {
if ( ! $dep || ! $dep['conditional']() ) {
continue;
}

$di_data[ $dep_data['hook'] ][ $dep_data['priority'] ][] = wp_array_slice_assoc( $dep_data, array( 'classname', 'conditional' ) );
}

foreach ( $di_data as $hook => $priorities ) {
ksort( $priorities );

foreach ( $priorities as $priority => $deps ) {
add_action( $hook, fn() => $this->load_dependencies( $deps ), $priority );
}
\add_action( $dep['hook'], static fn() => new $dep_class(), $dep['priority'] );
}
}

Expand All @@ -84,31 +72,12 @@ public function init_dependencies() {
*/
protected function get_dependency_data( string $dep_class ): ?array {
$metadata = get_decorators( $dep_class, Hookable::class );
$metadata = array_shift( $metadata );
$metadata = \array_shift( $metadata );

return $metadata ? array(
'conditional' => $metadata->conditional ?? '__return_true',
'hook' => $metadata->hook,
'priority' => $metadata->priority,
'classname' => $dep_class,
'conditional' => $metadata->conditional,
) : null;
}


/**
* Loads the dependencies
*
* @param array<string, callable|class-string> $deps Array of dependencies.
*/
protected function load_dependencies( array $deps ) {
$deps = wp_list_pluck(
array_filter(
$deps,
fn( $dep ) => $dep['conditional'](),
),
'classname'
);

array_walk( $deps, fn( $d ) => new $d() );
}
}
2 changes: 1 addition & 1 deletion src/Utils/oblak-wp-hook-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ function xwp_get_hooked_methods( object|string $obj, bool $all = false ): array

return array_filter(
wp_array_flatmap(
$methods,
static fn( $m ) => array(
$m->getName() => array(
'args' => $m->getNumberOfParameters(),
'hooks' => xwp_get_hook_decorators( $m ),
),
),
$methods,
),
static fn( $m ) => $m['hooks']
);
Expand Down
Loading

0 comments on commit 185c1ab

Please sign in to comment.