: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* Locates feeds on the site and logs information about them in the database.
use TwitterFeed\Builder\CTF_Db;
if ( ! defined( 'ABSPATH' ) ) {
private $expiration_time;
private $matching_entries;
public function __construct( $feed_details ) {
* Example of how $feed_details is structured
* 'feed_id' => $transient_name,
* 'post_id' => get_the_ID(),
if ( isset( $feed_details['atts'] ) && ! empty( $feed_details['atts']['feed'] ) ) {
$feed_details['feed_id'] = '*' . $feed_details['atts']['feed'];
$this->feed_details = $feed_details;
$this->matching_entries = array();
$this->expiration_time = time() - 2 * WEEK_IN_SECONDS;
* Returns records that match the post ID and feed ID
* of the feed being located
public function retrieve_matching_entries() {
$feed_locator_table_name = esc_sql( $wpdb->prefix . CTF_FEED_LOCATOR );
$results = $wpdb->get_results( $wpdb->prepare("
FROM $feed_locator_table_name
AND feed_id = %s", $this->feed_details['location']['post_id'], $this->feed_details['feed_id'] ),ARRAY_A );
* Add feed being located to the database
public function insert_entry() {
if ( empty( $this->feed_details['location']['post_id'] ) ) {
$feed_locator_table_name = esc_sql( $wpdb->prefix . CTF_FEED_LOCATOR );
$affected = $wpdb->query( $wpdb->prepare( "INSERT INTO $feed_locator_table_name
$this->feed_details['feed_id'],
$this->feed_details['location']['post_id'],
$this->feed_details['location']['html'],
ctf_json_encode( $this->feed_details['atts'] ),
date( 'Y-m-d H:i:s' ) ) );
* Update a record based on the existing "id" column. Location can change
* from "unknown" to one of footer, content, header, or sidebar.
public function update_entry( $id, $location ) {
$feed_locator_table_name = esc_sql( $wpdb->prefix . CTF_FEED_LOCATOR );
$query = $wpdb->query( $wpdb->prepare( "
UPDATE $feed_locator_table_name
SET last_update = %s, html_location = %s
WHERE id = %d;", date( 'Y-m-d H:i:s' ), $location, $id ) );
* Processes a feed being located based on whether or not the record
* exists as well as whether or not an unknown location needs to be
public function add_or_update_entry() {
if ( empty( $this->feed_details['feed_id'] ) ) {
$this->matching_entries = $this->retrieve_matching_entries();
if ( empty( $this->matching_entries ) ) {
$matching_indices = array();
$matched_location = false;
$non_unknown_match = false;
foreach ( $this->matching_entries as $index => $matching_entry ) {
$details_atts = is_array( $this->feed_details['atts'] ) ? $this->feed_details['atts'] : array();
$matching_atts = json_decode( $matching_entry['shortcode_atts'], true );
if ( ! is_array( $matching_atts ) ) {
$matching_atts = array();
$atts_diff = array_diff( $matching_atts, $details_atts ); // determines if the shortcode settings match the shortcode settings of an existing feed
if ( empty( $atts_diff ) ) {
$matching_indices[] = $matching_entry['id'];
if ( $matching_entry['html_location'] === $this->feed_details['location']['html'] ) {
$matched_location = $index;
$this->update_entry( $matching_entry['id'], $matching_entry['html_location'] );
if ( $matching_entry['html_location'] !== 'unknown' ) {
$non_unknown_match = $index;
if ( false === $matched_location ) {
// if there is no matched location, there is only one feed on the page, and the feed being checked has an unknown location, update the known location
if ( count( $matching_indices ) === 1
&& $this->feed_details['location']['html'] === 'unknown'
&& false !== $non_unknown_match ) {
$this->update_entry( $this->matching_entries[ $non_unknown_match ]['id'], $this->matching_entries[ $non_unknown_match ]['html_location'] );
if ( $this->feed_details['location']['html'] !== 'unknown'
&& false !== $unknown_match ) {
$this->update_entry( $this->matching_entries[ $unknown_match ]['id'], $this->feed_details['location']['html'] );
* Old feeds are only detected once a day to keep load on the server low.
public static function should_clear_old_locations() {
$ctf_statuses_option = get_option( 'ctf_statuses', array() );
$last_old_feed_check = isset( $ctf_statuses_option['feed_locator']['last_check'] ) ? $ctf_statuses_option['feed_locator']['last_check'] : 0;
return $last_old_feed_check < time() - DAY_IN_SECONDS;
* Old feeds are removed if they haven't been updated in two weeks.
public static function delete_old_locations() {
$feed_locator_table_name = esc_sql( $wpdb->prefix . CTF_FEED_LOCATOR );
$two_weeks_ago = date( 'Y-m-d H:i:s', time() - 2 * WEEK_IN_SECONDS );
$affected = $wpdb->query( $wpdb->prepare(
"DELETE FROM $feed_locator_table_name WHERE last_update < %s;", $two_weeks_ago ) );
$ctf_statuses_option = get_option( 'ctf_statuses', array() );
$ctf_statuses_option['feed_locator']['last_check'] = time();
if ( ! isset( $ctf_statuses_option['feed_locator']['initialized'] ) ) {
$ctf_statuses_option['feed_locator']['initialized'] = time();
update_option( 'ctf_statuses', $ctf_statuses_option, true );
* Feeds are located with the page load randomly (5% or 1/30 loads)
* to decrease load on the server.
* If the locating just started (within 5 minutes) it is run more often
* to collect feed locations quickly.
public static function should_do_locating() {
$ctf_statuses_option = get_option( 'ctf_statuses', array() );
if ( isset( $ctf_statuses_option['feed_locator']['initialized'] )
&& $ctf_statuses_option['feed_locator']['initialized'] < (time() - 300) ) {
$should_do_locating = rand( 1, 10 ) === 10;
$should_do_locating = rand( 1, 30 ) === 30;
$should_do_locating = apply_filters( 'ctf_should_do_locating', $should_do_locating );
return $should_do_locating;
* Simliar to the should_do_locating method but will add an additional
* database query to see if there is a feed with an unknown location that
* matches the details of the feed in question.
public static function should_do_ajax_locating( $feed_id, $post_id ) {
$should_do_locating = rand( 1, 50 ) === 50;
$should_do_locating = apply_filters( 'ctf_should_do_ajax_locating', $should_do_locating );
return $should_do_locating;
* Feeds are located with the page load randomly (1/30 loads)
* to decrease load on the server.
* If the locating just started (within 5 minutes) it is run more often
* to collect feed locations quickly.
public static function entries_need_locating( $feed_id, $post_id ) {
$feed_locator_table_name = esc_sql( $wpdb->prefix . CTF_FEED_LOCATOR );
$one_day_ago = date( 'Y-m-d H:i:s', time() - DAY_IN_SECONDS );
$results = $wpdb->get_results( $wpdb->prepare("
FROM $feed_locator_table_name
WHERE html_location = 'unknown'
LIMIT 1;", $one_day_ago, $feed_id, $post_id ),ARRAY_A );
return isset( $results[0] );
* A custom table stores locations
public static function create_table() {
$feed_locator_table_name = esc_sql( $wpdb->prefix . CTF_FEED_LOCATOR );
if ( $wpdb->get_var( "show tables like '$feed_locator_table_name'" ) != $feed_locator_table_name ) {
$sql = "CREATE TABLE " . $feed_locator_table_name . " (
id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
feed_id VARCHAR(50) DEFAULT '' NOT NULL,
post_id BIGINT(20) UNSIGNED NOT NULL,
html_location VARCHAR(50) DEFAULT 'unknown' NOT NULL,
shortcode_atts LONGTEXT NOT NULL,
$error = $wpdb->last_error;
$query = $wpdb->last_query;
if ( $wpdb->get_var( "show tables like '$feed_locator_table_name'" ) != $feed_locator_table_name ) {
$wpdb->query( "ALTER TABLE $feed_locator_table_name ADD INDEX feed_id (feed_id)" );
$wpdb->query( "ALTER TABLE $feed_locator_table_name ADD INDEX post_id (post_id)" );
* Counts the number of unique feeds in the database.
public static function count_unique() {
$feed_locator_table_name = esc_sql( $wpdb->prefix . CTF_FEED_LOCATOR );
$results_content = $wpdb->get_results( "
SELECT COUNT(*) AS num_entries
FROM $feed_locator_table_name
WHERE html_location = 'content'
$results_other = $wpdb->get_results( "
SELECT COUNT(*) AS num_entries
FROM $feed_locator_table_name
WHERE html_location != 'content'
AND html_location != 'unknown'
if ( isset( $results_content[0]['num_entries'] ) ) {
$total += (int)$results_content[0]['num_entries'];
if ( isset( $results_other[0]['num_entries'] ) ) {
$total += (int)$results_other[0]['num_entries'];
* Creates a summary of the located feeds in an array
public static function summary() {
$feed_locator_table_name = esc_sql( $wpdb->prefix . CTF_FEED_LOCATOR );
'label' => __( 'Content', 'custom-twitter-feeds' ),
'html_locations' => array( 'content', 'unknown' )
'label' => __( 'Header', 'custom-twitter-feeds' ),
'html_locations' => array( 'header' ),
'label' => __( 'Sidebar', 'custom-twitter-feeds' ),
'html_locations' => array( 'sidebar' ),
'label' => __( 'Footer', 'custom-twitter-feeds' ),
'html_locations' => array( 'footer' ),
$one_result_found = false;
foreach ( $locations as $key => $location ) {
$in = implode( "', '", $location['html_locations'] );
$group_by = isset( $location['group_by'] ) ? "GROUP BY " . $location['group_by'] : "";
$results = $wpdb->get_results("
FROM $feed_locator_table_name
WHERE html_location IN ('$in')
ORDER BY last_update ASC",ARRAY_A );
if ( isset( $results[0] ) ) {
$one_result_found = true;
$locations[ $key ]['results'] = $results;
if ( ! $one_result_found ) {
* Queries the locator table for feeds by feed_id
* @return array|object|null
public static function twitter_feed_locator_query( $args ) {
$feed_locator_table_name = esc_sql( $wpdb->prefix . CTF_FEED_LOCATOR );
if ( isset( $args['group_by'] ) ) {
$group_by = "GROUP BY " . esc_sql( $args['group_by'] );
$location_string = 'content';
if ( isset( $args['html_location'] ) ) {
$locations = array_map( 'esc_sql', $args['html_location'] );
$location_string = implode( "', '", $locations );
if ( isset( $args['page'] ) ) {
$page = (int)$args['page'] - 1;
$offset = max( 0, $page * CTF_Db::RESULTS_PER_PAGE );
if ( isset( $args['shortcode_atts'] ) ) {
$results = $wpdb->get_results( $wpdb->prepare("
FROM $feed_locator_table_name
WHERE shortcode_atts = %s
AND html_location IN ( '$location_string' )
OFFSET %d;", $args['shortcode_atts'], CTF_Db::RESULTS_PER_PAGE, $offset ),ARRAY_A );
$results = $wpdb->get_results( $wpdb->prepare("
FROM $feed_locator_table_name
AND html_location IN ( '$location_string' )
OFFSET %d;", $args['feed_id'], CTF_Db::RESULTS_PER_PAGE, $offset ),ARRAY_A );
* Queries all legacy feeds that have been located
* @return array|object|null
public static function legacy_twitter_feed_locator_query( $args ) {
$feed_locator_table_name = esc_sql( $wpdb->prefix . CTF_FEED_LOCATOR );