Skip to content

Commit

Permalink
selfupdate
Browse files Browse the repository at this point in the history
  • Loading branch information
ultronstudio committed Mar 4, 2024
1 parent 93284e3 commit d83a6c5
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions almahome.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,66 @@ function register_almahome_styles() {
wp_enqueue_style('almahome-style', plugin_dir_url(__FILE__) . 'css/custom-styles.css');
}

/**
* Check for updates to this plugin
*
* @param array $update Array of update data.
* @param array $plugin_data Array of plugin data.
* @param string $plugin_file Path to plugin file.
* @param string $locales Locale code.
*
* @return array|bool Array of update data or false if no update available.
*/
function self_update( $update, array $plugin_data, string $plugin_file, $locales ) {
// only check this plugin
if ( 'AlmaHome/almahome.php' !== $plugin_file ) {
return $update;
}

// already completed update check elsewhere
if ( ! empty( $update ) ) {
return $update;
}

// let's go get the latest version number from GitHub
$response = wp_remote_get(
'https://api.github.com/repos/ultronstudio/AlmaHome/releases/latest',
array(
'user-agent' => 'ultronstudio',
)
);

if ( is_wp_error( $response ) ) {
return;
} else {
$output = json_decode( wp_remote_retrieve_body( $response ), true );
}

$new_version_number = $output['tag_name'];
$is_update_available = version_compare( $plugin_data['Version'], $new_version_number, '<' );

if ( ! $is_update_available ) {
return false;
}

$new_url = $output['html_url'];
$new_package = $output['assets'][0]['browser_download_url'];

error_log('$plugin_data: ' . print_r( $plugin_data, true ));
error_log('$new_version_number: ' . $new_version_number );
error_log('$new_url: ' . $new_url );
error_log('$new_package: ' . $new_package );

return array(
'slug' => $plugin_data['TextDomain'],
'version' => $new_version_number,
'url' => $new_url,
'package' => $new_package,
);
}

add_filter( 'update_plugins_github.com', 'self_update', 10, 4 );

add_filter( 'auto_update_plugin', '__return_true' );
add_filter( 'plugin_row_meta', 'plugin_meta_links', 10, 4 );

Expand Down

0 comments on commit d83a6c5

Please sign in to comment.