• Hello NGG,

    Among other problematic behaviour (such as how this plugin resolves the “document root”), this plugin (as of v3.36) disables WordPress’ default actions on the template_redirect hook:

    • wp_old_slug_redirect
    • redirect_canonical

    Replacing them with an early action to resolve the request URI ($_SERVER['NGG_ORIG_REQUEST_URI']). If this server variable is undefined, the method half-heartedly attempts to directly call the original action functions but only if BuddyPress and WPML are not installed.

    class M_WordPress_Routing extends C_Base_Module
    {
    # ...
    function _register_hooks()
    {
    	add_action('template_redirect', array(&$this, 'restore_request_uri'), 1);
    
    	// These two things cause conflicts in NGG. So we temporarily
    	// disable them and then reactivate them, if they were used,
    	// in the restore_request_uri() method
    	if (has_action('template_redirect', 'wp_old_slug_redirect')) {
    		remove_action( 'template_redirect', 'wp_old_slug_redirect');
    	}
    	if (has_action('template_redirect', 'redirect_canonical')) {
    		remove_action( 'template_redirect', 'redirect_canonical');
    	}
    }
    # ...
    function restore_request_uri()
    {
    	if (isset($_SERVER['NGG_ORIG_REQUEST_URI']))
    	{
    		$request_uri = $_SERVER['NGG_ORIG_REQUEST_URI'];
    		$_SERVER['UNENCODED_URL'] = $_SERVER['HTTP_X_ORIGINAL_URL'] = $_SERVER['REQUEST_URI'] = $request_uri;
    
    		if (isset($_SERVER['ORIG_PATH_INFO'])) {
    			$_SERVER['PATH_INFO'] = $_SERVER['ORIG_PATH_INFO'];
    		}
    	}
    	// this is the proper behavior but it causes problems with WPML
    	else {
    		if (self::$_use_old_slugs) wp_old_slug_redirect();
    		if (self::$_use_canonical_redirect) redirect_canonical();
    	}
    }
    class M_Third_Party_Compat extends C_Base_Module
    {
    # ...
    function _register_hooks()
    {
    	# ...
    	// Like WPML, BuddyPress is incompatible with our routing hacks.
    	if (function_exists('buddypress'))
    	{
    		M_WordPress_Routing::$_use_canonical_redirect = FALSE;
    		M_WordPress_Routing::$_use_old_slugs = FALSE;
    	}
    
    	// WPML fix
    	if (class_exists('SitePress'))
    	{
    		M_WordPress_Routing::$_use_canonical_redirect = FALSE;
    		M_WordPress_Routing::$_use_old_slugs = FALSE;
    		add_action('template_redirect', array(&$this, 'fix_wpml_canonical_redirect'), 1);
    	}
    	# ...
    }
    # ...
    function fix_wpml_canonical_redirect()
    {
    	M_WordPress_Routing::$_use_canonical_redirect = FALSE;
    	M_WordPress_Routing::$_use_old_slugs = FALSE;
    }

    There must be a better way to handle this.

    Maybe by checking if the default template_redirect actions are present during your early restore_request_uri callback.

    For the project I’m working on, I implemented a complicated check to verify if NGG and WPML is disabling the default actions:

    add_action( 'template_redirect', function () : void {
    	global $sitepress;
    
    	if (
    		isset( $_SERVER['NGG_ORIG_REQUEST_URI'] ) ||
    		! class_exists( 'M_WordPress_Routing', false )
    	) {
    		return;
    	}
    
    	$has_wp_old_slug_redirect  = has_action( 'template_redirect', 'wp_old_slug_redirect' );
    	$has_wp_redirect_canonical = has_action( 'template_redirect', 'redirect_canonical' );
    
    	if (
    		! M_WordPress_Routing::$_use_old_slugs &&
    		! $has_wp_old_slug_redirect
    	) {
    		add_action( 'template_redirect', 'wp_old_slug_redirect' );
    	}
    
    	if (
    		! M_WordPress_Routing::$_use_canonical_redirect &&
    		! $has_wp_redirect_canonical
    	) {
    		$is_wpml_disabling_redirect_canonical = (
    			/** @see WPML_Root_Page_Actions::wpml_home_url_setup_root_page() */
    			(
    				class_exists( 'WPML_Root_Page', false ) &&
    				WPML_Root_Page::is_current_request_root() &&
    				! WPML_Root_Page::uses_html_root()
    			) ||
    			/** @se: WPML_Redirect_By_Param::template_redirect_action() */
    			(
    				$sitepress &&
    				$sitepress->get_wp_api()->is_front_page() &&
    				$sitepress->get_wp_api()->get_query_var( 'page' ) &&
    				$sitepress->get_default_language() !== $sitepress->get_current_language()
    			)
    		);
    
    		if ( ! $is_wpml_disabling_redirect_canonical ) {
    			add_action( 'template_redirect', 'redirect_canonical' );
    		}
    	}
    }, 2 );

    One thing I noticed with WPML v4.6, the wp_old_slug_redirect action is not disabled as far as I can see.

    Regards,

Viewing 1 replies (of 1 total)
  • Benjamin

    (@benjaminowens)

    Hi @mcaskill

    Thanks for your feedback! How NextGEN handles routing is definitely in need of some work; it’s become a bit of a legacy from the early 2.0 days. My apologies for the difficulty it added to your project.

    We are presently working on some major future changes to NextGEN that removes the framework that versions 2 & 3 were built on; the M_WordPress_Routing class will be renamed to Imagely\NGG\Util\Router eventually. We’re only just beginning alpha testing, but you can see the new routing file at https://github.com/imagely/nextgen-gallery/blob/35-remove-pope/src/Util/Router.php

Viewing 1 replies (of 1 total)
  • The topic ‘NGG breaks WordPress’ default “template_redirect” actions’ is closed to new replies.