pageName = $args['page_name']; $this->settingsName = $args['field_name']; $this->sectionName = $this->settingsName . '_section'; $this->tableList = Collection::make($args['table_list']); $this->optionValue = get_option($this->settingsName); $this->fieldLabel = isset($args['field_label']) ? stripslashes($args['field_label']) : ''; $this->classAttribute = isset($args['class_attribute']) ? $args['class_attribute'] : ''; $this->description = isset($args['description']) ? $args['description'] : ''; add_action('admin_init', [$this, 'optionsSettingsInit']); } public function optionsSettingsInit() { register_setting( $this->pageName, $this->settingsName, [ 'type' => 'string', 'group' => $this->pageName, 'description' => '', 'sanitize_callback' => [$this, 'optionCallback'] ] ); add_settings_section( $this->sectionName, '', '', $this->pageName ); add_settings_field( $this->settingsName, $this->fieldLabel, [$this, 'displayCallback'], $this->pageName, $this->sectionName, ['class' => $this->classAttribute] ); } public function optionCallback($value) { if (has_filter(Plugin::PREFIX_ . 'sanitize_option_' . $this->settingsName)) { return apply_filters(Plugin::PREFIX_ . 'sanitize_option_' . $this->settingsName, $value); } if (!$this->tableList->pluck('value')->contains($value)) { return; } return $value; } public function displayCallback() { print('
'); printf('%s', $this->fieldLabel); $this->tableList->map(function ($row) { $checked = checked($row['value'], $this->optionValue, false); if ($this->optionValue === false) { $checked = checked($row['value'], $this->tableList->first()['value'], false); } printf( '
', $this->settingsName, $row['value'], $checked, $row['label'] ); }); print($this->description($this->description)); print('
'); } }