| 1 |
<?php |
| 2 |
/** |
| 3 |
* Functions |
| 4 |
* |
| 5 |
* @package GamiPress\SimplePress\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_simplepress_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( 'simplepress_forum', $_REQUEST['post_type'] ) ) { |
| 34 |
// Forums |
| 35 |
|
| 36 |
$table = SPFORUMS; |
| 37 |
|
| 38 |
// Try to find the forums |
| 39 |
if ( ! empty( $search ) ) { |
| 40 |
$forums = $wpdb->get_results( $wpdb->prepare( |
| 41 |
"SELECT * FROM {$table} WHERE ( forum_name LIKE %s OR forum_name LIKE %s )", |
| 42 |
"%%{$search}%%", |
| 43 |
"{$search}%%" |
| 44 |
) ); |
| 45 |
} else { |
| 46 |
$forums = $wpdb->get_results( "SELECT * FROM {$table}" ); |
| 47 |
} |
| 48 |
|
| 49 |
// Build the results array |
| 50 |
$results = array(); |
| 51 |
|
| 52 |
foreach ( $forums as $forum ) { |
| 53 |
|
| 54 |
// Results should meet same structure like posts |
| 55 |
$results[] = array( |
| 56 |
'ID' => $forum->forum_id, |
| 57 |
'post_title' => $forum->forum_name, |
| 58 |
); |
| 59 |
|
| 60 |
} |
| 61 |
|
| 62 |
// Return our results |
| 63 |
wp_send_json_success( $results ); |
| 64 |
die; |
| 65 |
} else if( in_array( 'simplepress_topic', $_REQUEST['post_type'] ) ) { |
| 66 |
// Topics |
| 67 |
|
| 68 |
$table = SPTOPICS; |
| 69 |
|
| 70 |
// Try to find the topics |
| 71 |
if ( ! empty( $search ) ) { |
| 72 |
$topics = $wpdb->get_results( $wpdb->prepare( |
| 73 |
"SELECT * FROM {$table} WHERE ( topic_name LIKE %s OR topic_name LIKE %s )", |
| 74 |
"%%{$search}%%", |
| 75 |
"{$search}%%" |
| 76 |
) ); |
| 77 |
} else { |
| 78 |
$topics = $wpdb->get_results( "SELECT * FROM {$table}" ); |
| 79 |
} |
| 80 |
|
| 81 |
// Build the results array |
| 82 |
$results = array(); |
| 83 |
|
| 84 |
foreach ( $topics as $topic ) { |
| 85 |
|
| 86 |
// Results should meet same structure like posts |
| 87 |
$results[] = array( |
| 88 |
'ID' => $topic->topic_id, |
| 89 |
'post_title' => $topic->topic_name, |
| 90 |
'post_type' => gamipress_simplepress_get_forum_title( $topic->forum_id ), |
| 91 |
); |
| 92 |
|
| 93 |
} |
| 94 |
|
| 95 |
// Return our results |
| 96 |
wp_send_json_success( $results ); |
| 97 |
die; |
| 98 |
|
| 99 |
} |
| 100 |
|
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
} |
| 105 |
add_action( 'wp_ajax_gamipress_get_posts', 'gamipress_simplepress_ajax_get_posts', 5 ); |
| 106 |
|
| 107 |
// Helper function to get the forum title |
| 108 |
function gamipress_simplepress_get_forum_title( $forum_id ) { |
| 109 |
|
| 110 |
global $wpdb; |
| 111 |
|
| 112 |
$table = SPFORUMS; |
| 113 |
|
| 114 |
return $wpdb->get_var( $wpdb->prepare( "SELECT forum_name FROM {$table} WHERE forum_id = %d;", $forum_id ) ); |
| 115 |
|
| 116 |
} |
| 117 |
|
| 118 |
// Helper function to get the topic title |
| 119 |
function gamipress_simplepress_get_topic_title( $topic_id ) { |
| 120 |
|
| 121 |
global $wpdb; |
| 122 |
|
| 123 |
$table = SPTOPICS; |
| 124 |
|
| 125 |
return $wpdb->get_var( $wpdb->prepare( "SELECT topic_name FROM {$table} WHERE topic_id = %d;", $topic_id ) ); |
| 126 |
|
| 127 |
} |