Skip to content

Commit

Permalink
Theme Updater & Changelog
Browse files Browse the repository at this point in the history
* Add: Theme Updater
* Fix: Changelog Display in Dashboard
  • Loading branch information
alinashevchenkome committed Nov 15, 2022
1 parent ff5c73e commit 05f52cb
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 6 deletions.
4 changes: 4 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
== 1.1.0 | 15th November 2022 ==
* Add: Theme Updater
* Fix: Changelog Display in Dashboard

== 1.0.0 | 27th October 2022 ==
* Initial Version.

Expand Down
2 changes: 1 addition & 1 deletion functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @package Base
*/

define( 'AVANAM_VERSION', '1.0.0' );
define( 'AVANAM_VERSION', '1.1.0' );
define( 'AVANAM_MINIMUM_WP_VERSION', '5.4' );
define( 'AVANAM_MINIMUM_PHP_VERSION', '7.2' );

Expand Down
1 change: 1 addition & 0 deletions inc/class-theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ protected function get_default_components() : array {
new Entry_Title\Component(),
new Archive_Title\Component(),
new Third_Party\Component(),
new Updater\Component(),
);
if ( class_exists( '\Elementor\Plugin' ) ) {
$components[] = new Elementor\Component();
Expand Down
106 changes: 106 additions & 0 deletions inc/components/updater/component.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php
/**
* Base\Updater\Component class
*
* @package Base
*/

namespace Base\Updater;

use Base\Component_Interface;
use function Base\webapp;
use function add_filter;
use function get_template;
use function wp_get_theme;
use function get_bloginfo;

/**
* Class for improving updater among various core features.
*/
class Component implements Component_Interface {

/**
* Gets the unique identifier for the theme component.
*
* @return string Component slug.
*/
public function get_slug() : string {
return 'updater';
}

/**
* Adds the action and filter hooks to integrate with WordPress.
*/
public function initialize() {
add_filter( 'site_transient_update_themes', array( $this, 'base_theme_updater' ) );
}

/**
* Theme Updater : $transient
*
*/
public function base_theme_updater( $transient ) {

$stylesheet = get_template();

$theme = wp_get_theme();
$version = $theme->get( 'Version' );

if( false == $remote = get_transient( 'avanam-theme-update'.$version ) ) {

// connect to a remote server where the update information is stored
$remote = wp_remote_get(
'https://avanam.org/updates/avanam.json',
array(
'timeout' => 10,
'headers' => array(
'Accept' => 'application/json'
)
)
);

if(
is_wp_error( $remote )
|| 200 !== wp_remote_retrieve_response_code( $remote )
|| empty( wp_remote_retrieve_body( $remote ) )
) {
return $transient;
}

$remote = json_decode( wp_remote_retrieve_body( $remote ) );

if( ! $remote ) {
return $transient;
}

set_transient( 'avanam-theme-update'.$version, $remote, HOUR_IN_SECONDS );

}

$data = array(
'theme' => $stylesheet,
'url' => $remote->details_url,
'requires' => $remote->requires,
'requires_php' => $remote->requires_php,
'new_version' => $remote->version,
'package' => $remote->download_url,
);

if(
$remote
&& version_compare( $version, $remote->version, '<' )
&& version_compare( $remote->requires, get_bloginfo( 'version' ), '<' )
) {

$transient->response[ $stylesheet ] = $data;

} else {

$transient->no_update[ $stylesheet ] = $data;

}

return $transient;

}
}
14 changes: 12 additions & 2 deletions inc/dashboard/class-theme-dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,12 @@ public function get_changelog() {
if ( is_wp_error( $changelog_string ) ) {
return $changelog;
}
$changelog = explode( PHP_EOL, $changelog_string );

$changelog_string = str_replace("\\r\\n", "\n", $changelog_string);
$changelog_string = str_replace("\\n\\r", "\n", $changelog_string);

$changelog = preg_split("/\n/", $changelog_string);
//$changelog = explode( PHP_EOL, $changelog_string );
$releases = [];
foreach ( $changelog as $changelog_line ) {
if ( empty( $changelog_line ) ) {
Expand Down Expand Up @@ -213,7 +218,12 @@ public function get_pro_changelog() {
if ( is_wp_error( $changelog_string ) ) {
return $changelog;
}
$changelog = explode( PHP_EOL, $changelog_string );

$changelog_string = str_replace("\\r\\n", "\n", $changelog_string);
$changelog_string = str_replace("\\n\\r", "\n", $changelog_string);

$changelog = preg_split("/\n/", $changelog_string);
//$changelog = explode( PHP_EOL, $changelog_string );
$releases = [];
foreach ( $changelog as $changelog_line ) {
if ( empty( $changelog_line ) ) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "avanam",
"version": "1.0.0",
"version": "1.1.0",
"private": true,
"main": "inc/customizer/react/src/index.js",
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Tags: translation-ready, two-columns, right-sidebar, left-sidebar, footer-widget
Requires at least: 5.4
Tested up to: 6.1
Requires PHP: 7.2
Stable tag: 1.0.0
Stable tag: 1.1.0
License: GNU General Public License v3.0 (or later)
License URI: https://www.gnu.org/licenses/gpl-3.0.html

Expand Down
2 changes: 1 addition & 1 deletion style.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Theme URI: https://avanam.org
Author: The WP Rig Contributors
Author URI: https://github.com/wprig/wprig/
Description: This is starter WordPress theme called Avanam. Created for theme developers to start building beautiful WordPress themes using Avanam.
Version: 1.0.0
Version: 1.1.0
License: GNU General Public License v3.0 (or later)
License URI: https://www.gnu.org/licenses/gpl-3.0.html
Text Domain: avanam
Expand Down

0 comments on commit 05f52cb

Please sign in to comment.