Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extended the plugin to manage ad layers for individual terms #56

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ ad_layers_dfp_custom_targeting_field_args

ad_layers_post_types

ad_layers_taxonomy_types

*Ad_Layers_Post_Type*

ad_layers_taxonomies
Expand Down
40 changes: 40 additions & 0 deletions php/class-ad-layers-meta-boxes.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ class Ad_Layers_Meta_Boxes extends Ad_Layers_Singleton {
*/
public $post_types = array( 'post' );

/**
* Terms used by ad layers
*
* @var string
*/
public $terms = array();

/**
* Capability required to assign ads to posts.
*
Expand Down Expand Up @@ -48,6 +55,14 @@ public function setup() {
foreach ( $this->post_types as $post_type ) {
add_action( 'fm_post_' . $post_type, array( $this, 'add_meta_boxes' ) );
}

// Set terms used by ad layers
$this->terms = apply_filters( 'ad_layers_taxonomy_types', $this->terms );

// Add the custom meta boxes used for ad layers on terms
foreach ( $this->terms as $term ) {
add_action( 'fm_term_' . $term, array( $this, 'add_term_meta_boxes' ) );
}
}

/**
Expand All @@ -74,6 +89,31 @@ public function add_meta_boxes() {
);
$fm_ad_layer->add_meta_box( __( 'Ad Layer', 'ad-layers' ), $post_type, 'side', 'core' );
}

/**
* Adds the meta boxes required to manage ad layers on terms.
* @access public
*/
public function add_term_meta_boxes() {
// Get the term name
$term = str_replace( 'fm_term_', '', current_filter() );

// Add ad units
$fm_ad_layer = new Fieldmanager_Autocomplete(
array(
'name' => 'ad_layer',
'description' => __( 'Select a specific custom ad layer to use with this term.', 'ad-layers' ),
'datasource' => new Fieldmanager_Datasource_Post( array(
'query_args' => array(
'post_type' => array( Ad_Layers_Post_Type::instance()->get_post_type() ),
'post_status' => 'publish',
'order_by' => 'title',
),
) ),
)
);
$fm_ad_layer->add_term_meta_box( __( 'Ad Layer', 'ad-layers' ), $term );
}
}

Ad_Layers_Meta_Boxes::instance();
Expand Down
11 changes: 11 additions & 0 deletions php/class-ad-layers.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ public function set_active_ad_layer() {

// If the ad layer is filtered for this page, skip the logic below
$ad_layer = apply_filters( 'ad_layers_active_ad_layer', array(), $queried_object );

if ( ! empty( $ad_layer ) ) {
$this->ad_layer = $ad_layer;
return;
Expand Down Expand Up @@ -266,6 +267,16 @@ public function set_active_ad_layer() {
&& empty( $post_types )
&& ( empty( $page_types ) || in_array( $queried_object->taxonomy, $page_types ) ) ) {

$ad_layer_id = intval( get_term_meta( get_queried_object()->term_id, 'ad_layer', true ) );

if ( ! empty( $ad_layer_id ) ) {
$this->ad_layer = array(
'post_id' => $ad_layer_id,
'title' => get_the_title( $ad_layer_id ),
);
break;
}

// Check if there is taxonomy data
if ( ! empty( $taxonomy_terms ) ) {
// Check if this taxonomy matches
Expand Down
16 changes: 16 additions & 0 deletions tests/php/test-active-layer.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,4 +298,20 @@ public function test_active_layer_post_types_by_archive() {
$this->assertTrue( is_post_type_archive( $post_type_2 ) );
$this->assertSame( $layer_2, $this->get_active_ad_layer() );
}

public function test_active_layer_term_override() {
$layer_override = $this->build_and_get_layer( array( 'page_types' => 'test-taxonomy' ) );
$layer = $this->build_and_get_layer( array( 'page_types' => 'post_tag' ) );

$this->go_to( get_tag_link( $this->tag_id ) );
$this->assertTrue( is_tag() );

// the tag level ad-layer reflects here as active ad-layer.
$this->assertSame( $layer, $this->get_active_ad_layer() );

// After adding ad_layer to the term, the ad_layer for that term overrides the tag level ad-layer.
add_term_meta( $this->tag_id, 'ad_layer', $layer_override );
$this->assertNotSame( $layer, $this->get_active_ad_layer() );
$this->assertSame( $layer_override, $this->get_active_ad_layer() );
}
}