Ticket #38343: 38343.2.diff
File 38343.2.diff, 9.1 KB (added by , 8 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..5696e5a 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 … … class WP_Press_This { 1352 1358 <?php 1353 1359 1354 1360 wp_nonce_field( 'update-post_' . $post_ID, '_wpnonce', false ); 1355 wp_nonce_field( 'add-category', '_ajax_nonce-add-category', false );1356 1361 1357 1362 ?> 1358 1363 -
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..348a0ab 100644
262 262 * @param action string publish|draft 263 263 */ 264 264 function submitPost( action ) { 265 var data ;265 var data, toSend; 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 data = $( '#pressthis-form' ).serializeArray(); 277 278 toSend = { 279 'title': $( '#post_title' ).val(), 280 'content': $( '#post_content' ).val(), 281 'excerpt': $( '#post_excerpt' ).val(), 282 'post_status': action, 283 '_wpnonce': wpApi.nonce 284 } 285 276 286 277 287 $.ajax( { 278 type: 'post', 279 url: window.ajaxurl, 280 data: data 288 'type': 'POST', 289 'url': wpApi.url + 'posts?press-this-post-save=true&pt-force-redirect=' + $( '#pt-force-redirect' ).val(), 290 'data': toSend, 291 'dataType': 'json' 281 292 }).always( function() { 282 293 hideSpinner(); 283 294 clearNotices(); 284 295 $( '.publish-button' ).removeClass( 'is-saving' ); 285 296 }).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 ) ) {297 298 // We successfully saved the post. 299 if ( response.redirect ) { 300 if ( window.opener && ( settings.redirInParent || response.force ) ) { 290 301 try { 291 window.opener.location.href = response. data.redirect;302 window.opener.location.href = response.redirect; 292 303 293 304 window.setTimeout( function() { 294 305 window.self.close(); 295 306 }, 200 ); 296 307 } catch( er ) { 297 window.location.href = response. data.redirect;308 window.location.href = response.redirect; 298 309 } 299 310 } else { 300 window.location.href = response. data.redirect;311 window.location.href = response.redirect; 301 312 } 302 313 } 314 303 315 }).fail( function() { 304 316 renderError( __( 'serverError' ) ); 305 317 }); … … 344 356 */ 345 357 function saveNewCategory() { 346 358 var data, 347 name = $( '#new-category' ).val(); 359 name = $( '#new-category' ).val(), 360 catNonce = $( '#_ajax_nonce-add-category' ).val() || '', 361 parent = $( '#new-category-parent' ).val() || 0; 348 362 363 // Don't save if we don't have a category name. 349 364 if ( ! name ) { 350 365 return; 351 366 } 352 367 368 // Set up the save data. 353 369 data = { 354 action: 'press-this-add-category', 355 post_id: $( '#post_ID' ).val() || 0, 356 name: name, 357 new_cat_nonce: $( '#_ajax_nonce-add-category' ).val() || '', 358 parent: $( '#new-category-parent' ).val() || 0 370 'name': name, 371 'catNonce': catNonce, 372 '_wpnonce': wpApi.nonce 359 373 }; 360 374 361 $.post( window.ajaxurl, data, function( response ) { 362 if ( ! response.success ) { 363 renderError( response.data.errorMessage ); 364 } else { 365 var $parent, $ul, 366 $wrap = $( 'ul.categories-select' ); 375 // Set a parent if selected. 376 if ( parent > 0 ) { 377 data['parent'] = parent; 378 } 367 379 368 $.each( response.data, function( i, newCat ) { 369 var $node = $( '<li>' ).append( $( '<div class="category selected" tabindex="0" role="checkbox" aria-checked="true">' ) 370 .attr( 'data-term-id', newCat.term_id ) 371 .text( newCat.name ) ); 380 // Send the data to the REST API. 381 $.ajax( { 382 'type': 'POST', 383 'url': wpApi.url + 'categories?press-this-add-category=true', 384 'data': data, 385 'dataType': 'json' 386 }).always( function() { 387 }).done( function( newCat ) { 388 var $parent, $ul, 389 $wrap = $( 'ul.categories-select' ); 372 390 373 if ( newCat.parent ) { 374 if ( ! $ul || ! $ul.length ) { 375 $parent = $wrap.find( 'div[data-term-id="' + newCat.parent + '"]' ).parent(); 376 $ul = $parent.find( 'ul.children:first' ); 391 console.log (newCat); 377 392 378 if ( ! $ul.length ) { 379 $ul = $( '<ul class="children">' ).appendTo( $parent ); 380 } 381 } 393 var $node = $( '<li>' ).append( $( '<div class="category selected" tabindex="0" role="checkbox" aria-checked="true">' ) 394 .attr( 'data-term-id', newCat.term_id ) 395 .text( newCat.name ) ); 382 396 383 $ul.prepend( $node );384 } else{385 $wrap.prepend( $node);386 }397 if ( newCat.parent ) { 398 if ( ! $ul || ! $ul.length ) { 399 $parent = $wrap.find( 'div[data-term-id="' + newCat.parent + '"]' ).parent(); 400 $ul = $parent.find( 'ul.children:first' ); 387 401 388 $node.focus(); 389 } ); 402 if ( ! $ul.length ) { 403 $ul = $( '<ul class="children">' ).appendTo( $parent ); 404 } 405 } 390 406 391 refreshCatsCache(); 407 $ul.prepend( $node ); 408 } else { 409 $wrap.prepend( $node ); 392 410 } 393 } ); 411 412 $node.focus(); 413 414 refreshCatsCache(); 415 }).fail( function( response ) { 416 renderError( response.data.errorMessage ); 417 }); 418 394 419 } 395 420 396 421 /* *************************************************************** -
src/wp-includes/rest-api.php
diff --git src/wp-includes/rest-api.php src/wp-includes/rest-api.php index 8c252b4..96c1638 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 /**