SinglePostCache.php
| 1 | <?php |
| 2 | /** |
| 3 | * Class SinglePostCache |
| 4 | * |
| 5 | * @since 1.0 |
| 6 | */ |
| 7 | namespace TwitterFeed\SmashTwitter; |
| 8 | class SinglePostCache { |
| 9 | |
| 10 | /** |
| 11 | * @var array |
| 12 | */ |
| 13 | private $post_data; |
| 14 | |
| 15 | private $storage_data; |
| 16 | private $endpoint; |
| 17 | private $term; |
| 18 | |
| 19 | private $posts_table_name; |
| 20 | |
| 21 | private $feeds_posts_table_name; |
| 22 | |
| 23 | public function __construct( $post_data, $endpoint, $term ) { |
| 24 | $this->post_data = $post_data; |
| 25 | |
| 26 | $this->storage_data = array( |
| 27 | 'media_id' => '', |
| 28 | 'sizes' => '[]', |
| 29 | 'aspect_ratio' => 1, |
| 30 | 'images_done' => 0 |
| 31 | ); |
| 32 | $this->endpoint = $endpoint; |
| 33 | |
| 34 | $this->term = $term; |
| 35 | |
| 36 | global $wpdb; |
| 37 | |
| 38 | $this->posts_table_name = $wpdb->prefix . CTF_POSTS_TABLE; |
| 39 | $this->feeds_posts_table_name = $wpdb->prefix . CTF_FEEDS_POSTS_TABLE; |
| 40 | } |
| 41 | |
| 42 | public function get_storage_data() { |
| 43 | return $this->storage_data; |
| 44 | } |
| 45 | |
| 46 | public function get_post_data() { |
| 47 | return $this->post_data; |
| 48 | } |
| 49 | |
| 50 | public function set_storage_data( $key, $value ){ |
| 51 | return $this->storage_data[ $key ] = $value; |
| 52 | } |
| 53 | |
| 54 | public function db_record_exists() { |
| 55 | $feed_id_match = $this->db_record(); |
| 56 | if ( ! empty( $feed_id_match ) ) { |
| 57 | $this->storage_data['id'] = $feed_id_match['id']; |
| 58 | |
| 59 | $this->storage_data['media_id'] = $feed_id_match['media_id']; |
| 60 | $this->storage_data['sizes'] = $feed_id_match['sizes']; |
| 61 | $this->storage_data['aspect_ratio'] = $feed_id_match['aspect_ratio']; |
| 62 | $this->storage_data['images_done'] = $feed_id_match['images_done']; |
| 63 | $this->storage_data['json_data'] = $feed_id_match['json_data']; |
| 64 | } |
| 65 | return null !== $feed_id_match; |
| 66 | } |
| 67 | |
| 68 | public function db_record_exists_for_endpoint_and_term() { |
| 69 | global $wpdb; |
| 70 | $feeds_posts_table_name = $this->feeds_posts_table_name; |
| 71 | |
| 72 | if( isset( $this->post_data['id_str'] ) ){ |
| 73 | $feed_id_match = $wpdb->get_results( $wpdb->prepare( |
| 74 | "SELECT * FROM $feeds_posts_table_name |
| 75 | WHERE id = %s AND type = %s and term = %s LIMIT 1", $this->storage_data['id'], $this->endpoint, $this->term ), ARRAY_A ); |
| 76 | |
| 77 | if ( ! empty( $feed_id_match[0] ) ) { |
| 78 | return $feed_id_match[0]; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | return null; |
| 83 | } |
| 84 | |
| 85 | public function db_record() { |
| 86 | global $wpdb; |
| 87 | $table_name = $this->posts_table_name; |
| 88 | if( isset( $this->post_data['id_str'] ) ){ |
| 89 | $feed_id_match = $wpdb->get_results( $wpdb->prepare( |
| 90 | "SELECT * FROM $table_name |
| 91 | WHERE twitter_id = %s LIMIT 1", $this->post_data['id_str'] ), ARRAY_A ); |
| 92 | |
| 93 | if ( ! empty( $feed_id_match[0] ) ) { |
| 94 | return $feed_id_match[0]; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | return null; |
| 99 | } |
| 100 | |
| 101 | public function store() { |
| 102 | |
| 103 | $to_store = array( |
| 104 | array( 'twitter_id', $this->post_data['id_str'], '%s' ), |
| 105 | array( 'created_on', date( 'Y-m-d H:i:s' ), '%s' ), |
| 106 | array( 'last_requested', date( 'Y-m-d H:i:s' ), '%s' ), |
| 107 | array( 'time_stamp', date('Y-m-d H:i:s', strtotime( $this->post_data['created_at'] ) ), '%s' ), |
| 108 | array( 'json_data', json_encode( $this->post_data ), '%s' ), |
| 109 | array( 'media_id', '', '%s' ), |
| 110 | array( 'sizes', '{}', '%s' ), |
| 111 | array( 'aspect_ratio', 1, '%d' ), |
| 112 | array( 'images_done', 0, '%d' ), |
| 113 | ); |
| 114 | $data = array(); |
| 115 | $format = array(); |
| 116 | foreach ( $to_store as $single_store ) { |
| 117 | $data[ $single_store[0] ] = $single_store[1]; |
| 118 | $format[] = $single_store[2]; |
| 119 | } |
| 120 | |
| 121 | global $wpdb; |
| 122 | $table_name = $this->posts_table_name; |
| 123 | $error = $wpdb->insert( $table_name, $data, $format ); |
| 124 | |
| 125 | if ( $error !== false ) { |
| 126 | $insert_id = $wpdb->insert_id; |
| 127 | $to_store = array( |
| 128 | array( 'id', $insert_id, '%s' ), |
| 129 | array('feed_id', $this->term . '_' . $this->endpoint, '%s'), |
| 130 | array('type', $this->endpoint, '%s'), |
| 131 | array('term', $this->term, '%s'), |
| 132 | ); |
| 133 | $data = array(); |
| 134 | $format = array(); |
| 135 | foreach ( $to_store as $single_store ) { |
| 136 | $data[ $single_store[0] ] = $single_store[1]; |
| 137 | $format[] = $single_store[2]; |
| 138 | } |
| 139 | |
| 140 | $table_name = $this->feeds_posts_table_name; |
| 141 | $error = $wpdb->insert( $table_name, $data, $format ); |
| 142 | |
| 143 | } else { |
| 144 | // log error |
| 145 | } |
| 146 | |
| 147 | } |
| 148 | |
| 149 | public function update_single( $insert_feeds_posts ) |
| 150 | { |
| 151 | $to_store = array( |
| 152 | array('json_data', json_encode( $this->post_data ), '%s'), |
| 153 | array('time_stamp', date('Y-m-d H:i:s', strtotime( $this->post_data['created_at'] ) ), '%s'), |
| 154 | array('last_requested', date('Y-m-d H:i:s'), '%s'), |
| 155 | ); |
| 156 | $data = array(); |
| 157 | $format = array(); |
| 158 | foreach ($to_store as $single_store) { |
| 159 | $data[$single_store[0]] = $single_store[1]; |
| 160 | $format[] = $single_store[2]; |
| 161 | } |
| 162 | |
| 163 | global $wpdb; |
| 164 | $table_name = $this->posts_table_name; |
| 165 | $where = array(); |
| 166 | $where_format = array(); |
| 167 | $where['twitter_id'] = $this->post_data['id_str']; |
| 168 | $where_format[] = '%s'; |
| 169 | |
| 170 | $error = $wpdb->update($table_name, $data, $where, $format, $where_format); |
| 171 | |
| 172 | if ( $insert_feeds_posts ) { |
| 173 | $to_store = array( |
| 174 | array('id', $this->storage_data['id'], '%s'), |
| 175 | array('feed_id', $this->term . '_' . $this->endpoint, '%s'), |
| 176 | array('type', $this->endpoint, '%s'), |
| 177 | array('term', $this->term, '%s'), |
| 178 | ); |
| 179 | $data = array(); |
| 180 | $format = array(); |
| 181 | foreach ($to_store as $single_store) { |
| 182 | $data[$single_store[0]] = $single_store[1]; |
| 183 | $format[] = $single_store[2]; |
| 184 | } |
| 185 | $error = $wpdb->insert($this->feeds_posts_table_name, $data, $format); |
| 186 | |
| 187 | } |
| 188 | |
| 189 | if ($error !== false) { |
| 190 | $insert_id = $wpdb->insert_id; |
| 191 | } else { |
| 192 | // log error |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 |