PluginProbe
FeedWordPress / 2020.0118
FeedWordPress v2020.0118
trunk 0.8 0.9 0.91 0.95 0.96 0.97 0.98 0.981 0.99 0.991 0.992 0.993 2008.1030 2008.1101 2008.1105 2008.1214 2009.0612 2009.0613 2009.0618 2009.0707 2009.1111 2009.1112 2010.0127 2010.0528 All 65 releases
feedwordpress / syndicatedpostterm.class.php

syndicatedpostterm.class.php in FeedWordPress 2020.0118, at syndicatedpostterm.class.php

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