options = get_option( self::opt, array() ); $this->table = $GLOBALS['wpdb']->prefix . '404_log'; if( !isset( $this->options['db_version'] ) || $this->options['db_version'] < self::db_version ) { // upgrade placeholder $this->install_table(); $defaults = array( 'max_entries' => 500, 'also_record' => array( 'ip', 'ua', 'ref' ) ); foreach( $defaults as $k => $v ) if( !isset( $this->options[$k] ) ) $this->options[$k] = $v; $this->options['db_version'] = self::db_version; update_option( self::opt, $this->options ); } add_action( 'template_redirect', array( $this, 'log_404s' ) ); if( is_admin() ) { add_action( 'admin_menu', array( $this, 'settings_menu' ) ); add_action( 'admin_head', array( $this, 'load_table' ) ); } } private function install_table() { // remember, two spaces after PRIMARY KEY otherwise WP borks $sql = "CREATE TABLE $this->table ( id SMALLINT NOT NULL AUTO_INCREMENT, date DATETIME NOT NULL, url VARCHAR(512) NOT NULL, ref VARCHAR(512) NOT NULL default '', ip VARCHAR(40) NOT NULL default '', ua VARCHAR(512) NOT NULL default '', PRIMARY KEY (id) );"; require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); dbDelta($sql); } function settings_menu() { add_submenu_page('tools.php', '404 Error Log', '404 Error Log', 'manage_options', '404_error_log', array( $this, 'settings_page') ); } function load_table(){ // need to load the list table early for WP to catch it if( get_current_screen()->id == 'tools_page_404_error_log' ) { require_once( dirname( __FILE__ ) . '/includes/class-log-404-list-table.php' ); $this->list_table = new Log_404_List_Table( $this->options['also_record'] ); } } function settings_page() { ?>

404 Error Log

You do not currently have pretty permalinks enabled on your site. This means that WordPress does not handle requests for pages that are not found on your site (your web server handles them directly), and so this plugin cannot log them. You need to be using pretty permalinks in order for this plugin to work.

'; echo ''; return; } if( isset( $_GET['view'] ) && $_GET['view'] == 'options' ) $this->manage_options(); else $this->show_log(); } private function subsubsub(){ $manage_options = isset( $_GET['view'] ) && $_GET['view'] == 'options'; echo ''; } private function show_log(){ $this->list_table->prepare_items(); $this->subsubsub(); ?>
list_table->search_box( 'Search log', 'log' ); ?> list_table->display(); ?>
options['also_record'] = empty( $_POST['also_record'] ) ? array() : (array) $_POST['also_record']; $this->options['max_entries'] = abs( intval( $_POST['max_entries'] ) ); update_option( self::opt, $this->options ); echo '

Options updated.

'; } $this->subsubsub(); ?>
Additional data to record 'HTTP Referer', 'ip' => 'Client IP Address', 'ua' => 'Client User Agent' ) as $k => $v ) { $checked = checked( in_array( $k, $this->options['also_record'] ), true, false ); echo "
"; } ?>

current_time('mysql'), 'url' => $_SERVER['REQUEST_URI'] ); if( in_array( 'ip', $this->options['also_record'] ) ) $data['ip'] = $_SERVER['REMOTE_ADDR']; if( in_array( 'ref', $this->options['also_record'] ) && isset( $_SERVER['HTTP_REFERER'] ) ) $data['ref'] = $_SERVER['HTTP_REFERER']; if( in_array( 'ua', $this->options['also_record'] ) && isset( $_SERVER['HTTP_USER_AGENT'] ) ) $data['ua'] = $_SERVER['HTTP_USER_AGENT']; // trim stuff foreach( array( 'url', 'ref', 'ua' ) as $k ) if( isset( $data[$k] ) ) $data[$k] = substr( $data[$k], 0, 512 ); $wpdb->insert( $this->table, $data ); // pop old entry if we exceeded the limit $max = $this->options['max_entries']; $cutoff = $wpdb->get_var( "SELECT id FROM $this->table ORDER BY id DESC LIMIT $max,1" ); if( $cutoff ) $wpdb->query( "DELETE FROM $this->table WHERE id <= $cutoff" ); } } new Log_404();