Added class existence checks to prevent errors - #4588
Conversation
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR hardens the theme against missing/optional classes (likely in partial installs or autoloader failures) by adding guarded loading paths and safer module initialization, and updates admin dashboard helpers to tolerate missing dependencies.
Changes:
- Add
class_exists()/method_exists()guards before instantiating or calling into optional classes across core, customizer, migrations, and CSS generation. - Refactor admin dashboard data helpers to handle absent changelog/plugin helper dependencies.
- Add a PHPUnit test case covering a missing mapped class lookup in the autoloader.
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/test-neve-autoloader.php | Adds a test for missing mapped class behavior in the autoloader. |
| inc/customizer/loader.php | Skips customizer module loading when Factory is unavailable. |
| inc/core/styles/generator.php | Returns empty CSS when Dynamic_Selector is missing. |
| inc/core/settings/mods_migrator.php | Skips builder migration when Builder_Migrator is unavailable. |
| inc/core/factory.php | Avoids calling init() when a built module doesn’t implement it. |
| inc/core/dynamic_css.php | Skips missing legacy style classes; guards generator instantiation and generation. |
| inc/core/core_loader.php | Guards module/admin/front-end hook loading behind class existence checks; extracts hook wiring to private methods. |
| inc/core/admin.php | Skips mods migration when Mods_Migrator is unavailable. |
| inc/admin/dashboard/main.php | Makes dashboard functionality resilient to missing Plugin_Helper / Changelog_Handler by adding null-safe helpers. |
| header-footer-grid/Core/Builder/Abstract_Builder.php | Avoids using Css_Generator in Customizer when the class is missing. |
| globals/migrations.php | Skips migration flags when Migration_Flags is unavailable. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 11 out of 11 changed files in this pull request and generated no new comments.
Suppressed comments (5)
tests/test-neve-autoloader.php:224
mkdir()will emit warnings and can fail the test if the directory already exists (e.g., repeated runs, shared fixture dir). Consider guarding withis_dir()(or using an idempotent helper likewp_mkdir_p()if available), and also clean up the created file/directory at the end of the test (or intearDown()) to avoid cross-test pollution.
mkdir( $this->fixture_dir . 'widgets/', 0777, true );
file_put_contents(
$this->fixture_dir . 'widgets/deferred_widget.php',
'<?php namespace Neve_Fixture\\Widgets; class Deferred_Widget {}'
);
inc/core/factory.php:68
- The
_doing_it_wrong()version parameter is hardcoded to'1.0.0', which can become incorrect and misleading over time. Prefer using the actual theme/plugin version constant (or retrieving it from the theme metadata) so the warning accurately reflects the introducing version.
$message = sprintf(
'Module "%s" was built but does not implement an init() method.',
$this->namespace . $module_name
);
if ( function_exists( '_doing_it_wrong' ) ) {
_doing_it_wrong( __METHOD__, esc_html( $message ), '1.0.0' );
continue;
inc/core/factory.php:69
- If
_doing_it_wrong()is unavailable, the “missing init()” condition becomes completely silent, which makes diagnosing partially-loaded modules difficult in non-WP contexts (e.g., CLI, some tests, or early bootstrap). Consider adding a fallback notification path (e.g.,trigger_error(..., E_USER_WARNING)orerror_log) so the condition remains observable.
if ( function_exists( '_doing_it_wrong' ) ) {
_doing_it_wrong( __METHOD__, esc_html( $message ), '1.0.0' );
continue;
}
}
inc/core/core_loader.php:156
- Returning early here means the theme can run with no modules loaded and no visibility into why, which can be very hard to debug in production. If this guard is intended as a safety net, consider also emitting a diagnostic signal (e.g.,
_doing_it_wrong(),do_action( 'neve_missing_dependency', ... ), or a logged warning) so missing dependencies don’t fail silently.
private function load_modules() {
do_action( 'neve_before_modules_load' );
if ( ! class_exists( Factory::class ) ) {
return;
}
$factory = new Factory( $this->features );
inc/core/styles/generator.php:48
echo ''is a no-op and can be removed to simplify the control flow. With$echo === true, an earlyreturn;is sufficient; with$echo === false, returning''already covers callers expecting a string.
if ( ! class_exists( Dynamic_Selector::class ) ) {
if ( ! $echo ) {
return '';
}
echo '';
return;
}
Summary
Added a guard to check if the class exists before instantiating it. This prevents fatal errors when the class is not available.
Check before Pull Request is ready:
Closes #4576