: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
if ( !class_exists( 'EmbedPress_GoogleClient') ) {
require_once 'GoogleClient.php';
if ( !defined( 'EPGC_NOTICES_VERIFY_SUCCESS') ) {
define('EPGC_NOTICES_VERIFY_SUCCESS', __('Verify OK!', 'embedpress'));
define('EPGC_NOTICES_REVOKE_SUCCESS', __('Access revoked. This plugin does not have access to your calendars anymore.', 'embedpress'));
define('EPGC_NOTICES_REMOVE_SUCCESS', sprintf(__('Plugin data removed. Make sure to also manually revoke access to your calendars in the Google <a target="__blank" href="%s">Permissions</a> page!', 'embedpress'), 'https://myaccount.google.com/permissions'));
define('EPGC_NOTICES_CALENDARLIST_UPDATE_SUCCESS', __('Calendars updated.', 'embedpress'));
define('EPGC_NOTICES_COLORLIST_UPDATE_SUCCESS', __('Colors updated.', 'embedpress'));
define('EPGC_NOTICES_CACHE_DELETED', __('Cache deleted.', 'embedpress'));
define('EPGC_ERRORS_CLIENT_SECRET_MISSING', __('No client secret.', 'embedpress'));
define('EPGC_ERRORS_CLIENT_SECRET_INVALID', __('Invalid client secret.', 'embedpress'));
define('EPGC_ERRORS_ACCESS_TOKEN_MISSING', __('No access token.', 'embedpress'));
define('EPGC_ERRORS_REFRESH_TOKEN_MISSING', sprintf(__('Your refresh token is missing!<br><br>This can only be solved by manually revoking this plugin's access in the Google <a target="__blank" href="%s">Permissions</a> page and remove all plugin data.', 'embedpress'), 'https://myaccount.google.com/permissions'));
define('EPGC_ERRORS_ACCESS_REFRESH_TOKEN_MISSING', __('No access and refresh tokens.', 'embedpress'));
define('EPGC_ERRORS_REDIRECT_URI_MISSING', __('URI <code>%s</code> missing in the client secret file. Adjust your Google project and upload the new client secret file.', 'embedpress'));
define('EPGC_ERRORS_INVALID_FORMAT', __('Invalid format', 'embedpress'));
define('EPGC_ERRORS_NO_CALENDARS', __('No calendars', 'embedpress'));
define('EPGC_ERRORS_NO_SELECTED_CALENDARS', __('No selected calendars', 'embedpress'));
define('EPGC_ERRORS_TOKEN_AND_API_KEY_MISSING', __('Access token and API key are missing.', 'embedpress'));
define('EPGC_TRANSIENT_PREFIX', 'pgc_ev_');
define('EPGC_ENQUEUE_ACTION_PRIORITY', 11);
define( 'EPGC_REDIRECT_URL', admin_url('admin.php?page=embedpress&page_type=google-calendar'));
if (!defined('EPGC_EVENTS_MAX_RESULTS')) {
define('EPGC_EVENTS_MAX_RESULTS', 250);
if (!defined('EPGC_EVENTS_DEFAULT_TITLE')) {
define('EPGC_EVENTS_DEFAULT_TITLE', '');
if (!defined('EPGC_ASSET_URL')) {
define('EPGC_ASSET_URL', plugin_dir_url(__FILE__) .'assets/');
class Embedpress_Google_Helper {
public static function print_calendar_list($calendarList = []) {
if ( empty( $calendarList) ) {
$calendarList = static::getDecoded( 'epgc_calendarlist' ); //settings_selected_calendar_ids_json_cb
if ( ! empty( $calendarList ) ) {
$selectedCalendarIds = get_option( 'epgc_selected_calendar_ids' ); // array
if ( empty( $selectedCalendarIds ) ) {
$selectedCalendarIds = [];
<?php foreach ( $calendarList as $calendar ) { ?>
$calendarId = $calendar['id'];
$htmlId = md5( $calendarId );
<p class="epgc-calendar-filter">
<input id="<?php echo $htmlId; ?>" type="checkbox" name="epgc_selected_calendar_ids[]"
<?php if ( in_array( $calendarId, $selectedCalendarIds ) ) {
value="<?php echo esc_attr( $calendarId ); ?>"/>
<label for="<?php echo $htmlId; ?>">
<span class="epgc-calendar-color" style="background-color:<?php echo esc_attr( $calendar['backgroundColor'] ); ?>"></span>
<?php echo esc_html( $calendar['summary'] ); ?><?php if ( ! empty( $calendar['primary'] ) ) {
<br>ID: <?php echo esc_html( $calendarId ); ?>
$refreshToken = get_option( "epgc_refresh_token" );
if ( empty( $refreshToken ) ) {
static::show_notice( EPGC_ERRORS_REFRESH_TOKEN_MISSING, 'error', false );
<p><?php _e( 'No calendar was found.', 'embedpress' ); ?></p>
* Helper function to return array from option (that should be a JSON string).
* @return array or $default = null
public static function getDecoded($optionName, $default = null) {
$item = get_option($optionName);
// $item should be a JSON string.
return json_decode($item, true);
public static function show_notice($notice, $type, $dismissable) {
<div class="notice notice-<?php echo esc_attr($type); echo $dismissable ? ' is-dismissible' : ''; ?>">
<p><?php echo $notice; ?></p>
public static function ajax_get_calendar() {
check_ajax_referer('epgc_nonce');
if (empty($_POST['start']) || empty($_POST['end'])) {
throw new Exception(EPGC_ERRORS_INVALID_FORMAT);
// Start and end are in ISO8601 string format with timezone offset (e.g. 2018-09-01T12:30:00-05:00)
$start = $_POST['start'];
if (array_key_exists('thisCalendarids', $_POST) && !empty($_POST['thisCalendarids'])) {
$postedCalendarIds = array_map('trim', explode(',', $_POST['thisCalendarids']));
$privateSettingsCalendarListIds = array_map(function($item) {
}, static::getDecoded('epgc_calendarlist', []));
if (!empty($privateSettingsCalendarListIds)) {
$privateSettingsSelectedCalendarListIds = get_option('epgc_selected_calendar_ids');
// if (empty($postedCalendarIds)) {
// // If we have private selected calendars in settings and we get NO selected calendars from widget, shortcode, Gutenberg block, this means
// // ALL private calendars will be used.
// $postedCalendarIds = $privateSettingsSelectedCalendarListIds;
foreach ($postedCalendarIds as $calId) {
if (!in_array($calId, $privateSettingsCalendarListIds) || in_array($calId, $privateSettingsSelectedCalendarListIds)) {
$thisCalendarids[] = $calId;
$thisCalendarids = $postedCalendarIds;
$cacheTime = get_option('epgc_cache_time'); // empty == no cache!
// We can have mutiple calendars with different calendar selections,
// so key should be including calendar selection.
$transientKey = EPGC_TRANSIENT_PREFIX . $start . $end . md5(implode('-', $thisCalendarids));
$transientItems = !empty($cacheTime) ? get_transient($transientKey) : false;
$calendarListByKey = static::get_calendars_by_key($thisCalendarids);
if ($transientItems !== false) {
wp_send_json(['items' => $transientItems, 'calendars' => $calendarListByKey]);
$colorList = false; // false means not queried yet / otherwise [] or filled []
'maxResults' => EPGC_EVENTS_MAX_RESULTS,
'orderBy' => 'startTime',
'singleEvents' => 'true',
if (!empty($_POST['timeZone'])) {
$optParams['timeZone'] = $_POST['timeZone'];
$hasAccessToken = get_option('epgc_access_token');
if (!empty($hasAccessToken)) {
$client = static::getGoogleClient(true);
if ($client->isAccessTokenExpired()) {
if (!$client->getRefreshTOken()) {
throw new Exception(EPGC_ERRORS_REFRESH_TOKEN_MISSING);
$client->refreshAccessToken();
$service = new EmbedPress_GoogleCalendarClient($client);
foreach ($thisCalendarids as $calendarId) {
$results[$calendarId] = $service->getEvents($calendarId, $optParams);
} elseif (!empty(get_option('epgc_api_key'))) {
$referer = !empty($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : null;
$apiKey = get_option('epgc_api_key');
$service = new EmbedPress_GoogleCalendarClient(null);
foreach ($thisCalendarids as $calendarId) {
$results[$calendarId] = $service->getEventsPublic($calendarId, $optParams, $apiKey, $referer);
// No API key and no OAuth2 token
throw new Exception(EPGC_ERRORS_TOKEN_AND_API_KEY_MISSING);
foreach ($results as $calendarId => $events) {
foreach ($events as $item) {
'title' => empty($item['summary']) ? EPGC_EVENTS_DEFAULT_TITLE : $item['summary'],
'htmlLink' => $item['htmlLink'],
'description' => !empty($item['description']) ? $item['description'] : '',
'creator' => !empty($item['creator']) ? $item['creator'] : [],
'attendees' => !empty($item['attendees']) ? $item['attendees'] : [],
'attachments' => !empty($item['attachments']) ? $item['attachments'] : [],
'location' => !empty($item['location']) ? $item['location'] : ''
if (!empty($item['start']['date'])) {
$newItem['allDay'] = true;
$newItem['start'] = $item['start']['date'];
$newItem['end'] = $item['end']['date'];
// $newItem['timeZone'] = $item['start']['timeZone']; // TODO? end timezone also exists...
$newItem['start'] = $item['start']['dateTime'];
$newItem['end'] = $item['end']['dateTime'];
// $newItem['timeZone'] = $item['start']['timeZone']; // TODO? end timezone also exists...
if (!empty($item['colorId'])) {
if ($colorList === false) {
$colorList = static::getDecoded('pgc_colorlist', []);
if (array_key_exists('event', $colorList) && array_key_exists($item['colorId'], $colorList['event'])) {
$newItem['bColor'] = $colorList['event'][$item['colorId']]['background'];
$newItem['fColor'] = $colorList['event'][$item['colorId']]['foreground'];
if (!empty($cacheTime)) {
set_transient($transientKey, $items, $cacheTime * MINUTE_IN_SECONDS);
wp_send_json(['items' => $items, 'calendars' => $calendarListByKey]);
} catch (EmbedPress_GoogleClient_RequestException $ex) {
'error' => $ex->getMessage(),
'errorCode' => $ex->getCode(),
'errorDescription' => $ex->getDescription()]);
} catch (Exception $ex) {
'error' => $ex->getMessage(),
'errorCode' => $ex->getCode()]);
public static function get_calendars_by_key($calendarIds) {
$publicCalendarList = get_option('pgc_public_calendarlist');
if (empty($publicCalendarList)) {
$publicCalendarList = [];
$privateCalendarList = static::getDecoded('epgc_calendarlist', []);
if (empty($privateCalendarList)) {
$privateCalendarList = [];
$calendarList = $publicCalendarList + $privateCalendarList;
foreach ($calendarList as $cal) {
$keyedCalendarList[$cal['id']] = $cal;
foreach ($calendarIds as $calId) {
$cal = array_key_exists($calId, $keyedCalendarList) ? $keyedCalendarList[$calId] : [
'backgroundColor' => 'rgb(121, 134, 203)'
$calendarListByKey[$calId] = [
'summary' => $cal['summary'],
'backgroundColor' => $cal['backgroundColor']
return $calendarListByKey;
* Helper function that returns a valid Google Client.
* @return Embedpress_GoogleClient instance
* @param bool $withTokens If true, also get tokens.
public static function getGoogleClient($withTokens = false) {
$authConfig = get_option('epgc_client_secret');
if (empty($authConfig)) {
throw new Exception(EPGC_ERRORS_CLIENT_SECRET_MISSING);
$authConfig = static::getDecoded('epgc_client_secret');
if (empty($authConfig)) {
throw new Exception(EPGC_ERRORS_CLIENT_SECRET_INVALID);
$c = new Embedpress_GoogleClient($authConfig);
$c->setScope('https://www.googleapis.com/auth/calendar.readonly');
if (!self::check_redirect_uri($authConfig)) {
throw new Exception(sprintf(EPGC_ERRORS_REDIRECT_URI_MISSING, EPGC_REDIRECT_URL));
$c->setRedirectUri(EPGC_REDIRECT_URL);
$c->setTokenCallback(function($accessTokenInfo, $refreshToken) {
update_option('epgc_access_token', json_encode($accessTokenInfo, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES), false);
if (!empty($refreshToken)) {
update_option('epgc_refresh_token', $refreshToken, false);
$accessToken = static::getDecoded('epgc_access_token');
if (empty($accessToken)) {
throw new Exception(EPGC_ERRORS_ACCESS_TOKEN_MISSING);
$c->setAccessTokenInfo($accessToken);
$refreshToken = get_option("epgc_refresh_token");
if (empty($refreshToken)) {
throw new Exception(EPGC_ERRORS_REFRESH_TOKEN_MISSING);
$c->setRefreshToken($refreshToken);
* Helper function to check if we have a valid redirect uri in the client secret.
public static function check_redirect_uri($decodedClientSecret) {
return !empty($decodedClientSecret)
&& !empty($decodedClientSecret['web'])
&& !empty($decodedClientSecret['web']['redirect_uris'])
&& in_array(EPGC_REDIRECT_URL, $decodedClientSecret['web']['redirect_uris']);
* Get a valid formatted client secret.
* @return array|false Secret Array, false if no exists, Exception for invalid one
public static function get_valid_client_secret(&$error = '') {
$clientSecret = get_option('epgc_client_secret');
if (empty($clientSecret)) {
$clientSecret = static::getDecoded('epgc_client_secret');
|| empty($clientSecret['web'])
|| empty($clientSecret['web']['client_secret'])
|| empty($clientSecret['web']['client_id']))
$error = EPGC_ERRORS_CLIENT_SECRET_INVALID;
} elseif (!self::check_redirect_uri($clientSecret))
$error = sprintf(EPGC_ERRORS_REDIRECT_URI_MISSING, admin_url('options-general.php?page=pgc'));
public static function delete_calendar_cache() {
$wpdb->query("DELETE FROM " . $wpdb->options
. " WHERE option_name LIKE '_transient_timeout_" . EPGC_TRANSIENT_PREFIX . "%' OR option_name LIKE '_transient_" . EPGC_TRANSIENT_PREFIX . "%'");
* Helper function to delete all plugin options.
public static function delete_options($which) { // which = all, public, private
if ($which === 'all' || $which === 'private') {
delete_option('epgc_access_token');
delete_option('epgc_refresh_token');
delete_option('epgc_selected_calendar_ids');
delete_option('epgc_calendarlist');
delete_option('epgc_client_secret');
if ($which === 'all' || $which === 'public') {
delete_option('epgc_api_key');
delete_option('epgc_cache_time');
public static function uninstall() {
$client = static::getGoogleClient();
$accessToken = static::getDecoded('epgc_access_token');
if (!empty($accessToken)) {
$client->setAccessTokenInfo($accessToken);
$refreshToken = get_option("epgc_refresh_token");
if (!empty($refreshToken)) {
$client->setRefreshToken($refreshToken);
if (empty($accessToken) && empty($refreshToken)) {
throw new Exception(EPGC_ERRORS_ACCESS_REFRESH_TOKEN_MISSING);
} catch (Exception $ex) {
static::delete_plugin_data();
* Helper function to delete all plugin data.
public static function delete_plugin_data($which = 'all') {
self::delete_calendar_cache();
self::delete_options($which);
public static function removable_query_args($removable_query_args) {
$removable_query_args[] = 'epgcnotice';
return $removable_query_args;
public static function notices_init() {
if (!empty($_GET['epgcnotice'])) {
$epgcnotices = get_option('epgc_notices_' . get_current_user_id());
if (empty($epgcnotices)) {
delete_option('epgc_notices_' . get_current_user_id());
add_action('admin_notices', function() use ($epgcnotices) {
foreach ($epgcnotices as $notice) {
<div class="notice notice-<?php echo esc_attr($notice['type']); ?> is-dismissible">
<p><?php echo esc_html($notice['content']); ?></p>
* Helper function to add notice messages.
* @param bool $redirect Redirect if true.
public static function add_notice($content, $type = 'success', $redirect = false) {
$epgcnotices = get_option('epgc_notices_' . get_current_user_id());
if (empty($epgcnotices)) {
update_option('epgc_notices_' . get_current_user_id(), $epgcnotices, false);
wp_redirect(EPGC_REDIRECT_URL ."&epgcnotice=true");
* Helper function die with different kind of errors.
public static function embedpress_die($error = null) {
$backLink = '<br><br><a href="' . admin_url('admin.php?page=embedpress&page_type=google-calendar') . '">' . __('Back', 'embedpress') . '</a>';
wp_die(__('Unknown error', 'embedpress') . $backLink);
if ($error instanceof Exception) {
$x[] = $error->getCode();
$s[] = $error->getMessage();
if ($error instanceof Embedpress_GoogleClient_RequestException) {
if ($error->getDescription()) {
$s[] = $error->getDescription();
wp_die(implode("<br>", $s) . $backLink);
} elseif (is_array($error)) {
wp_die(implode("<br>", $error) . $backLink);
} elseif (is_string($error)) {
wp_die($error . $backLink);
wp_die(__('Unknown error format', 'embedpress') . $backLink);