Editing this post for clarification as I didn't do a good job the first time around --
I have a script that crawls through a directory and pulls info from WP sites and builds a JSON array of objects that will eventually be fed into a visual dashboard. The script works perfectly but I can't seem to get the siteurl
key to post in every object as it does in the first.
The key-pairs that have null need to be replaced with the corresponding siteurl
within it's array.
I feel I should be able to do this with jq and the --arg name value
option but I clearly am doing something wrong. Not sure if I'm not passing the variables correctly or if it's an issue with WPCLI.
Output and script are below.
[
{
"eventType": "WordpressSite",
"siteurl": "http://mytest1.com"
},
{
"eventType": "WordpressPlugin",
"plugin_name": "akismet",
"plugin_status": "inactive",
"plugin_update": "available",
"plugin_version": "4.0.8",
"siteurl": null
},
{
"eventType": "WordpressPlugin",
"plugin_name": "hello",
"plugin_status": "inactive",
"plugin_update": "none",
"plugin_version": "1.7",
"siteurl": null
},
{
"eventType": "WordpressUser",
"siteurl": null,
"user_email": "example.user@email.com",
"user_name": "testuser1",
"user_registered": "2018-11-26 17:44:09",
"user_role": "administrator"
}
]
[
{
"eventType": "WordpressSite",
"siteurl": "http://mytest2.com"
},
{
"eventType": "WordpressPlugin",
"plugin_name": "akismet",
"plugin_status": "inactive",
"plugin_update": "available",
"plugin_version": "4.0.8",
"siteurl": null
},
{
"eventType": "WordpressPlugin",
"plugin_name": "hello",
"plugin_status": "inactive",
"plugin_update": "none",
"plugin_version": "1.7",
"siteurl": null
},
{
"eventType": "WordpressUser",
"siteurl": null,
"user_email": "example.user@email.com",
"user_name": "testuser2",
"user_registered": "2018-11-26 17:44:04",
"user_role": "administrator"
}
]
#!/bin/bash
#for d in /var/www/* ; do
# echo "$d"
#done
for f in /var/www/*/public_html/; do
if [[ -d $f ]]; then
#echo "$f"
cd $f;
siteurl=$(wp option get siteurl)
users=$(wp user list --fields=display_name,user_email,user_registered,roles --format=json | jq '.[] | .eventType = "WordpressUser"' | jq . --arg siteurl $(wp option get siteurl) | jq '. + {"user_name": .display_name, "user_role": .roles, "siteurl": ."$siteurl"} | del (.display_name, .roles)')
plugins=$(wp plugin list --format=json | jq '.[] | .eventType = "WordpressPlugin"' | jq '. + {"plugin_name": .name, "plugin_status": .status, "plugin_version": .version, "plugin_update": .update, "siteurl": ."$siteurl"} | del(.name, .status, .version, .update)')
{
printf '{"eventType":"WordpressSite","siteurl":"%s"}\n' "$(wp option get siteurl)"
echo "$plugins"
echo "$users"
} | jq -sS .
cd ../..;
fi
done