-
Notifications
You must be signed in to change notification settings - Fork 0
/
facts.html
251 lines (187 loc) · 6.47 KB
/
facts.html
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
<?php
/*
Plugin Name: Facts
Description: Enter facts for Truth Teller
Version: 1.0
Author: Yuri Victor ( [email protected] )
License: MIT (attached)
*/
if ( ! class_exists( 'Facts' ) ):
final class Facts {
/** Constants *************************************************************/
const name = 'Facts'; // human-readable name of plugin
const key = 'facts'; // plugin slug, generally base filename and url endpoint
const version = '1.0';
/** Variables *************************************************************/
var $facts; // Array of objects
/** Load Methods **********************************************************/
/**
* Register with WordPress API on Construct
*
* @uses add_action() to hook methods into WordPress actions
*
*/
function __construct() {
self::includes();
self::add_actions();
//self::add_admin_actions();
}
/**
* Include the necessary files
*/
private static function includes() {
//require( dirname( __FILE__ ) . '/classes/class-facts-admin.php' );
}
/**
* Hook actions in that run on every page-load
*
* @uses add_action()
*/
private static function add_actions() {
add_action( 'init', array( __CLASS__, 'init' ) );
add_action( 'init', array( __CLASS__, 'register_cpt' ) );
add_action( 'init', array( __CLASS__, '_endpoints_add_endpoint' ) );
add_action( 'template_redirect', array( __CLASS__, '_endpoints_template_redirect' ) );
}
/**
* Hook actions in that run on every admin page-load
*
* @uses is_admin()
* @return if not in admin area
*/
private function add_admin_actions() {
// Bail if not in admin area
if ( ! is_admin() )
return;
$this->admin = new Facts_Admin( self::key, $this );
}
/** Public Methods ********************************************************/
/**
* Truth Teller initialization functions.
*
* This is where Truth Teller sets up any additional things it needs to run
* inside of WordPress.
*/
public static function init() {
register_activation_hook( __FILE__, '_endpoints_activate' );
register_deactivation_hook( __FILE__, '_endpoints_deactivate' );
}
/**
* Register "facts" custom post type
*
* @uses register_post_type
*/
public function register_cpt() {
$labels = array(
'name' => __( 'Facts', self::key ),
'singular_name' => __( 'Fact', self::key ),
'add_new' => __( 'Add fact', self::key ),
'add_new_item' => __( 'Add Plain English', self::key ),
'edit_item' => __( 'Edit Plain English', self::key )
);
$args = array(
'labels' => $labels,
'public' => false,
'show_ui' => true,
'supports' => array( 'editor', 'custom-fields' ),
'rewrite' => false,
'taxonomies' => array( 'post_tag' ),
);
register_post_type( self::key, $args );
}
/**
* Activate "facts" url for posts
*
* @uses flush_rewrite_rules
*/
public function _endpoints_activate() {
self::_endpoints_add_endpoint();
flush_rewrite_rules();
}
/**
* Add "facts" url for posts
*
* @uses add_rewrite_endpoint
*/
public function _endpoints_add_endpoint() {
add_rewrite_endpoint( 'facts', EP_PERMALINK | EP_PAGES );
}
/**
* Removes "facts" url for posts
*
* @uses flush_rewrite_rules
*/
public function _endpoints_deactivate() {
flush_rewrite_rules();
}
/**
* Redirects users to "facts" json
*
* @uses flush_rewrite_rules
* @param $wp_query array of query_vars
* @return if there is no "fact" query variable
*/
public function _endpoints_template_redirect() {
global $wp_query;
// if this is not a request for json or it's not a singular object then bail
if ( ! isset( $wp_query->query_vars['facts'] ) )
return;
self::_endpoints_do_json();
exit;
}
/**
* Create json feed of facts
*
* @uses flush_rewrite_rules
* @param $post array of posts
* @return if not in a post or missing the "fact" query variable
*
* plain_english, keywords, assertion( true, false ), source_url
*
*/
public function _endpoints_do_json() {
header( 'Content-Type: application/json' );
$query = new WP_Query( array(
'post_type' => 'facts',
'posts_per_page' => -1
) );
$count = 1;
$total = count( $query->posts );
$json = '{"facts":[';
foreach( $query->posts as $post ) {
$fact = array();
$fact['id'] = $post->ID;
$fact['plain_english'] = $post->post_content;
$fact['keywords'] = wp_get_post_tags( $post->ID, array( 'fields' => 'names' ) );
$fact['assertion'] = get_post_meta( $post->ID, 'assertion', true );
$fact['source_url'] = get_post_meta( $post->ID, 'URL', true );
$fact['source_org'] = get_post_meta( $post->ID, 'source_org', true );
$fact['speaker'] = get_post_meta( $post->ID, 'speaker', true );
$fact['party'] = get_post_meta( $post->ID, 'party', true );
$fact['complicated'] = get_post_meta( $post->ID, 'complicated', true );
$fact['post_title'] = get_post_meta( $post->ID, 'post_title', true );
$json .= json_encode( $fact );
if ( $count != $total ) {
$json .= ',';
}
$count++;
}
$json .= ']}';
echo $json;
}
/**
* Load a template. MVC FTW!
* @param string $template the template to load, without extension (assumes .php). File should be in templates/ folder
* @param args array of args to be run through extract and passed to template
*/
public function template( $template, $args = array() ) {
extract( $args );
if ( ! $template )
return false;
$path = dirname( __FILE__ ) . "/templates/{$template}.php";
$path = apply_filters( 'truthteller', $path, $template );
include $path;
}
}
$facts = new Facts();
endif;