Completed
Pull Request — master (#111)
by Luca
02:07
created

Duplicating::import()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 4
nop 1
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace lloc\Msls\ContentImport\Importers\PostFields;
4
5
use lloc\Msls\ContentImport\Importers\BaseImporter;
6
7
/**
8
 * Class Duplicating
9
 *
10
 * Overwrites the destination post fields with an exact copy of the source post fields.
11
 *
12
 * @package lloc\Msls\ContentImport\Importers\PostFields
13
 */
14
class Duplicating extends BaseImporter {
15
16
	const TYPE = 'duplicating';
17
18
	/**
19
	 * Returns an array of information about the importer.
20
	 *
21
	 * @return \stdClass
22
	 */
23
	public static function info() {
24
		return (object) [
25
			'slug'        => static::TYPE,
26
			'name'        => __( 'Duplicating', 'multisite-language-switcher' ),
27
			'description' => __( 'Copies the source post fields to the destination.', 'multisite-language-switcher' )
28
		];
29
	}
30
31
	public function import( array $data ) {
32
		$source_post = $this->import_coordinates->source_post;
33
34
		foreach ( $this->filter_fields() as $field ) {
35
			$value          = $source_post->{$field};
36
			$data[ $field ] = $value;
37
			$this->logger->log_success( 'post-field/added', [ $field => $value ] );
38
		}
39
40
		if ( ! doing_action( 'wp_insert_post_data' ) ) {
41
			$postarr = array_merge( $data, [ 'ID' => $this->import_coordinates->dest_post_id ] );
42
			wp_insert_post( $postarr );
43
		}
44
45
		return $data;
46
	}
47
48
	/**
49
	 * Filters the post fields that should be duplicated from the source post to the destination one.
50
	 *
51
	 * @return array
52
	 */
53
	public function filter_fields() {
54
		$fields = array(
55
			'post_content',
56
			'post_content_filtered',
57
			'post_title',
58
			'post_excerpt',
59
		);
60
61
		/**
62
		 * Filters the list of post fields that should be imported for a post.
63
		 *
64
		 * @param array $blacklist
65
		 * @param ImportCoordinates $import_coordinates
66
		 */
67
		$fields = apply_filters( 'msls_content_import_post_fields_whitelist', $fields, $this->import_coordinates );
68
69
		return $fields;
70
	}
71
}