From a2258135dcd542e0b53c45b30a50f60146f4febe Mon Sep 17 00:00:00 2001 From: Mike van den Hoek Date: Fri, 12 Jan 2024 14:33:55 +0100 Subject: [PATCH] (feat): implement setting for configuring a fileserver URL --- CHANGELOG.md | 4 ++ config/core.php | 11 ++-- config/metaboxes.php | 2 +- config/settings_pages.php | 28 +++++++++ openwoo.php | 4 +- src/OpenWOO/Settings/Settings.php | 30 +++++++++ .../Settings/SettingsServiceProvider.php | 62 +++++++++++++++++++ 7 files changed, 133 insertions(+), 8 deletions(-) create mode 100644 config/settings_pages.php create mode 100644 src/OpenWOO/Settings/Settings.php create mode 100644 src/OpenWOO/Settings/SettingsServiceProvider.php diff --git a/CHANGELOG.md b/CHANGELOG.md index c5db112..95061e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # CHANGELOG +## Version 4.2 + +- Feat: implement setting for configuring a fileserver URL + ## Version 4.1.5 - Feat: add publication date to the api response diff --git a/config/core.php b/config/core.php index 5114b2e..46d8a52 100644 --- a/config/core.php +++ b/config/core.php @@ -7,7 +7,7 @@ /** * Service Providers. */ - 'providers' => [ + 'providers' => [ /** * Global providers. */ @@ -15,14 +15,15 @@ Yard\OpenWOO\ElasticPress\ElasticPressServiceProvider::class, Yard\OpenWOO\Metabox\MetaboxServiceProvider::class, Yard\OpenWOO\Taxonomy\TaxonomyServiceProvider::class, - + Yard\OpenWOO\Settings\SettingsServiceProvider::class, + /** * Providers specific to the admin. */ 'admin' => [ - class_exists('\OWC\OpenPub\Base\Settings\SettingsPageOptions') ? Yard\OpenWOO\Admin\AdminServiceProvider::class : Yard\OpenWOO\Foundation\NullServiceProvider::class + class_exists('\OWC\OpenPub\Base\Settings\SettingsPageOptions') ? Yard\OpenWOO\Admin\AdminServiceProvider::class : Yard\OpenWOO\Foundation\NullServiceProvider::class, ], - 'cli' => [ + 'cli' => [ ], - ] + ], ]; diff --git a/config/metaboxes.php b/config/metaboxes.php index 1854663..9aebe33 100644 --- a/config/metaboxes.php +++ b/config/metaboxes.php @@ -274,7 +274,7 @@ [ 'name' => __('URL Bijlage', OWO_LANGUAGE_DOMAIN), 'id' => 'woo_URL_Bijlage', - 'type' => 'text', + 'type' => 'text_url', ], [ 'name' => __('Bijlage', OWO_LANGUAGE_DOMAIN), diff --git a/config/settings_pages.php b/config/settings_pages.php new file mode 100644 index 0000000..18d1df4 --- /dev/null +++ b/config/settings_pages.php @@ -0,0 +1,28 @@ + [ + 'id' => '_owc_openwoo_base_settings', + 'title' => __('OpenWOO instellingen', 'openwoo'), + 'object_types' => ['options-page'], + 'option_key' => '_owc_openwoo_base_settings', + 'tab_group' => 'openwoo-base', + 'tab_title' => __('Algemeen', 'openwoo'), + 'position' => 30, + 'icon_url' => 'dashicons-admin-settings', + 'fields' => [ + 'fileserver_title' => [ + 'name' => __('Fileserver', 'openwoo'), + 'desc' => __('Een eigen implementatie in een plug-in of thema is vereist voor het gebruik van een fileserver, deze plug-in biedt deze instellingen pagina aan waarmee je instellingen kunt gebruiken op elke gewenste plek.', 'openwoo'), + 'id' => 'openwoo_setting_fileserver_title', + 'type' => 'title', + ], + 'fileserver_url' => [ + 'name' => __('Fileserver URL', 'openwoo'), + 'desc' => __('URL met http(s)://', 'openwoo'), + 'id' => 'openwoo_setting_file_server_url', + 'type' => 'text', + ], + ], + ], +]; diff --git a/openwoo.php b/openwoo.php index d778c43..1e2e5b7 100644 --- a/openwoo.php +++ b/openwoo.php @@ -6,7 +6,7 @@ * Plugin Name: Yard | OpenWOO * Plugin URI: https://www.yard.nl/ * Description: Adds OpenWOO implementation - * Version: 4.1.5 + * Version: 4.2 * Author: Yard | Digital Agency * Author URI: https://www.yard.nl/ * License: GPL-3.0 @@ -30,7 +30,7 @@ define('OWO_LANGUAGE_DOMAIN', OWO_SLUG); define('OWO_DIR', basename(__DIR__)); define('OWO_ROOT_PATH', __DIR__); -define('OWO_VERSION', '4.1.5'); +define('OWO_VERSION', '4.2'); /** * Manual loaded file: the autoloader. diff --git a/src/OpenWOO/Settings/Settings.php b/src/OpenWOO/Settings/Settings.php new file mode 100644 index 0000000..511e180 --- /dev/null +++ b/src/OpenWOO/Settings/Settings.php @@ -0,0 +1,30 @@ +settings = $settings; + } + + public function getFileServerURL(): string + { + return $this->settings['_owc_openwoo_setting_file_server_url'] ?? ''; + } + + public static function make(): self + { + $defaultSettings = [ + '_owc_openwoo_setting_file_server_url' => '', + ]; + + return new static(wp_parse_args(\get_option('_owc_openwoo_base_settings'), $defaultSettings)); + } +} diff --git a/src/OpenWOO/Settings/SettingsServiceProvider.php b/src/OpenWOO/Settings/SettingsServiceProvider.php new file mode 100644 index 0000000..26867c2 --- /dev/null +++ b/src/OpenWOO/Settings/SettingsServiceProvider.php @@ -0,0 +1,62 @@ +plugin->loader->addAction('cmb2_admin_init', $this, 'registerSettingsPages', 10, 0); + } + + public function registerSettingsPages(): void + { + $settingsPages = $this->plugin->config->get('settings_pages'); + + if (! is_array($settingsPages)) { + return; + } + + foreach ($settingsPages as $page) { + if (! is_array($page)) { + continue; + } + + $this->registerSettingsPage($page); + } + } + + protected function registerSettingsPage(array $page): void + { + $fields = $page['fields'] ?? []; + unset($page['fields']); // Fields will be added later on. + + $optionsPage = \new_cmb2_box($page); + + if (empty($fields) || ! is_array($fields)) { + return; + } + + $this->registerSettingsPageFields($optionsPage, $fields); + } + + protected function registerSettingsPageFields(CMB2 $optionsPage, array $fields) + { + foreach ($fields as $field) { + if (! is_array($field)) { + continue; + } + + if (isset($field['id'])) { + $field['id'] = self::PREFIX . $field['id']; + } + + $optionsPage->add_field($field); + } + } +}