/**
  * Render post
  *
  * @since 0.1
  * @return InstantArticle
  */
 public function to_instant_article()
 {
     /**
      * Fires before the instant article is rendered.
      *
      * @since 0.1
      * @param Instant_Article_Post  $instant_article_post  The instant article post.
      */
     do_action('instant_articles_before_transform_post', $this);
     // Get time zone configured in WordPress. Default to UTC if no time zone configured.
     $date_time_zone = get_option('timezone_string') ? new DateTimeZone(get_option('timezone_string')) : new DateTimeZone('UTC');
     // Initialize transformer
     $file_path = plugin_dir_path(__FILE__) . 'rules-configuration.json';
     $configuration = file_get_contents($file_path);
     $transformer = new Transformer();
     $this->transformer = $transformer;
     $transformer->loadRules($configuration);
     $transformer = apply_filters('instant_articles_transformer_rules_loaded', $transformer);
     $settings_publishing = Instant_Articles_Option_Publishing::get_option_decoded();
     if (isset($settings_publishing['custom_rules_enabled']) && !empty($settings_publishing['custom_rules_enabled']) && isset($settings_publishing['custom_rules']) && !empty($settings_publishing['custom_rules'])) {
         $transformer->loadRules($settings_publishing['custom_rules']);
     }
     $transformer = apply_filters('instant_articles_transformer_custom_rules_loaded', $transformer);
     $blog_charset = get_option('blog_charset');
     $header = Header::create()->withPublishTime(Time::create(Time::PUBLISHED)->withDatetime(new DateTime($this->_post->post_date, $date_time_zone)))->withModifyTime(Time::create(Time::MODIFIED)->withDatetime(new DateTime($this->_post->post_modified, $date_time_zone)));
     $title = $this->get_the_title();
     if ($title) {
         $document = new DOMDocument();
         libxml_use_internal_errors(true);
         $document->loadHTML('<?xml encoding="' . $blog_charset . '" ?><h1>' . $title . '</h1>');
         libxml_use_internal_errors(false);
         $transformer->transform($header, $document);
     }
     if ($this->has_subtitle()) {
         $header->withSubTitle($this->get_the_subtitle());
     }
     $authors = $this->get_the_authors();
     foreach ($authors as $author) {
         $author_obj = Author::create();
         if ($author->display_name) {
             $author_obj->withName($author->display_name);
         }
         if ($author->bio) {
             $author_obj->withDescription($author->bio);
         }
         if ($author->user_url) {
             $author_obj->withURL($author->user_url);
         }
         $header->addAuthor($author_obj);
     }
     $kicker = $this->get_the_kicker();
     if ($kicker) {
         $header->withKicker($kicker);
     }
     $cover = $this->get_cover_media();
     if ($cover->getUrl()) {
         $header->withCover($cover);
     }
     $this->instant_article = InstantArticle::create()->withCanonicalUrl($this->get_canonical_url())->withHeader($header)->addMetaProperty('op:generator:application', 'facebook-instant-articles-wp')->addMetaProperty('op:generator:application:version', IA_PLUGIN_VERSION);
     $settings_style = Instant_Articles_Option_Styles::get_option_decoded();
     if (isset($settings_style['article_style']) && !empty($settings_style['article_style'])) {
         $this->instant_article->withStyle($settings_style['article_style']);
     } else {
         $this->instant_article->withStyle('default');
     }
     $libxml_previous_state = libxml_use_internal_errors(true);
     $document = new DOMDocument('1.0', get_option('blog_charset'));
     $content = $this->get_the_content();
     // DOMDocument isn’t handling encodings too well, so let’s help it a little.
     if (function_exists('mb_convert_encoding')) {
         $content = mb_convert_encoding($content, 'HTML-ENTITIES', get_option('blog_charset'));
     } else {
         $content = htmlspecialchars_decode(utf8_decode(htmlentities($content, ENT_COMPAT, 'utf-8', false)));
     }
     $result = $document->loadHTML('<!doctype html><html><body>' . $content . '</body></html>');
     // We need to make sure that scripts use absolute URLs and not relative URLs.
     $scripts = $document->getElementsByTagName('script');
     if (!empty($scripts)) {
         foreach ($scripts as $script) {
             $src = $script->getAttribute('src');
             $explode_src = parse_url($src);
             if (is_array($explode_src) && empty($explode_src['scheme']) && !empty($explode_src['host']) && !empty($explode_src['path'])) {
                 $src = 'https://' . $explode_src['host'] . $explode_src['path'];
             }
             $script->setAttribute('src', $src);
         }
     }
     libxml_clear_errors();
     libxml_use_internal_errors($libxml_previous_state);
     $document = apply_filters('instant_articles_parsed_document', $document);
     if ($result) {
         $transformer->transform($this->instant_article, $document);
     }
     $this->add_ads_from_settings();
     $this->add_analytics_from_settings();
     $this->instant_article = apply_filters('instant_articles_transformed_element', $this->instant_article);
     /**
      * Fires after the instant article is rendered.
      *
      * @since 0.1
      * @param Instant_Article_Post  $instant_article_post  The instant article post.
      */
     do_action('instant_articles_after_transform_post', $this);
     return $this->instant_article;
 }
 public static function render($ajax = false)
 {
     if (!current_user_can('manage_options')) {
         wp_die(esc_html('You do not have sufficient permissions to access this page.'));
     }
     // Read options (they are used on the templates)
     $current_state = Instant_Articles_Wizard_State::get_current_state();
     $fb_page_settings = Instant_Articles_Option_FB_Page::get_option_decoded();
     $fb_app_settings = Instant_Articles_Option_FB_App::get_option_decoded();
     $fb_helper = new Instant_Articles_Wizard_FB_Helper();
     $settings_url = self::get_url();
     // Handle redirection from Login flow
     // ----------------------------------
     // Only during STATE_APP_SETUP
     if ($current_state === Instant_Articles_Wizard_State::STATE_APP_SETUP) {
         $user_access_token = $fb_helper->get_fb_access_token();
         $permissions = $fb_helper->get_fb_permissions($user_access_token);
         // Trigger transition if all needed permissions are granted
         if ($user_access_token && isset($permissions['pages_manage_instant_articles']) && isset($permissions['pages_show_list'])) {
             Instant_Articles_Wizard_State::do_transition(Instant_Articles_Wizard_State::STATE_PAGE_SELECTION, array('app_id' => $fb_app_settings['app_id'], 'app_secret' => $fb_app_settings['app_secret'], 'user_access_token' => $user_access_token->getValue()));
             // Override step
             $current_state = Instant_Articles_Wizard_State::get_current_state();
         }
     }
     // ----------------------------------
     // Grabs the current configured style
     // ----------------------------------
     // Only during STATE_STYLE_SELECTION
     if ($current_state === Instant_Articles_Wizard_State::STATE_STYLE_SELECTION) {
         $settings_style = Instant_Articles_Option_Styles::get_option_decoded();
         if (isset($settings_style['article_style']) && !empty($settings_style['article_style'])) {
             $article_style = $settings_style['article_style'];
         } else {
             $article_style = 'default';
         }
     }
     // ----------------------------------
     // Check submission status
     // ----------------------------------
     // Only during STATE_REVIEW_SUBMISSION
     if ($current_state === Instant_Articles_Wizard_State::STATE_REVIEW_SUBMISSION) {
         $review_submission_status = Instant_Articles_Wizard_Review_Submission::getReviewSubmissionStatus();
         if ($review_submission_status === Instant_Articles_Wizard_Review_Submission::STATUS_NOT_SUBMITTED) {
             $articles_for_review = Instant_Articles_Wizard_Review_Submission::getArticlesForReview();
         }
     }
     // ----------------------------------
     include dirname(__FILE__) . '/templates/wizard-template.php';
 }
 /**
  * Render post
  *
  * @since 0.1
  * @return InstantArticle
  */
 public function to_instant_article()
 {
     /**
      * Fires before the instant article is rendered.
      *
      * @since 0.1
      * @param Instant_Article_Post  $instant_article_post  The instant article post.
      */
     do_action('instant_articles_before_transform_post', $this);
     // Get time zone configured in WordPress. Default to UTC if no time zone configured.
     $date_time_zone = get_option('timezone_string') ? new DateTimeZone(get_option('timezone_string')) : new DateTimeZone('UTC');
     // Initialize transformer
     $file_path = plugin_dir_path(__FILE__) . 'rules-configuration.json';
     $configuration = file_get_contents($file_path);
     $transformer = new Transformer();
     $this->transformer = $transformer;
     $transformer->loadRules($configuration);
     $transformer = apply_filters('instant_articles_transformer_rules_loaded', $transformer);
     $settings_publishing = Instant_Articles_Option_Publishing::get_option_decoded();
     if (isset($settings_publishing['custom_rules_enabled']) && !empty($settings_publishing['custom_rules_enabled']) && isset($settings_publishing['custom_rules']) && !empty($settings_publishing['custom_rules'])) {
         $transformer->loadRules($settings_publishing['custom_rules']);
     }
     $transformer = apply_filters('instant_articles_transformer_custom_rules_loaded', $transformer);
     $blog_charset = get_option('blog_charset');
     $header = Header::create()->withPublishTime(Time::create(Time::PUBLISHED)->withDatetime(new DateTime($this->_post->post_date, $date_time_zone)))->withModifyTime(Time::create(Time::MODIFIED)->withDatetime(new DateTime($this->_post->post_modified, $date_time_zone)));
     $title = $this->get_the_title();
     if ($title) {
         $document = new DOMDocument();
         libxml_use_internal_errors(true);
         $document->loadHTML('<?xml encoding="' . $blog_charset . '" ?><h1>' . $title . '</h1>');
         libxml_use_internal_errors(false);
         $transformer->transform($header, $document);
     }
     if ($this->has_subtitle()) {
         $header->withSubTitle($this->get_the_subtitle());
     }
     $authors = $this->get_the_authors();
     foreach ($authors as $author) {
         $author_obj = Author::create();
         if ($author->display_name) {
             $author_obj->withName($author->display_name);
         }
         if ($author->bio) {
             $author_obj->withDescription($author->bio);
         }
         if ($author->user_url) {
             $author_obj->withURL($author->user_url);
         }
         $header->addAuthor($author_obj);
     }
     $kicker = $this->get_the_kicker();
     if ($kicker) {
         $header->withKicker($kicker);
     }
     $cover = $this->get_cover_media();
     if ($cover->getUrl()) {
         $header->withCover($cover);
     }
     $this->instant_article = InstantArticle::create()->withCanonicalUrl($this->get_canonical_url())->withHeader($header)->addMetaProperty('op:generator:application', 'facebook-instant-articles-wp')->addMetaProperty('op:generator:application:version', IA_PLUGIN_VERSION);
     $settings_style = Instant_Articles_Option_Styles::get_option_decoded();
     if (isset($settings_style['article_style']) && !empty($settings_style['article_style'])) {
         $this->instant_article->withStyle($settings_style['article_style']);
     } else {
         $this->instant_article->withStyle('default');
     }
     $transformer->transformString($this->instant_article, $this->get_the_content(), get_option('blog_charset'));
     $this->add_ads_from_settings();
     $this->add_analytics_from_settings();
     $this->instant_article = apply_filters('instant_articles_transformed_element', $this->instant_article);
     /**
      * Fires after the instant article is rendered.
      *
      * @since 0.1
      * @param Instant_Article_Post  $instant_article_post  The instant article post.
      */
     do_action('instant_articles_after_transform_post', $this);
     return $this->instant_article;
 }
 public static function render($ajax = false)
 {
     if (!current_user_can('manage_options')) {
         wp_die(esc_html('You do not have sufficient permissions to access this page.'));
     }
     try {
         // Read options (they are used on the templates)
         $current_state = Instant_Articles_Wizard_State::get_current_state();
         $fb_page_settings = Instant_Articles_Option_FB_Page::get_option_decoded();
         $fb_app_settings = Instant_Articles_Option_FB_App::get_option_decoded();
         $fb_helper = new Instant_Articles_Wizard_FB_Helper();
         $settings_url = self::get_url();
         // Handle redirection from Login flow
         // ----------------------------------
         // Only during STATE_APP_SETUP
         if ($current_state === Instant_Articles_Wizard_State::STATE_APP_SETUP) {
             $user_access_token = $fb_helper->get_fb_access_token();
             $permissions = $fb_helper->get_fb_permissions($user_access_token);
             // Trigger transition if all needed permissions are granted
             if ($user_access_token && isset($permissions['pages_manage_instant_articles']) && isset($permissions['pages_show_list'])) {
                 Instant_Articles_Wizard_State::do_transition(Instant_Articles_Wizard_State::STATE_PAGE_SELECTION, array('app_id' => $fb_app_settings['app_id'], 'app_secret' => $fb_app_settings['app_secret'], 'user_access_token' => $user_access_token->getValue()));
                 // Override step
                 $current_state = Instant_Articles_Wizard_State::get_current_state();
             }
         }
         // ----------------------------------
         // Handle redirection from Login flow
         // ----------------------------------
         // Only during STATE_PAGE_SELECTION
         if ($current_state === Instant_Articles_Wizard_State::STATE_PAGE_SELECTION) {
             $fb_helper = new Instant_Articles_Wizard_FB_Helper();
             try {
                 $pages = $fb_helper->get_pages();
             } catch (Exception $e) {
                 // If we couldn't fetch the pages, revert to the App setup
                 Instant_Articles_Wizard_State::do_transition(Instant_Articles_Wizard_State::STATE_APP_SETUP);
             }
         }
         // Grabs the current configured style
         // ----------------------------------
         // Only during STATE_STYLE_SELECTION
         if ($current_state === Instant_Articles_Wizard_State::STATE_STYLE_SELECTION) {
             $settings_style = Instant_Articles_Option_Styles::get_option_decoded();
             if (isset($settings_style['article_style']) && !empty($settings_style['article_style'])) {
                 $article_style = $settings_style['article_style'];
             } else {
                 $article_style = 'default';
             }
         }
         // ----------------------------------
         // Check submission status
         // ----------------------------------
         // Only during STATE_REVIEW_SUBMISSION
         if ($current_state === Instant_Articles_Wizard_State::STATE_REVIEW_SUBMISSION) {
             $review_submission_status = Instant_Articles_Wizard_Review_Submission::getReviewSubmissionStatus();
             if ($review_submission_status === Instant_Articles_Wizard_Review_Submission::STATUS_NOT_SUBMITTED) {
                 $articles_for_review = Instant_Articles_Wizard_Review_Submission::getArticlesForReview();
                 // Map to Instant_Articles_Post instances
                 $instant_articles_for_review = array_map(function ($article) {
                     $instant_articles_post = new Instant_Articles_Post($article);
                     // Call transformation to load warnings
                     $instant_articles_post->to_instant_article();
                     return $instant_articles_post;
                 }, $articles_for_review);
                 // Filter articles with warnings and not forced
                 $instant_articles_with_warnings = array_filter($instant_articles_for_review, function ($article) {
                     $has_warnings = count($article->transformer->getWarnings()) > 0;
                     $force_submit = get_post_meta($article->get_the_id(), Instant_Articles_Publisher::FORCE_SUBMIT_KEY, true);
                     return $has_warnings && !$force_submit;
                 });
             }
         }
         // ----------------------------------
         include dirname(__FILE__) . '/templates/wizard-template.php';
     } catch (Exception $e) {
         if (Instant_Articles_Wizard_State::get_current_state() !== Instant_Articles_Wizard_State::STATE_APP_SETUP) {
             // Restarts the wizard
             Instant_Articles_Wizard_State::do_transition(Instant_Articles_Wizard_State::STATE_APP_SETUP);
             echo '<div class="error settings-error notice is-dismissible"><p><strong>' . esc_html('Error processing your request. Check server log for more details. Setup and login again to renew Application credentials. Error message: ' . $e->getMessage()) . '</strong></p></div>';
             Instant_Articles_Wizard::render($ajax, true);
         }
     }
 }