| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; // Exit if accessed directly. |
| 5 |
} |
| 6 |
|
| 7 |
class FEP_Cache { |
| 8 |
|
| 9 |
private static $instance; |
| 10 |
|
| 11 |
private $queue = array(); |
| 12 |
|
| 13 |
public static function init() { |
| 14 |
if ( ! self::$instance instanceof self ) { |
| 15 |
self::$instance = new self; |
| 16 |
} |
| 17 |
return self::$instance; |
| 18 |
} |
| 19 |
|
| 20 |
private function __construct() { |
| 21 |
} |
| 22 |
|
| 23 |
public function add_to_queue( $ids, $type ){ |
| 24 |
$ids = (array) $ids; |
| 25 |
$type = (string) $type; |
| 26 |
|
| 27 |
if( ! $ids || ! is_array( $ids ) || ! $type ){ |
| 28 |
return false; |
| 29 |
} |
| 30 |
if( ! isset( $this->queue[ $type ] ) || ! is_array( $this->queue[ $type ] ) ){ |
| 31 |
$this->queue[ $type ] = array(); |
| 32 |
} |
| 33 |
$this->queue[ $type ] = array_merge( $this->queue[ $type ], $ids ); |
| 34 |
} |
| 35 |
|
| 36 |
public function update_cache( $provided_ids, $type ){ |
| 37 |
$type = (string) $type; |
| 38 |
|
| 39 |
if( ! is_array( $provided_ids ) || ! $type ){ |
| 40 |
return array(); |
| 41 |
} |
| 42 |
if( ! isset( $this->queue[ $type ] ) || ! is_array( $this->queue[ $type ] ) ){ |
| 43 |
$this->queue[ $type ] = array(); |
| 44 |
} |
| 45 |
$ids = array_merge( $this->queue[ $type ], $provided_ids ); |
| 46 |
$this->queue[ $type ] = array(); |
| 47 |
|
| 48 |
$ids = array_unique( array_filter( array_map( 'absint', $ids ) ) ); |
| 49 |
if( ! $ids ){ |
| 50 |
return array(); |
| 51 |
} |
| 52 |
|
| 53 |
$callback = apply_filters( 'fep_update_cache_callback', array( $this, "update_cache_{$type}" ), $type ); |
| 54 |
|
| 55 |
return $callback( $ids, $provided_ids ); |
| 56 |
} |
| 57 |
|
| 58 |
function update_cache_messages( $message_ids, $provided_ids ){ |
| 59 |
global $wpdb; |
| 60 |
|
| 61 |
$need_query = array(); |
| 62 |
$return = array(); |
| 63 |
|
| 64 |
foreach( $message_ids as $message_id ){ |
| 65 |
$cached = wp_cache_get( $message_id, 'fep-message' ); |
| 66 |
if( false === $cached ){ |
| 67 |
$need_query[] = $message_id; |
| 68 |
} elseif( in_array( $message_id, $provided_ids ) ) { |
| 69 |
$return[ $message_id ] = new FEP_Message( $cached ); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
if( $need_query ){ |
| 74 |
$messages = $wpdb->get_results( 'SELECT * FROM ' . FEP_MESSAGE_TABLE . ' WHERE mgs_id IN (' . implode( ', ', $need_query ) . ')' ); |
| 75 |
|
| 76 |
foreach ( $messages as $key => $message ) { |
| 77 |
$message_id = (int) $message->mgs_id; |
| 78 |
wp_cache_add( $message_id, $message, 'fep-message' ); |
| 79 |
if( in_array( $message_id, $provided_ids ) ) { |
| 80 |
$return[ $message_id ] = new FEP_Message( $message ); |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
return $return; |
| 85 |
} |
| 86 |
|
| 87 |
function update_cache_meta( $message_ids, $provided_ids ){ |
| 88 |
return update_meta_cache( 'fep_message', $message_ids ); |
| 89 |
} |
| 90 |
|
| 91 |
function update_cache_user( $user_ids, $provided_ids ){ |
| 92 |
cache_users( $user_ids ); |
| 93 |
} |
| 94 |
|
| 95 |
function update_cache_participants( $message_ids, $provided_ids ){ |
| 96 |
global $wpdb; |
| 97 |
|
| 98 |
$need_query = array(); |
| 99 |
$return = array(); |
| 100 |
|
| 101 |
foreach( $message_ids as $message_id ){ |
| 102 |
$cached = wp_cache_get( $message_id, 'fep_participants' ); |
| 103 |
if( false === $cached ){ |
| 104 |
$need_query[] = $message_id; |
| 105 |
} elseif( in_array( $message_id, $provided_ids ) ) { |
| 106 |
$return[ $message_id ] = $cached; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
if( $need_query ){ |
| 111 |
$participants = array(); |
| 112 |
|
| 113 |
$results = $wpdb->get_results( 'SELECT * FROM ' . FEP_PARTICIPANT_TABLE . ' WHERE mgs_id IN (' . implode( ', ', $need_query ) . ')' ); |
| 114 |
|
| 115 |
foreach ( $results as $key => $result ) { |
| 116 |
$participants[ (int) $result->mgs_id ][] = $result; |
| 117 |
} |
| 118 |
foreach( $need_query as $id ){ |
| 119 |
if( ! isset( $participants[ $id ] ) ){ |
| 120 |
$participants[ $id ] = array(); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
foreach( $participants as $mgs_id => $participant ){ |
| 125 |
wp_cache_add( $mgs_id, $participant, 'fep_participants' ); |
| 126 |
|
| 127 |
if( in_array( $mgs_id, $provided_ids ) ){ |
| 128 |
$return[ $mgs_id ] = $participant; |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
return $return; |
| 133 |
} |
| 134 |
|
| 135 |
function update_cache_attachments( $message_ids, $provided_ids ){ |
| 136 |
global $wpdb; |
| 137 |
|
| 138 |
$need_query = array(); |
| 139 |
$return = array(); |
| 140 |
|
| 141 |
foreach( $message_ids as $message_id ){ |
| 142 |
$cached = wp_cache_get( $message_id, 'fep_attachments' ); |
| 143 |
if( false === $cached ){ |
| 144 |
$need_query[] = $message_id; |
| 145 |
} elseif( in_array( $message_id, $provided_ids ) ) { |
| 146 |
$return[ $message_id ] = $cached; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
if( $need_query ){ |
| 151 |
$attachments = array(); |
| 152 |
|
| 153 |
$results = $wpdb->get_results( 'SELECT * FROM ' . FEP_ATTACHMENT_TABLE . ' WHERE mgs_id IN (' . implode( ', ', $need_query ) . ')' ); |
| 154 |
|
| 155 |
foreach ( $results as $key => $result ) { |
| 156 |
$attachments[ (int) $result->mgs_id ][] = $result; |
| 157 |
} |
| 158 |
foreach( $need_query as $id ){ |
| 159 |
if( ! isset( $attachments[ $id ] ) ){ |
| 160 |
$attachments[ $id ] = array(); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
foreach( $attachments as $mgs_id => $attachment ){ |
| 165 |
wp_cache_add( $mgs_id, $attachment, 'fep_attachments' ); |
| 166 |
|
| 167 |
if( in_array( $mgs_id, $provided_ids ) ){ |
| 168 |
$return[ $mgs_id ] = $attachment; |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
return $return; |
| 173 |
} |
| 174 |
|
| 175 |
} //END Class |
| 176 |
|
| 177 |
|
| 178 |
|