From 6ab5164ca094941b31266f4586fba04f8f1ebd83 Mon Sep 17 00:00:00 2001 From: EarthlingDavey <15802017+EarthlingDavey@users.noreply.github.com> Date: Mon, 18 Nov 2024 17:06:53 +0000 Subject: [PATCH] CDPT-2259 Don't delete non-existent documents. (#781) * CDPT-2259 Don't delete non-existent documents. * Add package version check * Update composer-post-install.sh * Update composer-post-install.sh * Update composer-post-install.sh * Update composer-post-install.sh * Update composer-post-install.sh * Update composer-post-install.sh * Update composer-post-install.sh * Update composer-post-install.sh * Update composer-post-install.sh * Update composer-post-install.sh --- .env.example | 2 +- bin/composer-post-install.sh | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/.env.example b/.env.example index 72c58d218..5129dbd2d 100644 --- a/.env.example +++ b/.env.example @@ -36,7 +36,7 @@ DB_HOST=mariadb DB_USER=wordpress DB_PASSWORD=wordpress -OPENSEARCH_VERSION=2.12.0 +OPENSEARCH_VERSION=2.13.0 OPENSEARCH_URL=http://opensearch:9200 CACHE_HOST=redis diff --git a/bin/composer-post-install.sh b/bin/composer-post-install.sh index 324fbf48c..159c8e507 100755 --- a/bin/composer-post-install.sh +++ b/bin/composer-post-install.sh @@ -1,5 +1,17 @@ #!/usr/bin/env sh +# A function to verify version is supported, exit it it's not. +verify_composer_package_version() { + TARGET_PACKAGE=$1 + TARGET_VERSION=$2 + INSTALLED_VERSION=$(composer show $1 | sed -n '/versions/s/^[^0-9]\+\([^,]\+\).*$/\1/p') + + if [ "$INSTALLED_VERSION" != "$TARGET_VERSION" ] ; then + echo "$TARGET_PACKAGE target version is not installed - review composer-post-install.sh." + exit 1; + fi +} + # Add logging to amazon-s3-and-cloudfront-pro plugin. # Define the search and replace strings. @@ -73,3 +85,25 @@ if grep -q $TREE_VIEW_SEARCH $TREE_VIEW_FILE ; then echo "Fixing warning in cms-tree-page-view..." sed -i "s/$TREE_VIEW_SEARCH/$TREE_VIEW_REPLACE/g" $TREE_VIEW_FILE fi + + +# This find/replace is for the ElasticPress plugin. +# +# When a new post is created via the WordPress dashboard, the existing behaviour of the ElasticPress plugin +# is to delete the post from the search index. Delteing a post that's not in the index causes a 404 error to +# be logged on Cloud Platform's OpenSearch proxy server. +# +# This change adds a check to see if the post is in the index before deleting it, preventing the 404 error. + +ELASTIC_PRESS_TARGET_VERSION="5.1.3" +verify_composer_package_version "wpackagist-plugin/elasticpress" $ELASTIC_PRESS_TARGET_VERSION + +ELASTIC_PRESS_FILE=/var/www/html/public/app/mu-plugins/elasticpress/includes/classes/Indexable/Post/SyncManager.php +ELASTIC_PRESS_SEARCH="\t\$this->action_delete_post( \$post_id );" +ELASTIC_PRESS_REPLACE="\t\/\/ The following line was modified by composer-post-install.sh\n\t\t" +ELASTIC_PRESS_REPLACE="$ELASTIC_PRESS_REPLACE\t\$indexable->get( \$post_id ) \&\& \$this->action_delete_post( \$post_id );" + +if [ -f "$ELASTIC_PRESS_FILE" ] ; then + echo "Fixing warning in elasticpress. Checking for doc before deleting prevents 404s in logs..." + sed -i "s/$ELASTIC_PRESS_SEARCH/$ELASTIC_PRESS_REPLACE/g" $ELASTIC_PRESS_FILE +fi