widgets.php
1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
/**
* Widget utility functions.
*
* @package ContentControl
* @copyright (c) 2023 Code Atlantic LLC.
*/
namespace ContentControl\Widgets;
/**
* Retrieve data for a widget from options table.
*
* @param string $widget_id The unique ID of a widget.
*
* @return array<string,mixed> The array of widget settings or empty array if none
*/
function get_options( $widget_id ) {
static $options = [];
// If already loaded, return existing settings.
if ( ! isset( $options[ $widget_id ] ) ) {
$split_pos = strrpos( $widget_id, '-' );
if ( false === $split_pos ) {
return [];
}
// Examples: "text-2" will return "text", "recent-post-2" will return "recent-post".
$basename = substr( $widget_id, 0, $split_pos );
// Examples: "text-2" will return "2", "recent-post-2" will return "2".
$index = substr( $widget_id, $split_pos + 1 );
$widget_settings = \get_option( 'widget_' . $basename );
if ( isset( $widget_settings[ $index ] ) ) {
$options[ $widget_id ] = parse_options( $widget_settings[ $index ] );
}
}
return parse_options( isset( $options[ $widget_id ] ) ? $options[ $widget_id ] : [] );
}
/**
* Checks for & adds missing widget options to prevent errors or missing data.
*
* @param array<string,mixed> $options Widget options.
*
* @return array<string,mixed>
*/
function parse_options( $options = [] ) {
return wp_parse_args( $options, [
'which_users' => '',
'roles' => [],
] );
}