| 1 |
<?php |
| 2 |
/** |
| 3 |
* Functions |
| 4 |
* |
| 5 |
* @package GamiPress\Autonami\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_autonami_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 |
$results = array(); |
| 30 |
|
| 31 |
// Pull back the search string |
| 32 |
$search = isset( $_REQUEST['q'] ) ? $wpdb->esc_like( sanitize_text_field( $_REQUEST['q'] ) ) : ''; |
| 33 |
|
| 34 |
if( isset( $_REQUEST['post_type'] ) && in_array( 'autonami_tags', $_REQUEST['post_type'] ) ) { |
| 35 |
|
| 36 |
$tags = gamipress_autonami_get_tags(); |
| 37 |
|
| 38 |
foreach ( $tags as $tag ) { |
| 39 |
|
| 40 |
if( ! empty( $search ) ) { |
| 41 |
if( strpos( strtolower( $tag['name'] ), strtolower( $search ) ) === false ) { |
| 42 |
continue; |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
// Results should meet same structure like posts |
| 47 |
$results[] = array( |
| 48 |
'ID' => $tag['id'], |
| 49 |
'post_title' => $tag['name'], |
| 50 |
); |
| 51 |
|
| 52 |
} |
| 53 |
|
| 54 |
// Return our results |
| 55 |
wp_send_json_success( $results ); |
| 56 |
die; |
| 57 |
|
| 58 |
} else if( isset( $_REQUEST['post_type'] ) && in_array( 'autonami_lists', $_REQUEST['post_type'] ) ) { |
| 59 |
|
| 60 |
$lists = gamipress_autonami_get_lists(); |
| 61 |
|
| 62 |
foreach ( $lists as $list ) { |
| 63 |
|
| 64 |
if( ! empty( $search ) ) { |
| 65 |
if( strpos( strtolower( $list['name'] ), strtolower( $search ) ) === false ) { |
| 66 |
continue; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
// Results should meet same structure like posts |
| 71 |
$results[] = array( |
| 72 |
'ID' => $list['id'], |
| 73 |
'post_title' => $list['name'], |
| 74 |
); |
| 75 |
|
| 76 |
} |
| 77 |
|
| 78 |
// Return our results |
| 79 |
wp_send_json_success( $results ); |
| 80 |
die; |
| 81 |
|
| 82 |
} |
| 83 |
|
| 84 |
} |
| 85 |
add_action( 'wp_ajax_gamipress_get_posts', 'gamipress_autonami_ajax_get_posts', 5 ); |
| 86 |
|
| 87 |
// Helper function to get the contact user ID |
| 88 |
function gamipress_autonami_get_contact_user_id( $contact ) { |
| 89 |
|
| 90 |
$user_id = 0; |
| 91 |
|
| 92 |
$email = $contact->contact->get_email(); |
| 93 |
|
| 94 |
$user = get_user_by( 'email', $email ); |
| 95 |
|
| 96 |
// Make sure subscriber is a WordPress user |
| 97 |
if ( empty ( $user ) ) |
| 98 |
return; |
| 99 |
|
| 100 |
$user_id = $user->ID; |
| 101 |
|
| 102 |
return $user_id; |
| 103 |
|
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Get the tags |
| 108 |
* |
| 109 |
* @since 1.0.0 |
| 110 |
* |
| 111 |
* @return array |
| 112 |
*/ |
| 113 |
function gamipress_autonami_get_tags( ){ |
| 114 |
|
| 115 |
$tags = array(); |
| 116 |
|
| 117 |
$all_tags = BWFCRM_Tag::get_tags(); |
| 118 |
|
| 119 |
foreach ( $all_tags as $tag ) { |
| 120 |
$tags[] = array( |
| 121 |
'id' => $tag['ID'], |
| 122 |
'name' => $tag['name'], |
| 123 |
); |
| 124 |
} |
| 125 |
|
| 126 |
return $tags; |
| 127 |
|
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Get the tag title |
| 132 |
* |
| 133 |
* @since 1.0.0 |
| 134 |
* |
| 135 |
* @param int $tag_id |
| 136 |
* |
| 137 |
* @return string|null |
| 138 |
*/ |
| 139 |
function gamipress_autonami_get_tag_title( $tag_id ) { |
| 140 |
|
| 141 |
// Empty title if no ID provided |
| 142 |
if( absint( $tag_id ) === 0 ) { |
| 143 |
return ''; |
| 144 |
} |
| 145 |
|
| 146 |
$all_tags = BWFCRM_Tag::get_tags(); |
| 147 |
|
| 148 |
foreach ( $all_tags as $tag ) { |
| 149 |
|
| 150 |
if ( absint( $tag_id ) === absint( $tag['ID'] ) ){ |
| 151 |
$tag_title = $tag['name']; |
| 152 |
} |
| 153 |
|
| 154 |
} |
| 155 |
|
| 156 |
return $tag_title; |
| 157 |
|
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Get the lists |
| 162 |
* |
| 163 |
* @since 1.0.0 |
| 164 |
* |
| 165 |
* @return array |
| 166 |
*/ |
| 167 |
function gamipress_autonami_get_lists( ){ |
| 168 |
|
| 169 |
$lists = array(); |
| 170 |
|
| 171 |
$all_lists = BWFCRM_Lists::get_lists(); |
| 172 |
|
| 173 |
foreach ( $all_lists as $list ) { |
| 174 |
$lists[] = array( |
| 175 |
'id' => $list['ID'], |
| 176 |
'name' => $list['name'], |
| 177 |
); |
| 178 |
} |
| 179 |
|
| 180 |
return $lists; |
| 181 |
|
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Get the list title |
| 186 |
* |
| 187 |
* @since 1.0.0 |
| 188 |
* |
| 189 |
* @param int $list_id |
| 190 |
* |
| 191 |
* @return string|null |
| 192 |
*/ |
| 193 |
function gamipress_autonami_get_list_title( $list_id ) { |
| 194 |
|
| 195 |
// Empty title if no ID provided |
| 196 |
if( absint( $list_id ) === 0 ) { |
| 197 |
return ''; |
| 198 |
} |
| 199 |
|
| 200 |
$all_lists = BWFCRM_Lists::get_lists(); |
| 201 |
|
| 202 |
foreach ( $all_lists as $list ) { |
| 203 |
|
| 204 |
if ( absint( $list_id ) === absint( $list['ID'] ) ){ |
| 205 |
$list_title = $list['name']; |
| 206 |
} |
| 207 |
|
| 208 |
} |
| 209 |
|
| 210 |
return $list_title; |
| 211 |
|
| 212 |
} |