slug = $slug; $this->option_key = 'pys_' . $slug; } public function getSlug() { return $this->slug; } /** * Load options fields and options defaults from specified files * * @param string $fields Path to options fields file * @param string $defaults Path to options defaults file */ public function locateOptions( $fields, $defaults ) { $this->loadJSON( $fields, false ); $this->loadJSON( $defaults, true ); $this->defaults_json_path = $defaults; } public function resetToDefaults() { if ( ! file_exists( $this->defaults_json_path ) ) { return; } $content = file_get_contents( $this->defaults_json_path ); $values = json_decode( $content, true ); $this->updateOptions( $values ); } /** * Load options fields or options defaults from specified file * * @param string $file * @param bool $is_defaults */ private function loadJSON( $file, $is_defaults ) { if ( ! file_exists( $file ) ) { return; } $content = file_get_contents( $file ); $values = json_decode( $content, true ); if ( null === $values ) { return; } if ( $is_defaults ) { $this->defaults = $values; } else { $this->options = $values; } } /** * Add new option field * * @param string $key * @param string $field_type * @param mixed $default */ public function addOption( $key, $field_type, $default ) { $this->options[ $key ] = $field_type; $this->defaults[ $key ] = $default; } /** * Gets an option value or its default value * * @param string $key Option key * @param mixed $fallback Option fallback value if no default is set * * @return mixed The value specified for the option or a default value for the option. */ public function getOption( $key, $fallback = null ) { $this->maybeLoad(); // get option default if unset if ( ! isset( $this->values[ $key ] ) ) { $this->values[ $key ] = isset( $this->defaults[ $key ] ) ? $this->defaults[ $key ] : null; } // use fall back value if default is not set if ( null === $this->values[ $key ] && ! is_null( $fallback ) ) { $this->values[ $key ] = $fallback; } return $this->values[ $key ]; } public function setOption($key, $value){ $this->maybeLoad(); if (isset($value) ) { $this->values[ $key ] = $value; } } /** * Load values from database * * @param bool $force Force options load */ private function maybeLoad( $force = false ) { if ( $force || empty( $this->values ) ) { $this->values = get_option( $this->option_key, null ); } // if there are no settings defined, use default values if ( ! is_array( $this->values ) ) { $this->values = $this->defaults; } } public function reloadOptions() { $this->maybeLoad( true ); } /** * Sanitize and save options * * @param null|array $values Optional. If set, options values will be received from param instead of $_POST. */ public function updateOptions( $values = null ) { $this->maybeLoad(); if ( is_array( $values ) ) { $form_data = $values; } else { $form_data = isset( $_POST['pys'][ $this->slug ] ) ? $_POST['pys'][ $this->slug ] : array(); } // save posted fields foreach ( $form_data as $key => $value ) { if ( isset( $this->options[ $key ] ) ) { $this->values[ $key ] = $this->sanitize_form_field( $key, $value ); } } update_option( $this->option_key, $this->values ); } /** * Sanitize form field * * @param string $key Field key * @param array $value Field value * * @return mixed Sanitized field value */ private function sanitize_form_field( $key, $value ) { $type = $this->options[ $key ]; // look for very specific sanitization filter $filter_name = "{$this->option_key}_settings_sanitize_{$key}_field"; if ( has_filter( $filter_name ) ) { return apply_filters( $filter_name, $value ); } // look for a sanitize_FIELDTYPE_field method if ( is_callable( array( $this, 'sanitize_' . $type . '_field' ) ) ) { return $this->{'sanitize_' . $type . '_field'}( $value ); } // fallback to text return $this->sanitize_text_field( $value ); } /** * Output text input * * @param $key * @param string $placeholder * @param bool $disabled * @param bool $hidden * @param bool $empty */ public function render_text_input( $key, $placeholder = '', $disabled = false, $hidden = false, $empty = false) { $attr_name = "pys[$this->slug][$key]"; $attr_id = 'pys_' . $this->slug . '_' . $key; $attr_value = $empty == false ? $this->getOption( $key ) : ""; $classes = array( 'form-control' ); if( $hidden ) { $classes[] = 'form-control-hidden'; } $classes = implode( ' ', $classes ); ?> type="text" name="" id="" value="" placeholder="" class=""> slug][$key][]"; $attr_id = 'pys_' . $this->slug . '_' . $key . '_' . $index; $values = (array) $this->getOption( $key ); $attr_value = isset( $values[ $index ] ) ? $values[ $index ] : null; ?> slug][$key][]"; $attr_id = 'pys_' . $this->slug . '_' . $key . '_' . $index; $values = (array) $this->getOption( $key ); $attr_value = isset( $values[ $index ] ) ? $values[ $index ] : null; ?> slug][$key][]"; $attr_id = 'pys_' . $this->slug . '_' . $key . '_' . $index; $values = (array) $this->getOption( $key ); $attr_value = isset( $values[ $index ] ) ? $values[ $index ] : null; ?> name="" id="" value="" placeholder="" class="form-control"> slug][$key]"; $attr_id = 'pys_' . $this->slug . '_' . $key; $attr_value = $this->getOption( $key ); $classes = array( 'form-control' ); if( $hidden ) { $classes[] = 'form-control-hidden'; } $classes = implode( ' ', $classes ); ?> slug][$key]"; $attr_id = 'pys_' . $this->slug . '_' . $key; $attr_value = $this->getOption( $key ); $classes = array( 'custom-switch' ); if ( $collapse ) { $classes[] = 'collapse-control'; } if ( $disabled ) { $classes[] = 'disabled'; $attr_name = ""; $attr_value = false; } $classes = implode( ' ', $classes ); ?>
id="" class="custom-switch-input" data-target="pys_slug ); ?>__panel"> id="" class="custom-switch-input">
slug][$key]"; $attr_value = $this->getOption( $key ); ?> slug][$key]"; ?> slug][$key]"; $attr_id = 'pys_' . $this->slug . '_' . $key; $attr_value = $this->getOption( $key ); ?> type="number" name="" id="" value="" placeholder="" min="0" class="form-control"> slug][$key]"; $attr_id = 'pys_' . $this->slug . '_' . $key; $classes = array( 'form-control-sm' ); if ( $visibility_target ) { $classes[] = 'controls-visibility'; } $classes = implode( ' ', $classes ); ?> slug][$key][]"; $attr_id = 'pys_' . $this->slug . '_' . $key; $selected = $this->getOption( $key ); ?> slug][$key][]"; $attr_id = 'pys_' . $this->slug . '_' . $key; $tags = $this->getOption( $key ); $tags = is_array( $tags ) ? array_filter( $tags ) : array(); ?> $value ) { $new_value = $this->sanitize_text_field( $value ); if ( ! empty( $new_value ) && ! in_array( $new_value, $sanitized ) ) { $sanitized[ $key ] = $new_value; } } return $sanitized; } /** * Sanitize array field value * * @param $values * * @return array */ public function sanitize_array_textarea_field( $values ) { $values = is_array( $values ) ? $values : array(); $sanitized = array(); foreach ( $values as $key => $value ) { $new_value = $this->sanitize_textarea_field( $value ); if ( ! empty( $new_value ) && ! in_array( $new_value, $sanitized ) ) { $sanitized[ $key ] = $new_value; } } return $sanitized; } public function render_checkbox_input_array( $key, $label, $index = 0, $disabled = false ) { $attr_name = "pys[$this->slug][$key][]"; $attr_values = (array)$this->getOption( $key ); $value = "index_".$index; $valueIndex = array_search($value,$attr_values); ?>