Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format ingestion options #1

Open
wants to merge 15 commits into
base: default_comment_like
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 38 additions & 13 deletions class-instant-articles-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -636,11 +636,7 @@ public function to_instant_article() {

$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 );
$transformer->transformString( $header, '<h1>' . $title . '</h1>', $blog_charset );
}

if ( $this->has_subtitle() ) {
Expand Down Expand Up @@ -678,15 +674,19 @@ public function to_instant_article() {
->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' );
}
$this->set_appearance_from_settings();

if ( isset( $settings_style['rtl_enabled'] ) ) {
$this->instant_article->enableRTL();
// This block sets as default likes and/or comments based on the configuration setup,
// and call $transformer->transform will consider the defaults before building the Elements
//
// Warning: if you are using pthreads or any other multithreaded engine, consider replicating this to all processes.
if ( isset( $settings_publishing[ 'likes_on_media' ] ) ) {
Image::setDefaultLikeEnabled( $settings_publishing[ 'likes_on_media' ] );
Video::setDefaultLikeEnabled( $settings_publishing[ 'likes_on_media' ] );
}
if ( isset( $settings_publishing[ 'comments_on_media' ] ) ) {
Image::setDefaultCommentEnabled( $settings_publishing[ 'comments_on_media' ] );
Video::setDefaultCommentEnabled( $settings_publishing[ 'comments_on_media' ] );
}

$transformer->transformString( $this->instant_article, $this->get_the_content(), get_option( 'blog_charset' ) );
Expand Down Expand Up @@ -863,6 +863,31 @@ public function add_analytics_from_settings() {
}
}

/**
* Apply appearance settings for an InstantArticle.
*
* @since 3.3
*/
public function set_appearance_from_settings() {
$settings = Instant_Articles_Option_Styles::get_option_decoded();

if ( isset( $settings['article_style'] ) && ! empty( $settings['article_style'] ) ) {
$this->instant_article->withStyle( $settings['article_style'] );
} else {
$this->instant_article->withStyle( 'default' );
}

if ( isset( $settings['copyright'] ) && ! empty( $settings['copyright'] ) ) {
$this->instant_article->withFooter(
Footer::create()->withCopyright( $settings['copyright'] )
);
}

if ( isset( $settings['rtl_enabled'] ) ) {
$this->instant_article->enableRTL();
}
}

/**
* Article <head> style.
*
Expand Down
7 changes: 5 additions & 2 deletions class-instant-articles-publisher.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,16 @@ public static function submit_article( $post_id, $post ) {
$published = apply_filters( 'instant_articles_post_published', true, $post_id );
}

// Checks if it will render article formatted - With spaces and tabs.
$publishArticleFormatted = isset( $publishing_settings[ 'publish_formatted' ] ) ? (bool) $publishing_settings[ 'publish_formatted' ] : false;

try {
// Import the article.
$submission_id = $client->importArticle( $article, $published );
$submission_id = $client->importArticle( $article, $published, true, $publishArticleFormatted );
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 );
$submission_id = $client->importArticle( $article, false, true, $publishArticleFormatted );
update_post_meta( $post_id, self::SUBMISSION_ID_KEY, $submission_id );
}
}
Expand Down
5 changes: 5 additions & 0 deletions compat.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,8 @@
include( dirname( __FILE__ ) . '/compat/class-instant-articles-playbuzz.php' );
$playbuzz = new Instant_Articles_Playbuzz;
$playbuzz->init();

// Load support for Apester's plugin Medias
include( dirname( __FILE__ ) . '/compat/class-instant-articles-apester.php' );
$apester = new Instant_Articles_Apester;
$apester->init();
21 changes: 21 additions & 0 deletions compat/apester-rules-configuration.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"rules": [{
"class": "IgnoreRule",
"selector": "//script[contains(@src,'apester')]"
}, {
"class": "InteractiveRule",
"selector": "div.apester-media",
"properties": {
"interactive.iframe": {
"type": "multiple",
"children": [{
"type": "fragment",
"fragment": "<script type='text/javascript' src='//static.apester.com/js/sdk/latest/apester-sdk.min.js'></script>"
}, {
"type": "element",
"selector": "div.apester-media"
}]
}
}
}]
}
27 changes: 27 additions & 0 deletions compat/class-instant-articles-apester.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/**
* Support class for Apester
*
* @since 3.3.2
*
*/
class Instant_Articles_Apester {

/**
* Init the compat layer
*
*/
function init() {
add_filter( 'instant_articles_transformer_rules_loaded', array( 'Instant_Articles_Apester', 'transformer_loaded' ) );
}

public static function transformer_loaded( $transformer ) {
// Appends more rules to transformer
$file_path = plugin_dir_path( __FILE__ ) . 'apester-rules-configuration.json';
$configuration = file_get_contents( $file_path );
$transformer->loadRules( $configuration );

return $transformer;
}
}
18 changes: 12 additions & 6 deletions facebook-instant-articles.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,27 @@
* Description: Add support for Instant Articles for Facebook to your WordPress site.
* Author: Automattic, Dekode, Facebook
* Author URI: https://vip.wordpress.com/plugins/instant-articles/
* Version: 3.3.2
* Version: 3.3.3
* Text Domain: instant-articles
* License: GPLv2
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
*
* @package default
*/

/**
* Prints error about incompatible version. Extracted as function issue: #390
*
* @since 3.3.4
*/
function show_version_incompatible_warning() {
echo '<div class="error"><p>' .
esc_html__( 'Instant Articles for WP requires PHP 5.4 to function properly. Please upgrade PHP or deactivate Instant Articles for WP.', 'instant-articles' ) . '</p></div>';
}
if ( version_compare( PHP_VERSION, '5.4', '<' ) ) {
add_action(
'admin_notices',
function () {
echo '<div class="error"><p>' .
esc_html__( 'Instant Articles for WP requires PHP 5.4 to function properly. Please upgrade PHP or deactivate Instant Articles for WP.', 'instant-articles' ) . '</p></div>';
}
'show_version_incompatible_warning'
);
return;
} else {
Expand Down Expand Up @@ -61,7 +67,7 @@ function () {

defined( 'ABSPATH' ) || die( 'Shame on you' );

define( 'IA_PLUGIN_VERSION', '3.3.2' );
define( 'IA_PLUGIN_VERSION', '3.3.3' );
define( 'IA_PLUGIN_PATH_FULL', __FILE__ );
define( 'IA_PLUGIN_PATH', plugin_basename( __FILE__ ) );
define( 'IA_PLUGIN_FILE_BASENAME', pathinfo( __FILE__, PATHINFO_FILENAME ) );
Expand Down
18 changes: 16 additions & 2 deletions meta-box/class-instant-articles-meta-box.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,24 @@ public static function register_meta_box() {
* @param Post $post the post request content.
*/
public static function render_meta_box_loader( $post ) {
if ( ! current_user_can( 'edit_post', $post->ID ) ) {
return;
}

include( dirname( __FILE__ ) . '/meta-box-loader-template.php' );
}

/**
* Renderer for the Metabox.
*/
public static function force_submit() {
check_ajax_referer( 'instant-articles-force-submit', 'security' );
$post_id = intval( $_POST[ 'post_ID' ] );

if ( ! current_user_can( 'edit_post', $post_id ) ) {
wp_die( -1, 403 );
}

check_ajax_referer( 'instant-articles-force-submit-' . $post_id, 'security' );
$force = sanitize_text_field( $_POST[ 'force' ] ) === 'true';
update_post_meta( $post_id, Instant_Articles_Publisher::FORCE_SUBMIT_KEY, $force );
Instant_Articles_Publisher::submit_article( $post_id, get_post( $post_id ) );
Expand All @@ -74,8 +83,13 @@ public static function force_submit() {
* 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' ] );

if ( ! current_user_can( 'edit_post', $post_id ) ) {
wp_die( -1, 403 );
}

$ajax_nonce = wp_create_nonce( "instant-articles-force-submit-" . $post_id );
$post = get_post( $post_id );
$adapter = new Instant_Articles_Post( $post );
$article = $adapter->to_instant_article();
Expand Down
2 changes: 1 addition & 1 deletion meta-box/meta-box-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@
</div>
<div>
<label for="transformed">Transformed Markup:</label>
<textarea class="source" readonly><?php echo esc_textarea( $article->render( '', true ) ); ?></textarea>
<textarea class="source" readonly><?php echo esc_textarea( $article->render( null, true ) ); ?></textarea>
</div>
<br clear="all">
</div>
2 changes: 1 addition & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Contributors: trrine, olethomas, bjornjohansen, dekode, automattic, facebook
Tags: instant articles, facebook, mobile
Requires at least: 4.3
Tested up to: 4.6
Stable tag: 3.3.2
Stable tag: 3.3.3
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html

Expand Down
24 changes: 24 additions & 0 deletions wizard/class-instant-articles-option-publishing.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,30 @@ class Instant_Articles_Option_Publishing extends Instant_Articles_Option {
'default' => false,
'checkbox_label' => 'Publish articles containing warnings',
),

'publish_formatted' => array(
'label' => 'Format articles',
'description' => 'With this option enabled, articles will be formatted before ingestion. This helps finding errors and understand the final markup Instant Article document.',
'render' => 'checkbox',
'default' => false,
'checkbox_label' => 'Publish articles formatted',
),

'likes_on_media' => array(
'label' => 'Likes',
'description' => 'With this option enabled, any image or video will have the like action enabled by default.',
'render' => 'checkbox',
'default' => false,
'checkbox_label' => 'Enable like action on images and videos by default',
),

'comments_on_media' => array(
'label' => 'Comments',
'description' => 'With this option enabled, any image or video will have the comments enabled by default.',
'render' => 'checkbox',
'default' => false,
'checkbox_label' => 'Enable comments on images and videos by default',
),
);

/**
Expand Down
10 changes: 8 additions & 2 deletions wizard/class-instant-articles-option-styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,21 @@ class Instant_Articles_Option_Styles extends Instant_Articles_Option {
const OPTION_KEY = 'instant-articles-option-styles';

public static $sections = array(
'title' => 'Styles',
'description' => '<p>Assign your Instant Articles a custom style. To begin, customize a template using the Style Editor. Next, input the name of the style below.</p><p><strong>Note:</strong> If this field is left blank, the plugin will enable the “Default” style. Learn more about Instant Articles style options in the <a href="https://developers.facebook.com/docs/instant-articles/guides/design" target="_blank">Design Guide</a>.</p>',
'title' => 'Appearance',
);

public static $fields = array(

'article_style' => array(
'label' => 'Article Style',
'default' => 'default',
'description' => '<p>Assign your Instant Articles a custom style. To begin, customize a template using the Style Editor. Next, input the name of the style below.</p><p><strong>Note:</strong> If this field is left blank, the plugin will enable the “Default” style. Learn more about Instant Articles style options in the <a href="https://developers.facebook.com/docs/instant-articles/guides/design" target="_blank">Design Guide</a>.</p>',
),

'copyright' => array(
'label' => 'Copyright',
'default' => '',
'description' => 'The copyright details for your articles. Note: No markup tags can be used in this field. <a href="https://developers.facebook.com/docs/instant-articles/reference/footer" target="_blank">Learn more about Footer in Instant Articles</a>.',
),

'rtl_enabled' => array(
Expand Down