forked from Automattic/fb-instant-articles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfacebook-instant-articles.php
480 lines (427 loc) · 14.4 KB
/
facebook-instant-articles.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
<?php
/**
* Plugin Name: Instant Articles for WP
* 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: 4.0.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',
'show_version_incompatible_warning'
);
return;
} else {
$autoloader = require __DIR__ . '/vendor/autoload.php';
$autoloader->add( 'Facebook\\', __DIR__ . '/vendor/facebook/facebook-instant-articles-sdk-php/src' );
$autoloader->add( 'Facebook\\', __DIR__ . '/vendor/facebook/facebook-instant-articles-sdk-extensions-in-php/src' );
// Configures log to go through console.
\Logger::configure(
array(
'rootLogger' => array(
'appenders' => array( 'facebook-instantarticles-transformer' ),
),
'appenders' => array(
'facebook-instantarticles-transformer' => array(
'class' => 'LoggerAppenderConsole',
'threshold' => 'INFO',
'layout' => array(
'class' => 'LoggerLayoutSimple',
),
),
'facebook-instantarticles-client' => array(
'class' => 'LoggerAppenderConsole',
'threshold' => 'INFO',
'layout' => array(
'class' => 'LoggerLayoutSimple',
),
),
'instantarticles-wp-plugin' => array(
'class' => 'LoggerAppenderConsole',
'threshold' => 'INFO',
'layout' => array(
'class' => 'LoggerLayoutSimple',
),
),
),
)
);
defined( 'ABSPATH' ) || die( 'Shame on you' );
define( 'IA_PLUGIN_VERSION', '4.0.3' );
define( 'IA_PLUGIN_PATH_FULL', __FILE__ );
define( 'IA_PLUGIN_PATH', plugin_basename( __FILE__ ) );
define( 'IA_PLUGIN_FILE_BASENAME', pathinfo( __FILE__, PATHINFO_FILENAME ) );
define( 'IA_PLUGIN_TEXT_DOMAIN', 'instant-articles' );
define( 'IA_PLUGIN_FORCE_SUBMIT_KEY', 'instant_articles_force_submit' );
// Let users define their own feed slug.
if ( ! defined( 'INSTANT_ARTICLES_SLUG' ) ) {
define( 'INSTANT_ARTICLES_SLUG', 'instant-articles' );
}
require_once( dirname( __FILE__ ) . '/embeds.php' );
require_once( dirname( __FILE__ ) . '/class-instant-articles-post.php' );
require_once( dirname( __FILE__ ) . '/wizard/class-instant-articles-wizard.php' );
require_once( dirname( __FILE__ ) . '/meta-box/class-instant-articles-meta-box.php' );
require_once( dirname( __FILE__ ) . '/class-instant-articles-amp-markup.php' );
/**
* Plugin activation hook to add our rewrite rules.
*
* @since 0.1
*/
function instant_articles_activate() {
instant_articles_init();
flush_rewrite_rules();
}
register_activation_hook( __FILE__, 'instant_articles_activate' );
/**
* Show a message to set up the plugin when it is activated
*/
add_action( 'admin_notices', 'instant_articles_setup_admin_notice' );
add_action( 'network_admin_notices', 'instant_articles_setup_admin_notice' ); // also show message on multisite
function instant_articles_setup_admin_notice() {
global $pagenow;
if ( $pagenow === 'plugins.php' && ! Instant_Articles_Option_FB_Page::get_option_decoded()[ "page_id" ] ) {
$settings_url = Instant_Articles_Wizard::get_url();
echo '<div class="updated settings-error notice is-dismissible">';
echo '<p>Congrats, you\'ve installed the Instant Articles for WP plugin. Now <a href="' . esc_url_raw($settings_url) . '">set it up</a> to start publishing Instant Articles.';
echo '</div>';
}
}
/**
* Plugin activation hook to remove our rewrite rules.
*
* @since 0.1
*/
function instant_articles_deactivate() {
flush_rewrite_rules();
}
register_deactivation_hook( __FILE__, 'instant_articles_deactivate' );
/**
* Load plugin textdomain.
*
* @since 0.1
*/
function instant_articles_load_textdomain() {
load_plugin_textdomain( 'instant-articles', false, plugin_dir_path( __FILE__ ) . '/languages' );
}
add_action( 'plugins_loaded', 'instant_articles_load_textdomain' );
/**
* Plugin hook to load compat layers.
*
* @since 2.0
*/
function instant_articles_load_compat() {
require_once( dirname( __FILE__ ) . '/compat.php' );
}
add_action( 'plugins_loaded', 'instant_articles_load_compat' );
/**
* Register our special feed.
*
* @since 0.1
*/
function instant_articles_init() {
add_feed( INSTANT_ARTICLES_SLUG, 'instant_articles_feed' );
}
add_action( 'init', 'instant_articles_init' );
/**
* Feed display callback.
*
* @since 0.1
*/
function instant_articles_feed() {
// Load the feed template.
include( dirname( __FILE__ ) . '/feed-template.php' );
}
/**
* Whether currently processing an instant article.
*
* @param bool Set the status
* @return bool
*/
function is_transforming_instant_article( $set_status = null ) {
static $is_instant_article = false;
if ( isset( $set_status ) ) {
$is_instant_article = (bool) $set_status;
}
return $is_instant_article;
}
/**
* Modify the main query for our feed.
*
* We want the posts in the modified order, to provide Facebook with content updates even for older posts.
* Facebook will only import 100 posts at the time.
* Facebook will only update posts modified within the last 24 hours
*
* @param WP_Query $query The WP_Query object. Passed by reference.
*/
function instant_articles_query( $query ) {
if ( $query->is_main_query() && $query->is_feed( INSTANT_ARTICLES_SLUG ) ) {
$query->set( 'orderby', 'modified' );
$query->set( 'posts_per_page', 10 );
$query->set( 'posts_per_rss', 10 );
/**
* Filter the post types to include in the query.
*
* Default to `post` only, but allow other post types to be included by the theme/plugins.
*
* @since 2.12
*
* @param array $post_types Array of post types
*/
$post_types = apply_filters( 'instant_articles_post_types', array( 'post' ) );
$query->set( 'post_type', $post_types );
/**
* If the constant INSTANT_ARTICLES_LIMIT_POSTS is set to true, we will limit the feed
* to only include posts which are modified within the last 24 hours.
* Facebook will initially need 100 posts to pass the review, but will only update
* already imported articles if they are modified within the last 24 hours.
*/
if ( defined( 'INSTANT_ARTICLES_LIMIT_POSTS' ) && INSTANT_ARTICLES_LIMIT_POSTS ) {
$query->set( 'date_query', array(
array(
'column' => 'post_modified',
'after' => '1 day ago',
),
) );
}
}
}
add_action( 'pre_get_posts', 'instant_articles_query', 10, 1 );
/**
* Filter the SQL query to not include posts with empty content -- FB will complain.
*
* @since 0.1
* @param string $where The original where part of the SQL statement.
* @param WP_Query $query The WP_Query instance.
* @return string The modified where part of the SQL statement.
*/
function instant_articles_query_where( $where, $query ) {
// Don’t modify the SQL query with a potentially expensive WHERE clause if we’re OK with fewer posts than 100 and are OK with filtering in the loop.
if ( defined( 'INSTANT_ARTICLES_LIMIT_POSTS' ) && INSTANT_ARTICLES_LIMIT_POSTS ) {
return $where;
}
if ( $query->is_main_query() && $query->is_feed( INSTANT_ARTICLES_SLUG ) ) {
global $wpdb;
$where .= " AND {$wpdb->posts}.post_content NOT LIKE ''";
}
return $where;
}
add_filter( 'posts_where' , 'instant_articles_query_where', 10, 2 );
/**
* Register all scripts and styles for the admin UI.
*
* @since 0.4
*/
function instant_articles_register_scripts() {
wp_register_style(
'instant-articles-meta-box',
plugins_url( '/css/instant-articles-meta-box.css', __FILE__ ),
null,
IA_PLUGIN_VERSION,
false
);
wp_register_style(
'instant-articles-settings',
plugins_url( '/css/instant-articles-settings.css', __FILE__ ),
null,
IA_PLUGIN_VERSION,
false
);
wp_register_style(
'instant-articles-wizard',
plugins_url( '/css/instant-articles-wizard.css', __FILE__ ),
null,
IA_PLUGIN_VERSION,
false
);
wp_register_script(
'instant-articles-meta-box',
plugins_url( '/js/instant-articles-meta-box.js', __FILE__ ),
null,
IA_PLUGIN_VERSION,
false
);
wp_register_script(
'instant-articles-option-ads',
plugins_url( '/js/instant-articles-option-ads.js', __FILE__ ),
null,
IA_PLUGIN_VERSION,
false
);
wp_register_script(
'instant-articles-option-analytics',
plugins_url( '/js/instant-articles-option-analytics.js', __FILE__ ),
null,
IA_PLUGIN_VERSION,
false
);
wp_register_script(
'instant-articles-option-publishing',
plugins_url( '/js/instant-articles-option-publishing.js', __FILE__ ),
null,
IA_PLUGIN_VERSION,
false
);
wp_register_script(
'instant-articles-settings',
plugins_url( '/js/instant-articles-settings.js', __FILE__ ),
null,
IA_PLUGIN_VERSION,
false
);
}
add_action( 'init', 'instant_articles_register_scripts' );
/**
* Enqueue all scripts and styles for the admin UI.
*
* @since 0.4
*/
function instant_articles_enqueue_scripts() {
wp_enqueue_style( 'instant-articles-meta-box' );
wp_enqueue_style( 'instant-articles-settings-wizard' );
wp_enqueue_style( 'instant-articles-settings' );
wp_enqueue_style( 'instant-articles-wizard' );
wp_enqueue_script( 'instant-articles-meta-box' );
wp_enqueue_script( 'instant-articles-option-ads' );
wp_enqueue_script( 'instant-articles-option-analytics' );
wp_enqueue_script( 'instant-articles-option-publishing' );
wp_enqueue_script( 'instant-articles-settings' );
wp_enqueue_script( 'instant-articles-wizard' );
}
add_action( 'admin_enqueue_scripts', 'instant_articles_enqueue_scripts' );
/**
* 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
}
}
add_action( 'wp_head', 'inject_url_claiming_meta_tag' );
/**
* Automatically add meta tag for Instant Articles Open Publish scraper
*
* @since 4.0
*/
function inject_ia_markup_meta_tag() {
$post = get_post();
// If there's no current post, return
if ( ! $post ) {
return;
}
// Transform the post to an Instant Article.
$adapter = new Instant_Articles_Post( $post );
if ( $adapter->should_submit_post() ) {
$url = $adapter->get_canonical_url();
$url = add_query_arg( 'ia_markup', '1', $url );
$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 ( $dev_mode ) {
?>
<meta property="ia:markup_url_dev" content="<?php echo esc_attr( $url ); ?>" />
<?php
} else {
?>
<meta property="ia:markup_url" content="<?php echo esc_attr( $url ); ?>" />
<?php
}
}
}
add_action( 'wp_head', 'inject_ia_markup_meta_tag' );
// Injects the <link rel...> tag if the AMP Markup is enabled
add_action( 'wp_head', array('Instant_Articles_AMP_Markup', 'inject_link_rel') );
// Initialize the Instant Articles meta box.
Instant_Articles_Meta_Box::init();
function ia_markup_version( ) {
$post = get_post();
if (isset( $_GET[ 'ia_markup' ] ) && $_GET[ 'ia_markup' ]) {
// Transform the post to an Instant Article.
$adapter = new Instant_Articles_Post( $post );
$article = $adapter->to_instant_article();
echo $article->render( null, true );
die();
}
}
add_action( 'wp', 'ia_markup_version' );
// Add hook for generating the AMP markup
add_action( 'wp', array('Instant_Articles_AMP_Markup', 'markup_version') );
Instant_Articles_Wizard::init();
function rescrape_article( $post_id, $post ) {
$adapter = new Instant_Articles_Post( $post );
$old_slugs = get_post_meta( $post_id, '_wp_old_slug' );
if ( $adapter->should_submit_post() ) {
try {
$client = Facebook\HttpClients\HttpClientsFactory::createHttpClient( null );
$url_encoded = urlencode($adapter->get_canonical_url());
$client->send(
"https://graph.facebook.com/?id=$url_encoded&scrape=true",
'POST',
'',
array(),
60
);
foreach ( $old_slugs as $slug ) {
$clone_post = clone $post;
$clone_post->post_name = $slug;
$clone_adapter = new Instant_Articles_Post( $clone_post );
$url_encoded = urlencode($clone_adapter->get_canonical_url());
$client->send(
"https://graph.facebook.com/?id=$url_encoded&scrape=true",
'POST',
'',
array(),
60
);
}
} catch ( Exception $e ) {
Logger::getLogger( 'instantarticles-wp-plugin' )->error(
'Unable to submit article.',
$e->getTraceAsString()
);
}
}
}
add_action( 'save_post', 'rescrape_article', 999, 2 );
function invalidate_post_transformation_info_cache( $post_id, $post ) {
// These post metas are caches on the calculations made to decide if
// a post is in good state to be converted to an Instant Article or not
delete_post_meta( $post_id, '_has_warnings_after_transformation' );
delete_post_meta( $post_id, '_is_empty_after_transformation' );
}
add_action( 'save_post', 'invalidate_post_transformation_info_cache', 10, 2 );
// We also need to invalidate the transformation caches when the option containing
// the custom transformer rules is updated
function invalidate_all_posts_transformation_info_cache( $option ) {
if ( $option === Instant_Articles_Option_Publishing::OPTION_KEY ) {
// These post metas are caches on the calculations made to decide if
// a post is in good state to be converted to an Instant Article or not
delete_post_meta_by_key( '_has_warnings_after_transformation' );
delete_post_meta_by_key( '_is_empty_after_transformation' );
}
}
add_action( 'updated_option', 'invalidate_all_posts_transformation_info_cache', 10, 1 );
}