conditions = apply_filters( 'advanced-ads-visitor-conditions', array( 'mobile' => array( // type of the condition 'label' => __( 'mobile device', ADVADS_SLUG ), 'description' => __( 'Display ads only on mobile devices or hide them.', ADVADS_SLUG ), 'metabox' => array( 'Advanced_Ads_Visitor_Conditions', 'metabox_is_or_not' ), // callback to generate the metabox 'check' => array( 'Advanced_Ads_Visitor_Conditions', 'check_mobile' ) // callback for frontend check ), )); } /** * * @return Advanced_Ads_Plugin */ public static function get_instance() { // If the single instance hasn't been set, set it now. if ( null === self::$instance ) { self::$instance = new self; } return self::$instance; } /** * callback to display the "is not" condition * * @param arr $options options of the condition * @param int $index index of the condition */ static function metabox_is_or_not( $options, $index = 0 ){ if ( ! isset ( $options['type'] ) || '' === $options['type'] ) { return; } $type_options = self::get_instance()->conditions; if ( ! isset( $type_options[ $options['type'] ] ) ) { return; } // form name basis $name = self::FORM_NAME . '[' . $index . ']'; // options $operator = isset( $options['operator'] ) ? $options['operator'] : 'is'; $connector = isset( $options['connector'] ) ? $options['connector'] : 'and'; ?>

conditions; if ( ! isset( $type_options[ $options['type'] ] ) ) { return; } // form name basis $name = self::FORM_NAME . '[' . $index . ']'; // options $value = isset( $options['value'] ) ? $options['value'] : 0; $operator = isset( $options['operator'] ) ? $options['operator'] : 'is_equal'; $connector = isset( $options['connector'] ) ? $options['connector'] : 'and'; ?>

conditions; if ( is_array( $options ) && isset( $visitor_conditions[ $options['type'] ]['check'] ) ) { $check = $visitor_conditions[ $options['type'] ]['check']; } else { return true; } // call frontend check callback if ( method_exists( $check[0], $check[1] ) ) { return call_user_func( array( $check[0], $check[1] ), $options ); } return true; } /** * check mobile visitor condition in frontend * * @param arr $options options of the condition * @return bool true if can be displayed */ static function check_mobile( $options = array() ){ if ( ! isset( $options['operator'] ) ) { return true; } switch ( $options['operator'] ){ case 'is' : if ( ! wp_is_mobile() ) { return false; } break; case 'is_not' : if ( wp_is_mobile() ) { return false; } break; } return true; } }