| 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 |
| 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 $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 |
| 75 |
*/ |
| 76 |
function get_block_types() { |
| 77 |
return \get_option( 'content_control_known_blockTypes', [] ); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Sanitize expeced block type data. |
| 82 |
* |
| 83 |
* @param array $type Block type definition. |
| 84 |
* @return array Sanitized definition. |
| 85 |
*/ |
| 86 |
function sanitize_block_type( $type = [] ) { |
| 87 |
foreach ( $type as $key => $value ) { |
| 88 |
$type[ $key ] = is_array( $value ) |
| 89 |
? sanitize_block_type( $value ) |
| 90 |
: \sanitize_text_field( $value ); |
| 91 |
} |
| 92 |
|
| 93 |
return $type; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Update block type list. |
| 98 |
* |
| 99 |
* @param array $incoming_block_types Array of updated block type declarations. |
| 100 |
*/ |
| 101 |
function update_block_types( $incoming_block_types = [] ) { |
| 102 |
$block_types = []; |
| 103 |
|
| 104 |
// Convert to a named index for deduplication. |
| 105 |
foreach ( get_block_types() as $type ) { |
| 106 |
$block_types[ $type['name'] ] = $type; |
| 107 |
} |
| 108 |
|
| 109 |
// Add or update incoming block types into the array. |
| 110 |
foreach ( $incoming_block_types as $type ) { |
| 111 |
// Sanitize each new block type. |
| 112 |
$block_types[ $type['name'] ] = $type; |
| 113 |
} |
| 114 |
|
| 115 |
// Flatten values to a simple array for storage. |
| 116 |
$block_types = array_values( $block_types ); |
| 117 |
|
| 118 |
\update_option( 'content_control_known_blockTypes', $block_types ); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Get default denial message. |
| 123 |
* |
| 124 |
* @return string |
| 125 |
*/ |
| 126 |
function get_default_denial_message() { |
| 127 |
if ( \ContentControl\get_data_version( 'settings' ) === 1 ) { |
| 128 |
$settings = get_plugin_option( 'jp_cc_settings', [] ); |
| 129 |
|
| 130 |
return isset( $settings['default_denial_message'] ) ? $settings['default_denial_message'] : ''; |
| 131 |
} |
| 132 |
|
| 133 |
return get_plugin_option( 'defaultDenialMessage', '' ); |
| 134 |
} |
| 135 |
|