HEX
Server: Apache/2.4.65 (Debian)
System: Linux web6 5.10.0-36-amd64 #1 SMP Debian 5.10.244-1 (2025-09-29) x86_64
User: innocamp (1028)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: //home/buildfft/public_html/wp-content/plugins/custom-twitter-feeds/inc/CtfCache.php
<?php
/**
 * Twitter Feed Cache
 *
 * A bridge to use the new feed caches table
 *
 * @since 2.0
 */
namespace TwitterFeed;

class CtfCache {

	/**
	 * @var string
	 */
	private $feed_id;

	/**
	 * @var int
	 */
	private $page;

	/**
	 * @var int
	 */
	private $cache_time;

	public function __construct( $feed_id, $cache_time, $page ) {
		$this->feed_id = $feed_id;

		$this->page = $page;

		$this->cache_time = $cache_time;

		if ( is_admin() ) {
			$this->feed_id .= $this->maybe_customizer_suffix();
		}
	}

	/**
	 * Mimic WP get_transient
	 *
	 * @param string $transient_name
	 *
	 * @return false|string
	 */
	public function get_transient( $transient_name ) {
		$exclude_customizer_cache = true;

		if ( ! is_admin() ) {
			$exclude_customizer_cache = true;
		}
		$results = $this->query_ctf_feed_caches( $transient_name, $exclude_customizer_cache );

		if ( empty( $results ) ) {
			return false;
		}

		if ( empty( $results[0]['cache_value'] ) ) {
			return false;
		}

		if ( strtotime( $results[0]['last_updated'] ) < time() - $this->cache_time ) {
			return false;
		}

		return $results[0]['cache_value'];
	}

	/**
	 * Mimic WP get_option for persistent caches
	 *
	 * @param string $transient_name
	 *
	 * @return false|string
	 */
	public function get_persistent( $transient_name ) {
		$results = $this->query_ctf_feed_caches( $transient_name );

		if ( empty( $results ) ) {
			$results = get_option( $transient_name, false );

			if ( ! empty( $results ) ) {
				return $results;
			}
		}

		if ( empty( $results ) ) {
			return false;
		}

		if ( empty( $results[0]['cache_value'] ) ) {
			return false;
		}

		return $results[0]['cache_value'];
	}

	/**
	 * Mimic WP set_transient
	 *
	 * @param $transient_name
	 * @param $value
	 *
	 * @return bool|int
	 */
	public function set_transient( $transient_name, $value ) {
		return $this->update_or_insert( $transient_name, $value );
	}

	/**
	 * Mimic WP update_option for persistent
	 *
	 * @param $transient_name
	 * @param $value
	 *
	 * @return bool|int
	 */
	public function set_persistent( $transient_name, $value ) {
		return $this->update_or_insert( $transient_name, $value );
	}

	/**
	 * Update or insert cache data
	 *
	 * @param string $transient_name
	 * @param string $cache_value
	 * @param bool $include_backup
	 * @param bool $cron_update
	 *
	 * @return bool|int|\mysqli_result|resource|null
	 */
	public function update_or_insert( $transient_name, $cache_value, $include_backup = true, $cron_update = true ) {
		if ( strpos( $this->feed_id, '_CUSTOMIZER' ) !== false ) {
			$cron_update = false;
		}

		if ( ! empty( $this->page ) ) {
			$cron_update = false;
		}

		global $wpdb;
		$cache_table_name = $wpdb->prefix . 'ctf_feed_caches';

		$sql = $wpdb->prepare(
			"
			SELECT * FROM $cache_table_name
			WHERE feed_id = %s
			AND cache_key = %s",
			$this->feed_id,
			$transient_name
		);

		$existing = $wpdb->get_results( $sql, ARRAY_A );
		$data     = array();
		$where    = array();
		$format   = array();

		$data['cache_value'] = $cache_value;
		$format[]            = '%s';

		$data['last_updated'] = date( 'Y-m-d H:i:s' );
		$format[]             = '%s';

		if ( ! empty( $existing[0] ) ) {
			$where['feed_id'] = $this->feed_id;
			$where_format[]   = '%s';

			$where['cache_key'] = $transient_name;
			$where_format[]     = '%s';

			$affected = $wpdb->update( $cache_table_name, $data, $where, $format, $where_format );
		} else {
			$data['cache_key'] = $transient_name;
			$format[]          = '%s';

			$data['cron_update'] = $cron_update === true ? 'yes' : '';
			$format[]            = '%s';

			$data['feed_id'] = $this->feed_id;
			$format[]        = '%s';

			$affected = $wpdb->insert( $cache_table_name, $data, $format );
		}

		return $affected;
	}

	/**
	 * @param $transient_name
	 *
	 * @return array|false
	 */
	private function query_ctf_feed_caches( $transient_name, $exclude_customizer_cache = false ) {
		$feed_caches = wp_cache_get( $transient_name );
		if ( false === $feed_caches ) {
			global $wpdb;
			$cache_table_name = $wpdb->prefix . 'ctf_feed_caches';

			$sql = $wpdb->prepare(
				"
				SELECT * FROM $cache_table_name
				WHERE cache_key = %s
				ORDER BY last_updated DESC",
				$transient_name
			);

			$feed_caches = $wpdb->get_results( $sql, ARRAY_A );

			$customizer_cache_only_available = true;
			if ( $exclude_customizer_cache ) {
				foreach ( $feed_caches as $feed_cache ) {
					if ( strpos( $feed_cache['feed_id'], '_CUSTOMIZER' ) === false ) {
						$customizer_cache_only_available = false;
						$feed_caches = array( $feed_cache );
					}
				}
				if ( ! $customizer_cache_only_available ) {
					wp_cache_set( $transient_name, $feed_caches );
				} else {
					return array();
				}
			} else {
				wp_cache_set( $transient_name, $feed_caches );
			}
		}

		return $feed_caches;
	}

	/**
	 * @return string
	 */
	private function maybe_customizer_suffix() {
		$additional_suffix = '';
		$in_customizer     = ! empty( $_POST['previewSettings'] ) || ( isset( $_GET['page'] ) && $_GET['page'] === 'ctf-feed-builder' );
		if ( $in_customizer ) {
			$additional_suffix .= '_CUSTOMIZER';
		}

		return $additional_suffix;
	}
}