Skip to content

Commit

Permalink
Update from Github implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Lannoy committed Nov 19, 2024
1 parent 09470cd commit 742f39f
Show file tree
Hide file tree
Showing 6 changed files with 217 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
/.gitattributes export-ignore
/.gitignore export-ignore
/.github export-ignore
/.translations export-ignore
/.translations export-ignore
/plugin.json
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ __Traffic__ is a full featured analytics reporting tool that analyzes all inboun

See [WordPress directory page](https://wordpress.org/plugins/traffic/) or [official website](https://perfops.one/traffic).

> 🎁 Give this plugin a drive test on a free dummy site: [One-Click Test!](https://tastewp.com/new/?pre-installed-plugin-slug=traffic)
At this time, __Traffic__ can report, for inbound and outbound traffic:

* KPIs: number of calls, data volume, server error rate, quotas error rate, effective pass rate and perceived uptime;
Expand Down
179 changes: 178 additions & 1 deletion includes/plugin/class-updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@
*/
class Updater {

private $name = POKM_PRODUCT_NAME;

private $slug = POKM_SLUG;

private $version = POKM_VERSION;

private $product = POKM_PRODUCT_URL;

/**
* Initializes the class, set its properties and performs
* post-update processes if needed.
Expand All @@ -53,6 +61,12 @@ public function __construct() {
}
Nag::add( 'update', 'info', $message );
}
if ( ! ( defined( 'POO_SELFUPDATE_BYPASS' ) && POO_SELFUPDATE_BYPASS ) ) {
add_filter( 'plugins_api', [ $this, 'plugin_info' ], PHP_INT_MAX, 3 );
add_filter( 'site_transient_update_plugins', [ $this, 'info_update' ] );
add_action( 'upgrader_process_complete', [ $this, 'info_reset' ], 10, 2 );
add_filter( 'clean_url', [ $this, 'filter_logo' ], PHP_INT_MAX, 3 );
}
}

/**
Expand Down Expand Up @@ -88,6 +102,169 @@ private function update( $from ) {
*/
public function sc_get_changelog( $attributes ) {
$md = new Markdown();
return $md->get_shortcode( 'CHANGELOG.md', $attributes );

return $md->get_shortcode( 'CHANGELOG.md', $attributes );
}

/**
* Acquires infos about update
*
* @return object The remote info.
*/
private function gather_info() {
$remotes = get_transient( 'update-' . $this->slug );
if ( ! $remotes ) {
$remotes = new \stdClass();

$remote = wp_remote_get(
str_replace( 'github.com', 'raw.githubusercontent.com', $this->product ) . '/refs/heads/master/plugin.json',
[
'timeout' => 10,
'headers' => [
'Accept' => 'application/vnd.github+json'
]
]
);
if ( is_wp_error( $remote ) || 200 !== wp_remote_retrieve_response_code( $remote ) || empty( wp_remote_retrieve_body( $remote ) ) ) {
return false;
}
$plugin_info = json_decode( wp_remote_retrieve_body( $remote ), true );
$remotes->tested = $plugin_info['tested'] ?? '7.0';
$remotes->requires = $plugin_info['requires'] ?? '6.2';
$remotes->requires_php = $plugin_info['requires_php'] ?? '7.1';
$remotes->author = $plugin_info['author'] ?? '<a href="https://perfops.one">Pierre Lannoy / PerfOps One</a>';
$remotes->author_profile = $plugin_info['author_profile'] ?? 'https://profiles.wordpress.org/pierrelannoy/';

$remote = wp_remote_get(
str_replace( 'github.com', 'api.github.com/repos', $this->product ) . '/releases/latest',
[
'timeout' => 10,
'headers' => [
'Accept' => 'application/vnd.github+json'
]
]
);
if ( is_wp_error( $remote ) || 200 !== wp_remote_retrieve_response_code( $remote ) || empty( wp_remote_retrieve_body( $remote ) ) ) {
return false;
}
$release_info = json_decode( wp_remote_retrieve_body( $remote ), true );
if ( array_key_exists( 'tag_name', $release_info ) && array_key_exists( 'name', $release_info ) && array_key_exists( 'published_at', $release_info ) && array_key_exists( 'body', $release_info ) && array_key_exists( 'assets', $release_info ) && is_array( $release_info['assets'] ) ) {
$remotes->version = $release_info['tag_name'];
$remotes->download_url = $release_info['assets'][0]['browser_download_url'] ?? '-';
$remotes->last_updated = substr( $release_info['published_at'], 0, strpos( $release_info['published_at'], 'T' ) );
$remotes->changelog = '## ' . $release_info['name'] . "\r\n" . $release_info['body'];
} else {
return false;
}
if ( '-' === $remotes->download_url ) {
return false;
}

set_transient( 'update-' . $this->slug, $remotes, DAY_IN_SECONDS );
}

return $remotes;
}

/**
* Filters the url logo to be sure it is svg-inlined.
*
* @param string $good_protocol_url The cleaned URL to be returned.
* @param string $original_url The URL prior to cleaning.
* @param string $_context If 'display', replace ampersands and single quotes only.
*
* @return string $good_protocol_url The cleaned URL.
*/
function filter_logo( $good_protocol_url, $original_url, $_context ) {
if ( 'https://data.' . $this->slug === $original_url ) {
return Core::get_base64_logo();
}

return $good_protocol_url;
}

/**
* Filters the response for the current WordPress.org Plugin Installation API request.
*
* @param false|object|array $result The result object or array.
* @param string $action The type of information being requested from the Plugin Installation API.
* @param object $args Plugin API arguments.
* @@return false|object|array The result object or array.
*/
function plugin_info( $res, $action, $args ) {
if ( 'plugin_information' !== $action ) {
return $res;
}
if ( $this->slug !== $args->slug ) {
return $res;
}
$infos = $this->gather_info();
if ( ! $infos ) {
return $res;
}
$md = new Markdown();
$res = new \stdClass();
$res->name = $this->name;
$res->homepage = 'https://perfops.one/' . $this->slug;
$res->slug = $this->slug;
$res->is_community = true;
$res->external_repository_url = $this->product;
$res->tested = $infos->tested;
$res->requires = $infos->requires;
$res->requires_php = $infos->requires_php;
$res->last_updated = $infos->last_updated;
$res->author = $infos->author;
$res->author_profile = $infos->author_profile;
$res->version = $infos->version;
$res->download_link = $infos->download_url;
$res->trunk = $infos->download_url;
$res->sections = [
'changelog' => $md->get_inline( $infos->changelog, [] ) . '<br/><br/><p><a target="_blank" href="' . $res->homepage . '-changelog">CHANGELOG »</a></p>',
];
$res->banners = [
"low" => str_replace( 'github.com', 'raw.githubusercontent.com', $this->product ) . '/refs/heads/master/.wordpress-org/banner-772x250.jpg',
"high" => str_replace( 'github.com', 'raw.githubusercontent.com', $this->product ) . '/refs/heads/master/.wordpress-org/banner-1544x500.jpg'
];
return $res;
}

/**
* Updates infos transient
*
* @param object $transient The transient to update.
*
* @return object The updated transient.
*/
public function info_update( $transient ) {
if ( empty( $transient->checked ) ) {
return $transient;
}
$remote = $this->gather_info();
if ( $remote && version_compare( $this->version, $remote->version, '<' ) && version_compare( $remote->requires, get_bloginfo( 'version' ), '<=' ) && version_compare( $remote->requires_php, PHP_VERSION, '<' ) ) {
$res = new \stdClass();
$res->slug = $this->slug;
$res->plugin = $this->slug . '/' . $this->slug . '.php';
$res->new_version = $remote->version;
$res->tested = $remote->tested;
$res->package = $remote->download_url;
$res->icons = [
'svg' => 'https://data.' . $this->slug
];
$transient->response[ $res->plugin ] = $res;
}

return $transient;
}

/**
* Reset update infos
*
* @param Plugin_Upgrader $upgrader Upgrader instance.
* @param array $options Array of bulk item update data.
*/
public function info_reset( $upgrader, $options ) {
if ( 'update' === $options['action'] && 'plugin' === $options['type'] ) {
delete_transient( 'update-' . $this->slug );
}
}
}
30 changes: 30 additions & 0 deletions includes/system/class-markdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,36 @@ public function get_shortcode( $file, $attributes ) {
return $result;
}

/**
* Get the changelog.
*
* @param string $content The filename.
* @param array $attributes 'style' => 'markdown', 'html'.
* 'mode' => 'raw', 'clean'.
* @return string The output of the inline, ready to print.
*/
public function get_inline( $content, $attributes ) {
$_attributes = shortcode_atts(
[
'style' => 'html',
'mode' => 'clean',
],
$attributes
);
$style = $_attributes['style'];
$mode = $_attributes['mode'];
if ( $content ) {
switch ( $style ) {
case 'html':
$result = '<div class="markdown">' . $this->html_from_markdown( $content, ( 'clean' === $mode ) ) . '</div>';
break;
default:
$result = esc_html( $content );
}
}
return $result;
}

/**
* Format a changelog in html.
*
Expand Down
7 changes: 7 additions & 0 deletions plugin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"author" : "<a href='https://perfops.one'>Pierre Lannoy / PerfOps One</a>",
"author_profile" : "https://profiles.wordpress.org/pierrelannoy/",
"requires" : "6.2",
"tested" : "6.8",
"requires_php" : "8.1"
}
2 changes: 0 additions & 2 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ Full featured monitoring & analytics for WordPress APIs.

**Traffic** is a full featured analytics reporting tool that analyzes all inbound and outbound API calls made to/from your site.

> 🎁 Give this plugin a drive test on a free dummy site: [One-Click Test!](https://tastewp.com/new/?pre-installed-plugin-slug=traffic)

At this time, **Traffic** can report, for inbound and outbound traffic:

* KPIs: number of calls, data volume, server error rate, quotas error rate, effective pass rate and perceived uptime;
Expand Down

0 comments on commit 742f39f

Please sign in to comment.