| @@ -1,0 +1,158 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Functions | |
| 4 | + * | |
| 5 | + * @package GamiPress\BBForms\Functions | |
| 6 | + * @since 1.0.0 | |
| 7 | + */ | |
| 8 | + | |
| 9 | +// Exit if accessed directly | |
| 10 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 11 | + | |
| 12 | +/** | |
| 13 | + * Overrides GamiPress AJAX Helper for selecting posts | |
| 14 | + * | |
| 15 | + * @since 1.0.0 | |
| 16 | + */ | |
| 17 | +function gamipress_bbforms_ajax_get_posts() { | |
| 18 | + | |
| 19 | + // Security check, forces to die if not security passed | |
| 20 | + check_ajax_referer( 'gamipress_admin', 'nonce' ); | |
| 21 | + | |
| 22 | + // Check if user can manage GamiPress | |
| 23 | + if( ! current_user_can( gamipress_get_manager_capability() ) ) { | |
| 24 | + wp_send_json_error( __( 'You\'re not allowed to perform this action.', 'gamipress' ) ); | |
| 25 | + } | |
| 26 | + | |
| 27 | + global $wpdb; | |
| 28 | + | |
| 29 | + if( isset( $_REQUEST['post_type'] ) && in_array( 'bbforms', $_REQUEST['post_type'] ) ) { | |
| 30 | + | |
| 31 | + $results = array(); | |
| 32 | + | |
| 33 | + // Pull back the search string | |
| 34 | + $search = isset( $_REQUEST['q'] ) ? $wpdb->esc_like( sanitize_text_field( $_REQUEST['q'] ) ) : ''; | |
| 35 | + | |
| 36 | + // Setup table | |
| 37 | + $ct_table = ct_setup_table( 'bbforms_forms' ); | |
| 38 | + | |
| 39 | + // Get the forms | |
| 40 | + $forms = $wpdb->get_results( $wpdb->prepare( | |
| 41 | + "SELECT s.id, s.title | |
| 42 | + FROM {$ct_table->db->table_name} AS s | |
| 43 | + WHERE s.title LIKE %s", | |
| 44 | + "%%{$search}%%" | |
| 45 | + ) ); | |
| 46 | + | |
| 47 | + ct_reset_setup_table(); | |
| 48 | + | |
| 49 | + foreach ( $forms as $form ) { | |
| 50 | + | |
| 51 | + // Results should meet same structure like posts | |
| 52 | + $results[] = array( | |
| 53 | + 'ID' => $form->id, | |
| 54 | + 'post_title' => ! empty( $form->title ) ? $form->title : '(no title)', | |
| 55 | + ); | |
| 56 | + | |
| 57 | + } | |
| 58 | + | |
| 59 | + // Return our results | |
| 60 | + wp_send_json_success( $results ); | |
| 61 | + die; | |
| 62 | + | |
| 63 | + } | |
| 64 | + | |
| 65 | +} | |
| 66 | +add_action( 'wp_ajax_gamipress_get_posts', 'gamipress_bbforms_ajax_get_posts', 5 ); | |
| 67 | + | |
| 68 | +/** | |
| 69 | + * Get form categories | |
| 70 | + * | |
| 71 | + * @since 1.0.0 | |
| 72 | + * | |
| 73 | + * @return array | |
| 74 | + */ | |
| 75 | +function gamipress_bbforms_get_form_categories( ) { | |
| 76 | + | |
| 77 | + global $wpdb; | |
| 78 | + | |
| 79 | + // Setup table | |
| 80 | + $ct_table = ct_setup_table( 'bbforms_categories' ); | |
| 81 | + | |
| 82 | + // Query search | |
| 83 | + $categories = $wpdb->get_results( | |
| 84 | + "SELECT c.id, c.name | |
| 85 | + FROM {$ct_table->db->table_name} AS c" | |
| 86 | + ); | |
| 87 | + | |
| 88 | + ct_reset_setup_table(); | |
| 89 | + | |
| 90 | + return $categories; | |
| 91 | + | |
| 92 | +} | |
| 93 | + | |
| 94 | +/** | |
| 95 | + * Get form tags | |
| 96 | + * | |
| 97 | + * @since 1.0.0 | |
| 98 | + * | |
| 99 | + * @return array | |
| 100 | + */ | |
| 101 | +function gamipress_bbforms_get_form_tags( ) { | |
| 102 | + | |
| 103 | + global $wpdb; | |
| 104 | + | |
| 105 | + // Setup table | |
| 106 | + $ct_table = ct_setup_table( 'bbforms_tags' ); | |
| 107 | + | |
| 108 | + // Query search | |
| 109 | + $tags = $wpdb->get_results( | |
| 110 | + "SELECT t.id, t.name | |
| 111 | + FROM {$ct_table->db->table_name} AS t" | |
| 112 | + ); | |
| 113 | + | |
| 114 | + ct_reset_setup_table(); | |
| 115 | + | |
| 116 | + return $tags; | |
| 117 | + | |
| 118 | +} | |
| 119 | + | |
| 120 | +/** | |
| 121 | + * Get the category title | |
| 122 | + * | |
| 123 | + * @since 1.0.0 | |
| 124 | + * | |
| 125 | + * @param int $category_id | |
| 126 | + * | |
| 127 | + * @return string|null | |
| 128 | + */ | |
| 129 | +function gamipress_bbforms_get_category_title( $category_id ) { | |
| 130 | + | |
| 131 | + // Empty title if no ID provided | |
| 132 | + if( absint( $category_id ) === 0 ) { | |
| 133 | + return ''; | |
| 134 | + } | |
| 135 | + | |
| 136 | + return bbforms_get_category_name( $category_id ); | |
| 137 | + | |
| 138 | +} | |
| 139 | + | |
| 140 | +/** | |
| 141 | + * Get the tag title | |
| 142 | + * | |
| 143 | + * @since 1.0.0 | |
| 144 | + * | |
| 145 | + * @param int $tag_id | |
| 146 | + * | |
| 147 | + * @return string|null | |
| 148 | + */ | |
| 149 | +function gamipress_bbforms_get_tag_title( $tag_id ) { | |
| 150 | + | |
| 151 | + // Empty title if no ID provided | |
| 152 | + if( absint( $tag_id ) === 0 ) { | |
| 153 | + return ''; | |
| 154 | + } | |
| 155 | + | |
| 156 | + return bbforms_get_tag_name( $tag_id ); | |
| 157 | + | |
| 158 | +} | |