Duplicating   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 60
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A info() 0 7 1
A import() 0 27 3
A filter_post_meta() 0 14 1
1
<?php
2
3
namespace lloc\Msls\ContentImport\Importers\PostMeta;
4
5
use lloc\Msls\ContentImport\Importers\BaseImporter;
6
7
class Duplicating extends BaseImporter {
8
9
	const TYPE = 'duplicating';
10
11
	/**
12
	 * Returns an array of information about the importer.
13
	 *
14
	 * @return \stdClass
15
	 */
16
	public static function info() {
17
		return (object) [
18
			'slug'        => static::TYPE,
19
			'name'        => __( 'Duplicating', 'multisite-language-switcher' ),
20
			'description' => __( 'Copies the source post meta to the destination.', 'multisite-language-switcher' )
21
		];
22
	}
23
24
	public function import( array $data ) {
25
		$source_blog_id = $this->import_coordinates->source_blog_id;
26
		$source_post_id = $this->import_coordinates->source_post_id;
27
		$dest_post_id   = $this->import_coordinates->dest_post_id;
28
29
		switch_to_blog( $source_blog_id );
30
		$source_meta = get_post_custom( $source_post_id );
31
32
		switch_to_blog( $this->import_coordinates->dest_blog_id );
33
34
		$source_meta = $this->filter_post_meta( $source_meta );
35
36
		foreach ( $source_meta as $key => $entries ) {
37
			delete_post_meta( $dest_post_id, $key );
38
			foreach ( $entries as $entry ) {
39
				$entry = maybe_unserialize( $entry );
40
				add_post_meta( $dest_post_id, $key, $entry );
41
				$this->logger->log_success( 'meta/added', array( $key => $entry ) );
42
			}
43
		}
44
45
		wp_cache_delete( $dest_post_id, 'post_meta' );
46
47
		restore_current_blog();
48
49
		return $data;
50
	}
51
52
	public function filter_post_meta( array $meta ) {
53
		$blacklist = array( '_edit_last', '_thumbnail_id', '_edit_lock' );
54
55
		/**
56
		 * Filters the list of post meta that should not be imported for a post.
57
		 *
58
		 * @param array $blacklist
59
		 * @param array $meta
60
		 * @param ImportCoordinates $import_coordinates
61
		 */
62
		$blacklist = apply_filters( 'msls_content_import_post_meta_blacklist', $blacklist, $meta, $this->import_coordinates );
63
64
		return array_diff_key( $meta, array_combine( $blacklist, $blacklist ) );
65
	}
66
}