query; $pageid = $query['p']; if (!empty($pageid)) { $permalink = get_permalink($pageid); $status = get_post_status($pageid); if (($permalink != false) && ($status == 'publish')) { $redirect_id = $abj404dao->setupRedirect("", ABJ404_STATUS_AUTO, ABJ404_TYPE_POST, $permalink, $options['default_redirect'], 0); $abj404dao->logRedirectHit($redirect_id, $permalink); wp_redirect($permalink, esc_html($options['default_redirect'])); exit; } } } /** * @param type $urlRequest * @return boolean true if the request should be ignored. false otherwise. */ function shouldIgnoreRequest($urlRequest) { global $abj404logging; // Bail out if // not on 404 error page - because we're not supposed to, or // if we're currently on an admin screen - because we want admins to see 404s?, or // if we're receiving a "close connection" signal - because the user is no longer there. // Note: is_admin() does not mean the user is an admin - it returns true when the user is on an admin screen. if ((!is_404()) || (is_admin())) { return true; } // ignore requests that are supposed to be for an admin. $adminURL = parse_url(admin_url(), PHP_URL_PATH); if (substr($urlRequest, 0, strlen($adminURL)) == $adminURL) { $abj404logging->debugMessage("Ignoring request for admin URL: " . $urlRequest); return true; } // The user agent Zemanta Aggregator http://www.zemanta.com causes a lot of false positives on // posts that are still drafts and not actually published yet. It's from the plugin "WordPress Related Posts" // by https://www.sovrn.com/. This could be improved by letting the user specify when to ignore certain requests. if (strpos(strtolower(@$_SERVER['HTTP_USER_AGENT']), 'zemanta aggregator') !== false) { $abj404logging->debugMessage("Ignoring request from user agent: " . esc_html($_SERVER['HTTP_USER_AGENT']) . " for URL: " . esc_html($urlRequest)); return true; } return false; } /** * @param type $skip_db_check * @return array */ function getOptions($skip_db_check = "0") { global $abj404logic; $options = get_option('abj404_settings'); if ($options == "") { add_option('abj404_settings', '', '', 'no'); } // Check to make sure we aren't missing any new options. $defaults = $this->getDefaultOptions(); $missing = false; foreach ($defaults as $key => $value) { if (!isset( $options[$key]) || '' == $options[$key]) { $options[$key] = $value; $missing = true; } } if ($missing) { update_option('abj404_settings', $options); } if ($skip_db_check == "0") { if ($options['DB_VERSION'] != ABJ404_VERSION) { if (ABJ404_VERSION == "1.3.2") { //Unregister all crons. Some were bad. ABJ_404_Solution_PluginLogic::doUnregisterCrons(); //Register the good ones ABJ_404_Solution_PluginLogic::doRegisterCrons(); } // add the second part of the default destination page. $dest404page = $options['dest404page']; if (strpos($dest404page, '|') === false) { // not found if ($dest404page == '0') { $dest404page .= "|" . ABJ404_TYPE_404_DISPLAYED; } else { $dest404page .= '|' . ABJ404_TYPE_POST; } $options['dest404page'] = $dest404page; update_option('abj404_settings', $options); } $options = $abj404logic->doUpdateDBVersionOption(); } } return $options; } /** * @return array */ function getDefaultOptions() { $options = array( 'default_redirect' => '301', 'capture_404' => '1', 'capture_deletion' => 1095, 'manual_deletion' => '0', 'admin_notification' => '200', 'remove_matches' => '1', 'display_suggest' => '1', 'suggest_minscore' => '25', 'suggest_max' => '5', 'suggest_title' => '
' . __('No Results To Display.', '404-solution') . '
', 'suggest_cats' => '1', 'suggest_tags' => '1', 'auto_redirects' => '1', 'auto_score' => '90', 'auto_deletion' => '1095', 'auto_cats' => '1', 'auto_tags' => '1', 'force_permalinks' => '1', 'dest404page' => '0|' . ABJ404_TYPE_404_DISPLAYED, ); return $options; } function doUpdateDBVersionOption() { global $abj404logic; $options = $abj404logic->getOptions(1); $options['DB_VERSION'] = ABJ404_VERSION; update_option('abj404_settings', $options); return $options; } /** Remove cron jobs. */ static function doUnregisterCrons() { $crons = array('abj404_cleanupCronAction', 'abj404_duplicateCronAction', 'removeDuplicatesCron', 'deleteOldRedirectsCron'); for ($i = 0; $i < count($crons); $i++) { $cron_name = $crons[$i]; $timestamp1 = wp_next_scheduled($cron_name); while ($timestamp1 != False) { wp_unschedule_event($timestamp1, $cron_name); $timestamp1 = wp_next_scheduled($cron_name); } $timestamp2 = wp_next_scheduled($cron_name, ''); while ($timestamp2 != False) { wp_unschedule_event($timestamp2, $cron_name, ''); $timestamp2 = wp_next_scheduled($cron_name, ''); } wp_clear_scheduled_hook($cron_name); } } /** Create database tables. Register crons. etc. * @global type $abj404dao */ static function runOnPluginActivation() { global $abj404logic; add_option('abj404_settings', '', '', 'no'); ABJ_404_Solution_DataAccess::createDatabaseTables(); ABJ_404_Solution_PluginLogic::doRegisterCrons(); if (!isset($abj404logic)) { $abj404logic = new ABJ_404_Solution_PluginLogic(); } $abj404logic->doUpdateDBVersionOption(); } static function doRegisterCrons() { $timestampc = wp_next_scheduled('abj404_cleanupCronAction'); if ($timestampc == False) { wp_schedule_event(current_time('timestamp') - 86400, 'daily', 'abj404_cleanupCronAction'); } $timestampd = wp_next_scheduled('abj404_duplicateCronAction'); if ($timestampd == False) { wp_schedule_event(current_time('timestamp') - 3600, 'hourly', 'abj404_duplicateCronAction'); } } /** Do the passed in action and return the associated message. * @global type $abj404logic * @param type $action * @param string $sub * @return type */ function handlePluginAction($action, &$sub) { global $abj404logic; global $abj404dao; global $abj404logging; $message = ""; if ($action == "updateOptions") { if (check_admin_referer('abj404UpdateOptions') && is_admin()) { $sub = "abj404_options"; $message = $this->updateOptionsFromPOST(); if ($message == "") { $message = __('Options Saved Successfully!', '404-solution'); } else { $message .= __('Some options were not saved successfully.', '404-solution'); } } } else if ($action == "addRedirect") { if (check_admin_referer('abj404addRedirect') && is_admin()) { $message = $this->addAdminRedirect(); if ($message == "") { $message = __('New Redirect Added Successfully!', '404-solution'); } else { $message .= __('Error: unable to add new redirect.', '404-solution'); } } } else if ($action == "emptyRedirectTrash") { if (check_admin_referer('abj404_emptyRedirectTrash') && is_admin()) { $abj404logic->doEmptyTrash('redirects'); $message = __('All trashed URLs have been deleted!', '404-solution'); } } else if ($action == "emptyCapturedTrash") { if (check_admin_referer('abj404_emptyCapturedTrash') && is_admin()) { $abj404logic->doEmptyTrash('captured'); $message = __('All trashed URLs have been deleted!', '404-solution'); } } else if ($action == "purgeRedirects") { if (check_admin_referer('abj404_purgeRedirects') && is_admin()) { $message = $abj404dao->deleteSpecifiedRedirects(); } } else if (substr($action . '', 0, 4) == "bulk") { if (check_admin_referer('abj404_bulkProcess') && is_admin()) { if (!isset($_POST['idnum'])) { $abj404logging->errorMessage("No ID(s) specified for bulk action: " + esc_html($action)); echo sprintf(__("Error: No ID(s) specified for bulk action. (%s)", '404-solution'), esc_html($action), false); return; } $message = $abj404logic->doBulkAction($action, array_map('absint', $_POST['idnum'])); } } else if ($action == "deleteDebugFile") { if (check_admin_referer('abj404_deleteDebugFile') && is_admin()) { $filepath = $abj404logging->getDebugFilePath(); if (!file_exists($filepath)) { $message = sprintf(__("Debug file not found. (%s)", '404-solution'), $filepath); } else if ($abj404logging->deleteDebugFile()) { $message = sprintf(__("Debug file deleted. (%s)", '404-solution'), $filepath); } else { $message = sprintf(__("Issue deleting debug file. (%s)", '404-solution'), $filepath); } } } return $message; } /** Move redirects to trash. * @return type */ function hanldeTrashAction() { global $abj404dao; global $abj404logging; $message = ""; // Handle Trash Functionality if (isset($_GET['trash'])) { if (check_admin_referer('abj404_trashRedirect') && is_admin()) { $trash = ""; if ($_GET['trash'] == 0) { $trash = 0; } else if ($_GET['trash'] == 1) { $trash = 1; } else { $abj404logging->debugMessage("Unexpected trash operation: " . esc_html($_GET['trash'])); $message = __('Error: Bad trash operation specified.', '404-solution'); return $message; } $message = $abj404dao->moveRedirectsToTrash(absint($_GET['id']), $trash); if ($message == "") { if ($trash == 1) { $message = __('Redirect moved to trash successfully!', '404-solution'); } else { $message = __('Redirect restored from trash successfully!', '404-solution'); } } else { if ($trash == 1) { $message = __('Error: Unable to move redirect to trash.', '404-solution'); } else { $message = __('Error: Unable to move redirect from trash.', '404-solution'); } } } } return $message; } function handleDeleteLogAction() { $message = ""; //Handle Delete Functionality if (@$_GET['deleteDebugFile'] == '1') { if (check_admin_referer('abj404_deleteDebugFile') && is_admin()) { $abj404dao->deleteRedirect(absint($_GET['id'])); $message = __('Redirect Removed Successfully!', '404-solution'); } } return $message; } /** Delete redirects. * @global type $abj404dao * @return type */ function handleDeleteAction() { global $abj404dao; $message = ""; //Handle Delete Functionality if (@$_GET['remove'] == 1) { if (check_admin_referer('abj404_removeRedirect') && is_admin()) { if (preg_match('/[0-9]+/', $_GET['id'])) { $abj404dao->deleteRedirect(absint($_GET['id'])); $message = __('Redirect Removed Successfully!', '404-solution'); } } } return $message; } /** Set a redirect as ignored. * @return type */ function handleIgnoreAction() { global $abj404dao; global $abj404logging; $message = ""; //Handle Ignore Functionality if (isset($_GET['ignore'])) { if (check_admin_referer('abj404_ignore404') && is_admin()) { if ($_GET['ignore'] != 0 && $_GET['ignore'] != 1) { $abj404logging->debugMessage("Unexpected ignore operation: " . esc_html($_GET['ignore'])); $message = __('Error: Bad ignore operation specified.', '404-solution'); return $message; } if (preg_match('/[0-9]+/', $_GET['id'])) { if ($_GET['ignore'] == 1) { $newstatus = ABJ404_STATUS_IGNORED; } else { $newstatus = ABJ404_STATUS_CAPTURED; } $message = $abj404dao->updateRedirectTypeStatus(absint($_GET['id']), $newstatus); if ($message == "") { if ($newstatus == ABJ404_STATUS_CAPTURED) { $message = __('Removed 404 URL from ignored list successfully!', '404-solution'); } else { $message = __('404 URL marked as ignored successfully!', '404-solution'); } } else { if ($newstatus == ABJ404_STATUS_CAPTURED) { $message = __('Error: unable to remove URL from ignored list', '404-solution'); } else { $message = __('Error: unable to mark URL as ignored', '404-solution'); } } } } } return $message; } /** Edit redirect data. * @return type */ function handleEditAction(&$sub) { global $abj404dao; $message = ""; //Handle edit posts if (@$_POST['action'] == "editRedirect") { $id = $abj404dao->getPostOrGetSanitize('id'); $ids = $abj404dao->getPostOrGetSanitize('ids_multiple'); if (!($id === null && $ids === null) && (preg_match('/[0-9]+/', '' . $id) || preg_match('/[0-9]+/', '' . $ids))) { if (check_admin_referer('abj404editRedirect') && is_admin()) { $message = $this->updateRedirectData(); if ($message == "") { $message .= __('Redirect Information Updated Successfully!', '404-solution'); $sub = "redirects"; } else { $message .= __('Error: Unable to update redirect data.', '404-solution'); } } } } return $message; } /** * @global type $abj404dao * @param type $action * @param type $ids * @return string */ function doBulkAction($action, $ids) { global $abj404dao; global $abj404logging; $message = ""; // nonce already verified. $abj404logging->debugMessage("In doBulkAction. Action: " . esc_html($action == '' ? '(none)' : $action)) . ", ids: " . wp_kses_post(json_encode($ids)); if ($action == "bulkignore" || $action == "bulkcaptured") { if ($action == "bulkignore") { $status = ABJ404_STATUS_IGNORED; } else if ($action == "bulkcaptured") { $status = ABJ404_STATUS_CAPTURED; } else { $abj404logging->errorMessage("Unrecognized bulk action: " + esc_html($action)); echo sprintf(__("Error: Unrecognized bulk action. (%s)", '404-solution'), esc_html($action)); return; } $count = 0; foreach ($ids as $id) { $s = $abj404dao->updateRedirectTypeStatus($id, $status); if ($s == "") { $count++; } } if ($action == "bulkignore") { $message = $count . " " . __('URLs marked as ignored.', '404-solution'); } else if ($action == "bulkcaptured") { $message = $count . " " . __('URLs marked as captured.', '404-solution'); } else { $abj404logging->errorMessage("Unrecognized bulk action: " + esc_html($action)); echo sprintf(__("Error: Unrecognized bulk action. (%s)", '404-solution'), esc_html($action)); } } else if ($action == "bulktrash") { $count = 0; foreach ($ids as $id) { $s = $abj404dao->moveRedirectsToTrash($id, 1); if ($s == "") { $count ++; } } $message = $count . " " . __('URLs moved to trash', '404-solution'); } else if ($action == "bulkedit") { $cleanedIds = array_map('absint', $ids); $allids = implode(',', $cleanedIds); $editlink = "?page=abj404_solution&subpage=abj404_edit&ids_multiple=" . $allids; $abj404logging->debugMessage("Redirecting for edit: " . esc_html($editlink)); wp_redirect($editlink, '302'); exit; } else { $abj404logging->errorMessage("Unrecognized bulk action: " + esc_html($action)); echo sprintf(__("Error: Unrecognized bulk action. (%s)", '404-solution'), esc_html($action)); } return $message; } /** * This is for both empty trash buttons (page redirects and captured 404 URLs). * @param type $sub */ function doEmptyTrash($sub) { global $abj404dao; $tableOptions = $this->getTableOptions(); $rows = $abj404dao->getRedirects($sub, $tableOptions, 0); // nonce already verified. foreach ($rows as $row) { $abj404dao->deleteRedirect($row['id']); } } /** * @global type $abj404dao * @return string */ function updateRedirectData() { global $abj404dao; global $abj404logging; $message = ""; $fromURL = ""; $ids_multiple = ""; if (@$_POST['url'] == "" && @$_POST['ids_multiple'] != "") { $ids_multiple = array_map('absint', explode(',', $_POST['ids_multiple'])); } else if (@$_POST['url'] != "" && @$_POST['ids_multiple'] == "") { $fromURL = $_POST['url']; } else { $message .= __('Error: URL is a required field.', '404-solution') . "