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 | * A {@link Wordlift_Log_Service} instance. |
||
| 62 | * |
||
| 63 | * @since 3.15.0 |
||
| 64 | * @access private |
||
| 65 | * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
||
| 66 | */ |
||
| 67 | private $log; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Constructor to wire up the necessary actions |
||
| 71 | * |
||
| 72 | * Which hooks the asynchronous postback happens on can be set by the |
||
| 73 | * $auth_level parameter. There are essentially three options: logged in users |
||
| 74 | * only, logged out users only, or both. Set this when you instantiate an |
||
| 75 | * object by using one of the three class constants to do so: |
||
| 76 | * - LOGGED_IN |
||
| 77 | * - LOGGED_OUT |
||
| 78 | * - BOTH |
||
| 79 | * $auth_level defaults to BOTH |
||
| 80 | * |
||
| 81 | * @throws Exception If the class' $action value hasn't been set |
||
| 82 | * |
||
| 83 | * @param int $auth_level The authentication level to use (see above) |
||
| 84 | */ |
||
| 85 | public function __construct( $auth_level = self::BOTH ) { |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Add the shutdown action for launching the real postback if we don't |
||
| 112 | * get an exception thrown by prepare_data(). |
||
| 113 | * |
||
| 114 | * @uses func_get_args() To grab any arguments passed by the action |
||
| 115 | */ |
||
| 116 | public function launch() { |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Launch the request on the WordPress shutdown hook |
||
| 140 | * |
||
| 141 | * On VIP we got into data races due to the postback sometimes completing |
||
| 142 | * faster than the data could propogate to the database server cluster. |
||
| 143 | * This made WordPress get empty data sets from the database without |
||
| 144 | * failing. On their advice, we're moving the actual firing of the async |
||
| 145 | * postback to the shutdown hook. Supposedly that will ensure that the |
||
| 146 | * data at least has time to get into the object cache. |
||
| 147 | * |
||
| 148 | * @uses $_COOKIE To send a cookie header for async postback |
||
| 149 | * @uses apply_filters() |
||
| 150 | * @uses admin_url() |
||
| 151 | * @uses wp_remote_post() |
||
| 152 | */ |
||
| 153 | public function launch_on_shutdown() { |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Verify the postback is valid, then fire any scheduled events. |
||
| 187 | * |
||
| 188 | * @uses $_POST['_nonce'] |
||
| 189 | * @uses is_user_logged_in() |
||
| 190 | * @uses add_filter() |
||
| 191 | * @uses wp_die() |
||
| 192 | */ |
||
| 193 | public function handle_postback() { |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Create a random, one time use token. |
||
| 209 | * |
||
| 210 | * Based entirely on wp_create_nonce() but does not tie the nonce to the |
||
| 211 | * current logged-in user. |
||
| 212 | * |
||
| 213 | * @uses wp_nonce_tick() |
||
| 214 | * @uses wp_hash() |
||
| 215 | * |
||
| 216 | * @return string The one-time use token |
||
| 217 | */ |
||
| 218 | protected function create_async_nonce() { |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Verify that the correct nonce was used within the time limit. |
||
| 227 | * |
||
| 228 | * @uses wp_nonce_tick() |
||
| 229 | * @uses wp_hash() |
||
| 230 | * |
||
| 231 | * @param string $nonce Nonce to be verified |
||
| 232 | * |
||
| 233 | * @return bool Whether the nonce check passed or failed |
||
| 234 | */ |
||
| 235 | protected function verify_async_nonce( $nonce ) { |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Get a nonce action based on the $action property of the class |
||
| 255 | * |
||
| 256 | * @return string The nonce action for the current instance |
||
| 257 | */ |
||
| 258 | protected function get_nonce_action() { |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Prepare any data to be passed to the asynchronous postback |
||
| 270 | * |
||
| 271 | * The array this function receives will be a numerically keyed array from |
||
| 272 | * func_get_args(). It is expected that you will return an associative array |
||
| 273 | * so that the $_POST values used in the asynchronous call will make sense. |
||
| 274 | * |
||
| 275 | * The array you send back may or may not have anything to do with the data |
||
| 276 | * passed into this method. It all depends on the implementation details and |
||
| 277 | * what data is needed in the asynchronous postback. |
||
| 278 | * |
||
| 279 | * Do not set values for 'action' or '_nonce', as those will get overwritten |
||
| 280 | * later in launch(). |
||
| 281 | * |
||
| 282 | * @throws Exception If the postback should not occur for any reason |
||
| 283 | * |
||
| 284 | * @param array $data The raw data received by the launch method |
||
| 285 | * |
||
| 286 | * @return array The prepared data |
||
| 287 | */ |
||
| 288 | abstract protected function prepare_data( $data ); |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Run the do_action function for the asynchronous postback. |
||
| 292 | * |
||
| 293 | * This method needs to fetch and sanitize any and all data from the $_POST |
||
| 294 | * superglobal and provide them to the do_action call. |
||
| 295 | * |
||
| 296 | * The action should be constructed as "wp_async_task_$this->action" |
||
| 297 | */ |
||
| 298 | abstract protected function run_action(); |
||
| 299 | |||
| 300 | } |
||
| 301 | |||
| 304 |
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: