Plugin Directory

Changeset 2055658


Ignore:
Timestamp:
03/22/2019 09:13:57 PM (7 years ago)
Author:
danillonunes
Message:

Cache donations results

Location:
makemydonation-imo/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • makemydonation-imo/trunk/mmdimo.php

    r2052722 r2055658  
    245245    }
    246246  }
     247}
     248
     249function mmdimo_load_donations_by_post( $post_id = 0, $count = FALSE ) {
     250  $post = get_post( $post );
     251  $id = isset( $post->ID ) ? $post->ID : 0;
     252  $now = time();
     253  $expires = 60 * 60;
     254
     255  $meta_id = $count ? 'mmdimo_donations_count' : 'mmdimo_donations';
     256
     257  $donations = array(
     258    'content' => ''
     259  );
     260
     261  $cached_donations = get_post_meta( $id, $meta_id, TRUE );
     262
     263  if ( $cached_donations && isset($cached_donations['expires']) && $cached_donations['expires'] > $now ) {
     264    $donations = $cached_donations;
     265  }
     266  else {
     267    $mmdimo_case = get_post_meta( $id, 'mmdimo_case', TRUE );
     268    $case_id = $mmdimo_case && isset( $mmdimo_case['id'] ) ? $mmdimo_case['id'] : NULL;
     269    require_once( MMDIMO_PLUGIN_DIR . '/api.php' );
     270
     271    if ( $case_id && $content = mmdimo_api_case_donations_load($case_id, $count) ) {
     272
     273      $donations = array(
     274        'content' => $content,
     275        'expires' => $now + $expires
     276      );
     277
     278      update_post_meta( $id, $meta_id, $donations );
     279    }
     280  }
     281
     282
     283  return $donations['content'];
    247284}
    248285
  • makemydonation-imo/trunk/template.php

    r2055657 r2055658  
    152152  $post = get_post( $post );
    153153  $id = isset( $post->ID ) ? $post->ID : 0;
    154   $mmdimo_case = get_post_meta( $id, 'mmdimo_case', TRUE );
    155 
    156   require_once( MMDIMO_PLUGIN_DIR . '/api.php' );
    157   $donations_count = mmdimo_api_case_donations_load($mmdimo_case['id'], TRUE);
     154
     155  $donations_count = mmdimo_load_donations_by_post($id, TRUE);
    158156
    159157  if ( $post->ID && $donations_count['total'] ) {
     
    196194  $post = get_post( $post );
    197195  $id = isset( $post->ID ) ? $post->ID : 0;
    198   $mmdimo_case = get_post_meta( $id, 'mmdimo_case', TRUE );
    199 
    200   require_once( MMDIMO_PLUGIN_DIR . '/api.php' );
    201   $donations = mmdimo_api_case_donations_load($mmdimo_case['id']);
     196
     197  $donations = mmdimo_load_donations_by_post($id);
    202198
    203199  $default_display_attributes = array(
Note: See TracChangeset for help on using the changeset viewer.