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