public function apply($transformer, $instant_article, $node)
 {
     $ad = Ad::create();
     // Builds the ad
     $height = $this->getProperty(self::PROPERTY_AD_HEIGHT_URL, $node);
     if ($height) {
         $ad->withHeight($height);
     }
     $width = $this->getProperty(self::PROPERTY_AD_WIDTH_URL, $node);
     if ($width) {
         $ad->withWidth($width);
     }
     $url = $this->getProperty(self::PROPERTY_AD_URL, $node);
     if ($url) {
         $ad->withSource($url);
     }
     $embed_code = $this->getProperty(self::PROPERTY_AD_EMBED_URL, $node);
     if ($embed_code) {
         $ad->withHTML($embed_code);
     }
     if ($url || $embed_code) {
         $instant_article->addChild($ad);
     } else {
         $transformer->addWarning(new InvalidSelector('embed code or url', $instant_article, $node, $this));
     }
     return $instant_article;
 }
 public function apply($transformer, $header, $node)
 {
     $ad = Ad::create();
     // Builds the ad
     $height = $this->getProperty(self::PROPERTY_AD_HEIGHT_URL, $node);
     if ($height) {
         $ad->withHeight($height);
     }
     $width = $this->getProperty(self::PROPERTY_AD_WIDTH_URL, $node);
     if ($width) {
         $ad->withWidth($width);
     }
     $url = $this->getProperty(self::PROPERTY_AD_URL, $node);
     if ($url) {
         $ad->withSource($url);
     }
     $embed_code = $this->getProperty(self::PROPERTY_AD_EMBED_URL, $node);
     if ($embed_code) {
         $ad->withHTML($embed_code);
     }
     if ($url || $embed_code) {
         $header->addAd($ad);
     }
     return $header;
 }
 public function testRenderInlineWithHeightAndWidth()
 {
     $inline = '<h1>Some custom code</h1>' . '<script>alert("test & more test");</script>';
     $ad = Ad::create()->withHTML($inline)->withHeight(640)->withWidth(480);
     $expected = '<figure class="op-ad">' . '<iframe width="480" height="640">' . '<h1>Some custom code</h1>' . '<script>alert("test & more test");</script>' . '</iframe>' . '</figure>';
     $rendered = $ad->render();
     $this->assertEquals($expected, $rendered);
 }
 public function testInstantArticle()
 {
     $article = InstantArticle::create()->withCanonicalUrl('')->withHeader(Header::create())->addChild(Paragraph::create()->appendText('Some text to be within a paragraph for testing.'))->addChild(Paragraph::create())->addChild(Paragraph::create()->appendText(" \n \t "))->addChild(Image::create())->addChild(Image::create()->withURL(''))->addChild(SlideShow::create()->addImage(Image::create()->withURL('https://jpeg.org/images/jpegls-home.jpg'))->addImage(Image::create()))->addChild(Ad::create()->withSource('http://foo.com'))->addChild(Paragraph::create()->appendText('Other text to be within a second paragraph for testing.'))->addChild(Analytics::create())->withFooter(Footer::create());
     $expected = '<!doctype html>' . '<html>' . '<head>' . '<link rel="canonical" href=""/>' . '<meta charset="utf-8"/>' . '<meta property="op:generator" content="facebook-instant-articles-sdk-php"/>' . '<meta property="op:generator:version" content="' . InstantArticle::CURRENT_VERSION . '"/>' . '<meta property="op:markup_version" content="v1.0"/>' . '</head>' . '<body>' . '<article>' . '<p>Some text to be within a paragraph for testing.</p>' . '<figure class="op-slideshow">' . '<figure>' . '<img src="https://jpeg.org/images/jpegls-home.jpg"/>' . '</figure>' . '</figure>' . '<figure class="op-ad">' . '<iframe src="http://foo.com"></iframe>' . '</figure>' . '<p>Other text to be within a second paragraph for testing.</p>' . '</article>' . '</body>' . '</html>';
     $result = $article->render();
     $this->assertEquals($expected, $result);
     $warnings = InstantArticleValidator::check($article);
     $this->assertEquals(9, count($warnings));
 }
 /**
  * Replace all ads within this Article
  *
  * @param Ad[] $ads All the ads
  *
  * @return $this
  */
 public function withAds($ads)
 {
     Type::enforceArrayOf($ads, Ad::getClassName());
     $this->ads = $ads;
     return $this;
 }
 /**
  * Add all ad code(s) to the Header of an InstantArticle.
  *
  * @since 0.3
  */
 public function add_ads_from_settings()
 {
     $header = $this->instant_article->getHeader();
     $settings_ads = Instant_Articles_Option_Ads::get_option_decoded();
     $width = 300;
     $height = 250;
     $dimensions_match = array();
     $dimensions_raw = isset($settings_ads['dimensions']) ? $settings_ads['dimensions'] : null;
     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);
     $source_of_ad = isset($settings_ads['ad_source']) ? $settings_ads['ad_source'] : 'none';
     switch ($source_of_ad) {
         case 'none':
             break;
         case 'fan':
             if (!empty($settings_ads['fan_placement_id'])) {
                 $placement_id = $settings_ads['fan_placement_id'];
                 $ad->withSource(add_query_arg(array('placement' => $placement_id, 'adtype' => 'banner' . $width . 'x' . $height), 'https://www.facebook.com/adnw_request'));
                 $header->addAd($ad);
             }
             break;
         case 'iframe':
             if (!empty($settings_ads['iframe_url'])) {
                 $ad->withSource($settings_ads['iframe_url']);
                 $header->addAd($ad);
             }
             break;
         case 'embed':
             if (!empty($settings_ads['embed_code'])) {
                 $document = new DOMDocument();
                 $fragment = $document->createDocumentFragment();
                 $valid_html = @$fragment->appendXML($settings_ads['embed_code']);
                 if ($valid_html) {
                     $ad->withHTML($fragment);
                     $header->addAd($ad);
                 }
             }
             break;
         default:
             if (!empty($source_of_ad)) {
                 $registered_compat_ads = Instant_Articles_Option::get_registered_compat('instant_articles_compat_registry_ads');
                 foreach ($registered_compat_ads as $compat_id => $compat_info) {
                     if (array_key_exists($compat_id, $registered_compat_ads)) {
                         $document = new DOMDocument();
                         $fragment = $document->createDocumentFragment();
                         $valid_html = @$fragment->appendXML($compat_info['payload']);
                         if ($valid_html) {
                             $ad = Ad::create()->enableDefaultForReuse()->withWidth($width)->withHeight($height)->withHTML($fragment);
                             $header->addAd($ad);
                         }
                     }
                 }
             }
             break;
     }
     $this->instant_article->enableAutomaticAdPlacement();
 }
 /**
  * Adds new child elements to the front of this InstantArticle
  *
  * @param Element to be added to this Article.
  *
  * @return $this
  */
 public function unshiftChild($child)
 {
     Type::enforce($child, [Ad::getClassName(), Analytics::getClassName(), AnimatedGIF::getClassName(), Audio::getClassName(), Blockquote::getClassName(), Image::getClassName(), H1::getClassName(), H2::getClassName(), Interactive::getClassName(), ListElement::getClassName(), Map::getClassName(), Paragraph::getClassName(), Pullquote::getClassName(), RelatedArticles::getClassName(), Slideshow::getClassName(), SocialEmbed::getClassName(), Video::getClassName()]);
     array_unshift($this->children, $child);
     return $this;
 }