| 1 |
<?php |
| 2 |
/** |
| 3 |
* Functions |
| 4 |
* |
| 5 |
* @package AutomatorWP\Asgaros_Forum\Functions |
| 6 |
* @since 1.0.0 |
| 7 |
*/ |
| 8 |
// Exit if accessed directly |
| 9 |
if( !defined( 'ABSPATH' ) ) exit; |
| 10 |
|
| 11 |
/** |
| 12 |
* Options callback for select2 fields assigned to forums |
| 13 |
* |
| 14 |
* @since 1.0.0 |
| 15 |
* |
| 16 |
* @param stdClass $field |
| 17 |
* |
| 18 |
* @return array |
| 19 |
*/ |
| 20 |
function automatorwp_asgaros_forum_options_cb_forum( $field ) { |
| 21 |
|
| 22 |
// Setup vars |
| 23 |
$value = $field->escaped_value; |
| 24 |
$none_value = 'any'; |
| 25 |
$none_label = __( 'any forum', 'automatorwp-asgaros-forum' ); |
| 26 |
$options = automatorwp_options_cb_none_option( $field, $none_value, $none_label ); |
| 27 |
|
| 28 |
if( ! empty( $value ) ) { |
| 29 |
if( ! is_array( $value ) ) { |
| 30 |
$value = array( $value ); |
| 31 |
} |
| 32 |
|
| 33 |
foreach( $value as $forum_id ) { |
| 34 |
|
| 35 |
// Skip option none |
| 36 |
if( $forum_id === $none_value ) { |
| 37 |
continue; |
| 38 |
} |
| 39 |
|
| 40 |
$options[$forum_id] = automatorwp_asgaros_forum_get_forum_title( $forum_id ); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
return $options; |
| 45 |
|
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Get forums |
| 50 |
* |
| 51 |
* @since 1.0.0 |
| 52 |
* |
| 53 |
* @return array|false |
| 54 |
*/ |
| 55 |
function automatorwp_asgaros_forum_get_forums() { |
| 56 |
|
| 57 |
global $wpdb; |
| 58 |
$forums = array(); |
| 59 |
|
| 60 |
$results = $wpdb->get_results( "SELECT id, name FROM {$wpdb->prefix}forum_forums" ); |
| 61 |
|
| 62 |
foreach ( $results as $forum ){ |
| 63 |
|
| 64 |
$forums[] = array( |
| 65 |
'id' => $forum->id, |
| 66 |
'name' => $forum->name, |
| 67 |
); |
| 68 |
|
| 69 |
} |
| 70 |
|
| 71 |
return $forums; |
| 72 |
|
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Get the forum title |
| 77 |
* |
| 78 |
* @since 1.0.0 |
| 79 |
* |
| 80 |
* @param int $forum_id |
| 81 |
* |
| 82 |
* @return string|null |
| 83 |
*/ |
| 84 |
function automatorwp_asgaros_forum_get_forum_title( $forum_id ) { |
| 85 |
|
| 86 |
global $wpdb; |
| 87 |
|
| 88 |
// Empty title if no ID provided |
| 89 |
if( absint( $forum_id ) === 0 ) { |
| 90 |
return ''; |
| 91 |
} |
| 92 |
|
| 93 |
$forum_name = $wpdb->get_var( $wpdb->prepare( |
| 94 |
"SELECT name FROM {$wpdb->prefix}forum_forums WHERE id=%d", |
| 95 |
$forum_id ) ); |
| 96 |
|
| 97 |
return $forum_name; |
| 98 |
|
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Options callback for select2 fields assigned to topic |
| 103 |
* |
| 104 |
* @since 1.0.0 |
| 105 |
* |
| 106 |
* @param stdClass $field |
| 107 |
* |
| 108 |
* @return array |
| 109 |
*/ |
| 110 |
function automatorwp_asgaros_forum_options_cb_topic( $field ) { |
| 111 |
|
| 112 |
// Setup vars |
| 113 |
$value = $field->escaped_value; |
| 114 |
$none_value = 'any'; |
| 115 |
$none_label = __( 'any topic', 'automatorwp-asgaros-forum' ); |
| 116 |
$options = automatorwp_options_cb_none_option( $field, $none_value, $none_label ); |
| 117 |
|
| 118 |
if( ! empty( $value ) ) { |
| 119 |
if( ! is_array( $value ) ) { |
| 120 |
$value = array( $value ); |
| 121 |
} |
| 122 |
|
| 123 |
foreach( $value as $topic_id ) { |
| 124 |
|
| 125 |
// Skip option none |
| 126 |
if( $topic_id === $none_value ) { |
| 127 |
continue; |
| 128 |
} |
| 129 |
|
| 130 |
$options[$topic_id] = automatorwp_asgaros_forum_get_topic_title( $topic_id ); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
return $options; |
| 135 |
|
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Get topics |
| 140 |
* |
| 141 |
* @since 1.0.0 |
| 142 |
* |
| 143 |
* @return array|false |
| 144 |
*/ |
| 145 |
function automatorwp_asgaros_forum_get_topics( ) { |
| 146 |
|
| 147 |
global $wpdb; |
| 148 |
$topics = array(); |
| 149 |
|
| 150 |
$results = $wpdb->get_results( "SELECT id, name FROM {$wpdb->prefix}forum_topics" ); |
| 151 |
|
| 152 |
foreach ( $results as $topic ){ |
| 153 |
|
| 154 |
$topics[] = array( |
| 155 |
'id' => $topic->id, |
| 156 |
'name' => $topic->name, |
| 157 |
); |
| 158 |
|
| 159 |
} |
| 160 |
|
| 161 |
return $topics; |
| 162 |
|
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Get the forum title |
| 167 |
* |
| 168 |
* @since 1.0.0 |
| 169 |
* |
| 170 |
* @param int $forum_id |
| 171 |
* |
| 172 |
* @return string|null |
| 173 |
*/ |
| 174 |
function automatorwp_asgaros_forum_get_topic_title( $topic_id ) { |
| 175 |
|
| 176 |
global $wpdb; |
| 177 |
|
| 178 |
// Empty title if no ID provided |
| 179 |
if( absint( $topic_id ) === 0 ) { |
| 180 |
return ''; |
| 181 |
} |
| 182 |
|
| 183 |
$topic_name = $wpdb->get_var( $wpdb->prepare( |
| 184 |
"SELECT name FROM {$wpdb->prefix}forum_topics WHERE id=%d", |
| 185 |
$topic_id ) ); |
| 186 |
|
| 187 |
return $topic_name; |
| 188 |
|
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Get forum by topic |
| 193 |
* |
| 194 |
* @since 1.0.0 |
| 195 |
* |
| 196 |
* @return int|false |
| 197 |
*/ |
| 198 |
function automatorwp_asgaros_forum_get_forum_by_topic( $topic_id ) { |
| 199 |
|
| 200 |
global $wpdb; |
| 201 |
// Empty if no ID provided |
| 202 |
if( absint( $topic_id ) === 0 ) { |
| 203 |
return ''; |
| 204 |
} |
| 205 |
|
| 206 |
$forum_id = $wpdb->get_var( $wpdb->prepare( |
| 207 |
"SELECT parent_id FROM {$wpdb->prefix}forum_topics WHERE id=%d", |
| 208 |
$topic_id ) ); |
| 209 |
|
| 210 |
return $forum_id; |
| 211 |
|
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* Get topic by post |
| 216 |
* |
| 217 |
* @since 1.0.0 |
| 218 |
* |
| 219 |
* @return int|false |
| 220 |
*/ |
| 221 |
function automatorwp_asgaros_forum_get_topic_and_forum_by_post( $post_id ) { |
| 222 |
|
| 223 |
global $wpdb; |
| 224 |
// Empty if no ID provided |
| 225 |
if( absint( $post_id ) === 0 ) { |
| 226 |
return ''; |
| 227 |
} |
| 228 |
|
| 229 |
$data_post = array(); |
| 230 |
|
| 231 |
$results = $wpdb->get_results( $wpdb->prepare( |
| 232 |
"SELECT parent_id, forum_id FROM {$wpdb->prefix}forum_posts WHERE id=%d", |
| 233 |
$post_id ) ); |
| 234 |
|
| 235 |
foreach ( $results as $result ){ |
| 236 |
|
| 237 |
$data_post[] = array( |
| 238 |
'topic_id' => $result->parent_id, |
| 239 |
'forum_id' => $result->forum_id, |
| 240 |
); |
| 241 |
|
| 242 |
} |
| 243 |
|
| 244 |
return $data_post; |
| 245 |
|
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
* Get post author |
| 250 |
* |
| 251 |
* @since 1.0.0 |
| 252 |
* |
| 253 |
* @return int|false |
| 254 |
*/ |
| 255 |
function automatorwp_asgaros_forum_get_post_author( $post_id ) { |
| 256 |
|
| 257 |
global $wpdb; |
| 258 |
// Empty if no ID provided |
| 259 |
if( absint( $post_id ) === 0 ) { |
| 260 |
return ''; |
| 261 |
} |
| 262 |
|
| 263 |
$data_post = array(); |
| 264 |
|
| 265 |
$author_id = $wpdb->get_var( $wpdb->prepare( |
| 266 |
"SELECT author_id FROM {$wpdb->prefix}forum_posts WHERE id=%d", |
| 267 |
$post_id ) ); |
| 268 |
|
| 269 |
return $author_id; |
| 270 |
|
| 271 |
} |