PluginProbe
FeedWordPress / 2014.0805
FeedWordPress v2014.0805
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 2014.0805, at syndicatedpostterm.class.php

216 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 // Either this is a numbered term code, which supplies the ID
63 // and the taxonomy explicitly (e.g.: {category#2}; in which
64 // case we have set $this->tax to a unit array containing only
65 // the correct taxonomy, or else we have a term name and an
66 // ordered array of taxonomies to search for that term name. In
67 // either case, loop through and check each pair of term
68
69 foreach ($this->tax as $tax) :
70
71 if (!$this->is_forbidden_in($tax)) :
72
73 $found = $this->fetch_record_in($tax);
74 if ($found) :
75 // When TRUE, the term has been found
76 // and is now stored in $this->exists
77
78 // Save the taxonomy we found this in.
79 $this->exists_in = $tax;
80
81 break;
82 endif;
83
84 endif;
85
86 endforeach;
87
88 FeedWordPress::diagnostic(
89 'syndicated_posts:categories:test',
90 'CHECKED familiarity of term '
91 .json_encode($this->term)
92 .' across '.json_encode($this->tax)
93 . ' with result: '.json_encode($record)
94 );
95
96 return $this->exists;
97
98 } /* SyndicatedPostTerm::search () */
99
100 protected function fetch_record_in ($tax) {
101 $record = term_exists($this->term, $tax);
102
103 FeedWordPress::diagnostic(
104 'syndicated_posts:categories:test',
105 'CHECK existence of '.$tax.': '
106 .json_encode($this->term)
107 .' with result: '.json_encode($record)
108 );
109
110 $found = (!is_wp_error($record) and !!$record);
111
112 if ($found) :
113 $this->exists = $record;
114 endif;
115
116 return $found;
117 } /* SyndicatedPostTerm::fetch_record_in() */
118
119 public function is_forbidden_in ($tax = NULL) {
120 $forbid = false; // Innocent until proven guilty.
121
122 $term = $this->term;
123 if (is_null($tax) and (count($this->tax) > 0)) :
124 $tax = $this->tax[0];
125 endif;
126
127 if ($tax=='category' and strtolower($term)=='uncategorized') :
128 $forbid = true;
129 endif;
130
131 // Run it through a filter.
132 return apply_filters('syndicated_post_forbidden_term', $forbid, $term, $tax, $this->post);
133 } /* SyndicatedPostTerm::is_forbidden_in () */
134
135 public function taxonomy () {
136 if (is_null($this->exists_in)) :
137 $this->search();
138 endif;
139
140 return $this->exists_in;
141 } /* SyndicatedPostTerm::taxonomy () */
142
143 public function id () {
144 $term_id = NULL;
145
146 if (is_null($this->exists)) :
147 $this->search();
148 endif;
149
150 $term = $this->exists;
151 if (is_array($term) or is_object($term)) :
152
153 // For hash tables of any sort, use the term_id member
154 $term = (array) $term;
155 $term_id = intval($term['term_id']);
156
157 elseif (is_numeric($term)) :
158
159 // For a straight numeric response, just return number
160 $term_id = intval($term);
161
162 endif;
163
164 return $term_id;
165 } /* SyndicatedPostTerm::id () */
166
167 public function insert ($tax = NULL) {
168 $ret = NULL;
169
170 if (is_null($tax)) :
171 if (count($this->tax) > 0) :
172 $tax = $this->tax[0];
173 endif;
174 endif;
175
176 if (!$this->is_forbidden_in($tax)) :
177 $aTerm = wp_insert_term($this->term, $tax);
178 if (is_wp_error($aTerm)) :
179
180 // If debug mode is ON, this will halt us here.
181 FeedWordPress::noncritical_bug(
182 'term insertion problem', array(
183 'term' => $this->term,
184 'result' => $aTerm,
185 'post' => $post,
186 'this' => $this
187 ), __LINE__, __FILE__
188 );
189
190 // Otherwise, we'll continue & return NULL...
191
192 else :
193 $this->exists = $aTerm;
194 $this->exists_in = $tax;
195
196 $ret = $this->id();
197 endif;
198
199 FeedWordPress::diagnostic(
200 'syndicated_posts:categories',
201 'CREATED unfamiliar '.$tax.': '.json_encode($this->term).' with result: '.json_encode($aTerm)
202 );
203 else :
204 FeedWordPress::diagnostic(
205 'syndicated_posts:categories',
206 'Category: DID NOT CREATE unfamiliar '.$tax.': '.json_encode($this->term).':'
207 .' that '.$tax.' name is filtered out.'
208 );
209 endif;
210
211 return $ret;
212 } /* SyndicatedPostTerm::insert () */
213
214 } /* class SyndicatedPostTerm */
215
216