LLMS_Integration_Buddypress::modify_paginate_links( string $link )

Modify the pagination links for the endpoints in the bp member profile.


Description Description

This fixes the pagination not correctly working on the fist subnav.


Top ↑

Parameters Parameters

$link

(string) (Required) Default link.


Top ↑

Return Return

(string)


Top ↑

Source Source

File: includes/integrations/class.llms.integration.buddypress.php

	public function modify_paginate_links( $link ) {

		global $wp_rewrite;

		// With ugly permalinks actually the whole BuddyPress member profile page doesn't work.
		if ( ! get_option( 'permalink_structure' ) ) {
			return $link;
		}

		// Remove query vars if any, we'll add them back later.
		$query = wp_parse_url( $link, PHP_URL_QUERY );
		if ( $query ) {
			$link = str_replace( '?' . $query, '', $link );
		}

		// Retrieve link's page number.
		$parts = explode( '/', untrailingslashit( $link ) );
		$page  = end( $parts );

		// For links to page 1 let's remove it to avoid ugly URLs.
		if ( 1 === (int) $page ) {
			$link = str_replace(
				user_trailingslashit( $wp_rewrite->pagination_base . '/' . $page ),
				'',
				$link
			);
		}

		$endpoints = $this->get_profile_endpoints();

		// If we're not the first subnav, our job is done, add back the query var and return.
		if ( key( $endpoints ) !== $this->current_endpoint_key ) {
			return $query ? $link . '?' . $query : $link;
		}

		// Retrieve our first subnav menu item.
		$first_subnav_item = buddypress()->members->nav->get_secondary(
			array(
				/** This filter is documented above */
				'parent_slug' => apply_filters( 'llms_buddypress_main_nav_item_slug', _x( 'courses', 'BuddyPress profile main nav item slug', 'lifterlms' ) ),
				'slug'        => $endpoints[ $this->current_endpoint_key ]['endpoint'],
			)
		);

		if ( is_array( $first_subnav_item ) ) {
			$first_subnav_item = reset( $first_subnav_item );
		} else { // Bail.
			return $query ? $link . '?' . $query : $link;
		}

		$current_page = llms_get_paged_query_var();

		/**
		 * Here's the core of this filter.
		 *
		 * What happens is that the pagination links on the first page of the fist subnav,
		 * e.g. 'my-courses' endpoint (as example for the fist subnav) are of this type:
		 * `example.local/members/admin/courses/page/N`,
		 * where 'courses' is the slug of the main nav item.
		 * While the "working" paginate links must be of the type:
		 * `example.local/members/admin/courses/my-courses/page/N`
		 * where 'courses' is the slug of the main nav item, and 'my-courses' is the slug of
		 * the subnav item which is the default subnav for the main nav item.
		 *
		 * So what we do here is replacing the link that looks like:
		 * `example.local/members/admin/courses/page/N` to something like:
		 * `example.local/members/admin/courses/my-courses/page/N`
		 *
		 * Despite one might expect, `$first_subnav_item->link` doesn't point to `example.local/members/admin/courses/my-courses/`
		 * but to `example.local/members/admin/courses/`, which is the link of the parent nav, the main nav item,
		 * this because the 'my-courses' subnav item is the default of the 'courses' nav item (default_subnav_slug).
		 */
		if ( 1 === $current_page ) {
			$link = user_trailingslashit( $first_subnav_item->link . $first_subnav_item->slug . '/' . $wp_rewrite->pagination_base . '/' . $page );
		} elseif ( 1 === (int) $page ) {
			/**
			 * For links to page 1, when not on page 1, let's back on the main nav item URL, so we replace something like
			 * `example.local/members/admin/courses/my-courses/`
			 * to something like
			 * `example.local/members/admin/courses/`
			 */
			$link = $first_subnav_item->link;
		}

		return $query ? $link . '?' . $query : $link;

	}


Top ↑

Changelog Changelog

Changelog
Version Description
6.3.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.