PluginProbe
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More / trunk
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More vtrunk
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.10 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 2.0.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 47 releases
content-control / inc / functions / options.php

options.php in Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More trunk, at inc/functions/options.php

166 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Option functions.
4 *
5 * @package ContentControl
6 */
7
8 namespace ContentControl;
9
10 defined( 'ABSPATH' ) || exit;
11
12 /**
13 * Get all options
14 *
15 * @return array<string,mixed>
16 */
17 function get_all_plugin_options() {
18 return plugin( 'options' )->get_all();
19 }
20
21 /**
22 * Get an option
23 *
24 * Looks to see if the specified setting exists, returns default if not
25 *
26 * @param string $key Option key.
27 * @param mixed|bool $default_value Default value.
28 *
29 * @return mixed|void
30 */
31 function get_plugin_option( $key, $default_value = false ) {
32 return plugin( 'options' )->get( $key, $default_value );
33 }
34
35 /**
36 * Update an option
37 *
38 * Updates an setting value in both the db and the global variable.
39 * Warning: Passing in an empty, false or null string value will remove
40 * the key from the _options array.
41 *
42 * @param string $key The Key to update.
43 * @param string|bool|int $value The value to set the key to.
44 *
45 * @return boolean True if updated, false if not.
46 */
47 function update_plugin_option( $key = '', $value = false ) {
48 return plugin( 'options' )->update( $key, $value );
49 }
50
51 /**
52 * Update many values at once.
53 *
54 * @param array<string,mixed> $new_options Array of new replacement options.
55 *
56 * @return bool
57 */
58 function update_plugin_options( $new_options = [] ) {
59 return plugin( 'options' )->update_many( $new_options );
60 }
61
62 /**
63 * Remove an option
64 *
65 * @param string|string[] $keys Can be a single string or array of option keys.
66 *
67 * @return boolean True if updated, false if not.
68 */
69 function delete_plugin_options( $keys = '' ) {
70 return plugin( 'options' )->delete( $keys );
71 }
72
73 /**
74 * Get index of blockTypes.
75 *
76 * @return array<array{name:string,category:string,description:string,keywords:string[],title:string}>
77 */
78 function get_block_types() {
79 return \get_option( 'content_control_known_blockTypes', [] );
80 }
81
82 /**
83 * Delete block types cache.
84 *
85 * @return void
86 */
87 function delete_block_types_cache() {
88 \delete_option( 'content_control_known_blockTypes' );
89 }
90
91 /**
92 * Purge block types cache on update.
93 *
94 * @param string $old_version The old version.
95 * @param string $new_version The new version.
96 *
97 * @return void
98 */
99 function purge_block_types_cache_on_update( $old_version, $new_version ) {
100 if ( version_compare( '2.5.0', $new_version, '>=' ) && version_compare( $old_version, '2.5.0', '<' ) ) {
101 delete_block_types_cache();
102 }
103 }
104
105 add_action( 'content_control/update_version', __NAMESPACE__ . '\\purge_block_types_cache_on_update', 10, 2 );
106
107 /**
108 * Sanitize expetced block type data.
109 *
110 * @param array<string,string|string[]> $type Block type definition.
111 * @return array<string,mixed> Sanitized definition.
112 */
113 function sanitize_block_type( $type = [] ) {
114 foreach ( $type as $key => $value ) {
115 $type[ $key ] = is_array( $value )
116 ? sanitize_block_type( $value )
117 : \sanitize_text_field( $value );
118 }
119
120 return $type;
121 }
122
123 /**
124 * Update block type list.
125 *
126 * @param array<array{name:string,category:string,description:string,keywords:string[],title:string}> $incoming_block_types Array of updated block type declarations.
127 *
128 * @return void
129 */
130 function update_block_types( $incoming_block_types = [] ) {
131 $block_types = [];
132
133 // Convert to a named index for deduplication.
134 foreach ( get_block_types() as $type ) {
135 $block_types[ $type['name'] ] = $type;
136 }
137
138 // Add or update incoming block types into the array.
139 foreach ( $incoming_block_types as $type ) {
140 // Sanitize each new block type.
141 $block_types[ $type['name'] ] = $type;
142 }
143
144 // Flatten values to a simple array for storage.
145 $block_types = array_values( $block_types );
146
147 \update_option( 'content_control_known_blockTypes', $block_types, false );
148 }
149
150 /**
151 * Get default denial message.
152 *
153 * @return string
154 */
155 function get_default_denial_message() {
156 if ( \ContentControl\get_data_version( 'settings' ) === 1 ) {
157 $settings = get_plugin_option( 'jp_cc_settings', [] );
158
159 return isset( $settings['default_denial_message'] ) ? $settings['default_denial_message'] : '';
160 }
161
162 $default = __( 'This content is restricted.', 'content-control' );
163
164 return get_plugin_option( 'defaultDenialMessage', $default );
165 }
166