utils.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. /**
  3. * Build path data for current request.
  4. *
  5. * @return string|bool
  6. */
  7. function _s_backbone_get_request_path() {
  8. global $wp_rewrite;
  9. if ( $wp_rewrite->using_permalinks() ) {
  10. global $wp;
  11. // If called too early, bail
  12. if ( ! isset( $wp->request ) ) {
  13. return false;
  14. }
  15. // Determine path for paginated version of current request
  16. if ( false != preg_match( '#' . $wp_rewrite->pagination_base . '/\d+/?$#i', $wp->request ) ) {
  17. $path = preg_replace( '#' . $wp_rewrite->pagination_base . '/\d+$#i', $wp_rewrite->pagination_base . '/%d', $wp->request );
  18. } else {
  19. $path = $wp->request . '/' . $wp_rewrite->pagination_base . '/%d';
  20. }
  21. // Slashes everywhere we need them
  22. if ( 0 !== strpos( $path, '/' ) )
  23. $path = '/' . $path;
  24. $path = user_trailingslashit( $path );
  25. } else {
  26. // Clean up raw $_REQUEST input
  27. $path = array_map( 'sanitize_text_field', $_REQUEST );
  28. $path = array_filter( $path );
  29. $path['paged'] = '%d';
  30. $path = add_query_arg( $path, '/' );
  31. }
  32. return empty( $path ) ? false : $path;
  33. }
  34. /**
  35. * Return query string for current request, prefixed with '?'.
  36. *
  37. * @return string
  38. */
  39. function _s_backbone_get_request_parameters() {
  40. $uri = $_SERVER[ 'REQUEST_URI' ];
  41. $uri = preg_replace( '/^[^?]*(\?.*$)/', '$1', $uri, 1, $count );
  42. if ( $count != 1 ) {
  43. return '';
  44. }
  45. return $uri;
  46. }