3 // Creates a new post from the "Write Post" form using $_POST information.
4 function write_post() {
7 if (!current_user_can('edit_posts'))
8 die(__('You are not allowed to create posts or drafts on this blog.'));
11 $_POST['post_content'] = $_POST['content'];
12 $_POST['post_excerpt'] = $_POST['excerpt'];
13 $_POST['post_parent'] = $_POST['parent_id'];
14 $_POST['to_ping'] = $_POST['trackback_url'];
16 if (!empty ($_POST['post_author_override'])) {
17 $_POST['post_author'] = (int) $_POST['post_author_override'];
19 if (!empty ($_POST['post_author'])) {
20 $_POST['post_author'] = (int) $_POST['post_author'];
22 $_POST['post_author'] = (int) $_POST['user_ID'];
25 if (($_POST['post_author'] != $_POST['user_ID']) && !current_user_can('edit_others_posts'))
26 die(__('You cannot post as this user.'));
28 // What to do based on which button they pressed
29 if ('' != $_POST['saveasdraft'])
30 $_POST['post_status'] = 'draft';
31 if ('' != $_POST['saveasprivate'])
32 $_POST['post_status'] = 'private';
33 if ('' != $_POST['publish'])
34 $_POST['post_status'] = 'publish';
35 if ('' != $_POST['advanced'])
36 $_POST['post_status'] = 'draft';
37 if ('' != $_POST['savepage'])
38 $_POST['post_status'] = 'static';
40 if ('publish' == $_POST['post_status'] && !current_user_can('publish_posts'))
41 $_POST['post_status'] = 'draft';
43 if ('static' == $_POST['post_status'] && !current_user_can('edit_pages'))
44 die(__('This user cannot edit pages.'));
46 if (!isset ($_POST['comment_status']))
47 $_POST['comment_status'] = 'closed';
49 if (!isset ($_POST['ping_status']))
50 $_POST['ping_status'] = 'closed';
52 if (!empty ($_POST['edit_date'])) {
59 $jj = ($jj > 31) ? 31 : $jj;
60 $hh = ($hh > 23) ? $hh -24 : $hh;
61 $mn = ($mn > 59) ? $mn -60 : $mn;
62 $ss = ($ss > 59) ? $ss -60 : $ss;
63 $_POST['post_date'] = "$aa-$mm-$jj $hh:$mn:$ss";
64 $_POST['post_date_gmt'] = get_gmt_from_date("$aa-$mm-$jj $hh:$mn:$ss");
68 $post_ID = wp_insert_post($_POST);
71 // Reunite any orphaned attachments with their parent
72 if ( $_POST['temp_ID'] )
73 relocate_children($_POST['temp_ID'], $post_ID);
75 // Now that we have an ID we can fix any attachment anchor hrefs
76 fix_attachment_links($post_ID);
81 // Move child posts to a new parent
82 function relocate_children($old_ID, $new_ID) {
84 $old_ID = (int) $old_ID;
85 $new_ID = (int) $new_ID;
86 return $wpdb->query("UPDATE $wpdb->posts SET post_parent = $new_ID WHERE post_parent = $old_ID");
89 // Replace hrefs of attachment anchors with up-to-date permalinks.
90 function fix_attachment_links($post_ID) {
93 $post = & get_post($post_ID, ARRAY_A);
95 $search = "#<a[^>]+rel=('|\")[^'\"]*attachment[^>]*>#ie";
97 // See if we have any rel="attachment" links
98 if ( 0 == preg_match_all($search, $post['post_content'], $anchor_matches, PREG_PATTERN_ORDER) )
102 $search = "# id=(\"|')p(\d+)\\1#i";
103 foreach ( $anchor_matches[0] as $anchor ) {
104 if ( 0 == preg_match($search, $anchor, $id_matches) )
107 $id = $id_matches[2];
109 // While we have the attachment ID, let's adopt any orphans.
110 $attachment = & get_post($id, ARRAY_A);
111 if ( ! empty($attachment) && ! is_object(get_post($attachment['post_parent'])) ) {
112 $attachment['post_parent'] = $post_ID;
113 // Escape data pulled from DB.
114 $attachment = add_magic_quotes($attachment);
115 wp_update_post($attachment);
118 $post_search[$i] = $anchor;
119 $post_replace[$i] = preg_replace("#href=(\"|')[^'\"]*\\1#e", "stripslashes('href=\\1').get_attachment_link($id).stripslashes('\\1')", $anchor);
123 $post['post_content'] = str_replace($post_search, $post_replace, $post['post_content']);
125 // Escape data pulled from DB.
126 $post = add_magic_quotes($post);
128 return wp_update_post($post);
131 // Update an existing post with values provided in $_POST.
132 function edit_post() {
135 $post_ID = (int) $_POST['post_ID'];
137 if (!current_user_can('edit_post', $post_ID))
138 die(__('You are not allowed to edit this post.'));
141 $_POST['ID'] = (int) $_POST['post_ID'];
142 $_POST['post_content'] = $_POST['content'];
143 $_POST['post_excerpt'] = $_POST['excerpt'];
144 $_POST['post_parent'] = $_POST['parent_id'];
145 $_POST['to_ping'] = $_POST['trackback_url'];
147 if (!empty ($_POST['post_author_override'])) {
148 $_POST['post_author'] = (int) $_POST['post_author_override'];
150 if (!empty ($_POST['post_author'])) {
151 $_POST['post_author'] = (int) $_POST['post_author'];
153 $_POST['post_author'] = (int) $_POST['user_ID'];
156 if (($_POST['post_author'] != $_POST['user_ID']) && !current_user_can('edit_others_posts'))
157 die(__('You cannot post as this user.'));
159 // What to do based on which button they pressed
160 if ('' != $_POST['saveasdraft'])
161 $_POST['post_status'] = 'draft';
162 if ('' != $_POST['saveasprivate'])
163 $_POST['post_status'] = 'private';
164 if ('' != $_POST['publish'])
165 $_POST['post_status'] = 'publish';
166 if ('' != $_POST['advanced'])
167 $_POST['post_status'] = 'draft';
168 if ('' != $_POST['savepage'])
169 $_POST['post_status'] = 'static';
171 if ('publish' == $_POST['post_status'] && !current_user_can('publish_posts'))
172 $_POST['post_status'] = 'draft';
174 if ('static' == $_POST['post_status'] && !current_user_can('edit_pages'))
175 die(__('This user cannot edit pages.'));
177 if (!isset ($_POST['comment_status']))
178 $_POST['comment_status'] = 'closed';
180 if (!isset ($_POST['ping_status']))
181 $_POST['ping_status'] = 'closed';
183 if (!empty ($_POST['edit_date'])) {
190 $jj = ($jj > 31) ? 31 : $jj;
191 $hh = ($hh > 23) ? $hh -24 : $hh;
192 $mn = ($mn > 59) ? $mn -60 : $mn;
193 $ss = ($ss > 59) ? $ss -60 : $ss;
194 $_POST['post_date'] = "$aa-$mm-$jj $hh:$mn:$ss";
195 $_POST['post_date_gmt'] = get_gmt_from_date("$aa-$mm-$jj $hh:$mn:$ss");
199 if ($_POST['meta']) {
200 foreach ($_POST['meta'] as $key => $value)
201 update_meta($key, $value['key'], $value['value']);
204 if ($_POST['deletemeta']) {
205 foreach ($_POST['deletemeta'] as $key => $value)
211 wp_update_post($_POST);
213 // Now that we have an ID we can fix any attachment anchor hrefs
214 fix_attachment_links($post_ID);
219 function edit_comment() {
222 $comment_ID = (int) $_POST['comment_ID'];
223 $comment_post_ID = (int) $_POST['comment_post_ID'];
225 if (!current_user_can('edit_post', $comment_post_ID))
226 die(__('You are not allowed to edit comments on this post, so you cannot edit this comment.'));
228 $_POST['comment_author'] = $_POST['newcomment_author'];
229 $_POST['comment_author_email'] = $_POST['newcomment_author_email'];
230 $_POST['comment_author_url'] = $_POST['newcomment_author_url'];
231 $_POST['comment_approved'] = $_POST['comment_status'];
232 $_POST['comment_content'] = $_POST['content'];
233 $_POST['comment_ID'] = (int) $_POST['comment_ID'];
235 if (!empty ($_POST['edit_date'])) {
242 $jj = ($jj > 31) ? 31 : $jj;
243 $hh = ($hh > 23) ? $hh -24 : $hh;
244 $mn = ($mn > 59) ? $mn -60 : $mn;
245 $ss = ($ss > 59) ? $ss -60 : $ss;
246 $_POST['comment_date'] = "$aa-$mm-$jj $hh:$mn:$ss";
249 wp_update_comment($_POST);
252 // Get an existing post and format it for editing.
253 function get_post_to_edit($id) {
255 $richedit = ( 'true' == get_user_option('rich_editing') ) ? true : false;
257 $post = get_post($id);
259 $post->post_content = format_to_edit($post->post_content, $richedit);
260 $post->post_content = apply_filters('content_edit_pre', $post->post_content);
262 $post->post_excerpt = format_to_edit($post->post_excerpt);
263 $post->post_excerpt = apply_filters('excerpt_edit_pre', $post->post_excerpt);
265 $post->post_title = format_to_edit($post->post_title);
266 $post->post_title = apply_filters('title_edit_pre', $post->post_title);
268 $post->post_password = format_to_edit($post->post_password);
270 if ($post->post_status == 'static')
271 $post->page_template = get_post_meta($id, '_wp_page_template', true);
276 // Default post information to use when populating the "Write Post" form.
277 function get_default_post_to_edit() {
278 if ( !empty($_REQUEST['post_title']) )
279 $post_title = wp_specialchars(stripslashes($_REQUEST['post_title']));
280 else if ( !empty($_REQUEST['popuptitle']) ) {
281 $post_title = wp_specialchars(stripslashes($_REQUEST['popuptitle']));
282 $post_title = funky_javascript_fix($post_title);
287 if ( !empty($_REQUEST['content']) )
288 $post_content = wp_specialchars(stripslashes($_REQUEST['content']));
289 else if ( !empty($post_title) ) {
290 $text = wp_specialchars(stripslashes(urldecode($_REQUEST['text'])));
291 $text = funky_javascript_fix($text);
292 $popupurl = clean_url(stripslashes($_REQUEST['popupurl']));
293 $post_content = '<a href="'.$popupurl.'">'.$post_title.'</a>'."\n$text";
296 if ( !empty($_REQUEST['excerpt']) )
297 $post_excerpt = wp_specialchars(stripslashes($_REQUEST['excerpt']));
301 $post->post_status = 'draft';
302 $post->comment_status = get_settings('default_comment_status');
303 $post->ping_status = get_settings('default_ping_status');
304 $post->post_pingback = get_settings('default_pingback_flag');
305 $post->post_category = get_settings('default_category');
306 $post->post_content = apply_filters('default_content', $post_content);
307 $post->post_title = apply_filters('default_title', $post_title);
308 $post->post_excerpt = apply_filters('default_excerpt', $post_excerpt);
309 $post->page_template = 'default';
310 $post->post_parent = 0;
311 $post->menu_order = 0;
316 function get_comment_to_edit($id) {
318 $richedit = ( 'true' == get_user_option('rich_editing') ) ? true : false;
320 $comment = get_comment($id);
322 $comment->comment_ID = (int) $comment->comment_ID;
323 $comment->comment_post_ID = (int) $comment->comment_post_ID;
325 $comment->comment_content = format_to_edit($comment->comment_content);
326 $comment->comment_content = apply_filters('comment_edit_pre', $comment->comment_content);
328 $comment->comment_author = format_to_edit($comment->comment_author);
329 $comment->comment_author_email = format_to_edit($comment->comment_author_email);
330 $comment->comment_author_url = clean_url($comment->comment_author_url);
331 $comment->comment_author_url = format_to_edit($comment->comment_author_url);
336 function get_category_to_edit($id) {
337 $category = get_category($id);
342 function get_user_to_edit($user_id) {
343 $user = new WP_User($user_id);
344 $user->user_login = attribute_escape($user->user_login);
345 $user->user_email = attribute_escape($user->user_email);
346 $user->user_url = clean_url($user->user_url);
347 $user->first_name = attribute_escape($user->first_name);
348 $user->last_name = attribute_escape($user->last_name);
349 $user->display_name = attribute_escape($user->display_name);
350 $user->nickname = attribute_escape($user->nickname);
351 $user->aim = attribute_escape($user->aim);
352 $user->yim = attribute_escape($user->yim);
353 $user->jabber = attribute_escape($user->jabber);
354 $user->description = wp_specialchars($user->description);
359 // Creates a new user from the "Users" form using $_POST information.
361 function add_user() {
365 function edit_user($user_id = 0) {
366 global $current_user, $wp_roles, $wpdb;
370 $user->ID = (int) $user_id;
371 $userdata = get_userdata($user_id);
372 $user->user_login = $wpdb->escape($userdata->user_login);
378 if (isset ($_POST['user_login']))
379 $user->user_login = wp_specialchars(trim($_POST['user_login']));
381 $pass1 = $pass2 = '';
382 if (isset ($_POST['pass1']))
383 $pass1 = $_POST['pass1'];
384 if (isset ($_POST['pass2']))
385 $pass2 = $_POST['pass2'];
387 if (isset ($_POST['role']) && current_user_can('edit_users')) {
388 if($user_id != $current_user->id || $wp_roles->role_objects[$_POST['role']]->has_cap('edit_users'))
389 $user->role = $_POST['role'];
392 if (isset ($_POST['email']))
393 $user->user_email = wp_specialchars(trim($_POST['email']));
394 if (isset ($_POST['url'])) {
395 $user->user_url = clean_url(trim($_POST['url']));
396 $user->user_url = preg_match('/^(https?|ftps?|mailto|news|gopher):/is', $user->user_url) ? $user->user_url : 'http://'.$user->user_url;
398 if (isset ($_POST['first_name']))
399 $user->first_name = wp_specialchars(trim($_POST['first_name']));
400 if (isset ($_POST['last_name']))
401 $user->last_name = wp_specialchars(trim($_POST['last_name']));
402 if (isset ($_POST['nickname']))
403 $user->nickname = wp_specialchars(trim($_POST['nickname']));
404 if (isset ($_POST['display_name']))
405 $user->display_name = wp_specialchars(trim($_POST['display_name']));
406 if (isset ($_POST['description']))
407 $user->description = trim($_POST['description']);
408 if (isset ($_POST['jabber']))
409 $user->jabber = wp_specialchars(trim($_POST['jabber']));
410 if (isset ($_POST['aim']))
411 $user->aim = wp_specialchars(trim($_POST['aim']));
412 if (isset ($_POST['yim']))
413 $user->yim = wp_specialchars(trim($_POST['yim']));
417 /* checking that username has been typed */
418 if ($user->user_login == '')
419 $errors['user_login'] = __('<strong>ERROR</strong>: Please enter a username.');
421 /* checking the password has been typed twice */
422 do_action('check_passwords', array ($user->user_login, & $pass1, & $pass2));
425 if ($pass1 == '' || $pass2 == '')
426 $errors['pass'] = __('<strong>ERROR</strong>: Please enter your password twice.');
428 if ((empty ($pass1) && !empty ($pass2)) || (empty ($pass2) && !empty ($pass1)))
429 $errors['pass'] = __("<strong>ERROR</strong>: you typed your new password only once.");
432 /* Check for "\" in password */
433 if( strpos( " ".$pass1, "\\" ) )
434 $errors['pass'] = __('<strong>ERROR</strong>: Passwords may not contain the character "\\".');
436 /* checking the password has been typed twice the same */
437 if ($pass1 != $pass2)
438 $errors['pass'] = __('<strong>ERROR</strong>: Please type the same password in the two password fields.');
441 $user->user_pass = $pass1;
443 if ( !validate_username($user->user_login) )
444 $errors['user_login'] = __('<strong>ERROR</strong>: This username is invalid. Please enter a valid username.');
446 if (!$update && username_exists($user->user_login))
447 $errors['user_login'] = __('<strong>ERROR</strong>: This username is already registered, please choose another one.');
449 /* checking e-mail address */
450 if (empty ($user->user_email)) {
451 $errors['user_email'] = __("<strong>ERROR</strong>: please type an e-mail address");
453 if (!is_email($user->user_email)) {
454 $errors['user_email'] = __("<strong>ERROR</strong>: the email address isn't correct");
457 if (count($errors) != 0)
461 $user_id = wp_update_user(get_object_vars($user));
463 $user_id = wp_insert_user(get_object_vars($user));
464 wp_new_user_notification($user_id);
471 function get_link_to_edit($link_id) {
472 $link = get_link($link_id);
474 $link->link_url = clean_url($link->link_url);
475 $link->link_name = attribute_escape($link->link_name);
476 $link->link_image = attribute_escape($link->link_image);
477 $link->link_description = attribute_escape($link->link_description);
478 $link->link_rss = clean_url($link->link_rss);
479 $link->link_rel = attribute_escape($link->link_rel);
480 $link->link_notes = wp_specialchars($link->link_notes);
481 $link->post_category = $link->link_category;
486 function get_default_link_to_edit() {
487 if ( isset($_GET['linkurl']) )
488 $link->link_url = clean_url($_GET['linkurl']);
490 $link->link_url = '';
492 if ( isset($_GET['name']) )
493 $link->link_name = attribute_escape($_GET['name']);
495 $link->link_name = '';
500 function add_link() {
504 function edit_link($link_id = '') {
505 if (!current_user_can('manage_links'))
506 die(__("Cheatin' uh ?"));
508 $_POST['link_url'] = wp_specialchars($_POST['link_url']);
509 $_POST['link_url'] = clean_url($_POST['link_url']);
510 $_POST['link_name'] = wp_specialchars($_POST['link_name']);
511 $_POST['link_image'] = wp_specialchars($_POST['link_image']);
512 $_POST['link_rss'] = clean_url($_POST['link_rss']);
513 $auto_toggle = get_autotoggle($_POST['link_category']);
515 // if we are in an auto toggle category and this one is visible then we
516 // need to make the others invisible before we add this new one.
517 // FIXME Add category toggle func.
518 //if (($auto_toggle == 'Y') && ($link_visible == 'Y')) {
519 // $wpdb->query("UPDATE $wpdb->links set link_visible = 'N' WHERE link_category = $link_category");
522 if ( !empty($link_id) ) {
523 $_POST['link_id'] = $link_id;
524 return wp_update_link($_POST);
526 return wp_insert_link($_POST);
530 function url_shorten($url) {
531 $short_url = str_replace('http://', '', stripslashes($url));
532 $short_url = str_replace('www.', '', $short_url);
533 if ('/' == substr($short_url, -1))
534 $short_url = substr($short_url, 0, -1);
535 if (strlen($short_url) > 35)
536 $short_url = substr($short_url, 0, 32).'...';
540 function selected($selected, $current) {
541 if ($selected == $current)
542 echo ' selected="selected"';
545 function checked($checked, $current) {
546 if ($checked == $current)
547 echo ' checked="checked"';
550 function return_categories_list($parent = 0) {
552 return $wpdb->get_col("SELECT cat_ID FROM $wpdb->categories WHERE category_parent = $parent ORDER BY category_count DESC");
555 function sort_cats($cat1, $cat2) {
556 return strcasecmp($cat1['cat_name'], $cat2['cat_name']);
559 function get_nested_categories($default = 0, $parent = 0) {
560 global $post_ID, $mode, $wpdb;
563 $checked_categories = $wpdb->get_col("
565 FROM $wpdb->categories, $wpdb->post2cat
566 WHERE $wpdb->post2cat.category_id = cat_ID AND $wpdb->post2cat.post_id = '$post_ID'
569 if (count($checked_categories) == 0) {
570 // No selected categories, strange
571 $checked_categories[] = $default;
575 $checked_categories[] = $default;
578 $cats = return_categories_list($parent);
581 if (is_array($cats)) {
582 foreach ($cats as $cat) {
583 $result[$cat]['children'] = get_nested_categories($default, $cat);
584 $result[$cat]['cat_ID'] = $cat;
585 $result[$cat]['checked'] = in_array($cat, $checked_categories);
586 $result[$cat]['cat_name'] = get_the_category_by_ID($cat);
590 usort($result, 'sort_cats');
595 function write_nested_categories($categories) {
596 foreach ($categories as $category) {
597 echo '<label for="category-', $category['cat_ID'], '" class="selectit"><input value="', $category['cat_ID'], '" type="checkbox" name="post_category[]" id="category-', $category['cat_ID'], '"', ($category['checked'] ? ' checked="checked"' : ""), '/> ', wp_specialchars($category['cat_name']), "</label>\n";
599 if (isset ($category['children'])) {
600 echo "\n<span class='cat-nest'>\n";
601 write_nested_categories($category['children']);
607 function dropdown_categories($default = 0) {
608 write_nested_categories(get_nested_categories($default));
611 // Dandy new recursive multiple category stuff.
612 function cat_rows($parent = 0, $level = 0, $categories = 0) {
613 global $wpdb, $class;
616 $categories = $wpdb->get_results("SELECT * FROM $wpdb->categories ORDER BY cat_name");
619 foreach ($categories as $category) {
620 if ($category->category_parent == $parent) {
621 $category->cat_name = wp_specialchars($category->cat_name);
622 $pad = str_repeat('— ', $level);
623 if ( current_user_can('manage_categories') ) {
624 $edit = "<a href='categories.php?action=edit&cat_ID=$category->cat_ID' class='edit'>".__('Edit')."</a></td>";
625 $default_cat_id = get_option('default_category');
627 if ($category->cat_ID != $default_cat_id)
628 $edit .= "<td><a href='" . wp_nonce_url("categories.php?action=delete&cat_ID=$category->cat_ID", 'delete-category_' . $category->cat_ID ) . "' onclick=\"return deleteSomething( 'cat', $category->cat_ID, '" . sprintf(__("You are about to delete the category "%s". All of its posts will go to the default category.\\n"OK" to delete, "Cancel" to stop."), js_escape($category->cat_name))."' );\" class='delete'>".__('Delete')."</a>";
630 $edit .= "<td style='text-align:center'>".__("Default");
635 $class = ('alternate' == $class) ? '' : 'alternate';
636 echo "<tr id='cat-$category->cat_ID' class='$class'><th scope='row'>$category->cat_ID</th><td>$pad $category->cat_name</td>
637 <td>$category->category_description</td>
638 <td>$category->category_count</td>
641 cat_rows($category->cat_ID, $level +1, $categories);
649 function page_rows($parent = 0, $level = 0, $pages = 0) {
650 global $wpdb, $class, $post;
652 $pages = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_status = 'static' ORDER BY menu_order");
655 foreach ($pages as $post) {
657 if ($post->post_parent == $parent) {
658 $post->post_title = wp_specialchars($post->post_title);
659 $pad = str_repeat('— ', $level);
661 $class = ('alternate' == $class) ? '' : 'alternate';
663 <tr id='page-<?php echo $id; ?>' class='<?php echo $class; ?>'>
664 <th scope="row"><?php echo $post->ID; ?></th>
666 <?php echo $pad; ?><?php the_title() ?>
668 <td><?php the_author() ?></td>
669 <td><?php echo mysql2date('Y-m-d g:i a', $post->post_modified); ?></td>
670 <td><a href="<?php the_permalink(); ?>" rel="permalink" class="edit"><?php _e('View'); ?></a></td>
671 <td><?php if ( current_user_can('edit_pages') ) { echo "<a href='post.php?action=edit&post=$id' class='edit'>" . __('Edit') . "</a>"; } ?></td>
672 <td><?php if ( current_user_can('edit_pages') ) { echo "<a href='" . wp_nonce_url("post.php?action=delete&post=$id", 'delete-post_' . $id) . "' class='delete' onclick=\"return deleteSomething( 'page', " . $id . ", '" . sprintf(__("You are about to delete the "%s" page.\\n"OK" to delete, "Cancel" to stop."), js_escape(get_the_title()) ) . "' );\">" . __('Delete') . "</a>"; } ?></td>
677 page_rows($id, $level +1, $pages);
685 function wp_dropdown_cats($currentcat = 0, $currentparent = 0, $parent = 0, $level = 0, $categories = 0) {
686 global $wpdb, $bgcolor;
688 $categories = $wpdb->get_results("SELECT * FROM $wpdb->categories ORDER BY cat_name");
691 foreach ($categories as $category) {
692 if ($currentcat != $category->cat_ID && $parent == $category->category_parent) {
693 $count = $wpdb->get_var("SELECT COUNT(post_id) FROM $wpdb->post2cat WHERE category_id = $category->cat_ID");
694 $pad = str_repeat('– ', $level);
695 $category->cat_name = wp_specialchars($category->cat_name);
696 echo "\n\t<option value='$category->cat_ID'";
697 if ($currentparent == $category->cat_ID)
698 echo " selected='selected'";
699 echo ">$pad$category->cat_name</option>";
700 wp_dropdown_cats($currentcat, $currentparent, $category->cat_ID, $level +1, $categories);
708 function link_category_dropdown($fieldname, $selected = 0) {
711 $results = $wpdb->get_results("SELECT cat_id, cat_name, auto_toggle FROM $wpdb->linkcategories ORDER BY cat_id");
712 echo "\n<select name='$fieldname' size='1'>\n";
713 foreach ($results as $row) {
714 echo "\n\t<option value='$row->cat_id'";
715 if ($row->cat_id == $selected)
716 echo " selected='selected'";
717 echo ">$row->cat_id : " . wp_specialchars($row->cat_name);
718 if ($row->auto_toggle == 'Y')
719 echo ' (auto toggle)';
722 echo "\n</select>\n";
725 function wp_create_thumbnail($file, $max_side, $effect = '') {
727 // 1 = GIF, 2 = JPEG, 3 = PNG
729 if (file_exists($file)) {
730 $type = getimagesize($file);
732 // if the associated function doesn't exist - then it's not
733 // handle. duh. i hope.
735 if (!function_exists('imagegif') && $type[2] == 1) {
736 $error = __('Filetype not supported. Thumbnail not created.');
738 elseif (!function_exists('imagejpeg') && $type[2] == 2) {
739 $error = __('Filetype not supported. Thumbnail not created.');
741 elseif (!function_exists('imagepng') && $type[2] == 3) {
742 $error = __('Filetype not supported. Thumbnail not created.');
745 // create the initial copy from the original file
747 $image = imagecreatefromgif($file);
749 elseif ($type[2] == 2) {
750 $image = imagecreatefromjpeg($file);
752 elseif ($type[2] == 3) {
753 $image = imagecreatefrompng($file);
756 if (function_exists('imageantialias'))
757 imageantialias($image, TRUE);
759 $image_attr = getimagesize($file);
761 // figure out the longest side
763 if ($image_attr[0] > $image_attr[1]) {
764 $image_width = $image_attr[0];
765 $image_height = $image_attr[1];
766 $image_new_width = $max_side;
768 $image_ratio = $image_width / $image_new_width;
769 $image_new_height = $image_height / $image_ratio;
772 $image_width = $image_attr[0];
773 $image_height = $image_attr[1];
774 $image_new_height = $max_side;
776 $image_ratio = $image_height / $image_new_height;
777 $image_new_width = $image_width / $image_ratio;
781 $thumbnail = imagecreatetruecolor($image_new_width, $image_new_height);
782 @ imagecopyresampled($thumbnail, $image, 0, 0, 0, 0, $image_new_width, $image_new_height, $image_attr[0], $image_attr[1]);
784 // If no filters change the filename, we'll do a default transformation.
785 if ( basename($file) == $thumb = apply_filters('thumbnail_filename', basename($file)) )
786 $thumb = preg_replace('!(\.[^.]+)?$!', __('.thumbnail').'$1', basename($file), 1);
788 $thumbpath = str_replace(basename($file), $thumb, $file);
790 // move the thumbnail to it's final destination
792 if (!imagegif($thumbnail, $thumbpath)) {
793 $error = __("Thumbnail path invalid");
796 elseif ($type[2] == 2) {
797 if (!imagejpeg($thumbnail, $thumbpath)) {
798 $error = __("Thumbnail path invalid");
801 elseif ($type[2] == 3) {
802 if (!imagepng($thumbnail, $thumbpath)) {
803 $error = __("Thumbnail path invalid");
809 $error = __('File not found');
812 if (!empty ($error)) {
819 // Some postmeta stuff
820 function has_meta($postid) {
823 return $wpdb->get_results("
824 SELECT meta_key, meta_value, meta_id, post_id
826 WHERE post_id = '$postid'
827 ORDER BY meta_key,meta_id", ARRAY_A);
831 function list_meta($meta) {
838 <table id='meta-list' cellpadding="3">
840 <th><?php _e('Key') ?></th>
841 <th><?php _e('Value') ?></th>
842 <th colspan='2'><?php _e('Action') ?></th>
847 foreach ($meta as $entry) {
850 $style = 'alternate';
853 if ('_' == $entry['meta_key'] { 0 })
856 if ( is_serialized($entry['meta_value']) ) {
857 if ( is_serialized_string($entry['meta_value']) ) {
858 // this is a serialized string, so we should display it
859 $entry['meta_value'] = maybe_unserialize($entry['meta_value']);
861 // this is a serialized array/object so we should NOT display it
867 $entry['meta_key'] = attribute_escape( $entry['meta_key']);
868 $entry['meta_value'] = attribute_escape( $entry['meta_value']);
869 $entry['meta_id'] = (int) $entry['meta_id'];
872 <td valign='top'><input name='meta[{$entry['meta_id']}][key]' tabindex='6' type='text' size='20' value='{$entry['meta_key']}' /></td>
873 <td><textarea name='meta[{$entry['meta_id']}][value]' tabindex='6' rows='2' cols='30'>{$entry['meta_value']}</textarea></td>
874 <td align='center'><input name='updatemeta' type='submit' class='updatemeta' tabindex='6' value='".attribute_escape(__('Update'))."' /><br />
875 <input name='deletemeta[{$entry['meta_id']}]' type='submit' class='deletemeta' tabindex='6' value='".attribute_escape(__('Delete'))."' /></td>
884 // Get a list of previously defined keys
885 function get_meta_keys() {
888 $keys = $wpdb->get_col("
897 function meta_form() {
899 $keys = $wpdb->get_col("
903 ORDER BY meta_id DESC
906 <h3><?php _e('Add a new custom field:') ?></h3>
907 <table cellspacing="3" cellpadding="3">
909 <th colspan="2"><?php _e('Key') ?></th>
910 <th><?php _e('Value') ?></th>
913 <td align="right" width="18%">
914 <?php if ($keys) : ?>
915 <select id="metakeyselect" name="metakeyselect" tabindex="7">
916 <option value="#NONE#"><?php _e('- Select -'); ?></option>
919 foreach ($keys as $key) {
920 $key = attribute_escape($key);
921 echo "\n\t<option value='$key'>$key</option>";
924 </select> <?php _e('or'); ?>
927 <td><input type="text" id="metakeyinput" name="metakeyinput" tabindex="7" /></td>
928 <td><textarea id="metavalue" name="metavalue" rows="3" cols="25" tabindex="8"></textarea></td>
932 <p class="submit"><input type="submit" name="updatemeta" tabindex="9" value="<?php _e('Add Custom Field »') ?>" /></p>
937 function add_meta($post_ID) {
939 $post_ID = (int) $post_ID;
941 $protected = array( '_wp_attached_file', '_wp_attachment_metadata', '_wp_old_slug', '_wp_page_template' );
943 $metakeyselect = $wpdb->escape(stripslashes(trim($_POST['metakeyselect'])));
944 $metakeyinput = $wpdb->escape(stripslashes(trim($_POST['metakeyinput'])));
945 $metavalue = maybe_serialize(stripslashes((trim($_POST['metavalue']))));
946 $metavalue = $wpdb->escape($metavalue);
948 if ( ('0' === $metavalue || !empty ($metavalue)) && ((('#NONE#' != $metakeyselect) && !empty ($metakeyselect)) || !empty ($metakeyinput)) ) {
949 // We have a key/value pair. If both the select and the
950 // input for the key have data, the input takes precedence:
952 if ('#NONE#' != $metakeyselect)
953 $metakey = $metakeyselect;
956 $metakey = $metakeyinput; // default
958 if ( in_array($metakey, $protected) )
961 $result = $wpdb->query("
962 INSERT INTO $wpdb->postmeta
963 (post_id,meta_key,meta_value)
964 VALUES ('$post_ID','$metakey','$metavalue')
969 function delete_meta($mid) {
973 $result = $wpdb->query("DELETE FROM $wpdb->postmeta WHERE meta_id = '$mid'");
976 function update_meta($mid, $mkey, $mvalue) {
979 $protected = array( '_wp_attached_file', '_wp_attachment_metadata', '_wp_old_slug', '_wp_page_template' );
981 if ( in_array($mkey, $protected) )
984 $mvalue = maybe_serialize(stripslashes($mvalue));
985 $mvalue = $wpdb->escape($mvalue);
987 return $wpdb->query("UPDATE $wpdb->postmeta SET meta_key = '$mkey', meta_value = '$mvalue' WHERE meta_id = '$mid'");
990 function touch_time($edit = 1, $for_post = 1) {
991 global $month, $post, $comment;
994 $edit = ( ('draft' == $post->post_status) && (!$post->post_date || '0000-00-00 00:00:00' == $post->post_date) ) ? false : true;
996 echo '<fieldset><legend><input type="checkbox" class="checkbox" name="edit_date" value="1" id="timestamp" /> <label for="timestamp">'.__('Edit timestamp').'</label></legend>';
998 $time_adj = time() + (get_settings('gmt_offset') * 3600);
999 $post_date = ($for_post) ? $post->post_date : $comment->comment_date;
1000 $jj = ($edit) ? mysql2date('d', $post_date) : gmdate('d', $time_adj);
1001 $mm = ($edit) ? mysql2date('m', $post_date) : gmdate('m', $time_adj);
1002 $aa = ($edit) ? mysql2date('Y', $post_date) : gmdate('Y', $time_adj);
1003 $hh = ($edit) ? mysql2date('H', $post_date) : gmdate('H', $time_adj);
1004 $mn = ($edit) ? mysql2date('i', $post_date) : gmdate('i', $time_adj);
1005 $ss = ($edit) ? mysql2date('s', $post_date) : gmdate('s', $time_adj);
1007 echo "<select name=\"mm\">\n";
1008 for ($i = 1; $i < 13; $i = $i +1) {
1009 echo "\t\t\t<option value=\"$i\"";
1011 echo " selected='selected'";
1017 echo ">".$month["$ii"]."</option>\n";
1021 <input type="text" id="jj" name="jj" value="<?php echo $jj; ?>" size="2" maxlength="2" />
1022 <input type="text" id="aa" name="aa" value="<?php echo $aa ?>" size="4" maxlength="5" /> @
1023 <input type="text" id="hh" name="hh" value="<?php echo $hh ?>" size="2" maxlength="2" /> :
1024 <input type="text" id="mn" name="mn" value="<?php echo $mn ?>" size="2" maxlength="2" />
1025 <input type="hidden" id="ss" name="ss" value="<?php echo $ss ?>" size="2" maxlength="2" />
1028 _e('Existing timestamp');
1029 echo ": {$month[$mm]} $jj, $aa @ $hh:$mn";
1037 // insert_with_markers: Owen Winkler, fixed by Eric Anderson
1038 // Inserts an array of strings into a file (.htaccess), placing it between
1039 // BEGIN and END markers. Replaces existing marked info. Retains surrounding
1040 // data. Creates file if none exists.
1041 // Returns true on write success, false on failure.
1042 function insert_with_markers($filename, $marker, $insertion) {
1043 if (!file_exists($filename) || is_writeable($filename)) {
1044 if (!file_exists($filename)) {
1047 $markerdata = explode("\n", implode('', file($filename)));
1050 $f = fopen($filename, 'w');
1054 foreach ($markerdata as $markerline) {
1055 if (strstr($markerline, "# BEGIN {$marker}"))
1058 fwrite($f, "{$markerline}\n");
1059 if (strstr($markerline, "# END {$marker}")) {
1060 fwrite($f, "# BEGIN {$marker}\n");
1061 if (is_array($insertion))
1062 foreach ($insertion as $insertline)
1063 fwrite($f, "{$insertline}\n");
1064 fwrite($f, "# END {$marker}\n");
1071 fwrite($f, "# BEGIN {$marker}\n");
1072 foreach ($insertion as $insertline)
1073 fwrite($f, "{$insertline}\n");
1074 fwrite($f, "# END {$marker}\n");
1083 // extract_from_markers: Owen Winkler
1084 // Returns an array of strings from a file (.htaccess) from between BEGIN
1086 function extract_from_markers($filename, $marker) {
1089 if (!file_exists($filename)) {
1093 if ($markerdata = explode("\n", implode('', file($filename))));
1096 foreach ($markerdata as $markerline) {
1097 if (strstr($markerline, "# END {$marker}"))
1100 $result[] = $markerline;
1101 if (strstr($markerline, "# BEGIN {$marker}"))
1109 function got_mod_rewrite() {
1112 // take 3 educated guesses as to whether or not mod_rewrite is available
1116 if ( function_exists('apache_get_modules') ) {
1117 if ( !in_array('mod_rewrite', apache_get_modules()) )
1124 function save_mod_rewrite_rules() {
1125 global $is_apache, $wp_rewrite;
1126 $home_path = get_home_path();
1128 if (!$wp_rewrite->using_mod_rewrite_permalinks())
1131 if (!((!file_exists($home_path.'.htaccess') && is_writable($home_path)) || is_writable($home_path.'.htaccess')))
1134 if (! got_mod_rewrite())
1137 $rules = explode("\n", $wp_rewrite->mod_rewrite_rules());
1138 insert_with_markers($home_path.'.htaccess', 'WordPress', $rules);
1141 function the_quicktags() {
1143 <div id="quicktags">
1144 <script src="../wp-includes/js/quicktags.js" type="text/javascript"></script>
1145 <script type="text/javascript">if ( typeof tinyMCE == "undefined" || tinyMCE.configs.length < 1 ) edToolbar();</script>
1149 <script type="text/javascript">
1150 function edInsertContent(myField, myValue) {
1152 if (document.selection) {
1154 sel = document.selection.createRange();
1158 //MOZILLA/NETSCAPE support
1159 else if (myField.selectionStart || myField.selectionStart == "0") {
1160 var startPos = myField.selectionStart;
1161 var endPos = myField.selectionEnd;
1162 myField.value = myField.value.substring(0, startPos)
1164 + myField.value.substring(endPos, myField.value.length);
1166 myField.selectionStart = startPos + myValue.length;
1167 myField.selectionEnd = startPos + myValue.length;
1169 myField.value += myValue;
1177 function validate_current_theme() {
1178 $theme_loc = 'wp-content/themes';
1179 $theme_root = ABSPATH.$theme_loc;
1181 $template = get_settings('template');
1182 $stylesheet = get_settings('stylesheet');
1184 if (($template != 'default') && (!file_exists("$theme_root/$template/index.php"))) {
1185 update_option('template', 'default');
1186 update_option('stylesheet', 'default');
1187 do_action('switch_theme', 'Default');
1191 if (($stylesheet != 'default') && (!file_exists("$theme_root/$stylesheet/style.css"))) {
1192 update_option('template', 'default');
1193 update_option('stylesheet', 'default');
1194 do_action('switch_theme', 'Default');
1201 function get_broken_themes() {
1202 global $wp_broken_themes;
1205 return $wp_broken_themes;
1208 function get_page_templates() {
1209 $themes = get_themes();
1210 $theme = get_current_theme();
1211 $templates = $themes[$theme]['Template Files'];
1212 $page_templates = array ();
1214 if (is_array($templates)) {
1215 foreach ($templates as $template) {
1216 $template_data = implode('', file(ABSPATH.$template));
1217 preg_match("|Template Name:(.*)|i", $template_data, $name);
1218 preg_match("|Description:(.*)|i", $template_data, $description);
1221 $description = $description[1];
1223 if (!empty ($name)) {
1224 $page_templates[trim($name)] = basename($template);
1229 return $page_templates;
1232 function page_template_dropdown($default = '') {
1233 $templates = get_page_templates();
1234 foreach (array_keys($templates) as $template)
1235 : if ($default == $templates[$template])
1236 $selected = " selected='selected'";
1239 echo "\n\t<option value='".$templates[$template]."' $selected>$template</option>";
1243 function parent_dropdown($default = 0, $parent = 0, $level = 0) {
1244 global $wpdb, $post_ID;
1245 $items = $wpdb->get_results("SELECT ID, post_parent, post_title FROM $wpdb->posts WHERE post_parent = $parent AND post_status = 'static' ORDER BY menu_order");
1248 foreach ($items as $item) {
1249 // A page cannot be it's own parent.
1250 if (!empty ($post_ID)) {
1251 if ($item->ID == $post_ID) {
1255 $pad = str_repeat(' ', $level * 3);
1256 if ($item->ID == $default)
1257 $current = ' selected="selected"';
1261 echo "\n\t<option value='$item->ID'$current>$pad $item->post_title</option>";
1262 parent_dropdown($default, $item->ID, $level +1);
1269 function user_can_access_admin_page() {
1273 global $plugin_page;
1275 $parent = get_admin_page_parent();
1277 foreach ($menu as $menu_array) {
1278 //echo "parent array: " . $menu_array[2];
1279 if ($menu_array[2] == $parent) {
1280 if (!current_user_can($menu_array[1])) {
1288 if (isset ($submenu[$parent])) {
1289 if ( isset($plugin_page) ) {
1290 foreach ($submenu[$parent] as $submenu_array) {
1291 if ( $submenu_array[2] == $plugin_page ) {
1292 if (!current_user_can($submenu_array[1]))
1298 foreach ($submenu[$parent] as $submenu_array) {
1299 if ($submenu_array[2] == $pagenow) {
1300 if (!current_user_can($submenu_array[1]))
1311 function get_admin_page_title() {
1316 global $plugin_page;
1318 if (isset ($title) && !empty ($title)) {
1322 $hook = get_plugin_page_hook($plugin_page, $pagenow);
1324 $parent = $parent1 = get_admin_page_parent();
1325 if (empty ($parent)) {
1326 foreach ($menu as $menu_array) {
1327 if (isset ($menu_array[3])) {
1328 if ($menu_array[2] == $pagenow) {
1329 $title = $menu_array[3];
1330 return $menu_array[3];
1332 if (isset ($plugin_page) && ($plugin_page == $menu_array[2]) && ($hook == $menu_array[3])) {
1333 $title = $menu_array[3];
1334 return $menu_array[3];
1339 foreach (array_keys($submenu) as $parent) {
1340 foreach ($submenu[$parent] as $submenu_array) {
1341 if (isset ($submenu_array[3])) {
1342 if ($submenu_array[2] == $pagenow) {
1343 $title = $submenu_array[3];
1344 return $submenu_array[3];
1346 if (isset ($plugin_page) && ($plugin_page == $submenu_array[2]) && (($parent == $pagenow) || ($parent == $plugin_page) || ($plugin_page == $hook) || (($pagenow == 'admin.php') && ($parent1 != $submenu_array[2])))) {
1347 $title = $submenu_array[3];
1348 return $submenu_array[3];
1358 function get_admin_page_parent() {
1359 global $parent_file;
1363 global $plugin_page;
1365 if (isset ($parent_file) && !empty ($parent_file)) {
1366 return $parent_file;
1369 if ($pagenow == 'admin.php' && isset ($plugin_page)) {
1370 foreach ($menu as $parent_menu) {
1371 if ($parent_menu[2] == $plugin_page) {
1372 $parent_file = $plugin_page;
1373 return $plugin_page;
1378 foreach (array_keys($submenu) as $parent) {
1379 foreach ($submenu[$parent] as $submenu_array) {
1380 if ($submenu_array[2] == $pagenow) {
1381 $parent_file = $parent;
1384 if (isset ($plugin_page) && ($plugin_page == $submenu_array[2])) {
1385 $parent_file = $parent;
1395 function add_menu_page($page_title, $menu_title, $access_level, $file, $function = '') {
1396 global $menu, $admin_page_hooks;
1398 $file = plugin_basename($file);
1400 $menu[] = array ($menu_title, $access_level, $file, $page_title);
1402 $admin_page_hooks[$file] = sanitize_title($menu_title);
1404 $hookname = get_plugin_page_hookname($file, '');
1405 if (!empty ($function) && !empty ($hookname))
1406 add_action($hookname, $function);
1411 function add_submenu_page($parent, $page_title, $menu_title, $access_level, $file, $function = '') {
1415 $parent = plugin_basename($parent);
1416 $file = plugin_basename($file);
1418 // If the parent doesn't already have a submenu, add a link to the parent
1419 // as the first item in the submenu. If the submenu file is the same as the
1420 // parent file someone is trying to link back to the parent manually. In
1421 // this case, don't automatically add a link back to avoid duplication.
1422 if (!isset ($submenu[$parent]) && $file != $parent) {
1423 foreach ($menu as $parent_menu) {
1424 if ($parent_menu[2] == $parent) {
1425 $submenu[$parent][] = $parent_menu;
1430 $submenu[$parent][] = array ($menu_title, $access_level, $file, $page_title);
1432 $hookname = get_plugin_page_hookname($file, $parent);
1433 if (!empty ($function) && !empty ($hookname))
1434 add_action($hookname, $function);
1439 function add_options_page($page_title, $menu_title, $access_level, $file, $function = '') {
1440 return add_submenu_page('options-general.php', $page_title, $menu_title, $access_level, $file, $function);
1443 function add_management_page($page_title, $menu_title, $access_level, $file, $function = '') {
1444 return add_submenu_page('edit.php', $page_title, $menu_title, $access_level, $file, $function);
1447 function add_theme_page($page_title, $menu_title, $access_level, $file, $function = '') {
1448 return add_submenu_page('themes.php', $page_title, $menu_title, $access_level, $file, $function);
1451 function validate_file($file, $allowed_files = '') {
1452 if (false !== strpos($file, './'))
1455 if (':' == substr($file, 1, 1))
1458 if (!empty ($allowed_files) && (!in_array($file, $allowed_files)))
1464 function validate_file_to_edit($file, $allowed_files = '') {
1465 $file = stripslashes($file);
1467 $code = validate_file($file, $allowed_files);
1474 die(__('Sorry, can’t edit files with ".." in the name. If you are trying to edit a file in your WordPress home directory, you can just type the name of the file in.'));
1477 die(__('Sorry, can’t call files with their real path.'));
1480 die(__('Sorry, that file cannot be edited.'));
1484 function get_home_path() {
1485 $home = get_settings('home');
1486 if ($home != '' && $home != get_settings('siteurl')) {
1487 $home_path = parse_url($home);
1488 $home_path = $home_path['path'];
1489 $root = str_replace($_SERVER["PHP_SELF"], '', $_SERVER["SCRIPT_FILENAME"]);
1490 $home_path = trailingslashit($root.$home_path);
1492 $home_path = ABSPATH;
1498 function get_real_file_to_edit($file) {
1499 if ('index.php' == $file || '.htaccess' == $file) {
1500 $real_file = get_home_path().$file;
1502 $real_file = ABSPATH.$file;
1508 $wp_file_descriptions = array ('index.php' => __('Main Index Template'), 'style.css' => __('Stylesheet'), 'comments.php' => __('Comments'), 'comments-popup.php' => __('Popup Comments'), 'footer.php' => __('Footer'), 'header.php' => __('Header'), 'sidebar.php' => __('Sidebar'), 'archive.php' => __('Archives'), 'category.php' => __('Category Template'), 'page.php' => __('Page Template'), 'search.php' => __('Search Results'), 'single.php' => __('Single Post'), '404.php' => __('404 Template'), 'my-hacks.php' => __('my-hacks.php (legacy hacks support)'), '.htaccess' => __('.htaccess (for rewrite rules)'),
1510 'wp-layout.css' => __('Stylesheet'), 'wp-comments.php' => __('Comments Template'), 'wp-comments-popup.php' => __('Popup Comments Template'));
1512 function get_file_description($file) {
1513 global $wp_file_descriptions;
1515 if (isset ($wp_file_descriptions[basename($file)])) {
1516 return $wp_file_descriptions[basename($file)];
1518 elseif (file_exists(ABSPATH.$file)) {
1519 $template_data = implode('', file(ABSPATH.$file));
1520 if (preg_match("|Template Name:(.*)|i", $template_data, $name))
1524 return basename($file);
1527 function update_recently_edited($file) {
1528 $oldfiles = (array) get_option('recently_edited');
1530 $oldfiles = array_reverse($oldfiles);
1531 $oldfiles[] = $file;
1532 $oldfiles = array_reverse($oldfiles);
1533 $oldfiles = array_unique($oldfiles);
1534 if (5 < count($oldfiles))
1535 array_pop($oldfiles);
1537 $oldfiles[] = $file;
1539 update_option('recently_edited', $oldfiles);
1542 function get_plugin_data($plugin_file) {
1543 $plugin_data = implode('', file($plugin_file));
1544 preg_match("|Plugin Name:(.*)|i", $plugin_data, $plugin_name);
1545 preg_match("|Plugin URI:(.*)|i", $plugin_data, $plugin_uri);
1546 preg_match("|Description:(.*)|i", $plugin_data, $description);
1547 preg_match("|Author:(.*)|i", $plugin_data, $author_name);
1548 preg_match("|Author URI:(.*)|i", $plugin_data, $author_uri);
1549 if (preg_match("|Version:(.*)|i", $plugin_data, $version))
1550 $version = trim($version[1]);
1554 $description = wptexturize(trim($description[1]));
1556 $name = $plugin_name[1];
1557 $name = trim($name);
1559 if ('' != $plugin_uri[1] && '' != $name) {
1560 $plugin = '<a href="' . trim($plugin_uri[1]) . '" title="'.__('Visit plugin homepage').'">'.$plugin.'</a>';
1563 if ('' == $author_uri[1]) {
1564 $author = trim($author_name[1]);
1566 $author = '<a href="' . trim($author_uri[1]) . '" title="'.__('Visit author homepage').'">' . trim($author_name[1]) . '</a>';
1569 return array ('Name' => $name, 'Title' => $plugin, 'Description' => $description, 'Author' => $author, 'Version' => $version, 'Template' => $template[1]);
1572 function get_plugins() {
1575 if (isset ($wp_plugins)) {
1579 $wp_plugins = array ();
1580 $plugin_loc = 'wp-content/plugins';
1581 $plugin_root = ABSPATH.$plugin_loc;
1583 // Files in wp-content/plugins directory
1584 $plugins_dir = @ dir($plugin_root);
1586 while (($file = $plugins_dir->read()) !== false) {
1587 if (preg_match('|^\.+$|', $file))
1589 if (is_dir($plugin_root.'/'.$file)) {
1590 $plugins_subdir = @ dir($plugin_root.'/'.$file);
1591 if ($plugins_subdir) {
1592 while (($subfile = $plugins_subdir->read()) !== false) {
1593 if (preg_match('|^\.+$|', $subfile))
1595 if (preg_match('|\.php$|', $subfile))
1596 $plugin_files[] = "$file/$subfile";
1600 if (preg_match('|\.php$|', $file))
1601 $plugin_files[] = $file;
1606 if ( !$plugins_dir || !$plugin_files )
1609 foreach ( $plugin_files as $plugin_file ) {
1610 if ( !is_readable("$plugin_root/$plugin_file"))
1613 $plugin_data = get_plugin_data("$plugin_root/$plugin_file");
1615 if ( empty ($plugin_data['Name']) )
1618 $wp_plugins[plugin_basename($plugin_file)] = $plugin_data;
1621 uasort($wp_plugins, create_function('$a, $b', 'return strnatcasecmp($a["Name"], $b["Name"]);'));
1626 function get_plugin_page_hookname($plugin_page, $parent_page) {
1627 global $admin_page_hooks;
1629 $parent = get_admin_page_parent();
1631 if (empty ($parent_page) || 'admin.php' == $parent_page) {
1632 if (isset ($admin_page_hooks[$plugin_page]))
1633 $page_type = 'toplevel';
1635 if (isset ($admin_page_hooks[$parent]))
1636 $page_type = $admin_page_hooks[$parent];
1638 if (isset ($admin_page_hooks[$parent_page])) {
1639 $page_type = $admin_page_hooks[$parent_page];
1641 $page_type = 'admin';
1644 $plugin_name = preg_replace('!\.php!', '', $plugin_page);
1646 return $page_type.'_page_'.$plugin_name;
1649 function get_plugin_page_hook($plugin_page, $parent_page) {
1652 $hook = get_plugin_page_hookname($plugin_page, $parent_page);
1653 if (isset ($wp_filter[$hook]))
1659 function browse_happy() {
1660 $getit = __('WordPress recommends a better browser');
1662 <p id="bh" style="text-align: center;"><a href="http://browsehappy.com/" title="'.$getit.'"><img src="images/browse-happy.gif" alt="Browse Happy" /></a></p>
1665 if (strstr($_SERVER['HTTP_USER_AGENT'], 'MSIE'))
1666 add_action('admin_footer', 'browse_happy');
1668 function documentation_link($for) {
1672 function register_importer($id, $name, $description, $callback) {
1673 global $wp_importers;
1675 $wp_importers[$id] = array ($name, $description, $callback);
1678 function get_importers() {
1679 global $wp_importers;
1681 return $wp_importers;
1684 function current_theme_info() {
1685 $themes = get_themes();
1686 $current_theme = get_current_theme();
1687 $ct->name = $current_theme;
1688 $ct->title = $themes[$current_theme]['Title'];
1689 $ct->version = $themes[$current_theme]['Version'];
1690 $ct->parent_theme = $themes[$current_theme]['Parent Theme'];
1691 $ct->template_dir = $themes[$current_theme]['Template Dir'];
1692 $ct->stylesheet_dir = $themes[$current_theme]['Stylesheet Dir'];
1693 $ct->template = $themes[$current_theme]['Template'];
1694 $ct->stylesheet = $themes[$current_theme]['Stylesheet'];
1695 $ct->screenshot = $themes[$current_theme]['Screenshot'];
1696 $ct->description = $themes[$current_theme]['Description'];
1697 $ct->author = $themes[$current_theme]['Author'];
1702 // array wp_handle_upload ( array &file [, array overrides] )
1703 // file: reference to a single element of $_FILES. Call the function once for each uploaded file.
1704 // overrides: an associative array of names=>values to override default variables with extract($overrides, EXTR_OVERWRITE).
1705 // On success, returns an associative array of file attributes.
1706 // On failure, returns $overrides['upload_error_handler'](&$file, $message) or array('error'=>$message).
1707 function wp_handle_upload(&$file, $overrides = false) {
1708 // The default error handler.
1709 if (! function_exists('wp_handle_upload_error') ) {
1710 function wp_handle_upload_error(&$file, $message) {
1711 return array('error'=>$message);
1715 // You may define your own function and pass the name in $overrides['upload_error_handler']
1716 $upload_error_handler = 'wp_handle_upload_error';
1718 // $_POST['action'] must be set and its value must equal $overrides['action'] or this:
1719 $action = 'wp_handle_upload';
1721 // Courtesy of php.net, the strings that describe the error indicated in $_FILES[{form field}]['error'].
1722 $upload_error_strings = array(false,
1723 __("The uploaded file exceeds the <code>upload_max_filesize</code> directive in <code>php.ini</code>."),
1724 __("The uploaded file exceeds the <em>MAX_FILE_SIZE</em> directive that was specified in the HTML form."),
1725 __("The uploaded file was only partially uploaded."),
1726 __("No file was uploaded."),
1727 __("Missing a temporary folder."),
1728 __("Failed to write file to disk."));
1730 // All tests are on by default. Most can be turned off by $override[{test_name}] = false;
1734 // If you override this, you must provide $ext and $type!!!!
1737 // Install user overrides. Did we mention that this voids your warranty?
1738 if ( is_array($overrides) )
1739 extract($overrides, EXTR_OVERWRITE);
1741 // A correct form post will pass this test.
1742 if ( $test_form && (!isset($_POST['action']) || ($_POST['action'] != $action)) )
1743 return $upload_error_handler($file, __('Invalid form submission.'));
1745 // A successful upload will pass this test. It makes no sense to override this one.
1746 if ( $file['error'] > 0 )
1747 return $upload_error_handler($file, $upload_error_strings[$file['error']]);
1749 // A non-empty file will pass this test.
1750 if ( $test_size && !($file['size'] > 0) )
1751 return $upload_error_handler($file, __('File is empty. Please upload something more substantial.'));
1753 // A properly uploaded file will pass this test. There should be no reason to override this one.
1754 if (! @ is_uploaded_file($file['tmp_name']) )
1755 return $upload_error_handler($file, __('Specified file failed upload test.'));
1757 // A correct MIME type will pass this test. Override $mimes or use the upload_mimes filter.
1759 $wp_filetype = wp_check_filetype($file['name'], $mimes);
1761 extract($wp_filetype);
1763 if ( !$type || !$ext )
1764 return $upload_error_handler($file, __('File type does not meet security guidelines. Try another.'));
1767 // A writable uploads dir will pass this test. Again, there's no point overriding this one.
1768 if ( ! ( ( $uploads = wp_upload_dir() ) && false === $uploads['error'] ) )
1769 return $upload_error_handler($file, $uploads['error']);
1771 // Increment the file number until we have a unique file to save in $dir. Use $override['unique_filename_callback'] if supplied.
1772 if ( isset($unique_filename_callback) && function_exists($unique_filename_callback) ) {
1773 $filename = $unique_filename_callback($uploads['path'], $file['name']);
1776 $filename = str_replace('#', '_', $file['name']);
1777 $filename = str_replace(array('\\', "'"), '', $filename);
1782 while ( file_exists($uploads['path'] . "/$filename") ) {
1783 if ( '' == "$number$ext" )
1784 $filename = $filename . ++$number . $ext;
1786 $filename = str_replace("$number$ext", ++$number . $ext, $filename);
1788 $filename = str_replace($ext, '', $filename);
1789 $filename = sanitize_title_with_dashes($filename) . $ext;
1792 // Move the file to the uploads dir
1793 $new_file = $uploads['path'] . "/$filename";
1794 if ( false === @ move_uploaded_file($file['tmp_name'], $new_file) )
1795 die(printf(__('The uploaded file could not be moved to %s.'), $file['path']));
1797 // Set correct file permissions
1798 $stat = stat(dirname($new_file));
1799 $perms = $stat['mode'] & 0000666;
1800 @ chmod($new_file, $perms);
1803 $url = $uploads['url'] . "/$filename";
1805 return array('file' => $new_file, 'url' => $url, 'type' => $type);
1808 function wp_shrink_dimensions($width, $height, $wmax = 128, $hmax = 96) {
1809 if ( $height <= $hmax && $width <= $wmax )
1810 return array($width, $height);
1811 elseif ( $width / $height > $wmax / $hmax )
1812 return array($wmax, (int) ($height / $width * $wmax));
1814 return array((int) ($width / $height * $hmax), $hmax);
1817 function wp_import_cleanup($id) {
1818 wp_delete_attachment($id);
1821 function wp_import_upload_form($action) {
1823 <script type="text/javascript">
1824 function cancelUpload() {
1825 o = document.getElementById('uploadForm');
1827 o.action.value = 'view';
1831 <form enctype="multipart/form-data" id="uploadForm" method="post" action="<?php echo attribute_escape($action) ?>">
1832 <?php wp_nonce_field('import-upload'); ?>
1833 <label for="upload"><?php _e('File:'); ?></label><input type="file" id="upload" name="import" />
1834 <input type="hidden" name="action" value="save" />
1836 <input type="submit" value="<?php _e('Import'); ?>" />
1837 <input type="button" value="<?php _e('Cancel'); ?>" onclick="cancelUpload()" />
1843 function wp_import_handle_upload() {
1844 $overrides = array('test_form' => false, 'test_type' => false);
1845 $file = wp_handle_upload($_FILES['import'], $overrides);
1847 if ( isset($file['error']) )
1850 $url = $file['url'];
1851 $file = $file['file'];
1852 $filename = basename($file);
1854 // Construct the object array
1856 'post_title' => $filename,
1857 'post_content' => $url,
1858 'post_mime_type' => 'import',
1863 $id = wp_insert_attachment($object, $file);
1865 return array('file' => $file, 'id' => $id);
1868 function user_can_richedit() {
1869 if ( 'true' != get_user_option('rich_editing') )
1872 if ( preg_match('!opera[ /][2-8]|konqueror|safari!i', $_SERVER['HTTP_USER_AGENT']) )
1875 return true; // Best guess
1878 function the_attachment_links($id = false) {
1880 $post = & get_post($id);
1882 if ( $post->post_status != 'attachment' )
1885 $icon = get_attachment_icon($post->ID);
1888 <p><?php _e('Text linked to file') ?><br />
1889 <textarea rows="1" cols="40" type="text" class="attachmentlinks" readonly="readonly"><a href="<?php echo $post->guid ?>" class="attachmentlink"><?php echo basename($post->guid) ?></a></textarea></p>
1890 <p><?php _e('Text linked to subpost') ?><br />
1891 <textarea rows="1" cols="40" type="text" class="attachmentlinks" readonly="readonly"><a href="<?php echo get_attachment_link($post->ID) ?>" rel="attachment" id="<?php echo $post->ID ?>"><?php echo $post->post_title ?></a></textarea></p>
1892 <?php if ( $icon ) : ?>
1893 <p><?php _e('Thumbnail linked to file') ?><br />
1894 <textarea rows="1" cols="40" type="text" class="attachmentlinks" readonly="readonly"><a href="<?php echo $post->guid ?>" class="attachmentlink"><?php echo $icon ?></a></textarea></p>
1895 <p><?php _e('Thumbnail linked to subpost') ?><br />
1896 <textarea rows="1" cols="40" type="text" class="attachmentlinks" readonly="readonly"><a href="<?php echo get_attachment_link($post->ID) ?>" rel="attachment" id="<?php echo $post->ID ?>"><?php echo $icon ?></a></textarea></p>
1901 function get_udims($width, $height) {
1902 if ( $height <= 96 && $width <= 128 )
1903 return array($width, $height);
1904 elseif ( $width / $height > 4 / 3 )
1905 return array(128, (int) ($height / $width * 128));
1907 return array((int) ($width / $height * 96), 96);