options.php
3.38 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
/**
* Option functions.
*
* @package ContentControl
*/
namespace ContentControl;
/**
* Get all options
*
* @return array<string,mixed>
*/
function get_all_plugin_options() {
return plugin( 'options' )->get_all();
}
/**
* Get an option
*
* Looks to see if the specified setting exists, returns default if not
*
* @param string $key Option key.
* @param mixed|bool $default_value Default value.
*
* @return mixed|void
*/
function get_plugin_option( $key, $default_value = false ) {
return plugin( 'options' )->get( $key, $default_value );
}
/**
* Update an option
*
* Updates an setting value in both the db and the global variable.
* Warning: Passing in an empty, false or null string value will remove
* the key from the _options array.
*
* @param string $key The Key to update.
* @param string|bool|int $value The value to set the key to.
*
* @return boolean True if updated, false if not.
*/
function update_plugin_option( $key = '', $value = false ) {
return plugin( 'options' )->update( $key, $value );
}
/**
* Update many values at once.
*
* @param array<string,mixed> $new_options Array of new replacement options.
*
* @return bool
*/
function update_plugin_options( $new_options = [] ) {
return plugin( 'options' )->update_many( $new_options );
}
/**
* Remove an option
*
* @param string|string[] $keys Can be a single string or array of option keys.
*
* @return boolean True if updated, false if not.
*/
function delete_plugin_options( $keys = '' ) {
return plugin( 'options' )->delete( $keys );
}
/**
* Get index of blockTypes.
*
* @return array<array{name:string,category:string,description:string,keywords:string[],title:string}>
*/
function get_block_types() {
return \get_option( 'content_control_known_blockTypes', [] );
}
/**
* Sanitize expetced block type data.
*
* @param array<string,string|string[]> $type Block type definition.
* @return array<string,mixed> Sanitized definition.
*/
function sanitize_block_type( $type = [] ) {
foreach ( $type as $key => $value ) {
$type[ $key ] = is_array( $value )
? sanitize_block_type( $value )
: \sanitize_text_field( $value );
}
return $type;
}
/**
* Update block type list.
*
* @param array<array{name:string,category:string,description:string,keywords:string[],title:string}> $incoming_block_types Array of updated block type declarations.
*
* @return void
*/
function update_block_types( $incoming_block_types = [] ) {
$block_types = [];
// Convert to a named index for deduplication.
foreach ( get_block_types() as $type ) {
$block_types[ $type['name'] ] = $type;
}
// Add or update incoming block types into the array.
foreach ( $incoming_block_types as $type ) {
// Sanitize each new block type.
$block_types[ $type['name'] ] = $type;
}
// Flatten values to a simple array for storage.
$block_types = array_values( $block_types );
\update_option( 'content_control_known_blockTypes', $block_types );
}
/**
* Get default denial message.
*
* @return string
*/
function get_default_denial_message() {
if ( \ContentControl\get_data_version( 'settings' ) === 1 ) {
$settings = get_plugin_option( 'jp_cc_settings', [] );
return isset( $settings['default_denial_message'] ) ? $settings['default_denial_message'] : '';
}
$default = __( 'This content is restricted.', 'content-control' );
return get_plugin_option( 'defaultDenialMessage', $default );
}