diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1377554 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.swp diff --git a/admin.php b/admin.php index 6fa49e3..39b7b59 100755 --- a/admin.php +++ b/admin.php @@ -14,168 +14,162 @@ * @package NetxWPPlugin */ class netxMediaManagerPluginAdmin { - public static function init() { - new netxMediaManagerPluginAdmin(); - } - - /** - * Add sub page to the Settings Menu - */ - public function __construct() { - add_options_page('NetX Settings', 'NetX', 'administrator', __FILE__, array($this, 'settingsPage')); - add_action('admin_init', array($this, 'registerSettings')); - } - - /** - * Register plugin settings - */ - public function registerSettings() { - register_setting('netx_options', 'netx_options', array($this, 'netxValidateOptions')); - - add_settings_section('main_section', 'DAM Settings', array($this, 'mainSection'), __FILE__); - - add_settings_field('netx_username', 'NetX Username', array($this, 'netxUsername'), __FILE__, 'main_section'); - add_settings_field('netx_password', 'NetX Password', array($this, 'netxPassword'), __FILE__, 'main_section'); - add_settings_field('netx_uri', 'NetX URI', array($this, 'netxURI'), __FILE__, 'main_section'); - add_settings_field('netx_paging_size', 'NetX Media Page Size', array($this, 'netxPageSize'), __FILE__, 'main_section'); - - - add_settings_field('netx_base_category_id', 'NetX Base Category', array($this, 'netxCategory'), __FILE__, 'main_section'); - add_settings_field('netx_base_category_path', '', array($this, 'netxCategoryPath'), __FILE__, 'main_section'); - - add_settings_field('netx_access_token', 'NetX API Token', array($this, 'netxApiToken'), __FILE__, 'main_section'); - } - - /** - * Settings main section callback - */ - public function mainSection() { - echo '
'; - } - - /** - * Create NetX username field - */ - public function netxUsername() { - $options = get_option('netx_options'); - echo ''; - } - - /** - * Create NetX password field - */ - public function netxPassword() { - $options = get_option('netx_options'); - echo ''; - } - - public function netxApiToken() { - $options = get_option('netx_options'); - echo ''; - } - - /** - * Create NetX URI field - */ - public function netxURI() { - $options = get_option('netx_options'); - echo '

NetX Uri should not include protocol or any trailing path. The uri should only include domain name. Example: "subdomain.netx.net"

'; - echo ''; - } - - /** - * Create NetX Page Size field - */ - public function netxPageSize() { - $options = get_option('netx_options'); - echo ''; - } - - /** - * Create NetX Category dropdown - */ - public function netxCategory() { - $options = get_option('netx_options'); - if ( - (strlen($options['netx_username']) > 0) && - (strlen($options['netx_password']) > 0) && - (strlen($options['netx_uri']) > 0)) { - - try { - $netx = new netxRestWrapper(); - $catList = $netx->getCategoryPathList(); - echo ''; - } catch(Exception $e) { - echo('

Could not connect to NetX. Check username, password, and uri.

'); - } - } - } - - /** - * Create NetX Category Path hidden field - */ - public function netxCategoryPath(){ - $options = get_option('netx_options'); - echo ''; - } - - /** - * Validate options field input - */ - public function netxValidateOptions($input) { - try { - $tmpURI = preg_replace('/^https?:\/+/i', '', $input['netx_uri']); - $input['netx_uri'] = $tmpURI; - - if ($input['netx_base_category_id']) { - $input['netx_base_category_path'] = self::getCategoryPath($input['netx_base_category_id']); - } - } catch (Exception $e) { - error_log("netxValidateOptions: " . $e->getMessage()); - } - - return $input; - } - - /** - * Settings page - */ - public function settingsPage() { + public static function init() { + new netxMediaManagerPluginAdmin(); + } + + /** + * Add sub page to the Settings Menu + */ + public function __construct() { + add_options_page('NetX Settings', 'NetX', 'administrator', __FILE__, array($this, 'settingsPage')); + add_action('admin_init', array($this, 'registerSettings')); + } + + /** + * Register plugin settings + */ + public function registerSettings() { + register_setting('netx_options', 'netx_options', array($this, 'netxValidateOptions')); + + add_settings_section('main_section', 'DAM Settings', array($this, 'mainSection'), __FILE__); + + add_settings_field('netx_username', 'NetX Username', array($this, 'netxUsername'), __FILE__, 'main_section'); + add_settings_field('netx_password', 'NetX Password', array($this, 'netxPassword'), __FILE__, 'main_section'); + add_settings_field('netx_uri', 'NetX URI', array($this, 'netxURI'), __FILE__, 'main_section'); + add_settings_field('netx_uri_protocal', 'NetX URI Protocal', array($this, 'netxURIProtocal'), __FILE__, 'main_section'); + add_settings_field('netx_paging_size', 'NetX Media Page Size', array($this, 'netxPageSize'), __FILE__, 'main_section'); + + + add_settings_field('netx_base_category_id', 'NetX Folder', array($this, 'netxCategory'), __FILE__, 'main_section'); + add_settings_field('netx_base_category_path', '', array($this, 'netxCategoryPath'), __FILE__, 'main_section'); + + add_settings_field('netx_access_token', 'NetX API Token', array($this, 'netxApiToken'), __FILE__, 'main_section'); + } + + /** + * Settings main section callback + */ + public function mainSection() { + echo '
'; + } + + /** + * Create NetX username field + */ + public function netxUsername() { + $options = get_option('netx_options'); + echo ''; + } + + /** + * Create NetX password field + */ + public function netxPassword() { + $options = get_option('netx_options'); + echo ''; + } + + public function netxApiToken() { + $options = get_option('netx_options'); + echo ''; + } + + /** + * Create NetX URI field + */ + public function netxURI() { + $options = get_option('netx_options'); + echo '

NetX Uri should not include protocol or any trailing path. The uri should only include domain name. Example: "subdomain.netx.net"

'; + echo ''; + } + + /** + * Create NetX URI Protocl field + */ + public function netxURIProtocal() { + $options = get_option('netx_options'); + echo '

Should the netx api be contacted over Https? (HTTPS is strongly recommended)

'; + echo ''; + } + + /** + * Create NetX Page Size field + */ + public function netxPageSize() { + $options = get_option('netx_options'); + echo ''; + } + + /** + * Create NetX Category dropdown + */ + public function netxCategory() { + $options = get_option('netx_options'); + echo ''; + } + + /** + * Create NetX Category Path hidden field + */ + public function netxCategoryPath(){ + $options = get_option('netx_options'); + echo '

Current Folder Path: ' . $options['netx_base_category_path'] . '

'; + echo ''; + } + + /** + * Validate options field input + */ + public function netxValidateOptions($input) { + try { + $tmpURI = preg_replace('/^https?:\/+/i', '', $input['netx_uri']); + $input['netx_uri'] = $tmpURI; + + if ($input['netx_base_category_id']) { + $input['netx_base_category_path'] = self::getCategoryPath($input['netx_base_category_id'], $input); + } + } catch (Exception $e) { + error_log("netxValidateOptions: " . $e->getMessage()); + } + + return $input; + } + + /** + * Settings page + */ + public function settingsPage() { ?> -
-

-

NetX Settings

- This is where you manage NetX settings for the media library -
- - -

- -

-
-
+
+

+

NetX Settings

+ This is where you manage NetX settings for the media library +
+ + +

+ +

+
+
getCategoryPathList(); - $trans = array_flip($catList); - $path = $trans[$newCatID]; - return $path; - } + } + /** + * Runs when netx_base_category_id option is changed, so that we + * can look up the path for that category and save it in options + * as well. + */ + public function getCategoryPath($newCatID, $options = null) { + $netx = new netxRestWrapper($options); + $catList = $netx->getCategoryPathList(); + $trans = array_flip($catList); + $path = $trans[$newCatID]; + return $path; + } } // Admin menu/options pages diff --git a/includes/netxRestWrapper.php b/includes/netxRestWrapper.php index bc1e0f1..1e0d126 100755 --- a/includes/netxRestWrapper.php +++ b/includes/netxRestWrapper.php @@ -19,167 +19,171 @@ * @package NetxWPPlugin */ class netxRestWrapper { - /** - * NetX API object - * - * @var netxNetx NetX API object - * @access private - */ - private $netx = null; - - /** - * Constructor - * - * @return netxRestWrapper - */ - public function __construct() { - $options = get_option('netx_options'); - $netxUsername = $options['netx_username']; - $netxPassword = $options['netx_password']; - $netxURI = $options['netx_uri']; - $this->netx = new netxNetx($netxUsername, $netxPassword, $netxURI, true); - } - - /** - * Get netx API object - */ - public function getNetx(){ - return $this->netx; - } - - /** - * Get list of categories from server - * - * @return array category list - */ - public function getCategoryPathList() { - $list = $this->netx->getCategoryPathList(); - return $list; - } - - /** - * Import a file into DAM - * - * @return string full path to file - */ - public function importFile($filePath) { - $options = get_option('netx_options'); - $netxCategory = $options['netx_base_category_path']; - $config = netxConfig::getInstance(); - $config->setDeleteAfterImport(false); - $asset = $this->netx->fileImport($filePath, $netxCategory); - return $asset; - } - - public function netxThumbUrl($assetID) { - $url = NETX_PLUGIN_URL . 'proxy.php?aid=' . $assetID . '&type=t'; - return $url; - } - - public function netxPreviewUrl($assetID) { - $url = NETX_PLUGIN_URL . 'proxy.php?aid=' . $assetID . '&type=p'; - return $url; - } - - public function netxZoomUrl($assetID) { - $url = NETX_PLUGIN_URL . 'proxy.php?aid=' . $assetID . '&type=z'; - return $url; - } - - public function netxOriginalUrl($assetID) { - $url = NETX_PLUGIN_URL . 'proxy.php?aid=' . $assetID . '&type=o'; - return $url; - } - - public function getAssetData($assetID, $type, $isAttachment = false) { - $conn = $this->netx->getConnection(); - $proxy = new netxWordpressAssetLoader($conn); - - switch ($type) { - case 'o': - $proxy->getOriginal($assetID, $isAttachment); - break; - case 't': - $proxy->getThumbnail($assetID, $isAttachment); - break; - case 'p': - $proxy->getPreview($assetID, $isAttachment); - break; - case 'z': - $proxy->getZoom($assetID, $isAttachment); - break; - default: - $proxy->getView($assetID, $type, $isAttachment); - break; - } - - $ret = array(); - $ret['file'] = $proxy->getUploadFilePath(); - $ret['url'] = $proxy->getUploadUrlPath(); - - return $ret; - } - - public function doProxy($assetID, $type, $isAttachment = false) { - $conn = $this->netx->getConnection(); - - $options = get_option('netx_options'); - $cacheDir = $options['netx_cache_path'] . '/'; - - $cacheLifetime = $options['netx_cache_lifetime_seconds']; - - $proxy = new netxCachingProxy($conn, $cacheDir, $cacheLifetime); - - switch ($type) { - case 'o': - $proxy->getOriginal($assetID, $isAttachment); - break; - case 't': - $proxy->getThumbnail($assetID, $isAttachment); - break; - case 'p': - $proxy->getPreview($assetID, $isAttachment); - break; - case 'z': - $proxy->getZoom($assetID, $isAttachment); - break; - default: - $proxy->getView($assetID, $type, $isAttachment); - break; - } - } - - public function streamAsset($assetID, $type, $isAttachment = false) { - $conn = $this->netx->getConnection(); - - $proxy = new netxAssetProxy($conn); - - switch ($type) { - case 'o': - $proxy->getOriginal($assetID, $isAttachment); - break; - case 't': - $proxy->getThumbnail($assetID, $isAttachment); - break; - case 'p': - $proxy->getPreview($assetID, $isAttachment); - break; - case 'z': - $proxy->getZoom($assetID, $isAttachment); - break; - default: - $proxy->getView($assetID, $type, $isAttachment); - break; - } - - if($proxy->getContentLength == 0) { - $filename = dirname(__FILE__) . "/images/netx_nofile.png"; - $contents = file_get_contents($filename); - - echo($contents); - header("content-type: image/png"); - } - } + /** + * NetX API object + * + * @var netxNetx NetX API object + * @access private + */ + private $netx = null; + + /** + * Constructor + * + * @return netxRestWrapper + */ + public function __construct($options = null) { + if(!$options) { //if don't pass in options, lets pull from wordpress. + $options = get_option('netx_options'); + } + + $netxUsername = $options['netx_username']; + $netxPassword = $options['netx_password']; + $netxURI = $options['netx_uri']; + $useHttps = $options['netx_uri_protocal'] !== 'http'; + $this->netx = new netxNetx($netxUsername, $netxPassword, $netxURI, true, $useHttps); + } + + /** + * Get netx API object + */ + public function getNetx(){ + return $this->netx; + } + + /** + * Get list of categories from server + * + * @return array category list + */ + public function getCategoryPathList() { + $list = $this->netx->getCategoryPathList(); + return $list; + } + + /** + * Import a file into DAM + * + * @return string full path to file + */ + public function importFile($filePath) { + $options = get_option('netx_options'); + $netxCategory = $options['netx_base_category_path']; + $config = netxConfig::getInstance(); + $config->setDeleteAfterImport(false); + $asset = $this->netx->fileImport($filePath, $netxCategory); + return $asset; + } + + public function netxThumbUrl($assetID) { + $url = NETX_PLUGIN_URL . 'proxy.php?aid=' . $assetID . '&type=t'; + return $url; + } + + public function netxPreviewUrl($assetID) { + $url = NETX_PLUGIN_URL . 'proxy.php?aid=' . $assetID . '&type=p'; + return $url; + } + + public function netxZoomUrl($assetID) { + $url = NETX_PLUGIN_URL . 'proxy.php?aid=' . $assetID . '&type=z'; + return $url; + } + + public function netxOriginalUrl($assetID) { + $url = NETX_PLUGIN_URL . 'proxy.php?aid=' . $assetID . '&type=o'; + return $url; + } + + public function getAssetData($assetID, $type, $isAttachment = false) { + $conn = $this->netx->getConnection(); + $proxy = new netxWordpressAssetLoader($conn); + + switch ($type) { + case 'o': + $proxy->getOriginal($assetID, $isAttachment); + break; + case 't': + $proxy->getThumbnail($assetID, $isAttachment); + break; + case 'p': + $proxy->getPreview($assetID, $isAttachment); + break; + case 'z': + $proxy->getZoom($assetID, $isAttachment); + break; + default: + $proxy->getView($assetID, $type, $isAttachment); + break; + } + + $ret = array(); + $ret['file'] = $proxy->getUploadFilePath(); + $ret['url'] = $proxy->getUploadUrlPath(); + + return $ret; + } + + public function doProxy($assetID, $type, $isAttachment = false) { + $conn = $this->netx->getConnection(); + + $options = get_option('netx_options'); + $cacheDir = $options['netx_cache_path'] . '/'; + + $cacheLifetime = $options['netx_cache_lifetime_seconds']; + + $proxy = new netxCachingProxy($conn, $cacheDir, $cacheLifetime); + + switch ($type) { + case 'o': + $proxy->getOriginal($assetID, $isAttachment); + break; + case 't': + $proxy->getThumbnail($assetID, $isAttachment); + break; + case 'p': + $proxy->getPreview($assetID, $isAttachment); + break; + case 'z': + $proxy->getZoom($assetID, $isAttachment); + break; + default: + $proxy->getView($assetID, $type, $isAttachment); + break; + } + } + + public function streamAsset($assetID, $type, $isAttachment = false) { + $conn = $this->netx->getConnection(); + + $proxy = new netxAssetProxy($conn); + + switch ($type) { + case 'o': + $proxy->getOriginal($assetID, $isAttachment); + break; + case 't': + $proxy->getThumbnail($assetID, $isAttachment); + break; + case 'p': + $proxy->getPreview($assetID, $isAttachment); + break; + case 'z': + $proxy->getZoom($assetID, $isAttachment); + break; + default: + $proxy->getView($assetID, $type, $isAttachment); + break; + } + + if($proxy->getContentLength == 0) { + $filename = dirname(__FILE__) . "/images/netx_nofile.png"; + $contents = file_get_contents($filename); + + echo($contents); + header("content-type: image/png"); + } + } } ?> diff --git a/includes/netx_rest/netxAssetListProc.php b/includes/netx_rest/netxAssetListProc.php index cd21926..1aeae19 100755 --- a/includes/netx_rest/netxAssetListProc.php +++ b/includes/netx_rest/netxAssetListProc.php @@ -45,10 +45,10 @@ private function pageString($pageNum = 0) { * @param int $pageNum page number, or 0 for no paging * @return array array of netxAssetList objects */ - public function getAssetsByCategoryID($catID, $pageNum = 0) { + public function getAssetsByCategoryID($catID, $pageNum = 0) { $assetList = array(); $restCmd = '/category/id/' . $catID . $this->pageString($pageNum); - $xml = $this->doCommand($restCmd); + $xml = $this->doCommand($restCmd); $assetList = netxBeanFactory::parseAssetListXML($xml); return $assetList; } @@ -62,7 +62,7 @@ public function getAssetsByCategoryID($catID, $pageNum = 0) { */ public function getAssetsByCategoryPath($catPath, $pageNum = 0) { $assetList = array(); - $restCmd = '/category/path/' . $catPath . $this->pageString($pageNum); + $restCmd = '/category/path/' . $catPath . $this->pageString($pageNum); $xml = $this->doCommand($restCmd); $assetList = netxBeanFactory::parseAssetListXML($xml); return $assetList; diff --git a/includes/netx_rest/netxConfig.php b/includes/netx_rest/netxConfig.php index 276f99e..7b6d43b 100755 --- a/includes/netx_rest/netxConfig.php +++ b/includes/netx_rest/netxConfig.php @@ -319,8 +319,8 @@ public function setApiLoggingOn($isApiLoggingOn) { /** * Set API logging path - * * @param string $newApiLogPath API logging path + * */ public function setApiLogPath($newApiLogPath) { $this->apiLogPath = $newApiLogPath; diff --git a/includes/netx_rest/netxConnection.php b/includes/netx_rest/netxConnection.php index 01e374b..bae7f47 100755 --- a/includes/netx_rest/netxConnection.php +++ b/includes/netx_rest/netxConnection.php @@ -70,13 +70,13 @@ class netxConnection extends netxRestClient { * @param string $baseURI Base REST URI * @return netxConnection */ - protected function __construct($baseURI) { + protected function __construct($baseURI, $useHttps = true) { parent::__construct('login', $this); - $baseRestUri = 'http://' . $baseURI; + $baseRestUri = ($useHttps ? 'https://' : 'http://') . $baseURI; $this->restBaseURI = $baseRestUri; - $baseHref = 'http://' . $baseURI; + $baseHref = ($useHttps ? 'https://' : 'http://') . $baseURI; $this->restBaseHref = $baseHref; $this->http = netxHttp::getInstance(); @@ -143,8 +143,8 @@ public static function nullConnection($username, $password, $baseURI) { * @param string $baseURI base REST URI (i.e. 'poached.netx.net') * @return netxConnection */ - public static function ConnectBasicAuth($username, $password, $baseURI) { - $conn = new netxConnection($baseURI); + public static function ConnectBasicAuth($username, $password, $baseURI, $useHttps = true) { + $conn = new netxConnection($baseURI, $useHttps); $conn->doHttpBasicAuthConnect($username, $password); @@ -161,8 +161,8 @@ public static function ConnectBasicAuth($username, $password, $baseURI) { * @param string $baseURI base REST URI (i.e. 'poached.netx.net') * @return netxConnection */ - public static function ConnectLogin($username, $password, $baseURI) { - $conn = new netxConnection($baseURI); + public static function ConnectLogin($username, $password, $baseURI, $useHttps = true) { + $conn = new netxConnection($baseURI, $useHttps); $conn->doLoginConnect($username, $password); diff --git a/includes/netx_rest/netxNetX.php b/includes/netx_rest/netxNetX.php index 0318ee1..eec62c8 100755 --- a/includes/netx_rest/netxNetX.php +++ b/includes/netx_rest/netxNetX.php @@ -39,11 +39,11 @@ class netxNetX { * @param boolean $useHttpBasicAuth true to use HTTP basic auth, false to use the REST login command to log in * @return netxNetX */ - public function __construct($username, $password, $serverURL, $useHttpBasicAuth = false) { + public function __construct($username, $password, $serverURL, $useHttpBasicAuth = false, $useHttps = true) { if ($useHttpBasicAuth) { - $this->connection = netxConnection::ConnectBasicAuth($username, $password, $serverURL); + $this->connection = netxConnection::ConnectBasicAuth($username, $password, $serverURL, $useHttps); } else { - $this->connection = netxConnection::ConnectLogin($username, $password, $serverURL); + $this->connection = netxConnection::ConnectLogin($username, $password, $serverURL, $useHttps); } } @@ -151,7 +151,7 @@ public function createTopLevelCategory($catName) { * @param int $pageNum page number, or 0 for no paging * @return array array of netxAssetList objects */ - public function getAssetsByCategoryID($catID, $pageNum = 0) { + public function getAssetsByCategoryID($catID, $pageNum = 0) { $assetListProc = new netxAssetListProc($this->connection); $assetList = $assetListProc->getAssetsByCategoryID($catID, $pageNum); return $assetList; diff --git a/includes/netx_rest/netxRestClient.php b/includes/netx_rest/netxRestClient.php index 173aaf9..1993c27 100755 --- a/includes/netx_rest/netxRestClient.php +++ b/includes/netx_rest/netxRestClient.php @@ -216,7 +216,8 @@ protected function doCommand($cmdString = '') { $callString = $this->makeRestCallString($cmdString); $this->log($callString); $uri = $this->makeURI($callString); - $xml = $this->connection->getHttp()->get($uri); + $xml = $this->connection->getHttp()->get($uri); + $this->log($xml); return $xml; } diff --git a/netx-media-manager.php b/netx-media-manager.php index d25dbc2..9839f2c 100755 --- a/netx-media-manager.php +++ b/netx-media-manager.php @@ -6,16 +6,18 @@ Plugin Name: NetX Media Manager Plugin URI: http://netx.net Description: Sync the Wordpress media library with NetX -Version: 2.1.1 +Version: 2.1.5 Author: NetX, PNDLM Author URI: http://netx.net License: */ -define('WPNETX_VERSION', '2.1.1'); +define('WPNETX_VERSION', '2.1.5'); define('WPNETX_PLUGIN_URL', plugin_dir_url(__FILE__)); define('NETX_PLUGIN_URL', WP_PLUGIN_URL.'/'.str_replace(basename( __FILE__),"",plugin_basename(__FILE__))); +//error_reporting(-1); +//ini_set('display_errors', 'On'); // Make sure we don't expose any info if called directly if (!function_exists('add_action')) { @@ -51,6 +53,7 @@ static function unregisterSettings() { unregister_setting('netx_options', 'netx_username'); unregister_setting('netx_options', 'netx_password'); unregister_setting('netx_options', 'netx_uri'); + unregister_setting('netx_options', 'netx_uri_protocal'); unregister_setting('netx_options', 'netx_base_category_id'); unregister_setting('netx_options', 'netx_base_category_path'); unregister_setting('netx_options', 'netx_access_token'); diff --git a/netx-media.php b/netx-media.php index a0d1acd..651d701 100755 --- a/netx-media.php +++ b/netx-media.php @@ -4,427 +4,434 @@ */ class NetXMediaManager { - const NETX_ASSET_ID_KEY = '_netx_asset_id'; - const NETX_ASSET_VIEWNAME_KEY = '_netx_asset_view_name'; - const NETX_ENFORCE_DOWNLOAD_KEY = '_netx_enforce_download'; - const NETX_FULL_PROXY_URL = '_netx_proxy_url'; - const NETX_FILE_SIZE = '_netx_file_size'; - const NETX_FILE_NAME = '_netx_file_name'; - const NETX_FILE_TYPE = '_netx_file_type'; - - public static function init() { - new NetXMediaManager(); - } - - public function __construct() { - //include our client side code - add_action( 'admin_enqueue_scripts', array($this, 'setup_admin_client_scripts')); - - //add ajax endpoints - add_action( 'wp_ajax_netx_load', array($this, 'ajax_load')); - add_action( 'wp_ajax_netx_get_image_select_form', array($this, 'get_image_select_form')); - add_action( 'wp_ajax_netx_import_from_netx', array($this, 'import_from_netx')); - - //add our custom html filter. - add_filter('image_send_to_editor', array($this, 'wrap_image_html'), 10, 8); - } - - public function setup_admin_client_scripts() { - wp_enqueue_style('netx_style', plugins_url('scripts/style.css', __FILE__ )); - wp_enqueue_script('netx_script', plugins_url('scripts/script.js', __FILE__ )); - wp_localize_script('netx_script', 'netxScript', array( - 'ajaxLoaderUrl' => plugins_url('scripts/ajax-loader.gif', __FILE__ ), - 'debug' => "0" - )); - } - - //Ajax Callback. Returns the netx form loader as html - public function ajax_load() { - $wrapper = new netxRestWrapper(); - $netx = $wrapper->getNetx(); - $cats = $netx->getCategoryTree(); - $options = get_option('netx_options'); - ?> -
/wp-admin/media-upload.php?post_id=&tab=netx" method="post" enctype="multipart/form-data"> -
- -
- -
-
-
-
-
-
-
-
- -
-
- getNetx(); - - $assetID = $_POST['asset_id']; - $assetView = $_POST['asset_view']; - - $attachment_id = $this->AddMediaToLibraryFromNetx($wrapper, $netx, $assetID, $assetView, $_POST['enforce_download'] == 'true')['attachment_id']; - - echo($attachment_id); - - wp_die(); - } - - //Ajax Callback. Returns the netx form as html. - public function get_image_select_form() { - $options = get_option('netx_options'); - $wrapper = new netxRestWrapper(); - $netx = $wrapper->getNetx(); - - $supportedFileTypes = array( - //images - "jpg", - "jpeg", - "png", - "gif", - "ico", - //documents - "pdf", - "doc", - "ppt", - "odt", - "xls", - "psd", - //audio - "mp3", - "m4a", - "ogg", - "wav", - //video - "mp4", - "mov", - "wmv", - "avi", - "mpg", - "ogv", - "3gp", - "3g2" - ); - - $pagingSize = intval($options['netx_paging_size']); - if($pagingSize == 0) { - $pagingSize = 200; - } - - //get current category id - $currentCatId = (trim($_POST['catId']) != '') ? $_POST['catId'] : $options['netx_base_category_id']; - - //get assets in current category - $catAssets = $netx->getAssetsByCategoryID($currentCatId); - $catAssetsNum = 0; - foreach($catAssets as $key=>$asset){ - $catAssetsNum++; - } - - $postID = intval($_REQUEST['post_id']); -// $proxyURL = dirname(__FILE__) . '/proxy.php' - ?> - - getAssetsByCategoryID($currentCatId,$paged); - if($catAssetsPages > 1){ - - echo '
'; - if($paged != 1){ - echo ''; - } - for($i=($catAssetsPages <= 5) ? 1 : $paged;$i<=$paged+4;$i++){ - if($paged == $i){ - echo ''.$i.''; - } else { - echo ''.$i.''; - } - } - if($paged != $catAssetsPages){ - echo ''; - } - echo '
'; - } - - $catAssetsPageFiltered = array(); - - if(count($catAssetsPage) > 0) { - foreach($catAssetsPage as $key=>$asset){ - $assetID = $asset->getAssetID(); - $ast = $netx->getAsset($assetID); - - $fileIsSupported = false; - foreach($supportedFileTypes as $supportedFileType) { - $filename = $ast->getFile(); - - $substrlength = strlen('.' . $supportedFileType); - if(substr($filename, - $substrlength) === ('.' . $supportedFileType)) { - $fileIsSupported = true; - break; - } - } - - if($fileIsSupported) { - $catAssetsPageFiltered[$key] = $asset; - } - } - } - - if(count($catAssetsPageFiltered) > 0) { - foreach($catAssetsPageFiltered as $key=>$asset){ - $assetID = $asset->getAssetID(); - $ast = $netx->getAsset($assetID); - ?> -
- <?php echo $asset->getLabel1(); ?> - Show - -
getLabel1(); ?>
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
- -

No Files Found

- '; - } - - return $html; - } - - private function tree_node_view($node, $selectedId) { - $treeNodeClass = 'netx-tree-node collapsed'; - $treeNodeLinkClass = 'netx-tree-node-link'; - $treeToggleIconClass = 'dashicons dashicons-plus'; - - if($node['categoryId'] == $selectedId) { - $treeNodeClass = 'netx-tree-node'; - $treeNodeLinkClass = 'netx-tree-node-link selected'; - $treeToggleIconClass = 'dashicons dashicons-minus'; - } - ?> - -
  • - 0) { ?> - - - - - - - - -
  • - - $child) { - if(is_array($child['children']) && (count($child['children']) > 0)) { - $recursiveResult = $this->getRootCategoryFromTree($child['children'], $rootId); - - if($recursiveResult != null) { - return $recursiveResult; - } - } - } - } - } - - return null; - } - - private function AddMediaToLibraryFromNetx($wrapper, $netx, $assetID, $sizeStr, $directLink = false) - { - $ast = $netx->getAsset($assetID); - - $filename = $ast->getFile(); - $filesize = $ast->getFilesize(); - $filetype = $ast->getFiletypelabel(); - - if($directLink) { - $file = $wrapper->getAssetData($assetID, 't'); - } else { - if ($sizeStr === 'full') { - $file = $wrapper->getAssetData($assetID, 'o'); - } else if ($sizeStr === 'medium') { - $file = $wrapper->getAssetData($assetID, 'p'); - } else if ($sizeStr === 'thumbnail') { - $file = $wrapper->getAssetData($assetID, 't'); - } else { - $file = $wrapper->getAssetData($assetID, $sizeStr); - } - } - - if (!$file['error']) { - $wp_filetype = wp_check_filetype($filename, null); - $attachment = array( - 'post_mime_type' => $wp_filetype['type'], - 'post_title' => preg_replace('/\.[^.]+$/', '', $filename), - 'post_content' => '', - 'post_status' => 'inherit' - ); - $attachment_id = wp_insert_attachment($attachment, $file['file']); - if (!is_wp_error($attachment_id)) { - require_once(ABSPATH . "wp-admin" . '/includes/image.php'); - $attachment_data = wp_generate_attachment_metadata($attachment_id, $file['file']); - wp_update_attachment_metadata($attachment_id, $attachment_data); - - //set custom netx metadata - add_post_meta($attachment_id, NetXMediaManager::NETX_ASSET_ID_KEY, $assetID, true); - add_post_meta($attachment_id, NetXMediaManager::NETX_ASSET_VIEWNAME_KEY, $sizeStr, true); - - $downloadUrl = plugins_url('endpoints/asset.php', __FILE__); - - if ($sizeStr == 'thumbnail'){ - $sizeStr2 = 'thumb'; - } - else if ($sizeStr == 'medium'){ - $sizeStr2 = 'preview'; - } - else if ($sizeStr == 'full'){ - $sizeStr2 = 'original'; - } else { - $sizeStr2 = $sizeStr; - } - - $downloadUrl .= '?assetID=' . $assetID; - $downloadUrl .= '&sizeStr=' . $sizeStr2; - $downloadUrl .= '&filename=' . $filename; - - add_post_meta($attachment_id, NetXMediaManager::NETX_FULL_PROXY_URL, $downloadUrl, true); - add_post_meta($attachment_id, NetXMediaManager::NETX_FILE_SIZE, $filesize, true); - add_post_meta($attachment_id, NetXMediaManager::NETX_FILE_NAME, $filename, true); - add_post_meta($attachment_id, NetXMediaManager::NETX_FILE_TYPE, $filetype, true); - - if($directLink) { - add_post_meta($attachment_id, NetXMediaManager::NETX_ENFORCE_DOWNLOAD_KEY, "true", true); - } else { - add_post_meta($attachment_id, NetXMediaManager::NETX_ENFORCE_DOWNLOAD_KEY, "false", true); - } - } - } - - return array( - "file" => $file, - "attachment_id" => $attachment_id - ); - } + const NETX_ASSET_ID_KEY = '_netx_asset_id'; + const NETX_ASSET_VIEWNAME_KEY = '_netx_asset_view_name'; + const NETX_ENFORCE_DOWNLOAD_KEY = '_netx_enforce_download'; + const NETX_FULL_PROXY_URL = '_netx_proxy_url'; + const NETX_FILE_SIZE = '_netx_file_size'; + const NETX_FILE_NAME = '_netx_file_name'; + const NETX_FILE_TYPE = '_netx_file_type'; + + public static function init() { + new NetXMediaManager(); + } + + public function __construct() { + //include our client side code + add_action( 'admin_enqueue_scripts', array($this, 'setup_admin_client_scripts')); + + //add ajax endpoints + add_action( 'wp_ajax_netx_load', array($this, 'ajax_load')); + add_action( 'wp_ajax_netx_get_image_select_form', array($this, 'get_image_select_form')); + add_action( 'wp_ajax_netx_import_from_netx', array($this, 'import_from_netx')); + + //add our custom html filter. + add_filter('image_send_to_editor', array($this, 'wrap_image_html'), 10, 8); + + if (defined('WP_DEBUG_LOG') && true === WP_DEBUG_LOG) { + $config = netxConfig::getInstance(); + $config->setHttpLoggingOn(true); + $config->setApiLoggingOn(true); + } + } + + public function setup_admin_client_scripts() { + wp_enqueue_style('netx_style', plugins_url('scripts/style.css', __FILE__ )); + wp_enqueue_script('netx_script', plugins_url('scripts/script.js', __FILE__ )); + wp_localize_script('netx_script', 'netxScript', array( + 'ajaxLoaderUrl' => plugins_url('scripts/ajax-loader.gif', __FILE__ ), + 'debug' => "0" + )); + } + + //Ajax Callback. Returns the netx form loader as html + public function ajax_load() { + $wrapper = new netxRestWrapper(); + $netx = $wrapper->getNetx(); + $cats = $netx->getCategoryTree(); + $options = get_option('netx_options'); + ?> +
    /wp-admin/media-upload.php?post_id=&tab=netx" method="post" enctype="multipart/form-data"> +
    + +
    + +
    +
    +
    +
    +
    +
    +
    +
    + +
    +
    + getNetx(); + + $assetID = $_POST['asset_id']; + $assetView = $_POST['asset_view']; + + $attachment_id = $this->AddMediaToLibraryFromNetx($wrapper, $netx, $assetID, $assetView, $_POST['enforce_download'] == 'true')['attachment_id']; + + echo($attachment_id); + + wp_die(); + } + + //Ajax Callback. Returns the netx form as html. + public function get_image_select_form() { + $options = get_option('netx_options'); + $wrapper = new netxRestWrapper(); + $netx = $wrapper->getNetx(); + + $supportedFileTypes = array( + //images + "jpg", + "jpeg", + "png", + "gif", + "ico", + //documents + "pdf", + "doc", + "ppt", + "odt", + "xls", + "psd", + //audio + "mp3", + "m4a", + "ogg", + "wav", + //video + "mp4", + "mov", + "wmv", + "avi", + "mpg", + "ogv", + "3gp", + "3g2" + ); + + $pagingSize = intval($options['netx_paging_size']); + if($pagingSize == 0) { + $pagingSize = 200; + } + + //get current category id + $currentCatId = (trim($_POST['catId']) != '') ? $_POST['catId'] : $options['netx_base_category_id']; + + //get assets in current category + $catAssets = $netx->getAssetsByCategoryID($currentCatId); + $catAssetsNum = 0; + foreach($catAssets as $key=>$asset){ + $catAssetsNum++; + } + + $postID = intval($_REQUEST['post_id']); +// $proxyURL = dirname(__FILE__) . '/proxy.php' + ?> + + getAssetsByCategoryID($currentCatId, $paged); + if($catAssetsPages > 1){ + echo '
    '; + if($paged != 1){ + echo ''; + } + for($i = $startPage; $i <= $endPage; $i++){ + if($paged == $i){ + echo ''.$i.''; + } else { + echo ''.$i.''; + } + } + if($paged != $catAssetsPages){ + echo ''; + } + echo '
    '; + } + + $catAssetsPageFiltered = array(); + + if(count($catAssetsPage) > 0) { + foreach($catAssetsPage as $key=>$asset){ + $assetID = $asset->getAssetID(); + $ast = $netx->getAsset($assetID); + + $fileIsSupported = false; + foreach($supportedFileTypes as $supportedFileType) { + $filename = $ast->getFile(); + + $substrlength = strlen('.' . $supportedFileType); + if(substr($filename, - $substrlength) === ('.' . $supportedFileType)) { + $fileIsSupported = true; + break; + } + } + + if($fileIsSupported) { + $catAssetsPageFiltered[$key] = $asset; + } + } + } + + if(count($catAssetsPageFiltered) > 0) { + foreach($catAssetsPageFiltered as $key=>$asset){ + $assetID = $asset->getAssetID(); + $ast = $netx->getAsset($assetID); + ?> +
    + <?php echo $asset->getLabel1(); ?> + Show + +
    getLabel1(); ?>
    + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    + +

    No Files Found

    + '; + } + + return $html; + } + + private function tree_node_view($node, $selectedId) { + $treeNodeClass = 'netx-tree-node collapsed'; + $treeNodeLinkClass = 'netx-tree-node-link'; + $treeToggleIconClass = 'dashicons dashicons-plus'; + + if($node['categoryId'] == $selectedId) { + $treeNodeClass = 'netx-tree-node'; + $treeNodeLinkClass = 'netx-tree-node-link selected'; + $treeToggleIconClass = 'dashicons dashicons-minus'; + } + ?> + +
  • + 0) { ?> + + + + + + + + +
  • + + $child) { + if(is_array($child['children']) && (count($child['children']) > 0)) { + $recursiveResult = $this->getRootCategoryFromTree($child['children'], $rootId); + + if($recursiveResult != null) { + return $recursiveResult; + } + } + } + } + } + + return null; + } + + private function AddMediaToLibraryFromNetx($wrapper, $netx, $assetID, $sizeStr, $directLink = false) + { + $ast = $netx->getAsset($assetID); + + $filename = $ast->getFile(); + $filesize = $ast->getFilesize(); + $filetype = $ast->getFiletypelabel(); + + if($directLink) { + $file = $wrapper->getAssetData($assetID, 't'); + } else { + if ($sizeStr === 'full') { + $file = $wrapper->getAssetData($assetID, 'o'); + } else if ($sizeStr === 'medium') { + $file = $wrapper->getAssetData($assetID, 'p'); + } else if ($sizeStr === 'thumbnail') { + $file = $wrapper->getAssetData($assetID, 't'); + } else { + $file = $wrapper->getAssetData($assetID, $sizeStr); + } + } + + if (!$file['error']) { + $wp_filetype = wp_check_filetype($filename, null); + $attachment = array( + 'post_mime_type' => $wp_filetype['type'], + 'post_title' => preg_replace('/\.[^.]+$/', '', $filename), + 'post_content' => '', + 'post_status' => 'inherit' + ); + $attachment_id = wp_insert_attachment($attachment, $file['file']); + if (!is_wp_error($attachment_id)) { + require_once(ABSPATH . "wp-admin" . '/includes/image.php'); + $attachment_data = wp_generate_attachment_metadata($attachment_id, $file['file']); + wp_update_attachment_metadata($attachment_id, $attachment_data); + + //set custom netx metadata + add_post_meta($attachment_id, NetXMediaManager::NETX_ASSET_ID_KEY, $assetID, true); + add_post_meta($attachment_id, NetXMediaManager::NETX_ASSET_VIEWNAME_KEY, $sizeStr, true); + + $downloadUrl = plugins_url('endpoints/asset.php', __FILE__); + + if ($sizeStr == 'thumbnail'){ + $sizeStr2 = 'thumb'; + } + else if ($sizeStr == 'medium'){ + $sizeStr2 = 'preview'; + } + else if ($sizeStr == 'full'){ + $sizeStr2 = 'original'; + } else { + $sizeStr2 = $sizeStr; + } + + $downloadUrl .= '?assetID=' . $assetID; + $downloadUrl .= '&sizeStr=' . $sizeStr2; + $downloadUrl .= '&filename=' . $filename; + + add_post_meta($attachment_id, NetXMediaManager::NETX_FULL_PROXY_URL, $downloadUrl, true); + add_post_meta($attachment_id, NetXMediaManager::NETX_FILE_SIZE, $filesize, true); + add_post_meta($attachment_id, NetXMediaManager::NETX_FILE_NAME, $filename, true); + add_post_meta($attachment_id, NetXMediaManager::NETX_FILE_TYPE, $filetype, true); + + if($directLink) { + add_post_meta($attachment_id, NetXMediaManager::NETX_ENFORCE_DOWNLOAD_KEY, "true", true); + } else { + add_post_meta($attachment_id, NetXMediaManager::NETX_ENFORCE_DOWNLOAD_KEY, "false", true); + } + } + } + + return array( + "file" => $file, + "attachment_id" => $attachment_id + ); + } } add_action('init', array('NetXMediaManager', 'init')); -?> \ No newline at end of file +?> diff --git a/proxy.php b/proxy.php index 4a85420..5ca13d4 100755 --- a/proxy.php +++ b/proxy.php @@ -3,9 +3,6 @@ * @package NetxWPPlugin */ -error_reporting(E_ALL); -ini_set('display_errors','On'); - require_once(dirname(__FILE__) . '/../../../wp-blog-header.php'); require_once(dirname(__FILE__) . '/includes/netxRestWrapper.php'); diff --git a/scripts/script.js b/scripts/script.js index eadc026..304a182 100755 --- a/scripts/script.js +++ b/scripts/script.js @@ -11,19 +11,19 @@ $('.netx-upload-loading-area').html('
    '); //set the new catId link as selected - $('a.netx-tree-node-link.selected').removeClass('selected'); - $('a.netx-tree-node-link').filter(function(x) { return $(this).data("catId") == catId }).addClass('selected'); + $('a.netx-tree-node-link.selected').removeClass('selected'); + $('a.netx-tree-node-link').filter(function(x) { return $(this).data("catId") == catId }).addClass('selected'); //load and display the data. - lastJqueryAjaxRequest = $.post(ajaxurl, { action: 'netx_get_image_select_form', catId: catId }, function(resp) { - $('.netx-upload-loading-area').html(resp); + lastJqueryAjaxRequest = $.post(ajaxurl, { action: 'netx_get_image_select_form', catId: catId, pageId: pageId }, function(resp) { + $('.netx-upload-loading-area').html(resp); //hide the all of the element with class msg_body - $(".slidetoggle").hide(); + $(".slidetoggle").hide(); //toggle the componenet with class msg_body - $(".toggle").click(function(){ - $(this).parent().children('a.toggle').toggle(); - $(this).siblings(".slidetoggle").toggle(); + $(".toggle").click(function(){ + $(this).parent().children('a.toggle').toggle(); + $(this).siblings(".slidetoggle").toggle(); }); - $('a.page-numbers').click(function () { + $('a.page-numbers').click(function () { updateNetxMediaSelectorForm(catId, $(this).data("pageNum")); }); }); @@ -38,26 +38,26 @@ } }; - var initNetxForm = function () { - $.post(ajaxurl, {action: 'netx_load'}, function (resp) { - $('.netx-form-target').html(resp); + var initNetxForm = function () { + $.post(ajaxurl, {action: 'netx_load'}, function (resp) { + $('.netx-form-target').html(resp); - $('.netx-tree-node-toggle').click(function () { - $(this).parent('.netx-tree-node').toggleClass("collapsed"); + $('.netx-tree-node-toggle').click(function () { + $(this).parent('.netx-tree-node').toggleClass("collapsed"); - $(this).children('span.dashicons').toggleClass('dashicons-plus'); - $(this).children('span.dashicons').toggleClass('dashicons-minus'); - }); + $(this).children('span.dashicons').toggleClass('dashicons-plus'); + $(this).children('span.dashicons').toggleClass('dashicons-minus'); + }); - $('.netx-tree-node-link').click(function () { - if (!$(this).hasClass("selected")) { - updateNetxMediaSelectorForm($(this).data("catId")); - } else { - $(this).siblings('a.netx-tree-node-toggle').click(); - } - }); + $('.netx-tree-node-link').click(function () { + if (!$(this).hasClass("selected")) { + updateNetxMediaSelectorForm($(this).data("catId")); + } else { + $(this).siblings('a.netx-tree-node-toggle').click(); + } + }); - $('.netx-form-target').on('click', '.netx-add-item-submit', function () { + $('.netx-form-target').on('click', '.netx-add-item-submit', function () { var refreshMediaLib = function(callback) { // get wp outside iframe @@ -69,54 +69,54 @@ // refresh if( pwp.media.frame.content.get() && - pwp.media.frame.content.get().collection && - pwp.media.frame.content.get().collection.props + pwp.media.frame.content.get().collection && + pwp.media.frame.content.get().collection.props ) { - pwp.media.frame.content.get().collection.props.set({ignore: (+new Date())}); - pwp.media.frame.content.get().options.selection.reset(); + pwp.media.frame.content.get().collection.props.set({ignore: (+new Date())}); + pwp.media.frame.content.get().options.selection.reset(); } else if(pwp.media.frame.library && - pwp.media.frame.library.props + pwp.media.frame.library.props ) { - pwp.media.frame.library.props.set({ignore: (+new Date())}); + pwp.media.frame.library.props.set({ignore: (+new Date())}); } else { - var editor = pwp.media.editor.get(parent.wpActiveEditor); - - if(editor && editor.views && editor.views._views) { - var views = editor.views._views; - - if(views['.media-frame-content'] && views['.media-frame-content'].length) { - for(var i = 0; i < views['.media-frame-content'].length; i++) { - var view = views['.media-frame-content'][i]; - - if(view.views && view.views && view.views._views) { - var cView = view.views._views; - if(cView[""] && cView[""].length > 1) { - var target = cView[""][1]; - - if(target && target.collection && target.collection.props) { - target.collection.props.set({ignore:(+(new Date()))}); - } - } - } - } - } - } + var editor = pwp.media.editor.get(parent.wpActiveEditor); + + if(editor && editor.views && editor.views._views) { + var views = editor.views._views; + + if(views['.media-frame-content'] && views['.media-frame-content'].length) { + for(var i = 0; i < views['.media-frame-content'].length; i++) { + var view = views['.media-frame-content'][i]; + + if(view.views && view.views && view.views._views) { + var cView = view.views._views; + if(cView[""] && cView[""].length > 1) { + var target = cView[""][1]; + + if(target && target.collection && target.collection.props) { + target.collection.props.set({ignore:(+(new Date()))}); + } + } + } + } + } + } } callback(); }; var netxAssetId = $(this).data("assetId"); - var view = $('input[name="asset[' + netxAssetId + '][image-size]"]:checked').val(); - var enforceDownload = $('input[name="asset[' + netxAssetId + '][enable-download]"]:checked').val() == 'enable-download' ? 'true' : 'false'; + var view = $('input[name="asset[' + netxAssetId + '][image-size]"]:checked').val(); + var enforceDownload = $('input[name="asset[' + netxAssetId + '][enable-download]"]:checked').val() == 'enable-download' ? 'true' : 'false'; - //start ajax notification - $('body').append( - "
    " - ); + //start ajax notification + $('body').append( + "
    " + ); $.post(ajaxurl, { action: 'netx_import_from_netx', asset_id: netxAssetId, asset_view: view, enforce_download: enforceDownload }, function(resp) { refreshMediaLib(function() { - $('.netx-ajax').remove(); + $('.netx-ajax').remove(); // get wp outside iframe var pacf = parent.acf; @@ -126,107 +126,107 @@ var frame = null; if(pacf && pacf.media.frame && useAcf && (typeof pacf.media.frame.state === "function")) { - frame = pacf.media.frame; + frame = pacf.media.frame; } else { - frame = pwp.media.frame; - } + frame = pwp.media.frame; + } var selection = frame.state().get('selection'); - selection.add(pwp.media.attachment(resp)); + selection.add(pwp.media.attachment(resp)); $('.media-toolbar').show(); - $('.media-button-select').click(); - - //$('#netx-form').submit(); - - var setImageSrc = function(url) { - if($('.acf-image-value[value=' + resp + ']').length) { - $('.acf-image-value[value=' + resp + ']') - .siblings('.has-image') - .children('.acf-image-image') - .attr('src', url); - } else { - if(console && console.log && netxScript.debug == "1") { - console.log( - 'Could not find ' + - '.acf-image-value[value=' + resp + ']' + - ' to set preview image' - ); - } - } - - if($('.acf-image-uploader .acf-hidden input[type="hidden"][value="' + resp + '"]').length) { - $('.acf-image-uploader .acf-hidden input[type="hidden"][value="' + resp + '"]') - .closest('.acf-image-uploader') - .find('img') - .attr('src', url); - } else { - if(console && console.log && netxScript.debug == "1") { - console.log( - 'Could not find ' + - '.acf-image-uploader .acf-hidden input[type="hidden"][value="' + resp + '"]' + - ' to set preview image' - ); - } - } - }; - - var thumbnailUpdate = function () { - //if we're using acf let's update the image thumb - var attachment = pwp.media.attachment(resp); - if(attachment.attributes.url) { - setImageSrc(attachment.attributes.url); - } else { - setTimeout(thumbnailUpdate, 100); - } - }; - - setImageSrc(netxScript.ajaxLoaderUrl); - setTimeout(thumbnailUpdate, 100); - }); - }); - }); - - updateNetxMediaSelectorForm(); - }); - }; + $('.media-button-select').click(); + + //$('#netx-form').submit(); + + var setImageSrc = function(url) { + if($('.acf-image-value[value=' + resp + ']').length) { + $('.acf-image-value[value=' + resp + ']') + .siblings('.has-image') + .children('.acf-image-image') + .attr('src', url); + } else { + if(console && console.log && netxScript.debug == "1") { + console.log( + 'Could not find ' + + '.acf-image-value[value=' + resp + ']' + + ' to set preview image' + ); + } + } + + if($('.acf-image-uploader .acf-hidden input[type="hidden"][value="' + resp + '"]').length) { + $('.acf-image-uploader .acf-hidden input[type="hidden"][value="' + resp + '"]') + .closest('.acf-image-uploader') + .find('img') + .attr('src', url); + } else { + if(console && console.log && netxScript.debug == "1") { + console.log( + 'Could not find ' + + '.acf-image-uploader .acf-hidden input[type="hidden"][value="' + resp + '"]' + + ' to set preview image' + ); + } + } + }; + + var thumbnailUpdate = function () { + //if we're using acf let's update the image thumb + var attachment = pwp.media.attachment(resp); + if(attachment.attributes.url) { + setImageSrc(attachment.attributes.url); + } else { + setTimeout(thumbnailUpdate, 100); + } + }; + + setImageSrc(netxScript.ajaxLoaderUrl); + setTimeout(thumbnailUpdate, 100); + }); + }); + }); + + updateNetxMediaSelectorForm(); + }); + }; var setupNetxSelector = function () { - $('.media-frame-router .media-router').each(function() { - if (!$(this).children('.netx-media-selector-button').length) { - $(this) - .append( - $("", {"href": "#", "class": "netx-media-selector-button media-menu-item", "text": "NetX"}) - ); - - $('.media-menu-item').click(function () { - $(".media-menu-item").removeClass('active'); - $(this).addClass('active'); - - if ($(this).hasClass('netx-media-selector-button')) { - $(".media-frame-content").html( - "
    " - ); - - initNetxForm(); - - $('.media-toolbar').hide(); - } else { - $('.media-toolbar').show(); - } - }); - } - }); - - setTimeout(setupNetxSelector, 100); + $('.media-frame-router .media-router').each(function() { + if (!$(this).children('.netx-media-selector-button').length) { + $(this) + .append( + $("
    ", {"href": "#", "class": "netx-media-selector-button media-menu-item", "text": "NetX"}) + ); + + $('.media-menu-item').click(function () { + $(".media-menu-item").removeClass('active'); + $(this).addClass('active'); + + if ($(this).hasClass('netx-media-selector-button')) { + $(".media-frame-content").html( + "
    " + ); + + initNetxForm(); + + $('.media-toolbar').hide(); + } else { + $('.media-toolbar').show(); + } + }); + } + }); + + setTimeout(setupNetxSelector, 100); }; - $(function() { + $(function() { if($('.netx-form-target').length) { - initNetxForm(); + initNetxForm(); } - setupNetxSelector(); + setupNetxSelector(); }); -})(jQuery); \ No newline at end of file +})(jQuery);