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