I'm wondering if there is another way to refactor my conditionals if/else statements. I feel I'm repeating the same thing over and over,
Here is a snippet (Keep in mind is wayyyy longer than that, but it follows the same principle) I could do a switch statement but it does not reduce the total code quantity.
I just want to get a second opinion of the best approach for this code to go into production. Also important to mention that the statements that I'm comparing $screen->id; are most likely to be dynamically generated if the user selects the checkbox, but that is outside of the scope of the question.
//check admin screen
$screen = get_current_screen();
if ( $screen->id === 'topic') {
$in['content_css'] = get_template_directory_uri() . "/build/styles/tiny-mce-editor.css";
$in['block_formats'] = $topics_blocks;
return $in;
}
elseif ( $screen->id === 'provider-jobs') {
$in['content_css'] = get_template_directory_uri() . "/build/styles/tiny-mce-editor.css";
$in['block_formats'] = $providers_blocks;
return $in;
}
//for all the page options
else {
$in['block_formats'] = $global_blocks;
return $in;
}
}
Thanks! Any guidance is appreciated.
UPDATE! Here is the refactored code, a little bit more cleaner! And complete so the whole context shows.
//check admin screen
$screen = get_current_screen();
//global ones
$in['block_formats'] = $global_blocks;
$in['toolbar1'] = $global_toolbar;
if ( $screen->id === 'topic') {
$in['block_formats'] = $topics_blocks;
$in['toolbar1'] = $topics_toolbar;
} elseif ( $screen->id === 'forum') {
$in['block_formats'] = $forums_blocks;
$in['toolbar1'] = $forums_toolbar;
} elseif ( $screen->id === 'post') {
$in['block_formats'] = $blogs_blocks;
$in['toolbar1'] = $blogs_toolbar;
} elseif ( $screen->id === 'jobs') {
$in['block_formats'] = $jobs_blocks;
$in['toolbar1'] = $jobs_toolbar;
}
elseif ( $screen->id === 'provider-jobs') {
$in['block_formats'] = $providers_blocks;
$in['toolbar1'] = $providers_toolbar;
}
return $in;
elseis pretty redundant, you can go with just theifandelseifand then let the code just drop through to the$in['block_formats'] = $global_blocks; return $in;