Skip to content

Commit

Permalink
Merge pull request #17 from alleyinteractive/fix/issue-16/global-vari…
Browse files Browse the repository at this point in the history
…ables

Globalize new variables after loading plugin file
  • Loading branch information
dlh01 authored Feb 26, 2025
2 parents af1c79c + ad7272b commit 41c33c7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to `WP Plugin Loader` will be documented in this file.

## 0.2.0 - 2025-02-26

- Globalize variables after including the plugin file.

## 0.1.6 - 2024-12-17

- When a plugin is not found, exit with a status code of 1 and send a `500`
Expand Down
25 changes: 25 additions & 0 deletions src/class-wp-plugin-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,33 @@ protected function load_plugins(): void {
* @return void
*/
protected function handle_plugin_path( string $path ): void {
// What follows is mostly a copy of _wpcom_vip_include_plugin().

// Start by marking down the currently defined variables (so we can exclude them later).
$pre_include_variables = get_defined_vars();

// Support symlinks.
wp_register_plugin_realpath( $path );

// Now include.
require_once $path; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingVariable

// Disallow some global variables.
$disallowed_globals = [
'blacklist' => 0,
'pre_include_variables' => 0,
'new_variables' => 0,
'helper_file' => 0,
];

// Let's find out what's new by comparing the current variables to the previous ones.
$new_variables = array_diff_key( get_defined_vars(), $GLOBALS, $disallowed_globals, $pre_include_variables );

// Globalize each new variable.
foreach ( $new_variables as $new_variable => $value ) {
$GLOBALS[ $new_variable ] = $value; // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
}

// Mark the plugin as loaded if it is in the /plugins directory.
if ( 0 === strpos( $path, WP_PLUGIN_DIR ) ) {
$this->loaded_plugins[] = trim( substr( $path, strlen( WP_PLUGIN_DIR ) + 1 ), '/' );
Expand Down

0 comments on commit 41c33c7

Please sign in to comment.