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/admin/manager.php b/admin/manager.php index a86db1f..a56fc50 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 ) ); } + + // Update the `last_changed` key value with the current time. + wp_cache_set( 'last_changed', microtime(), 'posts' ); } return $saved; 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 diff --git a/includes/library.php b/includes/library.php index 991cb85..e315af3 100644 --- a/includes/library.php +++ b/includes/library.php @@ -425,9 +425,26 @@ function bu_navigation_get_posts( $args = '' ) { 'include_links' => true, 'suppress_filter_posts' => false, 'suppress_urls' => false, - ); + ); + $r = wp_parse_args( $args, $defaults ); + // 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 ) ); + + // Check if the results of the following query are in cache. + $cached_posts = wp_cache_get( $cache_key, 'bu-navigation-persistent' ); + + // If a cached value exists and its `last_changed` property matches + // 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']; + } + // Start building the query $where = $orderby = ''; @@ -532,6 +549,14 @@ function bu_navigation_get_posts( $args = '' ) { $posts = $items; } + // 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; }