Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: updates data api and scheduler add #7

Merged
merged 8 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
38 changes: 30 additions & 8 deletions flywp.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ final class FlyWP_Plugin {
*/
public $version = '1.3.1';

/**
* API Endpoint.
*
* @var string
*/
public $api_endpoint = 'https://app.flywp.com/api/site-api';

/**
* Plugin Constructor.
*
Expand All @@ -53,6 +60,7 @@ private function __construct() {

$this->add_action( 'plugins_loaded', 'init_plugin' );
register_activation_hook( __FILE__, [ $this, 'activate' ] );
register_deactivation_hook( __FILE__, [ $this, 'deactivate' ] );
}

/**
Expand All @@ -67,6 +75,10 @@ private function define_constants() {
define( 'FLYWP_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'FLYWP_PLUGIN_URL', plugin_dir_url( __FILE__ ) );

if ( ! defined( 'FLYWP_API_ENDPOINT' ) ) {
define( 'FLYWP_API_ENDPOINT', $this->api_endpoint );
}

if ( ! defined( 'FLYWP_API_KEY' ) ) {
define( 'FLYWP_API_KEY', '' );
}
Expand All @@ -84,6 +96,15 @@ public function activate() {
flush_rewrite_rules( false );
}

/**
* Plugin activation hook.
*
* @return void
*/
public function deactivate() {
( new FlyWP\Api\UpdatesData() )->deactivate();
}

/**
* Initialize plugin.
*
Expand All @@ -102,14 +123,15 @@ public function init_plugin() {
$this->frontend = new FlyWP\Frontend();
}

$this->router = new FlyWP\Router();
$this->rest = new FlyWP\Api();
$this->fastcgi = new FlyWP\Fastcgi_Cache();
$this->opcache = new FlyWP\Opcache();
$this->flyapi = new FlyWP\FlyApi();
$this->email = new FlyWP\Email();
$this->optimize = new FlyWP\Optimizations();
$this->litespeed = new FlyWP\Litespeed();
$this->router = new FlyWP\Router();
$this->rest = new FlyWP\Api();
$this->fastcgi = new FlyWP\Fastcgi_Cache();
$this->opcache = new FlyWP\Opcache();
$this->flyapi = new FlyWP\FlyApi();
$this->email = new FlyWP\Email();
$this->optimize = new FlyWP\Optimizations();
$this->litespeed = new FlyWP\Litespeed();
$this->updates_data = new FlyWP\Api\UpdatesData();
}

/**
Expand Down
3 changes: 2 additions & 1 deletion includes/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function __construct() {
new Api\Updates();
new Api\Cache();
new Api\Health();
new Api\UpdatesData();
}

/**
Expand All @@ -52,7 +53,7 @@ public function get_bearer_token() {
return false;
}

$auth_header = wp_unslash( $_SERVER['HTTP_AUTHORIZATION'] );
$auth_header = sanitize_text_field( wp_unslash( $_SERVER['HTTP_AUTHORIZATION'] ) );

if ( ! preg_match( '/Bearer\s(\S+)/', $auth_header, $matches ) ) {
return false;
Expand Down
203 changes: 203 additions & 0 deletions includes/Api/UpdatesData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
<?php

namespace FlyWP\Api;

class UpdatesData {
/**
* UpdatesData constructor.
*/
public function __construct() {
flywp()->router->get( 'updates-data', [ $this, 'respond' ] );

// Register the cron event
add_action( 'flywp_send_updates_data', [ $this, 'send_updates_data_to_api' ] );

// Schedule the cron event if not already scheduled
if ( ! wp_next_scheduled( 'flywp_send_updates_data' ) ) {
wp_schedule_event( time(), 'twicedaily', 'flywp_send_updates_data' );
}
}

/**
* Send updates data to the remote API.
*
* @return void
*/
public function send_updates_data_to_api() {
$updates_data = [
'wp_version' => get_bloginfo( 'version' ),
'updates' => $this->get_formatted_updates_data(),
];

flywp()->flyapi->post( '/updates-data', $updates_data );
}

/**
* Handle the request.
*
* @return void
*/
public function respond() {
$response = [
'wp_version' => get_bloginfo( 'version' ),
'updates' => $this->get_formatted_updates_data(),
];

wp_send_json( $response );
}

/**
* Get formatted updates data.
*
* @return array
*/
private function get_formatted_updates_data() {
return [
'core' => $this->get_formatted_core_updates(),
'plugins' => $this->get_formatted_plugin_updates(),
'themes' => $this->get_formatted_theme_updates(),
];
}

/**
* Get formatted core updates data.
*
* @return array
*/
private function get_formatted_core_updates() {
$core_data = $this->core_updates();

if ( ! $core_data['update_available'] ) {
return [];
}

return [
'installed_version' => $core_data['version'],
'latest_version' => $core_data['new_version'],
];
}

/**
* Get formatted plugin updates data.
*
* @return array
*/
private function get_formatted_plugin_updates() {
if ( ! function_exists( 'get_plugins' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
if ( ! function_exists( 'get_plugin_updates' ) ) {
require_once ABSPATH . 'wp-admin/includes/update.php';
}

$all_plugins = get_plugins();
$plugin_updates = get_plugin_updates();

$formatted_plugins = [];

foreach ( $plugin_updates as $plugin_file => $plugin_data ) {
$plugin_info = $all_plugins[ $plugin_file ];
$slug = dirname( $plugin_file );

$plugin_info = [
'slug' => $slug,
'name' => $plugin_info['Name'],
'installed_version' => $plugin_info['Version'],
'latest_version' => $plugin_data->update->new_version,
'is_active' => is_plugin_active( $plugin_file ),
];

$extra = [
'url' => $plugin_info['PluginURI'] ?? '',
'author' => $plugin_info['Author'] ?? '',
'file' => $plugin_file,
'textdomain' => $plugin_info['TextDomain'] ?? '',
'description' => $plugin_info['Description'] ?? '',
'php' => $plugin_info['RequiresPHP'] ?? '',
];

if ( ! empty( array_filter( $extra ) ) ) {
$plugin_info['extra'] = array_filter( $extra );
}

$formatted_plugins[] = $plugin_info;
}

return $formatted_plugins;
}

/**
* Get formatted theme updates data.
*
* @return array
*/
private function get_formatted_theme_updates() {
$theme_updates = get_theme_updates();

$formatted_themes = [];

foreach ( $theme_updates as $theme_slug => $theme_data ) {
$theme = wp_get_theme( $theme_slug );

$theme_info = [
'slug' => $theme_slug,
'name' => $theme->get( 'Name' ),
'installed_version' => $theme->get( 'Version' ),
'latest_version' => $theme_data->update['new_version'],
'is_active' => ( get_stylesheet() === $theme_slug ),
];

$extra = [
'url' => $theme->get( 'ThemeURI' ),
];

if ( ! empty( array_filter( $extra ) ) ) {
$theme_info['extra'] = array_filter( $extra );
}

$formatted_themes[] = $theme_info;
}

return $formatted_themes;
}

/**
* Check if WordPress core has an update available.
*
* @return array
*/
private function core_updates() {
$current = get_bloginfo( 'version' );

if ( ! function_exists( 'get_preferred_from_update_core' ) ) {
require_once ABSPATH . 'wp-admin/includes/update.php';
}

$update = get_preferred_from_update_core();

$response = [
'version' => $current,
'update_available' => false,
'new_version' => null,
];

if ( ! isset( $update->response ) || 'upgrade' !== $update->response ) {
return $response;
}

$response['update_available'] = true;
$response['new_version'] = $update->current;

return $response;
}

/**
* Deactivate the scheduler when the plugin is deactivated.
*/
public function deactivate() {
$timestamp = wp_next_scheduled( 'flywp_send_updates_data' );
if ( $timestamp ) {
wp_unschedule_event( $timestamp, 'flywp_send_updates_data' );
}
}
}
8 changes: 5 additions & 3 deletions includes/FlyApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function cache_toggle( $action = 'enable', $type = 'fastcgi' ) {
* @return string
*/
protected function get_endpoint() {
return apply_filters( 'flywp_api_endpoint', 'https://app.flywp.com/api/site-api' );
return apply_filters( 'flywp_api_endpoint', FLYWP_API_ENDPOINT );
}

/**
Expand Down Expand Up @@ -73,7 +73,7 @@ public function get( $path ) {
* Send a POST request to the API.
*
* @param string $path
* @param array $data
* @param array $data
*
* @return array|false
*/
Expand All @@ -86,11 +86,13 @@ public function post( $path, $data = [] ) {
'headers' => [
'Authorization' => 'Bearer ' . flywp()->get_key(),
],
'body' => $data,
'body' => $data,
]
);

if ( is_wp_error( $response ) ) {
error_log( print_r( $response, true ) );

return false;
}

Expand Down
Loading