Skip to content

Raise coverage #301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 75 additions & 55 deletions includes/MslsAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,16 @@ public static function init() {
* @param string $capability
*
* @since 2.0
*
*/
$caps = apply_filters( 'msls_admin_caps', 'manage_options' );
if ( current_user_can( $caps ) ) {
$title = __( 'Multisite Language Switcher', 'multisite-language-switcher' );
add_options_page( $title, $title, 'manage_options', $obj->get_menu_slug(), [ $obj, 'render' ] );
add_options_page( $title, $title, 'manage_options', $obj->get_menu_slug(), array( $obj, 'render' ) );

add_action( 'admin_init', [ $obj, 'register' ] );
add_action( 'admin_notices', [ $obj, 'has_problems' ] );
add_action( 'admin_init', array( $obj, 'register' ) );
add_action( 'admin_notices', array( $obj, 'has_problems' ) );

add_filter( 'msls_admin_validate', [ $obj, 'set_blog_language' ] );
add_filter( 'msls_admin_validate', array( $obj, 'set_blog_language' ) );
}
}

Expand Down Expand Up @@ -88,32 +87,41 @@ public function get_options_page_link() {
* You can use every method of the decorated object
*
* @param string $method
* @param mixed $args
* @param mixed $args
*
* @return mixed
*/
public function __call( $method, $args ) {
$parts = explode( '_', $method, 2 );

if ( is_array( $parts ) && 'rewrite' === $parts[0] ) {
return $this->render_rewrite( $parts[1] );
$this->render_rewrite( $parts[1] );
return;
}

$checkboxes = [
'activate_autocomplete' => __( 'Activate experimental autocomplete inputs',
'multisite-language-switcher' ),
'activate_content_import' => __( 'Activate the content import functionality',
'multisite-language-switcher' ),
$checkboxes = array(
'activate_autocomplete' => __(
'Activate experimental autocomplete inputs',
'multisite-language-switcher'
),
'activate_content_import' => __(
'Activate the content import functionality',
'multisite-language-switcher'
),
'sort_by_description' => __( 'Sort languages by description', 'multisite-language-switcher' ),
'exclude_current_blog' => __( 'Exclude this blog from output', 'multisite-language-switcher' ),
'only_with_translation' => __( 'Show only links with a translation', 'multisite-language-switcher' ),
'output_current_blog' => __( 'Display link to the current language', 'multisite-language-switcher' ),
'content_filter' => __( 'Add hint for available translations', 'multisite-language-switcher' ),
];
);

if ( isset ( $checkboxes[ $method ] ) ) {
echo ( new Group() )->add( new Checkbox( $method, $this->options->$method ) )->add( new Label( $method,
$checkboxes[ $method ] ) )->render();
if ( isset( $checkboxes[ $method ] ) ) {
echo ( new Group() )->add( new Checkbox( $method, $this->options->$method ) )->add(
new Label(
$method,
$checkboxes[ $method ]
)
)->render();
} else {
$value = ! empty( $this->options->$method ) ? $this->options->$method : '';
echo ( new Text( $method, $value ) )->render();
Expand All @@ -122,21 +130,26 @@ public function __call( $method, $args ) {

/**
* There is something wrong? Here comes the message...
*
* @return bool
*/
public function has_problems() {
public function has_problems(): bool {
$message = '';

if ( $this->options->is_empty() ) {
$message = sprintf(
__( 'Multisite Language Switcher is almost ready. You must <a href="%s">complete the configuration process</a>.',
'multisite-language-switcher' ),
__(
'Multisite Language Switcher is almost ready. You must <a href="%s">complete the configuration process</a>.',
'multisite-language-switcher'
),
esc_url( admin_url( $this->get_options_page_link() ) )
);
} elseif ( 1 == count( $this->options->get_available_languages() ) ) {
$message = sprintf(
__( 'There are no language files installed. You can <a href="%s">manually install some language files</a> or you could use a <a href="%s">plugin</a> to download these files automatically.',
'multisite-language-switcher' ),
__(
'There are no language files installed. You can <a href="%1$s">manually install some language files</a> or you could use a <a href="%2$s">plugin</a> to download these files automatically.',
'multisite-language-switcher'
),
esc_url( 'http://codex.wordpress.org/Installing_WordPress_in_Your_Language#Manually_Installing_Language_Files' ),
esc_url( 'http://wordpress.org/plugins/wp-native-dashboard/' )
);
Expand All @@ -148,43 +161,50 @@ public function has_problems() {
/**
* Render the options-page
*/
public function render() {
public function render(): void {
printf(
'<div class="wrap"><div class="icon32" id="icon-options-general"><br/></div><h1>%s</h1>%s<br class="clear"/><form action="options.php" method="post"><p>%s</p>',
__( 'Multisite Language Switcher Options', 'multisite-language-switcher' ),
$this->subsubsub(),
__( 'To achieve maximum flexibility, you have to configure each blog separately.',
'multisite-language-switcher' )
__(
'To achieve maximum flexibility, you have to configure each blog separately.',
'multisite-language-switcher'
)
);

settings_fields( 'msls' );
do_settings_sections( __CLASS__ );

printf(
'<p class="submit"><input name="Submit" type="submit" class="button button-primary" value="%s" /></p></form></div>',
( $this->options->is_empty() ? __( 'Configure', 'multisite-language-switcher' ) : __( 'Update',
'multisite-language-switcher' ) )
( $this->options->is_empty() ? __( 'Configure', 'multisite-language-switcher' ) : __(
'Update',
'multisite-language-switcher'
) )
);
}


/**
* Create a submenu which contains links to all blogs of the current user
*
* @return string
*/
public function subsubsub() {
$icon_type = $this->options->get_icon_type();

$arr = [];
$arr = array();
foreach ( $this->collection->get_plugin_active_blogs() as $blog ) {
$admin_url = get_admin_url( $blog->userblog_id, $this->get_options_page_link() );
$current = $blog->userblog_id == $this->collection->get_current_blog_id() ? ' class="current"' : '';

$arr[] = sprintf( '<a href="%1$s"%2$s>%3$s</a>', $admin_url, $current, $blog->get_title( $icon_type ) );
}

return empty( $arr ) ? '' : sprintf( '<ul class="subsubsub"><li>%s</li></ul>',
implode( ' | </li><li>', $arr ) );
return empty( $arr ) ? '' : sprintf(
'<ul class="subsubsub"><li>%s</li></ul>',
implode( ' | </li><li>', $arr )
);
}

/**
Expand All @@ -193,21 +213,21 @@ public function subsubsub() {
* @codeCoverageIgnore
*/
public function register() {
register_setting( 'msls', 'msls', [ $this, 'validate' ] );
register_setting( 'msls', 'msls', array( $this, 'validate' ) );

$sections = [
$sections = array(
'language_section' => __( 'Language Settings', 'multisite-language-switcher' ),
'main_section' => __( 'Main Settings', 'multisite-language-switcher' ),
'advanced_section' => __( 'Advanced Settings', 'multisite-language-switcher' ),
];
);

global $wp_rewrite;
if ( $wp_rewrite->using_permalinks() ) {
$sections['rewrites_section'] = __( 'Rewrites Settings', 'multisite-language-switcher' );
}

foreach ( $sections as $id => $title ) {
add_settings_section( $id, $title, [ $this, $id ], __CLASS__ );
add_settings_section( $id, $title, array( $this, $id ), __CLASS__ );
}

/**
Expand All @@ -216,7 +236,6 @@ public function register() {
* @param string $page
*
* @since 1.0
*
*/
do_action( 'msls_admin_register', __CLASS__ );
}
Expand All @@ -229,7 +248,7 @@ public function register() {
* @return int
*/
public function language_section(): int {
$map = [ 'blog_language' => __( 'Blog Language', 'multisite-language-switcher' ) ];
$map = array( 'blog_language' => __( 'Blog Language', 'multisite-language-switcher' ) );

return $this->add_settings_fields( $map, 'language_section' );
}
Expand All @@ -242,7 +261,7 @@ public function language_section(): int {
* @return int
*/
public function main_section(): int {
$map = [
$map = array(
'display' => __( 'Display', 'multisite-language-switcher' ),
'admin_display' => __( 'Admin Display', 'multisite-language-switcher' ),
'sort_by_description' => __( 'Sort languages', 'multisite-language-switcher' ),
Expand All @@ -255,7 +274,7 @@ public function main_section(): int {
'after_item' => __( 'Text/HTML after each item', 'multisite-language-switcher' ),
'content_filter' => __( 'Available translations hint', 'multisite-language-switcher' ),
'content_priority' => __( 'Hint priority', 'multisite-language-switcher' ),
];
);

return $this->add_settings_fields( $map, 'main_section' );
}
Expand All @@ -268,13 +287,13 @@ public function main_section(): int {
* @return int
*/
public function advanced_section(): int {
$map = [
$map = array(
'activate_autocomplete' => __( 'Autocomplete', 'multisite-language-switcher' ),
'image_url' => __( 'Custom URL for flag-images', 'multisite-language-switcher' ),
'reference_user' => __( 'Reference user', 'multisite-language-switcher' ),
'exclude_current_blog' => __( 'Exclude blog', 'multisite-language-switcher' ),
'activate_content_import' => __( 'Content import', 'multisite-language-switcher' ),
];
);

return $this->add_settings_fields( $map, 'advanced_section' );
}
Expand All @@ -287,23 +306,23 @@ public function advanced_section(): int {
* @return int
*/
public function rewrites_section(): int {
$map = [];
foreach ( get_post_types( [ 'public' => true ], 'objects' ) as $key => $object ) {
$map["rewrite_{$key}"] = sprintf( __( '%s Slug', 'multisite-language-switcher' ), $object->label );
$map = array();
foreach ( get_post_types( array( 'public' => true ), 'objects' ) as $key => $object ) {
$map[ "rewrite_{$key}" ] = sprintf( __( '%s Slug', 'multisite-language-switcher' ), $object->label );
}

return $this->add_settings_fields( $map, 'rewrites_section' );
}

/**
* @param array $map
* @param array $map
* @param string $section
*
* @return int
*/
protected function add_settings_fields( array $map, string $section ): int {
foreach ( $map as $id => $title ) {
add_settings_field( $id, $title, [ $this, $id ], __CLASS__, $section, [ 'label_for' => $id ] );
add_settings_field( $id, $title, array( $this, $id ), __CLASS__, $section, array( 'label_for' => $id ) );
}

/**
Expand All @@ -313,7 +332,6 @@ protected function add_settings_fields( array $map, string $section ): int {
* @param string $section
*
* @since 2.4.4
*
*/
do_action( "msls_admin_{$section}", __CLASS__, $section );

Expand Down Expand Up @@ -341,30 +359,34 @@ public function display() {
* Shows the select-form-field 'admin_display'
*/
public function admin_display() {
echo ( new Select( 'admin_display',
echo ( new Select(
'admin_display',
array(
'flag' => __( 'Flag', 'multisite-language-switcher' ),
'label' => __( 'Label', 'multisite-language-switcher' )
'label' => __( 'Label', 'multisite-language-switcher' ),
),
$this->options->admin_display ) )->render();
$this->options->admin_display
) )->render();
}

/**
* Shows the select-form-field 'reference_user'
*/
public function reference_user() {
$users = [];
$users = array();

foreach ( ( array ) apply_filters( 'msls_reference_users', $this->collection->get_users() ) as $user ) {
foreach ( (array) apply_filters( 'msls_reference_users', $this->collection->get_users() ) as $user ) {
$users[ $user->ID ] = $user->user_nicename;
}

if ( count( $users ) > self::MAX_REFERENCE_USERS ) {
$users = array_slice( $users, 0, self::MAX_REFERENCE_USERS, true );

$message = sprintf(
__( 'Multisite Language Switcher: Collection for reference user has been truncated because it exceeded the maximum of %s users. Please, use the hook "msls_reference_users" to filter the result before!',
'multisite-language-switcher' ),
__(
'Multisite Language Switcher: Collection for reference user has been truncated because it exceeded the maximum of %s users. Please, use the hook "msls_reference_users" to filter the result before!',
'multisite-language-switcher'
),
self::MAX_REFERENCE_USERS
);
trigger_error( $message );
Expand All @@ -390,7 +412,7 @@ public function description() {
* for the output
*/
public function content_priority() {
$temp = array_merge( range( 1, 10 ), [ 20, 50, 100 ] );
$temp = array_merge( range( 1, 10 ), array( 20, 50, 100 ) );
$arr = array_combine( $temp, $temp );
$selected = empty( $this->options->content_priority ) ? 10 : $this->options->content_priority;

Expand Down Expand Up @@ -429,7 +451,6 @@ public function validate( array $arr ) {
* @param array $arr
*
* @since 1.0
*
*/
$arr = (array) apply_filters( 'msls_admin_validate', $arr );

Expand Down Expand Up @@ -457,5 +478,4 @@ public function set_blog_language( array $arr ) {

return $arr;
}

}
Loading
Loading