Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
tillkruss committed Jun 28, 2023
2 parents 60467c1 + 822a221 commit 89e69d6
Show file tree
Hide file tree
Showing 12 changed files with 407 additions and 231 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
name: Code style

on: push
on:
pull_request: null
push: null

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
Expand All @@ -15,7 +17,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php: ['7.4', '8.2']
php: ['7.2', '7.4', '8.2']
relay: ['0.5.1']

steps:
Expand Down
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
# Changelog

## 2.4.2

- Made admin-bar script more robust
- Improved recovery instructions on `redis-error.php`
- Improved CloudLinux detection
- Localization improvements
- Prevent W3 Total Cache and LiteSpeed Cache from overwriting drop-in
- Fixed ACL username support when using Predis and array syntax

## 2.4.1

- Fix PHP `<=7.2` syntax error
- Fixed PHP `<=7.2` syntax error

## 2.4.0

Expand Down
4 changes: 2 additions & 2 deletions INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ For more connection examples see [Connections](https://github.com/rhubarbgroup/r

## Composer instructions

If you manage plugins using Composer follow these s
If you manage plugins using Composer follow these steps:

```bash
# Install the plugin:
Expand All @@ -62,6 +62,6 @@ wp config set WP_REDIS_PORT "6379"
wp config set WP_REDIS_DATABASE "15"
```

- [Configuration options](https://github.com/rhubarbgroup/redis-cache/#configuration).
- [Configuration options](https://github.com/rhubarbgroup/redis-cache/#configuration)
- [Connection examples](https://github.com/rhubarbgroup/redis-cache/#connections)
- [Scaling and replication](https://github.com/rhubarbgroup/redis-cache/#scaling)
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# Redis Object Cache for WordPress

A persistent object cache backend powered by Redis. Supports [Predis](https://github.com/predis/predis/), [PhpRedis (PECL)](https://github.com/phpredis/phpredis), [Relay](https://relaycache.com), replication, sentinels, clustering and [WP-CLI](http://wp-cli.org/).
A persistent object cache backend powered by Redis®¹. Supports [Predis](https://github.com/predis/predis/), [PhpRedis (PECL)](https://github.com/phpredis/phpredis), [Relay](https://relaycache.com), replication, sentinels, clustering and [WP-CLI](http://wp-cli.org/).

[![Redis Object Cache screenshots](/.wordpress-org/collage-sm.jpg?raw=true)](/.wordpress-org/collage.png?raw=true)

## Object Cache Pro

A **business class** Redis object cache backend. Truly reliable, highly optimized, fully customizable and with a dedicated engineer when you most need it.
A **business class** Redis®¹ object cache backend. Truly reliable, highly optimized, fully customizable and with a dedicated engineer when you most need it.

* Rewritten for raw performance
* 100% WordPress API compliant
Expand Down Expand Up @@ -37,7 +37,7 @@ The Redis Object Cache plugin comes with vast set of configuration options. If y
| `WP_REDIS_SCHEME` | `tcp` | The scheme used to connect: `tcp` or `unix` |
| `WP_REDIS_DATABASE` | `0` | The database used by the cache: `0-15` |
| `WP_REDIS_PREFIX` | | The prefix used for all cache keys to avoid data collisions, replaces `WP_CACHE_KEY_SALT`. Should be human readable, not a "salt". |
| `WP_REDIS_PASSWORD` | | The password of the Redis server, supports ACLs array: `['username', 'password']` |
| `WP_REDIS_PASSWORD` | | The password of the Redis server. Supports Redis ACLs arrays: `['user', 'password']` |
| `WP_REDIS_MAXTTL` | `0` | The maximum time-to-live of cache keys |
| `WP_REDIS_CLIENT` | | The client used to communicate with Redis: `predis`, `phpredis` or `relay` |
| `WP_REDIS_TIMEOUT` | `1` | The connection timeout in seconds |
Expand Down Expand Up @@ -70,7 +70,7 @@ Options that exist, but **should not**, **may break without notice** in future r

| Configuration constant | Default | Description |
| ----------------------------- | ----------- | ------------------------------------------------------------------- |
| `WP_REDIS_GRACEFUL` | `false` | Prevents exceptions from being thrown, but can cause data coruption |
| `WP_REDIS_GRACEFUL` | `false` | Prevents exceptions from being thrown, but will cause data corruption |
| `WP_REDIS_SELECTIVE_FLUSH` | `false` | Uses terribly slow Lua script for flushing |
| `WP_REDIS_UNFLUSHABLE_GROUPS` | `[]` | Uses terribly slow Lua script to prevent groups from being flushed |

Expand Down Expand Up @@ -101,8 +101,8 @@ Additional TLS/SSL stream connection options for connections can be defined usin

```php
define( 'WP_REDIS_SSL_CONTEXT', [
'verify_host' => false,
'verify_host_name' => false,
'verify_peer' => false,
'verify_peer_name' => false,
]);
```

Expand Down Expand Up @@ -220,3 +220,7 @@ Redis Object Cache has various hooks and the commonly used ones are listed below
| `redis_cache_expiration` | Filters the cache expiration for individual keys |
| `redis_cache_validate_dropin` | Filters whether the drop-in is valid |
| `redis_cache_add_non_persistent_groups` | Filters the groups to be marked as non persistent |

## Footnotes

¹ Redis is a registered trademark of Redis Ltd. Any rights therein are reserved to Redis Ltd. Any use by Redis Object Cache is for referential purposes only and does not indicate any sponsorship, endorsement or affiliation between Redis and Redis Object Cache.
2 changes: 1 addition & 1 deletion assets/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@
}];
}

if ( ! rediscache.disable_pro || ! rediscache.disable_banners ) {
if ( ! rediscache.disable_pro ) {
var pro_charts = {
time: function ( entry ) {
return [ entry[0], entry[1] * 0.5 ]
Expand Down
172 changes: 127 additions & 45 deletions includes/class-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ public function add_actions_and_filters() {
add_filter( 'qm/outputter/html', [ $this, 'register_qm_output' ] );

add_filter( 'perflab_disable_object_cache_dropin', '__return_true' );
add_filter( 'w3tc_config_item_objectcache.enabled', '__return_false' );
add_action( 'litespeed_init', [ $this, 'litespeed_disable_objectcache' ] );
}

/**
Expand Down Expand Up @@ -279,9 +281,13 @@ public function add_plugin_row_meta( array $plugin_meta, $plugin_file ) {
return $plugin_meta;
}

if ( self::acceleratewp_install() ) {
return $plugin_meta;
}

$plugin_meta[] = sprintf(
'<a href="%1$s"><span class="dashicons dashicons-star-filled" aria-hidden="true" style="font-size: 14px; line-height: 1.3"></span>%2$s</a>',
$this->link_to_ocp('meta-row'),
$this->link_to_ocp( 'meta-row' ),
esc_html_x( 'Upgrade to Pro', 'verb', 'redis-cache' )
);

Expand All @@ -299,10 +305,7 @@ public function link_to_ocp($medium, $as_html = true)
{
$ref = 'oss';

if (
( defined( 'WP_ROCKET_WEB_MAIN' ) && strpos( (string) WP_ROCKET_WEB_MAIN, 'cloudlinux.com' ) ) ||
( defined( 'WP_ROCKET_UPDATE_PATH' ) && strpos( (string) WP_ROCKET_UPDATE_PATH, 'cloudlinux' ) )
) {
if ( self::acceleratewp_install( true ) ) {
$ref = 'oss-cl';
}

Expand Down Expand Up @@ -376,8 +379,9 @@ public function enqueue_admin_scripts() {
'rediscache',
[
'jQuery' => 'jQuery',
'disable_pro' => $screen->id !== $this->screen,
'disable_banners' => defined( 'WP_REDIS_DISABLE_BANNERS' ) && WP_REDIS_DISABLE_BANNERS,
'disable_pro' => $screen->id !== $this->screen
|| ( defined( 'WP_REDIS_DISABLE_BANNERS' ) && WP_REDIS_DISABLE_BANNERS )
|| self::acceleratewp_install(),
'l10n' => [
'time' => __( 'Time', 'redis-cache' ),
'bytes' => __( 'Bytes', 'redis-cache' ),
Expand Down Expand Up @@ -732,52 +736,18 @@ public function render_admin_bar( $wp_admin_bar ) {
}

$nodeTitle = __( 'Object Cache', 'redis-cache' );
$flushMessage = __( 'Flushing cache...', 'redis-cache' );

$ajaxurl = esc_url( admin_url( 'admin-ajax.php' ) );
$nonce = wp_create_nonce();

$html = <<<HTML
<style>
#wpadminbar ul li.redis-cache-error { background: #c00; }
#wpadminbar:not(.mobile) .ab-top-menu > li.redis-cache-error:hover > .ab-item { background: #b30000; color: #fff; }
</style>
<script>
document.querySelector('#wp-admin-bar-redis-cache-flush > a')
.addEventListener('click', async function (event) {
event.preventDefault();
var node = document.querySelector('#wp-admin-bar-redis-cache');
var textNode = node.querySelector('.ab-item:first-child');
node.classList.remove('hover');
textNode.innerText = '{$flushMessage}';
try {
var data = new FormData();
data.append('action', 'roc_flush_cache');
data.append('nonce', '{$nonce}');
var response = await fetch('{$ajaxurl}', { method: 'POST', body: data });
textNode.innerText = await response.text();

setTimeout(function () { textNode.innerText = '{$nodeTitle}'; }, 3000);
} catch (error) {
textNode.innerText = '{$nodeTitle}';
alert('Object cache could not be flushed: ' + error);
}
});
</script>
HTML;
$style = preg_replace( '/\s+/', ' ', $this->admin_bar_style() );
$script = preg_replace( '/\s+/', ' ', $this->admin_bar_script() );
$html = "\n{$style}\n{$script}\n";

$redis_status = $this->get_redis_status();

$wp_admin_bar->add_node([
'id' => 'redis-cache',
'title' => $nodeTitle,
'meta' => [
'html' => preg_replace( '/\s+/', ' ', $html ),
'html' => $html,
'class' => $redis_status === false ? 'redis-cache-error' : '',
],
]);
Expand Down Expand Up @@ -849,6 +819,85 @@ public function render_admin_bar( $wp_admin_bar ) {
]);
}

/**
* Returns the admin-bar <style> tag.
*
* @return string
*/
protected function admin_bar_style()
{
return <<<HTML
<style>
#wpadminbar ul li.redis-cache-error {
background: #b30000;
}
#wpadminbar:not(.mobile) .ab-top-menu > li.redis-cache-error:hover > .ab-item {
background: #b30000;
color: #fff;
}
</style>
HTML;
}

/**
* Returns the admin-bar <script> tag.
*
* @return string
*/
protected function admin_bar_script()
{
$nonce = wp_create_nonce();
$ajaxurl = esc_url( admin_url( 'admin-ajax.php' ) );
$flushMessage = __( 'Flushing cache...', 'redis-cache' );

return <<<HTML
<script>
(function (element) {
if (! element) {
return;
}
element.addEventListener('click', async function (event) {
event.preventDefault();
var node = document.querySelector('#wp-admin-bar-redis-cache');
var textNode = node.querySelector('.ab-item:first-child');
if (! textNode.dataset.text) {
textNode.dataset.text = textNode.innerText;
}
node.classList.remove('hover');
textNode.innerText = '{$flushMessage}';
try {
var data = new FormData();
data.append('action', 'roc_flush_cache');
data.append('nonce', '{$nonce}');
var response = await fetch('{$ajaxurl}', {
method: 'POST',
body: data,
});
textNode.innerText = await response.text();
setTimeout(function () {
textNode.innerText = textNode.dataset.text;
}, 3000);
} catch (error) {
textNode.innerText = textNode.dataset.text;
alert('Object cache could not be flushed: ' + error);
}
});
})(
document.querySelector('#wp-admin-bar-redis-cache-flush > a')
);
</script>
HTML;
}

/**
* Executes admin actions
*
Expand Down Expand Up @@ -1514,4 +1563,37 @@ public function manage_redis_capability() {
public function current_user_can_manage_redis() {
return current_user_can( $this->manage_redis_capability() );
}

/**
* Prevent LiteSpeed Cache from overwriting the `object-cache.php` drop-in.
*
* @return void
*/
public function litespeed_disable_objectcache()
{
if ( isset( $_POST['LSCWP_CTRL'], $_POST['LSCWP_NONCE'], $_POST['object'] ) ) {
$_POST['object'] = '0';
}
}

/**
* Returns `true` if the plugin was installed by AccelerateWP from CloudLinux.
*
* @param bool $ignore_banner_constant
* @return bool
*/
public static function acceleratewp_install( $ignore_banner_constant = false ) {
$path = defined( 'WP_REDIS_PATH' ) ? WP_REDIS_PATH : null;
$scheme = defined( 'WP_REDIS_SCHEME' ) ? WP_REDIS_SCHEME : null;

if ( $scheme === 'unix' && strpos( (string) $path, '.clwpos/redis.sock' ) !== false ) {
if ( $ignore_banner_constant ) {
return true;
} else {
return defined( 'WP_REDIS_DISABLE_BANNERS' ) && WP_REDIS_DISABLE_BANNERS;
}
}

return false;
}
}
11 changes: 9 additions & 2 deletions includes/class-predis.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,15 @@ public function connect() {
}
}

if ( isset( $parameters['password'] ) && defined( 'WP_REDIS_USERNAME' ) ) {
$parameters['username'] = WP_REDIS_USERNAME;
if ( isset( $parameters['password'] ) ) {
if ( is_array( $parameters['password'] ) ) {
$parameters['username'] = array_shift( $parameters['password'] );
$parameters['password'] = implode( '', $parameters['password'] );
}

if ( defined( 'WP_REDIS_USERNAME' ) ) {
$parameters['username'] = WP_REDIS_USERNAME;
}
}

if ( defined( 'WP_REDIS_SSL_CONTEXT' ) && ! empty( WP_REDIS_SSL_CONTEXT ) ) {
Expand Down
Loading

0 comments on commit 89e69d6

Please sign in to comment.