| 1 |
<?php |
| 2 |
|
| 3 |
namespace FlyWP\Api; |
| 4 |
|
| 5 |
class Themes { |
| 6 |
|
| 7 |
/** |
| 8 |
* API constructor. |
| 9 |
*/ |
| 10 |
public function __construct() { |
| 11 |
flywp()->router->get( 'themes', [ $this, 'respond' ] ); |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* Handle request. |
| 16 |
* |
| 17 |
* @return void |
| 18 |
*/ |
| 19 |
public function respond( $args ) { |
| 20 |
$response = []; |
| 21 |
|
| 22 |
$themes = wp_get_themes(); |
| 23 |
$updates = get_site_transient( 'update_themes' ); |
| 24 |
|
| 25 |
foreach ( $themes as $key => $theme ) { |
| 26 |
$update = $this->get_update( $key, $updates ); |
| 27 |
|
| 28 |
$response[] = [ |
| 29 |
'name' => $key, |
| 30 |
'version' => $theme->get( 'Version' ), |
| 31 |
'description' => $theme->get( 'Description' ), |
| 32 |
'theme_uri' => $theme->get( 'ThemeURI' ), |
| 33 |
'author' => $theme->get( 'Author' ), |
| 34 |
'author_uri' => $theme->get( 'AuthorURI' ), |
| 35 |
'status' => $this->get_status( $theme ), |
| 36 |
'update_available' => $update ? true : false, |
| 37 |
'new_version' => $update ? $update['new_version'] : null, |
| 38 |
]; |
| 39 |
} |
| 40 |
|
| 41 |
wp_send_json( $response ); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Get theme active status. |
| 46 |
* |
| 47 |
* @param string $file |
| 48 |
* |
| 49 |
* @return string |
| 50 |
*/ |
| 51 |
private function get_status( $theme ) { |
| 52 |
if ( $theme->get_stylesheet_directory() === get_stylesheet_directory() ) { |
| 53 |
return 'active'; |
| 54 |
} |
| 55 |
|
| 56 |
if ( $theme->get_stylesheet_directory() === get_stylesheet_directory() ) { |
| 57 |
return 'parent'; |
| 58 |
} |
| 59 |
|
| 60 |
return 'inactive'; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Check if a theme has an update available. |
| 65 |
* |
| 66 |
* @param string $key |
| 67 |
* @param object $updates |
| 68 |
* |
| 69 |
* @return array|bool |
| 70 |
*/ |
| 71 |
private function get_update( $key, $updates ) { |
| 72 |
if ( isset( $updates->response[ $key ] ) ) { |
| 73 |
return [ |
| 74 |
'new_version' => $updates->response[ $key ]['new_version'], |
| 75 |
'package' => $updates->response[ $key ]['package'], |
| 76 |
]; |
| 77 |
} |
| 78 |
|
| 79 |
return false; |
| 80 |
} |
| 81 |
} |
| 82 |
|