__( 'PMP page level', 'advanced-ads-pro' ), 'description' => __( 'Display ads only on pages that need a specific membership level set with PaidMembershipsPro.', 'advanced-ads-pro' ), 'metabox' => array( 'Advanced_Ads_Pro_Module_PaidMembershipsPro', 'metabox_pmp_membership_level_display_condition' ), // callback to generate the metabox 'check' => array( 'Advanced_Ads_Pro_Module_PaidMembershipsPro', 'check_pmp_membership_level_display_condition' ) // callback for frontend check ); } return $conditions; } /** * check PMP membership level condition in frontend * * @since 1.14 * @param arr $options options of the condition * @param obj $ad Advanced_Ads_Ad * @return bool true if can be displayed */ static function check_pmp_membership_level_display_condition($options = array(), Advanced_Ads_Ad $ad) { if (!isset($options['value']) || !is_array($options['value'])) { return false; } if (isset($options['operator']) && $options['operator'] === 'is_not') { $operator = 'is_not'; } else { $operator = 'is'; } global $wpdb; $ad_options = $ad->options(); $post_id = isset($ad_options['post']['id']) ? $ad_options['post']['id'] : null; if( $post_id && defined( 'PMPRO_VERSION' ) ){ // get membership level of current page $page_levels = $wpdb->get_col("SELECT membership_id FROM {$wpdb->pmpro_memberships_pages} WHERE page_id = '{$post_id}'"); if ( is_array( $page_levels ) ){ switch( $operator ){ case 'is_not' : return ! count( array_intersect( $options['value'], $page_levels ) ); break; default : return count( array_intersect( $options['value'], $page_levels ) ); } } } return true; } /** * callback to display the PMP membership level condition * * @since 1.14 * @param arr $options options of the condition * @param int $index index of the condition */ static function metabox_pmp_membership_level_display_condition( $options, $index = 0, $form_name = '' ){ global $wpdb; if ( ! isset ( $options['type'] ) || '' === $options['type'] ) { return; } $type_options = Advanced_Ads_Display_Conditions::get_instance()->conditions; if (!isset($type_options[$options['type']])) { return; } // form name basis $name = Advanced_Ads_Display_Conditions::get_form_name_with_index( $form_name, $index ); // options $values = ( isset($options['value']) && is_array($options['value']) ) ? $options['value'] : array(); $operator = ( isset($options['operator']) && $options['operator'] === 'is_not' ) ? 'is_not' : 'is'; // get values and select operator based on previous settings ?> get_results( "SELECT * FROM {$wpdb->pmpro_membership_levels}", OBJECT ); ?>
__( 'PMP user level', 'advanced-ads-pro' ), 'description' => __( 'Display ads only to users with a specific membership level set with PaidMembershipsPro.', 'advanced-ads-pro' ), 'metabox' => array( 'Advanced_Ads_Pro_Module_PaidMembershipsPro', 'metabox_pmp_membership_level_visitor_condition' ), // callback to generate the metabox 'check' => array( 'Advanced_Ads_Pro_Module_PaidMembershipsPro', 'check_pmp_membership_level_visitor_condition' ) // callback for frontend check ); } return $conditions; } /** * check PMP membership level visitor condition in frontend * * @since 1.14 * @param arr $options options of the condition * @param obj $ad Advanced_Ads_Ad * @return bool true if can be displayed */ static function check_pmp_membership_level_visitor_condition($options = array(), Advanced_Ads_Ad $ad) { if (!isset($options['value']) || ! is_array($options['value'])) { return false; } if (isset($options['operator']) && $options['operator'] === 'is_not') { $operator = 'is_not'; } else { $operator = 'is'; } if( defined( 'PMPRO_VERSION' ) && function_exists( 'pmpro_getMembershipLevelForUser' ) ){ // get membership level of current user $current_user = wp_get_current_user(); $membership_level = pmpro_getMembershipLevelForUser( $current_user->ID ); if ( isset( $membership_level->ID ) ){ switch( $operator ){ case 'is_not' : return ! in_array( $membership_level->ID, $options['value'] ); break; default : return in_array( $membership_level->ID, $options['value'] ); } } elseif( ! isset( $membership_level->ID ) && count( $options['value'] ) ){ // return false, if ad needs a membership level, but user has none return $operator === 'is_not'; } } return true; } /** * callback to display the PMP membership level visitor condition * * @since 1.14 * @param arr $options options of the condition * @param int $index index of the condition */ static function metabox_pmp_membership_level_visitor_condition( $options, $index = 0, $form_name = '' ){ global $wpdb; if ( ! isset ( $options['type'] ) || '' === $options['type'] ) { return; } $type_options = Advanced_Ads_Visitor_Conditions::get_instance()->conditions; if (!isset($type_options[$options['type']])) { return; } // form name basis $name = Advanced_Ads_Pro_Module_Advanced_Visitor_Conditions::get_form_name_with_index( $form_name, $index ); // options $values = ( isset($options['value']) && is_array($options['value']) ) ? $options['value'] : array(); $operator = ( isset($options['operator']) && $options['operator'] === 'is_not' ) ? 'is_not' : 'is'; // get values and select operator based on previous settings ?> get_results( "SELECT * FROM {$wpdb->pmpro_membership_levels}", OBJECT ); $rand = md5( $form_name ); ?>