Completed
Push — update/sync-refactor-import-ac... ( 47110f )
by
unknown
10:30 queued 21s
created

Jetpack_Sync_Module_Import   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A name() 0 3 1
A init_listeners() 0 10 1
A sync_import_done() 0 18 2
C sync_import_end() 0 26 7
A get_importer_name() 0 4 2
A is_importer() 0 9 3
1
<?php
2
3
require_once dirname( __FILE__ ) . '/class.jetpack-sync-settings.php';
4
5
class Jetpack_Sync_Module_Import extends Jetpack_Sync_Module {
6
7
	private $import_end = false;
8
9
	public function name() {
10
		return 'import';
11
	}
12
13
	public function init_listeners( $callable ) {
14
		add_action( 'export_wp', $callable );
15
		add_action( 'jetpack_sync_import_end', $callable, 10, 2 );
16
17
		// Movable type, RSS, Livejournal
18
		add_action( 'import_done', array( $this, 'sync_import_done' ) );
19
20
		// WordPress, Blogger, Livejournal, woo tax rate
21
		add_action( 'import_end', array( $this, 'sync_import_end' ) );
22
	}
23
24
	public function sync_import_done( $importer ) {
25
		// We already ran an send the import
26
		if ( $this->import_end ) {
27
			return;
28
		}
29
30
		$importer_name = $this->get_importer_name( $importer );
31
32
		/**
33
		 * Sync Event that tells that the import is finished
34
		 *
35
		 * @since 5.0.0
36
		 *
37
		 * $param string $importer
38
		 */
39
		do_action( 'jetpack_sync_import_end', $importer, $importer_name );
40
		$this->import_end = true;
41
	}
42
43
	public function sync_import_end() {
44
		// We already ran an send the import
45
		if ( $this->import_end ) {
46
			return;
47
		}
48
49
		$this->import_end = true;
50
		$importer         = 'unknown';
51
		$backtrace        = wp_debug_backtrace_summary( null, 0, false );
52
		if ( $this->is_importer( $backtrace, 'Blogger_Importer' ) ) {
53
			$importer = 'blogger';
54
		}
55
56
		if ( 'unknown' === $importer && $this->is_importer( $backtrace, 'WC_Tax_Rate_Importer' ) ) {
57
			$importer = 'woo-tax-rate';
58
		}
59
60
		if ( 'unknown' === $importer && $this->is_importer( $backtrace, 'WP_Import' ) ) {
61
			$importer = 'wordpress';
62
		}
63
64
		$importer_name = $this->get_importer_name( $importer );
65
66
		/** This filter is already documented in sync/class.jetpack-sync-module-posts.php */
67
		do_action( 'jetpack_sync_import_end', $importer, $importer_name );
68
	}
69
70
	private function get_importer_name( $importer ) {
71
		$importers = get_importers();
72
		return isset( $importers[ $importer ] ) ? $importers[ $importer ][0] : 'Unknown Importer';
73
	}
74
75
	private function is_importer( $backtrace, $class_name ) {
76
		foreach ( $backtrace as $trace ) {
77
			if ( strpos( $trace, $class_name ) !== false ) {
78
				return true;
79
			}
80
		}
81
82
		return false;
83
	}
84
}
85