Skip to content

Commit

Permalink
Merge pull request #38 from BurdaMagazinOrg/7.x/fb-ia-sdk
Browse files Browse the repository at this point in the history
Final PR for 7.x-1.0-rc1
  • Loading branch information
m4olivei committed Apr 12, 2016
2 parents 28acddb + 0748cb9 commit 4d98fe8
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 28 deletions.
2 changes: 1 addition & 1 deletion fb_instant_articles.install
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
17 changes: 17 additions & 0 deletions fb_instant_articles.module
Original file line number Diff line number Diff line change
Expand Up @@ -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().
*/
Expand Down
35 changes: 10 additions & 25 deletions includes/admin.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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('<strong>Note:</strong> this module will automatically place the ads within your articles.'),
'#attributes' => array('class' => array('ad-type')),
);
Expand Down Expand Up @@ -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'),
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class DrupalInstantArticleDisplay {
/**
* Facebook Instant Articles version number.
*/
const FB_INSTANT_ARTICLES_VERSION = '0.1.0';
const FB_INSTANT_ARTICLES_VERSION = '7.x-1.0-rc1';

/**
* Node object for which we are building an InstantArticle object.
Expand Down Expand Up @@ -113,7 +113,10 @@ public static function create($node, $layoutSettings) {
}
$instantArticle->withHeader($header);

return new DrupalInstantArticleDisplay($node, $layoutSettings, $instantArticle);
$display = new DrupalInstantArticleDisplay($node, $layoutSettings, $instantArticle);
$display->addAnalyticsFromSettings();
$display->addAdsFromSettings();
return $display;
}

/**
Expand Down Expand Up @@ -550,4 +553,88 @@ private function fieldFormatSocialElement($items, $body) {
$body->addChild($social);
}
}

/**
* Add analytics tracking code if configured in settings
*/
private function addAnalyticsFromSettings() {
$analytics_embed_code = variable_get('fb_instant_articles_analytics_embed_code');
if ($analytics_embed_code) {
$document = new \DOMDocument();
$fragment = $document->createDocumentFragment();
$valid_html = @$fragment->appendXML($analytics_embed_code);
if ($valid_html) {
$this->instantArticle
->addChild(
Analytics::create()
->withHTML(
$fragment
)
);
}
}
}

/**
* 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();
}
}

0 comments on commit 4d98fe8

Please sign in to comment.