Skip to content

Commit

Permalink
WPCIVIUX-105 Implement transient caching for API4 get shortcode.
Browse files Browse the repository at this point in the history
  • Loading branch information
agileware-fj committed Oct 27, 2021
1 parent 0093200 commit 98520a5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
2 changes: 1 addition & 1 deletion civicrm-ux.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* Plugin Name: WP CiviCRM UX
* Plugin URI: https://github.com/agileware/wp-civicrm-ux
* Description: A better user experience for integrating WordPress and CiviCRM
* Version: 1.8.6
* Version: 1.9.0
* Requires at least: 5.8
* Requires PHP: 7.4
* Author: Agileware
Expand Down
36 changes: 24 additions & 12 deletions shortcodes/civicrm/api4-get.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ public function shortcode_callback( $atts = [], $content = null, $tag = '' ) {

// override default attributes with user attributes
$atts = $atts + [
'entity' => 'Contact',
];
'entity' => 'Contact',
];

// If "id" attribute exists but isn't an integer, replaced it with a GET parameter with that name.
// If "id" attribute exists but isn't an integer, replace it with a GET parameter with that name.
if( array_key_exists( 'id', $atts ) && !is_int( $atts['id'] ) ) {
$atts['id'] = (int) $_GET[$atts['id']];
if($atts['id'] < 1) {
Expand All @@ -40,7 +40,7 @@ public function shortcode_callback( $atts = [], $content = null, $tag = '' ) {

// Interpret attributes as where clauses.
foreach( $atts as $k => $v ) {
$v = html_entity_decode(preg_replace_callback('/^( ( " ) | \' )(?<value>.*)(?(2) " | \' )$/', fn($matches) => $matches[value], $v));
$v = html_entity_decode(preg_replace_callback('/^( ( " ) | \' )(?<value>.*)(?(2) " | \' )$/', fn($matches) => $matches[value], $v));
$k = preg_replace('/-(\w+)/', ':$1', $k);

switch($k) {
Expand Down Expand Up @@ -91,14 +91,22 @@ public function shortcode_callback( $atts = [], $content = null, $tag = '' ) {
}

try {
$trkey = $this->get_shortcode_name() . '__' . md5($atts['entity'] . ':get:' . json_encode($params));

$all = get_transient( $trkey );

if( $all !== FALSE ) {
return $all;
}

$all = '';

$class = "\\Civi\\Api4\\{$atts['entity']}";

$fields = $class::getFields(FALSE)
->addSelect( 'name', 'data_type', 'fk_entity' )
->execute()
->indexBy( 'name' );
->addSelect( 'name', 'data_type', 'fk_entity' )
->execute()
->indexBy( 'name' );

$results = civicrm_api4( $atts['entity'], 'get', $params );

Expand All @@ -120,9 +128,9 @@ public function shortcode_callback( $atts = [], $content = null, $tag = '' ) {

if( preg_match( '/^img( : (?<w> \d+ %? ) x (?<h> \d+ %? ) | : alt= (?<alt>.*) | : [^:]* )* /x', $match[ 'format' ], $m ) ) {
$output = '<img src="' . $output . '"'
. ($m['w'] ? " width=\"${m['w']}\" height=\"${m['h']}\"" : '') .
' alt="' . ($m['alt'] ? htmlentities($m['alt']) : '" role="presentation') .
'">';
. ($m['w'] ? " width=\"${m['w']}\" height=\"${m['h']}\"" : '') .
' alt="' . ($m['alt'] ? htmlentities($m['alt']) : '" role="presentation') .
'">';
}
}
else {
Expand All @@ -140,9 +148,13 @@ public function shortcode_callback( $atts = [], $content = null, $tag = '' ) {
$all .= do_shortcode( $output );
}

return trim($all);
$all = trim($all);

set_transient($trkey, $all, 4 * HOUR_IN_SECONDS);

return $all;
} catch (API_Exception $e) {
\Civi::log()->error('Error with API$ Shortcode on post #' . get_the_ID(). ': ' . $e->getMessage());
\Civi::log()->error('Error with API4 Shortcode on post #' . get_the_ID(). ': ' . $e->getMessage());

return '';
}
Expand Down

0 comments on commit 98520a5

Please sign in to comment.