Skip to content

Instantly share code, notes, and snippets.

@firefly05
Created January 23, 2021 03:32
Show Gist options
  • Save firefly05/293274646208fbeb284fbebbfebebc14 to your computer and use it in GitHub Desktop.
Save firefly05/293274646208fbeb284fbebbfebebc14 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Variable ROLEMAP will hold a reference list of all the current roles
# and the values to which they should be updated
declare -A ROLEMAP;
# List of roles to be converted to the 'editor' role
ROLE_TO_EDITOR=(
academy
academy_editor
advanced_editor
business_development
case_studies
case_studies_editor
contractors
copypress_contractors
country_manager
dradis_marketing
dradis_marketing_pls_pages
general_info_contractor
growth
hire_editor
hire_product
inclusion
indeed_one
industry_marketing
interview_questions_editor
ipa_money_team
jse_editor
jsrc_content_team
legal_only_qa
legal_read_only
lmo_editor
microsite_editor
partner_pages
prime
prime_resources_qa_only
product_marketing_editor
program
recruiter_hub_editor
security
seo
seo_team
seo_team_with_xml
smb_service
social_editor
);
# Key list of roles to 'editor' in ROLEMAP
ROLEMAP[editor]="${ROLE_TO_EDITOR[@]}";
# List of roles to be converted to 'deactivated' role
ROLE_TO_DEACTIVATE=(
author
brand_team
campaign_labs
career_guide
career_guide_contractors
career_guide_team
career_guide_team_with_prod
career_guide_with_prod
contributor
designer_read_access
recruiter_hub_author
subscriber
);
# Key list of roles to 'deactivated' in ROLEMAP
ROLEMAP[deactivated]="${ROLE_TO_DEACTIVATE[@]}";
#List of roles to be converted to 'publisher' role
ROLE_TO_PUBLISHER=(
about
academy_with_prod
advanced_editor_with_prod
assessments_with_prod
biz_dev_advanced
business_development_with_prod
country_manager_with_prod
dradis_marketing_w_tp_prod
growth_manager
growth_with_prod
ioa_administrator
jse_editor_with_prod
legal
prime_resources_with_prod
prime_testimonials_with_prod
prime_with_prod
recruiter_hub
seo_with_prod
);
# Key list of roles to 'publisher' in ROLEMAP
ROLEMAP[publisher]="${ROLE_TO_PUBLISHER[@]}";
# To perform a test, populate the TEST_SITES array with the full urls of the
# subsites you wish to run the scipt on. Leave TEST_SITES array empty to run the
# script against all the subsites
TEST_SITES=('');
# e.g. TEST_SITES=('https://en-ch.indeeddev.wpengine.test/' 'https://fr-ch.indeeddev.wpengine.test/' 'https://it-ch.indeeddev.wpengine.test/');
# Switch: if there are URLs set in TEST_SITES then perform a test run
# using those URLs. If there are no URLs in TEST_SITES, then perform
# an actual update will all the subsites
if [ "${#TEST_SITES[@]}" -gt 0 ]; then
SITES=("${TEST_SITES[@]}")
else
SITES=($(wp site list --field=url))
fi
# Get all the subsites and apply the search replace to each one
for SUBSITE in "${SITES[@]}"; do
echo "checking $SUBSITE ...";
# Loop through every role in the role map, updating each current value to the target val
for UPDATE_TO_ROLE in "${!ROLEMAP[@]}"; do
UPDATE_FROM_ROLE=(${ROLEMAP[$UPDATE_TO_ROLE]})
for ROLE in "${UPDATE_FROM_ROLE[@]}"; do
USERS_WITH_ROLE=($(wp user list --role=$ROLE --format=ids --url=$SUBSITE));
TOTAL_USERS_WITH_ROLE=${#USERS_WITH_ROLE[@]};
if [ $TOTAL_USERS_WITH_ROLE -gt 0 ]; then
echo "converting from $ROLE to $UPDATE_TO_ROLE:";
# Catches for users that require dual roles
# If the roles is either about, legal or recruiter_hub, simply assign
# the additional role of "publisher"
if [ $ROLE = "about" -o $ROLE = "legal" -o $ROLE = "recruiter_hub" ]; then
for USER in "${USERS_WITH_ROLE[@]}"; do
wp user add-role $USER $UPDATE_TO_ROLE --url=$SUBSITE;
done;
# If role is either legal_only_qa or legal_read_only, then assign
# both the role of admin (passed via $UPDATE_TO_ROLE) and legal
elif [ $ROLE = "legal_only_qa" -o $ROLE = "legal_read_only" ]; then
for USER in "${USERS_WITH_ROLE[@]}"; do
wp user update $USER --role=$UPDATE_TO_ROLE --url=$SUBSITE;
wp user add-role $USER legal --url=$SUBSITE;
done;
# If the role update is to a single role, then just bulk replace all the found matches
# with the new role
else
wp user update ${USERS_WITH_ROLE[@]} --role=$UPDATE_TO_ROLE --url=$SUBSITE;
fi
fi
done;
done
done;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment