Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 11 | abstract class WP_Async_Task { |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Constant identifier for a task that should be available to logged-in users |
||
| 15 | * |
||
| 16 | * See constructor documentation for more details. |
||
| 17 | */ |
||
| 18 | const LOGGED_IN = 1; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Constant identifier for a task that should be available to logged-out users |
||
| 22 | * |
||
| 23 | * See constructor documentation for more details. |
||
| 24 | */ |
||
| 25 | const LOGGED_OUT = 2; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Constant identifier for a task that should be available to all users regardless of auth status |
||
| 29 | * |
||
| 30 | * See constructor documentation for more details. |
||
| 31 | */ |
||
| 32 | const BOTH = 3; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * This is the argument count for the main action set in the constructor. It |
||
| 36 | * is set to an arbitrarily high value of twenty, but can be overridden if |
||
| 37 | * necessary |
||
| 38 | * |
||
| 39 | * @var int |
||
| 40 | */ |
||
| 41 | protected $argument_count = 20; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Priority to fire intermediate action. |
||
| 45 | * |
||
| 46 | * @var int |
||
| 47 | */ |
||
| 48 | protected $priority = 10; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | protected $action; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var array |
||
| 57 | */ |
||
| 58 | protected $_body_data; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Constructor to wire up the necessary actions |
||
| 62 | * |
||
| 63 | * Which hooks the asynchronous postback happens on can be set by the |
||
| 64 | * $auth_level parameter. There are essentially three options: logged in users |
||
| 65 | * only, logged out users only, or both. Set this when you instantiate an |
||
| 66 | * object by using one of the three class constants to do so: |
||
| 67 | * - LOGGED_IN |
||
| 68 | * - LOGGED_OUT |
||
| 69 | * - BOTH |
||
| 70 | * $auth_level defaults to BOTH |
||
| 71 | * |
||
| 72 | * @throws Exception If the class' $action value hasn't been set |
||
| 73 | * |
||
| 74 | * @param int $auth_level The authentication level to use (see above) |
||
| 75 | */ |
||
| 76 | public function __construct( $auth_level = self::BOTH ) { |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Add the shutdown action for launching the real postback if we don't |
||
| 100 | * get an exception thrown by prepare_data(). |
||
| 101 | * |
||
| 102 | * @uses func_get_args() To grab any arguments passed by the action |
||
| 103 | */ |
||
| 104 | public function launch() { |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Launch the request on the WordPress shutdown hook |
||
| 128 | * |
||
| 129 | * On VIP we got into data races due to the postback sometimes completing |
||
| 130 | * faster than the data could propogate to the database server cluster. |
||
| 131 | * This made WordPress get empty data sets from the database without |
||
| 132 | * failing. On their advice, we're moving the actual firing of the async |
||
| 133 | * postback to the shutdown hook. Supposedly that will ensure that the |
||
| 134 | * data at least has time to get into the object cache. |
||
| 135 | * |
||
| 136 | * @uses $_COOKIE To send a cookie header for async postback |
||
| 137 | * @uses apply_filters() |
||
| 138 | * @uses admin_url() |
||
| 139 | * @uses wp_remote_post() |
||
| 140 | */ |
||
| 141 | public function launch_on_shutdown() { |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Verify the postback is valid, then fire any scheduled events. |
||
| 167 | * |
||
| 168 | * @uses $_POST['_nonce'] |
||
| 169 | * @uses is_user_logged_in() |
||
| 170 | * @uses add_filter() |
||
| 171 | * @uses wp_die() |
||
| 172 | */ |
||
| 173 | public function handle_postback() { |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Create a random, one time use token. |
||
| 189 | * |
||
| 190 | * Based entirely on wp_create_nonce() but does not tie the nonce to the |
||
| 191 | * current logged-in user. |
||
| 192 | * |
||
| 193 | * @uses wp_nonce_tick() |
||
| 194 | * @uses wp_hash() |
||
| 195 | * |
||
| 196 | * @return string The one-time use token |
||
| 197 | */ |
||
| 198 | protected function create_async_nonce() { |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Verify that the correct nonce was used within the time limit. |
||
| 207 | * |
||
| 208 | * @uses wp_nonce_tick() |
||
| 209 | * @uses wp_hash() |
||
| 210 | * |
||
| 211 | * @param string $nonce Nonce to be verified |
||
| 212 | * |
||
| 213 | * @return bool Whether the nonce check passed or failed |
||
| 214 | */ |
||
| 215 | protected function verify_async_nonce( $nonce ) { |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Get a nonce action based on the $action property of the class |
||
| 235 | * |
||
| 236 | * @return string The nonce action for the current instance |
||
| 237 | */ |
||
| 238 | protected function get_nonce_action() { |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Prepare any data to be passed to the asynchronous postback |
||
| 250 | * |
||
| 251 | * The array this function receives will be a numerically keyed array from |
||
| 252 | * func_get_args(). It is expected that you will return an associative array |
||
| 253 | * so that the $_POST values used in the asynchronous call will make sense. |
||
| 254 | * |
||
| 255 | * The array you send back may or may not have anything to do with the data |
||
| 256 | * passed into this method. It all depends on the implementation details and |
||
| 257 | * what data is needed in the asynchronous postback. |
||
| 258 | * |
||
| 259 | * Do not set values for 'action' or '_nonce', as those will get overwritten |
||
| 260 | * later in launch(). |
||
| 261 | * |
||
| 262 | * @throws Exception If the postback should not occur for any reason |
||
| 263 | * |
||
| 264 | * @param array $data The raw data received by the launch method |
||
| 265 | * |
||
| 266 | * @return array The prepared data |
||
| 267 | */ |
||
| 268 | abstract protected function prepare_data( $data ); |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Run the do_action function for the asynchronous postback. |
||
| 272 | * |
||
| 273 | * This method needs to fetch and sanitize any and all data from the $_POST |
||
| 274 | * superglobal and provide them to the do_action call. |
||
| 275 | * |
||
| 276 | * The action should be constructed as "wp_async_task_$this->action" |
||
| 277 | */ |
||
| 278 | abstract protected function run_action(); |
||
| 279 | |||
| 280 | } |
||
| 281 | |||
| 284 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: