Skip to content

Commit 634dd9d

Browse files
authored
Merge pull request #1237 from woocommerce/fix/1236-version-information
Make version/source information available via new class
2 parents 79e8eaa + 412f067 commit 634dd9d

File tree

4 files changed

+122
-57
lines changed

4 files changed

+122
-57
lines changed

classes/ActionScheduler_AdminView.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,8 @@ public function add_help_tabs() {
251251
}
252252

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

258258
if ( ! empty( $as_source ) ) {
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
/**
4+
* Provides information about active and registered instances of Action Scheduler.
5+
*/
6+
class ActionScheduler_SystemInformation {
7+
/**
8+
* Returns information about the plugin or theme which contains the current active version
9+
* of Action Scheduler.
10+
*
11+
* If this cannot be determined, or if Action Scheduler is being loaded via some other
12+
* method, then it will return an empty array. Otherwise, if populated, the array will
13+
* look like the following:
14+
*
15+
* [
16+
* 'type' => 'plugin', # or 'theme'
17+
* 'name' => 'Name',
18+
* ]
19+
*
20+
* @return array
21+
*/
22+
public static function active_source(): array {
23+
$plugins = get_plugins();
24+
$plugin_files = array_keys( $plugins );
25+
26+
foreach ( $plugin_files as $plugin_file ) {
27+
$plugin_path = trailingslashit( WP_PLUGIN_DIR ) . dirname( $plugin_file );
28+
$plugin_file = trailingslashit( WP_PLUGIN_DIR ) . $plugin_file;
29+
30+
if ( 0 !== strpos( dirname( __DIR__ ), $plugin_path ) ) {
31+
continue;
32+
}
33+
34+
$plugin_data = get_plugin_data( $plugin_file );
35+
36+
if ( ! is_array( $plugin_data ) || empty( $plugin_data['Name'] ) ) {
37+
continue;
38+
}
39+
40+
return array(
41+
'type' => 'plugin',
42+
'name' => $plugin_data['Name'],
43+
);
44+
}
45+
46+
$themes = (array) search_theme_directories();
47+
48+
foreach ( $themes as $slug => $data ) {
49+
$needle = trailingslashit( $data['theme_root'] ) . $slug . '/';
50+
51+
if ( 0 !== strpos( __FILE__, $needle ) ) {
52+
continue;
53+
}
54+
55+
$theme = wp_get_theme( $slug );
56+
57+
if ( ! is_object( $theme ) || ! is_a( $theme, \WP_Theme::class ) ) {
58+
continue;
59+
}
60+
61+
return array(
62+
'type' => 'theme',
63+
// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
64+
'name' => $theme->Name,
65+
);
66+
}
67+
68+
return array();
69+
}
70+
71+
/**
72+
* Returns the directory path for the currently active installation of Action Scheduler.
73+
*
74+
* @return string
75+
*/
76+
public static function active_source_path(): string {
77+
return trailingslashit( dirname( __DIR__ ) );
78+
}
79+
80+
/**
81+
* Get registered sources.
82+
*
83+
* It is not always possible to obtain this information. For instance, if earlier versions (<=3.9.0) of
84+
* Action Scheduler register themselves first, then the necessary data about registered sources will
85+
* not be available.
86+
*
87+
* @return array<string, string>
88+
*/
89+
public static function get_sources() {
90+
$versions = ActionScheduler_Versions::instance();
91+
return method_exists( $versions, 'get_sources' ) ? $versions->get_sources() : array();
92+
}
93+
}

classes/ActionScheduler_Versions.php

Lines changed: 14 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ public function get_versions() {
5555
/**
5656
* Get registered sources.
5757
*
58+
* Use with caution: this method is only available as of Action Scheduler's 3.9.1
59+
* release and, owing to the way Action Scheduler is loaded, it's possible that the
60+
* class definition used at runtime will belong to an earlier version.
61+
*
62+
* @since 3.9.1
63+
*
5864
* @return array<string, string>
5965
*/
6066
public function get_sources() {
@@ -122,65 +128,24 @@ public static function initialize_latest_version() {
122128
* 'name' => 'Name',
123129
* ]
124130
*
131+
* @deprecated 3.9.2 Use ActionScheduler_SystemInformation::active_source().
132+
*
125133
* @return array
126134
*/
127135
public function active_source(): array {
128-
$file = __FILE__;
129-
$dir = __DIR__;
130-
$plugins = get_plugins();
131-
$plugin_files = array_keys( $plugins );
132-
133-
foreach ( $plugin_files as $plugin_file ) {
134-
$plugin_path = trailingslashit( WP_PLUGIN_DIR ) . dirname( $plugin_file );
135-
$plugin_file = trailingslashit( WP_PLUGIN_DIR ) . $plugin_file;
136-
137-
if ( 0 !== strpos( dirname( $dir ), $plugin_path ) ) {
138-
continue;
139-
}
140-
141-
$plugin_data = get_plugin_data( $plugin_file );
142-
143-
if ( ! is_array( $plugin_data ) || empty( $plugin_data['Name'] ) ) {
144-
continue;
145-
}
146-
147-
return array(
148-
'type' => 'plugin',
149-
'name' => $plugin_data['Name'],
150-
);
151-
}
152-
153-
$themes = (array) search_theme_directories();
154-
155-
foreach ( $themes as $slug => $data ) {
156-
$needle = trailingslashit( $data['theme_root'] ) . $slug . '/';
157-
158-
if ( 0 !== strpos( $file, $needle ) ) {
159-
continue;
160-
}
161-
162-
$theme = wp_get_theme( $slug );
163-
164-
if ( ! is_object( $theme ) || ! is_a( $theme, \WP_Theme::class ) ) {
165-
continue;
166-
}
167-
168-
return array(
169-
'type' => 'theme',
170-
// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
171-
'name' => $theme->Name,
172-
);
173-
}
174-
175-
return array();
136+
_deprecated_function( __METHOD__, '3.9.2', 'ActionScheduler_SystemInformation::active_source()' );
137+
return ActionScheduler_SystemInformation::active_source();
176138
}
177139

178140
/**
179141
* Returns the directory path for the currently active installation of Action Scheduler.
180142
*
143+
* @deprecated 3.9.2 Use ActionScheduler_SystemInformation::active_source_path().
144+
*
181145
* @return string
182146
*/
183147
public function active_source_path(): string {
184-
return trailingslashit( dirname( __DIR__ ) );
148+
_deprecated_function( __METHOD__, '3.9.2', 'ActionScheduler_SystemInformation::active_source_path()' );
149+
return ActionScheduler_SystemInformation::active_source_path();
185150
}
186151
}

classes/WP_CLI/System_Command.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

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

7+
use ActionScheduler_SystemInformation;
8+
use WP_CLI;
79
use function \WP_CLI\Utils\get_flag_value;
810

911
/**
@@ -99,7 +101,7 @@ public function status( array $args, array $assoc_args ) {
99101
*/
100102
public function version( array $args, array $assoc_args ) {
101103
$all = (bool) get_flag_value( $assoc_args, 'all' );
102-
$latest = $this->get_latest_version( $instance );
104+
$latest = $this->get_latest_version();
103105

104106
if ( ! $all ) {
105107
echo $latest;
@@ -139,16 +141,15 @@ public function version( array $args, array $assoc_args ) {
139141
*
140142
* @param array $args Positional args.
141143
* @param array $assoc_args Keyed args.
142-
* @uses \ActionScheduler_Versions::get_sources()
144+
* @uses ActionScheduler_SystemInformation::active_source_path()
143145
* @uses \WP_CLI\Formatter::display_items()
144146
* @uses $this->get_latest_version()
145147
* @return void
146148
*/
147149
public function source( array $args, array $assoc_args ) {
148150
$all = (bool) get_flag_value( $assoc_args, 'all' );
149151
$fullpath = (bool) get_flag_value( $assoc_args, 'fullpath' );
150-
$versions = \ActionScheduler_Versions::instance();
151-
$source = $versions->active_source_path();
152+
$source = ActionScheduler_SystemInformation::active_source_path();
152153
$path = $source;
153154

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

163-
$sources = $versions->get_sources();
164-
$rows = array();
164+
$sources = ActionScheduler_SystemInformation::get_sources();
165+
166+
if ( empty( $sources ) ) {
167+
WP_CLI::log( __( 'Detailed information about registered sources is not currently available.', 'action-scheduler' ) );
168+
return;
169+
}
170+
171+
$rows = array();
165172

166173
foreach ( $sources as $check_source => $version ) {
167174
$active = dirname( $check_source ) === $source;

0 commit comments

Comments
 (0)