From 5e0eea951087fc77964c7b43384d16197c1362c0 Mon Sep 17 00:00:00 2001 From: Eric Fernance Date: Tue, 18 Oct 2016 20:06:15 +1000 Subject: [PATCH 1/3] Adds initial repeatable and save data --- src/class.settings-api.php | 71 ++++++++++++++++++ src/js/jquery.repeatable.js | 142 ++++++++++++++++++++++++++++++++++++ 2 files changed, 213 insertions(+) create mode 100644 src/js/jquery.repeatable.js diff --git a/src/class.settings-api.php b/src/class.settings-api.php index da58270..87a624b 100755 --- a/src/class.settings-api.php +++ b/src/class.settings-api.php @@ -26,6 +26,12 @@ class WeDevs_Settings_API { */ protected $settings_fields = array(); + /** + * Tracks whether the repeatable JS has been loaded to prevent multiple calls enqueue_script + * @var bool + */ + protected $repeatable_loaded = false; + public function __construct() { add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); } @@ -132,6 +138,7 @@ function admin_init() { 'std' => isset( $option['default'] ) ? $option['default'] : '', 'sanitize_callback' => isset( $option['sanitize_callback'] ) ? $option['sanitize_callback'] : '', 'type' => $type, + 'children' => isset( $option['children'] ) ? $option['children'] : null, ); add_settings_field( $section . '[' . $option['name'] . ']', $option['label'], array( $this, 'callback_' . $type ), $section, $section, $args ); @@ -385,6 +392,70 @@ function callback_color( $args ) { echo $html; } + + /** + * Displays a repeatable for a settings field. + * + * @param array $args settings field args + */ + function callback_repeatable( $args ) { + + if (!$this->repeatable_loaded) { + $url = plugins_url("js/jquery.repeatable.js",__FILE__); + wp_enqueue_script("jquery.repeatable", $url, array("jquery")); + } + + // Add the repeatable + $ex_data = json_decode($this->get_option( $args['id'], $args['section'], $args['std']), true); + $html = '
'; + + foreach ($args['children'] as $child) { + $html .= ''; + $html .= ''; + } + $html .= ''; + $html .= '
'; + $html .= ''; + echo $html; + + // Add the field group template + $template = ''; + + echo $template; + + // Push in the JS to make the repeatable go. + echo ''; + + // On save grab all the values and insert them into the hidden field -- -- this needs to be improved + ?> + + iterated placeholder + template = template.replace(/\{[^\?\}]*\}/g, ""); // {valuePlaceholder} => "" + return $(template); + }; + + /** + * Determines if the add trigger + * needs to be disabled + * @return null + */ + var maintainAddBtn = function () { + if (!settings.max) { + return; + } + + if (total === settings.max) { + $(settings.addTrigger).attr("disabled", "disabled"); + } else if (total < settings.max) { + $(settings.addTrigger).removeAttr("disabled"); + } + }; + + /** + * Setup the repeater + * @return null + */ + (function () { + $(settings.addTrigger).on("click", addOne); + $("form").on("click", settings.deleteTrigger, deleteOne); + + if (!total) { + var toCreate = settings.startWith - total; + for (var j = 0; j < toCreate; j++) { + createOne(); + } + } + + })(); + }; + +})(jQuery); From 002196e079cc3ed61e5d386e7470787a4071394c Mon Sep 17 00:00:00 2001 From: Eric Fernance Date: Tue, 18 Oct 2016 20:40:56 +1000 Subject: [PATCH 2/3] Shows existing data --- src/class.settings-api.php | 20 +++++++++++++++----- src/js/jquery.repeatable.js | 1 + 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/class.settings-api.php b/src/class.settings-api.php index 87a624b..fc84a95 100755 --- a/src/class.settings-api.php +++ b/src/class.settings-api.php @@ -407,13 +407,23 @@ function callback_repeatable( $args ) { // Add the repeatable $ex_data = json_decode($this->get_option( $args['id'], $args['section'], $args['std']), true); + $ex_keys = array_keys($ex_data); + $html = '
'; - - foreach ($args['children'] as $child) { - $html .= ''; - $html .= ''; + + if (count($ex_keys) > 0) { + foreach ($ex_data[$ex_keys[0]] as $i=>$v) { + $html .= '
'; + foreach ($args['children'] as $child) { + $html .= ''; + $html .= ''; + } + $html .= ''; + $html .= '
'; + } } - $html .= ''; + echo "ERIC YOU NEED TO WRITE A DELETE CALL BACK"; + $html .= '
'; $html .= ''; echo $html; diff --git a/src/js/jquery.repeatable.js b/src/js/jquery.repeatable.js index c85a046..476088e 100644 --- a/src/js/jquery.repeatable.js +++ b/src/js/jquery.repeatable.js @@ -1,4 +1,5 @@ /*------------------------------------------- + * This is basically just a cut and paste of the below * https://github.com/jenwachter/jquery.repeatable *------------------------------------------*/ From d0a0e8ec99162f447e1e281d13eacfadcf1da7d7 Mon Sep 17 00:00:00 2001 From: Eric Fernance Date: Wed, 19 Oct 2016 07:03:25 +1000 Subject: [PATCH 3/3] Adds unique IDs for existing --- src/class.settings-api.php | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/class.settings-api.php b/src/class.settings-api.php index fc84a95..848bd25 100755 --- a/src/class.settings-api.php +++ b/src/class.settings-api.php @@ -394,7 +394,7 @@ function callback_color( $args ) { /** - * Displays a repeatable for a settings field. + * Displays a repeatable for a settings field. Sanitization, is applied to the final field. * * @param array $args settings field args */ @@ -416,13 +416,12 @@ function callback_repeatable( $args ) { $html .= '
'; foreach ($args['children'] as $child) { $html .= ''; - $html .= ''; + $html .= ''; } $html .= ''; $html .= '
'; } } - echo "ERIC YOU NEED TO WRITE A DELETE CALL BACK"; $html .= ''; $html .= ''; @@ -456,9 +455,7 @@ function callback_repeatable( $args ) { data[''].push(jQuery(el).val()); }); - - console.log(JSON.stringify(data)); - + jQuery("#_").val(JSON.stringify(data)); });