Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: Vion : Cocoon
- Version: 1.0.0
- Description: Retrieves images from Cocoon media
- Author: M. Dekker
- Author URI: http://www.totaldesign.com
- Text-domain: vion-cocoon
- */
- if ( ! defined( 'ABSPATH' ) ) {
- exit;
- } // Exit if accessed directly
- if ( ! class_exists( 'VionCocoon' ) ) {
- class VionCocoon {
- var $wsdl;
- var $bb;
- var $user; // Provided by Cocoon
- var $sk; // Provided by Cocoon
- var $filters = array();
- function __construct() {
- $this->wsdl = getenv( 'COCOON_API_WSDL' );
- $this->bb = getenv( 'COCOON_API_DOMAIN' );
- $this->user = getenv( 'COCOON_API_USERNAME' );
- $this->sk = getenv( 'COCOON_API_SECRET' );
- $this->settings = array(
- 'path' => trailingslashit( dirname( __FILE__ ) ),
- 'version' => '1.0.0',
- );
- try {
- $this->authenticate();
- } catch ( SoapFault $e ) {
- // throw error ?
- }
- }
- /**
- * @throws SoapFault
- */
- function authenticate() {
- $requestId = (string)microtime( true ); //Something unique
- // create hash
- $hash = $this->get_request_hash( $requestId );
- $oAuth = new stdClass;
- $oAuth->username = $this->user;
- $oAuth->requestId = $requestId;
- $oAuth->hash = $hash;
- //Init client
- $soap_connection = new SoapClient( $this->wsdl );
- $soap_connection->__setSoapHeaders( new SoapHeader( 'auth', 'authenticate', $oAuth ) );
- try {
- return $soap_connection;
- }
- catch ( SoapFault $oSoapFault ) {
- error_log( $oSoapFault->faultcode, $oSoapFault->getMessage() );
- // var_dump( $oSoapFault->faultcode, $oSoapFault->getMessage() );
- }
- }
- /**
- * Hash a string
- *
- * @param $request_id
- *
- * @return string
- */
- function get_request_hash( $request_id ) {
- $concatString = $this->bb . $this->user . $request_id . $this->sk;
- $hash = sha1( $concatString );
- return $hash;
- }
- /**
- * Get image url from Cocoon media lib
- *
- * @param bool $image_id
- *
- * @return bool|string
- */
- function get_image( $image_id, $size ) {
- $soap = $this->authenticate();
- $file_name = false;
- $image_info = $soap->getFile( $image_id );
- $image_path = 'files/' . $size . '/';
- if ( is_array( $image_info ) && isset( $image_info[ 'filename' ] ) ) {
- $file_name = ( is_array( $image_info ) && isset( $image_info[ 'filename' ] ) ) ? $image_info[ 'filename' ] : false;
- }
- $image_url = 'https://vionfoodgroup.use-cocoon.com/' . $image_path . $file_name . '.' . $image_info[ 'extension' ];
- return $image_url;
- }
- /**
- * return the one true VionCocoon instance
- *
- * @return VionCocoon
- */
- public static function get_instance() {
- static $instance;
- if ( $instance === null ) {
- $instance = new self();
- }
- return $instance;
- }
- }
- // initialize
- VionCocoon::get_instance();
- }
Add Comment
Please, Sign In to add comment