| 1 |
<?php |
| 2 |
/** |
| 3 |
* Settings functions. |
| 4 |
* |
| 5 |
* @package FaustWP |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WPE\FaustWP\Settings; |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* Determine if redirects are enabled. |
| 16 |
* |
| 17 |
* @return bool True if redirects are enabled, false if else. |
| 18 |
*/ |
| 19 |
function is_redirects_enabled() { |
| 20 |
return '1' === faustwp_get_setting( 'enable_redirects' ); |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Determine if rewrites are enabled. |
| 25 |
* |
| 26 |
* @return bool True if rewrites are enabled, false if else. |
| 27 |
*/ |
| 28 |
function is_rewrites_enabled() { |
| 29 |
return '1' === faustwp_get_setting( 'enable_rewrites' ); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Determine if themes are disabled. |
| 34 |
* |
| 35 |
* @return bool True if themes are disabled, false if else. |
| 36 |
*/ |
| 37 |
function is_themes_disabled() { |
| 38 |
return '1' === faustwp_get_setting( 'disable_theme' ); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Determine if sourcing images from WP domain is enabled. |
| 43 |
* |
| 44 |
* @return bool True if image sources from WP are enabled, false if else. |
| 45 |
*/ |
| 46 |
function is_image_source_replacement_enabled() { |
| 47 |
return '1' === faustwp_get_setting( 'enable_image_source' ); |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
/** |
| 52 |
* Get the secret key setting. |
| 53 |
* |
| 54 |
* @return string The secret key. |
| 55 |
*/ |
| 56 |
function get_secret_key() { |
| 57 |
return faustwp_get_setting( 'secret_key', '' ); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Get a Faust setting by name. |
| 62 |
* |
| 63 |
* @param string $name The setting name. |
| 64 |
* @param mixed $default Optional setting value. Default false. |
| 65 |
* |
| 66 |
* @return mixed The setting value. |
| 67 |
*/ |
| 68 |
function faustwp_get_setting( $name, $default = false ) { |
| 69 |
$value = $default; |
| 70 |
$settings = faustwp_get_settings(); |
| 71 |
|
| 72 |
if ( isset( $settings[ $name ] ) ) { |
| 73 |
$value = $settings[ $name ]; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Filter 'faustwp_get_setting'. |
| 78 |
* |
| 79 |
* @param mixed $value The setting value. |
| 80 |
* @param string $name The setting name. |
| 81 |
* @param mixed $default Optional setting value. |
| 82 |
*/ |
| 83 |
return apply_filters( 'faustwp_get_setting', $value, $name, $default ); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Update a Faust setting value. |
| 88 |
* |
| 89 |
* @link https://developer.wordpress.org/reference/functions/update_option/ |
| 90 |
* |
| 91 |
* @param string $name The setting name. |
| 92 |
* @param mixed $value The setting value. |
| 93 |
* |
| 94 |
* @return void |
| 95 |
*/ |
| 96 |
function faustwp_update_setting( $name, $value ) { |
| 97 |
$settings = faustwp_get_settings(); |
| 98 |
$settings[ $name ] = $value; |
| 99 |
|
| 100 |
update_option( 'faustwp_settings', $settings ); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Get all Faust settings. |
| 105 |
* |
| 106 |
* @return array An array of settings. |
| 107 |
*/ |
| 108 |
function faustwp_get_settings() { |
| 109 |
$settings = get_option( 'faustwp_settings', array() ); |
| 110 |
|
| 111 |
/** |
| 112 |
* Filter 'faustwp_get_settings'. |
| 113 |
* |
| 114 |
* @param array $settings Array of plugin settings. |
| 115 |
*/ |
| 116 |
return apply_filters( 'faustwp_get_settings', $settings ); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Applies the default settings to a site if settings don't already exist. |
| 121 |
* |
| 122 |
* @return void |
| 123 |
*/ |
| 124 |
function maybe_set_default_settings() { |
| 125 |
$secret_key = get_secret_key(); |
| 126 |
$settings = faustwp_get_settings(); |
| 127 |
|
| 128 |
if ( empty( $settings ) ) { |
| 129 |
faustwp_update_setting( 'disable_theme', '1' ); |
| 130 |
faustwp_update_setting( 'enable_rewrites', '1' ); |
| 131 |
faustwp_update_setting( 'enable_redirects', '1' ); |
| 132 |
|
| 133 |
// Force WP to regenerate rewrite rules without calling flush_rewrite_rules which breaks |
| 134 |
// things when used inside of `switch_to_blog()`. |
| 135 |
if ( is_multisite() ) { |
| 136 |
delete_option( 'rewrite_rules' ); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
if ( ! $secret_key ) { |
| 141 |
faustwp_update_setting( 'secret_key', wp_generate_uuid4() ); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Get the contents of an SVG icon. |
| 147 |
* |
| 148 |
* @param string $icon Filename of the SVG without the .svg extension. |
| 149 |
* @return string The SVG icon contents or empty string if the icon file does not exist. |
| 150 |
*/ |
| 151 |
function get_icon( $icon ) { |
| 152 |
$path = __DIR__ . '/assets/icons/' . $icon . '.svg'; |
| 153 |
|
| 154 |
if ( file_exists( $path ) ) { |
| 155 |
return file_get_contents( $path ); //phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
| 156 |
} |
| 157 |
|
| 158 |
return ''; |
| 159 |
} |
| 160 |
|