Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Chris-Code-Sample-1.1
<?php
//sc_media_upload.php
//
//2020.10.17.CT-TODO: - List latest 5 or 10 items from the MediaServer
// - Create New Function to List Media
// - Use this new function to check and see if media is uploaded correctly
//WP API DOC:
// https://developer.wordpress.org/rest-api/reference/media/
// https://developer.wordpress.org/rest-api/reference/media/#list-media
//2021.01.24.CT: -Session-Bug-Fix:
// If you want more info, check the following URL
// https://www.xspdf.com/resolution/53892058.html
//
add_shortcode('sc_media_upload','MediaInit');
//
class cMedia {
public $img_upload_button;
public $s_url_ajax;
public $convert_Api_Key;
//2019.09.16.CT:DONE
public $s_url_CloudConvertAPI;
public $WPAPIUser;
public $WPAPIPassword;
public $WPAPIservice_url;
//2019.10.05.CT:DONE
public $aFileTypesAllowed;
public $aFileTypesAllowedStr;
public $aFileTypesConverted;
public $aFileTypesAllowedJS;
//2019.10.27.CT:DONE
public $sFilename;
//2021.01.25.CT:DONE
public $sURLNextPage;
public $sNextPageLinkText;
public $bCloudConvertEnabled;
public function __construct () {
$this->img_upload_button = plugin_dir_url( __FILE__ ).'sa-upload-button.png';
$this->s_url_ajax = admin_url('admin-ajax.php');
$this->convert_Api_Key = "*****";
$this->s_url_CloudConvertAPI = "https://api.cloudconvert.com/v1/convert";
$this->WPAPIUser = "*****";
$this->WPAPIPassword = "*****";
$this->WPAPIservice_url = "*****/wp-json/wp/v2/media";
//2019.10.05.CT:DONE
// TODO:Future: Possible Values: jpg, png, mp4, mp3
$this->aFileTypesAllowed=array("jpg","JPG","png","PNG","jpeg","JPEG","mp3","MP3","gif","GIF");
$this->aFileTypesConverted=array("jpg","JPG","png","PNG","jpeg","JPEG","gif","GIF");
$this->aFileTypesAllowedJS = implode(",", $this->aFileTypesAllowed);
$this->initAllowedTypes();
//2019.10.27.CT:DONE
$this->sFilename="WPAPI";
}
public function initAllowedTypes() {
$types = '';
foreach($this->aFileTypesAllowed as $type) {
if($types != '')
$types .= ',';
$types .= '.'.$type;
}
$this->aFileTypesAllowedStr = $types;
}
}
//Main Shortcode Function - MediaInit:
//
function MediaInit($atts) {
extract(shortcode_atts(array(
'media_category' => 'Default',
'media_form_id' => '0',
'media_tag' => '2021-XXX-01-XXX',
'next_page_url_slug' => 'TODO-Next-Form-Page',
'next_page_link_text' => 'DONE-Click Here to Continue ->',
'cloud_convert_enabled' => '0',
), $atts));
//2021.01.21.CT - Need a Taxanomy to Prefix the media filename:
//2021.01.25.CT - MediaSubmitKey - Identifies Media to Form Entry
//
// sMediaSubmitKey = FormID-UserId-DateTimeKey
// DateTimeKey = https://www.php.net/manual/en/datetime.formats.compound.php
// DateTimeKey = https://stackoverflow.com/questions/20822821/what-is-a-unix-timestamp-and-why-use-it
// DateTimeKey = Unix Timestamp "@" "-"? [0-9]+ "@1215282385"
//
$sMediaSubmitKey = $media_form_id."-".get_current_user_id()."-".strtotime("now");
//
$sMediaFileNamePrefix =
$media_category
."-KEY-".$sMediaSubmitKey
."-TAG-".$media_tag."-FILE-";
//2021.01.21.CT - Current User Is Admin:
$bCurrentUserIsAdmin = 0;
//
if(current_user_can('administrator')) {
$bCurrentUserIsAdmin = 1;
}
else {
$bCurrentUserIsAdmin = 0;
}
$oMedia = new cMedia();
$oMedia->sFilename = $oMedia->sFilename.'-'.$sMediaFileNamePrefix;
//2021.01.29.CT:
$oMedia->bCloudConvertEnabled = cloud_convert_enabled;
//2021.01.29.CT:TODO - Pseudocode
//Add this code to Guzzle:
// IF ($oMedia->bCloudConvertEnabled = 1)
// Convert File via CloudConvert
// ELSE IF ($oMedia->bCloudConvertEnabled = 0)
// Do NOT ConvertFile (Do not send to CloudConvertAPI)
// Upload Directly to SoftArt MediaServer -> WPAPIservice_url
// END IF
//2021.01.25.CT:TODO: continue_form_url_slug
//ROOT = https://www.voice.club/
//sURLNextPage = ROOT + next_page_url_slug
//
$sURLRoot = "https://www.voice.club/";
$sURLParam_sMediaSubmitKey = "?MediaSubmitKey=".$sMediaSubmitKey;
//
$oMedia->sURLNextPage = $sURLRoot
.$next_page_url_slug
.$sURLParam_sMediaSubmitKey;
$oMedia->sNextPageLinkText = $next_page_link_text;
//
media_Init_Global_JS_Vars(
$oMedia->s_url_ajax,
$oMedia->aFileTypesAllowedStr,
$bCurrentUserIsAdmin);
upload_init($oMedia);
$oMedia_json = json_encode($oMedia);
$_SESSION['oMedia'] = $oMedia_json;
}
function media_Init_Global_JS_Vars(
$sURL_Admin_Ajax,
$aFileTypesAllowedJS,
$bCurrentUserIsAdmin) {
//
$sHTML = <<<HTML
<script type="text/javascript" >
var sa_ajax_url = "$sURL_Admin_Ajax";
var types_str = "$aFileTypesAllowedJS";
var sa_media_upload_file_types = types_str.split(",");
var upload_Image_width_Allowed = 500;
var upload_Image_height_Allowed = 500;
var bCurrentUserIsAdmin = $bCurrentUserIsAdmin;
</script>
HTML;
//
return $sHTML;
}
//2021.01.12.CT: htmlAdminOnly - Only Admin user can see this HTML:
function htmlAdminOnly_GetImageByURL() {
$sHTML = <<<HTML
<div>
<input id="image_path"
type="text"
placeholder="Enter Image URL"
style="width:80%;">
<input id="post_image"
type="button"
class="wpbf-button"
name="post_image"
value="Upload Image">
</div>
HTML;
if(current_user_can('administrator')) {
return $sHTML;
}
else {
return "";
}
//
}
//2021.01.12.CT: htmlAdminOnly - Only Admin user can see this HTML:
function htmlAdminOnly_GetMedia() {
$sHTML = <<<HTML
<div>
<input id="getMedia"
type="button"
class="wpbf-button"
name="getMedia"
value="get media">
</input>
<select id="resultNum"
name="resultNum"
style="width:100px;">
<option value="1">1</option>
<option value="5">5</option>
<option value="10">10</option>
<option value="15">15</option>
</select>
<div id='media'
style="display:none; padding-top:20px;">
</div>
</div>
HTML;
if(current_user_can('administrator')) {
return $sHTML;
}
else {
return "";
}
//
}
function upload_init($oMedia){
//
$htmlAdminOnly_GetImageByURL = htmlAdminOnly_GetImageByURL();
$htmlAdminOnly_GetMedia = htmlAdminOnly_GetMedia();
//
$sHTML = <<<HTML
<!-- 2020.03.31.CT - Text Below Does Not Align -->
<!-- <h3>Select File and Upload:</h3> -->
<form method="post" enctype="multipart/form-data" action="">
<div class="image-upload">
<label for="file-input">
<!-- 2021.01.26.CT-TODO: Replace URL Below with STAFF Image URL -->
<img id="imageUploadButton"
src="*****/sa-upload-button.png" />
</label>
<input id='fileToUpload'
type='file'
name='fileToUpload'
accept='$oMedia->aFileTypesAllowedStr'>
</input>
<div>
<!-- <b>Allowed File Types: $oMedia->aFileTypesAllowedStr </b> -->
<!-- <b>Allowed File Types: jpg, png, mp3, mp4</b> -->
</div>
$htmlAdminOnly_GetImageByURL
</div>
</form>
<div id="progressDivId"
style="display:none;
padding-top:20px;" >
<div id="bar"
class="line stripesLoader"
style="background-position:0%; background-color:green">
</div>
</div>
<div id='msg'
style="display:none; padding-top:20px;">
</div>
<hr>
$htmlAdminOnly_GetMedia
HTML;
//
return $sHTML;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment