|
| 1 | +<?php |
| 2 | +namespace WP_CLI\SQLite; |
| 3 | + |
| 4 | +use WP_CLI; |
| 5 | + |
| 6 | +class Base { |
| 7 | + |
| 8 | + protected $unsupported_arguments = []; |
| 9 | + |
| 10 | + /** |
| 11 | + * Get the version of the SQLite integration plugin if it is installed |
| 12 | + * and activated. |
| 13 | + * |
| 14 | + * @return false|string The version of the SQLite integration plugin or false if not found/activated. |
| 15 | + */ |
| 16 | + public static function get_sqlite_plugin_version() { |
| 17 | + // Check if there is a db.php file in the wp-content directory. |
| 18 | + if ( ! file_exists( ABSPATH . '/wp-content/db.php' ) ) { |
| 19 | + return false; |
| 20 | + } |
| 21 | + |
| 22 | + // If the file is found, we need to check that it is the sqlite integration plugin. |
| 23 | + $plugin_file = file_get_contents( ABSPATH . '/wp-content/db.php' ); |
| 24 | + if ( ! preg_match( '/define\( \'SQLITE_DB_DROPIN_VERSION\', \'([0-9.]+)\' \)/', $plugin_file ) ) { |
| 25 | + return false; |
| 26 | + } |
| 27 | + |
| 28 | + $plugin_path = self::get_plugin_directory(); |
| 29 | + if ( ! $plugin_path ) { |
| 30 | + return false; |
| 31 | + } |
| 32 | + |
| 33 | + // Try to get the version number from readme.txt |
| 34 | + $plugin_file = file_get_contents( $plugin_path . '/readme.txt' ); |
| 35 | + |
| 36 | + preg_match( '/^Stable tag:\s*?(.+)$/m', $plugin_file, $matches ); |
| 37 | + |
| 38 | + return isset( $matches[1] ) ? trim( $matches[1] ) : false; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Find the directory where the SQLite integration plugin is installed. |
| 43 | + * |
| 44 | + * @return string|null The directory where the SQLite integration plugin is installed or null if not found. |
| 45 | + */ |
| 46 | + protected static function get_plugin_directory() { |
| 47 | + $plugin_folders = [ |
| 48 | + ABSPATH . '/wp-content/plugins/sqlite-database-integration', |
| 49 | + ABSPATH . '/wp-content/mu-plugins/sqlite-database-integration', |
| 50 | + ]; |
| 51 | + |
| 52 | + foreach ( $plugin_folders as $folder ) { |
| 53 | + if ( file_exists( $folder ) && is_dir( $folder ) ) { |
| 54 | + return $folder; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + return null; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Load the necessary classes from the SQLite integration plugin. |
| 63 | + * |
| 64 | + * @return void |
| 65 | + * @throws WP_CLI\ExitException |
| 66 | + */ |
| 67 | + protected function load_dependencies() { |
| 68 | + $plugin_directory = self::get_plugin_directory(); |
| 69 | + if ( ! $plugin_directory ) { |
| 70 | + WP_CLI::error( 'Could not locate the SQLite integration plugin.' ); |
| 71 | + } |
| 72 | + |
| 73 | + $sqlite_plugin_version = self::get_sqlite_plugin_version(); |
| 74 | + if ( ! $sqlite_plugin_version ) { |
| 75 | + WP_CLI::error( 'Could not determine the version of the SQLite integration plugin.' ); |
| 76 | + } |
| 77 | + |
| 78 | + if ( version_compare( $sqlite_plugin_version, '2.1.11', '<' ) ) { |
| 79 | + WP_CLI::error( 'The SQLite integration plugin must be version 2.1.11 or higher.' ); |
| 80 | + } |
| 81 | + |
| 82 | + // Load the translator class from the plugin. |
| 83 | + if ( ! defined( 'SQLITE_DB_DROPIN_VERSION' ) ) { |
| 84 | + define( 'SQLITE_DB_DROPIN_VERSION', $sqlite_plugin_version ); // phpcs:ignore |
| 85 | + } |
| 86 | + |
| 87 | + // We also need to selectively load the necessary classes from the plugin. |
| 88 | + require_once $plugin_directory . '/php-polyfills.php'; |
| 89 | + require_once $plugin_directory . '/constants.php'; |
| 90 | + require_once $plugin_directory . '/wp-includes/sqlite/class-wp-sqlite-lexer.php'; |
| 91 | + require_once $plugin_directory . '/wp-includes/sqlite/class-wp-sqlite-query-rewriter.php'; |
| 92 | + require_once $plugin_directory . '/wp-includes/sqlite/class-wp-sqlite-translator.php'; |
| 93 | + require_once $plugin_directory . '/wp-includes/sqlite/class-wp-sqlite-token.php'; |
| 94 | + require_once $plugin_directory . '/wp-includes/sqlite/class-wp-sqlite-pdo-user-defined-functions.php'; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Check if the arguments passed to the command are supported. |
| 99 | + * |
| 100 | + * @param $args |
| 101 | + * |
| 102 | + * @return void |
| 103 | + * @throws WP_CLI\ExitException |
| 104 | + */ |
| 105 | + protected function check_arguments( $args ) { |
| 106 | + if ( array_intersect_key( $args, array_flip( $this->unsupported_arguments ) ) ) { |
| 107 | + WP_CLI::error( |
| 108 | + sprintf( |
| 109 | + 'The following arguments are not supported by SQLite exports: %s', |
| 110 | + implode( ', ', $this->unsupported_arguments ) |
| 111 | + ) |
| 112 | + ); |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments