strangerstudios / pmpro-register-helper

A robust plugin to collect additional fields for WordPress users. Fields can be collected at membership checkout, on the user's profile or for administrative view-only.
https://www.paidmembershipspro.com/add-ons/pmpro-register-helper-add-checkout-and-profile-fields/
29 stars 41 forks source link

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

Open ipokkel opened 3 years ago

ipokkel commented 3 years ago

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:

https://github.com/strangerstudios/pmpro-register-helper/blob/2780df68cde584b5087ad7b508aa68a84494046c/pmpro-register-helper.php#L1338-L1352

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]):

ipokkel commented 3 years ago

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' );