conditions = apply_filters( 'advanced-ads-visitor-conditions', array( 'mobile' => array( // type of the condition 'label' => __( 'device', 'advanced-ads' ), 'description' => __( 'Display ads only on mobile devices or hide them.', 'advanced-ads' ), 'metabox' => array( 'Advanced_Ads_Visitor_Conditions', 'mobile_is_or_not' ), // callback to generate the metabox 'check' => array( 'Advanced_Ads_Visitor_Conditions', 'check_mobile' ), // callback for frontend check 'helplink' => ADVADS_URL . 'manual/display-ads-either-on-mobile-or-desktop/#utm_source=advanced-ads&utm_medium=link&utm_campaign=edit-visitor-mobile' // link to help section ), 'loggedin' => array( 'label' => __( 'logged in visitor', 'advanced-ads' ), 'description' => __( 'Whether the visitor has to be logged in or not in order to see the ads.', 'advanced-ads' ), 'metabox' => array( 'Advanced_Ads_Visitor_Conditions', 'metabox_is_or_not' ), // callback to generate the metabox 'check' => array( 'Advanced_Ads_Visitor_Conditions', 'check_logged_in' ) // callback for frontend check ), )); ksort( $this->conditions ); } /** * * @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 render the mobile condition using the "is not" condition * * @param arr $options options of the condition * @param int $index index of the condition */ static function mobile_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'; ?>

' . sprintf(__( 'Display ads by the available space on the device or target tablets with the Responsive add-on', 'advanced-ads' ), ADVADS_URL . 'add-ons/responsive-ads/#utm_source=advanced-ads&utm_medium=link&utm_campaign=edit-visitor-responsive') . '

'; } } /** * 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'; ?>

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'; ?>

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

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, $ad ); } return true; } /** * render connector option * * @since 1.7.0.4 * @param int $index */ static function render_connector_option( $index = 0, $value = 'or' ){ $label = ( $value === 'or' ) ? __( 'or', 'advanced-ads' ) : __( 'and', 'advanced-ads' ); return ''; } /** * 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; } /** * check mobile visitor condition in frontend * * @since 1.6.3 * @param arr $options options of the condition * @return bool true if can be displayed */ static function check_logged_in( $options = array() ){ if ( ! isset( $options['operator'] ) ) { return true; } switch ( $options['operator'] ){ case 'is' : if ( ! is_user_logged_in() ) { return false; } break; case 'is_not' : if ( is_user_logged_in() ) { return false; } break; } return true; } /** * helper for check with strings * * @since 1.6.3 * @param str $string string that is going to be checked * @return bool true if ad can be displayed */ static function helper_check_string( $string = '', $options = array() ){ if ( ! isset( $options['operator'] ) || ! isset( $options['value'] ) || '' === $options['value'] ){ return true; } $operator = $options['operator']; $value = $options['value']; // check the condition by mode and bool $condition = true; switch ( $operator ){ // referrer contains string on any position case 'contain' : $condition = stripos( $string, $value ) !== false; break; // referrer does not contain string on any position case 'contain_not' : $condition = stripos( $string, $value ) === false; break; // referrer starts with the string case 'start' : $condition = stripos( $string, $value ) === 0; break; // referrer does not start with the string case 'start_not' : $condition = stripos( $string, $value ) !== 0; break; // referrer ends with the string case 'end' : $condition = $value === substr( $string, -strlen( $value ) ); break; // referrer does not end with the string case 'end_not' : $condition = $value !== substr( $string, -strlen( $value ) ); break; // referrer is equal to the string case 'match' : // strings do match, but should not or not match but should $condition = strcasecmp($value, $string) === 0; break; // referrer is not equal to the string case 'match_not' : // strings do match, but should not or not match but should $condition = strcasecmp($value, $string) !== 0; break; // string is a regular expression case 'regex' : // check regular expression first if( @preg_match( $value, null ) === false ){ Advanced_Ads::log( "Advanced Ads: regular expression '$value' in visitor condition is broken." ); } else { $condition = preg_match( $value, $string ); } break; // string is not a regular expression case 'regex_not' : if( @preg_match( $value, null ) === false ){ Advanced_Ads::log( "Advanced Ads: regular expression '$value' in visitor condition is broken." ); } else { $condition = ! preg_match( $value, $string ); } break; } return $condition; } }