| 1 |
<?php |
| 2 |
/** |
| 3 |
* Functions |
| 4 |
* |
| 5 |
* @package GamiPress\FluentCRM\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_fluentcrm_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( 'fluentcrm_tags', $_REQUEST['post_type'] ) ) { |
| 35 |
|
| 36 |
$tags = $wpdb->get_results( $wpdb->prepare( |
| 37 |
"SELECT t.id, t.title |
| 38 |
FROM {$wpdb->prefix}fc_tags AS t |
| 39 |
WHERE t.title LIKE %s", |
| 40 |
"%%{$search}%%" |
| 41 |
) ); |
| 42 |
|
| 43 |
foreach ( $tags as $tag ) { |
| 44 |
|
| 45 |
// Results should meet same structure like posts |
| 46 |
$results[] = array( |
| 47 |
'ID' => $tag->id, |
| 48 |
'post_title' => $tag->title, |
| 49 |
); |
| 50 |
|
| 51 |
} |
| 52 |
|
| 53 |
// Return our results |
| 54 |
wp_send_json_success( $results ); |
| 55 |
die; |
| 56 |
|
| 57 |
} else if( isset( $_REQUEST['post_type'] ) && in_array( 'fluentcrm_lists', $_REQUEST['post_type'] ) ) { |
| 58 |
|
| 59 |
$lists = $wpdb->get_results( $wpdb->prepare( |
| 60 |
"SELECT t.id, t.title |
| 61 |
FROM {$wpdb->prefix}fc_lists AS t |
| 62 |
WHERE t.title LIKE %s", |
| 63 |
"%%{$search}%%" |
| 64 |
) ); |
| 65 |
|
| 66 |
foreach ( $lists as $list ) { |
| 67 |
|
| 68 |
// Results should meet same structure like posts |
| 69 |
$results[] = array( |
| 70 |
'ID' => $list->id, |
| 71 |
'post_title' => $list->title, |
| 72 |
); |
| 73 |
|
| 74 |
} |
| 75 |
|
| 76 |
// Return our results |
| 77 |
wp_send_json_success( $results ); |
| 78 |
die; |
| 79 |
|
| 80 |
} |
| 81 |
|
| 82 |
} |
| 83 |
add_action( 'wp_ajax_gamipress_get_posts', 'gamipress_fluentcrm_ajax_get_posts', 5 ); |
| 84 |
|
| 85 |
// Helper function to get the subscriber user ID |
| 86 |
function gamipress_fluentcrm_get_subscriber_user_id( $subscriber ) { |
| 87 |
|
| 88 |
$user_id = 0; |
| 89 |
|
| 90 |
if( absint( $subscriber->user_id ) !== 0 ) { |
| 91 |
// Get the user ID |
| 92 |
$user_id = $subscriber->user_id; |
| 93 |
} else if( ! empty( $subscriber->email ) ) { |
| 94 |
// Search by email |
| 95 |
$user = get_user_by_email( $subscriber->email ); |
| 96 |
|
| 97 |
$user_id = $user->ID; |
| 98 |
} |
| 99 |
|
| 100 |
return $user_id; |
| 101 |
|
| 102 |
} |
| 103 |
|
| 104 |
// Get the tag title |
| 105 |
function gamipress_fluentcrm_get_tag_title( $tag_id ) { |
| 106 |
|
| 107 |
if( absint( $tag_id ) === 0 ) { |
| 108 |
return ''; |
| 109 |
} |
| 110 |
|
| 111 |
global $wpdb; |
| 112 |
|
| 113 |
return $wpdb->get_var( $wpdb->prepare( |
| 114 |
"SELECT title FROM {$wpdb->prefix}fc_tags WHERE id = %s", |
| 115 |
$tag_id |
| 116 |
) ); |
| 117 |
|
| 118 |
} |
| 119 |
|
| 120 |
// Get the list title |
| 121 |
function gamipress_fluentcrm_get_list_title( $list_id ) { |
| 122 |
|
| 123 |
if( absint( $list_id ) === 0 ) { |
| 124 |
return ''; |
| 125 |
} |
| 126 |
|
| 127 |
global $wpdb; |
| 128 |
|
| 129 |
return $wpdb->get_var( $wpdb->prepare( |
| 130 |
"SELECT title FROM {$wpdb->prefix}fc_lists WHERE id = %s", |
| 131 |
$list_id |
| 132 |
) ); |
| 133 |
|
| 134 |
} |