Put all^ PUC classes in namespaces.

^ Except dependencies like Parsedown.

The readme is now out of date. The legacy version of Parsedown was removed because we no longer need to support PHP versions older than 5.3. The stub file that loads ParsedownModern.php stays in place because it has the "class_exists" check.
This commit is contained in:
Yahnis Elsts 2022-10-08 23:15:37 +03:00
parent 90bee76fa9
commit 4b6127f0f2
39 changed files with 481 additions and 1888 deletions

View file

@ -1,6 +0,0 @@
<?php
if ( !class_exists('Puc_v4_Factory', false) ):

class Puc_v4_Factory extends Puc_v5p0_Factory { }

endif;

10
Puc/v5/PucFactory.php Normal file
View file

@ -0,0 +1,10 @@
<?php

namespace YahnisElsts\PluginUpdateChecker\v5;

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

class PucFactory extends \YahnisElsts\PluginUpdateChecker\v5p0\PucFactory {
}

endif;

View file

@ -1,25 +1,23 @@
<?php

if ( !class_exists('Puc_v5p0_Autoloader', false) ):
namespace YahnisElsts\PluginUpdateChecker\v5p0;

class Puc_v5p0_Autoloader {
private $prefix = '';
private $rootDir = '';
private $libraryDir = '';
if ( !class_exists(Autoloader::class, false) ):

class Autoloader {
const DEFAULT_NS_PREFIX = 'YahnisElsts\\PluginUpdateChecker\\';

private $prefix;
private $rootDir;
private $libraryDir;

private $staticMap;

public function __construct() {
$this->rootDir = dirname(__FILE__) . '/';

if ( version_compare(PHP_VERSION, '5.3', '>=') && __NAMESPACE__ ) {
$namespaceWithSlash = __NAMESPACE__ . '\\';
} else {
$namespaceWithSlash = '';
}

$nameParts = explode('_', substr(__CLASS__, strlen($namespaceWithSlash)), 3);
$this->prefix = $namespaceWithSlash . $nameParts[0] . '_' . $nameParts[1] . '_';
$namespaceWithSlash = __NAMESPACE__ . '\\';
$this->prefix = $namespaceWithSlash;

$this->libraryDir = $this->rootDir . '../..';
if ( !self::isPhar() ) {
@ -27,11 +25,31 @@ if ( !class_exists('Puc_v5p0_Autoloader', false) ):
}
$this->libraryDir = $this->libraryDir . '/';

$this->staticMap = array(
$namespaceWithSlash . 'PucReadmeParser' => 'vendor/PucReadmeParser.php',
$namespaceWithSlash . 'Parsedown' => 'vendor/Parsedown.php',
$namespaceWithSlash . 'Puc_v4_Factory' => 'Puc/v4/Factory.php',
//Usually, dependencies like Parsedown are in the global namespace,
//but if someone adds a custom namespace to the entire library, they
//will be in the same namespace as this class.
$isCustomNamespace = (
substr($namespaceWithSlash, 0, strlen(self::DEFAULT_NS_PREFIX)) !== self::DEFAULT_NS_PREFIX
);
$libraryPrefix = $isCustomNamespace ? $namespaceWithSlash : '';

$this->staticMap = array(
$libraryPrefix . 'PucReadmeParser' => 'vendor/PucReadmeParser.php',
$libraryPrefix . 'Parsedown' => 'vendor/Parsedown.php',
);

//Add the generic, major-version-only factory class to the static map.
$versionSeparatorPos = strrpos(__NAMESPACE__, '\\v');
if ( $versionSeparatorPos !== false ) {
$versionSegment = substr(__NAMESPACE__, $versionSeparatorPos + 1);
$pointPos = strpos($versionSegment, 'p');
if ( ($pointPos !== false) && ($pointPos > 1) ) {
$majorVersionSegment = substr($versionSegment, 0, $pointPos);
$majorVersionNs = __NAMESPACE__ . '\\' . $majorVersionSegment;
$this->staticMap[$majorVersionNs . '\\PucFactory'] =
'Puc/' . $majorVersionSegment . '/Factory.php';
}
}

spl_autoload_register(array($this, 'autoload'));
}
@ -49,18 +67,16 @@ if ( !class_exists('Puc_v5p0_Autoloader', false) ):

public function autoload($className) {
if ( isset($this->staticMap[$className]) && file_exists($this->libraryDir . $this->staticMap[$className]) ) {
/** @noinspection PhpIncludeInspection */
include ($this->libraryDir . $this->staticMap[$className]);
include($this->libraryDir . $this->staticMap[$className]);
return;
}

if (strpos($className, $this->prefix) === 0) {
if ( strpos($className, $this->prefix) === 0 ) {
$path = substr($className, strlen($this->prefix));
$path = str_replace('_', '/', $path);
$path = $this->rootDir . $path . '.php';

if (file_exists($path)) {
/** @noinspection PhpIncludeInspection */
if ( file_exists($path) ) {
include $path;
}
}

View file

@ -1,12 +1,17 @@
<?php
if ( !class_exists('Puc_v5p0_DebugBar_Extension', false) ):
namespace YahnisElsts\PluginUpdateChecker\v5p0\DebugBar;

class Puc_v5p0_DebugBar_Extension {
use YahnisElsts\PluginUpdateChecker\v5p0\PucFactory;
use YahnisElsts\PluginUpdateChecker\v5p0\UpdateChecker;

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

class Extension {
const RESPONSE_BODY_LENGTH_LIMIT = 4000;

/** @var Puc_v5p0_UpdateChecker */
/** @var UpdateChecker */
protected $updateChecker;
protected $panelClass = 'Puc_v5p0_DebugBar_Panel';
protected $panelClass = Panel::class;

public function __construct($updateChecker, $panelClass = null) {
$this->updateChecker = $updateChecker;
@ -79,7 +84,7 @@ if ( !class_exists('Puc_v5p0_DebugBar_Extension', false) ):

foreach (array_values($errors) as $num => $item) {
$wpError = $item['error'];
/** @var WP_Error $wpError */
/** @var \WP_Error $wpError */
printf('<h4>%d) %s</h4>', $num + 1, esc_html($wpError->get_error_message()));

echo '<dl>';
@ -92,7 +97,7 @@ if ( !class_exists('Puc_v5p0_DebugBar_Extension', false) ):
if ( isset($item['httpResponse']) ) {
if ( is_wp_error($item['httpResponse']) ) {
$httpError = $item['httpResponse'];
/** @var WP_Error $httpError */
/** @var \WP_Error $httpError */
printf(
'<dt>WordPress HTTP API error:</dt><dd>%s (<code>%s</code>)</dd>',
esc_html($httpError->get_error_message()),
@ -163,11 +168,11 @@ if ( !class_exists('Puc_v5p0_DebugBar_Extension', false) ):
$absolutePath = realpath(dirname(__FILE__) . '/../../../' . ltrim($filePath, '/'));

//Where is the library located inside the WordPress directory structure?
$absolutePath = Puc_v5p0_Factory::normalizePath($absolutePath);
$absolutePath = PucFactory::normalizePath($absolutePath);

$pluginDir = Puc_v5p0_Factory::normalizePath(WP_PLUGIN_DIR);
$muPluginDir = Puc_v5p0_Factory::normalizePath(WPMU_PLUGIN_DIR);
$themeDir = Puc_v5p0_Factory::normalizePath(get_theme_root());
$pluginDir = PucFactory::normalizePath(WP_PLUGIN_DIR);
$muPluginDir = PucFactory::normalizePath(WPMU_PLUGIN_DIR);
$themeDir = PucFactory::normalizePath(get_theme_root());

if ( (strpos($absolutePath, $pluginDir) === 0) || (strpos($absolutePath, $muPluginDir) === 0) ) {
//It's part of a plugin.

View file

@ -1,9 +1,12 @@
<?php
namespace YahnisElsts\PluginUpdateChecker\v5p0\DebugBar;

if ( !class_exists('Puc_v5p0_DebugBar_Panel', false) && class_exists('Debug_Bar_Panel', false) ):
use YahnisElsts\PluginUpdateChecker\v5p0\UpdateChecker;

class Puc_v5p0_DebugBar_Panel extends Debug_Bar_Panel {
/** @var Puc_v5p0_UpdateChecker */
if ( !class_exists(Panel::class, false) && class_exists('Debug_Bar_Panel', false) ):

class Panel extends \Debug_Bar_Panel {
/** @var UpdateChecker */
protected $updateChecker;

private $responseBox = '<div class="puc-ajax-response" style="display: none;"></div>';

View file

@ -1,12 +1,17 @@
<?php
if ( !class_exists('Puc_v5p0_DebugBar_PluginExtension', false) ):

class Puc_v5p0_DebugBar_PluginExtension extends Puc_v5p0_DebugBar_Extension {
/** @var Puc_v5p0_Plugin_UpdateChecker */
namespace YahnisElsts\PluginUpdateChecker\v5p0\DebugBar;

use YahnisElsts\PluginUpdateChecker\v5p0\Plugin\UpdateChecker;

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

class PluginExtension extends Extension {
/** @var UpdateChecker */
protected $updateChecker;

public function __construct($updateChecker) {
parent::__construct($updateChecker, 'Puc_v5p0_DebugBar_PluginPanel');
parent::__construct($updateChecker, PluginPanel::class);

add_action('wp_ajax_puc_v4_debug_request_info', array($this, 'ajaxRequestInfo'));
}

View file

@ -1,10 +1,13 @@
<?php
namespace YahnisElsts\PluginUpdateChecker\v5p0\DebugBar;

if ( !class_exists('Puc_v5p0_DebugBar_PluginPanel', false) ):
use YahnisElsts\PluginUpdateChecker\v5p0\Plugin\UpdateChecker;

class Puc_v5p0_DebugBar_PluginPanel extends Puc_v5p0_DebugBar_Panel {
if ( !class_exists(PluginPanel::class, false) ):

class PluginPanel extends Panel {
/**
* @var Puc_v5p0_Plugin_UpdateChecker
* @var UpdateChecker
*/
protected $updateChecker;


View file

@ -1,10 +1,14 @@
<?php

if ( !class_exists('Puc_v5p0_DebugBar_ThemePanel', false) ):
namespace YahnisElsts\PluginUpdateChecker\v5p0\DebugBar;

class Puc_v5p0_DebugBar_ThemePanel extends Puc_v5p0_DebugBar_Panel {
use YahnisElsts\PluginUpdateChecker\v5p0\Theme\UpdateChecker;

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

class ThemePanel extends Panel {
/**
* @var Puc_v5p0_Theme_UpdateChecker
* @var UpdateChecker
*/
protected $updateChecker;


View file

@ -1,5 +1,7 @@
<?php
if ( !class_exists('Puc_v5p0_InstalledPackage', false) ):
namespace YahnisElsts\PluginUpdateChecker\v5p0;

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

/**
* This class represents a currently installed plugin or theme.
@ -7,9 +9,9 @@ if ( !class_exists('Puc_v5p0_InstalledPackage', false) ):
* Not to be confused with the "package" field in WP update API responses that contains
* the download URL of a the new version.
*/
abstract class Puc_v5p0_InstalledPackage {
abstract class InstalledPackage {
/**
* @var Puc_v5p0_UpdateChecker
* @var UpdateChecker
*/
protected $updateChecker;


View file

@ -1,5 +1,11 @@
<?php
if ( !class_exists('Puc_v5p0_Metadata', false) ):
namespace YahnisElsts\PluginUpdateChecker\v5p0;

use LogicException;
use stdClass;
use WP_Error;

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

/**
* A base container for holding information about updates and plugin metadata.
@ -8,7 +14,7 @@ if ( !class_exists('Puc_v5p0_Metadata', false) ):
* @copyright 2016
* @access public
*/
abstract class Puc_v5p0_Metadata {
abstract class Metadata {

/**
* Create an instance of this class from a JSON document.
@ -17,7 +23,7 @@ if ( !class_exists('Puc_v5p0_Metadata', false) ):
* @param string $json
* @return self
*/
public static function fromJson(/** @noinspection PhpUnusedParameterInspection */ $json) {
public static function fromJson($json) {
throw new LogicException('The ' . __METHOD__ . ' method must be implemented by subclasses');
}

@ -27,7 +33,7 @@ if ( !class_exists('Puc_v5p0_Metadata', false) ):
* @return bool
*/
protected static function createFromJson($json, $target) {
/** @var StdClass $apiResponse */
/** @var \StdClass $apiResponse */
$apiResponse = json_decode($json);
if ( empty($apiResponse) || !is_object($apiResponse) ){
$errorMessage = "Failed to parse update metadata. Try validating your .json file with http://jsonlint.com/";
@ -53,10 +59,10 @@ if ( !class_exists('Puc_v5p0_Metadata', false) ):
/**
* No validation by default! Subclasses should check that the required fields are present.
*
* @param StdClass $apiResponse
* @return bool|WP_Error
* @param \StdClass $apiResponse
* @return bool|\WP_Error
*/
protected function validateMetadata(/** @noinspection PhpUnusedParameterInspection */ $apiResponse) {
protected function validateMetadata($apiResponse) {
return true;
}

@ -64,10 +70,10 @@ if ( !class_exists('Puc_v5p0_Metadata', false) ):
* Create a new instance by copying the necessary fields from another object.
*
* @abstract
* @param StdClass|self $object The source object.
* @param \StdClass|self $object The source object.
* @return self The new copy.
*/
public static function fromObject(/** @noinspection PhpUnusedParameterInspection */ $object) {
public static function fromObject($object) {
throw new LogicException('The ' . __METHOD__ . ' method must be implemented by subclasses');
}

@ -77,7 +83,7 @@ if ( !class_exists('Puc_v5p0_Metadata', false) ):
* avoids the "incomplete object" problem if the cached value is loaded
* before this class.
*
* @return StdClass
* @return \StdClass
*/
public function toStdClass() {
$object = new stdClass();
@ -95,8 +101,8 @@ if ( !class_exists('Puc_v5p0_Metadata', false) ):
/**
* Copy known fields from one object to another.
*
* @param StdClass|self $from
* @param StdClass|self $to
* @param \StdClass|self $from
* @param \StdClass|self $to
*/
protected function copyFields($from, $to) {
$fields = $this->getFieldNames();

View file

@ -1,11 +1,12 @@
<?php
namespace YahnisElsts\PluginUpdateChecker\v5p0;

if ( !class_exists('Puc_v5p0_OAuthSignature', false) ):
if ( !class_exists(OAuthSignature::class, false) ):

/**
* A basic signature generator for zero-legged OAuth 1.0.
*/
class Puc_v5p0_OAuthSignature {
class OAuthSignature {
private $consumerKey = '';
private $consumerSecret = '';

@ -85,7 +86,7 @@ if ( !class_exists('Puc_v5p0_OAuthSignature', false) ):
if ( is_callable('random_bytes') ) {
try {
$rand = random_bytes(16);
} catch (Exception $ex) {
} catch (\Exception $ex) {
//Fall back to mt_rand (below).
}
}

View file

@ -1,9 +1,14 @@
<?php
if ( !class_exists('Puc_v5p0_Plugin_Package', false) ):
namespace YahnisElsts\PluginUpdateChecker\v5p0\Plugin;

class Puc_v5p0_Plugin_Package extends Puc_v5p0_InstalledPackage {
use YahnisElsts\PluginUpdateChecker\v5p0\InstalledPackage;
use YahnisElsts\PluginUpdateChecker\v5p0\PucFactory;

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

class Package extends InstalledPackage {
/**
* @var Puc_v5p0_Plugin_UpdateChecker
* @var UpdateChecker
*/
protected $updateChecker;

@ -140,7 +145,6 @@ if ( !class_exists('Puc_v5p0_Plugin_Package', false) ):
}

if ( !function_exists('get_plugin_data') ) {
/** @noinspection PhpIncludeInspection */
require_once(ABSPATH . '/wp-admin/includes/plugin.php');
}
return get_plugin_data($this->pluginAbsolutePath, false, false);
@ -170,8 +174,8 @@ if ( !class_exists('Puc_v5p0_Plugin_Package', false) ):
$pluginPath = realpath($this->pluginAbsolutePath);
//If realpath() fails, just normalize the syntax instead.
if (($muPluginDir === false) || ($pluginPath === false)) {
$muPluginDir = Puc_v5p0_Factory::normalizePath(WPMU_PLUGIN_DIR);
$pluginPath = Puc_v5p0_Factory::normalizePath($this->pluginAbsolutePath);
$muPluginDir = PucFactory::normalizePath(WPMU_PLUGIN_DIR);
$pluginPath = PucFactory::normalizePath($this->pluginAbsolutePath);
}

$cachedResult = (strpos($pluginPath, $muPluginDir) === 0);

View file

@ -1,5 +1,9 @@
<?php
if ( !class_exists('Puc_v5p0_Plugin_Info', false) ):
namespace YahnisElsts\PluginUpdateChecker\v5p0\Plugin;

use YahnisElsts\PluginUpdateChecker\v5p0\Metadata;

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

/**
* A container class for holding and transforming various plugin metadata.
@ -8,7 +12,7 @@ if ( !class_exists('Puc_v5p0_Plugin_Info', false) ):
* @copyright 2016
* @access public
*/
class Puc_v5p0_Plugin_Info extends Puc_v5p0_Metadata {
class PluginInfo extends Metadata {
//Most fields map directly to the contents of the plugin's info.json file.
//See the relevant docs for a description of their meaning.
public $name;
@ -64,8 +68,8 @@ if ( !class_exists('Puc_v5p0_Plugin_Info', false) ):
/**
* Very, very basic validation.
*
* @param StdClass $apiResponse
* @return bool|WP_Error
* @param \StdClass $apiResponse
* @return bool|\WP_Error
*/
protected function validateMetadata($apiResponse) {
if (
@ -73,7 +77,7 @@ if ( !class_exists('Puc_v5p0_Plugin_Info', false) ):
|| empty($apiResponse->name)
|| empty($apiResponse->version)
) {
return new WP_Error(
return new \WP_Error(
'puc-invalid-metadata',
"The plugin metadata file does not contain the required 'name' and/or 'version' keys."
);
@ -88,7 +92,7 @@ if ( !class_exists('Puc_v5p0_Plugin_Info', false) ):
* @return object
*/
public function toWpFormat(){
$info = new stdClass;
$info = new \stdClass;

//The custom update API is built so that many fields have the same name and format
//as those returned by the native WordPress.org API. These can be assigned directly.

View file

@ -1,14 +1,16 @@
<?php
if ( !class_exists('Puc_v5p0_Plugin_Ui', false) ):
namespace YahnisElsts\PluginUpdateChecker\v5p0\Plugin;

if ( !class_exists('Ui', false) ):
/**
* Additional UI elements for plugins.
*/
class Puc_v5p0_Plugin_Ui {
class Ui {
private $updateChecker;
private $manualCheckErrorTransient = '';

/**
* @param Puc_v5p0_Plugin_UpdateChecker $updateChecker
* @param UpdateChecker $updateChecker
*/
public function __construct($updateChecker) {
$this->updateChecker = $updateChecker;
@ -172,7 +174,7 @@ if ( !class_exists('Puc_v5p0_Plugin_Ui', false) ):

foreach ($lastRequestApiErrors as $item) {
$wpError = $item['error'];
/** @var WP_Error $wpError */
/** @var \WP_Error $wpError */
if ( !in_array($wpError->get_error_code(), $questionableErrorCodes) ) {
$foundCriticalErrors = true;
break;
@ -254,7 +256,7 @@ if ( !class_exists('Puc_v5p0_Plugin_Ui', false) ):
}
foreach ($errors as $item) {
$wpError = $item['error'];
/** @var WP_Error $wpError */
/** @var \WP_Error $wpError */
$output .= sprintf(
$formatString,
$wpError->get_error_message(),

View file

@ -1,5 +1,9 @@
<?php
if ( !class_exists('Puc_v5p0_Plugin_Update', false) ):
namespace YahnisElsts\PluginUpdateChecker\v5p0\Plugin;

use YahnisElsts\PluginUpdateChecker\v5p0\Update as BaseUpdate;

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

/**
* A simple container class for holding information about an available update.
@ -8,7 +12,7 @@ if ( !class_exists('Puc_v5p0_Plugin_Update', false) ):
* @copyright 2016
* @access public
*/
class Puc_v5p0_Plugin_Update extends Puc_v5p0_Update {
class Update extends BaseUpdate {
public $id = 0;
public $homepage;
public $upgrade_notice;
@ -25,13 +29,13 @@ if ( !class_exists('Puc_v5p0_Plugin_Update', false) ):
* Create a new instance of PluginUpdate from its JSON-encoded representation.
*
* @param string $json
* @return Puc_v5p0_Plugin_Update|null
* @return self|null
*/
public static function fromJson($json){
//Since update-related information is simply a subset of the full plugin info,
//we can parse the update JSON as if it was a plugin info string, then copy over
//the parts that we care about.
$pluginInfo = Puc_v5p0_Plugin_Info::fromJson($json);
$pluginInfo = PluginInfo::fromJson($json);
if ( $pluginInfo !== null ) {
return self::fromPluginInfo($pluginInfo);
} else {
@ -43,18 +47,18 @@ if ( !class_exists('Puc_v5p0_Plugin_Update', false) ):
* Create a new instance of PluginUpdate based on an instance of PluginInfo.
* Basically, this just copies a subset of fields from one object to another.
*
* @param Puc_v5p0_Plugin_Info $info
* @return Puc_v5p0_Plugin_Update
* @param PluginInfo $info
* @return static
*/
public static function fromPluginInfo($info){
return self::fromObject($info);
return static::fromObject($info);
}

/**
* Create a new instance by copying the necessary fields from another object.
*
* @param StdClass|Puc_v5p0_Plugin_Info|Puc_v5p0_Plugin_Update $object The source object.
* @return Puc_v5p0_Plugin_Update The new copy.
* @param \StdClass|PluginInfo|self $object The source object.
* @return self The new copy.
*/
public static function fromObject($object) {
$update = new self();

View file

@ -1,5 +1,12 @@
<?php
if ( !class_exists('Puc_v5p0_Plugin_UpdateChecker', false) ):
namespace YahnisElsts\PluginUpdateChecker\v5p0\Plugin;

use YahnisElsts\PluginUpdateChecker\v5p0\InstalledPackage;
use YahnisElsts\PluginUpdateChecker\v5p0\UpdateChecker as BaseUpdateChecker;
use YahnisElsts\PluginUpdateChecker\v5p0\Scheduler;
use YahnisElsts\PluginUpdateChecker\v5p0\DebugBar;

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

/**
* A custom plugin update checker.
@ -8,7 +15,7 @@ if ( !class_exists('Puc_v5p0_Plugin_UpdateChecker', false) ):
* @copyright 2018
* @access public
*/
class Puc_v5p0_Plugin_UpdateChecker extends Puc_v5p0_UpdateChecker {
class UpdateChecker extends BaseUpdateChecker {
protected $updateTransient = 'update_plugins';
protected $translationType = 'plugin';

@ -17,7 +24,7 @@ if ( !class_exists('Puc_v5p0_Plugin_UpdateChecker', false) ):
public $muPluginFile = ''; //For MU plugins, the plugin filename relative to the mu-plugins directory.

/**
* @var Puc_v5p0_Plugin_Package
* @var Package
*/
protected $package;

@ -68,17 +75,17 @@ if ( !class_exists('Puc_v5p0_Plugin_UpdateChecker', false) ):
//Details: https://github.com/YahnisElsts/plugin-update-checker/issues/138#issuecomment-335590964
add_action('uninstall_' . $this->pluginFile, array($this, 'removeHooks'));

$this->extraUi = new Puc_v5p0_Plugin_Ui($this);
$this->extraUi = new Ui($this);
}

/**
* Create an instance of the scheduler.
*
* @param int $checkPeriod
* @return Puc_v5p0_Scheduler
* @return Scheduler
*/
protected function createScheduler($checkPeriod) {
$scheduler = new Puc_v5p0_Scheduler($this, $checkPeriod, array('load-plugins.php'));
$scheduler = new Scheduler($this, $checkPeriod, array('load-plugins.php'));
register_deactivation_hook($this->pluginFile, array($scheduler, 'removeUpdaterCron'));
return $scheduler;
}
@ -124,13 +131,13 @@ if ( !class_exists('Puc_v5p0_Plugin_UpdateChecker', false) ):
* @uses wp_remote_get()
*
* @param array $queryArgs Additional query arguments to append to the request. Optional.
* @return Puc_v5p0_Plugin_Info
* @return PluginInfo
*/
public function requestInfo($queryArgs = array()) {
list($pluginInfo, $result) = $this->requestMetadata('Puc_v5p0_Plugin_Info', 'request_info', $queryArgs);
list($pluginInfo, $result) = $this->requestMetadata('PluginInfo', 'request_info', $queryArgs);

if ( $pluginInfo !== null ) {
/** @var Puc_v5p0_Plugin_Info $pluginInfo */
/** @var PluginInfo $pluginInfo */
$pluginInfo->filename = $this->pluginFile;
$pluginInfo->slug = $this->slug;
}
@ -144,7 +151,7 @@ if ( !class_exists('Puc_v5p0_Plugin_UpdateChecker', false) ):
*
* @uses PluginUpdateChecker::requestInfo()
*
* @return Puc_v5p0_Update|null An instance of Plugin_Update, or NULL when no updates are available.
* @return Update|null An instance of Plugin Update, or NULL when no updates are available.
*/
public function requestUpdate() {
//For the sake of simplicity, this function just calls requestInfo()
@ -153,7 +160,7 @@ if ( !class_exists('Puc_v5p0_Plugin_UpdateChecker', false) ):
if ( $pluginInfo === null ){
return null;
}
$update = Puc_v5p0_Plugin_Update::fromPluginInfo($pluginInfo);
$update = Update::fromPluginInfo($pluginInfo);

$update = $this->filterUpdateResult($update);

@ -197,9 +204,9 @@ if ( !class_exists('Puc_v5p0_Plugin_UpdateChecker', false) ):
}

/**
* @param stdClass|null $updates
* @param stdClass $updateToAdd
* @return stdClass
* @param \stdClass|null $updates
* @param \stdClass $updateToAdd
* @return \stdClass
*/
protected function addUpdateToList($updates, $updateToAdd) {
if ( $this->package->isMuPlugin() ) {
@ -211,8 +218,8 @@ if ( !class_exists('Puc_v5p0_Plugin_UpdateChecker', false) ):
}

/**
* @param stdClass|null $updates
* @return stdClass|null
* @param \stdClass|null $updates
* @return \stdClass|null
*/
protected function removeUpdateFromList($updates) {
$updates = parent::removeUpdateFromList($updates);
@ -246,7 +253,7 @@ if ( !class_exists('Puc_v5p0_Plugin_UpdateChecker', false) ):
'banners' => array(),
'banners_rtl' => array(),
'tested' => '',
'compatibility' => new stdClass(),
'compatibility' => new \stdClass(),
)
);
}
@ -255,7 +262,7 @@ if ( !class_exists('Puc_v5p0_Plugin_UpdateChecker', false) ):
* Alias for isBeingUpgraded().
*
* @deprecated
* @param WP_Upgrader|null $upgrader The upgrader that's performing the current update.
* @param \WP_Upgrader|null $upgrader The upgrader that's performing the current update.
* @return bool
*/
public function isPluginBeingUpgraded($upgrader = null) {
@ -265,7 +272,7 @@ if ( !class_exists('Puc_v5p0_Plugin_UpdateChecker', false) ):
/**
* Is there an update being installed for this plugin, right now?
*
* @param WP_Upgrader|null $upgrader
* @param \WP_Upgrader|null $upgrader
* @return bool
*/
public function isBeingUpgraded($upgrader = null) {
@ -281,12 +288,12 @@ if ( !class_exists('Puc_v5p0_Plugin_UpdateChecker', false) ):
* Uses cached update data. To retrieve update information straight from
* the metadata URL, call requestUpdate() instead.
*
* @return Puc_v5p0_Plugin_Update|null
* @return Update|null
*/
public function getUpdate() {
$update = parent::getUpdate();
if ( isset($update) ) {
/** @var Puc_v5p0_Plugin_Update $update */
/** @var Update $update */
$update->filename = $this->pluginFile;
}
return $update;
@ -391,20 +398,20 @@ if ( !class_exists('Puc_v5p0_Plugin_UpdateChecker', false) ):
}

protected function createDebugBarExtension() {
return new Puc_v5p0_DebugBar_PluginExtension($this);
return new DebugBar\PluginExtension($this);
}

/**
* Create a package instance that represents this plugin or theme.
*
* @return Puc_v5p0_InstalledPackage
* @return InstalledPackage
*/
protected function createInstalledPackage() {
return new Puc_v5p0_Plugin_Package($this->pluginAbsolutePath, $this);
return new Package($this->pluginAbsolutePath, $this);
}

/**
* @return Puc_v5p0_Plugin_Package
* @return Package
*/
public function getInstalledPackage() {
return $this->package;

View file

@ -1,5 +1,12 @@
<?php
if ( !class_exists('Puc_v5p0_Factory', false) ):

namespace YahnisElsts\PluginUpdateChecker\v5p0;

use YahnisElsts\PluginUpdateChecker\v5p0\Plugin;
use YahnisElsts\PluginUpdateChecker\v5p0\Theme;
use YahnisElsts\PluginUpdateChecker\v5p0\Vcs;

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

/**
* A factory that builds update checker instances.
@ -11,7 +18,7 @@ if ( !class_exists('Puc_v5p0_Factory', false) ):
* At the moment it can only build instances of the UpdateChecker class. Other classes are
* intended mainly for internal use and refer directly to specific implementations.
*/
class Puc_v5p0_Factory {
class PucFactory {
protected static $classVersions = array();
protected static $sorted = false;

@ -23,7 +30,7 @@ if ( !class_exists('Puc_v5p0_Factory', false) ):
*
* @param string $fullPath Full path to the main plugin file or the theme's style.css.
* @param array $args Optional arguments. Keys should match the argument names of the buildUpdateChecker() method.
* @return Puc_v5p0_Plugin_UpdateChecker|Puc_v5p0_Theme_UpdateChecker|Puc_v5p0_Vcs_BaseChecker
* @return Plugin\UpdateChecker|Theme\UpdateChecker|Vcs\BaseChecker
*/
public static function buildFromHeader($fullPath, $args = array()) {
$fullPath = self::normalizePath($fullPath);
@ -44,7 +51,6 @@ if ( !class_exists('Puc_v5p0_Factory', false) ):
$metadataUrl = self::getServiceURI($fullPath);
}

/** @noinspection PhpUndefinedVariableInspection These variables are created by extract(), above. */
return self::buildUpdateChecker($metadataUrl, $fullPath, $slug, $checkPeriod, $optionName, $muPluginFile);
}

@ -54,15 +60,15 @@ if ( !class_exists('Puc_v5p0_Factory', false) ):
* This method automatically detects if you're using it for a plugin or a theme and chooses
* the appropriate implementation for your update source (JSON file, GitHub, BitBucket, etc).
*
* @see Puc_v5p0_UpdateChecker::__construct
* @see UpdateChecker::__construct
*
* @param string $metadataUrl The URL of the metadata file, a GitHub repository, or another supported update source.
* @param string $fullPath Full path to the main plugin file or to the theme directory.
* @param string $slug Custom slug. Defaults to the name of the main plugin file or the theme directory.
* @param int $checkPeriod How often to check for updates (in hours).
* @param string $optionName Where to store book-keeping info about update checks.
* @param string $optionName Where to store bookkeeping info about update checks.
* @param string $muPluginFile The plugin filename relative to the mu-plugins directory.
* @return Puc_v5p0_Plugin_UpdateChecker|Puc_v5p0_Theme_UpdateChecker|Puc_v5p0_Vcs_BaseChecker
* @return Plugin\UpdateChecker|Theme\UpdateChecker|Vcs\BaseChecker
*/
public static function buildUpdateChecker($metadataUrl, $fullPath, $slug = '', $checkPeriod = 12, $optionName = '', $muPluginFile = '') {
$fullPath = self::normalizePath($fullPath);
@ -77,7 +83,7 @@ if ( !class_exists('Puc_v5p0_Factory', false) ):
$type = 'Theme';
$id = $themeDirectory;
} else {
throw new RuntimeException(sprintf(
throw new \RuntimeException(sprintf(
'The update checker cannot determine if "%s" is a plugin or a theme. ' .
'This is a bug. Please contact the PUC developer.',
htmlentities($fullPath)
@ -90,10 +96,10 @@ if ( !class_exists('Puc_v5p0_Factory', false) ):
$apiClass = null;
if ( empty($service) ) {
//The default is to get update information from a remote JSON file.
$checkerClass = $type . '_UpdateChecker';
$checkerClass = $type . '\\UpdateChecker';
} else {
//You can also use a VCS repository like GitHub.
$checkerClass = 'Vcs_' . $type . 'UpdateChecker';
$checkerClass = 'Vcs\\' . $type . 'UpdateChecker';
$apiClass = $service . 'Api';
}

@ -111,11 +117,6 @@ if ( !class_exists('Puc_v5p0_Factory', false) ):
return null;
}

//Add the current namespace to the class name(s).
if ( version_compare(PHP_VERSION, '5.3', '>=') ) {
$checkerClass = __NAMESPACE__ . '\\' . $checkerClass;
}

if ( !isset($apiClass) ) {
//Plain old update checker.
return new $checkerClass($metadataUrl, $id, $slug, $checkPeriod, $optionName, $muPluginFile);
@ -131,10 +132,6 @@ if ( !class_exists('Puc_v5p0_Factory', false) ):
return null;
}

if ( version_compare(PHP_VERSION, '5.3', '>=') && (strpos($apiClass, '\\') === false) ) {
$apiClass = __NAMESPACE__ . '\\' . $apiClass;
}

return new $checkerClass(
new $apiClass($metadataUrl),
$id,
@ -241,7 +238,7 @@ if ( !class_exists('Puc_v5p0_Factory', false) ):
}

//URI was not found so throw an error.
throw new RuntimeException(
throw new \RuntimeException(
sprintf('Unable to locate URI in header of "%s"', htmlentities($fullPath))
);
}

View file

@ -1,11 +1,15 @@
<?php
if ( !class_exists('Puc_v5p0_Scheduler', false) ):
namespace YahnisElsts\PluginUpdateChecker\v5p0;

use YahnisElsts\PluginUpdateChecker\v5p0\Plugin;

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

/**
* The scheduler decides when and how often to check for updates.
* It calls @see Puc_v5p0_UpdateChecker::checkForUpdates() to perform the actual checks.
* It calls @see UpdateChecker::checkForUpdates() to perform the actual checks.
*/
class Puc_v5p0_Scheduler {
class Scheduler {
public $checkPeriod = 12; //How often to check for updates (in hours).
public $throttleRedundantChecks = false; //Check less often if we already know that an update is available.
public $throttledCheckPeriod = 72;
@ -13,7 +17,7 @@ if ( !class_exists('Puc_v5p0_Scheduler', false) ):
protected $hourlyCheckHooks = array('load-update.php');

/**
* @var Puc_v5p0_UpdateChecker
* @var UpdateChecker
*/
protected $updateChecker;

@ -22,7 +26,7 @@ if ( !class_exists('Puc_v5p0_Scheduler', false) ):
/**
* Scheduler constructor.
*
* @param Puc_v5p0_UpdateChecker $updateChecker
* @param UpdateChecker $updateChecker
* @param int $checkPeriod How often to check for updates (in hours).
* @param array $hourlyHooks
*/
@ -89,7 +93,7 @@ if ( !class_exists('Puc_v5p0_Scheduler', false) ):
* We look at the parameters to decide whether to call maybeCheckForUpdates() or not.
* We also check if the update checker has been removed by the update.
*
* @param WP_Upgrader $upgrader WP_Upgrader instance
* @param \WP_Upgrader $upgrader WP_Upgrader instance
* @param array $upgradeInfo extra information about the upgrade
*/
public function upgraderProcessComplete(
@ -130,7 +134,7 @@ if ( !class_exists('Puc_v5p0_Scheduler', false) ):
}
}

if ( is_a($this->updateChecker, 'Puc_v5p0_Plugin_UpdateChecker') ) {
if ( is_a($this->updateChecker, Plugin\UpdateChecker::class) ) {
if ( 'plugin' !== $upgradeInfo['type'] || !isset($upgradeInfo['plugins']) ) {
return;
}

View file

@ -1,8 +1,9 @@
<?php
namespace YahnisElsts\PluginUpdateChecker\v5p0;

if ( !class_exists('Puc_v5p0_StateStore', false) ):
if ( !class_exists(StateStore::class, false) ):

class Puc_v5p0_StateStore {
class StateStore {
/**
* @var int Last update check timestamp.
*/
@ -14,7 +15,7 @@ if ( !class_exists('Puc_v5p0_StateStore', false) ):
protected $checkedVersion = '';

/**
* @var Puc_v5p0_Update|null Cached update.
* @var Update|null Cached update.
*/
protected $update = null;

@ -65,7 +66,7 @@ if ( !class_exists('Puc_v5p0_StateStore', false) ):
}

/**
* @return null|Puc_v5p0_Update
* @return null|Update
*/
public function getUpdate() {
$this->lazyLoad();
@ -73,10 +74,10 @@ if ( !class_exists('Puc_v5p0_StateStore', false) ):
}

/**
* @param Puc_v5p0_Update|null $update
* @param Update|null $update
* @return $this
*/
public function setUpdate(Puc_v5p0_Update $update = null) {
public function setUpdate(Update $update = null) {
$this->lazyLoad();
$this->update = $update;
return $this;
@ -127,7 +128,7 @@ if ( !class_exists('Puc_v5p0_StateStore', false) ):
}

public function save() {
$state = new stdClass();
$state = new \stdClass();

$state->lastCheck = $this->lastCheck;
$state->checkedVersion = $this->checkedVersion;
@ -138,7 +139,7 @@ if ( !class_exists('Puc_v5p0_StateStore', false) ):
$updateClass = get_class($this->update);
$state->updateClass = $updateClass;
$prefix = $this->getLibPrefix();
if ( Puc_v5p0_Utils::startsWith($updateClass, $prefix) ) {
if ( Utils::startsWith($updateClass, $prefix) ) {
$state->updateBaseClass = substr($updateClass, strlen($prefix));
}
}
@ -169,8 +170,8 @@ if ( !class_exists('Puc_v5p0_StateStore', false) ):
return;
}

$this->lastCheck = intval(Puc_v5p0_Utils::get($state, 'lastCheck', 0));
$this->checkedVersion = Puc_v5p0_Utils::get($state, 'checkedVersion', '');
$this->lastCheck = intval(Utils::get($state, 'lastCheck', 0));
$this->checkedVersion = Utils::get($state, 'checkedVersion', '');
$this->update = null;

if ( isset($state->update) ) {

View file

@ -1,14 +1,18 @@
<?php
if ( !class_exists('Puc_v5p0_Theme_Package', false) ):
namespace YahnisElsts\PluginUpdateChecker\v5p0\Theme;

class Puc_v5p0_Theme_Package extends Puc_v5p0_InstalledPackage {
use YahnisElsts\PluginUpdateChecker\v5p0\InstalledPackage;

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

class Package extends InstalledPackage {
/**
* @var string Theme directory name.
*/
protected $stylesheet;

/**
* @var WP_Theme Theme object.
* @var \WP_Theme Theme object.
*/
protected $theme;


View file

@ -1,8 +1,12 @@
<?php

if ( !class_exists('Puc_v5p0_Theme_Update', false) ):
namespace YahnisElsts\PluginUpdateChecker\v5p0\Theme;

class Puc_v5p0_Theme_Update extends Puc_v5p0_Update {
use YahnisElsts\PluginUpdateChecker\v5p0\Update as BaseUpdate;

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

class Update extends BaseUpdate {
public $details_url = '';

protected static $extraFields = array('details_url');
@ -44,8 +48,8 @@ if ( !class_exists('Puc_v5p0_Theme_Update', false) ):
/**
* Create a new instance by copying the necessary fields from another object.
*
* @param StdClass|Puc_v5p0_Theme_Update $object The source object.
* @return Puc_v5p0_Theme_Update The new copy.
* @param \StdClass|self $object The source object.
* @return self The new copy.
*/
public static function fromObject($object) {
$update = new self();
@ -56,14 +60,14 @@ if ( !class_exists('Puc_v5p0_Theme_Update', false) ):
/**
* Basic validation.
*
* @param StdClass $apiResponse
* @return bool|WP_Error
* @param \StdClass $apiResponse
* @return bool|\WP_Error
*/
protected function validateMetadata($apiResponse) {
$required = array('version', 'details_url');
foreach($required as $key) {
if ( !isset($apiResponse->$key) || empty($apiResponse->$key) ) {
return new WP_Error(
return new \WP_Error(
'tuc-invalid-metadata',
sprintf('The theme metadata is missing the required "%s" key.', $key)
);

View file

@ -1,8 +1,15 @@
<?php

if ( !class_exists('Puc_v5p0_Theme_UpdateChecker', false) ):
namespace YahnisElsts\PluginUpdateChecker\v5p0\Theme;

class Puc_v5p0_Theme_UpdateChecker extends Puc_v5p0_UpdateChecker {
use YahnisElsts\PluginUpdateChecker\v5p0\UpdateChecker as BaseUpdateChecker;
use YahnisElsts\PluginUpdateChecker\v5p0\InstalledPackage;
use YahnisElsts\PluginUpdateChecker\v5p0\Scheduler;
use YahnisElsts\PluginUpdateChecker\v5p0\DebugBar;

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

class UpdateChecker extends BaseUpdateChecker {
protected $filterSuffix = 'theme';
protected $updateTransient = 'update_themes';
protected $translationType = 'theme';
@ -39,13 +46,13 @@ if ( !class_exists('Puc_v5p0_Theme_UpdateChecker', false) ):
/**
* Retrieve the latest update (if any) from the configured API endpoint.
*
* @return Puc_v5p0_Update|null An instance of Update, or NULL when no updates are available.
* @return Update|null An instance of Update, or NULL when no updates are available.
*/
public function requestUpdate() {
list($themeUpdate, $result) = $this->requestMetadata('Puc_v5p0_Theme_Update', 'request_update');
list($themeUpdate, $result) = $this->requestMetadata(Update::class, 'request_update');

if ( $themeUpdate !== null ) {
/** @var Puc_v5p0_Theme_Update $themeUpdate */
/** @var Update $themeUpdate */
$themeUpdate->slug = $this->slug;
}

@ -71,16 +78,16 @@ if ( !class_exists('Puc_v5p0_Theme_UpdateChecker', false) ):
* Create an instance of the scheduler.
*
* @param int $checkPeriod
* @return Puc_v5p0_Scheduler
* @return Scheduler
*/
protected function createScheduler($checkPeriod) {
return new Puc_v5p0_Scheduler($this, $checkPeriod, array('load-themes.php'));
return new Scheduler($this, $checkPeriod, array('load-themes.php'));
}

/**
* Is there an update being installed right now for this theme?
*
* @param WP_Upgrader|null $upgrader The upgrader that's performing the current update.
* @param \WP_Upgrader|null $upgrader The upgrader that's performing the current update.
* @return bool
*/
public function isBeingUpgraded($upgrader = null) {
@ -88,7 +95,7 @@ if ( !class_exists('Puc_v5p0_Theme_UpdateChecker', false) ):
}

protected function createDebugBarExtension() {
return new Puc_v5p0_DebugBar_Extension($this, 'Puc_v5p0_DebugBar_ThemePanel');
return new DebugBar\Extension($this, DebugBar\ThemePanel::class);
}

/**
@ -142,10 +149,10 @@ if ( !class_exists('Puc_v5p0_Theme_UpdateChecker', false) ):
/**
* Create a package instance that represents this plugin or theme.
*
* @return Puc_v5p0_InstalledPackage
* @return InstalledPackage
*/
protected function createInstalledPackage() {
return new Puc_v5p0_Theme_Package($this->stylesheet, $this);
return new Package($this->stylesheet, $this);
}
}


View file

@ -1,5 +1,9 @@
<?php
if ( !class_exists('Puc_v5p0_Update', false) ):
namespace YahnisElsts\PluginUpdateChecker\v5p0;

use stdClass;

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

/**
* A simple container class for holding information about an available update.
@ -7,7 +11,7 @@ if ( !class_exists('Puc_v5p0_Update', false) ):
* @author Janis Elsts
* @access public
*/
abstract class Puc_v5p0_Update extends Puc_v5p0_Metadata {
abstract class Update extends Metadata {
public $slug;
public $version;
public $download_url;

View file

@ -1,8 +1,12 @@
<?php
namespace YahnisElsts\PluginUpdateChecker\v5p0;

if ( !class_exists('Puc_v5p0_UpdateChecker', false) ):
use stdClass;
use WP_Error;

abstract class Puc_v5p0_UpdateChecker {
if ( !class_exists(UpdateChecker::class, false) ):

abstract class UpdateChecker {
protected $filterSuffix = '';
protected $updateTransient = '';
protected $translationType = ''; //"plugin" or "theme".
@ -36,22 +40,22 @@ if ( !class_exists('Puc_v5p0_UpdateChecker', false) ):
public $slug = '';

/**
* @var Puc_v5p0_InstalledPackage
* @var InstalledPackage
*/
protected $package;

/**
* @var Puc_v5p0_Scheduler
* @var Scheduler
*/
public $scheduler;

/**
* @var Puc_v5p0_UpgraderStatus
* @var UpgraderStatus
*/
protected $upgraderStatus;

/**
* @var Puc_v5p0_StateStore
* @var StateStore
*/
protected $updateState;

@ -66,7 +70,7 @@ if ( !class_exists('Puc_v5p0_UpdateChecker', false) ):
protected $cachedMetadataHost = 0;

/**
* @var Puc_v5p0_DebugBar_Extension|null
* @var DebugBar\Extension|null
*/
protected $debugBarExtension = null;

@ -89,8 +93,8 @@ if ( !class_exists('Puc_v5p0_UpdateChecker', false) ):

$this->package = $this->createInstalledPackage();
$this->scheduler = $this->createScheduler($checkPeriod);
$this->upgraderStatus = new Puc_v5p0_UpgraderStatus();
$this->updateState = new Puc_v5p0_StateStore($this->optionName);
$this->upgraderStatus = new UpgraderStatus();
$this->updateState = new StateStore($this->optionName);

if ( did_action('init') ) {
$this->loadTextDomain();
@ -218,12 +222,12 @@ if ( !class_exists('Puc_v5p0_UpdateChecker', false) ):
/**
* Create a package instance that represents this plugin or theme.
*
* @return Puc_v5p0_InstalledPackage
* @return InstalledPackage
*/
abstract protected function createInstalledPackage();

/**
* @return Puc_v5p0_InstalledPackage
* @return InstalledPackage
*/
public function getInstalledPackage() {
return $this->package;
@ -236,14 +240,14 @@ if ( !class_exists('Puc_v5p0_UpdateChecker', false) ):
* and substitute their own scheduler.
*
* @param int $checkPeriod
* @return Puc_v5p0_Scheduler
* @return Scheduler
*/
abstract protected function createScheduler($checkPeriod);

/**
* Check for updates. The results are stored in the DB option specified in $optionName.
*
* @return Puc_v5p0_Update|null
* @return Update|null
*/
public function checkForUpdates() {
$installedVersion = $this->getInstalledVersion();
@ -277,7 +281,7 @@ if ( !class_exists('Puc_v5p0_UpdateChecker', false) ):
/**
* Load the update checker state from the DB.
*
* @return Puc_v5p0_StateStore
* @return StateStore
*/
public function getUpdateState() {
return $this->updateState->lazyLoad();
@ -302,7 +306,7 @@ if ( !class_exists('Puc_v5p0_UpdateChecker', false) ):
* Uses cached update data. To retrieve update information straight from
* the metadata URL, call requestUpdate() instead.
*
* @return Puc_v5p0_Update|null
* @return Update|null
*/
public function getUpdate() {
$update = $this->updateState->getUpdate();
@ -323,16 +327,17 @@ if ( !class_exists('Puc_v5p0_UpdateChecker', false) ):
*
* Subclasses should run the update through filterUpdateResult before returning it.
*
* @return Puc_v5p0_Update An instance of Update, or NULL when no updates are available.
* @return Update An instance of Update, or NULL when no updates are available.
*/
abstract public function requestUpdate();

/**
* Filter the result of a requestUpdate() call.
*
* @param Puc_v5p0_Update|null $update
* @template T of Update
* @param T|null $update
* @param array|WP_Error|null $httpResult The value returned by wp_remote_get(), if any.
* @return Puc_v5p0_Update
* @return T
*/
protected function filterUpdateResult($update, $httpResult = null) {
//Let plugins/themes modify the update.
@ -355,9 +360,9 @@ if ( !class_exists('Puc_v5p0_UpdateChecker', false) ):
* "Compatibility: Unknown".
* The function mimics how wordpress.org API crafts the "tested" field out of "Tested up to".
*
* @param Puc_v5p0_Metadata|null $update
* @param Metadata|null $update
*/
protected function fixSupportedWordpressVersion(Puc_v5p0_Metadata $update = null) {
protected function fixSupportedWordpressVersion(Metadata $update = null) {
if ( !isset($update->tested) || !preg_match('/^\d++\.\d++$/', $update->tested) ) {
return;
}
@ -462,7 +467,7 @@ if ( !class_exists('Puc_v5p0_UpdateChecker', false) ):
* Store API errors that are generated when checking for updates.
*
* @internal
* @param WP_Error $error
* @param \WP_Error $error
* @param array|null $httpResponse
* @param string|null $url
* @param string|null $slug
@ -515,8 +520,8 @@ if ( !class_exists('Puc_v5p0_UpdateChecker', false) ):
/**
* Insert the latest update (if any) into the update list maintained by WP.
*
* @param stdClass $updates Update list.
* @return stdClass Modified update list.
* @param \stdClass $updates Update list.
* @return \stdClass Modified update list.
*/
public function injectUpdate($updates) {
//Is there an update to insert?
@ -543,9 +548,9 @@ if ( !class_exists('Puc_v5p0_UpdateChecker', false) ):
}

/**
* @param stdClass|null $updates
* @param stdClass|array $updateToAdd
* @return stdClass
* @param \stdClass|null $updates
* @param \stdClass|array $updateToAdd
* @return \stdClass
*/
protected function addUpdateToList($updates, $updateToAdd) {
if ( !is_object($updates) ) {
@ -558,8 +563,8 @@ if ( !class_exists('Puc_v5p0_UpdateChecker', false) ):
}

/**
* @param stdClass|null $updates
* @return stdClass|null
* @param \stdClass|null $updates
* @return \stdClass|null
*/
protected function removeUpdateFromList($updates) {
if ( isset($updates, $updates->response) ) {
@ -572,8 +577,8 @@ if ( !class_exists('Puc_v5p0_UpdateChecker', false) ):
* See this post for more information:
* @link https://make.wordpress.org/core/2020/07/30/recommended-usage-of-the-updates-api-to-support-the-auto-updates-ui-for-plugins-and-themes-in-wordpress-5-5/
*
* @param stdClass|null $updates
* @return stdClass
* @param \stdClass|null $updates
* @return \stdClass
*/
protected function addNoUpdateItem($updates) {
if ( !is_object($updates) ) {
@ -635,10 +640,10 @@ if ( !class_exists('Puc_v5p0_UpdateChecker', false) ):
/**
* Retrieve plugin or theme metadata from the JSON document at $this->metadataUrl.
*
* @param string $metaClass Parse the JSON as an instance of this class. It must have a static fromJson method.
* @param class-string<Update> $metaClass Parse the JSON as an instance of this class. It must have a static fromJson method.
* @param string $filterRoot
* @param array $queryArgs Additional query arguments.
* @return array [Puc_v5p0_Metadata|null, array|WP_Error] A metadata instance and the value returned by wp_remote_get().
* @return array<Metadata|null, array|WP_Error> A metadata instance and the value returned by wp_remote_get().
*/
protected function requestMetadata($metaClass, $filterRoot, $queryArgs = array()) {
//Query args to append to the URL. Plugins can add their own by using a filter callback (see addQueryArgFilter()).
@ -879,12 +884,12 @@ if ( !class_exists('Puc_v5p0_UpdateChecker', false) ):
*
* @param string $source The directory to copy to /wp-content/plugins or /wp-content/themes. Usually a subdirectory of $remoteSource.
* @param string $remoteSource WordPress has extracted the update to this directory.
* @param WP_Upgrader $upgrader
* @param \WP_Upgrader $upgrader
* @return string|WP_Error
*/
public function fixDirectoryName($source, $remoteSource, $upgrader) {
global $wp_filesystem;
/** @var WP_Filesystem_Base $wp_filesystem */
/** @var \WP_Filesystem_Base $wp_filesystem */

//Basic sanity checks.
if ( !isset($source, $remoteSource, $upgrader, $upgrader->skin, $wp_filesystem) ) {
@ -914,7 +919,7 @@ if ( !class_exists('Puc_v5p0_UpdateChecker', false) ):
);
}

/** @var WP_Upgrader_Skin $upgrader ->skin */
/** @var \WP_Upgrader_Skin $upgrader ->skin */
$upgrader->skin->feedback(sprintf(
'Renaming %s to %s&#8230;',
'<span class="code">' . basename($source) . '</span>',
@ -938,7 +943,7 @@ if ( !class_exists('Puc_v5p0_UpdateChecker', false) ):
/**
* Is there an update being installed right now, for this plugin or theme?
*
* @param WP_Upgrader|null $upgrader The upgrader that's performing the current update.
* @param \WP_Upgrader|null $upgrader The upgrader that's performing the current update.
* @return bool
*/
abstract public function isBeingUpgraded($upgrader = null);
@ -952,7 +957,7 @@ if ( !class_exists('Puc_v5p0_UpdateChecker', false) ):
*/
protected function isBadDirectoryStructure($remoteSource) {
global $wp_filesystem;
/** @var WP_Filesystem_Base $wp_filesystem */
/** @var \WP_Filesystem_Base $wp_filesystem */

$sourceFiles = $wp_filesystem->dirlist($remoteSource);
if ( is_array($sourceFiles) ) {
@ -980,13 +985,13 @@ if ( !class_exists('Puc_v5p0_UpdateChecker', false) ):
}

protected function createDebugBarExtension() {
return new Puc_v5p0_DebugBar_Extension($this);
return new DebugBar\Extension($this);
}

/**
* Display additional configuration details in the Debug Bar panel.
*
* @param Puc_v5p0_DebugBar_Panel $panel
* @param DebugBar\Panel $panel
*/
public function onDisplayConfiguration($panel) {
//Do nothing. Subclasses can use this to add additional info to the panel.

View file

@ -1,5 +1,7 @@
<?php
if ( !class_exists('Puc_v5p0_UpgraderStatus', false) ):
namespace YahnisElsts\PluginUpdateChecker\v5p0;

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

/**
* A utility class that helps figure out which plugin or theme WordPress is upgrading.
@ -8,7 +10,7 @@ if ( !class_exists('Puc_v5p0_UpgraderStatus', false) ):
* Core classes like Plugin_Upgrader don't expose the plugin file name during an in-progress update (AFAICT).
* This class uses a few workarounds and heuristics to get the file name.
*/
class Puc_v5p0_UpgraderStatus {
class UpgraderStatus {
private $currentType = null; //"plugin" or "theme".
private $currentId = null; //Plugin basename or theme directory name.

@ -27,7 +29,7 @@ if ( !class_exists('Puc_v5p0_UpgraderStatus', false) ):
* and upgrader implementations are liable to change without notice.
*
* @param string $pluginFile The plugin to check.
* @param WP_Upgrader|null $upgrader The upgrader that's performing the current update.
* @param \WP_Upgrader|null $upgrader The upgrader that's performing the current update.
* @return bool True if the plugin identified by $pluginFile is being upgraded.
*/
public function isPluginBeingUpgraded($pluginFile, $upgrader = null) {
@ -38,7 +40,7 @@ if ( !class_exists('Puc_v5p0_UpgraderStatus', false) ):
* Is there an update being installed for a specific theme?
*
* @param string $stylesheet Theme directory name.
* @param WP_Upgrader|null $upgrader The upgrader that's performing the current update.
* @param \WP_Upgrader|null $upgrader The upgrader that's performing the current update.
* @return bool
*/
public function isThemeBeingUpgraded($stylesheet, $upgrader = null) {
@ -50,7 +52,7 @@ if ( !class_exists('Puc_v5p0_UpgraderStatus', false) ):
*
* @param string $type
* @param string $id
* @param Plugin_Upgrader|WP_Upgrader|null $upgrader
* @param \Plugin_Upgrader|\WP_Upgrader|null $upgrader
* @return bool
*/
protected function isBeingUpgraded($type, $id, $upgrader = null) {
@ -76,7 +78,7 @@ if ( !class_exists('Puc_v5p0_UpgraderStatus', false) ):
* ['plugin', 'plugin-dir-name/plugin.php']
* ['theme', 'theme-dir-name']
*
* @param Plugin_Upgrader|WP_Upgrader $upgrader
* @param \Plugin_Upgrader|\WP_Upgrader $upgrader
* @return array
*/
private function getThingBeingUpgradedBy($upgrader) {
@ -89,13 +91,13 @@ if ( !class_exists('Puc_v5p0_UpgraderStatus', false) ):
$themeDirectoryName = null;

$skin = $upgrader->skin;
if ( isset($skin->theme_info) && ($skin->theme_info instanceof WP_Theme) ) {
if ( isset($skin->theme_info) && ($skin->theme_info instanceof \WP_Theme) ) {
$themeDirectoryName = $skin->theme_info->get_stylesheet();
} elseif ( $skin instanceof Plugin_Upgrader_Skin ) {
} elseif ( $skin instanceof \Plugin_Upgrader_Skin ) {
if ( isset($skin->plugin) && is_string($skin->plugin) && ($skin->plugin !== '') ) {
$pluginFile = $skin->plugin;
}
} elseif ( $skin instanceof Theme_Upgrader_Skin ) {
} elseif ( $skin instanceof \Theme_Upgrader_Skin ) {
if ( isset($skin->theme) && is_string($skin->theme) && ($skin->theme !== '') ) {
$themeDirectoryName = $skin->theme;
}
@ -122,7 +124,6 @@ if ( !class_exists('Puc_v5p0_UpgraderStatus', false) ):
*/
private function identifyPluginByHeaders($searchHeaders) {
if ( !function_exists('get_plugins') ){
/** @noinspection PhpIncludeInspection */
require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
}


View file

@ -1,8 +1,9 @@
<?php
namespace YahnisElsts\PluginUpdateChecker\v5p0;

if ( !class_exists('Puc_v5p0_Utils', false) ):
if ( !class_exists(Utils::class, false) ):

class Puc_v5p0_Utils {
class Utils {
/**
* Get a value from a nested array or object based on a path.
*

View file

@ -1,7 +1,12 @@
<?php
if ( !class_exists('Puc_v5p0_Vcs_Api') ):
namespace YahnisElsts\PluginUpdateChecker\v5p0\Vcs;

abstract class Puc_v5p0_Vcs_Api {
use Parsedown;
use PucReadmeParser;

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

abstract class Api {
const STRATEGY_LATEST_RELEASE = 'latest_release';
const STRATEGY_LATEST_TAG = 'latest_tag';
const STRATEGY_STABLE_TAG = 'stable_tag';
@ -38,7 +43,7 @@ if ( !class_exists('Puc_v5p0_Vcs_Api') ):
protected $localDirectory = null;

/**
* Puc_v5p0_Vcs_Api constructor.
* Api constructor.
*
* @param string $repositoryUrl
* @param array|string|null $credentials
@ -59,7 +64,7 @@ if ( !class_exists('Puc_v5p0_Vcs_Api') ):
* Figure out which reference (i.e. tag or branch) contains the latest version.
*
* @param string $configBranch Start looking in this branch.
* @return null|Puc_v5p0_Vcs_Reference
* @return null|Reference
*/
public function chooseReference($configBranch) {
$strategies = $this->getUpdateDetectionStrategies($configBranch);
@ -145,7 +150,7 @@ if ( !class_exists('Puc_v5p0_Vcs_Api') ):
* Get a branch.
*
* @param string $branchName
* @return Puc_v5p0_Vcs_Reference|null
* @return Reference|null
*/
abstract public function getBranch($branchName);

@ -153,7 +158,7 @@ if ( !class_exists('Puc_v5p0_Vcs_Api') ):
* Get a specific tag.
*
* @param string $tagName
* @return Puc_v5p0_Vcs_Reference|null
* @return Reference|null
*/
abstract public function getTag($tagName);

@ -161,7 +166,7 @@ if ( !class_exists('Puc_v5p0_Vcs_Api') ):
* Get the tag that looks like the highest version number.
* (Implementations should skip pre-release versions if possible.)
*
* @return Puc_v5p0_Vcs_Reference|null
* @return Reference|null
*/
abstract public function getLatestTag();

@ -187,7 +192,7 @@ if ( !class_exists('Puc_v5p0_Vcs_Api') ):
/**
* Check if a tag appears to be named like a version number.
*
* @param stdClass $tag
* @param \stdClass $tag
* @return bool
*/
protected function isVersionTag($tag) {
@ -199,8 +204,8 @@ if ( !class_exists('Puc_v5p0_Vcs_Api') ):
* Sort a list of tags as if they were version numbers.
* Tags that don't look like version number will be removed.
*
* @param stdClass[] $tags Array of tag objects.
* @return stdClass[] Filtered array of tags sorted in descending order.
* @param \stdClass[] $tags Array of tag objects.
* @return \stdClass[] Filtered array of tags sorted in descending order.
*/
protected function sortTagsByVersion($tags) {
//Keep only those tags that look like version numbers.
@ -214,8 +219,8 @@ if ( !class_exists('Puc_v5p0_Vcs_Api') ):
/**
* Compare two tags as if they were version number.
*
* @param stdClass $tag1 Tag object.
* @param stdClass $tag2 Another tag object.
* @param \stdClass $tag1 Tag object.
* @param \stdClass $tag2 Another tag object.
* @return int
*/
protected function compareTagNames($tag1, $tag2) {

View file

@ -1,7 +1,9 @@
<?php
if ( !interface_exists('Puc_v5p0_Vcs_BaseChecker', false) ):
namespace YahnisElsts\PluginUpdateChecker\v5p0\Vcs;

interface Puc_v5p0_Vcs_BaseChecker {
if ( !interface_exists(BaseChecker::class, false) ):

interface BaseChecker {
/**
* Set the repository branch to use for updates. Defaults to 'master'.
*
@ -19,7 +21,7 @@ if ( !interface_exists('Puc_v5p0_Vcs_BaseChecker', false) ):
public function setAuthentication($credentials);

/**
* @return Puc_v5p0_Vcs_Api
* @return Api
*/
public function getVcsApi();
}

View file

@ -1,9 +1,15 @@
<?php
if ( !class_exists('Puc_v5p0_Vcs_BitBucketApi', false) ):

class Puc_v5p0_Vcs_BitBucketApi extends Puc_v5p0_Vcs_Api {
namespace YahnisElsts\PluginUpdateChecker\v5p0\Vcs;

use YahnisElsts\PluginUpdateChecker\v5p0\OAuthSignature;
use YahnisElsts\PluginUpdateChecker\v5p0\Utils;

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

class BitBucketApi extends Api {
/**
* @var Puc_v5p0_OAuthSignature
* @var OAuthSignature
*/
private $oauth = null;

@ -23,7 +29,7 @@ if ( !class_exists('Puc_v5p0_Vcs_BitBucketApi', false) ):
$this->username = $matches['username'];
$this->repository = $matches['repository'];
} else {
throw new InvalidArgumentException('Invalid BitBucket repository URL: "' . $repositoryUrl . '"');
throw new \InvalidArgumentException('Invalid BitBucket repository URL: "' . $repositoryUrl . '"');
}

parent::__construct($repositoryUrl, $credentials);
@ -60,7 +66,7 @@ if ( !class_exists('Puc_v5p0_Vcs_BitBucketApi', false) ):
$ref = $branch->target->hash;
}

return new Puc_v5p0_Vcs_Reference(array(
return new Reference(array(
'name' => $ref,
'updated' => $branch->target->date,
'downloadUrl' => $this->getDownloadUrl($branch->name),
@ -71,7 +77,7 @@ if ( !class_exists('Puc_v5p0_Vcs_BitBucketApi', false) ):
* Get a specific tag.
*
* @param string $tagName
* @return Puc_v5p0_Vcs_Reference|null
* @return Reference|null
*/
public function getTag($tagName) {
$tag = $this->api('/refs/tags/' . $tagName);
@ -79,7 +85,7 @@ if ( !class_exists('Puc_v5p0_Vcs_BitBucketApi', false) ):
return null;
}

return new Puc_v5p0_Vcs_Reference(array(
return new Reference(array(
'name' => $tag->name,
'version' => ltrim($tag->name, 'v'),
'updated' => $tag->target->date,
@ -90,7 +96,7 @@ if ( !class_exists('Puc_v5p0_Vcs_BitBucketApi', false) ):
/**
* Get the tag that looks like the highest version number.
*
* @return Puc_v5p0_Vcs_Reference|null
* @return Reference|null
*/
public function getLatestTag() {
$tags = $this->api('/refs/tags?sort=-target.date');
@ -104,7 +110,7 @@ if ( !class_exists('Puc_v5p0_Vcs_BitBucketApi', false) ):
//Return the first result.
if ( !empty($versionTags) ) {
$tag = $versionTags[0];
return new Puc_v5p0_Vcs_Reference(array(
return new Reference(array(
'name' => $tag->name,
'version' => ltrim($tag->name, 'v'),
'updated' => $tag->target->date,
@ -118,7 +124,7 @@ if ( !class_exists('Puc_v5p0_Vcs_BitBucketApi', false) ):
* Get the tag/ref specified by the "Stable tag" header in the readme.txt of a given branch.
*
* @param string $branch
* @return null|Puc_v5p0_Vcs_Reference
* @return null|Reference
*/
protected function getStableTag($branch) {
$remoteReadme = $this->getRemoteReadme($branch);
@ -184,11 +190,11 @@ if ( !class_exists('Puc_v5p0_Vcs_BitBucketApi', false) ):
*
* @param string $url
* @param string $version
* @return mixed|WP_Error
* @return mixed|\WP_Error
*/
public function api($url, $version = '2.0') {
$url = ltrim($url, '/');
$isSrcResource = Puc_v5p0_Utils::startsWith($url, 'src/');
$isSrcResource = Utils::startsWith($url, 'src/');

$url = implode('/', array(
'https://api.bitbucket.org',
@ -227,7 +233,7 @@ if ( !class_exists('Puc_v5p0_Vcs_BitBucketApi', false) ):
return $document;
}

$error = new WP_Error(
$error = new \WP_Error(
'puc-bitbucket-http-error',
sprintf('BitBucket API error. Base URL: "%s", HTTP status code: %d.', $baseUrl, $code)
);
@ -243,7 +249,7 @@ if ( !class_exists('Puc_v5p0_Vcs_BitBucketApi', false) ):
parent::setAuthentication($credentials);

if ( !empty($credentials) && !empty($credentials['consumer_key']) ) {
$this->oauth = new Puc_v5p0_OAuthSignature(
$this->oauth = new OAuthSignature(
$credentials['consumer_key'],
$credentials['consumer_secret']
);

View file

@ -1,8 +1,11 @@
<?php
namespace YahnisElsts\PluginUpdateChecker\v5p0\Vcs;

if ( !class_exists('Puc_v5p0_Vcs_GitHubApi', false) ):
use Parsedown;

class Puc_v5p0_Vcs_GitHubApi extends Puc_v5p0_Vcs_Api {
if ( !class_exists(GitHubApi::class, false) ):

class GitHubApi extends Api {
/**
* @var string GitHub username.
*/
@ -48,7 +51,7 @@ if ( !class_exists('Puc_v5p0_Vcs_GitHubApi', false) ):
$this->userName = $matches['username'];
$this->repositoryName = $matches['repository'];
} else {
throw new InvalidArgumentException('Invalid GitHub repository URL: "' . $repositoryUrl . '"');
throw new \InvalidArgumentException('Invalid GitHub repository URL: "' . $repositoryUrl . '"');
}

parent::__construct($repositoryUrl, $accessToken);
@ -57,7 +60,7 @@ if ( !class_exists('Puc_v5p0_Vcs_GitHubApi', false) ):
/**
* Get the latest release from GitHub.
*
* @return Puc_v5p0_Vcs_Reference|null
* @return Reference|null
*/
public function getLatestRelease() {
$release = $this->api('/repos/:user/:repo/releases/latest');
@ -65,7 +68,7 @@ if ( !class_exists('Puc_v5p0_Vcs_GitHubApi', false) ):
return null;
}

$reference = new Puc_v5p0_Vcs_Reference(array(
$reference = new Reference(array(
'name' => $release->tag_name,
'version' => ltrim($release->tag_name, 'v'), //Remove the "v" prefix from "v1.2.3".
'downloadUrl' => $release->zipball_url,
@ -108,7 +111,7 @@ if ( !class_exists('Puc_v5p0_Vcs_GitHubApi', false) ):
/**
* Get the tag that looks like the highest version number.
*
* @return Puc_v5p0_Vcs_Reference|null
* @return Reference|null
*/
public function getLatestTag() {
$tags = $this->api('/repos/:user/:repo/tags');
@ -123,7 +126,7 @@ if ( !class_exists('Puc_v5p0_Vcs_GitHubApi', false) ):
}

$tag = $versionTags[0];
return new Puc_v5p0_Vcs_Reference(array(
return new Reference(array(
'name' => $tag->name,
'version' => ltrim($tag->name, 'v'),
'downloadUrl' => $tag->zipball_url,
@ -135,7 +138,7 @@ if ( !class_exists('Puc_v5p0_Vcs_GitHubApi', false) ):
* Get a branch by name.
*
* @param string $branchName
* @return null|Puc_v5p0_Vcs_Reference
* @return null|Reference
*/
public function getBranch($branchName) {
$branch = $this->api('/repos/:user/:repo/branches/' . $branchName);
@ -143,7 +146,7 @@ if ( !class_exists('Puc_v5p0_Vcs_GitHubApi', false) ):
return null;
}

$reference = new Puc_v5p0_Vcs_Reference(array(
$reference = new Reference(array(
'name' => $branch->name,
'downloadUrl' => $this->buildArchiveDownloadUrl($branch->name),
'apiResponse' => $branch,
@ -161,7 +164,7 @@ if ( !class_exists('Puc_v5p0_Vcs_GitHubApi', false) ):
*
* @param string $filename
* @param string $ref Reference name (e.g. branch or tag).
* @return StdClass|null
* @return \StdClass|null
*/
public function getLatestCommit($filename, $ref = 'master') {
$commits = $this->api(
@ -196,7 +199,7 @@ if ( !class_exists('Puc_v5p0_Vcs_GitHubApi', false) ):
*
* @param string $url
* @param array $queryParams
* @return mixed|WP_Error
* @return mixed|\WP_Error
*/
protected function api($url, $queryParams = array()) {
$baseUrl = $url;
@ -223,7 +226,7 @@ if ( !class_exists('Puc_v5p0_Vcs_GitHubApi', false) ):
return $document;
}

$error = new WP_Error(
$error = new \WP_Error(
'puc-github-http-error',
sprintf('GitHub API error. Base URL: "%s", HTTP status code: %d.', $baseUrl, $code)
);
@ -297,7 +300,7 @@ if ( !class_exists('Puc_v5p0_Vcs_GitHubApi', false) ):
*/
public function getTag($tagName) {
//The current GitHub update checker doesn't use getTag, so I didn't bother to implement it.
throw new LogicException('The ' . __METHOD__ . ' method is not implemented and should not be used.');
throw new \LogicException('The ' . __METHOD__ . ' method is not implemented and should not be used.');
}

public function setAuthentication($credentials) {
@ -350,7 +353,7 @@ if ( !class_exists('Puc_v5p0_Vcs_GitHubApi', false) ):
/**
* Does this asset match the file name regex?
*
* @param stdClass $releaseAsset
* @param \stdClass $releaseAsset
* @return bool
*/
protected function matchesAssetFilter($releaseAsset) {

View file

@ -1,8 +1,9 @@
<?php
namespace YahnisElsts\PluginUpdateChecker\v5p0\Vcs;

if ( !class_exists('Puc_v5p0_Vcs_GitLabApi', false) ):
if ( !class_exists(GitLabApi::class, false) ):

class Puc_v5p0_Vcs_GitLabApi extends Puc_v5p0_Vcs_Api {
class GitLabApi extends Api {
/**
* @var string GitLab username.
*/
@ -59,7 +60,7 @@ if ( !class_exists('Puc_v5p0_Vcs_GitLabApi', false) ):
//This is probably a repository in a subgroup, e.g. "/organization/category/repo".
$parts = explode('/', trim($path, '/'));
if ( count($parts) < 3 ) {
throw new InvalidArgumentException('Invalid GitLab.com repository URL: "' . $repositoryUrl . '"');
throw new \InvalidArgumentException('Invalid GitLab.com repository URL: "' . $repositoryUrl . '"');
}
$lastPart = array_pop($parts);
$this->userName = implode('/', $parts);
@ -76,7 +77,7 @@ if ( !class_exists('Puc_v5p0_Vcs_GitLabApi', false) ):

//We need at least /user-name/repository-name/
if ( count($segments) < 2 ) {
throw new InvalidArgumentException('Invalid GitLab repository URL: "' . $repositoryUrl . '"');
throw new \InvalidArgumentException('Invalid GitLab repository URL: "' . $repositoryUrl . '"');
}

//Get the username and repository name.
@ -101,7 +102,7 @@ if ( !class_exists('Puc_v5p0_Vcs_GitLabApi', false) ):
/**
* Get the latest release from GitLab.
*
* @return Puc_v5p0_Vcs_Reference|null
* @return Reference|null
*/
public function getLatestRelease() {
$releases = $this->api('/:id/releases');
@ -118,7 +119,7 @@ if ( !class_exists('Puc_v5p0_Vcs_GitLabApi', false) ):
return null;
}

$reference = new Puc_v5p0_Vcs_Reference(array(
$reference = new Reference(array(
'name' => $release->tag_name,
'version' => ltrim($release->tag_name, 'v'), //Remove the "v" prefix from "v1.2.3".
'downloadUrl' => '',
@ -177,7 +178,7 @@ if ( !class_exists('Puc_v5p0_Vcs_GitLabApi', false) ):
/**
* Get the tag that looks like the highest version number.
*
* @return Puc_v5p0_Vcs_Reference|null
* @return Reference|null
*/
public function getLatestTag() {
$tags = $this->api('/:id/repository/tags');
@ -191,7 +192,7 @@ if ( !class_exists('Puc_v5p0_Vcs_GitLabApi', false) ):
}

$tag = $versionTags[0];
return new Puc_v5p0_Vcs_Reference(array(
return new Reference(array(
'name' => $tag->name,
'version' => ltrim($tag->name, 'v'),
'downloadUrl' => $this->buildArchiveDownloadUrl($tag->name),
@ -203,7 +204,7 @@ if ( !class_exists('Puc_v5p0_Vcs_GitLabApi', false) ):
* Get a branch by name.
*
* @param string $branchName
* @return null|Puc_v5p0_Vcs_Reference
* @return null|Reference
*/
public function getBranch($branchName) {
$branch = $this->api('/:id/repository/branches/' . $branchName);
@ -211,7 +212,7 @@ if ( !class_exists('Puc_v5p0_Vcs_GitLabApi', false) ):
return null;
}

$reference = new Puc_v5p0_Vcs_Reference(array(
$reference = new Reference(array(
'name' => $branch->name,
'downloadUrl' => $this->buildArchiveDownloadUrl($branch->name),
'apiResponse' => $branch,
@ -244,7 +245,7 @@ if ( !class_exists('Puc_v5p0_Vcs_GitLabApi', false) ):
*
* @param string $url
* @param array $queryParams
* @return mixed|WP_Error
* @return mixed|\WP_Error
*/
protected function api($url, $queryParams = array()) {
$baseUrl = $url;
@ -267,7 +268,7 @@ if ( !class_exists('Puc_v5p0_Vcs_GitLabApi', false) ):
return json_decode($body);
}

$error = new WP_Error(
$error = new \WP_Error(
'puc-gitlab-http-error',
sprintf('GitLab API error. URL: "%s", HTTP status code: %d.', $baseUrl, $code)
);
@ -353,7 +354,7 @@ if ( !class_exists('Puc_v5p0_Vcs_GitLabApi', false) ):
* @return void
*/
public function getTag($tagName) {
throw new LogicException('The ' . __METHOD__ . ' method is not implemented and should not be used.');
throw new \LogicException('The ' . __METHOD__ . ' method is not implemented and should not be used.');
}

protected function getUpdateDetectionStrategies($configBranch) {

View file

@ -1,21 +1,26 @@
<?php
if ( !class_exists('Puc_v5p0_Vcs_PluginUpdateChecker') ):

class Puc_v5p0_Vcs_PluginUpdateChecker extends Puc_v5p0_Plugin_UpdateChecker implements Puc_v5p0_Vcs_BaseChecker {
namespace YahnisElsts\PluginUpdateChecker\v5p0\Vcs;

use YahnisElsts\PluginUpdateChecker\v5p0\Plugin;

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

class PluginUpdateChecker extends Plugin\UpdateChecker implements BaseChecker {
/**
* @var string The branch where to look for updates. Defaults to "master".
*/
protected $branch = 'master';

/**
* @var Puc_v5p0_Vcs_Api Repository API client.
* @var Api Repository API client.
*/
protected $api = null;

/**
* Puc_v5p0_Vcs_PluginUpdateChecker constructor.
* PluginUpdateChecker constructor.
*
* @param Puc_v5p0_Vcs_Api $api
* @param Api $api
* @param string $pluginFile
* @param string $slug
* @param int $checkPeriod
@ -42,7 +47,7 @@ if ( !class_exists('Puc_v5p0_Vcs_PluginUpdateChecker') ):
$api = $this->api;
$api->setLocalDirectory($this->package->getAbsoluteDirectoryPath());

$info = new Puc_v5p0_Plugin_Info();
$info = new Plugin\PluginInfo();
$info->filename = $this->pluginFile;
$info->slug = $this->slug;

@ -68,7 +73,7 @@ if ( !class_exists('Puc_v5p0_Vcs_PluginUpdateChecker') ):
//There's probably a network problem or an authentication error.
do_action(
'puc_api_error',
new WP_Error(
new \WP_Error(
'puc-no-update-source',
'Could not retrieve version information from the repository. '
. 'This usually means that the update checker either can\'t connect '
@ -127,7 +132,7 @@ if ( !class_exists('Puc_v5p0_Vcs_PluginUpdateChecker') ):
* Copy plugin metadata from a file header to a Plugin Info object.
*
* @param array $fileHeader
* @param Puc_v5p0_Plugin_Info $pluginInfo
* @param Plugin\PluginInfo $pluginInfo
*/
protected function setInfoFromHeader($fileHeader, $pluginInfo) {
$headerToPropertyMap = array(
@ -160,7 +165,7 @@ if ( !class_exists('Puc_v5p0_Vcs_PluginUpdateChecker') ):
* Copy plugin metadata from the remote readme.txt file.
*
* @param string $ref GitHub tag or branch where to look for the readme.
* @param Puc_v5p0_Plugin_Info $pluginInfo
* @param Plugin\PluginInfo $pluginInfo
*/
protected function setInfoFromRemoteReadme($ref, $pluginInfo) {
$readme = $this->api->getRemoteReadme($ref);
@ -193,7 +198,7 @@ if ( !class_exists('Puc_v5p0_Vcs_PluginUpdateChecker') ):
* and file names are described here:
* @link https://developer.wordpress.org/plugins/wordpress-org/plugin-assets/#plugin-icons
*
* @param Puc_v5p0_Plugin_Info $pluginInfo
* @param Plugin\PluginInfo $pluginInfo
*/
protected function setIconsFromLocalAssets($pluginInfo) {
$icons = $this->getLocalAssetUrls(array(
@ -222,7 +227,7 @@ if ( !class_exists('Puc_v5p0_Vcs_PluginUpdateChecker') ):
* and file names are described here:
* @link https://developer.wordpress.org/plugins/wordpress-org/plugin-assets/#plugin-headers
*
* @param Puc_v5p0_Plugin_Info $pluginInfo
* @param Plugin\PluginInfo $pluginInfo
*/
protected function setBannersFromLocalAssets($pluginInfo) {
$banners = $this->getLocalAssetUrls(array(

View file

@ -1,5 +1,7 @@
<?php
if ( !class_exists('Puc_v5p0_Vcs_Reference', false) ):
namespace YahnisElsts\PluginUpdateChecker\v5p0\Vcs;

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

/**
* This class represents a VCS branch or tag. It's intended as a read only, short-lived container
@ -13,7 +15,7 @@ if ( !class_exists('Puc_v5p0_Vcs_Reference', false) ):
* @property string|null $changelog
* @property int|null $downloadCount
*/
class Puc_v5p0_Vcs_Reference {
class Reference {
private $properties = array();

public function __construct($properties = array()) {

View file

@ -1,22 +1,27 @@
<?php

if ( !class_exists('Puc_v5p0_Vcs_ThemeUpdateChecker', false) ):
namespace YahnisElsts\PluginUpdateChecker\v5p0\Vcs;

class Puc_v5p0_Vcs_ThemeUpdateChecker extends Puc_v5p0_Theme_UpdateChecker implements Puc_v5p0_Vcs_BaseChecker {
use YahnisElsts\PluginUpdateChecker\v5p0\Theme;
use YahnisElsts\PluginUpdateChecker\v5p0\Utils;

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

class ThemeUpdateChecker extends Theme\UpdateChecker implements BaseChecker {
/**
* @var string The branch where to look for updates. Defaults to "master".
*/
protected $branch = 'master';

/**
* @var Puc_v5p0_Vcs_Api Repository API client.
* @var Api Repository API client.
*/
protected $api = null;

/**
* Puc_v5p0_Vcs_ThemeUpdateChecker constructor.
* ThemeUpdateChecker constructor.
*
* @param Puc_v5p0_Vcs_Api $api
* @param Api $api
* @param null $stylesheet
* @param null $customSlug
* @param int $checkPeriod
@ -36,7 +41,7 @@ if ( !class_exists('Puc_v5p0_Vcs_ThemeUpdateChecker', false) ):
$api = $this->api;
$api->setLocalDirectory($this->package->getAbsoluteDirectoryPath());

$update = new Puc_v5p0_Theme_Update();
$update = new Theme\Update();
$update->slug = $this->slug;

//Figure out which reference (tag or branch) we'll use to get the latest version of the theme.
@ -47,7 +52,7 @@ if ( !class_exists('Puc_v5p0_Vcs_ThemeUpdateChecker', false) ):
} else {
do_action(
'puc_api_error',
new WP_Error(
new \WP_Error(
'puc-no-update-source',
'Could not retrieve version information from the repository. '
. 'This usually means that the update checker either can\'t connect '
@ -61,13 +66,13 @@ if ( !class_exists('Puc_v5p0_Vcs_ThemeUpdateChecker', false) ):
//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.
$remoteHeader = $this->package->getFileHeader($api->getRemoteFile('style.css', $ref));
$update->version = Puc_v5p0_Utils::findNotEmpty(array(
$update->version = Utils::findNotEmpty(array(
$remoteHeader['Version'],
Puc_v5p0_Utils::get($updateSource, 'version'),
Utils::get($updateSource, 'version'),
));

//The details URL defaults to the Theme URI header or the repository URL.
$update->details_url = Puc_v5p0_Utils::findNotEmpty(array(
$update->details_url = Utils::findNotEmpty(array(
$remoteHeader['ThemeURI'],
$this->package->getHeaderValue('ThemeURI'),
$this->metadataUrl,

View file

@ -242,18 +242,18 @@ BitBucket doesn't have an equivalent to GitHub's releases, so the process is sli
```

Alternatively, if you're using a self-hosted GitLab instance, initialize the update checker like this:
```php
$myUpdateChecker = new Puc_v5p0_Vcs_PluginUpdateChecker(
new Puc_v5p0_Vcs_GitLabApi('https://myserver.com/user-name/repo-name/'),
__FILE__,
'unique-plugin-or-theme-slug'
);
//Optional: Add setAuthentication(...) and setBranch(...) as shown above.
```
```php
$myUpdateChecker = new Puc_v5p0_Vcs_PluginUpdateChecker(
new GitLabApi('https://myserver.com/user-name/repo-name/'),
__FILE__,
'unique-plugin-or-theme-slug'
);
//Optional: Add setAuthentication(...) and setBranch(...) as shown above.
```
If you're using a self-hosted GitLab instance and [subgroups or nested groups](https://docs.gitlab.com/ce/user/group/subgroups/index.html), you have to tell the update checker which parts of the URL are subgroups:
```php
$myUpdateChecker = new Puc_v5p0_Vcs_PluginUpdateChecker(
new Puc_v5p0_Vcs_GitLabApi('https://myserver.com/group-name/subgroup-level1/subgroup-level2/subgroup-level3/repo-name/', null, 'subgroup-level1/subgroup-level2/subgroup-level3'),
new GitLabApi('https://myserver.com/group-name/subgroup-level1/subgroup-level2/subgroup-level3/repo-name/', null, 'subgroup-level1/subgroup-level2/subgroup-level3'),
__FILE__,
'unique-plugin-or-theme-slug'
);

View file

@ -1,28 +0,0 @@
<?php
require dirname(__FILE__) . '/Puc/v5p0/Autoloader.php';
new Puc_v5p0_Autoloader();

require dirname(__FILE__) . '/Puc/v5p0/Factory.php';
require dirname(__FILE__) . '/Puc/v5/Factory.php';

//Register classes defined in this version with the factory.
foreach (
array(
'Plugin_UpdateChecker' => 'Puc_v5p0_Plugin_UpdateChecker',
'Theme_UpdateChecker' => 'Puc_v5p0_Theme_UpdateChecker',

'Vcs_PluginUpdateChecker' => 'Puc_v5p0_Vcs_PluginUpdateChecker',
'Vcs_ThemeUpdateChecker' => 'Puc_v5p0_Vcs_ThemeUpdateChecker',

'GitHubApi' => 'Puc_v5p0_Vcs_GitHubApi',
'BitBucketApi' => 'Puc_v5p0_Vcs_BitBucketApi',
'GitLabApi' => 'Puc_v5p0_Vcs_GitLabApi',
)
as $pucGeneralClass => $pucVersionedClass
) {
Puc_v4_Factory::addVersion($pucGeneralClass, $pucVersionedClass, '5.0');
//Also add it to the minor-version factory in case the major-version factory
//was already defined by another, older version of the update checker.
Puc_v5p0_Factory::addVersion($pucGeneralClass, $pucVersionedClass, '5.0');
}

34
load-v5p0.php Normal file
View file

@ -0,0 +1,34 @@
<?php

namespace YahnisElsts\PluginUpdateChecker\v5p0;

use YahnisElsts\PluginUpdateChecker\v5\PucFactory as MajorFactory;
use YahnisElsts\PluginUpdateChecker\v5p0\PucFactory as MinorFactory;

require __DIR__ . '/Puc/v5p0/Autoloader.php';
new Autoloader();

require __DIR__ . '/Puc/v5p0/PucFactory.php';
require __DIR__ . '/Puc/v5/PucFactory.php';

//Register classes defined in this version with the factory.
foreach (
array(
'Plugin\\UpdateChecker' => Plugin\UpdateChecker::class,
'Theme\\UpdateChecker' => Theme\UpdateChecker::class,

'Vcs\\PluginUpdateChecker' => Vcs\PluginUpdateChecker::class,
'Vcs\\ThemeUpdateChecker' => Vcs\ThemeUpdateChecker::class,

'GitHubApi' => Vcs\GitHubApi::class,
'BitBucketApi' => Vcs\BitBucketApi::class,
'GitLabApi' => Vcs\GitLabApi::class,
)
as $pucGeneralClass => $pucVersionedClass
) {
MajorFactory::addVersion($pucGeneralClass, $pucVersionedClass, '5.0');
//Also add it to the minor-version factory in case the major-version factory
//was already defined by another, older version of the update checker.
MinorFactory::addVersion($pucGeneralClass, $pucVersionedClass, '5.0');
}

View file

@ -1,9 +1,4 @@
<?php
if ( !class_exists('Parsedown', false) ) {
//Load the Parsedown version that's compatible with the current PHP version.
if ( version_compare(PHP_VERSION, '5.3.0', '>=') ) {
require __DIR__ . '/ParsedownModern.php';
} else {
require __DIR__ . '/ParsedownLegacy.php';
}
require __DIR__ . '/ParsedownModern.php';
}

File diff suppressed because it is too large Load diff