Skip to content

Commit

Permalink
fix(widgets): Fix widget field registration (Fixes #22) (#24)
Browse files Browse the repository at this point in the history
* chore(block): Return the `register()` method
* chore(field): Return the `register()` method
* enhance(acf-composer): Wrap the composer `boot()` method in an `init` filter for compatibility
  • Loading branch information
Log1x authored Jul 7, 2020
1 parent cf16366 commit 814d6a5
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public function compose()
}
]);

$this->register();
return $this->register();
}

/**
Expand Down
8 changes: 3 additions & 5 deletions src/Composer.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,9 @@ protected function register()
return;
}

add_filter('init', function () {
return acf_add_local_field_group(
$this->build($this->fields)
);
}, 20);
return acf_add_local_field_group(
$this->build($this->fields)
);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public function compose()
return;
}

$this->register();
return $this->register();
}
}
28 changes: 15 additions & 13 deletions src/Providers/AcfComposerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,22 @@ public function boot()
*/
public function compose()
{
foreach ((new Finder())->in($this->paths->all())->files() as $composer) {
$composer = $this->app->getNamespace() . str_replace(
['/', '.php'],
['\\', ''],
Str::after($composer->getPathname(), $this->app->path() . DIRECTORY_SEPARATOR)
);
add_filter('init', function () {
foreach ((new Finder())->in($this->paths->all())->files() as $composer) {
$composer = $this->app->getNamespace() . str_replace(
['/', '.php'],
['\\', ''],
Str::after($composer->getPathname(), $this->app->path() . DIRECTORY_SEPARATOR)
);

if (
is_subclass_of($composer, Composer::class) &&
! is_subclass_of($composer, Partial::class) &&
! (new ReflectionClass($composer))->isAbstract()
) {
(new $composer($this->app))->compose();
if (
is_subclass_of($composer, Composer::class) &&
! is_subclass_of($composer, Partial::class) &&
! (new ReflectionClass($composer))->isAbstract()
) {
(new $composer($this->app))->compose();
}
}
}
}, 0);
}
}
30 changes: 19 additions & 11 deletions src/Widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,28 @@ public function compose()

add_filter('widgets_init', function () {
register_widget($this->widget());
});

$this->widget = (object) collect(
Arr::get($GLOBALS, 'wp_registered_widgets')
)->filter(function ($value) {
return $value['name'] == $this->name;
})->pop();
$this->widget = (object) collect(
Arr::get($GLOBALS, 'wp_registered_widgets')
)->filter(function ($value) {
return $value['name'] == $this->name;
})->pop();

if (empty($this->widget) || empty($this->widget->id)) {
return;
}
if (empty($this->widget) || empty($this->widget->id)) {
return;
}

$this->widget->id = Str::start($this->widget->id, 'widget_');
$this->id = $this->widget->id;
$this->widget->id = Str::start($this->widget->id, 'widget_');
$this->id = $this->widget->id;
});

if (! Arr::has($this->fields, 'location.0.0')) {
Arr::set($this->fields, 'location.0.0', [
'param' => 'widget',
'operator' => '==',
'value' => $this->slug,
]);
}

return $this->register();
}
Expand Down

0 comments on commit 814d6a5

Please sign in to comment.