Services
1 month ago
AuthRoutine.php
1 month ago
CronUpdaterManager.php
1 month ago
ErrorReport.php
1 month ago
Request.php
1 month ago
SettingsFilter.php
1 month ago
SinglePostCache.php
1 month ago
TweetAggregator.php
1 month ago
TweetFilterer.php
1 month ago
TweetRepository.php
1 month ago
TweetSetModifier.php
1 month ago
TweetRepository.php
264 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class TweetRepository |
| 4 | * |
| 5 | * Aggregates Tweets stored in cache. |
| 6 | * |
| 7 | * @since 2.1 |
| 8 | */ |
| 9 | namespace TwitterFeed\SmashTwitter; |
| 10 | |
| 11 | use TwitterFeed\CtfCache; |
| 12 | use TwitterFeed\CTF_Feed; |
| 13 | |
| 14 | class TweetRepository |
| 15 | { |
| 16 | protected $types_and_terms; |
| 17 | |
| 18 | protected $cache_id; |
| 19 | |
| 20 | /** |
| 21 | * @var TweetAggregator |
| 22 | */ |
| 23 | protected $tweet_aggregator; |
| 24 | |
| 25 | /** |
| 26 | * @var CtfCache |
| 27 | */ |
| 28 | protected $feed_cache; |
| 29 | |
| 30 | /** |
| 31 | * @var TweetSetModifier |
| 32 | */ |
| 33 | protected $tweet_set_modifier; |
| 34 | |
| 35 | protected $tweets; |
| 36 | |
| 37 | protected $header_data; |
| 38 | |
| 39 | /** |
| 40 | * @var ErrorReport |
| 41 | */ |
| 42 | protected $error_report; |
| 43 | |
| 44 | public function __construct( $types_and_terms, $cache_id, TweetAggregator $tweet_aggregator, CtfCache $feed_cache, TweetSetModifier $tweet_set_modifier, ErrorReport $error_report) |
| 45 | { |
| 46 | $this->types_and_terms = $types_and_terms; |
| 47 | |
| 48 | $this->tweet_aggregator = $tweet_aggregator; |
| 49 | |
| 50 | $this->feed_cache = $feed_cache; |
| 51 | |
| 52 | $this->tweet_set_modifier = $tweet_set_modifier; |
| 53 | |
| 54 | $this->error_report = $error_report; |
| 55 | |
| 56 | $this->cache_id = $cache_id; |
| 57 | |
| 58 | } |
| 59 | |
| 60 | public function get_errors() |
| 61 | { |
| 62 | return $this->statuses['errors']; |
| 63 | } |
| 64 | |
| 65 | public function set_errors( $errors_array ) |
| 66 | { |
| 67 | $this->statuses['errors'] = $errors_array; |
| 68 | } |
| 69 | |
| 70 | public function add_error( $message, $instructions ) |
| 71 | { |
| 72 | $this->statuses['errors'][] = array( |
| 73 | 'message' => $message, |
| 74 | 'directions' => $instructions |
| 75 | ); |
| 76 | } |
| 77 | |
| 78 | public function set_tweets($tweets) |
| 79 | { |
| 80 | $this->tweets = $tweets; |
| 81 | } |
| 82 | |
| 83 | public function set_header_data($header_data) |
| 84 | { |
| 85 | $this->header_data = $header_data; |
| 86 | } |
| 87 | |
| 88 | public function get_tweets() |
| 89 | { |
| 90 | return $this->tweets; |
| 91 | } |
| 92 | |
| 93 | public function get_set_cache( $doing_cron_update = false ) |
| 94 | { |
| 95 | $this->tweets = $this->feed_cache->get_transient( $this->cache_id ); |
| 96 | |
| 97 | // Cache might come as empty or as an empty array string. |
| 98 | if ( ! $this->tweets || $doing_cron_update ) { |
| 99 | $this->tweets = $this->update_posts_cache(); |
| 100 | $endpoint = ! empty( $this->types_and_terms[0] ) ? $this->types_and_terms[0][0] : ''; |
| 101 | if ( $endpoint === 'usertimeline' && ! empty( $this->tweets[0]['user'] ) ) { |
| 102 | $this->set_header_data( $this->tweets[0]['user'] ); |
| 103 | $this->update_header_cache(); |
| 104 | } |
| 105 | } else { |
| 106 | if ( ! is_array( $this->tweets ) ) { |
| 107 | $this->tweets = json_decode( $this->tweets, true ); |
| 108 | } |
| 109 | $header_data = $this->feed_cache->get_transient( $this->cache_id . '_header' ); |
| 110 | $this->set_header_data( $header_data ); |
| 111 | } |
| 112 | |
| 113 | $this->set_tweets($this->tweets); |
| 114 | } |
| 115 | |
| 116 | public function paged_cache( $offset, $num ) { |
| 117 | $this->tweets = false; |
| 118 | $maybe_tweets = $this->feed_cache->get_transient( $this->cache_id ); |
| 119 | if ( $maybe_tweets ) { |
| 120 | $tweets_array = json_decode( $maybe_tweets, true ); |
| 121 | } else { |
| 122 | return false; |
| 123 | } |
| 124 | |
| 125 | if ( ! empty( $tweets_array ) && is_array( $tweets_array ) ) { |
| 126 | if ( $offset ) { |
| 127 | $num_available = count( $tweets_array ); |
| 128 | if ( $offset >= $num_available) { |
| 129 | return false; |
| 130 | } else { |
| 131 | if ( $num_available < $offset + $num ) { |
| 132 | $length = $num_available - $offset < 0 ? 0 : $num_available - $offset; |
| 133 | $this->tweets = array_slice( $tweets_array, $offset, $length ); |
| 134 | } else { |
| 135 | $this->tweets = array_slice( $tweets_array, $offset ); |
| 136 | } |
| 137 | |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | return true; |
| 143 | } |
| 144 | |
| 145 | public function update_posts_cache() |
| 146 | { |
| 147 | foreach ( $this->types_and_terms as $type_and_term ) { |
| 148 | $endpoint = $type_and_term[0]; |
| 149 | $term = $type_and_term[1]; |
| 150 | |
| 151 | $remote_posts = $this->get_remote_posts( $endpoint, $term ); |
| 152 | |
| 153 | if ( isset( $remote_posts[0]['id_str'] ) ) { |
| 154 | $remote_posts = CTF_Feed::reduceTweetSetData( $remote_posts ); |
| 155 | |
| 156 | $this->cache_single_posts_from_set($remote_posts, $endpoint, $term ); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | $posts = $this->posts_from_db(); |
| 161 | |
| 162 | $this->update_cache( $posts ); |
| 163 | |
| 164 | return $posts; |
| 165 | } |
| 166 | |
| 167 | public function posts_from_db() { |
| 168 | $aggregator = new TweetAggregator(); |
| 169 | |
| 170 | $posts = $aggregator->db_post_set( $this->types_and_terms ); |
| 171 | |
| 172 | return $aggregator->normalize_db_post_set( $posts ); |
| 173 | } |
| 174 | |
| 175 | public function update_cache( $posts ) { |
| 176 | $this->feed_cache->set_transient( $this->cache_id, json_encode( $posts ) ); |
| 177 | } |
| 178 | |
| 179 | public function update_header_cache() |
| 180 | { |
| 181 | $this->feed_cache->set_transient( $this->cache_id . '_header', json_encode( $this->header_data ), false, '' ); |
| 182 | } |
| 183 | |
| 184 | public function get_remote_posts( $endpoint, $term ) |
| 185 | { |
| 186 | $ctf_options = get_option( 'ctf_options', array() ); |
| 187 | |
| 188 | $site_access_token = ! empty( $ctf_options[ CTF_SITE_ACCESS_TOKEN_KEY ] ) ? $ctf_options[ CTF_SITE_ACCESS_TOKEN_KEY ] : false; |
| 189 | |
| 190 | if ( empty( $site_access_token ) ) { |
| 191 | $this->error_report->process_error( 'could_not_authenticate', true ); |
| 192 | return array(); |
| 193 | } |
| 194 | |
| 195 | $request = new Request( $endpoint, $term, array(), $site_access_token ); |
| 196 | |
| 197 | $response = $request->fetch(); |
| 198 | |
| 199 | // Prevent showing fatal error if the site access token is invalid. |
| 200 | if ( is_wp_error( $response ) ) { |
| 201 | $this->error_report->process_error( $response, true ); |
| 202 | return []; |
| 203 | } |
| 204 | $error = $request->get_error(); |
| 205 | if ( ! empty( $error ) ) { |
| 206 | $this->error_report->process_error( $error, true ); |
| 207 | } |
| 208 | |
| 209 | if ( ! empty( $response[0]['id_str'] ) ) { |
| 210 | $this->tweet_set_modifier->set_tweet_set( $response ); |
| 211 | $this->tweet_set_modifier->hydrate_tweet_set(); |
| 212 | $response = $this->tweet_set_modifier->get_hydrated_tweet_set(); |
| 213 | } |
| 214 | |
| 215 | return $response; |
| 216 | } |
| 217 | |
| 218 | public function cache_single_posts_from_set( $posts, $endpoint, $term ) |
| 219 | { |
| 220 | foreach ( $posts as $single_tweet ) { |
| 221 | $single_post_cache = new SinglePostCache( $single_tweet, $endpoint, $term ); |
| 222 | |
| 223 | if ( ! $single_post_cache->db_record_exists() ) { |
| 224 | $single_post_cache->store(); |
| 225 | } else { |
| 226 | if ( ! $single_post_cache->db_record_exists_for_endpoint_and_term() ) { |
| 227 | $single_post_cache->update_single( true ); |
| 228 | } else { |
| 229 | $single_post_cache->update_single( false ); |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | public function get_post_set_page($page = 1) |
| 236 | { |
| 237 | $posts = $this->get_posts(); |
| 238 | |
| 239 | $max = $this->settings['numPostDesktop']; |
| 240 | if ($this->settings['numPostTablet'] > $this->settings['numPostDesktop']) { |
| 241 | $max = $this->settings['numPostTablet']; |
| 242 | } |
| 243 | if ($this->settings['numPostMobile'] > $this->settings['numPostTablet']) { |
| 244 | $max = $this->settings['numPostMobile']; |
| 245 | } |
| 246 | |
| 247 | $offset = ($page - 1) * $max; |
| 248 | return is_array( $posts ) ? array_slice($posts, $offset, $max) : []; |
| 249 | } |
| 250 | |
| 251 | public function is_last_page($page) |
| 252 | { |
| 253 | $posts = $this->get_posts(); |
| 254 | $posts_per_page = $this->settings['numPostDesktop']; |
| 255 | if ($this->settings['numPostTablet'] > $this->settings['numPostDesktop']) { |
| 256 | $posts_per_page = $this->settings['numPostTablet']; |
| 257 | } |
| 258 | if ($this->settings['numPostMobile'] > $this->settings['numPostTablet']) { |
| 259 | $posts_per_page = $this->settings['numPostMobile']; |
| 260 | } |
| 261 | |
| 262 | return count($posts) <= ($page * (int) $posts_per_page); |
| 263 | } |
| 264 | } |