Skip to content

Commit

Permalink
Moved the production bulk editor to a dedicated class.
Browse files Browse the repository at this point in the history
  • Loading branch information
slimndap committed Jul 3, 2015
1 parent 13b312b commit 6f2ce9f
Show file tree
Hide file tree
Showing 8 changed files with 198 additions and 61 deletions.
5 changes: 0 additions & 5 deletions functions/wpt_admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ function __construct() {

// More hooks (always load, necessary for bulk editing through AJAX)
add_filter('request', array($this,'request'));
add_action( 'bulk_edit_custom_box', array($this,'bulk_edit_custom_box'), 10, 2 );

// Options
$this->options = get_option( 'wp_theatre' );
Expand Down Expand Up @@ -625,10 +624,6 @@ function quick_edit_custom_box($column_name, $post_type) {
}
}

function bulk_edit_custom_box($column_name, $post_type) {
wp_nonce_field($post_type, $post_type.'_nonce' );
}

/**
* Admin setting.
*/
Expand Down
87 changes: 87 additions & 0 deletions functions/wpt_bulk_editor.php
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();
}
}
31 changes: 0 additions & 31 deletions functions/wpt_setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -491,37 +491,6 @@ function updated_post_meta($meta_id, $object_id, $meta_key, $meta_value) {
}
}

add_action( 'wp_ajax_save_bulk_edit_'.WPT_Production::post_type_name, 'wp_ajax_save_bulk_edit_production' );
function wp_ajax_save_bulk_edit_production() {
$wpt_admin = new WPT_Admin();

// TODO perform nonce checking
remove_action( 'save_post', array( $this, 'save_post' ) );

$post_ids = ( ! empty( $_POST[ 'post_ids' ] ) ) ? $_POST[ 'post_ids' ] : array();
if ( ! empty( $post_ids ) && is_array( $post_ids ) ) {
if ($_POST['post_status']!=-1) {
// Status of production is updated
foreach( $post_ids as $post_id ) {
// Update status of connected Events
$events = $wpt_admin->get_events($post_id);
foreach($events as $event) {

$post = array(
'ID'=>$event->ID,
'post_status'=>$_POST[ 'post_status' ]
);
wp_update_post($post);
}
}
}
}

add_action( 'save_post', array( $this, 'save_post' ) );

die();
}



?>
2 changes: 1 addition & 1 deletion js/admin.js

Large diffs are not rendered by default.

25 changes: 1 addition & 24 deletions raw/coffee/admin.coffee
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# @codekit-prepend '../js/jquery.datetimepicker.js'
# @codekit-prepend 'wpt_bulk_editor.coffee'
# @codekit-prepend 'wpt_event_editor.coffee'
# @codekit-prepend 'wpt_production_permalink.coffee'

Expand All @@ -17,27 +18,3 @@ class wpt_admin_ticketspage
else
@.ticketspage.hide(500)

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', () ->
# define the bulk edit row
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: 'save_bulk_edit_wp_theatre_prod'
post_ids: post_ids
post_status: post_status
27 changes: 27 additions & 0 deletions raw/coffee/wpt_bulk_editor.coffee
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

80 changes: 80 additions & 0 deletions tests/test_bulk_editor.php
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() );
}

}

2 changes: 2 additions & 0 deletions theater.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ function __construct() {
$this->event_editor = new WPT_Event_Editor();
$this->cart = new WPT_Cart();
$this->production_permalink = new WPT_Production_Permalink();
$this->bulk_editor = new WPT_Bulk_Editor();
if (is_admin()) {
} else {
$this->frontend = new WPT_Frontend();
Expand Down Expand Up @@ -109,6 +110,7 @@ function includes() {
require_once(dirname(__FILE__) . '/functions/wpt_importer.php');
require_once(dirname(__FILE__) . '/functions/wpt_cart.php');
require_once(dirname(__FILE__) . '/functions/wpt_production_permalink.php');
require_once(dirname(__FILE__) . '/functions/wpt_bulk_editor.php');
if (is_admin()) {
} else {
require_once(dirname(__FILE__) . '/functions/wpt_frontend.php');
Expand Down

0 comments on commit 6f2ce9f

Please sign in to comment.