Skip to content

Commit

Permalink
Add pantheon-wp-coding-standards (#400)
Browse files Browse the repository at this point in the history
* add our coding standards

* use php 8 for linting

* phpcbf fixes

* ignore tests for linting

* adjust and fix sniffs for object-cache.php drop-in

* check if all the server global values

* fix sniffs for WP CLI commands

* do a composer update before install for 7.4

* make the $cache property public

* fix composer update & install in both 7.4 tests

* set $cache_hits property to true

* set redis_calls property to public

* set is_redis_connected property to public

* revert wp_redis_ignore_global_groups const value

* make all the private and protected properties public

* use short arrays again

* remove parenthesis when we instantiate the class

* set object-cache back to what it was

* remove extra isset checks

* add the issets back to the database param

* add original object-cache file back

* use the version of the upstream tests that maybe have the issues fixed

* update the upstream tests

* Revert "add original object-cache file back"

This reverts commit 1fc2b66.

* restore cli.php and wp-redis.php back to what they were

* remove the upstream behat tests
these are failing because they're old and need to be updated. they have nothing to do with the plugin itself

* add some echos

* wordpress isn't actually getting installed
break the brackets

* one line, brackets and remove the &

* guess we can't do this on one line

* don't need that semicolon
i don't think

* okay fine, put it back the way it was

* enable redis before flushing cache

* enable redis on the entire site (not env) and add a wait command

* enable redis early
so we aren't potentially waiting for other things later

* don't need to wait

* don't need to enable because it's already enabled

* don't need an isset, we already know it's set

* use strip all tags instead of sanitize_text_field

* don't activate redis twice and flush cache after wp-redis is active

* re-add composer.lock

* re-add composer update for 7.4
  • Loading branch information
jazzsequence authored and John Spellman committed May 8, 2023
1 parent 8361a0a commit 1db2b1c
Show file tree
Hide file tree
Showing 10 changed files with 813 additions and 463 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
lint:
working_directory: ~/pantheon-systems/wp-redis
docker:
- image: quay.io/pantheon-public/build-tools-ci:8.x-php8.2
- image: quay.io/pantheon-public/build-tools-ci:8.x-php8.0
steps:
- checkout
- restore_cache:
Expand Down
3 changes: 2 additions & 1 deletion bin/behat-prepare.sh
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,10 @@ terminus build:workflow:wait $TERMINUS_SITE.$TERMINUS_ENV
###
# Set up WordPress, theme, and plugins for the test run
###
echo "Installing WordPress..."
# Silence output so as not to show the password.
{
terminus wp $SITE_ENV -- core install --title=$TERMINUS_ENV-$TERMINUS_SITE --url=$PANTHEON_SITE_URL --admin_user=$WORDPRESS_ADMIN_USERNAME [email protected] --admin_password=$WORDPRESS_ADMIN_PASSWORD
terminus wp $SITE_ENV -- core install --title=$TERMINUS_ENV-$TERMINUS_SITE --url=$PANTHEON_SITE_URL --admin_user=$WORDPRESS_ADMIN_USERNAME [email protected] --admin_password=$WORDPRESS_ADMIN_PASSWORD
} &> /dev/null

echo "Flush cache and setup environment..."
Expand Down
62 changes: 33 additions & 29 deletions cli.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<?php
/**
* Various WP Redis CLI utility commands.
*/

/**
* Various WP Redis utility commands.
* Main WP Redis CLI command class.
*/
class WP_Redis_CLI_Command {

Expand All @@ -12,31 +15,32 @@ public function cli() {
global $redis_server;

if ( empty( $redis_server ) ) {
# Attempt to automatically load Pantheon's Redis config from the env.
if ( isset( $_SERVER['CACHE_HOST'] ) ) {
$redis_server = array(
'host' => $_SERVER['CACHE_HOST'],
'port' => $_SERVER['CACHE_PORT'],
'auth' => $_SERVER['CACHE_PASSWORD'],
'database' => isset( $_SERVER['CACHE_DB'] ) ? $_SERVER['CACHE_DB'] : 0,
);
// Attempt to automatically load Pantheon's Redis config from the env.
if ( isset( $_SERVER['CACHE_HOST'] ) && isset( $_SERVER['CACHE_PORT'] ) && isset( $_SERVER['CACHE_PASSWORD'] ) && isset( $_SERVER['CACHE_DB'] ) ) {
$redis_server = [
'host' => sanitize_text_field( $_SERVER['CACHE_HOST'] ),
'port' => sanitize_text_field( $_SERVER['CACHE_PORT'] ),
'auth' => sanitize_text_field( $_SERVER['CACHE_PASSWORD'] ),
'database' => sanitize_text_field( $_SERVER['CACHE_DB'] ),
];
} else {
$redis_server = array(
'host' => '127.0.0.1',
'port' => 6379,
'auth' => '',
$redis_server = [
'host' => '127.0.0.1',
'port' => 6379,
'auth' => '',
'database' => 0,
);
];
}
}

if ( ! isset( $redis_server['database'] ) ) {
$redis_server['database'] = 0;
}

$cmd = WP_CLI\Utils\esc_cmd( 'redis-cli -h %s -p %s -a %s -n %s', $redis_server['host'], $redis_server['port'], $redis_server['auth'], $redis_server['database'] );
$process = WP_CLI\Utils\proc_open_compat( $cmd, array( STDIN, STDOUT, STDERR ), $pipes );
$r = proc_close( $process );
$pipes = null;
$cmd = WP_CLI\Utils\esc_cmd( 'redis-cli -h %s -p %s -a %s -n %s', $redis_server['host'], $redis_server['port'], $redis_server['auth'], $redis_server['database'] );
$process = WP_CLI\Utils\proc_open_compat( $cmd, [ STDIN, STDOUT, STDERR ], $pipes );
$r = proc_close( $process );
exit( (int) $r );
}

Expand All @@ -56,11 +60,11 @@ public function cli() {
public function debug( $_, $assoc_args ) {
global $wp_object_cache;
$this->load_wordpress_with_template();
$data = array(
$data = [
'cache_hits' => $wp_object_cache->cache_hits,
'cache_misses' => $wp_object_cache->cache_misses,
'redis_calls' => $wp_object_cache->redis_calls,
);
];
WP_CLI::print_value( $data, $assoc_args );
}

Expand Down Expand Up @@ -134,14 +138,14 @@ public function enable() {
* Success: Redis stats reset.
*/
public function info( $_, $assoc_args ) {
global $wp_object_cache, $redis_server;
global $wp_object_cache;

if ( ! defined( 'WP_REDIS_OBJECT_CACHE' ) || ! WP_REDIS_OBJECT_CACHE ) {
WP_CLI::error( 'WP Redis object-cache.php file is missing from the wp-content/ directory.' );
}

if ( $wp_object_cache->is_redis_connected && WP_CLI\Utils\get_flag_value( $assoc_args, 'reset' ) ) {
// Redis::resetStat() isn't functional, see https://github.com/phpredis/phpredis/issues/928
// Redis::resetStat() isn't functional, see https://github.com/phpredis/phpredis/issues/928.
if ( $wp_object_cache->redis->eval( "return redis.call('CONFIG','RESETSTAT')" ) ) {
WP_CLI::success( 'Redis stats reset.' );
} else {
Expand All @@ -168,7 +172,7 @@ private function load_wordpress_with_template() {
// Set up the main WordPress query.
wp();

$interpreted = array();
$interpreted = [];
foreach ( $wp_query as $key => $value ) {
if ( 0 === stripos( $key, 'is_' ) && $value ) {
$interpreted[] = $key;
Expand All @@ -188,15 +192,15 @@ function( $template ) {
999
);

// Template is normally loaded in global scope, so we need to replicate
// Template is normally loaded in global scope, so we need to replicate.
foreach ( $GLOBALS as $key => $value ) {
// phpcs:ignore PHPCompatibility.Variables.ForbiddenGlobalVariableVariable.NonBareVariableFound
global $$key;
}

// Load the theme template.
ob_start();
require_once( ABSPATH . WPINC . '/template-loader.php' );
require_once ABSPATH . WPINC . '/template-loader.php';
ob_get_clean();
}

Expand All @@ -206,7 +210,7 @@ function( $template ) {
* @see http://stackoverflow.com/questions/2637945/getting-relative-path-from-absolute-path-in-php
*/
private static function get_relative_path( $from, $to ) {
// some compatibility fixes for Windows paths
// some compatibility fixes for Windows paths.
$from = is_dir( $from ) ? rtrim( $from, '\/' ) . '/' : $from;
$to = is_dir( $to ) ? rtrim( $to, '\/' ) . '/' : $to;
$from = str_replace( '\\', '/', $from );
Expand All @@ -217,15 +221,15 @@ private static function get_relative_path( $from, $to ) {
$rel_path = $to;

foreach ( $from as $depth => $dir ) {
// find first non-matching dir
// find first non-matching dir.
if ( $dir === $to[ $depth ] ) {
// ignore this directory
// ignore this directory.
array_shift( $rel_path );
} else {
// get number of remaining dirs to $from
// get number of remaining dirs to $from.
$remaining = count( $from ) - $depth;
if ( $remaining > 1 ) {
// add traversals up to first matching dir
// add traversals up to first matching dir.
$pad_length = ( count( $rel_path ) + $remaining - 1 ) * -1;
$rel_path = array_pad( $rel_path, $pad_length, '..' );
break;
Expand Down
6 changes: 2 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@
"behat/behat": "^3.1",
"behat/mink-extension": "^2.2",
"behat/mink-goutte-driver": "^1.2",
"pantheon-systems/pantheon-wordpress-upstream-tests": "dev-fix-behat-upstream-tests",
"wp-coding-standards/wpcs": "dev-develop as 2.3.1",
"dealerdirect/phpcodesniffer-composer-installer": "^1.0.0",
"pantheon-systems/pantheon-wp-coding-standards": "^1.0",
"pantheon-systems/pantheon-wordpress-upstream-tests": "dev-master",
"phpunit/phpunit": "^9",
"phpcompatibility/php-compatibility": "^9.3",
"yoast/phpunit-polyfills": "^1.0"
},
"scripts": {
Expand Down
Loading

0 comments on commit 1db2b1c

Please sign in to comment.