From a3c9d3c47787f32f0b4dd41219eb69b098cc0d62 Mon Sep 17 00:00:00 2001 From: AL Date: Mon, 30 Sep 2019 11:28:53 +0200 Subject: [PATCH] Moar --- README.md | 2 +- sleek-settings.php | 166 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 167 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7d15e6e..2c5074d 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ # Sleek Settings -TODO +Adds an options page to the admin with a few default fields like Google Maps API Key. Also provides a simple API to add more settings to the options page, and get them through `Sleek\Settings\add_setting($name, $args)` and `Sleek\Settings\get_setting($name)`. diff --git a/sleek-settings.php b/sleek-settings.php index ab16067..23e5563 100644 --- a/sleek-settings.php +++ b/sleek-settings.php @@ -1,2 +1,168 @@ false, + 'type' => 'text' + ], $args); + $label = $args['label'] ?? __(ucfirst(str_replace('_', ' ', $name)), 'sleek'); + + add_settings_field(SLEEK_SETTINGS_NAME . '_' . $name, $label, function () use ($name, $args) { + $options = get_option(SLEEK_SETTINGS_NAME); + + if ($args['type'] == 'textarea') { + echo ''; + } + else { + echo ''; + } + }, SLEEK_SETTINGS_SECTION_NAME, SLEEK_SETTINGS_SECTION_NAME); +} + +#################### +# Get settings field +function get_setting ($name) { + $options = get_option(SLEEK_SETTINGS_NAME); + + return $options[$name] ?? null; +} + +################ +# Add admin page +add_action('admin_menu', function () { + add_options_page(SLEEK_SETTINGS_TITLE, 'Sleek', 'manage_options', SLEEK_SETTINGS_PAGE_URL, function () { + ?> +
+

+
+ + + +
+
+ __('Google Maps API Key', 'sleek'), + 'type' => 'text' + ]); + add_setting('google_search_api_key', [ + 'label' => __('Google Search API Key', 'sleek'), + 'type' => 'text' + ]); + add_setting('google_search_engine_id', [ + 'label' => __('Google Search Engine ID', 'sleek'), + 'type' => 'text' + ]); + add_setting('head_code', [ + 'label' => esc_html__('Code inside ', 'sleek'), + 'type' => 'textarea' + ]); + add_setting('foot_code', [ + 'label' => esc_html__('Code just before ', 'sleek'), + 'type' => 'textarea' + ]); + add_setting('cookie_consent', [ + 'label' => esc_html__('Cookie consent text', 'sleek'), + 'type' => 'textarea' + ]); + add_setting('site_notice', [ + 'label' => esc_html__('Site notice', 'sleek'), + 'type' => 'textarea' + ]); +}); + +######## +# Header +add_action('wp_head', function () { + # Custom head code + if ($code = get_setting('head_code')) { + echo $code; + } + + # Cookie consent text + if ($consent = get_setting('cookie_consent')) { + $cookieConsent = $consent; + } + else { + $cookieUrl = get_option('wp_page_for_privacy_policy') ? get_permalink(get_option('wp_page_for_privacy_policy')) : 'https://cookiesandyou.com/'; + $cookieConsent = apply_filters('sleek_cookie_consent', sprintf(__('We use cookies to bring you the best possible experience when browsing our site. Read more | Accept', 'sleek'), $cookieUrl), $cookieUrl); + } + + echo ''; +}); + +######## +# Footer +add_action('wp_footer', function () { + # Custom foot code + if ($code = get_setting('foot_code')) { + echo $code; + } + + # Google Maps + if ($key = get_setting('google_maps_api_key')) { + echo ""; + } +}); + +############################ +# Include google maps JS api +add_action('wp_enqueue_scripts', function () { + if ($key = get_setting('google_maps_api_key')) { + wp_register_script('google_maps', 'https://maps.googleapis.com/maps/api/js?key=' . $key . '&callback=gmAsyncInit', [], null, true); + wp_enqueue_script('google_maps'); + } +}); + +################################ +# Add Google Maps API Key to ACF +add_action('init', function () { + if ($key = get_setting('google_maps_api_key')) { + add_filter('acf/fields/google_map/api', function ($api) use ($options) { + $api['key'] = $key; + + return $api; + }); + } +});