option_group, $this->option_name );
}
/**
* Render fields.
*
* @param string $page Page to be rendered fields for.
* @param string $section_id ID of the section to render fields for.
* @param array $fields List of fields to render.
*
* @return bool True on success, false on failure.
*/
public function render_fields( $page, $section_id, $fields ) {
if ( empty( $fields ) ) {
return false;
}
foreach ( $fields as $field ) {
$args = isset( $field['args'] ) ? $field['args'] : [];
if ( ! isset( $args['label_for'] ) ) {
$args['label_for'] = $field['id'];
}
if ( ! isset( $args['description'] ) ) {
$args['description'] = $field['description'];
}
if ( isset( $field['options'] ) ) {
$args['options'] = $field['options'];
}
add_settings_field(
$field['id'],
$field['title'],
[ $this, $field['callback'] ],
$page,
$section_id,
$args
);
}
return true;
}
/**
* Helper to render select.
*
* @param array $args List of passed arguments.
*/
public function input_select( $args ) {
?>
= $args['description'] ?>
= $args['description'] ?>
= $args['description'] ?>
= $args['description'] ?>
= $args['description'] ?>
= $args['description'] ?>
alert_key, 'anycomment_message', __( 'Settings Saved', 'anycomment' ), 'updated' );
}
settings_errors( $this->alert_key );
?>
';
$i = 0;
foreach ( (array) $wp_settings_sections[ $page ] as $section ) {
echo '
' . $section['title'] . '
';
$i ++;
}
echo '';
?>
{$section['title']}\n";
}
if ( $includeHeader && $section['callback'] ) {
call_user_func( $section['callback'], $section );
}
if ( ! isset( $wp_settings_fields ) || ! isset( $wp_settings_fields[ $page ] ) || ! isset( $wp_settings_fields[ $page ][ $section['id'] ] ) ) {
continue;
}
echo '';
$i ++;
}
}
/**
* Check whether there are any options set on model.
*
* @return bool
*/
public function hasOptions() {
$options = $this->getOptions();
if ( $options === null ) {
return false;
}
$nonEmptyCount = 0;
foreach ( $options as $key => $optionValue ) {
if ( ! empty( $optionValue ) ) {
$nonEmptyCount ++;
}
}
return $nonEmptyCount > 0;
}
/**
* Get single option.
*
* @param string $name Options name to search for.
*
* @return mixed|null
*/
public function getOption( $name ) {
$options = $this->getOptions();
$optionValue = isset( $options[ $name ] ) ? trim( $options[ $name ] ) : null;
return ! empty( $optionValue ) ? $optionValue : null;
}
/**
* Get list of social options.
* @return array|null
*/
public function getOptions() {
if ( $this->options === null ) {
$this->options = get_option( $this->option_name, null );
}
if ( ! empty( $this->default_options ) ) {
foreach ( $this->default_options as $key => $optionValue ) {
$setDefault = ! isset( $this->options[ $key ] ) && ! strpos( $key, 'toggle' ) ||
isset( $this->options[ $key ] ) && empty( $this->options[ $key ] );
if ( $setDefault ) {
$this->options[ $key ] = $optionValue;
}
}
}
return $this->options;
}
/**
* Get instance of currently running class.
* @return self
*/
public static function instance() {
$className = get_called_class();
if ( ! isset( self::$_instances[ $className ] ) ) {
self::$_instances[ $className ] = new $className( false );
}
return self::$_instances[ $className ];
}
}
endif;