: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* @author Alimuzzaman Alim <alimuzzamanalim@gmail.com>
* @link https://alim.dev/
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
namespace EmbedPress\Providers;
use Embera\Provider\ProviderAdapter;
use Embera\Provider\ProviderInterface;
class OpenSea extends ProviderAdapter implements ProviderInterface {
/** inline {@inheritdoc} */
protected $shouldSendRequest = false;
public static $curltimeout = 30;
/** inline {@inheritdoc} */
/** @var array Array with allowed params for the current Provider */
protected $allowedParams = [
'loadmoreBackgroundColor',
/** inline {@inheritdoc} */
protected static $hosts = [
/** inline {@inheritdoc} */
protected $httpsSupport = true;
public function getAllowedParams(){
return $this->allowedParams;
/** inline {@inheritdoc} */
public function validateUrl(Url $url) {
return (bool) (preg_match('~opensea\.io/(collection/(.*)|assets/.*[a-zA-Z0-9]+/[a-zA-Z0-9]+)~i', (string) $url));
/** inline {@inheritdoc} */
public function normalizeUrl(Url $url) {
/** inline {@inheritdoc} */
public function isAssets($url) {
return (bool) (preg_match('~opensea\.io/assets/.*[a-zA-Z0-9]+/[a-zA-Z0-9]+~i', (string) $url));
/** inline {@inheritdoc} */
public function isCollection($url) {
return (bool) (preg_match('~opensea\.io/collection/(.*)~i', (string) $url));
protected static function get_api_key() {
$settings = (array) get_option(EMBEDPRESS_PLG_NAME . ':opensea', []);
return !empty($settings['api_key']) ? $settings['api_key'] : '';
public function getStaticResponse() {
'provider_name' => $this->getProviderName(),
"provider_url" => "https://opensea.io/",
if($this->isAssets($url)){
$results['html'] = $this->getAssets($url);
else if($this->isCollection($url)){
$results['html'] = $this->getCollection($url);
* Get Opensea single assets data
public function getAssets($url) {
preg_match('~opensea\.io/assets/.*/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)~i', (string) $url, $matches);
$opensea_settings = get_option( EMBEDPRESS_PLG_NAME.':opensea');
$params = $this->getParams();
$api_key = 'e63d36afdf3f424d9adf1a06269d7ee3';
if(!empty($opensea_settings['api_key'])){
$api_key = $opensea_settings['api_key'];
if(!empty($matches[1]) && !empty($matches[2])){
'include_orders' => true,
$url = "https://api.opensea.io/api/v2/chain/ethereum/contract/$matches[1]/nfts/$matches[2]?" . http_build_query($param);
$asset_cache_key = md5($url . $api_key) . '_asset_cache';
$asset = get_transient($asset_cache_key);
$results = wp_remote_get($url, [
'Content-Type' => 'application/json',
if (!is_wp_error($results)) {
$jsonResult = json_decode($results['body']);
$asset = $this->nftNormalizeJSONData($jsonResult);
set_transient($asset_cache_key, $asset, DAY_IN_SECONDS);
delete_transient( $asset_cache_key );
if(isset($asset['id']) && $asset['collection_slug']){
$current_price = $this->getNFTCurrentPrice($api_key, $asset['collection_slug'], $asset['id']);
$template = $this->nftSingleItemTemplate($asset, $current_price);
<div class="ep-parent-wrapper ep-parent-ep-nft-gallery-r1a5mbx ">
<div class="ep-nft-gallery-wrapper ep-nft-gallery-r1a5mbx" data-id="ep-nft-gallery-r1a5mbx">
<div class="ep_nft_content_wrap ep_nft__wrapper nft_items ep-nft-single-item-wraper ep-list">
<?php print_r($template); ?>
<?php $html = ob_get_clean();
<!-- vertical=\"true\" -->
width=\"{$params['maxwidth']}\"
contractAddress=\"{$matches[1]}\"
tokenId=\"{$matches[2]}\">
<script src=\"https://unpkg.com/embeddable-nfts/dist/nft-card.min.js\"></script>";
* Get Opensea Collection assets data
public function getCollection($url) {
preg_match('~opensea\.io/collection/(.*)~i', (string) $url, $matches);
$opensea_settings = get_option( EMBEDPRESS_PLG_NAME.':opensea');
$api_key = 'e63d36afdf3f424d9adf1a06269d7ee3';
if(!empty($opensea_settings['api_key'])){
$api_key = $opensea_settings['api_key'];
if(!empty($opensea_settings['orderby'])){
$orderby = $opensea_settings['orderby'];
//This limit comes from Global Opensea Settings
if(!empty($opensea_settings['limit'])){
$limit = $opensea_settings['limit'];
$params = $this->getParams();
//This limit comes from Elementor and Gutenberg
if(! empty( $params['limit'] ) && $params['limit'] != 'false'){
$limit = $params['limit'];
if(! empty( $params['loadmore'] ) && $params['loadmore'] != 'false'){
$loadmore = $params['loadmore'];
if(! empty( $params['itemperpage'] ) && $params['itemperpage'] != 'false'){
$itemperpage = $params['itemperpage'];
if(! empty( $params['orderby'] ) && $params['orderby'] != 'false'){
$orderby = $params['orderby'];
if(! empty( $params['loadmorelabel'] ) && $params['loadmorelabel'] != 'false' && $params['loadmorelabel'] != 'true'){
$loadmorelabel = $params['loadmorelabel'];
// Embepress NFT item layout
if(! empty( $params['layout'] )){
$ep_layout = $params['layout'];
if( ! empty( $params['layout'] ) && $params['layout'] == 'ep-grid'){
if(! empty( $params['preset'] )){
$ep_preset = $params['preset'];
'order_direction' => $orderby,
'include_orders' => true,
$url = "https://api.opensea.io/api/v2/collection/$matches[1]/nfts?" . http_build_query($param);
$collection_assets_key = md5($url . $api_key) . '_collection_assets_cache';
$collection_asset = get_transient($collection_assets_key);
if (false === $collection_asset) {
$results = wp_remote_get($url, [
'Content-Type' => 'application/json',
if (!is_wp_error($results) ) {
$jsonResult = json_decode($results['body']);
if(isset($jsonResult->nfts) && is_array($jsonResult->nfts)){
$collection_asset = array();
foreach ($jsonResult->nfts as $key => $asset) {
$collection_asset[] = $this->normalizeJSONData($asset);
set_transient($collection_assets_key, $collection_asset, DAY_IN_SECONDS);
if(is_array($collection_asset)) : $id = $collection_asset[0]; endif;
delete_transient($collection_assets_key);
<?php if(!empty($collection_asset) && is_array($collection_asset) ): ?>
<div class="ep-parent-wrapper ep-parent-ep-nft-gallery-r1a5mbx ">
<div class="ep-nft-gallery-wrapper ep-nft-gallery-r1a5mbx" data-id="ep-nft-gallery-r1a5mbx" data-loadmorelabel="<?php echo esc_attr($loadmorelabel); ?>" data-itemparpage="<?php echo esc_attr($itemperpage); ?>" data-nftid="<?php echo esc_attr( 'ep-'.md5($url .uniqid()) ); ?>">
<div class="ep_nft_content_wrap ep_nft__wrapper nft_items <?php echo esc_attr( $ep_layout.' '.$ep_preset ); ?>" >
foreach ($collection_asset as $key => $asset) {
$template = $this->nftItemTemplate($asset);
<?php if(!empty($loadmore)): ?>
<?php if((isset($params['limit']) && isset($params['itemperpage'])) && $params['limit'] > $params['itemperpage']) : ?>
<div class="ep-loadmore-wrapper">
<button class="btn btn-primary nft-loadmore" data-iconcolor="<?php echo esc_attr($this->getColor('loadmoreTextColor') ); ?>" <?php echo $this->createStye('loadmoreTextColor', 'loadmoreTextFontsize', 'loadmoreBackgroundColor')?>> <?php echo esc_html($loadmorelabel); ?></button>
<?php $this->openSeaStyle($this->getParams()); ?>
<?php if(is_wp_error( $results ) && defined('WP_DEBUG') && WP_DEBUG): ?>
<h4 style="text-align: center"><?php echo esc_html($results->get_error_message()); ?></h4>
<?php elseif(isset($jsonResult->errors[0])): ?>
<h4 style="text-align: center"><?php echo esc_html($jsonResult->errors[0]); ?></h4>
<h4 style="text-align: center"><?php echo esc_html__('Something went wrong.', 'embedpress'); ?></h4>
<?php $html = ob_get_clean();
public function getNFTCurrentPrice($api_key, $collection_slug, $token_id){
$url = "https://api.opensea.io/api/v2/listings/collection/$collection_slug/nfts/$token_id/best";
$price_cache_key = md5($url . $api_key) . '_nft_price_cache';
$nft_price = get_transient($price_cache_key);
$results = wp_remote_get($url, [
'Content-Type' => 'application/json',
if (!is_wp_error($results)) {
$jsonResult = json_decode($results['body']);
$nft_price = $jsonResult->price->current->value ?? 0 / 1000000000000000000;
set_transient($price_cache_key, $nft_price, DAY_IN_SECONDS);
delete_transient( $price_cache_key );
public function normalizeJSONData($asset){
$current_price = isset($asset->seaport_sell_orders)?$asset->seaport_sell_orders:0;
$last_sale = isset($asset->last_sale->total_price)?$asset->last_sale->total_price:0;
$nftItem['id'] = isset($asset->identifier)?$asset->identifier:'';
$nftItem['name'] = isset($asset->name)?$asset->name:'';
$nftItem['permalink'] = isset($asset->opensea_url)?$asset->opensea_url:'';
$nftItem['description'] = isset($asset->description)?$asset->description:'';
$nftItem['image_url'] = isset($asset->image_url)?$asset->image_url:'';
$nftItem['image_thumbnail_url'] = isset($asset->image_thumbnail_url)?$asset->image_thumbnail_url:'';
$nftItem['image_preview_url'] = isset($asset->image_preview_url)?$asset->image_preview_url:'';
$nftItem['image_original_url'] = isset($asset->image_original_url)?$asset->image_original_url:'';
$nftItem['created_by'] = isset($asset->creator->user->username)?$asset->creator->user->username:'';
$nftItem['creator_img_url'] = isset($asset->asset_contract->image_url)?$asset->asset_contract->image_url:'';
$nftItem['current_price'] = isset($current_price[0]->current_price)?(float)($current_price[0]->current_price / 1000000000000000000) : 0;
$nftItem['last_sale'] = isset($asset->last_sale->total_price)?(float)($asset->last_sale->total_price / 1000000000000000000) : 0;
$nftItem['creator_url'] = 'https://opensea.io/'.$nftItem['created_by'];
public function nftNormalizeJSONData($asset){
$nftItem['id'] = isset($nft->identifier) ? $nft->identifier : '';
$nftItem['name'] = isset($nft->name) ? $nft->name : '';
$nftItem['permalink'] = isset($nft->opensea_url) ? $nft->opensea_url : '';
$nftItem['description'] = isset($nft->description) ? $nft->description : '';
$nftItem['image_url'] = isset($nft->image_url) ? $nft->image_url : '';
// Add other image properties as needed
$nftItem['created_by'] = isset($nft->creator) ? $nft->creator : '';
// Add other creator properties as needed
$traits = isset($nft->traits) ? $nft->traits : [];
$traitValues = array_column($traits, 'value');
$nftItem['traits'] = implode(', ', $traitValues);
$owners = isset($nft->owners) ? $nft->owners : [];
$ownerAddresses = array_column($owners, 'address');
$nftItem['owner_addresses'] = $ownerAddresses;
$rarity = isset($nft->rarity) ? $nft->rarity : '';
$nftItem['rank'] = isset($rarity->rank) ? $rarity->rank : 0;
// Add other rarity properties as needed
$nftItem['collection_name'] = isset($nft->collection) ? $nft->collection : '';