/**
  * Renderer for the Metabox.
  */
 public static function render_meta_box()
 {
     $post_id = intval(filter_input(INPUT_POST, 'post_ID'));
     $post = get_post($post_id);
     $adapter = new Instant_Articles_Post($post);
     $article = $adapter->to_instant_article();
     $canonical_url = $adapter->get_canonical_url();
     $submission_status = null;
     $published = 'publish' === $post->post_status;
     Instant_Articles_Wizard::menu_items();
     $settings_page_href = Instant_Articles_Wizard::get_url();
     if ($published) {
         try {
             $fb_app_settings = Instant_Articles_Option_FB_App::get_option_decoded();
             $fb_page_settings = Instant_Articles_Option_FB_Page::get_option_decoded();
             if (isset($fb_app_settings['app_id']) && isset($fb_app_settings['app_secret']) && isset($fb_page_settings['page_access_token']) && isset($fb_page_settings['page_id'])) {
                 // Instantiate a new Client to get the status of this article.
                 $client = Client::create($fb_app_settings['app_id'], $fb_app_settings['app_secret'], $fb_page_settings['page_access_token'], $fb_page_settings['page_id']);
                 $submission_status_id = get_post_meta($post_id, Instant_Articles_Publisher::SUBMISSION_ID_KEY, true);
                 if (!empty($submission_status_id)) {
                     $submission_status = $client->getSubmissionStatus($submission_status_id);
                 } else {
                     // Grab the latest status of this article and display.
                     $article_id = $client->getArticleIDFromCanonicalURL($canonical_url);
                     $submission_status = $client->getLastSubmissionStatus($article_id);
                 }
             }
         } catch (FacebookResponseException $e) {
             $submission_status = null;
         }
     }
     include dirname(__FILE__) . '/meta-box-template.php';
     die;
 }
 public static function getClient()
 {
     if (!static::isPageSet()) {
         return null;
     }
     $fb_app_settings = Instant_Articles_Option_FB_App::get_option_decoded();
     $fb_page_settings = Instant_Articles_Option_FB_Page::get_option_decoded();
     $client = Client::create($fb_app_settings['app_id'], $fb_app_settings['app_secret'], $fb_page_settings['page_access_token'], $fb_page_settings['page_id'], false);
     return $client;
 }
 /**
  * Returns the current step for the wizard.
  *
  * @since 0.5
  */
 public static function get_current_step_id()
 {
     $fb_page_settings = Instant_Articles_Option_FB_Page::get_option_decoded();
     $fb_app_settings = Instant_Articles_Option_FB_App::get_option_decoded();
     if (empty($fb_app_settings['app_id']) || empty($fb_app_settings['app_secret'])) {
         return self::STEP_ID_APP_SETUP;
     } elseif (empty($fb_page_settings['page_id']) || empty($fb_page_settings['page_name'])) {
         return self::STEP_ID_PAGE_SELECTION;
     } else {
         return self::STEP_ID_DONE;
     }
 }
 /**
  * Renderer for the Metabox.
  */
 public static function render_meta_box()
 {
     $ajax_nonce = wp_create_nonce("instant-articles-force-submit");
     $post_id = intval($_POST['post_ID']);
     $post = get_post($post_id);
     $adapter = new Instant_Articles_Post($post);
     $article = $adapter->to_instant_article();
     $canonical_url = $adapter->get_canonical_url();
     $submission_status = null;
     $published = 'publish' === $post->post_status;
     $dev_mode = false;
     $force_submit = get_post_meta($post_id, Instant_Articles_Publisher::FORCE_SUBMIT_KEY, true);
     $should_submit_post = apply_filters('instant_articles_should_submit_post', true, $adapter);
     Instant_Articles_Wizard::menu_items();
     $settings_page_href = Instant_Articles_Wizard::get_url();
     $publishing_settings = Instant_Articles_Option_Publishing::get_option_decoded();
     $publish_with_warnings = $publishing_settings['publish_with_warnings'];
     if ($published) {
         try {
             $fb_app_settings = Instant_Articles_Option_FB_App::get_option_decoded();
             $fb_page_settings = Instant_Articles_Option_FB_Page::get_option_decoded();
             $publishing_settings = Instant_Articles_Option_Publishing::get_option_decoded();
             $dev_mode = isset($publishing_settings['dev_mode']) ? $publishing_settings['dev_mode'] ? true : false : false;
             if (isset($fb_app_settings['app_id']) && isset($fb_app_settings['app_secret']) && isset($fb_page_settings['page_access_token']) && isset($fb_page_settings['page_id'])) {
                 // Instantiate a new Client to get the status of this article.
                 $client = Client::create($fb_app_settings['app_id'], $fb_app_settings['app_secret'], $fb_page_settings['page_access_token'], $fb_page_settings['page_id'], $dev_mode);
                 $submission_status_id = get_post_meta($post_id, Instant_Articles_Publisher::SUBMISSION_ID_KEY, true);
                 if (!empty($submission_status_id)) {
                     $submission_status = $client->getSubmissionStatus($submission_status_id);
                 } else {
                     // Grab the latest status of this article and display.
                     $article_id = $client->getArticleIDFromCanonicalURL($canonical_url);
                     $submission_status = $client->getLastSubmissionStatus($article_id);
                 }
             }
         } catch (FacebookResponseException $e) {
             $submission_status = null;
         }
     }
     include dirname(__FILE__) . '/meta-box-template.php';
     die;
 }
    /**
     * Automatically add meta tag for Instant Articles URL claiming
     * when page is set.
     *
     * @since 0.4
     */
    function inject_url_claiming_meta_tag()
    {
        $publishing_settings = Instant_Articles_Option_Publishing::get_option_decoded();
        $fb_page_settings = Instant_Articles_Option_FB_Page::get_option_decoded();
        if (isset($fb_page_settings['page_id'])) {
            ?>
			<meta property="fb:pages" content="<?php 
            echo esc_attr($fb_page_settings['page_id']);
            ?>
" />
			<?php 
        }
    }
 /**
  * Submits article to Instant Articles.
  *
  * @param string $post_id The identifier of post.
  * @param Post   $post The WP Post.
  */
 public static function submit_article($post_id, $post)
 {
     // Don't process if this is just a revision or an autosave.
     if (wp_is_post_revision($post) || wp_is_post_autosave($post)) {
         return;
     }
     // Don't process if this post is not published
     if ('publish' !== $post->post_status) {
         return;
     }
     // Only process posts
     $post_types = apply_filters('instant_articles_post_types', array('post'));
     if (!in_array($post->post_type, $post_types)) {
         return;
     }
     // Transform the post to an Instant Article.
     $adapter = new Instant_Articles_Post($post);
     $article = $adapter->to_instant_article();
     // Skip empty articles or articles missing title.
     // This is important because the save_post action is also triggered by bulk updates, but in this case
     // WordPress does not load the content field from DB for performance reasons. In this case, articles
     // will be empty here, despite of them actually having content.
     if (count($article->getChildren()) === 0 || !$article->getHeader() || !$article->getHeader()->getTitle()) {
         return;
     }
     // Instantiate an API client.
     try {
         $fb_app_settings = Instant_Articles_Option_FB_App::get_option_decoded();
         $fb_page_settings = Instant_Articles_Option_FB_Page::get_option_decoded();
         $publishing_settings = Instant_Articles_Option_Publishing::get_option_decoded();
         $dev_mode = isset($publishing_settings['dev_mode']) ? $publishing_settings['dev_mode'] ? true : false : false;
         if (isset($fb_app_settings['app_id']) && isset($fb_app_settings['app_secret']) && isset($fb_page_settings['page_access_token']) && isset($fb_page_settings['page_id'])) {
             $client = Client::create($fb_app_settings['app_id'], $fb_app_settings['app_secret'], $fb_page_settings['page_access_token'], $fb_page_settings['page_id'], $dev_mode);
             // Don't publish posts with password protection
             if (post_password_required($post)) {
                 // Unpublishes if already published and from now on it started to have password protection
                 $client->removeArticle($article->getCanonicalURL());
                 delete_post_meta($post_id, self::SUBMISSION_ID_KEY);
                 return;
             }
             // Don't process if contains warnings and blocker flag for transformation warnings is turned on.
             if (count($adapter->transformer->getWarnings()) > 0 && isset($publishing_settings['block_publish_with_warnings']) && $publishing_settings['block_publish_with_warnings']) {
                 // Unpublishes if already published
                 $client->removeArticle($article->getCanonicalURL());
                 delete_post_meta($post_id, self::SUBMISSION_ID_KEY);
                 return;
             }
             if ($dev_mode) {
                 $published = false;
             } else {
                 // Any publish status other than 'publish' means draft for the Instant Article.
                 $published = true;
             }
             try {
                 // Import the article.
                 $submission_id = $client->importArticle($article, $published);
                 update_post_meta($post_id, self::SUBMISSION_ID_KEY, $submission_id);
             } catch (Exception $e) {
                 // Try without taking live for pages not yet reviewed.
                 $submission_id = $client->importArticle($article, false);
                 update_post_meta($post_id, self::SUBMISSION_ID_KEY, $submission_id);
             }
         }
     } catch (Exception $e) {
         Logger::getLogger('instantarticles-wp-plugin')->error('Unable to submit article.', $e->getTraceAsString());
     }
 }
 /**
  * Transition for when the user decides to change the selected page.
  */
 private static function transition_edit_page()
 {
     Instant_Articles_Option_FB_Page::delete_option();
     // For backwards compatibility, we transition one step back
     // for users of the old versions that selected a page but the
     // plugin didn't save the user access token. In this case
     // we need the user to log in again.
     $fb_app_settings = Instant_Articles_Option_FB_App::get_option_decoded();
     $user_logged_in = !empty($fb_app_settings['user_access_token']);
     if (!$user_logged_in) {
         return update_option('instant-articles-current-state', self::STATE_APP_SETUP);
     }
     return update_option('instant-articles-current-state', self::STATE_PAGE_SELECTION);
 }
 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';
 }
 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);
         }
     }
 }
			<div>
				<a href="<?php 
        echo esc_attr($fb_helper->get_login_url());
        ?>
">
					<img  src="https://fbcdn-dragon-a.akamaihd.net/hphotos-ak-xtf1/t39.2178-6/11405239_920140564714397_256329502_n.png">
				</a>
			</div>

		<?php 
    } else {
        ?>

			<?php 
        $helper = new Facebook\InstantArticles\Client\Helper($fb_helper->fb_sdk);
        $fb_page_settings = Instant_Articles_Option_FB_Page::get_option_decoded();
        // Map GraphNode objects to simple value objects that are smaller when serialized.
        $pages_and_tokens = array_map(function ($page_node) {
            return (object) array('page_id' => $page_node->getField('id'), 'page_name' => $page_node->getField('name'), 'page_access_token' => $page_node->getField('access_token'), 'supports_instant_articles' => $page_node->getField('supports_instant_articles'));
        }, $helper->getPagesAndTokens($access_token)->all());
        $pages_and_tokens = array_filter($pages_and_tokens, function ($page) {
            return $page->supports_instant_articles;
        });
        ?>

			<?php 
        if (!empty($pages_and_tokens)) {
            ?>
				<p>Select the Facebook Pages where you will publish Instant Articles:</p>

				<select id="<?php