Advertisement
Imperative-Ideas

Add build category editing

Sep 26th, 2013
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 15.41 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Assign Categories Functionality
  5.  * @author : Aswin Anand
  6.  * @uri : http://www.aswinanand.com/blog/2008/10/bulk-assign-categories-to-multiple-posts/
  7.  */
  8.  
  9. class Walker_Category_Div extends Walker {
  10.     var $tree_type = 'category';
  11.     var $db_fields = array ('parent' => 'parent', 'id' => 'term_id');
  12.  
  13.     function start_lvl(&$output, $depth, $args) {
  14.         $output .= "<div class='children'>\n";
  15.     }
  16.  
  17.     function end_lvl(&$output, $depth, $args) {
  18.         $output .= "</div>";
  19.     }
  20.  
  21.     function start_el(&$output, $category, $depth, $args) {
  22.         extract($args);
  23.         $arrids = explode(';', $_POST['setcat_catids']);
  24.         $class = '';
  25.         if (gettype($popular_cats) !== "array") $class = '';
  26.         else $class = in_array( $category->term_id, $popular_cats ) ? ' class="popular-category"' : '';
  27.         $output .= "\n";
  28.         $output .= "<div id='category-$category->term_id'$class>" . '<span id="span_' . $category->term_id . '"><a id="a_' . $category->term_id . '" style="text-decoration: none;" href="javascript:;" onclick="toggle(this);">+</a>&nbsp;</span>' . '<label class="selectit">';
  29.         $output .= '<input onclick="selectAllChildren(this);" value="' . $category->term_id . '" type="checkbox" name="cate_' . $category->term_id . '" id="in-category-' . $category->term_id . '"' . (in_array( $category->term_id, $arrids ) ? ' checked="checked"' : "" ) . '/> ' . esc_html( apply_filters('the_category', $category->name )) . '</label>';
  30.     }
  31.  
  32.     function end_el(&$output, $category, $depth, $args) {
  33.         $output .= "</div>";
  34.     }
  35. }
  36.  
  37. function com_aswinanand_assignCategories() {
  38.     ?>
  39.     <style type="text/css">
  40.         .children {
  41.             position: relative;
  42.             margin-left: 2em;
  43.             display: none;
  44.         }
  45.         .selectit {
  46.             font-family: "Lucida Grande",Verdana,Arial,"Bitstream Vera Sans",sans-serif;
  47.             font-size: 11px;
  48.         }
  49.     </style>
  50.     <script type="text/javascript">
  51.         function $(id) {
  52.             return document.getElementById(id);
  53.         }
  54.  
  55.         function toggle(elt) {
  56.             var j = jQuery;
  57.             j(elt).parent().next().next().toggle();
  58.             elt.innerHTML = (j(elt).html() == "+") ? "&ndash;" : "+";
  59.         }
  60.  
  61.         function selectAllChildren(elt) {
  62.             var j = jQuery;
  63.             var selected = elt.checked;
  64.             var id = "#category-" + j(elt).attr('id').split("-")[2];
  65.             if ($("setcat_select_subcat").checked) {
  66.                 j(id).find("input[type=checkbox]").each(function() {
  67.                     j(this)[0].checked = selected;
  68.                 });
  69.             }
  70.         }
  71.  
  72.         function selectAll(elt, s) {
  73.             var elts = $(elt).getElementsByTagName("input");
  74.             var isIE;
  75.             if (elts.length > 0)
  76.                 isIE = (elts[0].getAttribute) ? false : true;
  77.             else return false;
  78.  
  79.             var id='', type='';
  80.             for (var i=0; i<elts.length; i++) {
  81.                 if (isIE) {
  82.                     id = elts[i].attributes["id"];
  83.                     type = elts[i].attributes["type"];
  84.                 } else {
  85.                     id = elts[i].getAttribute("id");
  86.                     type = elts[i].getAttribute("type");
  87.                 }
  88.                 if ("checkbox" == type) $(id).checked = s;
  89.             }
  90.         }
  91.  
  92.         function getCheckedItems(elt) {
  93.             var isIE;
  94.             var elts = $(elt).getElementsByTagName("input");
  95.             if (elts.length > 0) isIE = (elts[0].getAttribute) ? false : true;
  96.             var type = '', id = '', catids = '';
  97.             for (var i=0; i<elts.length; i++) {
  98.                 if (isIE) {
  99.                     id = elts[i].attributes["id"];
  100.                     type = elts[i].attributes["type"];
  101.                 }
  102.                 else {
  103.                     id = elts[i].getAttribute("id");
  104.                     type = elts[i].getAttribute("type");
  105.                 }
  106.  
  107.                 if ("checkbox" == type) {
  108.                     if ($(id).checked) {
  109.                         catids += $(id).value + ";";
  110.                     }
  111.                 } else continue;
  112.             }
  113.             if (catids.length > 0) return catids.substring(0, catids.length - 1);
  114.             return catids;
  115.         }
  116.  
  117.         function showAlert() {
  118.             var catids = getCheckedItems('setcat_cats');
  119.             if (0 == catids.length) {
  120.                 alert("Please select at least one category.");
  121.                 $('setcat_cats').focus();
  122.                 return false;
  123.             }
  124.             $("setcat_catids").value = catids;
  125.  
  126.             catids = getCheckedItems('setcat_posts');
  127.             if (0 == catids.length) {
  128.                 alert("Please select at least one post.");
  129.                 return false;
  130.             }
  131.             $("setcat_postids").value = catids;
  132.  
  133.             return true;
  134.         }
  135.     </script>
  136.     <?php
  137.  
  138.     /* Save Options */
  139.     if ( isset($_POST['setcat_action']) && "save_option" == $_POST['setcat_action'] ) {
  140.         if ( isset($_POST['setcat_select_subcat']) && "true" === $_POST['setcat_select_subcat'] ) {
  141.             update_option('setcat_select_subcat', "true");
  142.         } else {
  143.             update_option('setcat_select_subcat', "false");
  144.         }
  145.  
  146.         if ( isset($_POST['setcat_numposts']) ) {
  147.             if (is_numeric($_POST['setcat_numposts']) && $_POST['setcat_numposts'] > 0) {
  148.                 if ($_POST['setcat_numposts'] >= 500)
  149.                     update_option('setcat_numposts', 500);
  150.                 else
  151.                     update_option('setcat_numposts', $_POST['setcat_numposts']);
  152.             } else if ('all' == $_POST['setcat_numposts']) {
  153.                 update_option('setcat_numposts', 'all');
  154.             } else {
  155.                 update_option('setcat_numposts', 25);
  156.             }
  157.         }
  158.  
  159.         echo '<div id="message" class="updated fade"><p><strong>Options have been updated successfully.</strong>';
  160.         echo '<span style="float: right;"><a href="javascript:;" onclick="jQuery(\'#message\').hide();" title="Close this message">Close</a></span></p></div>';
  161.     }
  162.  
  163.     /* Apply selected categories to posts */
  164.     $arrids = null; $arrpostids = null; $res = null;
  165.     $suc = "";
  166.     $fai = "";
  167.     if (isset($_POST['setcat_catids']) && 0 != strlen(trim($_POST['setcat_catids'])) ) {
  168.         $arrids = explode(';', $_POST['setcat_catids']);
  169.         if (isset($_POST['setcat_postids']) && 0 != strlen(trim($_POST['setcat_postids'])) ) {
  170.             $arrpostids = explode(';', $_POST['setcat_postids']);
  171.             $preserve = isset($_POST['setcat_preserve']) ? true : false;
  172.             echo '<div id="message" class="updated fade">';
  173.             echo '<span style="float: right; margin-top: 5px;"><a href="javascript:;" onclick="jQuery(\'#message\').hide();" title="Close this message">Close</a></span>';
  174.             echo '<p><strong>';
  175.             foreach ($arrpostids as $ap) {
  176.                 if ($preserve) {
  177.                     $res = wp_get_object_terms($ap, 'category', array('fields'=>'ids'));
  178.                     $res = array_unique(array_merge($res, $arrids));
  179.                     $pdata = array('ID'=>$ap, 'post_category'=>$res);
  180.                 } else {
  181.                     $pdata = array('ID'=>$ap, 'post_category'=>$arrids);
  182.                 }
  183.                 if (is_numeric(wp_update_post($pdata))) $suc .= $ap . ", ";
  184.                 else                                   $fai .= $ap . ", ";
  185.             }
  186.             if (strlen($suc) > 0) echo "Categories have been successfully for the posts: " . substr($suc, 0, strlen($suc)-2) . "<br/>";
  187.             if (strlen($fai) > 0) echo "Categories have NOT been applied for the posts: " . substr($fai, 0, strlen($fai)-2);
  188.             echo '</strong>';
  189.             echo '</p></div>';
  190.         }
  191.     }
  192.  
  193.     /* Display the UI */
  194.     echo '<div class="wrap">';
  195.     echo '<h2>Search</h2>';
  196.     echo '<form method="post">';
  197.     echo '<table class="form-table">';
  198.     echo '<tr>';
  199.     echo '<th scope="row">';
  200.     echo '<label for="search">Search</label></th>';
  201.     echo '<td><input type="text" name="search" id="setcat_search" value="' . attribute_escape(apply_filters('the_search_query', $_POST['search'])) . '" />&nbsp;';
  202.     echo '<input type="submit" value="Search" class="button-secondary" /> ';
  203.     echo '<input type="submit" value="Clear Search Results" class="button-secondary" onclick="javascript:$(\'setcat_search\').value=\'\';" /> ';
  204.     echo '</td></tr>';
  205.     echo '</table>';
  206.     echo '</form>';
  207.     echo '<form name="setcat_options" id="setcat_options" method="post">';
  208.     echo '<h2>Options</h2><br/>';
  209.     echo '<label>Select/Unselect all sub-categories when selecting/unselecting a parent category.&nbsp;';
  210.     echo '<input type="checkbox" ';
  211.     if ( "true" == get_option('setcat_select_subcat', "false") ) {
  212.         echo 'checked="checked" ';
  213.     }
  214.     echo 'value="true" id="setcat_select_subcat" name="setcat_select_subcat"/></label>';
  215.     echo '<br/><br/>';
  216.     echo '<label>How many posts should be displayed in the table?&nbsp;';
  217.     echo '<select name="setcat_numposts" id="setcat_numposts">';
  218.     $numposts = get_option("setcat_numposts", 25);
  219.     if ( 10 == $numposts )
  220.         echo '<option value="10" selected>10</option>';
  221.     else
  222.         echo '<option value="10">10</option>';
  223.  
  224.     if ( 25 == $numposts )
  225.         echo '<option value="25" selected>25 (default)</option>';
  226.     else
  227.         echo '<option value="25">25 (default)</option>';
  228.  
  229.     if ( 50 == $numposts )
  230.         echo '<option value="50" selected>50</option>';
  231.     else
  232.         echo '<option value="50">50</option>';
  233.  
  234.     if ( 100 == $numposts )
  235.         echo '<option value="100" selected>100</option>';
  236.     else
  237.         echo '<option value="100">100</option>';
  238.  
  239.     if ( 250 == $numposts )
  240.         echo '<option value="250" selected>250</option>';
  241.     else
  242.         echo '<option value="250">250</option>';
  243.  
  244.     if ( 500 == $numposts )
  245.         echo '<option value="500" selected>500</option>';
  246.     else
  247.         echo '<option value="500">500</option>';
  248.  
  249.     if ( "all" == $numposts ) {
  250.         $numposts = 1000000;    // 1 million
  251.         echo '<option value="all" selected>ALL</option>';
  252.     } else
  253.         echo '<option value="all">ALL</option>';
  254.  
  255.     echo '</select>';
  256.     echo '</label>';
  257.     echo '<br/><br/>';
  258.     echo '<input type="hidden" name="setcat_action" value="save_option" />';
  259.     echo '<input type="submit" value="Save Options" class="button-secondary" /> ';
  260.     echo '</form>';
  261.     echo '<h2>Set Categories</h2>';
  262.     echo '<form method="post" id="setcatform" onsubmit="return showAlert();">';
  263.     global $wpdb;
  264.     $p = $wpdb->prefix;
  265.     $res = get_terms('category','get=all');
  266.     echo '<h3>Pick your categories</h3>';
  267.     echo '<a href="javascript:void(0);" onclick="javascript:selectAll(\'setcat_cats\',true);" class="selectit">Select All Categories</a> &nbsp;|&nbsp; ';
  268.     echo '<a href="javascript:void(0);" onclick="javascript:selectAll(\'setcat_cats\',false);" class="selectit">Unselect All Categories</a> <br/><br/>';
  269.     echo '<div id="setcat_cats">';
  270.  
  271.     $arrgs = array(
  272.         'hierarchical'=>true,
  273.         'hide_empty'=>false
  274.     );
  275.     $res1 = get_categories($arrgs);
  276.     $cat_args = array(
  277.         'show_option_all' => '', 'show_option_none' => '',
  278.         'orderby' => 'id', 'order' => 'ASC',
  279.         'show_last_update' => 0, 'show_count' => 0,
  280.         'hide_empty' => 1, 'child_of' => 0,
  281.         'exclude' => '', 'echo' => 1,
  282.         'selected' => 0, 'hierarchical' => 0,
  283.         'name' => 'cat', 'class' => 'postform',
  284.         'depth' => 0, 'tab_index' => 0
  285.     );
  286.     $argu = array($res1, 1000, wp_parse_args(array('hierarchical'=>true, 'hide_empty'=>false), $cat_args));
  287.     $wlk = new Walker_Category_Div();
  288.     echo call_user_func_array(array(&$wlk, 'walk'), $argu);
  289.  
  290.     echo '</div>';
  291.     echo '<input type="hidden" name="setcat_catids" id="setcat_catids" />';
  292.     echo '<hr/>';
  293.  
  294.     /* List Posts */
  295.     global $wp_query; global $post;
  296.     $wp_query->query(array('s'=>isset($_POST['search'])?$_POST['search']:'', 'paged'=>isset($_GET['paged'])?$_GET['paged']:1, 'posts_per_page'=>$numposts));
  297.     $page_links = paginate_links( array(
  298.         'base' => add_query_arg( 'paged', '%#%' ),
  299.         'format' => '',
  300.         'total' => $wp_query->max_num_pages,
  301.         'current' => isset($_GET['paged'])?$_GET['paged']:1
  302.     ));
  303.  
  304.     echo "<h3>Pick your posts</h3>";
  305.     echo '<input class="button-primary" type="submit" value="Apply Categories" /><br/><br/>';
  306.     echo '<table class="widefat">';
  307.     echo '<tr><td colspan="4"><a href="javascript:void(0);" title="Select All" onclick="javascript:selectAll(\'setcat_posts\',true);">Select All Posts</a> | <a href="javascript:void(0);" title="Unselect All" onclick="javascript:selectAll(\'setcat_posts\',false);">Unselect All Posts</a> | ';
  308.     echo '<label for="setcat_preserve">';
  309.     echo '<input ';
  310.     if (isset($_POST['setcat_preserve'])) echo 'checked="checked" ';
  311.     echo 'type="checkbox" id="setcat_preserve" name="setcat_preserve" value="1" /> Preserve existing categories for selected posts.</label> </td></tr>';
  312.     echo '<thead>';
  313.     echo '<tr><th></th><th>ID</th><th>Post Title</th><th>Categories</th></tr>';
  314.     echo '</thead>';
  315.     echo '<tbody id="setcat_posts">';
  316.     while (have_posts()) {
  317.         the_post();
  318.         echo '<tr><td align="center">';
  319.         echo '<input ';
  320.         // deselect posts after categories have been applied
  321.         // uncomment below to select the posts even after categories have been applied
  322.         /*if (null != $arrpostids)
  323.             if (in_array($post->ID, $arrpostids))
  324.                 echo 'checked="checked" ';*/
  325.         echo 'type="checkbox" id="' . $post->ID . '" name="' . $post->ID . '" value="' . $post->ID . '" />' . '</td>';
  326.         echo '<td align="center"><label for="' . $post->ID . '">' . $post->ID . '</label></td>';
  327.         echo '<td><label for="' . $post->ID . '"><a href="' . get_permalink($post->ID) . '" title="' . $post->post_title . '">' . $post->post_title . '</a></label></td>';
  328.         echo '<td>' . get_the_category_list(',', '', $post->ID) . '</td></tr>';
  329.     }
  330.     echo '</tbody>';
  331.     echo '<tfoot>';
  332.     echo '<tr><th></th><th>ID</th><th>Post Title</th><th>Categories</th></tr>';
  333.     echo '</tfoot>';
  334.     echo '<tr><td colspan="3"><a href="javascript:void(0);" title="Select All" onclick="javascript:selectAll(\'setcat_posts\',true);">Select All Posts</a> | <a href="javascript:void(0);" title="Unselect All" onclick="javascript:selectAll(\'setcat_posts\',false);">Unselect All Posts</a></td>';
  335.     echo '<td>';
  336.  
  337.     if ( $page_links )
  338.         echo "<div class='tablenav-pages'>$page_links</div>";
  339.  
  340.     echo '</td>';
  341.     echo '</tr>';
  342.     echo "</table>";
  343.     echo '<input type="hidden" name="setcat_postids" id="setcat_postids" />';
  344.     echo '<p class="submit"> <input class="button-primary" type="submit" value="Apply Categories" /> </p>';
  345.     echo '</form>';
  346.     echo '</div>';
  347.     echo '';
  348. }
  349.  
  350. function com_aswinanand_setAdminPages() {
  351.     add_management_page('Assign Categories', 'Assign Categories', 8, __FILE__, 'com_aswinanand_assignCategories');
  352. }
  353.  
  354. function com_aswinanand_removeSetCat() {
  355.     delete_option('setcat_select_subcat');
  356.     delete_option('setcat_numposts');
  357. }
  358.  
  359. add_action('admin_menu', 'com_aswinanand_setAdminPages');
  360. if (function_exists('register_uninstall_hook')) {
  361.     register_uninstall_hook(__FILE__, 'com_aswinanand_removeSetCat');
  362. }
  363.  
  364. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement