-
Notifications
You must be signed in to change notification settings - Fork 63
/
syndicatedpostterm.class.php
218 lines (163 loc) · 5.25 KB
/
syndicatedpostterm.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
class SyndicatedPostTerm {
private $term, $tax, $exists, $exists_in, $post;
public function __construct ($term, $taxonomies, $post) {
$catTax = 'category';
$this->post = $post;
// Default to these
$this->term = NULL;
$this->tax = NULL;
$this->exists = NULL;
$this->exists_in = NULL;
if (preg_match('/^{([^#}]*)#([0-9]+)}$/', $term, $backref)) :
$cat_id = (int) $backref[2];
$tax = $backref[1];
if (strlen($tax) < 1) :
$tax = $catTax;
endif;
$aTerm = get_term($cat_id, $tax);
if ( !is_wp_error($aTerm) and !! $aTerm) :
$this->exists = (array) $aTerm;
$this->exists_in = $this->exists['taxonomy'];
$this->term = $this->exists['name'];
$this->tax = array($this->exists['taxonomy']);
endif;
else :
$this->term = $term;
$this->tax = $taxonomies;
// Leave exists/exists_in empty until we search()
endif;
if (is_null($this->tax)) :
$this->tax = array($catTax);
endif;
} /* SyndicatedPostTerm constructor */
public function is_familiar () {
$ex = $this->exists;
if (is_null($this->exists)) :
$ex = $this->search();
endif;
FeedWordPress::diagnostic(
'syndicated_posts:categories',
'Assigned category '.json_encode($this->term)
.' by feed; checking '.json_encode($this->tax)
. ' with result: '.json_encode($ex)
);
return ( !is_wp_error($ex) and !! $ex);
} /* SyndicatedPostTerm::familiar () */
protected function search () {
// Initialize
$found = null;
// Either this is a numbered term code, which supplies the ID
// and the taxonomy explicitly (e.g.: {category#2}; in which
// case we have set $this->tax to a unit array containing only
// the correct taxonomy, or else we have a term name and an
// ordered array of taxonomies to search for that term name. In
// either case, loop through and check each pair of term
foreach ($this->tax as $tax) :
if ( ! $this->is_forbidden_in($tax)) :
$found = $this->fetch_record_in($tax);
if ($found) :
// When TRUE, the term has been found
// and is now stored in $this->exists
// Save the taxonomy we found this in.
$this->exists_in = $tax;
break;
endif;
endif;
endforeach;
FeedWordPress::diagnostic(
'syndicated_posts:categories:test',
'CHECKED familiarity of term '
.json_encode($this->term)
.' across '.json_encode($this->tax)
. ' with result: '.json_encode($found)
);
return $this->exists;
} /* SyndicatedPostTerm::search () */
protected function fetch_record_in ($tax) {
$record = term_exists($this->term, $tax);
FeedWordPress::diagnostic(
'syndicated_posts:categories:test',
'CHECK existence of '.$tax.': '
.json_encode($this->term)
.' with result: '.json_encode($record)
);
$found = ( !is_wp_error($record) and !! $record);
if ($found) :
$this->exists = $record;
endif;
return $found;
} /* SyndicatedPostTerm::fetch_record_in() */
public function is_forbidden_in ($tax = NULL) {
$forbid = false; // Innocent until proven guilty.
$term = $this->term;
if (is_null($tax) and (count($this->tax) > 0)) :
$tax = $this->tax[0];
endif;
if ($tax=='category' and strtolower($term)=='uncategorized') :
$forbid = true;
endif;
// Run it through a filter.
return apply_filters('syndicated_post_forbidden_term', $forbid, $term, $tax, $this->post);
} /* SyndicatedPostTerm::is_forbidden_in () */
public function taxonomy () {
if (is_null($this->exists_in)) :
$this->search();
endif;
return $this->exists_in;
} /* SyndicatedPostTerm::taxonomy () */
public function id () {
$term_id = NULL;
if (is_null($this->exists)) :
$this->search();
endif;
$term = $this->exists;
if (is_array($term) or is_object($term)) :
// For hash tables of any sort, use the term_id member
$term = (array) $term;
$term_id = intval($term['term_id']);
elseif (is_numeric($term)) :
// For a straight numeric response, just return number
$term_id = intval($term);
endif;
return $term_id;
} /* SyndicatedPostTerm::id () */
public function insert ($tax = NULL) {
$ret = NULL;
if (is_null($tax)) :
if (count($this->tax) > 0) :
$tax = $this->tax[0];
endif;
endif;
if ( ! $this->is_forbidden_in($tax)) :
$aTerm = wp_insert_term($this->term, $tax);
if (is_wp_error($aTerm)) :
// If debug mode is ON, this will halt us here.
FeedWordPressDiagnostic::noncritical_bug(
'term insertion problem', array(
'term' => $this->term,
'result' => $aTerm,
'post' => $this->post,
'this' => $this
), __LINE__, __FILE__
);
// Otherwise, we'll continue & return NULL...
else :
$this->exists = $aTerm;
$this->exists_in = $tax;
$ret = $this->id();
endif;
FeedWordPress::diagnostic(
'syndicated_posts:categories',
'CREATED unfamiliar '.$tax.': '.json_encode($this->term).' with result: '.json_encode($aTerm)
);
else :
FeedWordPress::diagnostic(
'syndicated_posts:categories',
'Category: DID NOT CREATE unfamiliar '.$tax.': '.json_encode($this->term).':'
.' that '.$tax.' name is filtered out.'
);
endif;
return $ret;
} /* SyndicatedPostTerm::insert () */
} /* class SyndicatedPostTerm */