| 1 |
<?php |
| 2 |
/** |
| 3 |
* Functions |
| 4 |
* |
| 5 |
* @package GamiPress\Mail_Mint\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_mail_mint_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( 'mail_mint_tags', $_REQUEST['post_type'] ) ) { |
| 35 |
|
| 36 |
$tags = gamipress_mail_mint_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( 'mail_mint_lists', $_REQUEST['post_type'] ) ) { |
| 59 |
|
| 60 |
$lists = gamipress_mail_mint_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 |
} else if( isset( $_REQUEST['post_type'] ) && in_array( 'mail_mint_forms', $_REQUEST['post_type'] ) ) { |
| 83 |
|
| 84 |
$lists = gamipress_mail_mint_get_forms(); |
| 85 |
|
| 86 |
foreach ( $lists as $list ) { |
| 87 |
|
| 88 |
if( ! empty( $search ) ) { |
| 89 |
if( strpos( strtolower( $list['name'] ), strtolower( $search ) ) === false ) { |
| 90 |
continue; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
// Results should meet same structure like posts |
| 95 |
$results[] = array( |
| 96 |
'ID' => $list['id'], |
| 97 |
'post_title' => $list['name'], |
| 98 |
); |
| 99 |
|
| 100 |
} |
| 101 |
|
| 102 |
// Return our results |
| 103 |
wp_send_json_success( $results ); |
| 104 |
die; |
| 105 |
|
| 106 |
} |
| 107 |
|
| 108 |
} |
| 109 |
add_action( 'wp_ajax_gamipress_get_posts', 'gamipress_mail_mint_ajax_get_posts', 5 ); |
| 110 |
|
| 111 |
/** |
| 112 |
* Get the tags |
| 113 |
* |
| 114 |
* @since 1.0.0 |
| 115 |
* |
| 116 |
* @return array |
| 117 |
*/ |
| 118 |
function gamipress_mail_mint_get_tags( ){ |
| 119 |
|
| 120 |
$tags = array(); |
| 121 |
|
| 122 |
// Bail if class does not exist |
| 123 |
if( ! class_exists ( 'Mint\MRM\DataBase\Models\ContactGroupModel' ) ) |
| 124 |
return; |
| 125 |
|
| 126 |
//Type: lists or tags |
| 127 |
$all_tags = Mint\MRM\DataBase\Models\ContactGroupModel::get_all_lists_or_tags( 'tags' ); |
| 128 |
|
| 129 |
foreach ( $all_tags as $tag ) { |
| 130 |
$tags[] = array( |
| 131 |
'id' => $tag['id'], |
| 132 |
'name' => $tag['title'], |
| 133 |
); |
| 134 |
} |
| 135 |
|
| 136 |
return $tags; |
| 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_mail_mint_get_tag_title( $tag_id ) { |
| 150 |
|
| 151 |
// Empty title if no ID provided |
| 152 |
if( absint( $tag_id ) === 0 ) |
| 153 |
return ''; |
| 154 |
|
| 155 |
// Bail if class does not exist |
| 156 |
if( ! class_exists ( 'Mint\MRM\DataBase\Models\ContactGroupModel' ) || ! class_exists ( 'MRM\Common\MrmCommon' ) ) |
| 157 |
return; |
| 158 |
|
| 159 |
$all_tags = Mint\MRM\DataBase\Models\ContactGroupModel::get_all_lists_or_tags( 'tags' ); |
| 160 |
|
| 161 |
$tag = MRM\Common\MrmCommon::search_for_id( strval( $tag_id ), $all_tags ); |
| 162 |
|
| 163 |
return $tag['title']; |
| 164 |
|
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Get the lists |
| 169 |
* |
| 170 |
* @since 1.0.0 |
| 171 |
* |
| 172 |
* @return array |
| 173 |
*/ |
| 174 |
function gamipress_mail_mint_get_lists( ){ |
| 175 |
|
| 176 |
$lists = array(); |
| 177 |
|
| 178 |
// Bail if class does not exist |
| 179 |
if( ! class_exists ( 'Mint\MRM\DataBase\Models\ContactGroupModel' ) ) |
| 180 |
return; |
| 181 |
|
| 182 |
//Type: lists or tags |
| 183 |
$all_lists = Mint\MRM\DataBase\Models\ContactGroupModel::get_all_lists_or_tags( 'lists' ); |
| 184 |
|
| 185 |
foreach ( $all_lists as $list ) { |
| 186 |
$lists[] = array( |
| 187 |
'id' => $list['id'], |
| 188 |
'name' => $list['title'], |
| 189 |
); |
| 190 |
} |
| 191 |
|
| 192 |
return $lists; |
| 193 |
|
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Get the list title |
| 198 |
* |
| 199 |
* @since 1.0.0 |
| 200 |
* |
| 201 |
* @param int $list_id |
| 202 |
* |
| 203 |
* @return string|null |
| 204 |
*/ |
| 205 |
function gamipress_mail_mint_get_list_title( $list_id ) { |
| 206 |
|
| 207 |
// Empty title if no ID provided |
| 208 |
if( absint( $list_id ) === 0 ) |
| 209 |
return ''; |
| 210 |
|
| 211 |
// Bail if class does not exist |
| 212 |
if( ! class_exists ( 'Mint\MRM\DataBase\Models\ContactGroupModel' ) || ! class_exists ( 'MRM\Common\MrmCommon' ) ) |
| 213 |
return; |
| 214 |
|
| 215 |
$all_lists = Mint\MRM\DataBase\Models\ContactGroupModel::get_all_lists_or_tags( 'lists' ); |
| 216 |
|
| 217 |
$list = MRM\Common\MrmCommon::search_for_id( strval( $list_id ), $all_lists ); |
| 218 |
|
| 219 |
return $list['title']; |
| 220 |
|
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
* Get forms |
| 225 |
* |
| 226 |
* @since 1.0.0 |
| 227 |
* |
| 228 |
* @return array |
| 229 |
*/ |
| 230 |
function gamipress_mail_mint_get_forms( ){ |
| 231 |
|
| 232 |
$forms = array(); |
| 233 |
|
| 234 |
// Bail if class does not exist |
| 235 |
if( ! class_exists ( 'Mint\MRM\DataBase\Models\FormModel' ) ) |
| 236 |
return ''; |
| 237 |
|
| 238 |
$all_forms = Mint\MRM\DataBase\Models\FormModel::get_all( 'id', 'asc', 'published' ); |
| 239 |
|
| 240 |
foreach ( $all_forms['data'] as $form ) { |
| 241 |
$forms[] = array( |
| 242 |
'id' => $form['id'], |
| 243 |
'name' => $form['title'], |
| 244 |
); |
| 245 |
} |
| 246 |
|
| 247 |
return $forms; |
| 248 |
|
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* Get the form title |
| 253 |
* |
| 254 |
* @since 1.0.0 |
| 255 |
* |
| 256 |
* @param int $list_id |
| 257 |
* |
| 258 |
* @return string|null |
| 259 |
*/ |
| 260 |
function gamipress_mail_mint_get_form_title( $form_id ) { |
| 261 |
|
| 262 |
// Empty title if no ID provided |
| 263 |
if( absint( $form_id ) === 0 ) |
| 264 |
return ''; |
| 265 |
|
| 266 |
// Bail if class does not exist |
| 267 |
if( ! class_exists ( 'Mint\MRM\DataBase\Models\FormModel' ) ) |
| 268 |
return ''; |
| 269 |
|
| 270 |
$form = Mint\MRM\DataBase\Models\FormModel::get( $form_id ); |
| 271 |
|
| 272 |
return $form['title']; |
| 273 |
|
| 274 |
} |