From 8d18c9a32362a7f5541ebccb0501b5c774820291 Mon Sep 17 00:00:00 2001 From: Jarryd Long Date: Sun, 22 Sep 2024 21:38:33 +0200 Subject: [PATCH 1/6] Standardizing Page Builders - Elementor --- ...ss-pmpro-elementor-content-restriction.php | 269 +++++++++++++++--- 1 file changed, 230 insertions(+), 39 deletions(-) diff --git a/includes/compatibility/elementor/class-pmpro-elementor-content-restriction.php b/includes/compatibility/elementor/class-pmpro-elementor-content-restriction.php index eeff5ea8c9..52fa7d99f9 100644 --- a/includes/compatibility/elementor/class-pmpro-elementor-content-restriction.php +++ b/includes/compatibility/elementor/class-pmpro-elementor-content-restriction.php @@ -19,27 +19,95 @@ 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', - ) + /** + * visibilityBlockEnabled + * segment - all, specific, logged_in + * levels + * show_noaccess + */ + + + $element->add_control( + 'pmpro_enable', array( + 'type' => \Elementor\Controls_Manager::SWITCHER, + 'label' => esc_html__( 'Enable Paid Memberships Pro module visibility?', 'textdomain' ), + 'options' => array( + 'yes' => esc_html__( 'Yes', 'textdomain' ), + 'no' => esc_html__( 'No', 'textdomain' ), + + ), + 'default' => 'no', + + ) + ); + + $element->add_control( + 'pmpro_content_visibility', array( + 'type' => \Elementor\Controls_Manager::SELECT, + 'label' => esc_html__( 'Content Visbility', 'textdomain' ), + 'options' => array( + 'show' => esc_html__( 'Show', 'textdomain' ), + 'hide' => esc_html__( 'Hide', 'textdomain' ), + ), + 'default' => 'show', + 'condition' => [ + 'pmpro_enable' => 'yes', + ], + ) ); - $element->add_control( + $element->add_control( + 'pmpro_show_content_to', array( + 'type' => \Elementor\Controls_Manager::SELECT, + 'label' => esc_html__( 'Show Content To', 'textdomain' ), + 'options' => array( + 'all' => esc_html__( 'All Members', 'textdomain' ), + 'specific' => esc_html__( 'Specific Membership Levels', 'textdomain' ), + 'logged_in' => esc_html__( 'Logged-In Users', 'textdomain' ), + ), + 'default' => 'all', + 'condition' => [ + 'pmpro_content_visibility' => 'show', + 'pmpro_enable' => 'yes', + ], + ) + ); + + $element->add_control( + 'pmpro_hide_content_from', array( + 'type' => \Elementor\Controls_Manager::SELECT, + 'label' => esc_html__( 'Hide Content From', 'textdomain' ), + 'options' => array( + 'all' => esc_html__( 'All Members', 'textdomain' ), + 'specific' => esc_html__( 'Specific Membership Levels', 'textdomain' ), + 'logged_in' => esc_html__( 'Logged-In Users', 'textdomain' ), + ), + 'default' => 'all', + 'condition' => [ + 'pmpro_content_visibility' => 'hide', + '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' ), + 'description' => __( 'Membership Levels', 'paid-memberships-pro' ), + 'condition' => [ + 'pmpro_show_content_to' => 'specific', + 'pmpro_hide_content_from' => 'specific', + 'pmpro_enable' => 'yes', + ], ) ); @@ -53,6 +121,9 @@ public function add_controls( $element, $args ) { 'label_off' => esc_html__( 'No', 'paid-memberships-pro' ), 'return_value' => 'yes', 'default' => 'no', + 'condition' => [ + 'pmpro_enable' => 'yes', + ], ) ); } @@ -70,16 +141,15 @@ 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 ); + $should_render = pmpro_elementor_has_access( $element ); + return apply_filters( 'pmpro_elementor_section_access', $should_render, $element ); } @@ -91,22 +161,103 @@ 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(); + + if( isset( $widget_settings['pmpro_enable'] ) && $widget_settings['pmpro_enable'] === 'yes' ) { - $show = $this->pmpro_elementor_has_access( $widget ); - $widget_settings = $widget->get_active_settings(); + if( isset( $widget_settings['pmpro_content_visibility'] ) && $widget_settings['pmpro_content_visibility'] === 'show' ) { + + if( isset( $widget_settings['pmpro_show_content_to'] ) && $widget_settings['pmpro_show_content_to'] === 'all' ) { + + if( pmpro_hasMembershipLevel() ) { + return $content; + } else { + if( isset( $widget_settings['pmpro_no_access_message'] ) && $widget_settings['pmpro_no_access_message'] === 'yes' ) { + return pmpro_get_no_access_message( NULL, array() ); + } else { + return ''; + } + } + + } else if( isset( $widget_settings['pmpro_show_content_to'] ) && $widget_settings['pmpro_show_content_to'] === 'specific' ) { + + if( pmpro_hasMembershipLevel( $widget_settings['pmpro_require_membership'] ) ) { + return $content; + } else { + if( isset( $widget_settings['pmpro_no_access_message'] ) && $widget_settings['pmpro_no_access_message'] === 'yes' ) { + return pmpro_get_no_access_message( NULL, $widget_settings['pmpro_require_membership'] ); + } else { + return ''; + } + } + + } else if( isset( $widget_settings['pmpro_show_content_to'] ) && $widget_settings['pmpro_show_content_to'] === 'logged_in' ) { + + if( is_user_logged_in() ) { + return $content; + } else { + if( isset( $widget_settings['pmpro_no_access_message'] ) && $widget_settings['pmpro_no_access_message'] === 'yes' ) { + return pmpro_get_no_access_message( NULL, array() ); + } else { + return ''; + } + } + + } + + } else { + + if( isset( $widget_settings['pmpro_hide_content_from'] ) && $widget_settings['pmpro_hide_content_from'] === 'all' ) { + + if( ! pmpro_hasMembershipLevel() ) { + return $content; + } else { + if( isset( $widget_settings['pmpro_no_access_message'] ) && $widget_settings['pmpro_no_access_message'] === 'yes' ) { + return pmpro_get_no_access_message( NULL, array() ); + } else { + return ''; + } + } + + } else if( isset( $widget_settings['pmpro_hide_content_from'] ) && $widget_settings['pmpro_hide_content_from'] === 'specific' ) { + + if( pmpro_hasMembershipLevel( $widget_settings['pmpro_require_membership'] ) ) { + return $content; + } else { + if( isset( $widget_settings['pmpro_no_access_message'] ) && $widget_settings['pmpro_no_access_message'] === 'yes' ) { + return pmpro_get_no_access_message( NULL, $widget_settings['pmpro_require_membership'] ); + } else { + return ''; + } + } + + } else if( isset( $widget_settings['pmpro_hide_content_from'] ) && $widget_settings['pmpro_hide_content_from'] === 'logged_in' ) { + + if( is_user_logged_in() ) { + return $content; + } else { + if( isset( $widget_settings['pmpro_no_access_message'] ) && $widget_settings['pmpro_no_access_message'] === 'yes' ) { + return pmpro_get_no_access_message( NULL, array() ); + } else { + return ''; + } + } + + } + + } + + } - 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; } @@ -118,20 +269,60 @@ public function pmpro_elementor_render_content( $content, $widget ){ public function pmpro_elementor_has_access( $element ) { $element_settings = $element->get_active_settings(); + + if( isset( $element_settings['pmpro_no_access_message'] ) && $element_settings['pmpro_no_access_message'] === 'yes' ) { + /** + * If the element has the no access message enabled, + * we should always show the element. + */ + return true; + } else { + /** + * If the element doesn't have the message enabled, + * we should check the visibility settings first. + */ + if( isset( $element_settings['pmpro_enable'] ) && $element_settings['pmpro_enable'] === 'yes' ) { - $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( isset( $element_settings['pmpro_content_visibility'] ) && $element_settings['pmpro_content_visibility'] === 'show' ) { + + if( isset( $element_settings['pmpro_show_content_to'] ) && $element_settings['pmpro_show_content_to'] === 'all' ) { + + $access = pmpro_hasMembershipLevel(); + + } else if( isset( $element_settings['pmpro_show_content_to'] ) && $element_settings['pmpro_show_content_to'] === 'specific' ) { + + $access = pmpro_hasMembershipLevel( $element_settings['pmpro_require_membership'] ); + + } else if( isset( $element_settings['pmpro_show_content_to'] ) && $element_settings['pmpro_show_content_to'] === 'logged_in' ) { + + $access = is_user_logged_in(); + + } + + } else if( isset( $element_settings['pmpro_content_visibility'] ) && $element_settings['pmpro_content_visibility'] === 'hide' ) { + + if( isset( $element_settings['pmpro_hide_content_from'] ) && $element_settings['pmpro_hide_content_from'] === 'all' ) { + + $access = ! pmpro_hasMembershipLevel(); + + } else if( isset( $element_settings['pmpro_hide_content_from'] ) && $element_settings['pmpro_hide_content_from'] === 'specific' ) { + + + $access = ! pmpro_hasMembershipLevel( $element_settings['pmpro_require_membership'] ); + + } else if( isset( $element_settings['pmpro_hide_content_from'] ) && $element_settings['pmpro_hide_content_from'] === 'logged_in' ) { + + $access = ! is_user_logged_in(); + + } + + } else { + $access = true; + } + } + } + + return apply_filters( 'pmpro_elementor_has_access', $access, $element, $restricted_levels ); } } From 9515bed18d91ea0ececb333d3dbb9f961ffbeb6b Mon Sep 17 00:00:00 2001 From: David Parker Date: Tue, 24 Sep 2024 11:55:30 -0400 Subject: [PATCH 2/6] Simplifying using pmpro_apply_block_visibility() --- includes/compatibility/elementor.php | 1 - ...ss-pmpro-elementor-content-restriction.php | 236 ++++-------------- 2 files changed, 52 insertions(+), 185 deletions(-) diff --git a/includes/compatibility/elementor.php b/includes/compatibility/elementor.php index 110ce666fd..9308a6ed26 100644 --- a/includes/compatibility/elementor.php +++ b/includes/compatibility/elementor.php @@ -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; } diff --git a/includes/compatibility/elementor/class-pmpro-elementor-content-restriction.php b/includes/compatibility/elementor/class-pmpro-elementor-content-restriction.php index 52fa7d99f9..e5a60dca23 100644 --- a/includes/compatibility/elementor/class-pmpro-elementor-content-restriction.php +++ b/includes/compatibility/elementor/class-pmpro-elementor-content-restriction.php @@ -25,37 +25,23 @@ protected function register_controls() { // Define controls public function add_controls( $element, $args ) { - /** - * visibilityBlockEnabled - * segment - all, specific, logged_in - * levels - * show_noaccess - */ - - $element->add_control( 'pmpro_enable', array( 'type' => \Elementor\Controls_Manager::SWITCHER, 'label' => esc_html__( 'Enable Paid Memberships Pro module visibility?', 'textdomain' ), - 'options' => array( - 'yes' => esc_html__( 'Yes', 'textdomain' ), - 'no' => esc_html__( 'No', 'textdomain' ), - - ), 'default' => 'no', - ) ); $element->add_control( - 'pmpro_content_visibility', array( + 'pmpro_invert_restrictions', array( 'type' => \Elementor\Controls_Manager::SELECT, - 'label' => esc_html__( 'Content Visbility', 'textdomain' ), 'options' => array( - 'show' => esc_html__( 'Show', 'textdomain' ), - 'hide' => esc_html__( 'Hide', 'textdomain' ), + '0' => esc_html__( 'Show content to...', 'textdomain' ), + '1' => esc_html__( 'Hide content from...', 'textdomain' ), ), - 'default' => 'show', + 'label_block' => 'true', + 'default' => '0', 'condition' => [ 'pmpro_enable' => 'yes', ], @@ -63,34 +49,16 @@ public function add_controls( $element, $args ) { ); $element->add_control( - 'pmpro_show_content_to', array( + 'pmpro_segment', array( 'type' => \Elementor\Controls_Manager::SELECT, - 'label' => esc_html__( 'Show Content To', 'textdomain' ), '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_content_visibility' => 'show', - 'pmpro_enable' => 'yes', - ], - ) - ); - - $element->add_control( - 'pmpro_hide_content_from', array( - 'type' => \Elementor\Controls_Manager::SELECT, - 'label' => esc_html__( 'Hide Content From', 'textdomain' ), - 'options' => array( - 'all' => esc_html__( 'All Members', 'textdomain' ), - 'specific' => esc_html__( 'Specific Membership Levels', 'textdomain' ), - 'logged_in' => esc_html__( 'Logged-In Users', 'textdomain' ), - ), - 'default' => 'all', - 'condition' => [ - 'pmpro_content_visibility' => 'hide', 'pmpro_enable' => 'yes', ], ) @@ -101,11 +69,9 @@ public function add_controls( $element, $args ) { 'type' => Controls_Manager::SELECT2, 'options' => pmpro_elementor_get_all_levels(), 'multiple' => 'true', - 'label_block' => 'true', - 'description' => __( 'Membership Levels', 'paid-memberships-pro' ), + 'label' => __( 'Membership Levels', 'paid-memberships-pro' ), 'condition' => [ - 'pmpro_show_content_to' => 'specific', - 'pmpro_hide_content_from' => 'specific', + 'pmpro_segment' => 'specific', 'pmpro_enable' => 'yes', ], ) @@ -117,12 +83,11 @@ 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', ], ) ); @@ -148,9 +113,22 @@ public function pmpro_elementor_should_render( $should_render, $element ) { return $should_render; } - $should_render = pmpro_elementor_has_access( $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( 'pmpro_elementor_section_access', $should_render, $element ); + return apply_filters_deprecated( 'pmpro_elementor_section_access', array( $should_render, $element ), 'TBD' ); } /** @@ -171,159 +149,49 @@ public function pmpro_elementor_render_content( $content, $widget ){ } $widget_settings = $widget->get_active_settings(); - - if( isset( $widget_settings['pmpro_enable'] ) && $widget_settings['pmpro_enable'] === 'yes' ) { - - if( isset( $widget_settings['pmpro_content_visibility'] ) && $widget_settings['pmpro_content_visibility'] === 'show' ) { - - if( isset( $widget_settings['pmpro_show_content_to'] ) && $widget_settings['pmpro_show_content_to'] === 'all' ) { - - if( pmpro_hasMembershipLevel() ) { - return $content; - } else { - if( isset( $widget_settings['pmpro_no_access_message'] ) && $widget_settings['pmpro_no_access_message'] === 'yes' ) { - return pmpro_get_no_access_message( NULL, array() ); - } else { - return ''; - } - } - - } else if( isset( $widget_settings['pmpro_show_content_to'] ) && $widget_settings['pmpro_show_content_to'] === 'specific' ) { - - if( pmpro_hasMembershipLevel( $widget_settings['pmpro_require_membership'] ) ) { - return $content; - } else { - if( isset( $widget_settings['pmpro_no_access_message'] ) && $widget_settings['pmpro_no_access_message'] === 'yes' ) { - return pmpro_get_no_access_message( NULL, $widget_settings['pmpro_require_membership'] ); - } else { - return ''; - } - } - - } else if( isset( $widget_settings['pmpro_show_content_to'] ) && $widget_settings['pmpro_show_content_to'] === 'logged_in' ) { - - if( is_user_logged_in() ) { - return $content; - } else { - if( isset( $widget_settings['pmpro_no_access_message'] ) && $widget_settings['pmpro_no_access_message'] === 'yes' ) { - return pmpro_get_no_access_message( NULL, array() ); - } else { - return ''; - } - } - - } - - } else { - - if( isset( $widget_settings['pmpro_hide_content_from'] ) && $widget_settings['pmpro_hide_content_from'] === 'all' ) { - - if( ! pmpro_hasMembershipLevel() ) { - return $content; - } else { - if( isset( $widget_settings['pmpro_no_access_message'] ) && $widget_settings['pmpro_no_access_message'] === 'yes' ) { - return pmpro_get_no_access_message( NULL, array() ); - } else { - return ''; - } - } - - } else if( isset( $widget_settings['pmpro_hide_content_from'] ) && $widget_settings['pmpro_hide_content_from'] === 'specific' ) { - - if( pmpro_hasMembershipLevel( $widget_settings['pmpro_require_membership'] ) ) { - return $content; - } else { - if( isset( $widget_settings['pmpro_no_access_message'] ) && $widget_settings['pmpro_no_access_message'] === 'yes' ) { - return pmpro_get_no_access_message( NULL, $widget_settings['pmpro_require_membership'] ); - } else { - return ''; - } - } - - } else if( isset( $widget_settings['pmpro_hide_content_from'] ) && $widget_settings['pmpro_hide_content_from'] === 'logged_in' ) { - - if( is_user_logged_in() ) { - return $content; - } else { - if( isset( $widget_settings['pmpro_no_access_message'] ) && $widget_settings['pmpro_no_access_message'] === 'yes' ) { - return pmpro_get_no_access_message( NULL, array() ); - } else { - return ''; - } - } - - } - } - + // If the block is not being restricted, bail. + if ( empty( $widget_settings['pmpro_enable'] ) || 'no' === $widget_settings['pmpro_enable'] ) { + return $content; } - 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 ); + } /** * 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(); - - if( isset( $element_settings['pmpro_no_access_message'] ) && $element_settings['pmpro_no_access_message'] === 'yes' ) { - /** - * If the element has the no access message enabled, - * we should always show the element. - */ - return true; - } else { - /** - * If the element doesn't have the message enabled, - * we should check the visibility settings first. - */ - if( isset( $element_settings['pmpro_enable'] ) && $element_settings['pmpro_enable'] === 'yes' ) { - if( isset( $element_settings['pmpro_content_visibility'] ) && $element_settings['pmpro_content_visibility'] === 'show' ) { - - if( isset( $element_settings['pmpro_show_content_to'] ) && $element_settings['pmpro_show_content_to'] === 'all' ) { - - $access = pmpro_hasMembershipLevel(); - - } else if( isset( $element_settings['pmpro_show_content_to'] ) && $element_settings['pmpro_show_content_to'] === 'specific' ) { - - $access = pmpro_hasMembershipLevel( $element_settings['pmpro_require_membership'] ); - - } else if( isset( $element_settings['pmpro_show_content_to'] ) && $element_settings['pmpro_show_content_to'] === 'logged_in' ) { - - $access = is_user_logged_in(); - - } - - } else if( isset( $element_settings['pmpro_content_visibility'] ) && $element_settings['pmpro_content_visibility'] === 'hide' ) { - - if( isset( $element_settings['pmpro_hide_content_from'] ) && $element_settings['pmpro_hide_content_from'] === 'all' ) { - - $access = ! pmpro_hasMembershipLevel(); - - } else if( isset( $element_settings['pmpro_hide_content_from'] ) && $element_settings['pmpro_hide_content_from'] === 'specific' ) { - - - $access = ! pmpro_hasMembershipLevel( $element_settings['pmpro_require_membership'] ); - - } else if( isset( $element_settings['pmpro_hide_content_from'] ) && $element_settings['pmpro_hide_content_from'] === 'logged_in' ) { - - $access = ! is_user_logged_in(); - - } - - } 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; } + + // 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, $restricted_levels ); + return apply_filters( 'pmpro_elementor_has_access', $access, $element, $element_settings['pmpro_require_membership'] ); } } From 931d6d1b2ffe6af9a68e78ef9e00d7a5056ad58b Mon Sep 17 00:00:00 2001 From: David Parker Date: Wed, 15 Jan 2025 10:18:40 -0500 Subject: [PATCH 3/6] Improved migration, adjusted new settings names --- ...ss-pmpro-elementor-content-restriction.php | 123 ++++++++++++++++-- 1 file changed, 114 insertions(+), 9 deletions(-) diff --git a/includes/compatibility/elementor/class-pmpro-elementor-content-restriction.php b/includes/compatibility/elementor/class-pmpro-elementor-content-restriction.php index e5a60dca23..623a1366a8 100644 --- a/includes/compatibility/elementor/class-pmpro-elementor-content-restriction.php +++ b/includes/compatibility/elementor/class-pmpro-elementor-content-restriction.php @@ -14,6 +14,10 @@ protected function content_restriction() { add_action( 'elementor/frontend/section/should_render', array( $this, 'pmpro_elementor_should_render' ), 10, 2 ); add_action( 'elementor/frontend/container/should_render', array( $this, 'pmpro_elementor_should_render' ), 10, 2 ); + // Migrate settings from old restriction method (pmpro_require_membership) to new method (pmpro_enable). + add_action( 'wp', array( $this, 'migrate_settings' ) ); // Migrate on frontend (also runs when editing but too late). + add_action( 'elementor/editor/before_enqueue_scripts', array( $this, 'migrate_settings' ) ); // Migrate on editor load. + } // Register controls to sections and widgets @@ -65,7 +69,7 @@ public function add_controls( $element, $args ) { ); $element->add_control( - 'pmpro_require_membership', array( + 'pmpro_levels', array( 'type' => Controls_Manager::SELECT2, 'options' => pmpro_elementor_get_all_levels(), 'multiple' => 'true', @@ -80,7 +84,7 @@ public function add_controls( $element, $args ) { // Only add this option to Widgets as we can replace the contents in widgets, not sections. if ( 'widget' === $element->get_type() ) { $element->add_control( - 'pmpro_no_access_message', array( + 'pmpro_show_noaccess', array( 'label' => esc_html__( 'Show no access message', 'paid-memberships-pro' ), 'type' => \Elementor\Controls_Manager::SWITCHER, 'return_value' => 'yes', @@ -122,9 +126,9 @@ public function pmpro_elementor_should_render( $should_render, $element ) { $apply_block_visibility_params = array( 'segment' => $element_settings['pmpro_segment'], - 'levels' => $element_settings['pmpro_require_membership'], + 'levels' => $element_settings['pmpro_levels'], 'invert_restrictions' => $element_settings['pmpro_invert_restrictions'], - 'show_noaccess' => $element_settings['pmpro_no_access_message'], + 'show_noaccess' => ! empty( $element_settings['pmpro_show_noaccess'] ), ); $should_render = ! empty( pmpro_apply_block_visibility( $apply_block_visibility_params, 'sample content' ) ); @@ -158,9 +162,9 @@ public function pmpro_elementor_render_content( $content, $widget ){ // 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'], + 'levels' => $widget_settings['pmpro_levels'], 'invert_restrictions' => $widget_settings['pmpro_invert_restrictions'], - 'show_noaccess' => $widget_settings['pmpro_no_access_message'], + 'show_noaccess' => $widget_settings['pmpro_show_noaccess'], ); return pmpro_apply_block_visibility( $apply_block_visibility_params, $content ); @@ -185,13 +189,114 @@ public function pmpro_elementor_has_access( $element ) { // 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'], + 'levels' => $element_settings['pmpro_levels'], 'invert_restrictions' => $element_settings['pmpro_invert_restrictions'], - 'show_noaccess' => $element_settings['pmpro_no_access_message'], + 'show_noaccess' => $element_settings['pmpro_show_noaccess'], ); $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'] ); + return apply_filters( 'pmpro_elementor_has_access', $access, $element, $element_settings['pmpro_levels'] ); + } + + /** + * Migrate settings from old restriction method (pmpro_require_membership) to new method (pmpro_enable). + * + * @since TBD + */ + public function migrate_settings() { + // Get the post being viewed. + $post_id = get_the_ID(); + if ( empty( $post_id ) ) { + return; + } + + // Get the _elementor_data for the post. + $elementor_data_string = get_post_meta( $post_id, '_elementor_data', true ); + if ( empty( $elementor_data_string ) ) { + return; + } + + // Decode the _elementor_data. + $elementor_data = json_decode( $elementor_data_string, true ); + if ( empty( $elementor_data ) ) { + return; + } + + // Track if we've migrated. + $migrated = false; + + // Get the container settings. + $container_settings = $elementor_data[0]['settings']; + if ( isset( $container_settings['pmpro_require_membership'] ) ) { + // Migrate the settings. + $elementor_data[0]['settings'] = $this->migrate_settings_helper( $container_settings ); + $migrated = true; + } + + // Loop through each element and migrate the settings. + foreach ( $elementor_data[0]['elements'] as $key => $element ) { + // Get the element settings. + $element_settings = $element['settings']; + + // If the element has the old pmpro_require_membership setting, migrate it. + if ( isset( $element_settings['pmpro_require_membership'] ) ) { + $elementor_data[0]['elements'][ $key ]['settings'] = $this->migrate_settings_helper( $element_settings ); + $migrated = true; + } + } + + // If we've migrated, save the updated _elementor_data. + if ( $migrated ) { + update_post_meta( $post_id, '_elementor_data', wp_json_encode( $elementor_data ) ); + } + } + + /** + * Heloper method for migrating settings from old restriction method (pmpro_require_membership) to new method (pmpro_enable). + * + * @since TBD + * + * @param array $settings The settings array to migrate. + * @return array The migrated settings array. + */ + private function migrate_settings_helper( $settings ) { + // If pmpro_require_membership is not set, bail. + if ( ! isset( $settings['pmpro_require_membership'] ) ) { + return $settings; + } + + // Figure out how to migrate the settings. + $settings['pmpro_enable'] = 'yes'; + if ( ! in_array( '0', $settings['pmpro_require_membership'] ) ) { + // If '0' is not in the array, then we can copy the pmpro_require_membership value and use "restrict to specific levels". + $settings['pmpro_levels'] = $settings['pmpro_require_membership']; + $settings['pmpro_segment'] = 'specific'; + $settings['pmpro_invert_restrictions'] = '0'; + $settings['show_noaccess'] = empty( $settings['pmpro_show_noaccess'] ) ? 'no' : 'yes'; + } elseif ( 1 === count( $settings['pmpro_require_membership'] ) ) { + // '0' is the only value in the array. This means that we should restrict to non-members. + $settings['pmpro_levels'] = array(); + $settings['pmpro_segment'] = 'all'; + $settings['pmpro_invert_restrictions'] = '1'; + $settings['show_noaccess'] = 'no'; + } else { + // '0' is in the array, but there are other values. This means that we need to block access to all levels that are not in the array. + // First, get all PMPro level IDs. + $all_levels = pmpro_getAllLevels( true, false ); + $all_levels_ids = wp_list_pluck( $all_levels, 'id' ); + + // Get the levels that are not in the pmpro_require_membership array. + $settings['pmpro_levels'] = array_diff( $all_levels_ids, $settings['pmpro_require_membership'] ); + $settings['pmpro_segment'] = 'specific'; + $settings['pmpro_invert_restrictions'] = '1'; + $settings['show_noaccess'] = 'no'; + } + + // Remove the old pmpro_require_membership setting. + unset( $settings['pmpro_require_membership'] ); + unset( $settings['pmpro_show_noaccess'] ); + + return $settings; } } From 069b65177ec647ff5976d4c2297bc1e25e55c93e Mon Sep 17 00:00:00 2001 From: David Parker Date: Wed, 15 Jan 2025 10:49:14 -0500 Subject: [PATCH 4/6] Fixing no access message migration --- .../elementor/class-pmpro-elementor-content-restriction.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/includes/compatibility/elementor/class-pmpro-elementor-content-restriction.php b/includes/compatibility/elementor/class-pmpro-elementor-content-restriction.php index 623a1366a8..1127af1344 100644 --- a/includes/compatibility/elementor/class-pmpro-elementor-content-restriction.php +++ b/includes/compatibility/elementor/class-pmpro-elementor-content-restriction.php @@ -272,7 +272,7 @@ private function migrate_settings_helper( $settings ) { $settings['pmpro_levels'] = $settings['pmpro_require_membership']; $settings['pmpro_segment'] = 'specific'; $settings['pmpro_invert_restrictions'] = '0'; - $settings['show_noaccess'] = empty( $settings['pmpro_show_noaccess'] ) ? 'no' : 'yes'; + $settings['show_noaccess'] = empty( $settings['pmpro_no_access_message'] ) ? 'no' : $settings['pmpro_no_access_message']; } elseif ( 1 === count( $settings['pmpro_require_membership'] ) ) { // '0' is the only value in the array. This means that we should restrict to non-members. $settings['pmpro_levels'] = array(); @@ -286,7 +286,7 @@ private function migrate_settings_helper( $settings ) { $all_levels_ids = wp_list_pluck( $all_levels, 'id' ); // Get the levels that are not in the pmpro_require_membership array. - $settings['pmpro_levels'] = array_diff( $all_levels_ids, $settings['pmpro_require_membership'] ); + $settings['pmpro_levels'] = array_values( array_diff( $all_levels_ids, $settings['pmpro_require_membership'] ) ); $settings['pmpro_segment'] = 'specific'; $settings['pmpro_invert_restrictions'] = '1'; $settings['show_noaccess'] = 'no'; @@ -294,7 +294,7 @@ private function migrate_settings_helper( $settings ) { // Remove the old pmpro_require_membership setting. unset( $settings['pmpro_require_membership'] ); - unset( $settings['pmpro_show_noaccess'] ); + unset( $settings['pmpro_no_access_message'] ); return $settings; } From d1e0ba4c3cb6bc2b64d53ebfa23277b57bfae694 Mon Sep 17 00:00:00 2001 From: David Parker Date: Wed, 15 Jan 2025 11:03:44 -0500 Subject: [PATCH 5/6] Fixing show no access message migration again --- .../elementor/class-pmpro-elementor-content-restriction.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/includes/compatibility/elementor/class-pmpro-elementor-content-restriction.php b/includes/compatibility/elementor/class-pmpro-elementor-content-restriction.php index 1127af1344..9285cf0269 100644 --- a/includes/compatibility/elementor/class-pmpro-elementor-content-restriction.php +++ b/includes/compatibility/elementor/class-pmpro-elementor-content-restriction.php @@ -272,13 +272,13 @@ private function migrate_settings_helper( $settings ) { $settings['pmpro_levels'] = $settings['pmpro_require_membership']; $settings['pmpro_segment'] = 'specific'; $settings['pmpro_invert_restrictions'] = '0'; - $settings['show_noaccess'] = empty( $settings['pmpro_no_access_message'] ) ? 'no' : $settings['pmpro_no_access_message']; + $settings['pmpro_show_noaccess'] = empty( $settings['pmpro_no_access_message'] ) ? 'no' : $settings['pmpro_no_access_message']; } elseif ( 1 === count( $settings['pmpro_require_membership'] ) ) { // '0' is the only value in the array. This means that we should restrict to non-members. $settings['pmpro_levels'] = array(); $settings['pmpro_segment'] = 'all'; $settings['pmpro_invert_restrictions'] = '1'; - $settings['show_noaccess'] = 'no'; + $settings['pmpro_show_noaccess'] = 'no'; } else { // '0' is in the array, but there are other values. This means that we need to block access to all levels that are not in the array. // First, get all PMPro level IDs. @@ -289,7 +289,7 @@ private function migrate_settings_helper( $settings ) { $settings['pmpro_levels'] = array_values( array_diff( $all_levels_ids, $settings['pmpro_require_membership'] ) ); $settings['pmpro_segment'] = 'specific'; $settings['pmpro_invert_restrictions'] = '1'; - $settings['show_noaccess'] = 'no'; + $settings['pmpro_show_noaccess'] = 'no'; } // Remove the old pmpro_require_membership setting. From 54677e38fd5b7b4a852d347c5086c77ae59c2e1a Mon Sep 17 00:00:00 2001 From: David Parker Date: Fri, 17 Jan 2025 10:06:25 -0500 Subject: [PATCH 6/6] Using new cache variable for 3.4 (removed non-member option) --- includes/compatibility/elementor.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/includes/compatibility/elementor.php b/includes/compatibility/elementor.php index 9308a6ed26..9b87c75add 100644 --- a/includes/compatibility/elementor.php +++ b/includes/compatibility/elementor.php @@ -26,7 +26,7 @@ function pmpro_elementor_compatibility() { */ function pmpro_elementor_get_all_levels() { - $levels_array = get_transient( 'pmpro_elementor_levels_cache' ); + $levels_array = get_transient( 'pmpro_elementor_levels_cache_3_4' ); if ( empty( $levels_array ) ) { $all_levels = pmpro_getAllLevels( true, false ); @@ -37,7 +37,7 @@ function pmpro_elementor_get_all_levels() { $levels_array[ $level->id ] = $level->name; } - set_transient( 'pmpro_elementor_levels_cache', $levels_array, 1 * DAY_IN_SECONDS ); + set_transient( 'pmpro_elementor_levels_cache_3_4', $levels_array, 1 * DAY_IN_SECONDS ); } $levels_array = apply_filters( 'pmpro_elementor_levels_array', $levels_array ); @@ -50,5 +50,6 @@ function pmpro_elementor_get_all_levels() { function pmpro_elementor_clear_level_cache( $level_id ) { delete_transient( 'pmpro_elementor_levels_cache' ); + delete_transient( 'pmpro_elementor_levels_cache_3_4' ); } add_action( 'pmpro_save_membership_level', 'pmpro_elementor_clear_level_cache' );