Quotes are syntactic in bash, so it understands that the outer pair of quotes are surrounding the command substitution and the inner pair are inside it. This will work regardless of any spaces or special characters in $dir.
$(..) is its own context so you just put double quotes around any variables like you normally would:
parent_dir="$(basename "$dir")"
[ and [[ can indeed be tricky. You may find ShellCheck useful, since it recognizes common problems like [ a=b ], [-e .bashrc], [ 1 > 2 ], [ -n var ], [ false ] and several others.
Bear in mind (pointed by the article as well) that variable assignment doesn't allow spaces around the equals sign, and that in this case the outer parentheses are unnecessary anyway.
For example:
How to quote $dir here? What if it contains spaces or other special characters?That's what I dislike about Bash.
Also, always start your scripts with
This prevents script from running after error, without any messages though.Also, I always make mistakes when using [ and [[.