Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move command over to new v2 structure #21

Merged
merged 29 commits into from
Aug 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/vendor/
composer.lock
73 changes: 73 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
sudo: false
dist: trusty

language: php
php: 7.2

notifications:
email:
on_success: never
on_failure: change

branches:
only:
- master

cache:
directories:
- $HOME/.composer/cache

env:
global:
- PATH="$TRAVIS_BUILD_DIR/vendor/bin:$PATH"
- WP_CLI_BIN_DIR="$TRAVIS_BUILD_DIR/vendor/bin"

before_install:
- |
# Remove Xdebug for a huge performance increase:
if [ -f ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini ]; then
phpenv config-rm xdebug.ini
else
echo "xdebug.ini does not exist"
fi
- |
# Raise PHP memory limit to 2048MB
echo 'memory_limit = 2048M' >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini
- composer validate

install:
- composer install
- composer prepare-tests

script:
- composer phpunit
- composer behat

jobs:
include:
- stage: sniff
script:
- composer lint
- composer phpcs
env: BUILD=sniff
- stage: test
php: 7.2
env: WP_VERSION=latest
- stage: test
php: 7.1
env: WP_VERSION=latest
- stage: test
php: 7.0
env: WP_VERSION=latest
- stage: test
php: 5.6
env: WP_VERSION=latest
- stage: test
php: 5.6
env: WP_VERSION=3.7.11
- stage: test
php: 5.6
env: WP_VERSION=trunk
- stage: test
php: 5.4
env: WP_VERSION=latest
48 changes: 41 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,49 @@
],
"homepage": "/~https://github.com/wp-cli/wp-super-cache-cli",
"license": "MIT",
"authors": [{
"name": "WP-CLI Team",
"homepage": "http://github.com/wp-cli",
"role": "Developer"
}],
"authors": [
{
"name": "WP-CLI Team",
"homepage": "http://github.com/wp-cli",
"role": "Developer"
}
],
"require": {
"php": ">=5.3.0"
"php": ">=5.4",
"wp-cli/wp-cli": "^2"
},
"require-dev": {
"wp-cli/extension-command": "^2",
"wp-cli/wp-cli-tests": "^2.0.7"
},
"config": {
"platform": {
"php": "5.4"
},
"process-timeout": 7200,
"sort-packages": true
},
"autoload": {
"files": [ "wp-super-cache-cli.php" ]
"psr-4": {
"": "src/"
},
"files": [
"wp-super-cache-cli.php"
]
},
"minimum-stability": "dev",
"prefer-stable": true,
"scripts": {
"behat": "run-behat-tests",
"lint": "run-linter-tests",
"phpcs": "run-phpcs-tests",
"phpunit": "run-php-unit-tests",
"prepare-tests": "install-package-tests",
"test": [
"@lint",
"@phpcs",
"@phpunit",
"@behat"
]
}
}
24 changes: 24 additions & 0 deletions features/manage-cache.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Feature: Generate cache

Scenario: Manage wp-super-cache via CLI
Given a WP install

When I run `wp plugin install wp-super-cache`
Then STDOUT should contain:
"""
Plugin installed successfully.
"""
And the wp-content/plugins/wp-super-cache directory should exist

When I try `wp super-cache enable`
Then STDERR should contain:
"""
Error: WP Super Cache needs to be enabled to use its WP-CLI commands.
"""

When I run `wp plugin activate wp-super-cache`
And I run `wp super-cache enable`
Then STDOUT should contain:
"""
Success: The WP Super Cache is enabled.
"""
61 changes: 61 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?xml version="1.0"?>
<ruleset name="WP-CLI">
<description>Custom ruleset for WP-CLI</description>

<!-- For help in understanding this file: /~https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml -->
<!-- For help in using PHPCS: /~https://github.com/squizlabs/PHP_CodeSniffer/wiki/Usage -->

<!-- What to scan -->
<file>.</file>
<!-- Ignoring Files and Folders:
/~https://github.com/squizlabs/PHP_CodeSniffer/wiki/Advanced-Usage#ignoring-files-and-folders -->
<exclude-pattern>*/.git/*</exclude-pattern>
<exclude-pattern>*/ci/*</exclude-pattern>
<exclude-pattern>*/features/*</exclude-pattern>
<exclude-pattern>*/packages/*</exclude-pattern>
<exclude-pattern>*/tests/*</exclude-pattern>
<exclude-pattern>*/utils/*</exclude-pattern>
<exclude-pattern>*/vendor/*</exclude-pattern>

<!-- How to scan -->
<arg value="sp"/> <!-- Show sniff and progress -->
<arg name="colors"/> <!-- Show results with colors -->
<arg name="extensions" value="php"/> <!-- Limit to PHP files -->

<!-- Rules: Check PHP version compatibility - see
/~https://github.com/PHPCompatibility/PHPCompatibilityWP -->
<rule ref="PHPCompatibilityWP">
<!-- Polyfill package is used so array_column() is available for PHP 5.4- -->
<exclude name="PHPCompatibility.PHP.NewFunctions.array_columnFound"/>
<!-- Both magic quotes directives set in wp-settings-cli.php to provide consistent starting point. -->
<exclude name="PHPCompatibility.PHP.DeprecatedIniDirectives.magic_quotes_runtimeDeprecatedRemoved"/>
<exclude name="PHPCompatibility.PHP.DeprecatedIniDirectives.magic_quotes_sybaseDeprecatedRemoved"/>
</rule>

<!-- For help in understanding this testVersion:
/~https://github.com/PHPCompatibility/PHPCompatibility#sniffing-your-code-for-compatibility-with-specific-php-versions -->
<config name="testVersion" value="5.4-"/>

<!-- Ignore php_uname mode issue, we're conditionally providing a callback -->
<rule ref="PHPCompatibility.PHP.NewFunctionParameters.php_uname_modeFound">
<exclude-pattern>*/php/commands/src/CLI_Command.php</exclude-pattern>
</rule>

<!-- Rules: WordPress Coding Standards - see
/~https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards -->
<rule ref="WordPress-Core">
<exclude name="Squiz.PHP.DisallowMultipleAssignments.Found" />
<exclude name="WordPress.NamingConventions.ValidVariableName.NotSnakeCaseMemberVar" />
<exclude name="WordPress.NamingConventions.ValidVariableName.MemberNotSnakeCase" />
<exclude name="WordPress.NamingConventions.ValidVariableName.NotSnakeCase" />
</rule>

<!-- For help in understanding these custom sniff properties:
/~https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/wiki/Customizable-sniff-properties -->
<rule ref="WordPress.Files.FileName">
<properties>
<property name="strict_class_file_names" value="false"/>
</properties>
<exclude name="WordPress.Files.FileName.NotHyphenatedLowercase" />
</rule>
</ruleset>
51 changes: 21 additions & 30 deletions cli.php → src/WP_Super_Cache_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Manages the WP Super Cache plugin
*/
class WPSuperCache_Command extends WP_CLI_Command {
class WP_Super_Cache_Command extends WP_CLI_Command {

/**
* Clear something from the cache.
Expand All @@ -13,25 +13,24 @@ class WPSuperCache_Command extends WP_CLI_Command {
function flush( $args = array(), $assoc_args = array() ) {
require_once( WPCACHEHOME . '/wp-cache-phase1.php' );
global $WPSC_HTTP_HOST;
$home_url = parse_url( home_url() );
$home_url = parse_url( home_url() );
$WPSC_HTTP_HOST = $home_url['host'];

if ( isset($assoc_args['post_id']) ) {
if ( isset( $assoc_args['post_id'] ) ) {
if ( is_numeric( $assoc_args['post_id'] ) ) {
wp_cache_post_change( $assoc_args['post_id'] );
} else {
WP_CLI::error('This is not a valid post id.');
WP_CLI::error( 'This is not a valid post id.' );
}

wp_cache_post_change( $assoc_args['post_id'] );
}
elseif ( isset( $assoc_args['permalink'] ) ) {
} elseif ( isset( $assoc_args['permalink'] ) ) {
$id = url_to_postid( $assoc_args['permalink'] );

if ( is_numeric( $id ) ) {
wp_cache_post_change( $id );
} else {
WP_CLI::error('There is no post with this permalink.');
WP_CLI::error( 'There is no post with this permalink.' );
}
} else {
wp_cache_clean_cache( $file_prefix, true );
Expand All @@ -47,23 +46,23 @@ function status( $args = array(), $assoc_args = array() ) {

$cache_stats = get_option( 'supercache_stats' );

if ( !empty( $cache_stats ) ) {
if ( ! empty( $cache_stats ) ) {
if ( $cache_stats['generated'] > time() - 3600 * 24 ) {
WP_CLI::line( 'Cache status: ' . ($super_cache_enabled ? '%gOn%n' : '%rOff%n') );
WP_CLI::line( 'Cache content on ' . date('r', $cache_stats['generated'] ) . ': ' );
WP_CLI::line( 'Cache status: ' . ( $super_cache_enabled ? '%gOn%n' : '%rOff%n' ) );
WP_CLI::line( 'Cache content on ' . date( 'r', $cache_stats['generated'] ) . ': ' );
WP_CLI::line();
WP_CLI::line( ' WordPress cache:' );
WP_CLI::line( ' Cached: ' . $cache_stats[ 'wpcache' ][ 'cached' ] );
WP_CLI::line( ' Expired: ' . $cache_stats[ 'wpcache' ][ 'expired' ] );
WP_CLI::line( ' Cached: ' . $cache_stats['wpcache']['cached'] );
WP_CLI::line( ' Expired: ' . $cache_stats['wpcache']['expired'] );
WP_CLI::line();
WP_CLI::line( ' WP Super Cache:' );
WP_CLI::line( ' Cached: ' . $cache_stats[ 'supercache' ][ 'cached' ] );
WP_CLI::line( ' Expired: ' . $cache_stats[ 'supercache' ][ 'expired' ] );
WP_CLI::line( ' Cached: ' . $cache_stats['supercache']['cached'] );
WP_CLI::line( ' Expired: ' . $cache_stats['supercache']['expired'] );
} else {
WP_CLI::error('The WP Super Cache stats are too old to work with (older than 24 hours).');
WP_CLI::error( 'The WP Super Cache stats are too old to work with (older than 24 hours).' );
}
} else {
WP_CLI::error('No WP Super Cache stats found.');
WP_CLI::error( 'No WP Super Cache stats found.' );
}
}

Expand All @@ -75,10 +74,10 @@ function enable( $args = array(), $assoc_args = array() ) {

wp_super_cache_enable();

if($super_cache_enabled) {
if ( $super_cache_enabled ) {
WP_CLI::success( 'The WP Super Cache is enabled.' );
} else {
WP_CLI::error('The WP Super Cache is not enabled, check its settings page for more info.');
WP_CLI::error( 'The WP Super Cache is not enabled, check its settings page for more info.' );
}
}

Expand All @@ -90,10 +89,10 @@ function disable( $args = array(), $assoc_args = array() ) {

wp_super_cache_disable();

if(!$super_cache_enabled) {
if ( ! $super_cache_enabled ) {
WP_CLI::success( 'The WP Super Cache is disabled.' );
} else {
WP_CLI::error('The WP Super Cache is still enabled, check its settings page for more info.');
WP_CLI::error( 'The WP Super Cache is still enabled, check its settings page for more info.' );
}
}

Expand All @@ -110,7 +109,7 @@ function preload( $args = array(), $assoc_args = array() ) {
$pending_cancel = get_option( 'preload_cache_stop' );

// Bail early if caching or preloading is disabled
if( ! $super_cache_enabled ) {
if ( ! $super_cache_enabled ) {
WP_CLI::error( 'The WP Super Cache is not enabled.' );
}

Expand Down Expand Up @@ -158,7 +157,7 @@ function preload( $args = array(), $assoc_args = array() ) {
*/
protected function preload_status( $preload_counter, $pending_cancel ) {
if ( is_array( $preload_counter ) && $preload_counter['c'] > 0 ) {
WP_CLI::line( sprintf( 'Currently caching from post %d to %d.', $preload_counter[ 'c' ] - 100, $preload_counter[ 'c' ] ) );
WP_CLI::line( sprintf( 'Currently caching from post %d to %d.', $preload_counter['c'] - 100, $preload_counter['c'] ) );

if ( $pending_cancel ) {
WP_CLI::warning( 'Pending preload cancel. It may take up to a minute for it to cancel completely.' );
Expand All @@ -168,11 +167,3 @@ protected function preload_status( $preload_counter, $pending_cancel ) {
}
}
}

WP_CLI::add_command( 'super-cache', 'WPSuperCache_Command', array(
'before_invoke' => function(){
if ( ! function_exists( 'wp_super_cache_enable' ) ) {
WP_CLI::error( 'WP Super Cache needs to be enabled to use its WP-CLI commands.' );
}
}
) );
2 changes: 2 additions & 0 deletions wp-cli.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require:
- wp-super-cache-cli.php
30 changes: 18 additions & 12 deletions wp-super-cache-cli.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
<?php
/*
Plugin Name: WP Super Cache CLI
Version: 1.0
Description: A CLI interface for the WP Super Cache plugin
Author: WP-CLI Team
Author URI: http://github.com/wp-cli
Plugin URI: http://github.com/wp-cli/wp-super-cache-cli
License: MIT
*/

if ( defined( 'WP_CLI' ) && WP_CLI ) {
require_once dirname( __FILE__ ) . '/cli.php';

$autoload = dirname( __FILE__ ) . '/vendor/autoload.php';

if ( file_exists( $autoload ) ) {
require_once $autoload;
}

if ( ! class_exists( 'WP_CLI' ) ) {
return;
}

WP_CLI::add_command(
'super-cache', 'WP_Super_Cache_Command', array(
'before_invoke' => function() {
if ( ! function_exists( 'wp_super_cache_enable' ) ) {
WP_CLI::error( 'WP Super Cache needs to be enabled to use its WP-CLI commands.' );
}
},
)
);