| 1 |
<?php |
| 2 |
/** |
| 3 |
* Functions |
| 4 |
* |
| 5 |
* @package AutomatorWP\FluentCRM\Functions |
| 6 |
* @since 1.0.0 |
| 7 |
*/ |
| 8 |
// Exit if accessed directly |
| 9 |
if( !defined( 'ABSPATH' ) ) exit; |
| 10 |
|
| 11 |
/** |
| 12 |
* Gets the subscriber object from a user ID |
| 13 |
* |
| 14 |
* @param int $user_id |
| 15 |
* |
| 16 |
* @return FluentCrm\App\Models\Subscriber|null |
| 17 |
*/ |
| 18 |
function automatorwp_fluentcrm_get_subscriber( $user_id ) { |
| 19 |
|
| 20 |
$subscriber = FluentCrm\App\Models\Subscriber::where( 'user_id', $user_id )->first(); |
| 21 |
|
| 22 |
if( ! $subscriber ) { |
| 23 |
$user = get_userdata( $user_id ); |
| 24 |
|
| 25 |
// Bail if user not exists |
| 26 |
if( ! $user ) { |
| 27 |
return; |
| 28 |
} |
| 29 |
|
| 30 |
$subscriber = FluentCrm\App\Models\Subscriber::where( 'email', $user->user_email )->first(); |
| 31 |
|
| 32 |
} |
| 33 |
|
| 34 |
return $subscriber; |
| 35 |
|
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Gets the subscriber user ID |
| 40 |
* |
| 41 |
* @param \FluentCrm\App\Models\Subscriber $subscriber |
| 42 |
* |
| 43 |
* @return int |
| 44 |
*/ |
| 45 |
function automatorwp_fluentcrm_get_subscriber_user_id( $subscriber ) { |
| 46 |
|
| 47 |
$user_id = 0; |
| 48 |
|
| 49 |
if( absint( $subscriber->user_id ) !== 0 ) { |
| 50 |
// Get the user ID |
| 51 |
$user_id = $subscriber->user_id; |
| 52 |
} else if( ! empty( $subscriber->email ) ) { |
| 53 |
// Search by email |
| 54 |
$user = get_user_by_email( $subscriber->email ); |
| 55 |
|
| 56 |
// To avoid get null value for user_id |
| 57 |
$user_id = ( ! empty( $user ) ? $user->ID : 0 ); |
| 58 |
} |
| 59 |
|
| 60 |
return $user_id; |
| 61 |
|
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Options callback for select2 fields assigned to tags |
| 66 |
* |
| 67 |
* @since 1.0.0 |
| 68 |
* |
| 69 |
* @param stdClass $field |
| 70 |
* |
| 71 |
* @return array |
| 72 |
*/ |
| 73 |
function automatorwp_fluentcrm_options_cb_tag( $field ) { |
| 74 |
|
| 75 |
// Setup vars |
| 76 |
$value = $field->escaped_value; |
| 77 |
$none_value = 'any'; |
| 78 |
$none_label = __( 'any tag', 'automatorwp' ); |
| 79 |
$options = automatorwp_options_cb_none_option( $field, $none_value, $none_label ); |
| 80 |
|
| 81 |
if( ! empty( $value ) ) { |
| 82 |
if( ! is_array( $value ) ) { |
| 83 |
$value = array( $value ); |
| 84 |
} |
| 85 |
|
| 86 |
foreach( $value as $tag_id ) { |
| 87 |
|
| 88 |
// Skip option none |
| 89 |
if( $tag_id === $none_value ) { |
| 90 |
continue; |
| 91 |
} |
| 92 |
|
| 93 |
$options[$tag_id] = automatorwp_fluentcrm_get_tag_title( $tag_id ); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
return $options; |
| 98 |
|
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Get the tag title |
| 103 |
* |
| 104 |
* @since 1.0.0 |
| 105 |
* |
| 106 |
* @param int $tag_id |
| 107 |
* |
| 108 |
* @return string|null |
| 109 |
*/ |
| 110 |
function automatorwp_fluentcrm_get_tag_title( $tag_id ) { |
| 111 |
|
| 112 |
// Empty title if no ID provided |
| 113 |
if( absint( $tag_id ) === 0 ) { |
| 114 |
return ''; |
| 115 |
} |
| 116 |
|
| 117 |
global $wpdb; |
| 118 |
|
| 119 |
return $wpdb->get_var( $wpdb->prepare( |
| 120 |
"SELECT title FROM {$wpdb->prefix}fc_tags WHERE id = %s", |
| 121 |
$tag_id |
| 122 |
) ); |
| 123 |
|
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Options callback for select2 fields assigned to lists |
| 128 |
* |
| 129 |
* @since 1.0.0 |
| 130 |
* |
| 131 |
* @param stdClass $field |
| 132 |
* |
| 133 |
* @return array |
| 134 |
*/ |
| 135 |
function automatorwp_fluentcrm_options_cb_list( $field ) { |
| 136 |
|
| 137 |
// Setup vars |
| 138 |
$value = $field->escaped_value; |
| 139 |
$none_value = 'any'; |
| 140 |
$none_label = __( 'any list', 'automatorwp' ); |
| 141 |
$options = automatorwp_options_cb_none_option( $field, $none_value, $none_label ); |
| 142 |
|
| 143 |
if( ! empty( $value ) ) { |
| 144 |
if( ! is_array( $value ) ) { |
| 145 |
$value = array( $value ); |
| 146 |
} |
| 147 |
|
| 148 |
foreach( $value as $list_id ) { |
| 149 |
|
| 150 |
// Skip option none |
| 151 |
if( $list_id === $none_value ) { |
| 152 |
continue; |
| 153 |
} |
| 154 |
|
| 155 |
$options[$list_id] = automatorwp_fluentcrm_get_list_title( $list_id ); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
return $options; |
| 160 |
|
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Get the list title |
| 165 |
* |
| 166 |
* @since 1.0.0 |
| 167 |
* |
| 168 |
* @param int $list_id |
| 169 |
* |
| 170 |
* @return string|null |
| 171 |
*/ |
| 172 |
function automatorwp_fluentcrm_get_list_title( $list_id ) { |
| 173 |
|
| 174 |
// Empty title if no ID provided |
| 175 |
if( absint( $list_id ) === 0 ) { |
| 176 |
return ''; |
| 177 |
} |
| 178 |
|
| 179 |
global $wpdb; |
| 180 |
|
| 181 |
return $wpdb->get_var( $wpdb->prepare( |
| 182 |
"SELECT title FROM {$wpdb->prefix}fc_lists WHERE id = %s", |
| 183 |
$list_id |
| 184 |
) ); |
| 185 |
|
| 186 |
} |