Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Split the fields to check for wporg and date seperatatly.
  • Loading branch information
janw-me committed Nov 10, 2023
commit 342aaa19f80849f68d1fa81689e8968dd9e267e2
55 changes: 39 additions & 16 deletions src/Plugin_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ class Plugin_Command extends \WP_CLI\CommandWithUpgrade {
protected $item_type = 'plugin';
protected $upgrade_refresh = 'wp_update_plugins';
protected $upgrade_transient = 'update_plugins';
protected $check_wporg = [ 'status' => false, 'update_date' => false ];

protected $obj_fields = array(
'name',
'status',
'update',
'version',
'wordpress.org',
);

/**
Expand Down Expand Up @@ -702,6 +702,7 @@ protected function get_item_list() {
$all_update_info = $this->get_update_info();
$update_info = ( isset( $all_update_info->response[ $file ] ) && null !== $all_update_info->response[ $file ] ) ? (array) $all_update_info->response[ $file ] : null;
$name = Utils\get_plugin_name( $file );
$wporg_info = $this->get_wporg_data( $name );

if ( ! isset( $duplicate_names[ $name ] ) ) {
$duplicate_names[ $name ] = array();
Expand All @@ -721,11 +722,11 @@ protected function get_item_list() {
'file' => $file,
'auto_update' => in_array( $file, $auto_updates, true ),
'author' => $details['Author'],
'wordpress.org' => $this->get_dotorg_status( $name ),
'wp_org' => $wporg_info['status'],
'wp_org_updated' => $wporg_info['last_updated'],
];

if ( null === $update_info ) {

// Get info for all plugins that don't have an update.
$plugin_update_info = isset( $all_update_info->no_update[ $file ] ) ? $all_update_info->no_update[ $file ] : null;

Expand Down Expand Up @@ -755,36 +756,49 @@ protected function get_item_list() {
*
* @return string The status of the plugin, includes the last update date.
*/
protected function get_dotorg_status( $plugin_name ) {
protected function get_wporg_data( $plugin_name ) {
$data = [
'status' => 'no_wp_org',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'status' => 'no_wp_org',
'status' => '',

We can leave this empty when it's not present.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was not happy about the label itself.
But personally I prefer when a check like this tells "something". Empty can feel like an unknown failure.

Are there other places where we leave fields empty?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there other places where we leave fields empty?

Yes:

CleanShot 2023-12-06 at 05 18 24@2x

'last_updated' => '-',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'last_updated' => '-',
'last_updated' => '',

We can leave this empty when it's not present.

];
if ( ! $this->check_wporg['status'] && ! $this->check_wporg['update_date'] ) {
return $data;
}

if ( $this->check_wporg ) {
$request = wp_remote_head( 'https://api.wordpress.org/plugins/info/1.0/' . $plugin_name );
$response_code = wp_remote_retrieve_response_code( $request );
if ( 200 === $response_code ) {
$data['status'] = 'active';
}
// Just because the plugin is not in the api, does not mean it was never on .org.
}

$request = wp_remote_get( "https://plugins.trac.wordpress.org/log/{$plugin_name}/?limit=1&mode=stop_on_copy&format=rss" );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we only make this request if we're checking for the date?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made a better check in d7ec274
The answer is, not always,

When the call to api.wp is not succesfull, we need to check the SVN to distinguish between closed and never on .org.

$response_code = wp_remote_retrieve_response_code( $request );
if ( 404 === $response_code ) {
return 'not on wordpress.org'; // This plugin was never on .org.
return $data; // This plugin was never on .org, there is no date to check.
}
if ( 'active' !== $data['status'] ) {
$data['status'] = 'closed'; // This plugin was on .org at some point, but not anymore.
}
if ( ! class_exists( 'SimpleXMLElement' ) ) {
WP_CLI::error( "The PHP extension 'SimpleXMLElement' is not available but is required for XML-formatted output." );
}

$update_date = 'Unknown last update date';

// log the date.
// Check the last update date.
$r_body = wp_remote_retrieve_body( $request );
if ( str_contains( $r_body, 'pubDate' ) ) {
// Very basic check, not validating the format or anything else.
// Very raw check, not validating the format or anything else.
$xml = simplexml_load_string( $r_body );
$xml_pub_date = $xml->xpath( '//pubDate' );
if ( $xml_pub_date ) {
$update_date = 'last updated ' . wp_date( get_option( 'date_format' ), (string) strtotime( $xml_pub_date[0] ) );
$data['last_updated'] = wp_date( 'Y-m-d', (string) strtotime( $xml_pub_date[0] ) );
}
}

$request = wp_remote_head( 'https://api.wordpress.org/plugins/info/1.0/' . $plugin_name );
$response_code = wp_remote_retrieve_response_code( $request );
if ( 200 === $response_code ) {
return 'active, ' . $update_date; // this plugin is on .org.
}

return 'closed, ' . $update_date;
return $data;
}

protected function filter_item_list( $items, $args ) {
Expand Down Expand Up @@ -1227,6 +1241,8 @@ public function delete( $args, $assoc_args = array() ) {
* * file
* * auto_update
* * author
* * wp_org
* * wp_org_updated
*
* ## EXAMPLES
*
Expand All @@ -1252,6 +1268,13 @@ public function delete( $args, $assoc_args = array() ) {
* @subcommand list
*/
public function list_( $_, $assoc_args ) {
$fields = Utils\get_flag_value( $assoc_args, 'fields' );
if ( ! empty( $fields ) ) {
$fields = explode( ',', $fields );
$this->check_wporg['status'] = in_array( 'wp_org', $fields, true );
$this->check_wporg['update_date'] = in_array( 'wp_org_updated', $fields, true );
}

parent::_list( $_, $assoc_args );
}

Expand Down