When a customer purchases a WP site in WooCommerce that is configured to be sourced from a site template, a series of complex processes kick off in the background. In this help article we aim to provide some color on how we’re handling this within the confines of WPCD’s WordPress technology.
Truth be told, this article is to remind ourselves about what we did! To be blunt, things can get quite hairy! So, lets dive right in.
Template sites reside on a different server. So, when a purchase is made two things have to happen:
This requires two back-to-back long running transactions that is sequenced properly.
For the first step, we use the function in our “Copy To New Server” tab, referred to internally as “site-sync”.
For the second step we use the function in our “Change Domain” tab.
One of the biggest challenges in WPCD (and also one of the benefits) is that a lot of activity occurs on action and filter hooks – some might say we overdo it on our ‘do_action’ calls. They are very very convenient for triggering things and then moving on. But sometimes these hooks can’t quite get the data they need if they are two steps removed from where the data originated. For this feature where we need to run multiple long-running transactions, we absolutely required that the data be able to be passed between multiple independent action hooks in a particular sequence.
The only real way to do that was to use the database to pass the data.
So, in order to handle the sequencing we added the PENDING LOGs table/CPT. (Later we’ll use this CPT to do bulk processing for servers and sites…)
Another thing we had to do was convert the ‘site sync’ and ‘change domain’ functions in the core plugin so that the could be triggered by action hooks instead of just being used directly in the UI via AJAX.
Now, the sequencing depends on a few things:
Thus, when the first long running transaction finishes (copying the template site – aka site-sync), its “I am done” action hook fires. That hook collects data from the PENDING LOGS using the COMMAND NAME as the key into the log. It does it’s thing but before it ends it updates the PENDING LOGS task as completed. It then fires off the second task (changing the domain of the newly copied site) by using a do_action call. That second task then does it’s thing, adds data to the PENDING LOGS table and, when it ends fires up its “I am done” action hook. That hook in turn picks up the data from the PENDING LOGS table, does its thing and marks the task complete.