Skip to content

Commit ca2f69f

Browse files
committed
Initialize config set if it is empty.
1 parent 8595f76 commit ca2f69f

File tree

2 files changed

+89
-7
lines changed

2 files changed

+89
-7
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
/**
3+
* Copyright 2012 Zendesk.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
class Zendesk_Zendesk_Model_Api_ConfigSets extends Zendesk_Zendesk_Model_Api_Abstract
19+
{
20+
CONST DEFAULT_WIDGET_COLOR = '#78a300';
21+
22+
CONST DEFAULT_WIDGET_POSITION = 'right';
23+
24+
/**
25+
* Finds the embeddable config sets in the account
26+
*
27+
* @return array
28+
*/
29+
public function find()
30+
{
31+
$response = $this->_call('embeddable/config');
32+
33+
return $response ? $response['embeds'] : [];
34+
}
35+
36+
/**
37+
* Initializes Zendesk's WebWidget
38+
*
39+
* @param array $config
40+
*/
41+
public function initialize($config = [])
42+
{
43+
$config = array_merge([], $this->_getDefaultWidgetConfig());
44+
45+
return $this->_call('embeddable/api/config_sets.json', null, 'POST', [
46+
'config_set' => $config,
47+
]);
48+
}
49+
50+
/**
51+
* Override the _getUrl method to prevent appending the api/v2 base path
52+
*
53+
* @param string $path
54+
* @return string
55+
*/
56+
protected function _getUrl($path)
57+
{
58+
return 'https://' . $this->getDomain() . '/' . trim($path, '/');
59+
}
60+
61+
/**
62+
* Returns the default widget config
63+
*
64+
* @return array
65+
*/
66+
private function _getDefaultWidgetConfig()
67+
{
68+
return [
69+
'color' => self::DEFAULT_WIDGET_COLOR,
70+
'position' => self::DEFAULT_WIDGET_POSITION,
71+
];
72+
}
73+
}

src/app/code/community/Zendesk/Zendesk/Model/Observer.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,17 +101,26 @@ public function saveConfig(Varien_Event_Observer $observer)
101101
// If the zendesk domain is not found in the web widget snippet (wrapped with quotes), generate it again
102102
$zDomain = Mage::getStoreConfig('zendesk/general/domain', $storeCode);
103103
$widgetSnippet = Mage::getStoreConfig('zendesk/frontend_features/web_widget_code_snippet', $storeCode);
104-
// Case insensitive search with single and double quotes, still better performance than 1 regexp search
105-
if($zDomain && stripos($widgetSnippet, "'{$zDomain}'") === false && stripos($widgetSnippet, '"'.$zDomain.'"') === false) {
106-
$webWidgetSnippet=<<<EOJS
104+
105+
if (! empty($zDomain)) {
106+
if ((bool) Mage::getStoreConfig('zendesk/frontend_features/web_widget_code_active')) {
107+
$embeds = Mage::getModel('zendesk/api_configsets')->find();
108+
if (! isset($embeds['launcher'])) {
109+
Mage::getModel('zendesk/api_configsets')->initialize();
110+
}
111+
}
112+
113+
// Case insensitive search with single and double quotes, still better performance than 1 regexp search
114+
$hasUnatchedSnippet = stripos($widgetSnippet, "'{$zDomain}'") === false && stripos($widgetSnippet, '"'.$zDomain.'"') === false;
115+
if (! $hasUnmatchedSnippet) {
116+
$webWidgetSnippet=<<<EOJS
107117
<!-- Start of Zendesk Widget script -->
108118
<script>/*<![CDATA[*/window.zEmbed||function(e,t){var n,o,d,i,s,a=[],r=document.createElement("iframe");window.zEmbed=function(){a.push(arguments)},window.zE=window.zE||window.zEmbed,r.src="javascript:false",r.title="",r.role="presentation",(r.frameElement||r).style.cssText="display: none",d=document.getElementsByTagName("script"),d=d[d.length-1],d.parentNode.insertBefore(r,d),i=r.contentWindow,s=i.document;try{o=s}catch(c){n=document.domain,r.src='javascript:var d=document.open();d.domain="'+n+'";void(0);',o=s}o.open()._l=function(){var o=this.createElement("script");n&&(this.domain=n),o.id="js-iframe-async",o.src=e,this.t=+new Date,this.zendeskHost=t,this.zEQueue=a,this.body.appendChild(o)},o.write('<body onload="document._l();">'),o.close()}("https://assets.zendesk.com/embeddable_framework/main.js","{$zDomain}");/*]]>*/</script>
109119
<!-- End of Zendesk Widget script -->
110120
EOJS;
111-
112-
Mage::getModel('core/config')->saveConfig('zendesk/frontend_features/web_widget_code_active', 1);
113-
Mage::getModel('core/config')->saveConfig('zendesk/frontend_features/web_widget_code_snippet', $webWidgetSnippet);
114-
} elseif (empty($zDomain)) {
121+
Mage::getModel('core/config')->saveConfig('zendesk/frontend_features/web_widget_code_snippet', $webWidgetSnippet);
122+
}
123+
} else {
115124
Mage::getModel('core/config')->saveConfig('zendesk/frontend_features/web_widget_code_snippet', '');
116125
}
117126
}

0 commit comments

Comments
 (0)