PluginProbe
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI / 8.0.2
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI v8.0.2
8.0.1 8.0.2 8.0.0 7.9.9.7 7.9.9.6 7.9.9.5 7.9.9.4 7.9.9.3 7.9.9.2 7.9.9.1 7.9.9 7.9.8 7.9.7 7.9.6 7.9.5 7.9.4 7.9.3 7.9.2 7.9.1 7.9.0 7.8.9 7.8.8 7.8.7 7.8.6 7.8.5 All 44 releases
gamipress / integrations / simplepress / includes / functions.php

functions.php in GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI 8.0.2, at integrations/simplepress/includes/functions.php

127 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 }