Skip to content

Instantly share code, notes, and snippets.

@michaelmcandrew
Last active November 22, 2018 02:26
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelmcandrew/1f21e95d13b48c2bdf4d0b300a28c298 to your computer and use it in GitHub Desktop.
Save michaelmcandrew/1f21e95d13b48c2bdf4d0b300a28c298 to your computer and use it in GitHub Desktop.
Proof of concept WP shortcode to display fields from logged in and other contacts
<?php
/*
This is very much WIP / proof of concept / scratching an itch. Lots of stuff missing, like ACL.
Just sharing some early attempts at WP plugin development. Feedback welcome.
For example...
[civicrm_user_fields]
Name: {user.display_name}
Address {user.address.street_address}}
[/civicrm_user_fields]
OR you can display data about other contacts by including the contact ID as a url query parameter
WARNING: no ACL on the below code!
url like https://example.org/about-me/?fid=123
[civicrm_user_fields querymap=friend:fid]
Name: {user.display_name} is friends with {friend.display_name} who lives in {friend.address.city}
[/civicrm_user_fields]
*/
add_shortcode('civicrm_user_fields', 'civicrm_user_fields');
function civicrm_user_fields($attr, $content)
{
$contacts['user']['params']['id'] = CRM_Core_Session::singleton()->getLoggedInContactID();
$parsed = $content;
while (strlen($parsed)) {
$tokenStart = strpos($parsed, '{');
if ($tokenStart !== false) {
$tokenEnd = strpos($parsed, '}');
$tokens[] = substr($parsed, $tokenStart, $tokenEnd - $tokenStart + 1);
$parsed = substr($parsed, $tokenEnd + 1);
} else {
$parsed = '';
}
}
$tokens = array_unique($tokens);
foreach ($tokens as $token) {
$parts = explode('.', substr($token, 1, -1));
if (count($parts) == 2) {
$contacts[$parts[0]]['params']['return'][] = $parts[1];
} elseif (count($parts) == 3) {
$contacts[$parts[0]]['related'][$parts[1]]['params']['return'][] = $parts[2];
}
}
$queryMaps = explode(',', $attr['querymap']);
foreach ($queryMaps as $querymap) {
$parts = explode(':', $querymap);
if (isset($contacts[$parts[0]])) {
$contacts[$parts[0]]['params']['id'] = $_GET[$parts[1]];
if (isset($contacts[$parts[0]]['related'])) {
foreach ($contacts[$parts[0]]['related'] as &$related) {
$related['params']['contact_id'] = $_GET[$parts[1]];
$related['params']['is_primary'] = 1;
}
}
}
}
foreach ($contacts as &$contact) {
$contact['result'] = civicrm_api3('Contact', 'getsingle', $contact['params']);
if (isset($contact['related'])) {
foreach ($contact['related'] as $entity => &$related) {
$related['result'] = civicrm_api3($entity, 'getsingle', $related['params']);
}
}
}
foreach ($tokens as $n => $token) {
$parts = explode('.', substr($token, 1, -1));
if (count($parts) == 2) {
$values[$n] = $contacts[$parts[0]]['result'][$parts[1]];
} elseif (count($parts) == 3) {
$values[$n] = $contacts[$parts[0]]['related'][$parts[1]]['result'][$parts[2]];
}
}
return str_replace($tokens, $values, $content);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment