/wp-content/plugins/nextgen-gallery/products/photocrati_nextgen/modules/nextgen_pagination/package.module.nextgen_pagination.php

https://github.com/livinglab/openlab · PHP · 118 lines · 102 code · 0 blank · 16 comment · 25 complexity · 500c124fc22d18137f00b470804ca2c3 MD5 · raw file

  1. <?php
  2. /**
  3. * Contains function(s) to generate a basic pagination widget
  4. */
  5. class Mixin_NextGen_Basic_Pagination extends Mixin
  6. {
  7. /**
  8. * Returns a formatted HTML string of a pagination widget
  9. *
  10. * @param mixed $selected_page
  11. * @param int $number_of_entities
  12. * @param int $entities_per_page
  13. * @param string|null $current_url (optional)
  14. * @return array Of data holding prev & next url locations and a formatted HTML string
  15. */
  16. public function create_pagination($selected_page, $number_of_entities, $entities_per_page = 0, $current_url = NULL)
  17. {
  18. $prev_symbol = apply_filters('ngg_prev_symbol', '&#9668;');
  19. $next_symbol = apply_filters('ngg_next_symbol', '&#9658;');
  20. if (empty($current_url)) {
  21. $current_url = $this->object->get_routed_url(TRUE);
  22. if (is_archive()) {
  23. $id = get_the_ID();
  24. if ($id == null) {
  25. global $post;
  26. $id = $post ? $post->ID : null;
  27. }
  28. if ($id != null && in_the_loop()) {
  29. $current_url = get_permalink($id);
  30. }
  31. }
  32. }
  33. // Early exit
  34. $return = array('prev' => '', 'next' => '', 'output' => "<div class='ngg-clear'></div>");
  35. if ($entities_per_page <= 0 || $number_of_entities <= 0) {
  36. return $return;
  37. }
  38. // Construct array of page urls
  39. $ending_ellipsis = $starting_ellipsis = FALSE;
  40. $number_of_pages = ceil($number_of_entities / $entities_per_page);
  41. $pages = [];
  42. for ($i = 1; $i <= $number_of_pages; $i++) {
  43. if ($selected_page === $i) {
  44. $pages['current'] = "<span class='current'>{$i}</span>";
  45. } else {
  46. $link = esc_attr($this->object->set_param_for($current_url, 'nggpage', $i));
  47. $pages[$i] = "<a class='page-numbers' data-pageid='{$i}' href='{$link}'>{$i}</a>";
  48. }
  49. }
  50. $after = $this->array_slice_from('current', $pages);
  51. if (count($after) > 3) {
  52. $after = array_merge($this->array_take_from_start(2, $after), ["<span class='ellipsis'>...</span>"], $this->array_take_from_end(1, $after));
  53. }
  54. $before = $this->array_slice_to('current', $pages);
  55. if (count($before) > 3) {
  56. $before = array_merge($this->array_take_from_start(1, $before), ["<span class='ellipsis'>...</span>"], $this->array_take_from_end(2, $before));
  57. array_pop($before);
  58. }
  59. $pages = array_merge($before, $after);
  60. if ($pages && count($pages) > 1) {
  61. // Next page
  62. if ($selected_page + 1 <= $number_of_pages) {
  63. $next_page = $selected_page + 1;
  64. $link = $return['next'] = $this->object->set_param_for($current_url, 'nggpage', $next_page);
  65. $pages[] = "<a class='prev' href='{$link}' data-pageid={$next_page}>{$next_symbol}</a>";
  66. }
  67. // Prev page
  68. if ($selected_page - 1 > 0) {
  69. $prev_page = $selected_page - 1;
  70. $link = $return['next'] = $this->object->set_param_for($current_url, 'nggpage', $prev_page);
  71. array_unshift($pages, "<a class='next' href='{$link}' data-pageid={$prev_page}>{$prev_symbol}</a>");
  72. }
  73. $return['output'] = "<div class='ngg-navigation'>" . implode("\n", $pages) . "</div>";
  74. }
  75. return $return;
  76. }
  77. function array_slice_from($find_key, $arr)
  78. {
  79. $retval = [];
  80. reset($arr);
  81. foreach ($arr as $key => $value) {
  82. if ($key == $find_key || $retval) {
  83. $retval[$key] = $value;
  84. }
  85. }
  86. reset($arr);
  87. return $retval;
  88. }
  89. function array_slice_to($find_key, $arr)
  90. {
  91. $retval = [];
  92. reset($arr);
  93. foreach ($arr as $key => $value) {
  94. $retval[$key] = $value;
  95. if ($key == $find_key) {
  96. break;
  97. }
  98. }
  99. reset($arr);
  100. return $retval;
  101. }
  102. function array_take_from_start($number, $arr)
  103. {
  104. $retval = [];
  105. foreach ($arr as $key => $value) {
  106. if (count($retval) < $number) {
  107. $retval[$key] = $value;
  108. } else {
  109. break;
  110. }
  111. }
  112. return $retval;
  113. }
  114. function array_take_from_end($number, $arr)
  115. {
  116. return array_reverse($this->array_take_from_start($number, array_reverse($arr)));
  117. }
  118. }