Last active
June 8, 2021 21:33
-
-
Save sc0ttkclark/2c8cbbdda752baa53beb9ab24d482ab1 to your computer and use it in GitHub Desktop.
PMPro integration for WPForms that adds "Require Membership" settings to the "General" settings of a Form. Also has integration with PMPro Series Add-On to prevent access to forms if not yet available in the series flow.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Add output that explains that the person does not have access to this form. | |
* | |
* Customize this to fit your needs! | |
*/ | |
function my_pmpro_wpform_locked_message() { | |
// Set your level ID here so they know where to go to sign up. Non-existent ID will just send them to the Membership Levels page. | |
$level_id = 9999999; | |
?> | |
<p> | |
<?php esc_html_e( 'You do not have access to this form.', 'pmpro-wpforms-snippets' ); ?> | |
<?php echo pmpro_getCheckoutButton( $level_id ); ?> | |
</p> | |
<?php | |
} | |
/** | |
* Add output that explains that the person does not have access to this form yet. | |
* | |
* Customize this to fit your needs! | |
*/ | |
function my_pmpro_wpform_locked_message_in_series() { | |
?> | |
<p> | |
<?php esc_html_e( 'You do not have access to this form yet.', 'pmpro-wpforms-snippets' ); ?> | |
</p> | |
<?php | |
} | |
/** | |
* Filter the list of post types supported for PMPro Series UI. | |
* | |
* @param array $post_types The list of post types supported for PMPro Series UI. | |
* | |
* @return array The list of post types supported. | |
*/ | |
function my_pmpro_wpform_post_types_for_series( array $post_types ) { | |
$post_types[] = 'wpforms'; | |
return $post_types; | |
} | |
add_filter( 'pmpros_post_types', 'my_pmpro_wpform_post_types_for_series' ); | |
/** | |
* Filter whether the form should be loaded based on if it is locked or not. | |
* | |
* @param bool $load_form Indicates whether a form should be loaded. | |
* @param array $form_data Form information. | |
* | |
* @return bool Whether the form is locked. | |
*/ | |
function my_pmpro_wpform_can_form_be_accessed( $load_form, $form_data ) { | |
// Check if PMPro is active. | |
if ( ! function_exists( 'pmpro_hasMembershipLevel' ) ) { | |
return $load_form; | |
} | |
// Don't do anything if we cannot load the form anyways. | |
if ( ! $load_form ) { | |
return $load_form; | |
} | |
// Get the form post ID. | |
$form_id = $form_data['id']; | |
// Get the current user ID to use when checking access. | |
$user_id = get_current_user_id(); | |
$required_levels = []; | |
// Loop through all of the settings to find our level-specific values. | |
foreach ( $form_data['settings'] as $key => $setting_value ) { | |
// It must start with "pmpro_require_membership_". | |
if ( 0 !== strpos( $key, 'pmpro_require_membership_' ) ) { | |
continue; | |
} | |
// It must be checked (1). | |
if ( 1 !== (int) $setting_value ) { | |
continue; | |
} | |
// Add the level ID to the list. | |
$required_levels[] = str_replace( 'pmpro_require_membership_', '', $key ); | |
} | |
// Check if they have access to the levels specified. | |
if ( $required_levels && ! pmpro_hasMembershipLevel( $required_levels, $user_id ) ) { | |
add_action( 'wpforms_frontend_not_loaded', 'my_pmpro_wpform_locked_message' ); | |
return false; | |
} | |
// Check if member has access from a series. | |
if ( function_exists( 'pmpros_hasAccess' ) ) { | |
add_action( 'wpforms_frontend_not_loaded', 'my_pmpro_wpform_locked_message_in_series' ); | |
return pmpros_hasAccess( $user_id, $form_id ); | |
} | |
return true; | |
} | |
add_filter( 'wpforms_frontend_load', 'my_pmpro_wpform_can_form_be_accessed', 10, 2 ); | |
/** | |
* Register panel fields. | |
* | |
* @param object $instance Settings panel instance. | |
*/ | |
function my_pmpro_wpform_register_panel_fields( $instance ) { | |
// Check if PMPro is active. | |
if ( ! function_exists( 'pmpro_getAllLevels' ) ) { | |
return; | |
} | |
$levels = pmpro_getAllLevels( true, true ); | |
$heading = false; | |
foreach ( $levels as $level ) { | |
$args = []; | |
if ( ! $heading ) { | |
$heading = true; | |
$args['before'] = '<h2>' . esc_html__( 'Require Membership', 'pmpro-wpforms-snippets' ) . '</h2>'; | |
} | |
wpforms_panel_field( | |
'checkbox', | |
'settings', | |
'pmpro_require_membership_' . sanitize_key( $level->id ), | |
$instance->form_data, | |
sprintf( esc_html__( 'Require Membership Level: %s', 'pmpro-wpforms-snippets' ), $level->name ), | |
$args | |
); | |
} | |
} | |
add_action( 'wpforms_form_settings_general', 'my_pmpro_wpform_register_panel_fields', 30, 2 ); | |
/** | |
* Filter the post link for Conditional Forms. | |
* | |
* @param string $post_link The post link. | |
* @param WP_Post $post The post object. | |
* | |
* @return string|void | |
*/ | |
function my_pmpro_wpform_filter_post_link( $post_link, $post ) { | |
if ( ! function_exists( 'wpforms_decode' ) ) { | |
return $post_link; | |
} | |
if ( 'wpforms' !== $post->post_type ) { | |
return $post_link; | |
} | |
$form_data = wpforms_decode( $post->post_content ); | |
if ( ! $form_data || empty( $form_data['settings']['conversational_forms_enable'] ) ) { | |
return $post_link; | |
} | |
return home_url( $post->post_name ); | |
} | |
add_filter( 'post_type_link', 'my_pmpro_wpform_filter_post_link', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment