Edit File by line

Deprecated: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in /home/sportsfever/public_html/filemanger/function.php on line 93
/home/sportsfe.../httpdocs/wp-conte.../plugins/embedpre.../EmbedPre.../ThirdPar.../Googleca...
File: Embedpress_Google_Helper.php
<?php
[0] Fix | Delete
[1] Fix | Delete
if ( !class_exists( 'EmbedPress_GoogleClient') ) {
[2] Fix | Delete
require_once 'GoogleClient.php';
[3] Fix | Delete
}
[4] Fix | Delete
if ( !defined( 'EPGC_NOTICES_VERIFY_SUCCESS') ) {
[5] Fix | Delete
define('EPGC_NOTICES_VERIFY_SUCCESS', __('Verify OK!', 'embedpress'));
[6] Fix | Delete
define('EPGC_NOTICES_REVOKE_SUCCESS', __('Access revoked. This plugin does not have access to your calendars anymore.', 'embedpress'));
[7] Fix | Delete
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'));
[8] Fix | Delete
define('EPGC_NOTICES_CALENDARLIST_UPDATE_SUCCESS', __('Calendars updated.', 'embedpress'));
[9] Fix | Delete
define('EPGC_NOTICES_COLORLIST_UPDATE_SUCCESS', __('Colors updated.', 'embedpress'));
[10] Fix | Delete
define('EPGC_NOTICES_CACHE_DELETED', __('Cache deleted.', 'embedpress'));
[11] Fix | Delete
[12] Fix | Delete
define('EPGC_ERRORS_CLIENT_SECRET_MISSING', __('No client secret.', 'embedpress'));
[13] Fix | Delete
define('EPGC_ERRORS_CLIENT_SECRET_INVALID', __('Invalid client secret.', 'embedpress'));
[14] Fix | Delete
define('EPGC_ERRORS_ACCESS_TOKEN_MISSING', __('No access token.', 'embedpress'));
[15] Fix | Delete
define('EPGC_ERRORS_REFRESH_TOKEN_MISSING', sprintf(__('Your refresh token is missing!<br><br>This can only be solved by manually revoking this plugin&#39;s access in the Google <a target="__blank" href="%s">Permissions</a> page and remove all plugin data.', 'embedpress'), 'https://myaccount.google.com/permissions'));
[16] Fix | Delete
define('EPGC_ERRORS_ACCESS_REFRESH_TOKEN_MISSING', __('No access and refresh tokens.', 'embedpress'));
[17] Fix | Delete
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'));
[18] Fix | Delete
define('EPGC_ERRORS_INVALID_FORMAT', __('Invalid format', 'embedpress'));
[19] Fix | Delete
define('EPGC_ERRORS_NO_CALENDARS', __('No calendars', 'embedpress'));
[20] Fix | Delete
define('EPGC_ERRORS_NO_SELECTED_CALENDARS', __('No selected calendars', 'embedpress'));
[21] Fix | Delete
define('EPGC_ERRORS_TOKEN_AND_API_KEY_MISSING', __('Access token and API key are missing.', 'embedpress'));
[22] Fix | Delete
define('EPGC_TRANSIENT_PREFIX', 'pgc_ev_');
[23] Fix | Delete
define('EPGC_ENQUEUE_ACTION_PRIORITY', 11);
[24] Fix | Delete
define( 'EPGC_REDIRECT_URL', admin_url('admin.php?page=embedpress&page_type=google-calendar'));
[25] Fix | Delete
}
[26] Fix | Delete
if (!defined('EPGC_EVENTS_MAX_RESULTS')) {
[27] Fix | Delete
define('EPGC_EVENTS_MAX_RESULTS', 250);
[28] Fix | Delete
}
[29] Fix | Delete
[30] Fix | Delete
if (!defined('EPGC_EVENTS_DEFAULT_TITLE')) {
[31] Fix | Delete
define('EPGC_EVENTS_DEFAULT_TITLE', '');
[32] Fix | Delete
}
[33] Fix | Delete
[34] Fix | Delete
[35] Fix | Delete
if (!defined('EPGC_ASSET_URL')) {
[36] Fix | Delete
define('EPGC_ASSET_URL', plugin_dir_url(__FILE__) .'assets/');
[37] Fix | Delete
}
[38] Fix | Delete
[39] Fix | Delete
class Embedpress_Google_Helper {
[40] Fix | Delete
[41] Fix | Delete
public static function print_calendar_list($calendarList = []) {
[42] Fix | Delete
if ( empty( $calendarList) ) {
[43] Fix | Delete
$calendarList = static::getDecoded( 'epgc_calendarlist' ); //settings_selected_calendar_ids_json_cb
[44] Fix | Delete
}
[45] Fix | Delete
if ( ! empty( $calendarList ) ) {
[46] Fix | Delete
$selectedCalendarIds = get_option( 'epgc_selected_calendar_ids' ); // array
[47] Fix | Delete
if ( empty( $selectedCalendarIds ) ) {
[48] Fix | Delete
$selectedCalendarIds = [];
[49] Fix | Delete
}
[50] Fix | Delete
?>
[51] Fix | Delete
<ul>
[52] Fix | Delete
<?php foreach ( $calendarList as $calendar ) { ?>
[53] Fix | Delete
<?php
[54] Fix | Delete
$calendarId = $calendar['id'];
[55] Fix | Delete
$htmlId = md5( $calendarId );
[56] Fix | Delete
?>
[57] Fix | Delete
<p class="epgc-calendar-filter">
[58] Fix | Delete
<input id="<?php echo $htmlId; ?>" type="checkbox" name="epgc_selected_calendar_ids[]"
[59] Fix | Delete
<?php if ( in_array( $calendarId, $selectedCalendarIds ) ) {
[60] Fix | Delete
echo ' checked ';
[61] Fix | Delete
} ?>
[62] Fix | Delete
value="<?php echo esc_attr( $calendarId ); ?>"/>
[63] Fix | Delete
<label for="<?php echo $htmlId; ?>">
[64] Fix | Delete
<span class="epgc-calendar-color" style="background-color:<?php echo esc_attr( $calendar['backgroundColor'] ); ?>"></span>
[65] Fix | Delete
<?php echo esc_html( $calendar['summary'] ); ?><?php if ( ! empty( $calendar['primary'] ) ) {
[66] Fix | Delete
echo ' (primary)';
[67] Fix | Delete
} ?>
[68] Fix | Delete
</label>
[69] Fix | Delete
<br>ID: <?php echo esc_html( $calendarId ); ?>
[70] Fix | Delete
</p>
[71] Fix | Delete
<?php } ?>
[72] Fix | Delete
</ul>
[73] Fix | Delete
<?php
[74] Fix | Delete
$refreshToken = get_option( "epgc_refresh_token" );
[75] Fix | Delete
if ( empty( $refreshToken ) ) {
[76] Fix | Delete
static::show_notice( EPGC_ERRORS_REFRESH_TOKEN_MISSING, 'error', false );
[77] Fix | Delete
}
[78] Fix | Delete
} else {
[79] Fix | Delete
?>
[80] Fix | Delete
<p><?php _e( 'No calendar was found.', 'embedpress' ); ?></p>
[81] Fix | Delete
<?php
[82] Fix | Delete
}
[83] Fix | Delete
}
[84] Fix | Delete
[85] Fix | Delete
/**
[86] Fix | Delete
* Helper function to return array from option (that should be a JSON string).
[87] Fix | Delete
* @return array or $default = null
[88] Fix | Delete
*/
[89] Fix | Delete
public static function getDecoded($optionName, $default = null) {
[90] Fix | Delete
$item = get_option($optionName);
[91] Fix | Delete
// $item should be a JSON string.
[92] Fix | Delete
if (!empty($item)) {
[93] Fix | Delete
return json_decode($item, true);
[94] Fix | Delete
}
[95] Fix | Delete
return $default;
[96] Fix | Delete
}
[97] Fix | Delete
[98] Fix | Delete
[99] Fix | Delete
public static function show_notice($notice, $type, $dismissable) {
[100] Fix | Delete
?>
[101] Fix | Delete
<div class="notice notice-<?php echo esc_attr($type); echo $dismissable ? ' is-dismissible' : ''; ?>">
[102] Fix | Delete
<p><?php echo $notice; ?></p>
[103] Fix | Delete
</div>
[104] Fix | Delete
<?php
[105] Fix | Delete
}
[106] Fix | Delete
[107] Fix | Delete
public static function ajax_get_calendar() {
[108] Fix | Delete
[109] Fix | Delete
check_ajax_referer('epgc_nonce');
[110] Fix | Delete
[111] Fix | Delete
try {
[112] Fix | Delete
[113] Fix | Delete
if (empty($_POST['start']) || empty($_POST['end'])) {
[114] Fix | Delete
throw new Exception(EPGC_ERRORS_INVALID_FORMAT);
[115] Fix | Delete
}
[116] Fix | Delete
[117] Fix | Delete
// Start and end are in ISO8601 string format with timezone offset (e.g. 2018-09-01T12:30:00-05:00)
[118] Fix | Delete
$start = $_POST['start'];
[119] Fix | Delete
$end = $_POST['end'];
[120] Fix | Delete
[121] Fix | Delete
$thisCalendarids = [];
[122] Fix | Delete
$postedCalendarIds = [];
[123] Fix | Delete
if (array_key_exists('thisCalendarids', $_POST) && !empty($_POST['thisCalendarids'])) {
[124] Fix | Delete
$postedCalendarIds = array_map('trim', explode(',', $_POST['thisCalendarids']));
[125] Fix | Delete
}
[126] Fix | Delete
$privateSettingsCalendarListIds = array_map(function($item) {
[127] Fix | Delete
return $item['id'];
[128] Fix | Delete
}, static::getDecoded('epgc_calendarlist', []));
[129] Fix | Delete
if (!empty($privateSettingsCalendarListIds)) {
[130] Fix | Delete
$privateSettingsSelectedCalendarListIds = get_option('epgc_selected_calendar_ids');
[131] Fix | Delete
// if (empty($postedCalendarIds)) {
[132] Fix | Delete
// // If we have private selected calendars in settings and we get NO selected calendars from widget, shortcode, Gutenberg block, this means
[133] Fix | Delete
// // ALL private calendars will be used.
[134] Fix | Delete
// $postedCalendarIds = $privateSettingsSelectedCalendarListIds;
[135] Fix | Delete
// }
[136] Fix | Delete
foreach ($postedCalendarIds as $calId) {
[137] Fix | Delete
if (!in_array($calId, $privateSettingsCalendarListIds) || in_array($calId, $privateSettingsSelectedCalendarListIds)) {
[138] Fix | Delete
$thisCalendarids[] = $calId;
[139] Fix | Delete
}
[140] Fix | Delete
}
[141] Fix | Delete
} else {
[142] Fix | Delete
$thisCalendarids = $postedCalendarIds;
[143] Fix | Delete
}
[144] Fix | Delete
[145] Fix | Delete
$cacheTime = get_option('epgc_cache_time'); // empty == no cache!
[146] Fix | Delete
[147] Fix | Delete
// We can have mutiple calendars with different calendar selections,
[148] Fix | Delete
// so key should be including calendar selection.
[149] Fix | Delete
$transientKey = EPGC_TRANSIENT_PREFIX . $start . $end . md5(implode('-', $thisCalendarids));
[150] Fix | Delete
[151] Fix | Delete
$transientItems = !empty($cacheTime) ? get_transient($transientKey) : false;
[152] Fix | Delete
[153] Fix | Delete
$calendarListByKey = static::get_calendars_by_key($thisCalendarids);
[154] Fix | Delete
[155] Fix | Delete
if ($transientItems !== false) {
[156] Fix | Delete
wp_send_json(['items' => $transientItems, 'calendars' => $calendarListByKey]);
[157] Fix | Delete
wp_die();
[158] Fix | Delete
}
[159] Fix | Delete
[160] Fix | Delete
$colorList = false; // false means not queried yet / otherwise [] or filled []
[161] Fix | Delete
[162] Fix | Delete
$results = [];
[163] Fix | Delete
[164] Fix | Delete
$optParams = array(
[165] Fix | Delete
'maxResults' => EPGC_EVENTS_MAX_RESULTS,
[166] Fix | Delete
'orderBy' => 'startTime',
[167] Fix | Delete
'singleEvents' => 'true',
[168] Fix | Delete
'timeMin' => $start,
[169] Fix | Delete
'timeMax' => $end,
[170] Fix | Delete
);
[171] Fix | Delete
if (!empty($_POST['timeZone'])) {
[172] Fix | Delete
$optParams['timeZone'] = $_POST['timeZone'];
[173] Fix | Delete
}
[174] Fix | Delete
[175] Fix | Delete
$hasAccessToken = get_option('epgc_access_token');
[176] Fix | Delete
[177] Fix | Delete
if (!empty($hasAccessToken)) {
[178] Fix | Delete
[179] Fix | Delete
$client = static::getGoogleClient(true);
[180] Fix | Delete
if ($client->isAccessTokenExpired()) {
[181] Fix | Delete
if (!$client->getRefreshTOken()) {
[182] Fix | Delete
throw new Exception(EPGC_ERRORS_REFRESH_TOKEN_MISSING);
[183] Fix | Delete
}
[184] Fix | Delete
$client->refreshAccessToken();
[185] Fix | Delete
}
[186] Fix | Delete
$service = new EmbedPress_GoogleCalendarClient($client);
[187] Fix | Delete
[188] Fix | Delete
foreach ($thisCalendarids as $calendarId) {
[189] Fix | Delete
$results[$calendarId] = $service->getEvents($calendarId, $optParams);
[190] Fix | Delete
}
[191] Fix | Delete
[192] Fix | Delete
} elseif (!empty(get_option('epgc_api_key'))) {
[193] Fix | Delete
[194] Fix | Delete
$referer = !empty($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : null;
[195] Fix | Delete
$apiKey = get_option('epgc_api_key');
[196] Fix | Delete
$service = new EmbedPress_GoogleCalendarClient(null);
[197] Fix | Delete
foreach ($thisCalendarids as $calendarId) {
[198] Fix | Delete
$results[$calendarId] = $service->getEventsPublic($calendarId, $optParams, $apiKey, $referer);
[199] Fix | Delete
}
[200] Fix | Delete
[201] Fix | Delete
} else {
[202] Fix | Delete
// No API key and no OAuth2 token
[203] Fix | Delete
throw new Exception(EPGC_ERRORS_TOKEN_AND_API_KEY_MISSING);
[204] Fix | Delete
}
[205] Fix | Delete
[206] Fix | Delete
$items = [];
[207] Fix | Delete
foreach ($results as $calendarId => $events) {
[208] Fix | Delete
foreach ($events as $item) {
[209] Fix | Delete
$newItem = [
[210] Fix | Delete
'title' => empty($item['summary']) ? EPGC_EVENTS_DEFAULT_TITLE : $item['summary'],
[211] Fix | Delete
'htmlLink' => $item['htmlLink'],
[212] Fix | Delete
'description' => !empty($item['description']) ? $item['description'] : '',
[213] Fix | Delete
'calId' => $calendarId,
[214] Fix | Delete
'creator' => !empty($item['creator']) ? $item['creator'] : [],
[215] Fix | Delete
'attendees' => !empty($item['attendees']) ? $item['attendees'] : [],
[216] Fix | Delete
'attachments' => !empty($item['attachments']) ? $item['attachments'] : [],
[217] Fix | Delete
'location' => !empty($item['location']) ? $item['location'] : ''
[218] Fix | Delete
];
[219] Fix | Delete
if (!empty($item['start']['date'])) {
[220] Fix | Delete
$newItem['allDay'] = true;
[221] Fix | Delete
$newItem['start'] = $item['start']['date'];
[222] Fix | Delete
$newItem['end'] = $item['end']['date'];
[223] Fix | Delete
// $newItem['timeZone'] = $item['start']['timeZone']; // TODO? end timezone also exists...
[224] Fix | Delete
} else {
[225] Fix | Delete
$newItem['start'] = $item['start']['dateTime'];
[226] Fix | Delete
$newItem['end'] = $item['end']['dateTime'];
[227] Fix | Delete
// $newItem['timeZone'] = $item['start']['timeZone']; // TODO? end timezone also exists...
[228] Fix | Delete
}
[229] Fix | Delete
if (!empty($item['colorId'])) {
[230] Fix | Delete
if ($colorList === false) {
[231] Fix | Delete
$colorList = static::getDecoded('pgc_colorlist', []);
[232] Fix | Delete
}
[233] Fix | Delete
if (array_key_exists('event', $colorList) && array_key_exists($item['colorId'], $colorList['event'])) {
[234] Fix | Delete
$newItem['bColor'] = $colorList['event'][$item['colorId']]['background'];
[235] Fix | Delete
$newItem['fColor'] = $colorList['event'][$item['colorId']]['foreground'];
[236] Fix | Delete
}
[237] Fix | Delete
}
[238] Fix | Delete
[239] Fix | Delete
$items[] = $newItem;
[240] Fix | Delete
}
[241] Fix | Delete
}
[242] Fix | Delete
[243] Fix | Delete
if (!empty($cacheTime)) {
[244] Fix | Delete
set_transient($transientKey, $items, $cacheTime * MINUTE_IN_SECONDS);
[245] Fix | Delete
}
[246] Fix | Delete
[247] Fix | Delete
wp_send_json(['items' => $items, 'calendars' => $calendarListByKey]);
[248] Fix | Delete
wp_die();
[249] Fix | Delete
} catch (EmbedPress_GoogleClient_RequestException $ex) {
[250] Fix | Delete
wp_send_json([
[251] Fix | Delete
'error' => $ex->getMessage(),
[252] Fix | Delete
'errorCode' => $ex->getCode(),
[253] Fix | Delete
'errorDescription' => $ex->getDescription()]);
[254] Fix | Delete
wp_die();
[255] Fix | Delete
} catch (Exception $ex) {
[256] Fix | Delete
wp_send_json([
[257] Fix | Delete
'error' => $ex->getMessage(),
[258] Fix | Delete
'errorCode' => $ex->getCode()]);
[259] Fix | Delete
wp_die();
[260] Fix | Delete
}
[261] Fix | Delete
}
[262] Fix | Delete
[263] Fix | Delete
public static function get_calendars_by_key($calendarIds) {
[264] Fix | Delete
[265] Fix | Delete
$publicCalendarList = get_option('pgc_public_calendarlist');
[266] Fix | Delete
if (empty($publicCalendarList)) {
[267] Fix | Delete
$publicCalendarList = [];
[268] Fix | Delete
}
[269] Fix | Delete
$privateCalendarList = static::getDecoded('epgc_calendarlist', []);
[270] Fix | Delete
if (empty($privateCalendarList)) {
[271] Fix | Delete
$privateCalendarList = [];
[272] Fix | Delete
}
[273] Fix | Delete
$calendarList = $publicCalendarList + $privateCalendarList;
[274] Fix | Delete
$keyedCalendarList = [];
[275] Fix | Delete
foreach ($calendarList as $cal) {
[276] Fix | Delete
$keyedCalendarList[$cal['id']] = $cal;
[277] Fix | Delete
}
[278] Fix | Delete
[279] Fix | Delete
$calendarListByKey = [];
[280] Fix | Delete
foreach ($calendarIds as $calId) {
[281] Fix | Delete
$cal = array_key_exists($calId, $keyedCalendarList) ? $keyedCalendarList[$calId] : [
[282] Fix | Delete
'summary' => $calId,
[283] Fix | Delete
'backgroundColor' => 'rgb(121, 134, 203)'
[284] Fix | Delete
];
[285] Fix | Delete
$calendarListByKey[$calId] = [
[286] Fix | Delete
'summary' => $cal['summary'],
[287] Fix | Delete
'backgroundColor' => $cal['backgroundColor']
[288] Fix | Delete
];
[289] Fix | Delete
}
[290] Fix | Delete
[291] Fix | Delete
return $calendarListByKey;
[292] Fix | Delete
}
[293] Fix | Delete
/**
[294] Fix | Delete
* Helper function that returns a valid Google Client.
[295] Fix | Delete
* @return Embedpress_GoogleClient instance
[296] Fix | Delete
* @param bool $withTokens If true, also get tokens.
[297] Fix | Delete
* @throws Exception.
[298] Fix | Delete
*/
[299] Fix | Delete
public static function getGoogleClient($withTokens = false) {
[300] Fix | Delete
[301] Fix | Delete
$authConfig = get_option('epgc_client_secret');
[302] Fix | Delete
if (empty($authConfig)) {
[303] Fix | Delete
throw new Exception(EPGC_ERRORS_CLIENT_SECRET_MISSING);
[304] Fix | Delete
}
[305] Fix | Delete
$authConfig = static::getDecoded('epgc_client_secret');
[306] Fix | Delete
if (empty($authConfig)) {
[307] Fix | Delete
throw new Exception(EPGC_ERRORS_CLIENT_SECRET_INVALID);
[308] Fix | Delete
}
[309] Fix | Delete
[310] Fix | Delete
$c = new Embedpress_GoogleClient($authConfig);
[311] Fix | Delete
$c->setScope('https://www.googleapis.com/auth/calendar.readonly');
[312] Fix | Delete
if (!self::check_redirect_uri($authConfig)) {
[313] Fix | Delete
throw new Exception(sprintf(EPGC_ERRORS_REDIRECT_URI_MISSING, EPGC_REDIRECT_URL));
[314] Fix | Delete
}
[315] Fix | Delete
$c->setRedirectUri(EPGC_REDIRECT_URL);
[316] Fix | Delete
$c->setTokenCallback(function($accessTokenInfo, $refreshToken) {
[317] Fix | Delete
update_option('epgc_access_token', json_encode($accessTokenInfo, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES), false);
[318] Fix | Delete
if (!empty($refreshToken)) {
[319] Fix | Delete
update_option('epgc_refresh_token', $refreshToken, false);
[320] Fix | Delete
}
[321] Fix | Delete
});
[322] Fix | Delete
[323] Fix | Delete
if ($withTokens) {
[324] Fix | Delete
$accessToken = static::getDecoded('epgc_access_token');
[325] Fix | Delete
if (empty($accessToken)) {
[326] Fix | Delete
throw new Exception(EPGC_ERRORS_ACCESS_TOKEN_MISSING);
[327] Fix | Delete
}
[328] Fix | Delete
$c->setAccessTokenInfo($accessToken);
[329] Fix | Delete
$refreshToken = get_option("epgc_refresh_token");
[330] Fix | Delete
if (empty($refreshToken)) {
[331] Fix | Delete
throw new Exception(EPGC_ERRORS_REFRESH_TOKEN_MISSING);
[332] Fix | Delete
}
[333] Fix | Delete
$c->setRefreshToken($refreshToken);
[334] Fix | Delete
}
[335] Fix | Delete
[336] Fix | Delete
return $c;
[337] Fix | Delete
[338] Fix | Delete
}
[339] Fix | Delete
[340] Fix | Delete
/**
[341] Fix | Delete
* Helper function to check if we have a valid redirect uri in the client secret.
[342] Fix | Delete
* @return bool
[343] Fix | Delete
*/
[344] Fix | Delete
public static function check_redirect_uri($decodedClientSecret) {
[345] Fix | Delete
return !empty($decodedClientSecret)
[346] Fix | Delete
&& !empty($decodedClientSecret['web'])
[347] Fix | Delete
&& !empty($decodedClientSecret['web']['redirect_uris'])
[348] Fix | Delete
&& in_array(EPGC_REDIRECT_URL, $decodedClientSecret['web']['redirect_uris']);
[349] Fix | Delete
}
[350] Fix | Delete
[351] Fix | Delete
[352] Fix | Delete
/**
[353] Fix | Delete
* Get a valid formatted client secret.
[354] Fix | Delete
* @return array|false Secret Array, false if no exists, Exception for invalid one
[355] Fix | Delete
**/
[356] Fix | Delete
public static function get_valid_client_secret(&$error = '') {
[357] Fix | Delete
$clientSecret = get_option('epgc_client_secret');
[358] Fix | Delete
if (empty($clientSecret)) {
[359] Fix | Delete
return false;
[360] Fix | Delete
}
[361] Fix | Delete
$clientSecret = static::getDecoded('epgc_client_secret');
[362] Fix | Delete
if (empty($clientSecret)
[363] Fix | Delete
|| empty($clientSecret['web'])
[364] Fix | Delete
|| empty($clientSecret['web']['client_secret'])
[365] Fix | Delete
|| empty($clientSecret['web']['client_id']))
[366] Fix | Delete
{
[367] Fix | Delete
$error = EPGC_ERRORS_CLIENT_SECRET_INVALID;
[368] Fix | Delete
} elseif (!self::check_redirect_uri($clientSecret))
[369] Fix | Delete
{
[370] Fix | Delete
$error = sprintf(EPGC_ERRORS_REDIRECT_URI_MISSING, admin_url('options-general.php?page=pgc'));
[371] Fix | Delete
}
[372] Fix | Delete
return $clientSecret;
[373] Fix | Delete
}
[374] Fix | Delete
[375] Fix | Delete
public static function delete_calendar_cache() {
[376] Fix | Delete
global $wpdb;
[377] Fix | Delete
$wpdb->query("DELETE FROM " . $wpdb->options
[378] Fix | Delete
. " WHERE option_name LIKE '_transient_timeout_" . EPGC_TRANSIENT_PREFIX . "%' OR option_name LIKE '_transient_" . EPGC_TRANSIENT_PREFIX . "%'");
[379] Fix | Delete
}
[380] Fix | Delete
[381] Fix | Delete
/**
[382] Fix | Delete
* Helper function to delete all plugin options.
[383] Fix | Delete
*/
[384] Fix | Delete
public static function delete_options($which) { // which = all, public, private
[385] Fix | Delete
if ($which === 'all' || $which === 'private') {
[386] Fix | Delete
delete_option('epgc_access_token');
[387] Fix | Delete
delete_option('epgc_refresh_token');
[388] Fix | Delete
delete_option('epgc_selected_calendar_ids');
[389] Fix | Delete
delete_option('epgc_calendarlist');
[390] Fix | Delete
delete_option('epgc_client_secret');
[391] Fix | Delete
}
[392] Fix | Delete
if ($which === 'all' || $which === 'public') {
[393] Fix | Delete
delete_option('epgc_api_key');
[394] Fix | Delete
}
[395] Fix | Delete
if ($which === 'all') {
[396] Fix | Delete
delete_option('epgc_cache_time');
[397] Fix | Delete
}
[398] Fix | Delete
}
[399] Fix | Delete
[400] Fix | Delete
public static function uninstall() {
[401] Fix | Delete
try {
[402] Fix | Delete
$client = static::getGoogleClient();
[403] Fix | Delete
$accessToken = static::getDecoded('epgc_access_token');
[404] Fix | Delete
if (!empty($accessToken)) {
[405] Fix | Delete
$client->setAccessTokenInfo($accessToken);
[406] Fix | Delete
}
[407] Fix | Delete
$refreshToken = get_option("epgc_refresh_token");
[408] Fix | Delete
if (!empty($refreshToken)) {
[409] Fix | Delete
$client->setRefreshToken($refreshToken);
[410] Fix | Delete
}
[411] Fix | Delete
if (empty($accessToken) && empty($refreshToken)) {
[412] Fix | Delete
throw new Exception(EPGC_ERRORS_ACCESS_REFRESH_TOKEN_MISSING);
[413] Fix | Delete
}
[414] Fix | Delete
$client->revoke();
[415] Fix | Delete
} catch (Exception $ex) {
[416] Fix | Delete
// Too bad...
[417] Fix | Delete
} finally {
[418] Fix | Delete
// Clear all plugin data
[419] Fix | Delete
static::delete_plugin_data();
[420] Fix | Delete
}
[421] Fix | Delete
}
[422] Fix | Delete
[423] Fix | Delete
/**
[424] Fix | Delete
* Helper function to delete all plugin data.
[425] Fix | Delete
*/
[426] Fix | Delete
public static function delete_plugin_data($which = 'all') {
[427] Fix | Delete
self::delete_calendar_cache();
[428] Fix | Delete
self::delete_options($which);
[429] Fix | Delete
}
[430] Fix | Delete
public static function removable_query_args($removable_query_args) {
[431] Fix | Delete
$removable_query_args[] = 'epgcnotice';
[432] Fix | Delete
return $removable_query_args;
[433] Fix | Delete
}
[434] Fix | Delete
[435] Fix | Delete
public static function notices_init() {
[436] Fix | Delete
if (!empty($_GET['epgcnotice'])) {
[437] Fix | Delete
$epgcnotices = get_option('epgc_notices_' . get_current_user_id());
[438] Fix | Delete
if (empty($epgcnotices)) {
[439] Fix | Delete
return;
[440] Fix | Delete
}
[441] Fix | Delete
delete_option('epgc_notices_' . get_current_user_id());
[442] Fix | Delete
add_action('admin_notices', function() use ($epgcnotices) {
[443] Fix | Delete
foreach ($epgcnotices as $notice) {
[444] Fix | Delete
?>
[445] Fix | Delete
<div class="notice notice-<?php echo esc_attr($notice['type']); ?> is-dismissible">
[446] Fix | Delete
<p><?php echo esc_html($notice['content']); ?></p>
[447] Fix | Delete
</div>
[448] Fix | Delete
<?php
[449] Fix | Delete
}
[450] Fix | Delete
});
[451] Fix | Delete
}
[452] Fix | Delete
}
[453] Fix | Delete
[454] Fix | Delete
/**
[455] Fix | Delete
* Helper function to add notice messages.
[456] Fix | Delete
* @param bool $redirect Redirect if true.
[457] Fix | Delete
*/
[458] Fix | Delete
public static function add_notice($content, $type = 'success', $redirect = false) {
[459] Fix | Delete
$epgcnotices = get_option('epgc_notices_' . get_current_user_id());
[460] Fix | Delete
if (empty($epgcnotices)) {
[461] Fix | Delete
$epgcnotices = [];
[462] Fix | Delete
}
[463] Fix | Delete
$epgcnotices[] = [
[464] Fix | Delete
'content' => $content,
[465] Fix | Delete
'type' => $type
[466] Fix | Delete
];
[467] Fix | Delete
update_option('epgc_notices_' . get_current_user_id(), $epgcnotices, false);
[468] Fix | Delete
if ($redirect) {
[469] Fix | Delete
wp_redirect(EPGC_REDIRECT_URL ."&epgcnotice=true");
[470] Fix | Delete
}
[471] Fix | Delete
}
[472] Fix | Delete
[473] Fix | Delete
/**
[474] Fix | Delete
* Helper function die with different kind of errors.
[475] Fix | Delete
*/
[476] Fix | Delete
public static function embedpress_die($error = null) {
[477] Fix | Delete
$backLink = '<br><br><a href="' . admin_url('admin.php?page=embedpress&page_type=google-calendar') . '">' . __('Back', 'embedpress') . '</a>';
[478] Fix | Delete
if (empty($error)) {
[479] Fix | Delete
wp_die(__('Unknown error', 'embedpress') . $backLink);
[480] Fix | Delete
}
[481] Fix | Delete
if ($error instanceof Exception) {
[482] Fix | Delete
$s = [];
[483] Fix | Delete
if ($error->getCode()) {
[484] Fix | Delete
$x[] = $error->getCode();
[485] Fix | Delete
}
[486] Fix | Delete
$s[] = $error->getMessage();
[487] Fix | Delete
if ($error instanceof Embedpress_GoogleClient_RequestException) {
[488] Fix | Delete
if ($error->getDescription()) {
[489] Fix | Delete
$s[] = $error->getDescription();
[490] Fix | Delete
}
[491] Fix | Delete
}
[492] Fix | Delete
wp_die(implode("<br>", $s) . $backLink);
[493] Fix | Delete
} elseif (is_array($error)) {
[494] Fix | Delete
wp_die(implode("<br>", $error) . $backLink);
[495] Fix | Delete
} elseif (is_string($error)) {
[496] Fix | Delete
wp_die($error . $backLink);
[497] Fix | Delete
} else {
[498] Fix | Delete
wp_die(__('Unknown error format', 'embedpress') . $backLink);
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function