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

Standardising Page Builders - Elementor #3144

Open
wants to merge 7 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion includes/compatibility/elementor.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ function pmpro_elementor_get_all_levels() {

$levels_array = array();

$levels_array[0] = __( 'Non-members', 'paid-memberships-pro' );
foreach( $all_levels as $level ) {
$levels_array[ $level->id ] = $level->name;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,61 @@ protected function content_restriction() {
// Register controls to sections and widgets
protected function register_controls() {
foreach( $this->locations as $where ) {
add_action('elementor/element/'.$where['element'].'/'.$this->section_name.'/before_section_end', array( $this, 'add_controls' ), 10, 2 );
add_action('elementor/element/'.$where['element'].'/'.$this->section_name.'/before_section_end', array( $this, 'add_controls' ), 10, 2 );
}
}

// Define controls
public function add_controls( $element, $args ) {
$element->add_control(
'pmpro_require_membership_heading', array(
'label' => __( 'Require Membership Level', 'paid-memberships-pro' ),
'type' => Controls_Manager::HEADING,
'separator' => 'before',
)
$element->add_control(
'pmpro_enable', array(
'type' => \Elementor\Controls_Manager::SWITCHER,
'label' => esc_html__( 'Enable Paid Memberships Pro module visibility?', 'textdomain' ),
'default' => 'no',
)
);

$element->add_control(
$element->add_control(
'pmpro_invert_restrictions', array(
'type' => \Elementor\Controls_Manager::SELECT,
'options' => array(
'0' => esc_html__( 'Show content to...', 'textdomain' ),
Copy link
Contributor

@andrewlimaza andrewlimaza Jan 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'textdomain' instances need to be updated to 'paid-memberships-pro'

'1' => esc_html__( 'Hide content from...', 'textdomain' ),
),
'label_block' => 'true',
'default' => '0',
'condition' => [
'pmpro_enable' => 'yes',
],
)
);

$element->add_control(
'pmpro_segment', array(
'type' => \Elementor\Controls_Manager::SELECT,
'options' => array(
'all' => esc_html__( 'All Members', 'textdomain' ),
'specific' => esc_html__( 'Specific Membership Levels', 'textdomain' ),
'logged_in' => esc_html__( 'Logged-In Users', 'textdomain' ),
),
'label_block' => 'true',
'default' => 'all',
'condition' => [
'pmpro_enable' => 'yes',
],
)
);

$element->add_control(
'pmpro_require_membership', array(
'type' => Controls_Manager::SELECT2,
'options' => pmpro_elementor_get_all_levels(),
'multiple' => 'true',
'label_block' => 'true',
'description' => __( 'Require membership level to see this content.', 'paid-memberships-pro' ),
'label' => __( 'Membership Levels', 'paid-memberships-pro' ),
'condition' => [
'pmpro_segment' => 'specific',
'pmpro_enable' => 'yes',
],
)
);

Expand All @@ -49,10 +83,12 @@ public function add_controls( $element, $args ) {
'pmpro_no_access_message', array(
'label' => esc_html__( 'Show no access message', 'paid-memberships-pro' ),
'type' => \Elementor\Controls_Manager::SWITCHER,
'label_on' => esc_html__( 'Yes', 'paid-memberships-pro' ),
'label_off' => esc_html__( 'No', 'paid-memberships-pro' ),
'return_value' => 'yes',
'default' => 'no',
'condition' => [
'pmpro_enable' => 'yes',
'pmpro_invert_restrictions' => '0',
],
)
);
}
Expand All @@ -70,17 +106,29 @@ public function pmpro_elementor_should_render( $should_render, $element ) {
// Don't hide content in editor mode.
if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
return $should_render;
}

}
// Bypass if it's already hidden.
if ( $should_render === false ) {
return $should_render;
}

// Checks if the element is restricted and then if the user has access.
$should_render = $this->pmpro_elementor_has_access( $element );

return apply_filters( 'pmpro_elementor_section_access', $should_render, $element );
$element_settings = $element->get_active_settings();

// If the block is not being restricted, then the user should be able to view it.
if ( empty( $element_settings['pmpro_enable'] ) || 'no' === $element_settings['pmpro_enable'] ) {
return true;
}

$apply_block_visibility_params = array(
'segment' => $element_settings['pmpro_segment'],
'levels' => $element_settings['pmpro_require_membership'],
'invert_restrictions' => $element_settings['pmpro_invert_restrictions'],
'show_noaccess' => $element_settings['pmpro_no_access_message'],
);
$should_render = ! empty( pmpro_apply_block_visibility( $apply_block_visibility_params, 'sample content' ) );

return apply_filters_deprecated( 'pmpro_elementor_section_access', array( $should_render, $element ), 'TBD' );
}

/**
Expand All @@ -91,48 +139,59 @@ public function pmpro_elementor_should_render( $should_render, $element ) {
public function pmpro_elementor_render_content( $content, $widget ){

// Don't hide content in editor mode.
if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
return $content;
}

// We can only use the no access message on a widget
if ( 'widget' !== $widget->get_type() ) {
return $content;
}

$widget_settings = $widget->get_active_settings();

$show = $this->pmpro_elementor_has_access( $widget );
$widget_settings = $widget->get_active_settings();
// If the block is not being restricted, bail.
if ( empty( $widget_settings['pmpro_enable'] ) || 'no' === $widget_settings['pmpro_enable'] ) {
return $content;
}

// Use the pmpro_apply_block_visibility() method to generate output.
$apply_block_visibility_params = array(
'segment' => $widget_settings['pmpro_segment'],
'levels' => $widget_settings['pmpro_require_membership'],
'invert_restrictions' => $widget_settings['pmpro_invert_restrictions'],
'show_noaccess' => $widget_settings['pmpro_no_access_message'],
);
return pmpro_apply_block_visibility( $apply_block_visibility_params, $content );

if ( ! $show ) {
// Show no content message here or not
if ( $widget_settings['pmpro_no_access_message'] === 'yes' ) {
$content = pmpro_get_no_access_message( NULL, $widget_settings['pmpro_require_membership'] );
} else {
$content = '';
}
}

return $content;
}

/**
* Figure out if the user has access to restricted content.
* @return bool True or false based if the user has access to the content or not.
* @since 2.3
* @deprecated TBD
*/
public function pmpro_elementor_has_access( $element ) {
_deprecated_function( __METHOD__, 'TBD' );

$element_settings = $element->get_active_settings();

$restricted_levels = $element_settings['pmpro_require_membership'];

// Just bail if the content isn't restricted at all.
if ( ! $restricted_levels ) {
return true;
}

if ( ! pmpro_hasMembershipLevel( $restricted_levels ) ) {
$access = false;
} else {
$access = true;
}
// If the block is not being restricted, then the user has access.
if ( empty( $element_settings['pmpro_enable'] ) || 'no' === $element_settings['pmpro_enable'] ) {
return true;
}

return apply_filters( 'pmpro_elementor_has_access', $access, $element, $restricted_levels );
// If pmpro_apply_block_visibility returns content, then we want the user to see it.
$apply_block_visibility_params = array(
'segment' => $element_settings['pmpro_segment'],
'levels' => $element_settings['pmpro_require_membership'],
'invert_restrictions' => $element_settings['pmpro_invert_restrictions'],
'show_noaccess' => $element_settings['pmpro_no_access_message'],
);
$access = ! empty( pmpro_apply_block_visibility( $apply_block_visibility_params, 'sample content' ) );

return apply_filters( 'pmpro_elementor_has_access', $access, $element, $element_settings['pmpro_require_membership'] );
}
}

Expand Down