Skip to:
Content

BuddyPress.org

Opened 9 years ago

Closed 8 years ago

#6507 closed enhancement (no action required)

Allow Ajax sub nav tab loading

Reported by: codemonkeybanana's profile CodeMonkeyBanana Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Core Keywords:
Cc: mikeymike81@…, espellcaste@…

Description

Hi,

With ajax subnav tab loading it needs to work for logged in and logged out users alike so I made function that will work for both and check if user has access to view tab.

function bp_legacy_theme_tab_template_loader() {
	global $bp;
	// Bail if not a POST action
	if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
		exit( '1' );

	// exit if full path not specified
	if (empty($_POST['component']) || empty($_POST['page']) || empty($_POST['tab'])) {
		if ( SCRIPT_DEBUG ) {
			exit( 'missing path component' );
		} else {
			exit( '1' );
		}
	}

	// create full path
	$component = sanitize_title( $_POST['component'] );
	$page = sanitize_title( $_POST['page'] );
	$tab = sanitize_title( $_POST['tab'] );
	$path = $component.'/single/'.$page.'/'.$tab;

	if ($bp->bp_options_nav[$page][$tab]['user_has_access'] === true)
	{
		// they are allowed to view it so return template
		bp_get_template_part( $path );
		exit( '0' );

	}
	else
	{
		if(SCRIPT_DEBUG) {
			exit( 'insufficient access' );
		}else {
			exit( '1' );
		}
	}

}

I haven't included javascript as I am working on theme with custom menu that works a bit different but just needs a small bit of code added to when it checks for no-ajax class to work although that would probably be confusing to read. I can post it if needed once I finish this theme.

Change History (8)

#1 @CodeMonkeyBanana
9 years ago

Gone round in circles and simplest way I can see to make this work is to add an additional template file into every directory that is named as the initial view. It will work because for instance "View" is called "public". Public cant be loaded by ajax because it will load the menu twice and outer html so you need just the action template so you can get back to original tab. It's backwards compatible but if someone partially copies updated template to existing theme they may have missing reference or end up with two versions of same tab.

So for instance add this file "buddypress/members/single/profile/view.php". Contents of file:

<?php
if (bp_is_active('xprofile'))
	bp_get_template_part('members/single/profile/profile-loop');

Do that for all folders and then ajax tabs will work fine with no problems and be secure. I looked at using the existing template hierarchy but it means creating a new file for each action or making some modifications that may not be secure and you would still need to add in additional template anyway.

I think best way to integrate it into bp_options_nav is to leave all css and id the way they are but add a data attribute with the action slug and then this can be used as input to function below.

Updated original code: (I need to update it again so it loops over name possibilities. First it will try name, then slug. That should get around naming inconsistencies, will do in a bit)

function bp_legacy_theme_tab_template_loader() {
	global $bp;
	// Bail if not a POST action
	if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
		exit( '1' );

	// exit if full path not specified
	if (empty($_POST['component']) || empty($_POST['tab']) || empty($_POST['slug'])) {
		if ( SCRIPT_DEBUG ) {
			exit( 'missing path component' );
		} else {
			exit( '1' );
		}
	}

	// get path
	$component = sanitize_title( $_POST['component'] );
	$tab_name = sanitize_title( $_POST['tab'] );
	$slug = sanitize_title( $_POST['slug'] );

	// create template list to search for in order
	// looks first for a new template file that is name but lowercase with spaces replaced to hyphen
	// then looks for the slug.  This will allow it to return to initial tab without loading double menu
	$templates = array();
	$templates[] = 'members/single/'.$component.'/'.$tab_name.'.php';
	$templates[] = 'members/single/'.$component.'/'.$slug.'.php';

	// check user has permission to view template part
	if ($bp->bp_options_nav[$component][$slug]['user_has_access'] == true)
	{
		do_action( 'get_template_part_' . $slug, $slug, '' );
		// Execute code for this ajax tab template part
		/**
		 * Fires before ajax tab template part is loaded.
		 *
		 * This is a variable hook that is dependent on the slug passed in.
		 *
		 * @param string $slug The tabs slug.
		 * @param string $name The tabs name, lower case with space replaced with hyphens.
		 */
		do_action( 'get_template_ajax_tab_' . $slug, $slug, $tab_name );

		// allow ajax template part specific filtering
		$templates = apply_filters( 'bp_get_template_part_ajax', $templates, $slug, $tab_name );

		// Return the part that is found
		bp_locate_template( $templates, true, false );
		exit();

	}
	else
	{
		if(SCRIPT_DEBUG) {
			exit( 'insufficient access' );
		}else {
			exit( '1' );
		}
	}

}
Last edited 9 years ago by CodeMonkeyBanana (previous) (diff)

#2 @CodeMonkeyBanana
9 years ago

  • Cc mikeymike81@… added

#3 @espellcaste
9 years ago

  • Cc espellcaste@… added

This ticket was mentioned in Slack in #buddypress by guerrilla. View the logs.


9 years ago

#5 @DJPaul
9 years ago

  • Milestone changed from Awaiting Review to Under Consideration

There was a bunch of discussion on this on Slack the other day. Did anyone record what the outcome was?

#6 @DJPaul
8 years ago

  • Component changed from Component - Any/All to Core

#7 @DJPaul
8 years ago

  • Milestone changed from Under Consideration to Awaiting Review

#8 @DJPaul
8 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

For consideration as a core feature, this report has had no traction so I'm closing it. Future work, involving implementing a REST API, will make this kind of feature easier to build client-side.

Note: See TracTickets for help on using tickets.