v1.2.0
This commit is contained in:
parent
e80820f416
commit
0e617fa428
12 changed files with 286 additions and 259 deletions
|
|
@ -280,4 +280,77 @@ class Robotstxt_Manager_Core_Client {
|
|||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the account's subscriptions (Core's /me/subscriptions),
|
||||
* cached in a transient for one hour. Keys: plugin_slug => row.
|
||||
*
|
||||
* @return array<string, array<string, mixed>> Rows keyed by plugin slug.
|
||||
*/
|
||||
public function get_subscriptions(): array {
|
||||
if ( ! $this->is_configured() ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$cache_key = 'robotstxt_manager_subscriptions';
|
||||
$cached = get_transient( $cache_key );
|
||||
|
||||
if ( is_array( $cached ) ) {
|
||||
$typed = array();
|
||||
foreach ( $cached as $slug => $row ) {
|
||||
if ( is_string( $slug ) && is_array( $row ) ) {
|
||||
$typed[ $slug ] = $row;
|
||||
}
|
||||
}
|
||||
|
||||
return $typed;
|
||||
}
|
||||
|
||||
$response = $this->get( '/me/subscriptions' );
|
||||
|
||||
if ( is_wp_error( $response ) || 200 !== (int) wp_remote_retrieve_response_code( $response ) ) {
|
||||
return array(); // Not cached — retried on the next view.
|
||||
}
|
||||
|
||||
$data = json_decode( wp_remote_retrieve_body( $response ), true );
|
||||
|
||||
if ( ! is_array( $data ) ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$rows = array();
|
||||
foreach ( $data as $row ) {
|
||||
if ( ! is_array( $row ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$slug = $row['plugin_slug'] ?? '';
|
||||
$slug = is_string( $slug ) ? $slug : '';
|
||||
|
||||
if ( '' === $slug ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$raw_status = $row['status'] ?? '';
|
||||
$raw_expires_at = $row['expires_at'] ?? '';
|
||||
|
||||
$rows[ $slug ] = array(
|
||||
'status' => is_string( $raw_status ) ? $raw_status : '',
|
||||
'expires_at' => is_string( $raw_expires_at ) ? $raw_expires_at : '',
|
||||
);
|
||||
}
|
||||
|
||||
set_transient( $cache_key, $rows, HOUR_IN_SECONDS );
|
||||
|
||||
return $rows;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears the cached subscriptions response (key change, manual refresh).
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function clear_subscriptions_cache(): void {
|
||||
delete_transient( 'robotstxt_manager_subscriptions' );
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue