Skip to content

Commit

Permalink
New disablePluginUpdateCheck class.
Browse files Browse the repository at this point in the history
  • Loading branch information
thefrosty committed Feb 13, 2019
1 parent 3fbc66e commit 829c110
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ PHP Version >= `7.2` will be required by version `1.5`.
## [1.4] - 2019-02-13
### PHP version bumped to >= 7.1
### Added
- New property in `PluginFactory::create` to disable plugin update checks.
- Add a new class `TheFrosty\WpUtilities\WpAdmin\disablePluginUpdateCheck` to disable plugin update checks. All you have
to do is instantiate it in a PluginInterface `add()` method.

## [1.3.1] - 2019-01-07
### Fixed
Expand Down
57 changes: 57 additions & 0 deletions src/WpAdmin/disablePluginUpdateCheck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php declare(strict_types=1);

namespace TheFrosty\WpUtilities\WpAdmin;

use TheFrosty\WpUtilities\Plugin\AbstractHookProvider;

/**
* Class disablePluginUpdateCheck
* @package TheFrosty\WpUtilities\WpAdmin
*/
class disablePluginUpdateCheck extends AbstractHookProvider
{
private const WP_ORG_UPDATE_CHECK = 'https://api.wordpress.org/plugins/update-check/';

/**
* Add class hooks
*/
public function addHooks()
{
$this->addFilter('http_request_args', [$this, 'httpRequestRemovePluginBasename']);
$this->addFilter('site_transient_update_plugins', [$this, 'transientRemovePluginBasename']);
}

/**
* Disable plugin update checks for the current plugin
* @link https://stackoverflow.com/a/39217270/558561
* @param array $args An array of HTTP request arguments.
* @param string $url The request URL.
* @return array
*/
protected function httpRequestRemovePluginBasename(array $args, string $url) : array
{
if (\strpos($url, self::WP_ORG_UPDATE_CHECK) === 0) {
if (!empty($args['body']['plugins'])) {
$plugins = \json_decode($args['body']['plugins'], true);
unset($plugins['plugins'][$this->getPlugin()->getBasename()]);
$args['body']['plugins'] = \wp_json_encode($plugins);
}
}
return $args;
}

/**
* Remove this plugin from the transient value via the core filter.
* @link https://gist.github.com/rniswonger/ee1b30e5fd3693bb5f92fbcfabe1654d
* @param mixed $value
* @return mixed
*/
protected function transientRemovePluginBasename($value)
{
if (isset($value) && \is_object($value) && (!empty($value->response) && \is_array($value->response))) {
unset($value->response[$this->getPlugin()->getBasename()]);
}

return $value;
}
}

0 comments on commit 829c110

Please sign in to comment.