/** * Register metabox for suggest tags, for post, and optionnaly page. * * @return void * @author Amaury Balmer */ public static function admin_menu() { add_meta_box('suggestedtags', __('Suggested tags', 'simpletags'), array(__CLASS__, 'metabox'), 'post', 'advanced', 'core'); if (is_page_have_tags()) { add_meta_box('suggestedtags', __('Suggested tags', 'simpletags'), array(__CLASS__, 'metabox'), 'page', 'advanced', 'core'); } }
/** * Register metabox * * @return void * @author Amaury Balmer */ function helperClickTags() { add_meta_box('st-clicks-tags', __('Click tags', 'simpletags'), array(&$this, 'boxClickTags'), 'post', 'advanced', 'core'); if (is_page_have_tags()) { add_meta_box('st-clicks-tags', __('Click tags', 'simpletags'), array(&$this, 'boxClickTags'), 'page', 'advanced', 'core'); } }
/** * Register metabox for suggest tags, for post, and optionnaly page. * * @return void * @author Amaury Balmer */ function helperSuggestTags() { add_meta_box('suggestedtags', __('Suggested tags', 'simpletags'), array(&$this, 'boxSuggestTags'), 'post', 'advanced', 'core'); if (is_page_have_tags()) { add_meta_box('suggestedtags', __('Suggested tags', 'simpletags'), array(&$this, 'boxSuggestTags'), 'page', 'advanced', 'core'); } }
function SimpleTags_Admin_Suggest() { global $pagenow; // Ajax action, JS Helper and admin action add_action('wp_ajax_' . 'simpletags', array(&$this, 'ajaxCheck')); // Box for post add_action('admin_menu', array(&$this, 'helperSuggestTags_Post'), 1); // Box for Page if (is_page_have_tags()) { add_action('admin_menu', array(&$this, 'helperSuggestTags_Page'), 1); } wp_register_script('st-helper-suggested-tags', STAGS_URL . '/inc/js/helper-suggested-tags.min.js', array('jquery', 'st-helper-add-tags'), STAGS_VERSION); wp_localize_script('st-helper-suggested-tags', 'stHelperSuggestedTagsL10n', array('title_bloc' => $this->getSuggestTagsTitle(), 'content_bloc' => __('Choose a provider to get suggested tags (local, yahoo or tag the net).', 'simpletags'))); // Register location $wp_post_pages = array('post.php', 'post-new.php'); $wp_page_pages = array('page.php', 'page-new.php'); // Helper for posts/pages if (in_array($pagenow, $wp_post_pages) || in_array($pagenow, $wp_page_pages) && is_page_have_tags()) { wp_enqueue_script('st-helper-suggested-tags'); } }
/** * Extended get_terms public static function support * - Limit category * - Limit days * - Selection restrict * - Min usage * * @param string|array $taxonomies * @param string $args * @return array */ public static function getTerms($taxonomies, $args = '') { global $wpdb; $empty_array = array(); $join_relation = false; $single_taxonomy = false; if (!is_array($taxonomies)) { $single_taxonomy = true; $taxonomies = array($taxonomies); } foreach ((array) $taxonomies as $taxonomy) { if (!taxonomy_exists($taxonomy)) { $error = new WP_Error('invalid_taxonomy', __('Invalid Taxonomy')); return $error; } } $in_taxonomies = "'" . implode("', '", $taxonomies) . "'"; $defaults = array('orderby' => 'name', 'order' => 'ASC', 'hide_empty' => true, 'exclude' => array(), 'exclude_tree' => array(), 'include' => array(), 'number' => '', 'fields' => 'all', 'slug' => '', 'parent' => '', 'hierarchical' => true, 'child_of' => 0, 'get' => '', 'name__like' => '', 'pad_counts' => false, 'offset' => '', 'search' => '', 'limit_days' => 0, 'category' => 0, 'min_usage' => 0, 'st_name__like' => ''); $args = wp_parse_args($args, $defaults); // Translate selection order $args['orderby'] = self::compatOldOrder($args['selectionby'], 'orderby'); $args['order'] = self::compatOldOrder($args['selection'], 'order'); $args['number'] = absint($args['number']); $args['offset'] = absint($args['offset']); $args['limit_days'] = absint($args['limit_days']); $args['min_usage'] = absint($args['min_usage']); if (!$single_taxonomy || !is_taxonomy_hierarchical($taxonomies[0]) || '' !== $args['parent']) { $args['child_of'] = 0; $args['hierarchical'] = false; $args['pad_counts'] = false; } if ('all' == $args['get']) { $args['child_of'] = 0; $args['hide_empty'] = 0; $args['hierarchical'] = false; $args['pad_counts'] = false; } extract($args, EXTR_SKIP); if ($child_of) { $hierarchy = _get_term_hierarchy($taxonomies[0]); if (!isset($hierarchy[$child_of])) { return $empty_array; } } if ($parent) { $hierarchy = _get_term_hierarchy($taxonomies[0]); if (!isset($hierarchy[$parent])) { return $empty_array; } } // $args can be whatever, only use the args defined in defaults to compute the key $filter_key = has_filter('list_terms_exclusions') ? serialize($GLOBALS['wp_filter']['list_terms_exclusions']) : ''; $key = md5(serialize(compact(array_keys($defaults))) . serialize($taxonomies) . $filter_key); $last_changed = wp_cache_get('last_changed', 's-terms'); if (!$last_changed) { $last_changed = time(); wp_cache_set('last_changed', $last_changed, 's-terms'); } $cache_key = "get_terms:{$key}:{$last_changed}"; $cache = wp_cache_get($cache_key, 's-terms'); if (false !== $cache) { $cache = apply_filters('get_terms', $cache, $taxonomies, $args); return $cache; } $_orderby = strtolower($orderby); if ('count' == $_orderby) { $orderby = 'tt.count'; } if ('random' == $_orderby) { $orderby = 'RAND()'; } else { if ('name' == $_orderby) { $orderby = 't.name'; } else { if ('slug' == $_orderby) { $orderby = 't.slug'; } else { if ('term_group' == $_orderby) { $orderby = 't.term_group'; } elseif (empty($_orderby) || 'id' == $_orderby) { $orderby = 't.term_id'; } } } } $orderby = apply_filters('get_terms_orderby', $orderby, $args); if (!empty($orderby)) { $orderby = "ORDER BY {$orderby}"; } else { $order = ''; } $where = ''; $inclusions = ''; if (!empty($include)) { $exclude = ''; $exclude_tree = ''; $interms = wp_parse_id_list($include); foreach ($interms as $interm) { if (empty($inclusions)) { $inclusions = ' AND ( t.term_id = ' . intval($interm) . ' '; } else { $inclusions .= ' OR t.term_id = ' . intval($interm) . ' '; } } } if (!empty($inclusions)) { $inclusions .= ')'; } $where .= $inclusions; $exclusions = ''; if (!empty($exclude_tree)) { $excluded_trunks = wp_parse_id_list($exclude_tree); foreach ($excluded_trunks as $extrunk) { $excluded_children = (array) get_terms($taxonomies[0], array('child_of' => intval($extrunk), 'fields' => 'ids')); $excluded_children[] = $extrunk; foreach ($excluded_children as $exterm) { if (empty($exclusions)) { $exclusions = ' AND ( t.term_id <> ' . intval($exterm) . ' '; } else { $exclusions .= ' AND t.term_id <> ' . intval($exterm) . ' '; } } } } if (!empty($exclude)) { $exterms = wp_parse_id_list($exclude); foreach ($exterms as $exterm) { if (empty($exclusions)) { $exclusions = ' AND ( t.term_id <> ' . intval($exterm) . ' '; } else { $exclusions .= ' AND t.term_id <> ' . intval($exterm) . ' '; } } } if (!empty($exclusions)) { $exclusions .= ')'; } $exclusions = apply_filters('list_terms_exclusions', $exclusions, $args); $where .= $exclusions; // ST Features : Restrict category if ($category != 0) { if (!is_array($taxonomies)) { $taxonomies = array($taxonomies); } $incategories = wp_parse_id_list($category); $taxonomies = "'" . implode("', '", $taxonomies) . "'"; $incategories = "'" . implode("', '", $incategories) . "'"; $where .= " AND tr.object_id IN ( "; $where .= "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 INNER JOIN {$wpdb->posts} as p ON tr.object_id=p.ID WHERE tt.term_id IN ({$incategories}) AND p.post_status='publish'"; $where .= " ) "; $join_relation = true; unset($incategories, $category); } // ST Features : Limit posts date if ($limit_days != 0) { $where .= " AND tr.object_id IN ( "; $where .= "SELECT DISTINCT ID FROM {$wpdb->posts} AS p WHERE p.post_status='publish' AND " . (is_page_have_tags() ? "p.post_type IN('page', 'post')" : "post_type = 'post'") . " AND p.post_date_gmt > '" . date('Y-m-d H:i:s', time() - $limit_days * 86400) . "'"; $where .= " ) "; $join_relation = true; unset($limit_days); } if (!empty($slug)) { $slug = sanitize_title($slug); $where .= " AND t.slug = '{$slug}'"; } if (!empty($name__like)) { $where .= " AND t.name LIKE '{$name__like}%'"; } if ('' !== $parent) { $parent = (int) $parent; $where .= " AND tt.parent = '{$parent}'"; } // ST Features : Another way to search if (strpos($st_name__like, ' ') !== false) { $st_terms_formatted = array(); $st_terms = preg_split('/[\\s,]+/', $st_name_like); foreach ((array) $st_terms as $st_term) { if (empty($st_term)) { continue; } $st_terms_formatted[] = "t.name LIKE '%" . like_escape($st_term) . "%'"; } $where .= " AND ( " . explode(' OR ', $st_terms_formatted) . " ) "; unset($st_term, $st_terms_formatted, $st_terms); } elseif (!empty($st_name__like)) { $where .= " AND t.name LIKE '%{$st_name__like}%'"; } // ST Features : Add min usage if ($hide_empty && !$hierarchical) { if ($min_usage == 0) { $where .= ' AND tt.count > 0'; } else { $where .= $wpdb->prepare(' AND tt.count >= %d', $min_usage); } } // don't limit the query results when we have to descend the family tree if (!empty($number) && !$hierarchical && empty($child_of) && '' === $parent) { if ($offset) { $limit = 'LIMIT ' . $offset . ',' . $number; } else { $limit = 'LIMIT ' . $number; } } else { $limit = ''; } if (!empty($search)) { $search = like_escape($search); $where .= " AND (t.name LIKE '%{$search}%')"; } $selects = array(); switch ($fields) { case 'all': $selects = array('t.*', 'tt.*'); break; case 'ids': case 'id=>parent': $selects = array('t.term_id', 'tt.parent', 'tt.count'); break; case 'names': $selects = array('t.term_id', 'tt.parent', 'tt.count', 't.name'); break; case 'count': $orderby = ''; $order = ''; $selects = array('COUNT(*)'); } $select_this = implode(', ', apply_filters('get_terms_fields', $selects, $args)); // Add inner to relation table ? $join_relation = $join_relation == false ? '' : "INNER JOIN {$wpdb->term_relationships} AS tr ON tt.term_taxonomy_id = tr.term_taxonomy_id"; $query = "SELECT {$select_this}\r\n\t\t\tFROM {$wpdb->terms} AS t\r\n\t\t\tINNER JOIN {$wpdb->term_taxonomy} AS tt ON t.term_id = tt.term_id\r\n\t\t\t{$join_relation}\r\n\t\t\tWHERE tt.taxonomy IN ({$in_taxonomies})\r\n\t\t\t{$where}\r\n\t\t\t{$orderby} {$order}\r\n\t\t\t{$limit}"; // GROUP BY t.term_id if ('count' == $fields) { $term_count = $wpdb->get_var($query); return $term_count; } $terms = $wpdb->get_results($query); if ('all' == $fields) { update_term_cache($terms); } if (empty($terms)) { wp_cache_add($cache_key, array(), 's-terms'); $terms = apply_filters('get_terms', array(), $taxonomies, $args); return $terms; } if ($child_of) { $children = _get_term_hierarchy($taxonomies[0]); if (!empty($children)) { $terms =& _get_term_children($child_of, $terms, $taxonomies[0]); } } // Update term counts to include children. if ($pad_counts && 'all' == $fields) { _pad_term_counts($terms, $taxonomies[0]); } // Make sure we show empty categories that have children. if ($hierarchical && $hide_empty && is_array($terms)) { foreach ($terms as $k => $term) { if (!$term->count) { $children = _get_term_children($term->term_id, $terms, $taxonomies[0]); if (is_array($children)) { foreach ($children as $child) { if ($child->count) { continue 2; } } } // It really is empty unset($terms[$k]); } } } reset($terms); $_terms = array(); if ('id=>parent' == $fields) { while ($term = array_shift($terms)) { $_terms[$term->term_id] = $term->parent; } $terms = $_terms; } elseif ('ids' == $fields) { while ($term = array_shift($terms)) { $_terms[] = $term->term_id; } $terms = $_terms; } elseif ('names' == $fields) { while ($term = array_shift($terms)) { $_terms[] = $term->name; } $terms = $_terms; } if (0 < $number && intval(@count($terms)) > $number) { $terms = array_slice($terms, $offset, $number); } wp_cache_add($cache_key, $terms, 's-terms'); $terms = apply_filters('get_terms', $terms, $taxonomies, $args); return $terms; }
/** * Call meta box public static function for taxonomy tags for each CPT * * @param string $post_type * @return boolean * @author Amaury Balmer */ public static function add_meta_boxes($post_type) { $taxonomies = get_object_taxonomies($post_type); if (in_array('post_tag', $taxonomies)) { if ($post_type == 'page' && !is_page_have_tags()) { return false; } remove_meta_box('post_tag' . 'div', $post_type, 'side'); remove_meta_box('tagsdiv-' . 'post_tag', $post_type, 'side'); add_meta_box('adv-tagsdiv', __('Tags (Simple Tags)', 'simpletags'), array(__CLASS__, 'metabox'), $post_type, 'side', 'core', array('taxonomy' => 'post_tag')); return true; } return false; }
/** * Method for add terms for all or specified posts * * @param string $taxonomy * @param string $match * @param string $new * @return boolean * @author Amaury Balmer */ function addMatchTerms($taxonomy = 'post_tag', $match, $new) { if (trim(str_replace(',', '', stripslashes($new))) == '') { $this->message = __('No new term(s) specified!', 'simpletags'); $this->status = 'error'; return false; } $match_terms = explode(',', $match); $new_terms = explode(',', $new); $match_terms = array_filter($match_terms, '_delete_empty_element'); $new_terms = array_filter($new_terms, '_delete_empty_element'); $counter = 0; if (!empty($match_terms)) { // Match and add // Get terms ID from old match names $terms_id = array(); foreach ((array) $match_terms as $match_term) { $term = get_term_by('name', $match_term, $taxonomy); $terms_id[] = (int) $term->term_id; } // Get object ID with terms ID $objects_id = get_objects_in_term($terms_id, $taxonomy, array('fields' => 'all_with_object_id')); // Add new tags for specified post foreach ((array) $objects_id as $object_id) { wp_set_object_terms($object_id, $new_terms, $taxonomy, true); // Append terms $counter++; } // Clean cache clean_object_term_cache($objects_id, $taxonomy); clean_term_cache($terms_id, $taxonomy); } else { // Add for all posts // Page or not ? $post_type_sql = is_page_have_tags() ? "post_type IN('page', 'post')" : "post_type = 'post'"; // TODO, CPT // Get all posts ID global $wpdb; $objects_id = $wpdb->get_col("SELECT ID FROM {$wpdb->posts} WHERE {$post_type_sql}"); // Add new tags for all posts foreach ((array) $objects_id as $object_id) { wp_set_object_terms($object_id, $new_terms, $taxonomy, true); // Append terms $counter++; } // Clean cache clean_object_term_cache($objects_id, $taxonomy); } if ($counter == 0) { $this->message = __('No term added.', 'simpletags'); } else { $this->message = sprintf(__('Term(s) added to %1s post(s).', 'simpletags'), $counter); } return true; }
/** * Call meta box function for taxonomy tags for each CPT * * @param string $post_type * @return boolean * @author Amaury Balmer */ function registerMetaBox($post_type) { $taxonomies = get_object_taxonomies($post_type); if (in_array('post_tag', $taxonomies)) { if ($post_type == 'page' && !is_page_have_tags()) { return false; } remove_meta_box('post_tag' . 'div', $post_type, 'side'); remove_meta_box('tagsdiv-' . 'post_tag', $post_type, 'side'); add_meta_box('adv-tagsdiv', __('Tags (Simple Tags)', 'simpletags'), array(&$this, 'boxTags'), $post_type, 'side', 'core', array('taxonomy' => 'post_tag')); return true; } return false; }
/** * Init somes JS and CSS need for simple tags. * * @return void * @author Amaury Balmer */ public static function admin_enqueue_scripts() { global $pagenow; // Helper simple tags wp_register_script('st-helper-add-tags', STAGS_URL . '/assets/js/helper-add-tags.js', array('jquery'), STAGS_VERSION); wp_register_script('st-helper-options', STAGS_URL . '/assets/js/helper-options.js', array('jquery'), STAGS_VERSION); // Register CSS wp_register_style('st-admin', STAGS_URL . '/assets/css/admin.css', array(), STAGS_VERSION, 'all'); // Register location $wp_post_pages = array('post.php', 'post-new.php'); $wp_page_pages = array('page.php', 'page-new.php'); // Common Helper for Post, Page and Plugin Page if (in_array($pagenow, $wp_post_pages) || in_array($pagenow, $wp_page_pages) && is_page_have_tags() || isset($_GET['page']) && in_array($_GET['page'], array('st_mass_terms', 'st_auto', 'st_options', 'st_manage'))) { wp_enqueue_style('st-admin'); } // add jQuery tabs for options page. Use jQuery UI Tabs from WP if (isset($_GET['page']) && $_GET['page'] == 'st_options') { wp_enqueue_script('jquery-ui-tabs'); wp_enqueue_script('st-helper-options'); } }