admin-functions.php
来自「php 开发的内容管理系统」· PHP 代码 · 共 1,864 行 · 第 1/4 页
PHP
1,864 行
function add_theme_page($page_title, $menu_title, $access_level, $file, $function = '') {
return add_submenu_page('themes.php', $page_title, $menu_title, $access_level, $file, $function);
}
function validate_file($file, $allowed_files = '') {
if (false !== strpos($file, './'))
return 1;
if (':' == substr($file, 1, 1))
return 2;
if (!empty ($allowed_files) && (!in_array($file, $allowed_files)))
return 3;
return 0;
}
function validate_file_to_edit($file, $allowed_files = '') {
$file = stripslashes($file);
$code = validate_file($file, $allowed_files);
if (!$code)
return $file;
switch ($code) {
case 1 :
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.'));
case 2 :
die(__('Sorry, can’t call files with their real path.'));
case 3 :
die(__('Sorry, that file cannot be edited.'));
}
}
function get_home_path() {
$home = get_settings('home');
if ($home != '' && $home != get_settings('siteurl')) {
$home_path = parse_url($home);
$home_path = $home_path['path'];
$root = str_replace($_SERVER["PHP_SELF"], '', $_SERVER["SCRIPT_FILENAME"]);
$home_path = trailingslashit($root.$home_path);
} else {
$home_path = ABSPATH;
}
return $home_path;
}
function get_real_file_to_edit($file) {
if ('index.php' == $file || '.htaccess' == $file) {
$real_file = get_home_path().$file;
} else {
$real_file = ABSPATH.$file;
}
return $real_file;
}
$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)'),
// Deprecated files
'wp-layout.css' => __('Stylesheet'), 'wp-comments.php' => __('Comments Template'), 'wp-comments-popup.php' => __('Popup Comments Template'));
function get_file_description($file) {
global $wp_file_descriptions;
if (isset ($wp_file_descriptions[basename($file)])) {
return $wp_file_descriptions[basename($file)];
}
elseif (file_exists(ABSPATH.$file)) {
$template_data = implode('', file(ABSPATH.$file));
if (preg_match("|Template Name:(.*)|i", $template_data, $name))
return $name[1];
}
return basename($file);
}
function update_recently_edited($file) {
$oldfiles = (array) get_option('recently_edited');
if ($oldfiles) {
$oldfiles = array_reverse($oldfiles);
$oldfiles[] = $file;
$oldfiles = array_reverse($oldfiles);
$oldfiles = array_unique($oldfiles);
if (5 < count($oldfiles))
array_pop($oldfiles);
} else {
$oldfiles[] = $file;
}
update_option('recently_edited', $oldfiles);
}
function get_plugin_data($plugin_file) {
$plugin_data = implode('', file($plugin_file));
preg_match("|Plugin Name:(.*)|i", $plugin_data, $plugin_name);
preg_match("|Plugin URI:(.*)|i", $plugin_data, $plugin_uri);
preg_match("|Description:(.*)|i", $plugin_data, $description);
preg_match("|Author:(.*)|i", $plugin_data, $author_name);
preg_match("|Author URI:(.*)|i", $plugin_data, $author_uri);
if (preg_match("|Version:(.*)|i", $plugin_data, $version))
$version = trim($version[1]);
else
$version = '';
$description = wptexturize(trim($description[1]));
$name = $plugin_name[1];
$name = trim($name);
$plugin = $name;
if ('' != $plugin_uri[1] && '' != $name) {
$plugin = '<a href="' . trim($plugin_uri[1]) . '" title="'.__('Visit plugin homepage').'">'.$plugin.'</a>';
}
if ('' == $author_uri[1]) {
$author = trim($author_name[1]);
} else {
$author = '<a href="' . trim($author_uri[1]) . '" title="'.__('Visit author homepage').'">' . trim($author_name[1]) . '</a>';
}
return array ('Name' => $name, 'Title' => $plugin, 'Description' => $description, 'Author' => $author, 'Version' => $version, 'Template' => $template[1]);
}
function get_plugins() {
global $wp_plugins;
if (isset ($wp_plugins)) {
return $wp_plugins;
}
$wp_plugins = array ();
$plugin_loc = 'wp-content/plugins';
$plugin_root = ABSPATH.$plugin_loc;
// Files in wp-content/plugins directory
$plugins_dir = @ dir($plugin_root);
if ($plugins_dir) {
while (($file = $plugins_dir->read()) !== false) {
if (preg_match('|^\.+$|', $file))
continue;
if (is_dir($plugin_root.'/'.$file)) {
$plugins_subdir = @ dir($plugin_root.'/'.$file);
if ($plugins_subdir) {
while (($subfile = $plugins_subdir->read()) !== false) {
if (preg_match('|^\.+$|', $subfile))
continue;
if (preg_match('|\.php$|', $subfile))
$plugin_files[] = "$file/$subfile";
}
}
} else {
if (preg_match('|\.php$|', $file))
$plugin_files[] = $file;
}
}
}
if (!$plugins_dir || !$plugin_files) {
return $wp_plugins;
}
sort($plugin_files);
foreach ($plugin_files as $plugin_file) {
if ( !is_readable("$plugin_root/$plugin_file"))
continue;
$plugin_data = get_plugin_data("$plugin_root/$plugin_file");
if (empty ($plugin_data['Name'])) {
continue;
}
$wp_plugins[plugin_basename($plugin_file)] = $plugin_data;
}
return $wp_plugins;
}
function get_plugin_page_hookname($plugin_page, $parent_page) {
global $admin_page_hooks;
$parent = get_admin_page_parent();
if (empty ($parent_page) || 'admin.php' == $parent_page) {
if (isset ($admin_page_hooks[$plugin_page]))
$page_type = 'toplevel';
else
if (isset ($admin_page_hooks[$parent]))
$page_type = $admin_page_hooks[$parent];
} else
if (isset ($admin_page_hooks[$parent_page])) {
$page_type = $admin_page_hooks[$parent_page];
} else {
$page_type = 'admin';
}
$plugin_name = preg_replace('!\.php!', '', $plugin_page);
return $page_type.'_page_'.$plugin_name;
}
function get_plugin_page_hook($plugin_page, $parent_page) {
global $wp_filter;
$hook = get_plugin_page_hookname($plugin_page, $parent_page);
if (isset ($wp_filter[$hook]))
return $hook;
else
return '';
}
function browse_happy() {
$getit = __('WordPress recommends a better browser');
echo '
<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>
';
}
if (strstr($_SERVER['HTTP_USER_AGENT'], 'MSIE'))
add_action('admin_footer', 'browse_happy');
function documentation_link($for) {
return;
}
function register_importer($id, $name, $description, $callback) {
global $wp_importers;
$wp_importers[$id] = array ($name, $description, $callback);
}
function get_importers() {
global $wp_importers;
return $wp_importers;
}
function current_theme_info() {
$themes = get_themes();
$current_theme = get_current_theme();
$ct->name = $current_theme;
$ct->title = $themes[$current_theme]['Title'];
$ct->version = $themes[$current_theme]['Version'];
$ct->parent_theme = $themes[$current_theme]['Parent Theme'];
$ct->template_dir = $themes[$current_theme]['Template Dir'];
$ct->stylesheet_dir = $themes[$current_theme]['Stylesheet Dir'];
$ct->template = $themes[$current_theme]['Template'];
$ct->stylesheet = $themes[$current_theme]['Stylesheet'];
$ct->screenshot = $themes[$current_theme]['Screenshot'];
$ct->description = $themes[$current_theme]['Description'];
$ct->author = $themes[$current_theme]['Author'];
return $ct;
}
// array wp_handle_upload ( array &file [, array overrides] )
// file: reference to a single element of $_FILES. Call the function once for each uploaded file.
// overrides: an associative array of names=>values to override default variables with extract($overrides, EXTR_OVERWRITE).
// On success, returns an associative array of file attributes.
// On failure, returns $overrides['upload_error_handler'](&$file, $message) or array('error'=>$message).
function wp_handle_upload(&$file, $overrides = false) {
// The default error handler.
if (! function_exists('wp_handle_upload_error') ) {
function wp_handle_upload_error(&$file, $message) {
return array('error'=>$message);
}
}
// You may define your own function and pass the name in $overrides['upload_error_handler']
$upload_error_handler = 'wp_handle_upload_error';
// $_POST['action'] must be set and its value must equal $overrides['action'] or this:
$action = 'wp_handle_upload';
// Courtesy of php.net, the strings that describe the error indicated in $_FILES[{form field}]['error'].
$upload_error_strings = array(false,
__("The uploaded file exceeds the <code>upload_max_filesize</code> directive in <code>php.ini</code>."),
__("The uploaded file exceeds the <em>MAX_FILE_SIZE</em> directive that was specified in the HTML form."),
__("The uploaded file was only partially uploaded."),
__("No file was uploaded."),
__("Missing a temporary folder."),
__("Failed to write file to disk."));
// All tests are on by default. Most can be turned off by $override[{test_name}] = false;
$test_form = true;
$test_size = true;
// If you override this, you must provide $ext and $type!!!!
$test_type = true;
// Install user overrides. Did we mention that this voids your warranty?
if ( is_array($overrides) )
extract($overrides, EXTR_OVERWRITE);
// A correct form post will pass this test.
if ( $test_form && (!isset($_POST['action']) || ($_POST['action'] != $action)) )
return $upload_error_handler($file, __('Invalid form submission.'));
// A successful upload will pass this test. It makes no sense to override this one.
if ( $file['error'] > 0 )
return $upload_error_handler($file, $upload_error_strings[$file['error']]);
// A non-empty file will pass this test.
if ( $test_size && !($file['size'] > 0) )
return $upload_error_handler($file, __('File is empty. Please upload something more substantial.'));
// A properly uploaded file will pass this test. There should be no reason to override this one.
if (! @ is_uploaded_file($file['tmp_name']) )
return $upload_error_handler($file, __('Specified file failed upload test.'));
// A correct MIME type will pass this test. Override $mimes or use the upload_mimes filter.
if ( $test_type ) {
$wp_filetype = wp_check_filetype($file['name'], $mimes);
extract($wp_filetype);
if ( !$type || !$ext )
return $upload_error_handler($file, __('File type does not meet security guidelines. Try another.'));
}
// A writable uploads dir will pass this test. Again, there's no point overriding this one.
if ( ! ( ( $uploads = wp_upload_dir() ) && false === $uploads['error'] ) )
return $upload_error_handler($file, $uploads['error']);
// Increment the file number until we have a unique file to save in $dir. Use $override['unique_filename_callback'] if supplied.
if ( isset($unique_filename_callback) && function_exists($unique_filename_callback) ) {
$filename = $unique_filename_callback($uploads['path'], $file['name']);
} else {
$number = '';
$filename = str_replace('#', '_', $file['name']);
$filename = str_replace(array('\\', "'"), '', $filename);
if ( empty($ext) )
$ext = '';
else
$ext = ".$ext";
while ( file_exists($uploads['path'] . "/$filename") ) {
if ( '' == "$number$ext" )
$filename = $filename . ++$number . $ext;
else
$filename = str_replace("$number$ext", ++$number . $ext, $filename);
}
$filename = str_replace($ext, '', $filename);
$filename = sanitize_title_with_dashes($filename) . $ext;
}
// Move the file to the uploads dir
$new_file = $uploads['path'] . "/$filename";
if ( false === @ move_uploaded_file($file['tmp_name'], $new_file) )
die(printf(__('The uploaded file could not be moved to %s.'), $file['path']));
// Set correct file permissions
$stat = stat(dirname($new_file));
$perms = $stat['mode'] & 0000666;
@ chmod($new_file, $perms);
// Compute the URL
$url = $uploads['url'] . "/$filename";
return array('file' => $new_file, 'url' => $url, 'type' => $type);
}
function wp_shrink_dimensions($width, $height, $wmax = 128, $hmax = 96) {
if ( $height <= $hmax && $width <= $wmax )
return array($width, $height);
elseif ( $width / $height > $wmax / $hmax )
return array($wmax, (int) ($height / $width * $wmax));
else
return array((int) ($width / $height * $hmax), $hmax);
}
function wp_import_cleanup($id) {
wp_delete_attachment($id);
}
function wp_import_upload_form($action) {
?>
<script type="text/javascript">
function cancelUpload() {
o = document.getElementById('uploadForm');
o.method = 'GET';
o.action.value = 'view';
o.submit();
}
</script>
<form enctype="multipart/form-data" id="uploadForm" method="post" action="<?php echo $action ?>">
<label for="upload"><?php _e('File:'); ?></label><input type="file" id="upload" name="import" />
<input type="hidden" name="action" value="save" />
<div id="buttons">
<input type="submit" value="<?php _e('Import'); ?>" />
<input type="button" value="<?php _e('Cancel'); ?>" onclick="cancelUpload()" />
</div>
</form>
<?php
}
function wp_import_handle_upload() {
$overrides = array('test_form' => false, 'test_type' => false);
$file = wp_handle_upload($_FILES['import'], $overrides);
if ( isset($file['error']) )
return $file;
$url = $file['url'];
$file = $file['file'];
$filename = basename($file);
// Construct the object array
$object = array(
'post_title' => $filename,
'post_content' => $url,
'post_mime_type' => 'import',
'guid' => $url
);
// Save the data
$id = wp_insert_attachment($object, $file);
return array('file' => $file, 'id' => $id);
}
function user_can_richedit() {
if ( 'true' != get_user_option('rich_editing') )
return false;
if ( preg_match('!opera[ /][2-8]|konqueror|safari!i', $_SERVER['HTTP_USER_AGENT']) )
return false;
return true; // Best guess
}
function the_attachment_links($id = false) {
$id = (int) $id;
$post = & get_post($id);
if ( $post->post_status != 'attachment' )
return false;
$icon = get_attachment_icon($post->ID);
?>
<p><?php _e('Text linked to file') ?><br />
<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>
<p><?php _e('Text linked to subpost') ?><br />
<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>
<?php if ( $icon ) : ?>
<p><?php _e('Thumbnail linked to file') ?><br />
<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>
<p><?php _e('Thumbnail linked to subpost') ?><br />
<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>
<?php endif; ?>
<?php
}
function get_udims($width, $height) {
if ( $height <= 96 && $width <= 128 )
return array($width, $height);
elseif ( $width / $height > 4 / 3 )
return array(128, (int) ($height / $width * 128));
else
return array((int) ($width / $height * 96), 96);
}
?>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?