| 1 |
<?php |
| 2 |
/** |
| 3 |
* Functions |
| 4 |
* |
| 5 |
* @package GamiPress\Asgaros_Forum\Functions |
| 6 |
* @since 1.0.0 |
| 7 |
*/ |
| 8 |
// Exit if accessed directly |
| 9 |
if( !defined( 'ABSPATH' ) ) exit; |
| 10 |
|
| 11 |
/** |
| 12 |
* Overrides GamiPress AJAX Helper for selecting posts |
| 13 |
* |
| 14 |
* @since 1.0.0 |
| 15 |
*/ |
| 16 |
function gamipress_asgaros_forum_ajax_get_posts() { |
| 17 |
// Security check, forces to die if not security passed |
| 18 |
check_ajax_referer( 'gamipress_admin', 'nonce' ); |
| 19 |
global $wpdb; |
| 20 |
|
| 21 |
$results = array(); |
| 22 |
|
| 23 |
// Pull back the search string |
| 24 |
$search = isset( $_REQUEST['q'] ) ? sanitize_text_field( $_REQUEST['q'] ) : ''; |
| 25 |
$search = $wpdb->esc_like( $search ); |
| 26 |
|
| 27 |
if( isset( $_REQUEST['post_type'] ) && in_array( 'asgaros_forum', $_REQUEST['post_type'] ) ) { |
| 28 |
|
| 29 |
// Get the forums |
| 30 |
$forums = $wpdb->get_results( $wpdb->prepare( |
| 31 |
"SELECT a.id, a.name |
| 32 |
FROM {$wpdb->prefix}forum_forums AS a |
| 33 |
WHERE a.name LIKE %s", |
| 34 |
"%%{$search}%%" |
| 35 |
) ); |
| 36 |
|
| 37 |
foreach ( $forums as $forum ) { |
| 38 |
|
| 39 |
// Results should meet same structure like posts |
| 40 |
$results[] = array( |
| 41 |
'ID' => $forum->id, |
| 42 |
'post_title' => $forum->name, |
| 43 |
); |
| 44 |
|
| 45 |
} |
| 46 |
|
| 47 |
// Return our results |
| 48 |
wp_send_json_success( $results ); |
| 49 |
die; |
| 50 |
|
| 51 |
} else if( isset( $_REQUEST['post_type'] ) && in_array( 'asgaros_topic', $_REQUEST['post_type'] ) ) { |
| 52 |
|
| 53 |
// Get the topics |
| 54 |
$topics = $wpdb->get_results( $wpdb->prepare( |
| 55 |
"SELECT a.id, a.name, a.parent_id |
| 56 |
FROM {$wpdb->prefix}forum_topics AS a |
| 57 |
WHERE a.name LIKE %s", |
| 58 |
"%%{$search}%%" |
| 59 |
) ); |
| 60 |
|
| 61 |
foreach ( $topics as $topic ) { |
| 62 |
|
| 63 |
// Results should meet same structure like posts |
| 64 |
$results[] = array( |
| 65 |
'ID' => $topic->id, |
| 66 |
'post_title' => $topic->name, |
| 67 |
'post_type' => gamipress_asgaros_forum_get_forum_title( $topic->parent_id ), |
| 68 |
); |
| 69 |
|
| 70 |
} |
| 71 |
|
| 72 |
$response = array( |
| 73 |
'results' => $results, |
| 74 |
'more_results' => false, |
| 75 |
); |
| 76 |
|
| 77 |
// Return our results |
| 78 |
wp_send_json_success( $results ); |
| 79 |
die; |
| 80 |
} |
| 81 |
|
| 82 |
} |
| 83 |
add_action( 'wp_ajax_gamipress_asgaros_forum_get_posts', 'gamipress_asgaros_forum_ajax_get_posts', 5 ); |
| 84 |
|
| 85 |
|
| 86 |
function gamipress_asgaros_forum_forum_options(){ |
| 87 |
|
| 88 |
} |
| 89 |
|
| 90 |
// Helper function to get the forum title |
| 91 |
function gamipress_asgaros_forum_get_forum_title( $forum_id ) { |
| 92 |
|
| 93 |
global $wpdb; |
| 94 |
|
| 95 |
// Empty title if no ID provided |
| 96 |
if( absint( $forum_id ) === 0 ) { |
| 97 |
return ''; |
| 98 |
} |
| 99 |
|
| 100 |
$forum_name = $wpdb->get_var( $wpdb->prepare( |
| 101 |
"SELECT name FROM {$wpdb->prefix}forum_forums WHERE id=%d", |
| 102 |
$forum_id ) ); |
| 103 |
|
| 104 |
return $forum_name; |
| 105 |
|
| 106 |
} |
| 107 |
|
| 108 |
// Helper function to get the topic title |
| 109 |
function gamipress_asgaros_forum_get_topic_title( $topic_id ) { |
| 110 |
|
| 111 |
global $wpdb; |
| 112 |
|
| 113 |
// Empty title if no ID provided |
| 114 |
if( absint( $topic_id ) === 0 ) { |
| 115 |
return ''; |
| 116 |
} |
| 117 |
|
| 118 |
$topic_name = $wpdb->get_var( $wpdb->prepare( |
| 119 |
"SELECT name FROM {$wpdb->prefix}forum_topics WHERE id=%d", |
| 120 |
$topic_id ) ); |
| 121 |
|
| 122 |
return $topic_name; |
| 123 |
|
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Get forum by topic |
| 128 |
* |
| 129 |
* @since 1.0.0 |
| 130 |
* |
| 131 |
* @return int|false |
| 132 |
*/ |
| 133 |
function gamipress_asgaros_forum_get_forum_by_topic( $topic_id ) { |
| 134 |
|
| 135 |
global $wpdb; |
| 136 |
// Empty if no ID provided |
| 137 |
if( absint( $topic_id ) === 0 ) { |
| 138 |
return ''; |
| 139 |
} |
| 140 |
|
| 141 |
$forum_id = $wpdb->get_var( $wpdb->prepare( |
| 142 |
"SELECT parent_id FROM {$wpdb->prefix}forum_topics WHERE id=%d", |
| 143 |
$topic_id ) ); |
| 144 |
|
| 145 |
return $forum_id; |
| 146 |
|
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Get topic and forum by post |
| 151 |
* |
| 152 |
* @since 1.0.0 |
| 153 |
* |
| 154 |
* @return int|false |
| 155 |
*/ |
| 156 |
function gamipress_asgaros_forum_get_topic_and_forum_by_post( $post_id ) { |
| 157 |
|
| 158 |
global $wpdb; |
| 159 |
// Empty if no ID provided |
| 160 |
if( absint( $post_id ) === 0 ) { |
| 161 |
return ''; |
| 162 |
} |
| 163 |
|
| 164 |
$data_post = array(); |
| 165 |
|
| 166 |
$results = $wpdb->get_results( $wpdb->prepare( |
| 167 |
"SELECT parent_id, forum_id FROM {$wpdb->prefix}forum_posts WHERE id=%d", |
| 168 |
$post_id ) ); |
| 169 |
|
| 170 |
foreach ( $results as $result ){ |
| 171 |
|
| 172 |
$data_post[] = array( |
| 173 |
'topic_id' => $result->parent_id, |
| 174 |
'forum_id' => $result->forum_id, |
| 175 |
); |
| 176 |
|
| 177 |
} |
| 178 |
|
| 179 |
return $data_post; |
| 180 |
|
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Get post author |
| 185 |
* |
| 186 |
* @since 1.0.0 |
| 187 |
* |
| 188 |
* @return int|false |
| 189 |
*/ |
| 190 |
function gamipress_asgaros_forum_get_post_author( $post_id ) { |
| 191 |
|
| 192 |
global $wpdb; |
| 193 |
// Empty if no ID provided |
| 194 |
if( absint( $post_id ) === 0 ) { |
| 195 |
return ''; |
| 196 |
} |
| 197 |
|
| 198 |
$author_id = $wpdb->get_var( $wpdb->prepare( |
| 199 |
"SELECT author_id FROM {$wpdb->prefix}forum_posts WHERE id=%d", |
| 200 |
$post_id ) ); |
| 201 |
|
| 202 |
return $author_id; |
| 203 |
|
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Get topic author |
| 208 |
* |
| 209 |
* @since 1.0.0 |
| 210 |
* |
| 211 |
* @return int|false |
| 212 |
*/ |
| 213 |
function gamipress_asgaros_forum_get_topic_author( $topic_id ) { |
| 214 |
|
| 215 |
global $wpdb; |
| 216 |
// Empty if no ID provided |
| 217 |
if( absint( $topic_id ) === 0 ) { |
| 218 |
return ''; |
| 219 |
} |
| 220 |
|
| 221 |
$author_id = $wpdb->get_var( $wpdb->prepare( |
| 222 |
"SELECT author_id FROM {$wpdb->prefix}forum_topics WHERE id=%d", |
| 223 |
$topic_id ) ); |
| 224 |
|
| 225 |
return $author_id; |
| 226 |
|
| 227 |
} |