|
Server : LiteSpeed System : Linux server51.dnsbootclub.com 4.18.0-553.62.1.lve.el8.x86_64 #1 SMP Mon Jul 21 17:50:35 UTC 2025 x86_64 User : nandedex ( 1060) PHP Version : 8.1.33 Disable Function : NONE Directory : /home/nandedex/www/wp-content/plugins11/financial-pack-pro/api/ |
<?php
if ( ! function_exists( 'bsfp_prepare_crypto_currency_exchange_data' ) ) {
/**
* Generates the crypto exchange data for converter
*
* @throws \Exception
* @return array|bool false on failure or array on success
*/
function bsfp_prepare_crypto_currency_exchange_data() {
if ( $cached = get_transient( 'fp_exchange_rate' ) ) {
return $cached;
}
if ( ! $api = bsfp_api_instance( 'crypto_compare' ) ) {
return false;
}
if ( ! class_exists( 'BC_Financial_API_Exporter' ) ) {
require BS_Financial_Pack_Pro::dir_path( 'api/adapters/class-bs-financial-api-exporter.php' );
}
$api_adapter = new BC_Financial_API_Exporter( $api );
$api->set_currencies( array( 'usd' ) );
$api->set_items( array_keys( bsfp_get_cryptocurrencies_list() ) );
if ( ! $results = $api_adapter->get( array( 'prices' => array(), 'logo' => array() ) ) ) {
return false;
}
set_transient( 'fp_exchange_rate', $results, HOUR_IN_SECONDS );
return $results;
}
}
if ( ! function_exists( 'bsfp_get_exchange_rate' ) ) {
/**
* Get cash exchange rate based un united states dollar
*
* @param array|string $currencies currencies code list as array or 'all' for all currencies
* @param string $base_currency
* @param bool $full_detail
*
* @throws Exception
* @return array
*/
function bsfp_get_exchange_rate( $currencies = 'all', $base_currency = 'USD', $full_detail = false ) {
$rates = array();
$currencies_list = bsfp_get_currencies_list();
$base_currency = strtoupper( $base_currency );
if ( 'all' === $currencies ) {
$filtered = &$currencies_list;
} else {
$currencies = array_map( 'strtoupper', $currencies );
$currencies = array_flip( $currencies );
$filtered = array_intersect_key( $currencies_list, $currencies );
}
if ( 'USD' === $base_currency ) {
$transform_factor = 1;
} elseif ( isset( $currencies_list[ $base_currency ]['rate'] ) ) {
$transform_factor = $currencies_list[ $base_currency ]['rate'];
} else {
return array();
}
foreach ( $filtered as $code => $info ) {
$info['rate'] /= $transform_factor;
$rates[ $code ] = $full_detail ? $info : $info['rate'];
}
return $rates;
}
}
if ( ! function_exists( 'bsfp_filter_treading_item' ) ) {
/**
* @access internal usage
*
* @param $item
*
* @return bool
*/
function bsfp_filter_treading_item( $item ) {
return ! empty( $item['is_trending'] );
}
}
if ( ! function_exists( 'bsfp_get_events' ) ) {
/**
* Get events list from the source
*
* @param string $source bitcoin
*
* @return array|bool false on failure or array on success
*/
function bsfp_get_events( $source = 'bitcoin' ) {
$cache_key = 'fp_' . $source . '_events';
if ( $cached = get_transient( $cache_key ) ) {
return $cached;
}
switch ( $source ) {
case 'coindesk':
$data = bsfp_get_coindesk_events();
break;
case 'coinmarketcap':
$data = bsfp_get_coinmarketcap_events();
break;
case 'cointelegraph':
$data = bsfp_get_cointelegraph_events();
break;
case 'bitcoin':
default:
$data = bsfp_get_bitcoin_events();
}
if ( $data === false ) {
return false;
}
set_transient( $cache_key, $data, DAY_IN_SECONDS );
return $data;
}
}
if ( ! function_exists( 'bsfp_get_response' ) ) {
/**
* Fetch a web page body.
*
* @param string $url
*
* @since 1.4.0
* @return string|bool false on failure or response body on success.
*
*/
function bsfp_get_response( $url ) {
if ( ! class_exists( 'BS_Financial_Pack_Pro_Utilities' ) ) {
require BS_Financial_Pack_Pro::dir_path( 'includes/class-bs-financial-pack-pro-utilities.php' );
}
if ( ! function_exists( 'str_get_html' ) ) {
require BS_Financial_Pack_Pro::dir_path( 'includes/libs/simple_html_dom.php' );
}
if ( ! $response = BS_Financial_Pack_Pro_Utilities::request( $url, array(), true ) ) {
return false;
}
return $response;
}
}
if ( ! function_exists( 'bsfp_get_dom' ) ) {
/**
* Get dom instance by url.
*
* @param string $url
*
* @since 1.4.0
* @return simple_html_dom|bool
*/
function bsfp_get_dom( $url ) {
if ( $response = bsfp_get_response( $url ) ) {
return str_get_html( $response );
}
return false;
}
}
if ( ! function_exists( 'bsfp_get_bitcoin_events' ) ) {
/**
* Fetch BitCoin Events and Conferences from bitcoin.org
*
* @return array|bool false on failure or array on success
*/
function bsfp_get_bitcoin_events() {
if ( ! $dom = bsfp_get_dom( 'https://bitcoin.org/en/events' ) ) {
return false;
}
$events = array();
foreach ( $dom->find( '.events .post-list', 0 )->children as $event ) {
$date = htmlspecialchars_decode( $event->find( '.post-date', 0 )->innertext );
$url = $event->find( '.post-inner a', 0 )->href;
$location = htmlspecialchars_decode( $event->find( '.post-location', 0 )->innertext );
$title = htmlspecialchars_decode( $event->find( '.post-name', 0 )->innertext );
if ( $url[0] === '/' ) {
$url = 'https://bitcoin.org' . $url;
}
$events[] = compact( 'date', 'location', 'url', 'title' );
}
return $events;
}
}
if ( ! file_exists( 'bsfp_get_coinmarketcap_events' ) ) {
/**
* Fetch Crypto Events Calendar from coinmarketcap.com
*
* @since 1.4.0
* @return array|bool false on failure or array on success
*/
function bsfp_get_coinmarketcap_events() {
if ( ! $dom = bsfp_get_dom( 'https://coinmarketcap.com/events/' ) ) {
return false;
}
$events = array();
foreach ( $dom->find( '.cal-event' ) as $event ) {
$link = $event->find( '.cal-event__name .cmc-link', 0 );
$date = htmlspecialchars_decode( $event->find( '.cal-event__meta-item', 0 )->innertext );
$url = $link->href;
if ( $url[0] === '/' ) {
$url = 'https://coinmarketcap.com' . $url;
}
$location = htmlspecialchars_decode( $event->find( '.cal-event__meta-item', 1 )->innertext );
$title = htmlspecialchars_decode( $link->innertext );
$events[] = compact( 'date', 'location', 'url', 'title' );
}
return $events;
}
}
if ( ! file_exists( 'bsfp_get_cointelegraph_events' ) ) {
/**
* Fetch Crypto Events Calendar from cointelegraph.com
*
* @since 1.4.0
* @return array|bool false on failure or array on success
*/
function bsfp_get_cointelegraph_events() {
if ( ! $response = bsfp_get_response( 'https://cointelegraph.com/api/v1/events?limit=20&offset=0' ) ) {
return false;
}
$response = json_decode( $response );
if ( empty( $response->events ) ) {
return false;
}
$events = array();
foreach ( $response->events as $event ) {
$date = date_i18n( 'D, d M Y', strtotime( $event->start_date ) );
$url = $event->website;
$location = isset( $event->city->title ) ? $event->city->title : __( 'Unknown', 'better-studio' );
$title = $event->title;
$events[] = compact( 'date', 'location', 'url', 'title' );
}
return $events;
}
}
if ( ! file_exists( 'bsfp_get_coindesk_events' ) ) {
/**
* Fetch Blockchain Events And Conferences from coindesk.com
*
* @since 1.4.0
* @return array|bool false on failure or array on success
*/
function bsfp_get_coindesk_events() {
if ( ! $dom = bsfp_get_dom( 'https://www.coindesk.com/bitcoin-events' ) ) {
return false;
}
$events = array();
foreach ( $dom->find( '.events-list .table tr' ) as $event ) {
$link = $event->find( 'a', 0 );
$data = $event->children;
$date = htmlspecialchars_decode( $data[0]->innertext );
$url = $link->href;
if ( $url[0] === '/' ) {
$url = 'https://www.coindesk.com' . $url;
}
$location = htmlspecialchars_decode( $data[2]->innertext );
$title = htmlspecialchars_decode( $link->innertext );
$events[] = compact( 'date', 'location', 'url', 'title' );
}
return $events;
}
}
if ( ! function_exists( 'bsfp_get_top_crypto_currency_list' ) ) {
/**
* Get list of trending crypto currencies
*
* @return array|bool array on success
*/
function bsfp_get_top_cryptocurrency_list() {
if ( $cache = get_transient( 'fp_top_crypto_currencies' ) ) {
return $cache;
}
if ( ! class_exists( 'BS_Financial_Pack_Pro_Utilities' ) ) {
require BS_Financial_Pack_Pro::dir_path( 'includes/class-bs-financial-pack-pro-utilities.php' );
}
if ( ! $response = BS_Financial_Pack_Pro_Utilities::request( 'https://api.coinmarketcap.com/v1/ticker/?start=0&limit=100', array() ) ) {
return false;
}
set_transient( 'fp_top_crypto_currencies', $response, WEEK_IN_SECONDS );
return $response;
}
}
if ( ! function_exists( 'bsfp_get_stock_market_top_gainers_list' ) ) {
/**
* Get list of top gainers in us stock market
*
* @return array
*/
function bsfp_get_stock_market_top_gainers_list( $count = - 1 ) {
$file_path = BS_Financial_Pack_Pro::dir_path( 'includes/data/top-market-caps.json' );
$stocks = json_decode( file_get_contents( $file_path ), true );
if ( $count ) {
return array_slice( $stocks, 0, $count, true );
}
return $stocks;
}
}
if ( ! function_exists( 'bsfp_get_currencies_list' ) ) {
/**
* Get list of physical currencies
*
* @return array|bool array on success
*/
function bsfp_get_currencies_list( $count = - 1 ) {
$cache = get_transient( 'fp_physical_currencies' );
if ( is_array( $cache ) ) {
if ( $count ) {
return array_slice( $cache, 0, $count, true );
}
return $cache;
}
if ( ! class_exists( 'BS_Financial_Pack_Pro_Utilities' ) ) {
/** @noinspection PhpIncludeInspection */
require BS_Financial_Pack_Pro::dir_path( 'includes/class-bs-financial-pack-pro-utilities.php' );
}
if ( ! $response = BS_Financial_Pack_Pro_Utilities::request( 'http://core.betterstudio.com/api/currency/rates.json' ) ) {
$response = BS_Financial_Pack_Pro_Utilities::request( 'http://core-cf.betterstudio.com/api/currency/rates.json' );
}
if ( ! $response ) {
$response = get_option( 'fp_physical_currencies', array() );
set_transient( 'fp_physical_currencies', $response, HOUR_IN_SECONDS / 2 );
if ( $count ) {
return array_slice( $response, 0, $count, true );
}
return $response;
}
set_transient( 'fp_physical_currencies', $response, HOUR_IN_SECONDS );
update_option( 'fp_physical_currencies', $response );
if ( $count ) {
return array_slice( $response, 0, $count, true );
}
return $response;
}
}
if ( ! function_exists( 'bsfp_get_cryptocurrencies_list' ) ) {
/**
* Get list of all cryptocurrencies
*
* @param int $count
*
* @return array
*/
function bsfp_get_cryptocurrencies_list( $count = - 1 ) {
$file_path = BS_Financial_Pack_Pro::dir_path( 'includes/data/all-cryptocurrencies.json' );
$coins = json_decode( file_get_contents( $file_path ), true );
if ( $count ) {
return array_slice( $coins, 0, $count, true );
}
return $coins;
}
}