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
TweetAggregator.php
127 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class FeedCache |
| 4 | * |
| 5 | * @since 1.0 |
| 6 | */ |
| 7 | namespace TwitterFeed\SmashTwitter; |
| 8 | |
| 9 | class TweetAggregator { |
| 10 | |
| 11 | /** |
| 12 | * @var array |
| 13 | */ |
| 14 | private $post_set = array(); |
| 15 | |
| 16 | private $upload_dir; |
| 17 | |
| 18 | private $upload_url; |
| 19 | |
| 20 | protected $missing_media_found; |
| 21 | |
| 22 | public function __construct() { |
| 23 | |
| 24 | $upload = wp_upload_dir(); |
| 25 | $upload_dir = $upload['basedir']; |
| 26 | $upload_dir = trailingslashit( $upload_dir ) . CTF_UPLOADS_NAME; |
| 27 | $this->upload_dir = $upload_dir; |
| 28 | |
| 29 | $upload_url = trailingslashit( $upload['baseurl'] ) . CTF_UPLOADS_NAME; |
| 30 | $this->upload_url = $upload_url; |
| 31 | $this->missing_media_found = false; |
| 32 | } |
| 33 | |
| 34 | public function add( $post_set ) { |
| 35 | return $this->post_set = array_merge( $this->post_set, $post_set ); |
| 36 | } |
| 37 | |
| 38 | public function db_post_set( $type_and_terms ) { |
| 39 | if ( ! is_array( $type_and_terms ) ) { |
| 40 | return array(); |
| 41 | } |
| 42 | global $wpdb; |
| 43 | $posts_table_name = $wpdb->prefix . CTF_POSTS_TABLE; |
| 44 | $feeds_posts_table_name = $wpdb->prefix . CTF_FEEDS_POSTS_TABLE; |
| 45 | |
| 46 | if ( count( $type_and_terms ) === 1 ) { |
| 47 | $type = $type_and_terms[0][0]; |
| 48 | $term = $type_and_terms[0][1]; |
| 49 | $results = $wpdb->get_results( |
| 50 | $wpdb->prepare( "SELECT * FROM $posts_table_name as p |
| 51 | LEFT JOIN $feeds_posts_table_name as f ON p.id = f.id |
| 52 | WHERE f.type = %s AND f.term = %s ORDER BY p.time_stamp DESC LIMIT 150;", $type, $term ), ARRAY_A ); |
| 53 | } else { |
| 54 | |
| 55 | $escaped_where_clauses = array(); |
| 56 | foreach ( $type_and_terms as $type_and_term ) { |
| 57 | $type = $type_and_term[0]; |
| 58 | $term = $type_and_term[1]; |
| 59 | |
| 60 | $escaped_where_clauses[] = "(f.type = '" . esc_sql( $type ) . "' AND f.term = '" . esc_sql( $term ) . "')"; |
| 61 | } |
| 62 | $escaped_where_clause_string = implode( ' OR ', $escaped_where_clauses ); |
| 63 | |
| 64 | if ( empty( $escaped_where_clause_string ) ) { |
| 65 | return array(); |
| 66 | } |
| 67 | |
| 68 | $results = $wpdb->get_results( "SELECT * FROM $posts_table_name as p |
| 69 | LEFT JOIN $feeds_posts_table_name as f ON p.id = f.id |
| 70 | WHERE $escaped_where_clause_string ORDER BY p.time_stamp DESC LIMIT 150;", ARRAY_A ); |
| 71 | } |
| 72 | |
| 73 | |
| 74 | return $results; |
| 75 | } |
| 76 | |
| 77 | public function normalize_db_post_set( $results ) { |
| 78 | $normalized_set = array(); |
| 79 | foreach ( $results as $result ) { |
| 80 | if ( ! empty( $result['json_data'] ) ) { |
| 81 | $post = json_decode( $result['json_data'], true ); |
| 82 | if ( ! empty( $post ) ) { |
| 83 | $post = self::add_local_image_urls( $post, $result ); |
| 84 | } |
| 85 | $normalized_set[] = $post; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | return $normalized_set; |
| 90 | } |
| 91 | |
| 92 | public function add_local_image_urls( $post, $result ) { |
| 93 | $return = $post; |
| 94 | return $return; |
| 95 | $base_url = $this->upload_url; |
| 96 | $resize_url = apply_filters( 'sbr_resize_url', trailingslashit( $base_url ) ); |
| 97 | |
| 98 | if ( ! empty( $post['reviewer']['avatar'] ) ) { |
| 99 | if ( ! empty( $result['avatar_id'] ) && $result['avatar_id'] !== 'error' ) { |
| 100 | $return['reviewer']['avatar_local'] = $resize_url . $result['avatar_id'] . '.png'; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | if ( ! empty( $post['media'] ) ) { |
| 105 | $sizes = ! empty( $result['sizes'] ) ? json_decode( $result['sizes'] ) : array(); |
| 106 | $i = 0; |
| 107 | foreach ( $post['media'] as $single_image ) { |
| 108 | if ( ! empty( $result['media_id'] ) && $result['media_id'] !== 'error' ) { |
| 109 | $return['media'][ $i ]['local'] = array(); |
| 110 | $media_id = $result['media_id']; |
| 111 | foreach ( $sizes as $size ) { |
| 112 | $local_url_for_size = $resize_url . $media_id . '-' . $i . '-' . $size . '.jpg'; |
| 113 | $return['media'][ $i ]['local'][ $size ] = $local_url_for_size; |
| 114 | } |
| 115 | } |
| 116 | $i ++; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | return $return; |
| 121 | } |
| 122 | |
| 123 | public function update_last_requested( $tweet_ids ) { |
| 124 | |
| 125 | } |
| 126 | } |
| 127 |