Example #1
0
function wbz404_adminRedirectsPage()
{
    global $wpdb;
    $sub = "redirects";
    $options = wbz404_getOptions();
    $tableOptions = wbz404_getTableOptions();
    wbz404_drawFilters($sub, $tableOptions);
    $columns['url']['title'] = "URL";
    $columns['url']['orderby'] = "url";
    $columns['url']['width'] = "25%";
    $columns['status']['title'] = "Status";
    $columns['status']['orderby'] = "status";
    $columns['status']['width'] = "5%";
    $columns['type']['title'] = "Type";
    $columns['type']['orderby'] = "type";
    $columns['type']['width'] = "10%";
    $columns['dest']['title'] = "Destination";
    $columns['dest']['orderby'] = "final_dest";
    $columns['dest']['width'] = "25%";
    $columns['code']['title'] = "Redirect";
    $columns['code']['orderby'] = "code";
    $columns['code']['width'] = "5%";
    $columns['hits']['title'] = "Hits";
    $columns['hits']['orderby'] = "hits";
    $columns['hits']['width'] = "10%";
    $columns['timestamp']['title'] = "Created";
    $columns['timestamp']['orderby'] = "timestamp";
    $columns['timestamp']['width'] = "10%";
    $columns['last_used']['title'] = "Last Used";
    $columns['last_used']['orderby'] = "";
    $columns['last_used']['width'] = "10%";
    date_default_timezone_set(get_option('timezone_string'));
    echo "<div class=\"tablenav\">";
    wbz404_drawPaginationLinks($sub, $tableOptions);
    if ($tableOptions['filter'] == '-1') {
        echo "<div class=\"alignleft actions\">";
        $eturl = "?page=wbz404_redirected&filter=-1";
        $trashaction = "wbz404_emptyRedirectTrash";
        $eturl = wp_nonce_url($eturl, $trashaction);
        echo "<form method=\"POST\" action=\"" . $eturl . "\">";
        echo "<input type=\"hidden\" name=\"action\" value=\"emptyRedirectTrash\">";
        echo "<input type=\"submit\" class=\"button-secondary\" value=\"" . wbz404_trans('Empty Trash') . "\">";
        echo "</form>";
        echo "</div>";
    }
    echo "</div>";
    echo "<table class=\"wp-list-table widefat fixed\">";
    echo "<thead>";
    wbz404_buildTableColumns($sub, $tableOptions, $columns);
    echo "</thead>";
    echo "<tfoot>";
    wbz404_buildTableColumns($sub, $tableOptions, $columns);
    echo "</tfoot>";
    echo "<tbody id=\"the-list\">";
    $rows = wbz404_getRecords($sub, $tableOptions);
    $displayed = 0;
    $y = 1;
    foreach ($rows as $row) {
        $displayed++;
        $status = "";
        if ($row['status'] == WBZ404_MANUAL) {
            $status = wbz404_trans('Manual');
        } else {
            if ($row['status'] == WBZ404_AUTO) {
                $status = wbz404_trans('Automatic');
            }
        }
        $type = "";
        $dest = "";
        $link = "";
        $title = wbz404_trans('Visit') . " ";
        if ($row['type'] == WBZ404_EXTERNAL) {
            $type = wbz404_trans('External');
            $dest = $row['final_dest'];
            $link = $row['final_dest'];
            $title .= $row['final_dest'];
        } else {
            if ($row['type'] == WBZ404_POST) {
                $type = wbz404_trans('Post/Page');
                $permalink = wbz404_permalinkInfo($row['final_dest'] . "|POST", 0);
                $dest = $permalink['title'];
                $link = $permalink['link'];
                $title .= $permalink['title'];
            } else {
                if ($row['type'] == WBZ404_CAT) {
                    $type = wbz404_trans('Category');
                    $permalink = wbz404_permalinkInfo($row['final_dest'] . "|CAT", 0);
                    $dest = $permalink['title'];
                    $link = $permalink['link'];
                    $title .= wbz404_trans('Category:') . " " . $permalink['title'];
                } else {
                    if ($row['type'] == WBZ404_TAG) {
                        $type = wbz404_trans('Tag');
                        $permalink = wbz404_permalinkInfo($row['final_dest'] . "|TAG", 0);
                        $dest = $permalink['title'];
                        $link = $permalink['link'];
                        $title .= wbz404_trans('Tag:') . " " . $permalink['title'];
                    }
                }
            }
        }
        $hits = $row['hits'];
        $last_used = wbz404_getRedirectLastUsed($row['id']);
        if ($last_used != 0) {
            $last = date("Y/m/d h:i:s A", $last_used);
        } else {
            $last = wbz404_trans('Never Used');
        }
        $editlink = "?page=wbz404_redirected&subpage=wbz404_edit&id=" . $row['id'];
        $logslink = "?page=wbz404_redirected&subpage=wbz404_logs&id=" . $row['id'];
        $trashlink = "?page=wbz404_redirected&id=" . $row['id'];
        $deletelink = "?page=wbz404_redirected&remove=1&id=" . $row['id'];
        if ($tableOptions['filter'] == -1) {
            $trashlink .= "&trash=0";
            $trashtitle = wbz404_trans('Restore');
        } else {
            $trashlink .= "&trash=1";
            $trashtitle = wbz404_trans('Trash');
        }
        if (!($tableOptions['orderby'] == "url" && $tableOptions['order'] == "ASC")) {
            $trashlink .= "&orderby=" . $tableOptions['orderby'] . "&order=" . $tableOptions['order'];
            $deletelink .= "&orderby=" . $tableOptions['orderby'] . "&order=" . $tableOptions['order'];
        }
        if ($tableOptions['filter'] != 0) {
            $trashlink .= "&filter=" . $tableOptions['filter'];
            $deletelink .= "&filter=" . $tableOptions['filter'];
        }
        $trashaction = "wbz404_trashRedirect";
        $trashlink = wp_nonce_url($trashlink, $trashaction);
        if ($tableOptions['filter'] == -1) {
            $deleteaction = "wbz404_removeRedirect";
            $deletelink = wp_nonce_url($deletelink, $deleteaction);
        }
        $class = "";
        if ($y == 0) {
            $class = " class=\"alternate\"";
            $y++;
        } else {
            $y = 0;
        }
        echo "<tr id=\"post-" . $row['id'] . "\"" . $class . ">";
        echo "<td></td>";
        echo "<td>";
        echo "<strong><a href=\"" . $editlink . "\" title=\"" . wbz404_trans('Edit Redirect Details') . "\">" . $row['url'] . "</a></strong>";
        echo "<div class=\"row-actions\">";
        if ($tableOptions['filter'] != -1) {
            echo "<span class=\"edit\"><a href=\"" . $editlink . "\" title=\"" . wbz404_trans('Edit Redirect Details') . "\">" . wbz404_trans('Edit') . "</a></span>";
            echo " | ";
        }
        echo "<span class=\"trash\"><a href=\"" . $trashlink . "\" title=\"" . wbz404_trans('Trash Redirected URL') . "\">" . $trashtitle . "</a></span>";
        echo " | ";
        echo "<span class=\"view\"><a href=\"" . $logslink . "\" title=\"" . wbz404_trans('View Redirect Logs') . "\">" . wbz404_trans('View Logs') . "</a></span>";
        if ($tableOptions['filter'] == -1) {
            echo " | ";
            echo "<span class=\"delete\"><a href=\"" . $deletelink . "\" title=\"" . wbz404_trans('Delete Redirect Permanently') . "\">" . wbz404_trans('Delete Permanently') . "</a></span>";
        }
        echo "</div>";
        echo "</td>";
        echo "<td>" . $status . "</td>";
        echo "<td>" . $type . "</td>";
        echo "<td><a href=\"" . $link . "\" title=\"" . $title . "\" target=\"_blank\">" . $dest . "</a></td>";
        echo "<td>" . $row['code'] . "</td>";
        echo "<td>" . $hits . "</td>";
        echo "<td>" . date("Y/m/d h:i:s A", $row['timestamp']) . "</td>";
        echo "<td>" . $last . "</td>";
        echo "<td></td>";
        echo "</tr>";
    }
    if ($displayed == 0) {
        echo "<tr>";
        echo "<td></td>";
        echo "<td colspan=\"8\" style=\"text-align: center; font-weight: bold;\">" . wbz404_trans('No Records To Display') . "</td>";
        echo "<td></td>";
        echo "</tr>";
    }
    echo "</tbody>";
    echo "</table>";
    echo "<div class=\"tablenav\">";
    wbz404_drawPaginationLinks($sub, $tableOptions);
    echo "</div>";
    if ($tableOptions['filter'] != -1) {
        echo "<h3>" . wbz404_trans('Add Manual Redirect') . "</h3>";
        $url = "?page=wbz404_redirected";
        if (!($tableOptions['orderby'] == "url" && $tableOptions['order'] == "ASC")) {
            $url .= "&orderby=" . $tableOptions['orderby'] . "&order=" . $tableOptions['order'];
        }
        if ($tableOptions['filter'] != 0) {
            $url .= "&filter=" . $tableOptions['filter'];
        }
        $action = "wbz404addRedirect";
        $link = wp_nonce_url($url, $action);
        echo "<form method=\"POST\" action=\"" . $link . "\">";
        echo "<input type=\"hidden\" name=\"action\" value=\"addRedirect\">";
        if (isset($_POST['url'])) {
            $postedURL = $_POST['url'];
        } else {
            $postedURL = "";
        }
        echo "<strong><label for=\"url\">" . wbz404_trans('URL') . ":</label></strong> <input id=\"url\" style=\"width: 200px;\" type=\"text\" name=\"url\" value=\"" . $postedURL . "\"> (" . wbz404_trans('Required') . ")<br>";
        echo "<strong><label for=\"dest\">" . wbz404_trans('Redirect to') . ":</strong> <select id=\"dest\" name=\"dest\">";
        $selected = "";
        if (isset($_POST['dest']) && $_POST['dest'] == "EXTERNAL") {
            $selected = " selected";
        }
        echo "<option value=\"EXTERNAL\"" . $selected . ">" . wbz404_trans('External Page') . "</options>";
        $query = "select id from {$wpdb->posts} where post_status='publish' and post_type='post' order by post_date desc";
        $rows = $wpdb->get_results($query);
        foreach ($rows as $row) {
            $id = $row->id;
            $theTitle = get_the_title($id);
            $thisval = $id . "|POST";
            $selected = "";
            if (isset($_POST['dest']) && $_POST['dest'] == $thisval) {
                $selected = " selected";
            }
            echo "<option value=\"" . $thisval . "\"" . $selected . ">" . wbz404_trans('Post') . ": " . $theTitle . "</option>";
        }
        $rows = get_pages();
        foreach ($rows as $row) {
            $id = $row->ID;
            $theTitle = $row->post_title;
            $thisval = $id . "|POST";
            $parent = $row->post_parent;
            while ($parent != 0) {
                $query = "select id, post_parent from {$wpdb->posts} where post_status='publish' and post_type='page' and id = {$parent}";
                $prow = $wpdb->get_row($query, OBJECT);
                if (!($prow == NULL)) {
                    $theTitle = get_the_title($prow->id) . " &raquo; " . $theTitle;
                    $parent = $prow->post_parent;
                } else {
                    break;
                }
            }
            $selected = "";
            if (isset($_POST['dest']) && $_POST['dest'] == $thisval) {
                $selected = " selected";
            }
            echo "<option value=\"" . $thisval . "\"" . $selected . ">" . wbz404_trans('Page') . ": " . $theTitle . "</option>";
        }
        $cats = get_categories('hierarchical=0');
        foreach ($cats as $cat) {
            $id = $cat->term_id;
            $theTitle = $cat->name;
            $thisval = $id . "|CAT";
            $selected = "";
            if (isset($_POST['dest']) && $_POST['dest'] == $thisval) {
                $selected = " selected";
            }
            echo "<option value=\"" . $thisval . "\"" . $selected . ">" . wbz404_trans('Category') . ": " . $theTitle . "</option>";
        }
        $tags = get_tags('hierarchical=0');
        foreach ($tags as $tag) {
            $id = $tag->term_id;
            $theTitle = $tag->name;
            $thisval = $id . "|TAG";
            $selected = "";
            if (isset($_POST['dest']) && $_POST['dest'] == $thisval) {
                $selected = " selected";
            }
            echo "<option value=\"" . $thisval . "\"" . $selected . ">" . wbz404_trans('Tag') . ": " . $theTitle . "</option>";
        }
        echo "</select><br>";
        if (isset($_POST['external'])) {
            $postedExternal = $_POST['external'];
        } else {
            $postedExternal = "";
        }
        echo "<strong><label for=\"external\">" . wbz404_trans('External URL') . ":</label></strong> <input id=\"external\" style=\"width: 200px;\" type=\"text\" name=\"external\" value=\"" . $postedExternal . "\"> (" . wbz404_trans('Required if Redirect to is set to External Page') . ")<br>";
        echo "<strong><label for=\"code\">" . wbz404_trans('Redirect Type') . ":</label></strong> <select id=\"code\" name=\"code\">";
        if (!isset($_POST['code']) || $_POST['code'] == "") {
            $codeselected = $options['default_redirect'];
        } else {
            $codeselected = $_POST['code'];
        }
        $codes = array(301, 302);
        foreach ($codes as $code) {
            $selected = "";
            if ($code == $codeselected) {
                $selected = " selected";
            }
            if ($code == 301) {
                $title = wbz404_trans('301 Permanent Redirect');
            } else {
                $title = wbz404_trans('302 Temporary Redirect');
            }
            echo "<option value=\"" . $code . "\"" . $selected . ">" . $title . "</option>";
        }
        echo "</select><br>";
        echo "<input type=\"submit\" value=\"" . wbz404_trans('Add Redirect') . "\" class=\"button-secondary\">";
        echo "</form>";
    }
}
Example #2
0
function wbz404_redirectCanonical($redirect, $request)
{
    if (is_single() || is_page()) {
        if (!is_feed() && !is_trackback() && !is_preview()) {
            $options = wbz404_getOptions();
            $urlRequest = $_SERVER['REQUEST_URI'];
            $urlParts = parse_url($urlRequest);
            $requestedURL = $urlParts['path'];
            $requestedURL .= wbz404_SortQuery($urlParts);
            #//Get URL data if it's already in our database
            $data = wbz404_loadRedirectData($requestedURL);
            if ($data['id'] != '0') {
                wbz404_ProcessRedirect($data);
            } else {
                if ($options['auto_redirects'] == '1' && $options['force_permalinks'] == '1') {
                    $theID = get_the_ID();
                    $permalink = wbz404_permalinkInfo($theID . "|POST", 0);
                    $urlParts = parse_url($permalink['link']);
                    $perma_link = $urlParts['path'];
                    $paged = get_query_var('page') ? get_query_var('page') : FALSE;
                    if (!$paged === FALSE) {
                        if ($urlParts[query] == "") {
                            if (substr($perma_link, -1) == "/") {
                                $perma_link .= $paged . "/";
                            } else {
                                $perma_link .= "/" . $paged;
                            }
                        } else {
                            $urlParts['query'] .= "&page=" . $paged;
                        }
                    }
                    $perma_link .= wbz404_SortQuery($urlParts);
                    if ($requestedURL != $perma_link) {
                        $redirect_id = wbz404_setupRedirect($requestedURL, WBZ404_AUTO, WBZ404_POST, $theID, $options['default_redirect'], 0);
                        wbz404_logRedirectHit($redirect_id, $perma_link);
                        wp_redirect($perma_link, $options['default_redirect']);
                        exit;
                    }
                }
            }
        }
    }
    if (is_404()) {
        return false;
    }
    return $redirect;
}
Example #3
0
function wbz404_ProcessRedirect($redirect)
{
    //A redirect record has already been found.
    if (($redirect['status'] == WBZ404_MANUAL || $redirect['status'] == WBZ404_AUTO) && $redirect['disabled'] == 0) {
        //It's a redirect, not a captured or ignored URL
        if ($redirect['type'] == WBZ404_EXTERNAL) {
            //It's a external url setup by the user
            wbz404_logRedirectHit($redirect['id'], $redirect['final_dest']);
            wp_redirect($redirect['final_dest'], $redirect['code']);
            exit;
        } else {
            $key = "";
            if ($redirect['type'] == WBZ404_POST) {
                $key = $redirect['final_dest'] . "|POST";
            } else {
                if ($redirect['type'] == WBZ404_CAT) {
                    $key = $redirect['final_dest'] . "|CAT";
                } else {
                    if ($redirect['type'] == WBZ404_TAG) {
                        $key = $redirect['final_dest'] . "|TAG";
                    }
                }
            }
            if ($key != "") {
                $permalink = wbz404_permalinkInfo($key, 0);
                wbz404_logRedirectHit($redirect['id'], $permalink['link']);
                wp_redirect($permalink['link'], $redirect['code']);
                exit;
            }
        }
    } else {
        wbz404_logRedirectHit($redirect['id'], '404');
    }
}
Example #4
0
function wbz404_process404()
{
    $options = wbz404_getOptions();
    $urlRequest = $_SERVER['REQUEST_URI'];
    $urlParts = parse_url($urlRequest);
    $requestedURL = $urlParts['path'];
    $queryArgs = wbz404_SortQuery($urlParts);
    /* The only case where we want the query string to be considered part of the URL we are processing
     * is when it contains 'page_id'. In all other cases, we want to strip the query args -- they are typically
     * the utm labels in those other situations, which we want to ignore.
     */
    if (strpos($queryArgs, 'page_id') !== false) {
        $requestedURL .= $queryArgs;
    }
    //Get URL data if it's already in our database
    $redirect = wbz404_loadRedirectData($requestedURL);
    if (is_404() && $requestedURL != "") {
        if ($redirect['id'] != '0') {
            //A redirect record exists.
            wbz404_ProcessRedirect($redirect);
        } else {
            //No redirect record.
            $found = 0;
            if ($options['auto_redirects'] == '1') {
                //Site owner wants automatic redirects
                $permalinks = wbz404_rankPermalinks($requestedURL, $options['auto_cats'], $options['auto_tags']);
                $minScore = $options['auto_score'];
                foreach ($permalinks as $k => $v) {
                    $permalink = wbz404_permalinkInfo($k, $v);
                    if ($permalink['score'] >= $minScore) {
                        $found = 1;
                        break;
                    } else {
                        //Score not high enough
                        break;
                    }
                }
                if ($found == 1) {
                    //We found a permalink that will work!
                    $type = 0;
                    if ($permalink['type'] == "POST") {
                        $type = WBZ404_POST;
                    } else {
                        if ($permalink['type'] == "CAT") {
                            $type = WBZ404_CAT;
                        } else {
                            if ($permalink['type'] == "TAG") {
                                $type = WBZ404_TAG;
                            }
                        }
                    }
                    if ($type != 0) {
                        $redirect_id = wbz404_setupRedirect($requestedURL, WBZ404_AUTO, $type, $permalink['id'], $options['default_redirect'], 0);
                    }
                }
            }
            if ($found == 1) {
                //Perform actual redirect
                wbz404_logRedirectHit($redirect_id, $permalink['link']);
                wp_redirect($permalink['link'], $options['default_redirect']);
                exit;
            } else {
                /* Workaround for the issue where bbPress user profile pages would be
                 * marked as 404 by the function handle_404() in the Wordpress core.
                 */
                $forums_user_pattern = '/\\/forums\\/users\\//';
                if (preg_match($forums_user_pattern, $requestedURL) && function_exists('bbpress')) {
                    $bbp = bbpress();
                    if (!empty($bbp->displayed_user)) {
                        return;
                    }
                }
                /* Redirect requests to non-existent category pages to the first page URL.
                 */
                $paged_pattern = '/\\/page\\/\\d\\d?\\/?$/';
                if (preg_match($paged_pattern, $requestedURL)) {
                    $target_url = preg_replace($paged_pattern, '/', $requestedURL);
                    error_log("Redirecting {$requestedURL} to {$target_url} in " . __FILE__);
                    wp_redirect($target_url, 302);
                }
                //Check for incoming 404 settings
                if ($options['capture_404'] == '1') {
                    $redirect_id = wbz404_setupRedirect($requestedURL, WBZ404_CAPTURED, 0, 0, $options['default_redirect'], 0);
                    wbz404_logRedirectHit($redirect_id, '404');
                }
            }
        }
    } else {
        if (is_single() || is_page()) {
            if (!is_feed() && !is_trackback() && !is_preview()) {
                $theID = get_the_ID();
                $permalink = wbz404_permalinkInfo($theID . "|POST", 0);
                $urlParts = parse_url($permalink['link']);
                $perma_link = $urlParts['path'];
                $paged = get_query_var('page') ? get_query_var('page') : false;
                if (!$paged === false) {
                    if ($urlParts[query] == "") {
                        if (substr($perma_link, -1) == "/") {
                            $perma_link .= $paged . "/";
                        } else {
                            $perma_link .= "/" . $paged;
                        }
                    } else {
                        $urlParts['query'] .= "&page=" . $paged;
                    }
                }
                $perma_link .= wbz404_SortQuery($urlParts);
                //Check for forced permalinks
                if ($options['force_permalinks'] == '1' && $options['auto_redirects'] == '1') {
                    if ($requestedURL != $perma_link) {
                        if ($redirect['id'] != '0') {
                            wbz404_ProcessRedirect($redirect);
                        } else {
                            $redirect_id = wbz404_setupRedirect($requestedURL, WBZ404_AUTO, WBZ404_POST, $permalink['id'], $options['default_redirect'], 0);
                            wbz404_logRedirectHit($redirect_id, $permalink['link']);
                            wp_redirect($permalink['link'], $options['default_redirect']);
                            exit;
                        }
                    }
                }
                if ($requestedURL == $perma_link) {
                    //Not a 404 Link. Check for matches
                    if ($options['remove_matches'] == '1') {
                        if ($redirect['id'] != '0') {
                            wbz404_cleanRedirect($redirect['id']);
                        }
                    }
                }
            }
        }
    }
}