| @@ -1,0 +1,145 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Ajax Functions | |
| 4 | + * | |
| 5 | + * @package AutomatorWP\wpForo\Ajax_Functions | |
| 6 | + * @since 1.0.0 | |
| 7 | + */ | |
| 8 | +// Exit if accessed directly | |
| 9 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 10 | + | |
| 11 | +/** | |
| 12 | + * Ajax function for selecting forums | |
| 13 | + * | |
| 14 | + * @since 1.0.0 | |
| 15 | + */ | |
| 16 | +function automatorwp_wpforo_ajax_get_forums() { | |
| 17 | + // Security check, forces to die if not security passed | |
| 18 | + check_ajax_referer( 'automatorwp_admin', 'nonce' ); | |
| 19 | + | |
| 20 | + // Permissions check | |
| 21 | + if( ! current_user_can( automatorwp_get_manager_capability() ) ) { | |
| 22 | + wp_send_json_error( __( 'You\'re not allowed to perform this action.', 'automatorwp' ) ); | |
| 23 | + } | |
| 24 | + | |
| 25 | + global $wpdb; | |
| 26 | + $results = array(); | |
| 27 | + | |
| 28 | + // Pull back the search string | |
| 29 | + $search = isset( $_REQUEST['q'] ) ? $wpdb->esc_like( $_REQUEST['q'] ) : ''; | |
| 30 | + | |
| 31 | + $table = WPF()->tables->forums; | |
| 32 | + $boards = WPF()->tables->boards; | |
| 33 | + | |
| 34 | + // Get the boards | |
| 35 | + $results_boards = $wpdb->get_results( | |
| 36 | + "SELECT boardid FROM {$boards}" | |
| 37 | + ); | |
| 38 | + | |
| 39 | + // Get the forums | |
| 40 | + $results = $wpdb->get_results( $wpdb->prepare( | |
| 41 | + "SELECT f.forumid AS id, f.title AS text | |
| 42 | + FROM {$table} AS f | |
| 43 | + WHERE f.title LIKE %s", | |
| 44 | + "%%{$search}%%" | |
| 45 | + ) ); | |
| 46 | + | |
| 47 | + if ( count( $results_boards ) > 1 ) { | |
| 48 | + foreach ( $results_boards as $board ) { | |
| 49 | + if ( $board->boardid !== '0' ){ | |
| 50 | + $table = $wpdb->prefix . 'wpforo_' . $board->boardid . '_forums'; | |
| 51 | + // Get the forums | |
| 52 | + $results_forums = $wpdb->get_results( $wpdb->prepare( | |
| 53 | + "SELECT f.forumid AS id, f.title AS text | |
| 54 | + FROM {$table} AS f | |
| 55 | + WHERE f.title LIKE %s", | |
| 56 | + "%%{$search}%%" | |
| 57 | + ) ); | |
| 58 | + | |
| 59 | + foreach ($results_forums as $forum ) { | |
| 60 | + | |
| 61 | + $results[] = array( | |
| 62 | + 'id' => $board->boardid . '-' . $forum->id, | |
| 63 | + 'text' => $forum->text, | |
| 64 | + ); | |
| 65 | + } | |
| 66 | + } | |
| 67 | + } | |
| 68 | + } | |
| 69 | + | |
| 70 | + // Prepend option none | |
| 71 | + $results = automatorwp_ajax_parse_extra_options( $results ); | |
| 72 | + | |
| 73 | + // Return our results | |
| 74 | + wp_send_json_success( $results ); | |
| 75 | + die; | |
| 76 | + | |
| 77 | +} | |
| 78 | +add_action( 'wp_ajax_automatorwp_wpforo_get_forums', 'automatorwp_wpforo_ajax_get_forums' ); | |
| 79 | + | |
| 80 | +/** | |
| 81 | + * Ajax function for selecting topics | |
| 82 | + * | |
| 83 | + * @since 1.0.0 | |
| 84 | + */ | |
| 85 | +function automatorwp_wpforo_ajax_get_topics() { | |
| 86 | + // Security check, forces to die if not security passed | |
| 87 | + check_ajax_referer( 'automatorwp_admin', 'nonce' ); | |
| 88 | + | |
| 89 | + // Permissions check | |
| 90 | + if( ! current_user_can( automatorwp_get_manager_capability() ) ) { | |
| 91 | + wp_send_json_error( __( 'You\'re not allowed to perform this action.', 'automatorwp' ) ); | |
| 92 | + } | |
| 93 | + | |
| 94 | + global $wpdb; | |
| 95 | + | |
| 96 | + // Pull back the search string | |
| 97 | + $search = isset( $_REQUEST['q'] ) ? $wpdb->esc_like( $_REQUEST['q'] ) : ''; | |
| 98 | + | |
| 99 | + $table = WPF()->tables->topics; | |
| 100 | + $boards = WPF()->tables->boards; | |
| 101 | + | |
| 102 | + // Get the boards | |
| 103 | + $results_boards = $wpdb->get_results( | |
| 104 | + "SELECT boardid FROM {$boards}" | |
| 105 | + ); | |
| 106 | + | |
| 107 | + // Get the topics | |
| 108 | + $results = $wpdb->get_results( $wpdb->prepare( | |
| 109 | + "SELECT f.topicid AS id, f.title AS text | |
| 110 | + FROM {$table} AS f | |
| 111 | + WHERE f.title LIKE %s", | |
| 112 | + "%%{$search}%%" | |
| 113 | + ) ); | |
| 114 | + | |
| 115 | + if ( count( $results_boards ) > 1 ) { | |
| 116 | + foreach ( $results_boards as $board ){ | |
| 117 | + if ( $board->boardid !== '0' ) { | |
| 118 | + $table = $wpdb->prefix . 'wpforo_' . $board->boardid . '_topics'; | |
| 119 | + // Get the topics | |
| 120 | + $results_topics = $wpdb->get_results( $wpdb->prepare( | |
| 121 | + "SELECT f.topicid AS id, f.title AS text | |
| 122 | + FROM {$table} AS f | |
| 123 | + WHERE f.title LIKE %s", | |
| 124 | + "%%{$search}%%" | |
| 125 | + ) ); | |
| 126 | + | |
| 127 | + foreach ($results_topics as $topic ) { | |
| 128 | + $results[] = array( | |
| 129 | + 'id' => $board->boardid . '-' . $topic->id, | |
| 130 | + 'text' => $topic->text, | |
| 131 | + ); | |
| 132 | + } | |
| 133 | + } | |
| 134 | + } | |
| 135 | + } | |
| 136 | + | |
| 137 | + // Prepend option none | |
| 138 | + $results = automatorwp_ajax_parse_extra_options( $results ); | |
| 139 | + | |
| 140 | + // Return our results | |
| 141 | + wp_send_json_success( $results ); | |
| 142 | + die; | |
| 143 | + | |
| 144 | +} | |
| 145 | +add_action( 'wp_ajax_automatorwp_wpforo_get_topics', 'automatorwp_wpforo_ajax_get_topics' ); | |