我有一些预写的对象,可以执行诸如文件检查MIME类型之类的操作,然后是一个主上载对象,但它们都不使用数据库。所以我很好奇是否可以将这些Obejcts集成到Wordpress插件中。
defined( \'ABSPATH\' ) or die( \'No script kiddies please!\' );
add_action(\'admin_menu\', \'file_upload_menu\');
function file_upload_menu()
{
add_options_page(\'E4K File Uploader\', \'E4k File Uploader\', \'manage_options\', \'e4k-File-Uploader\', \'file_upload_options\');
}
function file_upload_options()
{
class Example
{
public function canI(){ echo \'is this allowed?\'; }
}
(new Example)->canI();
}
这样可能吗?我不是Wordpress开发人员,我只是来自开发CMS和API的PHP背景。这对我来说是新的,但我是被要求的,所以我只是好奇Wordpress和/或其插件中OOP的局限性。
Actual Code (Class):
class FileSecure
{
public $Allowed;
private $Info;
public function __construct($allow)
{
$this->Allowed = $allow;
$this->Info = new finfo();
}
public function upload($file, $dir)
{
$target = $dir . basename($file["name"]);
(self::Check($file))? move_uploaded_file($file[\'tmp_name\'], $target) : "";
}
public function Check($file)
{
if (in_array($fileType = $this->Info->file($file, FILEINFO_MIME_TYPE, $this->Allowed))) { return true; } else { return false; }
}
}
$fileCheck = array(
\'Image\' => new FileSecure( [\'image/bmp\', \'image/gif\', \'image/jpeg\', \'image/png\'] ),
\'Text\' => new FileSecure( [\'text/plain\'] ),
\'Compressed\' => new FileSecure( [\'application/zip\', \'application/x-rar-compressed\'] )
);
Actual Code (back-end function part):
function file_upload_options()
{
if (!current_user_can(\'manage_options\'))
{
wp_die(__(\'You do not have sufficient permissions to access this page.\'));
}
if (isset($_FILES[\'Picture\']))
{
require_once \'e4k-file-upload-class.php\';
if($fileCheck[\'Image\']->upload($_FILES[\'Picture\'], \'Uploads/\')): echo \'<script>alert("Successfully uploaded");</script>\'; endif;
}
echo \'<form enctype="multipart/form-data" method="POST">\';
echo \'<input type="hidden" name="MAX_FILE_SIZE" value="30000" />\';
echo \'Select a file to upload: <input name="Picture" type="file" />\';
echo \'<input type="submit" value="Upload File" />\';
echo \'</form>\';
echo \'<br /> <br />\';
echo \'Current Libary: <br /> <br />\';
foreach(uploaded_files_iterate(\'Upload/\') as $file)
{
(is_string($file))? $file : \'<a href="\'.Get_template_directory_uri().\'/Uploads/\'.$file[\'name\'].\'">\'.$file[\'name\'].\'</a> <br />\';
}
}