File: /volume1/web/tom-marchisa/wp-content/themes/enfold-child/functions.php
<?php
/*
* Add your own functions here. You can also copy some of the theme functions into this file.
* Wordpress will use those functions instead of the original functions then.
*/
/* This filter removes the creation of the avia-merged-styles......css file that Enfold saw in it's wisdom to load after all the other css files. By loading it last it overwrote all custom css */
/* the following is a test conducted on 04272022 */
/* Following code provides ability for using Customm CSS in Enfold Advanced Editor */
add_theme_support('avia_template_builder_custom_css');
add_action( 'admin_post_nopriv_construct_generic_email', 'process_construct_generic_email' );
add_action( 'admin_post_construct_generic_email', 'process_construct_generic_email' );
function process_construct_generic_email()
{
global $wpdb;
echo('<div style="margin-left:50px;margin-top:50px;">Constructing Email<br/></div>');
echo('<div style="margin-left:50px;margin-top-50px;">');
$email_header_id = $_POST['email_header_id'] ;
$email_body_id = $_POST['email_body_id'] ;
$email_footer_id = $_POST['email_footer_id'] ;
$email_name = $_POST['email_name'] ;
$language = $_POST['language'] ;
$entered_by_user_id = get_current_user_id();
$profile_user_data = get_userdata( $entered_by_user_id );
$first_name = $profile_user_data->first_name ;
$last_name = $profile_user_data->last_name ;
$full_name = $first_name.' '.$last_name ;
$post_type = 'em_content' ;
$post_status = 'publish' ;
if($language == "Italian")
{
$em_header_category = 12 ;
$em_body_category = 13 ;
$em_footer_category = 14 ;
}
else /* English */
{
$em_header_category = 8 ;
$em_body_category = 6 ;
$em_footer_category = 7 ;
}
$get_content_error = false ;
/* Get Email Header */
$header_row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = $email_header_id AND post_status = '$post_status' AND post_type = '$post_type'" ));
if (!empty($header_row))
{
$email_header_content = $header_row->post_content ;
$email_header = trim($email_header_content) ;
}
else
{
echo('EMAIL HEADER NOT FOUND<br/>') ;
$get_content_error = true ;
}
/* Get Email Body */
$body_row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = $email_body_id AND post_status = '$post_status' AND post_type = '$post_type'" ));
if (!empty($body_row))
{
$email_body_content = $body_row->post_content ;
$email_body = trim($email_body_content) ;
$email_title = $body_row->post_title ;
}
else
{
$get_content_error = true ;
}
/* Get Email Footer */
$footer_row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = $email_footer_id AND post_status = '$post_status' AND post_type = '$post_type'" ));
if (!empty($footer_row))
{
$email_footer_content = $footer_row->post_content ;
$email_footer = trim($email_footer_content) ;
}
else
{
echo('EMAIL FOOTER NOT FOUND<br/>') ;
$get_content_error = true ;
}
if(!$get_content_error)
{
/* Insert email into Emails table */
$email_complete = trim($email_header).trim($email_body).trim($email_footer) ;
$entered_by = $full_name ;
$entered_date = date("Y-m-d H:i:s");
$insert_status = $wpdb->insert(
'user_emails',
array(
'title' => $email_title , /* String */
'name' => $email_name , /* String */
'email_header_id' => $email_header_id , /* Integer */
'email_body_id' => $email_body_id , /* Integer */
'email_footer_id' => $email_footer_id , /* Integer */
'date_created' => $entered_date , /* String */
'created_by' => $entered_by, /* string */
'email_complete_content' => $email_complete, /*String */
'language' => $language /* string */
),
array('%s','%s','%d','%d','%d','%s','%s','%s','%s')
);
if(!$insert_status)
{
echo('ERROR in trying to insert email into user_emails table<br/>') ;
}
else
{
echo('SUCCESS in inserting email into database<br/>') ;
}
}
else
{
echo('ERROR in trying to retrieve header, body, and footer from wp-posts table<br/>') ;
}
echo('<br/><br/><a href="/administration-menu/">Return to Administration Menu</a>');
echo('</div>');
}
add_action( 'admin_post_nopriv_select_generic_email', 'process_select_generic_email' );
add_action( 'admin_post_select_generic_email', 'process_select_generic_email' );
function process_select_generic_email()
{
global $wpdb;
$email_id = $_POST['email_id'] ;
$email_target = $_POST['email_target'] ;
$selected_by_user_id = get_current_user_id();
$profile_user_data = get_userdata( $selected_by_user_id );
$first_name = $profile_user_data->first_name ;
$last_name = $profile_user_data->last_name ;
$full_name = $first_name.' '.$last_name ;
$email_selected_by = $full_name ;
$email_selected_date = date("Y-m-d H:i:s");
echo('<div style="margin-left:50px;margin-top:50px;">Inserting Selected Email into Queue<br/></div>');
echo('<div style="margin-left:50px;margin-top:50px;">Selected Email will target '.$email_target.'<br/></div>');
echo('<div style="margin-left:50px;margin-top:50px;">');
/* Delete all records in queued_user_email to ensure only one exists */
$delete_status = $wpdb->query( "DELETE FROM queued_user_email") ;
if(!$delete_status)
{
echo('FAILURE to delete previous queued email<br/>') ;
}
/* Insert selected email into Queued Emails table */
$insert_status = $wpdb->insert(
'queued_user_email',
array(
'id' => $email_id , /* Integer */
'selected_by' => $email_selected_by , /* string */
'queued_date' => $email_selected_date, /* String */
'email_target' =>$email_target
),
array('%d','%s','%s','%s')
);
if(!$insert_status)
{
echo('ERROR in trying to insert email into queued_user_email database table<br/>') ;
}
else
{
echo('SUCCESS in inserting queued email into database table<br/>') ;
}
echo('<br/><br/><a href="/administration-menu/">Return to Administration Menu</a>');
echo('</div>');
}
add_action( 'admin_post_nopriv_send_generic_emails', 'process_send_generic_emails' );
add_action( 'admin_post_send_generic_emails', 'process_send_generic_emails' );
function process_send_generic_emails()
{
global $wpdb;
$single = TRUE ;
$email_id = $_POST['email_id'];
$email_subject_form = $_POST['email_subject_form'];
$send_emails = true ;
$recipients = email_target_name();
if($recipients == "email_tester")
{
$test_or_prod = "TEST" ;
}
else
{
$test_or_prod = "PROD" ;
}
if(isset($_POST['user_id']))
{
/* Retrieving each selected user_id and loading local array */
$arr_idx = -1 ;
foreach ($_POST['user_id'] as $user_id)
{
$arr_idx = $arr_idx + 1 ;
$user_id_arr[$arr_idx] = $user_id ;
}
$number_user_ids = $arr_idx - 1 ;
$last_array_idx = $arr_idx ;
}
else
{
$send_emails = FALSE ; /* no user_ids selected */
}
/* Test for missing input fields */
echo('<div style="margin-left:50px;margin-top:50px;">Sending Emails<br/></div>');
echo('<div style="margin-left:50px;margin-top:50px;">');
/* retrieve email from user_email */
$email_row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM user_emails WHERE id = $email_id " ));
if (!empty($email_row))
{
$email_header_id = $email_row->email_header_id ;
$email_body_id = $email_row->email_body_id ;
$email_footer_id = $email_row->email_footer_id ;
$email_title = $email_row->title ;
$email_language = $email_row->language ;
}
else
{
echo('EMAIL NOT FOUND<br/>') ;
$send_emails = false ;
}
/* get Email Header */
$post_type = 'em_content' ;
$post_status = 'publish' ;
$header_row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = $email_header_id AND post_status = '$post_status' AND post_type = '$post_type'" ));
if (!empty($header_row))
{
$email_header= $header_row->post_content ;
}
else
{
echo('EMAIL HEADER NOT FOUND<br/>') ;
$send_emails = false ;
}
/* get email body */
$post_type = 'em_content' ;
$post_status = 'publish' ;
$body_row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = $email_body_id AND post_status = '$post_status' AND post_type = '$post_type'" ));
if (!empty($body_row))
{
$email_body = $body_row->post_content ;
$email_title = $body_row->post_title ;
$email_subject_post = $email_title ;
}
else
{
echo('EMAIL BODY NOT FOUND<br/>') ;
$send_emails = false ;
}
/* get email footer */
$post_type = 'em_content' ;
$post_status = 'publish' ;
$footer_row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = $email_footer_id AND post_status = '$post_status' AND post_type = '$post_type'" ));
if (!empty($footer_row))
{
$email_footer = $footer_row->post_content ;
}
else
{
echo('EMAIL FOOTER NOT FOUND<br/>') ;
$send_emails = false ;
}
if(isset($email_subject_form))
{
$subject_len = strlen($email_subject_form) ;
if($subject_len > 2)
{
$email_subject = $email_subject_form ;
}
else
{
$email_subject = $email_subject_post ;
}
}
else
{
$email_subject = $email_subject_post ;
}
if($recipients == "email_tester")
{
$email_subject_qualified = 'TM-TST --'.$email_subject ;
}
else
{
$email_subject_qualified = $email_subject ;
}
$subject = $email_subject_qualified ;
$email_sender_account = '<tom@tom-marchisa.com>';
$email_sender = "Tom Marchisa" ;
$email_sender_header = 'From: Tom Marchisa '.$email_sender_account ;
/* display to-be-sent-email */
if($send_emails)
{
echo('</div>');
echo('<div style="margin-left:50px;margin-top:50px;">');
echo('The following Email will be sent <span style="font-weight:bold;font-size:1.1em;"> '.$email_sender_header.'</span><br/>');
echo('The Subject line will be:<span style="font-weight:bold;font-size:1.1em;"> '.$subject.'</span><br/><br/>');
echo('Email will be sent to: <span style="font-weight:bold;font-size:1.1em;"> '.$recipients.'</span><br/><br/>');
echo('</div>');
echo('<div style="border-width:2px;border-style:solid;border-color:black;padding:10px;">');
/* add 2 blank lines after post content */
$body_complete = $email_header.$email_body.$email_footer ;
echo('FINAL EMAIL='.$body_complete.'<br/><br/>') ;
echo('</div>');
date_default_timezone_set('America/Los_Angeles');
$email_sent_date = date("Y-m-d H:i:s");
$user_cnt = 0 ;
$sent_cnt = 0 ;
$sent_text = "Email Sent" ;
echo('<table>');
echo('<tr><td>Count</td><td>Status</td><td>User ID</td><td>Email Address</td><td>User Name</td><td>Username</td></tr>');
/* loop through local array of targeted users with values sent via POST variable */
$upper_limit = $last_array_idx ;
$users_loop = -1 ;
while($users_loop < $upper_limit)
{
$users_loop = $users_loop + 1 ;
$user_id = $user_id_arr[$users_loop] ;
$user_info = get_userdata($user_id);
$email_address = $user_info->user_email;
$current_name = $user_info->display_name ;
$couples_names = get_user_meta($user_id,"couples_names",$single) ;
$username = $user_info->user_login ;
$table_user_id = $user_id ;
$salutation = 'Hi '. $couples_names.',<br/>';
$header_with_salutation = $email_header.$salutation ;
/* replace placeholder with user data */
/* username and PW are identical */
$username_password = '<span style="font-weight:bold;font-size:1.5em;">'.$username.'</span>' ;
echo('UNPW='.$username_password.'<br/>');
$email_body_w_UNPW = str_replace('[placeholder2]', $username_password, $email_body);
$body_complete = trim($header_with_salutation).trim($email_body_w_UNPW).trim($email_footer) ;
$to = $email_address ;
$headers[] = $email_sender_header ;
$headers[] = 'Bcc: tmarchisa@gmail.com';
$headers[] = 'Content-Type: text/html; charset=UTF-8';
$sent_cnt = $sent_cnt + 1 ;
wp_mail( $to, $subject, $body_complete, $headers );
/* sleep(2) ; */
$insert_status = insert_sent_email_into_db($user_id,$email_sent_date,$email_sender,$test_or_prod,$email_header_id,$email_body_id,$email_footer_id,$email_id,$email_language,$body_complete) ;
if(!$insert_status)
{
echo('EMAIL NOT INSERTED for '.$user_id.'<br/>');
}
echo('<tr><td>'.$sent_cnt.' . </td><td>'.$sent_text.'</td><td>'.$table_user_id.'</td><td>'.$email_address.'</td><td>'.$current_name.'</td><td>'.$username.'</td></tr>') ;
} /* FOR EACH User in array */
echo('</table>');
echo('Sent Email count = '.$sent_cnt.' ');
} /* Send Emails */
else
{
echo('Emails NOT sent <br/><br/>');
}
echo('<br/><br/><a href="/administration-menu/">Return to Administration Menu</a>');
echo('</div>');
}
function insert_sent_email_into_db($user_id,$sent_date,$sent_from,$test_or_production,$email_header_id,$email_body_id,$email_footer_id,$email_id,$email_language,$body)
{
global $wpdb;
$wpdb->show_errors();
$insert_status = $wpdb->insert(
'user_emails_sent',
array(
'user_id' => $user_id ,
'sent_date' => $sent_date ,
'sent_from' => $sent_from ,
'test_or_production' => $test_or_production ,
'email_header_id' => $email_header_id ,
'email_body_id' => $email_body_id ,
'email_footer_id' => $email_footer_id,
'user_emails_id' => $email_id,
'language' => $email_language,
'email_body' => $body
),
array('%d','%s','%s','%s','%d','%d','%d','%d','%s','%s')
);
if(!($insert_status)) /* Failure to Insert */
{
return FALSE ;
$wpdb->hide_errors();
}
else
{
return TRUE ;
$wpdb->hide_errors();
}
}
function email_target_name()
{
global $wpdb ;
$queued_emails = $wpdb->get_results ( "SELECT * FROM queued_user_email " );
if(!empty($queued_emails) )
{
foreach ($queued_emails as $queued_email) /* there will be only 1 */
{
$email_target = $queued_email->email_target ;
}
}
else
{
$email_target = "NONE" ;
}
return $email_target ;
}
function wp_mail_smtp_dev_reply_to( $args )
{
$reply_to = 'Reply-To: Tom Marchisa <tom@tom-marchisa.com>';
if ( ! empty( $args[ 'headers' ] ) )
{
if ( ! is_array( $args[ 'headers' ] ) ) {
$args[ 'headers' ] = array_filter( explode( "\n", str_replace( "\r\n", "\n", $args[ 'headers' ] ) ) );
}
// Filter out all other Reply-To headers.
$args[ 'headers' ] = array_filter( $args[ 'headers' ], function ( $header )
{
return strpos( strtolower( $header ), 'reply-to' ) !== 0;
} );
}
else
{
$args[ 'headers' ] = [];
}
$args[ 'headers' ][] = $reply_to;
return $args;
}
add_filter( 'wp_mail', 'wp_mail_smtp_dev_reply_to', PHP_INT_MAX );
function is_admin_acct()
{
$my_user_id = get_current_user_id();
/* Tom */
if($my_user_id == 1)
{
$admin_user = TRUE ;
}
else
{
$admin_user = FALSE ;
}
return $admin_user ;
}
function is_email_tester()
{
$my_user_id = get_current_user_id();
/* Tom */
if($my_user_id == 1)
{
$email_tester = TRUE ;
}
else
{
$email_tester = FALSE ;
}
return $email_tester ;
}
/* added 04-26-2025 */
add_filter( 'upload_mimes', 'ar_add_flac_support' );
/**
* Introduces functionality that allows us to upload Flac files which are not
* supported by the enc_type="multipart/form-data" attribute.
*
* @param array $mimes The current array of MIME types that are supported.
* @return array $mimes The updated array of supported MIME types.
*/
function ar_add_flac_support( $mimes ) {
$mimes = array_merge(
$mimes,
array(
'flac' => 'application/x-flac',
)
);
return $mimes;
}
function get_audio_artist($audio_post_id)
{
global $wpdb ;
$double_quote = chr(34) ;
$meta_key = '_wp_attachment_metadata' ;
$one_row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM wp_postmeta WHERE (post_id = $audio_post_id AND meta_key = '$meta_key' )" ) );
if ( !empty($one_row) )
{
$music_metadata = $one_row->meta_value ;
$music_metadata_UPPER = strtoupper($music_metadata) ;
$search_str_upper = "ARTIST".$double_quote;
$find_artist_label_pos = strpos($music_metadata_UPPER,$search_str_upper) ;
/* echo('POS='.$find_artist_label_pos.'<br/>'); */
$falp_plus_8 = $find_artist_label_pos + 8 ;
$artist_label_forward_string = substr($music_metadata, $falp_plus_8) ;
/* echo('AFS='.$artist_label_forward_string.'<br/>'); */
$find_artist_dq1 = strpos($artist_label_forward_string,$double_quote) ;
/* echo('FDQ='.$find_artist_dq1.'<br/>'); */
$start2 = $find_artist_dq1 + 1 ;
$artist_start_string = substr($artist_label_forward_string,$start2) ;
/* echo('ASStr='.$artist_start_string.'<br/>'); */
$find_artist_dq2 = strpos($artist_start_string,$double_quote) ;
$artist_len = $find_artist_dq2 ;
$artist = substr($artist_start_string,0,$artist_len) ;
return $artist ;
}
else
{
return false ;
}
}
function get_audio_genre($audio_post_id)
{
global $wpdb ;
$double_quote = chr(34) ;
$meta_key = '_wp_attachment_metadata' ;
$one_row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM wp_postmeta WHERE (post_id = $audio_post_id AND meta_key = '$meta_key' )" ) );
if ( !empty($one_row) )
{
$music_metadata = $one_row->meta_value ;
$music_metadata_UPPER = strtoupper($music_metadata) ;
$find_genre_label_pos = strpos($music_metadata_UPPER,"GENRE") ;
/* echo('POS='.$find_genre_label_pos.'<br/>'); */
$falp_plus_8 = $find_genre_label_pos + 8 ;
$genre_label_forward_string = substr($music_metadata, $falp_plus_8) ;
/* echo('AFS='.$genre_label_forward_string.'<br/>'); */
$find_genre_dq1 = strpos($genre_label_forward_string,$double_quote) ;
/* echo('FDQ='.$find_genre_dq1.'<br/>'); */
$start2 = $find_genre_dq1 + 1 ;
$genre_start_string = substr($genre_label_forward_string,$start2) ;
/* echo('ASStr='.$genre_start_string.'<br/>'); */
$find_genre_dq2 = strpos($genre_start_string,$double_quote) ;
$genre_len = $find_genre_dq2 ;
$genre = substr($genre_start_string,0,$genre_len) ;
return $genre ;
}
else
{
return false ;
}
}
function get_music_info_from_post($music_post_id)
{
global $wpdb ;
$single = TRUE ;
$bl1 = " " ;
$music_url = get_post_field("guid",$music_post_id) ;
if($music_url)
{
$artist = get_audio_artist($music_post_id) ;
$genre = get_audio_genre($music_post_id) ;
if($genre == "dataformat")
{
$genre = "Unknown" ;
}
if($genre == "Religious")
{
$genre = "Spiritual" ;
}
$music_title = get_post_field("post_title",$music_post_id) ;
$file_len = strlen($music_url) ;
$file_type_start = $file_len - 3 ;
$last_char_in_filename_pos = $file_len - 5 ;
$last_char_in_filename = substr($music_url,$last_char_in_filename_pos,1) ;
$length_wo_type = $file_len - 4 ;
$music_file_type = substr($music_url,$file_type_start);
$file_wo_dot_extension = substr($music_url,0,$length_wo_type) ;
return array($music_url,$music_title,$artist,$genre) ;
}
else
{
return array($bl1,$bl1,$bl1,$bl1) ;
}
}
function get_img_url_from_post($post_id)
{
global $wpdb ;
$period = chr(46) ;
$dq = chr(34) ;
$single = TRUE ;
$meta_key1 = '_thumbnail_id' ;
$one_row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->postmeta WHERE post_id = $post_id AND meta_key = '$meta_key1'" ) );
if(!empty($one_row))
{
$img_post_id = $one_row->meta_value ;
}
$meta_key2 = '_wp_attachment_metadata' ;
$one_row2 = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->postmeta WHERE post_id = $img_post_id AND meta_key = '$meta_key2'" ) );
if(!empty($one_row2))
{
$img_info = $one_row2->meta_value ;
}
$find_jpg = strpos($img_info,'jpg') ;
$end_str = $find_jpg + 3 ;
$sub_string = substr($img_info,0,$end_str) ;
$find_dq = strrpos($sub_string,$dq) ;
$file_start = $find_dq + 1 ;
$file_name = substr($sub_string,$file_start) ;
$img_url = '/wp-content/uploads/music_file_images/'.$file_name ;
return $img_url ;
}