Skip to content

Commit

Permalink
Merge pull request #1237 from woocommerce/fix/1236-version-information
Browse files Browse the repository at this point in the history
Make version/source information available via new class
  • Loading branch information
jorgeatorres authored Jan 31, 2025
2 parents 79e8eaa + 412f067 commit 634dd9d
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 57 deletions.
4 changes: 2 additions & 2 deletions classes/ActionScheduler_AdminView.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,8 @@ public function add_help_tabs() {
}

$as_version = ActionScheduler_Versions::instance()->latest_version();
$as_source = ActionScheduler_Versions::instance()->active_source();
$as_source_path = ActionScheduler_Versions::instance()->active_source_path();
$as_source = ActionScheduler_SystemInformation::active_source();
$as_source_path = ActionScheduler_SystemInformation::active_source_path();
$as_source_markup = sprintf( '<code>%s</code>', esc_html( $as_source_path ) );

if ( ! empty( $as_source ) ) {
Expand Down
93 changes: 93 additions & 0 deletions classes/ActionScheduler_SystemInformation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

/**
* Provides information about active and registered instances of Action Scheduler.
*/
class ActionScheduler_SystemInformation {
/**
* Returns information about the plugin or theme which contains the current active version
* of Action Scheduler.
*
* If this cannot be determined, or if Action Scheduler is being loaded via some other
* method, then it will return an empty array. Otherwise, if populated, the array will
* look like the following:
*
* [
* 'type' => 'plugin', # or 'theme'
* 'name' => 'Name',
* ]
*
* @return array
*/
public static function active_source(): array {
$plugins = get_plugins();
$plugin_files = array_keys( $plugins );

foreach ( $plugin_files as $plugin_file ) {
$plugin_path = trailingslashit( WP_PLUGIN_DIR ) . dirname( $plugin_file );
$plugin_file = trailingslashit( WP_PLUGIN_DIR ) . $plugin_file;

if ( 0 !== strpos( dirname( __DIR__ ), $plugin_path ) ) {
continue;
}

$plugin_data = get_plugin_data( $plugin_file );

if ( ! is_array( $plugin_data ) || empty( $plugin_data['Name'] ) ) {
continue;
}

return array(
'type' => 'plugin',
'name' => $plugin_data['Name'],
);
}

$themes = (array) search_theme_directories();

foreach ( $themes as $slug => $data ) {
$needle = trailingslashit( $data['theme_root'] ) . $slug . '/';

if ( 0 !== strpos( __FILE__, $needle ) ) {
continue;
}

$theme = wp_get_theme( $slug );

if ( ! is_object( $theme ) || ! is_a( $theme, \WP_Theme::class ) ) {
continue;
}

return array(
'type' => 'theme',
// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
'name' => $theme->Name,
);
}

return array();
}

/**
* Returns the directory path for the currently active installation of Action Scheduler.
*
* @return string
*/
public static function active_source_path(): string {
return trailingslashit( dirname( __DIR__ ) );
}

/**
* Get registered sources.
*
* It is not always possible to obtain this information. For instance, if earlier versions (<=3.9.0) of
* Action Scheduler register themselves first, then the necessary data about registered sources will
* not be available.
*
* @return array<string, string>
*/
public static function get_sources() {
$versions = ActionScheduler_Versions::instance();
return method_exists( $versions, 'get_sources' ) ? $versions->get_sources() : array();
}
}
63 changes: 14 additions & 49 deletions classes/ActionScheduler_Versions.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ public function get_versions() {
/**
* Get registered sources.
*
* Use with caution: this method is only available as of Action Scheduler's 3.9.1
* release and, owing to the way Action Scheduler is loaded, it's possible that the
* class definition used at runtime will belong to an earlier version.
*
* @since 3.9.1
*
* @return array<string, string>
*/
public function get_sources() {
Expand Down Expand Up @@ -122,65 +128,24 @@ public static function initialize_latest_version() {
* 'name' => 'Name',
* ]
*
* @deprecated 3.9.2 Use ActionScheduler_SystemInformation::active_source().
*
* @return array
*/
public function active_source(): array {
$file = __FILE__;
$dir = __DIR__;
$plugins = get_plugins();
$plugin_files = array_keys( $plugins );

foreach ( $plugin_files as $plugin_file ) {
$plugin_path = trailingslashit( WP_PLUGIN_DIR ) . dirname( $plugin_file );
$plugin_file = trailingslashit( WP_PLUGIN_DIR ) . $plugin_file;

if ( 0 !== strpos( dirname( $dir ), $plugin_path ) ) {
continue;
}

$plugin_data = get_plugin_data( $plugin_file );

if ( ! is_array( $plugin_data ) || empty( $plugin_data['Name'] ) ) {
continue;
}

return array(
'type' => 'plugin',
'name' => $plugin_data['Name'],
);
}

$themes = (array) search_theme_directories();

foreach ( $themes as $slug => $data ) {
$needle = trailingslashit( $data['theme_root'] ) . $slug . '/';

if ( 0 !== strpos( $file, $needle ) ) {
continue;
}

$theme = wp_get_theme( $slug );

if ( ! is_object( $theme ) || ! is_a( $theme, \WP_Theme::class ) ) {
continue;
}

return array(
'type' => 'theme',
// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
'name' => $theme->Name,
);
}

return array();
_deprecated_function( __METHOD__, '3.9.2', 'ActionScheduler_SystemInformation::active_source()' );
return ActionScheduler_SystemInformation::active_source();
}

/**
* Returns the directory path for the currently active installation of Action Scheduler.
*
* @deprecated 3.9.2 Use ActionScheduler_SystemInformation::active_source_path().
*
* @return string
*/
public function active_source_path(): string {
return trailingslashit( dirname( __DIR__ ) );
_deprecated_function( __METHOD__, '3.9.2', 'ActionScheduler_SystemInformation::active_source_path()' );
return ActionScheduler_SystemInformation::active_source_path();
}
}
19 changes: 13 additions & 6 deletions classes/WP_CLI/System_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaping output is not necessary in WP CLI.

use ActionScheduler_SystemInformation;
use WP_CLI;
use function \WP_CLI\Utils\get_flag_value;

/**
Expand Down Expand Up @@ -99,7 +101,7 @@ public function status( array $args, array $assoc_args ) {
*/
public function version( array $args, array $assoc_args ) {
$all = (bool) get_flag_value( $assoc_args, 'all' );
$latest = $this->get_latest_version( $instance );
$latest = $this->get_latest_version();

if ( ! $all ) {
echo $latest;
Expand Down Expand Up @@ -139,16 +141,15 @@ public function version( array $args, array $assoc_args ) {
*
* @param array $args Positional args.
* @param array $assoc_args Keyed args.
* @uses \ActionScheduler_Versions::get_sources()
* @uses ActionScheduler_SystemInformation::active_source_path()
* @uses \WP_CLI\Formatter::display_items()
* @uses $this->get_latest_version()
* @return void
*/
public function source( array $args, array $assoc_args ) {
$all = (bool) get_flag_value( $assoc_args, 'all' );
$fullpath = (bool) get_flag_value( $assoc_args, 'fullpath' );
$versions = \ActionScheduler_Versions::instance();
$source = $versions->active_source_path();
$source = ActionScheduler_SystemInformation::active_source_path();
$path = $source;

if ( ! $fullpath ) {
Expand All @@ -160,8 +161,14 @@ public function source( array $args, array $assoc_args ) {
\WP_CLI::halt( 0 );
}

$sources = $versions->get_sources();
$rows = array();
$sources = ActionScheduler_SystemInformation::get_sources();

if ( empty( $sources ) ) {
WP_CLI::log( __( 'Detailed information about registered sources is not currently available.', 'action-scheduler' ) );
return;
}

$rows = array();

foreach ( $sources as $check_source => $version ) {
$active = dirname( $check_source ) === $source;
Expand Down

0 comments on commit 634dd9d

Please sign in to comment.