forked from whyisjake/Simple-Instant-Articles-for-Facebook
/
simple-fb-instant-articles.php
executable file
·223 lines (181 loc) · 5.46 KB
/
simple-fb-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
<?php
/*
Plugin Name: Simple Facebook Instant Articles
Version: 0.5.0
Description: Add support to Facebook Instant Articles
Author: Jake Spurlock
Author URI: http://jakespurlock.com
*/
require_once( 'includes/functions.php' );
require_once( 'includes/shortcodes.php' );
class Simple_FB_Instant_Articles {
/**
* The one instance of Simple_FB_Instant_Articles.
*
* @var Simple_FB_Instant_Articles
*/
private static $instance;
/**
* Endpoint query var
*/
private $token = 'fb';
/**
* Endpoint query var
*/
private $endpoint = 'fb-instant';
/**
* Instantiate or return the one Simple_FB_Instant_Articles instance.
*
* @return Simple_FB_Instant_Articles
*/
public static function instance( $file = null, $version = '' ) {
if ( is_null( self::$instance ) ) {
self::$instance = new self( $file, $version );
}
return self::$instance;
}
/**
* Template Path
*/
private $template_path;
/**
* Initiate actions.
*
* @return Simple_FB_Instant_Articles
*/
public function __construct( $file, $version ) {
add_action( 'init', array( $this, 'init' ) );
add_action( 'init', array( $this, 'add_feed' ) );
add_action( 'wp', array( $this, 'add_actions' ) );
add_filter( 'pre_get_posts', array( $this, 'pre_get_posts' ) );
add_action( 'simple_fb_before_feed', array( $this, 'pre_render' ) );
add_action( 'simple_fb_pre_render', array( $this, 'pre_render' ) );
// Setup the props.
$this->version = $version;
$this->dir = dirname( $file );
$this->file = $file;
$this->template_path = trailingslashit( $this->dir ) . 'templates/';
$this->home_url = trailingslashit( home_url() );
}
/**
* Kickoff method
* @return void
*/
public function init() {
add_rewrite_endpoint( $this->endpoint, EP_PERMALINK );
register_activation_hook( __FILE__, 'flush_rewrite_rules' );
register_deactivation_hook( __FILE__, 'flush_rewrite_rules' );
}
/**
* Add the template redirect, and maybe more!
*/
function add_actions() {
if ( ! is_singular() ) {
return;
}
if ( false !== get_query_var( $this->endpoint, false ) ) {
add_action( 'template_redirect', array( $this, 'template_redirect' ) );
}
}
/**
* Redirect the template for the Instant Article post.
* @return [type] [description]
*/
public function template_redirect() {
$this->render( get_queried_object_id() );
exit;
}
/**
* Based on the post ID, render the Instant Articles page.
* @param int $post_id Post ID
* @return void
*/
public function render( $post_id ) {
do_action( 'simple_fb_pre_render', $post_id );
if ( have_posts() ) {
the_post();
include( apply_filters( 'simple_fb_article_template_file', $this->template_path . '/article.php' ) );
}
}
/**
* Register FB feed
* @return void
*/
public function add_feed() {
$feed_slug = apply_filters( 'simple_fb_feed_slug', $this->token );
add_feed( $feed_slug, array( $this, 'feed_template' ) );
}
/**
* Modify the query before getting any posts.
*
* @param WP_Query $query WP Query object
*
* @return void
*/
public function pre_get_posts( WP_Query $query ) {
$feed_slug = apply_filters( 'simple_fb_feed_slug', $this->token );
if ( $query->is_main_query() && $query->is_feed( $feed_slug ) ) {
$query->set( 'posts_per_rss', intval( apply_filters( 'simple_fb_posts_per_rss', get_option( 'posts_per_rss', 10 ) ) ) );
// Allow easy access to modify query args for the FB IA feed.
do_action( 'simple_fb_pre_get_posts', $query );
}
}
/**
* Load feed template
* @return void
*/
public function feed_template() {
global $wp_query;
// Prevent 404 on feed
$wp_query->is_404 = false;
status_header( 200 );
$file_name = 'feed.php';
$user_template_file = apply_filters( 'simple_fb_feed_template_file', trailingslashit( get_template_directory() ) . $file_name );
// Any functions hooked in here must NOT output any data or else feed will break
do_action( 'simple_fb_before_feed' );
// Load user feed template if it exists, otherwise use plugin template
if ( file_exists( $user_template_file ) ) {
require( $user_template_file );
} else {
require( $this->template_path . $file_name );
}
// Any functions hooked in here must NOT output any data or else feed will break
do_action( 'simple_fb_after_feed' );
}
/**
* Modify content before render ready for FB IA.
*
* @return void
*/
public function pre_render() {
add_filter( 'the_permalink_rss', array( $this, 'rss_permalink' ) );
// Ensure oEmbeds are in the correct format.
add_filter( 'embed_handler_html', array( $this, 'format_social_embed' ) );
add_filter( 'embed_oembed_html', array( $this, 'format_social_embed' ) );
}
public function rss_permalink( $link ) {
return trailingslashit( $link ) . $this->endpoint;
}
/**
* Ensure oEmbeds are in the correct format.
*
* Social embeds Ref: https://developers.facebook.com/docs/instant-articles/reference/social
*
* @param string $html HTML markup to be embeded into post sontent.
*
* @return string HTML wrapped in FB IA markup for social embeds.
*/
public function format_social_embed( $html ) {
return '<figure class="op-social"><iframe>' . $html . '</iframe></figure>';
}
}
/**
* Instantiate or return the one Simple_FB_Instant_Articles instance.
*
* @return Simple_FB_Instant_Articles
*/
function simple_fb_instant_articles( $file, $version ) {
return Simple_FB_Instant_Articles::instance( $file, $version );
}
// Kick off the plugin on init
simple_fb_instant_articles( __FILE__, '0.5.0' );