Completed
Push — add/importer-stats ( fd32d3 )
by
unknown
06:42
created

Jetpack_Import_Stats   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 8 2
A get_calling_class() 0 24 5
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
		$action = current_filter();
29
		$backtrace = wp_debug_backtrace_summary( null, 0, false );
30
31
		$do_action_pos = -1;
32
		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...
33
			// Find the location in the stack of the calling action
34
			if ( preg_match( "/^do_action\\(\'([^\']+)/", $backtrace[ $i ], $matches ) ) {
35
				if ( $matches[1] === $action ) {
36
					$do_action_pos = $i;
37
					break;
38
				}
39
			}
40
		}
41
42
		// if the action wasn't called, the calling class is unknown
43
		if ( -1 === $do_action_pos ) {
44
			return 'unknown';
45
		}
46
47
		// The calling class is next in the stack, after $do_action_pos
48
		list( $className ) = explode( '->', $backtrace[ $do_action_pos + 1 ] );
49
		return $className;
50
	}
51
52
	public static function log_import_progress( $importer ) {
53
		// prefer self-reported importer-names
54
		if ( ! $importer ) {
55
			// fall back to inferring by calling class name
56
			$importer = self::get_calling_class();
57
		}
58
		
59
		// Give known importers a "friendly" name
60
		if ( isset( self::$known_importers[ $importer ] ) ) {
61
			$importer = self::$known_importers[ $importer ];
62
		}
63
		$action = current_filter();
64
		// map action to event name
65
		$event_name = self::$action_event_name_map[ $action ];
66
		
67
		$current_user = wp_get_current_user();
68
69
		// Record event to Tracks
70
		jetpack_tracks_record_event( $current_user, $event_name, array(
71
			'importer' => $importer,
72
		) );
73
	}
74
}
75
76
add_action( 'init', array( 'Jetpack_Import_Stats', 'init' ) );
77