Make WordPress Core


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/admin-functions.php

    r4418 r3501  
    11<?php
    22
     3// Creates a new post from the "Write Post" form using $_POST information.
    34function write_post() {
    4     $result = wp_write_post();
    5     if( is_wp_error($result) )
    6         wp_die( $result->get_error_message() );
    7     else
    8         return $result;
    9 }
    10 
    11 // Creates a new post from the "Write Post" form using $_POST information.
    12 function wp_write_post() {
    135    global $user_ID;
    146
    15     if ( 'page' == $_POST['post_type'] ) {
    16         if ( !current_user_can('edit_pages') )
    17             return new WP_Error('edit_pages', __('You are not allowed to create pages on this blog.'));
    18     } else {
    19         if ( !current_user_can('edit_posts') )
    20             return new WP_Error('edit_posts', __('You are not allowed to create posts or drafts on this blog.'));
    21     }
     7    if (!current_user_can('edit_posts'))
     8        die(__('You are not allowed to create posts or drafts on this blog.'));
    229
    2310    // Rename.
     
    2916    if (!empty ($_POST['post_author_override'])) {
    3017        $_POST['post_author'] = (int) $_POST['post_author_override'];
    31     } else {
     18    } else
    3219        if (!empty ($_POST['post_author'])) {
    3320            $_POST['post_author'] = (int) $_POST['post_author'];
     
    3623        }
    3724
    38     }
    39 
    40     if ($_POST['post_author'] != $_POST['user_ID']) {
    41         if ( 'page' == $_POST['post_type'] ) {
    42             if ( !current_user_can('edit_others_pages') )
    43                 return new WP_Error('edit_others_pages', __('You cannot create pages as this user.'));
    44         } else {
    45             if ( !current_user_can('edit_others_posts') )
    46                 return new WP_Error('edit_others_posts', __('You cannot post as this user.'));
    47 
    48         }
    49     }
     25    if (($_POST['post_author'] != $_POST['user_ID']) && !current_user_can('edit_others_posts'))
     26        die(__('You cannot post as this user.'));
    5027
    5128    // What to do based on which button they pressed
     
    5835    if ('' != $_POST['advanced'])
    5936        $_POST['post_status'] = 'draft';
    60 
    61     if ( 'page' == $_POST['post_type'] ) {
    62         if ('publish' == $_POST['post_status'] && !current_user_can('publish_pages'))
    63             $_POST['post_status'] = 'draft';
    64     } else {
    65         if ('publish' == $_POST['post_status'] && !current_user_can('publish_posts'))
    66             $_POST['post_status'] = 'draft';
    67     }
    68 
    69     if (!isset ($_POST['comment_status']))
    70         $_POST['comment_status'] = 'closed';
    71 
    72     if (!isset ($_POST['ping_status']))
    73         $_POST['ping_status'] = 'closed';
    74 
    75     if (!empty ($_POST['edit_date'])) {
    76         $aa = $_POST['aa'];
    77         $mm = $_POST['mm'];
    78         $jj = $_POST['jj'];
    79         $hh = $_POST['hh'];
    80         $mn = $_POST['mn'];
    81         $ss = $_POST['ss'];
    82         $jj = ($jj > 31) ? 31 : $jj;
    83         $hh = ($hh > 23) ? $hh -24 : $hh;
    84         $mn = ($mn > 59) ? $mn -60 : $mn;
    85         $ss = ($ss > 59) ? $ss -60 : $ss;
    86         $_POST['post_date'] = sprintf("%04d-%02d-%02d %02d:%02d:%02d", $aa, $mm, $jj, $hh, $mn, $ss);
    87         $_POST['post_date_gmt'] = get_gmt_from_date($_POST['post_date']);
    88     }
    89 
    90     // Create the post.
    91     $post_ID = wp_insert_post($_POST);
    92     add_meta($post_ID);
    93 
    94     // Reunite any orphaned attachments with their parent
    95     if ( $_POST['temp_ID'] )
    96         relocate_children($_POST['temp_ID'], $post_ID);
    97 
    98     // Now that we have an ID we can fix any attachment anchor hrefs
    99     fix_attachment_links($post_ID);
    100 
    101     return $post_ID;
    102 }
    103 
    104 // Move child posts to a new parent
    105 function relocate_children($old_ID, $new_ID) {
    106     global $wpdb;
    107     $old_ID = (int) $old_ID;
    108     $new_ID = (int) $new_ID;
    109     return $wpdb->query("UPDATE $wpdb->posts SET post_parent = $new_ID WHERE post_parent = $old_ID");
    110 }
    111 
    112 // Replace hrefs of attachment anchors with up-to-date permalinks.
    113 function fix_attachment_links($post_ID) {
    114     global $wp_rewrite;
    115 
    116     $post = & get_post($post_ID, ARRAY_A);
    117 
    118     $search = "#<a[^>]+rel=('|\")[^'\"]*attachment[^>]*>#ie";
    119 
    120     // See if we have any rel="attachment" links
    121     if ( 0 == preg_match_all($search, $post['post_content'], $anchor_matches, PREG_PATTERN_ORDER) )
    122         return;
    123 
    124     $i = 0;
    125     $search = "#[\s]+rel=(\"|')(.*?)wp-att-(\d+)\\1#i";
    126     foreach ( $anchor_matches[0] as $anchor ) {
    127         if ( 0 == preg_match($search, $anchor, $id_matches) )
    128             continue;
    129 
    130         $id = $id_matches[3];
    131 
    132         // While we have the attachment ID, let's adopt any orphans.
    133         $attachment = & get_post($id, ARRAY_A);
    134         if ( ! empty($attachment) && ! is_object(get_post($attachment['post_parent'])) ) {
    135             $attachment['post_parent'] = $post_ID;
    136             // Escape data pulled from DB.
    137             $attachment = add_magic_quotes($attachment);
    138             wp_update_post($attachment);
    139         }
    140 
    141         $post_search[$i] = $anchor;
    142         $post_replace[$i] = preg_replace("#href=(\"|')[^'\"]*\\1#e", "stripslashes('href=\\1').get_attachment_link($id).stripslashes('\\1')", $anchor);
    143         ++$i;
    144     }
    145 
    146     $post['post_content'] = str_replace($post_search, $post_replace, $post['post_content']);
    147 
    148     // Escape data pulled from DB.
    149     $post = add_magic_quotes($post);
    150 
    151     return wp_update_post($post);
    152 }
    153 
    154 // Update an existing post with values provided in $_POST.
    155 function edit_post() {
    156     global $user_ID;
    157 
    158     $post_ID = (int) $_POST['post_ID'];
    159 
    160     if ( 'page' == $_POST['post_type'] ) {
    161         if ( !current_user_can('edit_page', $post_ID) )
    162             wp_die(__('You are not allowed to edit this page.'));
    163     } else {
    164         if ( !current_user_can('edit_post', $post_ID) )
    165             wp_die(__('You are not allowed to edit this post.'));
    166     }
    167 
    168     // Rename.
    169     $_POST['ID'] = (int) $_POST['post_ID'];
    170     $_POST['post_content'] = $_POST['content'];
    171     $_POST['post_excerpt'] = $_POST['excerpt'];
    172     $_POST['post_parent'] = $_POST['parent_id'];
    173     $_POST['to_ping'] = $_POST['trackback_url'];
    174 
    175     if (!empty ($_POST['post_author_override'])) {
    176         $_POST['post_author'] = (int) $_POST['post_author_override'];
    177     } else
    178         if (!empty ($_POST['post_author'])) {
    179             $_POST['post_author'] = (int) $_POST['post_author'];
    180         } else {
    181             $_POST['post_author'] = (int) $_POST['user_ID'];
    182         }
    183 
    184     if ($_POST['post_author'] != $_POST['user_ID']) {
    185         if ( 'page' == $_POST['post_type'] ) {
    186             if ( !current_user_can('edit_others_pages') )
    187                 wp_die(__('You cannot edit pages as this user.'));
    188         } else {
    189             if ( !current_user_can('edit_others_posts') )
    190                 wp_die(__('You cannot edit posts as this user.'));
    191 
    192         }
    193     }
    194 
    195     // What to do based on which button they pressed
    196     if ('' != $_POST['saveasdraft'])
     37    if ('' != $_POST['savepage'])
     38        $_POST['post_status'] = 'static';
     39
     40    if ('publish' == $_POST['post_status'] && !current_user_can('publish_posts'))
    19741        $_POST['post_status'] = 'draft';
    198     if ('' != $_POST['saveasprivate'])
    199         $_POST['post_status'] = 'private';
    200     if ('' != $_POST['publish'])
    201         $_POST['post_status'] = 'publish';
    202     if ('' != $_POST['advanced'])
    203         $_POST['post_status'] = 'draft';
    204 
    205     if ( 'page' == $_POST['post_type'] ) {
    206         if ('publish' == $_POST['post_status'] && !current_user_can('edit_published_pages'))
    207             $_POST['post_status'] = 'draft';
    208     } else {
    209         if ('publish' == $_POST['post_status'] && !current_user_can('edit_published_posts'))
    210             $_POST['post_status'] = 'draft';
    211     }
    212 
    213     if (!isset ($_POST['comment_status']))
    214         $_POST['comment_status'] = 'closed';
    215 
    216     if (!isset ($_POST['ping_status']))
    217         $_POST['ping_status'] = 'closed';
     42
     43    if ('static' == $_POST['post_status'] && !current_user_can('edit_pages'))
     44        die(__('This user cannot edit pages.'));
    21845
    21946    if (!empty ($_POST['edit_date'])) {
     
    23259    }
    23360
     61    // Create the post.
     62    $post_ID = wp_insert_post($_POST);
     63    add_meta($post_ID);
     64
     65    // Reunite any orphaned attachments with their parent
     66    if ( $_POST['temp_ID'] )
     67        relocate_children($_POST['temp_ID'], $post_ID);
     68
     69    // Now that we have an ID we can fix any attachment anchor hrefs
     70    fix_attachment_links($post_ID);
     71
     72    return $post_ID;
     73}
     74
     75// Move child posts to a new parent
     76function relocate_children($old_ID, $new_ID) {
     77    global $wpdb;
     78    $old_ID = (int) $old_ID;
     79    $new_ID = (int) $new_ID;
     80    return $wpdb->query("UPDATE $wpdb->posts SET post_parent = $new_ID WHERE post_parent = $old_ID");
     81}
     82
     83// Replace hrefs of attachment anchors with up-to-date permalinks.
     84function fix_attachment_links($post_ID) {
     85    global $wp_rewrite;
     86
     87    $post = & get_post($post_ID);
     88
     89    $search = "#<a[^>]+rel=('|\")[^'\"]*attachment[^>]*>#ie";
     90
     91    // See if we have any rel="attachment" links
     92    if ( 0 == preg_match_all($search, $post->post_content, $anchor_matches, PREG_PATTERN_ORDER) )
     93        return;
     94
     95    $i = 0;
     96    $search = "# id=(\"|')p(\d+)\\1#i";
     97    foreach ( $anchor_matches[0] as $anchor ) {
     98        if ( 0 == preg_match($search, $anchor, $id_matches) )
     99            continue;
     100
     101        $id = $id_matches[2];
     102
     103        // While we have the attachment ID, let's adopt any orphans.
     104        $attachment = & get_post($id);
     105        if ( ! is_object(get_post($attachment->post_parent)) ) {
     106            $attachment->post_parent = $post_ID;
     107            wp_update_post($attachment);
     108        }
     109
     110        $post_search[$i] = $anchor;
     111        $post_replace[$i] = preg_replace("#href=(\"|')[^'\"]*\\1#e", "stripslashes('href=\\1').get_attachment_link($id).stripslashes('\\1')", $anchor);
     112        ++$i;
     113    }
     114
     115    $post->post_content = str_replace($post_search, $post_replace, $post->post_content);
     116
     117    return wp_update_post($post);
     118}
     119
     120// Update an existing post with values provided in $_POST.
     121function edit_post() {
     122    global $user_ID;
     123
     124    $post_ID = (int) $_POST['post_ID'];
     125
     126    if (!current_user_can('edit_post', $post_ID))
     127        die(__('You are not allowed to edit this post.'));
     128
     129    // Rename.
     130    $_POST['ID'] = (int) $_POST['post_ID'];
     131    $_POST['post_content'] = $_POST['content'];
     132    $_POST['post_excerpt'] = $_POST['excerpt'];
     133    $_POST['post_parent'] = $_POST['parent_id'];
     134    $_POST['to_ping'] = $_POST['trackback_url'];
     135
     136    if (!empty ($_POST['post_author_override'])) {
     137        $_POST['post_author'] = (int) $_POST['post_author_override'];
     138    } else
     139        if (!empty ($_POST['post_author'])) {
     140            $_POST['post_author'] = (int) $_POST['post_author'];
     141        } else {
     142            $_POST['post_author'] = (int) $_POST['user_ID'];
     143        }
     144
     145    if (($_POST['post_author'] != $_POST['user_ID']) && !current_user_can('edit_others_posts'))
     146        die(__('You cannot post as this user.'));
     147
     148    // What to do based on which button they pressed
     149    if ('' != $_POST['saveasdraft'])
     150        $_POST['post_status'] = 'draft';
     151    if ('' != $_POST['saveasprivate'])
     152        $_POST['post_status'] = 'private';
     153    if ('' != $_POST['publish'])
     154        $_POST['post_status'] = 'publish';
     155    if ('' != $_POST['advanced'])
     156        $_POST['post_status'] = 'draft';
     157    if ('' != $_POST['savepage'])
     158        $_POST['post_status'] = 'static';
     159
     160    if ('publish' == $_POST['post_status'] && !current_user_can('publish_posts'))
     161        $_POST['post_status'] = 'draft';
     162
     163    if ('static' == $_POST['post_status'] && !current_user_can('edit_pages'))
     164        die(__('This user cannot edit pages.'));
     165
     166    if (!isset ($_POST['comment_status']))
     167        $_POST['comment_status'] = 'closed';
     168
     169    if (!isset ($_POST['ping_status']))
     170        $_POST['ping_status'] = 'closed';
     171
     172    if (!empty ($_POST['edit_date'])) {
     173        $aa = $_POST['aa'];
     174        $mm = $_POST['mm'];
     175        $jj = $_POST['jj'];
     176        $hh = $_POST['hh'];
     177        $mn = $_POST['mn'];
     178        $ss = $_POST['ss'];
     179        $jj = ($jj > 31) ? 31 : $jj;
     180        $hh = ($hh > 23) ? $hh -24 : $hh;
     181        $mn = ($mn > 59) ? $mn -60 : $mn;
     182        $ss = ($ss > 59) ? $ss -60 : $ss;
     183        $_POST['post_date'] = "$aa-$mm-$jj $hh:$mn:$ss";
     184        $_POST['post_date_gmt'] = get_gmt_from_date("$aa-$mm-$jj $hh:$mn:$ss");
     185    }
     186
    234187    // Meta Stuff
    235188    if ($_POST['meta']) {
     
    237190            update_meta($key, $value['key'], $value['value']);
    238191    }
    239 
     192   
    240193    if ($_POST['deletemeta']) {
    241194        foreach ($_POST['deletemeta'] as $key => $value)
     
    260213
    261214    if (!current_user_can('edit_post', $comment_post_ID))
    262         wp_die(__('You are not allowed to edit comments on this post, so you cannot edit this comment.'));
     215        die(__('You are not allowed to edit comments on this post, so you cannot edit this comment.'));
    263216
    264217    $_POST['comment_author'] = $_POST['newcomment_author'];
     
    288241// Get an existing post and format it for editing.
    289242function get_post_to_edit($id) {
     243    global $richedit;
     244    $richedit = ( 'true' == get_user_option('rich_editing') ) ? true : false;
    290245
    291246    $post = get_post($id);
    292247
    293     $post->post_content = format_to_edit($post->post_content, user_can_richedit());
     248    $post->post_content = format_to_edit($post->post_content, $richedit);
    294249    $post->post_content = apply_filters('content_edit_pre', $post->post_content);
    295250
     
    300255    $post->post_title = apply_filters('title_edit_pre', $post->post_title);
    301256
    302     $post->post_password = format_to_edit($post->post_password);
    303 
    304     if ($post->post_type == 'page')
     257    if ($post->post_status == 'static')
    305258        $post->page_template = get_post_meta($id, '_wp_page_template', true);
    306259
     
    334287
    335288    $post->post_status = 'draft';
    336     $post->comment_status = get_option('default_comment_status');
    337     $post->ping_status = get_option('default_ping_status');
    338     $post->post_pingback = get_option('default_pingback_flag');
    339     $post->post_category = get_option('default_category');
     289    $post->comment_status = get_settings('default_comment_status');
     290    $post->ping_status = get_settings('default_ping_status');
     291    $post->post_pingback = get_settings('default_pingback_flag');
     292    $post->post_category = get_settings('default_category');
    340293    $post->post_content = apply_filters('default_content', $post_content);
    341294    $post->post_title = apply_filters('default_title', $post_title);
     
    349302
    350303function get_comment_to_edit($id) {
     304    global $richedit;
     305    $richedit = ( 'true' == get_user_option('rich_editing') ) ? true : false;
     306
    351307    $comment = get_comment($id);
    352308
    353     $comment->comment_content = format_to_edit($comment->comment_content, user_can_richedit());
     309    $comment->comment_content = format_to_edit($comment->comment_content, $richedit);
    354310    $comment->comment_content = apply_filters('comment_edit_pre', $comment->comment_content);
    355311
     
    367323}
    368324
    369 function wp_dropdown_roles( $default = false ) {
    370     global $wp_roles;
    371     $r = '';
    372     foreach($wp_roles->role_names as $role => $name)
    373         if ( $default == $role ) // Make default first in list
    374             $p = "\n\t<option selected='selected' value='$role'>$name</option>";
    375         else
    376             $r .= "\n\t<option value='$role'>$name</option>";
    377     echo $p . $r;
    378 }
    379 
    380 
    381 function get_user_to_edit($user_id) {
    382     $user = new WP_User($user_id);
    383     $user->user_login = wp_specialchars($user->user_login, 1);
    384     $user->user_email = wp_specialchars($user->user_email, 1);
    385     $user->user_url = wp_specialchars($user->user_url, 1);
    386     $user->first_name = wp_specialchars($user->first_name, 1);
    387     $user->last_name = wp_specialchars($user->last_name, 1);
    388     $user->display_name = wp_specialchars($user->display_name, 1);
    389     $user->nickname = wp_specialchars($user->nickname, 1);
    390     $user->aim = wp_specialchars($user->aim, 1);
    391     $user->yim = wp_specialchars($user->yim, 1);
    392     $user->jabber = wp_specialchars($user->jabber, 1);
    393     $user->description = wp_specialchars($user->description);
    394 
    395     return $user;
    396 }
    397 
    398325// Creates a new user from the "Users" form using $_POST information.
    399326
    400327function add_user() {
    401     if ( func_num_args() ) { // The hackiest hack that ever did hack
    402         global $current_user, $wp_roles;
    403         $user_id = func_get_arg(0);
    404 
    405         if (isset ($_POST['role'])) {
    406             if($user_id != $current_user->id || $wp_roles->role_objects[$_POST['role']]->has_cap('edit_users')) {
    407                 $user = new WP_User($user_id);
    408                 $user->set_role($_POST['role']);
    409             }
    410         }
    411     } else {
    412         add_action('user_register', 'add_user'); // See above
    413         return edit_user();
    414     }
     328    return edit_user();
    415329}
    416330
    417331function edit_user($user_id = 0) {
    418332    global $current_user, $wp_roles, $wpdb;
     333
    419334    if ($user_id != 0) {
    420335        $update = true;
     
    436351        $pass2 = $_POST['pass2'];
    437352
    438     if (isset ($_POST['role']) && current_user_can('edit_users')) {
     353    if (isset ($_POST['role'])) {
    439354        if($user_id != $current_user->id || $wp_roles->role_objects[$_POST['role']]->has_cap('edit_users'))
    440355            $user->role = $_POST['role'];
     
    456371        $user->display_name = wp_specialchars(trim($_POST['display_name']));
    457372    if (isset ($_POST['description']))
    458         $user->description = trim($_POST['description']);
     373        $user->description = wp_specialchars(trim($_POST['description']));
    459374    if (isset ($_POST['jabber']))
    460375        $user->jabber = wp_specialchars(trim($_POST['jabber']));
     
    464379        $user->yim = wp_specialchars(trim($_POST['yim']));
    465380
    466     $errors = new WP_Error();
     381    $errors = array ();
    467382
    468383    /* checking that username has been typed */
    469384    if ($user->user_login == '')
    470         $errors->add('user_login', __('<strong>ERROR</strong>: Please enter a username.'));
     385        $errors['user_login'] = __('<strong>ERROR</strong>: Please enter a username.');
    471386
    472387    /* checking the password has been typed twice */
    473     do_action_ref_array('check_passwords', array ($user->user_login, & $pass1, & $pass2));
     388    do_action('check_passwords', array ($user->user_login, & $pass1, & $pass2));
    474389
    475390    if (!$update) {
    476391        if ($pass1 == '' || $pass2 == '')
    477             $errors->add('pass', __('<strong>ERROR</strong>: Please enter your password twice.'));
     392            $errors['pass'] = __('<strong>ERROR</strong>: Please enter your password twice.');
    478393    } else {
    479394        if ((empty ($pass1) && !empty ($pass2)) || (empty ($pass2) && !empty ($pass1)))
    480             $errors->add('pass', __("<strong>ERROR</strong>: you typed your new password only once."));
     395            $errors['pass'] = __("<strong>ERROR</strong>: you typed your new password only once.");
    481396    }
    482397
    483398    /* Check for "\" in password */
    484399    if( strpos( " ".$pass1, "\\" ) )
    485         $errors->add('pass', __('<strong>ERROR</strong>: Passwords may not contain the character "\\".'));
     400        $errors['pass'] = __('<strong>ERROR</strong>: Passwords may not contain the character "\\".');
    486401
    487402    /* checking the password has been typed twice the same */
    488403    if ($pass1 != $pass2)
    489         $errors->add('pass', __('<strong>ERROR</strong>: Please type the same password in the two password fields.'));
     404        $errors['pass'] = __('<strong>ERROR</strong>: Please type the same password in the two password fields.');
    490405
    491406    if (!empty ($pass1))
     
    493408
    494409    if ( !validate_username($user->user_login) )
    495         $errors->add('user_login', __('<strong>ERROR</strong>: This username is invalid.  Please enter a valid username.'));
     410        $errors['user_login'] = __('<strong>ERROR</strong>: This username is invalid.  Please enter a valid username.');
    496411
    497412    if (!$update && username_exists($user->user_login))
    498         $errors->add('user_login', __('<strong>ERROR</strong>: This username is already registered, please choose another one.'));
     413        $errors['user_login'] = __('<strong>ERROR</strong>: This username is already registered, please choose another one.');
    499414
    500415    /* checking e-mail address */
    501416    if (empty ($user->user_email)) {
    502         $errors->add('user_email', __("<strong>ERROR</strong>: please type an e-mail address"));
     417        $errors['user_email'] = __("<strong>ERROR</strong>: please type an e-mail address");
    503418    } else
    504419        if (!is_email($user->user_email)) {
    505             $errors->add('user_email', __("<strong>ERROR</strong>: the email address isn't correct"));
    506         }
    507 
    508     if ( $errors->get_error_codes() )
     420            $errors['user_email'] = __("<strong>ERROR</strong>: the email address isn't correct");
     421        }
     422
     423    if (count($errors) != 0)
    509424        return $errors;
    510425
     
    515430        wp_new_user_notification($user_id);
    516431    }
    517     return $user_id;
     432
     433    return $errors;
    518434}
    519435
     
    521437function get_link_to_edit($link_id) {
    522438    $link = get_link($link_id);
    523 
     439   
    524440    $link->link_url = wp_specialchars($link->link_url, 1);
    525441    $link->link_name = wp_specialchars($link->link_name, 1);
    526     $link->link_image = wp_specialchars($link->link_image, 1);
    527     $link->link_description = wp_specialchars($link->link_description, 1);
     442    $link->link_description = wp_specialchars($link->link_description);
    528443    $link->link_notes = wp_specialchars($link->link_notes);
    529     $link->link_rss = wp_specialchars($link->link_rss, 1);
    530     $link->link_rel = wp_specialchars($link->link_rel, 1);
    531     $link->post_category = $link->link_category;
    532 
     444    $link->link_rss = wp_specialchars($link->link_rss);
     445   
    533446    return $link;
    534447}
     
    539452    else
    540453        $link->link_url = '';
    541 
     454   
    542455    if ( isset($_GET['name']) )
    543456        $link->link_name = wp_specialchars($_GET['name'], 1);
    544457    else
    545458        $link->link_name = '';
    546 
    547     $link->link_visible = 'Y';
    548 
     459       
    549460    return $link;
    550461}
    551462
    552463function add_link() {
    553     return edit_link();
     464    return edit_link(); 
    554465}
    555466
    556467function edit_link($link_id = '') {
    557468    if (!current_user_can('manage_links'))
    558         wp_die(__("Cheatin' uh ?"));
     469        die(__("Cheatin' uh ?"));
    559470
    560471    $_POST['link_url'] = wp_specialchars($_POST['link_url']);
     
    563474    $_POST['link_image'] = wp_specialchars($_POST['link_image']);
    564475    $_POST['link_rss'] = wp_specialchars($_POST['link_rss']);
    565     $_POST['link_category'] = $_POST['post_category'];
     476    $auto_toggle = get_autotoggle($_POST['link_category']);
     477   
     478    // if we are in an auto toggle category and this one is visible then we
     479    // need to make the others invisible before we add this new one.
     480    // FIXME Add category toggle func.
     481    //if (($auto_toggle == 'Y') && ($link_visible == 'Y')) {
     482    //  $wpdb->query("UPDATE $wpdb->links set link_visible = 'N' WHERE link_category = $link_category");
     483    //}
    566484
    567485    if ( !empty($link_id) ) {
     
    595513function return_categories_list($parent = 0) {
    596514    global $wpdb;
    597     return $wpdb->get_col("SELECT cat_ID FROM $wpdb->categories WHERE category_parent = $parent ORDER BY category_count DESC");
     515    return $wpdb->get_col("SELECT cat_ID FROM $wpdb->categories WHERE category_parent = $parent ORDER BY category_count DESC LIMIT 100");
    598516}
    599517
     
    603521
    604522function get_nested_categories($default = 0, $parent = 0) {
    605     global $post_ID, $link_id, $mode, $wpdb;
     523    global $post_ID, $mode, $wpdb;
    606524
    607525    if ($post_ID) {
     
    616534            $checked_categories[] = $default;
    617535        }
    618     } else if ($link_id) {
    619         $checked_categories = $wpdb->get_col("
    620              SELECT category_id
    621              FROM $wpdb->categories, $wpdb->link2cat
    622              WHERE $wpdb->link2cat.category_id = cat_ID AND $wpdb->link2cat.link_id = '$link_id'
    623              ");
    624 
    625         if (count($checked_categories) == 0) {
    626             // No selected categories, strange
    627             $checked_categories[] = $default;
    628         }   
     536
    629537    } else {
    630538        $checked_categories[] = $default;
     
    642550        }
    643551    }
    644 
     552   
    645553    usort($result, 'sort_cats');
    646554
     
    650558function write_nested_categories($categories) {
    651559    foreach ($categories as $category) {
    652         echo '<li id="category-', $category['cat_ID'], '"><label for="in-category-', $category['cat_ID'], '" class="selectit"><input value="', $category['cat_ID'], '" type="checkbox" name="post_category[]" id="in-category-', $category['cat_ID'], '"', ($category['checked'] ? ' checked="checked"' : ""), '/> ', wp_specialchars($category['cat_name']), "</label></li>\n";
    653 
    654         if ( $category['children'] ) {
    655             echo "<ul>\n";
     560        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";
     561
     562        if (isset ($category['children'])) {
     563            echo "\n<span class='cat-nest'>\n";
    656564            write_nested_categories($category['children']);
    657             echo "</ul>\n";
     565            echo "</span>\n";
    658566        }
    659567    }
     
    664572}
    665573
    666 function return_link_categories_list($parent = 0) {
    667     global $wpdb;
    668     return $wpdb->get_col("SELECT cat_ID FROM $wpdb->categories WHERE category_parent = $parent ORDER BY link_count DESC");
    669 }
    670 
    671 function get_nested_link_categories( $default = 0, $parent = 0 ) {
    672     global $post_ID, $link_id, $mode, $wpdb;
    673 
    674     if ($link_id) {
    675         $checked_categories = $wpdb->get_col("
    676              SELECT category_id
    677              FROM $wpdb->categories, $wpdb->link2cat
    678              WHERE $wpdb->link2cat.category_id = cat_ID AND $wpdb->link2cat.link_id = '$link_id'
    679              ");
    680 
    681         if (count($checked_categories) == 0) {
    682             // No selected categories, strange
    683             $checked_categories[] = $default;
    684         }   
    685     } else {
    686         $checked_categories[] = $default;
    687     }
    688 
    689     $cats = return_link_categories_list($parent);
    690     $result = array ();
    691 
    692     if (is_array($cats)) {
    693         foreach ($cats as $cat) {
    694             $result[$cat]['children'] = get_nested_link_categories($default, $cat);
    695             $result[$cat]['cat_ID'] = $cat;
    696             $result[$cat]['checked'] = in_array($cat, $checked_categories);
    697             $result[$cat]['cat_name'] = get_the_category_by_ID($cat);
    698         }
    699     }
    700 
    701     usort($result, 'sort_cats');
    702 
    703     return $result;
    704 }
    705 
    706 function dropdown_link_categories($default = 0) {
    707     write_nested_categories(get_nested_link_categories($default));
    708 }
    709 
    710574// Dandy new recursive multiple category stuff.
    711575function cat_rows($parent = 0, $level = 0, $categories = 0) {
     576    global $wpdb, $class;
     577
    712578    if (!$categories)
    713         $categories = get_categories('hide_empty=0');
     579        $categories = $wpdb->get_results("SELECT * FROM $wpdb->categories ORDER BY cat_name");
    714580
    715581    if ($categories) {
    716582        foreach ($categories as $category) {
    717583            if ($category->category_parent == $parent) {
    718                 echo "\t" . _cat_row( $category, $level );
     584                $category->cat_name = wp_specialchars($category->cat_name);
     585                $count = $wpdb->get_var("SELECT COUNT(post_id) FROM $wpdb->post2cat WHERE category_id = $category->cat_ID");
     586                $pad = str_repeat('&#8212; ', $level);
     587                if ( current_user_can('manage_categories') ) {
     588                    $edit = "<a href='categories.php?action=edit&amp;cat_ID=$category->cat_ID' class='edit'>".__('Edit')."</a></td>";
     589                    $default_cat_id = get_option('default_category');
     590                   
     591                    if ($category->cat_ID != $default_cat_id)
     592                        $edit .= "<td><a href='categories.php?action=delete&amp;cat_ID=$category->cat_ID' onclick=\"return deleteSomething( 'cat', $category->cat_ID, '".sprintf(__("You are about to delete the category &quot;%s&quot;.  All of its posts will go to the default category.\\n&quot;OK&quot; to delete, &quot;Cancel&quot; to stop."), wp_specialchars($category->cat_name, 1))."' );\" class='delete'>".__('Delete')."</a>";
     593                    else
     594                        $edit .= "<td style='text-align:center'>".__("Default");
     595                }
     596                else
     597                    $edit = '';
     598
     599                $class = ('alternate' == $class) ? '' : 'alternate';
     600                echo "<tr id='cat-$category->cat_ID' class='$class'><th scope='row'>$category->cat_ID</th><td>$pad $category->cat_name</td>
     601                                <td>$category->category_description</td>
     602                                <td>$count</td>
     603                                <td>$edit</td>
     604                                </tr>";
    719605                cat_rows($category->cat_ID, $level +1, $categories);
    720606            }
     
    725611}
    726612
    727 function _cat_row( $category, $level, $name_override = false ) {
    728     global $class;
    729 
    730     $pad = str_repeat('&#8212; ', $level);
    731     if ( current_user_can('manage_categories') ) {
    732         $edit = "<a href='categories.php?action=edit&amp;cat_ID=$category->cat_ID' class='edit'>".__('Edit')."</a></td>";
    733         $default_cat_id = get_option('default_category');
    734         $default_link_cat_id = get_option('default_link_category');
    735 
    736         if ( ($category->cat_ID != $default_cat_id) && ($category->cat_ID != $default_link_cat_id) )
    737             $edit .= "<td><a href='" . wp_nonce_url("categories.php?action=delete&amp;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 &quot;%s&quot;.\\nAll of its posts will go into the default category of &quot;%s&quot;\\nAll of its bookmarks will go into the default category of &quot;%s&quot;.\\n&quot;OK&quot; to delete, &quot;Cancel&quot; to stop."), js_escape($category->cat_name), js_escape(get_catname($default_cat_id)), js_escape(get_catname($default_link_cat_id))) . "' );\" class='delete'>".__('Delete')."</a>";
    738         else
    739             $edit .= "<td style='text-align:center'>".__("Default");
    740     } else
    741         $edit = '';
    742 
    743     $class = ( ( defined('DOING_AJAX') && DOING_AJAX ) || " class='alternate'" == $class ) ? '' : " class='alternate'";
    744 
    745     $category->category_count = number_format( $category->category_count );
    746     $category->link_count = number_format( $category->link_count );
    747     return "<tr id='cat-$category->cat_ID'$class>
    748         <th scope='row' style='text-align: center'>$category->cat_ID</th>
    749         <td>" . ( $name_override ? $name_override : $pad . ' ' . $category->cat_name ) . "</td>
    750         <td>$category->category_description</td>
    751         <td align='center'>$category->category_count</td>
    752         <td align='center'>$category->link_count</td>
    753         <td>$edit</td>\n\t</tr>\n";
    754 }
    755 
    756 function page_rows($parent = 0, $level = 0, $pages = 0, $hierarchy = true) {
     613function page_rows($parent = 0, $level = 0, $pages = 0) {
    757614    global $wpdb, $class, $post;
    758 
    759615    if (!$pages)
    760         $pages = get_pages('sort_column=menu_order');
    761 
    762     if (! $pages)
    763         return false;
    764 
    765     foreach ($pages as $post) {
    766         setup_postdata($post);
    767         if ( $hierarchy && ($post->post_parent != $parent) )
    768             continue;
    769 
    770         $post->post_title = wp_specialchars($post->post_title);
    771         $pad = str_repeat('&#8212; ', $level);
    772         $id = $post->ID;
    773         $class = ('alternate' == $class) ? '' : 'alternate';
     616        $pages = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_status = 'static' ORDER BY menu_order");
     617
     618    if ($pages) {
     619        foreach ($pages as $post) {
     620            start_wp();
     621            if ($post->post_parent == $parent) {
     622                $post->post_title = wp_specialchars($post->post_title);
     623                $pad = str_repeat('&#8212; ', $level);
     624                $id = $post->ID;
     625                $class = ('alternate' == $class) ? '' : 'alternate';
    774626?>
    775627  <tr id='page-<?php echo $id; ?>' class='<?php echo $class; ?>'>
    776     <th scope="row" style="text-align: center"><?php echo $post->ID; ?></th>
     628    <th scope="row"><?php echo $post->ID; ?></th>
    777629    <td>
    778       <?php echo $pad; ?><?php the_title() ?>
    779       <?php if ('private' == $post->post_status) _e(' - <strong>Private</strong>'); ?>
     630      <?php echo $pad; ?><?php the_title() ?>
    780631    </td>
    781632    <td><?php the_author() ?></td>
    782633    <td><?php echo mysql2date('Y-m-d g:i a', $post->post_modified); ?></td>
    783634    <td><a href="<?php the_permalink(); ?>" rel="permalink" class="edit"><?php _e('View'); ?></a></td>
    784     <td><?php if ( current_user_can('edit_page', $id) ) { echo "<a href='page.php?action=edit&amp;post=$id' class='edit'>" . __('Edit') . "</a>"; } ?></td>
    785     <td><?php if ( current_user_can('delete_page', $id) ) { echo "<a href='" . wp_nonce_url("page.php?action=delete&amp;post=$id", 'delete-page_' . $id) .  "' class='delete' onclick=\"return deleteSomething( 'page', " . $id . ", '" . sprintf(__("You are about to delete the &quot;%s&quot; page.\\n&quot;OK&quot; to delete, &quot;Cancel&quot; to stop."), js_escape(get_the_title()) ) . "' );\">" . __('Delete') . "</a>"; } ?></td>
     635    <td><?php if ( current_user_can('edit_pages') ) { echo "<a href='post.php?action=edit&amp;post=$id' class='edit'>" . __('Edit') . "</a>"; } ?></td>
     636    <td><?php if ( current_user_can('edit_pages') ) { echo "<a href='post.php?action=delete&amp;post=$id' class='delete' onclick=\"return deleteSomething( 'page', " . $id . ", '" . sprintf(__("You are about to delete the &quot;%s&quot; page.\\n&quot;OK&quot; to delete, &quot;Cancel&quot; to stop."), wp_specialchars(get_the_title('','',0), 1)) . "' );\">" . __('Delete') . "</a>"; } ?></td>
    786637  </tr>
    787638
    788639<?php
    789         if ( $hierarchy) page_rows($id, $level + 1, $pages);
    790     }
    791 }
    792 
    793 function user_row( $user_object, $style = '' ) {
    794     if ( !(is_object($user_object) && is_a($user_object, 'WP_User')) )
    795         $user_object = new WP_User( (int) $user_object );
    796     $email = $user_object->user_email;
    797     $url = $user_object->user_url;
    798     $short_url = str_replace('http://', '', $url);
    799     $short_url = str_replace('www.', '', $short_url);
    800     if ('/' == substr($short_url, -1))
    801         $short_url = substr($short_url, 0, -1);
    802     if (strlen($short_url) > 35)
    803         $short_url =  substr($short_url, 0, 32).'...';
    804     $numposts = get_usernumposts($user_object->ID);
    805     $r = "<tr id='user-$user_object->ID'$style>
    806         <td><input type='checkbox' name='users[]' id='user_{$user_object->ID}' value='{$user_object->ID}' /> <label for='user_{$user_object->ID}'>{$user_object->ID}</label></td>
    807         <td><label for='user_{$user_object->ID}'><strong>$user_object->user_login</strong></label></td>
    808         <td><label for='user_{$user_object->ID}'>$user_object->first_name $user_object->last_name</label></td>
    809         <td><a href='mailto:$email' title='" . sprintf(__('e-mail: %s'), $email) . "'>$email</a></td>
    810         <td><a href='$url' title='website: $url'>$short_url</a></td>";
    811     $r .= "\n\t\t<td align='center'>";
    812     if ($numposts > 0) {
    813         $r .= "<a href='edit.php?author=$user_object->ID' title='" . __('View posts by this author') . "' class='edit'>";
    814         $r .= sprintf(__('View %1$s %2$s'), $numposts, __ngettext('post', 'posts', $numposts));
    815     }
    816     $r .= "</td>\n\t\t<td>";
    817     $edit_link = add_query_arg('wp_http_referer', wp_specialchars(urlencode(stripslashes($_SERVER['REQUEST_URI']))), "user-edit.php?user_id=$user_object->ID");
    818     if ( current_user_can('edit_user', $user_object->ID) )
    819         $r .= "<a href='$edit_link' class='edit'>".__('Edit')."</a>";
    820     $r .= "</td>\n\t</tr>";
    821     return $r;
     640
     641                page_rows($id, $level +1, $pages);
     642            }
     643        }
     644    } else {
     645        return false;
     646    }
    822647}
    823648
    824649function wp_dropdown_cats($currentcat = 0, $currentparent = 0, $parent = 0, $level = 0, $categories = 0) {
    825     global $wpdb;
    826     if (!$categories)
    827         $categories = get_categories('hide_empty=0');
    828 
     650    global $wpdb, $bgcolor;
     651    if (!$categories) {
     652        $categories = $wpdb->get_results("SELECT * FROM $wpdb->categories ORDER BY cat_name");
     653    }
    829654    if ($categories) {
    830655        foreach ($categories as $category) {
    831656            if ($currentcat != $category->cat_ID && $parent == $category->category_parent) {
     657                $count = $wpdb->get_var("SELECT COUNT(post_id) FROM $wpdb->post2cat WHERE category_id = $category->cat_ID");
    832658                $pad = str_repeat('&#8211; ', $level);
    833659                $category->cat_name = wp_specialchars($category->cat_name);
     
    844670}
    845671
     672function link_category_dropdown($fieldname, $selected = 0) {
     673    global $wpdb;
     674   
     675    $results = $wpdb->get_results("SELECT cat_id, cat_name, auto_toggle FROM $wpdb->linkcategories ORDER BY cat_id");
     676    echo "\n<select name='$fieldname' size='1'>\n";
     677    foreach ($results as $row) {
     678        echo "\n\t<option value='$row->cat_id'";
     679        if ($row->cat_id == $selected)
     680            echo " selected='selected'";
     681        echo ">$row->cat_id : " . wp_specialchars($row->cat_name);
     682        if ($row->auto_toggle == 'Y')
     683            echo ' (auto toggle)';
     684        echo "</option>";
     685    }
     686    echo "\n</select>\n";
     687}
     688
    846689function wp_create_thumbnail($file, $max_side, $effect = '') {
    847690
     
    934777        return $error;
    935778    } else {
    936         apply_filters( 'wp_create_thumbnail', $thumbpath );
    937779        return $thumbpath;
    938780    }
     
    954796    global $post_ID;
    955797    // Exit if no meta
    956     if (!$meta) {
    957         echo '<tbody id="the-list"><tr style="display: none;"><td>&nbsp;</td></tr></tbody>'; //TBODY needed for list-manipulation JS
     798    if (!$meta)
    958799        return;
    959     }
    960800    $count = 0;
    961801?>
    962     <thead>
     802<table id='meta-list' cellpadding="3">
    963803    <tr>
    964804        <th><?php _e('Key') ?></th>
     
    966806        <th colspan='2'><?php _e('Action') ?></th>
    967807    </tr>
    968     </thead>
    969808<?php
    970     $r ="\n\t<tbody id='the-list'>";
     809
     810
    971811    foreach ($meta as $entry) {
    972812        ++ $count;
     
    977817        if ('_' == $entry['meta_key'] { 0 })
    978818            $style .= ' hidden';
    979 
    980         if ( is_serialized($entry['meta_value']) ) {
    981             if ( is_serialized_string($entry['meta_value']) ) {
    982                 // this is a serialized string, so we should display it
    983                 $entry['meta_value'] = maybe_unserialize($entry['meta_value']);
    984             } else {
    985                 // this is a serialized array/object so we should NOT display it
    986                 --$count;
    987                 continue;
    988             }
    989         }
    990 
    991         $key_js = js_escape($entry['meta_key']);
    992         $entry['meta_key'] = wp_specialchars( $entry['meta_key'], true );
    993         $entry['meta_value'] = wp_specialchars( $entry['meta_value'], true );
    994         $r .= "\n\t<tr id='meta-{$entry['meta_id']}' class='$style'>";
    995         $r .= "\n\t\t<td valign='top'><input name='meta[{$entry['meta_id']}][key]' tabindex='6' type='text' size='20' value='{$entry['meta_key']}' /></td>";
    996         $r .= "\n\t\t<td><textarea name='meta[{$entry['meta_id']}][value]' tabindex='6' rows='2' cols='30'>{$entry['meta_value']}</textarea></td>";
    997         $r .= "\n\t\t<td align='center'><input name='updatemeta' type='submit' class='updatemeta' tabindex='6' value='".__('Update')."' /><br />";
    998         $r .= "\n\t\t<input name='deletemeta[{$entry['meta_id']}]' type='submit' onclick=\"return deleteSomething( 'meta', {$entry['meta_id']}, '";
    999         $r .= sprintf(__("You are about to delete the &quot;%s&quot; custom field on this post.\\n&quot;OK&quot; to delete, &quot;Cancel&quot; to stop."), $key_js);
    1000         $r .= "' );\" class='deletemeta' tabindex='6' value='".__('Delete')."' /></td>";
    1001         $r .= "\n\t</tr>";
    1002     }
    1003     echo $r;
    1004     echo "\n\t</tbody>";
     819        echo "
     820            <tr class='$style'>
     821                <td valign='top'><input name='meta[{$entry['meta_id']}][key]' tabindex='6' type='text' size='20' value='{$entry['meta_key']}' /></td>
     822                <td><textarea name='meta[{$entry['meta_id']}][value]' tabindex='6' rows='2' cols='30'>{$entry['meta_value']}</textarea></td>
     823                <td align='center'><input name='updatemeta' type='submit' class='updatemeta' tabindex='6' value='".__('Update')."' /><br />
     824                <input name='deletemeta[{$entry['meta_id']}]' type='submit' class='deletemeta' tabindex='6' value='".__('Delete')."' /></td>
     825            </tr>
     826        ";
     827    }
     828    echo "
     829        </table>
     830    ";
    1005831}
    1006832
     
    1020846function meta_form() {
    1021847    global $wpdb;
    1022     $limit = (int) apply_filters('postmeta_form_limit', 30);
    1023848    $keys = $wpdb->get_col("
    1024         SELECT meta_key
    1025         FROM $wpdb->postmeta
    1026         GROUP BY meta_key
    1027         ORDER BY meta_id DESC
    1028         LIMIT $limit");
    1029     natcasesort($keys);
     849            SELECT meta_key
     850            FROM $wpdb->postmeta
     851            GROUP BY meta_key
     852            ORDER BY meta_id DESC
     853            LIMIT 10");
    1030854?>
    1031855<h3><?php _e('Add a new custom field:') ?></h3>
    1032 <table id="newmeta" cellspacing="3" cellpadding="3">
     856<table cellspacing="3" cellpadding="3">
    1033857    <tr>
    1034858<th colspan="2"><?php _e('Key') ?></th>
     
    1037861    <tr valign="top">
    1038862        <td align="right" width="18%">
    1039 <?php if ( $keys ) : ?>
     863<?php if ($keys) : ?>
    1040864<select id="metakeyselect" name="metakeyselect" tabindex="7">
    1041865<option value="#NONE#"><?php _e('- Select -'); ?></option>
    1042866<?php
    1043867
    1044     foreach ( $keys as $key ) {
    1045         $key = wp_specialchars($key, 1);
     868    foreach ($keys as $key) {
    1046869        echo "\n\t<option value='$key'>$key</option>";
    1047870    }
     
    1055878
    1056879</table>
    1057 <p class="submit"><input type="submit" id="updatemetasub" name="updatemeta" tabindex="9" value="<?php _e('Add Custom Field &raquo;') ?>" /></p>
     880<p class="submit"><input type="submit" name="updatemeta" tabindex="9" value="<?php _e('Add Custom Field &raquo;') ?>" /></p>
    1058881<?php
    1059882
     
    1062885function add_meta($post_ID) {
    1063886    global $wpdb;
    1064     $post_ID = (int) $post_ID;
    1065887
    1066888    $metakeyselect = $wpdb->escape(stripslashes(trim($_POST['metakeyselect'])));
    1067889    $metakeyinput = $wpdb->escape(stripslashes(trim($_POST['metakeyinput'])));
    1068     $metavalue = maybe_serialize(stripslashes((trim($_POST['metavalue']))));
    1069     $metavalue = $wpdb->escape($metavalue);
     890    $metavalue = $wpdb->escape(stripslashes(trim($_POST['metavalue'])));
    1070891
    1071892    if ( ('0' === $metavalue || !empty ($metavalue)) && ((('#NONE#' != $metakeyselect) && !empty ($metakeyselect)) || !empty ($metakeyinput)) ) {
     
    1073894        // input for the key have data, the input takes precedence:
    1074895
    1075         if ('#NONE#' != $metakeyselect)
     896        if ('#NONE#' != $metakeyselect)
    1076897            $metakey = $metakeyselect;
    1077898
     
    1084905                        VALUES ('$post_ID','$metakey','$metavalue')
    1085906                    ");
    1086         return $wpdb->insert_id;
    1087     }
    1088     return false;
     907    }
    1089908} // add_meta
    1090909
    1091910function delete_meta($mid) {
    1092911    global $wpdb;
    1093     $mid = (int) $mid;
    1094 
    1095     return $wpdb->query("DELETE FROM $wpdb->postmeta WHERE meta_id = '$mid'");
     912
     913    $result = $wpdb->query("DELETE FROM $wpdb->postmeta WHERE meta_id = '$mid'");
    1096914}
    1097915
    1098916function update_meta($mid, $mkey, $mvalue) {
    1099917    global $wpdb;
    1100     $mvalue = maybe_serialize(stripslashes($mvalue));
    1101     $mvalue = $wpdb->escape($mvalue);
    1102     $mid = (int) $mid;
     918
    1103919    return $wpdb->query("UPDATE $wpdb->postmeta SET meta_key = '$mkey', meta_value = '$mvalue' WHERE meta_id = '$mid'");
    1104920}
    1105921
    1106 function get_post_meta_by_id($mid) {
    1107     global $wpdb;
    1108     $mid = (int) $mid;
    1109 
    1110     $meta = $wpdb->get_row("SELECT * FROM $wpdb->postmeta WHERE meta_id = '$mid'");
    1111     if ( is_serialized_string($meta->meta_value) )
    1112         $meta->meta_value = maybe_unserialize($meta->meta_value);
    1113     return $meta;
    1114 }
    1115 
    1116922function touch_time($edit = 1, $for_post = 1) {
    1117     global $wp_locale, $post, $comment;
     923    global $month, $post, $comment;
    1118924
    1119925    if ( $for_post )
     
    1122928    echo '<fieldset><legend><input type="checkbox" class="checkbox" name="edit_date" value="1" id="timestamp" /> <label for="timestamp">'.__('Edit timestamp').'</label></legend>';
    1123929
    1124     $time_adj = time() + (get_option('gmt_offset') * 3600);
     930    $time_adj = time() + (get_settings('gmt_offset') * 3600);
    1125931    $post_date = ($for_post) ? $post->post_date : $comment->comment_date;
    1126932    $jj = ($edit) ? mysql2date('d', $post_date) : gmdate('d', $time_adj);
     
    1131937    $ss = ($edit) ? mysql2date('s', $post_date) : gmdate('s', $time_adj);
    1132938
    1133     echo "<select name=\"mm\" onchange=\"edit_date.checked=true\">\n";
     939    echo "<select name=\"mm\">\n";
    1134940    for ($i = 1; $i < 13; $i = $i +1) {
    1135941        echo "\t\t\t<option value=\"$i\"";
    1136942        if ($i == $mm)
    1137             echo ' selected="selected"';
    1138         echo '>' . $wp_locale->get_month($i) . "</option>\n";
     943            echo " selected='selected'";
     944        if ($i < 10) {
     945            $ii = "0".$i;
     946        } else {
     947            $ii = "$i";
     948        }
     949        echo ">".$month["$ii"]."</option>\n";
    1139950    }
    1140951?>
    1141952</select>
    1142 <input type="text" id="jj" name="jj" value="<?php echo $jj; ?>" size="2" maxlength="2" onchange="edit_date.checked=true"/>
    1143 <input type="text" id="aa" name="aa" value="<?php echo $aa ?>" size="4" maxlength="5" onchange="edit_date.checked=true" /> @
    1144 <input type="text" id="hh" name="hh" value="<?php echo $hh ?>" size="2" maxlength="2" onchange="edit_date.checked=true" /> :
    1145 <input type="text" id="mn" name="mn" value="<?php echo $mn ?>" size="2" maxlength="2" onchange="edit_date.checked=true" />
    1146 <input type="hidden" id="ss" name="ss" value="<?php echo $ss ?>" size="2" maxlength="2" onchange="edit_date.checked=true" />
     953<input type="text" id="jj" name="jj" value="<?php echo $jj; ?>" size="2" maxlength="2" />
     954<input type="text" id="aa" name="aa" value="<?php echo $aa ?>" size="4" maxlength="5" /> @
     955<input type="text" id="hh" name="hh" value="<?php echo $hh ?>" size="2" maxlength="2" /> :
     956<input type="text" id="mn" name="mn" value="<?php echo $mn ?>" size="2" maxlength="2" />
     957<input type="hidden" id="ss" name="ss" value="<?php echo $ss ?>" size="2" maxlength="2" />
    1147958<?php
    1148959    if ( $edit ) {
    1149960        _e('Existing timestamp');
    1150         //echo ': ' . $wp_locale->get_month($mm) . "$jj, $aa @ $hh:$mn";
    1151         echo sprintf(__(': %1$s %2$s, %3$s @ %4$s:%5$s'), $wp_locale->get_month($mm), $jj, $aa, $hh, $mn);
     961        echo ": {$month[$mm]} $jj, $aa @ $hh:$mn";
    1152962    }
    1153963?>
     
    1174984        if ($markerdata) {
    1175985            $state = true;
    1176             foreach ($markerdata as $n => $markerline) {
     986            foreach ($markerdata as $markerline) {
    1177987                if (strstr($markerline, "# BEGIN {$marker}"))
    1178988                    $state = false;
    1179                 if ($state) {
    1180                     if ( $n + 1 < count($markerdata) )
    1181                         fwrite($f, "{$markerline}\n");
    1182                     else
    1183                         fwrite($f, "{$markerline}");
    1184                 }
     989                if ($state)
     990                    fwrite($f, "{$markerline}\n");
    1185991                if (strstr($markerline, "# END {$marker}")) {
    1186992                    fwrite($f, "# BEGIN {$marker}\n");
     
    12651071}
    12661072
     1073function the_quicktags() {
     1074    // Browser detection sucks, but until Safari supports the JS needed for this to work people just assume it's a bug in WP
     1075    if (!strstr($_SERVER['HTTP_USER_AGENT'], 'Safari'))
     1076        echo '
     1077        <div id="quicktags">
     1078            <script src="../wp-includes/js/quicktags.js" type="text/javascript"></script>
     1079            <script type="text/javascript">if ( typeof tinyMCE == "undefined" || tinyMCE.configs.length < 1 ) edToolbar();</script>
     1080        </div>
     1081';
     1082    else echo '
     1083<script type="text/javascript">
     1084function edInsertContent(myField, myValue) {
     1085    //IE support
     1086    if (document.selection) {
     1087        myField.focus();
     1088        sel = document.selection.createRange();
     1089        sel.text = myValue;
     1090        myField.focus();
     1091    }
     1092    //MOZILLA/NETSCAPE support
     1093    else if (myField.selectionStart || myField.selectionStart == "0") {
     1094        var startPos = myField.selectionStart;
     1095        var endPos = myField.selectionEnd;
     1096        myField.value = myField.value.substring(0, startPos)
     1097                      + myValue
     1098                      + myField.value.substring(endPos, myField.value.length);
     1099        myField.focus();
     1100        myField.selectionStart = startPos + myValue.length;
     1101        myField.selectionEnd = startPos + myValue.length;
     1102    } else {
     1103        myField.value += myValue;
     1104        myField.focus();
     1105    }
     1106}
     1107</script>
     1108';
     1109}
     1110
     1111function validate_current_theme() {
     1112    $theme_loc = 'wp-content/themes';
     1113    $theme_root = ABSPATH.$theme_loc;
     1114
     1115    $template = get_settings('template');
     1116    $stylesheet = get_settings('stylesheet');
     1117
     1118    if (($template != 'default') && (!file_exists("$theme_root/$template/index.php"))) {
     1119        update_option('template', 'default');
     1120        update_option('stylesheet', 'default');
     1121        do_action('switch_theme', 'Default');
     1122        return false;
     1123    }
     1124
     1125    if (($stylesheet != 'default') && (!file_exists("$theme_root/$stylesheet/style.css"))) {
     1126        update_option('template', 'default');
     1127        update_option('stylesheet', 'default');
     1128        do_action('switch_theme', 'Default');
     1129        return false;
     1130    }
     1131
     1132    return true;
     1133}
     1134
    12671135function get_broken_themes() {
    12681136    global $wp_broken_themes;
     
    13091177function parent_dropdown($default = 0, $parent = 0, $level = 0) {
    13101178    global $wpdb, $post_ID;
    1311     $items = $wpdb->get_results("SELECT ID, post_parent, post_title FROM $wpdb->posts WHERE post_parent = $parent AND post_type = 'page' ORDER BY menu_order");
     1179    $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");
    13121180
    13131181    if ($items) {
     
    13371205    global $menu;
    13381206    global $submenu;
    1339     global $_wp_menu_nopriv;
    1340     global $_wp_submenu_nopriv;
    1341     global $plugin_page;
    13421207
    13431208    $parent = get_admin_page_parent();
    1344     /*echo "pa: $parent pn: $pagenow pp: $plugin_page<br/>";
    1345     echo "<pre>";
    1346     print_r($_wp_menu_nopriv);
    1347     print_r($_wp_submenu_nopriv);
    1348     echo "</pre>";*/
    1349     if ( isset($_wp_submenu_nopriv[$parent][$pagenow]) )
    1350         return false;
    1351 
    1352     if ( isset($plugin_page) && isset($_wp_submenu_nopriv[$parent][$plugin_page]) )
    1353         return false;
    1354    
    1355     if ( empty($parent) ) {
    1356         if ( isset($_wp_menu_nopriv[$pagenow]) )
    1357             return false;
    1358         if ( isset($_wp_submenu_nopriv[$pagenow][$pagenow]) )
    1359             return false;
    1360         if ( isset($plugin_page) && isset($_wp_submenu_nopriv[$pagenow][$plugin_page]) )
    1361             return false;
    1362         foreach (array_keys($_wp_submenu_nopriv) as $key) {
    1363             if ( isset($_wp_submenu_nopriv[$key][$pagenow]) )
     1209
     1210    foreach ($menu as $menu_array) {
     1211        //echo "parent array: " . $menu_array[2];
     1212        if ($menu_array[2] == $parent) {
     1213            if (!current_user_can($menu_array[1])) {
    13641214                return false;
    1365             if ( isset($plugin_page) && isset($_wp_submenu_nopriv[$key][$plugin_page]) )
    1366             return false;   
    1367         }
    1368         return true;
     1215            } else {
     1216                break;
     1217            }
     1218        }
    13691219    }
    13701220
     
    13721222        foreach ($submenu[$parent] as $submenu_array) {
    13731223            if ($submenu_array[2] == $pagenow) {
    1374                 if (current_user_can($submenu_array[1]))
     1224                if (!current_user_can($submenu_array[1])) {
     1225                    return false;
     1226                } else {
    13751227                    return true;
    1376                 else
    1377                     return false;
    1378             }
    1379         }
    1380     }
    1381 
    1382     foreach ($menu as $menu_array) {
    1383         if ($menu_array[2] == $parent) {
    1384             if (current_user_can($menu_array[1]))
    1385                 return true;
    1386             else
    1387                 return false;
    1388         }
    1389     }
    1390    
     1228                }
     1229            }
     1230        }
     1231    }
     1232
    13911233    return true;
    13921234}
     
    14451287    global $pagenow;
    14461288    global $plugin_page;
    1447     global $_wp_real_parent_file;
    1448     global $_wp_menu_nopriv;
    1449     global $_wp_submenu_nopriv;
    1450 
    1451     if ( !empty ($parent_file) ) {
    1452         if ( isset($_wp_real_parent_file[$parent_file]) )
    1453             $parent_file = $_wp_real_parent_file[$parent_file];
    1454 
     1289
     1290    if (isset ($parent_file) && !empty ($parent_file)) {
    14551291        return $parent_file;
    14561292    }
     
    14601296            if ($parent_menu[2] == $plugin_page) {
    14611297                $parent_file = $plugin_page;
    1462                 if ( isset($_wp_real_parent_file[$parent_file]) )
    1463                     $parent_file = $_wp_real_parent_file[$parent_file];
    1464                 return $parent_file;
    1465             }
    1466         }
    1467         if ( isset($_wp_menu_nopriv[$plugin_page]) ) {
    1468             $parent_file = $plugin_page;
    1469             if ( isset($_wp_real_parent_file[$parent_file]) )
    1470                     $parent_file = $_wp_real_parent_file[$parent_file];
    1471             return $parent_file;
    1472         }           
    1473     }
    1474 
    1475     if ( isset($plugin_page) && isset($_wp_submenu_nopriv[$pagenow][$plugin_page]) ) {
    1476         $parent_file = $pagenow;
    1477         if ( isset($_wp_real_parent_file[$parent_file]) )
    1478             $parent_file = $_wp_real_parent_file[$parent_file];
    1479         return $parent_file;       
     1298                return $plugin_page;
     1299            }
     1300        }
    14801301    }
    14811302
    14821303    foreach (array_keys($submenu) as $parent) {
    14831304        foreach ($submenu[$parent] as $submenu_array) {
    1484             if ( isset($_wp_real_parent_file[$parent]) )
    1485                 $parent = $_wp_real_parent_file[$parent];
    14861305            if ($submenu_array[2] == $pagenow) {
    14871306                $parent_file = $parent;
     
    15181337    global $submenu;
    15191338    global $menu;
    1520     global $_wp_real_parent_file;
    1521     global $_wp_submenu_nopriv;
    1522     global $_wp_menu_nopriv;
    1523 
     1339
     1340    $parent = plugin_basename($parent);
    15241341    $file = plugin_basename($file);
    1525 
    1526     $parent = plugin_basename($parent);
    1527     if ( isset($_wp_real_parent_file[$parent]) )
    1528         $parent = $_wp_real_parent_file[$parent];
    1529 
    1530     if ( !current_user_can($access_level) ) {
    1531         $_wp_submenu_nopriv[$parent][$file] = true;
    1532         return false;
    1533     }
    15341342
    15351343    // If the parent doesn't already have a submenu, add a link to the parent
     
    15371345    // parent file someone is trying to link back to the parent manually.  In
    15381346    // this case, don't automatically add a link back to avoid duplication.
    1539     if (!isset ($submenu[$parent]) && $file != $parent  ) {
     1347    if (!isset ($submenu[$parent]) && $file != $parent) {
    15401348        foreach ($menu as $parent_menu) {
    1541             if ( $parent_menu[2] == $parent && current_user_can($parent_menu[1]) ) {
     1349            if ($parent_menu[2] == $parent) {
    15421350                $submenu[$parent][] = $parent_menu;
    15431351            }
     
    15891397    switch ($code) {
    15901398        case 1 :
    1591             wp_die(__('Sorry, can&#8217;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.'));
     1399            die(__('Sorry, can&#8217;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.'));
    15921400
    15931401        case 2 :
    1594             wp_die(__('Sorry, can&#8217;t call files with their real path.'));
     1402            die(__('Sorry, can&#8217;t call files with their real path.'));
    15951403
    15961404        case 3 :
    1597             wp_die(__('Sorry, that file cannot be edited.'));
     1405            die(__('Sorry, that file cannot be edited.'));
    15981406    }
    15991407}
    16001408
    16011409function get_home_path() {
    1602     $home = get_option('home');
    1603     if ($home != '' && $home != get_option('siteurl')) {
     1410    $home = get_settings('home');
     1411    if ($home != '' && $home != get_settings('siteurl')) {
    16041412        $home_path = parse_url($home);
    16051413        $home_path = $home_path['path'];
     
    16331441        return $wp_file_descriptions[basename($file)];
    16341442    }
    1635     elseif ( file_exists( ABSPATH . $file ) && is_file( ABSPATH . $file ) ) {
    1636         $template_data = implode('', file( ABSPATH . $file ));
     1443    elseif (file_exists(ABSPATH.$file)) {
     1444        $template_data = implode('', file(ABSPATH.$file));
    16371445        if (preg_match("|Template Name:(.*)|i", $template_data, $name))
    16381446            return $name[1];
     
    16651473    preg_match("|Author URI:(.*)|i", $plugin_data, $author_uri);
    16661474    if (preg_match("|Version:(.*)|i", $plugin_data, $version))
    1667         $version = trim($version[1]);
     1475        $version = $version[1];
    16681476    else
    16691477        $version = '';
    16701478
    1671     $description = wptexturize(trim($description[1]));
     1479    $description = wptexturize($description[1]);
    16721480
    16731481    $name = $plugin_name[1];
     
    16751483    $plugin = $name;
    16761484    if ('' != $plugin_uri[1] && '' != $name) {
    1677         $plugin = '<a href="' . trim($plugin_uri[1]) . '" title="'.__('Visit plugin homepage').'">'.$plugin.'</a>';
     1485        $plugin = '<a href="'.$plugin_uri[1].'" title="'.__('Visit plugin homepage').'">'.$plugin.'</a>';
    16781486    }
    16791487
    16801488    if ('' == $author_uri[1]) {
    1681         $author = trim($author_name[1]);
    1682     } else {
    1683         $author = '<a href="' . trim($author_uri[1]) . '" title="'.__('Visit author homepage').'">' . trim($author_name[1]) . '</a>';
     1489        $author = $author_name[1];
     1490    } else {
     1491        $author = '<a href="'.$author_uri[1].'" title="'.__('Visit author homepage').'">'.$author_name[1].'</a>';
    16841492    }
    16851493
     
    16951503
    16961504    $wp_plugins = array ();
    1697     $plugin_root = ABSPATH . PLUGINDIR;
     1505    $plugin_loc = 'wp-content/plugins';
     1506    $plugin_root = ABSPATH.$plugin_loc;
    16981507
    16991508    // Files in wp-content/plugins directory
     
    17201529    }
    17211530
    1722     if ( !$plugins_dir || !$plugin_files )
     1531    if (!$plugins_dir || !$plugin_files) {
    17231532        return $wp_plugins;
    1724 
    1725     foreach ( $plugin_files as $plugin_file ) {
    1726         if ( !is_readable("$plugin_root/$plugin_file") )
     1533    }
     1534
     1535    sort($plugin_files);
     1536
     1537    foreach ($plugin_files as $plugin_file) {
     1538        if ( !is_readable("$plugin_root/$plugin_file"))
    17271539            continue;
    17281540
    17291541        $plugin_data = get_plugin_data("$plugin_root/$plugin_file");
    17301542
    1731         if ( empty ($plugin_data['Name']) )
     1543        if (empty ($plugin_data['Name'])) {
    17321544            continue;
     1545        }
    17331546
    17341547        $wp_plugins[plugin_basename($plugin_file)] = $plugin_data;
    17351548    }
    1736 
    1737     uasort($wp_plugins, create_function('$a, $b', 'return strnatcasecmp($a["Name"], $b["Name"]);'));
    17381549
    17391550    return $wp_plugins;
     
    18441655        __("Failed to write file to disk."));
    18451656
     1657    // Accepted MIME types are set here as PCRE. Override with $override['mimes'].
     1658    $mimes = apply_filters('upload_mimes', array (
     1659        'jpg|jpeg|jpe' => 'image/jpeg',
     1660        'gif' => 'image/gif',
     1661        'png' => 'image/png',
     1662        'bmp' => 'image/bmp',
     1663        'tif|tiff' => 'image/tiff',
     1664        'ico' => 'image/x-icon',
     1665        'asf|asx|wax|wmv|wmx' => 'video/asf',
     1666        'avi' => 'video/avi',
     1667        'mov|qt' => 'video/quicktime',
     1668        'mpeg|mpg|mpe' => 'video/mpeg',
     1669        'txt|c|cc|h' => 'text/plain',
     1670        'rtx' => 'text/richtext',
     1671        'css' => 'text/css',
     1672        'htm|html' => 'text/html',
     1673        'mp3|mp4' => 'audio/mpeg',
     1674        'ra|ram' => 'audio/x-realaudio',
     1675        'wav' => 'audio/wav',
     1676        'ogg' => 'audio/ogg',
     1677        'mid|midi' => 'audio/midi',
     1678        'wma' => 'audio/wma',
     1679        'rtf' => 'application/rtf',
     1680        'js' => 'application/javascript',
     1681        'pdf' => 'application/pdf',
     1682        'doc' => 'application/msword',
     1683        'pot|pps|ppt' => 'application/vnd.ms-powerpoint',
     1684        'wri' => 'application/vnd.ms-write',
     1685        'xla|xls|xlt|xlw' => 'application/vnd.ms-excel',
     1686        'mdb' => 'application/vnd.ms-access',
     1687        'mpp' => 'application/vnd.ms-project',
     1688        'swf' => 'application/x-shockwave-flash',
     1689        'class' => 'application/java',
     1690        'tar' => 'application/x-tar',
     1691        'zip' => 'application/zip',
     1692        'gz|gzip' => 'application/x-gzip',
     1693        'exe' => 'application/x-msdownload'
     1694    ));
     1695
    18461696    // All tests are on by default. Most can be turned off by $override[{test_name}] = false;
    18471697    $test_form = true;
     
    18711721        return $upload_error_handler($file, __('Specified file failed upload test.'));
    18721722
    1873     // A correct MIME type will pass this test. Override $mimes or use the upload_mimes filter.
     1723    // A correct MIME type will pass this test.
    18741724    if ( $test_type ) {
    1875         $wp_filetype = wp_check_filetype($file['name'], $mimes);
    1876 
    1877         extract($wp_filetype);
     1725        $type = false;
     1726        $ext = false;
     1727        foreach ($mimes as $ext_preg => $mime_match) {
     1728            $ext_preg = '![^.]\.(' . $ext_preg . ')$!i';
     1729            if ( preg_match($ext_preg, $file['name'], $ext_matches) ) {
     1730                $type = $mime_match;
     1731                $ext = $ext_matches[1];
     1732            }
     1733        }
    18781734
    18791735        if ( !$type || !$ext )
     
    19021758                $filename = str_replace("$number$ext", ++$number . $ext, $filename);
    19031759        }
    1904         $filename = str_replace($ext, '', $filename);
    1905         $filename = sanitize_title_with_dashes($filename) . $ext;
    19061760    }
    19071761
     
    19091763    $new_file = $uploads['path'] . "/$filename";
    19101764    if ( false === @ move_uploaded_file($file['tmp_name'], $new_file) )
    1911         wp_die(printf(__('The uploaded file could not be moved to %s.'), $uploads['path']));
     1765        die(printf(__('The uploaded file could not be moved to %s.'), $file['path']));
    19121766
    19131767    // Set correct file permissions
     
    19181772    // Compute the URL
    19191773    $url = $uploads['url'] . "/$filename";
    1920    
    1921     $return = apply_filters( 'wp_handle_upload', array('file' => $new_file, 'url' => $url, 'type' => $type) );
    1922 
    1923     return $return;
     1774
     1775    return array('file' => $new_file, 'url' => $url, 'type' => $type);
    19241776}
    19251777
     
    19381790
    19391791function wp_import_upload_form($action) {
    1940     $size = strtolower( ini_get('upload_max_filesize') );
    1941     $bytes = 0;
    1942     if ( strstr( $size, 'k' ) )
    1943         $bytes = $size * 1024;
    1944     if ( strstr( $size, 'm' ) )
    1945         $bytes = $size * 1024 * 1024;
    1946     if ( strstr( $size, 'g' ) )
    1947         $bytes = $size * 1024 * 1024 * 1024;
    19481792?>
    1949 <form enctype="multipart/form-data" id="import-upload-form" method="post" action="<?php echo $action ?>">
    1950 <p>
    1951 <label for="upload"><?php _e('Choose a file from your computer:'); ?></label> (<?php printf( __('Maximum size: %s'), $size ); ?>)
    1952 <input type="file" id="upload" name="import" size="25" />
     1793<script type="text/javascript">
     1794function cancelUpload() {
     1795o = document.getElementById('uploadForm');
     1796o.method = 'GET';
     1797o.action.value = 'view';
     1798o.submit();
     1799}
     1800</script>
     1801<form enctype="multipart/form-data" id="uploadForm" method="POST" action="<?php echo $action ?>">
     1802<label for="upload"><?php _e('File:'); ?></label><input type="file" id="upload" name="import" />
    19531803<input type="hidden" name="action" value="save" />
    1954 <input type="hidden" name="max_file_size" value="<?php echo $bytes; ?>" />
    1955 </p>
    1956 <p class="submit">
    1957 <input type="submit" value="<?php _e('Upload file and import'); ?> &raquo;" />
    1958 </p>
     1804<div id="buttons">
     1805<input type="submit" value="<?php _e('Import'); ?>" />
     1806<input type="button" value="<?php _e('Cancel'); ?>" onclick="cancelUpload()" />
     1807</div>
    19591808</form>
    1960 <?php
     1809<?php   
    19611810}
    19621811
     
    19691818
    19701819    $url = $file['url'];
    1971     $file = addslashes( $file['file'] );
     1820    $file = $file['file'];
    19721821    $filename = basename($file);
    19731822
     
    19861835}
    19871836
     1837function user_can_richedit() {
     1838    if ( 'true' != get_user_option('rich_editing') )
     1839        return false;
     1840
     1841    if ( preg_match('!opera[ /][2-8]|konqueror|safari!i', $_SERVER['HTTP_USER_AGENT']) )
     1842        return false;
     1843
     1844    return true; // Best guess
     1845}
     1846
    19881847function the_attachment_links($id = false) {
    19891848    $id = (int) $id;
    19901849    $post = & get_post($id);
    19911850
    1992     if ( $post->post_type != 'attachment' )
     1851    if ( $post->post_status != 'attachment' )
    19931852        return false;
    19941853
    19951854    $icon = get_attachment_icon($post->ID);
    1996     $attachment_data = get_post_meta( $id, '_wp_attachment_metadata', true );
    1997     $thumb = isset($attachment_data['thumb']);
     1855
    19981856?>
    1999 <form id="the-attachment-links">
    2000 <table>
    2001     <col />
    2002     <col class="widefat" />
    2003     <tr>
    2004         <th scope="row"><?php _e('URL') ?></th>
    2005         <td><textarea rows="1" cols="40" type="text" class="attachmentlinks" readonly="readonly"><?php echo $post->guid ?></textarea></td>
    2006     </tr>
     1857<p><?php _e('Text linked to file') ?><br />
     1858<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>
     1859<p><?php _e('Text linked to subpost') ?><br />
     1860<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>
    20071861<?php if ( $icon ) : ?>
    2008     <tr>
    2009         <th scope="row"><?php $thumb ? _e('Thumbnail linked to file') : _e('Image linked to file'); ?></th>
    2010         <td><textarea rows="1" cols="40" type="text" class="attachmentlinks" readonly="readonly"><a href="<?php echo $post->guid; ?>"><?php echo $icon ?></a></textarea></td>
    2011     </tr>
    2012     <tr>
    2013         <th scope="row"><?php $thumb ? _e('Thumbnail linked to page') : _e('Image linked to file'); ?></th>
    2014         <td><textarea rows="1" cols="40" type="text" class="attachmentlinks" readonly="readonly"><a href="<?php echo get_attachment_link($post->ID) ?>" rel="attachment wp-att-<?php echo $post->ID; ?>"><?php echo $icon ?></a></textarea></td>
    2015     </tr>
    2016 <?php else : ?>
    2017     <tr>
    2018         <th scope="row"><?php _e('Link to file') ?></th>
    2019         <td><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></td>
    2020     </tr>
    2021     <tr>
    2022         <th scope="row"><?php _e('Link to page') ?></th>
    2023         <td><textarea rows="1" cols="40" type="text" class="attachmentlinks" readonly="readonly"><a href="<?php echo get_attachment_link($post->ID) ?>" rel="attachment wp-att-<?php echo $post->ID ?>"><?php the_title(); ?></a></textarea></td>
    2024     </tr>
     1862<p><?php _e('Thumbnail linked to file') ?><br />
     1863<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>
     1864<p><?php _e('Thumbnail linked to subpost') ?><br />
     1865<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>
    20251866<?php endif; ?>
    2026 </table>
    2027 </form>
    20281867<?php
    20291868}
     
    20381877}
    20391878
    2040 function wp_reset_vars($vars) {
    2041     for ($i=0; $i<count($vars); $i += 1) {
    2042         $var = $vars[$i];
    2043         global $$var;
    2044 
    2045         if (!isset($$var)) {
    2046             if (empty($_POST["$var"])) {
    2047                 if (empty($_GET["$var"]))
    2048                     $$var = '';
    2049                 else
    2050                     $$var = $_GET["$var"];
    2051             } else {
    2052                 $$var = $_POST["$var"];
    2053             }
    2054         }
    2055     }
    2056 }
    2057 
    2058 // If siteurl or home changed, reset cookies and flush rewrite rules.
    2059 function update_home_siteurl($old_value, $value) {
    2060     global $wp_rewrite, $user_login, $user_pass_md5;
    2061 
    2062     if ( defined("WP_INSTALLING") )
    2063         return;
    2064 
    2065     // If home changed, write rewrite rules to new location.
    2066     $wp_rewrite->flush_rules();
    2067     // Clear cookies for old paths.
    2068     wp_clearcookie();
    2069     // Set cookies for new paths.
    2070     wp_setcookie($user_login, $user_pass_md5, true, get_option('home'), get_option('siteurl'));
    2071 }
    2072 
    2073 add_action('update_option_home', 'update_home_siteurl', 10, 2);
    2074 add_action('update_option_siteurl', 'update_home_siteurl', 10, 2);
    2075 
    2076 function wp_crop_image($src_file, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false) {
    2077     if ( ctype_digit($src_file) ) // Handle int as attachment ID
    2078         $src_file = get_attached_file($src_file);
    2079 
    2080     $src = wp_load_image($src_file);
    2081 
    2082     if ( !is_resource($src) )
    2083         return $src;
    2084 
    2085     $dst = imagecreatetruecolor($dst_w, $dst_h);
    2086 
    2087     if ( $src_abs ) {
    2088         $src_w -= $src_x;
    2089         $src_h -= $src_y;
    2090     }
    2091 
    2092     imageantialias($dst, true);
    2093     imagecopyresampled($dst, $src, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
    2094 
    2095     if ( !$dst_file )
    2096         $dst_file = str_replace(basename($src_file), 'cropped-'.basename($src_file), $src_file);
    2097 
    2098     $dst_file = preg_replace('/\\.[^\\.]+$/', '.jpg', $dst_file);
    2099 
    2100     if ( imagejpeg($dst, $dst_file) )
    2101         return $dst_file;
    2102     else
    2103         return false;
    2104 }
    2105 
    2106 function wp_load_image($file) {
    2107     if ( ctype_digit($file) )
    2108         $file = get_attached_file($file);
    2109 
    2110     if ( !file_exists($file) )
    2111         return "File '$file' doesn't exist?";
    2112 
    2113     $contents = file_get_contents($file);
    2114 
    2115     $image = imagecreatefromstring($contents);
    2116 
    2117     if ( !is_resource($image) )
    2118         return "File '$file' is not image?";
    2119 
    2120     return $image;
    2121 }
    2122 
    21231879?>
Note: See TracChangeset for help on using the changeset viewer.