本文整理汇总了PHP中WP_Filesystem函数的典型用法代码示例。如果您正苦于以下问题:PHP WP_Filesystem函数的具体用法?PHP WP_Filesystem怎么用?PHP WP_Filesystem使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了WP_Filesystem函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: generate
/**
* Generate a child theme.
*
* @since 1.1.0
*/
public function generate()
{
global $wp_filesystem;
WP_Filesystem();
$parent = wp_get_theme($this->template);
if (!$parent->exists()) {
return new WP_Error('invalid_template', esc_html__('Invalid parent theme slug.', 'audiotheme-agent'));
}
$parts = explode('/', $parent->get_template());
$slug = sprintf('%s-child', reset($parts));
$directory = path_join($parent->get_theme_root(), $slug);
if ($wp_filesystem->exists($directory)) {
return new WP_Error('directory_exists', esc_html__('Child theme directory already exists.', 'audiotheme-agent'));
}
if (false === $wp_filesystem->mkdir($directory)) {
return new WP_Error('fs_error', esc_html__('Could not create child theme directory.', 'audiotheme-agent'));
}
$source = audiotheme_agent()->get_path('data/child-theme/');
copy_dir($source, $directory);
if ($parent->get_screenshot()) {
$wp_filesystem->copy(path_join($parent->get_template_directory(), $parent->get_screenshot('relative')), path_join($directory, $parent->get_screenshot('relative')));
}
$data = array('{{author}}' => wp_get_current_user()->display_name, '{{author_url}}' => wp_get_current_user()->user_url, '{{name}}' => $parent->get('Name'), '{{slug}}' => $parent->get_template(), '{{url}}' => esc_url(home_url()));
$files = array('functions.php', 'style.css');
foreach ($files as $file) {
$filename = path_join($directory, $file);
$contents = $wp_filesystem->get_contents($filename);
$contents = str_replace(array_keys($data), array_values($data), $contents);
$wp_filesystem->put_contents($filename, $contents);
}
return true;
}
示例2: uploads_proxy
/**
* Get uploads from the production site and store them
* in the local filesystem if they don't already exist.
*
* @return void
*/
function uploads_proxy()
{
global $wp_filesystem;
WP_Filesystem();
// The relative request path
$requestPath = $_SERVER['REQUEST_URI'];
// The relative uploads path
$uploadsPath = str_replace(get_bloginfo('url'), '', wp_upload_dir()['baseurl']);
// Check if a upload was requested
if (strpos($requestPath, $uploadsPath) === 0) {
// The absolute remote path to the upload
$remotePath = UP_SITEURL . $requestPath;
// Get the remote upload file
$response = wp_remote_get($remotePath);
// Check the response code
if ($response['response']['code'] === 200) {
// The file path relative to the uploads path to store the upload file to
$relativeUploadFile = str_replace($uploadsPath, '', $_SERVER['REQUEST_URI']);
// The absolute file path to store the upload file to
$absoluteUploadFile = wp_upload_dir()['basedir'] . $relativeUploadFile;
// Make sure the upload directory exists
wp_mkdir_p(pathinfo($absoluteUploadFile)['dirname']);
if ($wp_filesystem->put_contents(urldecode($absoluteUploadFile), $response['body'], FS_CHMOD_FILE)) {
// Redirect to the stored upload
wp_redirect($requestPath);
}
}
}
}
示例3: eventon_generate_options_css
function eventon_generate_options_css($newdata = '')
{
/** Define some vars **/
$data = $newdata;
$uploads = wp_upload_dir();
//$css_dir = get_template_directory() . '/css/'; // Shorten code, save 1 call
$css_dir = AJDE_EVCAL_DIR . '/' . EVENTON_BASE . '/assets/css/';
//$css_dir = plugin_dir_path( __FILE__ ). '/assets/css/';
//echo $css_dir;
/** Save on different directory if on multisite **/
if (is_multisite()) {
$aq_uploads_dir = trailingslashit($uploads['basedir']);
} else {
$aq_uploads_dir = $css_dir;
}
/** Capture CSS output **/
ob_start();
require $css_dir . 'dynamic_styles.php';
$css = ob_get_clean();
//print_r($css);
/** Write to options.css file **/
WP_Filesystem();
global $wp_filesystem;
if (!$wp_filesystem->put_contents($aq_uploads_dir . 'eventon_dynamic_styles.css', $css, 0777)) {
return true;
}
}
示例4: _get_wp_filesystem
/**
* @throws EE_Error
* @return WP_Filesystem_Base
*/
private static function _get_wp_filesystem()
{
global $wp_filesystem;
// no filesystem setup ???
if (!$wp_filesystem instanceof WP_Filesystem_Base) {
// if some eager beaver's just trying to get in there too early...
if (!did_action('wp_loaded')) {
$msg = __('An attempt to access and/or write to a file on the server could not be completed due to a lack of sufficient credentials.', 'event_espresso');
if (WP_DEBUG) {
$msg .= '<br />' . __('The WP Filesystem can not be accessed until after the "wp_loaded" hook has run, so it\'s best not to attempt access until the "admin_init" hookpoint.', 'event_espresso');
}
throw new EE_Error($msg);
} else {
// should be loaded if we are past the wp_loaded hook...
if (!function_exists('WP_Filesystem')) {
require_once ABSPATH . 'wp-admin/includes/file.php';
}
// basically check for direct or previously configured access
if (!WP_Filesystem()) {
// turn on output buffering so that we can capture the credentials form
ob_start();
$credentials = request_filesystem_credentials('');
// store credentials form for the time being
EEH_File::$_credentials_form = ob_get_clean();
// if credentials do NOT exist
if ($credentials === FALSE) {
add_action('admin_notices', array('EEH_File', 'display_request_filesystem_credentials_form'), 999);
throw new EE_Error(__('An attempt to access and/or write to a file on the server could not be completed due to a lack of sufficient credentials.', 'event_espresso'));
}
}
}
}
return $wp_filesystem;
}
示例5: _validate_form
protected function _validate_form()
{
$url = wp_nonce_url('admin.php?page=vimeography-my-themes');
if (false === ($creds = request_filesystem_credentials($url))) {
// if we get here, then we don't have credentials yet,
// but have just produced a form for the user to fill in,
// so stop processing for now
return true;
// stop the normal page form from displaying
}
// now we have some credentials, try to get the wp_filesystem running
if (!WP_Filesystem($creds)) {
// our credentials were no good, ask the user for them again
request_filesystem_credentials($url);
return true;
}
if (empty($_FILES)) {
return;
}
// if this fails, check_admin_referer() will automatically print a "failed" page and die.
if (!empty($_FILES) && check_admin_referer('vimeography-install-theme', 'vimeography-theme-verification')) {
$name = substr(wp_filter_nohtml_kses($_FILES['vimeography-theme']['name']), 0, -4);
if ($_FILES['vimeography-theme']['type'] != 'application/zip') {
$this->messages[] = array('type' => 'error', 'heading' => 'Ruh Roh.', 'message' => 'Make sure you are uploading the actual .zip file, not a subfolder or file.');
} else {
global $wp_filesystem;
if (!unzip_file($_FILES['vimeography-theme']['tmp_name'], VIMEOGRAPHY_THEME_PATH)) {
$this->messages[] = array('type' => 'error', 'heading' => 'Ruh Roh.', 'message' => 'The theme could not be installed.');
} else {
$this->messages[] = array('type' => 'success', 'heading' => 'Theme installed.', 'message' => 'You can now use the "' . $name . '" theme in your galleries.');
}
}
}
}
示例6: delete
/**
* put your comment there...
*
*/
public function delete()
{
// Initialize.
WP_Filesystem();
// initialize vars.
$wpFileSystem =& $GLOBALS['wp_filesystem'];
$ids = array();
$dbDriver = cssJSToolbox::getInstance()->getDBDriver();
$fsConfig = cssJSToolbox::$config->fileSystem;
// Import dependencies.
cssJSToolbox::import('framework:db:mysql:xtable.inc.php');
CJTxTable::import('template');
// Delete only templates in "trash" state!
// For more security don't even delete templates with SYSTEM Attribute flag
// is turned ON!
$sysFlag = CJTTemplateTable::ATTRIBUTES_SYSTEM_FLAG;
$idsQueryList = implode(',', $this->inputs['ids']);
$templates = $dbDriver->select("SELECT id, queueName `directory`\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tFROM #__cjtoolbox_templates \n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tWHERE ID IN ({$idsQueryList}) AND ((attributes & {$sysFlag}) = 0) AND (`state` = 'trash')");
if (!empty($templates)) {
// Deleing template directory files.
foreach ($templates as $template) {
// Absolute path to template directory!
$templateDirectoryAbsPath = WP_CONTENT_DIR . "/{$fsConfig->contentDir}/{$fsConfig->templatesDir}/{$template->directory}";
// Delete template directory RECUSIVLY!
$wpFileSystem->rmdir($templateDirectoryAbsPath, true);
}
// Get templates IDs to delete.
$ids = implode(', ', array_keys($templates));
// Permenantly delete all templates data from
// templates table and all refernced tables.
$dbDriver->startTransaction()->delete("DELETE FROM #__cjtoolbox_block_templates WHERE templateId IN ({$ids})")->delete("DELETE FROM #__cjtoolbox_template_revisions WHERE templateId IN ({$ids})")->delete("DELETE FROM #__cjtoolbox_templates WHERE id IN ({$ids})")->commit()->processQueue();
}
return $ids;
}
示例7: download
public function download()
{
if (!function_exists('WP_Filesystem')) {
require ABSPATH . 'wp-admin/includes/file.php';
}
global $wp_filesystem;
WP_Filesystem();
$u = wp_upload_dir();
$basedir = $u['basedir'];
$remove = str_replace(get_option('siteurl'), '', $u['baseurl']);
$basedir = str_replace($remove, '', $basedir);
$abspath = $basedir . $this->get_local_path();
$dir = dirname($abspath);
if (!is_dir($dir) && !wp_mkdir_p($dir)) {
$this->display_and_exit("Please check permissions. Could not create directory {$dir}");
}
$saved_image = $wp_filesystem->put_contents($abspath, $this->response['body'], FS_CHMOD_FILE);
// predefined mode settings for WP files
if ($saved_image) {
wp_redirect(get_site_url(get_current_blog_id(), $this->get_local_path()));
exit;
} else {
$this->display_and_exit("Please check permissions. Could not write image {$dir}");
}
}
示例8: export
function export()
{
global $wpdb;
$px_table_name = $wpdb->prefix . 'gcm_users';
$query = "SELECT * FROM {$px_table_name}";
$datas = $wpdb->get_results($query);
$url = wp_nonce_url('admin.php?page=px-gcm-export', 'px-gcm-export');
if (false === ($creds = request_filesystem_credentials($url, '', false, false, null))) {
return true;
}
if (!WP_Filesystem($creds)) {
// our credentials were not good, ask the user for them again
request_filesystem_credentials($url, '', true, false, null);
return true;
}
global $wp_filesystem;
$contentdir = trailingslashit($wp_filesystem->wp_content_dir());
$in = "Databse ID;GCM Registration ID;Device OS;Device Model;Created At;Messages sent to this Device;\n";
foreach ($datas as $data) {
$in .= $data->id . ";" . $data->gcm_regid . ";" . $data->os . ";" . $data->model . ";" . $data->created_at . ";" . $data->send_msg . "\n";
}
mb_convert_encoding($in, "ISO-8859-1", "UTF-8");
if (!$wp_filesystem->put_contents($contentdir . 'GCM-Export.csv', $in, FS_CHMOD_FILE)) {
echo 'Failed saving file';
}
return content_url() . "/GCM-Export.csv";
}
示例9: __construct
function __construct()
{
// register lazy autoloading
spl_autoload_register('self::lazy_loader');
// filter for s3 images
add_filter('wp_get_attachment_url', array($this, 'wp_get_attachment_url'), 10, 2);
add_filter('image_downsize', array($this, 'image_downsize'), 10, 2);
// admin only functionality
if (is_admin()) {
// ensure that $wp_filesystem is activated for this plugin
if (!function_exists('download_url')) {
require_once ABSPATH . 'wp-admin/includes/file.php';
}
global $wp_filesystem;
WP_Filesystem();
// enable the settings
$this->rsrgroup = new WC_RSRGroup_Integration();
// TODO: fix " Call to undefined function get_plugin_data() "
// $this->plugin_data = get_plugin_data( __FILE__ );
$this->path = self::get_plugin_path();
$this->dir = trailingslashit(basename($this->path));
$this->url = plugins_url() . '/' . $this->dir;
add_action('init', array($this, 'load_textdomain'));
add_action('init', array($this, 'load_custom_fields'));
add_action('admin_menu', array($this, 'admin_menu'));
add_filter('manage_upload_columns', array($this, 'manage_upload_columns'));
add_action('manage_media_custom_column', array($this, 'manage_media_custom_column'), 10, 2);
add_action('woocommerce_rsrgroup_import_inventory', array($this, 'import_inventory'));
// WooCommerce actions
add_action('woocommerce_product_options_pricing', array($this, 'product_options_pricing'));
add_action('woocommerce_product_options_sku', array($this, 'product_options_sku'));
add_action('woocommerce_process_product_meta_simple', array($this, 'process_product_meta'));
}
}
示例10: xt_upgrade
/**
* Upgrade
*
* All the functionality for upgrading XT Themes
*
* @since 1.0.0
*/
function xt_upgrade()
{
global $XT_Theme, $wpdb, $wp_filesystem, $post;
$xt_version_option_key = XT_THEME_ID . '_version';
$xt_theme = wp_get_theme();
$old_version = get_option($xt_version_option_key, '1.0.0');
// false
$new_version = $XT_Theme->parent_version;
if ($new_version !== $old_version) {
/*
* 1.0.4
*
* @created 2015-02-21
*/
if ($old_version < '1.0.4') {
$xt_upload_dir = XT_Theme::get_upload_dir();
if (empty($wp_filesystem)) {
require_once ABSPATH . '/wp-admin/includes/file.php';
WP_Filesystem();
}
if ($wp_filesystem->is_dir($xt_upload_dir['dir'] . '/assets/')) {
$wp_filesystem->delete($xt_upload_dir['dir'] . '/assets/', true, 'd');
$wp_filesystem->delete($xt_upload_dir['dir'] . '/bower_components/', true, 'd');
$wp_filesystem->delete($xt_upload_dir['dir'] . '/cache/', true, 'd');
}
update_option($xt_version_option_key, '1.0.4');
}
update_option($xt_version_option_key, $new_version);
self::sass_compile_flag();
xt_redirect_after_migration();
}
}
示例11: __construct
public function __construct()
{
if (!function_exists('WP_Filesystem')) {
require_once ABSPATH . 'wp-admin/includes/file.php';
}
WP_Filesystem();
}
示例12: try_download
/**
* Try downloading and saving the image locally and then redirect to it.
* @param string $img_path Path of the image inside uploads folder
*/
public function try_download($img_path)
{
if (!function_exists('WP_Filesystem')) {
require ABSPATH . 'wp-admin/includes/file.php';
}
global $wp_filesystem;
WP_Filesystem();
$mirror_url = $this->get_mirror_url($img_path);
if ($mirror_url === false) {
status_header(404);
exit;
}
// Download
$response = wp_remote_get($mirror_url);
// Die if not successful.
if (is_wp_error($response) || 200 !== $response['response']['code']) {
wp_die(__('Unable to download the file.', 'h1aid'));
}
$body = wp_remote_retrieve_body($response);
$abspath = $this->content_base_dir;
$destination = trailingslashit($abspath) . $img_path;
// Save to file system.
$result = $wp_filesystem->put_contents($destination, $response['body'], FS_CHMOD_FILE);
// predefined mode settings for WP files
// Redirect if successful.
if (true === $result) {
wp_redirect(trailingslashit($this->content_base_url) . $img_path, 301);
exit;
} else {
wp_die(__('Unable to save file to filesystem.', 'h1aid'));
}
}
示例13: save_temp_image
/**
* Save the submitted image as a temporary file.
*
* @todo Revisit file handling.
*
* @param string $img Base64 encoded image.
* @return false|string File name on success, false on failure.
*/
protected function save_temp_image($img)
{
// Strip the "data:image/png;base64," part and decode the image.
$img = explode(',', $img);
$img = isset($img[1]) ? base64_decode($img[1]) : base64_decode($img[0]);
if (!$img) {
return false;
}
// Upload to tmp folder.
$filename = 'user-feedback-' . date('Y-m-d-H-i');
$tempfile = wp_tempnam($filename);
if (!$tempfile) {
return false;
}
// WordPress adds a .tmp file extension, but we want .png.
if (rename($tempfile, $filename . '.png')) {
$tempfile = $filename . '.png';
}
if (!WP_Filesystem(request_filesystem_credentials(''))) {
return false;
}
/**
* WordPress Filesystem API.
*
* @var \WP_Filesystem_Base $wp_filesystem
*/
global $wp_filesystem;
$success = $wp_filesystem->put_contents($tempfile, $img);
if (!$success) {
return false;
}
return $tempfile;
}
示例14: save_file
/**
* Save the redirect file
*
* @return bool
*/
public function save_file()
{
global $wp_filesystem;
// Generate file content
$file_content = $this->generate_file_content();
if (null == $file_content) {
return false;
}
// Set the filesystem URL
$url = wp_nonce_url('admin.php?page=wpseo_redirects#top#settings', 'update-htaccess');
// Get the credentials
$credentials = request_filesystem_credentials($url, '', false, WPSEO_Redirect_File_Manager::get_htaccess_file_path());
// Check if WP_Filesystem is working
if (WP_Filesystem($credentials, WPSEO_Redirect_File_Manager::get_htaccess_file_path())) {
// Read current htaccess
$htaccess = '';
if (file_exists(WPSEO_Redirect_File_Manager::get_htaccess_file_path())) {
$htaccess = file_get_contents(WPSEO_Redirect_File_Manager::get_htaccess_file_path());
}
$htaccess = preg_replace("`# BEGIN YOAST REDIRECTS.*# END YOAST REDIRECTS" . PHP_EOL . "`is", "", $htaccess);
// New Redirects
$file_content = "# BEGIN YOAST REDIRECTS" . PHP_EOL . "<IfModule mod_rewrite.c>" . PHP_EOL . "RewriteEngine On" . PHP_EOL . $file_content . "</IfModule>" . PHP_EOL . "# END YOAST REDIRECTS" . PHP_EOL;
// Prepend our redirects to htaccess file
$htaccess = $file_content . $htaccess;
// Update the .htaccess file
$wp_filesystem->put_contents(WPSEO_Redirect_File_Manager::get_htaccess_file_path(), $htaccess, FS_CHMOD_FILE);
}
}
示例15: parseFile
/**
* Parse PDF file
*
* @param string $filename
*
* @return Document
*/
public function parseFile($filename)
{
global $wp_filesystem;
WP_Filesystem();
$content = $wp_filesystem->exists($filename) ? $wp_filesystem->get_contents($filename) : '';
return $this->parseContent($content);
}