Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conditionally load PHP classes #10531

Merged
merged 1 commit into from
Oct 11, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,34 @@
// These files only need to be loaded if within a rest server instance
// which this class will exist if that is the case.
if ( class_exists( 'WP_REST_Controller' ) ) {
require dirname( __FILE__ ) . '/class-wp-rest-blocks-controller.php';
require dirname( __FILE__ ) . '/class-wp-rest-autosaves-controller.php';
require dirname( __FILE__ ) . '/class-wp-rest-block-renderer-controller.php';
require dirname( __FILE__ ) . '/class-wp-rest-search-controller.php';
require dirname( __FILE__ ) . '/class-wp-rest-search-handler.php';
require dirname( __FILE__ ) . '/class-wp-rest-post-search-handler.php';
if ( ! class_exists( 'WP_REST_Blocks_Controller' ) ) {
Copy link

Choose a reason for hiding this comment

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

I'm curious if there's a reason why we use these class checks, rather than require_once for every class?

Copy link
Member Author

Choose a reason for hiding this comment

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

@kwight require_once works based on file path, not the class defined within the file. Because WordPress core's paths are different, you'd end up with fatals if you used require_once.

require dirname( __FILE__ ) . '/class-wp-rest-blocks-controller.php';
}
if ( ! class_exists( 'WP_REST_Autosaves_Controller' ) ) {
require dirname( __FILE__ ) . '/class-wp-rest-autosaves-controller.php';
}
if ( ! class_exists( 'WP_REST_Block_Renderer_Controller' ) ) {
require dirname( __FILE__ ) . '/class-wp-rest-block-renderer-controller.php';
}
if ( ! class_exists( 'WP_REST_Search_Controller' ) ) {
require dirname( __FILE__ ) . '/class-wp-rest-search-controller.php';
}
if ( ! class_exists( 'WP_REST_Search_Handler' ) ) {
require dirname( __FILE__ ) . '/class-wp-rest-search-handler.php';
}
if ( ! class_exists( 'WP_REST_Post_Search_Handler' ) ) {
require dirname( __FILE__ ) . '/class-wp-rest-post-search-handler.php';
}
require dirname( __FILE__ ) . '/rest-api.php';
}

require dirname( __FILE__ ) . '/meta-box-partial-page.php';
require dirname( __FILE__ ) . '/class-wp-block-type.php';
require dirname( __FILE__ ) . '/class-wp-block-type-registry.php';
if ( ! class_exists( 'WP_Block_Type' ) ) {
require dirname( __FILE__ ) . '/class-wp-block-type.php';
}
if ( ! class_exists( 'WP_Block_Type_Registry' ) ) {
require dirname( __FILE__ ) . '/class-wp-block-type-registry.php';
}
require dirname( __FILE__ ) . '/blocks.php';
require dirname( __FILE__ ) . '/client-assets.php';
require dirname( __FILE__ ) . '/compat.php';
Expand Down