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 | class WP_Logging { |
||
12 | |||
13 | |||
14 | /** |
||
15 | * Class constructor. |
||
16 | * |
||
17 | * @since 1.0 |
||
18 | * |
||
19 | * @access public |
||
20 | * @return void |
||
|
|||
21 | */ |
||
22 | function __construct() { |
||
34 | |||
35 | /** |
||
36 | * Allows you to tie in a cron job and prune old logs. |
||
37 | * |
||
38 | * @since 1.1 |
||
39 | * @access public |
||
40 | * |
||
41 | * @uses $this->get_logs_to_prune() Returns array of posts via get_posts of logs to prune |
||
42 | * @uses $this->prune_old_logs() Deletes the logs that we don't want anymore |
||
43 | */ |
||
44 | public function prune_logs(){ |
||
59 | |||
60 | /** |
||
61 | * Deletes the old logs that we don't want |
||
62 | * |
||
63 | * @since 1.1 |
||
64 | * @access private |
||
65 | * |
||
66 | * @param array/obj $logs required The array of logs we want to prune |
||
67 | * |
||
68 | * @uses wp_delete_post() Deletes the post from WordPress |
||
69 | * |
||
70 | * @filter wp_logging_force_delete_log Allows user to override the force delete setting which bypasses the trash |
||
71 | */ |
||
72 | private function prune_old_logs( $logs ){ |
||
82 | |||
83 | /** |
||
84 | * Returns an array of posts that are prune candidates. |
||
85 | * |
||
86 | * @since 1.1 |
||
87 | * @access private |
||
88 | * |
||
89 | * @return array $old_logs The array of posts that were returned from get_posts |
||
90 | * |
||
91 | * @uses apply_filters() Allows users to change given args |
||
92 | * @uses get_posts() Returns an array of posts from given args |
||
93 | * |
||
94 | * @filter wp_logging_prune_when Users can change how long ago we are looking for logs to prune |
||
95 | * @filter wp_logging_prune_query_args Gives users access to change any query args for pruning |
||
96 | */ |
||
97 | private function get_logs_to_prune(){ |
||
117 | |||
118 | /** |
||
119 | * Log types |
||
120 | * |
||
121 | * Sets up the default log types and allows for new ones to be created |
||
122 | * |
||
123 | * @access private |
||
124 | * @since 1.0 |
||
125 | * |
||
126 | * @return array |
||
127 | */ |
||
128 | |||
129 | private static function log_types() { |
||
136 | |||
137 | |||
138 | /** |
||
139 | * Registers the wp_log Post Type |
||
140 | * |
||
141 | * @access public |
||
142 | * @since 1.0 |
||
143 | * |
||
144 | * @uses register_post_type() |
||
145 | * |
||
146 | * @return void |
||
147 | */ |
||
148 | |||
149 | public function register_post_type() { |
||
165 | |||
166 | |||
167 | /** |
||
168 | * Registers the Type Taxonomy |
||
169 | * |
||
170 | * The Type taxonomy is used to determine the type of log entry |
||
171 | * |
||
172 | * @access public |
||
173 | * @since 1.0 |
||
174 | * |
||
175 | * @uses register_taxonomy() |
||
176 | * @uses term_exists() |
||
177 | * @uses wp_insert_term() |
||
178 | * |
||
179 | * @return void |
||
180 | */ |
||
181 | |||
182 | public function register_taxonomy() { |
||
194 | |||
195 | |||
196 | /** |
||
197 | * Check if a log type is valid |
||
198 | * |
||
199 | * Checks to see if the specified type is in the registered list of types |
||
200 | * |
||
201 | * @access private |
||
202 | * @since 1.0 |
||
203 | * |
||
204 | * |
||
205 | * @return array |
||
206 | */ |
||
207 | |||
208 | private static function valid_type( $type ) { |
||
211 | |||
212 | |||
213 | /** |
||
214 | * Create new log entry |
||
215 | * |
||
216 | * This is just a simple and fast way to log something. Use self::insert_log() |
||
217 | * if you need to store custom meta data |
||
218 | * |
||
219 | * @access private |
||
220 | * @since 1.0 |
||
221 | * |
||
222 | * @uses self::insert_log() |
||
223 | * |
||
224 | * @return int The ID of the new log entry |
||
225 | */ |
||
226 | |||
227 | public static function add( $title = '', $message = '', $parent = 0, $type = null ) { |
||
239 | |||
240 | |||
241 | /** |
||
242 | * Stores a log entry |
||
243 | * |
||
244 | * @access private |
||
245 | * @since 1.0 |
||
246 | * |
||
247 | * @uses wp_parse_args() |
||
248 | * @uses wp_insert_post() |
||
249 | * @uses update_post_meta() |
||
250 | * @uses wp_set_object_terms() |
||
251 | * @uses sanitize_key() |
||
252 | * |
||
253 | * @return int The ID of the newly created log item |
||
254 | */ |
||
255 | |||
256 | public static function insert_log( $log_data = array(), $log_meta = array() ) { |
||
291 | |||
292 | |||
293 | /** |
||
294 | * Update and existing log item |
||
295 | * |
||
296 | * @access private |
||
297 | * @since 1.0 |
||
298 | * |
||
299 | * @uses wp_parse_args() |
||
300 | * @uses wp_update_post() |
||
301 | * @uses update_post_meta() |
||
302 | * |
||
303 | * @return bool True if successful, false otherwise |
||
304 | */ |
||
305 | public static function update_log( $log_data = array(), $log_meta = array() ) { |
||
330 | |||
331 | |||
332 | /** |
||
333 | * Easily retrieves log items for a particular object ID |
||
334 | * |
||
335 | * @access private |
||
336 | * @since 1.0 |
||
337 | * |
||
338 | * @uses self::get_connected_logs() |
||
339 | * |
||
340 | * @return array |
||
341 | */ |
||
342 | |||
343 | public static function get_logs( $object_id = 0, $type = null, $paged = null ) { |
||
347 | |||
348 | |||
349 | /** |
||
350 | * Retrieve all connected logs |
||
351 | * |
||
352 | * Used for retrieving logs related to particular items, such as a specific purchase. |
||
353 | * |
||
354 | * @access private |
||
355 | * @since 1.0 |
||
356 | * |
||
357 | * @uses wp_parse_args() |
||
358 | * @uses get_posts() |
||
359 | * @uses get_query_var() |
||
360 | * @uses self::valid_type() |
||
361 | * |
||
362 | * @return array / false |
||
363 | */ |
||
364 | |||
365 | View Code Duplication | public static function get_connected_logs( $args = array() ) { |
|
399 | |||
400 | |||
401 | /** |
||
402 | * Retrieves number of log entries connected to particular object ID |
||
403 | * |
||
404 | * @access private |
||
405 | * @since 1.0 |
||
406 | * |
||
407 | * @uses WP_Query() |
||
408 | * @uses self::valid_type() |
||
409 | * |
||
410 | * @return int |
||
411 | */ |
||
412 | |||
413 | public static function get_log_count( $object_id = 0, $type = null, $meta_query = null ) { |
||
443 | |||
444 | } |
||
445 | $GLOBALS['wp_logs'] = new WP_Logging(); |
||
446 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.