| 1 |
<?php |
| 2 |
|
| 3 |
namespace FlyWP\Api; |
| 4 |
|
| 5 |
use WP_Error; |
| 6 |
|
| 7 |
class UpdatesData { |
| 8 |
private const CRON_HOOK = 'flywp_send_updates_data'; |
| 9 |
private const CRON_INTERVAL = 'twicedaily'; |
| 10 |
|
| 11 |
/** |
| 12 |
* UpdatesData constructor. |
| 13 |
*/ |
| 14 |
public function __construct() { |
| 15 |
$this->initialize_routes(); |
| 16 |
$this->initialize_cron_job(); |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Initialize API routes. |
| 21 |
*/ |
| 22 |
private function initialize_routes(): void { |
| 23 |
flywp()->router->get( 'updates-data', [ $this, 'respond' ] ); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Initialize cron job for sending updates data. |
| 28 |
*/ |
| 29 |
private function initialize_cron_job(): void { |
| 30 |
add_action( self::CRON_HOOK, [ $this, 'send_updates_data_to_api' ] ); |
| 31 |
|
| 32 |
if ( ! wp_next_scheduled( self::CRON_HOOK ) ) { |
| 33 |
wp_schedule_event( time(), self::CRON_INTERVAL, self::CRON_HOOK ); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Send updates data to the remote API. |
| 39 |
*/ |
| 40 |
public function send_updates_data_to_api(): void { |
| 41 |
$updates_data = $this->get_updates_data(); |
| 42 |
flywp()->flyapi->post( '/updates-data', $updates_data ); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Handle the API request. |
| 47 |
*/ |
| 48 |
public function respond(): void { |
| 49 |
wp_send_json( $this->get_updates_data() ); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Get updates data. |
| 54 |
* |
| 55 |
* @return array |
| 56 |
*/ |
| 57 |
private function get_updates_data(): array { |
| 58 |
return [ |
| 59 |
'wp_version' => get_bloginfo( 'version' ), |
| 60 |
'updates' => $this->get_formatted_updates_data(), |
| 61 |
]; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Get formatted updates data. |
| 66 |
* |
| 67 |
* @return array |
| 68 |
*/ |
| 69 |
private function get_formatted_updates_data(): array { |
| 70 |
return [ |
| 71 |
'core' => $this->get_formatted_core_updates(), |
| 72 |
'plugins' => $this->get_formatted_plugin_updates(), |
| 73 |
'themes' => $this->get_formatted_theme_updates(), |
| 74 |
]; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Get formatted core updates data. |
| 79 |
* |
| 80 |
* @return array |
| 81 |
*/ |
| 82 |
private function get_formatted_core_updates(): array { |
| 83 |
$core_data = $this->get_core_updates(); |
| 84 |
|
| 85 |
if ( ! $core_data['update_available'] ) { |
| 86 |
return []; |
| 87 |
} |
| 88 |
|
| 89 |
return [ |
| 90 |
'installed_version' => $core_data['version'], |
| 91 |
'latest_version' => $core_data['new_version'], |
| 92 |
]; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Get formatted plugin updates data. |
| 97 |
* |
| 98 |
* @return array |
| 99 |
*/ |
| 100 |
private function get_formatted_plugin_updates(): array { |
| 101 |
$this->load_required_files(); |
| 102 |
|
| 103 |
$all_plugins = get_plugins(); |
| 104 |
$plugin_updates = get_plugin_updates(); |
| 105 |
|
| 106 |
$formatted_plugins = []; |
| 107 |
|
| 108 |
foreach ( $plugin_updates as $plugin_file => $plugin_data ) { |
| 109 |
$plugin_info = $all_plugins[ $plugin_file ]; |
| 110 |
$slug = dirname( $plugin_file ); |
| 111 |
|
| 112 |
$formatted_plugins[] = $this->format_plugin_data( $plugin_info, $plugin_data, $plugin_file, $slug ); |
| 113 |
} |
| 114 |
|
| 115 |
return $formatted_plugins; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Get formatted theme updates data. |
| 120 |
* |
| 121 |
* @return array |
| 122 |
*/ |
| 123 |
private function get_formatted_theme_updates(): array { |
| 124 |
$theme_updates = get_theme_updates(); |
| 125 |
|
| 126 |
$formatted_themes = []; |
| 127 |
|
| 128 |
foreach ( $theme_updates as $theme_slug => $theme_data ) { |
| 129 |
$theme = wp_get_theme( $theme_slug ); |
| 130 |
$formatted_themes[] = $this->format_theme_data( $theme, $theme_data, $theme_slug ); |
| 131 |
} |
| 132 |
|
| 133 |
return $formatted_themes; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Check if WordPress core has an update available. |
| 138 |
* |
| 139 |
* @return array |
| 140 |
*/ |
| 141 |
private function get_core_updates(): array { |
| 142 |
$current = get_bloginfo( 'version' ); |
| 143 |
|
| 144 |
$this->load_required_files(); |
| 145 |
|
| 146 |
$update = get_preferred_from_update_core(); |
| 147 |
|
| 148 |
$response = [ |
| 149 |
'version' => $current, |
| 150 |
'update_available' => false, |
| 151 |
'new_version' => null, |
| 152 |
]; |
| 153 |
|
| 154 |
if ( ! isset( $update->response ) || $update->response !== 'upgrade' ) { |
| 155 |
return $response; |
| 156 |
} |
| 157 |
|
| 158 |
$response['update_available'] = true; |
| 159 |
$response['new_version'] = $update->current; |
| 160 |
|
| 161 |
return $response; |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Format plugin data. |
| 166 |
* |
| 167 |
* @param array $plugin_info |
| 168 |
* @param object $plugin_data |
| 169 |
* @param string $plugin_file |
| 170 |
* @param string $slug |
| 171 |
* |
| 172 |
* @return array |
| 173 |
*/ |
| 174 |
private function format_plugin_data( array $plugin_info, object $plugin_data, string $plugin_file, string $slug ): array { |
| 175 |
$formatted_plugin = [ |
| 176 |
'slug' => $slug, |
| 177 |
'name' => $plugin_info['Name'], |
| 178 |
'installed_version' => $plugin_info['Version'], |
| 179 |
'latest_version' => $plugin_data->update->new_version, |
| 180 |
'is_active' => is_plugin_active( $plugin_file ), |
| 181 |
]; |
| 182 |
|
| 183 |
$extra = [ |
| 184 |
'url' => $plugin_info['PluginURI'] ?? '', |
| 185 |
'author' => $plugin_info['Author'] ?? '', |
| 186 |
'file' => $plugin_file, |
| 187 |
'textdomain' => $plugin_info['TextDomain'] ?? '', |
| 188 |
'description' => $plugin_info['Description'] ?? '', |
| 189 |
'php' => $plugin_info['RequiresPHP'] ?? '', |
| 190 |
]; |
| 191 |
|
| 192 |
if ( ! empty( array_filter( $extra ) ) ) { |
| 193 |
$formatted_plugin['extra'] = array_filter( $extra ); |
| 194 |
} |
| 195 |
|
| 196 |
return $formatted_plugin; |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Format theme data. |
| 201 |
* |
| 202 |
* @param \WP_Theme $theme |
| 203 |
* @param object $theme_data |
| 204 |
* @param string $theme_slug |
| 205 |
* |
| 206 |
* @return array |
| 207 |
*/ |
| 208 |
private function format_theme_data( \WP_Theme $theme, object $theme_data, string $theme_slug ): array { |
| 209 |
$formatted_theme = [ |
| 210 |
'slug' => $theme_slug, |
| 211 |
'name' => $theme->get( 'Name' ), |
| 212 |
'installed_version' => $theme->get( 'Version' ), |
| 213 |
'latest_version' => $theme_data->update['new_version'], |
| 214 |
'is_active' => ( get_stylesheet() === $theme_slug ), |
| 215 |
]; |
| 216 |
|
| 217 |
$extra = [ |
| 218 |
'url' => $theme->get( 'ThemeURI' ), |
| 219 |
]; |
| 220 |
|
| 221 |
if ( ! empty( array_filter( $extra ) ) ) { |
| 222 |
$formatted_theme['extra'] = array_filter( $extra ); |
| 223 |
} |
| 224 |
|
| 225 |
return $formatted_theme; |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* Load required WordPress files. |
| 230 |
*/ |
| 231 |
private function load_required_files(): void { |
| 232 |
if ( ! function_exists( 'get_plugins' ) ) { |
| 233 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 234 |
} |
| 235 |
if ( ! function_exists( 'get_plugin_updates' ) ) { |
| 236 |
require_once ABSPATH . 'wp-admin/includes/update.php'; |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Deactivate the scheduler when the plugin is deactivated. |
| 242 |
*/ |
| 243 |
public function deactivate(): void { |
| 244 |
$timestamp = wp_next_scheduled( self::CRON_HOOK ); |
| 245 |
if ( $timestamp ) { |
| 246 |
wp_unschedule_event( $timestamp, self::CRON_HOOK ); |
| 247 |
} |
| 248 |
} |
| 249 |
} |
| 250 |
|