Skip to content

Commit

Permalink
Add namespace to prevent linting error and move noop functions out of…
Browse files Browse the repository at this point in the history
… the namespace to ensure they are global
  • Loading branch information
jeroenpf committed Jul 23, 2024
1 parent 963fc04 commit c23bd71
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 9 deletions.
2 changes: 2 additions & 0 deletions src/DB_Command.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use WP_CLI\DB\WP_SQLite_Export;
use WP_CLI\DB\WP_SQLite_Import;
use WP_CLI\Formatter;
use WP_CLI\Utils;

Expand Down
17 changes: 8 additions & 9 deletions src/WP_SQLite_Base.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
namespace WP_CLI\DB;

class WP_SQLite_Base {

Expand All @@ -23,21 +24,19 @@ public static function get_sqlite_version() {
protected function load_dependencies() {
$plugin_directory = $this->get_plugin_directory();
if ( ! $plugin_directory ) {
throw new Exception( 'Could not locate the SQLite integration plugin.' );
throw new \Exception( 'Could not locate the SQLite integration plugin.' );
}

// Load the translator class from the plugin.
if ( ! defined( 'SQLITE_DB_DROPIN_VERSION' ) ) {
define( 'SQLITE_DB_DROPIN_VERSION', self::get_sqlite_version() ); // phpcs:ignore
}

# A hack to add the do_action and apply_filters functions to the global namespace.
# This is necessary because during the import WP has not been loaded, and we
# need to define these functions to avoid fatal errors.
if ( ! function_exists( 'do_action' ) ) {
function do_action() {} // phpcs:ignore
function apply_filters() {} // phpcs:ignore
}
# WordPress is not loaded during the execution of the export and import commands.
# The SQLite database integration plugin uses do_action and apply_filters to hook
# into the WordPress core. To prevent a fatal error, we can define these functions
# as no-op functions.
require_once __DIR__ . '/noop.php';

// We also need to selectively load the necessary classes from the plugin.
require_once $plugin_directory . '/php-polyfills.php';
Expand Down Expand Up @@ -69,7 +68,7 @@ protected function get_plugin_directory() {

protected function check_arguments( $args ) {
if ( array_intersect_key( $args, array_flip( $this->unsupported_arguments ) ) ) {
WP_CLI::error(
\WP_CLI::error(
sprintf(
'The following arguments are not supported by SQLite exports: %s',
implode( ', ', $this->unsupported_arguments )
Expand Down
6 changes: 6 additions & 0 deletions src/WP_SQLite_Export.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
<?php
namespace WP_CLI\DB;

use Exception;
use PDO;
use WP_CLI;
use WP_SQLite_Translator;

class WP_SQLite_Export extends WP_SQLite_Base {

Expand Down
6 changes: 6 additions & 0 deletions src/WP_SQLite_Import.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
<?php
namespace WP_CLI\DB;

use Exception;
use Generator;
use WP_CLI;
use WP_SQLite_Translator;

class WP_SQLite_Import extends WP_SQLite_Base {

Expand Down
15 changes: 15 additions & 0 deletions src/noop.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* While using the SQLite database integration plugin for the import and export command, do_action and
* apply_filters are used to hook into the WordPress core. These functions might not be available in
* the context of the import and export commands of the WP-CLI. To prevent the fatal error, we can
* define these functions as no-op functions.
*/

if ( ! function_exists( 'do_action' ) ) {
function do_action() {} // phpcs:ignore
}

if ( ! function_exists( 'apply_filters' ) ) {
function apply_filters() {} // phpcs:ignore
}

0 comments on commit c23bd71

Please sign in to comment.