Ticket #19556: 19556.diff
File 19556.diff, 14.3 KB (added by , 11 years ago) |
---|
-
trunk/src/wp-admin/includes/template.php
8 8 * @subpackage Administration 9 9 */ 10 10 11 //12 // Category Checklists13 //14 15 /**16 * Walker to output an unordered list of category checkbox <input> elements.17 *18 * @see Walker19 * @see wp_category_checklist()20 * @see wp_terms_checklist()21 * @since 2.5.122 */23 class Walker_Category_Checklist extends Walker {24 var $tree_type = 'category';25 var $db_fields = array ('parent' => 'parent', 'id' => 'term_id'); //TODO: decouple this26 27 function start_lvl( &$output, $depth = 0, $args = array() ) {28 $indent = str_repeat("\t", $depth);29 $output .= "$indent<ul class='children'>\n";30 }31 32 function end_lvl( &$output, $depth = 0, $args = array() ) {33 $indent = str_repeat("\t", $depth);34 $output .= "$indent</ul>\n";35 }36 37 function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {38 extract($args);39 if ( empty($taxonomy) )40 $taxonomy = 'category';41 42 if ( $taxonomy == 'category' )43 $name = 'post_category';44 else45 $name = 'tax_input['.$taxonomy.']';46 47 $class = in_array( $category->term_id, $popular_cats ) ? ' class="popular-category"' : '';48 $output .= "\n<li id='{$taxonomy}-{$category->term_id}'$class>" . '<label class="selectit"><input value="' . $category->term_id . '" type="checkbox" name="'.$name.'[]" id="in-'.$taxonomy.'-' . $category->term_id . '"' . checked( in_array( $category->term_id, $selected_cats ), true, false ) . disabled( empty( $args['disabled'] ), false, false ) . ' /> ' . esc_html( apply_filters('the_category', $category->name )) . '</label>';49 }50 51 function end_el( &$output, $category, $depth = 0, $args = array() ) {52 $output .= "</li>\n";53 }54 }55 56 /**57 * Output an unordered list of checkbox <input> elements labelled58 * with category names.59 *60 * @see wp_terms_checklist()61 * @since 2.5.162 *63 * @param int $post_id Mark categories associated with this post as checked. $selected_cats must not be an array.64 * @param int $descendants_and_self ID of the category to output along with its descendents.65 * @param bool|array $selected_cats List of categories to mark as checked.66 * @param bool|array $popular_cats Override the list of categories that receive the "popular-category" class.67 * @param object $walker Walker object to use to build the output.68 * @param bool $checked_ontop Move checked items out of the hierarchy and to the top of the list.69 */70 function wp_category_checklist( $post_id = 0, $descendants_and_self = 0, $selected_cats = false, $popular_cats = false, $walker = null, $checked_ontop = true ) {71 wp_terms_checklist( $post_id, array(72 'taxonomy' => 'category',73 'descendants_and_self' => $descendants_and_self,74 'selected_cats' => $selected_cats,75 'popular_cats' => $popular_cats,76 'walker' => $walker,77 'checked_ontop' => $checked_ontop78 ) );79 }80 81 /**82 * Output an unordered list of checkbox <input> elements labelled83 * with term names. Taxonomy independent version of wp_category_checklist().84 *85 * @since 3.0.086 *87 * @param int $post_id88 * @param array $args89 */90 function wp_terms_checklist($post_id = 0, $args = array()) {91 $defaults = array(92 'descendants_and_self' => 0,93 'selected_cats' => false,94 'popular_cats' => false,95 'walker' => null,96 'taxonomy' => 'category',97 'checked_ontop' => true98 );99 $args = apply_filters( 'wp_terms_checklist_args', $args, $post_id );100 101 extract( wp_parse_args($args, $defaults), EXTR_SKIP );102 103 if ( empty($walker) || !is_a($walker, 'Walker') )104 $walker = new Walker_Category_Checklist;105 106 $descendants_and_self = (int) $descendants_and_self;107 108 $args = array('taxonomy' => $taxonomy);109 110 $tax = get_taxonomy($taxonomy);111 $args['disabled'] = !current_user_can($tax->cap->assign_terms);112 113 if ( is_array( $selected_cats ) )114 $args['selected_cats'] = $selected_cats;115 elseif ( $post_id )116 $args['selected_cats'] = wp_get_object_terms($post_id, $taxonomy, array_merge($args, array('fields' => 'ids')));117 else118 $args['selected_cats'] = array();119 120 if ( is_array( $popular_cats ) )121 $args['popular_cats'] = $popular_cats;122 else123 $args['popular_cats'] = get_terms( $taxonomy, array( 'fields' => 'ids', 'orderby' => 'count', 'order' => 'DESC', 'number' => 10, 'hierarchical' => false ) );124 125 if ( $descendants_and_self ) {126 $categories = (array) get_terms($taxonomy, array( 'child_of' => $descendants_and_self, 'hierarchical' => 0, 'hide_empty' => 0 ) );127 $self = get_term( $descendants_and_self, $taxonomy );128 array_unshift( $categories, $self );129 } else {130 $categories = (array) get_terms($taxonomy, array('get' => 'all'));131 }132 133 if ( $checked_ontop ) {134 // Post process $categories rather than adding an exclude to the get_terms() query to keep the query the same across all posts (for any query cache)135 $checked_categories = array();136 $keys = array_keys( $categories );137 138 foreach( $keys as $k ) {139 if ( in_array( $categories[$k]->term_id, $args['selected_cats'] ) ) {140 $checked_categories[] = $categories[$k];141 unset( $categories[$k] );142 }143 }144 145 // Put checked cats on top146 echo call_user_func_array(array(&$walker, 'walk'), array($checked_categories, 0, $args));147 }148 // Then the rest of them149 echo call_user_func_array(array(&$walker, 'walk'), array($categories, 0, $args));150 }151 152 /**153 * Retrieve a list of the most popular terms from the specified taxonomy.154 *155 * If the $echo argument is true then the elements for a list of checkbox156 * <input> elements labelled with the names of the selected terms is output.157 * If the $post_ID global isn't empty then the terms associated with that158 * post will be marked as checked.159 *160 * @since 2.5.0161 *162 * @param string $taxonomy Taxonomy to retrieve terms from.163 * @param int $default Unused.164 * @param int $number Number of terms to retrieve. Defaults to 10.165 * @param bool $echo Optionally output the list as well. Defaults to true.166 * @return array List of popular term IDs.167 */168 function wp_popular_terms_checklist( $taxonomy, $default = 0, $number = 10, $echo = true ) {169 $post = get_post();170 171 if ( $post && $post->ID )172 $checked_terms = wp_get_object_terms($post->ID, $taxonomy, array('fields'=>'ids'));173 else174 $checked_terms = array();175 176 $terms = get_terms( $taxonomy, array( 'orderby' => 'count', 'order' => 'DESC', 'number' => $number, 'hierarchical' => false ) );177 178 $tax = get_taxonomy($taxonomy);179 180 $popular_ids = array();181 foreach ( (array) $terms as $term ) {182 $popular_ids[] = $term->term_id;183 if ( !$echo ) // hack for AJAX use184 continue;185 $id = "popular-$taxonomy-$term->term_id";186 $checked = in_array( $term->term_id, $checked_terms ) ? 'checked="checked"' : '';187 ?>188 189 <li id="<?php echo $id; ?>" class="popular-category">190 <label class="selectit">191 <input id="in-<?php echo $id; ?>" type="checkbox" <?php echo $checked; ?> value="<?php echo (int) $term->term_id; ?>" <?php disabled( ! current_user_can( $tax->cap->assign_terms ) ); ?> />192 <?php echo esc_html( apply_filters( 'the_category', $term->name ) ); ?>193 </label>194 </li>195 196 <?php197 }198 return $popular_ids;199 }200 201 11 /** 202 12 * {@internal Missing Short Description}} 203 13 * -
trunk/src/wp-includes/category-template.php
1189 1189 1190 1190 return $r; 1191 1191 } 1192 1193 // 1194 // Category Checklists 1195 // 1196 1197 /** 1198 * Walker to output an unordered list of category checkbox <input> elements. 1199 * 1200 * @see Walker 1201 * @see wp_category_checklist() 1202 * @see wp_terms_checklist() 1203 * @since 2.5.1 1204 */ 1205 class Walker_Category_Checklist extends Walker { 1206 var $tree_type = 'category'; 1207 var $db_fields = array ('parent' => 'parent', 'id' => 'term_id'); //TODO: decouple this 1208 1209 function start_lvl( &$output, $depth = 0, $args = array() ) { 1210 $indent = str_repeat("\t", $depth); 1211 $output .= "$indent<ul class='children'>\n"; 1212 } 1213 1214 function end_lvl( &$output, $depth = 0, $args = array() ) { 1215 $indent = str_repeat("\t", $depth); 1216 $output .= "$indent</ul>\n"; 1217 } 1218 1219 function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) { 1220 extract($args); 1221 1222 if ( empty($taxonomy) ) 1223 $taxonomy = 'category'; 1224 1225 if ( !isset($name) || !$name ) { 1226 if ( $taxonomy == 'category' ) 1227 $name = 'post_category'; 1228 else 1229 $name = 'tax_input['.$taxonomy.']'; 1230 } 1231 1232 $class = in_array( $category->term_id, $popular_cats ) ? ' class="popular-category"' : ''; 1233 $output .= "\n<li id='{$taxonomy}-{$category->term_id}'$class>" . '<label class="selectit"><input value="' . $category->term_id . '" type="checkbox" name="'.$name.'[]" id="in-'.$taxonomy.'-' . $category->term_id . '"' . checked( in_array( $category->term_id, $selected_cats ), true, false ) . disabled( empty( $args['disabled'] ), false, false ) . ' /> ' . esc_html( apply_filters('the_category', $category->name )) . '</label>'; 1234 } 1235 1236 function end_el( &$output, $category, $depth = 0, $args = array() ) { 1237 $output .= "</li>\n"; 1238 } 1239 } 1240 1241 /** 1242 * Output an unordered list of checkbox <input> elements labelled 1243 * with category names. 1244 * 1245 * @see wp_terms_checklist() 1246 * @since 2.5.1 1247 * 1248 * @param int $post_id Mark categories associated with this post as checked. $selected_cats must not be an array. 1249 * @param int $descendants_and_self ID of the category to output along with its descendents. 1250 * @param bool|array $selected_cats List of categories to mark as checked. 1251 * @param bool|array $popular_cats Override the list of categories that receive the "popular-category" class. 1252 * @param object $walker Walker object to use to build the output. 1253 * @param bool $checked_ontop Move checked items out of the hierarchy and to the top of the list. 1254 */ 1255 function wp_category_checklist( $post_id = 0, $descendants_and_self = 0, $selected_cats = false, $popular_cats = false, $walker = null, $checked_ontop = true ) { 1256 wp_terms_checklist( $post_id, array( 1257 'taxonomy' => 'category', 1258 'descendants_and_self' => $descendants_and_self, 1259 'selected_cats' => $selected_cats, 1260 'popular_cats' => $popular_cats, 1261 'walker' => $walker, 1262 'checked_ontop' => $checked_ontop 1263 ) ); 1264 } 1265 1266 /** 1267 * Output an unordered list of checkbox <input> elements labelled 1268 * with term names. Taxonomy independent version of wp_category_checklist(). 1269 * 1270 * @since 3.0.0 1271 * 1272 * @param int $post_id 1273 * @param array $args 1274 */ 1275 function wp_terms_checklist($post_id = 0, $args = array()) { 1276 $defaults = array( 1277 'descendants_and_self' => 0, 1278 'selected_cats' => false, 1279 'popular_cats' => false, 1280 'walker' => null, 1281 'taxonomy' => 'category', 1282 'checked_ontop' => true 1283 ); 1284 $args = apply_filters( 'wp_terms_checklist_args', $args, $post_id ); 1285 1286 extract( wp_parse_args($args, $defaults), EXTR_SKIP ); 1287 1288 if ( empty($walker) || !is_a($walker, 'Walker') ) 1289 $walker = new Walker_Category_Checklist; 1290 1291 $descendants_and_self = (int) $descendants_and_self; 1292 1293 $args = array('taxonomy' => $taxonomy); 1294 1295 if ( isset($name) ) 1296 $args['name'] = $name; 1297 1298 if ( is_array( $selected_cats ) ) 1299 $args['selected_cats'] = $selected_cats; 1300 elseif ( $post_id ) 1301 $args['selected_cats'] = wp_get_object_terms($post_id, $taxonomy, array_merge($args, array('fields' => 'ids'))); 1302 else 1303 $args['selected_cats'] = array(); 1304 1305 if ( is_array( $popular_cats ) ) 1306 $args['popular_cats'] = $popular_cats; 1307 else 1308 $args['popular_cats'] = get_terms( $taxonomy, array( 'fields' => 'ids', 'orderby' => 'count', 'order' => 'DESC', 'number' => 10, 'hierarchical' => false ) ); 1309 1310 if ( $descendants_and_self ) { 1311 $categories = (array) get_terms($taxonomy, array( 'child_of' => $descendants_and_self, 'hierarchical' => 0, 'hide_empty' => 0 ) ); 1312 $self = get_term( $descendants_and_self, $taxonomy ); 1313 array_unshift( $categories, $self ); 1314 } else { 1315 $categories = (array) get_terms($taxonomy, array('get' => 'all')); 1316 } 1317 1318 if ( $checked_ontop ) { 1319 // Post process $categories rather than adding an exclude to the get_terms() query to keep the query the same across all posts (for any query cache) 1320 $checked_categories = array(); 1321 $keys = array_keys( $categories ); 1322 1323 foreach( $keys as $k ) { 1324 if ( in_array( $categories[$k]->term_id, $args['selected_cats'] ) ) { 1325 $checked_categories[] = $categories[$k]; 1326 unset( $categories[$k] ); 1327 } 1328 } 1329 1330 // Put checked cats on top 1331 echo call_user_func_array(array(&$walker, 'walk'), array($checked_categories, 0, $args)); 1332 } 1333 // Then the rest of them 1334 echo call_user_func_array(array(&$walker, 'walk'), array($categories, 0, $args)); 1335 } 1336 1337 /** 1338 * Retrieve a list of the most popular terms from the specified taxonomy. 1339 * 1340 * If the $echo argument is true then the elements for a list of checkbox 1341 * <input> elements labelled with the names of the selected terms is output. 1342 * If the $post_ID global isn't empty then the terms associated with that 1343 * post will be marked as checked. 1344 * 1345 * @since 2.5.0 1346 * 1347 * @param string $taxonomy Taxonomy to retrieve terms from. 1348 * @param int $default Unused. 1349 * @param int $number Number of terms to retrieve. Defaults to 10. 1350 * @param bool $echo Optionally output the list as well. Defaults to true. 1351 * @return array List of popular term IDs. 1352 */ 1353 function wp_popular_terms_checklist( $taxonomy, $default = 0, $number = 10, $echo = true ) { 1354 $post = get_post(); 1355 1356 if ( $post && $post->ID ) 1357 $checked_terms = wp_get_object_terms($post->ID, $taxonomy, array('fields'=>'ids')); 1358 else 1359 $checked_terms = array(); 1360 1361 $terms = get_terms( $taxonomy, array( 'orderby' => 'count', 'order' => 'DESC', 'number' => $number, 'hierarchical' => false ) ); 1362 1363 $popular_ids = array(); 1364 foreach ( (array) $terms as $term ) { 1365 $popular_ids[] = $term->term_id; 1366 if ( !$echo ) // hack for AJAX use 1367 continue; 1368 $id = "popular-$taxonomy-$term->term_id"; 1369 $checked = in_array( $term->term_id, $checked_terms ) ? 'checked="checked"' : ''; 1370 ?> 1371 1372 <li id="<?php echo $id; ?>" class="popular-category"> 1373 <label class="selectit"> 1374 <input id="in-<?php echo $id; ?>" type="checkbox" <?php echo $checked; ?> value="<?php echo (int) $term->term_id; ?>" /> 1375 <?php echo esc_html( apply_filters( 'the_category', $term->name ) ); ?> 1376 </label> 1377 </li> 1378 1379 <?php 1380 } 1381 return $popular_ids; 1382 } 1383 No newline at end of file