I have pre-written Objects that do such things like file checking the MIME type, then a main upload object but neither of which use the Database. So I am just curious to whether I could intergrate these Obejcts inside a Wordpress Plugin.
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();
}
Is something like that Possible? I am not a Wordpress developer, I just come from a PHP background developing CMS's and API's. This is new to me but was requested so I am just curious to limitations of OOP within Wordpress and/or its plugins.
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 />';
}
}
require_once()and then instance the class to get the Object inside the function, I just wanted to know if You can use OOP inside of wordpress - I was just told that its limited to using Codex and not creating your own Objects.