Skip to content

Commit 61e6de2

Browse files
committed
Make it safer to access information about registered Action Scheduler versions/sources.
The problem with making this information available via ActionScheduler_Versions is that we don't know which version of that class will be loaded at runtime, therefore we can't safely access newly added methods or properties (or, not directly).
1 parent 3ed89cb commit 61e6de2

File tree

3 files changed

+96
-7
lines changed

3 files changed

+96
-7
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: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
* @return array<string, string>
84+
*/
85+
public static function get_sources() {
86+
$versions = ActionScheduler_Versions::instance();
87+
return method_exists( $versions, 'get_sources' ) ? $versions->get_sources() : array();
88+
}
89+
}

classes/WP_CLI/System_Command.php

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

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

7+
use ActionScheduler_SystemInformation;
78
use function \WP_CLI\Utils\get_flag_value;
89

910
/**
@@ -99,7 +100,7 @@ public function status( array $args, array $assoc_args ) {
99100
*/
100101
public function version( array $args, array $assoc_args ) {
101102
$all = (bool) get_flag_value( $assoc_args, 'all' );
102-
$latest = $this->get_latest_version( $instance );
103+
$latest = $this->get_latest_version();
103104

104105
if ( ! $all ) {
105106
echo $latest;
@@ -139,16 +140,15 @@ public function version( array $args, array $assoc_args ) {
139140
*
140141
* @param array $args Positional args.
141142
* @param array $assoc_args Keyed args.
142-
* @uses \ActionScheduler_Versions::get_sources()
143+
* @uses ActionScheduler_SystemInformation::active_source_path()
143144
* @uses \WP_CLI\Formatter::display_items()
144145
* @uses $this->get_latest_version()
145146
* @return void
146147
*/
147148
public function source( array $args, array $assoc_args ) {
148149
$all = (bool) get_flag_value( $assoc_args, 'all' );
149150
$fullpath = (bool) get_flag_value( $assoc_args, 'fullpath' );
150-
$versions = \ActionScheduler_Versions::instance();
151-
$source = $versions->active_source_path();
151+
$source = ActionScheduler_SystemInformation::active_source_path();
152152
$path = $source;
153153

154154
if ( ! $fullpath ) {
@@ -160,7 +160,7 @@ public function source( array $args, array $assoc_args ) {
160160
\WP_CLI::halt( 0 );
161161
}
162162

163-
$sources = $versions->get_sources();
163+
$sources = ActionScheduler_SystemInformation::get_sources();
164164
$rows = array();
165165

166166
foreach ( $sources as $check_source => $version ) {

0 commit comments

Comments
 (0)