From 78df528b13c2d7350695070c6062b2212332cfb9 Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Sun, 20 Nov 2022 11:25:42 +0100 Subject: [PATCH 1/9] move dark-mode styling into dashboard.css Dark mode styling has been added inline using the "doing_dark_mode" hook which is no longer called in current plugin versions. Include the CSS definition directly into dashboard.css and deprecate the hook for adding inline styles. --- css/dashboard.css | 4 ++++ inc/class-cachify.php | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/css/dashboard.css b/css/dashboard.css index 6b790189..aad8035a 100644 --- a/css/dashboard.css +++ b/css/dashboard.css @@ -5,6 +5,10 @@ fill: #606a73; } +html.dark-mode body:not(.block-editor-page) #dashboard_right_now .cachify-icon { + fill: #bbc8d4; +} + #dashboard_right_now li a.cachify-glance::before { content: ""; padding: 0; diff --git a/inc/class-cachify.php b/inc/class-cachify.php index 748ad2f4..8863ff02 100644 --- a/inc/class-cachify.php +++ b/inc/class-cachify.php @@ -155,8 +155,6 @@ public function __construct() { add_action( 'admin_enqueue_scripts', array( __CLASS__, 'admin_dashboard_styles' ) ); - add_action( 'doing_dark_mode', array( __CLASS__, 'admin_dashboard_dark_mode_styles' ) ); - add_action( 'transition_comment_status', array( __CLASS__, 'touch_comment' ), 10, 3 ); add_action( 'edit_comment', array( __CLASS__, 'edit_comment' ) ); @@ -1608,6 +1606,8 @@ public static function admin_dashboard_styles() { * Fixing some admin dashboard styles * * @since 2.3.0 + * + * @deprecated included in dashboard.css since 2.4 */ public static function admin_dashboard_dark_mode_styles() { wp_add_inline_style( 'cachify-dashboard', '#dashboard_right_now .cachify-icon use { fill: #bbc8d4; }' ); From 2e9500e9988af8db07e0f250dcd1425ae0053b47 Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Tue, 22 Nov 2022 16:01:00 +0100 Subject: [PATCH 2/9] introduce unit tests --- .distignore | 6 ++ .github/workflows/test.yml | 41 +++++++- .gitignore | 2 + bin/install-wp-tests.sh | 163 +++++++++++++++++++++++++++++++ composer.json | 7 +- phpunit.xml | 25 +++++ tests/bootstrap.php | 30 ++++++ tests/test-cachify-apc.php | 31 ++++++ tests/test-cachify-db.php | 65 ++++++++++++ tests/test-cachify-hdd.php | 105 ++++++++++++++++++++ tests/test-cachify-memcached.php | 34 +++++++ tests/test-cachify.php | 107 ++++++++++++++++++++ 12 files changed, 614 insertions(+), 2 deletions(-) create mode 100644 bin/install-wp-tests.sh create mode 100644 phpunit.xml create mode 100644 tests/bootstrap.php create mode 100644 tests/test-cachify-apc.php create mode 100644 tests/test-cachify-db.php create mode 100644 tests/test-cachify-hdd.php create mode 100644 tests/test-cachify-memcached.php create mode 100644 tests/test-cachify.php diff --git a/.distignore b/.distignore index f3aedc0b..59c966ae 100644 --- a/.distignore +++ b/.distignore @@ -2,7 +2,9 @@ /.git /.github /.wordpress-org +/bin /node_modules +/tests /vendor # Files @@ -10,10 +12,14 @@ /.editorconfig /.gitattributes /.gitignore +/.phpunit.result.cache /.stylelintrc.json /composer.json /composer.lock /package.json /package-lock.json /phpcs.xml +/phpunit.xml +/phpunit.coverage.xml +/phpunit.report.xml /README.md diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 50788e22..30b9bc6e 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,6 +1,45 @@ -name: Coding Standards +name: Tests on: [push, pull_request] jobs: + unit: + runs-on: ubuntu-latest + strategy: + matrix: + include: + - php: '5.6' + wordpress: '4.7' + - php: '7.4' + wordpress: '5.9' + - php: '8.0' + wordpress: '6.0' + - php: '8.1' + wordpress: 'latest' + - php: '8.1' + wordpress: 'nightly' + steps: + - name: Checkout + uses: actions/checkout@v3 + with: + fetch-depth: 0 + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{matrix.php}} + tools: composer + - name: Install + run: composer install --no-interaction + - name: Build + run: composer build + - name: Setup DB + uses: shogo82148/actions-setup-mysql@v1 + with: + mysql-version: 'mysql-5.7' + root-password: "root" + - name: Setup WP + run: bash bin/install-wp-tests.sh wordpress_test root root 127.0.0.1 "${{ matrix.wordpress }}" + - name: PHP unit tests + run: composer test + quality: runs-on: ubuntu-latest steps: diff --git a/.gitignore b/.gitignore index f5833602..d2694b1a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,8 @@ .idea/ +.phpunit.result.cache css/*.min.css vendor/ node_modules/ composer.lock package-lock.json +phpunit.*.xml diff --git a/bin/install-wp-tests.sh b/bin/install-wp-tests.sh new file mode 100644 index 00000000..b9447efd --- /dev/null +++ b/bin/install-wp-tests.sh @@ -0,0 +1,163 @@ +#!/usr/bin/env bash + +if [ $# -lt 3 ]; then + echo "usage: $0 [db-host] [wp-version] [skip-database-creation]" + exit 1 +fi + +DB_NAME=$1 +DB_USER=$2 +DB_PASS=$3 +DB_HOST=${4-localhost} +WP_VERSION=${5-latest} +SKIP_DB_CREATE=${6-false} + +TMPDIR=${TMPDIR-/tmp} +TMPDIR=$(echo $TMPDIR | sed -e "s/\/$//") +WP_TESTS_DIR=${WP_TESTS_DIR-$TMPDIR/wordpress-tests-lib} +WP_CORE_DIR=${WP_CORE_DIR-$TMPDIR/wordpress/} + +download() { + if [ `which curl` ]; then + curl -s "$1" > "$2"; + elif [ `which wget` ]; then + wget -nv -O "$2" "$1" + fi +} + +if [[ $WP_VERSION =~ ^[0-9]+\.[0-9]+$ ]]; then + WP_TESTS_TAG="branches/$WP_VERSION" +elif [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0-9]+ ]]; then + if [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0] ]]; then + # version x.x.0 means the first release of the major version, so strip off the .0 and download version x.x + WP_TESTS_TAG="tags/${WP_VERSION%??}" + else + WP_TESTS_TAG="tags/$WP_VERSION" + fi +elif [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then + WP_TESTS_TAG="trunk" +else + # http serves a single offer, whereas https serves multiple. we only want one + download http://api.wordpress.org/core/version-check/1.7/ /tmp/wp-latest.json + grep '[0-9]+\.[0-9]+(\.[0-9]+)?' /tmp/wp-latest.json + LATEST_VERSION=$(grep -o '"version":"[^"]*' /tmp/wp-latest.json | sed 's/"version":"//') + if [[ -z "$LATEST_VERSION" ]]; then + echo "Latest WordPress version could not be found" + exit 1 + fi + WP_TESTS_TAG="tags/$LATEST_VERSION" +fi + +set -ex + +install_wp() { + + if [ -d $WP_CORE_DIR ]; then + return; + fi + + mkdir -p $WP_CORE_DIR + + if [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then + mkdir -p $TMPDIR/wordpress-nightly + download https://wordpress.org/nightly-builds/wordpress-latest.zip $TMPDIR/wordpress-nightly/wordpress-nightly.zip + unzip -q $TMPDIR/wordpress-nightly/wordpress-nightly.zip -d $TMPDIR/wordpress-nightly/ + mv $TMPDIR/wordpress-nightly/wordpress/* $WP_CORE_DIR + else + if [ $WP_VERSION == 'latest' ]; then + local ARCHIVE_NAME='latest' + elif [[ $WP_VERSION =~ [0-9]+\.[0-9]+ ]]; then + # https serves multiple offers, whereas http serves single. + download https://api.wordpress.org/core/version-check/1.7/ $TMPDIR/wp-latest.json + if [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0] ]]; then + # version x.x.0 means the first release of the major version, so strip off the .0 and download version x.x + LATEST_VERSION=${WP_VERSION%??} + else + # otherwise, scan the releases and get the most up to date minor version of the major release + local VERSION_ESCAPED=`echo $WP_VERSION | sed 's/\./\\\\./g'` + LATEST_VERSION=$(grep -o '"version":"'$VERSION_ESCAPED'[^"]*' $TMPDIR/wp-latest.json | sed 's/"version":"//' | head -1) + fi + if [[ -z "$LATEST_VERSION" ]]; then + local ARCHIVE_NAME="wordpress-$WP_VERSION" + else + local ARCHIVE_NAME="wordpress-$LATEST_VERSION" + fi + else + local ARCHIVE_NAME="wordpress-$WP_VERSION" + fi + download https://wordpress.org/${ARCHIVE_NAME}.tar.gz $TMPDIR/wordpress.tar.gz + tar --strip-components=1 -zxmf $TMPDIR/wordpress.tar.gz -C $WP_CORE_DIR + fi + + download https://raw.github.com/markoheijnen/wp-mysqli/master/db.php $WP_CORE_DIR/wp-content/db.php +} + +install_test_suite() { + # portable in-place argument for both GNU sed and Mac OSX sed + if [[ $(uname -s) == 'Darwin' ]]; then + local ioption='-i .bak' + else + local ioption='-i' + fi + + # set up testing suite if it doesn't yet exist + if [ ! -d $WP_TESTS_DIR ]; then + # set up testing suite + mkdir -p $WP_TESTS_DIR + svn co --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/includes/ $WP_TESTS_DIR/includes + svn co --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/data/ $WP_TESTS_DIR/data + fi + + if [ ! -f wp-tests-config.php ]; then + download https://develop.svn.wordpress.org/${WP_TESTS_TAG}/wp-tests-config-sample.php "$WP_TESTS_DIR"/wp-tests-config.php + # remove all forward slashes in the end + WP_CORE_DIR=$(echo $WP_CORE_DIR | sed "s:/\+$::") + sed $ioption "s:dirname( __FILE__ ) . '/src/':'$WP_CORE_DIR/':" "$WP_TESTS_DIR"/wp-tests-config.php + sed $ioption "s/youremptytestdbnamehere/$DB_NAME/" "$WP_TESTS_DIR"/wp-tests-config.php + sed $ioption "s/yourusernamehere/$DB_USER/" "$WP_TESTS_DIR"/wp-tests-config.php + sed $ioption "s/yourpasswordhere/$DB_PASS/" "$WP_TESTS_DIR"/wp-tests-config.php + sed $ioption "s|localhost|${DB_HOST}|" "$WP_TESTS_DIR"/wp-tests-config.php + fi + + # Modify the WP_UnitTestCase class to use the polyfilled version for PHPUnit cross-compatibility. + # This is a dirty "backport" of the polyfills used in WP 5.9 and might fail with future updates. + if [ ! -f "$WP_TESTS_DIR"/includes/abstract-testcase.php ]; then + local testcase_file="$WP_TESTS_DIR"/includes/testcase.php + sed $ioption 's/class WP_UnitTestCase extends PHPUnit_Framework_TestCase /class WP_UnitTestCase extends Yoast\\PHPUnitPolyfills\\TestCases\\TestCase /' "$testcase_file" + sed $ioption 's/setUpBeforeClass[(][)]/set_up_before_class()/g' "$testcase_file" + sed $ioption 's/tearDownAfterClass[(][)]/tear_down_after_class()/g' "$testcase_file" + sed $ioption 's/setUp[(][)]/set_up()/g' "$testcase_file" + sed $ioption 's/tearDown[(][)]/tear_down()/g' "$testcase_file" + fi + +} + +install_db() { + + if [ ${SKIP_DB_CREATE} = "true" ]; then + return 0 + fi + + # parse DB_HOST for port or socket references + local PARTS=(${DB_HOST//\:/ }) + local DB_HOSTNAME=${PARTS[0]}; + local DB_SOCK_OR_PORT=${PARTS[1]}; + local EXTRA="" + + if ! [ -z $DB_HOSTNAME ] ; then + if [ $(echo $DB_SOCK_OR_PORT | grep -e '^[0-9]\{1,\}$') ]; then + EXTRA=" --host=$DB_HOSTNAME --port=$DB_SOCK_OR_PORT --protocol=tcp" + elif ! [ -z $DB_SOCK_OR_PORT ] ; then + EXTRA=" --socket=$DB_SOCK_OR_PORT" + elif ! [ -z $DB_HOSTNAME ] ; then + EXTRA=" --host=$DB_HOSTNAME --protocol=tcp" + fi + fi + + # create database + mysqladmin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA +} + +install_wp +install_test_suite +install_db diff --git a/composer.json b/composer.json index c3405e4e..ee42659f 100644 --- a/composer.json +++ b/composer.json @@ -28,9 +28,11 @@ "require-dev": { "dealerdirect/phpcodesniffer-composer-installer": "^v0.7", "matthiasmullie/minify": "^1.3", + "phpunit/phpunit": "^5|^7|^9", "squizlabs/php_codesniffer": "^3.7", "phpcompatibility/phpcompatibility-wp": "^2.1", - "wp-coding-standards/wpcs": "^2.3" + "wp-coding-standards/wpcs": "^2.3", + "yoast/phpunit-polyfills": "^1.0" }, "scripts": { "post-install-cmd": [ @@ -63,6 +65,9 @@ ], "lint-php": [ "phpcs --standard=phpcs.xml -s" + ], + "test": [ + "phpunit" ] }, "config": { diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 00000000..b16e11ae --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,25 @@ + + + + + cachify.php + inc + + + + + + + + ./tests/ + + + + + + diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 00000000..2a307196 --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,30 @@ +Test Me

Test Content.

', + 3600 + ); + + $cached = Cachify_DB::get_item( '965b4abf2414e45036ab90c9d3f8dbc7' ); + self::assertIsArray( $cached, 'item was not stored' ); + self::assertEquals( + 'Test Me

Test Content.

', + $cached['data'], + 'unexpected data in cache' + ); + self::assertIsInt( $cached['meta']['queries'], 'number of queries not filled' ); + self::assertIsString( $cached['meta']['timer'], 'timing not filled' ); + self::assertIsString( $cached['meta']['memory'], 'memory not filled' ); + self::assertIsInt( $cached['meta']['time'], 'time not filled' ); + + // Another item. + Cachify_DB::store_item( + 'ef7e4a0540f6cde19e6eb658c69b0064', + 'Test 2

Test Content #2.

', + 3600 + ); + self::assertIsArray( Cachify_DB::get_item( 'ef7e4a0540f6cde19e6eb658c69b0064' ), 'second item was not stored' ); + + // Delete the first item. + Cachify_DB::delete_item( '965b4abf2414e45036ab90c9d3f8dbc7' ); + self::assertFalse( Cachify_DB::get_item( '965b4abf2414e45036ab90c9d3f8dbc7' ), 'first item was not deleted' ); + self::assertIsArray( Cachify_DB::get_item( 'ef7e4a0540f6cde19e6eb658c69b0064' ), 'second item should still be present' ); + } +} diff --git a/tests/test-cachify-hdd.php b/tests/test-cachify-hdd.php new file mode 100644 index 00000000..80bf3656 --- /dev/null +++ b/tests/test-cachify-hdd.php @@ -0,0 +1,105 @@ +Test Me

Test Content.

', + 3600, // Ignored. + false + ); + self::assertTrue( Cachify_HDD::get_item() ); + self::assertTrue( is_file( CACHIFY_CACHE_DIR . DIRECTORY_SEPARATOR . 'example.org/testme/index.html' ) ); + $cached = file_get_contents( CACHIFY_CACHE_DIR . DIRECTORY_SEPARATOR . 'example.org/testme/index.html' ); + self::assertStringStartsWith( + 'Test Me

Test Content.

+ +', $cached ); + + // Another item. + self::go_to( '/test2/' ); + Cachify_HDD::store_item( + 'ef7e4a0540f6cde19e6eb658c69b0064', // Ignored. + 'Test 2

Test Content #2.

', + 3600, // Ignored. + true + ); + self::assertTrue( is_file( CACHIFY_CACHE_DIR . DIRECTORY_SEPARATOR . 'example.org/test2/index.html' ) ); + $cached = file_get_contents( CACHIFY_CACHE_DIR . DIRECTORY_SEPARATOR . 'example.org/test2/index.html' ); + self::assertStringStartsWith( + 'Test 2

Test Content #2.

+ +', $cached ); + + // Delete the first item. + Cachify_HDD::delete_item( '965b4abf2414e45036ab90c9d3f8dbc7', 'http://example.org/testme/' ); + self::assertFalse( is_file( CACHIFY_CACHE_DIR . DIRECTORY_SEPARATOR . 'example.org/testme/index.html' ), 'first item was not deleted' ); + self::assertTrue( is_file( CACHIFY_CACHE_DIR . DIRECTORY_SEPARATOR . 'example.org/test2/index.html' ), 'second item should still be present' ); + + // Clear the cache. + Cachify_HDD::clear_cache(); + self::assertFalse( is_dir( CACHIFY_CACHE_DIR . DIRECTORY_SEPARATOR . 'example.org/test2' ) ); + self::assertFalse( Cachify_HDD::get_item() ); + } +} diff --git a/tests/test-cachify-memcached.php b/tests/test-cachify-memcached.php new file mode 100644 index 00000000..84cc0b58 --- /dev/null +++ b/tests/test-cachify-memcached.php @@ -0,0 +1,34 @@ + 10, + 'test_2' => 20, + ); + } + ); + + // Call flush registration. + Cachify::register_flush_cache_hooks(); + + // Verify that the filter has been called. + self::assertNotNull( $original_capture, 'Filter not called' ); + self::assertEquals( 12, count( $original_capture ), 'Unexpected number of default hooks' ); + self::assertEmpty( + array_filter( + $original_capture, + function( $v ) { + return 10 !== $v; + } + ), + 'All default filters should have priority 10' + ); + + // Verify that the action has been hooked with given priority. + self::assertEquals( + 10, + has_action( 'test_1', array( Cachify::class, 'flush_total_cache' ) ), + 'Flush action not hooked as expected' + ); + self::assertEquals( + 20, + has_action( 'test_2', array( Cachify::class, 'flush_total_cache' ) ), + 'Flush action not hooked as expected' + ); + } + + /** + * Test registration of scripts. + */ + public function test_register_scripts() { + Cachify::register_scripts(); + self::assertTrue( wp_script_is( 'cachify-admin-bar-flush', 'registered' ) ); + $script = wp_scripts()->registered['cachify-admin-bar-flush']; + self::assertStringEndsWith( + '/js/admin-bar-flush.min.js', + $script->src, + 'unexpected script source' + ); + } + + /** + * Test registration of styles. + */ + public function test_register_styles() { + Cachify::register_styles(); + self::assertTrue( wp_style_is( 'cachify-dashboard', 'registered' ) ); + self::assertTrue( wp_style_is( 'cachify-admin-bar-flush', 'registered' ) ); + + $style = wp_styles()->registered['cachify-dashboard']; + self::assertStringEndsWith( + '/css/dashboard.min.css', + $style->src, + 'unexpected dashboard style source' + ); + + $style = wp_styles()->registered['cachify-admin-bar-flush']; + self::assertStringEndsWith( + '/css/admin-bar-flush.min.css', + $style->src, + 'unexpected admin bar style source' + ); + } + + /** + * Test single site plugin activation. + */ + public function test_on_activation() { + self::assertFalse( get_option( 'cachify' ), 'Cachify option should not be initialized initially' ); + Cachify::on_activation(); + self::assertEquals( array() , get_option( 'cachify' ), 'Cachify option not initialized' ); + } +} From 8c2397f3afcd80416867f9dbd222d418deb76c13 Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Tue, 22 Nov 2022 16:46:32 +0100 Subject: [PATCH 3/9] add .sonarcloud.properties to narrow the analysis scope --- .distignore | 1 + .sonarcloud.properties | 9 +++++++++ 2 files changed, 10 insertions(+) create mode 100644 .sonarcloud.properties diff --git a/.distignore b/.distignore index 59c966ae..19556d45 100644 --- a/.distignore +++ b/.distignore @@ -13,6 +13,7 @@ /.gitattributes /.gitignore /.phpunit.result.cache +/.sonarcloud.properties /.stylelintrc.json /composer.json /composer.lock diff --git a/.sonarcloud.properties b/.sonarcloud.properties new file mode 100644 index 00000000..67986297 --- /dev/null +++ b/.sonarcloud.properties @@ -0,0 +1,9 @@ +# Path to sources. +sonar.sources=inc,js,css,cachify.php +sonar.exclusions=**/*.min.css,**/*.min.js +#sonar.inclusions= + +# Path to tests. +sonar.tests=tests +#sonar.test.exclusions= +#sonar.test.inclusions= From 38345db52dbf592ae24889475056f103fd4c7107 Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Sun, 20 Nov 2022 17:00:02 +0100 Subject: [PATCH 4/9] clean up PHPdoc blocks * consistent use of int/bool over integer/boolean * consistent indentation of param amd return blocks * use since tags instead of change, where applicable * remove redundant change tags * use version number that actually exist (2.0, 2.3.0, ...) --- inc/class-cachify-apc.php | 57 +++-- inc/class-cachify-cli.php | 13 +- inc/class-cachify-db.php | 78 ++++--- inc/class-cachify-hdd.php | 110 +++++----- inc/class-cachify-memcached.php | 77 ++++--- inc/class-cachify.php | 356 +++++++++++++++----------------- 6 files changed, 320 insertions(+), 371 deletions(-) diff --git a/inc/class-cachify-apc.php b/inc/class-cachify-apc.php index 39761f4f..0e8c17c9 100644 --- a/inc/class-cachify-apc.php +++ b/inc/class-cachify-apc.php @@ -16,10 +16,9 @@ final class Cachify_APC { /** * Availability check * - * @since 2.0.7 - * @change 2.0.7 + * @return bool TRUE when installed * - * @return boolean true/false TRUE when installed + * @since 2.0.7 */ public static function is_available() { return extension_loaded( 'apc' ); @@ -28,10 +27,9 @@ public static function is_available() { /** * Caching method as string * - * @since 2.1.2 - * @change 2.1.2 + * @return string Caching method * - * @return string Caching method + * @since 2.1.2 */ public static function stringify_method() { return 'APC'; @@ -40,13 +38,13 @@ public static function stringify_method() { /** * Store item in cache * - * @since 2.0 - * @change 2.3.0 + * @param string $hash Hash of the entry. + * @param string $data Content of the entry. + * @param int $lifetime Lifetime of the entry. + * @param bool $sig_detail Show details in signature. * - * @param string $hash Hash of the entry. - * @param string $data Content of the entry. - * @param integer $lifetime Lifetime of the entry. - * @param bool $sig_detail Show details in signature. + * @since 2.0 + * @since 2.3.0 added $sigDetail parameter */ public static function store_item( $hash, $data, $lifetime, $sig_detail ) { /* Do not store empty data. */ @@ -66,11 +64,11 @@ public static function store_item( $hash, $data, $lifetime, $sig_detail ) { /** * Read item from cache * - * @since 2.0 - * @change 2.0 + * @param string $hash Hash of the entry. * - * @param string $hash Hash of the entry. - * @return mixed Content of the entry + * @return mixed Content of the entry + * + * @since 2.0 */ public static function get_item( $hash ) { return ( function_exists( 'apc_exists' ) ? apc_exists( $hash ) : apc_fetch( $hash ) ); @@ -79,11 +77,10 @@ public static function get_item( $hash ) { /** * Delete item from cache * - * @since 2.0 - * @change 2.0 + * @param string $hash Hash of the entry. + * @param string $url URL of the entry [optional]. * - * @param string $hash Hash of the entry. - * @param string $url URL of the entry [optional]. + * @since 2.0 */ public static function delete_item( $hash, $url = '' ) { apc_delete( $hash ); @@ -92,8 +89,7 @@ public static function delete_item( $hash, $url = '' ) { /** * Clear the cache * - * @since 2.0.0 - * @change 2.0.7 + * @since 2.0 */ public static function clear_cache() { if ( ! self::is_available() ) { @@ -106,8 +102,7 @@ public static function clear_cache() { /** * Print the cache * - * @since 2.0 - * @change 2.0 + * @since 2.0 */ public static function print_cache() { return; @@ -116,10 +111,9 @@ public static function print_cache() { /** * Get the cache size * - * @since 2.0 - * @change 2.0 + * @return mixed Cache size * - * @return mixed Cache size + * @since 2.0 */ public static function get_stats() { /* Info */ @@ -136,11 +130,12 @@ public static function get_stats() { /** * Generate signature * - * @since 2.0 - * @change 2.3.0 + * @param bool $detail Show details in signature. + * + * @return string Signature string * - * @param bool $detail Show details in signature. - * @return string Signature string + * @since 2.0 + * @since 2.3.0 added $detail parameter */ private static function _cache_signature( $detail ) { return sprintf( diff --git a/inc/class-cachify-cli.php b/inc/class-cachify-cli.php index 7317f191..4dc81d4c 100644 --- a/inc/class-cachify-cli.php +++ b/inc/class-cachify-cli.php @@ -16,11 +16,10 @@ final class Cachify_CLI { /** * Flush Cache Callback * - * @since 2.3.0 - * @change 2.3.0 - * * @param array $args the CLI arguments as array. * @param array $assoc_args the CLI arguments as associative array. + * + * @since 2.3.0 */ public static function flush_cache( $args, $assoc_args ) { // Set default arguments. @@ -38,11 +37,10 @@ public static function flush_cache( $args, $assoc_args ) { /** * Get cache size * - * @since 2.3.0 - * @change 2.3.0 - * * @param array $args the CLI arguments as array. * @param array $assoc_args the CLI arguments as associative array. + * + * @since 2.3.0 */ public static function get_cache_size( $args, $assoc_args ) { // Set default arguments. @@ -62,8 +60,7 @@ public static function get_cache_size( $args, $assoc_args ) { /** * Register CLI Commands * - * @since 2.3.0 - * @change 2.3.0 + * @since 2.3.0 */ public static function add_commands() { // Add flush command. diff --git a/inc/class-cachify-db.php b/inc/class-cachify-db.php index 86011462..003c0394 100644 --- a/inc/class-cachify-db.php +++ b/inc/class-cachify-db.php @@ -16,10 +16,9 @@ final class Cachify_DB { /** * Availability check * - * @since 2.0.7 - * @change 2.0.7 + * @return bool TRUE when installed * - * @return boolean true/false TRUE when installed + * @since 2.0.7 */ public static function is_available() { return true; @@ -28,10 +27,9 @@ public static function is_available() { /** * Caching method as string * - * @since 2.1.2 - * @change 2.1.2 + * @return string Caching method * - * @return string Caching method + * @since 2.1.2 */ public static function stringify_method() { return 'DB'; @@ -40,17 +38,17 @@ public static function stringify_method() { /** * Store item in cache * - * @since 2.0 - * @change 2.0 + * @param string $hash Hash of the entry. + * @param string $data Content of the entry. + * @param int $lifetime Lifetime of the entry. * - * @param string $hash Hash of the entry. - * @param string $data Content of the entry. - * @param integer $lifetime Lifetime of the entry. + * @since 2.0 */ public static function store_item( $hash, $data, $lifetime ) { /* Do not store empty data. */ if ( empty( $data ) ) { trigger_error( __METHOD__ . ': Empty input.', E_USER_WARNING ); + return; } @@ -73,11 +71,11 @@ public static function store_item( $hash, $data, $lifetime ) { /** * Read item from cache * - * @since 2.0 - * @change 2.0 + * @param string $hash Hash of the entry. + * + * @return mixed Content of the entry * - * @param string $hash Hash of the entry. - * @return mixed Content of the entry + * @since 2.0 */ public static function get_item( $hash ) { return get_transient( $hash ); @@ -86,11 +84,10 @@ public static function get_item( $hash ) { /** * Delete item from cache * - * @since 2.0 - * @change 2.0 + * @param string $hash Hash of the entry. + * @param string $url URL of the entry [optional]. * - * @param string $hash Hash of the entry. - * @param string $url URL of the entry [optional]. + * @since 2.0 */ public static function delete_item( $hash, $url = '' ) { delete_transient( $hash ); @@ -99,8 +96,7 @@ public static function delete_item( $hash, $url = '' ) { /** * Clear the cache * - * @since 2.0 - * @change 2.0 + * @since 2.0 */ public static function clear_cache() { /* Init */ @@ -113,11 +109,11 @@ public static function clear_cache() { /** * Print the cache * - * @since 2.0 - * @change 2.3.0 + * @param bool $sig_detail Show details in signature. + * @param array $cache Array of cache values. * - * @param bool $sig_detail Show details in signature. - * @param array $cache Array of cache values. + * @since 2.0 + * @since 2.3.0 added $sig_detail parameter */ public static function print_cache( $sig_detail, $cache ) { /* No array? */ @@ -141,16 +137,16 @@ public static function print_cache( $sig_detail, $cache ) { /** * Get the cache size * - * @since 2.0 - * @change 2.0 + * @return int Column size * - * @return integer Column size + * @since 2.0 */ public static function get_stats() { /* Init */ global $wpdb; /* Read */ + return $wpdb->get_var( 'SELECT SUM( CHAR_LENGTH(option_value) ) FROM `' . $wpdb->options . "` WHERE `option_name` LIKE ('\_transient%.cachify')" ); @@ -159,12 +155,13 @@ public static function get_stats() { /** * Generate signature * - * @since 2.0 - * @change 2.3.0 + * @param bool $detail Show details in signature. + * @param array $meta Content of metadata. + * + * @return string Signature string * - * @param bool $detail Show details in signature. - * @param array $meta Content of metadata. - * @return string Signature string + * @since 2.0 + * @since 2.3.0 added $detail parameter */ private static function _cache_signature( $detail, $meta ) { /* No array? */ @@ -210,10 +207,9 @@ private static function _cache_signature( $detail, $meta ) { /** * Return query count * - * @since 0.1 - * @change 2.0 + * @return int Number of queries * - * @return integer Number of queries + * @since 0.1 */ private static function _page_queries() { return $GLOBALS['wpdb']->num_queries; @@ -222,10 +218,9 @@ private static function _page_queries() { /** * Return execution time * - * @since 0.1 - * @change 2.0 + * @return int Execution time in seconds * - * @return integer Execution time in seconds + * @since 0.1 */ private static function _page_timer() { return timer_stop( 0, 2 ); @@ -234,10 +229,9 @@ private static function _page_timer() { /** * Return memory consumption * - * @since 0.7 - * @change 2.0 + * @return string Formatted memory size * - * @return string Formatted memory size + * @since 0.7 */ private static function _page_memory() { return ( function_exists( 'memory_get_usage' ) ? size_format( memory_get_usage(), 2 ) : 0 ); diff --git a/inc/class-cachify-hdd.php b/inc/class-cachify-hdd.php index f971baa6..8d521a1e 100644 --- a/inc/class-cachify-hdd.php +++ b/inc/class-cachify-hdd.php @@ -16,10 +16,9 @@ final class Cachify_HDD { /** * Availability check * - * @since 2.0.7 - * @change 2.0.7 + * @return bool TRUE when installed * - * @return boolean true/false TRUE when installed + * @since 2.0.7 */ public static function is_available() { $option = get_option( 'permalink_structure' ); @@ -29,9 +28,9 @@ public static function is_available() { /** * Returns if gzip file creation is enabled * - * @since 2.4.0 - * * @return bool + * + * @since 2.4.0 */ public static function is_gzip_enabled() { /** @@ -45,10 +44,9 @@ public static function is_gzip_enabled() { /** * Caching method as string * - * @since 2.1.2 - * @change 2.1.2 + * @return string Caching method * - * @return string Caching method + * @since 2.1.2 */ public static function stringify_method() { return 'HDD'; @@ -57,13 +55,13 @@ public static function stringify_method() { /** * Store item in cache * - * @since 2.0 - * @change 2.3.0 + * @param string $hash Hash of the entry [ignored]. + * @param string $data Content of the entry. + * @param int $lifetime Lifetime of the entry [ignored]. + * @param bool $sig_detail Show details in signature. * - * @param string $hash Hash of the entry [ignored]. - * @param string $data Content of the entry. - * @param integer $lifetime Lifetime of the entry [ignored]. - * @param bool $sig_detail Show details in signature. + * @since 2.0 + * @since 2.3.0 added $sig_details parameter */ public static function store_item( $hash, $data, $lifetime, $sig_detail ) { /* Do not store empty data. */ @@ -81,10 +79,9 @@ public static function store_item( $hash, $data, $lifetime, $sig_detail ) { /** * Read item from cache * - * @since 2.0 - * @change 2.0 + * @return bool True if cache is present. * - * @return boolean True if cache is present. + * @since 2.0 */ public static function get_item() { return is_readable( @@ -95,11 +92,10 @@ public static function get_item() { /** * Delete item from cache * - * @since 2.0 - * @change 2.0 + * @param string $hash Hash of the entry [ignored]. + * @param string $url URL of the entry. * - * @param string $hash Hash of the entry [ignored]. - * @param string $url URL of the entry. + * @since 2.0 */ public static function delete_item( $hash, $url ) { self::_clear_dir( @@ -110,8 +106,7 @@ public static function delete_item( $hash, $url ) { /** * Clear the cache * - * @since 2.0 - * @change 2.0 + * @since 2.0 */ public static function clear_cache() { self::_clear_dir( @@ -123,8 +118,7 @@ public static function clear_cache() { /** * Print the cache * - * @since 2.0 - * @change 2.3 + * @since 2.0 */ public static function print_cache() { $filename = self::_file_html(); @@ -138,10 +132,9 @@ public static function print_cache() { /** * Get the cache size * - * @since 2.0 - * @change 2.0 + * @return int Directory size * - * @return integer Directory size + * @since 2.0 */ public static function get_stats() { return self::_dir_size( CACHIFY_CACHE_DIR ); @@ -150,11 +143,12 @@ public static function get_stats() { /** * Generate signature * - * @since 2.0 - * @change 2.3.0 + * @param bool $detail Show details in signature. * - * @param bool $detail Show details in signature. - * @return string Signature string + * @return string Signature string + * + * @since 2.0 + * @since 2.3.0 added $detail parameter */ private static function _cache_signature( $detail ) { return sprintf( @@ -171,10 +165,9 @@ private static function _cache_signature( $detail ) { /** * Initialize caching process * - * @since 2.0 - * @change 2.0 + * @param string $data Cache content. * - * @param string $data Cache content. + * @since 2.0 */ private static function _create_files( $data ) { $file_path = self::_file_path(); @@ -200,11 +193,10 @@ private static function _create_files( $data ) { /** * Create cache file * - * @since 2.0 - * @change 2.0 + * @param string $file Path to cache file. + * @param string $data Cache content. * - * @param string $file Path to cache file. - * @param string $data Cache content. + * @since 2.0 */ private static function _create_file( $file, $data ) { /* Writable? */ @@ -230,11 +222,10 @@ private static function _create_file( $file, $data ) { /** * Clear directory * - * @since 2.0 - * @change 2.0.5 + * @param string $dir Directory path. + * @param bool $recursive true for clearing subdirectories as well. * - * @param string $dir Directory path. - * @param boolean $recursive true for clearing subdirectories as well. + * @since 2.0 */ private static function _clear_dir( $dir, $recursive = false ) { /* Remote training slash */ @@ -285,11 +276,11 @@ private static function _clear_dir( $dir, $recursive = false ) { /** * Get directory size * - * @since 2.0 - * @change 2.0 + * @param string $dir Directory path. + * + * @return mixed Directory size * - * @param string $dir Directory path. - * @return mixed Directory size + * @since 2.0 */ public static function _dir_size( $dir = '.' ) { /* Is directory? */ @@ -330,11 +321,11 @@ public static function _dir_size( $dir = '.' ) { /** * Path to cache file * - * @since 2.0 - * @change 2.0 + * @param string $path Request URI or permalink [optional]. * - * @param string $path Request URI or permalink [optional]. - * @return string Path to cache file + * @return string Path to cache file + * + * @since 2.0 */ private static function _file_path( $path = null ) { $prefix = is_ssl() ? 'https-' : ''; @@ -362,11 +353,11 @@ private static function _file_path( $path = null ) { /** * Path to HTML file * - * @since 2.0 - * @change 2.3.0 + * @param string $file_path File path [optional]. + * + * @return string Path to HTML file * - * @param string $file_path File path [optional]. - * @return string Path to HTML file + * @since 2.0 */ private static function _file_html( $file_path = '' ) { return ( empty( $file_path ) ? self::_file_path() : $file_path ) . 'index.html'; @@ -375,11 +366,11 @@ private static function _file_html( $file_path = '' ) { /** * Path to GZIP file * - * @since 2.0 - * @change 2.3.0 + * @param string $file_path File path [optional]. * - * @param string $file_path File path [optional]. - * @return string Path to GZIP file + * @return string Path to GZIP file + * + * @since 2.0 */ private static function _file_gzip( $file_path = '' ) { return ( empty( $file_path ) ? self::_file_path() : $file_path ) . 'index.html.gz'; @@ -447,6 +438,5 @@ private static function _user_can_delete( $file ) { } return true; - } } diff --git a/inc/class-cachify-memcached.php b/inc/class-cachify-memcached.php index 27cfb013..688fbe5f 100644 --- a/inc/class-cachify-memcached.php +++ b/inc/class-cachify-memcached.php @@ -16,18 +16,18 @@ final class Cachify_MEMCACHED { /** * Memcached-Object * - * @since 2.0.7 - * @var object + * @var object + * + * @since 2.0.7 */ private static $_memcached; /** * Availability check * - * @since 2.0.7 - * @change 2.0.7 + * @return bool TRUE when installed * - * @return boolean true/false TRUE when installed + * @since 2.0.7 */ public static function is_available() { return class_exists( 'Memcached' ) @@ -38,10 +38,9 @@ public static function is_available() { /** * Caching method as string * - * @since 2.1.2 - * @change 2.1.2 + * @return string Caching method * - * @return string Caching method + * @since 2.1.2 */ public static function stringify_method() { return 'Memcached'; @@ -50,13 +49,13 @@ public static function stringify_method() { /** * Store item in cache * - * @param string $hash Hash of the entry [ignored]. - * @param string $data Content of the entry. - * @param integer $lifetime Lifetime of the entry. - * @param bool $sig_detail Show details in signature. + * @param string $hash Hash of the entry [ignored]. + * @param string $data Content of the entry. + * @param int $lifetime Lifetime of the entry. + * @param bool $sig_detail Show details in signature. * - * @since 2.0.7 - * @change 2.3.0 + * @since 2.0.7 + * @since 2.3.0 added $sig_detail parameter */ public static function store_item( $hash, $data, $lifetime, $sig_detail ) { /* Do not store empty data. */ @@ -81,11 +80,11 @@ public static function store_item( $hash, $data, $lifetime, $sig_detail ) { /** * Read item from cache * - * @since 2.0.7 - * @change 2.0.7 + * @param string $hash Hash of the entry. + * + * @return mixed Content of the entry * - * @param string $hash Hash of the entry. - * @return mixed Content of the entry + * @since 2.0.7 */ public static function get_item( $hash ) { /* Server connect */ @@ -102,11 +101,10 @@ public static function get_item( $hash ) { /** * Delete item from cache * - * @since 2.0.7 - * @change 2.0.7 + * @param string $hash Hash of the entry. + * @param string $url URL of the entry [optional]. * - * @param string $hash Hash of the entry. - * @param string $url URL of the entry [optional]. + * @since 2.0.7 */ public static function delete_item( $hash, $url = '' ) { /* Server connect */ @@ -123,8 +121,7 @@ public static function delete_item( $hash, $url = '' ) { /** * Clear the cache * - * @since 2.0.7 - * @change 2.0.7 + * @since 2.0.7 */ public static function clear_cache() { /* Server connect */ @@ -143,8 +140,7 @@ public static function clear_cache() { /** * Print the cache * - * @since 2.0.7 - * @change 2.0.7 + * @since 2.0.7 */ public static function print_cache() { return; @@ -153,10 +149,9 @@ public static function print_cache() { /** * Get the cache size * - * @since 2.0.7 - * @change 2.0.7 + * @return mixed Cache size * - * @return mixed Cache size + * @since 2.0.7 */ public static function get_stats() { /* Server connect */ @@ -186,11 +181,12 @@ public static function get_stats() { /** * Generate signature * - * @since 2.0.7 - * @change 2.3.0 + * @param bool $detail Show details in signature. * - * @param bool $detail Show details in signature. - * @return string Signature string + * @return string Signature string + * + * @since 2.0.7 + * @since 2.3.0 added $detail parameter */ private static function _cache_signature( $detail ) { return sprintf( @@ -207,11 +203,11 @@ private static function _cache_signature( $detail ) { /** * Path of cache file * - * @since 2.0.7 - * @change 2.0.7 + * @param string $path Request URI or permalink [optional]. + * + * @return string Path to cache file * - * @param string $path Request URI or permalink [optional]. - * @return string Path to cache file + * @since 2.0.7 */ private static function _file_path( $path = null ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.InputNotValidated @@ -230,12 +226,11 @@ private static function _file_path( $path = null ) { /** * Connect to Memcached server * - * @since 2.0.7 - * @change 2.1.8 + * @hook array cachify_memcached_servers Array with memcached servers * - * @hook array cachify_memcached_servers Array with memcached servers + * @return bool TRUE on success * - * @return boolean true/false TRUE on success + * @since 2.0.7 */ private static function _connect_server() { /* Not enabled? */ diff --git a/inc/class-cachify.php b/inc/class-cachify.php index 748ad2f4..4a91d1af 100644 --- a/inc/class-cachify.php +++ b/inc/class-cachify.php @@ -16,32 +16,36 @@ final class Cachify { /** * Plugin options * - * @since 2.0 - * @var array + * @var array + * + * @since 2.0 */ private static $options; /** * Caching method * - * @since 2.0 - * @var object + * @var object + * + * @since 2.0 */ private static $method; /** - * Whether we are on an Nginx server or not. + * Whether we are on a Nginx server or not. + * + * @var bool * * @since 2.2.5 - * @var boolean */ private static $is_nginx; /** * Method settings * - * @since 2.0.9 - * @var integer + * @var int + * + * @since 2.0.9 */ const METHOD_DB = 0; const METHOD_APC = 1; @@ -51,8 +55,9 @@ final class Cachify { /** * Minify settings * - * @since 2.0.9 - * @var integer + * @var int + * + * @since 2.0.9 */ const MINIFY_DISABLED = 0; const MINIFY_HTML_ONLY = 1; @@ -61,7 +66,7 @@ final class Cachify { /** * REST endpoints * - * @var string + * @var string */ const REST_NAMESPACE = 'cachify/v1'; const REST_ROUTE_FLUSH = 'flush'; @@ -69,8 +74,7 @@ final class Cachify { /** * Pseudo constructor * - * @since 2.0.5 - * @change 2.0.5 + * @since 2.0.5 */ public static function instance() { new self(); @@ -79,10 +83,7 @@ public static function instance() { /** * Constructor * - * @since 1.0.0 - * @change 2.2.2 - * - * @return void + * @since 1.0 */ public function __construct() { /* Set defaults */ @@ -177,8 +178,7 @@ public function __construct() { /** * Deactivation hook * - * @since 2.1.0 - * @change 2.1.0 + * @since 2.1.0 */ public static function on_deactivation() { /* Remove hdd cache cron when hdd is selected */ @@ -195,8 +195,7 @@ public static function on_deactivation() { /** * Activation hook * - * @since 1.0 - * @change 2.1.0 + * @since 1.0 */ public static function on_activation() { /* Multisite & Network */ @@ -221,10 +220,10 @@ public static function on_activation() { /** * Plugin installation on new WPMS site. * - * @since 1.0 - * @since 2.4 supports WP_Site argument - * * @param int|WP_Site $new_site New site ID or object. + * + * @since 1.0 + * @since 2.4.0 supports WP_Site argument */ public static function install_later( $new_site ) { /* No network plugin */ @@ -245,8 +244,7 @@ public static function install_later( $new_site ) { /** * Actual installation of the options * - * @since 1.0 - * @change 2.0 + * @since 1.0 */ private static function _install_backend() { add_option( @@ -261,8 +259,7 @@ private static function _install_backend() { /** * Uninstalling of the plugin per MU blog. * - * @since 1.0 - * @change 2.1.0 + * @since 1.0 */ public static function on_uninstall() { /* Global */ @@ -292,10 +289,10 @@ public static function on_uninstall() { /** * Uninstalling of the plugin for WPMS site. * - * @since 1.0 - * @since 2.4 supports WP_Site argument - * * @param int|WP_Site $old_site Old site ID or object. + * + * @since 1.0 + * @since 2.4.0 supports WP_Site argument */ public static function uninstall_later( $old_site ) { /* No network plugin */ @@ -316,8 +313,7 @@ public static function uninstall_later( $old_site ) { /** * Actual uninstalling of the plugin * - * @since 1.0 - * @change 1.0 + * @since 1.0 */ private static function _uninstall_backend() { /* Option */ @@ -330,10 +326,9 @@ private static function _uninstall_backend() { /** * Get IDs of installed blogs * - * @since 1.0 - * @change 1.0 + * @return array Blog IDs * - * @return array Blog IDs + * @since 1.0 */ private static function _get_blog_ids() { /* Global */ @@ -384,8 +379,7 @@ public static function register_scripts() { /** * Register the language file * - * @since 2.1.3 - * @change 2.3.2 + * @since 2.1.3 */ public static function register_textdomain() { load_plugin_textdomain( 'cachify' ); @@ -394,8 +388,7 @@ public static function register_textdomain() { /** * Set default options * - * @since 2.0 - * @change 2.0.7 + * @since 2.0 */ private static function _set_default_vars() { /* Options */ @@ -422,10 +415,9 @@ private static function _set_default_vars() { /** * Get options * - * @since 2.0 - * @change 2.3.0 + * @return array Array of option values * - * @return array Array of option values + * @since 2.0 */ private static function _get_options() { return wp_parse_args( @@ -449,7 +441,7 @@ private static function _get_options() { * * @since 1.0 * @since 2.1.9 - * @since 2.4 Removed $data parameter and return value. + * @since 2.4.0 Removed $data parameter and return value. */ public static function robots_txt() { /* HDD only */ @@ -461,7 +453,7 @@ public static function robots_txt() { /** * HDD Cache expiration cron action. * - * @since 2.4 + * @since 2.4.0 */ public static function run_hdd_cache_cron() { Cachify_HDD::clear_cache(); @@ -474,7 +466,7 @@ public static function run_hdd_cache_cron() { * * @return array Array of non-default schedules with our tasks added. * - * @since 2.4 + * @since 2.4.0 */ public static function add_cron_cache_expiration( $schedules ) { $schedules['cachify_cache_expire'] = array( @@ -487,11 +479,11 @@ public static function add_cron_cache_expiration( $schedules ) { /** * Add the action links * - * @since 1.0 - * @change 1.0 + * @param array $data Initial array with action links. * - * @param array $data Initial array with action links. - * @return array Merged array with action links. + * @return array Merged array with action links. + * + * @since 1.0 */ public static function action_links( $data ) { /* Permissions? */ @@ -519,12 +511,12 @@ public static function action_links( $data ) { /** * Meta links of the plugin * - * @since 0.5 - * @change 2.0.5 + * @param array $input Initial array with meta links. + * @param string $page Current page. * - * @param array $input Initial array with meta links. - * @param string $page Current page. - * @return array Merged array with meta links. + * @return array Merged array with meta links. + * + * @since 0.5 */ public static function row_meta( $input, $page ) { /* Permissions */ @@ -544,11 +536,11 @@ public static function row_meta( $input, $page ) { /** * Add cache properties to dashboard * - * @since 2.0.0 - * @change 2.2.2 + * @param array $items Initial array with dashboard items. + * + * @return array Merged array with dashboard items. * - * @param array $items Initial array with dashboard items. - * @return array Merged array with dashboard items. + * @since 2.0.0 */ public static function add_dashboard_count( $items = array() ) { /* Skip */ @@ -601,10 +593,9 @@ public static function add_dashboard_count( $items = array() ) { /** * Get the cache size * - * @since 2.0.6 - * @change 2.0.6 + * @return int Cache size in bytes. * - * @return integer Cache size in bytes. + * @since 2.0.6 */ public static function get_cache_size() { $size = get_transient( 'cachify_cache_size' ); @@ -631,13 +622,13 @@ public static function get_cache_size() { /** * Add flush icon to admin bar menu * - * @since 1.2 - * @change 2.2.2 - * @change 2.4.0 Adjust icon for flush request via AJAX + * @hook mixed cachify_user_can_flush_cache * - * @hook mixed cachify_user_can_flush_cache + * @param object $wp_admin_bar Object of menu items. * - * @param object $wp_admin_bar Object of menu items. + * @since 1.2 + * @since 2.2.2 + * @since 2.4.0 Adjust icon for flush request via AJAX */ public static function add_flush_icon( $wp_admin_bar ) { /* Quit */ @@ -679,9 +670,9 @@ public static function add_flush_icon( $wp_admin_bar ) { /** * Returns the dashicon class for the success state in admin bar flush button * - * @since 2.4.0 - * * @return string + * + * @since 2.4.0 */ public static function get_dashicon_success_class() { global $wp_version; @@ -695,11 +686,11 @@ public static function get_dashicon_success_class() { /** * Add a script to query the REST endpoint and animate the flush icon in admin bar menu * - * @since 2.4.0 + * @hook mixed cachify_user_can_flush_cache ? * - * @hook mixed cachify_user_can_flush_cache ? + * @param object $wp_admin_bar Object of menu items. * - * @param object $wp_admin_bar Object of menu items. + * @since 2.4.0 */ public static function add_flush_icon_script( $wp_admin_bar ) { /* Quit */ @@ -728,7 +719,7 @@ public static function add_flush_icon_script( $wp_admin_bar ) { /** * Registers an REST endpoint for the flush operation * - * @change 2.4.0 + * @since 2.4.0 */ public static function add_flush_rest_endpoint() { register_rest_route( @@ -751,9 +742,9 @@ public static function add_flush_rest_endpoint() { /** * Check if user can manage options * - * @since 2.4.0 + * @return bool * - * @return bool + * @since 2.4.0 */ public static function user_can_manage_options() { return current_user_can( 'manage_options' ); @@ -762,13 +753,13 @@ public static function user_can_manage_options() { /** * Process plugin's meta actions * - * @since 0.5 - * @change 2.2.2 - * @change 2.4.0 Extract cache flushing to own method and always redirect to referer with new value for `_cachify` param. - * * @hook mixed cachify_user_can_flush_cache * - * @param array $data Metadata of the plugin. + * @param array $data Metadata of the plugin. + * + * @since 0.5 + * @since 2.2.2 + * @since 2.4.0 Extract cache flushing to own method and always redirect to referer with new value for `_cachify` param. */ public static function process_flush_request( $data ) { /* Skip if not a flush request */ @@ -865,10 +856,10 @@ public static function flush_cache() { /** * Notice after successful flushing of the cache * - * @since 1.2 - * @change 2.2.2 + * @hook mixed cachify_user_can_flush_cache * - * @hook mixed cachify_user_can_flush_cache + * @since 1.2 + * @since 2.2.2 */ public static function flush_notice() { /* No admin */ @@ -885,10 +876,10 @@ public static function flush_notice() { /** * Remove page from cache or flush on comment edit * - * @since 0.1.0 - * @change 2.1.2 + * @param int $id Comment ID. * - * @param integer $id Comment ID. + * @since 0.1.0 + * @since 2.1.2 */ public static function edit_comment( $id ) { if ( self::$options['reset_on_comment'] ) { @@ -903,12 +894,13 @@ public static function edit_comment( $id ) { /** * Remove page from cache or flush on new comment * - * @since 0.1.0 - * @change 2.1.2 + * @param mixed $approved Comment status. + * @param array $comment Array of properties. + * + * @return mixed Comment status. * - * @param mixed $approved Comment status. - * @param array $comment Array of properties. - * @return mixed Comment status. + * @since 0.1 + * @since 2.1.2 */ public static function pre_comment( $approved, $comment ) { /* Approved comment? */ @@ -926,12 +918,12 @@ public static function pre_comment( $approved, $comment ) { /** * Remove page from cache or flush on comment edit * - * @since 0.1 - * @change 2.1.2 + * @param string $new_status New status. + * @param string $old_status Old status. + * @param object $comment The comment. * - * @param string $new_status New status. - * @param string $old_status Old status. - * @param object $comment The comment. + * @since 0.1 + * @since 2.1.2 */ public static function touch_comment( $new_status, $old_status, $comment ) { if ( $new_status !== $old_status ) { @@ -946,8 +938,8 @@ public static function touch_comment( $new_status, $old_status, $comment ) { /** * Generate publish hook for custom post types * - * @since 2.1.7 Make the function public - * @since 2.0.3 + * @since 2.0.3 + * @since 2.1.7 Make the function public * * @deprecated no longer used since 2.4 */ @@ -974,11 +966,10 @@ public static function register_publish_hooks() { /** * Removes the post type cache on post updates * - * @since 2.0.3 - * @change 2.3.0 + * @param int $post_id Post ID. + * @param object $post Post object. * - * @param integer $post_id Post ID. - * @param object $post Post object. + * @since 2.0.3 * * @deprecated no longer used since 2.4 */ @@ -1009,11 +1000,11 @@ public static function publish_post_types( $post_id, $post ) { /** * Removes the post type cache if saved or updated * + * @param int $id Post ID. + * * @since 2.0.3 * @since 2.1.7 Make the function public. - * @since 2.4 Renamed to save_update_trash_post with $id parameter. - * - * @param integer $id Post ID. + * @since 2.4.0 Renamed to save_update_trash_post with $id parameter. */ public static function save_update_trash_post( $id ) { $status = get_post_status( $id ); @@ -1027,12 +1018,12 @@ public static function save_update_trash_post( $id ) { /** * Removes the post type cache before an existing post type is updated in the db * + * @param int $id Post ID. + * @param array $data Post data. + * * @since 2.0.3 * @since 2.3.0 - * @since 2.4 Renamed to post_update. - * - * @param integer $id Post ID. - * @param array $data Post data. + * @since 2.4.0 Renamed to post_update. */ public static function post_update( $id, $data ) { $new_status = $data['post_status']; @@ -1047,9 +1038,9 @@ public static function post_update( $id, $data ) { /** * Clear cache when any post type has been created or updated * - * @since 2.4 + * @param int|WP_Post $post Post ID or object. * - * @param integer|WP_Post $post Post ID or object. + * @since 2.4.0 */ public static function flush_cache_for_posts( $post ) { if ( is_int( $post ) ) { @@ -1076,9 +1067,9 @@ public static function flush_cache_for_posts( $post ) { /** * Flush post cache on WooCommerce stock changes. * - * @since 2.4 - * * @param int|WC_Product $product Product ID or object. + * + * @since 2.4.0 */ public static function flush_woocommerce( $product ) { if ( is_int( $product ) ) { @@ -1093,10 +1084,9 @@ public static function flush_woocommerce( $product ) { /** * Removes a page (id) from cache * - * @since 2.0.3 - * @change 2.1.3 + * @param int $post_id Post ID. * - * @param integer $post_id Post ID. + * @since 2.0.3 */ public static function remove_page_cache_by_post_id( $post_id ) { $post_id = (int) $post_id; @@ -1110,10 +1100,9 @@ public static function remove_page_cache_by_post_id( $post_id ) { /** * Removes a page url from cache * - * @since 0.1 - * @change 2.1.3 + * @param string $url Page URL. * - * @param string $url Page URL. + * @since 0.1 */ public static function remove_page_cache_by_url( $url ) { $url = (string) $url; @@ -1134,10 +1123,9 @@ public static function remove_page_cache_by_url( $url ) { /** * Get cache validity * - * @since 2.0.0 - * @change 2.1.7 + * @return int Validity period in seconds. * - * @return integer Validity period in seconds. + * @since 2.0.0 */ private static function _cache_expires() { return HOUR_IN_SECONDS * self::$options['cache_expires']; @@ -1146,9 +1134,9 @@ private static function _cache_expires() { /** * Determine if cache details should be printed in signature * - * @since 2.3.0 + * @return bool Show details in signature. * - * @return bool Show details in signature. + * @since 2.3.0 */ private static function _signature_details() { return 1 === self::$options['sig_detail']; @@ -1157,12 +1145,12 @@ private static function _signature_details() { /** * Get hash value for caching * - * @since 0.1 - * @change 2.0 - * @change 2.4.0 Fix issue with port in URL. + * @param string $url URL to hash [optional]. + * + * @return string Cachify hash value. * - * @param string $url URL to hash [optional]. - * @return string Cachify hash value. + * @since 0.1 + * @since 2.0 */ private static function _cache_hash( $url = '' ) { $prefix = is_ssl() ? 'https-' : ''; @@ -1180,11 +1168,12 @@ private static function _cache_hash( $url = '' ) { /** * Split by comma * - * @since 0.9.1 - * @change 1.0 + * @param string $input String to split. * - * @param string $input String to split. - * @return array Splitted values. + * @return array Splitted values. + * + * @since 0.9.1 + * @since 1.0 */ private static function _preg_split( $input ) { return (array) preg_split( '/,/', $input, -1, PREG_SPLIT_NO_EMPTY ); @@ -1193,10 +1182,9 @@ private static function _preg_split( $input ) { /** * Check for index page * - * @since 0.6 - * @change 1.0 + * @return bool TRUE if index * - * @return boolean TRUE if index + * @since 0.6 */ private static function _is_index() { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.InputNotValidated @@ -1206,10 +1194,9 @@ private static function _is_index() { /** * Check for mobile devices * - * @since 0.9.1 - * @change 2.3.0 + * @return bool TRUE if mobile * - * @return boolean TRUE if mobile + * @since 0.9.1 */ private static function _is_mobile() { $templatedir = get_template_directory(); @@ -1219,10 +1206,9 @@ private static function _is_mobile() { /** * Check if user is logged in or marked * - * @since 2.0.0 - * @change 2.0.5 + * @return bool TRUE on "marked" users * - * @return boolean $diff TRUE on "marked" users + * @since 2.0.0 */ private static function _is_logged_in() { /* Logged in */ @@ -1248,7 +1234,7 @@ private static function _is_logged_in() { /** * Register all hooks to flush the total cache * - * @since 2.4.0 + * @since 2.4.0 */ public static function register_flush_cache_hooks() { /* Define all default flush cache hooks */ @@ -1280,13 +1266,11 @@ public static function register_flush_cache_hooks() { /** * Define exclusions for caching * - * @since 0.2 - * @change 2.3.0 - * @change 2.4.0 Add check for sitemap feature and skip cache for other request methods than GET. + * @hook bool cachify_skip_cache * - * @return boolean TRUE on exclusion + * @return bool TRUE on exclusion * - * @hook boolean cachify_skip_cache + * @since 0.2 */ private static function _skip_cache() { @@ -1361,13 +1345,13 @@ private static function _skip_cache() { /** * Minify HTML code * - * @since 0.9.2 - * @change 2.0.9 + * @hook array cachify_minify_ignore_tags + * + * @param string $data Original HTML code. * - * @param string $data Original HTML code. - * @return string Minified code + * @return string Minified code * - * @hook array cachify_minify_ignore_tags + * @since 0.9.2 */ private static function _minify_cache( $data ) { /* Disabled? */ @@ -1426,11 +1410,10 @@ private static function _minify_cache( $data ) { /** * Flush total cache * - * @since 0.1 - * @change 2.0 - * @change 2.4.0 Do not flush cache for post revisions. + * @param bool $clear_all_methods Flush all caching methods (default: FALSE). * - * @param bool $clear_all_methods Flush all caching methods (default: FALSE). + * @since 0.1 + * @since 2.0 */ public static function flush_total_cache( $clear_all_methods = false ) { // We do not need to flush the cache for saved post revisions. @@ -1466,11 +1449,12 @@ public static function flush_total_cache( $clear_all_methods = false ) { /** * Assign the cache * - * @since 0.1 - * @change 2.0 + * @param string $data Content of the page. + * + * @return string Content of the page. * - * @param string $data Content of the page. - * @return string Content of the page. + * @since 0.1 + * @since 2.0 */ public static function set_cache( $data ) { /* Empty? */ @@ -1481,13 +1465,13 @@ public static function set_cache( $data ) { /** * Filters whether the buffered data should actually be cached * - * @since 2.3 - * * @param bool $should_cache Whether the data should be cached. * @param string $data The actual data. * @param object $method Instance of the selected caching method. * @param string $cache_hash The cache hash. * @param int $cache_expires Cache validity period. + * + * @since 2.3.0 */ $should_cache = apply_filters( 'cachify_store_item', @@ -1503,12 +1487,12 @@ public static function set_cache( $data ) { /** * Filters the buffered data itself * - * @since 2.4 - * * @param string $data The actual data. * @param object $method Instance of the selected caching method. * @param string $cache_hash The cache hash. * @param int $cache_expires Cache validity period. + * + * @since 2.4.0 */ $data = apply_filters( 'cachify_modify_output', $data, self::$method, self::_cache_hash(), self::_cache_expires() ); @@ -1530,8 +1514,7 @@ public static function set_cache( $data ) { /** * Manage the cache. * - * @since 0.1 - * @change 2.3 + * @since 0.1 */ public static function manage_cache() { /* No caching? */ @@ -1568,10 +1551,9 @@ public static function manage_cache() { /** * Register CSS * - * @since 1.0 - * @change 2.3.0 + * @param string $hook Current hook. * - * @param string $hook Current hook. + * @since 1.0 */ public static function add_admin_resources( $hook ) { /* Hooks check */ @@ -1616,8 +1598,7 @@ public static function admin_dashboard_dark_mode_styles() { /** * Add options page * - * @since 1.0 - * @change 2.2.2 + * @since 1.0 */ public static function add_page() { add_options_page( @@ -1635,10 +1616,9 @@ public static function add_page() { /** * Available caching methods * - * @since 2.0.0 - * @change 2.1.3 + * @return array Array of actually available methods. * - * @return array Array of actually available methods. + * @since 2.0 */ private static function _method_select() { /* Defaults */ @@ -1670,10 +1650,9 @@ private static function _method_select() { /** * Minify cache dropdown * - * @since 2.1.3 - * @change 2.1.3 + * @return array Key => value array * - * @return array Key => value array + * @since 2.1.3 */ private static function _minify_select() { return array( @@ -1686,8 +1665,7 @@ private static function _minify_select() { /** * Register settings * - * @since 1.0 - * @change 1.0 + * @since 1.0 */ public static function register_settings() { register_setting( @@ -1703,11 +1681,12 @@ public static function register_settings() { /** * Validate options * - * @since 1.0.0 - * @change 2.1.3 + * @param array $data Array of form values. + * + * @return array Array of validated values. * - * @param array $data Array of form values. - * @return array Array of validated values. + * @since 1.0 + * @since 2.1.3 */ public static function validate_options( $data ) { /* Empty data? */ @@ -1745,8 +1724,7 @@ public static function validate_options( $data ) { /** * Display options page * - * @since 1.0 - * @change 2.3.0 + * @since 1.0 */ public static function options_page() { $options = self::_get_options(); @@ -1795,11 +1773,11 @@ public static function options_page() { /** * Return an array with all settings tabs applicable in context of current plugin options. * - * @since 2.3.0 - * @change 2.3.0 - * * @param array $options the options. + * * @return array + * + * @since 2.3.0 */ private static function _get_tabs( $options ) { /* Settings tab is always present */ From 3d7190f2b0fe09c8f3fa331921f19641a710f0a5 Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Tue, 22 Nov 2022 16:55:19 +0100 Subject: [PATCH 5/9] remove minified js from sources --- .gitignore | 1 + js/admin-bar-flush.min.js | 6 ------ 2 files changed, 1 insertion(+), 6 deletions(-) delete mode 100644 js/admin-bar-flush.min.js diff --git a/.gitignore b/.gitignore index f5833602..47f959ac 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ .idea/ css/*.min.css +js/*.min.js vendor/ node_modules/ composer.lock diff --git a/js/admin-bar-flush.min.js b/js/admin-bar-flush.min.js deleted file mode 100644 index 16d25798..00000000 --- a/js/admin-bar-flush.min.js +++ /dev/null @@ -1,6 +0,0 @@ -(function(){var is_flushing=!1,admin_bar_cachify_list_item=document.getElementById('wp-admin-bar-cachify'),flush_link=admin_bar_cachify_list_item.querySelector('a.ab-item'),fallback_url=flush_link.getAttribute('href'),aria_live_area=document.querySelector('.ab-aria-live-area');var button=document.createRange().createContextualFragment('');flush_link.parentNode.replaceChild(button,flush_link);var admin_bar_icon=admin_bar_cachify_list_item.querySelector('#wp-admin-bar-cachify .ab-icon');document.querySelector('#wp-admin-bar-cachify .ab-item').addEventListener('click',flush);admin_bar_icon.addEventListener('animationend',function(){admin_bar_icon.classList.remove('animate-fade')});function flush_icon_remove_classes(){var classes=['animate-fade','animate-pulse','dashicons-trash','dashicons-yes','dashicons-yes-alt','dashicons-dismiss',];for(var i=0;i Date: Tue, 17 Jan 2023 13:20:19 +0100 Subject: [PATCH 6/9] fix content negotiation (#265) (#273) Add conditions for the HTTP "Accept" header to both cache generation and webserver configuration so that only HTML content is served from cache. --- inc/class-cachify.php | 7 +++++++ inc/setup/cachify.hdd.htaccess.php | 1 + inc/setup/cachify.hdd.nginx.php | 3 +++ inc/setup/cachify.memcached.nginx.php | 5 +++++ 4 files changed, 16 insertions(+) diff --git a/inc/class-cachify.php b/inc/class-cachify.php index 4a91d1af..78da24ea 100644 --- a/inc/class-cachify.php +++ b/inc/class-cachify.php @@ -1339,6 +1339,13 @@ private static function _skip_cache() { return true; } + /* Content Negotiation */ + + // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized + if ( isset( $_SERVER['HTTP_ACCEPT'] ) && false === strpos( $_SERVER['HTTP_ACCEPT'], 'text/html' ) ) { + return true; + } + return false; } diff --git a/inc/setup/cachify.hdd.htaccess.php b/inc/setup/cachify.hdd.htaccess.php index 1f64549a..4ab3335c 100644 --- a/inc/setup/cachify.hdd.htaccess.php +++ b/inc/setup/cachify.hdd.htaccess.php @@ -26,6 +26,7 @@ RewriteRule .* - [E=CACHIFY_DIR:/] {{GZIP}} # Main Rules + RewriteCond %{HTTP_ACCEPT} .*text/html.* RewriteCond %{REQUEST_METHOD} GET RewriteCond %{QUERY_STRING} ="" RewriteCond %{REQUEST_URI} !^/(wp-admin|wp-content/cache)/.* diff --git a/inc/setup/cachify.hdd.nginx.php b/inc/setup/cachify.hdd.nginx.php index 7aaf1f04..38b001c6 100644 --- a/inc/setup/cachify.hdd.nginx.php +++ b/inc/setup/cachify.hdd.nginx.php @@ -24,6 +24,9 @@ if ( $query_string ) { return 405; } + if ( $http_accept !~* "text/html" ) { + return 405; + } if ( $request_method = POST ) { return 405; } diff --git a/inc/setup/cachify.memcached.nginx.php b/inc/setup/cachify.memcached.nginx.php index 2b737b8c..ca3b1518 100644 --- a/inc/setup/cachify.memcached.nginx.php +++ b/inc/setup/cachify.memcached.nginx.php @@ -26,6 +26,11 @@ if ( $query_string ) { return 405; } + + if ( $http_accept !~* "text/html" ) { + return 405; + } + if ( $request_method = POST ) { return 405; } From 9401228e52bcb5fb813f26f5d96f5c398db29dc5 Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Fri, 14 Apr 2023 18:19:39 +0200 Subject: [PATCH 7/9] add user-agent definition to robots.txt (#282) The "Disallow" line may be added outside a previous "User-agent" block or behind another customized definition. To ensure that we generate a valid robots.txt, we now generate a complete block. --- CHANGELOG.md | 2 +- inc/class-cachify.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b36210c..061e671e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ Requires PHP 5.6 and WordPress 4.7 or above * Enhance: adjust styling for setup instructions (#215, props timse201) * Enhance: update hooks for Multisite initialization in WordPress 5.1 and above (#246, props ouun) * Enhance: rework flush hooks and add some third-party triggers for Autoptimize and WooCommerce (#225, props timse201) - +* Fix: correctly add user-agent to robots.txt (#282) (#283) ## 2.3.2 * Fix: enforce WordPress environment for caching modules (#221, props timse201) diff --git a/inc/class-cachify.php b/inc/class-cachify.php index 78da24ea..1f888985 100644 --- a/inc/class-cachify.php +++ b/inc/class-cachify.php @@ -446,7 +446,7 @@ private static function _get_options() { public static function robots_txt() { /* HDD only */ if ( self::METHOD_HDD === self::$options['use_apc'] ) { - echo 'Disallow: */cache/cachify/'; + echo "User-agent: *\nDisallow: */cache/cachify/\n"; } } From a4f634cd3463baccd2ba53c151b116f187f33cf8 Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Fri, 14 Apr 2023 18:30:10 +0200 Subject: [PATCH 8/9] use robots_txt hook instead of do_robots action --- inc/class-cachify.php | 11 +++++++---- tests/test-cachify.php | 29 +++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/inc/class-cachify.php b/inc/class-cachify.php index 1f888985..92eebf01 100644 --- a/inc/class-cachify.php +++ b/inc/class-cachify.php @@ -171,7 +171,7 @@ public function __construct() { } else { /* Frontend */ add_action( 'template_redirect', array( __CLASS__, 'manage_cache' ), 0 ); - add_action( 'do_robots', array( __CLASS__, 'robots_txt' ) ); + add_filter( 'robots_txt', array( __CLASS__, 'robots_txt' ) ); } } @@ -439,15 +439,18 @@ private static function _get_options() { /** * Modify robots.txt * + * @param string $output The robots.txt output. + * * @since 1.0 * @since 2.1.9 - * @since 2.4.0 Removed $data parameter and return value. */ - public static function robots_txt() { + public static function robots_txt( $output ) { /* HDD only */ if ( self::METHOD_HDD === self::$options['use_apc'] ) { - echo "User-agent: *\nDisallow: */cache/cachify/\n"; + $output .= "\nUser-agent: *\nDisallow: */cache/cachify/\n"; } + + return $output; } /** diff --git a/tests/test-cachify.php b/tests/test-cachify.php index f32225eb..052ccf6b 100644 --- a/tests/test-cachify.php +++ b/tests/test-cachify.php @@ -104,4 +104,33 @@ public function test_on_activation() { Cachify::on_activation(); self::assertEquals( array() , get_option( 'cachify' ), 'Cachify option not initialized' ); } + + + /** + * Test hook for robots.txt customization. + */ + public function test_robots_txt() { + // Initial robots.txt content. + $robots_txt = "User-agent: *\nDisallow: /wordpress/wp-admin/\nAllow: /wordpress/wp-admin/admin-ajax.php\n"; + + // DB cache enabled. + update_option( 'cachify' , array( 'use_apc' => Cachify::METHOD_DB ) ); + new Cachify(); + + self::assertEquals( + $robots_txt, + Cachify::robots_txt( $robots_txt ), + 'robots.tst should not be modified using DB cache' + ); + + // HDD cache enabled. + update_option( 'cachify' , array( 'use_apc' => Cachify::METHOD_HDD ) ); + new Cachify(); + + self::assertEquals( + $robots_txt . "\nUser-agent: *\nDisallow: */cache/cachify/\n", + Cachify::robots_txt( $robots_txt ), + 'robots.tst should have been modified using HDD cache' + ); + } } From f166ed8d8bfbee563ce7c068e99ba5602336ca57 Mon Sep 17 00:00:00 2001 From: Raffael Date: Mon, 21 Aug 2023 20:29:19 +0200 Subject: [PATCH 9/9] invalidate cache when permalink changes (#286) Migrate the action on "save_post" to "post_updated" and invalidate the page cache, if the permalink was changed, i.e. remove the leftover cache data for a no longer existing page. --- CHANGELOG.md | 1 + inc/class-cachify.php | 12 +++++++----- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b36210c..f7834bf8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file. This projec Requires PHP 5.6 and WordPress 4.7 or above +* Fix: invalidate cache when permalink changes (#285, #286, props raffaelj) * Enhance: adjust styling for setup instructions (#215, props timse201) * Enhance: update hooks for Multisite initialization in WordPress 5.1 and above (#246, props ouun) * Enhance: rework flush hooks and add some third-party triggers for Autoptimize and WooCommerce (#225, props timse201) diff --git a/inc/class-cachify.php b/inc/class-cachify.php index 78da24ea..a6c73c2e 100644 --- a/inc/class-cachify.php +++ b/inc/class-cachify.php @@ -93,7 +93,7 @@ public function __construct() { /* Flush Hooks */ add_action( 'init', array( __CLASS__, 'register_flush_cache_hooks' ), 10, 0 ); - add_action( 'save_post', array( __CLASS__, 'save_update_trash_post' ) ); + add_action( 'post_updated', array( __CLASS__, 'save_update_trash_post' ), 10, 3 ); add_action( 'pre_post_update', array( __CLASS__, 'post_update' ), 10, 2 ); add_action( 'cachify_remove_post_cache', array( __CLASS__, 'remove_page_cache_by_post_id' ) ); @@ -1000,14 +1000,16 @@ public static function publish_post_types( $post_id, $post ) { /** * Removes the post type cache if saved or updated * - * @param int $id Post ID. + * @param int $id Post ID. + * @param WP_Post $post_after Post object following the update. + * @param WP_Post $post_before Post object before the update. * * @since 2.0.3 * @since 2.1.7 Make the function public. - * @since 2.4.0 Renamed to save_update_trash_post with $id parameter. + * @since 2.4.0 Renamed to save_update_trash_post and introduced parameters. */ - public static function save_update_trash_post( $id ) { - $status = get_post_status( $id ); + public static function save_update_trash_post( $id, $post_after, $post_before ) { + $status = get_post_status( $post_before ); /* Post type published? */ if ( 'publish' === $status ) {