Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion classes/apis/class-common-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public function deploy( Batch $batch, $auto_import = true ) {
$messages = ( isset( $response['messages'] ) ? $response['messages'] : array() );

// Delete batch after deploy.
$delete_batch = apply_filters( 'sme_delete_batch_after_deploy', true );
$delete_batch = apply_filters( 'sme_delete_batch_after_deploy', $batch->get_batch_setting( 'delete_batch_after_deploy' ));

/*
* Batch has been deployed and should no longer be accessible by user,
Expand Down
86 changes: 86 additions & 0 deletions classes/controllers/class-batch-ctrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ class Batch_Ctrl {
*/
private $post_dao;

/**
* @var Batch_Settings_Prefix
*/
static public $batch_settings_prefix = '_sme_batch_setting';

/**
* Constructor.
*
Expand Down Expand Up @@ -211,13 +216,17 @@ public function edit_batch() {
// Get WordPress options settings for this batch.
$wp_options = $this->get_wp_options_settings( $batch );

// Get batch settings specific for this batch.
$batch_settings = $this->get_batch_settings( $batch );

$data = array(
'batch' => $batch,
'label' => $label,
'filters' => $filters,
'table' => $table,
'post_ids' => implode( ',', $post_ids ),
'wp_options' => $wp_options,
'batch_settings' => $batch_settings
);

$this->template->render( 'edit-batch', $data );
Expand Down Expand Up @@ -855,6 +864,9 @@ private function handle_edit_batch_form_data( Batch $batch, $request_data ) {
// Set whether WordPress options should be included in batch or not.
$this->should_include_wp_options( $batch, $request_data );

// Additional settings for this batch.
$this->process_batch_settings( $batch, $request_data );

// Posts that was previously in this batch.
$old_post_ids = $this->batch_dao->get_post_meta( $batch->get_id(), 'sme_selected_post' );

Expand Down Expand Up @@ -892,6 +904,28 @@ private function should_include_wp_options( Batch $batch, $request ) {
update_post_meta( $batch->get_id(), '_sme_include_wp_options', $include_wp_options );
}

/**
* Save each batch option as post metadata
*
* @param Batch $batch
* @param array $request
*/
private function process_batch_settings( Batch $batch, $request ) {

//Reset all settings
$_batch_settings = array();
$_batch_prefix_len = strlen($this->batch_settings_prefix);

foreach( $request as $meta_key => $meta_value ) {
if ( substr($meta_key, 0, $_batch_prefix_len) == $this->batch_settings_prefix ) {
$_batch_settings[substr($meta_key, $_batch_prefix_len)] = $meta_value;
}
}

add_post_meta( $batch->get_id(), $this->batch_settings_prefix, $_batch_settings, true );
}


/**
* Get WordPress options settings.
*
Expand Down Expand Up @@ -933,6 +967,58 @@ private function get_wp_options_settings( Batch $batch ) {
return $settings;
}

/**
* Get batch settings.
*
* @param Batch $batch
* @return array Associative array.
* The following keys will always be available:
* - title (string)
* - settings (array)
*/
private function get_batch_settings( Batch $batch ) {
return array(
'title' => __( 'Batch Settings', 'sme-content-staging' ),
'settings' => $this->prepare_batch_settings(
$batch,
array(
'delete_batch_after_deploy' => 'Delete this batch after deploying?'
)
)
);
}


/**
* Create an array of settings as batch settings.
*
* @param $settings
* @return array Numeric array were each item consist of an associative
* array with the following keys:
* Each array will always contain string values only.
* - (string) id
* - (string) title
* - (string) checked
*/
private function prepare_batch_settings( Batch $batch, $settings ) {

$_settings = array();
foreach ( $settings as $key => $description ) {

//Create a unique ID
$id = $this->batch_settings_prefix . $key;

// put everything together and append to $_settings
$_settings[] = array(
'id' => $id,
'title' => __( $description, 'sme-content-staging' ),
'checked' => $batch->get_batch_setting( $key ) ? "checked" : ""
);
}

return $_settings;
}

/**
* Pre-flight checks for a specific part of a batch.
*
Expand Down
16 changes: 16 additions & 0 deletions classes/models/class-batch.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Me\Stenberg\Content\Staging\Models;

use Exception;
use Me\Stenberg\Content\Staging\Controllers\Batch_Ctrl;

class Batch extends Model {

Expand Down Expand Up @@ -97,6 +98,13 @@ class Batch extends Model {
*/
private $post_rel_keys;

/**
* Custom data added by third-party developer.
*
* @var array
*/
private $batch_settings;

/**
* Custom data added by third-party developer.
*
Expand Down Expand Up @@ -332,6 +340,14 @@ public function get_post_rel_keys() {
return $this->post_rel_keys;
}

public function get_batch_settings() {
return get_post_meta( $this->get_id(), Batch_Ctrl::$batch_settings_prefix, true );
}

public function get_batch_setting( $key ) {
return (isset($this->get_batch_settings()[$key]));
}

/**
* Replace custom data with custom data in provided array.
*
Expand Down
11 changes: 11 additions & 0 deletions templates/edit-batch.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@
<br><br>
</p>

<h2><?php echo $batch_settings['title']; ?></h2>
<?php foreach ($batch_settings['settings'] as $_setting): ?>
<p>
<input type="checkbox" name="<?php echo $_setting['id']; ?>" id="<?php echo $_setting['id']; ?>" <?php echo $_setting['checked']; ?>>
<label for="<?php echo $_setting['id']; ?>">
<?php echo $_setting['title']; ?>
</label>
<br><br>
</p>
<?php endforeach; ?>

<?php do_action( 'sme_view_edit_batch_pre_buttons', $batch ); ?>

<?php submit_button( 'Save Batch', 'primary', 'submit', false ); ?>
Expand Down