From 292b8bc9159e7cca6a547195f9e627f525efe739 Mon Sep 17 00:00:00 2001 From: mattclegg Date: Thu, 24 Sep 2015 00:14:51 +0100 Subject: [PATCH 1/2] Add support for batch based settings --- classes/controllers/class-batch-ctrl.php | 85 ++++++++++++++++++++++++ classes/models/class-batch.php | 16 +++++ templates/edit-batch.php | 11 +++ 3 files changed, 112 insertions(+) diff --git a/classes/controllers/class-batch-ctrl.php b/classes/controllers/class-batch-ctrl.php index 7df6ed3..f938173 100644 --- a/classes/controllers/class-batch-ctrl.php +++ b/classes/controllers/class-batch-ctrl.php @@ -47,6 +47,11 @@ class Batch_Ctrl { */ private $post_dao; + /** + * @var Batch_Settings_Prefix + */ + static public $batch_settings_prefix = '_sme_batch_setting'; + /** * Constructor. * @@ -211,6 +216,9 @@ 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, @@ -218,6 +226,7 @@ public function edit_batch() { 'table' => $table, 'post_ids' => implode( ',', $post_ids ), 'wp_options' => $wp_options, + 'batch_settings' => $batch_settings ); $this->template->render( 'edit-batch', $data ); @@ -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' ); @@ -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. * @@ -933,6 +967,57 @@ 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( + ) + ) + ); + } + + + /** + * 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. * diff --git a/classes/models/class-batch.php b/classes/models/class-batch.php index e3ca961..c21589b 100644 --- a/classes/models/class-batch.php +++ b/classes/models/class-batch.php @@ -2,6 +2,7 @@ namespace Me\Stenberg\Content\Staging\Models; use Exception; +use Me\Stenberg\Content\Staging\Controllers\Batch_Ctrl; class Batch extends Model { @@ -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. * @@ -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. * diff --git a/templates/edit-batch.php b/templates/edit-batch.php index f8d10a8..1c80095 100644 --- a/templates/edit-batch.php +++ b/templates/edit-batch.php @@ -31,6 +31,17 @@

+

+ +

+ > + +

+

+ + From edc048edd697b3afe794b4d7630018fb8f99ea1f Mon Sep 17 00:00:00 2001 From: mattclegg Date: Thu, 24 Sep 2015 00:15:54 +0100 Subject: [PATCH 2/2] Add delete batch as a setting --- classes/apis/class-common-api.php | 2 +- classes/controllers/class-batch-ctrl.php | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/classes/apis/class-common-api.php b/classes/apis/class-common-api.php index 5e117f5..d69e1c5 100644 --- a/classes/apis/class-common-api.php +++ b/classes/apis/class-common-api.php @@ -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, diff --git a/classes/controllers/class-batch-ctrl.php b/classes/controllers/class-batch-ctrl.php index f938173..2e4478b 100644 --- a/classes/controllers/class-batch-ctrl.php +++ b/classes/controllers/class-batch-ctrl.php @@ -982,6 +982,7 @@ private function get_batch_settings( Batch $batch ) { 'settings' => $this->prepare_batch_settings( $batch, array( + 'delete_batch_after_deploy' => 'Delete this batch after deploying?' ) ) );