Completed
Push — development ( 5ed9d2...646735 )
by
unknown
04:14
created

WP_Toolset   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 89
rs 10
c 1
b 0
f 0
ccs 0
cts 31
cp 0
wmc 14
lcom 0
cbo 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
A get_post_title() 0 4 2
A get_term_title() 0 7 2
A get_user_title() 0 3 1
A get_comment_title() 0 3 1
A get_term_by_descriptor() 0 18 6
A wildcard_term_descriptor_to_full_term_descriptor() 0 10 2
1
<?php
2
3
namespace Carbon_Fields\Toolset;
4
5
use Carbon_Fields\Exception\Incorrect_Syntax_Exception;
6
7
/**
8
 * Provides common tools when dealing with WordPress data such as posts, terms etc.
9
 */
10
class WP_Toolset {
11
12
	/**
13
	 * Get post title
14
	 *
15
	 * @param int $id
16
	 * @return string $title The title of the item.
17
	 */
18
	public function get_post_title( $id ) {
19
		$post = get_post( $id );
20
		return $post ? $post->post_title : '';
21
	}
22
23
	/**
24
	 * Get term title
25
	 *
26
	 * @param int $id
27
	 * @param string $subtype
28
	 * @return string $title The title of the item.
29
	 */
30
	public function get_term_title( $id, $subtype = '' ) {
31
		$term = get_term_by( 'id', $id, $subtype );
32
		if ( ! $term ) {
33
			return 'N/A';
34
		}
35
		return $term->name;
36
	}
37
38
	/**
39
	 * Get user title
40
	 *
41
	 * @param int $id
42
	 * @return string $title The title of the item.
43
	 */
44
	public function get_user_title( $id ) {
45
		return get_the_author_meta( 'user_login', $id );
46
	}
47
48
	/**
49
	 * Get comment title
50
	 *
51
	 * @param int $id
52
	 * @return string $title The title of the item.
53
	 */
54
	public function get_comment_title( $id ) {
55
		return get_comment_text( $id );
56
	}
57
58
	/**
59
	 * Get term object for descriptor array
60
	 *
61
	 * @param array $term_descriptor
62
	 * @return object
63
	 */
64
	public function get_term_by_descriptor( $term_descriptor ) {
65
		if ( ! is_array( $term_descriptor ) || ! isset( $term_descriptor['value'] ) || ! isset( $term_descriptor['taxonomy'] ) ) {
66
			Incorrect_Syntax_Exception::raise( 'Term descriptor passed is invalid. Please supply an array with a "value" and a "taxonomy" key: ' . print_r( $term_descriptor, true ) );
67
			return null;
68
		}
69
70
		$value = $term_descriptor['value'];
71
		$field = isset( $term_descriptor['field'] ) ? $term_descriptor['field'] : 'id';
72
		$taxonomy = $term_descriptor['taxonomy'];
73
		$term = get_term_by( $field, $value, $taxonomy );
74
75
		if ( ! $term ) {
76
			Incorrect_Syntax_Exception::raise( 'Failed to load term for descriptor: ' . print_r( $term_descriptor, true ) );
77
			return new \WP_Term( new \stdClass() );
78
		}
79
80
		return $term;
81
	}
82
83
	/**
84
	 * Decorate any term descriptor to include the full term and taxonomy objects
85
	 *
86
	 * @return mixed
87
	 */
88
	public function wildcard_term_descriptor_to_full_term_descriptor( $descriptor ) {
89
		if ( ! isset( $descriptor['field'] ) ) {
90
			$descriptor['field'] = 'id';
91
		}
92
93
		$term = $this->get_term_by_descriptor( $descriptor );
94
		$descriptor['term_object'] = $term;
95
		$descriptor['taxonomy_object'] = get_taxonomy( $term->taxonomy );
96
		return $descriptor;
97
	}
98
}
99