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

Select2 script and style not enqueued if level parameter not present #219

Open
3 tasks done
ipokkel opened this issue Sep 3, 2021 · 1 comment
Open
3 tasks done

Comments

@ipokkel
Copy link
Member

ipokkel commented Sep 3, 2021

Describe the bug
A select2 Register Helper field type is rendered as a multi-select field and the console has a javascript error notification of select2 not being a valid function when there is no level URL parameter.

We should consider additional conditional checks if the user is on the checkout page to load the required CSS & JS here:

function pmprorh_enqueue_select2($hook) {
global $pmpro_pages;
// only include on front end and user profiles
if( ( !is_admin() && (
!empty( $_REQUEST['level'] ) ||
!empty( $pmpro_level ) ||
class_exists("Theme_My_Login") && method_exists('Theme_My_Login', 'is_tml_page') && Theme_My_Login::is_tml_page("profile") ) ||
( isset( $pmpro_pages['member_profile_edit'] ) && is_page( $pmpro_pages['member_profile_edit'] ) ) ) ||
$hook == 'profile.php' ||
$hook == 'user-edit.php' ) {
wp_enqueue_style('select2', plugins_url('css/select2.min.css', __FILE__), '', '4.0.3', 'screen');
wp_enqueue_script('select2', plugins_url('js/select2.min.js', __FILE__), array( 'jquery' ), '4.0.3' );
}
}

To Reproduce
Steps to reproduce the behavior:

  1. Create a select2 RH field in a customization plugin.
  2. Navigate to the membership levels page and select any level
  3. Confirm on the checkout page that the select2 field is rendered as expected.
  4. Remove URL parameter level, e.g. level=1, and load page
  5. Confirm that the select2 field is rendered as a multi-select field type
  6. Open the browser's developer tools and confirm that select2 CSS and JS files are not loaded.

Screenshots
If applicable, please attach a screenshot to make your issue clearer.

Expected behavior
A clear and concise description of what you expected to happen.

Isolating the problem (mark completed items with an [x]):

  • I have deactivated other plugins and confirmed this bug occurs when only Paid Memberships Pro plugin is active.
  • This bug happens with a default WordPress theme active, or Memberlite.
  • I can reproduce this bug consistently using the steps above.
@ipokkel
Copy link
Member Author

ipokkel commented Sep 3, 2021

A temporary workaround to load the required files would be to load a customization recipe in a customization plugin that also checks if the user is on the checkout page or whether a level for checkout exists.

function my_pmprorh_enqueue_select2( $hook ) {

	if ( ! defined( 'PMPRORH_URL' ) || ! function_exists( 'pmpro_getLevelAtCheckout' ) ) {
		return;
	}

	global $pmpro_pages;

	$level = pmpro_getLevelAtCheckout();

	// only include on front end and user profiles
	if (
			( ! is_admin() &&
				( ! empty( $_REQUEST['level'] ) ||
					! empty( $pmpro_level ) ||
					class_exists( 'Theme_My_Login' ) && method_exists( 'Theme_My_Login', 'is_tml_page' ) && Theme_My_Login::is_tml_page( 'profile' )
				) ||
				( isset( $pmpro_pages['member_profile_edit'] ) && is_page( $pmpro_pages['member_profile_edit'] ) ) ||
				( isset( $pmpro_pages['checkout'] ) && is_page( $pmpro_pages['checkout'] ) ) ||
				( isset( $level->id ) )
			) ||
			$hook == 'profile.php' ||
			$hook == 'user-edit.php' 
		) {
		wp_enqueue_style( 'select2', PMPRORH_URL . '/css/select2.min.css', '', '4.0.3', 'screen' );
		wp_enqueue_script( 'select2', PMPRORH_URL . '/js/select2.min.js', array( 'jquery' ), '4.0.3' );
	}
}
add_action( 'wp_enqueue_scripts', 'my_pmprorh_enqueue_select2' );

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