This repository has been archived by the owner on Jul 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kayako2wordpress.php
291 lines (235 loc) · 9.9 KB
/
kayako2wordpress.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
/*
Plugin Name: Kayako2Wordpress
Plugin URI: https://github.com/sierag/kayako2wordpress
Description: List flagged Kayako tickets in Wordpress for processing tickets content your Wordpress website. Communicates with the Kayako API.
Version: 1.0
Author: sierag
Author URI: www.sierag.nl
Author Email: [email protected]
License:
Copyright 2013 TODO ([email protected])
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
require_once("KayakoAPILibrary_PHP-v1.1.1/kyIncludes.php");
/**
* Initialization.
*/
// TODO: rename this class to a proper name for your plugin
class Kayako2Wordpress {
/*--------------------------------------------*
* Constructor
*--------------------------------------------*/
/**
* Initializes the plugin by setting localization, filters, and administration functions.
*/
function __construct() {
// Load plugin text domain
add_action( 'init', array( $this, 'plugin_textdomain' ) );
// Register admin styles and scripts
add_action( 'admin_print_styles', array( $this, 'register_admin_styles' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'register_admin_scripts' ) );
if ( is_admin() ){
/* Settings page html code */
add_action( 'admin_menu', array( $this, 'kayako2wordpress_admin_menu' ) );
if( ( substr( $_SERVER["PHP_SELF"], -11 ) == 'plugins.php' || $_REQUEST['page'] == "kayako2wordpress" ) && !(get_option( 'kayako_settings' ) ) ) {
add_action( 'admin_notices', array( $this, 'at_force_fill_credentials' ) );
}
}
// Register site styles and scripts
add_action( 'wp_enqueue_scripts', array( $this, 'register_plugin_styles' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'register_plugin_scripts' ) );
// Register hooks that are fired when the plugin is activated, deactivated, and uninstalled, respectively.
register_activation_hook( __FILE__, array( $this, 'activate' ) );
register_deactivation_hook( __FILE__, array( $this, 'deactivate' ) );
register_uninstall_hook( __FILE__, array( $this, 'uninstall' ) );
} // end constructor
/**
* Fired when the plugin is activated.
*
* @param boolean $network_wide True if WPMU superadmin uses "Network Activate" action, false if WPMU is disabled or plugin is activated on an individual blog
*/
public function activate( $network_wide ) {
if ( version_compare( get_bloginfo( 'version' ) , '2.9' , '<' ) ) {
deactivate_plugins( basename( dirname( __FILE__ ) ) . '/' . basename( __FILE__ ) );
wp_die( sprintf( __( "Require wordpress greater than 2.9") ) );
}
// check if object is returned. Than we can say that
if(get_option('kayako_settings_installed') !== FALSE) {
return update_option('kayako_settings_installed', false);
}
return add_option('kayako_settings_installed', false);
} // end activate
/**
* Fired when the plugin is deactivated.
*
* @param boolean $network_wide True if WPMU superadmin uses "Network Activate" action, false if WPMU is disabled or plugin is activated on an individual blog
*/
public function deactivate( $network_wide ) {
// TODO: Define deactivation functionality here
} // end deactivate
/**
* Fired when the plugin is uninstalled.
*
* @param boolean $network_wide True if WPMU superadmin uses "Network Activate" action, false if WPMU is disabled or plugin is activated on an individual blog
*/
public function uninstall( $network_wide ) {
delete_option("kayako_settings");
delete_option('kayako_settings_installed');
} // end uninstall
/**
* Loads the plugin text domain for translation
*/
public function plugin_textdomain() {
// TODO: replace "plugin-name-locale" with a unique value for your plugin
$domain = 'plugin-name-locale';
$locale = apply_filters( 'plugin_locale', get_locale(), $domain );
load_textdomain( $domain, WP_LANG_DIR.'/'.$domain.'/'.$domain.'-'.$locale.'.mo' );
load_plugin_textdomain( $domain, FALSE, dirname( plugin_basename( __FILE__ ) ) . '/lang/' );
} // end plugin_textdomain
/**
* Registers and enqueues admin-specific styles.
*/
public function register_admin_styles() {
// TODO: Change 'plugin-name' to the name of your plugin
wp_enqueue_style( 'plugin-name-admin-styles', plugins_url( 'kayako2wordpress/css/admin.css' ) );
} // end register_admin_styles
/**
* Registers and enqueues admin-specific JavaScript.
*/
public function register_admin_scripts() {
// TODO: Change 'plugin-name' to the name of your plugin
wp_enqueue_script( 'kayako2wordpress', plugins_url( 'kayako2wordpress/js/admin.js' ) );
} // end register_admin_scripts
public function kayako2wordpress_admin_menu() {
add_options_page( 'Kayako2Wordpress', 'Kayako2Wordpress', 'administrator','kayako2wordpress-settings', 'kayako2wordpress_admin_page');
add_management_page( 'Kayako2Wordpress', 'Kayako2Wordpress', 'administrator','kayako2wordpress-settings', 'kayako2wordpress_display_page');
}
public function at_force_fill_credentials(){
$message = ( $_REQUEST['page'] == "kayako2wordpress" ? 'any of the service fields with corresponding keys / IDs ' : 'the <b><a href="'.admin_url( 'options-general.php?page=kayako2wordpress-settings' ).'">settings</a></b> page with the credentials' );
echo '<div class="error"><p>Fill '.$message.' to enable Kayako2Wordpress.</p></div>';
}
/**
* Registers and enqueues plugin-specific styles.
*/
public function register_plugin_styles() {
// TODO: Change 'plugin-name' to the name of your plugin
wp_enqueue_style( 'plugin-name-plugin-styles', plugins_url( 'plugin-name/css/display.css' ) );
} // end register_plugin_styles
/**
* Registers and enqueues plugin-specific scripts.
*/
public function register_plugin_scripts() {
// TODO: Change 'plugin-name' to the name of your plugin
wp_enqueue_script( 'plugin-name-plugin-script', plugins_url( 'plugin-name/js/display.js' ) );
} // end register_plugin_scripts
/**
* Registers and enqueues plugin-specific scripts.
*/
public function initSettings() {
$data = $this->getSettings();
$config = new kyConfig($data['kayako_url'], $data['kayako_key'], $data['kayako_secret']);
$config->setDebugEnabled(false);
kyConfig::set($config);
return $data['kayako_tag'];
}
/**
* Registers and enqueues plugin-specific scripts.
*/
public function checkSettings() {
$data = $this->getSettings();
$config = new kyConfig($data['kayako_url'], $data['kayako_key'], $data['kayako_secret']);
$config->setDebugEnabled(false);
kyConfig::set($config);
$obj = kyTicketStatus::getAll()->first();
update_option('kayako_settings_installed', is_object($obj));
return is_object($obj);
} // end register_plugin_scripts
/**
* Sets/Updates the Kayko settings
* @param type $data
* @return boolean
*/
public function saveSettings($data) {
if(!is_array($data)) {
return false;
}
if(array_key_exists('kayako_url', $data))
$this->settings['kayako_url'] = $data['kayako_url'];
if(array_key_exists('kayako_key', $data))
$this->settings['kayako_key'] = $data['kayako_key'];
if(array_key_exists('kayako_secret', $data))
$this->settings['kayako_secret'] = $data['kayako_secret'];
if(array_key_exists('kayako_tag', $data))
$this->settings['kayako_tag'] = $data['kayako_tag'];
if(array_key_exists('kayako_new_tag', $data))
$this->settings['kayako_new_tag'] = $data['kayako_new_tag'];
if(array_key_exists('kayako_frontend_url', $data))
$this->settings['kayako_frontend_url'] = $data['kayako_frontend_url'];
if(get_option('kayako_settings') !== FALSE) {
return update_option('kayako_settings', $this->settings);
}
return add_option('kayako_settings', $this->settings);
}
/**
* Fetch the current Kayako settings
* @return mixed An asociative array of settings
*/
public function getSettings() {
return $this->settings = get_option('kayako_settings');
}
/**
* Fetch the current Kayako settings
* @return mixed An asociative array of settings
*/
public function getTagFromSettings() {
$settings = get_option('kayako_settings');
return $settings['kayako_tag'];
}
/**
* Fetch the current Kayako settings
* @return mixed An asociative array of settings
*/
public function getNewTagFromSettings() {
$settings = get_option('kayako_settings');
return $settings['kayako_new_tag'];
}
public function getFrontendURLFromSettings() {
$settings = get_option('kayako_settings');
return $settings['kayako_frontend_url'];
}
public function getTickets() {
$this->initSettings();
return kyTicket::search($this->getTagFromSettings(), array(kyTicket::SEARCH_TAGS));
}
public function getTicket($ticket_id) {
$this->initSettings();
return kyTicket::search($ticket_id, array(kyTicket::SEARCH_TICKET_ID));
}
public function removeTicket($ticket_id, $displayname) {
$ticket = $this->getTicket($ticket_id);
$user = $ticket->first()->getOwnerStaff();
$ticket->first()->newNote($user, $displayname . " decided not to create a knowledgebase article from this ticket.")->create();
}
} // end class
// TODO: Update the instantiation call of your plugin to the name given at the class definition
$kayako2wordpress = new Kayako2Wordpress();
function kayako2wordpress_admin_page() {
if ( version_compare( get_bloginfo( 'version' ) , '3.3' , '<' ) )
wp_head();
require( "views/admin.php" );
}
function kayako2wordpress_display_page() {
if ( version_compare( get_bloginfo( 'version' ) , '3.3' , '<' ) )
wp_head();
require( "views/display.php" );
}