diff --git a/fb_instant_articles.install b/fb_instant_articles.install index 966f859..b169cfa 100644 --- a/fb_instant_articles.install +++ b/fb_instant_articles.install @@ -14,7 +14,7 @@ function fb_instant_articles_uninstall() { variable_del('fb_instant_articles_ad_type'); variable_del('fb_instant_articles_ads_iframe_url'); variable_del('fb_instant_articles_ads_an_placement_id'); - variable_del('fb_instant_articles_ads_dimension'); + variable_del('fb_instant_articles_ads_dimensions'); variable_del('fb_instant_articles_ads_embed_code'); variable_del('fb_instant_articles_analytics_embed_code'); variable_del('fb_instant_articles_enable_logging'); diff --git a/fb_instant_articles.module b/fb_instant_articles.module index b9e2439..d51128b 100644 --- a/fb_instant_articles.module +++ b/fb_instant_articles.module @@ -5,6 +5,23 @@ * Hook implementations for Facebook Instant Articles Base module. */ +define('FB_INSTANT_ARTICLES_AD_TYPE_NONE', 'none'); +define('FB_INSTANT_ARTICLES_AD_TYPE_FBAN', 'fban'); +define('FB_INSTANT_ARTICLES_AD_TYPE_SOURCE_URL', 'source_url'); +define('FB_INSTANT_ARTICLES_AD_TYPE_EMBED_CODE', 'embed_code'); + +/** + * Get an array of possible ad types and their descriptive names + **/ +function fb_instant_articles_get_ad_types() { + return array( + FB_INSTANT_ARTICLES_AD_TYPE_NONE => t('None'), + FB_INSTANT_ARTICLES_AD_TYPE_FBAN => t('Facebook Audience Network'), + FB_INSTANT_ARTICLES_AD_TYPE_SOURCE_URL => t('Source URL'), + FB_INSTANT_ARTICLES_AD_TYPE_EMBED_CODE => t('Embed Code'), + ); +} + /** * Implements hook_menu(). */ diff --git a/includes/admin.inc b/includes/admin.inc index d4585b5..6a30960 100644 --- a/includes/admin.inc +++ b/includes/admin.inc @@ -5,11 +5,6 @@ * Settings for Facebook Instant Articles Base module. */ -define('FB_INSTANT_ARTICLES_AD_TYPE_NONE', 'None'); -define('FB_INSTANT_ARTICLES_AD_TYPE_FBAN', 'Facebook Audience Network'); -define('FB_INSTANT_ARTICLES_AD_TYPE_SOURCE_URL', 'Source URL'); -define('FB_INSTANT_ARTICLES_AD_TYPE_EMBED_CODE', 'Embed Code'); - /** * Form constructor for Facebook Instant Articles Base settings form. */ @@ -75,12 +70,7 @@ function fb_instant_articles_module_config_ads($form) { '#type' => 'select', '#title' => t('Ad Type'), '#default_value' => variable_get('fb_instant_articles_ad_type', FB_INSTANT_ARTICLES_AD_TYPE_NONE), - '#options' => array( - FB_INSTANT_ARTICLES_AD_TYPE_NONE => FB_INSTANT_ARTICLES_AD_TYPE_NONE, - FB_INSTANT_ARTICLES_AD_TYPE_FBAN => FB_INSTANT_ARTICLES_AD_TYPE_FBAN, - FB_INSTANT_ARTICLES_AD_TYPE_SOURCE_URL => FB_INSTANT_ARTICLES_AD_TYPE_SOURCE_URL, - FB_INSTANT_ARTICLES_AD_TYPE_EMBED_CODE => FB_INSTANT_ARTICLES_AD_TYPE_EMBED_CODE, - ), + '#options' => fb_instant_articles_get_ad_types(), '#description' => t('Note: this module will automatically place the ads within your articles.'), '#attributes' => array('class' => array('ad-type')), ); @@ -113,20 +103,6 @@ function fb_instant_articles_module_config_ads($form) { ), ); - $form['ads']['fb_instant_articles_ads_dimension'] = array( - '#type' => 'select', - '#title' => t('Ad Dimensions'), - '#options' => array( - 0 => t('Large (300 x 250)'), - ), - '#default_value' => variable_get('fb_instant_articles_ads_dimension'), - '#states' => array( - 'visible' => array( - '[name=fb_instant_articles_ad_type]' => array('value' => FB_INSTANT_ARTICLES_AD_TYPE_EMBED_CODE), - ), - ), - ); - $form['ads']['fb_instant_articles_ads_embed_code'] = array( '#type' => 'textarea', '#title' => t('Ad Embed Code'), @@ -141,6 +117,15 @@ function fb_instant_articles_module_config_ads($form) { ), ); + $form['ads']['fb_instant_articles_ads_dimensions'] = array( + '#type' => 'select', + '#title' => t('Ad Dimensions'), + '#options' => array( + '300x250' => t('Large (300 x 250)'), + ), + '#default_value' => variable_get('fb_instant_articles_ads_dimensions'), + ); + return $form; } diff --git a/modules/fb_instant_articles_display/src/DrupalInstantArticleDisplay.php b/modules/fb_instant_articles_display/src/DrupalInstantArticleDisplay.php index 26291fe..8e869f5 100644 --- a/modules/fb_instant_articles_display/src/DrupalInstantArticleDisplay.php +++ b/modules/fb_instant_articles_display/src/DrupalInstantArticleDisplay.php @@ -115,6 +115,7 @@ public static function create($node, $layoutSettings) { $display = new DrupalInstantArticleDisplay($node, $layoutSettings, $instantArticle); $display->addAnalyticsFromSettings(); + $display->addAdsFromSettings(); return $display; } @@ -573,4 +574,67 @@ private function addAnalyticsFromSettings() { } } } + + /** + * Add ads if configured in settings + */ + public function addAdsFromSettings() { + $ad_type = variable_get('fb_instant_articles_ad_type', FB_INSTANT_ARTICLES_AD_TYPE_NONE); + if ($ad_type === FB_INSTANT_ARTICLES_AD_TYPE_NONE) { + return; + } + $width = 300; + $height = 250; + $dimensions_match = array(); + $dimensions_raw = variable_get('fb_instant_articles_ads_dimensions'); + if (preg_match('/^(?:\s)*(\d+)x(\d+)(?:\s)*$/', $dimensions_raw, $dimensions_match)) { + $width = intval($dimensions_match[1]); + $height = intval($dimensions_match[2]); + } + $ad = Ad::create() + ->enableDefaultForReuse() + ->withWidth($width) + ->withHeight($height); + $header = $this->instantArticle->getHeader(); + switch ($ad_type) { + case FB_INSTANT_ARTICLES_AD_TYPE_FBAN: + $an_placement_id = variable_get('fb_instant_articles_ads_an_placement_id'); + if ($an_placement_id) { + $ad->withSource( + url('https://www.facebook.com/adnw_request', array( + 'query' => array( + 'placement' => $ad_placement_id, + 'adtype' => 'banner' . $width . 'x' . $height, + ), + )) + ); + $header->addAd($ad); + } + break; + case FB_INSTANT_ARTICLES_AD_TYPE_SOURCE_URL: + $iframe_url = variable_get('fb_instant_articles_ads_iframe_url'); + if ($iframe_url) { + $ad->withSource( + $iframe_url + ); + $header->addAd($ad); + } + break; + case FB_INSTANT_ARTICLES_AD_TYPE_EMBED_CODE: + $embed_code = variable_get('fb_instant_articles_ads_embed_code'); + if ($embed_code) { + $document = new \DOMDocument(); + $fragment = $document->createDocumentFragment(); + $valid_html = @$fragment->appendXML($embed_code); + if ($valid_html) { + $ad->withHTML( + $fragment + ); + $header->addAd($ad); + } + } + break; + } + $this->instantArticle->enableAutomaticAdPlacement(); + } }