: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
namespace EmbedPress\Ends\Back;
use EmbedPress\Ends\Handler as EndHandlerAbstract;
use EmbedPress\Shortcode;
use EmbedPress\Includes\Classes\Helper;
(defined('ABSPATH') && defined('EMBEDPRESS_IS_LOADED')) or die("No direct script access allowed.");
* The admin-facing functionality of the plugin.
* Defines the plugin name, version, and enqueue the admin-specific stylesheets and scripts.
* @subpackage EmbedPress/Ends/Back
* @author EmbedPress <help@embedpress.com>
* @copyright Copyright (C) 2023 WPDeveloper. All rights reserved.
* @license GPLv3 or later
class Handler extends EndHandlerAbstract
* Method that register all scripts for the admin area.
public function __construct($pluginName, $pluginVersion)
parent::__construct($pluginName, $pluginVersion);
add_action('wp_ajax_delete_instagram_account', [$this, 'delete_instagram_account']);
// add_action('init', [$this, 'handle_instagram_data']);
add_action('wp_ajax_get_instagram_userdata_ajax', [$this, 'get_instagram_userdata_ajax']);
add_action('wp_ajax_nopriv_get_instagram_userdata_ajax', [$this, 'get_instagram_userdata_ajax']);
if (!empty($_GET['page_type']) && $_GET['page_type'] == 'calendly') {
add_action('init', [$this, 'handle_calendly_data']);
if (defined('EMBEDPRESS_SL_ITEM_SLUG') && is_admin()) {
add_action('admin_enqueue_scripts', [$this, 'enqueueLisenceScripts']);
add_action('wp_ajax_sync_instagram_data_ajax', [$this, 'sync_instagram_data_ajax']);
add_action('wp_ajax_nopriv_sync_instagram_data_ajax', [$this, 'sync_instagram_data_ajax']);
public function get_instagram_userdata_ajax()
if (!current_user_can('manage_options')) {
wp_send_json_error(array('message' => 'You do not have sufficient permissions to access this functionality.'));
if (isset($_POST['_nonce']) && wp_verify_nonce($_POST['_nonce'], 'embedpress_elements_action')) {
if (isset($_POST['access_token'])) {
$access_token = sanitize_text_field($_POST['access_token']);
$account_type = sanitize_text_field($_POST['account_type']);
$user_data = $this->get_instagram_userdata($access_token, $account_type);
$this->handle_instagram_data($user_data);
$access_token = sanitize_text_field($_POST['access_token']);
$account_type = sanitize_text_field($_POST['account_type']);
$user_id = $this->get_instagram_userid($access_token, $account_type);
$option_key = 'ep_instagram_feed_data';
$feed_data = get_option($option_key, array());
$feed_userinfo = Helper::getInstagramUserInfo($access_token, $account_type, $user_id, true);
$feed_posts = Helper::getInstagramPosts($access_token, $account_type, $user_id, 100, true);
'feed_userinfo' => $feed_userinfo,
'feed_posts' => $feed_posts,
delete_transient('instagram_user_info_' . $user_id);
delete_transient('instagram_posts_' . $user_id);
update_option('ep_instagram_feed_data', $feed_data);
$feed_data['error'] = "Access token Invalid or expired.";
wp_send_json($feed_data);
wp_send_json_error('Access token not provided');
wp_send_json_error('Nonce verification failed');
public function sync_instagram_data_ajax()
if (!current_user_can('manage_options')) {
wp_send_json_error(array('message' => 'You do not have sufficient permissions to access this functionality.'));
if (isset($_POST['_nonce']) && wp_verify_nonce($_POST['_nonce'], 'embedpress_elements_action')) {
if (isset($_POST['access_token'])) {
$access_token = sanitize_text_field($_POST['access_token']);
$account_type = sanitize_text_field($_POST['account_type']);
$user_id = sanitize_text_field($_POST['user_id']);
$option_key = 'ep_instagram_feed_data';
$feed_data = get_option($option_key, array());
$feed_userinfo = Helper::getInstagramUserInfo($access_token, $account_type, $user_id, true);
$feed_posts = Helper::getInstagramPosts($access_token, $account_type, $user_id, 100, true);
'feed_userinfo' => $feed_userinfo,
'feed_posts' => $feed_posts,
delete_transient('instagram_user_info_' . $user_id);
delete_transient('instagram_posts_' . $user_id);
update_option('ep_instagram_feed_data', $feed_data);
wp_send_json($feed_data);
wp_send_json_error('Access token not provided');
wp_send_json_error('Nonce verification failed');
public function get_instagram_userid($access_token)
$response = "https://graph.facebook.com/v19.0/me/accounts?fields=connected_instagram_account{id}&access_token=$access_token";
if (!is_wp_error($response)) {
$body = wp_remote_retrieve_body($response);
$data = json_decode($body, true);
// Extract the connected Instagram account ID
if (isset($data['data'][0]['connected_instagram_account']['id'])) {
return $data['data'][0]['connected_instagram_account']['id'];
$user_data['error'] = "Error: Unable to connect to Instagram API.";
public function get_instagram_profile_picture($access_token, $userid)
public function get_instagram_user_id($access_token, $account_type)
// Check if user data is already cached
$user_id = get_transient('instagram_user_id_' . $access_token);
if ($account_type == 'personal') {
$response = wp_remote_get('https://graph.instagram.com/me?fields=id,username,account_type&access_token=' . $access_token);
$response = wp_remote_get('https://graph.facebook.com/v19.0/me/accounts?fields=connected_instagram_account{id,name,username,followers_count}&access_token=' . $access_token);
if (!is_wp_error($response)) {
$body = wp_remote_retrieve_body($response);
$data = json_decode($body, true);
if ($account_type == 'personal') {
if (isset($data['id']) && isset($data['username'])) {
set_transient('instagram_user_id_' . $access_token, $data['id'], HOUR_IN_SECONDS);
$data['error'] = "Access token Invalid or expired.";
if (isset($data['data'][0]['connected_instagram_account']['id']) && isset($data['data'][0]['connected_instagram_account']['username'])) {
set_transient('instagram_user_id_' . $access_token, $data['data'][0]['connected_instagram_account']['id'], HOUR_IN_SECONDS);
return $data['data'][0]['connected_instagram_account']['id'];
$data['error'] = "Access token Invalid or expired.";
$data['error'] = "Error: Unable to connect to Instagram API.";
public function get_instagram_userdata($access_token, $account_type)
// Check if user data is already cached
$user_data = get_transient('instagram_user_data_' . $access_token);
if ($account_type == 'personal') {
$response = wp_remote_get('https://graph.instagram.com/me?fields=id,username,account_type&access_token=' . $access_token);
$response = wp_remote_get('https://graph.facebook.com/v19.0/me/accounts?fields=connected_instagram_account{id,name,username,followers_count}&access_token=' . $access_token);
if (!is_wp_error($response)) {
$body = wp_remote_retrieve_body($response);
$data = json_decode($body, true);
if ($account_type == 'personal') {
if (isset($data['id']) && isset($data['username'])) {
$user_data['access_token'] = $access_token;
$user_data['user_id'] = $data['id'];
$user_data['username'] = $data['username'];
$user_data['account_type'] = $account_type;
set_transient('instagram_user_data_' . $access_token, $user_data, HOUR_IN_SECONDS);
$user_data['error'] = "Access token Invalid or expired.";
if (isset($data['data'][0]['connected_instagram_account']['id']) && isset($data['data'][0]['connected_instagram_account']['username'])) {
$user_data['access_token'] = $access_token;
$user_data['user_id'] = $data['data'][0]['connected_instagram_account']['id']; // Assuming 'id' refers to Facebook account ID
$user_data['instagram_id'] = $data['data'][0]['connected_instagram_account']['id'];
$user_data['username'] = $data['data'][0]['connected_instagram_account']['username'];
$user_data['account_type'] = $account_type;
set_transient('instagram_user_data_' . $access_token, $user_data, HOUR_IN_SECONDS);
$user_data['error'] = "Access token Invalid or expired.";
$user_data['error'] = "Error: Unable to connect to Instagram API.";
public function handle_instagram_data($user_data)
if (empty($user_data['error'])) {
$user_id = isset($user_data['user_id']) ? $user_data['user_id'] : '';
$username = isset($user_data['username']) ? $user_data['username'] : '';
$account_type = isset($user_data['account_type']) ? $user_data['account_type'] : '';
$access_token = isset($user_data['access_token']) ? $user_data['access_token'] : '';
$get_instagram_data = get_option('ep_instagram_account_data');
'account_type' => $account_type,
'access_token' => $access_token,
if (!empty($get_instagram_data)) {
foreach ($get_instagram_data as &$data) {
if ($data['user_id'] === $user_id) {
// If user_id matches, update the data
$data['username'] = $username;
$data['account_type'] = $account_type;
$data['access_token'] = $access_token;
// If user_id does not exist, add new data
$get_instagram_data[] = $token_data[0];
// If $get_instagram_data is empty, add the new data directly
$get_instagram_data = $token_data;
update_option('ep_instagram_account_data', $get_instagram_data);
wp_redirect(admin_url('admin.php?page=embedpress&page_type=instagram'), 301);
public function handle_calendly_data()
if (empty($_GET['_nonce'])) {
$verify = wp_verify_nonce($_GET['_nonce'], 'calendly_nonce');
// Check if access_token or calendly_status is present and nonce is invalid
echo esc_html__('Invalid nonce', 'embedpress');
if ((!empty($_GET['_nonce']) && $verify) && (!empty($_GET['access_token']) && isset($_GET['page_type']) && $_GET['page_type'] == 'calendly') || (isset($_GET['calendly_status']) && ($_GET['calendly_status'] == 'sync' || $_GET['calendly_status'] == 'connect'))) {
if ($_GET['calendly_status'] === 'connect') {
update_option('is_calendly_connected', true);
if (isset($_GET['access_token']) && !empty($_GET['access_token'])) {
$access_token = $_GET['access_token'];
$refresh_token = $_GET['refresh_token'];
$expires_in = $_GET['expires_in'];
$created_at = $_GET['created_at'];
} elseif (isset($_GET['calendly_status']) && ($_GET['calendly_status'] == 'sync' || $_GET['calendly_status'] == 'connect')) {
$token_data = get_option('calendly_tokens');
$access_token = $token_data['access_token'];
$refresh_token = $token_data['refresh_token'];
$expires_in = $token_data['expires_in'];
$created_at = $token_data['created_at'];
// Create an array to store the tokens and expiration time
'access_token' => $access_token,
'refresh_token' => $refresh_token,
'expires_in' => $expires_in,
'created_at' => $created_at
// Save the serialized data in a single option key
update_option('calendly_tokens', $token_data);
$user_info = json_decode(Helper::getCalendlyUserInfo($access_token), true);
if (!empty($user_info['resource']['uri'])) {
$event_types = Helper::getCalaendlyEventTypes($user_info['resource']['uri'], $access_token);
$scheduled_events = Helper::getCalaendlyScheduledEvents($user_info['resource']['uri'], $access_token);
if (is_array($scheduled_events['collection'])) {
foreach ($scheduled_events['collection'] as $event) :
$uuid = Helper::getCalendlyUuid($event['uri']);
$invite_list[$uuid] = Helper::getListEventInvitee($uuid, $access_token);
update_option('calendly_user_info', $user_info);
if (is_embedpress_pro_active() && (empty($event_types['title']))) {
update_option('calendly_event_types', $event_types);
update_option('calendly_scheduled_events', $scheduled_events);
update_option('calendly_invitees_list', $invite_list);
if (!is_embedpress_pro_active()) {
update_option('calendly_event_types', []);
update_option('calendly_scheduled_events', []);
update_option('calendly_invitees_list', []);
wp_redirect(admin_url('admin.php?page=embedpress&page_type=calendly'), 302);
public function enqueueScripts()
if ('post.php' === $pagenow) {
$urlSchemes = apply_filters('embedpress:getAdditionalURLSchemes', $this->getUrlSchemes());
EMBEDPRESS_URL_ASSETS . 'js/pdfobject.min.js',
wp_enqueue_script("bootbox-bootstrap", EMBEDPRESS_URL_ASSETS . 'js/vendor/bootstrap/bootstrap.min.js', ['jquery'], $this->pluginVersion, false);
wp_enqueue_script("bootbox", EMBEDPRESS_URL_ASSETS . 'js/vendor/bootbox.min.js', ['jquery', 'bootbox-bootstrap'], $this->pluginVersion, true);
wp_enqueue_script($this->pluginName, EMBEDPRESS_URL_ASSETS . 'js/preview.js', ['jquery', 'bootbox'], $this->pluginVersion, true);
wp_localize_script($this->pluginName, '$data', [
'baseUrl' => get_site_url() . '/',
'versionUID' => $this->pluginVersion,
'EMBEDPRESS_SHORTCODE' => EMBEDPRESS_SHORTCODE,
'EMBEDPRESS_URL_ASSETS' => EMBEDPRESS_URL_ASSETS,
'urlSchemes' => $urlSchemes,
if ('post.php' === $pagenow || 'post-new.php' === $pagenow) {
EMBEDPRESS_URL_ASSETS . 'js/plyr.polyfilled.js',
EMBEDPRESS_URL_ASSETS . 'js/gutneberg-script.js',
wp_enqueue_style('plyr', EMBEDPRESS_URL_ASSETS . 'css/plyr.css', array(), $this->pluginVersion);
wp_enqueue_style($this->pluginName, EMBEDPRESS_URL_ASSETS . 'css/embedpress.css', array(), $this->pluginVersion);
EMBEDPRESS_URL_ASSETS . 'js/carousel.min.js',
EMBEDPRESS_URL_ASSETS . 'js/initCarousel.js',
['jquery', 'cg-carousel'],
wp_enqueue_style('cg-carousel', EMBEDPRESS_URL_ASSETS . 'css/carousel.min.css', $this->pluginVersion, true);
wp_enqueue_style($this->pluginName, EMBEDPRESS_URL_ASSETS . 'css/embedpress.css', $this->pluginVersion, true);
//load embedpress admin js
EMBEDPRESS_URL_ASSETS . 'js/admin.js',
['jquery', 'wp-i18n', 'wp-url'],
wp_localize_script($this->pluginName, 'EMBEDPRESS_ADMIN_PARAMS', [
'ajaxurl' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('embedpress')
$installedPlugins = Core::getPlugins();
if (count($installedPlugins) > 0) {
foreach ($installedPlugins as $plgSlug => $plgNamespace) {
$plgScriptPathRelative = "assets/js/embedpress.{$plgSlug}.js";
$plgName = "embedpress-{$plgSlug}";
if (file_exists(WP_PLUGIN_DIR . "/{$plgName}/{$plgScriptPathRelative}")) {