Duplicating   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

3 Methods

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