Completed
Push — develop ( 2ce797...6e9dfc )
by
unknown
02:54
created

Wordlift_Topic_Taxonomy_Service   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 173
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 8
c 4
b 0
f 1
lcom 0
cbo 1
dl 0
loc 173
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A get_instance() 0 4 1
B init() 0 41 1
A get_or_create_term_from_topic_entity() 0 20 2
A set_topic_for() 0 13 2
A unlink_topic_for() 0 3 1
1
<?php
2
3
/**
4
 * Provide custom taxonomy topic related services.
5
 *
6
 * @since 3.6.0
7
 */
8
class Wordlift_Topic_Taxonomy_Service {
9
10
	/**
11
	 * The Log service.
12
	 *
13
	 * @since 3.6.0
14
	 * @access private
15
	 * @var \Wordlift_Log_Service $log_service The Log service.
16
	 */
17
	private $log_service;
18
19
	/**
20
	 * Taxonomy name.
21
	 *
22
	 * @since 3.6.0
23
	 */
24
	const TAXONOMY_NAME = 'wl_topic';
25
26
	/**
27
	 * Taxonomy object type.
28
	 *
29
	 * @since 3.6.0
30
	 */
31
	const TAXONOMY_OBJECT_TYPE = 'post';
32
33
	/**
34
	 * Taxonomy slug.
35
	 *
36
	 * @since 3.6.0
37
	 */
38
	const TAXONOMY_SLUG = 'topic';
39
40
	/**
41
	 * A singleton instance of the Wordlift_Topic_Taxonomy_Service service.
42
	 *
43
	 * @since 3.6.0
44
	 * @access private
45
	 * @var \Wordlift_Topic_Taxonomy_Service $instance A singleton instance of Wordlift_Topic_Taxonomy_Service.
46
	 */
47
	private static $instance;
48
49
	/**
50
	 * Create a Wordlift_Topic_Taxonomy_Service instance.
51
	 *
52
	 * @since 3.6.0
53
	 *
54
	 */
55
	public function __construct() {
56
57
		$this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' );
58
59
		// Set the singleton instance.
60
		self::$instance = $this;
61
62
	}
63
64
	/**
65
	 * Get the singleton instance of the Entity service.
66
	 *
67
	 * @since 3.6.0
68
	 * @return \Wordlift_Entity_Service The singleton instance of the Entity service.
69
	 */
70
	public static function get_instance() {
71
72
		return self::$instance;
73
	}
74
75
	/**
76
	 * Just register the topic taxonomy.
77
	 * 
78
	 * @since 3.6.0
79
	 *
80
	 */
81
	public function init() {
82
83
		// See https://codex.wordpress.org/Function_Reference/register_taxonomy
84
		$labels = array(
85
			'name'              => _x( 'Topics', 'taxonomy general name' ),
86
			'singular_name'     => _x( 'Topic', 'taxonomy singular name' ),
87
			'search_items'      => __( 'Search Topics' ),
88
			'all_items'         => __( 'All Topics' ),
89
			'parent_item'       => __( 'Parent Topic' ),
90
			'parent_item_colon' => __( 'Parent Topic:' ),
91
			'edit_item'         => __( 'Edit Topic' ),
92
			'update_item'       => __( 'Update Topic' ),
93
			'add_new_item'      => __( 'Add New Topic' ),
94
			'new_item_name'     => __( 'New Topic' ),
95
			'menu_name'         => __( 'Topics' ),
96
		);
97
98
		$capabilities = array(
99
			'manage_terms' => null,
100
			'edit_terms'   => null,
101
			'delete_terms' => null,
102
			'assign_terms' => 'edit_posts'
103
		);
104
105
		$args = array(
106
			'labels'            => $labels,
107
			'capabilities'      => $capabilities,
108
			'hierarchical'      => true,
109
			'show_admin_column' => false,
110
			'show_ui'			=> false,
111
			'rewrite'			=> array(
112
				'slug'	=> self::TAXONOMY_SLUG
113
				)
114
		);
115
116
		// Register taxonomy
117
		register_taxonomy( 
118
			self::TAXONOMY_NAME, self::TAXONOMY_OBJECT_TYPE, $args 
119
		);
120
121
	}
122
123
	/**
124
	 * Get or create a taxonomy term from a given entity topic.
125
	 * 
126
	 * @since 3.6.0
127
	 *
128
	 */
129
	public function get_or_create_term_from_topic_entity( $topic ) {
130
		
131
		// Define taxonomy term slug 
132
		$term_slug = sanitize_title( $topic->post_title );
133
		// Look for an existing taxonomy term with a given slug 
134
		if ( $term = get_term_by( 'slug', $term_slug, self::TAXONOMY_NAME ) ) {
135
			return (int) $term->term_id;
136
		}
137
		// Otherwise create a new term and return it
138
		$result = wp_insert_term( 
139
			$topic->post_title, 
140
			self::TAXONOMY_NAME, 
141
			array(
142
				'slug'			=>	$term_slug,
143
				'description'	=>	$topic->post_content
144
			) 
145
		);
146
147
		return  (int) $result[ 'term_id' ];
148
	}
149
150
	/**
151
	 * Set a topic for a given post.
152
	 * 
153
	 * @since 3.6.0
154
	 *
155
	 */
156
	public function set_topic_for( $post_id, $topic_id ) {
157
		// Retrieve the topic entity post 
158
		$topic_entity_post = get_post( $topic_id );
159
		// If current topic does not exist in db false is returned
160
		if ( NULL === $topic_entity_post ) {
161
			return false;
162
		}
163
		// Create the proper taxonomy term if needed
164
		$term_id = $this->get_or_create_term_from_topic_entity( $topic_entity_post );
165
		// Link the term to the current post
166
		wp_set_post_terms( $post_id, $term_id, self::TAXONOMY_NAME, false );
167
		return true;
168
	}
169
170
	/**
171
	 * Unlink any topic for a given post.
172
	 * 
173
	 * @since 3.6.0
174
	 *
175
	 */
176
	public function unlink_topic_for( $post_id ) {
177
		wp_delete_object_term_relationships( $post_id, self::TAXONOMY_NAME );
178
	}
179
180
}
181