File: /var/www/newfaith.focalat.com/new-faith/wp-content/themes/christian-child/functions.php
<?php
/**
* Theme functions and definitions.
*/
function christian_child_enqueue_styles() {
wp_enqueue_style( 'christian-child-style',
get_stylesheet_directory_uri() . '/style.css',
array(),
wp_get_theme()->get('Version')
);
}
add_action( 'wp_enqueue_scripts', 'christian_child_enqueue_styles', 11 );
/**
* Function for add background image into login page
*
* @return void
*/
function newfaith_login_background() {
echo '<style type="text/css">
body.login {
background-image: url("https://newfaith.focalat.com/wp-content/uploads/2024/08/church-newfaith-min.jpg");
background-size: cover;
background-position: center;
}
#login {
background: #9CB5B4;
}
body.login {
position: absolute;
right: 65vw;
display: flex;
}
.login form {
background: #f5efe8;
}
</style>';
}
add_action('login_enqueue_scripts', 'newfaith_login_background');
/**
* Function to change logo in login page
*
* @return void
*/
function newfaith_login_logo() {
echo '<style type="text/css">
h1 a {
background-image: url("https://newfaith.focalat.com/wp-content/uploads/2024/07/site_logo.png") !important;
}
</style>';
}
add_action('login_enqueue_scripts', 'newfaith_login_logo');
/**
* Function to remove br tag from contact form 7
*
* @param [type] $form
* @return void
*/
function custom_wpcf7_form_elements($form) {
// Remove <br> tags
$form = str_replace('<br />', '', $form);
$form = str_replace('<br>', '', $form);
$form = str_replace('<br/>', '', $form);
return $form;
}
add_filter('wpcf7_form_elements', 'custom_wpcf7_form_elements');
/**
* SMTP Configuration
*
* @param [type] $phpmailer
* @return void
*/
function configure_smtp($phpmailer)
{
$phpmailer->isSMTP();
$phpmailer->Host = SMTP_HOST;
$phpmailer->SMTPAuth = SMTP_AUTH;
$phpmailer->Port = SMTP_PORT;
$phpmailer->Username = SMTP_USER;
$phpmailer->Password = SMTP_PASS;
$phpmailer->SMTPSecure = SMTP_SECURE;
$phpmailer->From = SMTP_FROM;
$phpmailer->FromName = SMTP_NAME;
}
add_action('phpmailer_init', 'configure_smtp');
/* Recurring Event functionality */
function add_event_recurrence_meta_box()
{
add_meta_box(
'event_recurrence_meta_box',
'Event Recurrence',
'render_event_recurrence_meta_box',
'tribe_events',
'side',
'high'
);
}
add_action('add_meta_boxes', 'add_event_recurrence_meta_box');
// Render the meta box content
function render_event_recurrence_meta_box($post)
{
$recurrence = get_post_meta($post->ID, '_EventRecurrence', true);
?>
<p>
<label for="event_recurrence">Recurrence Frequency:</label>
<select name="event_recurrence" id="event_recurrence">
<option value="">None</option>
<option value="weekly" <?php selected($recurrence, 'weekly'); ?>>Weekly</option>
<option value="monthly" <?php selected($recurrence, 'monthly'); ?>>Monthly</option>
<option value="yearly" <?php selected($recurrence, 'yearly'); ?>>Yearly</option>
</select>
</p>
<?php
}
// Save the meta box data
function save_event_recurrence_meta_box_data($post_id)
{
if (array_key_exists('event_recurrence', $_POST)) {
update_post_meta(
$post_id,
'_EventRecurrence',
sanitize_text_field($_POST['event_recurrence'])
);
}
}
add_action('save_post', 'save_event_recurrence_meta_box_data');
// Schedule the cron job
function schedule_recurring_event_update()
{
if (!wp_next_scheduled('update_recurring_events_hook')) {
wp_schedule_event(time(), 'hourly', 'update_recurring_events_hook');
error_log('Scheduled cron job for update_recurring_events_hook.');
} else {
error_log('Cron job for update_recurring_events_hook already scheduled.');
}
}
add_action('wp', 'schedule_recurring_event_update'); // use 'wp' instead of 'init'
// Clear the cron job on plugin deactivation or theme switch
function clear_recurring_event_cron()
{
// $timestamp = wp_next_scheduled('update_recurring_events_hook');
// wp_unschedule_event($timestamp, 'update_recurring_events_hook');
$timestamp = wp_next_scheduled('update_recurring_events_hook');
if ($timestamp) {
wp_unschedule_event($timestamp, 'update_recurring_events_hook');
error_log('Cleared cron job for update_recurring_events_hook.');
}
}
add_action('switch_theme', 'clear_recurring_event_cron');
add_action('wp_logout', 'clear_recurring_event_cron'); // To ensure cron job is cleared when logging out
// Function to update recurring events
function update_recurring_events()
{
error_log('update_recurring_events() triggered: ' . current_time('Y-m-d H:i:s'));
$args = array(
'post_type' => 'tribe_events',
'meta_query' => array(
array(
'key' => '_EventEndDate',
'value' => current_time('Y-m-d H:i:s'),
'compare' => '<'
),
),
'posts_per_page' => -1,
);
$events = get_posts($args);
foreach ($events as $event) {
$recurrence = get_post_meta($event->ID, '_EventRecurrence', true);
$start_date = get_post_meta($event->ID, '_EventStartDate', true);
$end_date = get_post_meta($event->ID, '_EventEndDate', true);
$current_time = current_time('timestamp');
$end_time = strtotime($end_date);
if (!$recurrence && $current_time >= $end_time) {
// If no recurrence is found and the event has ended, set status to 'draft'
wp_update_post(array(
'ID' => $event->ID,
'post_status' => 'draft'
));
error_log("Event ID {$event->ID} has no recurrence and is now set to 'draft'.");
continue; // Skip the rest of the loop since the event is now a draft
}
// Recurrence logic for events with recurrence set
$update = false;
$new_start_date = $start_date;
$new_end_date = $end_date;
switch ($recurrence) {
case 'weekly':
if ($current_time >= $end_time) {
$new_start_date = strtotime('+1 week', strtotime($start_date));
$new_end_date = strtotime('+1 week', $end_time);
$update = true;
}
break;
case 'monthly':
if ($current_time >= $end_time) {
$new_start_date = strtotime('+1 month', strtotime($start_date));
$new_end_date = strtotime('+1 month', $end_time);
$update = true;
}
break;
case 'yearly':
if ($current_time >= $end_time) {
$new_start_date = strtotime('+1 year', strtotime($start_date));
$new_end_date = strtotime('+1 year', $end_time);
$update = true;
}
break;
default:
error_log("Unknown recurrence type '{$recurrence}' for Event ID {$event->ID}");
break;
}
if ($update) {
update_post_meta($event->ID, '_EventStartDate', date('Y-m-d H:i:s', $new_start_date));
update_post_meta($event->ID, '_EventEndDate', date('Y-m-d H:i:s', $new_end_date));
error_log("Event ID {$event->ID} updated to " . date('Y-m-d H:i:s', $new_start_date) . " - " . date('Y-m-d H:i:s', $new_end_date));
}
}
}
add_action('update_recurring_events_hook', 'update_recurring_events');
/* Allow html for excerpt field */
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
function custom_wp_trim_excerpt($text) {
global $post;
if ('' == $text) {
$text = get_the_content('');
$text = strip_shortcodes($text);
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
$text = wp_strip_all_tags($text, true); // Strips all tags except the ones you want
// Allow specific HTML tags in the excerpt
$allowed_tags = '<a>,<em>,<strong>,<img>,<br>,<p>'; // Add tags you want to allow
$text = strip_tags($text, $allowed_tags);
$excerpt_length = apply_filters('excerpt_length', 55);
$excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
$text = wp_trim_words($text, $excerpt_length, $excerpt_more);
}
return apply_filters('wp_trim_excerpt', $text, $post);
}
add_filter('get_the_excerpt', 'custom_wp_trim_excerpt');