Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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