1 | <?php |
---|
2 | |
---|
3 | error_reporting(E_ALL); |
---|
4 | |
---|
5 | class WP_Categories_to_Tags { |
---|
6 | var $categories_to_convert = array(); |
---|
7 | var $all_categories = array(); |
---|
8 | |
---|
9 | function header() { |
---|
10 | print '<div class="wrap">'; |
---|
11 | print '<h2>' . __('Convert Categories to Tags') . '</h2>'; |
---|
12 | } |
---|
13 | |
---|
14 | function footer() { |
---|
15 | print '</div>'; |
---|
16 | } |
---|
17 | |
---|
18 | function populate_all_categories() { |
---|
19 | $this->all_categories =& get_categories('hide_empty=0&hierarchal=0'); |
---|
20 | } |
---|
21 | |
---|
22 | function welcome() { |
---|
23 | print '<div class="narrow">'; |
---|
24 | print '<p>' . __('Howdy! This converter allows you to selectively convert existing categories to tags. To get started, check the checkboxes of the categories you wish to be converted, then click the Convert button.') . '</p>'; |
---|
25 | print '<p>' . __('Keep in mind that if you convert a category with child categories, those child categories get their parent setting removed, so they\'re in the root.') . '</p>'; |
---|
26 | |
---|
27 | $this->categories_form(); |
---|
28 | |
---|
29 | print '</div>'; |
---|
30 | } |
---|
31 | |
---|
32 | function categories_form() { |
---|
33 | $this->populate_all_categories(); |
---|
34 | |
---|
35 | print '<form action="admin.php?import=wp-cat2tag&step=2" method="post">'; |
---|
36 | print '<ul style="list-style:none">'; |
---|
37 | |
---|
38 | $hier = _get_category_hierarchy(); |
---|
39 | |
---|
40 | foreach ($this->all_categories as $category) { |
---|
41 | if ((int) $category->category_parent == 0) { |
---|
42 | print '<li><label><input type="checkbox" name="cats_to_convert[]" value="' . intval($category->cat_ID) . '" /> ' . $category->cat_name . ' (' . $category->category_count . ')</label>'; |
---|
43 | |
---|
44 | if (isset($hier[$category->cat_ID])) { |
---|
45 | $this->_category_children($category, $hier); |
---|
46 | } |
---|
47 | |
---|
48 | print '</li>'; |
---|
49 | } |
---|
50 | } |
---|
51 | |
---|
52 | print '</ul>'; |
---|
53 | print '<p class="submit"><input type="submit" name="submit" value="' . __('Convert »') . '" /></p>'; |
---|
54 | print '</form>'; |
---|
55 | } |
---|
56 | |
---|
57 | function _category_children($parent, $hier) { |
---|
58 | print '<ul style="list-style:none">'; |
---|
59 | |
---|
60 | foreach ($hier[$parent->cat_ID] as $child_id) { |
---|
61 | $child =& get_category($child_id); |
---|
62 | |
---|
63 | print '<li><label><input type="checkbox" name="cats_to_convert[]" value="' . intval($child->cat_ID) . '" /> ' . $child->cat_name . ' (' . $child->category_count . ')</label>'; |
---|
64 | |
---|
65 | if (isset($hier[$child->cat_ID])) { |
---|
66 | $this->_category_children($child, $hier); |
---|
67 | } |
---|
68 | |
---|
69 | print '</li>'; |
---|
70 | } |
---|
71 | |
---|
72 | print '</ul>'; |
---|
73 | } |
---|
74 | |
---|
75 | function convert_them() { |
---|
76 | global $wpdb; |
---|
77 | |
---|
78 | if (!isset($_POST['cats_to_convert']) || !is_array($_POST['cats_to_convert'])) { |
---|
79 | print '<div class="narrow">'; |
---|
80 | print '<p>' . sprintf(__('Uh, oh. Something didn\'t work. Please <a href="%s">try again</a>.'), 'admin.php?import=wp-cat2tag') . '</p>'; |
---|
81 | print '</div>'; |
---|
82 | } |
---|
83 | |
---|
84 | $this->categories_to_convert = $_POST['cats_to_convert']; |
---|
85 | $hier = _get_category_hierarchy(); |
---|
86 | |
---|
87 | print '<ul>'; |
---|
88 | |
---|
89 | foreach ($this->categories_to_convert as $cat_id) { |
---|
90 | $cat_id = (int) $cat_id; |
---|
91 | |
---|
92 | print '<li>' . __('Converting category') . ' #' . $cat_id . '... '; |
---|
93 | |
---|
94 | if (!category_exists($cat_id)) { |
---|
95 | _e('Category doesn\'t exist!'); |
---|
96 | } else { |
---|
97 | $category =& get_category($cat_id); |
---|
98 | |
---|
99 | if ($category->link_count > 0) { |
---|
100 | $type = $category->type | TAXONOMY_TAG; |
---|
101 | } else { |
---|
102 | $type = TAXONOMY_TAG; |
---|
103 | } |
---|
104 | |
---|
105 | $wpdb->query("UPDATE $wpdb->categories SET type = '$type' WHERE cat_ID = '{$category->cat_ID}'"); |
---|
106 | |
---|
107 | $wpdb->query("UPDATE $wpdb->post2cat SET rel_type = 'tag' WHERE cat_ID = '{$category->cat_ID}'"); |
---|
108 | $wpdb->query("UPDATE $wpdb->categories SET tag_count = '{$category->category_count}', category_count = '0' WHERE cat_ID = '{$category->cat_ID}'"); |
---|
109 | |
---|
110 | _e('Converted successfully.'); |
---|
111 | } |
---|
112 | |
---|
113 | print '</li>'; |
---|
114 | } |
---|
115 | |
---|
116 | print '</ul>'; |
---|
117 | } |
---|
118 | |
---|
119 | function init() { |
---|
120 | if (!isset($_GET['step'])) { |
---|
121 | $step = 1; |
---|
122 | } else { |
---|
123 | $step = (int) $_GET['step']; |
---|
124 | } |
---|
125 | |
---|
126 | $this->header(); |
---|
127 | |
---|
128 | switch ($step) { |
---|
129 | case 1: |
---|
130 | $this->welcome(); |
---|
131 | break; |
---|
132 | case 2: |
---|
133 | $this->convert_them(); |
---|
134 | break; |
---|
135 | } |
---|
136 | |
---|
137 | $this->footer(); |
---|
138 | } |
---|
139 | } |
---|
140 | |
---|
141 | $wp_cat2tag_importer = new WP_Categories_to_Tags; |
---|
142 | |
---|
143 | register_importer('wp-cat2tag', __('Categories to Tags Converter'), __('Convert existing categories to tags, selectively.'), array(&$wp_cat2tag_importer, 'init')); |
---|
144 | |
---|
145 | ?> |
---|