Total Complexity | 66 |
Total Lines | 588 |
Duplicated Lines | 0 % |
Changes | 12 | ||
Bugs | 1 | Features | 1 |
Complex classes like Object_Sync_Sf_Logging often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Object_Sync_Sf_Logging, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class Object_Sync_Sf_Logging extends WP_Logging { |
||
16 | |||
17 | protected $wpdb; |
||
18 | protected $version; |
||
19 | protected $slug; |
||
20 | protected $option_prefix; |
||
21 | |||
22 | public $enabled; |
||
23 | public $statuses_to_log; |
||
24 | |||
25 | private $schedule_name; |
||
26 | |||
27 | |||
28 | /** |
||
29 | * Constructor which sets content type and pruning for logs |
||
30 | * |
||
31 | * @param object $wpdb An instance of the wpdb class. |
||
32 | * @param string $version The version of this plugin. |
||
33 | * @param string $slug The plugin slug |
||
34 | * @param string $option_prefix The plugin's option prefix |
||
35 | * @throws \Exception |
||
36 | */ |
||
37 | public function __construct( $wpdb, $version, $slug = '', $option_prefix = '' ) { |
||
51 | |||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Start. This creates a schedule for pruning logs, and also the custom content type |
||
56 | * |
||
57 | * @throws \Exception |
||
58 | */ |
||
59 | private function init() { |
||
60 | if ( true === filter_var( $this->enabled, FILTER_VALIDATE_BOOLEAN ) ) { |
||
61 | add_filter( 'cron_schedules', array( $this, 'add_prune_interval' ) ); |
||
62 | add_filter( 'wp_log_types', array( $this, 'set_log_types' ), 10, 1 ); |
||
63 | add_filter( 'wp_logging_should_we_prune', array( $this, 'set_prune_option' ), 10, 1 ); |
||
64 | add_filter( 'wp_logging_prune_when', array( $this, 'set_prune_age' ), 10, 1 ); |
||
65 | add_filter( 'wp_logging_prune_query_args', array( $this, 'set_prune_args' ), 10, 1 ); |
||
66 | add_filter( 'wp_logging_post_type_args', array( $this, 'set_log_visibility' ), 10, 1 ); |
||
67 | add_filter( 'pre_wp_unique_post_slug', array( $this, 'set_log_slug' ), 10, 5 ); |
||
68 | |||
69 | // add a sortable Type column to the posts admin |
||
70 | add_filter( 'manage_edit-wp_log_columns', array( $this, 'type_column' ), 10, 1 ); |
||
71 | add_filter( 'manage_edit-wp_log_sortable_columns', array( $this, 'sortable_columns' ), 10, 1 ); |
||
72 | add_action( 'manage_wp_log_posts_custom_column', array( $this, 'type_column_content' ), 10, 2 ); |
||
73 | |||
74 | // filter the log posts admin by log type |
||
75 | add_filter( 'parse_query', array( $this, 'posts_filter' ), 10, 1 ); |
||
76 | add_action( 'restrict_manage_posts', array( $this, 'restrict_log_posts' ) ); |
||
77 | |||
78 | // when the schedule might change |
||
79 | add_action( 'update_option_' . $this->option_prefix . 'logs_how_often_unit', array( $this, 'check_log_schedule' ), 10, 3 ); |
||
80 | add_action( 'update_option_' . $this->option_prefix . 'logs_how_often_number', array( $this, 'check_log_schedule' ), 10, 3 ); |
||
81 | |||
82 | $this->save_log_schedule(); |
||
83 | } |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * Set visibility for the post type |
||
88 | * |
||
89 | * @param array $log_args The post arguments |
||
90 | * @return array $log_args |
||
91 | */ |
||
92 | public function set_log_visibility( $log_args ) { |
||
93 | // set public to true overrides the WP_DEBUG setting that is the default on the class |
||
94 | // capabilities makes it so (currently) only admin users can see the log posts in their admin view |
||
95 | // note: a public value of true is required to show Logs as a nav menu item on the admin. |
||
96 | // however, if we don't set exclude_from_search to true and publicly_queryable to false, logs *can* appear in search results |
||
97 | $log_args['public'] = true; |
||
98 | $log_args['publicly_queryable'] = false; |
||
99 | $log_args['exclude_from_search'] = true; |
||
100 | $log_args['capabilities'] = array( |
||
101 | 'edit_post' => $this->capability, |
||
102 | 'read_post' => $this->capability, |
||
103 | 'delete_post' => $this->capability, |
||
104 | 'edit_posts' => $this->capability, |
||
105 | 'edit_others_posts' => $this->capability, |
||
106 | 'delete_posts' => $this->capability, |
||
107 | 'publish_posts' => $this->capability, |
||
108 | 'read_private_posts' => $this->capability, |
||
109 | ); |
||
110 | |||
111 | $log_args = apply_filters( $this->option_prefix . 'logging_post_type_args', $log_args ); |
||
112 | |||
113 | return $log_args; |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Create a (probably unique) post name for logs in a more performant manner than wp_unique_post_slug(). |
||
118 | * |
||
119 | * @param string $override_slug Short-circuit return value. |
||
120 | * @param string $slug The desired slug (post_name). |
||
121 | * @param int $post_ID The post ID |
||
122 | * @param string $post_status The post status |
||
123 | * @param string $post_type The post type |
||
124 | * @return string |
||
125 | */ |
||
126 | public function set_log_slug( $override_slug, $slug, $post_ID, $post_status, $post_type ) { |
||
127 | if ( 'wp_log' === $post_type ) { |
||
128 | $override_slug = uniqid( $post_type . '-', true ) . '-' . wp_generate_password( 32, false ); |
||
129 | } |
||
130 | return $override_slug; |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Add a Type column to the posts admin for this post type |
||
135 | * |
||
136 | * @param array $columns |
||
137 | * @return array $columns |
||
138 | */ |
||
139 | public function type_column( $columns ) { |
||
140 | $columns['type'] = __( 'Type', 'object-sync-for-salesforce' ); |
||
141 | return $columns; |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Make the Type column in the posts admin for this post type sortable |
||
146 | * |
||
147 | * @param array $columns |
||
148 | * @return array $columns |
||
149 | */ |
||
150 | public function sortable_columns( $columns ) { |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Add the content for the Type column in the posts admin for this post type |
||
157 | * |
||
158 | * @param string $column_name |
||
159 | * @param int $post_id |
||
160 | */ |
||
161 | public function type_column_content( $column_name, $post_id ) { |
||
175 | } |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * Filter log posts by the taxonomy from the dropdown when a value is present |
||
180 | * |
||
181 | * @param object $query |
||
182 | * @return object $query |
||
183 | */ |
||
184 | public function posts_filter( $query ) { |
||
185 | global $pagenow; |
||
186 | $type = 'wp_log'; |
||
187 | $taxonomy = 'wp_log_type'; |
||
188 | if ( is_admin() && 'edit.php' === $pagenow ) { |
||
189 | if ( isset( $_GET['post_type'] ) && esc_attr( $_GET['post_type'] ) === $type ) { |
||
190 | if ( isset( $_GET[ $taxonomy ] ) && '' !== $_GET[ $taxonomy ] ) { |
||
191 | $query->post_type = $type; |
||
192 | $query->tax_query = array( |
||
193 | array( |
||
194 | 'taxonomy' => $taxonomy, |
||
195 | 'field' => 'slug', |
||
196 | 'terms' => esc_attr( $_GET[ $taxonomy ] ), |
||
197 | ), |
||
198 | ); |
||
199 | } |
||
200 | } |
||
201 | } |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * Add a filter form for the log admin so we can filter by wp_log_type taxonomy values |
||
206 | * |
||
207 | * @param object $query |
||
208 | * @return object $query |
||
209 | */ |
||
210 | public function restrict_log_posts() { |
||
211 | $type = 'wp_log'; |
||
212 | $taxonomy = 'wp_log_type'; |
||
213 | // only add filter to post type you want |
||
214 | if ( isset( $_GET['post_type'] ) && esc_attr( $_GET['post_type'] ) === $type ) { |
||
215 | // get wp_log_type |
||
216 | $terms = get_terms( |
||
217 | [ |
||
218 | 'taxonomy' => $taxonomy, |
||
219 | 'hide_empty' => true, |
||
220 | ] |
||
221 | ); |
||
222 | ?> |
||
223 | <select name="wp_log_type"> |
||
224 | <option value=""><?php _e( 'All log types ', 'object-sync-for-salesforce' ); ?></option> |
||
225 | <?php |
||
226 | $current_log_type = isset( $_GET[ $taxonomy ] ) ? esc_attr( $_GET[ $taxonomy ] ) : ''; |
||
227 | foreach ( $terms as $key => $term ) { |
||
228 | printf( |
||
229 | '<option value="%s"%s>%s</option>', |
||
230 | $term->slug, |
||
231 | $term->slug == $current_log_type ? ' selected="selected"' : '', |
||
232 | $term->name |
||
233 | ); |
||
234 | } |
||
235 | ?> |
||
236 | </select> |
||
237 | <?php |
||
238 | } |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * When the cron settings change, clear the relevant schedule |
||
243 | * |
||
244 | * @param mixed $old_value Previous option value |
||
245 | * @param mixed $new_value New option value |
||
246 | * @param string $option Name of option |
||
247 | */ |
||
248 | public function check_log_schedule( $old_value, $new_value, $option ) { |
||
249 | $clear_schedule = false; |
||
250 | $schedule_unit = get_option( $this->option_prefix . 'logs_how_often_unit', '' ); |
||
251 | $schedule_number = get_option( $this->option_prefix . 'logs_how_often_number', '' ); |
||
252 | if ( $this->option_prefix . 'logs_how_often_unit' === $option ) { |
||
253 | $old_frequency = $this->get_schedule_frequency( $old_value, $schedule_number ); |
||
254 | $new_frequency = $this->get_schedule_frequency( $new_value, $schedule_number ); |
||
255 | $old_key = $old_frequency['key']; |
||
256 | $new_key = $new_frequency['key']; |
||
257 | if ( $old_key !== $new_key ) { |
||
258 | $clear_schedule = true; |
||
259 | } |
||
260 | } |
||
261 | if ( $this->option_prefix . 'logs_how_often_number' === $option ) { |
||
262 | $old_frequency = $this->get_schedule_frequency( $schedule_unit, $old_value ); |
||
263 | $new_frequency = $this->get_schedule_frequency( $schedule_unit, $new_value ); |
||
264 | $old_key = $old_frequency['key']; |
||
265 | $new_key = $new_frequency['key']; |
||
266 | if ( $old_key !== $new_key ) { |
||
267 | $clear_schedule = true; |
||
268 | } |
||
269 | } |
||
270 | if ( true === $clear_schedule ) { |
||
271 | wp_clear_scheduled_hook( $this->schedule_name ); |
||
272 | $this->save_log_schedule(); |
||
273 | } |
||
274 | } |
||
275 | |||
276 | /** |
||
277 | * Save a cron schedule |
||
278 | * |
||
279 | */ |
||
280 | public function save_log_schedule() { |
||
281 | global $pagenow; |
||
282 | if ( ( 'options.php' !== $pagenow ) && ( ! isset( $_GET['page'] ) || $this->slug . '-admin' !== $_GET['page'] ) ) { |
||
283 | return; |
||
284 | } |
||
285 | $schedule_unit = get_option( $this->option_prefix . 'logs_how_often_unit', '' ); |
||
286 | $schedule_number = get_option( $this->option_prefix . 'logs_how_often_number', '' ); |
||
287 | $frequency = $this->get_schedule_frequency( $schedule_unit, $schedule_number ); |
||
288 | $key = $frequency['key']; |
||
289 | if ( ! wp_next_scheduled( $this->schedule_name ) ) { |
||
290 | wp_schedule_event( time(), $key, $this->schedule_name ); |
||
291 | } |
||
292 | } |
||
293 | |||
294 | /** |
||
295 | * Add interval to wp schedules based on admin settings |
||
296 | * |
||
297 | * @param array $schedules An array of scheduled cron items. |
||
298 | * @return array $frequency |
||
299 | */ |
||
300 | public function add_prune_interval( $schedules ) { |
||
314 | |||
315 | } |
||
316 | |||
317 | /** |
||
318 | * Convert the schedule frequency from the admin settings into an array |
||
319 | * interval must be in seconds for the class to use it |
||
320 | * |
||
321 | * @param string $unit A unit of time. |
||
322 | * @param number $number The number of those units. |
||
323 | * @return array |
||
324 | */ |
||
325 | public function get_schedule_frequency( $unit, $number ) { |
||
326 | |||
327 | switch ( $unit ) { |
||
328 | case 'minutes': |
||
329 | $seconds = 60; |
||
330 | break; |
||
331 | case 'hours': |
||
332 | $seconds = 3600; |
||
333 | break; |
||
334 | case 'days': |
||
335 | $seconds = 86400; |
||
336 | break; |
||
337 | default: |
||
338 | $seconds = 0; |
||
339 | } |
||
340 | |||
341 | $key = $unit . '_' . $number; |
||
342 | |||
343 | return array( |
||
344 | 'key' => $key, |
||
345 | 'seconds' => $seconds, |
||
346 | ); |
||
347 | |||
348 | } |
||
349 | |||
350 | /** |
||
351 | * Set terms for Salesforce logs |
||
352 | * |
||
353 | * @param array $terms An array of string log types in the WP_Logging class. |
||
354 | * @return array $terms |
||
355 | */ |
||
356 | public function set_log_types( $terms ) { |
||
357 | $terms[] = 'salesforce'; |
||
358 | return $terms; |
||
359 | } |
||
360 | |||
361 | /** |
||
362 | * Should logs be pruned at all? |
||
363 | * |
||
364 | * @param string $should_we_prune Whether to prune old log items. |
||
365 | * @return string $should_we_prune Whether to prune old log items. |
||
366 | */ |
||
367 | public function set_prune_option( $should_we_prune ) { |
||
371 | } |
||
372 | |||
373 | /** |
||
374 | * Set how often to prune the Salesforce logs |
||
375 | * |
||
376 | * @param string $how_old How old the oldest non-pruned log items should be allowed to be. |
||
377 | * @return string $how_old |
||
378 | */ |
||
379 | public function set_prune_age( $how_old ) { |
||
380 | $value = get_option( $this->option_prefix . 'logs_how_old', '' ) . ' ago'; |
||
381 | if ( '' !== $value ) { |
||
382 | return $value; |
||
383 | } else { |
||
384 | return $how_old; |
||
385 | } |
||
386 | } |
||
387 | |||
388 | /** |
||
389 | * Set arguments for only getting the Salesforce logs |
||
390 | * |
||
391 | * @param array $args Argument array for get_posts determining what posts are eligible for pruning. |
||
392 | * @return array $args |
||
393 | */ |
||
394 | public function set_prune_args( $args ) { |
||
395 | $args['wp_log_type'] = 'salesforce'; |
||
396 | return $args; |
||
397 | } |
||
398 | |||
399 | /** |
||
400 | * Setup new log entry |
||
401 | * |
||
402 | * Check and see if we should log anything, and if so, send it to add() |
||
403 | * |
||
404 | * @access public |
||
405 | * @since 1.0 |
||
406 | * |
||
407 | * @param string|array $title_or_params A log post title, or the full array of parameters |
||
408 | * @param string $message The log message. |
||
409 | * @param string|0 $trigger The type of log triggered. Usually one of: debug, notice, warning, error. |
||
410 | * @param int $parent The parent WordPress object. |
||
411 | * @param string $status The log status. |
||
412 | * |
||
413 | * @uses self::add() |
||
414 | * @see Object_Sync_Sf_Mapping::__construct() the location of the bitmasks that define the logging triggers. |
||
415 | * |
||
416 | * @return void |
||
417 | */ |
||
418 | public function setup( $title_or_params, $message = '', $trigger = 0, $parent = 0, $status = '' ) { |
||
419 | |||
420 | if ( is_array( $title_or_params ) ) { |
||
421 | $title = $title_or_params['title']; |
||
422 | $message = $title_or_params['message']; |
||
423 | $trigger = $title_or_params['trigger']; |
||
424 | $parent = $title_or_params['parent']; |
||
425 | $status = $title_or_params['status']; |
||
426 | } else { |
||
427 | $title = $title_or_params; |
||
428 | } |
||
429 | |||
430 | if ( ! is_array( maybe_unserialize( $this->statuses_to_log ) ) ) { |
||
431 | if ( $status === $this->statuses_to_log ) { |
||
432 | $this->add( $title, $message, $parent ); |
||
433 | } else { |
||
434 | return; |
||
435 | } |
||
436 | } |
||
437 | |||
438 | if ( true === filter_var( $this->enabled, FILTER_VALIDATE_BOOLEAN ) && in_array( $status, maybe_unserialize( $this->statuses_to_log ), true ) ) { |
||
439 | $triggers_to_log = get_option( $this->option_prefix . 'triggers_to_log', array() ); |
||
440 | // if we force strict on this in_array, it fails because the mapping triggers are bit operators, as indicated in Object_Sync_Sf_Mapping class's method __construct() |
||
441 | if ( in_array( $trigger, maybe_unserialize( $triggers_to_log ) ) || 0 === $trigger ) { |
||
442 | $this->add( $title, $message, $parent ); |
||
443 | } elseif ( is_array( $trigger ) && array_intersect( $trigger, maybe_unserialize( $triggers_to_log ) ) ) { |
||
444 | $this->add( $title, $message, $parent ); |
||
445 | } |
||
446 | } |
||
447 | } |
||
448 | |||
449 | /** |
||
450 | * Create new log entry |
||
451 | * |
||
452 | * This is just a simple and fast way to log something. Use self::insert_log() |
||
453 | * if you need to store custom meta data |
||
454 | * |
||
455 | * @access public |
||
456 | * @since 1.0 |
||
457 | * |
||
458 | * @param string $title A log post title. |
||
459 | * |
||
460 | * @uses self::insert_log() |
||
461 | * @param string $message The log message. |
||
462 | * @param int $parent The parent WordPress object. |
||
463 | * @param string $type The type of log message; defaults to 'salesforce'. |
||
464 | * |
||
465 | * @return int The ID of the new log entry |
||
466 | */ |
||
467 | public static function add( $title = '', $message = '', $parent = 0, $type = 'salesforce' ) { |
||
477 | |||
478 | } |
||
479 | |||
480 | |||
481 | /** |
||
482 | * Easily retrieves log items for a particular object ID |
||
483 | * |
||
484 | * @access private |
||
485 | * @since 1.0 |
||
486 | * |
||
487 | * @param int $object_id A WordPress object ID. |
||
488 | * @param string $type The type of log item; defaults to 'salesforce' because that's the type of logs we create. |
||
489 | * @param int $paged Which page of results do we want? |
||
490 | * |
||
491 | * @uses self::get_connected_logs() |
||
492 | * |
||
493 | * @return array |
||
494 | */ |
||
495 | public static function get_logs( $object_id = 0, $type = 'salesforce', $paged = null ) { |
||
496 | return self::get_connected_logs( |
||
497 | array( |
||
498 | 'post_parent' => (int) $object_id, |
||
499 | 'paged' => (int) $paged, |
||
500 | 'log_type' => (string) $type, |
||
501 | ) |
||
502 | ); |
||
503 | } |
||
504 | |||
505 | |||
506 | /** |
||
507 | * Retrieve all connected logs |
||
508 | * |
||
509 | * Used for retrieving logs related to particular items, such as a specific purchase. |
||
510 | * |
||
511 | * @access private |
||
512 | * @since 1.0 |
||
513 | * |
||
514 | * @param Array $args An array of arguments for get_posts(). |
||
515 | * |
||
516 | * @uses wp_parse_args() |
||
517 | * @uses get_posts() |
||
518 | * @uses get_query_var() |
||
519 | * @uses self::valid_type() |
||
520 | * |
||
521 | * @return array / false |
||
522 | */ |
||
523 | public static function get_connected_logs( $args = array() ) { |
||
556 | |||
557 | } |
||
558 | |||
559 | |||
560 | /** |
||
561 | * Retrieves number of log entries connected to particular object ID |
||
562 | * |
||
563 | * @access private |
||
564 | * @since 1.0 |
||
565 | * |
||
566 | * @param int $object_id A WordPress object ID. |
||
567 | * @param string $type The type of log item; defaults to 'salesforce' because that's the type of logs we create. |
||
568 | * @param Array $meta_query A WordPress meta query, parseable by WP_Meta_Query. |
||
569 | * |
||
570 | * @uses WP_Query() |
||
571 | * @uses self::valid_type() |
||
572 | * |
||
573 | * @return int |
||
574 | */ |
||
575 | public static function get_log_count( $object_id = 0, $type = 'salesforce', $meta_query = null ) { |
||
603 | |||
604 | } |
||
605 | |||
606 | } |
||
607 |