# gamipress/8.0.1/integrations/simplepress/includes/functions.php

GamiPress – Gamification plugin to reward points, badges &amp; ranks in WordPress, now with AI, version 8.0.1. 127 lines.

- Page: https://pluginprobe.com/plugins/gamipress/8.0.1/code/integrations/simplepress/includes/functions.php
- Raw: https://pluginprobe.com/plugins/gamipress/8.0.1/raw/integrations/simplepress/includes/functions.php
- Modified: 2026-09-14T10:50:56+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/gamipress/8.0.1/code/integrations/simplepress/includes/functions.php#L10-L20`.

```php
<?php
/**
 * Functions
 *
 * @package GamiPress\SimplePress\Functions
 * @since 1.0.0
 */
// Exit if accessed directly
if( !defined( 'ABSPATH' ) ) exit;

/**
 * Overrides GamiPress AJAX Helper for selecting posts
 *
 * @since 1.0.0
 */
function gamipress_simplepress_ajax_get_posts() {

    // Security check, forces to die if not security passed
    check_ajax_referer( 'gamipress_admin', 'nonce' );

    // Check if user can manage GamiPress
    if( ! current_user_can( gamipress_get_manager_capability() ) ) {
        wp_send_json_error( __( 'You\'re not allowed to perform this action.', 'gamipress' ) );
    }

    global $wpdb;

    if( isset( $_REQUEST['post_type'] ) ) {

        // Get the user input
        $search = isset( $_REQUEST['q'] ) ? $wpdb->esc_like( sanitize_text_field( $_REQUEST['q'] ) ) : '';

        if( in_array( 'simplepress_forum', $_REQUEST['post_type'] ) ) {
            // Forums

            $table = SPFORUMS;

            // Try to find the forums
            if ( ! empty( $search ) ) {
                $forums = $wpdb->get_results( $wpdb->prepare(
                    "SELECT * FROM {$table} WHERE ( forum_name LIKE %s OR forum_name LIKE %s )",
                    "%%{$search}%%",
                    "{$search}%%"
                ) );
            } else {
                $forums = $wpdb->get_results( "SELECT * FROM {$table}" );
            }

            // Build the results array
            $results = array();

            foreach ( $forums as $forum ) {

                // Results should meet same structure like posts
                $results[] = array(
                    'ID' => $forum->forum_id,
                    'post_title' => $forum->forum_name,
                );

            }

            // Return our results
            wp_send_json_success( $results );
            die;
        } else if( in_array( 'simplepress_topic', $_REQUEST['post_type'] ) ) {
            // Topics

            $table = SPTOPICS;

            // Try to find the topics
            if ( ! empty( $search ) ) {
                $topics = $wpdb->get_results( $wpdb->prepare(
                    "SELECT * FROM {$table} WHERE ( topic_name LIKE %s OR topic_name LIKE %s )",
                    "%%{$search}%%",
                    "{$search}%%"
                ) );
            } else {
                $topics = $wpdb->get_results( "SELECT * FROM {$table}" );
            }

            // Build the results array
            $results = array();

            foreach ( $topics as $topic ) {

                // Results should meet same structure like posts
                $results[] = array(
                    'ID' => $topic->topic_id,
                    'post_title' => $topic->topic_name,
                    'post_type' => gamipress_simplepress_get_forum_title( $topic->forum_id ),
                );

            }

            // Return our results
            wp_send_json_success( $results );
            die;

        }

    }


}
add_action( 'wp_ajax_gamipress_get_posts', 'gamipress_simplepress_ajax_get_posts', 5 );

// Helper function to get the forum title
function gamipress_simplepress_get_forum_title( $forum_id ) {

    global $wpdb;

    $table = SPFORUMS;

    return $wpdb->get_var( $wpdb->prepare( "SELECT forum_name FROM {$table} WHERE forum_id = %d;", $forum_id ) );

}

// Helper function to get the topic title
function gamipress_simplepress_get_topic_title( $topic_id ) {

    global $wpdb;

    $table = SPTOPICS;

    return $wpdb->get_var( $wpdb->prepare( "SELECT topic_name FROM {$table} WHERE topic_id = %d;", $topic_id ) );

}
```
