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

Allow DFP async slots to accept multiple ad sizes #82

Merged
merged 13 commits into from Sep 15, 2015
39 changes: 32 additions & 7 deletions providers/doubleclick-for-publishers-async.php
Expand Up @@ -164,15 +164,19 @@ public function filter_output_html( $output_html, $tag_id ) {
continue;

$tt = $tag['url_vars'];
$matching_ad_code = $ad_code_manager->get_matching_ad_code( $tag['tag'] );
if ( ! empty( $matching_ad_code ) ) {
// @todo There might be a case when there are two tags registered with the same dimensions
// and the same tag id ( which is just a div id ). This confuses DFP Async, so we need to make sure
// that tags are unique
$matching_ad_code = $ad_code_manager->get_matching_ad_code( $tag['tag'] );
if ( ! empty( $matching_ad_code ) ) {
// @todo There might be a case when there are two tags registered with the same dimensions
// and the same tag id ( which is just a div id ). This confuses DFP Async, so we need to make sure
// that tags are unique

// Parse ad tags to output flexible unit dimensions
$unit_sizes = $this->parse_ad_tag_sizes( $tt );

?>
googletag.defineSlot('/<?php echo esc_attr( $matching_ad_code['url_vars']['dfp_id'] ); ?>/<?php echo esc_attr( $matching_ad_code['url_vars']['tag_name'] ); ?>', [<?php echo (int)$tt['width'] ?>, <?php echo (int)$tt['height'] ?>], "acm-ad-tag-<?php echo esc_attr( $matching_ad_code['url_vars']['tag_id'] ); ?>").addService(googletag.pubads());
googletag.defineSlot('/<?php echo esc_attr( $matching_ad_code['url_vars']['dfp_id'] ); ?>/<?php echo esc_attr( $matching_ad_code['url_vars']['tag_name'] ); ?>', [<?php echo $unit_sizes; ?>], "acm-ad-tag-<?php echo esc_attr( $matching_ad_code['url_vars']['tag_id'] ); ?>").addService(googletag.pubads());
<?php
}
}
endforeach;
?>
googletag.pubads().enableSingleRequest();
Expand Down Expand Up @@ -204,6 +208,27 @@ public function action_wp_head() {
do_action( 'acm_tag', 'dfp_head' );
}

/**
* Allow ad sizes to be defined as arrays or as basic width x height.
* The purpose of this is to solve for flex units, where multiple ad
* sizes may be required to load in the same ad unit.
*/
public function parse_ad_tag_sizes( $url_vars ) {
if ( empty( $url_vars ) )
return;

$unit_sizes_output = '';
if ( ! empty( $url_vars['sizes'] ) ) {
foreach( $url_vars['sizes'] as $unit_size ) {
$unit_sizes_output .= '[' . (int)$unit_size['width'] . ',' . (int)$unit_size['height'] . '],';
}
$unit_sizes_output = trim( $unit_sizes_output, ',' );
} else { // fallback for old style width x height
$unit_sizes_output = (int)$url_vars['width'] . ',' . (int)$url_vars['height'];
}
return $unit_sizes_output;
}

}

class Doubleclick_For_Publishers_Async_ACM_WP_List_Table extends ACM_WP_List_Table {
Expand Down