Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Support for limited subsite associations #143

Open
Idealien opened this issue Apr 7, 2020 · 1 comment
Open

Feature Request: Support for limited subsite associations #143

Idealien opened this issue Apr 7, 2020 · 1 comment

Comments

@Idealien
Copy link
Contributor

Idealien commented Apr 7, 2020

Description
The plugin is exceptional when a multi-site setup has every sub-site representing a different language of the same content. However, when a multi-site has multiple actual content sub-sites, there are multiple places where the UI presents all sub-sites for selection that are not appropriate for use case such as:

  • Post Edit Screen - Doc Sidebar for MLSwitcher - Separate drop-down per sub-site
  • Post Lists Screen - Separate column per translation/potential translation
  • Front-End Site - When Menu Settings/Theme Location defined

To Reproduce

  • Create a multi-site network with 8 sub-sites (EN1, EN2, EN3, EN4, FR1, FR2, FR3, FR4)

Expected behaviour

  • Provide an option under Multisite Language Switcher Options / Main Settings that lets each site be (optionally) associated to N number of sites. (multi-select field type)
  • When this has been defined, anywhere that currently displays ALL sub-sites would only display the associated sub-sites.
  • In reproduced example, EN1/FR1 would see each other, EN2/FR2, etc.

There is a code-based temporary solution possible through the msls_blog_collection_construct filter

add_filter( 'msls_blog_collection_construct', 'msls_sites_collection_restrictions' );
function msls_sites_collection_restrictions( $sites ) {

	$restrict = true;

	//Ensure front-end only are restricted.
	if ( is_admin() && isset( $_GET['page'] ) ) {
		$admin_path = strtolower( sanitize_text_field( $_GET['page'] ) );
		if ( $admin_path === 'mslsadmin' ) {
			$restruct = false;
		}
	}

	if ( ! $restrict ) {
		return $sites;
	}

	//Find out the current blog and let it's language counterpart be displayed.
	//@TODO - Refactor this so the en/fr association is stored in site options.
	$current_blog_id = get_current_blog_id();

	switch ( $current_blog_id ) {

		case 1:
			$keep_blog_id = 2;
			break;
		case 2:
			$keep_blog_id = 1;
			break;
		case 3:
			$keep_blog_id = 4;
			break;
		case 4:
			$keep_blog_id = 3;
			break;
	}

	foreach ( $sites as $key => $site ) {
		if ( $key !== $keep_blog_id && $key !== $current_blog_id ) {
			unset( $sites[ $key ] );
		}
	}

	return $sites;
}

Screenshots
image
image

Environment (please complete the following information):

  • OS: Linux
  • Browser: All
  • PHP Version 7.2
  • Plugin Version: 2.3 and 2.4.3
@Idealien
Copy link
Contributor Author

Idealien commented Sep 6, 2020

Hoping this could be flagged as an enhancement item for 2.5?

I have updated the temporary workaround code mentioned above to read from a separate option defined through an ACF meta form. It means that code does not have to update with every new subsite added, but does introduce small issue when migrating subsites between dev/stg/prod environments if the subsite ID changes. Generally, accessing the settings panel (both ACF data and WPMSLS) to re-save settings post migration addresses it.

add_filter( 'msls_blog_collection_construct', 'msls_sites_collection_restrictions' );
function msls_sites_collection_restrictions( $sites ) {
	$restrict = true;

	//Ensure front-end only are restricted.
	if ( is_admin() && isset( $_GET['page'] ) ) {
		$admin_path = strtolower( sanitize_text_field( $_GET['page'] ) );
		if ( $admin_path === 'mslsadmin' ) {
			$restruct = false;
		}
	}

	if ( ! $restrict ) {
		return $sites;
	}

	$current_blog_id = get_current_blog_id();
	$bilingual_id = false;

	//Retrieve bilingual site associated in ACF option stored under 'CPC Settings'
	if ( function_exists( 'get_field' ) ) {
		$bilingual_id = get_field( 'bilingual_subsite', 'option' );
	}

	//Only present the associated language subsite
	if ( $bilingual_id ) {
		$new_sites = array();
		foreach ( $sites as $key => $site ) {
			if ( $site->blog_id == $bilingual_id  || $site->blog_id == $current_blog_id ) {
				$new_sites[ $key ] = $sites[ $key ];
			}
		}
		if ( ! empty( $new_sites ) ) {
			return $new_sites;
		}
	}
	return $sites;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant