Ticket #38343: 38343.diff
File 38343.diff, 6.1 KB (added by , 9 years ago) |
---|
-
src/wp-admin/includes/class-wp-press-this.php
diff --git src/wp-admin/includes/class-wp-press-this.php src/wp-admin/includes/class-wp-press-this.php index 71ce515..a535eb8 100644
class WP_Press_This { 1272 1272 wp_enqueue_script( 'json2' ); 1273 1273 wp_enqueue_script( 'editor' ); 1274 1274 1275 // Localize API plugin settings and schema. 1276 $api_settings = array( 1277 'url' => esc_url_raw( get_rest_url() . 'wp/v2/' ), 1278 'nonce' => wp_create_nonce( 'wp_rest' ), 1279 ); 1280 wp_localize_script( 'press-this', 'wpApi', $api_settings ); 1275 1281 $supports_formats = false; 1276 1282 $post_format = 0; 1277 1283 -
src/wp-admin/js/press-this.js
diff --git src/wp-admin/js/press-this.js src/wp-admin/js/press-this.js index bf2e830..2b4bf63 100644
262 262 * @param action string publish|draft 263 263 */ 264 264 function submitPost( action ) { 265 var data ;265 var data, toSend, headerParams; 266 266 267 267 saveAlert = false; 268 268 showSpinner(); 269 269 270 270 if ( 'publish' === action ) { 271 $( '#post_status' ).val( 'publish');271 $( '#post_status' ).val( action ); 272 272 } 273 273 274 274 prepareFormData(); 275 data = $( '#pressthis-form' ).serialize(); 275 276 $( '#_wpnonce' ).val( wpApi.nonce ); 277 data = $( '#pressthis-form' ).serializeArray(); 278 279 toSend = { 280 'title': $( '#post_title' ).val(), 281 'content': $( '#post_content' ).val(), 282 'excerpt': $( '#post_excerpt' ).val(), 283 'post_status': action, 284 '_wpnonce': $( '#_wpnonce' ).val() 285 } 286 276 287 277 288 $.ajax( { 278 type: 'post', 279 url: window.ajaxurl, 280 data: data 289 type: 'POST', 290 url: wpApi.url + 'posts?press-this-post-save=true&pt-force-redirect=' + $( '#pt-force-redirect' ).val(), 291 data: toSend, 292 dataType: 'json', 293 headers: headerParams 281 294 }).always( function() { 282 295 hideSpinner(); 283 296 clearNotices(); 284 297 $( '.publish-button' ).removeClass( 'is-saving' ); 285 298 }).done( function( response ) { 286 if ( ! response.success ) { 287 renderError( response.data.errorMessage );288 } else if ( response.data.redirect ) {289 if ( window.opener && ( settings.redirInParent || response. data.force ) ) {299 300 // We successfully saved the post. 301 if ( response.redirect ) { 302 if ( window.opener && ( settings.redirInParent || response.force ) ) { 290 303 try { 291 window.opener.location.href = response. data.redirect;304 window.opener.location.href = response.redirect; 292 305 293 306 window.setTimeout( function() { 294 307 window.self.close(); 295 308 }, 200 ); 296 309 } catch( er ) { 297 window.location.href = response. data.redirect;310 window.location.href = response.redirect; 298 311 } 299 312 } else { 300 window.location.href = response. data.redirect;313 window.location.href = response.redirect; 301 314 } 302 315 } 316 303 317 }).fail( function() { 304 318 renderError( __( 'serverError' ) ); 305 319 }); -
src/wp-includes/rest-api.php
diff --git src/wp-includes/rest-api.php src/wp-includes/rest-api.php index 1a7da17..cb6e44a 100644
function rest_api_default_filters() { 164 164 add_filter( 'rest_post_dispatch', 'rest_send_allow_header', 10, 3 ); 165 165 166 166 add_filter( 'rest_pre_dispatch', 'rest_handle_options_request', 10, 3 ); 167 add_filter( 'rest_prepare_post', 'wp_prepare_press_this_response', 10, 3 ); 168 add_filter( 'rest_pre_insert_post', 'wp_pre_insert_press_this_post', 10, 2 ); 169 170 171 } 172 173 /** 174 * Filter Press This posts before returning from the API. 175 * 176 * 177 * @param WP_REST_Response $response The response object. 178 * @param WP_Post $post The original post. 179 * @param WP_REST_Request $request Request used to generate the response. 180 */ 181 function wp_prepare_press_this_response( $response, $post, $request ) { 182 183 $attributes = $request->get_attributes(); 184 $params = $request->get_query_params(); 185 186 // Only modify Quick Press responses. 187 if ( ! isset( $params['press-this-post-save'] ) ) { 188 return $response; 189 } 190 191 // Match the existing ajax handler logic. 192 $forceRedirect = false; 193 194 if ( 'publish' === get_post_status( $post->ID ) ) { 195 $redirect = get_post_permalink( $post->ID ); 196 } elseif ( isset( $params['pt-force-redirect'] ) && $params['pt-force-redirect'] === 'true' ) { 197 $forceRedirect = true; 198 $redirect = get_edit_post_link( $post->ID, 'js' ); 199 } else { 200 $redirect = false; 201 } 202 203 /** 204 * Filters the URL to redirect to when Press This saves. 205 * 206 * @since 4.2.0 207 * 208 * @param string $url Redirect URL. If `$status` is 'publish', this will be the post permalink. 209 * Otherwise, the default is false resulting in no redirect. 210 * @param int $post->ID Post ID. 211 * @param string $status Post status. 212 */ 213 $redirect = apply_filters( 'press_this_save_redirect', $redirect, $post->ID, $post->post_status ); 214 215 if ( $redirect ) { 216 $response->data['redirect'] = $redirect; 217 $response->data['force'] = $forceRedirect; 218 } else { 219 $response->data['postSaved'] = true; 220 } 221 222 return $response; 223 } 224 225 /** 226 * Filter Press This posts before they are inserted into the database. 227 * 228 * @param stdClass $prepared_post An object representing a single post prepared 229 * for inserting or updating the database. 230 * @param WP_REST_Request $request Request object. 231 */ 232 function wp_pre_insert_press_this_post( $prepared_post, $request ) { 233 234 // Only modify Quick Press posts. 235 if ( ! isset( $request->data['press-this-post-save'] ) ) { 236 return $prepared_post; 237 } 238 239 // @todo category ? 240 241 $post_data = $prepared_post->to_array(); 242 include( ABSPATH . 'wp-admin/includes/class-wp-press-this.php' ); 243 $wp_press_this = new WP_Press_This(); 244 245 // Side load images for this post. 246 $post_data['post_content'] = $wp_press_this->side_load_images( $post_id, $post_data['post_content'] ); 247 248 /** 249 * Filters the post data of a Press This post before saving/updating. 250 * 251 * The {@see 'side_load_images'} action has already run at this point. 252 * 253 * @since 4.5.0 254 * 255 * @param array $post_data The post data. 256 */ 257 $post_data = apply_filters( 'press_this_save_post', $post_data ); 258 259 return $post_data; 167 260 } 168 261 169 262 /**