WordPress.org

Make WordPress Core

Opened 5 months ago

Last modified 5 months ago

#48224 new enhancement

Refactor paginate_links and add 4 new functions (paginate_array and format_paginate_link)

Reported by: ngdangtu Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 5.3
Component: Themes Keywords: has-patch
Focuses: template Cc:

Description

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.

Attachments (1)

general-template.patch (18.4 KB) - added by ngdangtu 5 months ago.
Somehow the old changed in this file (use for other ticket) also got tangle in. I'm really sorry about this. I really don't know how to seperate it.

Download all attachments as: .zip

Change History (2)

@ngdangtu
5 months ago

Somehow the old changed in this file (use for other ticket) also got tangle in. I'm really sorry about this. I really don't know how to seperate it.

#1 @SergeyBiryukov
5 months ago

  • Component changed from Customize to Themes
  • Focuses template added; ui removed
Note: See TracTickets for help on using tickets.