Tag: Tutorials

  • Fix for the current branch has no upstream branch error message

    This is one of those git config things you set once and forget years later when you setup a new machine. How did we get here? You can git push expecting to fly up to the remote repository right?

    fatal: The current branch feature-1234 has no upstream branch.
    To push the current branch and set the remote as upstream, use
    
        git push --set-upstream origin feature-1234
    
    To have this happen automatically for branches without a tracking
    upstream, see 'push.autoSetupRemote' in 'git help config'.

    To get rid of this message forever and prevent git from gatekeeping you just run this:

    git config --global push.default current

    This will set your git config to only push your current branch when running git push. Enjoy never having to worry about this again!

  • Learning in the new year

    Learning in the new year

    For much of last year, I kept my Duolingo streak alive ???? There’s no shortage of ways to stay frosty and learn something new in 2023. Here’s some ideas:

    Udemy and Coursera have awesome affordable courses. Codecademy and code schools can teach your programming fundamentals. Learn a new language with Duolingo. Learn from the experts with MasterClass. There’s tons of free basic cooking classes on YouTube. Already know how to cook, but want to level up your chef skills? You can pay for a fully interactive Sur la table course taken over zoom.

    Good luck everyone!

  • Pro-tip: how to quickly change audio input on macOS Monterey

    I love this trick more than any other macOS lifehack because 9 times out of 10 if I am on my MacBook, I am probably wearing headphones. The snag is, my headphones input mic is not great, let me tell ya.

    My favorite noise-cancelling headphones Sony XM4‘s have a small audio problem with Bluetooth. When they’re connected via Bluetooth, good ol macOS wants to use them as both input and output by default, but due to bandwidth problems the headphones switch its audio output to something like mono:

    For headsets and other audio devices, there are multiple relevant profiles: The Headset Profile (HSP) and/or the Hands Free Profile (HFP) are used when you want to use the device in two directions, so to speak – speaker output and microphone input. With HSP or HFP, the available bandwidth gets split between the speaker and the microphone, so the quality of the audio output will be lower. Maybe that’s what you mean by “mono”.

    The audio output can be so poor in fact, that I find it unusable. I’m in this weird position where I love using my headphones, but only if they are used for audio output alone. So, I am constantly switching my input to the System mic instead of my headphones mic ????

    Without further ado, here’s the macOS prot-tip! All you need to do is Option-Click on the volume icon in your menu bar (if you’re currently connected to headphones or some other audio output device, like a TV the icon may appear different):

    1. Hover over the volume icon in your menu bar
    2. ⌥ Click on the volume icon
    3. It should reveal a dropdown with Input and Output devices listed
  • Anders Snylund’s Bulletproof TypeScript useLocalStorage hook

    I’ve found myself using localStorage more and more these days. Turns out there’s a fancy hook for that. Not surprised! But where’s the love for TypeScript?

    Turns out, Anders has done the refactoring for us:

    import { useState } from "react";
    
    export const useLocalStorage = <T>(key: string, initialValue: T): [string, typeof setValue] => {
      const [storedValue, setStoredValue] = useState(() => {
        try {
          const item = window.localStorage.getItem(key)
          return item ? JSON.parse(item) : initialValue
        } catch (error) {
          return initialValue
        }
      });
    
      const setValue = (value: T): void => {
        try {
          const valueToStore = value instanceof Function ? value(storedValue) : value
          setStoredValue(valueToStore)
          window.localStorage.setItem(key, JSON.stringify(valueToStore))
        } catch (error) {
          return error
        }
      };
    
      return [storedValue, setValue]
    }
    
  • FOUC fix for next.js apps

    Next.js is super duper dope. Versatile. Fast. Easy to understand and a dead-simple configuration. What’s not to love? Out-of-the-box, it seems to just work. Pair it with GraphQL and it’s game over. Nothing seems to compare so far in my testing.

    My one problem so far, has been FOUC with styled-components. Truly a horrible issue. However, thanks to this bloke, there’s an easy solution.

    First create a _document.js file in your pages directory. Assuming you are working with styled-components, drop this in there:

    import Document from "next/document";
    import { ServerStyleSheet } from "styled-components";
    export default class MyDocument extends Document {
      static async getInitialProps(ctx) {
        const sheet = new ServerStyleSheet();
        const originalRenderPage = ctx.renderPage;
        try {
          ctx.renderPage = () =>
            originalRenderPage({
              enhanceApp: (App) => (props) =>
                sheet.collectStyles(<App {...props} />),
            });
          const initialProps = await Document.getInitialProps(ctx);
          return {
            ...initialProps,
            styles: (
              <>
                {initialProps.styles}
                {sheet.getStyleElement()}
              </>
            ),
          };
        } finally {
          sheet.seal();
        }
      }
    }

    That’s literally it! Rebuild your app, and serve and he FOUC issue should be resolved!

  • The Headless Stack: WordPress, GraphQL and Gatsby

    For years I’ve been building sites with WordPress. The PHP page templating, media library, and The Loop was the holy trinity for me when I first got started with web development. Later, open-source tools like Trellis helped me speed up my development process by automating server provisioning. Sage, helped by speeding up starter theme development even faster. Now we have new paradigms such as the WP REST API and Gutenberg. We can more easily and more speedily, piece together custom blocks for editors. Every week, WordPress is inching closer and closer to becoming an agnostic CMS.

    Static PHP page generation in WordPress is pretty good out of the box. But these days, everyone and their mom wants to write JSX. So, what’s the solve here? WordPress is deeply intertwined with PHP, and themes are not exactly prepped to work well with React. One could simply start with React and Redux and you’d probably end up with something like this. Not a terrible solution, albeit still a theme-based approach, not exactly performant, and the reducers alone give me a headache. Those would be firing quite frequently. We could be more efficient.

    Enter Gatsby, or if you’re really saavy, you could opt for Next.js. Postlight has probably the most promising starter repo I have ever seen for Next.js. In this post I’ll be going over Gatsby specifically, but I definitely want to explore other avenues as well. The Jamstack, as it has come to be known is here in full-force, and it works really well. So, naturally I want to get my hands on some other approaches.

    To get started, all you just need a Gatsby site, a data store, and a means to deploy it. Some prefer Netlify, others prefer Now. To be honest, they’ll all fairly inexpensive these days, if not free. But hosting your data store will cost you some dough. You don’t have to break the bank though. The cheapest Droplet on DigitalOcean (which is roughly ~$5/month) should more than suffice.

    I setup my WordPress installation here at api.https://stephen.nyc but, Postlight’s approach is interesting and notable, they have their WordPress served out of port 8080. As far as I can tell, you can’t do that with Gatsby. Not a deal breaker for me, but something for you to know. So, for the WordPress + Gatsby stack to work you’ll need to setup your API url under a sub domain (or separate domain I found want) at something like cms.example.com or if you want to follow me, api.example.com works too.

    Next, there’s a lot of configuring you need to do according to their blog post tutorial. Namely, you’ll need to install some plugins on your WordPress site to expose your WordPress Data to GraphQL:

    Those two should suffice for a basic setup. Next, you’re going to have to configure your gatsby-config.js file a little bit in your Gatsby site repo. You can download this gatsby-starter-wordpress-blog and fork it. We just need to designate that API url here:

    module.exports = {
      ...
      plugins: [
        ...,
        {
            resolve: `gatsby-source-wordpress-experimental`,
            options: {
                // the only required plugin option for WordPress is the GraphQL url.
                url: process.env.WPGRAPHQL_URL || `https://api.example.co/graphql`,
            }
        },
      ]
    }

    As you can see here, gatsby-source-wordpress is currently in beta, so it carries the -experimental slug in the Gatsby Plugin name until v4 ships. But at a minimum, this is all you need to get started. From here, you can create pages in src/pages and GraphQL queries to hydrate your views with content.

    I won’t go further than here in terms of setup, because really, the world is your oyster from here. After you fork the starter blog, you just run yarn && gatsby develop, clear up any GraphQL errors, and your local Gatsby site should run at localhost:8000, and your GraphQL playground runs at localhost:8000/___graphql

    I would recommend reading up on how Gatsby routing works, and how to write (and inspect) GraphQL queries. Those two skills will give you the power to craft whatever kind of site you want.

    Now, with Gatsby, it runs all the GraphQL queries at build-time. So, when you deploy to Netlify or Now or wherever, all the pages/posts and content are generated ahead of time. The inverse would be having the client render content at run-time. Dynamic pages are neato, but they are dreadfully slow. With this setup, we can have both! For static content (think like, blog posts or pages), we can use Gatsby’s static build capabilities. But if you need dynamic content (imagine comments, or loading things in real-time), you can leverage something like @apollo/client. With Apollo, you can fetch and run queries at run-time. That’s what I use to power my personal bookmarks!

    Suggested reading:

    I imagine I’ll continue writing more about this headless revolution, so stay tuned. If you have any questions or feedback, leave me a comment below.

  • Curbed Marfa Travel Guide

    Curbed Marfa Travel Guide

    Executive Editor at Vox, Mercedes Kraus, penned a travel guide for would-be visitors to Marfa. Marfa is located in West Texas. Heck, even the Simpsons visited Prada Marfa:

    The Simpsons, S30E11: “Mad about the toy”

    Texas, is well known for many things. A couple of venerable and memorable characters from Texas’s past include Sam Houston and Lyndon B. Johnson. A few of my personal favorite things about Texas: the tall skies, grassy hills, semi-arid desert landscapes, swell thunderstorms, quiet dive bars, and loud honky-tonks. It’s easy to forget that Texas has a substantial art culture in Marfa. But, its there damnit! It has frequently been overshadowed by larger-than-life subjects such as Austin’s tech boom, and of course Texas oil booms.

    You can read the entire travel guide here at Curbed. But, I loved this pro-tip on arting and getting to know locals in Marfa:

    For art: Don’t let anyone tell you to skip Chinati. I recommend either the full tour ($25) or all three self-guided tours ($30). The self-guided are “the sheds” (where I experienced a visual symphony), the Dan Flavin buildings (for your Instagram fulfillment), and the new Robert Irwin—an artwork and experience that is in fact an entire building. The thing that I think you, a fan of this newsletter, would really miss if you don’t do the full tour is the arena. If you are unable to take the Judd Foundation tour (see above), you must do the full Chinati tour so that you can experience the arena. (Pro tip: get to know your docent—locals in Marfa are super friendly, will give you great tips, and might even invite you to a local party or happening.)

    For context, The Chinati Foundation was founded based on Donald Judd’s ideas and principles. Honorary Texan, Donald Judd is essentially Marfa’s Patron Saint of Art. For good reason too. If not for him, Marfa would look a helluva lot different.

    Mercedes is on-point about getting to know your locals too. Don’t be shy. Texas’s state motto is, after all simply, friendship. You might just make a friend. Having a Texan in your contact book is like personally knowing a hobbit. Cherished, magical and kind.

  • Pretty Git Log in One Line

    I use git log a lot. It’s real handy. But you know what — it’s pretty unwieldy and takes up quite a bit of terminal real estate. I finally decided to do something about it. Every time I ran git log, my terminal becomes a total mess. I knew about this little dingle:

    git log --pretty=oneline --abbrev-commit

    Which prints out a friendly, easy-to-digest, single-lined log:

    a2a722b (HEAD -> master, origin/master) Fix the NoteDetail styles.
    4fdcbc7 Split the NoteCell and NoteDetail.
    ccc4dc2 Connected the NoteCell with data.
    bce0d12 Broken components into separate pieces.
    071ea13 Change destinations, now the targeting works.
    0ecea01 NoteDetail and split it from the RootView.
    ...

    Which is great! But… can we do better? Some googling around lead me to this post by Mattias Geniar:

    There are longer versions of that same --pretty parameter. In fact, it allows you to specify all the fields you want in the output.

    Sweet! So, running the same command and flagging the specified fields like so, prints a more concise log. Awesome!

    $ git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
    
    a2a722b (HEAD -> master, origin/master) Fix the NoteDetail styles. (2 days ago) <Stephen Petrey>
    4fdcbc7 Split the NoteCell and NoteDetail. (2 days ago) <Stephen Petrey>
    ccc4dc2 Connected the NoteCell with data. (2 days ago) <Stephen Petrey>
    bce0d12 Broken components into separate pieces. (4 days ago) <Stephen Petrey>
    071ea13 Change destinations, now the targeting works. (5 days ago) <Stephen Petrey>
    0ecea01 NoteDetail and split it from the RootView. (6 days ago) <Stephen Petrey>
    ...

    Mattias has the excellent suggestion of configuring a Git Alias for this:

    $ git config --global alias.logline "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"

    Now, instead of having to type out the command and flag the fields you want to print, you can just type, git logline and wham! It should print out a prettified git log that takes up way less space in your terminal.

  • How to Disable WordPress Emojis

    First off, I didn’t write this. Ryan Hellyer did. I only came across this fix from a post written by Justin Downey. The short-end of it is, drop this block of code into the bottom of your functions.php file. I made some tiny tweaks:

    if (!function_exists('disable_emojis')) {
      function disable_emojis() {
        remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
        remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
        remove_action( 'wp_print_styles', 'print_emoji_styles' );
        remove_action( 'admin_print_styles', 'print_emoji_styles' );
        remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
        remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );
        remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );
      }
      add_action( 'init', 'disable_emojis' );
    }

    I really needed this personally, because MailChimp was parsing my RSS feed verbatim, and for whatever reason, when WordPress added Twemoji to core in 4.2 it forces the emoji markup to be an inline-image — which, I get it. It’s useful for older devices who may not support emoji. But it was completely wrecking my email campaigns styles, yuck! ????

    Thankfully, this function works in disabling the Twemoji images altogether! Tested up to: 5.2.2 ????

  • How to Rename a Branch Locally and Remote in Git

    This took some googling, so I thought I would document my findings here.

    Locally

    If you’re already on the branch you’d like to rename, just run:

    git branch -m new-name

    If you’re on say master, and you have a branch you want to rename now but no checkout, run:

    git branch -m old-name new-name

    Now that you’ve taken care of your local branch, there’s the unfinished business of mending your remote branch with renaming.

    Remote

    If you use Github or Gitlab (or whatever) you’re going to want to remove the old-name branch and push the new-name branch:

    git push origin :old-name new-name

    And finally, push to reset the upstream branch so your local and remote branches are up-to-date:

    git push origin -u new-name
  • How to Move Existing, Uncommitted Work to a New Branch in Git

    So here’s a new one for ya.

    I was working on a branch all day, closed the laptop, went home and fell asleep. Nothing out of the ordinary. The next day, I awoke, and upon returning to work, immediately began working on a new problem.

    The issue arose when I discovered to my horror that I was still on the same branch from yesterday! *Gasp*

    Lest we forget, we have Git at our disposal — so I take a breath and dive in:

    git status

    This will probably return a list of changes not staged like this:

    On branch fix-from-yesterday
    Your branch is up to date with 'origin/fix-from-yesterday-'.

    Changes not staged for commit:
    (use "git add …" to update what will be committed)
    (use "git checkout -- …" to discard changes in working directory)
    modified: project/api-v4.php
    modified: web/assets/js/some-project/package.json
    ...

    Now that we have a pulse on things, we can do this:

    git checkout -b new-branch-of-changes-for-today

    This will leave your current branch as is, create and checkout a new branch and keep all your changes. You can then make a commit with:

    git add <the files you want commit>

    and finally, commit to your new branch with ol trusty:

    git commit -m "Make sure your description is short and sweet"

    And there you have it! According to the git-checkout documentation, -b and -B are interchangeable. Here’s the rub:

    Specifying -b causes a new branch to be created as if git-branch[1] were called and then checked out. In this case you can use the --track or --no-track options, which will be passed to git branch. As a convenience, --track without -b implies branch creation; see the description of --trackbelow.


    If -B is given, <new_branch> is created if it doesn’t exist; otherwise, it is reset. This is the transactional equivalent of

  • The Palm Leaf Grasshopper

    The Palm Leaf Grasshopper

    Posted by: u/rolandroflz

    I thought this was pretty cool!

    This is a palm leaf grasshopper! There’s much discussion in the Reddit thread about the material, origin, and proper Japanese nomenclature. Typical and to be expected of any high-quality Reddit post honestly — however, one user in particular commented a web archive link to something extraordinary.

    An old, MS-painted tutorial on how to make such a grasshopper out of palm leaves:

    A real treasure.

  • Balance Biking

    Balance Biking

    I learned something interesting the other day.

    Learning to ride a bike with training wheels are apparently, a bad idea. Learning to ride a bike with balance bikes, are a better idea. Personally, I like to call it a butt-scooter, but I digress. (Note to reader: I’m not an expert on this whatsoever, links to real experts are below)

    The general hypothesis is, learning to a ride a bike is all about splitting up the problem into parts. You really only have two issues any newcomer has to iron out:

    1. Balancing
    2. Pedaling

    Which really comes down to two different learning models:

    1. Learn to balance on a balance bike (playing with a butt-scooter)
    2. Then one learns to pedal on a real bike (multi-tasking pedaling and balancing)

    Breaking the problem into two steps seems easy… too easy.

    Basically, step one is: trick kids into learning how to balance (it’s a butt-scooter for christ’s sake) on their own before he/she learns how to pedal. A child falling over on a balance is less likely to happen as well. Their feet always hang off the side, ready to catch themselves. There’s no pedals, so you they can only ride as fast as they can waddle with the butt-scooter in hand.

    Balancing on any bike is basically all about inertia and movement. So just leave that up the children to decide what feels comfortable.

    If you throw training-wheels on a bike and hand it off, a child immediately loses the ability to feel the inertia of the bike and more importantly — they lose the ability to correct. Which could be dangerous when it comes time to remove the training wheels. While they’re learning all about pedaling and multi-tasking with training-wheels on, it becomes super apparent why it’s not ideal to take those training wheels off. Because, well… gravity.

    The poor kids that had training wheels have never experienced it before! No wonder kids fall down when you take them off!

    A typical balance bike

    Balance bikes are an absolutely brilliant idea. The additional upshot with these little inventions (apart from saving your kids knees and elbows from certain duress) is kids can begin playing with balance bikes at a much younger age bicycles with training wheels. That’s just my opinion. The example above is very tiny. Designed for children of 2 years aged, at the earliest. Given its construction, it could even be used indoors on carpet. Because, I mean — c’mon lets be honest. Balance bikes are basically just butt scooters for kids.

    Again, I’m not a parent (yet) and nor am I an expert on how to raise kids. But there are seasoned authors out there who are. Such as CyclingUK, which has a great blog post on where to start with balance bikes and when to transition to pedals.

  • How to Delete a Git Commit, Locally and Remote

    Alright, we’ve all been there. You got some merge conflicts. Or there’s a last minute hotfix. You feverishly make a commit. Then, your worst fear is realized only after you’ve pushed it remotely.

    You made a mistake. The horror.

    GIF by Justin - Find & Share on GIPHY

    First, don’t sweat it. You have options. Most of the time, when I do this, I just need to remove the commit altogether. Other times, I need to make revisions, and replace that commit altogether. Tabula rasa amirite?

    Okay so let’s get started. First, I’m making a few assumptions. I’m doing work on a branch separate from master. Let’s say you’re working on Navigation, I would be on a branch called navigation or nav-component or something. It’s good practice to keep your work separate, and branched.

    Alrighty, with that out of the way, let’s run git log on our example navigation branch:

    $ git log --pretty=oneline --abbrev-commit
    32cd83e Made a change with a mistake
    b9u3cc5 Changed again
    105fd3d Updated some content
    5cd7718 Fixed the NavBar
    a1842d4 Initial commit

    Alright so looking over our log here, we can see at the top, that commit 32cd83e is the most recent commit. It’s the problematic commit in question. To edit out commit history we are going to use rebase:

    $ git rebase -i HEAD~2

    Before you run this, let’s break down what we’re doing here. A classic use-case of rebase is when you’re on a branch say, navigation. But, let’s say that it’s been a week since you branched. It’s likely that master has diverged since you created your branch. What divergence means, is that the branch (we’re on) navigation is currently out-of-date and lacks the new history (from the past week) on master. We don’t have to worry about that here per se.

    But, when you work in repos that multiple people contribute to, a branch can be out-of-date very often. If you use master as the center of truth, the organization can use rebase to bring other contributors history into your branch, and therefor, up-to-date with master. Neat huh?

    Anyways, you could say what we’re doing here, is similar. Instead of using rebase to replay work from a branch on top of master, we want to rewrite the commit history of the branch we’re currently on. After running the command above, you should see something like this:

    b9u3cc5 Changed again
    32cd83e Made a change with a mistake
    
    # Rebase 105fd3d..46cd867 onto 105fd3d
    #
    # Commands:
    #  p, pick = use commit
    #  r, reword = use commit, but edit the commit message
    #  e, edit = use commit, but stop for amending
    #  s, squash = use commit, but meld into previous commit
    #  f, fixup = like "squash", but discard this commit's log message
    #  x, exec = run command (the rest of the line) using shell
    #
    # If you remove a line here THAT COMMIT WILL BE LOST.
    # However, if you remove everything, the rebase will be aborted.
    #

    Here, (and admittedly confusingly) the most recent commit, 32cd83e we want to destroy is listed at the bottom instead of the top. You can see pretty clearly what the HEAD~2 argument does. It flags rebase to only show the most recent 2 commits in this interactive rebase wizard. Because, that’s all we need to see for context.

    So, the options we have now is we can remove that line altogether, and the commit will be dropped into oblivion, as if it never happened. If we remove the problematic commit, and exit the rebase wizard, we can now check our history on navigation:

    $ git log --pretty=oneline --abbrev-commit
    b9u3cc5 Changed again
    105fd3d Updated some content
    5cd7718 Fixed the NavBar
    a1842d4 Initial commit

    Look ma! No mistake! 32cd83e is gone! Now that we have this history locally, let’s push it up, remotely:

    $ git push origin +navigation

    The + flag before the branch name signals that this will be a force push. Alternatively you could have written the same command as:

    $ git push -f origin navigation

    I like using this technique, because I enjoy committing everything. Mistakes and all. Because, well for one, why not? Documenting your code history can be helpful sometimes. Besides, ultimately, you can use rebase to squash and fixup your commit history anyways. So, when it comes time to push to remote, I just take a beat and git rebase -i HEAD~N to keep my history lean. I’ve only recently learned the pains of not keeping my git history pretty and concise. So, now I’m a convert, and everyone benefits from a readable git history.

    Further Reading:

  • See the Last 10 Branches You Worked On in Git

    As part of my ongoing effort to become more effective at Git, I stumbled into an interesting problem. At Vimeo, we have a lot of branches named after issues reported in Jira. Some branches take on the name of the project or component or a very specific fix. We also have branches that don’t follow naming-conventions at all. It happens. Anyways, somedays, I forget the name of the branches I’ve worked on, say yesterday. Other times, I forgot what work I did on which branch. I grew tired of tab-switching between my terminal and Jira or Github to go and find my work history. I don’t need to see my commits, I just need to see a list of recent branches.

    So, I began googling around the web, and came across this post. It’s a good solution I think, David Walsh seems to agree too. Basically, it lists the most recent branches you’ve worked on. But ideally, I’d rather opt for an alias in my .bashrc. I don’t want to clutter up my .gitconfig. That’s, just like my opinion man. Without further ado, here’s my alias in my .bashrc:

    # ~/.bashrc
    
    alias recent-branches="git for-each-ref --sort=-committerdate --count=10 --format='%(refname:short)' refs/heads/"

    Stupid simple, readable and short. Just the way I like it. There are of course, more complex solutions to this simple problem. But I’m not a huge fan of those longwinded solutions. Anyways, when I run the recent-branches alias in my terminal:

    ➜  example-repo git:(feature-pages) recent-branches
    nav-bar-component-fix
    GH-48
    features-pages
    FR-47
    XR-73
    VP-71
    new-privacy-policy-page
    VP-70
    VP-49
    GH-46

    Nifty huh? As you can see, the alias command is really just one long git command. It takes a count=10 flag. So if you need to see more branches in your history, just inflate the value accordingly. Enjoy! ✌️

  • PHP Namespaces

    PHP Namespaces

    If you build or customize WordPress themes, coming across Namespaces are going to become more and more commonplace. I think they’re pretty cool. I have never enjoyed having to write (or parse for that matter), custom_functions_with_a_weird_long_name.

    Steve Grunwell has a fantastic primer on Namespaces, and how to use them in WordPress. I also learned about Singletons (and their problems in a OOP environment) and why Namespaces are effective.

    In short, functions are typically defined in plugins a variety of ways. Not long ago, developers might have written a function like this:

    <?php
     
    function get_recent_posts( $limit = 5 ) {
      return new WP_Query( array(
        'post_type'      => 'post',
        'post_status'    => 'publish',
        'orderby'        => 'post_date',
        'order'          => 'desc',
        'posts_per_page' => $limit,
      ) );
    }

    Innocent, naive function writing.

    Function name collisions are were pretty likely to occur, so we got longer and longer functions which led to Class encapsulations, so these functions began to make a mess of your codebase with global variable declarations. Steve goes on:

    From here, developers seem to have split into two factions: the first, dead-set on removing global variables, moved toward the Singleton pattern:

    class MyPlugin() {
     
      protected static $instance;
     
      protected function __construct() {
        // Made protected to prevent `new MyPlugin()` calls.
      }
     
      public function get_instance() {
        if ( ! self::$instance ) {
          self::$instance = new MyPlugin();
        }
     
        return self::$instance;
      }
     
      public function get_recent_posts( $limit = 5 ) {
        return new WP_Query( /* ... */ );
      }
    }
     
    # Usage:
    $plugin       = MyPlugin::get_instance();
    $recent_posts = $plugin->get_recent_posts();

    Yikes, what a pickle… But, behold! The proper (and more maintainable effort) with Namespaces:

    <?php
     
    namespace SteveGrunwell\MyPlugin;
     
    use WP_Query;
     
    function get_recent_posts( $limit = 5 ) {
      return new WP_Query( array(
        'post_type'      => 'post',
        'post_status'    => 'publish',
        'orderby'        => 'post_date',
        'order'          => 'desc',
        'posts_per_page' => $limit,
      ) );
    }

    What a wild and crazy ride! Seriously, give Steve’s post a full read.