-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwp-WCH-assetpicker.php
223 lines (184 loc) · 6.71 KB
/
wp-WCH-assetpicker.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
/*
/*******************************************************************************
* Copyright Acoustic Corp. 2017
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************
Plugin Name: Acoustic Content Asset Palette
Description: This plugin provides an installable plugin for WordPress which integrates with Acoustic Content.
Version: 1.0.12
Author: Acoustic Content
Author URI: https://acoustic.com/products/content/
Plugin URI: https://github.com/acoustic-content-samples/sample-wp-wch-asset-palette
*/
/**
* Define some useful constants
**/
define('WCH_ASSETPICKER_VERSION', '1.0');
define('WCH_ASSETPICKER_DIR', plugin_dir_path(__FILE__));
define('WCH_ASSETPICKER_URL', plugin_dir_url(__FILE__));
/**
* Load files
*
**/
function wch_assetpicker_load(){
if(is_admin()) //load admin files only in admin
require_once(WCH_ASSETPICKER_DIR.'includes/admin.php');
require_once(WCH_ASSETPICKER_DIR.'includes/core.php');
}
wp_enqueue_script('jquery');
wch_assetpicker_load();
function getApiUrl() {
return esc_attr( get_option('apiUrl') );
}
function getHostRendering($apiurl) {
$temp2= substr($apiurl,strpos($apiurl,"my"));
$temp3= substr($temp2,0,4+strpos($temp2,".com"));
return esc_attr( $temp3 );
}
function getTenantId($apiurl) {
$temp2= substr($apiurl,5+strpos($apiurl,"/api/"));
return esc_attr( $temp2 );
}
function custom_meta_box_markup($object)
{
wp_nonce_field(basename(__FILE__), "meta-box-nonce");
$apiUrlPHP = getApiUrl();
$hostRendering = getHostRendering($apiUrlPHP);
$tenantId = getTenantId($apiUrlPHP);
?>
<script type="text/javascript">
var apiUrl="<?php echo "$apiUrlPHP" ?>";
var typeOptions="";
if ("<?php echo get_option('type-image') ?>"==="1") {
if (typeOptions==="" ) {
typeOptions = typeOptions+"assetType:image";
} else {
typeOptions = typeOptions+"%20assetType:image";
}
}
if ("<?php echo get_option('type-video') ?>"==="1") {
if (typeOptions==="" ) {
typeOptions = typeOptions+"assetType:video";
} else {
typeOptions = typeOptions+"%20assetType:video";
}
}
if ("<?php echo get_option('type-file') ?>"==="1") {
if (typeOptions==="" ) {
typeOptions = typeOptions+"assetType:file";
} else {
typeOptions = typeOptions+"%20assetType:file";
}
}
if ("<?php echo get_option('type-document') ?>"==="1") {
if (typeOptions==="" ) {
typeOptions = typeOptions+"assetType:document";
} else {
typeOptions = typeOptions+"%20assetType:document";
}
}
if (typeOptions.length>0) {
typeOptions="&fq="+typeOptions;
}
var hostRendering="<?php echo "$hostRendering" ?>";
var tenantId="<?php echo "$tenantId" ?>";
</script>
<style type="text/css">
.wchHidden {
display: none;
}
</style>
<script type="text/javascript" src="<?php echo WCH_ASSETPICKER_URL?>/pickerResultHandler.js"></script>
<script type="text/javascript">
function resultHandler(e) {
// no need to close the picker as we have it integrated
// $('#pickerDialog').dialog('close');
// console.log(e);
// alert(e.data);
var result = JSON.parse(e.data);
var resourceURL = assembleResourcURL(result);
console.log("assembled resource url " + resourceURL);
if (resourceURL) {
var targetNode = getRteNode();
if (targetNode) {
//if image
if ( resourceURL.toLowerCase().endsWith("jpeg") || resourceURL.toLowerCase().endsWith("jpg") || resourceURL.toLowerCase().endsWith("gif") || resourceURL.toLowerCase().endsWith("png") ) {
var img = jQuery('<img id="dynamic">');
img.attr("src", resourceURL);
img.appendTo(targetNode);
} else {
var link = jQuery('<a id="dynamic">'+resourceURL+'</a>');
link.attr("href", resourceURL);
link.appendTo(targetNode);
}
}
}
}
// 1. 'addEventListener' is for standards-compliant web browsers and 'attachEvent' is for IE Browsers
var eventMethod = window.addEventListener ? 'addEventListener' : 'attachEvent';
var eventer = window[eventMethod];
// 2. if 'attachEvent', then we need to select 'onmessage' as the event
// else if 'addEventListener', then we need to select 'message' as the event
var messageEvent = eventMethod === 'attachEvent' ? 'onmessage' : 'message';
// Listen to message from child iFrame window
console.log("pickerResultHandler register event listener");
eventer(messageEvent, resultHandler, false);
function search() {
var nodes = document.querySelectorAll("#wchframe");
for (var i = nodes.length-1; i >= 0; i--) {
console.log("Initializing iframe");
nodes[i].src = "https://content-us.goacoustic.com/content-picker/picker.html?fq=classification:asset"+typeOptions+"&apiUrl="+encodeURIComponent(apiUrl);
}
jQuery("#wchframe").removeClass("wchHidden");
jQuery("#wchframeloader").addClass("wchHidden");
};
jQuery( document ).ready(function() {
search();
});
</script>
<div>
<div id="wchframeloader">Please wait... WCH connection is initializing...</div>
<iframe id="wchframe" class="wchHidden" height="800" width="100%"></iframe>
</div>
<?php
}
function add_custom_meta_box()
{
add_meta_box("demo-meta-box", "Acoustic Content Asset Palette", "custom_meta_box_markup", "page", "normal", "high", null);
add_meta_box("demo-meta-box", "Acoustic Content Asset Palette", "custom_meta_box_markup", "post", "normal", "high", null);
}
//add to right column
add_action("add_meta_boxes", "add_custom_meta_box");
//add to center column
add_action("edit_post", "add_custom_meta_box");
/**
* Activation, Deactivation and Uninstall Functions
*
**/
register_activation_hook(__FILE__, 'wch_assetpicker_activation');
register_deactivation_hook(__FILE__, 'wch_assetpicker_deactivation');
function wch_assetpicker_activation() {
//actions to perform once on plugin activation go here
//register uninstaller
register_uninstall_hook(__FILE__, 'wch_assetpicker_uninstall');
}
function wch_assetpicker_deactivation() {
// actions to perform once on plugin deactivation go here
}
function wch_assetpicker_uninstall(){
//actions to perform once on plugin uninstall go here
}
include "settings.php";
?>