Skip to content

Commit 60a540b

Browse files
committed
Merge branch 'develop'
2 parents 426cfbc + f5864b8 commit 60a540b

10 files changed

+140
-17
lines changed

CHANGES.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
#### [unreleased]
22

3+
#### 8.7.0 / 2019-02-24
4+
* update `Readme_Parser` for changelog and description parsing
5+
* add filter `github_updater_temp_readme_filepath` to change default location if server has permissions issues, fixes [#766](https://github.com/afragen/github-updater/issues/766)
6+
* fix `Readme_Parser` to use `version_compare()` when checking compatibility with `create_contributors()`
7+
* add commit hash and timestamp to branch data, timestamp not returned by this particular GitHub API call 😞
8+
* add filter `github_updater_remote_is_newer` to use your own version comparison function
9+
310
#### 8.6.3 / 2019-02-04
411
* use Update PHP messaging as in WP 5.1 in version check
512

github-updater.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* Plugin Name: GitHub Updater
1313
* Plugin URI: https://github.com/afragen/github-updater
1414
* Description: A plugin to automatically update GitHub, Bitbucket, GitLab, or Gitea hosted plugins, themes, and language packs. It also allows for remote installation of plugins or themes into WordPress.
15-
* Version: 8.6.3
15+
* Version: 8.7.0
1616
* Author: Andy Fragen
1717
* License: GNU General Public License v2
1818
* License URI: http://www.gnu.org/licenses/gpl-2.0.html

src/GitHub_Updater/API/API_Interface.php

+11
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,17 @@ public function parse_meta_response( $response);
135135
*/
136136
public function parse_changelog_response( $response);
137137

138+
/**
139+
* Parse API response and return array of branch data.
140+
*
141+
* @access public
142+
*
143+
* @param \stdClass $response API response.
144+
*
145+
* @return array Array of branch data.
146+
*/
147+
public function parse_branch_response( $response );
148+
138149
/**
139150
* Add values for individual repo add_setting_field().
140151
*

src/GitHub_Updater/API/Bitbucket_API.php

+20
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,26 @@ function ( $e ) use ( &$arr ) {
311311
public function parse_changelog_response( $response ) {
312312
}
313313

314+
/**
315+
* Parse API response and return array of branch data.
316+
*
317+
* @param \stdClass $response API response.
318+
*
319+
* @return array Array of branch data.
320+
*/
321+
public function parse_branch_response( $response ) {
322+
if ( $this->validate_response( $response ) ) {
323+
return $response;
324+
}
325+
$branches = [];
326+
foreach ( $response as $branch ) {
327+
$branches[ $branch->name ]['download'] = $this->construct_download_link( $branch->name );
328+
$branches[ $branch->name ]['commit_hash'] = $branch->target->hash;
329+
$branches[ $branch->name ]['commit_timestamp'] = $branch->target->date;
330+
}
331+
return $branches;
332+
}
333+
314334
/**
315335
* Parse tags and create download links.
316336
*

src/GitHub_Updater/API/GitHub_API.php

+19
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,25 @@ function ( $e ) use ( &$arr ) {
297297
return $arr;
298298
}
299299

300+
/**
301+
* Parse API response and return array of branch data.
302+
*
303+
* @param \stdClass $response API response.
304+
*
305+
* @return array Array of branch data.
306+
*/
307+
public function parse_branch_response( $response ) {
308+
if ( $this->validate_response( $response ) ) {
309+
return $response;
310+
}
311+
$branches = [];
312+
foreach ( $response as $branch ) {
313+
$branches[ $branch->name ]['download'] = $this->construct_download_link( $branch->name );
314+
$branches[ $branch->name ]['commit_hash'] = $branch->commit->sha;
315+
}
316+
return $branches;
317+
}
318+
300319
/**
301320
* Parse tags and create download links.
302321
*

src/GitHub_Updater/API/GitLab_API.php

+20
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,26 @@ function ( $e ) use ( &$arr ) {
367367
return $arr;
368368
}
369369

370+
/**
371+
* Parse API response and return array of branch data.
372+
*
373+
* @param \stdClass $response API response.
374+
*
375+
* @return array Array of branch data.
376+
*/
377+
public function parse_branch_response( $response ) {
378+
if ( $this->validate_response( $response ) ) {
379+
return $response;
380+
}
381+
$branches = [];
382+
foreach ( $response as $branch ) {
383+
$branches[ $branch->name ]['download'] = $this->construct_download_link( $branch->name );
384+
$branches[ $branch->name ]['commit_hash'] = $branch->commit->id;
385+
$branches[ $branch->name ]['commit_timestamp'] = $branch->commit->committed_date;
386+
}
387+
return $branches;
388+
}
389+
370390
/**
371391
* Parse tags and create download links.
372392
*

src/GitHub_Updater/API/Gitea_API.php

+20
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,26 @@ function ( $e ) use ( &$arr ) {
268268
public function parse_changelog_response( $response ) {
269269
}
270270

271+
/**
272+
* Parse API response and return array of branch data.
273+
*
274+
* @param \stdClass $response API response.
275+
*
276+
* @return array Array of branch data.
277+
*/
278+
public function parse_branch_response( $response ) {
279+
if ( $this->validate_response( $response ) ) {
280+
return $response;
281+
}
282+
$branches = [];
283+
foreach ( $response as $branch ) {
284+
$branches[ $branch->name ]['download'] = $this->construct_download_link( $branch->name );
285+
$branches[ $branch->name ]['commit_hash'] = $branch->commit->id;
286+
$branches[ $branch->name ]['commit_timestamp'] = $branch->commit->timestamp;
287+
}
288+
return $branches;
289+
}
290+
271291
/**
272292
* Parse tags and create download links.
273293
*

src/GitHub_Updater/Readme_Parser.php

+29-10
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@
2626
*/
2727
class Readme_Parser extends Parser {
2828

29+
/**
30+
* Holds absolute filepath to temp readme file.
31+
*
32+
* @var string
33+
*/
34+
protected $readme_path;
35+
2936
/**
3037
* Constructor.
3138
*
@@ -39,8 +46,17 @@ class Readme_Parser extends Parser {
3946
*/
4047
public function __construct( $file ) {
4148
$file_path = WP_CONTENT_DIR . '/tmp-readme.txt';
42-
$file_path = file_put_contents( $file_path, $file ) ? $file_path : false;
43-
parent::__construct( $file_path );
49+
50+
/**
51+
* Filter location of temporary readme filepath.
52+
*
53+
* @since 8.7.0
54+
*
55+
* @param string $file_path Absolute filepath to temp readme file.
56+
*/
57+
$this->readme_path = apply_filters( 'github_updater_temp_readme_filepath', $file_path );
58+
$this->readme_path = file_put_contents( $this->readme_path, $file ) ? $this->readme_path : false;
59+
parent::__construct( $this->readme_path );
4460
}
4561

4662
/**
@@ -71,9 +87,10 @@ public function parse_data() {
7187
$data[ $key ] = 'contributors' === $key ? $this->create_contributors( $value ) : $value;
7288
}
7389
$data = $this->faq_as_h4( $data );
74-
$data = $this->changelog_as_h4( $data );
90+
$data = $this->readme_section_as_h4( 'changelog', $data );
91+
$data = $this->readme_section_as_h4( 'description', $data );
7592

76-
@unlink( WP_CONTENT_DIR . '/tmp-readme.txt' );
93+
@unlink( $this->readme_path );
7794

7895
return $data;
7996
}
@@ -101,7 +118,7 @@ private function create_contributors( $users ) {
101118
$contributors[ $contributor ]['display_name'] = $contributor;
102119
$contributors[ $contributor ]['profile'] = '//profiles.wordpress.org/' . $contributor;
103120
$contributors[ $contributor ]['avatar'] = 'https://wordpress.org/grav-redirect.php?user=' . $contributor;
104-
if ( $wp_version < '5.1-alpha' ) {
121+
if ( version_compare( $wp_version, '5.1-alpha', '<' ) ) {
105122
$contributors[ $contributor ] = '//profiles.wordpress.org/' . $contributor;
106123
}
107124
}
@@ -130,19 +147,21 @@ public function faq_as_h4( $data ) {
130147
}
131148

132149
/**
133-
* Converts wp.org readme changelog items to h4 style.
150+
* Converts wp.org readme section items to h4 style.
151+
*
152+
* @param string $section Readme section.
153+
* @param array $data Array of parsed readme data.
134154
*
135-
* @param mixed $data Array of parsed readme data.
136155
* @return array $data
137156
*/
138-
public function changelog_as_h4( $data ) {
139-
if ( empty( $data['sections']['changelog'] ) || false !== strpos( $data['sections']['changelog'], '<h4>' ) ) {
157+
public function readme_section_as_h4( $section, $data ) {
158+
if ( empty( $data['sections'][ $section ] ) || false !== strpos( $data['sections'][ $section ], '<h4>' ) ) {
140159
return $data;
141160
}
142161
$pattern = '~<p>=(.*)=</p>~';
143162
$replace = '<h4>$1</h4>';
144163

145-
$data['sections']['changelog'] = preg_replace( $pattern, $replace, $data['sections']['changelog'] );
164+
$data['sections'][ $section ] = preg_replace( $pattern, $replace, $data['sections'][ $section ] );
146165

147166
return $data;
148167
}

src/GitHub_Updater/Traits/API_Common.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -306,9 +306,7 @@ public function get_remote_api_branches( $git, $request ) {
306306
}
307307

308308
if ( $response ) {
309-
foreach ( $response as $branch ) {
310-
$branches[ $branch->name ] = $this->construct_download_link( $branch->name );
311-
}
309+
$branches = $this->parse_branch_response( $response );
312310
$this->type->branches = $branches;
313311
$this->set_repo_cache( 'branches', $branches );
314312

src/GitHub_Updater/Traits/GHU_Trait.php

+12-3
Original file line numberDiff line numberDiff line change
@@ -156,15 +156,24 @@ public function get_error_codes() {
156156
public function can_update_repo( $type ) {
157157
$wp_version = get_bloginfo( 'version' );
158158

159-
$remote_is_newer = isset( $type->remote_version )
160-
? version_compare( $type->remote_version, $type->local_version, '>' )
161-
: false;
162159
$wp_version_ok = ! empty( $type->requires )
163160
? version_compare( $wp_version, $type->requires, '>=' )
164161
: true;
165162
$php_version_ok = ! empty( $type->requires_php )
166163
? version_compare( PHP_VERSION, $type->requires_php, '>=' )
167164
: true;
165+
$remote_is_newer = isset( $type->remote_version )
166+
? version_compare( $type->remote_version, $type->local_version, '>' )
167+
: false;
168+
169+
/**
170+
* Filter $remote_is_newer if you use another method to test for updates.
171+
*
172+
* @since 8.7.0
173+
* @param bool $remote_is_newer
174+
* @param \stdClass $type Plugin/Theme data.
175+
*/
176+
$remote_is_newer = apply_filters( 'github_updater_remote_is_newer', $remote_is_newer, $type );
168177

169178
return $remote_is_newer && $wp_version_ok && $php_version_ok;
170179
}

0 commit comments

Comments
 (0)