Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 837 lines (697 sloc) 26.947 kb
8ff4206 Mohammad Jangda v1
mjangda authored
1 <?php
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
2
8ff4206 Mohammad Jangda v1
mjangda authored
3 /**
4 * Plugin Name: Liveblog
1fc681a Nikolay Bachiyski Add metadata URLs
nb authored
5 * Plugin URI: http://wordpress.org/extend/plugins/liveblog/
8ff4206 Mohammad Jangda v1
mjangda authored
6 * Description: Blogging: at the speed of live.
aa5c82c Nikolay Bachiyski Bump 1.2
nb authored
7 * Version: 1.2
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
8 * Author: WordPress.com VIP, Automattic
1fc681a Nikolay Bachiyski Add metadata URLs
nb authored
9 * Author URI: http://vip.wordpress.com/
10 * Text Domain: liveblog
8ff4206 Mohammad Jangda v1
mjangda authored
11 */
12
13 if ( ! class_exists( 'WPCOM_Liveblog' ) ) :
14
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
15 /**
16 * The main Liveblog class used to setup everything this plugin needs.
17 *
18 * Liveblog currently uses a custom comment-type to circumvent post cache
19 * issues frequently experienced by other live-blog implimentations. It comes
20 * with a simple and effective templating mechanism, complete with all of the
21 * CSS, JS, and AJAX needed to make this a turn-key installation.
22 *
23 * This class is a big container for a bunch of static methods, similar to a
24 * factory but without object inheritance or instantiation.
8413aff John James Jacoby Uploading:
johnjamesjacoby authored
25 *
5ad84ba Nikolay Bachiyski Fix typo
nb authored
26 * Things yet to be implemented:
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
27 *
28 * -- Change "Read More" to "View Liveblog"
29 * -- Manual refresh button
30 * -- Allow marking of liveblog as ended
31 * -- Allow comment modifications; need to store modified date as comment_meta
32 */
33 final class WPCOM_Liveblog {
34
35 /** Constants *************************************************************/
8ff4206 Mohammad Jangda v1
mjangda authored
36
aa5c82c Nikolay Bachiyski Bump 1.2
nb authored
37 const version = '1.2';
1d5d7f9 Nikolay Bachiyski Flush the rewrite rules on rules update
nb authored
38 const rewrites_version = 1;
cd6d574 John James Jacoby * Code clean up.
johnjamesjacoby authored
39 const key = 'liveblog';
40 const url_endpoint = 'liveblog';
41 const edit_cap = 'publish_posts';
42 const nonce_key = 'liveblog_nonce';
8ff4206 Mohammad Jangda v1
mjangda authored
43
1d7244f Mohammad Jangda Increase the default refresh interval
mjangda authored
44 const refresh_interval = 10; // how often should we refresh
ed1991c Nikolay Bachiyski Shut the auto-update only on consecutive failures
nb authored
45 const max_consecutive_retries = 100; // max number of failed tries before polling is disabled
46 const delay_threshold = 5; // how many failed tries after which we should increase the refresh interval
47 const delay_multiplier = 2; // by how much should we inscrease the refresh interval
8ff4206 Mohammad Jangda v1
mjangda authored
48
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
49 /** Variables *************************************************************/
50
51 private static $post_id = null;
52 private static $entry_query = null;
53 private static $do_not_cache_response = false;
1b8e8d5 Nikolay Bachiyski Streamline logic for getting recent entries
nb authored
54
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
55 /** Load Methods **********************************************************/
56
57 /**
58 * @uses add_action() to hook methods into WordPress actions
59 * @uses add_filter() to hook methods into WordPress filters
60 */
61 public static function load() {
e5351e5 Mohammad Jangda load_plugin_textdomain so the plugin can be translated.
mjangda authored
62 load_plugin_textdomain( 'liveblog', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
63
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
64 self::includes();
65 self::add_actions();
66 self::add_filters();
67 self::add_admin_actions();
68 }
69
70 /**
71 * Include the necessary files
72 */
73 private static function includes() {
74 require( dirname( __FILE__ ) . '/classes/class-wpcom-liveblog-entry.php' );
75 require( dirname( __FILE__ ) . '/classes/class-wpcom-liveblog-entry-query.php' );
e691fe3 John James Jacoby Include ms.php theme-side for its filesize and available space functions...
johnjamesjacoby authored
76
77 // Manually include ms.php theme-side in multisite environments because
78 // we need its filesize and available space functions.
79 if ( ! is_admin() && is_multisite() ) {
8f5fb3c John James Jacoby Remove preceding slash. ABSPATH is already slashed for us.
johnjamesjacoby authored
80 require_once( ABSPATH . 'wp-admin/includes/ms.php' );
e691fe3 John James Jacoby Include ms.php theme-side for its filesize and available space functions...
johnjamesjacoby authored
81 }
02b5fdb Nikolay Bachiyski Add convertor from extend read me to github readme
nb authored
82
83 if ( defined( 'WP_CLI' ) && WP_CLI ) {
84 require( dirname( __FILE__ ) . '/classes/class-wpcom-liveblog-wp-cli.php' );
85 }
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
86 }
8ff4206 Mohammad Jangda v1
mjangda authored
87
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
88 /**
89 * Hook actions in that run on every page-load
90 *
91 * @uses add_action()
92 */
93 private static function add_actions() {
cd6d574 John James Jacoby * Code clean up.
johnjamesjacoby authored
94 add_action( 'init', array( __CLASS__, 'init' ) );
7994c3e Nikolay Bachiyski Re-add the rewrite rules on permalink change
nb authored
95 add_action( 'init', array( __CLASS__, 'add_rewrite_rules' ) );
96 add_action( 'permalink_structure_changed', array( __CLASS__, 'add_rewrite_rules' ) );
4221823 Nikolay Bachiyski Flush the rewrite rules later
nb authored
97 // flush the rewrite rules a lot later so that we don't interfere with other plugins using rewrite rules
98 add_action( 'init', array( __CLASS__, 'flush_rewrite_rules' ), 1000 );
cd6d574 John James Jacoby * Code clean up.
johnjamesjacoby authored
99 add_action( 'wp_enqueue_scripts', array( __CLASS__, 'enqueue_scripts' ) );
ea5a8a6 Nikolay Bachiyski Introduce archiving
nb authored
100 add_action( 'admin_enqueue_scripts', array( __CLASS__, 'admin_enqueue_scripts' ) );
101 add_action( 'wp_ajax_set_liveblog_state_for_post', array( __CLASS__, 'admin_ajax_set_liveblog_state_for_post' ) );
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
102 }
8ff4206 Mohammad Jangda v1
mjangda authored
103
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
104 /**
105 * Hook filters in that run on every page-load
106 *
107 * @uses add_filter()
108 */
109 private static function add_filters() {
cd6d574 John James Jacoby * Code clean up.
johnjamesjacoby authored
110 add_filter( 'template_redirect', array( __CLASS__, 'handle_request' ) );
c3dc8bd Mohammad Jangda Only apply liveblog-entry CSS class to liveblog comment types.
mjangda authored
111 add_filter( 'comment_class', array( __CLASS__, 'add_comment_class' ), 10, 3 );
8ff4206 Mohammad Jangda v1
mjangda authored
112 }
113
cd6d574 John James Jacoby * Code clean up.
johnjamesjacoby authored
114 /**
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
115 * Hook actions in that run on every admin page-load
116 *
117 * @uses add_action()
118 * @uses is_admin()
119 */
120 private static function add_admin_actions() {
121
122 // Bail if not in admin area
123 if ( ! is_admin() )
124 return;
125
126 add_action( 'add_meta_boxes', array( __CLASS__, 'add_meta_box' ) );
127 }
128
129 /** Public Methods ********************************************************/
130
131 /**
132 * Liveblog initialization functions.
133 *
134 * This is where Liveblog sets up any additional things it needs to run
135 * inside of WordPress. Where some plugins would register post types or
136 * taxonomies, we modify endpoints and add post type support for Liveblog.
cd6d574 John James Jacoby * Code clean up.
johnjamesjacoby authored
137 */
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
138 public static function init() {
139 /**
140 * Add liveblog support to the 'post' post type. This is done here so
141 * we can possibly introduce this to other post types later.
142 */
8ff4206 Mohammad Jangda v1
mjangda authored
143 add_post_type_support( 'post', self::key );
8314495 Nikolay Bachiyski Add an action after liveblog's init
nb authored
144 do_action( 'after_liveblog_init' );
8ff4206 Mohammad Jangda v1
mjangda authored
145 }
146
7994c3e Nikolay Bachiyski Re-add the rewrite rules on permalink change
nb authored
147 public function add_rewrite_rules() {
1d5d7f9 Nikolay Bachiyski Flush the rewrite rules on rules update
nb authored
148 add_rewrite_endpoint( self::url_endpoint, EP_PERMALINK );
7994c3e Nikolay Bachiyski Re-add the rewrite rules on permalink change
nb authored
149 }
1d5d7f9 Nikolay Bachiyski Flush the rewrite rules on rules update
nb authored
150
7994c3e Nikolay Bachiyski Re-add the rewrite rules on permalink change
nb authored
151 public static function flush_rewrite_rules() {
1d5d7f9 Nikolay Bachiyski Flush the rewrite rules on rules update
nb authored
152 if ( get_option( 'liveblog_rewrites_version' ) != self::rewrites_version ) {
153 flush_rewrite_rules();
154 update_option( 'liveblog_rewrites_version', self::rewrites_version );
155 }
156 }
157
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
158 /**
159 * This is where a majority of the magic happens.
160 *
161 * Hooked to template_redirect, this method tries to add anything it can to
162 * the current post output. If nothing needs to be added, we redirect back
163 * to the permalink.
164 *
165 * @return If request has been handled
166 */
167 public static function handle_request() {
168
8ff4206 Mohammad Jangda v1
mjangda authored
169 if ( ! self::is_viewing_liveblog_post() )
170 return;
171
cd6d574 John James Jacoby * Code clean up.
johnjamesjacoby authored
172 self::$post_id = get_the_ID();
b28b38d Nikolay Bachiyski Rename the query class to match WP conventions
nb authored
173 self::$entry_query = new WPCOM_Liveblog_Entry_Query( self::$post_id, self::key );
1b8e8d5 Nikolay Bachiyski Streamline logic for getting recent entries
nb authored
174
4872b2a Nikolay Bachiyski Use front-end AJAX URLs for same-origin requests
nb authored
175 if ( self::is_initial_page_request() )
8ff4206 Mohammad Jangda v1
mjangda authored
176 add_filter( 'the_content', array( __CLASS__, 'add_liveblog_to_content' ) );
4872b2a Nikolay Bachiyski Use front-end AJAX URLs for same-origin requests
nb authored
177 else
178 self::handle_ajax_request();
179 }
180
181 private static function handle_ajax_request() {
182
183 $endpoint_suffix = get_query_var( self::url_endpoint );
184
185 if ( !$endpoint_suffix ) {
186 // we redirect, because if somebody accessed <permalink>/liveblog
187 // they probably did that in the URL bar, not via AJAX
188 wp_safe_redirect( get_permalink() );
189 exit();
8ff4206 Mohammad Jangda v1
mjangda authored
190 }
2b0244c Nikolay Bachiyski Remove trailing whitespace
nb authored
191 wp_cache_delete( self::key . '_entries_asc_' . self::$post_id, 'liveblog' );
8ff4206 Mohammad Jangda v1
mjangda authored
192
4872b2a Nikolay Bachiyski Use front-end AJAX URLs for same-origin requests
nb authored
193 $suffix_to_method = array(
194 '\d+/\d+' => 'ajax_entries_between',
4c7e77b Nikolay Bachiyski Consolidate all CRUD AJAX actions
nb authored
195 'crud' => 'ajax_crud_entry',
4872b2a Nikolay Bachiyski Use front-end AJAX URLs for same-origin requests
nb authored
196 'preview' => 'ajax_preview_entry',
197 );
198
199 $response_method = 'ajax_unknown';
200
201 foreach( $suffix_to_method as $suffix_re => $method ) {
202 if ( preg_match( "%^$suffix_re/?%", $endpoint_suffix ) ) {
203 $response_method = $method;
204 break;
205 }
1b8e8d5 Nikolay Bachiyski Streamline logic for getting recent entries
nb authored
206 }
207
4872b2a Nikolay Bachiyski Use front-end AJAX URLs for same-origin requests
nb authored
208 self::$response_method();
209
8ff4206 Mohammad Jangda v1
mjangda authored
210 }
1b8e8d5 Nikolay Bachiyski Streamline logic for getting recent entries
nb authored
211
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
212 /**
213 * Look for any new Liveblog entries, and return them via JSON
214 */
215 public static function ajax_entries_between() {
216
217 // Set some defaults
cd6d574 John James Jacoby * Code clean up.
johnjamesjacoby authored
218 $latest_timestamp = 0;
219 $entries_for_json = array();
558008c Nikolay Bachiyski Use the same current timestamp at all places
nb authored
220
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
221 // Look for entry boundaries
1b8e8d5 Nikolay Bachiyski Streamline logic for getting recent entries
nb authored
222 list( $start_timestamp, $end_timestamp ) = self::get_timestamps_from_query();
cd6d574 John James Jacoby * Code clean up.
johnjamesjacoby authored
223
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
224 // Bail if there is no end timestamp
cd6d574 John James Jacoby * Code clean up.
johnjamesjacoby authored
225 if ( empty( $end_timestamp ) ) {
70b347a Mohammad Jangda Add missing textdomain to translated strings
mjangda authored
226 self::send_user_error( __( 'A timestamp is missing. Correct URL: <permalink>/liveblog/<from>/</to>/', 'liveblog' ) );
1b8e8d5 Nikolay Bachiyski Streamline logic for getting recent entries
nb authored
227 }
228
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
229 // Do not cache if it's too soon
bd6c841 Nikolay Bachiyski Use the Date header to get server-side date
nb authored
230 if ( $end_timestamp > time() )
6c66757 Nikolay Bachiyski Do not let the client cache future requests
nb authored
231 self::$do_not_cache_response = true;
232
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
233 // Get liveblog entries within the start and end boundaries
b28b38d Nikolay Bachiyski Rename the query class to match WP conventions
nb authored
234 $entries = self::$entry_query->get_between_timestamps( $start_timestamp, $end_timestamp );
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
235 if ( empty( $entries ) ) {
7e39692 Mohammad Jangda Introduce some new hooks.
mjangda authored
236 do_action( 'liveblog_entry_request_empty' );
237
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
238 self::json_return( array(
239 'entries' => array(),
240 'latest_timestamp' => null
241 ) );
242 }
d79a82d Nikolay Bachiyski Gather common entry functionality in a new class
nb authored
243
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
244 /**
245 * Loop through each liveblog entry, set the most recent timestamp, and
246 * put the JSON data for each entry into a neat little array.
247 */
1b8e8d5 Nikolay Bachiyski Streamline logic for getting recent entries
nb authored
248 foreach( $entries as $entry ) {
cd6d574 John James Jacoby * Code clean up.
johnjamesjacoby authored
249 $latest_timestamp = max( $latest_timestamp, $entry->get_timestamp() );
d79a82d Nikolay Bachiyski Gather common entry functionality in a new class
nb authored
250 $entries_for_json[] = $entry->for_json();
1b8e8d5 Nikolay Bachiyski Streamline logic for getting recent entries
nb authored
251 }
252
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
253 // Setup our data to return via JSON
1b8e8d5 Nikolay Bachiyski Streamline logic for getting recent entries
nb authored
254 $result_for_json = array(
cd6d574 John James Jacoby * Code clean up.
johnjamesjacoby authored
255 'entries' => $entries_for_json,
256 'latest_timestamp' => $latest_timestamp,
1b8e8d5 Nikolay Bachiyski Streamline logic for getting recent entries
nb authored
257 );
258
7e39692 Mohammad Jangda Introduce some new hooks.
mjangda authored
259 do_action( 'liveblog_entry_request', $result_for_json );
260
e977561 Nikolay Bachiyski Get rid of extra json_return() arguments
nb authored
261 self::json_return( $result_for_json );
1b8e8d5 Nikolay Bachiyski Streamline logic for getting recent entries
nb authored
262 }
263
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
264 /** Private _is_ Methods **************************************************/
265
266 /**
267 * Are we viewing a liveblog post?
268 *
269 * @uses is_single()
270 * @uses is_liveblog_post()
271 * @return bool
272 */
273 private static function is_viewing_liveblog_post() {
274 return (bool) ( is_single() && self::is_liveblog_post() );
1b8e8d5 Nikolay Bachiyski Streamline logic for getting recent entries
nb authored
275 }
276
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
277 /**
278 * Is this the initial page request?
279 *
280 * Note that we do not use get_query_var() - it returns '' for all requests,
281 * which is valid for /post-name/liveblog/
282 *
283 * @global WP_Query $wp_query
284 * @return bool
285 */
286 private static function is_initial_page_request() {
8ff4206 Mohammad Jangda v1
mjangda authored
287 global $wp_query;
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
288
289 return (bool) ! isset( $wp_query->query_vars[self::key] );
8ff4206 Mohammad Jangda v1
mjangda authored
290 }
1b8e8d5 Nikolay Bachiyski Streamline logic for getting recent entries
nb authored
291
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
292 /**
293 * Is this an ajax request for the entries?
294 *
295 * @uses get_query_var() to check for the url_endpoint
296 * @return bool
297 */
298 private static function is_entries_ajax_request() {
299 return (bool) get_query_var( self::url_endpoint );
300 }
1b8e8d5 Nikolay Bachiyski Streamline logic for getting recent entries
nb authored
301
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
302 /**
303 * Is a given post_id a liveblog enabled post?
304 *
305 * @global WP_Post $post
306 * @param int $post_id
307 * @return bool
308 */
309 private static function is_liveblog_post( $post_id = null ) {
ea5a8a6 Nikolay Bachiyski Introduce archiving
nb authored
310 $state = self::get_liveblog_state( $post_id );
311 return $state && 'disable' != $state;
312 }
313
314 private static function get_liveblog_state( $post_id = null ) {
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
315 if ( empty( $post_id ) ) {
316 global $post;
317 $post_id = $post->ID;
318 }
ea5a8a6 Nikolay Bachiyski Introduce archiving
nb authored
319 $state = get_post_meta( $post_id, self::key, true );
320 // backwards compatibility with older values
321 if ( 1 == $state ) {
322 $state = 'enable';
323 }
324 if ( !$state ) {
325 $state = 'disable';
326 }
327 return $state;
1b8e8d5 Nikolay Bachiyski Streamline logic for getting recent entries
nb authored
328 }
329
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
330 /**
331 * Get timestamps from the current WP_Query
332 *
333 * Ensures that two timestamps exist, and returns a properly formatted empty
334 * array if not.
335 *
336 * @return array
337 */
338 private static function get_timestamps_from_query() {
339
340 // Look for timestamps and bail if none
25be635 Mohammad Jangda Support for non-pretty permalinks
mjangda authored
341 $stamps = rtrim( get_query_var( self::url_endpoint ), '/' );
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
342 if ( empty( $stamps ) )
1b8e8d5 Nikolay Bachiyski Streamline logic for getting recent entries
nb authored
343 return array( false, false );
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
344
345 // Get timestamps from the query variable
346 $timestamps = explode( '/', $stamps );
347
348 // Bail if there are not 2 timestamps
349 if ( 2 !== count( $timestamps ) )
350 return array( false, false );
351
352 // Return integer timestamps in an array
353 return array_map( 'intval', $timestamps );
1b8e8d5 Nikolay Bachiyski Streamline logic for getting recent entries
nb authored
354 }
355
4c7e77b Nikolay Bachiyski Consolidate all CRUD AJAX actions
nb authored
356 public static function ajax_crud_entry() {
b58c3a0 Nikolay Bachiyski Remove leading underscores from function names
nb authored
357 self::ajax_current_user_can_edit_liveblog();
358 self::ajax_check_nonce();
8ff4206 Mohammad Jangda v1
mjangda authored
359
4c7e77b Nikolay Bachiyski Consolidate all CRUD AJAX actions
nb authored
360 $args = array();
8ff4206 Mohammad Jangda v1
mjangda authored
361
4c7e77b Nikolay Bachiyski Consolidate all CRUD AJAX actions
nb authored
362 $crud_action = isset( $_POST['crud_action'] ) ? $_POST['crud_action'] : 0;
8ff4206 Mohammad Jangda v1
mjangda authored
363
4c7e77b Nikolay Bachiyski Consolidate all CRUD AJAX actions
nb authored
364 if ( !in_array( $crud_action, array( 'insert', 'update', 'delete' ) ) ) {
365 self::send_user_error( sprintf( __( 'Invalid entry crud_action: %s', 'liveblog' ), $crud_action ) );
a2723d4 Nikolay Bachiyski Commit entry modifications
nb authored
366 }
367
4c7e77b Nikolay Bachiyski Consolidate all CRUD AJAX actions
nb authored
368 $args['post_id'] = isset( $_POST['post_id'] ) ? intval( $_POST['post_id'] ) : 0;
369 $args['content'] = isset( $_POST['content'] ) ? $_POST['content'] : '';
370 $args['entry_id'] = isset( $_POST['entry_id'] ) ? intval( $_POST['entry_id'] ) : 0;
e92599e Nikolay Bachiyski Switch the delete endpoint to use the new method
nb authored
371
4c7e77b Nikolay Bachiyski Consolidate all CRUD AJAX actions
nb authored
372 $args['user'] = wp_get_current_user();
e92599e Nikolay Bachiyski Switch the delete endpoint to use the new method
nb authored
373
4c7e77b Nikolay Bachiyski Consolidate all CRUD AJAX actions
nb authored
374 $entry = call_user_func( array( 'WPCOM_Liveblog_Entry', $crud_action ), $args );
e92599e Nikolay Bachiyski Switch the delete endpoint to use the new method
nb authored
375
376 if ( is_wp_error( $entry ) ) {
377 self::send_server_error( $entry->get_error_message() );
378 }
379
380 // Do not send latest_timestamp. If we send it the client won't get
381 // older entries. Since we send only the new one, we don't know if there
382 // weren't any entries in between.
383 self::json_return( array(
384 'entries' => array( $entry->for_json() ),
385 'latest_timestamp' => null
386 ) );
387 }
388
5aee585 Nikolay Bachiyski Add ajax endpoint for preview
nb authored
389 function ajax_preview_entry() {
390 $entry_content = isset( $_REQUEST['entry_content'] ) ? $_REQUEST['entry_content'] : '';
5460519 Mohammad Jangda Fix preview which was returning slashed data.
mjangda authored
391 $entry_content = stripslashes( wp_filter_post_kses( $entry_content ) );
5aee585 Nikolay Bachiyski Add ajax endpoint for preview
nb authored
392 $entry_content = WPCOM_Liveblog_Entry::render_content( $entry_content );
7e39692 Mohammad Jangda Introduce some new hooks.
mjangda authored
393
394 do_action( 'liveblog_preview_entry', $entry_content );
395
5aee585 Nikolay Bachiyski Add ajax endpoint for preview
nb authored
396 self::json_return( array( 'html' => $entry_content ) );
397 }
398
4872b2a Nikolay Bachiyski Use front-end AJAX URLs for same-origin requests
nb authored
399 public function ajax_unknown() {
400 self::send_user_error( __( 'Unknown liveblog action', 'liveblog' ) );
401 }
402
403
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
404 /** Comment Methods *******************************************************/
8ff4206 Mohammad Jangda v1
mjangda authored
405
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
406 /**
407 * Add a liveblog class to each comment, so they can be styled
408 *
409 * @param array $classes
410 * @return string
411 */
c3dc8bd Mohammad Jangda Only apply liveblog-entry CSS class to liveblog comment types.
mjangda authored
412 public static function add_comment_class( $classes, $class, $comment_id ) {
413 if ( self::key == get_comment_type( $comment_id ) )
414 $classes[] = 'liveblog-entry';
8ff4206 Mohammad Jangda v1
mjangda authored
415 return $classes;
416 }
417
ea5a8a6 Nikolay Bachiyski Introduce archiving
nb authored
418 public static function admin_enqueue_scripts() {
419 wp_enqueue_style( self::key, plugins_url( 'css/liveblog-admin.css', __FILE__ ) );
281438d Nikolay Bachiyski Use actual information for AJAX action
nb authored
420 wp_enqueue_script( 'liveblog-admin', plugins_url( 'js/liveblog-admin.js', __FILE__ ) );
421 wp_localize_script( 'liveblog-admin', 'liveblog_admin_settings', array(
422 'nonce_key' => self::nonce_key,
423 'nonce' => wp_create_nonce( self::nonce_key ),
424 ) );
ea5a8a6 Nikolay Bachiyski Introduce archiving
nb authored
425 }
f157181 John James Jacoby Comment Status:
johnjamesjacoby authored
426
427 /**
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
428 * Enqueue the necessary CSS and JS that liveblog needs to function.
429 *
430 * @return If not a liveblog post
431 */
432 public static function enqueue_scripts() {
433
8ff4206 Mohammad Jangda v1
mjangda authored
434 if ( ! self::is_viewing_liveblog_post() )
435 return;
436
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
437 wp_enqueue_style( self::key, plugins_url( 'css/liveblog.css', __FILE__ ) );
db742c8 Nikolay Bachiyski Highlight new posts
nb authored
438 wp_enqueue_script( self::key, plugins_url( 'js/liveblog.js', __FILE__ ), array( 'jquery', 'jquery-color' ), self::version, true );
cd6d574 John James Jacoby * Code clean up.
johnjamesjacoby authored
439
8413aff John James Jacoby Uploading:
johnjamesjacoby authored
440 if ( self::current_user_can_edit_liveblog() ) {
a456b55 Nikolay Bachiyski Add Preview interface
nb authored
441 wp_enqueue_script( 'liveblog-publisher', plugins_url( 'js/liveblog-publisher.js', __FILE__ ), array( self::key, 'jquery-ui-tabs' ), self::version, true );
212e64d Zach Hookable Uploader
zbtirrell authored
442 wp_enqueue_script( 'liveblog-plupload', plugins_url( 'js/plupload.js', __FILE__ ), array( self::key, 'wp-plupload', 'jquery' ) );
c65e58f Nikolay Bachiyski Make the function name a verb
nb authored
443 self::add_default_plupload_settings();
cd6d574 John James Jacoby * Code clean up.
johnjamesjacoby authored
444 }
8ff4206 Mohammad Jangda v1
mjangda authored
445
c0c3085 Nikolay Bachiyski Add spinner for inserting an entry
nb authored
446 if ( wp_script_is( 'jquery.spin', 'registered' ) ) {
447 wp_enqueue_script( 'jquery.spin' );
448 } else {
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
449 wp_enqueue_script( 'spin', plugins_url( 'js/spin.js', __FILE__ ), false, '1.2.4' );
450 wp_enqueue_script( 'jquery.spin', plugins_url( 'js/jquery.spin.js', __FILE__ ), array( 'jquery', 'spin' ) );
c0c3085 Nikolay Bachiyski Add spinner for inserting an entry
nb authored
451 }
452
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
453 wp_localize_script( self::key, 'liveblog_settings',
cd6d574 John James Jacoby * Code clean up.
johnjamesjacoby authored
454 apply_filters( 'liveblog_settings', array(
455 'permalink' => get_permalink(),
456 'post_id' => get_the_ID(),
ea5a8a6 Nikolay Bachiyski Introduce archiving
nb authored
457 'state' => self::get_liveblog_state(),
8ff4206 Mohammad Jangda v1
mjangda authored
458
cd6d574 John James Jacoby * Code clean up.
johnjamesjacoby authored
459 'key' => self::key,
460 'nonce_key' => self::nonce_key,
461 'latest_entry_timestamp' => self::$entry_query->get_latest_timestamp(),
8ff4206 Mohammad Jangda v1
mjangda authored
462
cd6d574 John James Jacoby * Code clean up.
johnjamesjacoby authored
463 'refresh_interval' => self::refresh_interval,
ed1991c Nikolay Bachiyski Shut the auto-update only on consecutive failures
nb authored
464 'max_consecutive_retries'=> self::max_consecutive_retries,
cd6d574 John James Jacoby * Code clean up.
johnjamesjacoby authored
465 'delay_threshold' => self::delay_threshold,
466 'delay_multiplier' => self::delay_multiplier,
8ff4206 Mohammad Jangda v1
mjangda authored
467
ea5a8a6 Nikolay Bachiyski Introduce archiving
nb authored
468 'endpoint_url' => self::get_entries_endpoint_url(),
cd6d574 John James Jacoby * Code clean up.
johnjamesjacoby authored
469
470 // i18n
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
471 'update_nag_singular' => __( '%d new update', 'liveblog' ),
cd6d574 John James Jacoby * Code clean up.
johnjamesjacoby authored
472 'update_nag_plural' => __( '%d new updates', 'liveblog' ),
28802ff Nikolay Bachiyski Delete entry functionality
nb authored
473 'delete_confirmation' => __( 'Do you really want do delete this entry? There is no way back.', 'liveblog' ),
cd6d574 John James Jacoby * Code clean up.
johnjamesjacoby authored
474 ) )
475 );
8ff4206 Mohammad Jangda v1
mjangda authored
476 }
477
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
478 /**
8413aff John James Jacoby Uploading:
johnjamesjacoby authored
479 * Sets up some default Plupload settings so we can upload meda theme-side
480 *
481 * @global type $wp_scripts
482 */
c65e58f Nikolay Bachiyski Make the function name a verb
nb authored
483 private static function add_default_plupload_settings() {
8413aff John James Jacoby Uploading:
johnjamesjacoby authored
484 global $wp_scripts;
485
486 $defaults = array(
487 'runtimes' => 'html5,silverlight,flash,html4',
488 'file_data_name' => 'async-upload',
489 'multiple_queues' => true,
490 'max_file_size' => self::max_upload_size() . 'b',
491 'url' => admin_url( 'admin-ajax.php', 'relative' ),
492 'flash_swf_url' => includes_url( 'js/plupload/plupload.flash.swf' ),
493 'silverlight_xap_url' => includes_url( 'js/plupload/plupload.silverlight.xap' ),
70b347a Mohammad Jangda Add missing textdomain to translated strings
mjangda authored
494 'filters' => array( array( 'title' => __( 'Allowed Files', 'liveblog' ), 'extensions' => '*') ),
8413aff John James Jacoby Uploading:
johnjamesjacoby authored
495 'multipart' => true,
496 'urlstream_upload' => true,
497 'multipart_params' => array(
498 'action' => 'upload-attachment',
499 '_wpnonce' => wp_create_nonce( 'media-form' )
500 )
501 );
502
503 $settings = array(
504 'defaults' => $defaults,
505 'browser' => array(
506 'mobile' => wp_is_mobile(),
507 'supported' => _device_can_upload(),
508 )
509 );
510
511 $script = 'var _wpPluploadSettings = ' . json_encode( $settings ) . ';';
512 $data = $wp_scripts->get_data( 'wp-plupload', 'data' );
513
514 if ( ! empty( $data ) )
515 $script = "$data\n$script";
516
517 $wp_scripts->add_data( 'wp-plupload', 'data', $script );
518 }
519
520 /**
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
521 * Get the URL of a specific liveblog entry.
522 *
523 * @return string
524 */
525 private static function get_entries_endpoint_url() {
0dccc81 Nikolay Bachiyski Add correct endpoing to draft URLs with query args
nb authored
526 $post_permalink = get_permalink( self::$post_id );
527 if ( false !== strpos( $post_permalink, '?p=' ) )
528 $url = add_query_arg( self::url_endpoint, '', $post_permalink ) . '='; // returns something like ?p=1&liveblog=
25be635 Mohammad Jangda Support for non-pretty permalinks
mjangda authored
529 else
0dccc81 Nikolay Bachiyski Add correct endpoing to draft URLs with query args
nb authored
530 $url = trailingslashit( trailingslashit( $post_permalink ) . self::url_endpoint ); // returns something like /2012/01/01/post/liveblog/
7330fcb Nikolay Bachiyski Allow the endpoint URL to be filtered
nb authored
531 $url = apply_filters( 'liveblog_endpoint_url', $url, self::$post_id );
25be635 Mohammad Jangda Support for non-pretty permalinks
mjangda authored
532 return $url;
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
533 }
cb778e4 Mohammad Jangda Load initial entries server-side rather than via AJAX for SEO benefit
mjangda authored
534
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
535 /** Display Methods *******************************************************/
cd6d574 John James Jacoby * Code clean up.
johnjamesjacoby authored
536
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
537 /**
538 * Filter the_content and add the liveblog theme-side UI above the normal
539 * content area.
540 *
541 * @param string $content
542 * @return string
543 */
544 public static function add_liveblog_to_content( $content ) {
cb778e4 Mohammad Jangda Load initial entries server-side rather than via AJAX for SEO benefit
mjangda authored
545
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
546 $liveblog_output = '<div id="liveblog-container" class="'. self::$post_id .'">';
547 $liveblog_output .= self::get_editor_output();
548 $liveblog_output .= '<div id="liveblog-update-spinner"></div>';
549 $liveblog_output .= self::get_all_entry_output();
ba2c2ae Mohammad Jangda Switch around ids with classes just to be future-safe. Add a new wrapper...
mjangda authored
550 $liveblog_output .= '</div>';
8ff4206 Mohammad Jangda v1
mjangda authored
551
09e766c Mohammad Jangda New filter: liveblog_add_to_content
mjangda authored
552 $liveblog_output = apply_filters( 'liveblog_add_to_content', $liveblog_output, $content, self::$post_id );
553
7205206 Nikolay Bachiyski Put the post contents before the liveblog
nb authored
554 return $content . $liveblog_output;
8ff4206 Mohammad Jangda v1
mjangda authored
555 }
556
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
557 /**
558 * Return the posting area for the end-user to liveblog from
559 *
560 * @return string
561 */
562 private static function get_editor_output() {
b58c3a0 Nikolay Bachiyski Remove leading underscores from function names
nb authored
563 if ( ! self::current_user_can_edit_liveblog() )
8ff4206 Mohammad Jangda v1
mjangda authored
564 return;
ea5a8a6 Nikolay Bachiyski Introduce archiving
nb authored
565 if ( 'archive' == self::get_liveblog_state() ) {
566 return;
567 }
8ff4206 Mohammad Jangda v1
mjangda authored
568
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
569 // Get the template part
570 return self::get_template_part( 'liveblog-form.php' );
8ff4206 Mohammad Jangda v1
mjangda authored
571 }
572
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
573 /**
574 * Get all the liveblog entries for this post
575 */
576 private static function get_all_entry_output() {
8413aff John James Jacoby Uploading:
johnjamesjacoby authored
577
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
578 // Get liveblog entries
9c2b7c3 Nikolay Bachiyski Order entries reverse-chronologically on initial load
nb authored
579 $entries = (array) self::$entry_query->get_all();
8ff4206 Mohammad Jangda v1
mjangda authored
580
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
581 // Get the template part
582 return self::get_template_part( 'liveblog-loop.php', compact( 'entries' ) );
583 }
584
585 /**
586 * Get the template part in an output buffer and return it
587 *
588 * @param string $template_name
589 * @param array $template_variables
590 */
591 public static function get_template_part( $template_name, $template_variables = array() ) {
592 ob_start();
593 extract( $template_variables );
594 include( dirname( __FILE__ ) . '/templates/' . $template_name );
595 return ob_get_clean();
8ff4206 Mohammad Jangda v1
mjangda authored
596 }
8413aff John James Jacoby Uploading:
johnjamesjacoby authored
597
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
598 /** Admin Methods *********************************************************/
599
600 /**
601 * Register the metabox with the supporting post-type
602 *
603 * @param string $post_type
604 */
605 public static function add_meta_box( $post_type ) {
606
607 // Bail if not supported
608 if ( ! post_type_supports( $post_type, self::key ) )
609 return;
8ff4206 Mohammad Jangda v1
mjangda authored
610
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
611 add_meta_box( self::key, __( 'Liveblog', 'liveblog' ), array( __CLASS__, 'display_meta_box' ) );
8ff4206 Mohammad Jangda v1
mjangda authored
612 }
cd6d574 John James Jacoby * Code clean up.
johnjamesjacoby authored
613
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
614 /**
615 * Output the metabox
616 *
617 * @param WP_Post $post
618 */
619 public static function display_meta_box( $post ) {
ea5a8a6 Nikolay Bachiyski Introduce archiving
nb authored
620 $entries_query = new WPCOM_Liveblog_Entry_Query( $post->ID, self::key );
621 $current_state = self::get_liveblog_state( $post->ID );
622 $primary_from_current = array(
623 'enable' => 'archive',
624 'disable' => 'enable',
625 );
626 $default_button = array( 'primary' => false, 'current' => false, );
627 $buttons = array(
628 'enable' => array( 'value' => 'enable', 'text' => __( 'Activate', 'liveblog' ),
feecd6b Nikolay Bachiyski Add text domains
nb authored
629 'description' => __( 'Enables liveblog on this post. Posting tools are enabled for editors, visitors get the latest updates.' , 'liveblog'), 'active-text' => sprintf( __( 'There is an <strong>enabled</strong> liveblog on this post. <a href="%s">Visit the liveblog &rarr;</a>', 'liveblog' ), get_permalink( $post ) ), ),
ea5a8a6 Nikolay Bachiyski Introduce archiving
nb authored
630 'archive' => array( 'value' => 'archive', 'text' => __( 'Archive', 'liveblog' ),
feecd6b Nikolay Bachiyski Add text domains
nb authored
631 'description' => __( 'Archives the liveblog on this post. Posting tools are hidden, visitors still see all the liveblog entries.' , 'liveblog'), 'active-text' => sprintf( __( 'There is an <strong>archived</strong> liveblog on this post. <a href="%s">Visit the liveblog archive &rarr;</a>', 'liveblog' ), get_permalink( $post ) ) ),
ea5a8a6 Nikolay Bachiyski Introduce archiving
nb authored
632 'disable' => array( 'value' => 'disable', 'text' => __( 'Disable', 'liveblog' ),
cbb8f91 Nikolay Bachiyski Add a note to keeping entries to active-text, too
nb authored
633 'description' => __( 'Disables the liveblog on this post. Turns it into a normal WordPress post, without a liveblog.' , 'liveblog'), 'active-text' => __( 'This is a normal WordPress post, without a liveblog.', 'liveblog' ), ),
ea5a8a6 Nikolay Bachiyski Introduce archiving
nb authored
634 );
635 foreach( $buttons as &$button ) {
636 $button = array_merge( $default_button, $button );
637 }
638 $buttons[$current_state]['current'] = true;
639 if ( isset( $primary_from_current[$current_state] ) ) {
640 $buttons[$primary_from_current[$current_state]]['primary'] = true;
641 }
642 if ( $entries_query->has_any() ) {
feecd6b Nikolay Bachiyski Add text domains
nb authored
643 $buttons['disable']['description'] .= ' ' . __( 'Existing entries are kept, but hidden.' , 'liveblog');
cbb8f91 Nikolay Bachiyski Add a note to keeping entries to active-text, too
nb authored
644 $buttons['disable']['active-text'] .= ' ' . __( 'Existing entries are kept, but hidden.' , 'liveblog');
ea5a8a6 Nikolay Bachiyski Introduce archiving
nb authored
645 }
646 $active_text = $buttons[$current_state]['active-text'];
647 echo self::get_template_part( 'meta_box.php', compact( 'active_text', 'buttons' ) );
8ff4206 Mohammad Jangda v1
mjangda authored
648 }
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
649
ea5a8a6 Nikolay Bachiyski Introduce archiving
nb authored
650 public function admin_ajax_set_liveblog_state_for_post() {
651 $post_id = isset( $_REQUEST['post_id'] )? $_REQUEST['post_id'] : 0;
652 $new_state = isset( $_REQUEST['state'] )? $_REQUEST['state'] : '';
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
653
e447a3c Nikolay Bachiyski Check for permissions and nonces
nb authored
654 self::ajax_current_user_can_edit_liveblog();
655 self::ajax_check_nonce();
8ff4206 Mohammad Jangda v1
mjangda authored
656
ea5a8a6 Nikolay Bachiyski Introduce archiving
nb authored
657 if ( !$REQUEST = get_post( $post_id ) ) {
feecd6b Nikolay Bachiyski Add text domains
nb authored
658 self::send_user_error( __( "Non-existing post ID: $post_id" , 'liveblog') );
ea5a8a6 Nikolay Bachiyski Introduce archiving
nb authored
659 }
660
661 if ( wp_is_post_revision( $post_id ) ) {
feecd6b Nikolay Bachiyski Add text domains
nb authored
662 self::send_user_error( __( "The post is a revision: $post_id" , 'liveblog') );
ea5a8a6 Nikolay Bachiyski Introduce archiving
nb authored
663 }
c9f98b5 Mohammad Jangda Only update/delete liveblog postmeta when it's changing.
mjangda authored
664
ea5a8a6 Nikolay Bachiyski Introduce archiving
nb authored
665 self::set_liveblog_state( $post_id, $_REQUEST['state'] );
666 self::display_meta_box( $REQUEST );
667 exit;
668 }
c9f98b5 Mohammad Jangda Only update/delete liveblog postmeta when it's changing.
mjangda authored
669
ea5a8a6 Nikolay Bachiyski Introduce archiving
nb authored
670 private function set_liveblog_state( $post_id, $state ) {
671 if ( in_array( $state, array( 'enable', 'archive' ) ) ) {
672 update_post_meta( $post_id, self::key, $state );
673 do_action( "liveblog_{$state}_post", $post_id );
674 } elseif ( 'disable' == $state ) {
8ff4206 Mohammad Jangda v1
mjangda authored
675 delete_post_meta( $post_id, self::key );
7e39692 Mohammad Jangda Introduce some new hooks.
mjangda authored
676 do_action( 'liveblog_disable_post', $post_id );
ea5a8a6 Nikolay Bachiyski Introduce archiving
nb authored
677 } else {
678 return false;
7e39692 Mohammad Jangda Introduce some new hooks.
mjangda authored
679 }
8ff4206 Mohammad Jangda v1
mjangda authored
680 }
2ba7f86 Nikolay Bachiyski Remove trailing whitespace
nb authored
681
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
682 /** Error Methods *********************************************************/
683
684 /**
685 * Can the current user edit liveblog data (non-ajax)
686 *
687 * @return bool
688 */
689 public static function current_user_can_edit_liveblog() {
690 return (bool) current_user_can( apply_filters( 'liveblog_edit_cap', self::edit_cap ) );
8ff4206 Mohammad Jangda v1
mjangda authored
691 }
692
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
693 /**
694 * Can the current user edit liveblog data (ajax)
695 *
696 * Sends an error if not
697 */
698 public static function ajax_current_user_can_edit_liveblog() {
b58c3a0 Nikolay Bachiyski Remove leading underscores from function names
nb authored
699 if ( ! self::current_user_can_edit_liveblog() ) {
69f9cbb Nikolay Bachiyski Add more specific HTTP error codes
nb authored
700 self::send_forbidden_error( __( "Cheatin', uh?", 'liveblog' ) );
8ff4206 Mohammad Jangda v1
mjangda authored
701 }
702 }
703
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
704 /**
705 * Check for valid intention, and send an error if there is none
706 *
707 * @param string $action
708 */
709 public static function ajax_check_nonce( $action = self::nonce_key ) {
8ff4206 Mohammad Jangda v1
mjangda authored
710 if ( ! isset( $_REQUEST[ self::nonce_key ] ) || ! wp_verify_nonce( $_REQUEST[ self::nonce_key ], $action ) ) {
69f9cbb Nikolay Bachiyski Add more specific HTTP error codes
nb authored
711 self::send_forbidden_error( __( 'Sorry, we could not authenticate you.', 'liveblog' ) );
8ff4206 Mohammad Jangda v1
mjangda authored
712 }
713 }
714
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
715 /** Feedback **************************************************************/
716
717 /**
718 * Send an error message
719 * @param type $message
720 */
69f9cbb Nikolay Bachiyski Add more specific HTTP error codes
nb authored
721 private static function send_server_error( $message ) {
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
722 self::status_header_with_message( 500, $message );
723 exit();
724 }
725
69f9cbb Nikolay Bachiyski Add more specific HTTP error codes
nb authored
726 private static function send_user_error( $message ) {
727 self::status_header_with_message( 406, $message );
728 exit();
729 }
730
731 private static function send_forbidden_error( $message ) {
732 self::status_header_with_message( 403, $message );
733 exit();
734 }
735
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
736 /**
737 * Encode some data and echo it (possibly without cached headers)
738 *
739 * @param array $data
740 */
741 private static function json_return( $data ) {
20c5b02 Nikolay Bachiyski Get rid of the intermediate layer completely
nb authored
742 $json_data = json_encode( $data );
8ff4206 Mohammad Jangda v1
mjangda authored
743
f2908df Nikolay Bachiyski Get rid if return functionality in json_return()
nb authored
744 header( 'Content-Type: application/json' );
cd6d574 John James Jacoby * Code clean up.
johnjamesjacoby authored
745 if ( self::$do_not_cache_response )
6c66757 Nikolay Bachiyski Do not let the client cache future requests
nb authored
746 nocache_headers();
20c5b02 Nikolay Bachiyski Get rid of the intermediate layer completely
nb authored
747
748 echo $json_data;
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
749 exit();
d81e042 Nikolay Bachiyski Extract sending errors in its own method
nb authored
750 }
751
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
752 /**
753 * Modify the header and description in the global array
754 *
755 * @global array $wp_header_to_desc
756 * @param int $status
757 * @param string $message
758 */
759 private static function status_header_with_message( $status, $message ) {
2532397 Nikolay Bachiyski Send HTTP errors instead of the success flag
nb authored
760 global $wp_header_to_desc;
cd6d574 John James Jacoby * Code clean up.
johnjamesjacoby authored
761
762 $status = absint( $status );
763 $official_message = isset( $wp_header_to_desc[$status] ) ? $wp_header_to_desc[$status] : '';
2532397 Nikolay Bachiyski Send HTTP errors instead of the success flag
nb authored
764 $wp_header_to_desc[$status] = $message;
cd6d574 John James Jacoby * Code clean up.
johnjamesjacoby authored
765
2532397 Nikolay Bachiyski Send HTTP errors instead of the success flag
nb authored
766 status_header( $status );
cd6d574 John James Jacoby * Code clean up.
johnjamesjacoby authored
767
2532397 Nikolay Bachiyski Send HTTP errors instead of the success flag
nb authored
768 $wp_header_to_desc[$status] = $official_message;
769 }
8413aff John James Jacoby Uploading:
johnjamesjacoby authored
770
771 /** Plupload Helpers ******************************************************/
772
773 /**
774 * Convert hours to bytes
775 *
776 * @param unknown_type $size
777 * @return unknown
778 */
779 private static function convert_hr_to_bytes( $size ) {
780 $size = strtolower( $size );
781 $bytes = (int) $size;
782
783 if ( strpos( $size, 'k' ) !== false )
784 $bytes = intval( $size ) * 1024;
785 elseif ( strpos( $size, 'm' ) !== false )
786 $bytes = intval( $size ) * 1024 * 1024;
787 elseif ( strpos( $size, 'g' ) !== false )
788 $bytes = intval( $size ) * 1024 * 1024 * 1024;
789
790 return $bytes;
791 }
792
793 /**
794 * Convert bytes to hour
795 *
796 * @param string $bytes
797 * @return string
798 */
799 private static function convert_bytes_to_hr( $bytes ) {
800 $units = array( 0 => 'B', 1 => 'kB', 2 => 'MB', 3 => 'GB' );
801 $log = log( $bytes, 1024 );
802 $power = (int) $log;
803 $size = pow( 1024, $log - $power );
804
805 return $size . $units[$power];
806 }
807
808 /**
809 * Get the maximum upload file size
810 *
811 * @see wp_max_upload_size()
812 * @return string
813 */
814 private static function max_upload_size() {
815 $u_bytes = self::convert_hr_to_bytes( ini_get( 'upload_max_filesize' ) );
816 $p_bytes = self::convert_hr_to_bytes( ini_get( 'post_max_size' ) );
817 $bytes = apply_filters( 'upload_size_limit', min( $u_bytes, $p_bytes ), $u_bytes, $p_bytes );
818
819 return $bytes;
a6429c8 Nikolay Bachiyski Remove trailing whitespace
nb authored
820 }
8ff4206 Mohammad Jangda v1
mjangda authored
821 }
822
0a0ca28 John James Jacoby Weekend updates:
johnjamesjacoby authored
823 /**
824 * Load the one true WPCOM_Liveblog instance
825 *
826 * Loaded late on the 'plugins_loaded' hook to allow any other plugin to sneak
827 * in ahead of it, to add actions, filters, etc...
828 *
829 * @uses WPCOM_Liveblog::load()
830 */
831 function wpcom_liveblog_load() {
832 WPCOM_Liveblog::load();
833 }
834 add_action( 'plugins_loaded', 'wpcom_liveblog_load', 999 );
835
8ff4206 Mohammad Jangda v1
mjangda authored
836 endif;
Something went wrong with that request. Please try again.