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++ ) { |
|
|
|
|
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
|
|
|
|
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: