Skip to content

DSWP-1300 centralize theme hook registration - #208

Merged
robrien-bcgov merged 4 commits into
mainfrom
refactor/dswp-1300-standardize-block-theme-hook-registration
Oct 28, 2026
Merged

robrien-bcgov merged 4 commits into
mainfrom
refactor/dswp-1300-standardize-block-theme-hook-registration

Conversation

@robrien-bcgov

@robrien-bcgov robrien-bcgov commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor
image

This pull request refactors the theme's initialization process to ensure constructors are side-effect-free and that all WordPress hooks are registered explicitly via a new init method in the Setup class. It also improves test coverage for hook registration and updates how admin notices are managed. Below are the most important changes:

Refactoring Theme Initialization and Hook Registration:

  • Constructors for ThemeSupports, RegisterCustomPatternsPostType, and BcgovSettings no longer register hooks or execute behavior; all hook registration is now centralized in the Setup::init() method. (src/Actions/ThemeSupports.php, src/Actions/RegisterCustomPatternsPostType.php, src/Actions/BcgovSettings.php, [1] [2] [3]
  • The main entry point (functions.php) now creates a Setup instance and calls its init() method, instead of relying on the constructor for hook registration.
  • The Setup class's constructor is replaced with an init() method that registers all necessary actions and filters, ensuring clearer control over when hooks are added. [1] [2]

Testing and Reliability Improvements:

  • Adds a new test class HookRegistrationTest to ensure constructors are side-effect-free and that hooks are only registered after calling Setup::init().

Admin Notices Handling:

  • Updates admin notice instantiation to use the singleton pattern (AdminNotices::get_instance()) throughout the codebase, and removes redundant hook registration from the AdminNotices constructor. (src/Utils/AdminNotices.php, src/Actions/Dependencies.php, [1] [2] [3]

These changes collectively improve code maintainability, testability, and ensure predictable theme initialization.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Taxonomy registration runs after the pattern loader, causing it to create a non-hierarchical fallback that loses ancestor paths in nested pattern category labels.

1 open issue
What changed in this PR

This PR makes theme initialization explicit by moving hook registration into Setup::init() and removing constructor-driven behavior.

Changes:

  • Updates the theme entry point to call Setup::init().
  • Centralizes admin notice rendering and uses a shared notice instance.
  • Adds tests for side-effect-free constructors and explicit hook registration.
File Description
Tests/​Actions/​HookRegistrationTest.php Tests constructor behavior and hook registration through init().
src/​Utils/​AdminNotices.php Removes hook registration from the constructor.
src/​Setup.php Introduces explicit initialization and centralizes hook registration.
src/​Actions/​ThemeSupports.php Removes constructor-triggered theme setup.
src/​Actions/​RegisterCustomPatternsPostType.php Removes constructor-triggered post type registration.
src/​Actions/​Dependencies.php Uses the shared admin notice instance for dependency warnings.
src/​Actions/​BcgovSettings.php Removes constructor-based settings hook registration.
functions.php Calls Setup::init() after creating the setup instance.

Review effort: Balanced

Comment thread src/Setup.php Resolved
robrien-bcgov and others added 3 commits October 28, 2026 12:26
Changed priority of custom pattern registration action.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
The bcgov/wordpress-utils package is distributed via a private Satis
repository that requires authentication in GitHub Actions. Using
--prefer-source instead of --prefer-dist will install from the public
GitHub repository instead, avoiding the HTTP 403 error.

Fixes the PHP lint/test workflow failure in PR #208.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Comment thread .github/workflows/php-lint-test.yml Resolved
@robrien-bcgov
robrien-bcgov requested a balanced review from Copilot October 28, 2026 20:05
@robrien-bcgov
robrien-bcgov marked this pull request as ready for review October 28, 2026 20:05
@robrien-bcgov
robrien-bcgov requested a review from a team as a code owner October 28, 2026 20:05

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues remain for the changes centralizing explicit theme initialization in Setup::init().

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@ShawnTurple ShawnTurple left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks, this looks great

Comment thread .github/workflows/php-lint-test.yml Resolved
@robrien-bcgov
robrien-bcgov merged commit 8a5d501 into main Oct 28, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants