Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
afragen committed Feb 24, 2019
2 parents 426cfbc + f5864b8 commit 60a540b
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 17 deletions.
7 changes: 7 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
#### [unreleased]

#### 8.7.0 / 2019-02-24
* update `Readme_Parser` for changelog and description parsing
* 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)
* fix `Readme_Parser` to use `version_compare()` when checking compatibility with `create_contributors()`
* add commit hash and timestamp to branch data, timestamp not returned by this particular GitHub API call 😞
* add filter `github_updater_remote_is_newer` to use your own version comparison function

#### 8.6.3 / 2019-02-04
* use Update PHP messaging as in WP 5.1 in version check

Expand Down
2 changes: 1 addition & 1 deletion github-updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* Plugin Name: GitHub Updater
* Plugin URI: https://github.com/afragen/github-updater
* 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.
* Version: 8.6.3
* Version: 8.7.0
* Author: Andy Fragen
* License: GNU General Public License v2
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
Expand Down
11 changes: 11 additions & 0 deletions src/GitHub_Updater/API/API_Interface.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,17 @@ public function parse_meta_response( $response);
*/
public function parse_changelog_response( $response);

/**
* Parse API response and return array of branch data.
*
* @access public
*
* @param \stdClass $response API response.
*
* @return array Array of branch data.
*/
public function parse_branch_response( $response );

/**
* Add values for individual repo add_setting_field().
*
Expand Down
20 changes: 20 additions & 0 deletions src/GitHub_Updater/API/Bitbucket_API.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,26 @@ function ( $e ) use ( &$arr ) {
public function parse_changelog_response( $response ) {
}

/**
* Parse API response and return array of branch data.
*
* @param \stdClass $response API response.
*
* @return array Array of branch data.
*/
public function parse_branch_response( $response ) {
if ( $this->validate_response( $response ) ) {
return $response;
}
$branches = [];
foreach ( $response as $branch ) {
$branches[ $branch->name ]['download'] = $this->construct_download_link( $branch->name );
$branches[ $branch->name ]['commit_hash'] = $branch->target->hash;
$branches[ $branch->name ]['commit_timestamp'] = $branch->target->date;
}
return $branches;
}

/**
* Parse tags and create download links.
*
Expand Down
19 changes: 19 additions & 0 deletions src/GitHub_Updater/API/GitHub_API.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,25 @@ function ( $e ) use ( &$arr ) {
return $arr;
}

/**
* Parse API response and return array of branch data.
*
* @param \stdClass $response API response.
*
* @return array Array of branch data.
*/
public function parse_branch_response( $response ) {
if ( $this->validate_response( $response ) ) {
return $response;
}
$branches = [];
foreach ( $response as $branch ) {
$branches[ $branch->name ]['download'] = $this->construct_download_link( $branch->name );
$branches[ $branch->name ]['commit_hash'] = $branch->commit->sha;
}
return $branches;
}

/**
* Parse tags and create download links.
*
Expand Down
20 changes: 20 additions & 0 deletions src/GitHub_Updater/API/GitLab_API.php
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,26 @@ function ( $e ) use ( &$arr ) {
return $arr;
}

/**
* Parse API response and return array of branch data.
*
* @param \stdClass $response API response.
*
* @return array Array of branch data.
*/
public function parse_branch_response( $response ) {
if ( $this->validate_response( $response ) ) {
return $response;
}
$branches = [];
foreach ( $response as $branch ) {
$branches[ $branch->name ]['download'] = $this->construct_download_link( $branch->name );
$branches[ $branch->name ]['commit_hash'] = $branch->commit->id;
$branches[ $branch->name ]['commit_timestamp'] = $branch->commit->committed_date;
}
return $branches;
}

/**
* Parse tags and create download links.
*
Expand Down
20 changes: 20 additions & 0 deletions src/GitHub_Updater/API/Gitea_API.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,26 @@ function ( $e ) use ( &$arr ) {
public function parse_changelog_response( $response ) {
}

/**
* Parse API response and return array of branch data.
*
* @param \stdClass $response API response.
*
* @return array Array of branch data.
*/
public function parse_branch_response( $response ) {
if ( $this->validate_response( $response ) ) {
return $response;
}
$branches = [];
foreach ( $response as $branch ) {
$branches[ $branch->name ]['download'] = $this->construct_download_link( $branch->name );
$branches[ $branch->name ]['commit_hash'] = $branch->commit->id;
$branches[ $branch->name ]['commit_timestamp'] = $branch->commit->timestamp;
}
return $branches;
}

/**
* Parse tags and create download links.
*
Expand Down
39 changes: 29 additions & 10 deletions src/GitHub_Updater/Readme_Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@
*/
class Readme_Parser extends Parser {

/**
* Holds absolute filepath to temp readme file.
*
* @var string
*/
protected $readme_path;

/**
* Constructor.
*
Expand All @@ -39,8 +46,17 @@ class Readme_Parser extends Parser {
*/
public function __construct( $file ) {
$file_path = WP_CONTENT_DIR . '/tmp-readme.txt';
$file_path = file_put_contents( $file_path, $file ) ? $file_path : false;
parent::__construct( $file_path );

/**
* Filter location of temporary readme filepath.
*
* @since 8.7.0
*
* @param string $file_path Absolute filepath to temp readme file.
*/
$this->readme_path = apply_filters( 'github_updater_temp_readme_filepath', $file_path );
$this->readme_path = file_put_contents( $this->readme_path, $file ) ? $this->readme_path : false;
parent::__construct( $this->readme_path );
}

/**
Expand Down Expand Up @@ -71,9 +87,10 @@ public function parse_data() {
$data[ $key ] = 'contributors' === $key ? $this->create_contributors( $value ) : $value;
}
$data = $this->faq_as_h4( $data );
$data = $this->changelog_as_h4( $data );
$data = $this->readme_section_as_h4( 'changelog', $data );
$data = $this->readme_section_as_h4( 'description', $data );

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

return $data;
}
Expand Down Expand Up @@ -101,7 +118,7 @@ private function create_contributors( $users ) {
$contributors[ $contributor ]['display_name'] = $contributor;
$contributors[ $contributor ]['profile'] = '//profiles.wordpress.org/' . $contributor;
$contributors[ $contributor ]['avatar'] = 'https://wordpress.org/grav-redirect.php?user=' . $contributor;
if ( $wp_version < '5.1-alpha' ) {
if ( version_compare( $wp_version, '5.1-alpha', '<' ) ) {
$contributors[ $contributor ] = '//profiles.wordpress.org/' . $contributor;
}
}
Expand Down Expand Up @@ -130,19 +147,21 @@ public function faq_as_h4( $data ) {
}

/**
* Converts wp.org readme changelog items to h4 style.
* Converts wp.org readme section items to h4 style.
*
* @param string $section Readme section.
* @param array $data Array of parsed readme data.
*
* @param mixed $data Array of parsed readme data.
* @return array $data
*/
public function changelog_as_h4( $data ) {
if ( empty( $data['sections']['changelog'] ) || false !== strpos( $data['sections']['changelog'], '<h4>' ) ) {
public function readme_section_as_h4( $section, $data ) {
if ( empty( $data['sections'][ $section ] ) || false !== strpos( $data['sections'][ $section ], '<h4>' ) ) {
return $data;
}
$pattern = '~<p>=(.*)=</p>~';
$replace = '<h4>$1</h4>';

$data['sections']['changelog'] = preg_replace( $pattern, $replace, $data['sections']['changelog'] );
$data['sections'][ $section ] = preg_replace( $pattern, $replace, $data['sections'][ $section ] );

return $data;
}
Expand Down
4 changes: 1 addition & 3 deletions src/GitHub_Updater/Traits/API_Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,7 @@ public function get_remote_api_branches( $git, $request ) {
}

if ( $response ) {
foreach ( $response as $branch ) {
$branches[ $branch->name ] = $this->construct_download_link( $branch->name );
}
$branches = $this->parse_branch_response( $response );
$this->type->branches = $branches;
$this->set_repo_cache( 'branches', $branches );

Expand Down
15 changes: 12 additions & 3 deletions src/GitHub_Updater/Traits/GHU_Trait.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,24 @@ public function get_error_codes() {
public function can_update_repo( $type ) {
$wp_version = get_bloginfo( 'version' );

$remote_is_newer = isset( $type->remote_version )
? version_compare( $type->remote_version, $type->local_version, '>' )
: false;
$wp_version_ok = ! empty( $type->requires )
? version_compare( $wp_version, $type->requires, '>=' )
: true;
$php_version_ok = ! empty( $type->requires_php )
? version_compare( PHP_VERSION, $type->requires_php, '>=' )
: true;
$remote_is_newer = isset( $type->remote_version )
? version_compare( $type->remote_version, $type->local_version, '>' )
: false;

/**
* Filter $remote_is_newer if you use another method to test for updates.
*
* @since 8.7.0
* @param bool $remote_is_newer
* @param \stdClass $type Plugin/Theme data.
*/
$remote_is_newer = apply_filters( 'github_updater_remote_is_newer', $remote_is_newer, $type );

return $remote_is_newer && $wp_version_ok && $php_version_ok;
}
Expand Down

0 comments on commit 60a540b

Please sign in to comment.