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