Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
afragen committed Apr 23, 2022
2 parents 72a8d45 + 6e5b07e commit 6266eab
Show file tree
Hide file tree
Showing 13 changed files with 237 additions and 113 deletions.
8 changes: 8 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
#### [unreleased]

#### 10.10.0 / 2022-04-23
* add most of `move_dir()` and `is_virtualbox()`
* update above to work with Rollback
* put `move_dir()` and `is_virtualbox()` in `class Shim` for simpler usage from core functions
* make a new directory in `wp-content/upgrade/` for download slug fixing in `Base::upgrader_source_selection` to make better use of `move_dir()`
* delete new upgrade directory
* update API error caching, default 60 minute timeout

#### 10.9.0 / 2022-04-04
* revert usage of `move_dir()` and `is_virtualbox()` -- for now

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"afragen/wordpress-plugin-readme-parser": "dev-master",
"afragen/singleton": "dev-master",
"afragen/wp-dependency-installer": "^4",
"freemius/wordpress-sdk": "*"
"freemius/wordpress-sdk": "^2.4"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion git-updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* Plugin Name: Git Updater
* Plugin URI: https://git-updater.com
* Description: A plugin to automatically update GitHub hosted plugins, themes, and language packs. Additional API plugins available for Bitbucket, GitLab, Gitea, and Gist.
* Version: 10.9.0
* Version: 10.10.0
* Author: Andy Fragen
* License: MIT
* Domain Path: /languages
Expand Down
4 changes: 2 additions & 2 deletions languages/git-updater.pot
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
# This file is distributed under the MIT.
msgid ""
msgstr ""
"Project-Id-Version: Git Updater 10.9.0\n"
"Project-Id-Version: Git Updater 10.10.0\n"
"Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/git-updater\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"POT-Creation-Date: 2022-04-05T00:02:21+00:00\n"
"POT-Creation-Date: 2022-04-23T16:26:19+00:00\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"X-Generator: WP-CLI 2.6.0\n"
"X-Domain: git-updater\n"
Expand Down
3 changes: 2 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# Git Updater

Contributors: afragen
Donate link: https://thefragens.com/git-updater-donate
Tags: plugin, theme, language pack, updater
Requires at least: 5.2
Requires PHP: 5.6
Tested up to: 5.9
Tested up to: trunk
Stable tag: master
License: MIT

Expand Down
69 changes: 35 additions & 34 deletions src/Git_Updater/API/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,9 @@ public function api( $url ) {
$auth_header = $this->add_auth_header( [], $url );
$type = $this->return_repo_type();

// Use cached API failure data to avoid hammering the GitHub API.
// Use cached API failure data to avoid hammering the API.
$response = $this->get_repo_cache( md5( $url ) );
$cached = isset( $response['error_cache'] );
$response = $response ? $response['error_cache'] : $response;
$response = ! $response
? wp_remote_get( $url, array_merge( $this->default_http_get_args, $auth_header ) )
Expand All @@ -196,41 +197,41 @@ public function api( $url ) {

return $response;
}
if ( ! in_array( $code, $allowed_codes, true ) ) {

// Cache GitHub API failure data.
$wait = in_array( $type['git'], [ 'github', 'gist' ], true )
? GitHub_API::ratelimit_reset( $response, $this->type->slug )
: 0;
$timeout = get_site_transient( 'gu_refresh_cache' )
|| ( ! empty( static::$options['github_access_token'] ) && 403 !== $code )
? 0 : $wait;
$this->set_repo_cache( 'error_cache', $response, md5( $url ), "+{$timeout} minutes" );

static::$error_code[ $this->type->slug ] = isset( static::$error_code[ $this->type->slug ] ) ? static::$error_code[ $this->type->slug ] : [];
static::$error_code[ $this->type->slug ] = array_merge(
static::$error_code[ $this->type->slug ],
[
'repo' => $this->type->slug,
'code' => $code,
'name' => $this->type->name,
'git' => $this->type->git,
]
);
Singleton::get_instance( 'Messages', $this )->create_error_message( $type['git'] );

if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
$response_body = \json_decode( wp_remote_retrieve_body( $response ) );
if ( null !== $response_body && \property_exists( $response_body, 'message' ) ) {
$log_message = "Git Updater Error: {$this->type->name} ({$this->type->slug}:{$this->type->branch}) - {$response_body->message}";
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
error_log( $log_message );
}
// Cache HTTP API error code for 60 minutes.
if ( ! in_array( $code, $allowed_codes, true ) && ! $cached ) {
$timeout = 60;

// Set timeout to GitHub rate limit reset.
if ( in_array( $type['git'], [ 'github', 'gist' ], true ) ) {
$timeout = GitHub_API::ratelimit_reset( $response, $this->type->slug );
}
$this->set_repo_cache( 'error_cache', $response, md5( $url ), "+{$timeout} minutes" );

return false;
}

static::$error_code[ $this->type->slug ] = isset( static::$error_code[ $this->type->slug ] ) ? static::$error_code[ $this->type->slug ] : [];
static::$error_code[ $this->type->slug ] = array_merge(
static::$error_code[ $this->type->slug ],
[
'repo' => $this->type->slug,
'code' => $code,
'name' => $this->type->name,
'git' => $this->type->git,
]
);
Singleton::get_instance( 'Messages', $this )->create_error_message( $type['git'] );

if ( $cached && defined( 'WP_DEBUG' ) && WP_DEBUG ) {
$response_body = \json_decode( wp_remote_retrieve_body( $response ) );
if ( null !== $response_body && \property_exists( $response_body, 'message' ) ) {
$log_message = "Git Updater Error: {$this->type->name} ({$this->type->slug}:{$this->type->branch}) - {$response_body->message}";
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
error_log( $log_message );
}
}

/**
* Filter HTTP GET remote response body.
*
Expand Down Expand Up @@ -390,8 +391,9 @@ protected function exit_no_update( $response, $branch = false ) {
return empty( static::$options['branch_switch'] );
}

// phpcs:ignore WordPress.Security.NonceVerification.Missing
return ! isset( $_POST['gu_refresh_cache'] ) && ! $response && ! $this->can_update_repo( $this->type );
$refresh = get_site_transient( 'gu_refresh_cache' );

return ! $refresh && ! $response && ! $this->can_update_repo( $this->type );
}

/**
Expand Down Expand Up @@ -459,8 +461,7 @@ protected function sort_tags( $parsed_tags ) {
public function get_local_info( $repo, $file ) {
$response = false;

// phpcs:ignore WordPress.Security.NonceVerification.Missing
if ( isset( $_POST['gu_refresh_cache'] ) ) {
if ( get_site_transient( 'gu_refresh_cache' ) ) {
return $response;
}

Expand Down
17 changes: 15 additions & 2 deletions src/Git_Updater/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -461,13 +461,16 @@ public function get_changelog_filename( $repo ) {
* @param \Plugin_Upgrader|\Theme_Upgrader $upgrader An Upgrader object.
* @param array $hook_extra Array of hook data.
*
* @return string
* @return string|\WP_Error
*/
public function upgrader_source_selection( $source, $remote_source, $upgrader, $hook_extra = null ) {
global $wp_filesystem;

$slug = null;
$repo = null;
$new_source = null;
$upgrader_object = null;
$remote_source = $wp_filesystem->wp_content_dir() . 'upgrade/';

/*
* Rename plugins.
Expand Down Expand Up @@ -517,8 +520,18 @@ public function upgrader_source_selection( $source, $remote_source, $upgrader, $
$new_source = $this->fix_misnamed_directory( $new_source, $remote_source, $upgrader_object, $slug );

if ( $source !== $new_source ) {
$this->move( $source, $new_source );
if ( function_exists( 'move_dir' ) ) {
$result = \move_dir( $source, $new_source );
} else {
new Shim();
$result = move_dir( $source, $new_source );
}
if ( \is_wp_error( $result ) ) {
return $result;
}
}
// Clean up $new_source directory.
add_action( 'upgrader_install_package_result', [ $this, 'delete_upgrade_source' ], 10, 2 );

return trailingslashit( $new_source );
}
Expand Down
138 changes: 138 additions & 0 deletions src/Git_Updater/Shim.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* Class Shim
*
* Provides PHP 5.6 compatible shims.
* Loads WP 6.1 modified functions from Rollback.
*/
class Shim {

Expand All @@ -40,3 +41,140 @@ public static function dirname( $path, $level = 1 ) {
}
}
}

/**
* Moves a directory from one location to another via the rename() PHP function.
* If the renaming failed, falls back to copy_dir().
*
* Assumes that WP_Filesystem() has already been called and setup.
*
* @since 6.1.0
*
* @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
*
* @param string $from Source directory.
* @param string $to Destination directory.
* @return true|WP_Error True on success, WP_Error on failure.
*/
function move_dir( $from, $to ) {
global $wp_filesystem;

$result = false;

/*
* Skip the rename() call on VirtualBox environments.
* There are some known issues where rename() can fail on shared folders
* without reporting an error properly.
*
* More details:
* https://www.virtualbox.org/ticket/8761#comment:24
* https://www.virtualbox.org/ticket/17971
*/
if ( 'direct' === $wp_filesystem->method && ! is_virtualbox() ) {
$wp_filesystem->rmdir( $to );

$result = @rename( $from, $to );
}

// Non-direct filesystems use some version of rename without a fallback.
if ( 'direct' !== $wp_filesystem->method ) {
$result = $wp_filesystem->move( $from, $to );
}

if ( ! $result ) {
if ( ! $wp_filesystem->is_dir( $to ) ) {
if ( ! $wp_filesystem->mkdir( $to, FS_CHMOD_DIR ) ) {
return new \WP_Error( 'mkdir_failed_move_dir', __( 'Could not create directory.' ), $to );
}
}

$result = copy_dir( $from, $to, [ basename( $to ) ] );
}

return $result;
}

/**
* Attempt to detect a VirtualBox environment.
*
* This attempts all known methods of detecting VirtualBox.
*
* @global $wp_filesystem The filesystem.
*
* @since 6.1.0
*
* @return bool Whether or not VirtualBox was detected.
*/
function is_virtualbox() {
global $wp_filesystem;
static $is_virtualbox;

if ( ! defined( 'WP_RUN_CORE_TESTS' ) && null !== $is_virtualbox ) {
return $is_virtualbox;
}

/*
* Filters whether the current environment uses VirtualBox.
*
* @since 6.1.0
*
* @param bool $is_virtualbox Whether the current environment uses VirtualBox.
* Default: false.
*/
if ( apply_filters( 'is_virtualbox', false ) ) {
$is_virtualbox = true;
return $is_virtualbox;
}

// Detection via Composer.
if ( function_exists( 'getenv' ) && 'virtualbox' === getenv( 'COMPOSER_RUNTIME_ENV' ) ) {
$is_virtualbox = true;
return $is_virtualbox;
}

$virtualbox_unames = [ 'vvv' ];

// Detection via `php_uname()`.
if ( function_exists( 'php_uname' ) && in_array( php_uname( 'n' ), $virtualbox_unames, true ) ) {
$is_virtualbox = true;
return $is_virtualbox;
}

/*
* Vagrant can use alternative providers.
* This isn't reliable without some additional check(s).
*/
$virtualbox_usernames = [ 'vagrant' ];

// Detection via user name with POSIX.
if ( function_exists( 'posix_getpwuid' ) && function_exists( 'posix_geteuid' ) ) {
$user = posix_getpwuid( posix_geteuid() );
if ( $user && in_array( $user['name'], $virtualbox_usernames, true ) ) {
$is_virtualbox = true;
return $is_virtualbox;
}
}

// Initialize the filesystem if not set.
if ( ! $wp_filesystem ) {
require_once ABSPATH . '/wp-admin/includes/file.php';
WP_Filesystem();
}

// Detection via file owner.
if ( in_array( $wp_filesystem->owner( __FILE__ ), $virtualbox_usernames, true ) ) {
$is_virtualbox = true;
return $is_virtualbox;
}

// Detection via file group.
if ( in_array( $wp_filesystem->group( __FILE__ ), $virtualbox_usernames, true ) ) {
$is_virtualbox = true;
return $is_virtualbox;
}

// Give up.
$is_virtualbox = false;

return $is_virtualbox;
}
Loading

0 comments on commit 6266eab

Please sign in to comment.