Skip to content

Commit

Permalink
added direct github update
Browse files Browse the repository at this point in the history
  • Loading branch information
jibon57 committed Feb 28, 2022
1 parent 5d977d5 commit 76ac02b
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
9 changes: 9 additions & 0 deletions plugnmeet/admin/class-plugnmeet-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ public function enqueue_scripts($hook_suffix)
wp_localize_script($this->plugin_name, 'ajax_admin', $script);
}

public function register_plugin_update()
{
if (!class_exists("Plugnmeet_Update")) {
require plugin_dir_path(dirname(__FILE__)) . 'admin/class-plugnmeet-update.php';
}

new Plugnmeet_Update($this->version, $this->plugin_name);
}

public function addMenuPages($hook_suffix)
{
if (!class_exists("Plugnmeet_MenusPages")) {
Expand Down
106 changes: 106 additions & 0 deletions plugnmeet/admin/class-plugnmeet-update.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php
/**
* @package Plugnmeet
* @subpackage Plugnmeet/admin
* @author Jibon Costa <jibon@mynaparrot.com>
*/

if (!defined('PLUGNMEET_BASE_NAME')) {
die;
}

class Plugnmeet_Update
{
protected $current_version;
protected $plugin_slug;
protected $plugin_dir;


public function __construct($current_version, $plugin_slug)
{
$this->current_version = $current_version;
$this->plugin_dir = $plugin_slug;
$this->plugin_slug = $plugin_slug . ".php";

add_filter('pre_set_site_transient_update_plugins', array($this, 'check_update'));
add_filter('plugins_api', array($this, 'check_info'), 10, 3);
}

public function check_update($transient)
{
if (empty($transient->checked)) {
return $transient;
}

$res = $this->fetchInfoFromGithub();
if (!$res->status) {
return $transient;
}

// If a newer version is available, add the update
if (version_compare($this->current_version, $res->version, '<')) {
$obj = new stdClass();
$obj->slug = $this->plugin_slug;
$obj->new_version = $res->version;
$obj->url = $res->url;
$obj->package = $res->package;

$transient->response["$this->plugin_dir/$this->plugin_slug"] = $obj;
}

return $transient;
}

public function check_info($obj, $action, $arg)
{
if (($action == 'query_plugins' || $action == 'plugin_information') &&
isset($arg->slug) && $arg->slug === $this->plugin_slug) {
$res = $this->fetchInfoFromGithub();
if (!$res->status) {
return $obj;
}

$obj = new stdClass();
$obj->slug = $this->plugin_slug;
$obj->name = "Plug-N-Meet WordPress";
$obj->new_version = $res->version;
$obj->url = $res->url;
$obj->package = $res->package;
$obj->download_link = $res->package;
$obj->sections = array(
'changelog' => $res->body
);
}

return $obj;
}

private function fetchInfoFromGithub()
{
$header = array(
"Accept: application/vnd.github.v3+json"
);
$output = new stdClass();
$output->status = false;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.github.com/repos/mynaparrot/plugNmeet-WordPress/releases");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:84.0) Gecko/20100101 Firefox/84.0");
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$response = curl_exec($ch);

if ($response) {
$new = json_decode($response)[0];

$output->status = true;
$output->version = str_replace("v", "", $new->tag_name);
$output->package = $new->assets[0]->browser_download_url;
$output->url = $new->html_url;
$output->body = $new->body;
}

return $output;
}
}
1 change: 1 addition & 0 deletions plugnmeet/includes/class-plugnmeet.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ private function define_admin_hooks()

$this->loader->add_action('admin_enqueue_scripts', $plugin_admin, 'enqueue_styles');
$this->loader->add_action('admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts');
$this->loader->add_action('init', $plugin_admin, 'register_plugin_update');
$this->loader->add_action('admin_init', $plugin_admin, 'register_settings');
$this->loader->add_action('admin_menu', $plugin_admin, 'addMenuPages');

Expand Down

0 comments on commit 76ac02b

Please sign in to comment.