[go: nahoru, domu]

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

POC: WPBakery Compatibility #2585

Open
wants to merge 6 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions includes/compatibility.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ function pmpro_compatibility_checker() {
'check_type' => 'constant',
'check_value' => 'BLUEHOST_PLUGIN_VERSION',
],
[
'file' => 'wp-bakery.php',
'check_type' => 'function',
'check_value' => 'vc_add_param'
],
];

foreach ( $compat_checks as $value ) {
Expand Down
90 changes: 90 additions & 0 deletions includes/compatibility/wp-bakery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php
/**
* WP Bakery Compatibility
*
* @since TBD
*/

/**
* Set the VC directory to our compat folder
*
*/
vc_set_shortcodes_templates_dir( PMPRO_DIR . '/includes/compatibility/wp-bakery' );

/**
* Create a text field that allows you to enter the level IDs that can see this content.
*/
function pmpro_wpbakery_levels_parameter() {
return array(
'type' => 'textfield',
'heading' => 'Paid Memberships Pro - Require Membership Level',
'param_name' => 'pmpro_levels',
'value' => '',
'description' => __( 'Enter comma-separated level IDs. Set to "0" to show this content for non-members.', 'paid-memberships-pro' ),
);
}

function pmpro_wpbakery_show_noaccess_message() {
return array(
'type' => 'dropdown',
'heading' => 'Paid Memberships Pro - Show No Access Message',
'param_name' => 'pmpro_no_access_message',
'value' => array( 'Yes', 'No' ), //Does not accept an associative array, cannot be translated.
'description' => __( 'Displays a no access message to non-members.', 'paid-memberships-pro' ),
);
}

/**
* To add the parameter to additional fields, reference:
* vc_add_param( 'field_type', pmpro_wpbakery_levels_parameter() );
*/
vc_add_param( 'vc_column_text', pmpro_wpbakery_levels_parameter() ); //Text Field
vc_add_param( 'vc_column_text', pmpro_wpbakery_show_noaccess_message() ); //Text Field

/**
* Determines if content should be shown or not
*
* @since TBD
*/
function pmpro_wpbakery_show_content( $output, $atts ) {

// Don't run if we're in the VC editor.
if ( vc_is_inline() ) {
return $output;
}

// Return content if no levels are set.
if ( empty( $atts['pmpro_levels'] ) ) {
return $output;
}

// Get the levels that can see this content.
$restricted_levels = $atts['pmpro_levels'];
$levels_array = explode( ",", $restricted_levels );

// Get the show no access message setting.
$show_no_access_message = ( ! empty( $atts['pmpro_no_access_message'] ) ) ? $atts['pmpro_no_access_message'] : 'Yes';

// Check if the current user has access to this content.
if ( ! pmpro_hasMembershipLevel( $levels_array ) ) {
$access = false;
} else {
$access = true;
}

// Allow filtering of the access.
$access = apply_filters( 'pmpro_wpbakery_has_access', $access, $output, $levels_array, $atts );

// Show the content or not.
if ( ! $access ) {
// User doesn't have access. Don't show the content.
// Optionally show the no access message.
if ( $show_no_access_message === 'Yes' ) {
return pmpro_get_no_access_message( NULL, $levels_array );
}
} else {
// User has access. Show the content.
return $output;
}

}
42 changes: 42 additions & 0 deletions includes/compatibility/wp-bakery/vc_column_text.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
die( '-1' );
}

/**
* Shortcode attributes
* @var $atts
* @var $el_class
* @var $el_id
* @var $css_animation
* @var $css
* @var $content - shortcode content
* Shortcode class
* @var WPBakeryShortCode_Vc_Column_text $this
*/
$el_class = $el_id = $css = $css_animation = '';
$atts = vc_map_get_attributes( $this->getShortcode(), $atts );
extract( $atts );

$class_to_filter = 'wpb_text_column wpb_content_element ' . $this->getCSSAnimation( $css_animation );
$class_to_filter .= vc_shortcode_custom_css_class( $css, ' ' ) . $this->getExtraClass( $el_class );
$css_class = apply_filters( VC_SHORTCODE_CUSTOM_CSS_FILTER_TAG, $class_to_filter, $this->settings['base'], $atts );
$wrapper_attributes = array();
if ( ! empty( $el_id ) ) {
$wrapper_attributes[] = 'id="' . esc_attr( $el_id ) . '"';
}
$output = '
<div class="' . esc_attr( $css_class ) . '" ' . implode( ' ', $wrapper_attributes ) . '>
<div class="wpb_wrapper">
' . wpb_js_remove_wpautop( $content, true ) . '
</div>
</div>
';

/**
* Paid Memberships Pro will check to see if any levels are required, and show/hide the content as expected.
*/
if ( ! empty( $atts['pmpro_levels'] ) ) {
$output = pmpro_wpbakery_show_content( $output, $atts );
}
echo $output;