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