Instant Articles for WP - Version 4.0.2

Version Description

Download this release

Release Info

Developer diegoquinteiro
Plugin Icon 128x128 Instant Articles for WP
Version 4.0.2
Comparing to
See all releases

Code changes from version 4.0.1 to 4.0.2

CHANGELOG.md CHANGED
@@ -1,6 +1,10 @@
1
  ## Change Log
2
 
3
- ### 4.0.1 (2017/06/28 18:41 +00:00)
 
 
 
 
4
  - [#706](https://github.com/automattic/facebook-instant-articles-wp/pull/706) Check for array index before using it (@diegoquinteiro)
5
  - [#705](https://github.com/automattic/facebook-instant-articles-wp/pull/705) Enable deletion of JSON AMP Style and removes an undef index (@vkama)
6
  - [#704](https://github.com/automattic/facebook-instant-articles-wp/pull/704) Fixed several php notices. Also fixed a bug in should_subit_post() (@vkama)
1
  ## Change Log
2
 
3
+ ### 4.0.2 (2017/06/30 19:32 +00:00)
4
+ - [#708](https://github.com/automattic/facebook-instant-articles-wp/pull/708) Do not process non-post pages. Fixes #707 (@diegoquinteiro)
5
+ - [#709](https://github.com/automattic/facebook-instant-articles-wp/pull/709) Add cache layer for avoiding transforming the article at page render (@diegoquinteiro)
6
+
7
+ ### 4.0.1 (2017/06/28 18:48 +00:00)
8
  - [#706](https://github.com/automattic/facebook-instant-articles-wp/pull/706) Check for array index before using it (@diegoquinteiro)
9
  - [#705](https://github.com/automattic/facebook-instant-articles-wp/pull/705) Enable deletion of JSON AMP Style and removes an undef index (@vkama)
10
  - [#704](https://github.com/automattic/facebook-instant-articles-wp/pull/704) Fixed several php notices. Also fixed a bug in should_subit_post() (@vkama)
class-instant-articles-post.php CHANGED
@@ -871,18 +871,65 @@ class Instant_Articles_Post {
871
  }
872
  }
873
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
874
  /**
875
  * Returns whether the article should be ingested as an Instant Article.
876
  */
877
  public function should_submit_post() {
878
 
879
- $post = get_post( $this->get_the_id() );
880
-
881
  $fb_page_settings = Instant_Articles_Option_FB_Page::get_option_decoded();
882
  if ( isset( $fb_page_settings[ 'page_id' ] ) && !$fb_page_settings[ 'page_id' ] ) {
883
  return false;
884
  }
885
 
 
 
886
  // Don't process if this is just a revision or an autosave.
887
  if ( wp_is_post_revision( $post ) || wp_is_post_autosave( $post ) ) {
888
  return false;
@@ -899,38 +946,34 @@ class Instant_Articles_Post {
899
  return false;
900
  }
901
 
902
- // Transform the post to an Instant Article.
903
- $adapter = new Instant_Articles_Post( $post );
904
- $instant_article = $this->to_instant_article();
 
 
 
 
 
 
905
 
906
  // Skip empty articles or articles missing title.
907
  // This is important because the save_post action is also triggered by bulk updates, but in this case
908
  // WordPress does not load the content field from DB for performance reasons. In this case, articles
909
  // will be empty here, despite of them actually having content.
910
- if ( count( $instant_article->getChildren() ) === 0 || ! $instant_article->getHeader() || ! $instant_article->getHeader()->getTitle() ) {
911
- return false;
912
- }
913
-
914
- // Don't publish posts with password protection
915
- if ( post_password_required( $post ) ) {
916
  return false;
917
  }
918
 
919
  // Don't process if contains warnings and blocker flag for transformation warnings is turned on.
920
  $publishing_settings = Instant_Articles_Option_Publishing::get_option_decoded();
921
  $force_submit = get_post_meta( $post->ID, IA_PLUGIN_FORCE_SUBMIT_KEY, true );
922
- if ( count( $this->transformer->getWarnings() ) > 0
923
  && ( ! isset( $publishing_settings[ 'publish_with_warnings' ] ) || ! $publishing_settings[ 'publish_with_warnings' ] )
924
  && ( ! $force_submit )
925
  ) {
926
  return false;
927
  }
928
 
929
- // Allow to disable post submit via filter
930
- if ( false === apply_filters( 'instant_articles_should_submit_post', true, $adapter ) ) {
931
- return false;
932
- }
933
-
934
  return true;
935
  }
936
 
871
  }
872
  }
873
 
874
+ /**
875
+ * Returns whether the transformation results in an empty document
876
+ */
877
+ public function is_empty_after_transformation() {
878
+ // This post meta is a cache on the calculations made by this function
879
+ $cache = get_post_meta( $this->get_the_id(), '_is_empty_after_transformation', true );
880
+ if ( $cache ) {
881
+ // We use 'yes' or 'no' to avoid booleans because
882
+ // get_post_meta() returns false when the key is not found
883
+ return ( $cache === 'yes' );
884
+ }
885
+
886
+ $instant_article = $this->to_instant_article();
887
+ // Skip empty articles or articles missing title.
888
+ // This is important because the save_post action is also triggered by bulk updates, but in this case
889
+ // WordPress does not load the content field from DB for performance reasons. In this case, articles
890
+ // will be empty here, despite of them actually having content.
891
+ if ( count( $instant_article->getChildren() ) === 0 || ! $instant_article->getHeader() || ! $instant_article->getHeader()->getTitle() ) {
892
+ update_post_meta( $this->get_the_id(), '_is_empty_after_transformation', 'yes' );
893
+ return true;
894
+ }
895
+ update_post_meta( $this->get_the_id(), '_is_empty_after_transformation', 'no' );
896
+ return false;
897
+ }
898
+
899
+
900
+ /**
901
+ * Returns whether the transformation raises warnings
902
+ */
903
+ public function has_warnings_after_transformation() {
904
+ // This post meta is a cache on the calculations made by this function
905
+ $cache = get_post_meta( $this->get_the_id(), '_has_warnings_after_transformation', true );
906
+ if ( $cache ) {
907
+ // We use 'yes' or 'no' to avoid booleans because
908
+ // get_post_meta() returns false when the key is not found
909
+ return ( $cache === 'yes' );
910
+ }
911
+
912
+ $instant_article = $this->to_instant_article();
913
+ if ( count( $this->transformer->getWarnings() ) > 0 ) {
914
+ update_post_meta( $this->get_the_id(), '_has_warnings_after_transformation', 'yes' );
915
+ return true;
916
+ }
917
+ update_post_meta( $this->get_the_id(), '_has_warnings_after_transformation', 'no' );
918
+ return false;
919
+ }
920
+
921
  /**
922
  * Returns whether the article should be ingested as an Instant Article.
923
  */
924
  public function should_submit_post() {
925
 
 
 
926
  $fb_page_settings = Instant_Articles_Option_FB_Page::get_option_decoded();
927
  if ( isset( $fb_page_settings[ 'page_id' ] ) && !$fb_page_settings[ 'page_id' ] ) {
928
  return false;
929
  }
930
 
931
+ $post = $this->_post;
932
+
933
  // Don't process if this is just a revision or an autosave.
934
  if ( wp_is_post_revision( $post ) || wp_is_post_autosave( $post ) ) {
935
  return false;
946
  return false;
947
  }
948
 
949
+ // Don't publish posts with password protection
950
+ if ( post_password_required( $post ) ) {
951
+ return false;
952
+ }
953
+
954
+ // Allow to disable post submit via filter
955
+ if ( false === apply_filters( 'instant_articles_should_submit_post', true, $this ) ) {
956
+ return false;
957
+ }
958
 
959
  // Skip empty articles or articles missing title.
960
  // This is important because the save_post action is also triggered by bulk updates, but in this case
961
  // WordPress does not load the content field from DB for performance reasons. In this case, articles
962
  // will be empty here, despite of them actually having content.
963
+ if ( $this->is_empty_after_transformation() ) {
 
 
 
 
 
964
  return false;
965
  }
966
 
967
  // Don't process if contains warnings and blocker flag for transformation warnings is turned on.
968
  $publishing_settings = Instant_Articles_Option_Publishing::get_option_decoded();
969
  $force_submit = get_post_meta( $post->ID, IA_PLUGIN_FORCE_SUBMIT_KEY, true );
970
+ if ( $this->has_warnings_after_transformation()
971
  && ( ! isset( $publishing_settings[ 'publish_with_warnings' ] ) || ! $publishing_settings[ 'publish_with_warnings' ] )
972
  && ( ! $force_submit )
973
  ) {
974
  return false;
975
  }
976
 
 
 
 
 
 
977
  return true;
978
  }
979
 
facebook-instant-articles.php CHANGED
@@ -4,7 +4,7 @@
4
  * Description: Add support for Instant Articles for Facebook to your WordPress site.
5
  * Author: Automattic, Dekode, Facebook
6
  * Author URI: https://vip.wordpress.com/plugins/instant-articles/
7
- * Version: 4.0.1
8
  * Text Domain: instant-articles
9
  * License: GPLv2
10
  * License URI: http://www.gnu.org/licenses/gpl-2.0.html
@@ -68,7 +68,7 @@ if ( version_compare( PHP_VERSION, '5.4', '<' ) ) {
68
 
69
  defined( 'ABSPATH' ) || die( 'Shame on you' );
70
 
71
- define( 'IA_PLUGIN_VERSION', '4.0.1' );
72
  define( 'IA_PLUGIN_PATH_FULL', __FILE__ );
73
  define( 'IA_PLUGIN_PATH', plugin_basename( __FILE__ ) );
74
  define( 'IA_PLUGIN_FILE_BASENAME', pathinfo( __FILE__, PATHINFO_FILENAME ) );
@@ -364,6 +364,12 @@ if ( version_compare( PHP_VERSION, '5.4', '<' ) ) {
364
  */
365
  function inject_ia_markup_meta_tag() {
366
  $post = get_post();
 
 
 
 
 
 
367
  // Transform the post to an Instant Article.
368
  $adapter = new Instant_Articles_Post( $post );
369
  if ( $adapter->should_submit_post() ) {
@@ -451,5 +457,24 @@ if ( version_compare( PHP_VERSION, '5.4', '<' ) ) {
451
  }
452
  add_action( 'save_post', 'rescrape_article', 999, 2 );
453
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
454
 
455
  }
4
  * Description: Add support for Instant Articles for Facebook to your WordPress site.
5
  * Author: Automattic, Dekode, Facebook
6
  * Author URI: https://vip.wordpress.com/plugins/instant-articles/
7
+ * Version: 4.0.2
8
  * Text Domain: instant-articles
9
  * License: GPLv2
10
  * License URI: http://www.gnu.org/licenses/gpl-2.0.html
68
 
69
  defined( 'ABSPATH' ) || die( 'Shame on you' );
70
 
71
+ define( 'IA_PLUGIN_VERSION', '4.0.2' );
72
  define( 'IA_PLUGIN_PATH_FULL', __FILE__ );
73
  define( 'IA_PLUGIN_PATH', plugin_basename( __FILE__ ) );
74
  define( 'IA_PLUGIN_FILE_BASENAME', pathinfo( __FILE__, PATHINFO_FILENAME ) );
364
  */
365
  function inject_ia_markup_meta_tag() {
366
  $post = get_post();
367
+
368
+ // If there's no current post, return
369
+ if ( ! $post ) {
370
+ return;
371
+ }
372
+
373
  // Transform the post to an Instant Article.
374
  $adapter = new Instant_Articles_Post( $post );
375
  if ( $adapter->should_submit_post() ) {
457
  }
458
  add_action( 'save_post', 'rescrape_article', 999, 2 );
459
 
460
+ function invalidate_post_transformation_info_cache( $post_id, $post ) {
461
+ // These post metas are caches on the calculations made to decide if
462
+ // a post is in good state to be converted to an Instant Article or not
463
+ delete_post_meta( $post_id, '_has_warnings_after_transformation' );
464
+ delete_post_meta( $post_id, '_is_empty_after_transformation' );
465
+ }
466
+ add_action( 'save_post', 'invalidate_post_transformation_info_cache', 10, 2 );
467
+
468
+ // We also need to invalidate the transformation caches when the option containing
469
+ // the custom transformer rules is updated
470
+ function invalidate_all_posts_transformation_info_cache( $option ) {
471
+ if ( $option === Instant_Articles_Option_Publishing::OPTION_KEY ) {
472
+ // These post metas are caches on the calculations made to decide if
473
+ // a post is in good state to be converted to an Instant Article or not
474
+ delete_post_meta_by_key( '_has_warnings_after_transformation' );
475
+ delete_post_meta_by_key( '_is_empty_after_transformation' );
476
+ }
477
+ }
478
+ add_action( 'updated_option', 'invalidate_all_posts_transformation_info_cache', 10, 1 );
479
 
480
  }
readme.txt CHANGED
@@ -3,7 +3,7 @@ Contributors: trrine, olethomas, bjornjohansen, dekode, automattic, facebook
3
  Tags: instant articles, facebook, mobile
4
  Requires at least: 4.3
5
  Tested up to: 4.8
6
- Stable tag: 4.0.1
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
 
@@ -85,7 +85,11 @@ Usually simply visiting the permalinks settings page in the WordPress dashboard
85
 
86
  == Changelog ==
87
 
88
- ### 4.0.1 (2017/06/28 18:41 +00:00)
 
 
 
 
89
  - [#706](https://github.com/automattic/facebook-instant-articles-wp/pull/706) Check for array index before using it (@diegoquinteiro)
90
  - [#705](https://github.com/automattic/facebook-instant-articles-wp/pull/705) Enable deletion of JSON AMP Style and removes an undef index (@vkama)
91
  - [#704](https://github.com/automattic/facebook-instant-articles-wp/pull/704) Fixed several php notices. Also fixed a bug in should_subit_post() (@vkama)
3
  Tags: instant articles, facebook, mobile
4
  Requires at least: 4.3
5
  Tested up to: 4.8
6
+ Stable tag: 4.0.2
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
 
85
 
86
  == Changelog ==
87
 
88
+ ### 4.0.2 (2017/06/30 19:32 +00:00)
89
+ - [#708](https://github.com/automattic/facebook-instant-articles-wp/pull/708) Do not process non-post pages. Fixes #707 (@diegoquinteiro)
90
+ - [#709](https://github.com/automattic/facebook-instant-articles-wp/pull/709) Add cache layer for avoiding transforming the article at page render (@diegoquinteiro)
91
+
92
+ ### 4.0.1 (2017/06/28 18:48 +00:00)
93
  - [#706](https://github.com/automattic/facebook-instant-articles-wp/pull/706) Check for array index before using it (@diegoquinteiro)
94
  - [#705](https://github.com/automattic/facebook-instant-articles-wp/pull/705) Enable deletion of JSON AMP Style and removes an undef index (@vkama)
95
  - [#704](https://github.com/automattic/facebook-instant-articles-wp/pull/704) Fixed several php notices. Also fixed a bug in should_subit_post() (@vkama)