From 19405ad8940b9fa26077f1d8629f6b8f4fad95ca Mon Sep 17 00:00:00 2001 From: philcable Date: Thu, 26 Sep 2019 11:27:55 -0700 Subject: [PATCH 1/8] Store results of `bu_navigation_get_posts` in persistent cache This opts to create a new cache group - `bu-navigation-persistent` - due to the `bu-navigation` group being set as non-persistent (see https://github.com/bu-ist/bu-navigation/blob/master/bu-navigation.php#L86). A copy of core's `last_changed` cache value is stored in the new cache group, and will leveraged for resetting the cache of the query results. --- includes/library.php | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/includes/library.php b/includes/library.php index 991cb85..1e1a787 100644 --- a/includes/library.php +++ b/includes/library.php @@ -428,6 +428,45 @@ function bu_navigation_get_posts( $args = '' ) { ); $r = wp_parse_args( $args, $defaults ); + // Get the `last_changed` key from the core `posts` group. + // This key is updated by core in `clean_post_cache`. + $last_changed = wp_cache_get( 'last_changed', 'posts' ); + + // Get the `posts_last_changed` key for comparison. + $bu_nav_posts_last_changed = wp_cache_get( 'posts_last_changed', 'bu-navigation-persistent' ); + + // If the `last_changed` key has no value, set it using the current time. + if ( ! $last_changed ) { + $last_changed = microtime(); + wp_cache_set( 'last_changed', $last_changed, 'posts' ); + } + + // If the `posts_last_changed` key has no value, set it using the current time. + if ( ! $bu_nav_posts_last_changed ) { + $bu_nav_posts_last_changed = microtime(); + wp_cache_set( 'posts_last_changed', $bu_nav_posts_last_changed, 'bu-navigation-persistent' ); + } + + // Create the key to use for storing the results of the following query. + $cache_key = 'get_posts:' . md5( wp_json_encode( $r ) ); + + // Check if we have the results of the following query in cache. + $posts = wp_cache_get( $cache_key, 'bu-navigation-persistent' ); + + // If the core and BU Navigation last changed values match up, + // and the results of the the following query are already in cache, + // return it now. + if ( $last_changed === $bu_nav_posts_last_changed && $posts ) { + return $posts; + } + + // If the core and BU Navigation last changed values don't match, + // or the results of the the following query are not yet cached, + // set `posts_last_changed` to match core's `last_changed` key value. + if ( $last_changed !== $bu_nav_posts_last_changed ) { + wp_cache_set( 'posts_last_changed', $last_changed, 'bu-navigation-persistent' ); + } + // Start building the query $where = $orderby = ''; @@ -532,6 +571,9 @@ function bu_navigation_get_posts( $args = '' ) { $posts = $items; } + // Cache results. + wp_cache_set( $cache_key, $posts, 'bu-navigation-persistent' ); + return $posts; } From 1c0110b1ba9b30c55945f710da7b225241344eb4 Mon Sep 17 00:00:00 2001 From: philcable Date: Thu, 26 Sep 2019 11:48:17 -0700 Subject: [PATCH 2/8] Reset the `posts_last_changed` when the menu is saved --- admin/manager.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/admin/manager.php b/admin/manager.php index a86db1f..7064f4f 100644 --- a/admin/manager.php +++ b/admin/manager.php @@ -415,6 +415,9 @@ public function save() { // @todo notify user of error messages from WP_Error objects $this->plugin->log( '%s - Errors encountered during navman save: %s', __METHOD__, print_r( $errors, true ) ); } + + $bu_nav_posts_last_changed = microtime(); + wp_cache_set( 'posts_last_changed', $bu_nav_posts_last_changed, 'bu-navigation-persistent' ); } return $saved; From 0d012309682dae68ab199c6c7415910a3e1f02f9 Mon Sep 17 00:00:00 2001 From: philcable Date: Thu, 26 Sep 2019 12:27:35 -0700 Subject: [PATCH 3/8] Shorten variable and cache key names --- admin/manager.php | 5 +++-- includes/library.php | 35 ++++++++++++++++++----------------- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/admin/manager.php b/admin/manager.php index 7064f4f..fb6fa38 100644 --- a/admin/manager.php +++ b/admin/manager.php @@ -416,8 +416,9 @@ public function save() { $this->plugin->log( '%s - Errors encountered during navman save: %s', __METHOD__, print_r( $errors, true ) ); } - $bu_nav_posts_last_changed = microtime(); - wp_cache_set( 'posts_last_changed', $bu_nav_posts_last_changed, 'bu-navigation-persistent' ); + // Update the `last_changed` key value with the current time. + $bu_nav_last_changed = microtime(); + wp_cache_set( 'last_changed', $bu_nav_last_changed, 'bu-navigation-persistent' ); } return $saved; diff --git a/includes/library.php b/includes/library.php index 1e1a787..68e79f0 100644 --- a/includes/library.php +++ b/includes/library.php @@ -428,43 +428,44 @@ function bu_navigation_get_posts( $args = '' ) { ); $r = wp_parse_args( $args, $defaults ); - // Get the `last_changed` key from the core `posts` group. + // Get the `last_changed` key from the WP core `posts`cache group. // This key is updated by core in `clean_post_cache`. $last_changed = wp_cache_get( 'last_changed', 'posts' ); - // Get the `posts_last_changed` key for comparison. - $bu_nav_posts_last_changed = wp_cache_get( 'posts_last_changed', 'bu-navigation-persistent' ); + // Get the `last_changed` key from the `bu-navigation-persistent` cache group. + $bu_nav_last_changed = wp_cache_get( 'last_changed', 'bu-navigation-persistent' ); - // If the `last_changed` key has no value, set it using the current time. + // If WP's `last_changed` key has no value, set it using the current time. if ( ! $last_changed ) { $last_changed = microtime(); wp_cache_set( 'last_changed', $last_changed, 'posts' ); } - // If the `posts_last_changed` key has no value, set it using the current time. - if ( ! $bu_nav_posts_last_changed ) { - $bu_nav_posts_last_changed = microtime(); - wp_cache_set( 'posts_last_changed', $bu_nav_posts_last_changed, 'bu-navigation-persistent' ); + // If BU Navigation's `last_changed` key has no value, + // set it using the current time. + if ( ! $bu_nav_last_changed ) { + $bu_nav_last_changed = microtime(); + wp_cache_set( 'last_changed', $bu_nav_last_changed, 'bu-navigation-persistent' ); } // Create the key to use for storing the results of the following query. $cache_key = 'get_posts:' . md5( wp_json_encode( $r ) ); - // Check if we have the results of the following query in cache. + // Check if the results of the following query are in cache. $posts = wp_cache_get( $cache_key, 'bu-navigation-persistent' ); - // If the core and BU Navigation last changed values match up, - // and the results of the the following query are already in cache, + // If the core and BU Navigation `last_changed` values match up, + // and the results of the following query are already in cache, // return it now. - if ( $last_changed === $bu_nav_posts_last_changed && $posts ) { + if ( $last_changed === $bu_nav_last_changed && $posts ) { return $posts; } - // If the core and BU Navigation last changed values don't match, - // or the results of the the following query are not yet cached, - // set `posts_last_changed` to match core's `last_changed` key value. - if ( $last_changed !== $bu_nav_posts_last_changed ) { - wp_cache_set( 'posts_last_changed', $last_changed, 'bu-navigation-persistent' ); + // If the core and BU Navigation `last_changed` values don't match, + // or the results of the following query are not yet cached, + // set `last_changed` to match core's `last_changed` key value. + if ( $last_changed !== $bu_nav_last_changed ) { + wp_cache_set( 'last_changed', $last_changed, 'bu-navigation-persistent' ); } // Start building the query From adef5c1e994e800d6f8ee217d6e9260bd64bd2cd Mon Sep 17 00:00:00 2001 From: philcable Date: Thu, 26 Sep 2019 13:35:48 -0700 Subject: [PATCH 4/8] Refactor cache bust handling The previous approach had a significant flaw in that it could potentially never update the cache value for some calls to `bu_navigation_get_posts`, as the now-removed BU Navigation-specific `last_changed` key would be updated the first time the function was called. --- admin/manager.php | 3 +-- includes/library.php | 35 +++++++++++------------------------ 2 files changed, 12 insertions(+), 26 deletions(-) diff --git a/admin/manager.php b/admin/manager.php index fb6fa38..a56fc50 100644 --- a/admin/manager.php +++ b/admin/manager.php @@ -417,8 +417,7 @@ public function save() { } // Update the `last_changed` key value with the current time. - $bu_nav_last_changed = microtime(); - wp_cache_set( 'last_changed', $bu_nav_last_changed, 'bu-navigation-persistent' ); + wp_cache_set( 'last_changed', microtime(), 'posts' ); } return $saved; diff --git a/includes/library.php b/includes/library.php index 68e79f0..9228c14 100644 --- a/includes/library.php +++ b/includes/library.php @@ -432,40 +432,22 @@ function bu_navigation_get_posts( $args = '' ) { // This key is updated by core in `clean_post_cache`. $last_changed = wp_cache_get( 'last_changed', 'posts' ); - // Get the `last_changed` key from the `bu-navigation-persistent` cache group. - $bu_nav_last_changed = wp_cache_get( 'last_changed', 'bu-navigation-persistent' ); - // If WP's `last_changed` key has no value, set it using the current time. if ( ! $last_changed ) { - $last_changed = microtime(); - wp_cache_set( 'last_changed', $last_changed, 'posts' ); - } - - // If BU Navigation's `last_changed` key has no value, - // set it using the current time. - if ( ! $bu_nav_last_changed ) { - $bu_nav_last_changed = microtime(); - wp_cache_set( 'last_changed', $bu_nav_last_changed, 'bu-navigation-persistent' ); + wp_cache_set( 'last_changed', microtime(), 'posts' ); } // Create the key to use for storing the results of the following query. $cache_key = 'get_posts:' . md5( wp_json_encode( $r ) ); // Check if the results of the following query are in cache. - $posts = wp_cache_get( $cache_key, 'bu-navigation-persistent' ); + $cached_posts = wp_cache_get( $cache_key, 'bu-navigation-persistent' ); // If the core and BU Navigation `last_changed` values match up, // and the results of the following query are already in cache, // return it now. - if ( $last_changed === $bu_nav_last_changed && $posts ) { - return $posts; - } - - // If the core and BU Navigation `last_changed` values don't match, - // or the results of the following query are not yet cached, - // set `last_changed` to match core's `last_changed` key value. - if ( $last_changed !== $bu_nav_last_changed ) { - wp_cache_set( 'last_changed', $last_changed, 'bu-navigation-persistent' ); + if ( $cached_posts && $cached_posts['last_changed'] === $last_changed ) { + return $cached_posts['query']; } // Start building the query @@ -572,8 +554,13 @@ function bu_navigation_get_posts( $args = '' ) { $posts = $items; } - // Cache results. - wp_cache_set( $cache_key, $posts, 'bu-navigation-persistent' ); + // Cache the `last_changed` value and the query results. + $cache_value = array( + 'last_changed' => $last_changed, + 'query' => $posts, + ); + + wp_cache_set( $cache_key, $cache_value, 'bu-navigation-persistent' ); return $posts; From 98a75e1ae0b8efa76061d4eb2c630ee8a244e5d9 Mon Sep 17 00:00:00 2001 From: philcable Date: Thu, 26 Sep 2019 14:10:43 -0700 Subject: [PATCH 5/8] Update inline comment --- includes/library.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/includes/library.php b/includes/library.php index 9228c14..c59a6c3 100644 --- a/includes/library.php +++ b/includes/library.php @@ -443,9 +443,8 @@ function bu_navigation_get_posts( $args = '' ) { // Check if the results of the following query are in cache. $cached_posts = wp_cache_get( $cache_key, 'bu-navigation-persistent' ); - // If the core and BU Navigation `last_changed` values match up, - // and the results of the following query are already in cache, - // return it now. + // If we have a cached value and its `last_changed` property matches + // WP's `last_changed` cache value, return the cached query results. if ( $cached_posts && $cached_posts['last_changed'] === $last_changed ) { return $cached_posts['query']; } From 218d635a1e41a442de5f67a72f578aa63f55354d Mon Sep 17 00:00:00 2001 From: philcable Date: Thu, 26 Sep 2019 15:24:01 -0700 Subject: [PATCH 6/8] Fix inline comments Sorry... --- includes/library.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/library.php b/includes/library.php index c59a6c3..ef6a338 100644 --- a/includes/library.php +++ b/includes/library.php @@ -428,7 +428,7 @@ function bu_navigation_get_posts( $args = '' ) { ); $r = wp_parse_args( $args, $defaults ); - // Get the `last_changed` key from the WP core `posts`cache group. + // Get the `last_changed` key from the WP core `posts` cache group. // This key is updated by core in `clean_post_cache`. $last_changed = wp_cache_get( 'last_changed', 'posts' ); @@ -443,7 +443,7 @@ function bu_navigation_get_posts( $args = '' ) { // Check if the results of the following query are in cache. $cached_posts = wp_cache_get( $cache_key, 'bu-navigation-persistent' ); - // If we have a cached value and its `last_changed` property matches + // If a cached value exists and its `last_changed` property matches // WP's `last_changed` cache value, return the cached query results. if ( $cached_posts && $cached_posts['last_changed'] === $last_changed ) { return $cached_posts['query']; From 25739d64959d26c0ec2711dc08109ba457ab2ffc Mon Sep 17 00:00:00 2001 From: philcable Date: Wed, 9 Oct 2019 15:17:47 -0700 Subject: [PATCH 7/8] Use `wp_cache_get_last_changed` to gain a bit of efficiency --- includes/library.php | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/includes/library.php b/includes/library.php index ef6a338..e315af3 100644 --- a/includes/library.php +++ b/includes/library.php @@ -425,17 +425,12 @@ function bu_navigation_get_posts( $args = '' ) { 'include_links' => true, 'suppress_filter_posts' => false, 'suppress_urls' => false, - ); - $r = wp_parse_args( $args, $defaults ); + ); - // Get the `last_changed` key from the WP core `posts` cache group. - // This key is updated by core in `clean_post_cache`. - $last_changed = wp_cache_get( 'last_changed', 'posts' ); + $r = wp_parse_args( $args, $defaults ); - // If WP's `last_changed` key has no value, set it using the current time. - if ( ! $last_changed ) { - wp_cache_set( 'last_changed', microtime(), 'posts' ); - } + // Get last changed date for the WP core `posts` cache group. + $last_changed = wp_cache_get_last_changed( 'posts' ); // Create the key to use for storing the results of the following query. $cache_key = 'get_posts:' . md5( wp_json_encode( $r ) ); @@ -444,7 +439,8 @@ function bu_navigation_get_posts( $args = '' ) { $cached_posts = wp_cache_get( $cache_key, 'bu-navigation-persistent' ); // If a cached value exists and its `last_changed` property matches - // WP's `last_changed` cache value, return the cached query results. + // the last changed date for the WP core `posts` cache group, + // return the cached query results. if ( $cached_posts && $cached_posts['last_changed'] === $last_changed ) { return $cached_posts['query']; } From 2fb5d88b4f7d377e7722a5893dc31aef8e49b05a Mon Sep 17 00:00:00 2001 From: Todd Milliken Date: Fri, 11 Oct 2019 13:17:23 -0400 Subject: [PATCH 8/8] :construction_worker: Update failing travis tests Uses the working setup from bu-custom-roles which was pulled from latest wp scaffold plugin --- .travis.yml | 68 +++++++++++++++++++++++------------------ bin/install-wp-tests.sh | 49 +++++++++++++++++++++-------- 2 files changed, 75 insertions(+), 42 deletions(-) diff --git a/.travis.yml b/.travis.yml index f3c856a..aea3f2a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,40 +1,51 @@ -language: php - sudo: false +dist: trusty + +language: php -php: - - 5.6 - - 7.0 - - 7.1 +notifications: + email: + on_success: never + on_failure: change -env: - - WP_VERSION=nightly - - WP_VERSION=latest - - WP_VERSION=4.7 - - WP_VERSION=4.6 +branches: + only: + - /.*/ cache: directories: + - vendor - $HOME/.composer/cache -before_install: - - composer self-update - -install: - - composer install --prefer-dist +matrix: + include: + - php: 7.2 + env: WP_VERSION=latest + - php: 7.2 + env: WP_VERSION=4.9.10 + - php: 7.0 + env: WP_VERSION=latest + - php: 7.0 + env: WP_VERSION=4.9.10 before_script: - export PATH="$HOME/.composer/vendor/bin:$PATH" - | - if [[ ${TRAVIS_PHP_VERSION:0:2} == "7." ]]; then - composer global require "phpunit/phpunit=5.7.*" + if [ -f ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini ]; then + phpenv config-rm xdebug.ini else - composer global require "phpunit/phpunit=4.8.*" + echo "xdebug.ini does not exist" + fi + - | + if [[ ! -z "$WP_VERSION" ]] ; then + bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION + composer global require "phpunit/phpunit=4.8.*|5.7.*" + fi + - | + if [[ "$WP_TRAVISCI" == "phpcs" ]] ; then + composer global require wp-coding-standards/wpcs + phpcs --config-set installed_paths $HOME/.composer/vendor/wp-coding-standards/wpcs fi - - - git config --global user.email "travis-ci@codeclimate.com" - - git config --global user.name "Travis CI" - - bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION script: - | @@ -42,10 +53,7 @@ script: phpunit WP_MULTISITE=1 phpunit fi - -after_script: - - ./bin/codeclimate.sh - -addons: - codeclimate: - repo_token: CODECLIMATE_REPO_TOKEN \ No newline at end of file + - | + if [[ "$WP_TRAVISCI" == "phpcs" ]] ; then + phpcs + fi diff --git a/bin/install-wp-tests.sh b/bin/install-wp-tests.sh index 555b6ad..49e3f34 100755 --- a/bin/install-wp-tests.sh +++ b/bin/install-wp-tests.sh @@ -12,8 +12,10 @@ DB_HOST=${4-localhost} WP_VERSION=${5-latest} SKIP_DB_CREATE=${6-false} -WP_TESTS_DIR=${WP_TESTS_DIR-/tmp/wordpress-tests-lib} -WP_CORE_DIR=${WP_CORE_DIR-/tmp/wordpress/} +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 @@ -23,8 +25,15 @@ download() { fi } -if [[ $WP_VERSION =~ [0-9]+\.[0-9]+(\.[0-9]+)? ]]; then - WP_TESTS_TAG="tags/$WP_VERSION" +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 @@ -50,18 +59,34 @@ install_wp() { mkdir -p $WP_CORE_DIR if [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then - mkdir -p /tmp/wordpress-nightly - download https://wordpress.org/nightly-builds/wordpress-latest.zip /tmp/wordpress-nightly/wordpress-nightly.zip - unzip -q /tmp/wordpress-nightly/wordpress-nightly.zip -d /tmp/wordpress-nightly/ - mv /tmp/wordpress-nightly/wordpress/* $WP_CORE_DIR + 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 /tmp/wordpress.tar.gz - tar --strip-components=1 -zxmf /tmp/wordpress.tar.gz -C $WP_CORE_DIR + 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 @@ -80,7 +105,7 @@ install_test_suite() { # 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/images/ $WP_TESTS_DIR/data/images + 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 @@ -124,4 +149,4 @@ install_db() { install_wp install_test_suite -install_db +install_db \ No newline at end of file