Exemplo n.º 1
0
/**
 * wpsc_pagination generates and returns the urls for pagination
 * @access public
 *
 * @since 3.8
 * @param $totalpages (INT) Number of pages,
 * @param $per_page (INT) Number of products per page
 * @param $current_page (INT) Current Product page number
 * @param $page_link (STRING) URL of Page
 *
 * @return
 */
function wpsc_pagination($totalpages = '', $per_page = '', $current_page = '', $page_link = '')
{
    global $wp_query, $wpsc_query, $wp_the_query;
    $num_paged_links = 4;
    //amount of links to show on either side of current page
    $additional_links = '';
    //additional links, items per page and products order
    if (get_option('permalink_structure') != '') {
        $additional_links_separator = '?';
    } else {
        $additional_links_separator = '&';
    }
    if (!empty($_GET['items_per_page'])) {
        $additional_links = $additional_links_separator . 'items_per_page=' . $_GET['items_per_page'];
        $additional_links_separator = '&';
    }
    if (!empty($_GET['product_order'])) {
        $additional_links .= $additional_links_separator . 'product_order=' . $_GET['product_order'];
    }
    if (!empty($_GET['range'])) {
        $additional_links .= $additional_links_separator . 'range=' . $_GET['range'];
    }
    $additional_links = apply_filters('wpsc_pagination_additional_links', $additional_links);
    //end of additional links
    if (empty($totalpages)) {
        $totalpages = $wp_query->max_num_pages;
    }
    if (empty($per_page)) {
        $per_page = (int) get_option('wpsc_products_per_page');
    }
    $current_page = absint(get_query_var('paged'));
    if ($current_page == 0) {
        $current_page = 1;
    }
    if (empty($page_link)) {
        $page_link = wpsc_a_page_url();
    }
    //if there is no pagination
    if (!get_option('permalink_structure')) {
        $category = '?';
        if (!empty($wp_query->query_vars['wpsc_product_category'])) {
            $category = '?wpsc_product_category=' . $wp_query->query_vars['wpsc_product_category'];
        }
        if (isset($wp_query->query_vars['wpsc_product_category']) && is_string($wp_query->query_vars['wpsc_product_category'])) {
            $page_link = get_option('blogurl') . $category . '&paged';
        } else {
            $page_link = get_option('product_list_url') . $category . '&paged';
        }
        $separator = '=';
    } else {
        global $wpsc_query;
        $separator = 'page/';
        if (isset($wp_query->query_vars['wpsc_product_category'])) {
            $category_id = get_term_by('slug', $wp_query->query_vars['wpsc_product_category'], 'wpsc_product_category');
            $page_link = trailingslashit(get_term_link($category_id, 'wpsc_product_category'));
            // in case we're displaying a category using shortcode, need to use the page's URL instead of the taxonomy URL
            if ($wp_the_query->is_page()) {
                $page = $wp_the_query->get_queried_object();
                if (preg_match('/\\[wpsc\\_products[^\\]]*category_id=/', $page->post_content)) {
                    $page_link = trailingslashit(get_permalink($page->ID));
                    $separator = '';
                }
            }
        } elseif (is_tax('product_tag')) {
            $tag = get_queried_object();
            $page_link = trailingslashit(get_term_link((int) $tag->term_id, 'product_tag'));
        } elseif ($wpsc_query->is_front_page()) {
            $page_link = trailingslashit(home_url());
        } else {
            $page_link = trailingslashit(get_option('product_list_url'));
            $separator = '';
        }
    }
    // If there's only one page, return now and don't bother
    if ($totalpages == 1) {
        return;
    }
    // Pagination Prefix
    $output = __('Pages: ', 'wp-e-commerce');
    if (get_option('permalink_structure')) {
        // Should we show the FIRST PAGE link?
        if ($current_page > 1) {
            $output .= "<a href=\"" . esc_url($page_link . $additional_links) . "\" title=\"" . __('First Page', 'wp-e-commerce') . "\">" . __('&laquo; First', 'wp-e-commerce') . "</a>";
        }
        // Should we show the PREVIOUS PAGE link?
        if ($current_page > 1) {
            $previous_page = $current_page - 1;
            if ($previous_page == 1) {
                $output .= " <a href=\"" . esc_url($page_link . $additional_links) . "\" title=\"" . __('Previous Page', 'wp-e-commerce') . "\">" . __('&lt; Previous', 'wp-e-commerce') . "</a>";
            } else {
                $output .= " <a href=\"" . esc_url($page_link . $separator . $previous_page . $additional_links) . "\" title=\"" . __('Previous Page', 'wp-e-commerce') . "\">" . __('&lt; Previous', 'wp-e-commerce') . "</a>";
            }
        }
        $i = $current_page - $num_paged_links;
        $count = 1;
        if ($i <= 0) {
            $i = 1;
        }
        while ($i < $current_page) {
            if ($count <= $num_paged_links) {
                if ($i == 1) {
                    $output .= " <a href=\"" . esc_url($page_link . $additional_links) . "\" title=\"" . sprintf(__('Page %s', 'wp-e-commerce'), $i) . " \">" . $i . "</a>";
                } else {
                    $output .= " <a href=\"" . esc_url($page_link . $separator . $i . '/' . $additional_links) . "\" title=\"" . sprintf(__('Page %s', 'wp-e-commerce'), $i) . " \">" . $i . "</a>";
                }
            }
            $i++;
            $count++;
        }
        // Current Page Number
        if ($current_page > 0) {
            $output .= "<span class='current'>{$current_page}</span>";
        }
        //Links after Current Page
        $i = $current_page + $num_paged_links;
        $count = 1;
        if ($current_page < $totalpages) {
            while ($i > $current_page) {
                if ($count < $num_paged_links + 1 && $count + $current_page <= $totalpages) {
                    $output .= " <a href=\"" . esc_url($page_link . $separator . ($count + $current_page) . '/' . $additional_links) . "\" title=\"" . sprintf(__('Page %s', 'wp-e-commerce'), $count + $current_page) . "\">" . ($count + $current_page) . "</a>";
                    $i++;
                } else {
                    break;
                }
                $count++;
            }
        }
        if ($current_page < $totalpages) {
            $next_page = $current_page + 1;
            $output .= "<a href=\"" . esc_url($page_link . $separator . $next_page . '/' . $additional_links) . "\" title=\"" . __('Next Page', 'wp-e-commerce') . "\">" . __('Next &gt;', 'wp-e-commerce') . "</a>";
        }
        // Should we show the LAST PAGE link?
        if ($current_page < $totalpages) {
            $output .= "<a href=\"" . esc_url($page_link . $separator . $totalpages . '/' . $additional_links) . "\" title=\"" . __('Last Page', 'wp-e-commerce') . "\">" . __('Last &raquo;', 'wp-e-commerce') . "</a>";
        }
    } else {
        // Should we show the FIRST PAGE link?
        if ($current_page > 1) {
            $output .= "<a href=\"" . esc_url(remove_query_arg('paged')) . "\" title=\"" . __('First Page', 'wp-e-commerce') . "\">" . __('&laquo; First', 'wp-e-commerce') . "</a>";
        }
        // Should we show the PREVIOUS PAGE link?
        if ($current_page > 1) {
            $previous_page = $current_page - 1;
            if ($previous_page == 1) {
                $output .= " <a href=\"" . esc_url(remove_query_arg('paged')) . $additional_links . "\" title=\"" . __('Previous Page', 'wp-e-commerce') . "\">" . __('&lt; Previous', 'wp-e-commerce') . "</a>";
            } else {
                $output .= " <a href=\"" . esc_url(add_query_arg('paged', $current_page - 1)) . $additional_links . "\" title=\"" . __('Previous Page', 'wp-e-commerce') . "\">" . __('&lt; Previous', 'wp-e-commerce') . "</a>";
            }
        }
        $i = $current_page - $num_paged_links;
        $count = 1;
        if ($i <= 0) {
            $i = 1;
        }
        while ($i < $current_page) {
            if ($count <= $num_paged_links) {
                if ($i == 1) {
                    $output .= " <a href=\"" . esc_url(remove_query_arg('paged')) . "\" title=\"" . sprintf(__('Page %s', 'wp-e-commerce'), $i) . " \">" . $i . "</a>";
                } else {
                    $output .= " <a href=\"" . esc_url(add_query_arg('paged', $i)) . "\" title=\"" . sprintf(__('Page %s', 'wp-e-commerce'), $i) . " \">" . $i . "</a>";
                }
            }
            $i++;
            $count++;
        }
        // Current Page Number
        if ($current_page > 0) {
            $output .= "<span class='current'>{$current_page}</span>";
        }
        //Links after Current Page
        $i = $current_page + $num_paged_links;
        $count = 1;
        if ($current_page < $totalpages) {
            while ($i > $current_page) {
                if ($count < $num_paged_links && $count + $current_page <= $totalpages) {
                    $output .= " <a href=\"" . esc_url(add_query_arg('paged', $count + $current_page)) . "\" title=\"" . sprintf(__('Page %s', 'wp-e-commerce'), $count + $current_page) . "\">" . ($count + $current_page) . "</a>";
                    $i++;
                } else {
                    break;
                }
                $count++;
            }
        }
        if ($current_page < $totalpages) {
            $next_page = $current_page + 1;
            $output .= "<a href=\"" . esc_url(add_query_arg('paged', $next_page)) . "\" title=\"" . __('Next Page', 'wp-e-commerce') . "\">" . __('Next &gt;', 'wp-e-commerce') . "</a>";
        }
        // Should we show the LAST PAGE link?
        if ($current_page < $totalpages) {
            $output .= "<a href=\"" . esc_url(add_query_arg('paged', $totalpages)) . "\" title=\"" . __('Last Page', 'wp-e-commerce') . "\">" . __('Last &raquo;', 'wp-e-commerce') . "</a>";
        }
    }
    // Return the output.
    echo apply_filters('wpsc_pagination', $output, $totalpages, $per_page, $current_page, $page_link);
}
Exemplo n.º 2
0
/**
 * wpsc_pagination generates and returns the urls for pagination
 * @access public
 *
 * @since 3.8
 * @param $totalpages (INT) Number of pages, 
 * @param $per_page (INT) Number of products per page
 * @param $current_page (INT) Current Product page number
 * @param $page_link (STRING) URL of Page
 *
 * @return 
 */
function wpsc_pagination($totalpages = '', $per_page = '', $current_page = '', $page_link = '')
{
    global $wp_query;
    $num_paged_links = 4;
    //amount of links to show on either side of current page
    $additional_links = '';
    //additional links, items per page and products order
    if (get_option('permalink_structure') != '') {
        $additional_links_separator = '?';
    } else {
        $additional_links_separator = '&';
    }
    if (!empty($_GET['items_per_page'])) {
        $additional_links = $additional_links_separator . 'items_per_page=' . $_GET['items_per_page'];
        $additional_links_separator = '&';
    }
    if (!empty($_GET['product_order'])) {
        $additional_links .= $additional_links_separator . 'product_order=' . $_GET['product_order'];
    }
    $additional_links = apply_filters('wpsc_pagination_additional_links', $additional_links);
    //end of additional links
    if (empty($totalpages)) {
        $totalpages = $wp_query->max_num_pages;
    }
    if (empty($per_page)) {
        $per_page = (int) get_option('wpsc_products_per_page');
    }
    $current_page = absint(get_query_var('paged'));
    if ($current_page == 0) {
        $current_page = 1;
    }
    if (empty($page_link)) {
        $page_link = wpsc_a_page_url();
    }
    //if there is no pagination
    if (!get_option('permalink_structure')) {
        $category = '?';
        if (isset($wp_query->query_vars['wpsc_product_category'])) {
            $category = '?wpsc_product_category=' . $wp_query->query_vars['wpsc_product_category'];
        }
        if (isset($wp_query->query_vars['wpsc_product_category']) && is_string($wp_query->query_vars['wpsc_product_category'])) {
            $page_link = get_option('blogurl') . $category . '&amp;paged';
        } else {
            $page_link = get_option('product_list_url') . $category . '&amp;paged';
        }
        $separator = '=';
    } else {
        // This will need changing when we get product categories sorted
        if (isset($wp_query->query_vars['wpsc_product_category'])) {
            $page_link = trailingslashit(get_option('product_list_url')) . $wp_query->query_vars['wpsc_product_category'] . '/';
        } else {
            $page_link = trailingslashit(get_option('product_list_url'));
        }
        $separator = 'page/';
    }
    // If there's only one page, return now and don't bother
    if ($totalpages == 1) {
        return;
    }
    // Pagination Prefix
    $output = __('Pages: ', 'wpsc');
    if (get_option('permalink_structure')) {
        // Should we show the FIRST PAGE link?
        if ($current_page > 1) {
            $output .= "<a href=\"" . esc_url($page_link . $additional_links) . "\" title=\"" . __('First Page', 'wpsc') . "\">" . __('&laquo; First', 'wpsc') . "</a>";
        }
        // Should we show the PREVIOUS PAGE link?
        if ($current_page > 1) {
            $previous_page = $current_page - 1;
            if ($previous_page == 1) {
                $output .= " <a href=\"" . esc_url($page_link . $additional_links) . "\" title=\"" . __('Previous Page', 'wpsc') . "\">" . __('&lt; Previous', 'wpsc') . "</a>";
            } else {
                $output .= " <a href=\"" . esc_url($page_link . $separator . $previous_page . $additional_links) . "\" title=\"" . __('Previous Page', 'wpsc') . "\">" . __('&lt; Previous', 'wpsc') . "</a>";
            }
        }
        $i = $current_page - $num_paged_links;
        $count = 1;
        if ($i <= 0) {
            $i = 1;
        }
        while ($i < $current_page) {
            if ($count <= $num_paged_links) {
                if ($count == 1) {
                    $output .= " <a href=\"" . esc_url($page_link . $additional_links) . "\" title=\"" . sprintf(__('Page %s', 'wpsc'), $i) . " \">" . $i . "</a>";
                } else {
                    $output .= " <a href=\"" . esc_url($page_link . $separator . $i . $additional_links) . "\" title=\"" . sprintf(__('Page %s', 'wpsc'), $i) . " \">" . $i . "</a>";
                }
            }
            $i++;
            $count++;
        }
        // Current Page Number
        if ($current_page > 0) {
            $output .= "<span class='current'>{$current_page}</span>";
        }
        //Links after Current Page
        $i = $current_page + $num_paged_links;
        $count = 1;
        if ($current_page < $totalpages) {
            while ($i > $current_page) {
                if ($count < $num_paged_links && $count + $current_page <= $totalpages) {
                    $output .= " <a href=\"" . esc_url($page_link . $separator . ($count + $current_page) . $additional_links) . "\" title=\"" . sprintf(__('Page %s', 'wpsc'), $count + $current_page) . "\">" . ($count + $current_page) . "</a>";
                    $i++;
                } else {
                    break;
                }
                $count++;
            }
        }
        if ($current_page < $totalpages) {
            $next_page = $current_page + 1;
            $output .= "<a href=\"" . esc_url($page_link . $separator . $next_page . $additional_links) . "\" title=\"" . __('Next Page', 'wpsc') . "\">" . __('Next &gt;', 'wpsc') . "</a>";
        }
        // Should we show the LAST PAGE link?
        if ($current_page < $totalpages) {
            $output .= "<a href=\"" . esc_url($page_link . $separator . $totalpages . $additional_links) . "\" title=\"" . __('Last Page', 'wpsc') . "\">" . __('Last &raquo;', 'wpsc') . "</a>";
        }
    } else {
        // Should we show the FIRST PAGE link?
        if ($current_page > 1) {
            $output .= "<a href=\"" . remove_query_arg('paged') . "\" title=\"" . __('First Page', 'wpsc') . "\">" . __('&laquo; First', 'wpsc') . "</a>";
        }
        // Should we show the PREVIOUS PAGE link?
        if ($current_page > 1) {
            $previous_page = $current_page - 1;
            if ($previous_page == 1) {
                $output .= " <a href=\"" . remove_query_arg('paged') . $additional_links . "\" title=\"" . __('Previous Page', 'wpsc') . "\">" . __('&lt; Previous', 'wpsc') . "</a>";
            } else {
                $output .= " <a href=\"" . add_query_arg('paged', $current_page - 1) . $additional_links . "\" title=\"" . __('Previous Page', 'wpsc') . "\">" . __('&lt; Previous', 'wpsc') . "</a>";
            }
        }
        $i = $current_page - $num_paged_links;
        $count = 1;
        if ($i <= 0) {
            $i = 1;
        }
        while ($i < $current_page) {
            if ($count <= $num_paged_links) {
                if ($i == 1) {
                    $output .= " <a href=\"" . remove_query_arg('paged') . "\" title=\"" . sprintf(__('Page %s', 'wpsc'), $i) . " \">" . $i . "</a>";
                } else {
                    $output .= " <a href=\"" . add_query_arg('paged', $i) . "\" title=\"" . sprintf(__('Page %s', 'wpsc'), $i) . " \">" . $i . "</a>";
                }
            }
            $i++;
            $count++;
        }
        // Current Page Number
        if ($current_page > 0) {
            $output .= "<span class='current'>{$current_page}</span>";
        }
        //Links after Current Page
        $i = $current_page + $num_paged_links;
        $count = 1;
        if ($current_page < $totalpages) {
            while ($i > $current_page) {
                if ($count < $num_paged_links && $count + $current_page <= $totalpages) {
                    $output .= " <a href=\"" . add_query_arg('paged', $count + $current_page) . "\" title=\"" . sprintf(__('Page %s', 'wpsc'), $count + $current_page) . "\">" . ($count + $current_page) . "</a>";
                    $i++;
                } else {
                    break;
                }
                $count++;
            }
        }
        if ($current_page < $totalpages) {
            $next_page = $current_page + 1;
            $output .= "<a href=\"" . add_query_arg('paged', $next_page) . "\" title=\"" . __('Next Page', 'wpsc') . "\">" . __('Next &gt;', 'wpsc') . "</a>";
        }
        // Should we show the LAST PAGE link?
        if ($current_page < $totalpages) {
            $output .= "<a href=\"" . add_query_arg('paged', $totalpages) . "\" title=\"" . __('Last Page', 'wpsc') . "\">" . __('Last &raquo;', 'wpsc') . "</a>";
        }
    }
    // Return the output.
    echo $output;
}