Edit File by line

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