function cpm_action_build_storyline_schema() { global $cpm_config; update_option('comicpress-enable-storyline-support', isset($_POST['enable-storyline-support']) ? 1 : 0); update_option('comicpress-storyline-show-top-category', isset($_POST['show-top-category']) ? 1 : 0); if (isset($_POST['enable-storyline-support'])) { $cpm_config->is_cpm_modifying_categories = true; $categories_to_create = array(); $categories_to_rename = array(); $category_ids_to_clean = array(); extract(cpm_get_all_comic_categories()); $comic_posts = cpm_query_posts(); $comic_posts_by_category_id = array(); foreach ($comic_posts as $post) { foreach (wp_get_post_categories($post->ID) as $category) { if (!isset($comic_posts_by_category_id[$category])) { $comic_posts_by_category_id[$category] = array(); } $comic_posts_by_category_id[$category][] = $post->ID; } } foreach ($_POST as $field => $value) { $value = stripslashes($value); $parts = explode("/", $field); if ($parts[0] == "0" && count($parts) > 1) { $category_id = end($parts); $category = get_category($category_id); if (!empty($category)) { $category = (array) $category; if ($category['cat_name'] != $value) { $cpm_config->messages[] = sprintf(__('Category <strong>%1$s</strong> renamed to <strong>%2$s</strong>.', 'comicpress-manager'), $category['cat_name'], $value); $category['cat_name'] = $value; wp_update_category($category); $category_ids_to_clean[] = $category_id; } } else { $categories_to_create[$field] = $value; } if (($index = array_search($field, $category_tree)) !== false) { array_splice($category_tree, $index, 1); } } } if (isset($_POST['original-categories'])) { foreach (explode(",", $_POST['original-categories']) as $node) { if (!isset($_POST[$node])) { $category_id = end(explode("/", $node)); $category = get_category($category_id); $original_cat_name = $category->cat_name; // ensure that we're not deleting a ComicPress category $ok = true; foreach (array('comiccat', 'blogcat') as $type) { if ($category_id == $cpm_config->properties[$type]) { $ok = false; } } // ensure that the category truly is a child of the comic category if ($ok) { $category = get_category($category_id); $ok = false; if (!is_wp_error($category)) { while ($category->parent != 0 && $category->parent != $cpm_config->properties['comiccat']) { $category = get_category($category->parent); } if ($category->parent == $cpm_config->properties['comiccat']) { $ok = true; } } } if ($ok) { wp_delete_category($category_id); $category_ids_to_clean[] = $category_id; $cpm_config->messages[] = sprintf(__('Category <strong>%s</strong> deleted.', 'comicpress-manager'), $original_cat_name); } } } } uksort($categories_to_create, 'cpm_sort_category_keys_by_length'); $changed_field_ids = array(); $removed_field_ids = array(); $target_category_ids = array(); foreach ($categories_to_create as $field => $value) { $original_field = $field; foreach ($changed_field_ids as $changed_field => $new_field) { if (strpos($field, $changed_field) === 0 && strlen($field) > strlen($changed_field)) { $field = str_replace($changed_field, $new_field, $field); break; } } $parts = explode("/", $field); $target_id = array_pop($parts); $parent_id = array_pop($parts); if (!category_exists($value)) { $category_id = wp_create_category($value, $parent_id); $category_ids_to_clean[] = $category_id; array_push($parts, $parent_id); array_push($parts, $category_id); $changed_field_ids[$original_field] = implode("/", $parts); $cpm_config->messages[] = sprintf(__('Category <strong>%s</strong> created.', 'comicpress-manager'), $value); } else { $cpm_config->warnings[] = sprintf(__("The category %s already exists. Please enter a new name.", 'comicpress-manager'), $value); $removed_field_ids[] = $field; } } $order = array_diff(explode(",", $_POST['order']), $removed_field_ids); for ($i = 0; $i < count($order); ++$i) { if (isset($changed_field_ids[$order[$i]])) { $order[$i] = $changed_field_ids[$order[$i]]; } } // ensure we're writing sane data $new_order = array(); $valid_comic_categories = array(); foreach ($order as $node) { $parts = explode("/", $node); if ($parts[0] == "0" && count($parts) > 1) { $new_order[] = $node; $valid_comic_categories[] = end($parts); } } $comic_categories_preserved = array(); foreach ($comic_posts as $post) { $categories = wp_get_post_categories($post->ID); if (count(array_intersect($valid_comic_categories, $categories)) == 0) { $all_parent_categories = array(); foreach ($comic_posts_by_category_id as $category => $post_ids) { if (in_array($post->ID, $post_ids)) { foreach ($new_order as $node) { $parts = explode("/", $node); if ($category == end($parts)) { $parts = explode("/", $node); array_pop($parts); if (count($parts) > 1) { $all_parent_categories[] = implode("/", $parts); } } } } } if (count($all_parent_categories) > 0) { foreach ($all_parent_categories as $category_node) { if (in_array($category_node, $new_order)) { $categories[] = end(explode("/", $category_node)); } } } else { $categories[] = $cpm_config->properties['comiccat']; } wp_set_post_categories($post->ID, $categories); $comic_categories_preserved[] = $post->ID; } } if (count($comic_categories_preserved) > 0) { $cpm_config->messages[] = sprintf(__("The following orphaned comic posts were placed into their original category's parent: <strong>%s</strong>"), implode(", ", $comic_categories_preserved)); } $cpm_config->messages[] = __('Storyline structure saved.', 'comicpress-manager'); update_option("comicpress-storyline-category-order", implode(",", $new_order)); clean_term_cache($category_ids_to_clean, 'category'); wp_cache_flush(); } }
/** * Create a list of checkboxes that can be used to select additional categories. */ function cpm_generate_additional_categories_checkboxes($override_name = null) { global $cpm_config; $additional_categories = array(); $invalid_ids = array($cpm_config->properties['blogcat']); extract(cpm_get_all_comic_categories()); foreach ($category_tree as $node) { $invalid_ids[] = end(explode('/', $node)); } foreach (get_all_category_ids() as $cat_id) { if (!in_array($cat_id, $invalid_ids)) { $category = get_category($cat_id); $additional_categories[strtolower($category->cat_name)] = $category; } } ksort($additional_categories); $name = !empty($override_name) ? $override_name : "additional-categories"; $selected_additional_categories = explode(",", cpm_option("cpm-default-additional-categories")); $category_checkboxes = array(); if (count($additional_categories) > 0) { foreach ($additional_categories as $category) { $checked = in_array($category->cat_ID, $selected_additional_categories) ? "checked" : ""; $category_checkboxes[] = "<input id=\"additional-" . $category->cat_ID . "\" type=\"checkbox\" name=\"{$name}[]\" value=\"" . $category->cat_ID . "\" {$checked} /> <label for=\"additional-" . $category->cat_ID . "\">" . $category->cat_name . "</label><br />"; } } return $category_checkboxes; }
/** * Normalize a storyline structure, merging it with category changes as necessary. * @return array A compact()ed array with the $max_id found and the $category_tree. */ function cpm_normalize_storyline_structure() { global $cpm_config; extract(cpm_get_all_comic_categories()); do { $did_normalize = false; // sort it by this order as best as possible if ($result = get_option("comicpress-storyline-category-order")) { $sorted_tree = explode(",", $result); $new_sorted_tree = array(); foreach ($sorted_tree as $node) { if (in_array($node, $category_tree)) { $new_sorted_tree[] = $node; } else { $did_normalize = true; } } $sorted_tree = $new_sorted_tree; foreach ($category_tree as $node) { if (!in_array($node, $sorted_tree)) { // try to find the nearest sibling $parts = explode("/", $node); while (count($parts) > 0) { array_pop($parts); $node_snippit = implode("/", $parts); $last_sibling = null; for ($i = 0; $i < count($sorted_tree); ++$i) { if (strpos($sorted_tree[$i], $node_snippit) === 0) { $last_sibling = $i; } } if (!is_null($last_sibling)) { $did_normalize = true; array_splice($sorted_tree, $last_sibling + 1, 0, $node); break; } } } } $category_tree = $sorted_tree; } else { sort($category_tree); } if ($did_normalize || empty($result)) { update_option("comicpress-storyline-category-order", implode(",", $category_tree)); } } while ($did_normalize); return compact('category_tree', 'max_id'); }