/** * Put together the article body for this post * * @since 0.1 * @return string The content */ protected function _get_the_content() { // Try to get the content from a transient, but only if the cached version have the same modtime $cache_mod_time = get_transient('instantarticles_mod_' . $this->get_the_ID()); if ($cache_mod_time == get_post_modified_time('Y-m-d H:i:s', true, $this->get_the_ID())) { $content = get_transient('instantarticles_content_' . $this->get_the_ID()); if ($content !== false && strlen($content)) { return $content; } } global $post, $more; // force $more $orig_more = $more; $more = 1; // If we’re not it the loop or otherwise properly setup $reset_postdata = false; if ($this->get_the_ID() !== $post->ID) { $post = get_post($this->get_the_ID()); setup_postdata($post); $reset_postdata = true; } // Now get the content $content = get_the_content(); /** * Apply the default filter 'the_content' for the post content * * @since 0.1 * @param string $content The current post content. */ $content = apply_filters('the_content', $content); // Maybe cleanup some globals after us? $more = $orig_more; if ($reset_postdata) { wp_reset_postdata(); } // Some people choose to disable wpautop. Due to the Instant Articles spec, we really want it in! $content = wpautop($content); /** * Filter the post content for Instant Articles * * @since 0.1 * @param string $content The post content. */ $content = apply_filters('instant_articles_content', $content); if (class_exists('DOMDocument') && has_action('instant_articles_register_dom_transformation_filters')) { /* If we have filters that wants to work on the DOM, we generate one instance of DOMDocument they can all work on, instead of having to handle the conversion themselves. */ $libxml_previous_state = libxml_use_internal_errors(true); $DOMDocument = new DOMDocument('1.0', get_option('blog_charset')); // 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')); } $result = $DOMDocument->loadHTML('<!doctype html><html><body>' . $content . '</body></html>'); libxml_clear_errors(); libxml_use_internal_errors($libxml_previous_state); if ($result) { // Register the DOM transformation filters if not done yet if (!did_action('instant_articles_register_dom_transformation_filters')) { do_action('instant_articles_register_dom_transformation_filters'); } Instant_Articles_DOM_Transform_Filter_Runner::run($DOMDocument, $this->get_the_ID()); $body = $DOMDocument->getElementsByTagName('body')->item(0); $filtered_content = ''; foreach ($body->childNodes as $node) { $filtered_content .= $DOMDocument->saveXML($node); } $content = $filtered_content; unset($filtered_content); } } // Cache the content set_transient('instantarticles_mod_' . $this->get_the_ID(), get_post_modified_time('Y-m-d H:i:s', true, $this->get_the_ID()), WEEK_IN_SECONDS); set_transient('instantarticles_content_' . $this->get_the_ID(), $content, WEEK_IN_SECONDS); return $content; }
/** * Register included DOM transformation filters * * @since 0.1 */ function instant_articles_register_transformation_filters() { include dirname(__FILE__) . '/dom-transform-filters/class-instant-articles-dom-transform-filter-video.php'; Instant_Articles_DOM_Transform_Filter_Runner::register('Instant_Articles_DOM_Transform_Filter_Video'); include dirname(__FILE__) . '/dom-transform-filters/class-instant-articles-dom-transform-filter-image.php'; Instant_Articles_DOM_Transform_Filter_Runner::register('Instant_Articles_DOM_Transform_Filter_Image'); include dirname(__FILE__) . '/dom-transform-filters/class-instant-articles-dom-transform-filter-blockquote.php'; Instant_Articles_DOM_Transform_Filter_Runner::register('Instant_Articles_DOM_Transform_Filter_Blockquote'); include dirname(__FILE__) . '/dom-transform-filters/class-instant-articles-dom-transform-filter-unordered-list.php'; Instant_Articles_DOM_Transform_Filter_Runner::register('Instant_Articles_DOM_Transform_Filter_Unordered_List'); include dirname(__FILE__) . '/dom-transform-filters/class-instant-articles-dom-transform-filter-ordered-list.php'; Instant_Articles_DOM_Transform_Filter_Runner::register('Instant_Articles_DOM_Transform_Filter_Ordered_List'); include dirname(__FILE__) . '/dom-transform-filters/class-instant-articles-dom-transform-filter-table.php'; Instant_Articles_DOM_Transform_Filter_Runner::register('Instant_Articles_DOM_Transform_Filter_Table'); include dirname(__FILE__) . '/dom-transform-filters/class-instant-articles-dom-transform-filter-address.php'; Instant_Articles_DOM_Transform_Filter_Runner::register('Instant_Articles_DOM_Transform_Filter_Address'); //Instant articles only support h1 and h2. Convert h3-h6 to h2. include dirname(__FILE__) . '/dom-transform-filters/class-instant-articles-dom-transform-filter-heading.php'; Instant_Articles_DOM_Transform_Filter_Runner::register('Instant_Articles_DOM_Transform_Filter_Heading'); // Remove empty elements include dirname(__FILE__) . '/dom-transform-filters/class-instant-articles-dom-transform-filter-emptyelements.php'; Instant_Articles_DOM_Transform_Filter_Runner::register('Instant_Articles_DOM_Transform_Filter_Emptyelements'); }