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

Filters for Super Cool Ad Inserter Ads #839

Merged
merged 6 commits into from
Mar 24, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions newspack-theme/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,80 @@ function newspack_coauthors_in_rss( $the_author ) {
}
add_filter( 'the_author', 'newspack_coauthors_in_rss' );

/**
* Determine minimum theme breakpoint, below which responsive Ads media queries should not have a min-width.
*
* @param array $media_queries An array of objects, one for each size, with width|height|min_width|min_height.
* @param string $placement ID of the ad placement.
* @param string $context Optional second string describing the ad placement. For Widget placements, the ID of the Widget.
* @return array The correct array of media query data.
*/
function newspack_theme_newspack_ads_media_queries( $media_queries, $placement, $context ) {
if ( 'newspack_ads_widget' === $placement && strpos( $context, 'scaip' ) === 0 ) {
switch ( get_page_template_slug() ) {
case 'single-wide.php':
foreach ( $media_queries as $index => &$media_query ) {
$next_media_query = ( count( $media_queries ) > $index + 1 ) ? $media_queries[ $index + 1 ] : null;
if ( intval( $media_query['width'] ) > 1200 ) {
$media_query['min_width'] = null;
$media_query['max_width'] = null;
} else {
$media_query['min_width'] = ceil( intval( $media_query['width'] ) / 0.9 );
if ( $next_media_query['width'] && $next_media_query['width'] <= 1200 ) {
$media_query['max_width'] = ceil( $next_media_query['width'] / 0.9 - 1 );
} else {
$media_query['max_width'] = null;
}
}
}
break;
case 'single-feature.php':
default:
foreach ( $media_queries as $index => &$media_query ) {
$next_media_query = ( count( $media_queries ) > $index + 1 ) ? $media_queries[ $index + 1 ] : null;
if ( intval( $media_query['width'] ) > 780 ) {
$media_query['min_width'] = null;
$media_query['max_width'] = null;
} else if ( intval( $media_query['width'] ) > ceil( 782 * 0.585 ) ) {
$media_query['min_width'] = ceil( intval( $media_query['width'] ) / 0.585 );
if ( $next_media_query['width'] && $next_media_query['width'] <= 780 ) {
$media_query['max_width'] = ceil( $next_media_query['width'] / 0.585 - 1 );
} else {
$media_query['max_width'] = null;
}
} else {
$media_query['min_width'] = ceil( intval( $media_query['width'] ) / 0.9 );
if ( $next_media_query['width'] && $next_media_query['width'] <= 780 ) {
$media_query['max_width'] = ceil( $next_media_query['width'] / 0.585 - 1 );
} else {
$media_query['max_width'] = null;
}
}
}
break;
}
}
return $media_queries;
}
add_filter( 'newspack_ads_media_queries', 'newspack_theme_newspack_ads_media_queries', 10, 3 );

/**
* Should a particular Ad deployment use responsive placement.
*
* @param boolean $responsive Default value of whether to use responsive placement.
* @param string $placement ID of the ad placement.
* @param string $context Optional second string describing the ad placement. For Widget placements, the ID of the Widget.
* @return boolean Whether to use responsive placement.
*/
function newspack_theme_newspack_ads_maybe_use_responsive_placement( $responsive, $placement, $context ) {
// Apply Responsive placement to widgets using Super Cool Ad Inserter.
if ( 'newspack_ads_widget' === $placement && strpos( $context, 'scaip' ) === 0 ) {
return true;
}
return $responsive;
}
add_filter( 'newspack_ads_maybe_use_responsive_placement', 'newspack_theme_newspack_ads_maybe_use_responsive_placement', 10, 3 );

/**
* Notify about child theme deprecation.
* TODO: Remove after child theme code is removed.
Expand Down