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 |
||
29 | class EED_Batch extends EED_Module |
||
30 | { |
||
31 | |||
32 | /** |
||
33 | * Possibly value for $_REQUEST[ 'batch' ]. Indicates to run a job that |
||
34 | * processes data only |
||
35 | */ |
||
36 | const batch_job = 'job'; |
||
37 | /** |
||
38 | * Possibly value for $_REQUEST[ 'batch' ]. Indicates to run a job that |
||
39 | * produces a file for download |
||
40 | */ |
||
41 | const batch_file_job = 'file'; |
||
42 | /** |
||
43 | * Possibly value for $_REQUEST[ 'batch' ]. Indicates this request is NOT |
||
44 | * for a batch job. It's the same as not providing the $_REQUEST[ 'batch' ] |
||
45 | * at all |
||
46 | */ |
||
47 | const batch_not_job = 'none'; |
||
48 | |||
49 | /** |
||
50 | * |
||
51 | * @var string 'file', or 'job', or false to indicate its not a batch request at all |
||
52 | */ |
||
53 | protected $_batch_request_type = null; |
||
54 | |||
55 | /** |
||
56 | * Because we want to use the response in both the localized JS and in the body |
||
57 | * we need to make this response available between method calls |
||
58 | * |
||
59 | * @var \EventEspressoBatchRequest\Helpers\JobStepResponse |
||
60 | */ |
||
61 | protected $_job_step_response = null; |
||
62 | |||
63 | /** |
||
64 | * @var LoaderInterface |
||
65 | */ |
||
66 | protected $loader; |
||
67 | |||
68 | /** |
||
69 | * Gets the batch instance |
||
70 | * |
||
71 | * @return EED_Batch |
||
72 | */ |
||
73 | public static function instance() |
||
77 | |||
78 | /** |
||
79 | * Sets hooks to enable batch jobs on the frontend. Disabled by default |
||
80 | * because it's an attack vector and there are currently no implementations |
||
81 | */ |
||
82 | public static function set_hooks() |
||
91 | |||
92 | /** |
||
93 | * Initializes some hooks for the admin in order to run batch jobs |
||
94 | */ |
||
95 | public static function set_hooks_admin() |
||
106 | |||
107 | /** |
||
108 | * @since 4.9.80.p |
||
109 | * @return LoaderInterface |
||
110 | * @throws InvalidArgumentException |
||
111 | * @throws InvalidDataTypeException |
||
112 | * @throws InvalidInterfaceException |
||
113 | */ |
||
114 | protected function getLoader() |
||
121 | |||
122 | /** |
||
123 | * Enqueues batch scripts on the frontend or admin, and creates a job |
||
124 | */ |
||
125 | public function enqueue_scripts() |
||
147 | |||
148 | /** |
||
149 | * Create a batch job, enqueues a script to run it, and localizes some data for it |
||
150 | */ |
||
151 | public function enqueue_scripts_styles_batch_create() |
||
170 | |||
171 | /** |
||
172 | * Creates a batch job which will download a file, enqueues a script to run the job, and localizes some data for it |
||
173 | */ |
||
174 | public function enqueue_scripts_styles_batch_file_create() |
||
199 | |||
200 | /** |
||
201 | * Enqueues scripts and styles common to any batch job, and creates |
||
202 | * a job from the request data, and stores the response in the |
||
203 | * $this->_job_step_response property |
||
204 | * |
||
205 | * @return \EventEspressoBatchRequest\Helpers\JobStepResponse |
||
206 | */ |
||
207 | protected function _enqueue_batch_job_scripts_and_styles_and_start_job() |
||
246 | |||
247 | /** |
||
248 | * If we are doing a frontend batch job, this makes it so WP shows our template's HTML |
||
249 | * |
||
250 | * @param string $template |
||
251 | * @return string |
||
252 | */ |
||
253 | public function override_template($template) |
||
260 | |||
261 | /** |
||
262 | * Adds an admin page which doesn't appear in the admin menu |
||
263 | */ |
||
264 | public function register_admin_pages() |
||
275 | |||
276 | /** |
||
277 | * Renders the admin page, after most of the work was already done during enqueuing scripts |
||
278 | * of creating the job and localizing some data |
||
279 | */ |
||
280 | public function show_admin_page() |
||
287 | |||
288 | /** |
||
289 | * Receives ajax calls for continuing a job |
||
290 | */ |
||
291 | View Code Duplication | public function batch_continue() |
|
298 | |||
299 | /** |
||
300 | * Receives the ajax call to cleanup a job |
||
301 | * |
||
302 | * @return type |
||
303 | */ |
||
304 | View Code Duplication | public function batch_cleanup() |
|
311 | |||
312 | |||
313 | /** |
||
314 | * Returns a json response |
||
315 | * |
||
316 | * @param array $data The data we want to send echo via in the JSON response's "data" element |
||
317 | * |
||
318 | * The returned json object is created from an array in the following format: |
||
319 | * array( |
||
320 | * 'notices' => '', // - contains any EE_Error formatted notices |
||
321 | * 'data' => array() //this can be any key/value pairs that a method returns for later json parsing by the js. |
||
322 | * We're also going to include the template args with every package (so js can pick out any specific template |
||
323 | * args that might be included in here) |
||
324 | * 'isEEajax' => true,//indicates this is a response from EE |
||
325 | * ) |
||
326 | */ |
||
327 | protected function _return_json($data) |
||
344 | |||
345 | /** |
||
346 | * Gets the job step response which was done during the enqueuing of scripts |
||
347 | * |
||
348 | * @return \EventEspressoBatchRequest\Helpers\JobStepResponse |
||
349 | */ |
||
350 | public function job_step_response() |
||
354 | |||
355 | /** |
||
356 | * Gets the batch request type indicated in the $_REQUEST |
||
357 | * |
||
358 | * @return string: EED_Batch::batch_job, EED_Batch::batch_file_job, EED_Batch::batch_not_job |
||
359 | */ |
||
360 | public function batch_request_type() |
||
377 | |||
378 | /** |
||
379 | * Unnecessary |
||
380 | * |
||
381 | * @param type $WP |
||
382 | */ |
||
383 | public function run($WP) |
||
386 | } |
||
387 |