Skip to content

Commit

Permalink
feat: update theme checker
Browse files Browse the repository at this point in the history
  • Loading branch information
seatonjiang committed Oct 4, 2020
1 parent 4a71ab3 commit 50a0e2a
Show file tree
Hide file tree
Showing 43 changed files with 593 additions and 245 deletions.
2 changes: 1 addition & 1 deletion inc/update-checker/Puc/v4/Factory.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
if ( !class_exists('Puc_v4_Factory', false) ):

class Puc_v4_Factory extends Puc_v4p9_Factory { }
class Puc_v4_Factory extends Puc_v4p10_Factory { }

endif;
22 changes: 19 additions & 3 deletions inc/update-checker/Puc/v4p9/Autoloader.php → inc/update-checker/Puc/v4p10/Autoloader.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

if ( !class_exists('Puc_v4p9_Autoloader', false) ):
if ( !class_exists('Puc_v4p10_Autoloader', false) ):

class Puc_v4p9_Autoloader {
class Puc_v4p10_Autoloader {
private $prefix = '';
private $rootDir = '';
private $libraryDir = '';
Expand All @@ -14,7 +14,12 @@ public function __construct() {
$nameParts = explode('_', __CLASS__, 3);
$this->prefix = $nameParts[0] . '_' . $nameParts[1] . '_';

$this->libraryDir = realpath($this->rootDir . '../..') . '/';
$this->libraryDir = $this->rootDir . '../..';
if ( !self::isPhar() ) {
$this->libraryDir = realpath($this->libraryDir);
}
$this->libraryDir = $this->libraryDir . '/';

$this->staticMap = array(
'PucReadmeParser' => 'vendor/PucReadmeParser.php',
'Parsedown' => 'vendor/Parsedown.php',
Expand All @@ -24,6 +29,17 @@ public function __construct() {
spl_autoload_register(array($this, 'autoload'));
}

/**
* Determine if this file is running as part of a Phar archive.
*
* @return bool
*/
private static function isPhar() {
//Check if the current file path starts with "phar://".
static $pharProtocol = 'phar://';
return (substr(__FILE__, 0, strlen($pharProtocol)) === $pharProtocol);
}

public function autoload($className) {
if ( isset($this->staticMap[$className]) && file_exists($this->libraryDir . $this->staticMap[$className]) ) {
/** @noinspection PhpIncludeInspection */
Expand Down
25 changes: 17 additions & 8 deletions ...e-checker/Puc/v4p9/DebugBar/Extension.php → ...-checker/Puc/v4p10/DebugBar/Extension.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php
if ( !class_exists('Puc_v4p9_DebugBar_Extension', false) ):
if ( !class_exists('Puc_v4p10_DebugBar_Extension', false) ):

class Puc_v4p9_DebugBar_Extension {
class Puc_v4p10_DebugBar_Extension {
const RESPONSE_BODY_LENGTH_LIMIT = 4000;

/** @var Puc_v4p9_UpdateChecker */
/** @var Puc_v4p10_UpdateChecker */
protected $updateChecker;
protected $panelClass = 'Puc_v4p9_DebugBar_Panel';
protected $panelClass = 'Puc_v4p10_DebugBar_Panel';

public function __construct($updateChecker, $panelClass = null) {
$this->updateChecker = $updateChecker;
Expand Down Expand Up @@ -142,6 +142,15 @@ protected function preAjaxRequest() {
@ini_set('display_errors', 'On');
}

/**
* Remove hooks that were added by this extension.
*/
public function removeHooks() {
remove_filter('debug_bar_panels', array($this, 'addDebugBarPanel'));
remove_action('debug_bar_enqueue_scripts', array($this, 'enqueuePanelDependencies'));
remove_action('wp_ajax_puc_v4_debug_check_now', array($this, 'ajaxCheckNow'));
}

/**
* @param string $filePath
* @return string
Expand All @@ -150,11 +159,11 @@ private function getLibraryUrl($filePath) {
$absolutePath = realpath(dirname(__FILE__) . '/../../../' . ltrim($filePath, '/'));

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

$pluginDir = Puc_v4p9_Factory::normalizePath(WP_PLUGIN_DIR);
$muPluginDir = Puc_v4p9_Factory::normalizePath(WPMU_PLUGIN_DIR);
$themeDir = Puc_v4p9_Factory::normalizePath(get_theme_root());
$pluginDir = Puc_v4p10_Factory::normalizePath(WP_PLUGIN_DIR);
$muPluginDir = Puc_v4p10_Factory::normalizePath(WPMU_PLUGIN_DIR);
$themeDir = Puc_v4p10_Factory::normalizePath(get_theme_root());

if ( (strpos($absolutePath, $pluginDir) === 0) || (strpos($absolutePath, $muPluginDir) === 0) ) {
//It's part of a plugin.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

if ( !class_exists('Puc_v4p9_DebugBar_Panel', false) && class_exists('Debug_Bar_Panel', false) ):
if ( !class_exists('Puc_v4p10_DebugBar_Panel', false) && class_exists('Debug_Bar_Panel', false) ):

class Puc_v4p9_DebugBar_Panel extends Debug_Bar_Panel {
/** @var Puc_v4p9_UpdateChecker */
class Puc_v4p10_DebugBar_Panel extends Debug_Bar_Panel {
/** @var Puc_v4p10_UpdateChecker */
protected $updateChecker;

private $responseBox = '<div class="puc-ajax-response" style="display: none;"></div>';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php
if ( !class_exists('Puc_v4p9_DebugBar_PluginExtension', false) ):
if ( !class_exists('Puc_v4p10_DebugBar_PluginExtension', false) ):

class Puc_v4p9_DebugBar_PluginExtension extends Puc_v4p9_DebugBar_Extension {
/** @var Puc_v4p9_Plugin_UpdateChecker */
class Puc_v4p10_DebugBar_PluginExtension extends Puc_v4p10_DebugBar_Extension {
/** @var Puc_v4p10_Plugin_UpdateChecker */
protected $updateChecker;

public function __construct($updateChecker) {
parent::__construct($updateChecker, 'Puc_v4p9_DebugBar_PluginPanel');
parent::__construct($updateChecker, 'Puc_v4p10_DebugBar_PluginPanel');

add_action('wp_ajax_puc_v4_debug_request_info', array($this, 'ajaxRequestInfo'));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

if ( !class_exists('Puc_v4p9_DebugBar_PluginPanel', false) ):
if ( !class_exists('Puc_v4p10_DebugBar_PluginPanel', false) ):

class Puc_v4p9_DebugBar_PluginPanel extends Puc_v4p9_DebugBar_Panel {
class Puc_v4p10_DebugBar_PluginPanel extends Puc_v4p10_DebugBar_Panel {
/**
* @var Puc_v4p9_Plugin_UpdateChecker
* @var Puc_v4p10_Plugin_UpdateChecker
*/
protected $updateChecker;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

if ( !class_exists('Puc_v4p9_DebugBar_ThemePanel', false) ):
if ( !class_exists('Puc_v4p10_DebugBar_ThemePanel', false) ):

class Puc_v4p9_DebugBar_ThemePanel extends Puc_v4p9_DebugBar_Panel {
class Puc_v4p10_DebugBar_ThemePanel extends Puc_v4p10_DebugBar_Panel {
/**
* @var Puc_v4p9_Theme_UpdateChecker
* @var Puc_v4p10_Theme_UpdateChecker
*/
protected $updateChecker;

Expand Down
69 changes: 64 additions & 5 deletions inc/update-checker/Puc/v4p9/Factory.php → inc/update-checker/Puc/v4p10/Factory.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
if ( !class_exists('Puc_v4p9_Factory', false) ):
if ( !class_exists('Puc_v4p10_Factory', false) ):

/**
* A factory that builds update checker instances.
Expand All @@ -11,28 +11,58 @@
* 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_v4p9_Factory {
class Puc_v4p10_Factory {
protected static $classVersions = array();
protected static $sorted = false;

protected static $myMajorVersion = '';
protected static $latestCompatibleVersion = '';

/**
* A wrapper method for buildUpdateChecker() that reads the metadata URL from the plugin or theme header.
*
* @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_v4p10_Plugin_UpdateChecker|Puc_v4p10_Theme_UpdateChecker|Puc_v4p10_Vcs_BaseChecker
*/
public static function buildFromHeader($fullPath, $args = array()) {
$fullPath = self::normalizePath($fullPath);

//Set up defaults.
$defaults = array(
'metadataUrl' => '',
'slug' => '',
'checkPeriod' => 12,
'optionName' => '',
'muPluginFile' => '',
);
$args = array_merge($defaults, array_intersect_key($args, $defaults));
extract($args, EXTR_SKIP);

//Check for the service URI
if ( empty($metadataUrl) ) {
$metadataUrl = self::getServiceURI($fullPath);
}

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

/**
* Create a new instance of the update checker.
*
* 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_v4p9_UpdateChecker::__construct
* @see Puc_v4p10_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 $muPluginFile The plugin filename relative to the mu-plugins directory.
* @return Puc_v4p9_Plugin_UpdateChecker|Puc_v4p9_Theme_UpdateChecker|Puc_v4p9_Vcs_BaseChecker
* @return Puc_v4p10_Plugin_UpdateChecker|Puc_v4p10_Theme_UpdateChecker|Puc_v4p10_Vcs_BaseChecker
*/
public static function buildUpdateChecker($metadataUrl, $fullPath, $slug = '', $checkPeriod = 12, $optionName = '', $muPluginFile = '') {
$fullPath = self::normalizePath($fullPath);
Expand Down Expand Up @@ -178,6 +208,35 @@ protected static function getThemeDirectoryName($absolutePath) {
return null;
}

/**
* Get the service URI from the file header.
*
* @param string $fullPath
* @return string
*/
private static function getServiceURI($fullPath) {
//Look for the URI
if ( is_readable($fullPath) ) {
$seek = array(
'github' => 'GitHub URI',
'gitlab' => 'GitLab URI',
'bucket' => 'BitBucket URI',
);
$seek = apply_filters('puc_get_source_uri', $seek);
$data = get_file_data($fullPath, $seek);
foreach ($data as $key => $uri) {
if ( $uri ) {
return $uri;
}
}
}

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

/**
* Get the name of the hosting service that the URL points to.
*
Expand Down Expand Up @@ -294,4 +353,4 @@ public static function addVersion($generalClass, $versionedClass, $version) {
}
}

endif;
endif;
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?php
if ( !class_exists('Puc_v4p9_InstalledPackage', false) ):
if ( !class_exists('Puc_v4p10_InstalledPackage', false) ):

/**
* This class represents a currently installed plugin or theme.
*
* 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_v4p9_InstalledPackage {
abstract class Puc_v4p10_InstalledPackage {
/**
* @var Puc_v4p9_UpdateChecker
* @var Puc_v4p10_UpdateChecker
*/
protected $updateChecker;

Expand Down
4 changes: 2 additions & 2 deletions inc/update-checker/Puc/v4p9/Metadata.php → inc/update-checker/Puc/v4p10/Metadata.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
if ( !class_exists('Puc_v4p9_Metadata', false) ):
if ( !class_exists('Puc_v4p10_Metadata', false) ):

/**
* A base container for holding information about updates and plugin metadata.
Expand All @@ -8,7 +8,7 @@
* @copyright 2016
* @access public
*/
abstract class Puc_v4p9_Metadata {
abstract class Puc_v4p10_Metadata {

/**
* Create an instance of this class from a JSON document.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

if ( !class_exists('Puc_v4p9_OAuthSignature', false) ):
if ( !class_exists('Puc_v4p10_OAuthSignature', false) ):

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
if ( !class_exists('Puc_v4p9_Plugin_Info', false) ):
if ( !class_exists('Puc_v4p10_Plugin_Info', false) ):

/**
* A container class for holding and transforming various plugin metadata.
Expand All @@ -8,7 +8,7 @@
* @copyright 2016
* @access public
*/
class Puc_v4p9_Plugin_Info extends Puc_v4p9_Metadata {
class Puc_v4p10_Plugin_Info extends Puc_v4p10_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;
Expand All @@ -27,6 +27,7 @@ class Puc_v4p9_Plugin_Info extends Puc_v4p9_Metadata {

public $requires;
public $tested;
public $requires_php;
public $upgrade_notice;

public $rating;
Expand Down Expand Up @@ -94,6 +95,7 @@ public function toWpFormat(){
$sameFormat = array(
'name', 'slug', 'version', 'requires', 'tested', 'rating', 'upgrade_notice',
'num_ratings', 'downloaded', 'active_installs', 'homepage', 'last_updated',
'requires_php',
);
foreach($sameFormat as $field){
if ( isset($this->$field) ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
if ( !class_exists('Puc_v4p9_Plugin_Package', false) ):
if ( !class_exists('Puc_v4p10_Plugin_Package', false) ):

class Puc_v4p9_Plugin_Package extends Puc_v4p9_InstalledPackage {
class Puc_v4p10_Plugin_Package extends Puc_v4p10_InstalledPackage {
/**
* @var Puc_v4p9_Plugin_UpdateChecker
* @var Puc_v4p10_Plugin_UpdateChecker
*/
protected $updateChecker;

Expand Down Expand Up @@ -170,8 +170,8 @@ public function isMuPlugin() {
$pluginPath = realpath($this->pluginAbsolutePath);
//If realpath() fails, just normalize the syntax instead.
if (($muPluginDir === false) || ($pluginPath === false)) {
$muPluginDir = Puc_v4p9_Factory::normalizePath(WPMU_PLUGIN_DIR);
$pluginPath = Puc_v4p9_Factory::normalizePath($this->pluginAbsolutePath);
$muPluginDir = Puc_v4p10_Factory::normalizePath(WPMU_PLUGIN_DIR);
$pluginPath = Puc_v4p10_Factory::normalizePath($this->pluginAbsolutePath);
}

$cachedResult = (strpos($pluginPath, $muPluginDir) === 0);
Expand Down
Loading

0 comments on commit 50a0e2a

Please sign in to comment.