| 1 |
<?php |
| 2 |
/** |
| 3 |
* Functions |
| 4 |
* |
| 5 |
* @package AutomatorWP\Integrations\MailerPress\Functions |
| 6 |
* @author AutomatorWP <[email protected]> |
| 7 |
* @since 1.0.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
// Exit if accessed directly |
| 11 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 12 |
|
| 13 |
/** |
| 14 |
* Options callback for select2 fields assigned to tags |
| 15 |
* |
| 16 |
* @since 1.0.0 |
| 17 |
* |
| 18 |
* @param stdClass $field |
| 19 |
* |
| 20 |
* @return array |
| 21 |
*/ |
| 22 |
function automatorwp_mailerpress_options_cb_tag( $field ) { |
| 23 |
|
| 24 |
// Setup vars |
| 25 |
$value = $field->escaped_value; |
| 26 |
$none_value = 'any'; |
| 27 |
$none_label = __( 'any tag', 'automatorwp' ); |
| 28 |
$options = automatorwp_options_cb_none_option( $field, $none_value, $none_label ); |
| 29 |
|
| 30 |
if( ! empty( $value ) ) { |
| 31 |
if( ! is_array( $value ) ) { |
| 32 |
$value = array( $value ); |
| 33 |
} |
| 34 |
|
| 35 |
foreach( $value as $tag_id ) { |
| 36 |
|
| 37 |
// Skip option none |
| 38 |
if( $tag_id === $none_value ) { |
| 39 |
continue; |
| 40 |
} |
| 41 |
|
| 42 |
$options[$tag_id] = automatorwp_mailerpress_get_tag_name( $tag_id ); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
return $options; |
| 47 |
|
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Get all MailerPress tags |
| 52 |
* |
| 53 |
* @since 1.0.0 |
| 54 |
* |
| 55 |
* @return array |
| 56 |
*/ |
| 57 |
function automatorwp_mailerpress_get_tags() { |
| 58 |
|
| 59 |
$tags = array(); |
| 60 |
|
| 61 |
// Bail if class does not exist |
| 62 |
if( ! class_exists ( 'MailerPress\Models\Tags' ) ) |
| 63 |
return; |
| 64 |
|
| 65 |
// Get tags data |
| 66 |
$tags_data = new MailerPress\Models\Tags; |
| 67 |
$all_tags = $tags_data->getAll(); |
| 68 |
|
| 69 |
foreach ( $all_tags as $tag ) { |
| 70 |
|
| 71 |
$tags[] = array( |
| 72 |
'id' => $tag->tag_id, |
| 73 |
'name' => $tag->name, |
| 74 |
); |
| 75 |
} |
| 76 |
|
| 77 |
return $tags; |
| 78 |
|
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Get the tag name |
| 83 |
* |
| 84 |
* @since 1.0.0 |
| 85 |
* |
| 86 |
* @param string $list_id |
| 87 |
* |
| 88 |
* @return array |
| 89 |
*/ |
| 90 |
function automatorwp_mailerpress_get_tag_name( $tag_id ) { |
| 91 |
|
| 92 |
// Empty title if no ID provided |
| 93 |
if( absint( $tag_id ) === 0 ) { |
| 94 |
return ''; |
| 95 |
} |
| 96 |
|
| 97 |
global $wpdb; |
| 98 |
|
| 99 |
$tag_name = $wpdb->get_var( "SELECT name FROM {$wpdb->prefix}mailerpress_tags WHERE tag_id = {$tag_id}" ); |
| 100 |
|
| 101 |
return $tag_name; |
| 102 |
|
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Add tag to contact |
| 107 |
* |
| 108 |
* @since 1.0.0 |
| 109 |
* |
| 110 |
* @param int $tag_id |
| 111 |
* @param int $contact_id |
| 112 |
* |
| 113 |
* @return bool |
| 114 |
*/ |
| 115 |
function automatorwp_mailerpress_add_tag_contact( $tag_id, $contact_id ) { |
| 116 |
|
| 117 |
global $wpdb; |
| 118 |
|
| 119 |
$contact_tags_table = $wpdb->prefix . 'mailerpress_contact_tags'; |
| 120 |
|
| 121 |
// Check if contact has the tag |
| 122 |
$contact_has_tag = $wpdb->get_row( |
| 123 |
$wpdb->prepare( |
| 124 |
"SELECT * FROM {$contact_tags_table} WHERE contact_id = %d AND tag_id = %d", |
| 125 |
$contact_id, |
| 126 |
$tag_id |
| 127 |
) |
| 128 |
); |
| 129 |
|
| 130 |
// Add tag to contact |
| 131 |
if ( ! $contact_has_tag ){ |
| 132 |
$insert_result = $wpdb->insert( |
| 133 |
$contact_tags_table, |
| 134 |
array( |
| 135 |
'contact_id' => $contact_id, |
| 136 |
'tag_id' => $tag_id, |
| 137 |
), |
| 138 |
array( '%d', '%d' ), |
| 139 |
); |
| 140 |
|
| 141 |
return true; |
| 142 |
} |
| 143 |
|
| 144 |
return false; |
| 145 |
|
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Add contact |
| 150 |
* |
| 151 |
* @since 1.0.0 |
| 152 |
* |
| 153 |
* @param int $args |
| 154 |
* |
| 155 |
* @return bool |
| 156 |
*/ |
| 157 |
function automatorwp_mailerpress_add_contact( $args ) { |
| 158 |
|
| 159 |
global $wpdb; |
| 160 |
|
| 161 |
$contact_table = $wpdb->prefix . 'mailerpress_contact'; |
| 162 |
|
| 163 |
// Add contact |
| 164 |
$result = $wpdb->insert( |
| 165 |
$contact_table, |
| 166 |
$args, |
| 167 |
); |
| 168 |
|
| 169 |
return (bool) $result; |
| 170 |
|
| 171 |
} |