Skip to content

Commit 2b53f9f

Browse files
authored
Merge pull request #1222 from crstauf/help-source-name
Display name of source of Action Scheduler library in the Help pulldown.
2 parents 903e424 + 36475cc commit 2b53f9f

File tree

3 files changed

+83
-6
lines changed

3 files changed

+83
-6
lines changed

classes/ActionScheduler_AdminView.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,19 @@ public function add_help_tabs() {
250250
return;
251251
}
252252

253-
$as_version = ActionScheduler_Versions::instance()->latest_version();
254-
$as_source = ActionScheduler_Versions::instance()->active_source();
253+
$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();
256+
$as_source_markup = sprintf( '<code>%s</code>', esc_html( $as_source_path ) );
257+
258+
if ( ! empty( $as_source ) ) {
259+
$as_source_markup = sprintf(
260+
'%s: <abbr title="%s">%s</abbr>',
261+
ucfirst( $as_source['type'] ),
262+
esc_attr( $as_source_path ),
263+
esc_html( $as_source['name'] )
264+
);
265+
}
255266

256267
$screen->add_help_tab(
257268
array(
@@ -267,7 +278,7 @@ public function add_help_tabs() {
267278
'<p>' .
268279
esc_html__( 'Action Scheduler is currently being loaded from the following location. This can be useful when debugging, or if requested by the support team.', 'action-scheduler' ) .
269280
'</p>' .
270-
'<p><code>' . esc_html( $as_source ) . '</code></p>' .
281+
'<p>' . $as_source_markup . '</p>' .
271282
'<h3>' . esc_html__( 'WP CLI', 'action-scheduler' ) . '</h3>' .
272283
'<p>' .
273284
sprintf(

classes/ActionScheduler_Versions.php

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,77 @@ public static function initialize_latest_version() {
110110
}
111111

112112
/**
113-
* Get directory of active source.
113+
* Returns information about the plugin or theme which contains the current active version
114+
* of Action Scheduler.
115+
*
116+
* If this cannot be determined, or if Action Scheduler is being loaded via some other
117+
* method, then it will return an empty array. Otherwise, if populated, the array will
118+
* look like the following:
119+
*
120+
* [
121+
* 'type' => 'plugin', # or 'theme'
122+
* 'name' => 'Name',
123+
* ]
124+
*
125+
* @return array
126+
*/
127+
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();
176+
}
177+
178+
/**
179+
* Returns the directory path for the currently active installation of Action Scheduler.
114180
*
115181
* @return string
116182
*/
117-
public function active_source() {
183+
public function active_source_path(): string {
118184
return trailingslashit( dirname( __DIR__ ) );
119185
}
120186
}

classes/WP_CLI/System_Command.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public function source( array $args, array $assoc_args ) {
148148
$all = (bool) get_flag_value( $assoc_args, 'all' );
149149
$fullpath = (bool) get_flag_value( $assoc_args, 'fullpath' );
150150
$versions = \ActionScheduler_Versions::instance();
151-
$source = $versions->active_source();
151+
$source = $versions->active_source_path();
152152
$path = $source;
153153

154154
if ( ! $fullpath ) {

0 commit comments

Comments
 (0)