Skip to content

Commit

Permalink
Generic WIP ; next is check get an updated package from remote with s…
Browse files Browse the repository at this point in the history
…cheduler, then with webhook
  • Loading branch information
froger-me committed Jan 7, 2024
1 parent 886ffe6 commit 3834f73
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 36 deletions.
5 changes: 5 additions & 0 deletions inc/class-wp-packages-update-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public function __construct( $init_hooks = false ) {
*******************************************************************/

public function action_scheduler_failed_execution( $action_id, Exception $exception, $context = '' ) {

if ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) {
return;
}

php_log(
array(
'action_id' => $action_id,
Expand Down
12 changes: 12 additions & 0 deletions inc/class-wppus-remote-sources-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,18 @@ public static function clear_schedules() {
return $manager->clear_remote_check_scheduled_hooks();
}

public static function register_schedules() {
$manager = new self();
$result = false;

if ( apply_filters( 'wppus_use_recurring_schedule', true ) ) {
$frequency = get_option( 'wppus_remote_repository_check_frequency', 'daily' );
$result = $manager->reschedule_remote_check_recurring_events( $frequency );
}

return $result;
}

public function reschedule_remote_check_recurring_events( $frequency ) {

if ( WPPUS_Update_API::is_doing_api_request() ) {
Expand Down
55 changes: 35 additions & 20 deletions lib/proxy-update-checker/Proxuc/Generic/UpdateChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Anyape\ProxyUpdateChecker\Generic;

use YahnisElsts\PluginUpdateChecker\v5p3\UpdateChecker as BaseUpdateChecker;
use YahnisElsts\PluginUpdateChecker\v5p3\Utils;

if (!class_exists(UpdateChecker::class, false)):

Expand All @@ -16,9 +17,9 @@ public function __construct($metadataUrl, $directoryName, $slug = null, $checkPe
$this->slug = !empty($slug) ? $slug : $this->directoryName;
$this->optionName = $optionName;

if ( empty($this->optionName) ) {
if (empty($this->optionName)) {

if ( $this->filterSuffix === '' ) {
if ($this->filterSuffix === '') {
$this->optionName = 'external_updates-' . $this->slug;
} else {
$this->optionName = $this->getUniqueName('external_updates');
Expand All @@ -32,31 +33,53 @@ public function __construct($metadataUrl, $directoryName, $slug = null, $checkPe
* @return string
*/
protected function getUpdateListKey() {
php_log();
return $this->directoryName;
}

public function requestUpdate() {
php_log();
list($genericUpdate, $result) = $this->requestMetadata(Update::class, 'request_update');
$api = $this->api;
$api->setLocalDirectory($this->Vcs_getAbsoluteDirectoryPath());

$update = new Update();
$update->slug = $this->slug;
$update->version = null;

//Figure out which reference (tag or branch) we'll use to get the latest version of the theme.
$updateSource = $api->chooseReference($this->branch);
if ($updateSource) {
$ref = $updateSource->name;
$update->download_url = $updateSource->downloadUrl;
} else {
return 'source_not_found';
}

//Get headers from the main stylesheet in this branch/tag. Its "Version" header and other metadata
//are what the WordPress install will actually see after upgrading, so they take precedence over releases/tags.
$file = $api->getRemoteFile('wppus.json', $ref);

if (!empty($file)) {
$remoteHeader = json_decode($file, true);
$update->version = Utils::findNotEmpty(array(
$remoteHeader['Version'],
Utils::get($updateSource, 'version'),
));
}

if ($genericUpdate !== null) {
/** @var Update $genericUpdate */
$genericUpdate->slug = $this->slug;
if (empty($update->version)) {
//It looks like we didn't find a valid update after all.
$update = null;
}

$genericUpdate = $this->filterUpdateResult($genericUpdate, $result);
$update = $this->filterUpdateResult($update);

return $genericUpdate;
return $update;
}

protected function filterUpdateResult($update, $httpResult = null) {
php_log();
return apply_filters($this->getUniqueName('request_update_result'), $update, $httpResult);
}

protected function getNoUpdateItemFields() {
php_log();
$fields = parent::getNoUpdateItemFields();

unset($fields['requires_php']);
Expand All @@ -68,42 +91,34 @@ protected function getNoUpdateItemFields() {
}

public function userCanInstallUpdates() {
php_log();
return false;
}

protected function createScheduler($checkPeriod) {
php_log();
return null;
}

public function isBeingUpgraded($upgrader = null) {
php_log();
return false;
}

public function addQueryArgFilter($callback){
php_log();
$this->addFilter('request_update_query_args', $callback);
}

public function addHttpRequestArgFilter($callback) {
php_log();
$this->addFilter('request_update_options', $callback);
}

public function addResultFilter($callback) {
php_log();
$this->addFilter('request_update_result', $callback, 10, 2);
}

protected function createInstalledPackage() {
php_log();
return null;
}

protected function getInstalledTranslations() {
php_log();
return array();
}
}
Expand Down
5 changes: 0 additions & 5 deletions lib/proxy-update-checker/Proxuc/Vcs/GenericUpdateChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,10 @@ public function __construct($api, $slug, $generic_file_name, $package_container,
}

public function Vcs_getAbsoluteDirectoryPath() {
php_log();
return trailingslashit($this->genericAbsolutePath);
}

public function requestInfo($unused = null) {
php_log();
$update = $this->requestUpdate();
$info = null;

Expand Down Expand Up @@ -81,21 +79,18 @@ public function requestInfo($unused = null) {
}

public function setBranch($branch) {
php_log();
$this->branch = $branch;

return $this;
}

public function setAuthentication($credentials) {
php_log();
$this->api->setAuthentication($credentials);

return $this;
}

public function getVcsApi() {
php_log();
return $this->api;
}
}
Expand Down
25 changes: 14 additions & 11 deletions lib/proxy-update-checker/Proxuc/Vcs/ThemeUpdateChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use YahnisElsts\PluginUpdateChecker\v5p3\Theme\Update;
use YahnisElsts\PluginUpdateChecker\v5p3\Theme\UpdateChecker;

if ( ! class_exists(Proxuc_Vcs_ThemeUpdateChecker::class, false) ):
if (! class_exists(Proxuc_Vcs_ThemeUpdateChecker::class, false)):

class Proxuc_Vcs_ThemeUpdateChecker extends UpdateChecker implements BaseChecker {

Expand Down Expand Up @@ -47,10 +47,10 @@ public function __construct($api, $slug, $unused, $package_container, $optionNam

$this->optionName = $optionName;

if ( empty($this->optionName) ) {
if (empty($this->optionName)) {
//BC: Initially the library only supported plugin updates and didn't use type prefixes
//in the option name. Lets use the same prefix-less name when possible.
if ( $this->filterSuffix === '' ) {
if ($this->filterSuffix === '') {
$this->optionName = 'external_updates-' . $this->slug;
} else {
$this->optionName = $this->getUniqueName('external_updates');
Expand All @@ -61,7 +61,6 @@ public function __construct($api, $slug, $unused, $package_container, $optionNam
}

public function Vcs_getAbsoluteDirectoryPath() {

return trailingslashit($this->themeAbsolutePath);
}

Expand All @@ -75,7 +74,8 @@ public function requestUpdate() {

//Figure out which reference (tag or branch) we'll use to get the latest version of the theme.
$updateSource = $api->chooseReference($this->branch);
if ( $updateSource ) {

if ($updateSource) {
$ref = $updateSource->name;
$update->download_url = $updateSource->downloadUrl;
} else {
Expand All @@ -85,15 +85,16 @@ public function requestUpdate() {
//Get headers from the main stylesheet in this branch/tag. Its "Version" header and other metadata
//are what the WordPress install will actually see after upgrading, so they take precedence over releases/tags.
$file = $api->getRemoteFile('style.css', $ref);
if ( ! empty($file) ) {

if (!empty($file)) {
$remoteHeader = $this->package->getFileHeader($file);
$update->version = Utils::findNotEmpty(array(
$remoteHeader['Version'],
Utils::get($updateSource, 'version'),
));
}

if ( empty($update->version) ) {
if (empty($update->version)) {
//It looks like we didn't find a valid update after all.
$update = null;
}
Expand All @@ -107,9 +108,9 @@ public function requestInfo($unused = null) {
$update = $this->requestUpdate();
$info = null;

if ( $update && 'source_not_found' !== $update ) {
if ($update && 'source_not_found' !== $update) {

if ( !empty($update->download_url) ) {
if (!empty($update->download_url)) {
$update->download_url = $this->api->signDownloadUrl($update->download_url);
}

Expand All @@ -119,7 +120,7 @@ public function requestInfo($unused = null) {
'main_file' => 'style.css',
'download_url' => $update->download_url,
);
} elseif ( 'source_not_found' === $update ) {
} elseif ('source_not_found' === $update) {

return new WP_Error(
'puc-no-update-source',
Expand All @@ -135,11 +136,13 @@ public function requestInfo($unused = null) {

public function setBranch($branch) {
$this->branch = $branch;

return $this;
}

public function setAuthentication($credentials) {
$this->api->setAuthentication($credentials);

return $this;
}

Expand All @@ -150,7 +153,7 @@ public function getVcsApi() {
public function getUpdate() {
$update = parent::getUpdate();

if ( isset($update) && !empty($update->download_url) ) {
if (isset($update) && !empty($update->download_url)) {
$update->download_url = $this->api->signDownloadUrl($update->download_url);
}

Expand Down

0 comments on commit 3834f73

Please sign in to comment.