The Launch: A React+Redux WordPress theme

I think it’s just about done, so I decided to go ‘live’ with it.

You’re looking at it now. What do you think?

Here’s my first post about this theme…

I’ll probably tweak it over the next weeks, months. But I’m itching to give it a test drive. For now it’s good enough for government work… which means a lot less than it did just a few months ago.

I’lll also write more about the process, and how it works asap.

Find a bug? Let me know.

 

A Weekly Book Project

I’ve taken a step back from following all the podcasts I’d been listening to, for obvious reasons. I have the 3 I listen to, to keep up with the news… And that’s it. At a certain point, reading more news stories will only make me less effective.

In 2015 I set out to read a book a week, and ended up reading 83 books. In order to keep my sanity, I plan to attempt this project again this year.

I’ve already started, and you can follow my progress.

One thing I’d like to do differently this time around, as you might have noticed, is I want to absorb more of what I read. So I started writing a few paragraphs about each book, a takeaway, once I’m done.

I also wouldn’t mind company on this journey. I love discussing ideas, and what I’ve read and am thinking about. You can see from my 2015 list and my most recent additions that it’s an eclectic collection. So if you’d like to suggest a book you’d like to discuss, or know what I’m reading before I’ve finished, let me know and I’d be more than happy to discuss.

For the curious, the secret to accomplishing this, is to plan. I always have several books in my queue that I’m in the middle of, and a few shorter books (Shakespeare plays, etc.) that I can finish in a day or so in case the end of the week surprises me. That way I’m always ready to finish a book each week.

Plugin Concept: Request Post Feedback (with revisions)

I was listening to the Post Status Draft podcast from about a year ago where Brian, the founder of Post Status, interviews Matt Mullenweg.

In their discussion they Brian mentioned the Drafts with Friends plugin and how he’d like to see something like Google Docs collaboration, editing.

That got me thinking about what the MVP, minimum viable product, for that would look like… and I came up with the Request Post Feedback plugin.

Here’s how it works:

Under the content editor there is a new meta box. At the top of the box you can enter an email to generate a new feedback link. This is how Posts With Friends already works.

At the bottom of the meta box you can manage the different links that you already generated and see if someone has already given feedback.

When you send a link to someone they aren’t taken to the admin; rather, they are taken to the front-end of your site. Where the post would normally be, there is an embedded editor with the content of your post ready for their perusal.

When they submit their feedback it is saved as a revision post_type of your post.

What that means is that you can leverage the revisions browser comparison tool already built into WordPress and see what they suggested.

If you like the concept try it out. It’s not yet live on the WordPress plugin repository, because I haven’t decided if I’ll maintain it yet. But I welcome you to download it from the project repo and try it out. Please note: while I made sure that it was solidly built, it’s still an MVP/Proof of concept.

As always please share your comments below.

 

Developing A Theme for WordPress Using ReactJS, Redux, and the WP REST API

To being, check out the project here.

This is a work in progress, but I got enough done this weekend that I’d like to share.

I wanted to work on a project using ReactJS and Redux. I’ve heard a lot about it, and wanted to explore it on my own. I do that with every technology before I bring it to the office. Since WordPress incorporated the WP REST API in the core for version 4.7 I though that that might be a great opportunity to explore both the API using ReactJS+Redux.

There were a number of interesting challenges I faced building this. The idea was to make a theme that would work 100% like any other WordPress theme; that could be installed and work normally.

As it currently stands there are some missing features, which I’ll get to later. But this is past a proof-of-concept and well along the path to be fully operational.

I will explain more about how it all fits together in a future post, maybe when I load it onto this site. In this post I just wanted to muse about the process so far, and invite others to join in the project or share their ideas and implementations.

Endpoints: Handling Pretty Permalinks

The first challenge was to figure out how to handle “Pretty Permalinks.” You know what they are. Back in the early days of CMSs all URLs ran off of GET variables (http://blog.example.com?p=23). But because Google gave better SEO juice to readable URLs we all started getting urls that were readable. This was incorporated into WordPress as well.

The problem with implementing this in a single-page JS app is that the default path for posts is  /year/month/name-of-post/. That can make routing complicated. Not only that, but you can change the permalink format too. So in order for this theme to work, I’d have to handle that.

My solution was to have a catchall route and build a custom endpoint for Pretty Permalinks that would be powered by the url_to_postid() method. This ended up working pretty well and is what powers both single pages and posts.

Endpoints: Menus

Another challenge was how to handle menus. They’re dynamic, and not included in the current API. To solve this I borrowed the main class from the WP API Menus plugin. (I love GPL, don’t you?) It was written by Fulvio Notarstefano, and was beautifully implemented.

I contemplated having the menu data loaded as js variables on page load, since they don’t change. But I wanted the challenge of figuring out how to implement multiple versions of the same element with ReactJS+Redux, so they got their own endpoint.

Endpoints: Featured Images

I also modified the main post endpoint to include featured images so that I wouldn’t have to do a separate call for each image on archive pages.

Search

Another challenge, in general, was handling the different routes for different actions. It’s not all that complicated once you get the hang of it. But working in single page apps can get interesting, and if you’re not careful you can lose the benefits of using frameworks like Redux.

All in all the WP REST API is quite robust and is now polished. If you check out my redux actions you can see that I’m using the same endpoint for the main index archive, single posts and search. Which is really nice. I’ll be expanding this file for the taxonomies as well. Apparently all you need is to add a filter GET variable.

What’s still missing from the project?

There’s still a lot to do. First and foremost, there is currently only minimal styling.

There are a whole lot of things that are needed to make it a theme the might be accepted into the theme repo. First of all, I haven’t done anything with taxonomies yet.

I’d also like to add widgets. That is another interesting problem which I’ll probably solve the same way I did pretty permalinks — add a new endpoint just for them. I’ll have to render them completely on the server, then pass them down via the API. It’s not pretty, but it should work.

I’ll also want to clean up the hooks that run before the page loads. Since the entire theme is running off the API, directly from the index.php, it won’t need a lot of the server-side processing, as that is done already in the API.

The customizer isn’t playing well with the menus, I’ll have to figure that out.

I’m sure there’s a whole bunch more that needs to be done too. I’ll write about what I discover in the summary post once (if) this theme goes live.

Closing thoughts

It’s a fun project and I’ll look forward to playing more with ReactJS and Redux. The first thing I noticed was how snappy it was. It also forces you to think about the different components in a very healthy way. When designing components, each one should be independent. You should be able to drop any one in any other project (for the most part) and use it with minimal wiring up. That’s good coding practices.

Join me!

If you find this project interesting please feel free to share with me your ideas for it.

Thoughts about “Who”

Finished reading “Who” this week. I found it an interesting approach to hiring. I’d read it if I were looking for a job as well to prepare for interviews, though it’s not designed for that.
Takeaways:

  • Take hiring seriously, this should be obvious but it’s easy to let it get lost in all the other work.
  • Manage the task of hiring as your would any other project with a clearly defined role, and process in advance.
  • Don’t hesitate to walk through every aspect of their career with them, ask about anything ambiguous. Everyone has things they sweep under the carpet, so it’s important to understand this when you are evaluating a candidate, but you also want to make sure you don’t miss things you need to know first.

Things I would change: 

  • I found the tone of the book somewhat annoying. It talks itself up a lot. It backs up it’s self-promotion, but there could have been a better way to do it.
  • This makes the process even more intimidating, something that’s already difficult to do.

Is npm not installing properly on Vagrant running on Windows?

Get a Mac! Kidding. (I actually think Microsoft has been more innovative lately.)

Run the flag --no-bin-links when you install anything with npm and it will fix your issue.

I have a dev Vagrant that I share with my team. I tend to mock things up when I’m playing around at home in the evenings. I work on a Mac at home. So when I confidently came into work with the plan to set up a box for my team to work on, I lost a bunch of time trying to figure out why the npm development tools I wanted, would not install.

According to the npm documentation:

The --no-bin-links argument will prevent npm from creating symlinks for any binaries the package might contain.

Why? I found this discussion on the Vagrant github:

…there is a fundamental problem with using Vagrant + VMWare Workstation + Windows + Linux VMs…

It seems that VMWare on Windows simply does not support the ability to create a Linux symlink in a shared folder.

 

Another option is to set bin-links to false in your global npm configurationnpm config set bin-links false 

This will mean that you won’t have to type in the flag --no-bin-links each time, which might be a good solution if you are trying to build development tools to make your team’s lives easier.

Refactoring a WordPress plugin, Object-oriented, a start

I recently dissected  an Object Oriented WordPress plugin boilerplate. This exploration was part of a project I am undertaking to explore best practices for implementing Object Oriented principles in WordPress development.

When it comes to implementing coding principles, I’m generally not a purist — I aim to be utilitarian. One of the dictums I drum into my team is: “Make it work, then make it work well.” Do not take that to mean I’m sloppy, the second half of that statement holds as much weight as the first.

In the spirit of “make it work well”, I decided to refactor my Assets Manager plugin for this exploration, as I have not updated it in a while.

My goal here was not to create a pure Object Oriented plugin implementing all the glorious principles. There is a lot more I can do, and I will discuss some of those ideas as I explain what I did. Mainly, in this refactor, I wanted to accomplish the following:

  1. Implement the Single Responsibility Principle.
  2. Decouple the various components of the plugin from each other.
  3. Lay the groundwork for further implementation of more principles.

Single Responsibility Principle

Of of the banes of every developer is cutting through spaghetti code. I’ve discovered — from refactoring multitudes of lines of code — that the simplest, most effective way to reduce and prevent spaghetti is by applying this principle. Like spaghetti, your code cannot get tangled if it’s too short to tangle up.

Decouple the various components

While recompilation isn’t an issue with PHP/WordPress, there are still benefits to decoupling. First, reusability: if your components work independent of each other you can stash your pieces away for another project. You also don’t have to repeat your code if you need the same functionality elsewhere. Don’t Repeat Yourself!!!

But perhaps, more important than reusability is stability. The more interdependent components are in your project, the more fragile your project is overall. If you change one thing, the more other components depend on that piece, the more chance you have that you are breaking one of those other components.

This is the whole reason for the Open/Closed Principle as well as the Interface Segregation Principle. Aside from SOLID, though, this is also the reason why WordPress is riddled through and through with hooks. WordPress hooks allow for developers to change the functionality of the core, or other people’s plugins, without having to change the core, or foreign plugin, code.

Taking Stock

If you look at the previous version you’ll see that the extent of Object Oriented implementation is an encapsulation of the code, and that’s about it.

This isn’t a bad practice per-se. If you are going to keep to best practices of compatibility standards with WordPress, you cannot use php namespaces in your plugins. Namespaces were introduced in PHP 5.3 and, as of writing this, WordPress still supports as far back as PHP 5.2.4.

A first step you can take when implementing Objects in your WordPress plugins is to wrap your code in a class. If you do this, you do not have to worry about the names of your functions conflicting with the names of other functions in other plugins. This approach is called the “God Class,” as it creates one class that does it all. It’s not all that sophisticated, nor is it really Object Oriented either.

On this topic, once you start adding classes to your plugins, there is a fascinating and thorough breakdown of the different ways to instantiate a WordPress plugin as a class on the WordPress Stack Exchange community.

Following is a breakdown of the files in the new-and-improved plugin:

wp-asssets-manger.php

This should be renamed to match the directory (assets-manager), but I’m not sure whether the plugin upgrade process will work if I do this. Because this file is not named as the plugin directory, it makes generating test suites more complicated, but it’s also not a big deal.

The main job of this class is to instantiate all the other pieces of the plugin; I may implement an autoloader here at some point. While this class is dependent on ALL the other classes, since the only thing it does is instantiate all the other classes, it still is a fairly stable class. It doesn’t care about the intricacies of the other classes it uses. As long as the function names stay the same, you will only have to change this class if you need to add another component.

The other minor job of this class is to ensure that the rewrite rules are flushed when the post type is being created. Yes, this class is supposed to do one thing, but other, more pure ways of doing this would be convoluted — creating then deleting options in the database. I thought that that was silly to do simply to keep with a guideline, so I opted for this small infraction.

inc/Asset_Post_Type.php

When classes do one thing, they tend to be boring, and that’s a good thing.

I have an empty constructor here because the logical alternative would be to put into it  the hooks call. That would make tests more complicated, so I didn’t do that.

inc/Asset.php

I tried to include in here all the specific logic about how each individual asset works: the metadata, when are users allowed to access an asset, the asset link, etc. I did this to pull it away from the god class. The next step will be to pull out the specific restrictions as components onto themselves.

Currently, they all hook into pre_asset_serve which is a hook I put into the class that serves the asset. Each restriction should independently hook into the asset class.

Additionally, the UI is still intertwined with Admin.php, so if you were to add features to the asset, you would need to change that, as well as the JS file. I think this is a step in the right direction, perhaps next iteration will include this decoupling.

inc/Serve_Attachment.php

This does exactly what it says, nothing else. It hooks into WordPress before headers are sent, if the request is an Asset, it serves that request.

This was built with the Open closed principle in mind, open for extension, closed for modification. One of the most powerful features of WordPress are the hooks; which, are the embodiment of the Open closed principle.

Every WordPress developer learns at one point or another early in their career that you do not change the core, or anything that may be updated for that matter.

How is this learned?

Well, they make a change, to the core, or a plugin, or theme. Then, at some point in the future they forget that they had made that change and they update whatever it is they made the change to.

And everything breaks.

Well, maybe not everything; but whatever they were trying to accomplish with their modification is now lost.

Hooks are peppered throughout the core, well developed themes, and plugins. They allow other developers to hook into the code and tell it to do something without changing the code. This way when an update comes out, they can update without losing those changes.

Serve_Attachment.php has the hook do_action( 'pre_asset_serve', $this->attachment_id ); before the asset is served. I eat my own dog food here and hook into that action from Check_Asset_Restrictions.php to see if that asset should actually be served.

inc/Check_Asset_Restrictions.php

This is the other side of my implementation of the Open Closed principle, discussed in the previous file.

This class hooks into pre_asset_serve and runs all the checks it needs to. This class needs to have a certain amount of knowledge about how Asset.php works, so it’s not ideal. This is another example of how it’s a work in progress.

The fun thing about this is it runs entirely on hooks. It hooks into pre_asset_serve and it has a filter itself through which others can change the no_serve_message.

inc/Admin.php

This class is responsible for the admin display of the assets on the post type page.

As I wrote above. Ideally, each feature of the asset — expires, require login etc. –should be a separate component hooking in here, and where the file is served. Perhaps I’ll do that in the next iteration. Doing that would coincide better with the Interface segregation principle.

As-is, it think this approach is better than it was before. The admin display

inc/Update_Assets.php

This is the biggest violation of the Single Responsibility principle. On the one hand it’s responsible for the AJAX requests. On the other hand it’s responsible for ALL the AJAX requests. It wouldn’t be difficult to break up. Next version.

inc/Log_Assets_Access.php

Completely self-contained, hooks directly into pre_serve_asset, does (almost) one thing.

I put create_log_table() into here, because it is a single function that is needed to make it work. It’s static so it can be accessed on plugin activation without instantiating the whole class.

js/asset-manager.js

This could benefit from the implementation of a JavaScript framework. I’m considering BackboneJS for the next iteration of the plugin. I’d use that library since it’s already included in the WordPress core.

My goal for this file was twofold:

  • To implement wp.media.
    Back when I first built this, wp.media didn’t exist. I implemented  Plupload as a jQuery plugin in my original version. By using wp.media here I’m making the plugin “future proof.” If ever WordPress moves away from Plupload, they would implement whatever alternative via wp.media.
  • To break apart big functions.
    There was a huge spaghetti mess here before. Now that each “thing” is its own function I can iterate further in the next version.

Conclusion

This isn’t a finished product, nor is it the best I can do. But it’s a good step in the right direction. I learned a lot exploring what other people are doing with their plugins, and it was fun to flex my refactoring muscles to do this project. I hope you enjoyed, I did!

If you have any ideas about what steps you would want to see taken next, feel free to comment below, or use the contact form on the contact page.

Image credit

Technological Evolution, Microsoft vs Apple

I’m writing this from my iPad Pro with the Smart Keyboard. It’s a beautiful experience. It’s also a natural evolution of where technology has been going.

This past week Microsoft has displayed that it understands this. Apple has not.

Writing here is not a perfect experience: I still think multitasking on iOS could benefit by some improvements. But I enjoy being able to reach up, touch the screen and do things. When I switch back to my MacBook Pro, I’ll invariably find myself reaching up to the screen to manipulate something.

There have been several innovations along the path of technological evolution that have sparked exponential adoption and further innovation.

“Evolutionary processes creates a capability and uses that capability to bring on the next stage.”

– Ray Kurzweil, TED (Minute 5:21)

There is a rabbinical tradition that on the sixth day, before sundown, God created the first pair of tongs–giving the world’s first blacksmith the necessary tool with which to make more. But it’s more likely that man iterated on his tools, building up to some sort of tong. This is how technology develops.

Similarly, with interfaces, we build off previous iterations of interfaces towards more efficient tools. Punch cards led to keyboards; command line interfaces led to GUIs; mice led to touchscreen.

“Progress of technical science is characterized by the fact: first, that more and more energy is utilized for human purposes, and secondly, that the transformation of the raw energies into useful forms of energy is attended by ever-increasing efficiency.”

– William Ostwald, Efficiency

Our tools progress towards being more efficient. That is what defines technological progress. Whatever issues that are lacking in touch interface are not because of it being less ideal an interface than command line (no, VIM is not better)–it is because of the implementation of the tool. Newtons were horrific, but iPhones are a beautiful experience.

Another concept Kurzweil talks about is exponential growth. Not only did RNA lead to the Cambrian period, it led to the Cambrian explosion. Not only that, but as far as we know, all cellular life has DNA, an evolutionary step past RNA. When a biological process is surpassed, it tends to be left behind. This is also true of technology. Our trains are not steam-powered anymore, nor do we use vacuum tubes in our computers.

iOS was originally forked off of OSX. It was done so because OSX was a solid platform, but somewhat over-bloated for the limited resources of the original iPhones. There were speculations about if/when they would be re-merged.

Microsoft has already done so with Windows. While I’m not a big Windows fan, I will admit that that was the right play and the proof of that is this past week’s tech announcements from both companies.

I’ve been an Apple fanboy for a very long time, it pains me to say this.

It is doubtful whether a contextual touchscreen on a keyboard provides added efficiency in your workflow–if it does, it is linear. This past year Apple has not demonstrated exponential growth, their iterations have been little more than linear, or catching up to parallel technologies. Waterproofing a watch, putting an accelerometer into a TV remote, adding zoom to a camera. These all add to the experience, but other tech is surpassing these technologies.

Microsoft, on the other hand, has been developing entirely new interfaces. Kinect tracks your movement, creating a whole new way to interact with games. Hololense will bring AR to a place where it can be useful.

The Surface Studio looks beautiful. They are embracing touch interface as the next step in the evolution of interfaces. It’s more than a linear step along the way–I don’t know if it’s exponential, but it shames a slightly augmented keyboard. It combines the power of a desktop with the interactive interface of a tablet. The result looks like a better experience than the sum of its parts. This is technological progress.

Cover image 

How to Globally Ignore files from git

TLDR;

  1. nano ~/.gitignore
  2. git config --global core.excludesfile '~/.gitignore'

Background

Git is all the rage, why wouldn’t it be? You can save every iteration of your work; it’s the persistent undo button for developers. Seriously, if you’re not using it, start now. Find a tutorial, stop everything you’re doing and get on that. It will make your work 1000% more efficient.</intro>

One issue I came across with my workflow is that my Mac, like everyone else’s, places a .DS_Store file in every directory. There is absolutely no situation in which I could possibly my .DS_Store files to be included in my repo.

The Solution?

Global .gitignore files.

If you’re new to git, a .gitignore file (don’t forget that preceding period) is placed in your git repo. When you are committing git will ignore all the files in that directory, and deeper, that are listed in the .gitignore file.

If you’ve never used it before, here’s how .gitignore works. Typically you would add directories that hold dependencies that can easily be fetched again and take up space.

You can also set a global .gitignore for the files you know  you will never want to include in any repo (like the .DS_Store files).

The first line in the TLDR above creates the file that will be used as the global .gitignore in a good location for such a file. Typically config files for a specific user are placed in that user’s home directory. ~/ is a shortcut to that location.

The second line sets the .gitignore file you just created to be used in the global config. This way you never have to add the file specified there to any local .gitignore files in the future.

Warning

Just like with all global config settings, make sure you only put there things you’ll never want to include in any project whatsoever. Down the line you might wish you handn’t set a config file when you’re pulling your hair out trying to figure out why file X or Y isn’t getting included in your project.

Source, image

E7 – We hold these truths to be self-evident

On August 28, 1963 Dr. Martin Luther King Jr. gave one of the most powerful speeches of all time.

I know I cannot do this speech justice. But to be honest, I’m not doing any of these speeches justice.

I’m studying and I’m practicing.

I’m studying history, and what makes a great speech and I’m practicing my oratory. I’m doing so with the words of masters in my mouth, and I’m grateful for the opportunity to study these great words.

So please forgive me for butchering some of the most moving and eloquent words spoken…

Why this speech?

First and foremost because, as Dr. King stated, my destiny is tied up with their destiny and my freedom is inextricably bound to their freedom.

I hope that one day it will seem confounding that this speech needed to be said, because it’s message is obvious and should be obvious to everyone.

But sadly today, this speech still rings true today, and it needs to be read and reread, and listened to not for it’s stunning poetry, but for it’s message.