Skip to content

Commit

Permalink
WIP fix CS issues
Browse files Browse the repository at this point in the history
  • Loading branch information
carstingaxion committed Feb 27, 2024
1 parent 775375f commit 5ecfcad
Show file tree
Hide file tree
Showing 8 changed files with 226 additions and 242 deletions.
95 changes: 63 additions & 32 deletions inc/impressum/namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ function bootstrap(): void {
add_action( 'plugins_loaded', __NAMESPACE__ . '\\load_plugin', 4 );
}


function load_plugin() {
/**
* Load the plugin itself and its modifications.
*
* @return void
*/
function load_plugin(): void {

require_once FT_VENDOR_DIR . PLUGINPATH; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingCustomConstant

Expand All @@ -43,41 +47,56 @@ function load_plugin() {
return;
}

add_action( 'impressum_country_after_sort', __NAMESPACE__ . '\\impressum_country_after_sort' );
add_action( 'impressum_legal_entity_after_sort', __NAMESPACE__ . '\\impressum_legal_entity_after_sort' );
add_filter( 'impressum_country_after_sort', __NAMESPACE__ . '\\impressum_country_after_sort' );
add_filter( 'impressum_legal_entity_after_sort', __NAMESPACE__ . '\\impressum_legal_entity_after_sort' );

add_action( 'admin_head-settings_page_impressum', __NAMESPACE__ . '\\cleanup_admin_ui' );
}


function filter_options() {
/**
* Handle options
*
* @return void
*/
function filter_options(): void {

$_options = [
'dismissed-impressum_welcome_notice' => true,
// OPTION => [] // DO NOT HANDLE, as it's the user-data
// OPTION => [] // DO NOT HANDLE, as it's the user-data!
];

// gets added to the 'OptionsCollection'
// from within itself on creation
new Options\Factory(
$_options,
'Figuren_Theater\Options\Option',
BASENAME,
);
// Gets added to the 'OptionsCollection'
// from within itself on creation.
new Options\Factory(
$_options,
'Figuren_Theater\Options\Option',
BASENAME,
);
}


// set Impressum-location, ft_geo-catgeories and WP_LANG
// in one go, by just .... updating an option
//
function pre_update_ft_geo_option_from_imprint( mixed $new_value, mixed $old_value, string $option_name ): mixed {
/**
* Set Impressum-location, ft_geo-catgeories and WP_LANG in one go, by just .... updating an option.
*
* Filters a specific option before its value is (maybe) serialized and updated.
*
* The dynamic portion of the hook name, `$option`, refers to the option name.
*
* @see https://developer.wordpress.org/reference/hooks/pre_update_option_option/
*
* @param array<string, string> $new_value The new, unserialized option value.
* @param array<string, string> $old_value The old option value.
* @param string $option_name Name of the option in the DB.
*
* @return array<string, string> The new, updated option value ready for being saved to the database.
*/
function pre_update_ft_geo_option_from_imprint( array $new_value, array $old_value, string $option_name ): array {

// do nothing,
// if nothing (on the address) has changed
// $new_value['country'] could be unset by Figuren_Theater\Onboarding\Sites\Installation\set_imprint_page()
// so check it
// Do nothing, if nothing (on the address) has changed.
// Do check '$new_value['country']', which could be unset by Figuren_Theater\Onboarding\Sites\Installation\set_imprint_page().
if ( isset( $new_value['country'] ) &&
// can be bool, if non existent yet
// Can be bool, if non existent yet.
isset( $old_value['country'] ) &&
$old_value['country'] === $new_value['country']
&&
Expand All @@ -86,41 +105,44 @@ function pre_update_ft_geo_option_from_imprint( mixed $new_value, mixed $old_val
return $new_value;
}

// otherwise
// start the geo-engines :)
// Start the geo-engines :) !
$ft_geo_options_bridge = new Geo\ft_geo_options_bridge();

Check failure on line 109 in inc/impressum/namespace.php

View workflow job for this annotation

GitHub Actions / call-workflow-build-test-measure / Static Analysis: PHP

Instantiated class Figuren_Theater\inc\Geo\ft_geo_options_bridge not found.
$ft_geo = $ft_geo_options_bridge->update_option_ft_geo( $old_value, $new_value, $option_name );

Check failure on line 110 in inc/impressum/namespace.php

View workflow job for this annotation

GitHub Actions / call-workflow-build-test-measure / Static Analysis: PHP

Call to method update_option_ft_geo() on an unknown class Figuren_Theater\inc\Geo\ft_geo_options_bridge.

// set adress to verified version
// Set adress to verified version.
if ( isset( $ft_geo['address'] ) && ! empty( $ft_geo['address'] ) ) {
$new_value['address'] = $ft_geo['address'];
} else {
// or reset
// Or reset.
$new_value['address'] = $old_value['address'];
}
// set country to verified version
// Set country to verified version.
if ( isset( $ft_geo['geojson']['properties']['address']['country'] ) ) {
// crazy country codes by 'Impressum' plugin
// Crazy country codes by 'Impressum' plugin.
$_country_helper = [
'de' => 'deu',
'at' => 'aut',
'ch' => 'che',
];
$new_value['country'] = $_country_helper[ $ft_geo['geojson']['properties']['address']['country_code'] ];
} else {
// or reset
// Or reset.
$new_value['country'] = $old_value['country'];
}

// go on and save
// Go on and save.
return $new_value;
}


/**
* Reduce countries to relevant ones.
*
* Filter the countries after localized alphabetical sorting.
*
* @param array $countries The current countries
* @param array<string, string> $countries The current countries.
*
* @return array<string, string> $countries The filtered countries.
*/
function impressum_country_after_sort( array $countries ): array {
return [
Expand All @@ -132,9 +154,13 @@ function impressum_country_after_sort( array $countries ): array {


/**
* Reduce legal entities to relevant ones.
*
* Filter the legal entities after localized alphabetical sorting.
*
* @param array $countries The current countries
* @param array<string, string> $legal_entities The current legal entities.
*
* @return array<string, string> The filtered legal entities.
*/
function impressum_legal_entity_after_sort( array $legal_entities ): array {
return [
Expand All @@ -144,6 +170,11 @@ function impressum_legal_entity_after_sort( array $legal_entities ): array {
}


/**
* Reduce up-sell bloat & clutter from admin settings-page.
*
* @return void
*/
function cleanup_admin_ui(): void {
echo '<style>
.settings_page_impressum .nav-tab-wrapper,
Expand Down
4 changes: 3 additions & 1 deletion inc/namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@

/**
* Register module.
*
* @return void
*/
function register() {
function register(): void {

$default_settings = [
'enabled' => true, // Needs to be set.
Expand Down
78 changes: 36 additions & 42 deletions inc/preferred-languages/namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@
namespace Figuren_Theater\Onboarding\Preferred_Languages;

use Figuren_Theater\Onboarding\Impressum;

use FT_VENDOR_DIR;

use Figuren_Theater; // FT

use Figuren_Theater;
use function add_action;
use function update_option;

Expand All @@ -29,6 +26,7 @@ function bootstrap(): void {
add_action( 'plugins_loaded', __NAMESPACE__ . '\\load_plugin', 4 );
}


/**
* Conditionally load the plugin itself and its modifications.
*
Expand All @@ -39,47 +37,43 @@ function load_plugin(): void {
require_once FT_VENDOR_DIR . PLUGINPATH; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingCustomConstant

// Fires after the value of 'impressum_imprint_options' has been successfully updated.
//
// update_option_ is not triggered reliably
// so switch to pre_update_option_
add_action( 'pre_update_option_' . Impressum\OPTION, __NAMESPACE__ . '\\set_pref_lang_from_impressum', 100, 2 );
// Because 'update_option_' is not triggered reliably, so switch to 'pre_update_option_'.
add_filter( 'pre_update_option_' . Impressum\OPTION, __NAMESPACE__ . '\\set_pref_lang_from_impressum', 100, 2 );
}


/**
* 'ft_geo' was successfully changed,
* so lets change 'WPLANG' accordingly.
*
* Fires after the value of a specific option has been successfully updated.
* Filters a specific option before its value is (maybe) serialized and updated.
*
* The dynamic portion of the hook name, `$option`, refers to the option name.
*
* @since 2.0.1
* @since 4.4.0 The `$option` parameter was added.
* @see https://developer.wordpress.org/reference/hooks/pre_update_option_option/
*
* @param mixed $old_o The old option value.
* @param mixed $o The new option value.
* @param string $option Option name.
* @param array<string, string> $new_value The new option value.
* @param array<string, string> $old_value The old option value.
*
* @return array<string, string>
*/
// function set_pref_lang_from_impressum( $old_o, $o ) {
function set_pref_lang_from_impressum( $o, $old_o ): array {

// do nothing,
// if nothing (on the address) has changed
// $o['country'] could be unset by Figuren_Theater\Onboarding\Sites\Installation\set_imprint_page()
// so check it
if ( isset( $o['country'] ) &&
// can be bool, if non existent yet
isset( $old_o['country'] ) &&
$old_o['country'] === $o['country'] &&
$old_o['address'] === $o['address']
function set_pref_lang_from_impressum( array $new_value, array $old_value ): array {

// Do nothing, if nothing (on the address) has changed.
// $o['country'] could be unset by Figuren_Theater\Onboarding\Sites\Installation\set_imprint_page(),
// so check it.
if ( isset( $new_value['country'] ) &&
// Can be bool, if non existent yet.
isset( $old_value['country'] ) &&
$old_value['country'] === $new_value['country'] &&
$old_value['address'] === $new_value['address']
) {
return $o;
return $new_value;
}

// change order of default translations,
// based on sites' country
switch ( $o['country'] ) {
// Change order of default translations,
// based on sites' country.
switch ( $new_value['country'] ) {

case 'che':
$_informal_defaults = [
Expand All @@ -91,8 +85,8 @@ function set_pref_lang_from_impressum( $o, $old_o ): array {
$_formal_defaults = [
'de_CH',
'de_DE_formal',
'de_CH_informal', // fallback, better than default en_US
'de_DE', // fallback, better than default en_US
'de_CH_informal', // Fallback, better than default en_US.
'de_DE', // Fallback, better than default en_US.
];
break;

Expand All @@ -106,15 +100,15 @@ function set_pref_lang_from_impressum( $o, $old_o ): array {
$_formal_defaults = [
'de_DE_formal',
'de_CH',
'de_AT', // fallback, better than default en_US
'de_DE', // fallback, better than default en_US
'de_AT', // Fallback, better than default en_US.
'de_DE', // Fallback, better than default en_US.
];
break;

case 'deu':
default:
// this is the informal-default
// the typical use-case for theater-people
// This is the informal-default,
// the typical use-case for theater-people.
$_informal_defaults = [
'de_DE',
'de_CH_informal',
Expand All @@ -125,21 +119,21 @@ function set_pref_lang_from_impressum( $o, $old_o ): array {
$_formal_defaults = [
'de_DE_formal',
'de_CH',
'de_DE', // fallback, better than default en_US
'de_DE', // Fallback, better than default en_US.
];
break;
}

// get current ft_site
// Get current 'ft_site'.
$ft_site = Figuren_Theater\FT::site();

Check failure on line 128 in inc/preferred-languages/namespace.php

View workflow job for this annotation

GitHub Actions / call-workflow-build-test-measure / Static Analysis: PHP

Call to static method site() on an unknown class Figuren_Theater\FT.
$use_formal = (bool) $ft_site->has_feature( [ 'in-foermlicher-sprache' ] );

// switch formal and informal translations, based on choosen feature
// Switch formal and informal translations, based on choosen feature.
$_defaults = ( $use_formal ) ? $_formal_defaults : $_informal_defaults;

update_option( 'preferred_languages', join( ',', $_defaults ), 'yes' );
update_option( 'preferred_languages', join( ',', $_defaults ), 'yes' );

update_option( 'WPLANG', $_defaults[0], 'yes' );
update_option( 'WPLANG', $_defaults[0], 'yes' );

return $o;
return $new_value;
}
Loading

0 comments on commit 5ecfcad

Please sign in to comment.