/** * SyndicatedPost::validate_post_id() * * @param array $dbpost An array representing the post we attempted to insert or update * @param mixed $ns A string or array representing the namespace (class, method) whence this method was called. */ function validate_post_id($dbpost, $is_update, $ns) { if (is_array($ns)) { $ns = implode('::', $ns); } else { $ns = (string) $ns; } // This should never happen. if (!is_numeric($this->_wp_id) or $this->_wp_id == 0) { $verb = $is_update ? 'update existing' : 'insert new'; $guid = $this->guid(); $url = $this->permalink(); $feed = $this->link->uri(array('add_params' => true)); // wp_insert_post failed. Diagnostics, or barf up a critical bug // notice if we are in debug mode. $mesg = "Failed to {$verb} item [{$guid}]. WordPress API returned no valid post ID.\n" . "\t\tID = " . serialize($this->_wp_id) . "\n" . "\t\tURL = " . MyPHP::val($url) . "\t\tFeed = " . MyPHP::val($feed); FeedWordPress::diagnostic('updated_feeds:errors', "WordPress API error: {$mesg}"); FeedWordPress::diagnostic('feed_items:rejected', $mesg); $mesg = <<<EOM The WordPress API returned an invalid post ID \t\t\t when FeedWordPress tried to {$verb} item {$guid} \t\t\t [URL: {$url}] \t\t\t from the feed at {$feed} {$ns}::_wp_id EOM; FeedWordPress::noncritical_bug($mesg, array("\$this->_wp_id" => $this->_wp_id, "\$dbpost" => $dbpost), __LINE__, __FILE__); } }
public function insert($tax = NULL) { $ret = NULL; if (is_null($tax)) { if (count($this->tax) > 0) { $tax = $this->tax[0]; } } 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. FeedWordPress::noncritical_bug('term insertion problem', array('term' => $this->term, 'result' => $aTerm, 'post' => $post, 'this' => $this), __LINE__, __FILE__); // Otherwise, we'll continue & return NULL... } else { $this->exists = $aTerm; $this->exists_in = $tax; $ret = $this->id(); } 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.'); } return $ret; }
/** * category_ids: look up (and create) category ids from a list of categories * * @param array $cats * @param string $unfamiliar_category * @param array|null $taxonomies * @return array */ function category_ids($cats, $unfamiliar_category = 'create', $taxonomies = NULL, $params = array()) { $singleton = isset($params['singleton']) ? $params['singleton'] : true; $allowFilters = isset($params['filters']) ? $params['filters'] : false; $catTax = 'category'; if (is_null($taxonomies)) { $taxonomies = array('category'); } // We need to normalize whitespace because (1) trailing // whitespace can cause PHP and MySQL not to see eye to eye on // VARCHAR comparisons for some versions of MySQL (cf. // <http://dev.mysql.com/doc/mysql/en/char.html>), and (2) // because I doubt most people want to make a semantic // distinction between 'Computers' and 'Computers ' $cats = array_map('trim', $cats); $terms = array(); foreach ($taxonomies as $tax) { $terms[$tax] = array(); } foreach ($cats as $cat_name) { if (preg_match('/^{([^#}]*)#([0-9]+)}$/', $cat_name, $backref)) { $cat_id = (int) $backref[2]; $tax = $backref[1]; if (strlen($tax) < 1) { $tax = $catTax; } $term = term_exists($cat_id, $tax); if (!is_wp_error($term) and !!$term) { if (!isset($terms[$tax])) { $terms[$tax] = array(); } $terms[$tax][] = $cat_id; } } elseif (strlen($cat_name) > 0) { $familiar = false; foreach ($taxonomies as $tax) { if ($tax != 'category' or strtolower($cat_name) != 'uncategorized') { $term = term_exists($cat_name, $tax); if (!is_wp_error($term) and !!$term) { $familiar = true; if (is_array($term)) { $term_id = (int) $term['term_id']; } else { $term_id = (int) $term; } if (!isset($terms[$tax])) { $terms[$tax] = array(); } $terms[$tax][] = $term_id; break; // We're done here. } } } if (!$familiar) { if ('tag' == $unfamiliar_category) { $unfamiliar_category = 'create:post_tag'; } if (preg_match('/^create(:(.*))?$/i', $unfamiliar_category, $ref)) { $tax = $catTax; // Default if (isset($ref[2]) and strlen($ref[2]) > 2) { $tax = $ref[2]; } $term = wp_insert_term($cat_name, $tax); if (is_wp_error($term)) { FeedWordPress::noncritical_bug('term insertion problem', array('cat_name' => $cat_name, 'term' => $term, 'this' => $this), __LINE__, __FILE__); } else { if (!isset($terms[$tax])) { $terms[$tax] = array(); } $terms[$tax][] = (int) $term['term_id']; } } } } } $filtersOn = $allowFilters; if ($allowFilters) { $filters = array_filter($this->link->setting('match/filter', 'match_filter', array()), 'remove_dummy_zero'); $filtersOn = ($filtersOn and is_array($filters) and count($filters) > 0); } // Check for filter conditions foreach ($terms as $tax => $term_ids) { if ($filtersOn and count($term_ids) == 0 and in_array($tax, $filters)) { $terms = NULL; // Drop the post break; } else { $terms[$tax] = array_unique($term_ids); } } if ($singleton and count($terms) == 1) { // If we only searched one, just return the term IDs $terms = end($terms); } return $terms; }
function category_ids($cats, $unfamiliar_category = 'create', $tags_too = false) { global $wpdb; // We need to normalize whitespace because (1) trailing // whitespace can cause PHP and MySQL not to see eye to eye on // VARCHAR comparisons for some versions of MySQL (cf. // <http://dev.mysql.com/doc/mysql/en/char.html>), and (2) // because I doubt most people want to make a semantic // distinction between 'Computers' and 'Computers ' $cats = array_map('trim', $cats); $tags = array(); $cat_ids = array(); foreach ($cats as $cat_name) { if (preg_match('/^{#([0-9]+)}$/', $cat_name, $backref)) { $cat_id = (int) $backref[1]; if (function_exists('is_term') and is_term($cat_id, 'category')) { $cat_ids[] = $cat_id; } elseif (get_category($cat_id)) { $cat_ids[] = $cat_id; } } elseif (strlen($cat_name) > 0) { $esc = $wpdb->escape($cat_name); $resc = $wpdb->escape(preg_quote($cat_name)); // WordPress 2.3+ if (function_exists('is_term')) { $cat_id = is_term($cat_name, 'category'); if ($cat_id) { $cat_ids[] = $cat_id['term_id']; // There must be a better way to do this... } elseif ($results = $wpdb->get_results("SELECT\tterm_id\n\t\t\t\t\t\tFROM {$wpdb->term_taxonomy}\n\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\tLOWER(description) RLIKE\n\t\t\t\t\t\t\tCONCAT('(^|\\n)a\\.?k\\.?a\\.?( |\\t)*:?( |\\t)*', LOWER('{$resc}'), '( |\\t|\\r)*(\\n|\$)')")) { foreach ($results as $term) { $cat_ids[] = (int) $term->term_id; } } elseif ('tag' == $unfamiliar_category) { $tags[] = $cat_name; } elseif ('create' === $unfamiliar_category) { $term = wp_insert_term($cat_name, 'category'); if (is_wp_error($term)) { FeedWordPress::noncritical_bug('term insertion problem', array('cat_name' => $cat_name, 'term' => $term, 'this' => $this), __LINE__); } else { $cat_ids[] = $term['term_id']; } } // WordPress 1.5.x - 2.2.x } else { $results = $wpdb->get_results("SELECT cat_ID\n\t\t\t\t\tFROM {$wpdb->categories}\n\t\t\t\t\tWHERE\n\t\t\t\t\t (LOWER(cat_name) = LOWER('{$esc}'))\n\t\t\t\t\t OR (LOWER(category_description)\n\t\t\t\t\t RLIKE CONCAT('(^|\\n)a\\.?k\\.?a\\.?( |\\t)*:?( |\\t)*', LOWER('{$resc}'), '( |\\t|\\r)*(\\n|\$)'))\n\t\t\t\t\t"); if ($results) { foreach ($results as $term) { $cat_ids[] = (int) $term->cat_ID; } } elseif ('create' === $unfamiliar_category) { if (function_exists('wp_insert_category')) { $cat_id = wp_insert_category(array('cat_name' => $cat_name)); // And into the database we go. } else { $nice_kitty = sanitize_title($cat_name); $wpdb->query(sprintf("\n\t\t\t\t\t\t\t\tINSERT INTO {$wpdb->categories}\n\t\t\t\t\t\t\t\tSET\n\t\t\t\t\t\t\t\t cat_name='%s',\n\t\t\t\t\t\t\t\t category_nicename='%s'\n\t\t\t\t\t\t\t\t", $wpdb->escape($cat_name), $nice_kitty)); $cat_id = $wpdb->insert_id; } $cat_ids[] = $cat_id; } } } } if (count($cat_ids) == 0 and $unfamiliar_category === 'filter') { $cat_ids = NULL; // Drop the post } else { $cat_ids = array_unique($cat_ids); } if ($tags_too) { $ret = array($cat_ids, $tags); } else { $ret = $cat_ids; } return $ret; }