Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MilanSavaliya/d90904ff35e04c00031fc796b47a71ad to your computer and use it in GitHub Desktop.
Save MilanSavaliya/d90904ff35e04c00031fc796b47a71ad to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: Custom Protect Content Shortcode for Membership 2 Pro
Plugin URI: https://gist.github.com/MilanSavaliya/d90904ff35e04c00031fc796b47a71ad
Description: This plugin provides custom shortcode for displaying protection message to member who belongs to multiple membership( In respective to AND Condition )
Version: 1.0
Author: Milan Savaliya
License: GPLv2 or later
Text Domain: membership2
*/
if( !class_exists( 'Wpmu_Membership2_Implementation' ) ){
class Wpmu_Membership2_Implementation {
protected $api = null;
// Function is always executed. Will create 1 Implementation object.
static public function setup() {
static $Inst = null;
if ( null === $Inst ) {
$Inst = new Wpmu_Membership2_Implementation();
}
}
// Function set up the api hook.
private function __construct() {
add_action( 'ms_init', array( $this, 'init' ) );
}
// Function is only run when Membership2 is present + active.
public function init( $api ) {
$this->api = $api;
// The main init code should come here now!
add_shortcode( 'wpmu-custom-ms-protect-content', array( $this, 'wpmu_custom_ms_protect_content' ) );
}
public function wpmu_custom_ms_protect_content( $atts, $content = null ){
$atts = apply_filters(
'ms_model_shortcode_protect_content_shortcode_atts',
shortcode_atts(
array(
'id' => '',
'access' => true,
'silent' => false,
'msg' => false,
),
$atts
)
);
extract( $atts );
$membership_ids = explode( ',', $id );
$isMemberBelongsToAllSuppliedMemberships = $this->is_member_of( $membership_ids );
if ( $silent ) {
$msg = '';
}
$access = lib3()->is_true( $access );
if ( ! $access ) {
// No access to member of membership_ids
if ( $isMemberBelongsToAllSuppliedMemberships ) {
// User belongs to these memberships and therefore cannot see
// this content...
if ( $silent ) {
// Silent protection: Do not show a message, simply hide it
$content = '';
} else {
$content = '<div class="ms-protection-msg">';
if ( ! empty( $msg ) ) {
$content .= $msg;
} else {
$membership_names = MS_Model_Membership::get_membership_names(
array( 'post__in' => $membership_ids )
);
$content .= __( 'No access to members of: ', 'membership2' );
$content .= implode( ', ', $membership_names );
}
$content .= '</div>';
}
}
} else {
// Give access to member of membership_ids
if ( !$isMemberBelongsToAllSuppliedMemberships ) {
// User does not belong to these memberships and therefore
// cannot see this content...
if ( $silent ) {
// Silent protection: Do not show a message, simply hide it
$content = '';
} else {
$content = '<div class="ms-protection-msg">';
if ( ! empty( $msg ) ) {
$content .= $msg;
} else {
$membership_names = MS_Model_Membership::get_membership_names(
array( 'post__in' => $membership_ids )
);
$content .= __( 'Content protected to members of: ', 'membership2' );
$content .= implode( ', ', $membership_names );
}
$content .= '</div>';
}
}
}
return apply_filters(
'ms_rule_shortcode_model_protect_content_shortcode_content',
do_shortcode( $content ),
$atts,
$content,
''
);
}
public function is_member_of( $ids ) {
if( !is_array( $ids ) ) {
ms_has_membership( $ids );
}
foreach ( $ids as $id ){
if( ms_has_membership( $id ) == false ){
return false;
}
}
return true;
}
// Add other event handlers and helper functions.
// You can use $this->api in other functions to access the API object.
}
Wpmu_Membership2_Implementation::setup();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment