
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
<?php
/**
 * Functions for the plugin
 *
 * @link       https://codesupply.co
 * @since      1.0.0
 *
 * @package    Basic Twitter
 * @subpackage Includes
 */

/**
 * Convert links to clickable format
 *
 * @param string $name Specific template.
 * @param array  $twitter Array of twitter.
 * @param array  $params Array of params.
 */
function btw_template_handler( $name, $twitter, $params ) {
	$templates = apply_filters( 'btw_templates', array() );

	$new = isset( $templates['default'] ) ? false : true;

	if ( $new && count( $templates ) > 0 ) {
		$first_item = array_shift( $templates );

		if ( function_exists( $first_item['func'] ) ) {
			call_user_func( $first_item['func'], $twitter, $params );
		} else {
			call_user_func( 'btw_default_template', $twitter, $params );
		}
	} elseif ( isset( $templates[ $name ] ) && function_exists( $templates[ $name ]['func'] ) ) {
		call_user_func( $templates[ $name ]['func'], $twitter, $params );
	} else {
		call_user_func( 'btw_default_template', $twitter, $params );
	}
}

/**
 * Autoload files in the directory.
 *
 * @param string $path Directory path.
 * @param string $pattern Regex pattern.
 * @since    1.0.0
 */
function btw_autoload_files( $path, $pattern = false ) {
	if ( is_dir( $path ) ) {
		$files = scandir( $path );
	} else {
		return false;
	}

	// loop folders.
	foreach ( $files as $file ) {
		$path_file = $path . '/' . basename( $file );

		if ( $pattern && ! preg_match( "/$pattern/", basename( $file ) ) ) {
			continue;
		}

		if ( file_exists( $path_file ) && 'index.php' !== $file ) {

			if ( is_dir( $path_file ) && file_exists( $path_file . "/$file.php" ) ) {
				require_once( $path_file . "/$file.php" );
			} elseif ( is_file( $path_file ) && preg_match( '/\.php$/i', $path_file ) ) {
				require_once( $path_file );
			}
		}
	}
}

/**
 * Convert links to clickable format
 *
 * @param string $links       Text with links.
 * @param bool   $targetblank Open links in a new tab.
 * @return string Text with replaced links.
 */
function btw_convert_links( $links, $targetblank = true ) {

	// The target.
	$target = $targetblank ? ' target="_blank" ' : '';

	// Convert link to url.
	$links = preg_replace( '/\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[A-Z0-9+&@#\/%=~_|]/i', '<a href="\0" target="_blank">\0</a>', $links );

	// Convert @ to follow.
	$links = preg_replace( '/(@([_a-z0-9\-]+))/i', '<a href="http://twitter.com/$2" title="Follow $2" $target >$1</a>', $links );

	// Convert # to search.
	$links = preg_replace( '/(#([_a-z0-9\-]+))/i', '<a href="https://twitter.com/search?q=$2" title="Search $1" $target >$1</a>', $links );

	// Return links.
	return $links;
}

/**
 * Convert dates to readable format
 *
 * @param string $a Time string (timeformat).
 * @return string Formatted time.
 */
function btw_relative_time( $a ) {

	// Get current timestampt.
	$b = strtotime( 'now' );

	// Get timestamp when tweet created.
	$c = strtotime( $a );

	// Get difference.
	$d = $b - $c;

	// Calculate different time values.
	$minute = 60;
	$hour   = $minute * 60;
	$day    = $hour * 24;
	$week   = $day * 7;

	if ( is_numeric( $d ) && $d > 0 ) :

		// If less then 3 seconds.
		if ( $d < 3 ) {
			return esc_html__( 'right now', 'basic-twitter' );
		}

		// If less then minute.
		if ( $d < $minute ) {
			return floor( $d ) . esc_html__( ' seconds ago', 'basic-twitter' );
		}

		// If less then 2 minutes.
		if ( $d < $minute * 2 ) {
			return esc_html__( 'about 1 minute ago', 'basic-twitter' );
		}

		// If less then hour.
		if ( $d < $hour ) {
			return floor( $d / $minute ) . esc_html__( ' minutes ago', 'basic-twitter' );
		}

		// If less then 2 hours.
		if ( $d < $hour * 2 ) {
			return esc_html__( 'about 1 hour ago', 'basic-twitter' );
		}

		// If less then day.
		if ( $d < $day ) {
			return floor( $d / $hour ) . esc_html__( ' hours ago', 'basic-twitter' );
		}

		// If more then day, but less then 2 days.
		if ( $d > $day && $d < $day * 2 ) {
			return esc_html__( 'yesterday', 'basic-twitter' );
		}

		// If less then year.
		if ( $d < $day * 365 ) {
			return floor( $d / $day ) . esc_html__( ' days ago', 'basic-twitter' );
		}

		// else return more than a year.
		return esc_html__( 'over a year ago', 'basic-twitter' );
	endif;
}

/**
 * Get timeline twitter
 *
 * @param string $consumer_key Twitter consumer key.
 * @param string $consumer_secret Twitter consumer secret.
 * @param string $access_token Twitter access token.
 * @param string $access_token_secret Twitter access token secret.
 * @param array  $options Timeline options.
 * @return string Json twitter.
 */
function btw_get_twitter( $consumer_key, $consumer_secret, $access_token, $access_token_secret, $options ) {
	if ( ! empty( $consumer_key ) && ! empty( $consumer_secret ) && ! empty( $access_token ) && ! empty( $access_token_secret ) ) {
		$settings = array(
			'consumer_key'				=> $consumer_key,
			'consumer_secret'			=> $consumer_secret,
			'oauth_access_token'		=> $access_token,
			'oauth_access_token_secret'	=> $access_token_secret,
		);

		$url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
		$connect = new TwitterAPIExchange( $settings );
		$twitter = $connect->setGetfield( sprintf( '?%s', http_build_query( $options ) ) )->buildOauth( $url, 'GET' )->performRequest();

		return json_decode( $twitter );
	}
}

/**
 *
 * Check templates
 *
 * @return bool Logic var.
 */
function btw_templates_check() {
	$templates = apply_filters( 'btw_templates', array() );

	if ( count( $templates ) > 1 ) {
		return true;
	} else {
		return false;
	}
}

/**
 * Get templates options
 *
 * @return array Items.
 */
function btw_get_templates_options() {
	$options = array();

	$templates = apply_filters( 'btw_templates', array() );

	if ( $templates ) {
		foreach ( $templates as $key => $item ) {
			if ( isset( $item['name'] ) ) {
				$options[ $key ] = $item['name'];
			}
		}
	}

	return $options;
}
