Created
November 6, 2023 09:10
-
-
Save leoloso/a51c95585bd28845a22b6377b8f8a3e3 to your computer and use it in GitHub Desktop.
GraphQL query to generate a table in HTML with the roles and capabilities on the WordPress site, and a ✅ or ❌ in each cell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
query ExportData { | |
roles { | |
capabilities | |
@export(as: "roleCapabilities", type: DICTIONARY) | |
} | |
capabilities | |
@export(as: "allCapabilities") | |
@remove | |
markdownTableHeader: _arrayJoin( | |
array: $__capabilities, | |
separator: " | " | |
) | |
@strPrepend(string: "| | ") | |
@strAppend(string: """ | | |
""") | |
@export(as: "markdownTableHeader") | |
@remove | |
markdownTableHeaderSeparator: _echo( | |
value: $__capabilities | |
) | |
@underEachArrayItem | |
@applyField( | |
name: "_echo", | |
arguments: { | |
value: "| --- " | |
} | |
setResultInResponse: true | |
) | |
@passOnwards(as: "dottedItems") | |
@applyField( | |
name: "_arrayJoin", | |
arguments: { | |
array: $dottedItems | |
separator: "" | |
} | |
setResultInResponse: true | |
) | |
@strPrepend(string: "| --- ") | |
@strAppend(string: """ | | |
""") | |
@export(as: "markdownTableHeaderSeparator") | |
@remove | |
} | |
query FormatData | |
@depends(on: "ExportData") | |
{ | |
roleCapabilityDictionaries: _echo(value: $roleCapabilities) | |
@underEachJSONObjectProperty( | |
passKeyOnwardsAs: "role" | |
passValueOnwardsAs: "capabilities" | |
affectDirectivesUnderPos: [1, 2] | |
) | |
@applyField( | |
name: "_arrayDiff" | |
arguments: { | |
arrays: [$allCapabilities, $capabilities] | |
} | |
passOnwardsAs: "missingCapabilities" | |
) | |
@applyField( | |
name: "_echo" | |
arguments: { | |
value: { | |
has: $capabilities, | |
missing: $missingCapabilities | |
all: $allCapabilities | |
} | |
} | |
setResultInResponse: true | |
) | |
@export(as: "roleCapabilityDictionaries") | |
@remove | |
} | |
query AdaptData | |
@depends(on: "FormatData") | |
{ | |
adaptedRoleCapabilityDictionaries: _echo(value: $roleCapabilityDictionaries) | |
@underEachJSONObjectProperty( | |
passKeyOnwardsAs: "role" | |
passValueOnwardsAs: "capabilityObject" | |
affectDirectivesUnderPos: [1, 2] | |
) | |
@applyField( | |
name: "_objectProperty", | |
arguments: { | |
object: $capabilityObject, | |
by: { | |
key: "has" | |
} | |
}, | |
passOnwardsAs: "roleHasCapabilities" | |
) | |
@underJSONObjectProperty( | |
by: { | |
key: "all" | |
} | |
) | |
@underEachArrayItem( | |
passValueOnwardsAs: "cap" | |
affectDirectivesUnderPos: [1, 2, 4] | |
) | |
@applyField( | |
name: "_inArray", | |
arguments: { | |
array: $roleHasCapabilities | |
value: $cap | |
} | |
passOnwardsAs: "roleHasCapability" | |
) | |
@if( | |
condition: $roleHasCapability | |
) | |
@applyField( | |
name: "_echo" | |
arguments: { | |
value: "| ✅ " | |
} | |
setResultInResponse: true | |
) | |
@unless( | |
condition: $roleHasCapability | |
) | |
@applyField( | |
name: "_echo" | |
arguments: { | |
value: "| ❌ " | |
} | |
setResultInResponse: true | |
) | |
@export(as: "adaptedRoleCapabilityDictionaries") | |
@remove | |
} | |
query CombineData | |
@depends(on: "AdaptData") | |
{ | |
combinedRoleCapabilityDictionaries: _echo(value: $adaptedRoleCapabilityDictionaries) | |
@underEachJSONObjectProperty( | |
passValueOnwardsAs: "capabilityObject" | |
affectDirectivesUnderPos: [1, 2, 3, 4, 5, 6] | |
passKeyOnwardsAs: "roleID" | |
) | |
@applyField( | |
name: "_objectProperty", | |
arguments: { | |
object: $capabilityObject, | |
by: { | |
key: "all" | |
} | |
}, | |
passOnwardsAs: "capabilityStrings" | |
) | |
@applyField( | |
name: "_arrayJoin", | |
arguments: { | |
array: $capabilityStrings, | |
separator: "" | |
}, | |
setResultInResponse: true | |
) | |
@strPrepend(string: "** ") | |
@strPrepend(string: $roleID) | |
@strPrepend(string: "| **") | |
@strAppend(string: """| | |
""") | |
@export(as: "combinedRoleCapabilityDictionaries") | |
@remove | |
} | |
query PrintData | |
@depends(on: "CombineData") | |
{ | |
markdownTableRows: _objectValues(object: $combinedRoleCapabilityDictionaries) | |
@remove | |
markdownTableBody: _arrayJoin(array: $__markdownTableRows, separator: "") | |
@remove | |
markdownTable: _sprintf( | |
string: "%s%s%s", | |
values: [$markdownTableHeader, $markdownTableHeaderSeparator, $__markdownTableBody] | |
) | |
@remove | |
htmlTable: _strConvertMarkdownToHTML(text: $__markdownTable) | |
@remove | |
simplerHTMLTable: _strReplace(in: $__htmlTable, search: "\n", replaceWith: "") | |
@remove | |
formattedHTMLTable: _strReplaceMultiple(in: $__simplerHTMLTable, search: ["<table>", "<thead>"], replaceWith: ["<table class='table table-striped table-bordered'>", "<thead class='table-dark'>"]) | |
@remove | |
htmlReport: _sprintf( | |
string: "<html><head><title>%s</title><link href='%s' rel='stylesheet' crossorigin='anonymous'></head><body>%s</body></html>", | |
values: [ | |
"Table of Roles and Capabilities", | |
"https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css", | |
$__formattedHTMLTable | |
] | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment