Permalink
Browse files

Extract sending errors in its own method

 Please enter the commit message for your changes. Lines starting
  • Loading branch information...
1 parent 2532397 commit d81e042fe7fda81938ad949cb0c9523d9018941c @nb nb committed Jul 11, 2012
Showing with 9 additions and 10 deletions.
  1. +9 −10 liveblog.php
View
19 liveblog.php
@@ -151,7 +151,7 @@ function ajax_insert_entry() {
$post_id = isset( $_POST['post_id'] ) ? intval( $_POST['post_id'] ) : 0;
if ( ! $post_id )
- self::json_return( false, __( "Sorry, that's an invalid post_id", 'liveblog' ) );
+ self::send_error( __( "Sorry, that's an invalid post_id", 'liveblog' ) );
$user = wp_get_current_user();
@@ -177,7 +177,7 @@ function ajax_insert_entry() {
$new_comment_id = wp_insert_comment( $entry );
if ( !$new_comment_id ) {
- self::json_return( false, __( 'Error posting entry!' ) );
+ self::send_error( __( 'Error posting entry!' ) );
}
$entry = WPCOM_Liveblog_Entry::from_comment( get_comment( $new_comment_id ) );
@@ -306,7 +306,7 @@ function get_entries_endpoint_url() {
function ajax_current_user_can_edit_liveblog() {
if ( ! self::current_user_can_edit_liveblog() ) {
- self::json_return( false, __( 'Cheatin\', uh?', 'liveblog' ) );
+ self::send_error( __( 'Cheatin\', uh?', 'liveblog' ) );
}
}
function current_user_can_edit_liveblog() {
@@ -315,7 +315,7 @@ function current_user_can_edit_liveblog() {
function ajax_check_nonce( $action = 'liveblog_nonce' ) {
if ( ! isset( $_REQUEST[ self::nonce_key ] ) || ! wp_verify_nonce( $_REQUEST[ self::nonce_key ], $action ) ) {
- self::json_return( false, __( 'Sorry, we could not authenticate you.', 'liveblog' ) );
+ self::send_error( __( 'Sorry, we could not authenticate you.', 'liveblog' ) );
}
}
@@ -326,12 +326,6 @@ function json_return( $success, $message, $data = array() ) {
'data' => $data,
) );
- $success = intval( $success );
- if ( !$success ) {
- self::status_header_with_message( 500, $message );
- exit;
- }
-
header( 'Content-Type: application/json' );
if ( self::$do_not_cache_response ) {
nocache_headers();
@@ -340,6 +334,11 @@ function json_return( $success, $message, $data = array() ) {
exit;
}
+ function send_error( $message ) {
+ self::status_header_with_message( 500, $message );
+ exit;
+ }
+
function status_header_with_message( $status, $message ) {
global $wp_header_to_desc;
$status = absint( $status );

0 comments on commit d81e042

Please sign in to comment.