Chris-Code-Sample-1.3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: sa-wp-guzzle | |
Description: Voice.club - Guzzle - HTTP Layer | |
Version: 2020.07.27 | |
Author: SoftArt | |
*/ | |
//2020.07.27.CT: Composer Lib + sa-wp-guzzle - Production Install: | |
//------ | |
//COMPOSER - Install -> SSH + SUDO access via PuTTY or CodeAnywhere SSH: | |
// STEP #1: https://getcomposer.org/download/ | |
// STEP #2: | |
// SoftArt (CodeAnywhere):$ composer require guzzlehttp/guzzle:^7.0 | |
// Voice.club (production):$ php composer.phar require guzzlehttp/guzzle:^7.0 | |
// STEP #3: Activate Plugin: sa-wp-guzzle | |
// | |
// http://docs.guzzlephp.org/en/latest/quickstart.html | |
//------ | |
// Load Composer | |
require 'vendor/autoload.php'; | |
use GuzzleHttp\Client; | |
use GuzzleHttp\Psr7\Request; | |
//NOTE: SoftArt API - Global URL + Token: | |
// $_SESSION["sa_api_token"] | |
// $_SESSION["sa_api_url"] | |
//Guzzle - Set Global $client (for this plugin): | |
// | |
function GuzzleClient() { | |
$client = new Client([ | |
// Base URI is used with relative requests | |
'base_uri' => $_SESSION["sa_api_url"], | |
// You can set any number of default request options. | |
'timeout' => 2.0, | |
]); | |
return $client; | |
} | |
////////// | |
// BEGIN: Guzzle - WordPress Connect to SoftArtAPI --> Get MS-SQL Data | |
// Connecting WordPress to an External Microsoft SQL Server DB | |
////////// | |
////////// | |
// BEGIN: Guzzle - MediaUpload | |
// NOTE: Process Uploaded Image using 2 distinct external APIs | |
// 1. CloudConvert.com - API - to convert, resize, and roate image | |
// 2. WordPress API - Transfer and Host Final Converted Image | |
////////// | |
//2020.11.21.CT:DONE: Move saveFile --> Plugin --> sa_wp_guzzle: | |
function sa_guzzle_postWPAPIMedia() { | |
session_start(); | |
//NOTE: Required to Deserialize Class Object from Session | |
$oMedia_json= $_SESSION['oMedia']; | |
$oMedia = json_decode($oMedia_json); | |
$ext; | |
$img_name; | |
$img_path; | |
$post; | |
if(isset($_FILES['fileToUpload'])) { | |
$img_name = $_FILES["fileToUpload"]["name"]; | |
$img_path = $_FILES["fileToUpload"]["tmp_name"]; | |
$ext = pathinfo($_FILES['fileToUpload']['name'], PATHINFO_EXTENSION); | |
$new_name = str_replace($ext,"converted.".$ext,$img_name); | |
//2020.12.06.CT:CloudConvert API Params: Upload File from PC (local file) | |
$post =[ | |
[ | |
'name' => 'apikey', | |
'contents' => $oMedia->convert_Api_Key | |
], | |
[ | |
'name' => 'inputformat', | |
'contents' => $ext | |
], | |
[ | |
'name' => 'outputformat', | |
'contents' => $ext | |
], | |
[ | |
'name' => 'input', | |
'contents' => 'upload' | |
], | |
[ | |
'name' => 'filename', | |
'contents' => $new_name | |
], | |
[ | |
'name' => 'converteroptions[auto_orient]', | |
'contents' => 'true' | |
], | |
[ | |
'name' => 'converteroptions[resize]', | |
'contents' => '500' | |
], | |
[ | |
'name' => 'wait', | |
'contents' => 'true' | |
], | |
[ | |
'name' => 'download', | |
'contents' => 'false' | |
], | |
[ | |
'name' => 'file', | |
'contents' => fopen($img_path, 'r'), | |
'filename' => $img_name | |
] | |
]; | |
} | |
else if(isset($_POST['image_path'])) { | |
$img_path = $_POST['image_path']; | |
$ext = pathinfo($img_path, PATHINFO_EXTENSION); | |
$img_name = pathinfo($img_path, PATHINFO_FILENAME); | |
$new_name = str_replace($ext,"converted.".$ext,$img_name); | |
//2020.12.06.CT:CloudConvert API Params: Upload File from URL | |
$post =[ | |
[ | |
'name' => 'apikey', | |
'contents' => $oMedia->convert_Api_Key | |
], | |
[ | |
'name' => 'inputformat', | |
'contents' => $ext | |
], | |
[ | |
'name' => 'outputformat', | |
'contents' => $ext | |
], | |
[ | |
'name' => 'input', | |
'contents' => 'download' | |
], | |
[ | |
'name' => 'converteroptions[auto_orient]', | |
'contents' => 'true' | |
], | |
[ | |
'name' => 'converteroptions[resize]', | |
'contents' => '500' | |
], | |
[ | |
'name' => 'wait', | |
'contents' => 'true' | |
], | |
[ | |
'name' => 'download', | |
'contents' => 'false' | |
], | |
[ | |
'name' => 'file', | |
'contents' => $img_path | |
] | |
]; | |
} | |
else { | |
exit(); | |
} | |
if (in_array($ext, $oMedia->aFileTypesAllowed)) { | |
if ($oMedia->bCloudConvertEnabled == 1 && in_array($ext, $oMedia->aFileTypesConverted)) { | |
//2020.12.06.CT:Guzzle: | |
$headers = ['Connection' => 'keep-alive', | |
'cache-control'=> 'no-cache']; | |
$client = new Client([ | |
'base_uri' => $oMedia->s_url_CloudConvertAPI | |
]); | |
$response = $client->request('POST', | |
$oMedia->s_url_CloudConvertAPI, | |
['multipart' => $post, 'headers' => $headers]); | |
$sBody = $response->getBody()->getContents(); | |
$json = json_decode($sBody); | |
// | |
saveFile("http:".$json->output->url, $oMedia->sFilename."-".$json->output->filename, | |
$oMedia->WPAPIservice_url, $oMedia->WPAPIUser, $oMedia->WPAPIPassword, | |
$oMedia->sURLNextPage,$oMedia->sNextPageLinkText); | |
} | |
else { | |
saveFile($img_path, $oMedia->sFilename."-".$img_name, | |
$oMedia->WPAPIservice_url, $oMedia->WPAPIUser, $oMedia->WPAPIPassword, | |
$oMedia->sURLNextPage,$oMedia->sNextPageLinkText); | |
} | |
} | |
else { | |
echo '<pre id="msg">'; | |
echo "<font color='red'>This file type (".$ext.") is not supported. </font>"; | |
echo '</pre>'; | |
} | |
exit(); | |
} | |
add_action('wp_ajax_postWPAPIMedia', 'sa_guzzle_postWPAPIMedia'); | |
add_action( 'wp_ajax_nopriv_postWPAPIMedia', 'sa_guzzle_postWPAPIMedia'); | |
function saveFile($img_path, $img_name, | |
$service_url, $_APIUser, $_APIPassword, | |
$sURLNextPage,$sNextPageLinkText) | |
{ | |
//$curl = curl_init($service_url); | |
$data = file_get_contents($img_path); | |
if($data === false) | |
{ | |
echo '<pre id="msg"><font color="red"> | |
Error - Contact Admin - Failed to Access the File. | |
</font></pre>'; | |
} | |
else | |
{ | |
$headers = ['cache-control'=> 'no-cache', | |
'content-disposition'=> 'attachment; filename='.$img_name]; | |
$client = new Client([ | |
'base_uri' => $service_url | |
]); | |
$response = $client->request('POST', $service_url, | |
['body' => $data | |
, 'headers' => $headers | |
, 'auth' => [ | |
$_APIUser, | |
$_APIPassword | |
]]); | |
$sBody = $response->getBody()->getContents(); | |
$json = json_decode($sBody); | |
//2020.03.31.CT: Clean Up Messages for Production: | |
echo '<pre id="msg">'; | |
if(!empty($json)) { | |
if(!empty($json->data->status) && $json->data->status != 200) | |
echo "<font color='red'>Error - Contact Admin</br>".$json->message."</font>"; | |
else { | |
//2020.03.31.CT:DONE: Pass in Shortcode PARAM - PageToRedirectOnSuccess | |
$sURL = $sURLNextPage; | |
echo "<h2><a href='".$sURL."'>$sNextPageLinkText</a></h2>"; | |
} | |
} | |
else { | |
echo "<font color='red'>Error - Contact Admin</font>"; | |
} | |
echo '</pre>'; | |
} | |
} | |
function sa_guzzle_getWPAPIMedia() { | |
$resultNum = $_GET['resultNum']; | |
if(is_null($resultNum)) { | |
$resultNum = 1; | |
} | |
$client = new Client([ | |
'base_uri' => 'https://*****/wp-json/wp/v2', | |
'timeout' => 2.0, | |
]); | |
$dataArray = array("per_page"=>$resultNum); | |
$data = http_build_query($dataArray); | |
$headers = ['Accept' => 'application/json', | |
'cache-control'=> 'no-cache']; | |
try { | |
$response = $client->request('GET', | |
'https://*****/wp-json/wp/v2/media?'.$data, | |
[ | |
'auth' => [ | |
'*****', | |
'*****' | |
], | |
'headers' => $headers | |
]); | |
$sBody = $response->getBody()->getContents(); | |
$json = json_decode($sBody); | |
} | |
catch ( Exception $e ) { | |
$json = null; | |
} | |
echo '<pre >'; | |
if(!empty($json)) | |
{ | |
foreach($json as $key=>$item) | |
{ | |
if($item->media_type == "image") | |
echo '<img src="'.$item->source_url | |
.'" width="140" height="140" style="padding:10px;"/>'; | |
else if($item->media_type == "file") | |
echo '<audio controls><source src="' | |
.$item->source_url.'" type="' | |
.$item->mime_type.'"></audio>'; | |
if(($key+1)%5 == 0) | |
echo '</br>'; | |
} | |
} | |
else { | |
echo "<font color='red'>Error - Contact Admin</font>"; | |
} | |
echo '</pre>'; | |
exit(); | |
} | |
add_action('wp_ajax_getWPAPIMedia', 'sa_guzzle_getWPAPIMedia'); | |
add_action( 'wp_ajax_nopriv_getWPAPIMedia', 'sa_guzzle_getWPAPIMedia'); | |
//--------------------------- | |
//2020.11.21.CT:END: | |
////////// | |
// END: Guzzle - MediaUpload | |
// WP-API + CloudConvert API | |
////////// |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment