diff --git a/src/class.settings-api.php b/src/class.settings-api.php index da58270..848bd25 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,77 @@ function callback_color( $args ) { echo $html; } + + /** + * Displays a repeatable for a settings field. Sanitization, is applied to the final 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); + $ex_keys = array_keys($ex_data); + + $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 .= '
'; + $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);