Skip to content

Commit e9d9533

Browse files
committed
add plugin uptdate checker
1 parent 195ae04 commit e9d9533

File tree

73 files changed

+10433
-2
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+10433
-2
lines changed

simple-metadata-lifecycle.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/*
44
Plugin Name: Simple Metadata Life Cycle
55
Plugin URI: https://github.com/my-language-skills/simple-metadata-lyfecycle
6-
Description: Simple Metadata add-on for life-cycle inforamtion of web-site content.
6+
Description: Simple Metadata add-on for life-cycle inforamtion of web-site content.
77
Version: 1.0
88
Author: My Language Skills team
99
Author URI: https://github.com/my-language-skills
@@ -45,4 +45,16 @@
4545
<?php
4646
});
4747
}
48-
}
48+
}
49+
50+
/*
51+
* Auto update from github
52+
*
53+
* @since 1.0
54+
*/
55+
require 'vendor/plugin-update-checker/plugin-update-checker.php';
56+
$myUpdateChecker = Puc_v4_Factory::buildUpdateChecker(
57+
'https://github.com/my-language-skills/simple-metadata-lifecycle/',
58+
__FILE__,
59+
'simple-metadata-lifecycle'
60+
);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[*]
2+
charset=utf-8
3+
end_of_line=lf
4+
insert_final_newline=false
5+
indent_style=tab
6+
tab_width=4
7+
8+
[{phpunit.xml.dist,*.jhm,*.xslt,*.xul,*.rng,*.xsl,*.xsd,*.ant,*.tld,*.fxml,*.jrxml,*.xml,*.jnlp,*.wsdl}]
9+
indent_style=space
10+
indent_size=4
11+
12+
[*.svg]
13+
indent_style=space
14+
indent_size=4
15+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
if ( !class_exists('Puc_v4_Factory', false) ):
3+
4+
class Puc_v4_Factory extends Puc_v4p6_Factory { }
5+
6+
endif;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
if ( !class_exists('Puc_v4p6_Autoloader', false) ):
4+
5+
class Puc_v4p6_Autoloader {
6+
private $prefix = '';
7+
private $rootDir = '';
8+
private $libraryDir = '';
9+
10+
private $staticMap;
11+
12+
public function __construct() {
13+
$this->rootDir = dirname(__FILE__) . '/';
14+
$nameParts = explode('_', __CLASS__, 3);
15+
$this->prefix = $nameParts[0] . '_' . $nameParts[1] . '_';
16+
17+
$this->libraryDir = realpath($this->rootDir . '../..') . '/';
18+
$this->staticMap = array(
19+
'PucReadmeParser' => 'vendor/readme-parser.php',
20+
'Parsedown' => 'vendor/ParsedownLegacy.php',
21+
);
22+
if ( version_compare(PHP_VERSION, '5.3.0', '>=') ) {
23+
$this->staticMap['Parsedown'] = 'vendor/Parsedown.php';
24+
}
25+
26+
spl_autoload_register(array($this, 'autoload'));
27+
}
28+
29+
public function autoload($className) {
30+
if ( isset($this->staticMap[$className]) && file_exists($this->libraryDir . $this->staticMap[$className]) ) {
31+
/** @noinspection PhpIncludeInspection */
32+
include ($this->libraryDir . $this->staticMap[$className]);
33+
return;
34+
}
35+
36+
if (strpos($className, $this->prefix) === 0) {
37+
$path = substr($className, strlen($this->prefix));
38+
$path = str_replace('_', '/', $path);
39+
$path = $this->rootDir . $path . '.php';
40+
41+
if (file_exists($path)) {
42+
/** @noinspection PhpIncludeInspection */
43+
include $path;
44+
}
45+
}
46+
}
47+
}
48+
49+
endif;
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
<?php
2+
if ( !class_exists('Puc_v4p6_DebugBar_Extension', false) ):
3+
4+
class Puc_v4p6_DebugBar_Extension {
5+
const RESPONSE_BODY_LENGTH_LIMIT = 4000;
6+
7+
/** @var Puc_v4p6_UpdateChecker */
8+
protected $updateChecker;
9+
protected $panelClass = 'Puc_v4p6_DebugBar_Panel';
10+
11+
public function __construct($updateChecker, $panelClass = null) {
12+
$this->updateChecker = $updateChecker;
13+
if ( isset($panelClass) ) {
14+
$this->panelClass = $panelClass;
15+
}
16+
17+
add_filter('debug_bar_panels', array($this, 'addDebugBarPanel'));
18+
add_action('debug_bar_enqueue_scripts', array($this, 'enqueuePanelDependencies'));
19+
20+
add_action('wp_ajax_puc_v4_debug_check_now', array($this, 'ajaxCheckNow'));
21+
}
22+
23+
/**
24+
* Register the PUC Debug Bar panel.
25+
*
26+
* @param array $panels
27+
* @return array
28+
*/
29+
public function addDebugBarPanel($panels) {
30+
if ( $this->updateChecker->userCanInstallUpdates() ) {
31+
$panels[] = new $this->panelClass($this->updateChecker);
32+
}
33+
return $panels;
34+
}
35+
36+
/**
37+
* Enqueue our Debug Bar scripts and styles.
38+
*/
39+
public function enqueuePanelDependencies() {
40+
wp_enqueue_style(
41+
'puc-debug-bar-style-v4',
42+
$this->getLibraryUrl("/css/puc-debug-bar.css"),
43+
array('debug-bar'),
44+
'20171124'
45+
);
46+
47+
wp_enqueue_script(
48+
'puc-debug-bar-js-v4',
49+
$this->getLibraryUrl("/js/debug-bar.js"),
50+
array('jquery'),
51+
'20170516'
52+
);
53+
}
54+
55+
/**
56+
* Run an update check and output the result. Useful for making sure that
57+
* the update checking process works as expected.
58+
*/
59+
public function ajaxCheckNow() {
60+
if ( $_POST['uid'] !== $this->updateChecker->getUniqueName('uid') ) {
61+
return;
62+
}
63+
$this->preAjaxRequest();
64+
$update = $this->updateChecker->checkForUpdates();
65+
if ( $update !== null ) {
66+
echo "An update is available:";
67+
echo '<pre>', htmlentities(print_r($update, true)), '</pre>';
68+
} else {
69+
echo 'No updates found.';
70+
}
71+
72+
$errors = $this->updateChecker->getLastRequestApiErrors();
73+
if ( !empty($errors) ) {
74+
printf('<p>The update checker encountered %d API error%s.</p>', count($errors), (count($errors) > 1) ? 's' : '');
75+
76+
foreach (array_values($errors) as $num => $item) {
77+
$wpError = $item['error'];
78+
/** @var WP_Error $wpError */
79+
printf('<h4>%d) %s</h4>', $num + 1, esc_html($wpError->get_error_message()));
80+
81+
echo '<dl>';
82+
printf('<dt>Error code:</dt><dd><code>%s</code></dd>', esc_html($wpError->get_error_code()));
83+
84+
if ( isset($item['url']) ) {
85+
printf('<dt>Requested URL:</dt><dd><code>%s</code></dd>', esc_html($item['url']));
86+
}
87+
88+
if ( isset($item['httpResponse']) ) {
89+
if ( is_wp_error($item['httpResponse']) ) {
90+
$httpError = $item['httpResponse'];
91+
/** @var WP_Error $httpError */
92+
printf(
93+
'<dt>WordPress HTTP API error:</dt><dd>%s (<code>%s</code>)</dd>',
94+
esc_html($httpError->get_error_message()),
95+
esc_html($httpError->get_error_code())
96+
);
97+
} else {
98+
//Status code.
99+
printf(
100+
'<dt>HTTP status:</dt><dd><code>%d %s</code></dd>',
101+
wp_remote_retrieve_response_code($item['httpResponse']),
102+
wp_remote_retrieve_response_message($item['httpResponse'])
103+
);
104+
105+
//Headers.
106+
echo '<dt>Response headers:</dt><dd><pre>';
107+
foreach (wp_remote_retrieve_headers($item['httpResponse']) as $name => $value) {
108+
printf("%s: %s\n", esc_html($name), esc_html($value));
109+
}
110+
echo '</pre></dd>';
111+
112+
//Body.
113+
$body = wp_remote_retrieve_body($item['httpResponse']);
114+
if ( $body === '' ) {
115+
$body = '(Empty response.)';
116+
} else if ( strlen($body) > self::RESPONSE_BODY_LENGTH_LIMIT ) {
117+
$length = strlen($body);
118+
$body = substr($body, 0, self::RESPONSE_BODY_LENGTH_LIMIT)
119+
. sprintf("\n(Long string truncated. Total length: %d bytes.)", $length);
120+
}
121+
122+
printf('<dt>Response body:</dt><dd><pre>%s</pre></dd>', esc_html($body));
123+
}
124+
}
125+
echo '<dl>';
126+
}
127+
}
128+
129+
exit;
130+
}
131+
132+
/**
133+
* Check access permissions and enable error display (for debugging).
134+
*/
135+
protected function preAjaxRequest() {
136+
if ( !$this->updateChecker->userCanInstallUpdates() ) {
137+
die('Access denied');
138+
}
139+
check_ajax_referer('puc-ajax');
140+
141+
error_reporting(E_ALL);
142+
@ini_set('display_errors', 'On');
143+
}
144+
145+
/**
146+
* @param string $filePath
147+
* @return string
148+
*/
149+
private function getLibraryUrl($filePath) {
150+
$absolutePath = realpath(dirname(__FILE__) . '/../../../' . ltrim($filePath, '/'));
151+
152+
//Where is the library located inside the WordPress directory structure?
153+
$absolutePath = Puc_v4p6_Factory::normalizePath($absolutePath);
154+
155+
$pluginDir = Puc_v4p6_Factory::normalizePath(WP_PLUGIN_DIR);
156+
$muPluginDir = Puc_v4p6_Factory::normalizePath(WPMU_PLUGIN_DIR);
157+
$themeDir = Puc_v4p6_Factory::normalizePath(get_theme_root());
158+
159+
if ( (strpos($absolutePath, $pluginDir) === 0) || (strpos($absolutePath, $muPluginDir) === 0) ) {
160+
//It's part of a plugin.
161+
return plugins_url(basename($absolutePath), $absolutePath);
162+
} else if ( strpos($absolutePath, $themeDir) === 0 ) {
163+
//It's part of a theme.
164+
$relativePath = substr($absolutePath, strlen($themeDir) + 1);
165+
$template = substr($relativePath, 0, strpos($relativePath, '/'));
166+
$baseUrl = get_theme_root_uri($template);
167+
168+
if ( !empty($baseUrl) && $relativePath ) {
169+
return $baseUrl . '/' . $relativePath;
170+
}
171+
}
172+
173+
return '';
174+
}
175+
}
176+
177+
endif;

0 commit comments

Comments
 (0)