Admin
3 weeks ago
Builder
3 weeks ago
Integrations
3 weeks ago
SmashTwitter
3 weeks ago
UsageTracking
3 weeks ago
V2
3 weeks ago
blocks
3 weeks ago
CTF_Display_Elements.php
3 weeks ago
CTF_Feed.php
3 weeks ago
CTF_Feed_Locator.php
3 weeks ago
CTF_GDPR_Integrations.php
3 weeks ago
CTF_Parse.php
3 weeks ago
CTF_Settings.php
3 weeks ago
CtfAdmin.php
3 weeks ago
CtfCache.php
3 weeks ago
CtfFeed.php
3 weeks ago
CtfOauthConnect.php
3 weeks ago
SB_Twitter_Cron_Updater.php
3 weeks ago
admin-hooks.php
3 weeks ago
ctf-functions.php
3 weeks ago
notices.php
3 weeks ago
widget.php
3 weeks ago
CtfCache.php
228 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Twitter Feed Cache |
| 4 | * |
| 5 | * A bridge to use the new feed caches table |
| 6 | * |
| 7 | * @since 2.0 |
| 8 | */ |
| 9 | namespace TwitterFeed; |
| 10 | |
| 11 | class CtfCache { |
| 12 | |
| 13 | /** |
| 14 | * @var string |
| 15 | */ |
| 16 | private $feed_id; |
| 17 | |
| 18 | /** |
| 19 | * @var int |
| 20 | */ |
| 21 | private $page; |
| 22 | |
| 23 | /** |
| 24 | * @var int |
| 25 | */ |
| 26 | private $cache_time; |
| 27 | |
| 28 | public function __construct( $feed_id, $cache_time, $page ) { |
| 29 | $this->feed_id = $feed_id; |
| 30 | |
| 31 | $this->page = $page; |
| 32 | |
| 33 | $this->cache_time = $cache_time; |
| 34 | |
| 35 | if ( is_admin() ) { |
| 36 | $this->feed_id .= $this->maybe_customizer_suffix(); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Mimic WP get_transient |
| 42 | * |
| 43 | * @param string $transient_name |
| 44 | * |
| 45 | * @return false|string |
| 46 | */ |
| 47 | public function get_transient( $transient_name ) { |
| 48 | $results = $this->query_ctf_feed_caches( $transient_name ); |
| 49 | |
| 50 | if ( empty( $results ) ) { |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | if ( empty( $results[0]['cache_value'] ) ) { |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | if ( strtotime( $results[0]['last_updated'] ) < time() - $this->cache_time ) { |
| 59 | return false; |
| 60 | } |
| 61 | |
| 62 | return $results[0]['cache_value']; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Mimic WP get_option for persistent caches |
| 67 | * |
| 68 | * @param string $transient_name |
| 69 | * |
| 70 | * @return false|string |
| 71 | */ |
| 72 | public function get_persistent( $transient_name ) { |
| 73 | $results = $this->query_ctf_feed_caches( $transient_name ); |
| 74 | |
| 75 | if ( empty( $results ) ) { |
| 76 | $results = get_option( $transient_name, false ); |
| 77 | |
| 78 | if ( ! empty( $results ) ) { |
| 79 | return $results; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | if ( empty( $results ) ) { |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | if ( empty( $results[0]['cache_value'] ) ) { |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | return $results[0]['cache_value']; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Mimic WP set_transient |
| 96 | * |
| 97 | * @param $transient_name |
| 98 | * @param $value |
| 99 | * |
| 100 | * @return bool|int |
| 101 | */ |
| 102 | public function set_transient( $transient_name, $value, $backup = false, $cron_update = true ) { |
| 103 | return $this->update_or_insert( $transient_name, $value, $backup, $cron_update ); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Mimic WP update_option for persistent |
| 108 | * |
| 109 | * @param $transient_name |
| 110 | * @param $value |
| 111 | * |
| 112 | * @return bool|int |
| 113 | */ |
| 114 | public function set_persistent( $transient_name, $value ) { |
| 115 | return $this->update_or_insert( $transient_name, $value ); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Update or insert cache data |
| 120 | * |
| 121 | * @param string $transient_name |
| 122 | * @param string $cache_value |
| 123 | * @param bool $include_backup |
| 124 | * @param bool $cron_update |
| 125 | * |
| 126 | * @return bool|int|\mysqli_result|resource|null |
| 127 | */ |
| 128 | public function update_or_insert( $transient_name, $cache_value, $include_backup = true, $cron_update = true ) { |
| 129 | if ( strpos( $this->feed_id, '_CUSTOMIZER' ) !== false ) { |
| 130 | $cron_update = false; |
| 131 | } |
| 132 | |
| 133 | if ( ! empty( $this->page ) ) { |
| 134 | $cron_update = false; |
| 135 | } |
| 136 | |
| 137 | if ( $this->feed_id === 'legacy' ) { |
| 138 | $cron_update = false; |
| 139 | } |
| 140 | |
| 141 | global $wpdb; |
| 142 | $cache_table_name = $wpdb->prefix . 'ctf_feed_caches'; |
| 143 | |
| 144 | $sql = $wpdb->prepare( |
| 145 | " |
| 146 | SELECT * FROM $cache_table_name |
| 147 | WHERE feed_id = %s |
| 148 | AND cache_key = %s", |
| 149 | $this->feed_id, |
| 150 | $transient_name |
| 151 | ); |
| 152 | |
| 153 | $existing = $wpdb->get_results( $sql, ARRAY_A ); |
| 154 | $data = array(); |
| 155 | $where = array(); |
| 156 | $format = array(); |
| 157 | |
| 158 | $data['cache_value'] = $cache_value; |
| 159 | $format[] = '%s'; |
| 160 | |
| 161 | $data['last_updated'] = date( 'Y-m-d H:i:s' ); |
| 162 | $format[] = '%s'; |
| 163 | |
| 164 | if ( ! empty( $existing[0] ) ) { |
| 165 | $where['feed_id'] = $this->feed_id; |
| 166 | $where_format[] = '%s'; |
| 167 | |
| 168 | $where['cache_key'] = $transient_name; |
| 169 | $where_format[] = '%s'; |
| 170 | |
| 171 | $affected = $wpdb->update( $cache_table_name, $data, $where, $format, $where_format ); |
| 172 | } else { |
| 173 | $data['cache_key'] = $transient_name; |
| 174 | $format[] = '%s'; |
| 175 | |
| 176 | $data['cron_update'] = $cron_update === true ? 'yes' : ''; |
| 177 | $format[] = '%s'; |
| 178 | |
| 179 | $data['feed_id'] = $this->feed_id; |
| 180 | $format[] = '%s'; |
| 181 | |
| 182 | $affected = $wpdb->insert( $cache_table_name, $data, $format ); |
| 183 | } |
| 184 | |
| 185 | return $affected; |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * @param $transient_name |
| 190 | * |
| 191 | * @return array|false |
| 192 | */ |
| 193 | private function query_ctf_feed_caches( $transient_name ) { |
| 194 | $feed_cache = wp_cache_get( $transient_name ); |
| 195 | if ( false === $feed_cache || ! is_array( $feed_cache ) ) { |
| 196 | global $wpdb; |
| 197 | $cache_table_name = $wpdb->prefix . 'ctf_feed_caches'; |
| 198 | |
| 199 | $sql = $wpdb->prepare( |
| 200 | " |
| 201 | SELECT * FROM $cache_table_name |
| 202 | WHERE cache_key = %s |
| 203 | ORDER BY last_updated DESC", |
| 204 | $transient_name |
| 205 | ); |
| 206 | |
| 207 | $feed_cache = $wpdb->get_results( $sql, ARRAY_A ); |
| 208 | |
| 209 | wp_cache_set( $transient_name, $feed_cache ); |
| 210 | } |
| 211 | |
| 212 | return $feed_cache; |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * @return string |
| 217 | */ |
| 218 | private function maybe_customizer_suffix() { |
| 219 | $additional_suffix = ''; |
| 220 | $in_customizer = ! empty( $_POST['previewSettings'] ) || ( isset( $_GET['page'] ) && $_GET['page'] === 'ctf-feed-builder' ); |
| 221 | if ( $in_customizer ) { |
| 222 | $additional_suffix .= '_CUSTOMIZER'; |
| 223 | } |
| 224 | |
| 225 | return $additional_suffix; |
| 226 | } |
| 227 | } |
| 228 |