-
Notifications
You must be signed in to change notification settings - Fork 0
/
guthrie.php
executable file
·488 lines (434 loc) · 19 KB
/
guthrie.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
<?php
/*
Plugin Name: Guthrie
Plugin URI: http://guthrie.sethmurphy.com
Description: A place to share your identity with those you know.
Version: 0.8.1
Author: Seth Murphy
Author URI: http://sethmurphy.com
License: GPL2
Copyright 2011 Seth Murphy (email : [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
*/
$guthrie = new Guthrie;
class Guthrie {
public $status_message = '';
private $ajax;
function Guthrie() {
$this->__construct();
}
function __construct() {
// Do our install stuff upon activation
register_activation_hook( __FILE__, array( &$this, 'install') );
// Do our uninstall'ish stuff upon deactivation
// NOTE: unistall, which removes all data, is in uninstall.php
register_deactivation_hook( __FILE__, array( &$this, 'deactivate') );
// Add the admin menu ( all other admin hooks are in menu() )
add_action( 'admin_menu', array( &$this, 'admin_menu') );
// used to embed a profiles anywhere using [GUTHRIE]
add_filter( 'the_content', array( &$this, 'profile') );
// register our styles (non-admin)
add_action('wp_print_styles', array(&$this, 'add_guthrie_stylesheet'));
// register our menu hook to allow hiding of menu items (default hidden)
add_filter( 'get_pages', array( &$this, 'set_profile_page_visibility' ) );
// register our ajax calls (all handled by the same delagator with work oflaoded to a separate class when needed)
add_action( 'wp_ajax_guthrie_update_profile_field_roles', array( &$this, 'do_ajax' ) ); // action = 'guthrie_update_profile_field_roles'
add_action( 'wp_ajax_guthrie_update_profile_invitation_roles', array( &$this, 'do_ajax' ) ); // action = 'guthrie_update_profile_invitation_roles'
add_action( 'wp_ajax_guthrie_update_profile_field_value', array( &$this, 'do_ajax' ) ); // action = 'guthrie_update_profile_field_value'
add_action( 'wp_ajax_guthrie_update_profile_field_sequence', array( &$this, 'do_ajax' ) ); // action = 'guthrie_update_profile_field_sequence'
add_action( 'wp_ajax_guthrie_remove_profile_field_instance', array( &$this, 'do_ajax' ) ); // action = 'guthrie_remove_profile_field_instance'
add_action( 'wp_ajax_guthrie_update_profile_role_name', array( &$this, 'do_ajax' ) ); // action = 'guthrie_update_profile_role_name'
add_action( 'wp_ajax_guthrie_update_profile_role_description', array( &$this, 'do_ajax' ) ); // action = 'guthrie_update_profile_role_description'
add_action( 'wp_ajax_guthrie_update_profile_page_visibility', array( &$this, 'do_ajax' ) ); // action = 'guthrie_update_profile_page_visibility'
add_action( 'wp_ajax_guthrie_update_profile_invitation_name', array( &$this, 'do_ajax' ) ); // action = 'guthrie_update_profile_invitation_name'
add_action( 'wp_ajax_guthrie_update_profile_invitation_description', array( &$this, 'do_ajax' ) ); // action = 'guthrie_update_profile_invitation_description'
add_action( 'wp_ajax_guthrie_remove_profile_invitation', array( &$this, 'do_ajax' ) ); // action = 'guthrie_remove_profile_invitation'
add_action( 'wp_ajax_guthrie_remove_profile_role', array( &$this, 'do_ajax' ) ); // action = 'guthrie_remove_profile_role'
add_action( 'wp_ajax_guthrie_remove_profile_field_instance', array( &$this, 'do_ajax' ) ); // action = 'guthrie_remove_profile_field_instance'
}
function install () {
require( WP_PLUGIN_DIR . '/guthrie/guthrie_install.php' );
$g_install = new Guthrie_Install;
$g_install->create_database(); // make sure we have a database
$g_install->populate_database_defaults(); // populate the database tables with default values(inluding test values for now) if they are empty
$g_install->create_default_guthrie_profile_page();
}
function deactivate () {
// just remove our default page
// data is removed upon deactivation
require( WP_PLUGIN_DIR . '/guthrie/guthrie_install.php' );
$g_install = new Guthrie_Install;
$g_install->delete_default_guthrie_profile_page();
}
/*********************************************************
* Add a minimal stylesheet for our Guthrie Profile
* Loads on every page, but it is small ...
* Does NOT include any CSS for the admin section
* Themes should overide the styles if they wish
********************************************************/
function add_guthrie_stylesheet(){
$styleUrl = plugins_url('/css/guthrie.css', __FILE__);
$styleFile = WP_PLUGIN_DIR . '/guthrie/css/guthrie.css';
if ( file_exists($styleFile) ) {
wp_register_style('guthrie', $styleUrl);
wp_enqueue_style('guthrie');
}
}
/**
* Generic function to show a message to the user using WP's
* standard CSS classes to make use of the already-defined
* message colour scheme.
*
* @param $message The message you want to tell the user.
* @param $errormsg If true, the message is an error, so use
* the red message style. If false, the message is a status
* message, so use the yellow information message style.
*/
function show_message( $message, $errormsg = false )
{
if ( $errormsg ) {
echo '<div id="message" class="error">';
} else {
echo '<div id="message" class="updated fade">';
}
echo "<p><strong>$message</strong></p></div>";
}
/**
* Just show our message (with possible checking if we only want
* to show message to certain users.
*/
function show_admin_messages($errormsg = false)
{
if($this->status_message != '') {
$this->show_message( $this->status_message, $errormsg );
$this->status_message = '';
}
}
/**
* create our admin menu
*/
function admin_menu() {
$mypage = add_options_page( 'Guthrie Options', 'Guthrie', 'manage_options', 'guthrie', array( &$this, 'options') );
add_action( "admin_print_scripts-$mypage", array(&$this,'admin_head') );
add_action( 'admin_notices', array( &$this, 'show_admin_messages' ) );
}
/**
* set the visibility of the default profile page menu item
*/
function set_profile_page_visibility( $pages ) {
$length = count( $pages );
$page_id = get_option( 'guthrie_default_post_id' );
$show_page = get_option( 'guthrie_show_profile_page' );
if ( isset( $show_page ) && 'true' == $show_page ) {
$show_page = true;
} else {
$show_page = false;
}
$new_pages = array();
for ( $i = 0; $i < $length; $i++ ) {
$page = $pages[ $i ];
if ( $page->ID == $page_id ) {
if ( $show_page ) { $new_pages[] = $page; }
} else {
$new_pages[] = $page;
}
}
return $new_pages;
}
/* manage our plugin option in admin */
function options() {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
}
include WP_PLUGIN_DIR . '/guthrie/admin_options_page.php';
}
function admin_head() {
$this->admin_print_scripts();
$this->admin_print_styles();
wp_print_styles();
}
function admin_print_styles() {
// enqueue here
$styleUrl = plugins_url('/css/admin-style.css', __FILE__);
$styleFile = WP_PLUGIN_DIR . '/guthrie/css/admin-style.css';
if ( file_exists($styleFile) ) {
wp_register_style('guthrie-admin', $styleUrl);
wp_enqueue_style('guthrie-admin');
}
$styleUrl = plugins_url( '/css/chosen.css', __FILE__ );
$styleFile = WP_PLUGIN_DIR . '/guthrie/css/chosen.css';
if ( file_exists( $styleFile ) ) {
wp_register_style( 'chosen', $styleUrl );
wp_enqueue_style( 'chosen' );
}
$styleUrl = plugins_url( '/css/guthrie.editinplace.jquery.css', __FILE__ );
$styleFile = WP_PLUGIN_DIR . '/guthrie/css/guthrie.editinplace.jquery.css';
if ( file_exists( $styleFile ) ) {
wp_register_style( 'guthrie-editinplace', $styleUrl );
wp_enqueue_style( 'guthrie-editinplace' );
}
// we are still in our head, but have written the styles already, so force a new write.
//wp_enqueue_style('guthrie-style')
}
function admin_print_scripts() {
$scriptUrl = plugins_url( '/js/jquery.ui.min.js', __FILE__ );
$scriptFile = WP_PLUGIN_DIR . '/guthrie/js/jquery.ui.min.js';
if ( file_exists( $scriptFile ) ) {
wp_deregister_script( 'jquery-ui' );
wp_register_script( 'jquery-ui', $scriptUrl );
wp_enqueue_script( 'jquery-ui' );
} else {
wp_deregister_script( 'jquery-ui' );
wp_register_script( 'jquery-ui', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js' );
wp_enqueue_script( 'jquery-ui' );
}
$scriptUrl = plugins_url( '/js/jquery.min.js', __FILE__ );
$scriptFile = WP_PLUGIN_DIR . '/guthrie/js/jquery.min.js';
if ( file_exists( $scriptFile ) ) {
wp_deregister_script( 'jquery' );
wp_register_script( 'jquery', $scriptUrl );
wp_enqueue_script( 'jquery' );
} else {
wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js' );
wp_enqueue_script( 'jquery' );
}
$scriptUrl = plugins_url( '/js/chosen.jquery.js', __FILE__ );
$scriptFile = WP_PLUGIN_DIR . '/guthrie/js/chosen.jquery.js';
if ( file_exists( $scriptFile ) ) {
wp_register_script( 'chosen', $scriptUrl );
wp_enqueue_script( 'chosen' );
}
$scriptUrl = plugins_url( '/js/guthrie.js', __FILE__ );
$scriptFile = WP_PLUGIN_DIR . '/guthrie/js/guthrie.js';
if ( file_exists( $scriptFile ) ) {
wp_register_script( 'guthrie', $scriptUrl );
wp_enqueue_script( 'guthrie' );
}
$scriptUrl = plugins_url( '/js/validate.js', __FILE__ );
$scriptFile = WP_PLUGIN_DIR . '/guthrie/js/validate.js';
if ( file_exists( $scriptFile ) ) {
wp_register_script( 'guthrie-validate', $scriptUrl );
wp_enqueue_script( 'guthrie-validate' );
}
$scriptUrl = plugins_url( '/js/guthrie.editinplace.jquery.js', __FILE__ );
$scriptFile = WP_PLUGIN_DIR . '/guthrie/js/guthrie.editinplace.jquery.js';
if ( file_exists( $scriptFile ) ) {
wp_register_script( 'guthrie-editinplace', $scriptUrl );
wp_enqueue_script( 'guthrie-editinplace' );
}
/* our ajax files for the options page*/
wp_localize_script( 'guthrie-ajax-request', 'GuthrieAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
/* to update our field roles in place */
$scriptUrl = plugins_url( '/js/ajax-update-field-roles.js', __FILE__ );
$scriptFile = WP_PLUGIN_DIR . '/guthrie/js/ajax-update-field-roles.js';
if ( file_exists( $scriptFile ) ) {
wp_register_script( 'guthrie-ajax-update-field-roles', $scriptUrl );
wp_enqueue_script( 'guthrie-ajax-update-field-roles' );
}
/* to update our invitation roles in place */
$scriptUrl = plugins_url( '/js/ajax-update-invitation-roles.js', __FILE__ );
$scriptFile = WP_PLUGIN_DIR . '/guthrie/js/ajax-update-invitation-roles.js';
if ( file_exists( $scriptFile ) ) {
wp_register_script( 'guthrie-ajax-update-invitation-roles', $scriptUrl );
wp_enqueue_script( 'guthrie-ajax-update-invitation-roles' );
}
}
// display our profile
function profile( $content = false ) {
global $wpdb;
global $page_id;
if ( ! ( strpos( $content , '[GUTHRIE]' ) === false ) ) {
$html = '';
if ( get_the_ID() == get_option( "guthrie_default_post_id" ) ) {
// dirty, inline css
$html = "<style>h3.post-title {display:none;}</style>";
}
$role_id = 0;
// check if we passed a key
$key = "";
if ( array_key_exists ( 'key', $_REQUEST ) ) {
$key = $_REQUEST[ 'key' ];
} else {
// check if we are passed a role and can manage options
if ( current_user_can( 'manage_options' ) && array_key_exists ( 'role_id', $_REQUEST ) ) {
$role_id = $_REQUEST[ 'role_id' ];
}
}
$invitation_id = null;
if ( isset( $key ) && $key != "" ) {
// get our invitation by key
$table_name = $wpdb->prefix . "guthrie_profile_invitation";
$invitation_id = $wpdb->get_var( "select id from $table_name where guid='$key'" );
}
if ( ! isset( $invitation_id ) || '' == $invitation_id ) {
$invitation_id = 1; // the default public profile
}
$profilefields = null;
if ( $role_id > 0 ) {
$profilefields = $this->get_profile_field_instances_by_role( $role_id );
} else {
// now get our profile based on the invitation id
$profilefields = $this->get_profile_field_instances( $invitation_id );
}
$html.='<div class="profile-field-values" >';
foreach ( $profilefields as $field ) {
$value = $field->value;
if( $this->validate_url( $value ) ) {
$mailto = '';
if( $this->validate_email_address( $value ) ) {
$mailto = 'mailto:';
}
$value = '<a href="' . $mailto . $value . '">' . $value . '</a>';
}
$html.='<div class="profile-field-value profile-field-value_' . $field->tag . '" >'. stripslashes( $value ) .'</div>';
}
$html.='<div class="clearfix"></div></div>';
$content = str_replace ( '[GUTHRIE]' , $html , $content );
}
return $content;
}
/********************************
* See if we are a valid Email Address
*******************************/
private function validate_url( $url )
{
$pattern = '/^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/';
return preg_match( $pattern, $url );
}
/********************************
* See if we are a valid Email Address
* From: http://www.linuxjournal.com/article/9585
*******************************/
function validate_email_address($email) {
$pattern = "/^[-\w.]+@([A-z0-9][-A-z0-9]+\.)+[A-z]{2,4}$/";
return preg_match( $pattern, $email );
}
/****************************************
* Data access methods
* These are used accross screens and may be called more than once due to the modular nature of the code
* Therefore they are cached in a private variable for each requests lifecyle
****************************************/
private $profile_field_types;
// get our types
function get_profile_field_types(){
if( ! isset( $this->profile_field_types ) ){
global $wpdb;
$table_name = $wpdb->prefix . "guthrie_profile_field_type";
$sql = "SELECT * FROM $table_name";
$this->profile_field_types = $wpdb->get_results( $sql, OBJECT );
}
return $this->profile_field_types;
}
private $profile_roles;
// get our types
function get_profile_roles(){
if( ! isset( $this->profile_roles ) ){
global $wpdb;
$table_name = $wpdb->prefix . "guthrie_profile_role";
$sql = "SELECT pr.id, pr.name, pr.description " .
"FROM $table_name AS pr " .
"ORDER BY sequence, id";
$this->profile_roles = $wpdb->get_results( $sql, OBJECT );
}
return $this->profile_roles;
}
private $profile_invitations;
// get our invitations
function get_profile_invitations(){
if( ! isset( $this->profile_invitations ) ){
global $wpdb;
$table_name = $wpdb->prefix . "guthrie_profile_invitation";
$sql = "SELECT pi.id, pi.name, pi.description, pi.email, pi.guid, group_concat(ir.profile_role_id) as roles " .
"FROM $table_name as pi " .
"LEFT JOIN " . $wpdb->prefix . "guthrie_profile_invitation_role ir ON ir.profile_invitation_id = pi.id " .
"GROUP BY pi.id ".
"ORDER BY pi.id;";
$this->profile_field_roles = $wpdb->get_results( $sql, OBJECT );
}
return $this->profile_field_roles;
}
private $profile_field_instances;
function get_profile_field_instances( $invitation_id = null ){
if( ! isset( $this->profile_field_instances ) ){
global $wpdb;
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
$where_clause = '';
$invitation_join = '';
if( isset( $invitation_id ) && '0' != $invitation_id){
$invitation_join .= 'LEFT JOIN ' . $wpdb->prefix . 'guthrie_profile_invitation_role ir ON ir.profile_role_id = r.id ';
$invitation_join .= 'LEFT JOIN ' . $wpdb->prefix . 'guthrie_profile_invitation i ON i.id = ir.profile_invitation_id ';
$where_clause = 'WHERE i.id=' . $invitation_id . ' ';
} else {
if ( '0' == $invitation_id ) {
$where_clause = 'WHERE r.id=1 '; // default public profile
}
}
$sql = 'SELECT f.id, fi.id AS profile_field_instance_id, f.tag, f.name, f.description, fi.value, group_concat(r.id) AS roles ' .
'FROM ' . $wpdb->prefix . 'guthrie_profile_field_instance as fi ' .
'LEFT JOIN ' . $wpdb->prefix . 'guthrie_profile_field f ON f.id = fi.profile_field_id ' .
'LEFT JOIN ' . $wpdb->prefix . 'guthrie_profile_field_role fr ON f.id = fr.profile_field_id ' .
'LEFT JOIN ' . $wpdb->prefix . 'guthrie_profile_role r ON r.id = fr.profile_role_id ' .
$invitation_join .
$where_clause .
'GROUP BY f.id ' .
'ORDER BY fi.sequence, f.sequence, r.sequence, profile_field_instance_id;';
//echo($sql);
$this->profile_field_instances = $wpdb->get_results( $sql, OBJECT );
}
return $this->profile_field_instances;
}
private $profile_field_instances_by_role;
function get_profile_field_instances_by_role( $role_id ){
if( ! isset( $this->profile_field_instances ) ){
global $wpdb;
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
$sql = 'SELECT f.id, fi.id AS profile_field_instance_id, f.tag, f.name, f.description, fi.value, group_concat(r.id) AS roles ' .
'FROM ' . $wpdb->prefix . 'guthrie_profile_field_instance as fi ' .
'LEFT JOIN ' . $wpdb->prefix . 'guthrie_profile_field f ON f.id = fi.profile_field_id ' .
'LEFT JOIN ' . $wpdb->prefix . 'guthrie_profile_field_role fr ON f.id = fr.profile_field_id ' .
'LEFT JOIN ' . $wpdb->prefix . 'guthrie_profile_role r ON r.id = fr.profile_role_id ' .
'WHERE r.id = ' . $role_id . ' ' .
'GROUP BY f.id ' .
'ORDER BY fi.sequence, f.sequence, r.sequence, profile_field_instance_id;';
//echo($sql);
$this->profile_field_instances_by_role = $wpdb->get_results( $sql, OBJECT );
}
return $this->profile_field_instances_by_role;
}
/****************************************
* End data access methods
****************************************/
/***************************************************
* Delegate for our AJAX calls in guthrie_ajax.php
***************************************************/
function do_ajax() {
if( ! isset( $this->ajax ) ) {
require_once( 'guthrie_ajax.php' );
$this->ajax = new Guthrie_Ajax( &$this );
}
$method = substr($_POST['action'], 8);
call_user_func( array($this->ajax, $method) );
exit;
}
/********************************************************
* some common gui stuff
********************************************************/
function delete_button( $element_id ) {
return '<span class="delete-button" id="' . $element_id . '"/>';
}
function drag_button() {
return '<span class="drag-button" />';
}
function preview_button() {
return '<span class="preview-button" />';
}
}