| 1 |
<?php |
| 2 |
/** |
| 3 |
* Functions |
| 4 |
* |
| 5 |
* @package GamiPress\wpForo\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_wpforo_ajax_get_posts() { |
| 17 |
|
| 18 |
// Security check, forces to die if not security passed |
| 19 |
check_ajax_referer( 'gamipress_admin', 'nonce' ); |
| 20 |
|
| 21 |
// Check if user can manage GamiPress |
| 22 |
if( ! current_user_can( gamipress_get_manager_capability() ) ) { |
| 23 |
wp_send_json_error( __( 'You\'re not allowed to perform this action.', 'gamipress' ) ); |
| 24 |
} |
| 25 |
|
| 26 |
global $wpdb; |
| 27 |
|
| 28 |
if( isset( $_REQUEST['post_type'] ) ) { |
| 29 |
|
| 30 |
// Get the user input |
| 31 |
$search = isset( $_REQUEST['q'] ) ? $wpdb->esc_like( sanitize_text_field( $_REQUEST['q'] ) ) : ''; |
| 32 |
|
| 33 |
if( in_array( 'wpforo_forum', $_REQUEST['post_type'] ) ) { |
| 34 |
// Forums |
| 35 |
|
| 36 |
$table = WPF()->tables->forums; |
| 37 |
$boards = WPF()->tables->boards; |
| 38 |
|
| 39 |
// Get the boards |
| 40 |
$results_boards = $wpdb->get_results( $wpdb->prepare( |
| 41 |
"SELECT boardid FROM {$boards}" |
| 42 |
) ); |
| 43 |
|
| 44 |
// Try to find the forums |
| 45 |
if ( ! empty( $search ) ) { |
| 46 |
$forums = $wpdb->get_results( $wpdb->prepare( |
| 47 |
"SELECT * FROM {$table} WHERE ( title LIKE %s OR title LIKE %s )", |
| 48 |
"%%{$search}%%", |
| 49 |
"{$search}%%" |
| 50 |
) ); |
| 51 |
} else { |
| 52 |
$forums = $wpdb->get_results( "SELECT * FROM {$table}" ); |
| 53 |
} |
| 54 |
|
| 55 |
// Build the results array |
| 56 |
$results = array(); |
| 57 |
|
| 58 |
foreach ( $forums as $forum ) { |
| 59 |
|
| 60 |
// Results should meet same structure like posts |
| 61 |
$results[] = array( |
| 62 |
'ID' => $forum->forumid, |
| 63 |
'post_title' => $forum->title, |
| 64 |
); |
| 65 |
|
| 66 |
} |
| 67 |
|
| 68 |
if ( count( $results_boards ) > 1 ) { |
| 69 |
foreach ( $results_boards as $board ){ |
| 70 |
if ( $board->boardid !== '0' ){ |
| 71 |
$table = $wpdb->prefix . 'wpforo_' . $board->boardid . '_forums'; |
| 72 |
|
| 73 |
// Try to find the forums |
| 74 |
if ( ! empty( $search ) ) { |
| 75 |
$results_forums = $wpdb->get_results( $wpdb->prepare( |
| 76 |
"SELECT * FROM {$table} WHERE ( title LIKE %s OR title LIKE %s )", |
| 77 |
"%%{$search}%%", |
| 78 |
"{$search}%%" |
| 79 |
) ); |
| 80 |
} else { |
| 81 |
$results_forums = $wpdb->get_results( "SELECT * FROM {$table}" ); |
| 82 |
} |
| 83 |
|
| 84 |
foreach ($results_forums as $forum ) { |
| 85 |
$forum_id = $board->boardid . '-' . $forum->forumid; |
| 86 |
$results[] = array( |
| 87 |
'ID' => $forum_id, |
| 88 |
'post_title' => $forum->title, |
| 89 |
); |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
// Return our results |
| 96 |
wp_send_json_success( $results ); |
| 97 |
die; |
| 98 |
} else if( in_array( 'wpforo_topic', $_REQUEST['post_type'] ) ) { |
| 99 |
// Topics |
| 100 |
|
| 101 |
$table = WPF()->tables->topics; |
| 102 |
$boards = WPF()->tables->boards; |
| 103 |
|
| 104 |
// Get the boards |
| 105 |
$results_boards = $wpdb->get_results( $wpdb->prepare( |
| 106 |
"SELECT boardid FROM {$boards}" |
| 107 |
) ); |
| 108 |
|
| 109 |
// Try to find the forums |
| 110 |
if ( ! empty( $search ) ) { |
| 111 |
$topics = $wpdb->get_results( $wpdb->prepare( |
| 112 |
"SELECT * FROM {$table} WHERE ( title LIKE %s OR title LIKE %s )", |
| 113 |
"%%{$search}%%", |
| 114 |
"{$search}%%" |
| 115 |
) ); |
| 116 |
} else { |
| 117 |
$topics = $wpdb->get_results( "SELECT * FROM {$table}" ); |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
// Build the results array |
| 122 |
$results = array(); |
| 123 |
|
| 124 |
foreach ( $topics as $topic ) { |
| 125 |
|
| 126 |
// Results should meet same structure like posts |
| 127 |
$results[] = array( |
| 128 |
'ID' => $topic->topicid, |
| 129 |
'post_title' => $topic->title, |
| 130 |
'post_type' => gamipress_wpforo_get_forum_title( $topic->forumid ), |
| 131 |
); |
| 132 |
|
| 133 |
} |
| 134 |
|
| 135 |
if ( count( $results_boards ) > 1 ) { |
| 136 |
foreach ( $results_boards as $board ){ |
| 137 |
if ( $board->boardid !== '0' ){ |
| 138 |
$table = $wpdb->prefix . 'wpforo_' . $board->boardid . '_topics'; |
| 139 |
|
| 140 |
// Try to find the topics |
| 141 |
if ( ! empty( $search ) ) { |
| 142 |
$results_topics = $wpdb->get_results( $wpdb->prepare( |
| 143 |
"SELECT * FROM {$table} WHERE ( title LIKE %s OR title LIKE %s )", |
| 144 |
"%%{$search}%%", |
| 145 |
"{$search}%%" |
| 146 |
) ); |
| 147 |
} else { |
| 148 |
$results_topics = $wpdb->get_results( "SELECT * FROM {$table}" ); |
| 149 |
} |
| 150 |
|
| 151 |
foreach ($results_topics as $topic ) { |
| 152 |
$forum_id = $board->boardid . '-' . $topic->forumid; |
| 153 |
$results[] = array( |
| 154 |
'ID' => $board->boardid . '-' . $topic->topicid, |
| 155 |
'post_title' => $topic->title, |
| 156 |
'post_type' => gamipress_wpforo_get_forum_title( $forum_id ), |
| 157 |
); |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
$response = array( |
| 164 |
'results' => $results, |
| 165 |
'more_results' => false, |
| 166 |
); |
| 167 |
|
| 168 |
// Return our results |
| 169 |
wp_send_json_success( $response ); |
| 170 |
die; |
| 171 |
|
| 172 |
} |
| 173 |
|
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
} |
| 178 |
add_action( 'wp_ajax_gamipress_wpforo_get_posts', 'gamipress_wpforo_ajax_get_posts', 5 ); |
| 179 |
|
| 180 |
|
| 181 |
function gamipress_wpforo_forum_options(){ |
| 182 |
|
| 183 |
} |
| 184 |
|
| 185 |
// Helper function to get the forum title |
| 186 |
function gamipress_wpforo_get_forum_title( $forum_id ) { |
| 187 |
|
| 188 |
global $wpdb; |
| 189 |
|
| 190 |
if ( strpos( $forum_id, '-' ) ) { |
| 191 |
$board = explode('-', $forum_id)[0]; |
| 192 |
$forum_id = explode('-', $forum_id)[1]; |
| 193 |
$table = $table = $wpdb->prefix . 'wpforo_' . $board . '_forums'; |
| 194 |
|
| 195 |
} else { |
| 196 |
$table = WPF()->tables->forums; |
| 197 |
} |
| 198 |
|
| 199 |
return $wpdb->get_var( $wpdb->prepare( |
| 200 |
"SELECT f.title FROM {$table} AS f WHERE f.forumid = %d", |
| 201 |
$forum_id |
| 202 |
) ); |
| 203 |
|
| 204 |
} |
| 205 |
|
| 206 |
// Helper function to get the topic title |
| 207 |
function gamipress_wpforo_get_topic_title( $topic_id ) { |
| 208 |
|
| 209 |
global $wpdb; |
| 210 |
|
| 211 |
if ( strpos( $topic_id, '-' ) ) { |
| 212 |
$board = explode('-', $topic_id)[0]; |
| 213 |
$topic_id = explode('-', $topic_id)[1]; |
| 214 |
$table = $table = $wpdb->prefix . 'wpforo_' . $board . '_topics'; |
| 215 |
|
| 216 |
} else { |
| 217 |
$table = WPF()->tables->topics; |
| 218 |
} |
| 219 |
|
| 220 |
return $wpdb->get_var( $wpdb->prepare( |
| 221 |
"SELECT f.title FROM {$table} AS f WHERE f.topicid = %d", |
| 222 |
$topic_id |
| 223 |
) ); |
| 224 |
|
| 225 |
} |