Skip to content

Commit

Permalink
Merge pull request #718 from code4recovery/issue425
Browse files Browse the repository at this point in the history
Issue425
  • Loading branch information
97eighty authored Mar 4, 2022
2 parents ffeff08 + f5efafa commit 5d05236
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 24 deletions.
8 changes: 8 additions & 0 deletions includes/admin_meeting.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@
// Delete the attendance_option metadata tag, don't need it
delete_metadata('post', 0, 'attendance_option', false, true);

// Remove old cache info
if ($tmpstr = get_option('tsml_cache')) {
if (file_exists(WP_CONTENT_DIR . $tmpstr)) {
unlink(WP_CONTENT_DIR . $tmpstr);
}
delete_option('tsml_cache');
}

//Rebuild the meeting cache
tsml_cache_rebuild();

Expand Down
7 changes: 4 additions & 3 deletions includes/ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ function tsml_ajax_csv()
}

//get data source
$meetings = tsml_get_meetings();
$meetings = tsml_get_meetings([], false, true);

//define columns to output, always in English for portability (per Poland NA)
$columns = [
Expand Down Expand Up @@ -201,6 +201,7 @@ function tsml_ajax_csv()
'conference_phone_notes' => 'Conference Phone Notes',
'author' => 'Author',
'slug' => 'Slug',
'data_source' => 'Data Source',
'updated' => 'Updated',
];

Expand All @@ -223,13 +224,13 @@ function tsml_ajax_csv()
} elseif ($column == 'day') {
$line[] = $tsml_days[$meeting[$column]];
} elseif ($column == 'types') {
$types = $meeting[$column];
$types = !empty($meeting[$column]) ? $meeting[$column] : [];
if (!is_array($types)) $types = [];
foreach ($types as &$type) $type = $tsml_programs[$tsml_program]['types'][trim($type)];
sort($types);
$line[] = $escape . implode(', ', $types) . $escape;
} elseif (strstr($column, 'notes')) {
$line[] = $escape . strip_tags(str_replace($escape, str_repeat($escape, 2), $meeting[$column])) . $escape;
$line[] = $escape . strip_tags(str_replace($escape, str_repeat($escape, 2), !empty($meeting[$column]) ? $meeting[$column] : '')) . $escape;
} elseif (array_key_exists($column, $meeting)) {
$line[] = $escape . str_replace($escape, '', $meeting[$column]) . $escape;
} else {
Expand Down
68 changes: 52 additions & 16 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1070,9 +1070,9 @@ function tsml_feedback_url($post)
//function: get meetings based on unsanitized $arguments
//$from_cache is only false when calling from tsml_cache_rebuild()
//used: tsml_ajax_meetings(), single-locations.php, archive-meetings.php
function tsml_get_meetings($arguments = [], $from_cache = true)
function tsml_get_meetings($arguments = [], $from_cache = true, $full_export = false)
{
global $tsml_cache, $tsml_contact_fields;
global $tsml_cache, $tsml_contact_fields, $tsml_contact_display;

//start by grabbing all meetings
if ($from_cache && file_exists(WP_CONTENT_DIR . $tsml_cache) && $meetings = file_get_contents(WP_CONTENT_DIR . $tsml_cache)) {
Expand Down Expand Up @@ -1126,6 +1126,11 @@ function tsml_get_meetings($arguments = [], $from_cache = true)
'types' => empty($meeting_meta[$post->ID]['types']) ? [] : array_values(unserialize($meeting_meta[$post->ID]['types'])),
], $locations[$post->post_parent]);

// Include the data source when doing a full export
if ($full_export && isset($meeting_meta[$post->ID]['data_source'])) {
$meeting['data_source'] = $meeting_meta[$post->ID]['data_source'];
}

//append contact info to meeting
if (!empty($meeting_meta[$post->ID]['group_id']) && array_key_exists($meeting_meta[$post->ID]['group_id'], $groups)) {
$meeting = array_merge($meeting, $groups[$meeting_meta[$post->ID]['group_id']]);
Expand All @@ -1137,6 +1142,15 @@ function tsml_get_meetings($arguments = [], $from_cache = true)
}
}

// Only show contact information when 'public' or doing a full export
if ($tsml_contact_display !== 'public' && !$full_export) {
for ($i = 1; $i < 4; $i++) {
unset($meeting['contact_' . $i . '_name']);
unset($meeting['contact_' . $i . '_email']);
unset($meeting['contact_' . $i . '_phone']);
}
}

// Ensure each meeting has an address approximate value
if (empty($meeting['approximate'])) {
$meeting['approximate'] = 'no';
Expand Down Expand Up @@ -1166,7 +1180,9 @@ function tsml_get_meetings($arguments = [], $from_cache = true)
}, $meetings);

//write array to cache
file_put_contents(WP_CONTENT_DIR . $tsml_cache, json_encode($meetings));
if (!$full_export) {
file_put_contents(WP_CONTENT_DIR . $tsml_cache, json_encode($meetings));
}
}

//check if we are filtering
Expand Down Expand Up @@ -1238,7 +1254,7 @@ function tsml_get_meta($type, $id = null)
$keys = [
'tsml_group' => '"website", "website_2", "email", "phone", "mailing_address", "venmo", "square", "paypal", "last_contact"' . (current_user_can('edit_posts') ? ', "contact_1_name", "contact_1_email", "contact_1_phone", "contact_2_name", "contact_2_email", "contact_2_phone", "contact_3_name", "contact_3_email", "contact_3_phone"' : ''),
'tsml_location' => '"formatted_address", "latitude", "longitude", "approximate"',
'tsml_meeting' => '"day", "time", "end_time", "types", "group_id", "website", "website_2", "email", "phone", "mailing_address", "venmo", "square", "paypal", "last_contact", "attendance_option", "conference_url", "conference_url_notes", "conference_phone", "conference_phone_notes"' . (current_user_can('edit_posts') ? ', "contact_1_name", "contact_1_email", "contact_1_phone", "contact_2_name", "contact_2_email", "contact_2_phone", "contact_3_name", "contact_3_email", "contact_3_phone"' : ''),
'tsml_meeting' => '"day", "time", "end_time", "types", "group_id", "website", "website_2", "email", "phone", "mailing_address", "venmo", "square", "paypal", "last_contact", "attendance_option", "conference_url", "conference_url_notes", "conference_phone", "conference_phone_notes", "data_source"' . (current_user_can('edit_posts') ? ', "contact_1_name", "contact_1_email", "contact_1_phone", "contact_2_name", "contact_2_email", "contact_2_phone", "contact_3_name", "contact_3_email", "contact_3_phone"' : ''),
];
if (!array_key_exists($type, $keys)) return trigger_error('tsml_get_meta for unexpected type ' . $type);
$meta = [];
Expand Down Expand Up @@ -1303,7 +1319,7 @@ function tsml_meeting_types($types)
//called from admin_import.php (both CSV and JSON)
function tsml_import_buffer_set($meetings, $data_source_url = null, $data_source_parent_region_id = null)
{
global $tsml_programs, $tsml_program, $tsml_days, $tsml_meeting_attendance_options;
global $tsml_programs, $tsml_program, $tsml_days, $tsml_meeting_attendance_options, $tsml_data_sources;

if (strpos($data_source_url, "spreadsheets.google.com") !== false) {
$meetings = tsml_import_reformat_googlesheet($meetings);
Expand Down Expand Up @@ -1376,7 +1392,24 @@ function tsml_import_buffer_set($meetings, $data_source_url = null, $data_source
$count_meetings = count($meetings);
for ($i = 0; $i < $count_meetings; $i++) {

$meetings[$i]['data_source'] = $data_source_url;
// If the meeting doesn't have a data_source, use the one from the function call
if (empty($meetings[$i]['data_source'])) {
$meetings[$i]['data_source'] = $data_source_url;
} else {
// Check if this data sources is in our list of feeds
if (!array_key_exists($meetings[$i]['data_source'], $tsml_data_sources)) {
// Not already there, so add it
$tsml_data_sources[$meetings[$i]['data_source']] = [
'status' => 'OK',
'last_import' => current_time('timestamp'),
'count_meetings' => 0,
'name' => parse_url($meetings[$i]['data_source'], PHP_URL_HOST),
'parent_region_id' => $data_source_parent_region_id,
'change_detect' => null,
'type' => 'JSON',
];
}
}
$meetings[$i]['data_source_parent_region_id'] = $data_source_parent_region_id;

//do wordpress sanitization
Expand Down Expand Up @@ -1537,6 +1570,9 @@ function tsml_import_buffer_set($meetings, $data_source_url = null, $data_source
$meetings[$i]['row'] = $i + 2;
}

//save data source configuration
update_option('tsml_data_sources', $tsml_data_sources);

//allow user-defined function to filter the meetings (for gal-aa.org)
if (function_exists('tsml_import_filter')) {
$meetings = array_filter($meetings, 'tsml_import_filter');
Expand Down Expand Up @@ -1924,15 +1960,15 @@ function tsml_scan_data_source($data_source_url)
return;
}

//try fetching
//try fetching
$response = wp_remote_get($data_source_url, array(
'timeout' => 30,
'sslverify' => false,
));

if (is_array($response) && !empty($response['body']) && ($body = json_decode($response['body'], true))) {
$meetings = $body;
//allow theme-defined function to reformat prior to import
//allow theme-defined function to reformat prior to import
if (function_exists('tsml_import_reformat')) {
$meetings = tsml_import_reformat($body);
}
Expand All @@ -1959,7 +1995,7 @@ function tsml_scan_data_source($data_source_url)
}
}

// send Changes Detected email
// send Changes Detected email
$subject = __('Data Source Changes Detected', '12-step-meeting-list') . ': ' . $data_source_name;
if (tsml_email($tsml_notification_addresses, str_replace("'s", "s", $subject), $message)) {
_e("<div class='bg-success text-light'>Data Source changes were detected during the daily sychronization check with this feed: $data_source_url.<br></div>", '12-step-meeting-list');
Expand Down Expand Up @@ -2049,11 +2085,11 @@ function tsml_import_has_changes($meetings, $data_source_count_meetings, $data_s
}

//function: Creates and configures cron job to run a scheduled data source scan
//used: admin-import.php
//used: admin-import.php
function tsml_CreateAndScheduleCronJob($data_source_url, $data_source_name)
{

$timestamp = tsml_strtotime('tomorrow midnight'); // Use tsml_strtotime to incorporate local site timezone with UTC.
$timestamp = tsml_strtotime('tomorrow midnight'); // Use tsml_strtotime to incorporate local site timezone with UTC.

// Get the timestamp for the next event when found.
$ts = wp_next_scheduled("tsml_scan_data_source", array($data_source_url));
Expand All @@ -2064,7 +2100,7 @@ function tsml_CreateAndScheduleCronJob($data_source_url, $data_source_name)
// When adding a data source we schedule its daily cron job
register_activation_hook(__FILE__, 'tsml_activate_data_source_scan');

//Schedule the refresh
//Schedule the refresh
if (wp_schedule_event($timestamp, "daily", "tsml_scan_data_source", array($data_source_url)) === false) {
tsml_debug("$data_source_name data source scan scheduling failed!");
} else {
Expand All @@ -2074,8 +2110,8 @@ function tsml_CreateAndScheduleCronJob($data_source_url, $data_source_name)
}
}

//function: incorporates wp timezone into php's StrToTime() function
//used: here, admin-import.php
//function: incorporates wp timezone into php's StrToTime() function
//used: here, admin-import.php
function tsml_strtotime($str)
{
// This function behaves a bit like PHP's StrToTime() function, but taking into account the Wordpress site's timezone
Expand Down Expand Up @@ -2103,8 +2139,8 @@ function tsml_strtotime($str)
return $datetime->format('U');
}

//function: incorporates wp timezone into php's date() function
//used: here, admin-import.php
//function: incorporates wp timezone into php's date() function
//used: here, admin-import.php
function tsml_date_localised($format, $timestamp = null)
{
// This function behaves a bit like PHP's Date() function, but taking into account the Wordpress site's timezone
Expand Down
2 changes: 1 addition & 1 deletion includes/shortcodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ function tsml_ui()
],
$tsml_ui_config
));
$data = admin_url('admin-ajax.php') . '?action=meetings&nonce=' . wp_create_nonce($tsml_nonce);
$data = content_url('meetings.json');
return '<div id="tsml-ui"
data-src="' . $data . '"
data-timezone="' . get_option('timezone_string', 'America/New_York') . '"
Expand Down
5 changes: 1 addition & 4 deletions includes/variables.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
$tsml_bounds = get_option('tsml_bounds');

//get the secret cache location
if (!$tsml_cache = get_option('tsml_cache')) {
$tsml_cache = '/tsml-cache-' . substr(str_shuffle(md5(microtime())), 0, 10) . '.json';
update_option('tsml_cache', $tsml_cache);
}
$tsml_cache = '/meetings.json';

// Define attendance options
$tsml_meeting_attendance_options = [
Expand Down

0 comments on commit 5d05236

Please sign in to comment.