. */ // exit if add_action or plugins_url functions do not exist if (!function_exists('add_action') || !function_exists('plugins_url')) exit; // function to replace wp_die if it doesn't exist if (!function_exists('wp_die')) : function wp_die ($message = 'wp_die') { die($message); } endif; // define some definitions if they already are not !defined('WP_CONTENT_DIR') && define('WP_CONTENT_DIR', ABSPATH . 'wp-content'); !defined('WP_PLUGIN_DIR') && define('WP_PLUGIN_DIR', WP_CONTENT_DIR . '/plugins'); !defined('WP_CONTENT_URL') && define('WP_CONTENT_URL', get_option('siteurl') . '/wp-content'); !defined('WP_PLUGIN_URL') && define('WP_PLUGIN_URL', WP_CONTENT_URL . '/plugins'); // don't load directly !defined('ABSPATH') && die('-1'); if(!function_exists('set_current_screen')): /** * Set the current screen object * * @since 3.0.0 * * @uses $current_screen * * @param string $id Screen id, optional. */ function aa_set_current_screen( $id = '' ) { global $current_screen, $hook_suffix, $typenow, $taxnow; $action = ''; if ( empty($id) ) { $current_screen = $hook_suffix; $current_screen = str_replace('.php', '', $current_screen); if ( preg_match('/-add|-new$/', $current_screen) ) $action = 'add'; $current_screen = str_replace('-new', '', $current_screen); $current_screen = str_replace('-add', '', $current_screen); $current_screen = array('id' => $current_screen, 'base' => $current_screen); } else { $id = sanitize_key($id); if ( false !== strpos($id, '-') ) { list( $id, $typenow ) = explode('-', $id, 2); if ( taxonomy_exists( $typenow ) ) { $id = 'edit-tags'; $taxnow = $typenow; $typenow = ''; } } $current_screen = array('id' => $id, 'base' => $id); } $current_screen = (object) $current_screen; $current_screen->action = $action; // Map index to dashboard if ( 'index' == $current_screen->base ) $current_screen->base = 'dashboard'; if ( 'index' == $current_screen->id ) $current_screen->id = 'dashboard'; if ( 'edit' == $current_screen->id ) { if ( empty($typenow) ) $typenow = 'post'; $current_screen->id .= '-' . $typenow; $current_screen->post_type = $typenow; } elseif ( 'post' == $current_screen->id ) { if ( empty($typenow) ) $typenow = 'post'; $current_screen->id = $typenow; $current_screen->post_type = $typenow; } elseif ( 'edit-tags' == $current_screen->id ) { if ( empty($taxnow) ) $taxnow = 'post_tag'; $current_screen->id = 'edit-' . $taxnow; $current_screen->taxonomy = $taxnow; } $current_screen = apply_filters('current_screen', $current_screen); } endif; /** * AA_G404 * * @package * @author AskApache * @version 2009 * @access public * http://googlesystem.blogspot.com/2008/02/google-toolbar-and-404-error-pages.html */ class AA_G404 { var $options = array(); // an array of options and values var $plugin = array(); // array to hold plugin information var $code = array('css' => '', 'javascript' => '', 'html' => ''); // array to hold the css, javascript, and html /** * Defined blank for loading optimization */ function AA_G404() {} /** * Loads options named by opts array into correspondingly named class vars */ function LoadOptions($opts=array('options', 'code', 'plugin')) { foreach ($opts as $pn) $this->{$pn} = get_option("askapache_google_404_{$pn}"); } /** * Saves options from class vars passed in by opts array and the adsense key and api key */ function SaveOptions($opts=array('options','code','plugin')) { foreach ($opts as $pn) update_option("askapache_google_404_{$pn}", $this->{$pn}); $this->save_keys(); } /** * Gets and sets the default values for the plugin options, then saves them */ function default_options() { // get all the plugin array data $this->plugin = $this->get_plugin_data(); // original code that comes with plugin $this->code = $this->get_default_code(); $ads_key = get_option('aa_google_404_adsense_key'); $api_key = get_option('aa_google_404_api_key'); $ana_key = get_option('aa_google_404_analytics_key'); $mana_key = get_option('aa_google_404_mobile_analytics_key'); foreach ( array("code", "plugin", "orig_code", "iframe_one_time") as $pn ) delete_option("askapache_google_404_{$pn}" ); // default options $this->options = array( 'api_key' => ($api_key !== false && strlen($api_key) > 5) ? $api_key : '', // ABQIAAAAGpnYzhlFfhxcnc02U1NT1hSrXMCP0pDj9HHVk8NG53Pp2_-7KxSdZ5paIt0ciL3cNLv20-kmmxlTcA 'adsense_key' => ($ads_key !== false && strlen($ads_key) > 5) ? $ads_key : '', // pub-4356884677303281 'analytics_key' => ($ana_key !== false && strlen($ana_key) > 5) ? $ana_key : '', // UA-732153-7 'mobile_analytics_key' => ($mana_key !== false && strlen($mana_key) > 5) ? $mana_key : '', // UA-732153-7 'analytics_url' => '"/404/?page=" + document.location.pathname + document.location.search + "&from=" + document.referrer', 'enabled' => '1', // 404 error handling is ON by default 'google_ajax' => '1', // google ajax search results are ON by default 'google_404' => '0', // googles new 404 script is OFF by default 'analytics_log' => '1', // 'mobile_analytics_log' => '0', 'robots_meta' => '1', // adding noindex,follow robot meta tag to error pages is ON by default 'robots_tag' => 'noindex,follow', // the value of the robot meta on error pages 'related_posts' => '1', // showing related posts on error pages is ON by default 'related_num' => 10, // number of related posts to show 'related_length'=> 240, // length of related posts excerpts 'recent_posts' => '1', // showing recent posts on error pages is ON by default 'recent_num' => 6, // number of recent posts to show 'tag_cloud' => '1', // showing a tag cloud on error pages is ON by default 'tag_cloud_num' => 100, // number tags used to create cloud 'show_result_site' => '1', 'show_result_video' => '1', 'show_result_blogs' => '1', 'show_result_cse' => '1', 'show_result_image' => '1', 'show_result_news' => '1', 'show_result_web' => '1', 'show_result_local' => '1', '404_handler' => trailingslashit(WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . str_replace("\\","/",basename(dirname(__FILE__)))) . '404.php', // the file location of 404 template 'iframe_preview'=> '0', // iframe preview in admin area is OFF by default 'iframe_url' => site_url('wordpress-google-AskApache/f2?askapache=htaccess-plugin&missing-5+this-post') // the url for the iframe preview ); // Save all these variables to database $this->SaveOptions(); } /** * currently code is not saved across upgrades due to a potential security issue */ function upgrade_settings() { // initiate all vars to empty string $api_key=$adsense_key=$analytics_key=$code_html=$code_js=$code_css=$code=$options=''; // get the oldest options from earliest version foreach (array('api_key','adsense_key', 'analytics_key', 'code_html', 'code_js', 'code_css') as $pn) $$pn = get_option("aa_google_404_{$pn}"); // get old options if more current version foreach (array('code','options') as $pn) $$pn = get_option("askapache_google_404_{$pn}"); // first make sure to transfer over any existing api key, as the google ajax now wont work without it if ($api_key !== false && strlen($api_key) > 5 ) update_option('aa_google_404_api_key', $api_key); if ($adsense_key !== false && strlen($adsense_key) > 5 ) update_option('aa_google_404_adsense_key', $adsense_key); if ($analytics_key !== false && strlen($analytics_key) > 5 ) update_option('aa_google_404_analytics_key', $analytics_key); if ($options !==false && is_array($options) && array_key_exists('api_key', $options) && strlen($options['api_key']) > 5 ) update_option('aa_google_404_api_key', $options['api_key']); } /** * Saves the api and adsense keys */ function save_keys() { update_option('aa_google_404_api_key', $this->options['api_key']); update_option('aa_google_404_adsense_key', $this->options['adsense_key']); update_option('aa_google_404_analytics_key', $this->options['analytics_key']); } /** * Gets the default code for css, html, and javascript by reading the original file in this plugins folder/f/orig.(css|javascript|html) */ function get_default_code() { foreach(array_keys($original_code = array('css' => '', 'html' => '', 'javascript' => '')) as $pn) $original_code["{$pn}"] = $this->_readfile(trailingslashit(WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . str_replace("\\","/",basename(dirname(__FILE__)))) . "f/orig.{$pn}"); return $original_code; } /** * Loads the options into the class vars. * Adds this plugins 'load' function to the 'load-plugin' hook. * Adds this plugins 'admin_print_styles' function to the 'admin_print_styles-plugin' hook. */ function init() { $this->LoadOptions(); add_action("load-{$this->plugin['hook']}", array(&$this, 'load')); add_action("admin_print_styles-{$this->plugin['hook']}", create_function('', 'echo "";')); add_action("admin_footer-{$this->plugin['hook']}", create_function('', 'echo "";')); } /** * The load function executed by the load-plugin hook. Passes control of request handling to the 'handle_post' function. * Adds the meta-boxes and the contextual help. * Enqueues the neccessary js and css files for plugin adminstration. */ function load() { global $screen,$current_screen; if(function_exists('set_current_screen')) set_current_screen(); else if(function_exists('aa_set_current_screen')) aa_set_current_screen(); // parse and handle post requests to plugin 'POST' == $_SERVER['REQUEST_METHOD'] && $this->handle_post(); // add meta boxes - can someone help me figure this damn metabox situation out.. not even 2.9 is extendable enough //foreach(array('404 Options', 'Plugin Options', 'Google Options', 'Robot Options', 'Recent Posts Options', 'Related Posts Options', 'Popular Tag Cloud', 'CSS Code', 'HTML Code', 'Javascript Code') as $box) //foreach(array('Main Options', 'CSS Code', 'HTML Code', 'Javascript Code') as $box) //add_meta_box(preg_replace('/\W/i', '', strtolower(str_replace(' ', '_', "ag4_box_{$box}"))), "{$box}", array(&$this, "print_box"), $current_screen->id, 'normal', 'high'); // add contextual help $help = '
For super-advanced users, or those with access and knowledge of Apache .htaccess/httpd.conf files';
$help .=' you should check that your error pages are correctly returning a 404 Not Found';
$help .=' HTTP Header and not a 200 OK Header which appears to be the default for many WP installs, this plugin attempts to fix this using PHP, but the best way I have found';
$help .=' is to add the following to your .htaccess file.
ErrorDocument 404 /index.php?error=404'."\n".'Redirect 404 /index.php?error=404'; $help .= '
You can check your headers by requesting a bad url on your site using my online tool Advanced HTTP Headers.
'; $help .= 'The goal of this plugin is to boost your sites SEO by telling search engines to ignore your error pages, with the focus on human users to increase people staying on your site and being'; $help .= ' able to find what they were originally looking for on your site. Because I am obsessed with fast web pages, many various speed/efficiency improvements are also on the horizon.
'; $help .= 'Another feature that I am using with beta versions of this plugin, is tracking information for you to go over at your leisure, to fix recurring problems. The information is collected'; $help .= ' is the requested url that wasnt found, the referring url that contains the invalid link.
'; $help .= 'The reason I didnt include it in this release is because for sites like AskApache with a very high volume of traffic (and thus 404 requests) this feature can create a bottleneck and '; $help .= 'slow down or freeze a blog if thousands of 404 errors are being requested and saved to the database. This could also very quickly be used by malicious entities as a Denial of Service '; $help .= 'attack. So I am figuring out and putting into place limits.. like once a specific requested url resulting in a not found error has been requested 100x in a day, an email is sent to the '; $help .= 'blog administrator. But to prevent Email DoS and similar problems with the number and interval of emails allowed by your email provider other considerations on limits need to be examined.
'; $help .= 'Future versions of this plugin will add this option for everyone.. Basically, there will be an option to switch to using a 100% javascript (instead of javascript + php) method of '; $help .= 'handling 404 errors, this will be BIG because the plugin will simply create 1 static html file named 404.html and then use .htaccess ErrorDocument to redirect all 404 errors to this '; $help .= 'static html file. The downside is the only way to get stuff like related posts and recent posts would be to use ajax or to create the 404.html static file at regular intervals or for '; $help .= 'multiple error requests. This will help tremendously in keeping your site and server speedy as it will reduce CPU/Memory/Disk IO/and Database Queries to almost nothing. Stay tuned.
'; $help .= 'One other big improvement or feature-add is to show the admin a list of error urls and allow the admin to specify the correct url that the error url should point to. Then using mod_rewrite '; $help .= 'rules automatically generated by the plugin and added to .htaccess these error urls will 301 redirect to the correct urls, boosting your SEO further and also helping your visitors. A '; $help .= 'big difference between this method and other redirection plugins is that it will use mod_rewrite, I would really like to avoid using php to redirect or rewrite to other urls, as this method'; $help .= ' has a HUGE downside in terms of your site and servers speed, bandwidth usage, CPU/Memory usage, Disk Input/Output (writes/reads), security issues, Database Usage, among other problems.
'; $help .= 'Anyone smart enough to find and use this plugin deserves to earn a little income too, so I am working on integrating AdSense into the Search Results. Currently this is very new and not '; $help .= 'enabled or allowed by Google in certain circumstances and just isnt a feature yet of the Google AJAX API. At the very least I am going to add a custom search engine results for your site '; $help .= 'that will allow you to display relevant ads, but I am still waiting for some clarification from my Google Homeslices on whether we can use the AJAX API to display ADS on 404 error pages '; $help .= 'automatically based on the requested url or if that violates the Google TOS, which is something I would never condone or even get close to violating. If not then we will have to settle '; $help .= 'for no ADS being displayed automatically and only being displayed if the user actually types something in the search box. So go get your AdSense account (free) and also sign up for a '; $help .= 'Google CSE (custom search engine) as soon as possible.
'; $help .= 'Please visit AskApache.com or send me an email at webmaster@askapache.com
"; echo ''; echo '
options[$id],false)." />";
echo "
";
echo "
";
echo "
options[$id]) ? $this->options[$id] : '')."' />
Expect:",
422 => "The server understands the media type of the request entity, but was unable to process the contained instructions.",
423 => "The requested resource is currently locked. The lock must be released or proper identification given before the method can be applied.",
424 => "The method could not be performed on the resource because the requested action depended on another action and that other action failed.",
425 => '',
426 => "The requested resource can only be retrieved using SSL. Either upgrade your client, or try requesting the page using https://",
500 => '',
501 => "%M% to %U% not supported.",
502 => "The proxy server received an invalid response from an upstream server.",
503 => "The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later.",
504 => "The proxy server did not receive a timely response from the upstream server.",
505 => '',
506 => "A variant for the requested resource %U% is itself a negotiable resource. This indicates a configuration error.",
507 => "The method could not be performed. There is insufficient free space left in your storage allocation.",
510 => "A mandatory extension policy in the request is not accepted by the server for this resource."
);
/**
*/
function AA_G404_Handler(){}
/**
*/
function handle_it()
{
global $AA_G404, $wp_did_header, $wp_did_template_redirect, $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID;
if (is_array($wp_query->query_vars)) extract($wp_query->query_vars, EXTR_SKIP);
// reset AA_G404_Handler vars
$this->uri = $this->sc = $this->msg = $this->reason = '';
$this->uri = esc_attr(stripslashes($_SERVER['REQUEST_URI']));
$this->sc = absint((isset($_SERVER['REDIRECT_STATUS']) && $_SERVER['REDIRECT_STATUS'] != 200) ? $_SERVER['REDIRECT_STATUS'] : (!isset($_REQUEST['error'])) ? 404 : $_REQUEST['error']);
if ('HTTP/1.1' != $_SERVER["SERVER_PROTOCOL"] && 'HTTP/1.0' != $_SERVER["SERVER_PROTOCOL"] && $_SERVER["SERVER_PROTOCOL"] = 'HTTP/1.0') $this->sc = 505;
$this->reason = get_status_header_desc($this->sc);
if ($this->sc == 402 || $this->sc == 409 || $this->sc == 425 || $this->sc == 500 || $this->sc == 505) $this->msg = 'The server encountered an internal error or misconfiguration and was unable to complete your request.';
else $this->msg = (array_key_exists($this->sc, $this->ASC) !== false) ? str_replace(array('%U%', '%M%'), array($this->uri, $_SERVER['REQUEST_METHOD']), $this->ASC["{$this->sc}"]) : 'Error';
if ($this->sc == 400 || $this->sc == 403 || $this->sc == 405 || floor($this->sc / 100) == 5) {
if ($this->sc == 405) @header('Allow: GET,HEAD,POST,OPTIONS,TRACE', 1, 405);
echo "\n\n{$this->msg}
\n
' . wp_tag_cloud(array('echo' => false)) . '
' : ''); $sr = array( '%error_title%' => $this->sc . ' ' . $this->reason, '%related_posts%' => $related, '%recent_posts%' => $recent, '%google_helper%' => $google_helper, '%tag_cloud%' => $tag_cloud, ); if ($AA_G404->options['google_ajax'] == '1') echo str_replace(array_keys($sr), array_values($sr), $AA_G404->code['html']); if ($AA_G404->options['mobile_analytics_log'] == '1')echo $this->mobile_tracker_image(); } /** */ function wp_header() { if (!is_404()) return; global $AA_G404; if ($AA_G404->options['analytics_log'] == '1') : ?> options['google_ajax'] == '1') { if ($AA_G404->options['show_result_site'] != '1')$AA_G404->code['javascript']=str_replace('this.rCT.addSearcher(gSearchSite);','//this.rCT.addSearcher(gSearchSite);',$AA_G404->code['javascript']); if ($AA_G404->options['show_result_video'] != '1')$AA_G404->code['javascript']=str_replace('this.lCT.addSearcher(gSearchVideo, sOPT);','//this.lCT.addSearcher(gSearchVideo, sOPT);',$AA_G404->code['javascript']); if ($AA_G404->options['show_result_image'] != '1')$AA_G404->code['javascript']=str_replace('this.lCT.addSearcher(gSearchImage, sOPT);','//this.lCT.addSearcher(gSearchImage, sOPT);',$AA_G404->code['javascript']); if ($AA_G404->options['show_result_blogs'] != '1')$AA_G404->code['javascript']=str_replace('this.rCT.addSearcher(gSearchBlog);','//this.rCT.addSearcher(gSearchBlog);',$AA_G404->code['javascript']); if ($AA_G404->options['show_result_web'] != '1')$AA_G404->code['javascript']=str_replace('this.rCT.addSearcher(gSearchWeb);','//this.rCT.addSearcher(gSearchWeb);',$AA_G404->code['javascript']); if ($AA_G404->options['show_result_news'] != '1') $AA_G404->code['javascript']=str_replace('this.rCT.addSearcher(gSearchNews);','//this.rCT.addSearcher(gSearchNews);',$AA_G404->code['javascript']); if ($AA_G404->options['show_result_cse'] != '1') $AA_G404->code['javascript']=str_replace('this.rCT.addSearcher(gSearchCSE);','//this.rCT.addSearcher(gSearchCSE);',$AA_G404->code['javascript']); if ($AA_G404->options['show_result_local'] != '1')$AA_G404->code['javascript']=str_replace('this.rCT.addSearcher(gSearchLocal);','//this.rCT.addSearcher(gSearchLocal);',$AA_G404->code['javascript']); } printf('%9$s%1$s' . '%1$s%9$s', "\n", get_option('blogname'), str_replace('www.', '', $_SERVER['HTTP_HOST']), $this->get_keywords('|', 6), ' ',//'OR allinurl:' . $this->get_keywords(' ', 2), get_bloginfo('language'), get_bloginfo('wpurl'), 'http://www.google.com/jsapi?key=' . $AA_G404->options['api_key'], "", str_replace('}', "};", $AA_G404->code['javascript']), preg_replace(array('/\0+/','/(\\\\0)+/','/\s\s+/',"/(\r\n|\n|\r)/",'/\/\*(.*?)\*\//','/(:|,|;) /','# +{#','#{ +#','#} +#','# +}#','#;}#','#,+#','# +,#'),array('','',' ',"\n",'',"\\1",'{','{','}','}','}',',',','), $AA_G404->code['css']) ); if($AA_G404->options['robots_meta'] == '1') echo "\n" . '' . "\n"; } function mobile_tracker_image() { global $AA_G404; return '.%4$s%4$s', get_permalink($r->ID), esc_attr(stripslashes(apply_filters('the_title', $r->post_title))), substr(wp_trim_excerpt(stripslashes(strip_tags($r->post_content))), 0, $l), "\n"); } } return $out; } } if (!function_exists('aa_google_404')) { /** */ function aa_google_404() { global $AA_G404_Handler, $AA_G404; if (!is_object($AA_G404))$AA_G404 = new AA_G404(); if (!is_object($AA_G404_Handler))$AA_G404_Handler = new AA_G404_Handler(); $AA_G404_Handler->output(); } } $AA_G404 = new AA_G404(); add_action('init', array(&$AA_G404, 'init')); add_action('template_redirect', array(&$AA_G404, 'template_redirect'),200); /** * * * @return */ function aa_google_404_fulltext() { global $wpdb, $AA_G404; $wpdb->hide_errors(); $wpdb->query('ALTER TABLE '.$wpdb->posts.' ENGINE = MYISAM;'); $wpdb->query('ALTER TABLE '.$wpdb->posts.' DROP INDEX post_related'); $wpdb->query('ALTER TABLE '.$wpdb->posts.' ADD FULLTEXT post_related ( post_title , post_content )'); $wpdb->show_errors(); if(!is_object($AA_G404))$AA_G404=new AA_G404(); $AA_G404->upgrade_settings();$AA_G404->default_options(); } if (is_admin()) : register_activation_hook(__FILE__, 'aa_google_404_fulltext'); //add_filter('screen_meta_screen', //create_function('$a', 'if($a == "askapachegoogle")return "settings_page_askapache-google-404";else return $a;')); /*add_filter('screen_settings', create_function('$a', 'if($a == "settings_page_askapache-google-404")return "settings_page_askapache-google-404";else return $a;')); */ add_filter('screen_layout_columns', create_function('$a', 'if(!array_key_exists("settings_page_askapache-google-404",$a))return array_merge($a,array("settings_page_askapache-google-404"=>2));else return $a;')); add_action('admin_menu', create_function('', 'global $AA_G404; if(!is_object($AA_G404))$AA_G404=new AA_G404(); add_options_page( "AskApache Google 404", "AA Google 404", "administrator", "askapache-google-404.php", array(&$AA_G404,"options_page"));')); add_filter('plugin_action_links_askapache-google-404/askapache-google-404.php', create_function('$l', 'return array_merge(array("Settings"), $l);')); add_action('deactivate_askapache-google-404/askapache-google-404.php', create_function('', 'foreach ( array("code", "plugin", "orig_code", "iframe_one_time") as $pn ) delete_option("askapache_google_404_{$pn}" );')); add_action('admin_footer-settings_page_askapache-google-404', create_function('','$g="";$g.="\n\n";echo $g;')); endif; ?>%3$s...
%4$s