-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbbpress-wp-support.php
More file actions
154 lines (141 loc) · 4.91 KB
/
bbpress-wp-support.php
File metadata and controls
154 lines (141 loc) · 4.91 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
<?php
/**
* Plugin Name: Wordpress BB Press Support
* Description: Add support system for Wordpress BBpress support
* Version: 1.0
* Author: Treenity
* Author URI: http://www.treenit-web.fr
* License: GNU
*
* @package bbpress-wp-support
*/
namespace treenity\wordpress_bbsupport;
define( 'PLUGIN_TEXT_DOMAIN', 'bbpress-wp-support' );
require_once ABSPATH . 'wp-admin/includes/plugin.php';
if ( ! is_plugin_active( 'bbpress/bbpress.php' ) ) {
return;
}
/**
* Add our form.
*
* @todo: Display only if we are on the support forum
*/
function add_support_form() {
wp_enqueue_style( 'bbpress-wp-support', plugin_dir_url( __FILE__ ) . 'assets/css/global.css' );
wp_enqueue_script( 'bbpress-wp-support', plugin_dir_url( __FILE__ ) . 'assets/js/global.min.js', array( 'jquery' ), false, true );
?>
<div id="bbpcs">
<div class="bbpcs__container">
<div class="bbpcs__header">
<div class="bbpcs__header__title">
Support system
</div>
</div>
<?php include_once( 'parts/bbpcs-form.php' ) ?>
<?php include_once( 'parts/ppbcs-summary.php' ) ?>
</div>
</div>
<?php
}
add_action( 'bbp_theme_before_topic_form', __NAMESPACE__ . '\\add_support_form' );
/**
* Save custom fields values
*
* @param int $post_id The current post ID.
*
* @return int $post_id The post_ID.
*/
function save_support_params( $post_id ) {
if ( ! isset( $_POST['support'] ) || is_admin() ) {
return $post_id;
}
// Remove Parser field, we don't need to save it
unset( $_POST['support']['parser'] );
foreach ( $_POST['support'] as $support_field_key => $support_field_value ) {
if ( ! empty( $support_field_value ) ) {
switch ( $support_field_key ) {
case 'wp_version':
if ( ! $parent = get_term_by( 'name', 'Wordpress', 'topic-tag' ) ) {
$parent = wp_insert_term( 'Wordpress', 'topic-tag' );
$parent_id = $parent['term_id'];
} else {
$parent_id = $parent->term_id;
}
$term_id = wp_set_object_terms( $post_id, 'WP ' . $support_field_value, 'topic-tag', true );
wp_update_term( $term_id[0], 'topic-tag', array( 'parent' => $parent_id ) );
break;
case 'php_version':
if ( ! $parent = get_term_by( 'name', 'PHP', 'topic-tag' ) ) {
$parent = wp_insert_term( 'PHP', 'topic-tag' );
$parent_id = $parent['term_id'];
} else {
$parent_id = $parent->term_id;
}
$term_id = wp_set_object_terms( $post_id, 'PHP ' . substr( $support_field_value, 0, 3 ), 'topic-tag', true );
wp_update_term( $term_id[0], 'topic-tag', array( 'parent' => $parent_id ) );
break;
case 'mysql_version':
if ( ! $parent = get_term_by( 'name', 'Mysql', 'topic-tag' ) ) {
$parent = wp_insert_term( 'Mysql', 'topic-tag' );
$parent_id = $parent['term_id'];
} else {
$parent_id = $parent->term_id;
}
$term_id = wp_set_object_terms( $post_id, 'Mysql ' . substr( $support_field_value, 0, 3 ), 'topic-tag', true );
wp_update_term( $term_id[0], 'topic-tag', array( 'parent' => $parent_id ) );
break;
case 'theme_name':
if ( ! $parent = get_term_by( 'name', 'Themes', 'topic-tag' ) ) {
$parent = wp_insert_term( 'Themes', 'topic-tag' );
$parent_id = $parent['term_id'];
} else {
$parent_id = $parent->term_id;
}
$term_id = wp_set_object_terms( $post_id, $support_field_value, 'topic-tag', true );
wp_update_term( $term_id[0], 'topic-tag', array( 'parent' => $parent_id ) );
break;
case 'plugins':
if ( ! $parent = get_term_by( 'name', 'Plugins', 'topic-tag' ) ) {
$parent = wp_insert_term( 'Plugins', 'topic-tag' );
$parent_id = $parent['term_id'];
} else {
$parent_id = $parent->term_id;
}
$plugins = explode( ',', $support_field_value );
foreach ( $plugins as $plugin ) {
$term_id = wp_set_object_terms( $post_id, $plugin, 'topic-tag', true );
wp_update_term( $term_id[0], 'topic-tag', array( 'parent' => $parent_id ) );
}
break;
}
update_post_meta( $post_id, 'bbpcs_support_' . $support_field_key, $support_field_value );
} else {
delete_post_meta( $post_id, 'bbpcs_support_' . $support_field_key );
}
}
return $post_id;
}
add_action( 'save_post_topic', __NAMESPACE__ . '\\save_support_params' );
/**
* Allow BBP topic-tag taxonomy to have childrens
*
* @param array $taxonomy The bbp topic-tag taxonomy.
*
* @return mixed
*/
function add_taxonomy_childs( $taxonomy ) {
$taxonomy['hierarchical'] = true;
return $taxonomy;
}
add_filter( 'bbp_register_topic_taxonomy', __NAMESPACE__ . '\\add_taxonomy_childs' );
/**
* Add bbpcs_alert_empty_form to global JS variables.
*/
function add_js_variables() {
?>
<script type="text/javascript">
var bbpcs_alert_empty_form = '<?php _ex( 'You have to give us some Support informations before we can help you.<br/>Please fill the support form as much as you can.', 'alert', PLUGIN_TEXT_DOMAIN ) ?>';
</script>
<?php
}
add_action( 'wp_head', __NAMESPACE__ . '\\add_js_variables' );