This repository was archived by the owner on Mar 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDecisionTree.php
More file actions
491 lines (419 loc) · 18.5 KB
/
DecisionTree.php
File metadata and controls
491 lines (419 loc) · 18.5 KB
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
489
490
491
<?php
/*
Plugin Name: Decision Tree
Plugin URI: http://sidecar.tv/decision_tree
Description: A Decision Tree Builder
Version: 1.1
Author: SideCar Apps
Author URI: http://sidecar.tv/
License: Commerical
*/
/*
* Keep version above in sync with version in app_config.php
*/
require_once 'app_config.php';
require_once 'sidecar/sidecar_track.php';
class DecisionTree {
static function create_post_type() {
if($GLOBALS['wp_version'] >= 3.8){
$menu_icon = "dashicons-networking";
}
else{
$menu_icon = "";
}
register_post_type( sidecar_app_config::$post_type,
array(
'labels' => array(
'menu_name' => 'Trees',
'name' => __( 'Decision Trees' ),
'singular_name' => __( 'Decision Tree' ),
'add_new_item' => __( 'Add New Tree' ),
'all_items' => 'All Trees'
),
'public' => false,
'show_ui' => true,
'show_in_menu' => self::get_plugin_path().'/decision_tree_dashboard.php',
'query_var'=>false,
'has_archive' => false,
'supports' => array( 'title' ),
'register_meta_box_cb' => array('DecisionTree', 'add_metabox'),
'exclude_from_search' => 'true',
'publicly_queryable' => 'false'
)
);
}
/**
* This method along with register_buttons below it, add DT insert buttons to the visual editor
*/
static function add_buttons($plugin_array){
$plugin_array['decision_tree_insert_button'] = plugins_url('/dt_tinymce_plugin.js',__file__);
return $plugin_array;
}
static function register_buttons($buttons){
array_push($buttons, 'decision_tree_insert_button');
return $buttons;
}
/**
* This method adds the insert button to the text editor
*/
static function add_quicktags(){
if (wp_script_is('quicktags')){
?>
<script type="text/javascript">
QTags.addButton( 'dt_insert_tree', 'Decision Tree', dt_insert_prompt,
null, null, 'Decision Tree', 500 );
function dt_insert_prompt(e, c, ed){
Decision_Tree_Insert.decision_tree_init(ed, e, c);
Decision_Tree_Insert.decision_tree_prompt_and_insert('text');
}
</script>
<?php
}
}
/**
* This Adds the meta-box, i.e. the form area to the Decision Tree Custom Post Type Amdin Page
*/
static function add_metabox(){
add_meta_box('decision_tree_metabox', 'Tree Box', array('DecisionTree', 'print_metabox_form'),
sidecar_app_config::$post_type, 'normal');
}
/**
* TODO: Take the saved data from the serialized blob in the DB and output it to the page as JSON(?)
* This prints out the form on the Decision Tree Admin Page
*/
static function print_metabox_form($post){
$donated = self::get_donation_status();
//the JS included will place the form in this div
print "
<div id='dtree_entry_block_errors' >Oopsie!
<div id='dtree_entry_block_errors_hide'>X</div>
<ul id='dtree_entry_block_error_msg' /></ul>
</div>
<div id='dtree_entry_preview_outer' >
<input type='submit' name='dt_preview_button' id='dt_preview_button' class='button
button-primary button-large' value='Preview Decision Tree' >
<div id='dtree_entry_preview_inner' ></div>
</div>
<div id='dtree_entry_block'></div>
<input type='hidden' name='dtree_blob' id='dtree_blob' value=''/>
<input type='button' class='button decision_tree_add_step' value='Add Step'>
<br>
<script type='text/javascript'>
jQuery(document).ready(function($) {
DTree_field_set = new DTree('".$donated."');
DTree_field_set.initializeForm();
});
</script>
";
// attempt to get blog from DB and print it if we got it
$dtree_blob = get_post_meta($post->ID, 'dtree_blob', true);
if (!empty($dtree_blob)){
print "
<script type='text/javascript'>
raw_DB_value = '$dtree_blob';
var DTree_object_restored = JSON.parse(raw_DB_value);
</script>
";
}
print self::get_html_comment_shoutout();
}
/**
* save the DT JSON object to the DB
* @param type $id id of the post as provided by wp
*/
static function save_metabox($id){
if (isset($_POST['dtree_blob'])){
update_post_meta( $id, 'dtree_blob', wp_slash($_POST['dtree_blob']));
}
}
/**
* TODO:
* This returns the decision tree contents to the front end to be rendered and displayed to the user
*/
static function decision_tree_shortcode( $atts ) {
//add_action( 'wp_enqueue_scripts', array('DecisionTree', 'load_scripts') );
self::load_scripts();
extract( shortcode_atts( array(
'id' => '0',
), $atts ) );
$donated = self::get_donation_status();
$dtree_blob = get_post_meta($atts['id'], 'dtree_blob', true);
$dtree_post = get_post($atts['id']);
$dt_id = $atts['id'];
$dt_title = addslashes($dtree_post->post_title);
//output the JSON DT Blob and have JS on DOM ready turn it into an object
//then on the client side call process on the first question
$output = "<script type='text/javascript'>";
$output.="jQuery(document).ready(function($) {\n";
$output.= " dt_tree_$dt_id = jQuery.parseJSON('$dtree_blob'); \n";
$output.= " if (dt_tree_$dt_id != undefined){ \n";
$output.= " dt_tree_$dt_id.donated = '$donated'; \n";
$output.= " dt_tree_$dt_id.title = '$dt_title'; \n";
$output.= " process_question($dt_id);\n";
$output.= " } \n";
$output.= "});\n";
$output.= "</script>\n";
$output.= "<form id='decision_tree_area_".$atts['id']."' class='decision_tree_area'></form>\n";
return $output.self::get_html_comment_shoutout();
}
/**
* returns a list of the decision trees via ajax
*/
static function list_trees() {
$args = array( 'post_type' => sidecar_app_config::$post_type, 'orderby'=> 'title', 'posts_per_page'=>-1);
$myposts = get_posts( $args );
foreach($myposts as $post){
$option_list[$post->ID]=$post->post_title;
}
print json_encode($option_list);
die(); // this is required to return a proper result
}
/*
* Load scripts required for the visitor facing side
*/
static function load_scripts(){
wp_enqueue_script('decision_tree_js',
plugins_url( '/decision_tree.js' , __FILE__ ),
array('jquery', 'jquery-ui-core'));
}
/**
* Loads scripts into the admin head
*/
static function load_admin_scripts_head(){
//this script should only load on the DT edit/add page so make sure that is where we are
$screen_obj = get_current_screen();
if($screen_obj->id ==sidecar_app_config::$post_type && $screen_obj->post_type ==sidecar_app_config::$post_type){
wp_enqueue_script('decision_tree_admin_js',
plugins_url( '/decision_tree_admin.js' , __FILE__ ),
array('jquery', 'jquery-ui-core', 'jquery-ui-dialog'));
}
}
/*
* Load scripts required for the admin facing side
*/
static function load_admin_scripts(){
wp_enqueue_script('decision_tree_js',
plugins_url( '/decision_tree.js' , __FILE__ ),
array('jquery', 'jquery-ui-core', 'jquery-ui-dialog'));
wp_enqueue_script('decision_tree_insert_tag_js',
plugins_url( '/decision_tree_insert_tag.js' , __FILE__ ),
array('jquery', 'jquery-ui-core', 'jquery-ui-dialog'));
wp_enqueue_style( 'decision_tree_admin_style',
plugins_url( 'decision_tree_admin.css' , __FILE__ ) , false );
//admin css for pre 3.8 use
if($GLOBALS['wp_version'] < 3.8){
wp_enqueue_style( 'decision_tree_admin_pre38_style',
plugins_url( 'decision_tree_admin_pre38.css' , __FILE__ ) , false );
}
wp_enqueue_style( 'decision_tree_jquery_ui',
'//code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css', false );
wp_enqueue_style( 'prefix-font-awesome',
'//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css',
array(), '4.0.3' );
wp_enqueue_style( 'decisiontree_local_style',
plugins_url( 'decisiontree_local_style.css' , __FILE__ ) , false );
add_settings_section(
'dt_setting_section',
'',
array('DecisionTree', 'dt_setting_section_callback_function'),
'dt_options_group'
);
add_settings_field(
'dt_idonated_setting',
'I donated! Hide Public Attribution',
array('DecisionTree', 'dt_idonated_setting_callback_function'),
'dt_options_group',
'dt_setting_section'
);
//register option page
register_setting( 'dt_options_group', 'dt_idonated_setting');
//tracking options
add_settings_section(
'dt_track_setting_section',
'',
array('DecisionTree', 'dt_setting_section_callback_function'),
'dt_track_options_group'
);
add_settings_field(
'dt_track_setting',
'Please Track Me Nicely',
array('DecisionTree', 'dt_track_setting_callback_function'),
'dt_track_options_group',
'dt_track_setting_section'
);
//register option page
register_setting( 'dt_track_options_group', 'dt_track_setting');
}
static function dt_setting_section_callback_function() {
//echo '<p>Click here if you donated and want to turn off the DecisionTree attribution:</p>';
}
static function dt_idonated_setting_callback_function() {
echo '<input name="dt_idonated_setting" id="gv_thumbnails_insert_into_excerpt"
type="checkbox" value="1" class="code" '
.checked( 1, get_option( 'dt_idonated_setting' ), false ) . ' />';
}
static function dt_track_setting_callback_function() {
echo '<input name="dt_track_setting" id="gv_thumbnails_insert_into_excerpt"
type="checkbox" value="1" class="code" '
.checked( 1, get_option( 'dt_track_setting' ), false ) . ' />';
}
/*
* Load styles
*/
static function load_styles(){
wp_enqueue_style( 'decision_tree_jquery_ui',
'//code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css', false );
wp_enqueue_style( 'decisiontree_local_style',
plugins_url( 'decisiontree_local_style.css' , __FILE__ ) , false );
wp_enqueue_style( 'prefix-font-awesome',
'//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css', array(), '4.0.3' );
}
static function register_menu_items(){
if($GLOBALS['wp_version'] >= 3.8){
$menu_icon = "dashicons-networking";
}
else{
$menu_icon = "none";
}
add_menu_page('Decision Trees', 'Decision Trees', 'edit_posts',
self::get_plugin_path().'/decision_tree_dashboard.php', '', $menu_icon, 25.123);
//add the dashboard page
add_submenu_page(self::get_plugin_path().'/decision_tree_dashboard.php', 'Dashboard',
'Dashboard', 'edit_posts', self::get_plugin_path().'/decision_tree_dashboard.php' );
//add the add new page
add_submenu_page(self::get_plugin_path().'/decision_tree_dashboard.php', 'Add New',
'Add New', 'edit_posts', 'post-new.php?post_type=decision_tree' );
//add the instructions page
add_submenu_page(self::get_plugin_path().'/decision_tree_dashboard.php', 'Instructions',
'Instructions', 'edit_posts', self::get_plugin_path().'/decision_tree_instructions.php' );
}
/**
* light wrapper to get donation setting from DB.
* @return string of donation status of "1" or "0"
*/
static function get_donation_status(){
$status = get_option('dt_idonated_setting');
// make sure we have a "0" or a "1"
if ( $status !== '1'){
$status = '0';
}
return $status;
}
/**
* light wrapper to get track setting from DB.
* @return string of track status of "1" or "0"
*/
static function get_track_status(){
$status = get_option('dt_track_setting');
// make sure we have a "0" or a "1"
if ( $status !== '1'){
$status = '0';
}
return $status;
}
static function get_html_comment_shoutout(){
return "
<!--
__ __ __ __ __ __ __ __
.--| .-----.----|__.-----|__.-----.-----. | |_.----.-----.-----. | |--.--.--. .-----|__.--| .-----.----.---.-.----. | |_.--.--.
| _ | -__| __| |__ --| | _ | | | _| _| -__| -__| | _ | | | |__ --| | _ | -__| __| _ | ___| _| | |
|_____|_____|____|__|_____|__|_____|__|__| |____|__| |_____|_____| |_____|___ | |_____|__|_____|_____|____|___._|__||__|____||___/
|_____|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
||||This is decision tree! A plugin for WordPress. Find out more at http://sidecar.tv/decision_tree||||
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| -->";
}
/**
* Runs on activation of the plugin in the UL
*/
static function on_activation() {
if(!get_option('install_UUID')){
update_option('install_UUID', self::generate_UUID() );
}
sidecar_track::track_event('installation', 'activate');
}
/**
* Runs on delete of the plugin from the UI
*/
static function on_uninstall(){
sidecar_track::track_event('installation', 'uninstall');
}
/**
* Runs on deativation of the plugin
*/
static function on_deactivate(){
sidecar_track::track_event('installation', 'deactivate');
}
/**
* Runs on change to the i donated setting
*/
static function on_update_idonated($new_val, $old_val){
if($new_val=='1'){
sidecar_track::track_event('donation_attribution', 'deactivated');
}
if($new_val=='0'){
sidecar_track::track_event('donation_attribution', 'activated');
}
return $new_val;
}
/**
* generate a UUID for this installation of the app
* @via Andrew Moore http://www.php.net/manual/en/function.uniqid.php#94959
*/
static function generate_UUID() {
return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
// 32 bits for "time_low"
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
// 16 bits for "time_mid"
mt_rand( 0, 0xffff ),
// 16 bits for "time_hi_and_version",
// four most significant bits holds version number 4
mt_rand( 0, 0x0fff ) | 0x4000,
// 16 bits, 8 bits for "clk_seq_hi_res",
// 8 bits for "clk_seq_low",
// two most significant bits holds zero and one for variant DCE1.1
mt_rand( 0, 0x3fff ) | 0x8000,
// 48 bits for "node"
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
);
}
/**
* Set the decision tree columns
*/
static function list_dt_list_columns( $defaults ) {
$defaults['author'] = 'Added By';
return $defaults;
}
/**
* Runs on admin page view, currently just fires for listing of all trees
*/
static function admin_page_notice(){
$screen = get_current_screen();
if( sidecar_app_config::$post_type== $screen->post_type
&& 'edit' == $screen->base ){
$post_count = wp_count_posts(sidecar_app_config::$post_type);
sidecar_track::track_event('list_tree', 'count', 'published', $post_count->publish);
}
}
static function get_plugin_path(){
return dirname(plugin_basename(__FILE__));
}
}
add_action('admin_menu', array('DecisionTree', 'register_menu_items'), 8);
add_action('init', array('DecisionTree', 'create_post_type'));
add_shortcode( 'decisiontree', array('DecisionTree', 'decision_tree_shortcode'));
add_action( 'wp_enqueue_scripts', array('DecisionTree', 'load_styles') );
add_action( 'admin_init', array('DecisionTree', 'load_admin_scripts') );
add_action( 'admin_head', array('DecisionTree', 'load_admin_scripts_head') );
add_action( 'save_post', array('DecisionTree', 'save_metabox') );
add_filter( 'mce_external_plugins', array('DecisionTree', 'add_buttons') );
add_filter( 'mce_buttons', array('DecisionTree', 'register_buttons') );
add_action( 'admin_print_footer_scripts', array('DecisionTree','add_quicktags' ));
add_action( 'wp_ajax_decision_tree_list_trees', array('DecisionTree', 'list_trees'));
register_activation_hook( __FILE__, array( 'DecisionTree', 'on_activation' ) );
register_uninstall_hook( __FILE__, array( 'DecisionTree', 'on_uninstall' ) );
register_deactivation_hook( __FILE__, array( 'DecisionTree', 'on_deactivate' ) );
add_filter( 'pre_update_option_dt_idonated_setting', array( 'DecisionTree', 'on_update_idonated' ) );
add_filter('manage_decision_tree_posts_columns', array( 'DecisionTree', 'list_dt_list_columns' ));
add_action( 'admin_notices', array( 'DecisionTree', 'admin_page_notice' ));
?>