-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved the production bulk editor to a dedicated class.
- Loading branch information
Showing
8 changed files
with
198 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
/** | ||
* Production bulk editing. | ||
* | ||
* Makes sure that all events inherit the post_status of their parent production, | ||
* while bulk editing productions. | ||
* | ||
* @since 0.12 | ||
*/ | ||
class WPT_Bulk_Editor { | ||
|
||
function __construct() { | ||
add_action( 'admin_init', array( $this, 'enqueue_scripts' ) ); | ||
add_action( 'wp_ajax_wpt_bulk_editor', array( $this, 'update_event_post_status' ) ); | ||
} | ||
|
||
/** | ||
* Adds the bulk editor nonce to the javascript variables. | ||
* | ||
* @since 0.12 | ||
* @access public | ||
* @return void | ||
*/ | ||
public function enqueue_scripts() { | ||
wp_localize_script( | ||
'wp_theatre_admin', | ||
'wpt_bulk_editor_security', | ||
array( | ||
'nonce' => wp_create_nonce( 'wpt_bulk_editor_ajax_nonce' ), | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Updates the status of events when bulk editing productions. | ||
* | ||
* Triggered through AJAX when the user clicks the 'update' button. | ||
* Finished before the production data is submitted to the server. | ||
* See wpt_bulk_editor.coffee. | ||
* | ||
* @since 0.12 | ||
* @access public | ||
* @return void | ||
*/ | ||
function update_event_post_status() { | ||
global $wp_theatre; | ||
|
||
check_ajax_referer( 'wpt_bulk_editor_ajax_nonce', 'wpt_bulk_editor_ajax_nonce' , true ); | ||
|
||
if ( | ||
! empty( $_POST['post_ids'] ) && | ||
is_array( $_POST['post_ids'] ) && | ||
1 != $_POST['post_status'] | ||
) { | ||
|
||
// Status of production is updated | ||
foreach ( $_POST['post_ids'] as $post_id ) { | ||
|
||
// Update status of connected Events | ||
$args = array( | ||
'status' => array( 'any', 'auto-draft' ), | ||
'production' => $post_id, | ||
); | ||
$events = $wp_theatre->events->get( $args ); | ||
|
||
foreach ( $events as $event ) { | ||
// Keep trashed events in the trash. | ||
if ( 'trash' == get_post_status( $event->ID ) ) { | ||
continue; | ||
} | ||
|
||
$post = array( | ||
'ID' => $event->ID, | ||
'post_status' => $_POST['post_status'], | ||
); | ||
|
||
wp_update_post( $post ); | ||
} | ||
} | ||
} | ||
|
||
echo 'ok'; | ||
|
||
wp_die(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
jQuery -> | ||
### | ||
Update all connected events when bulk updating productions. | ||
See: http://codex.wordpress.org/Plugin_API/Action_Reference/bulk_edit_custom_box | ||
### | ||
jQuery( '#bulk_edit' ).live 'click', () -> | ||
bulk_row = jQuery '#bulk-edit' | ||
# get the selected post ids that are being edited | ||
post_ids = new Array() | ||
bulk_row.find( '#bulk-titles' ).children().each () -> | ||
post_ids.push jQuery(@).attr( 'id' ).replace( /^(ttle)/i, '' ) | ||
|
||
# get the data | ||
post_status = bulk_row.find( 'select[name="_status"]' ).val(); | ||
|
||
# save the data | ||
jQuery.ajax | ||
url: ajaxurl | ||
type: 'POST' | ||
async: false | ||
cache: false | ||
data: | ||
action: 'wpt_bulk_editor' | ||
post_ids: post_ids | ||
post_status: post_status | ||
wpt_bulk_editor_ajax_nonce: wpt_bulk_editor_security.nonce | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
/** | ||
* Test case for the Ajax callbacks. | ||
* | ||
* @group ajax | ||
*/ | ||
class WPT_Test_Bulk_Editor_Ajax extends WP_Ajax_UnitTestCase { | ||
function create_event() { | ||
$event_args = array( | ||
'post_type' => WPT_Event::post_type_name, | ||
); | ||
return $this->factory->post->create( $event_args ); | ||
} | ||
|
||
function create_event_for_production($production_id) { | ||
$event_id = $this->create_event(); | ||
add_post_meta( $event_id, WPT_Production::post_type_name, $production_id, true ); | ||
return $event_id; | ||
} | ||
|
||
function create_production() { | ||
$production_args = array( | ||
'post_type' => WPT_Production::post_type_name, | ||
); | ||
return $this->factory->post->create( $production_args ); | ||
} | ||
|
||
function test_events_inherit_production_status_in_bulk() { | ||
global $wp_theatre; | ||
|
||
$production1 = $this->create_production(); | ||
wp_update_post( | ||
array( | ||
'ID' => $production1, | ||
'post_status' => 'draft', | ||
) | ||
); | ||
|
||
$event1 = $this->create_event_for_production( $production1 ); | ||
wp_update_post( | ||
array( | ||
'ID' => $event1, | ||
'post_status' => 'draft', | ||
) | ||
); | ||
|
||
$production2 = $this->create_production(); | ||
wp_update_post( | ||
array( | ||
'ID' => $production2, | ||
'post_status' => 'draft', | ||
) | ||
); | ||
|
||
$event2 = $this->create_event_for_production( $production2 ); | ||
wp_update_post( | ||
array( | ||
'ID' => $event2, | ||
'post_status' => 'draft', | ||
) | ||
); | ||
|
||
$_POST = array( | ||
'wpt_bulk_editor_ajax_nonce' => wp_create_nonce( 'wpt_bulk_editor_ajax_nonce' ), | ||
'post_ids' => array( $production1, $production2 ), | ||
'post_status' => 'publish', | ||
); | ||
|
||
try { | ||
$this->_handleAjax( 'wpt_bulk_editor' ); | ||
} catch ( WPAjaxDieContinueException $e ) { | ||
// We expected this, do nothing. | ||
} | ||
|
||
// Expect 2 upcoming and published events. | ||
$this->assertCount( 2, $wp_theatre->events->get() ); | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters