-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
353 lines (315 loc) · 13.5 KB
/
index.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<?php
/*
Plugin Name: Formulize Plugin
Plugin URI: http://www.freeformsolutions.ca/en/formulize
Description: Data management and reporting on the web, in your CMS, and on your mobile device.
Version: 1.0
Author: Formulize Project
Author URI: http://www.freeformsolutions.ca/en/formulize
License: GPL2
*/
$formulize_path = get_option('formulize_path', NULL);
if (file_exists($formulize_path . DIRECTORY_SEPARATOR . 'integration_api.php')) {
include_once($formulize_path . DIRECTORY_SEPARATOR . 'integration_api.php');
}
if (!class_exists('FormulizePluginSettings')) {
define('FORMULIZE_PLUGIN_ID', 'formulize-plugin');
define('FORMULIZE_PLUGIN_NAME', 'Formulize Plugin');
class FormulizePluginSettings {
/** function/method
* Usage: return absolute file path
* Arg(1): string
* Return: string
*/
public static function file_path($file) {
return ABSPATH.'wp-content/plugins/'.str_replace(basename(__FILE__),"",plugin_basename(__FILE__)).$file;
}
/** function/method
* Usage: hooking the plugin settings
* Arg(0): null
* Return: void
*/
public static function register() {
register_setting(FORMULIZE_PLUGIN_ID.'_options', 'formulize_path');
register_setting(FORMULIZE_PLUGIN_ID.'_options', 'synchronize_users_button');
}
/** function/method
* Usage: hooking (registering) the plugin menu
* Arg(0): null
* Return: void
*/
public static function menu() {
// Create menu tab
add_options_page(FORMULIZE_PLUGIN_NAME.' Plugin Settings', FORMULIZE_PLUGIN_NAME, 'manage_options',
FORMULIZE_PLUGIN_ID.'_options', array('FormulizePluginSettings', 'settings_page'));
}
/** function/method
* Usage: show settings form page
* Arg(0): null
* Return: void
*/
public static function settings_page() {
if (!current_user_can('manage_options')) {
wp_die(__('You do not have sufficient permissions to access this page.'));
}
$plugin_id = FORMULIZE_PLUGIN_ID;
// display settings page
include(self::file_path('options.php'));
}
/** function/method
* Usage: filtering the content
* Arg(1): string
* Return: string
*/
public static function content_with_quote($content) {
return $content . '<p><blockquote>' . get_option('kkpo_quote') . '</blockquote></p>';
}
}
/* Add box to the post and page screens */
function formulize_meta_box() {
add_meta_box('formulize_sectionid',
__('Formulize', 'formulize_textlabel'),
'formulize_inner_custom_box',
'page'
);
}
function formulize_options_link($links) {
$links[] = '<a href="'.admin_url().'options-general.php?page=formulize-plugin_options">'.__(Settings).'</a>';
return $links;
}
/* Print box content */
function formulize_inner_custom_box($post) {
global $post;
$meta = get_post_meta($post->ID, 'formulize_select', true);
$values = get_post_custom($post->ID);
$selected = isset($values['formulize_select']) ? esc_attr($values['formulize_select']) : '';
// We'll use this nonce field later on when saving.
wp_nonce_field('my_formulize_nonce', 'formulize_nonce');
if (class_exists("Formulize")) {
$screen_names = Formulize::getScreens();
array_unshift($screen_names, 'No Screens');
echo '<label for="formulize_select">Choose screen: </label>';
echo '<select name="formulize_select" id="formulize_select">';
if (count($screen_names) > 0) {
foreach ($screen_names as $id) {
echo '<option ', $meta == $id ? ' selected="selected"' : '', '>', $id, '</option>';
}
} else {
// No settings
echo '<option value=0> No screens found </option>';
}
echo '</select>';
} else {
$link = formulize_options_link(array());
echo "Set Formulize path in the ".$link[0].".";
}
}
/* When the post is saved, saves our custom data */
function formulize_save_postdata($post_id) {
// verify if this is an auto save routine
// If our form hasn't been submitted we don't want to do anything
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return;
// if our nonce isn't there, or we can't verify it, bail
if (!isset($_POST['formulize_nonce']) || !wp_verify_nonce($_POST['formulize_nonce'], 'my_formulize_nonce'))
return;
// now we can actually save the data
$allowed = array(
'a' => array(
// on allow a tags
'href' => array() // and those anchors can only have href attribute
)
);
$old = get_post_meta($post_id, 'formulize_select', true);
$new = $_POST['formulize_select'];
if ($new && $new != $old) {
update_post_meta($post_id, 'formulize_select', $new);
} elseif ('' == $new && $old) {
delete_post_meta($post_id, 'formulize_select', $old);
}
}
if (is_admin()) {
add_action('admin_init', array('FormulizePluginSettings', 'register'));
add_action('admin_menu', array('FormulizePluginSettings', 'menu'));
}
add_filter('the_content', array('FormulizePluginSettings', 'content_with_quote'));
}
/**
* This function is used to insert the contents of a Formulize table on a Wordpress
* page.
*/
function insertFormulize($content) {
//Here we echo the original content of the page.
echo $content;
if (class_exists("Formulize")) {
//Here is where we obtain the screen we should display. Once we find the adequate screen name, we will
//render our Formulize screen, then break from our loop to ensure only one screen is rendered.
$custom_fields = get_post_custom($GLOBALS['post']->ID);
$formulize_screen_id = -1; // Default is to have no screens displayed
$screen_names = Formulize::getScreens();
foreach($screen_names as $id=>$name) {
if ($custom_fields['formulize_select'][0] == $name) {
Formulize::renderScreen($id);
break;
}
}
}
}
/**
* This function handles the addition of the Formulize style-sheet to the plugin. It will ensure
* that the necessary .css file is included. Additionally, it will also start a session for us with
* formulize. Depending on whether you are a registered user or anonymous user it will sync accordingly
* and grant permissions for interactions with the various forms as such.
*
*/
function insertFormulizeStylesheet() {
//Here we register and enqueue the style sheet for the plug in.
wp_register_style('newstyle', plugins_url('newstyle.css', __FILE__));
wp_enqueue_style('newstyle');
}
// Wordpress doesn't have unique IDs for roles, so we do it ourselves inside this function
function wp_role_id_for_name($name) {
// Wordpress does not use role_ids, assign arbitrary values to wordpress built-in roles to satisfy formulize
$name = strtolower($name); // sometimes role names are capitalized?
$formulize_role_ids = array(
"administrator" => 1,
"editor" => 2,
"author" => 3,
"contributor" => 4,
"subscriber" => 5,
);
return (isset($formulize_role_ids[$name]) ? $formulize_role_ids[$name] : null);
}
/*
* This function is called when a new user registers on the wordpress site.
* It is called as the new user form is submitted, and pushes the information
* to formulize as the new user is created on WP.
*/
function addUser($userID) {
$wpUser = get_userdata($userID);
$userData = array(
'uid'=>$wpUser->ID,
'uname'=>$wpUser->display_name,
'login_name'=>$wpUser->user_login,
'email'=>$wpUser->user_email,
'timezone_offset'=> 0,
);
$formUser = new FormulizeUser($userData);
Formulize::createUser($formUser);
updateUserRole($userID, $wpUser->roles[0]);
}
/**
* This function will delete a user from the Formulize database once the user is deleted on Wordpress.
*
* AS OF 03/02/12 - DeleteUser in Formulize API calls die(). This causes the script to end and breaks WP functionality.
* Do not uncomment the delete user hook.
*/
function deleteUser($userID) {
Formulize::deleteUser($userID);
}
//Need to add a check in here so we don't synchronized a user twice...same for adding a user.
//I.e. We need a function in the API to query the formulize database so that we can confirm whether
//the user already exists (Or is this in the API?)
function synchronizeUsers() {
// first, sync all the groups
global $wp_roles;
if (! isset($wp_roles))
$wp_roles = new WP_Roles();
$roles_names = $wp_roles->get_names();
foreach ($roles_names as $key => $role_name) {
Formulize::createGroup(wp_role_id_for_name($role_name), $role_name, $role_name, $role_name);
}
// now sync all the users
$users = get_users();
foreach ($users as $wpUser) {
$userData = array(
'uid'=>$wpUser->ID,
'uname'=>$wpUser->display_name,
'login_name'=>$wpUser->user_login,
'email'=>$wpUser->user_email,
'timezone_offset'=> 0,
);
$formUser = new FormulizeUser($userData);
if (Formulize::createUser($formUser) == FALSE) {
echo "FALSE FALSE!";
} else {
// only create the groups the first time around
sync_user_groups($wpUser);
}
echo '<li>'. $wpUser->user_email . " " . $wpUser->ID . " " . $wpUser->display_name . " " . $wpUser->user_login . '</li>';
}
}
/**
* This function will update the information for a user in the Formulize database. It will be run
* at the end of every profile update currently, such that as soon as User information is edited, the
* information stored in Formulize will likewise be updated.
*
* AS OF 03/02/12 - Update Users function in API appear to do nothing yet. This function has no effect as of yet.
*/
function updateUser($wpUser) {
$userData = array(
'uid'=>$wpUser->ID,
'uname'=>$wpUser->display_name,
'login_name'=>$wpUser->user_login,
'email'=>$wpUser->user_email,
'timezone_offset'=> 0,
);
if ($wpUser->ID!=1) {
if (Formulize::updateUser($wpUser->ID,$userData)) {
}
}
}
function initializeUserInfo() {
if (class_exists("Formulize")) {
$current_user = wp_get_current_user();
if (!($current_user instanceof WP_User)) {
$GLOBALS['formulizeHostSystemUserId'] = 0;
} else {
$GLOBALS['formulizeHostSystemUserId'] = $current_user->ID;
}
// Initialize Formulize
Formulize::init();
global $wp_roles;
$roles = $wp_roles->roles;
$rolesArray = array();
do {
$rolesArray[] = key($roles);
} while(next($roles) !== FALSE);
}
}
if (isset($_GET['syncUsers'])) {
if ($_GET['syncUsers'] == TRUE) {
synchronizeUsers();
}
}
function updateUserRole($user_id, $role_name) {
$user = get_userdata($user_id);
// user account was remove from a role, but we don't know which one, so sync all groups
if ($user)
sync_user_groups($user);
}
function sync_user_groups(WP_User $user) {
global $wp_roles;
if (! isset($wp_roles))
$wp_roles = new WP_Roles();
$roles_names = $wp_roles->get_names();
$roles_names = array_map("strtolower", $roles_names);
foreach ($roles_names as $key => $role_name) {
if (in_array($role_name, $user->roles)) {
Formulize::addUserToGroup($user->ID, wp_role_id_for_name($role_name));
} else {
Formulize::removeUserFromGroup($user->ID, wp_role_id_for_name($role_name));
}
}
}
add_action('init','initializeUserInfo');
add_action('wp_enqueue_scripts', 'insertFormulizeStylesheet');
// The delete function in the API calls the die function. Uncommenting this and attempting to delete a user crashes WP.
add_action('delete_user','deleteUser');
add_action('edit_user_profile', 'updateUser'); // <-- Update user is stub. Doesn't do anything yet in API.
add_action('add_meta_boxes', 'formulize_meta_box');
add_action('save_post', 'formulize_save_postdata');
add_action('user_register','addUser'); // <--Currently this function works and updates the Formulize site.
add_filter('the_content','insertFormulize'); //Need to fix this hook so that the table is displayed appropriately on each page
add_filter("plugin_action_links_".plugin_basename(__FILE__), 'formulize_options_link');
add_action('set_user_role', 'updateUserRole', 10, 2);