Admin_Page::add_sections()
last analyzed

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
nc 1
dl 0
loc 1
c 0
b 0
f 0
1
<?php
2
/**
3
 * Settings Page
4
 *
5
 * @package SimpleCalendar/Admin
6
 */
7
namespace SimpleCalendar\Abstracts;
8
9
if ( ! defined( 'ABSPATH' ) ) {
10
	exit;
11
}
12
13
/**
14
 * The Admin Page.
15
 *
16
 * @since 3.0.0
17
 */
18
abstract class Admin_Page {
19
20
	/**
21
	 * Admin page ID.
22
	 *
23
	 * @access public
24
	 * @var string
25
	 */
26
	public $id = '';
27
28
	/**
29
	 * Option group.
30
	 *
31
	 * @access public
32
	 * @var string
33
	 */
34
	public $option_group = '';
35
36
	/**
37
	 * Admin Page label.
38
	 *
39
	 * @access public
40
	 * @var string
41
	 */
42
	public $label = '';
43
44
	/**
45
	 * Admin Page description.
46
	 *
47
	 * @access public
48
	 * @var string
49
	 */
50
	public $description = '';
51
52
	/**
53
	 * Amdin Page settings sections.
54
	 *
55
	 * @access public
56
	 * @var array Associative array with section id (key) and section name (value)
57
	 */
58
	public $sections;
59
60
	/**
61
	 * Admin Page settings fields.
62
	 *
63
	 * @access public
64
	 * @var array
65
	 */
66
	public $fields;
67
68
	/**
69
	 * Saved values.
70
	 *
71
	 * @access protected
72
	 * @var array
73
	 */
74
	protected $values = array();
75
76
	/**
77
	 * Get admin page settings.
78
	 *
79
	 * @since  3.0.0
80
	 *
81
	 * @return array
82
	 */
83
	public function get_settings() {
84
85
		$settings = array();
86
87
		$settings[ $this->id ] = array(
88
			'label'         => $this->label,
89
			'description'   => $this->description,
90
		);
91
92
		if ( ! empty( $this->sections ) && is_array( $this->sections ) ) {
93
94
			foreach ( $this->sections as $section => $content ) {
95
96
				$settings[ $this->id ]['sections'][ $section ] = array(
97
					'title'         => isset( $content['title'] ) ? $content['title'] : '',
98
					'description'   => isset( $content['description'] ) ? $content['description'] : '',
99
					'callback'      => array( $this, 'add_settings_section_callback' ),
100
					'fields'        => isset( $this->fields[ $section ] ) ? $this->fields[ $section ] : '',
101
				);
102
103
			}
104
105
		}
106
107
		return apply_filters( 'simcal_get_' . $this->option_group . '_' . $this->id , $settings );
108
	}
109
110
	/**
111
	 * Get option value.
112
	 *
113
	 * @since  3.0.0
114
	 * @access protected
115
	 *
116
	 * @param  string $section
117
	 * @param  string $setting
118
	 *
119
	 * @return string
120
	 */
121
	protected function get_option_value( $section, $setting ) {
122
123
		$option = $this->values;
124
125
		if ( ! empty( $option ) && is_array( $option ) ) {
126
			return isset( $option[ $section ][ $setting ] ) ? $option[ $section ][ $setting ] : '';
127
		}
128
129
		return '';
130
	}
131
132
	/**
133
	 * Add sections for this page.
134
	 *
135
	 * @since  3.0.0
136
	 *
137
	 * @return array
138
	 */
139
	abstract public function add_sections();
140
141
	/**
142
	 * Get settings fields.
143
	 *
144
	 * @since  3.0.0
145
	 *
146
	 * @return array
147
	 */
148
	abstract public function add_fields();
149
150
	/**
151
	 * Default basic callback for page sections.
152
	 *
153
	 * @since  3.0.0
154
	 *
155
	 * @param  array $section
156
	 *
157
	 * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
158
	 */
159
	public function add_settings_section_callback( $section ) {
160
161
		$callback    = isset( $section['callback'][0] ) ? $section['callback'][0] : '';
162
		$sections    = isset( $callback->sections ) ? $callback->sections : '';
163
		$description = isset( $sections[ $section['id'] ]['description'] ) ? $sections[ $section['id'] ]['description'] : '';
164
		$default     = $description ? '<p>' . $description . '</p>' : '';
165
166
		echo apply_filters( 'simcal_' . $this->option_group . '_' . $this->id . '_sections_callback', $default );
167
	}
168
169
	/**
170
	 * Register setting callback.
171
	 *
172
	 * Callback function for sanitizing and validating options before they are updated.
173
	 *
174
	 * @since  3.0.0
175
	 *
176
	 * @param  array $settings Settings inputs.
177
	 *
178
	 * @return array Sanitized settings.
0 ignored issues
show
Documentation introduced by
Should the return type not be array|string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
179
	 */
180
	public function validate( $settings ) {
181
182
		$sanitized = '';
183
184
		if ( is_array( $settings ) ) {
185
			foreach ( $settings as $k => $v ) {
186
				$sanitized[ $k ] = simcal_sanitize_input( $v );
187
			}
188
		} else {
189
			$sanitized = simcal_sanitize_input( $settings );
190
		}
191
192
		return $sanitized;
193
	}
194
195
}
196