array(
'label' => __( 'post type', 'advanced-ads' ),
'description' => __( 'Choose the public post types on which to display the ad.', 'advanced-ads' ),
'metabox' => array( 'Advanced_Ads_Display_Conditions', 'metabox_post_type' ), // callback to generate the metabox.
'check' => array( 'Advanced_Ads_Display_Conditions', 'check_post_type' ), // 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
),
// post id condition.
'postids' => array(
'label' => __( 'specific pages', 'advanced-ads' ),
'description' => __( 'Choose on which individual posts, pages and public post type pages you want to display or hide ads.', 'advanced-ads' ),
'metabox' => array( 'Advanced_Ads_Display_Conditions', 'metabox_post_ids' ), // callback to generate the metabox.
'check' => array( 'Advanced_Ads_Display_Conditions', 'check_post_ids' ), // callback for frontend check.
),
// general conditions.
'general' => array(
'label' => __( 'general conditions', 'advanced-ads' ),
// 'description' => __( 'Choose on which individual posts, pages and public post type pages you want to display or hide ads.', 'advanced-ads' ),
'metabox' => array( 'Advanced_Ads_Display_Conditions', 'metabox_general' ), // callback to generate the metabox.
'check' => array( 'Advanced_Ads_Display_Conditions', 'check_general' ), // callback for frontend check.
),
// author conditions.
'author' => array(
'label' => __( 'author', 'advanced-ads' ),
// 'description' => __( 'Choose on which individual posts, pages and public post type pages you want to display or hide ads.', 'advanced-ads' ),
'metabox' => array( 'Advanced_Ads_Display_Conditions', 'metabox_author' ), // callback to generate the metabox.
'check' => array( 'Advanced_Ads_Display_Conditions', 'check_author' ), // callback for frontend check.
),
// display ads only in content older or younger than a specific age.
'content_age' => array(
'label' => __( 'content age', 'advanced-ads' ),
'description' => __( 'Display ads based on age of the page.', 'advanced-ads' ),
'metabox' => array( 'Advanced_Ads_Display_Conditions', 'metabox_content_age' ), // callback to generate the metabox.
'check' => array( 'Advanced_Ads_Display_Conditions', 'check_content_age' ), // callback for frontend check.
),
// condition for taxonomies in general.
'taxonomy' => array(
'label' => __( 'taxonomy', 'advanced-ads' ),
'description' => __( 'Display ads based on the taxonomy of an archive page.', 'advanced-ads' ),
'metabox' => array( 'Advanced_Ads_Display_Conditions', 'metabox_taxonomies' ), // callback to generate the metabox.
'check' => array( 'Advanced_Ads_Display_Conditions', 'check_taxonomy' ), // callback for frontend check.
),
);
// register a condition for each taxonomy for posts.
$taxonomies = get_taxonomies( array( 'public' => true, 'publicly_queryable' => true ), 'objects', 'or' );
$tax_label_counts = array();
foreach ( $taxonomies as $_tax ) :
if ( in_array( $_tax->name, array( 'advanced_ads_groups' ), true ) ) {
continue;
}
/**
* Count names of taxonomies and adjust label if there are duplicates.
* we can’t use `array_count_values` here because "label" might not always be a simple string (though it should be)
*/
$tax_label_counts[ $_tax->label ] = isset( $tax_label_counts[ $_tax->label ] ) ? $tax_label_counts[ $_tax->label ] + 1 : $tax_label_counts[ $_tax->label ] = 1;
// add tax type to label if we find it multiple times.
if ( $tax_label_counts[ $_tax->label ] < 2 ) {
$label = $_tax->label;
$archive_label = $_tax->labels->singular_name;
} else {
$label = sprintf( '%s (%s)', $_tax->label, $_tax->name );
$archive_label = sprintf( '%s (%s)', $_tax->labels->singular_name, $_tax->name );
}
$conditions[ 'taxonomy_' . $_tax->name ] = array(
'label' => $label,
// 'description' => sprintf(__( 'Choose terms from the %s taxonomy a post must belong to for showing or hiding ads.', 'advanced-ads' ), $_tax->label ),
'metabox' => array( 'Advanced_Ads_Display_Conditions', 'metabox_taxonomy_terms' ), // callback to generate the metabox
'check' => array( 'Advanced_Ads_Display_Conditions', 'check_taxonomies' ), // callback for frontend check
'taxonomy' => $_tax->name, // unique for this type: the taxonomy name
);
$conditions[ 'archive_' . $_tax->name ] = array(
'label' => sprintf( __( 'archive: %s', 'advanced-ads' ), $archive_label ),
// 'description' => sprintf(__( 'Choose on which %s archive page ads are hidden or displayeds.', 'advanced-ads' ), $_tax->label ),
'metabox' => array( 'Advanced_Ads_Display_Conditions', 'metabox_taxonomy_terms' ), // callback to generate the metabox.
'check' => array( 'Advanced_Ads_Display_Conditions', 'check_taxonomy_archive' ), // callback for frontend check.
'taxonomy' => $_tax->name, // unique for this type: the taxonomy name.
);
endforeach;
$this->conditions = apply_filters( 'advanced-ads-display-conditions', $conditions );
}
/**
* Instance of Advanced_Ads_Plugin
*
* @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;
}
/**
* Get the conditions array alphabetically by label
*
* @since 1.8.12
*/
public function get_conditions() {
uasort( $this->conditions, 'Advanced_Ads_Admin::sort_condition_array_by_label' );
return $this->conditions;
}
/**
* Controls frontend checks for conditions
*
* @param array $options options of the condition.
* @param Advanced_Ads_Ad $ad ad.
*
* @return bool false, if ad can’t be delivered
*/
public static function frontend_check( $options = array(), $ad = false ) {
$display_conditions = self::get_instance()->conditions;
if ( is_array( $options ) && isset( $options['type'] ) && isset( $display_conditions[ $options['type'] ]['check'] ) ) {
$check = $display_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 the list of set display conditions
*
* @param array $set_conditions array of existing conditions.
* @param string $list_target ID of the list with the conditions.
* @param string $form_name prefix of the form field name attribute.
*/
public static function render_condition_list( array $set_conditions, $list_target = '', $form_name = '' ) {
$conditions = self::get_instance()->get_conditions();
// use default form name if not given explicitly.
// @todo create a random form name, in case we have more than one form per page and the parameter is not given.
$form_name = ! $form_name ? self::FORM_NAME : $form_name;
include ADVADS_BASE_PATH . 'admin/views/conditions/display-conditions-list.php';
/**
* Prepare condition form
*
* @todo if needed, allow to disable the form to add new conditions
*/
// add mockup conditions if add-ons are missing.
$pro_conditions = array();
if ( ! defined( 'AAP_VERSION' ) ) {
$pro_conditions[] = __( 'parent page', 'advanced-ads' );
$pro_conditions[] = __( 'post meta', 'advanced-ads' );
$pro_conditions[] = __( 'page template', 'advanced-ads' );
$pro_conditions[] = __( 'url parameters', 'advanced-ads' );
}
if ( ! defined( 'AAR_VERSION' ) ) {
$pro_conditions[] = __( 'accelerated mobile pages', 'advanced-ads' );
}
asort( $pro_conditions );
// the action to call using AJAX.
$action = 'load_display_conditions_metabox';
$connector_default = 'or';
include ADVADS_BASE_PATH . 'admin/views/conditions/display-conditions-form-top.php';
include ADVADS_BASE_PATH . 'admin/views/conditions/conditions-form.php';
}
/**
* Render connector option
*
* @param int $index incremental index of the options.
* @param string $value connector value.
* @param string $form_name name of the form, falls back to class constant.
*
* @return string HTML for connector option.
* @since 1.7.0.4
*/
public static function render_connector_option( $index = 0, $value = 'or', $form_name ) {
$label = ( 'or' === $value ) ? __( 'or', 'advanced-ads' ) : __( 'and', 'advanced-ads' );
$name = self::get_form_name_with_index( $form_name, $index );
// create random value to identify the form field.
$rand = md5( $form_name );
return '
';
}
/**
* Render type field
*
* @param string $type type of the current condition.
* @param string $name name of the form, falls back to class constant.
*/
public static function render_type_field( $type, $name ) {
?>
conditions;
if ( ! isset( $type_options[ $options['type'] ] ) ) {
return;
}
// get values and select operator based on previous settings.
$operator = ( isset( $options['operator'] ) && 'is_not' === $options['operator'] ) ? 'is_not' : 'is';
$values = ( isset( $options['value'] ) && is_array( $options['value'] ) ) ? $options['value'] : array();
// form name basis.
$name = self::get_form_name_with_index( $form_name, $index );
self::render_type_field( $options['type'], $name );
// load operator template.
include ADVADS_BASE_PATH . 'admin/views/conditions/condition-operator.php';
// set defaults.
$post_types = get_post_types(
array(
'public' => true,
'publicly_queryable' => true,
),
'object',
'or'
);
?>