Completed
Push — add/importer-stats ( 7e9415...6df637 )
by
unknown
10:19 queued 03:18
created

Jetpack_Import_Stats   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 90
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 8 2
B get_calling_class() 0 42 10
A log_import_progress() 0 22 3
1
<?php
2
3
class Jetpack_Import_Stats {
4
	static $known_importers = array(
5
		'Blogger_Importer' => 'blogger',
6
		'LJ_API_Import' => 'livejournal',
7
		'MT_Import' => 'mt',
8
		'RSS_Import' => 'rss',
9
		'WP_Import' => 'wordpress',
10
	);
11
12
	static $action_event_name_map = array(
13
		'import_start' => 'jetpack_import_start',
14
		'import_done'  => 'jetpack_import_done',
15
		'import_end'   => 'jetpack_import_done',
16
	);
17
18
	public static function init() {
19
		// Only handle import actions for sites that have agreed to TOS 
20
		if ( Jetpack::jetpack_tos_agreed() ) {
21
			add_action( 'import_start', array( 'Jetpack_Import_Stats', 'log_import_progress' ) );
22
			add_action( 'import_done',  array( 'Jetpack_Import_Stats', 'log_import_progress' ) );
23
			add_action( 'import_end',   array( 'Jetpack_Import_Stats', 'log_import_progress' ) );
24
		}
25
	}
26
27
	private static function get_calling_class() {
28
		// If WP_Importer doesn't exist, neither will any importer that extends it
29
		if ( ! class_exists( 'WP_Importer' ) ){
30
			return 'unknown';
31
		}
32
33
		$action = current_filter();
34
		$backtrace = wp_debug_backtrace_summary( null, 0, false );
35
36
		$do_action_pos = -1;
37
		for ( $i = 0; $i < count( $backtrace ); $i++ ) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
38
			// Find the location in the stack of the calling action
39
			if ( preg_match( "/^do_action\\(\'([^\']+)/", $backtrace[ $i ], $matches ) ) {
40
				if ( $matches[1] === $action ) {
41
					$do_action_pos = $i;
42
					break;
43
				}
44
			}
45
		}
46
47
		// if the action wasn't called, the calling class is unknown
48
		if ( -1 === $do_action_pos ) {
49
			return 'unknown';
50
		}
51
52
		// continue iterating the stack looking for a caller that extends WP_Import
53
		for ( $i = $do_action_pos + 1; $i < count( $backtrace ); $i++ ) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
54
			// grab only class_name from the trace
55
			list( $class_name ) = explode( '->', $backtrace[ $i ] );
56
57
			// check if the class extends WP_Importer
58
			if ( class_exists( $class_name ) ) {
59
				$parents = class_parents( $class_name );
60
				if ( $parents && in_array( 'WP_Importer', $parents ) ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $parents of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
61
					return $class_name;
62
				}
63
			}
64
		}
65
66
		// If we've exhausted the stack without a match, the calling class is unknown
67
		return 'unknown';
68
	}
69
70
	public static function log_import_progress( $importer ) {
71
		// prefer self-reported importer-names
72
		if ( ! $importer ) {
73
			// fall back to inferring by calling class name
74
			$importer = self::get_calling_class();
75
		}
76
		
77
		// Give known importers a "friendly" name
78
		if ( isset( self::$known_importers[ $importer ] ) ) {
79
			$importer = self::$known_importers[ $importer ];
80
		}
81
		$action = current_filter();
82
		// map action to event name
83
		$event_name = self::$action_event_name_map[ $action ];
84
		
85
		$current_user = wp_get_current_user();
86
87
		// Record event to Tracks
88
		jetpack_tracks_record_event( $current_user, $event_name, array(
89
			'importer' => $importer,
90
		) );
91
	}
92
}
93
94
add_action( 'init', array( 'Jetpack_Import_Stats', 'init' ) );
95