Skip to content

Commit

Permalink
⬆️ 升级版本更新依赖组件
Browse files Browse the repository at this point in the history
  • Loading branch information
Licoy committed Feb 4, 2023
1 parent bf68906 commit e99e0f5
Show file tree
Hide file tree
Showing 120 changed files with 1,306 additions and 2,254 deletions.
14 changes: 7 additions & 7 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -504,34 +504,34 @@ function pk_update()
$check_period = 6;
}
$current_theme_dir_name = basename(dirname(__FILE__));
include('update-checker/update-checker.php');
include('update-checker/plugin-update-checker.php');
switch ($update_server) {
case 'github':
{
$pkUpdateChecker = Puc_v4_Factory::buildUpdateChecker(
$pkUpdateChecker = YahnisElsts\PluginUpdateChecker\v5\PucFactory::buildUpdateChecker(
'https://github.com/Licoy/wordpress-theme-puock',
__FILE__,
'unique-plugin-or-theme-slug',
PUOCK,
$check_period
);
}
break;
case 'fastgit':
{
$pkUpdateChecker = Puc_v4_Factory::buildUpdateChecker(
$pkUpdateChecker = YahnisElsts\PluginUpdateChecker\v5\PucFactory::buildUpdateChecker(
'https://licoy.cn/go/puock-update.php?r=fastgit',
__FILE__,
'unique-plugin-or-theme-slug',
PUOCK,
$check_period
);
}
break;
default:
{
$pkUpdateChecker = Puc_v4_Factory::buildUpdateChecker(
$pkUpdateChecker = YahnisElsts\PluginUpdateChecker\v5\PucFactory::buildUpdateChecker(
'https://licoy.cn/go/puock-update.php?r=worker',
__FILE__,
$current_theme_dir_name,
PUOCK,
$check_period
);
}
Expand Down
Empty file modified update-checker/.editorconfig
100644 → 100755
Empty file.
Empty file modified update-checker/.gitignore
100644 → 100755
Empty file.
6 changes: 0 additions & 6 deletions update-checker/Puc/v4/Factory.php

This file was deleted.

63 changes: 0 additions & 63 deletions update-checker/Puc/v4p11/Autoloader.php

This file was deleted.

33 changes: 0 additions & 33 deletions update-checker/Puc/v4p11/DebugBar/PluginExtension.php

This file was deleted.

10 changes: 10 additions & 0 deletions update-checker/Puc/v5/PucFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace YahnisElsts\PluginUpdateChecker\v5;

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

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

endif;
86 changes: 86 additions & 0 deletions update-checker/Puc/v5p0/Autoloader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace YahnisElsts\PluginUpdateChecker\v5p0;

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__) . '/';

$namespaceWithSlash = __NAMESPACE__ . '\\';
$this->prefix = $namespaceWithSlash;

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

//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'));
}

/**
* 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]) ) {
include($this->libraryDir . $this->staticMap[$className]);
return;
}

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

if ( file_exists($path) ) {
include $path;
}
}
}
}

endif;
53 changes: 31 additions & 22 deletions ...-checker/Puc/v4p11/DebugBar/Extension.php → ...e-checker/Puc/v5p0/DebugBar/Extension.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
<?php
if ( !class_exists('Puc_v4p11_DebugBar_Extension', false) ):
namespace YahnisElsts\PluginUpdateChecker\v5p0\DebugBar;

class Puc_v4p11_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_v4p11_UpdateChecker */
/** @var UpdateChecker */
protected $updateChecker;
protected $panelClass = 'Puc_v4p11_DebugBar_Panel';
protected $panelClass = Panel::class;

public function __construct($updateChecker, $panelClass = null) {
$this->updateChecker = $updateChecker;
if ( isset($panelClass) ) {
$this->panelClass = $panelClass;
}

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

add_filter('debug_bar_panels', array($this, 'addDebugBarPanel'));
add_action('debug_bar_enqueue_scripts', array($this, 'enqueuePanelDependencies'));

add_action('wp_ajax_puc_v4_debug_check_now', array($this, 'ajaxCheckNow'));
add_action('wp_ajax_puc_v5_debug_check_now', array($this, 'ajaxCheckNow'));
}

/**
Expand All @@ -42,17 +47,17 @@ public function addDebugBarPanel($panels) {
*/
public function enqueuePanelDependencies() {
wp_enqueue_style(
'puc-debug-bar-style-v4',
'puc-debug-bar-style-v5',
$this->getLibraryUrl("/css/puc-debug-bar.css"),
array('debug-bar'),
'20171124'
'20221008'
);

wp_enqueue_script(
'puc-debug-bar-js-v4',
'puc-debug-bar-js-v5',
$this->getLibraryUrl("/js/debug-bar.js"),
array('jquery'),
'20201209'
'20221008'
);
}

Expand All @@ -61,14 +66,16 @@ public function enqueuePanelDependencies() {
* the update checking process works as expected.
*/
public function ajaxCheckNow() {
if ( $_POST['uid'] !== $this->updateChecker->getUniqueName('uid') ) {
//phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce is checked in preAjaxRequest().
if ( !isset($_POST['uid']) || ($_POST['uid'] !== $this->updateChecker->getUniqueName('uid')) ) {
return;
}
$this->preAjaxRequest();
$update = $this->updateChecker->checkForUpdates();
if ( $update !== null ) {
echo "An update is available:";
echo '<pre>', htmlentities(print_r($update, true)), '</pre>';
//phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r -- For debugging output.
echo '<pre>', esc_html(print_r($update, true)), '</pre>';
} else {
echo 'No updates found.';
}
Expand All @@ -79,8 +86,8 @@ public function ajaxCheckNow() {

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

echo '<dl>';
printf('<dt>Error code:</dt><dd><code>%s</code></dd>', esc_html($wpError->get_error_code()));
Expand All @@ -92,7 +99,7 @@ public function ajaxCheckNow() {
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()),
Expand All @@ -102,8 +109,8 @@ public function ajaxCheckNow() {
//Status code.
printf(
'<dt>HTTP status:</dt><dd><code>%d %s</code></dd>',
wp_remote_retrieve_response_code($item['httpResponse']),
wp_remote_retrieve_response_message($item['httpResponse'])
esc_html(wp_remote_retrieve_response_code($item['httpResponse'])),
esc_html(wp_remote_retrieve_response_message($item['httpResponse']))
);

//Headers.
Expand Down Expand Up @@ -142,7 +149,9 @@ protected function preAjaxRequest() {
}
check_ajax_referer('puc-ajax');

//phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.runtime_configuration_error_reporting -- Part of a debugging feature.
error_reporting(E_ALL);
//phpcs:ignore WordPress.PHP.IniSet.display_errors_Blacklisted
@ini_set('display_errors', 'On');
}

Expand All @@ -152,7 +161,7 @@ protected function preAjaxRequest() {
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'));
remove_action('wp_ajax_puc_v5_debug_check_now', array($this, 'ajaxCheckNow'));
}

/**
Expand All @@ -163,11 +172,11 @@ private function getLibraryUrl($filePath) {
$absolutePath = realpath(dirname(__FILE__) . '/../../../' . ltrim($filePath, '/'));

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

$pluginDir = Puc_v4p11_Factory::normalizePath(WP_PLUGIN_DIR);
$muPluginDir = Puc_v4p11_Factory::normalizePath(WPMU_PLUGIN_DIR);
$themeDir = Puc_v4p11_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.
Expand Down
Loading

0 comments on commit e99e0f5

Please sign in to comment.