[wp-trac] [WordPress Trac] #48224: Refactor paginate_links and add 4 new functions (paginate_array and format_paginate_link)

WordPress Trac noreply at wordpress.org
Sat Oct 5 16:04:19 UTC 2019


#48224: Refactor paginate_links and add 4 new functions (paginate_array and
format_paginate_link)
-------------------------+-----------------------------
 Reporter:  ngdangtu     |      Owner:  (none)
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  Customize    |    Version:  trunk
 Severity:  normal       |   Keywords:  has-patch
  Focuses:  ui           |
-------------------------+-----------------------------
 The current paginate_links has limited the creativity of making
 pagination. It returns a fixed template rather than a plain data. Because
 of that, frontend dev will have a hard time in query necessary data to
 make a custom pagination.

 My suggestion is create 4 new functions.

 The first function, called paginate_array, only return plain data. Almost
 90% its code is from paginate_links. Its job will only return data array
 in this format: (see pseudocode below)


 {{{
 [
         ["prev"]        => [
                 ["url"]         => "http://localhost/wp/page/20/",
                 ["class"]       => "prev page-numbers"
         ],
         ["pages"]       => [
                 [
                         ["type"]        => "page",
                         ["url"]         => "http://localhost/wp/",
                         ["label"]       => 1,
                         ["class"]       => "page-numbers"
                 ],
                 [
                         ["type"]        => "etc",
                         ["label"]       => "…",
                         ["class"]       => "page-numbers dots"
                 ],
                 [
                         ["type"]        => "page",
                         ["url"]         => "http://localhost/wp/page/19/",
                         ["label"]       => 19,
                         ["class"]       => "page-numbers"
                 ],
                 [
                         ["type"]        => "page",
                         ["url"]         => "http://localhost/wp/page/20/",
                         ["label"]       => 20,
                         ["class"]       => "page-numbers"
                 ],
                 [
                         ["type"]        => "page",
                         ["url"]         => NULL,
                         ["label"]       => 21,
                         ["class"]       => "page-numbers"
                 ],
                 [
                         ["type"]        => "page",
                         ["url"]         => "http://localhost/wp/page/22/",
                         ["label"]       => 22,
                         ["class"]       => "page-numbers"
                 ]
         ],
         ["next"]        => [
                 ["url"]         => "http://localhost/wp/page/22/",
                 ["class"]       => "next page-numbers"
         ]
 ]
 }}}

 From here dev can pass data to a loop and put it in any kind of layout
 they want.

 The second function is format_paginate_link which is a supported function
 for the first one. Beside that job, it can be use for quick format
 paginate link from place with strict context and only a link needed. For
 example, if only a link of total page is needed, there is no reason to
 call paginate_array. Dev can just get total pages from WP_Query class.

 The third and fourth functions are default_paginate_base and
 default_paginate_format. Those are extracted from the paginate_links. The
 two functions were created due to the fact that both paginate_array and
 format_paginate_link are need those code, and it cannot be place in one
 side. I hope somebody could find it useful someday.

 The original/mother function now becomes a template warp. It gets data
 from paginate_array and add it to the default layout. It is kept for
 backward reason.


 The original default args also upgrade **base**, **total**, **current**
 and **format** as well. It default code won't be pre-executed, it will
 check the inputs first and only execute if inputs are empty.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/48224>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list