Completed
Push — develop ( 765c38...eb44bf )
by David
03:19
created

Wordlift_Mapping_Service::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
1
<?php
2
/**
3
 * Services: Mapping Service.
4
 *
5
 * @since 3.20.0
6
 * @package Wordlift
7
 * @subpackage Wordlift/includes/mapping
8
 */
9
10
/**
11
 * Define the Wordlift_Mapping_Service class.
12
 *
13
 * @since 3.20.0
14
 */
15
class Wordlift_Mapping_Service {
16
17
	/**
18
	 * The mapping's options.
19
	 *
20
	 * @since 3.20.0
21
	 * @access private
22
	 * @var array $options The mapping's options.
23
	 */
24
	private $options;
25
26
	/**
27
	 * The {@link Wordlift_Entity_Type_Service} instance.
28
	 *
29
	 * @since 3.20.0
30
	 * @access private
31
	 * @var \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance.
32
	 */
33
	private $entity_type_service;
34
35
	/**
36
	 * The singleton instance.
37
	 *
38
	 * @since 3.20.0
39
	 * @access private
40
	 * @var \Wordlift_Mapping_Service $instance The singleton instance.
41
	 */
42
	private static $instance;
43
44
	/**
45
	 * Create a {@link Wordlift_Mapping_Service} instance.
46
	 *
47
	 * @since 3.20.0
48
	 *
49
	 * @param \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance.
50
	 */
51
	public function __construct( $entity_type_service ) {
52
53
		// Set the entity type service instance.
54
		$this->entity_type_service = $entity_type_service;
55
56
		// Load the options.
57
		$this->options = get_option( 'wl_mappings', array() );
58
59
		// Hook to `wl_valid_entity_post_types` and to `wl_default_entity_types_for_post_type`.
60
		add_filter( 'wl_valid_entity_post_types', array( $this, 'valid_entity_post_types', ), 9 );
61
		add_filter( 'wl_default_entity_types_for_post_typewl_default_entity_types_for_post_type', array(
62
			$this,
63
			'default_entity_types_for_post_type',
64
		), 9, 2 );
65
66
		// Set the singleton instance.
67
		self::$instance = $this;
68
69
	}
70
71
	/**
72
	 * Get the singleton instance.
73
	 *
74
	 * @since 3.20.0
75
	 *
76
	 * @return \Wordlift_Mapping_Service The singleton instance.
77
	 */
78
	public static function get_instance() {
79
80
		return self::$instance;
81
	}
82
83
	/**
84
	 * Save the options.
85
	 *
86
	 * @since 3.20.0
87
	 */
88
	private function save_options() {
89
90
		update_option( 'wl_mappings', $this->options, true );
91
92
	}
93
94
	/**
95
	 * Set the default entity types for a post type.
96
	 *
97
	 * @since 3.20.0
98
	 *
99
	 * @param string $post_type Post type.
100
	 * @param array  $entity_types An array of entity types slugs.
101
	 */
102
	public function set_entity_types_for_post_type( $post_type, $entity_types ) {
103
104
		$this->options[ $post_type ] = $entity_types;
105
		$this->save_options();
106
107
	}
108
109
	/**
110
	 * Hook to `wl_valid_entity_post_types` to declare schema.org support for the configured post types.
111
	 *
112
	 * @since 3.20.0
113
	 *
114
	 * @param array $post_types The default post types.
115
	 *
116
	 * @return array The supported post types.
117
	 */
118
	public function valid_entity_post_types( $post_types ) {
119
120
		return array_merge( $post_types, array_keys( $this->options ) );
121
	}
122
123
	/**
124
	 * Hook to `wl_default_entity_types_for_post_type` to declare the entity types for a post type.
125
	 *
126
	 * @since 3.20.0
127
	 *
128
	 * @param array  $default The default entity types.
129
	 * @param string $post_type The post type.
130
	 *
131
	 * @return array The default entity types.
132
	 */
133
	public function default_entity_types_for_post_type( $default, $post_type ) {
134
135
		return isset( $this->options[ $post_type ] ) ? $this->options[ $post_type ] : $default;
136
	}
137
138
	/**
139
	 * Update the post type with the entity types, starting at the specified offset.
140
	 *
141
	 * @since 3.20.0
142
	 *
143
	 * @param string $post_type The post type.
144
	 * @param array  $entity_types The entity types.
145
	 * @param int    $offset The offset (0 by default).
146
	 *
147
	 * @return array {
148
	 * The result array.
149
	 *
150
	 * @type int     $current The current offset.
151
	 * @type int     $next The next offset.
152
	 * @type int     $count The total element count.
153
	 * }
154
	 */
155
	public function update( $post_type, $entity_types, $offset = 0 ) {
156
157
		$entity_type_service = $this->entity_type_service;
158
		$tax_query           = $this->get_tax_query( $entity_types );
159
160
		return Wordlift_Batch_Action::process( $post_type, $offset, $tax_query, function ( $post_id ) use ( $entity_type_service, $entity_types ) {
161
			foreach ( $entity_types as $entity_type ) {
162
				$entity_type_service->set( $post_id, $entity_type, false );
163
			}
164
		} );
165
	}
166
167
	/**
168
	 * Count the number of posts that need to be assigned with the entity types.
169
	 *
170
	 * @since 3.20.0
171
	 *
172
	 * @param string $post_type The post type.
173
	 * @param array  $entity_types An array of entity types.
174
	 *
175
	 * @return int The number of posts to be assigned with entity types.
176
	 */
177
	public function count( $post_type, $entity_types ) {
178
179
		$tax_query = $this->get_tax_query( $entity_types );
180
181
		return Wordlift_Batch_Action::count( $post_type, $tax_query );
182
	}
183
184
	/**
185
	 * Get the taxonomy query for the specified entity types.
186
	 *
187
	 * @since 3.20.0
188
	 *
189
	 * @param array $entity_types The entity types.
190
	 *
191
	 * @return array The tax query.
192
	 */
193
	private function get_tax_query( $entity_types ) {
194
195
		$entity_type_service = $this->entity_type_service;
196
		$entity_types_terms  = array_filter( array_map( function ( $item ) use ( $entity_type_service ) {
197
			return $entity_type_service->get_term_by_uri( $item );
198
		}, $entity_types ) );
199
200
		$entity_types_terms_ids = array_map( function ( $term ) {
201
			return $term->term_id;
202
		}, $entity_types_terms );
203
204
		$tax_query = array(
205
			'tax_query' => array(
206
				array(
207
					'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
208
					'field'    => 'term_id',
209
					'terms'    => $entity_types_terms_ids,
210
					'operator' => 'NOT IN',
211
				),
212
			),
213
		);
214
215
		return $tax_query;
216
	}
217
218
}
219