ID;
}
if( !is_404() )
{
return;
}
$avoid_log_bots = tomas_check_bots();
if ($avoid_log_bots)
{
return;
}
//3.5.1
$pagenotfoundurl = sanitize_text_field($_SERVER['REQUEST_URI']);
//$pagenotfoundurl = $_SERVER['REQUEST_URI'];
if (isset($_SERVER['HTTP_REFERER']))
{
//3.5.1
$pagenotfoundreferrers = sanitize_text_field($_SERVER['HTTP_REFERER']);
//$pagenotfoundreferrers = $_SERVER['HTTP_REFERER'];
}
else
{
$pagenotfoundreferrers = '';
}
//3.5.1
$pagenotfoundip = sanitize_text_field($_SERVER['REMOTE_ADDR']);
$pagenotfounduseragent = sanitize_text_field($_SERVER['HTTP_USER_AGENT']);
// $pagenotfoundip = $_SERVER['REMOTE_ADDR'];
// $pagenotfounduseragent = $_SERVER['HTTP_USER_AGENT'];
$pagenotfounddate = current_time('mysql');
$table_name = $table_prefix . "404s";
$selectEnable404Log = get_option('selectEnable404Log');
//3.4.9
if ($selectEnable404Log <> 'yes')
{
$m_mysql = $wpdb->prepare("INSERT INTO $table_name (pagenotfoundurl,pagenotfoundreferrers,pagenotfoundip, pagenotfounduseragent, pagenotfounddate)
VALUES (%s, %s, %s, %s, %s)",
$wpdb->escape($pagenotfoundurl),$wpdb->escape($pagenotfoundreferrers),
$wpdb->escape($pagenotfoundip),$wpdb->escape($pagenotfounduseragent),$wpdb->escape($pagenotfounddate)
);
$wpdb->query($m_mysql);
}
$select404toEmail = get_option('select404toEmail');
if ('YES' == $select404toEmail)
{
@tomas_404_to_email($post_id,$pagenotfoundurl);
}
$var_redirectstatuscode = redirectstatuscode();
//3.5.1
$pagenotfound404requesturl = sanitize_text_field(trim($_SERVER['REQUEST_URI']));
// $pagenotfound404requesturl = trim($_SERVER['REQUEST_URI']);
$posttoredirectid = '';
//start 3.4.1
if ((false === stripos($pagenotfound404requesturl, '.jpg')) && (false === stripos($pagenotfound404requesturl, '.jpeg')) && (false === stripos($pagenotfound404requesturl, '.png')) && (false === stripos($pagenotfound404requesturl, '.gif')))
{
}
else
{
$redirect404imagetospecificimage = get_option('redirect404imagetospecificimage');
$inputredirect404imagetospecificimage = get_option('inputredirect404imagetospecificimage');
if ('YES' == $redirect404imagetospecificimage)
{
if (!(empty($inputredirect404imagetospecificimage)))
{
wp_safe_redirect($inputredirect404imagetospecificimage,$var_redirectstatuscode);
die();
}
}
else
{
}
}
// end 3.4.1
if (!(empty($pagenotfound404requesturl)))
{
$results404redirectpostid = $wpdb->get_var(
"SELECT post_id FROM {$wpdb->postmeta} WHERE `meta_key` = '404surlredirect' AND `meta_value` = '$pagenotfound404requesturl'"
);
if (!(empty($results404redirectpostid)))
{
$posttoredirect = get_post($results404redirectpostid);
if ((isset($posttoredirect->ID)) && (! (empty($posttoredirect->ID) )))
{
$post_redirect_404s_page_url = get_permalink($posttoredirect->ID);
if (!(empty($post_redirect_404s_page_url)))
{
wp_safe_redirect($post_redirect_404s_page_url,$var_redirectstatuscode);
die();
}
}
}
}
$redirect404tospecificpage = get_option('redirect404tospecificpage');
if ('YES' == $redirect404tospecificpage)
{
$inputredirect404tospecificpage = get_option('inputredirect404tospecificpage');
if (!(empty($inputredirect404tospecificpage)))
{
wp_safe_redirect($inputredirect404tospecificpage,$var_redirectstatuscode);
die();
}
}
$select404Redirect = get_option('select404Redirect');
if ($select404Redirect == 'homepage')
{
wp_safe_redirect(home_url(),$var_redirectstatuscode);
die();
}
if ($select404Redirect == 'customurl')
{
$inputText404RedirectCustomURL = get_option('inputText404RedirectCustomURL');
if (!(empty($inputText404RedirectCustomURL)))
{
$var_redirectstatuscode = redirectstatuscode();
wp_safe_redirect($inputText404RedirectCustomURL,$var_redirectstatuscode);
die();
}
else
{
$var_redirectstatuscode = redirectstatuscode();
wp_safe_redirect(home_url(),$var_redirectstatuscode);
die();
}
}
}
function tomas_check_bots()
{
$bots = array();
$bots[] = 'bot';
$bots[] = 'spider';
$bots[] = 'crawler';
//3.5.1
$user_bots_or_not_agent = sanitize_text_field($_SERVER['HTTP_USER_AGENT']);
// $user_bots_or_not_agent = $_SERVER['HTTP_USER_AGENT'];
$user_bots_or_not_result = false;
foreach ( $bots as $bot_single )
{
$bot_single = trim($bot_single);
if ( !(empty($bot_single)))
{
$bot_single = strtolower($bot_single);
$user_bots_or_not_agent = strtolower($user_bots_or_not_agent);
if (strpos( $user_bots_or_not_agent, $bot_single ) === false)
{
$user_bots_or_not_result = false;
}
else
{
$user_bots_or_not_result = true;
}
}
else
{
$user_bots_or_not_result = false;
}
}
return $user_bots_or_not_result;
}
add_action( 'template_redirect', 'tomas_process_404s_log' );
function tomas_manage_404s()
{
global $wpdb,$table_prefix;
$table_name = $table_prefix . "404s";
$m_checkHadThere = "SELECT * FROM $table_name";
$m_checkResult = $wpdb->get_results($m_checkHadThere,ARRAY_A);
?>
query($clearLogsQuery);
messageBarFor404s('404 logs has been removed');
}
}
$selectClear404Log = get_option('selectClear404Log');
if (isset($_POST['enable404LogSubmit']))
{
//$selectEnable404Log = $_POST['selectEnable404Log'];
$selectEnable404Log = sanitize_text_field($_POST['selectEnable404Log']); //3.4.5
update_option('selectEnable404Log',$selectEnable404Log);
}
$selectEnable404Log = get_option('selectEnable404Log');
if (isset($_POST['404RedirectSubmit']))
{
//$select404Redirect = $_POST['select404Redirect'];
$select404Redirect = sanitize_text_field($_POST['select404Redirect']); //3.4.5
update_option('select404Redirect',$select404Redirect);
}
$select404Redirect = get_option('select404Redirect');
if (isset($_POST['inputText404RedirectCustomURL']))
{
//$inputText404RedirectCustomURL = $_POST['inputText404RedirectCustomURL'];
$inputText404RedirectCustomURL = sanitize_text_field($_POST['inputText404RedirectCustomURL']); //3.4.5
update_option('inputText404RedirectCustomURL',$inputText404RedirectCustomURL);
}
$inputText404RedirectCustomURL = get_option('inputText404RedirectCustomURL');
if (isset($_POST['use404to301Submit']))
{
//$selectuse404to301 = $_POST['selectuse404to301'];
$selectuse404to301 = sanitize_text_field($_POST['selectuse404to301']); //3.4.5
update_option('selectuse404to301',$selectuse404to301);
}
$selectuse404to301 = get_option('selectuse404to301');
if (isset($_POST['404toEmailSubmit']))
{
//$select404toEmail = $_POST['select404toEmail'];
$select404toEmail = sanitize_text_field($_POST['select404toEmail']); //3.4.5
update_option('select404toEmail',$select404toEmail);
}
$select404toEmail = get_option('select404toEmail');
if (isset($_POST['inputText404toEmail']))
{
//$inputText404toEmail = $_POST['inputText404toEmail'];
$inputText404toEmail = sanitize_text_field($_POST['inputText404toEmail']); //3.4.5
update_option('inputText404toEmail',$inputText404toEmail);
}
$inputText404toEmail = get_option('inputText404toEmail');
if (isset($_POST['redirect404tospecificpageSubmit']))
{
//$redirect404tospecificpage = $_POST['redirect404tospecificpage'];
$redirect404tospecificpage = sanitize_text_field($_POST['redirect404tospecificpage']); //3.4.5
update_option('redirect404tospecificpage',$redirect404tospecificpage);
}
$redirect404tospecificpage = get_option('redirect404tospecificpage');
if (isset($_POST['inputredirect404tospecificpage']))
{
//$inputredirect404tospecificpage = $_POST['inputredirect404tospecificpage'];
$inputredirect404tospecificpage = sanitize_text_field($_POST['inputredirect404tospecificpage']); //3.4.5
update_option('inputredirect404tospecificpage',$inputredirect404tospecificpage);
}
$inputredirect404tospecificpage = get_option('inputredirect404tospecificpage');
//start 3.4.1
if (isset($_POST['redirect404tospecificimageSubmit']))
{
//$redirect404imagetospecificimage = $_POST['redirect404imagetospecificimage'];
$redirect404imagetospecificimage = sanitize_text_field($_POST['redirect404imagetospecificimage']); //3.4.5
update_option('redirect404imagetospecificimage',$redirect404imagetospecificimage);
}
$redirect404imagetospecificimage = get_option('redirect404imagetospecificimage');
if (isset($_POST['inputredirect404imagetospecificimage']))
{
//$inputredirect404imagetospecificimage = $_POST['inputredirect404imagetospecificimage'];
$inputredirect404imagetospecificimage = sanitize_text_field($_POST['inputredirect404imagetospecificimage']); //3.4.5
update_option('inputredirect404imagetospecificimage',$inputredirect404imagetospecificimage);
}
$inputredirect404imagetospecificimage = get_option('inputredirect404imagetospecificimage');
if ('YES' == $redirect404imagetospecificimage)
{
if (!(empty($inputredirect404imagetospecificimage)))
{
}
}
else
{
}
//end 3.4.1
$title = '404s Global Settings';
setting_panel_404s_head($title);
$title = 'CLear 404 Log Records from Database?';
$content = '';
$content .= '';
tomas_setting_panel_404s($title, $content);
$title = 'Enable / Disable Trace New 404 logs';
$content = '';
$content .= '';
tomas_setting_panel_404s($title, $content);
$title = 'Redirect 404 to HomePage?';
$content = '';
$content .= '';
tomas_setting_panel_404s($title, $content);
$title = 'Use 301 status code or 302 status code to redirect all 404 pages?';
$content = '';
$content .= '';
tomas_setting_panel_404s($title, $content);
//!!! 2.8.3
$title = 'Send 404 Alert with Page URL to Email?';
$content = '';
$content .= '';
tomas_setting_panel_404s($title, $content);
$title = 'Redirect 404 to Specific Page?';
$content = '';
$content .= '';
tomas_setting_panel_404s($title, $content);
// start 3.4.1
$title = 'Redirect broken images to specific image?';
$content = '';
$content .= '';
tomas_setting_panel_404s($title, $content);
//end 3.4.1
}
function messageBarFor404s($p_message)
{
echo "";
echo $p_message;
echo "
";
}
function changeselectboxtoshowcustomurlfield()
{
?>