/** * Export our plugin settings * * @param string $file_name The name of the file to create. */ public static function yikes_mailchimp_settings_export($file_name) { // get an array of all of our plugin settings (on the settings pages), to loop over $plugin_settings = array('yikes-mc-api-key' => yikes_get_mc_api_key(), 'yikes-mc-api-validation' => get_option('yikes-mc-api-validation', 'invalid_api_key'), 'optin-checkbox-init' => get_option('optin-checkbox-init', ''), 'yikes-mc-recaptcha-status' => get_option('yikes-mc-recaptcha-status', ''), 'yikes-mc-recaptcha-site-key' => get_option('yikes-mc-recaptcha-site-key', ''), 'yikes-mc-recaptcha-secret-key' => get_option('yikes-mc-recaptcha-secret-key', ''), 'yikes-mailchimp-debug-status' => get_option('yikes-mailchimp-debug-status', '')); $titles = array(); $content = array(); foreach ($plugin_settings as $option_name => $option_value) { $titles[] = $option_name; } // Generate the output file. $output_filename = $file_name . '-' . date('m-d-Y') . '.csv'; $output_handle = @fopen('php://output', 'w'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Content-Description: File Transfer'); header('Content-type: text/csv'); header('Content-Disposition: attachment; filename=' . $output_filename); header('Expires: 0'); header('Pragma: public'); // Parse results to csv format $first = true; foreach ($plugin_settings as $option_name => $option_value) { // Add table headers if ($first) { fputcsv($output_handle, $titles); $first = false; } $content[] = $option_value; } fputcsv($output_handle, $content); // Close output file stream fclose($output_handle); die; }
public static function check_list_for_interest_groups($list_id = '', $integration_type = '', $load = false) { if (!$list_id) { $list_id = $_POST['list_id']; } if (!$integration_type) { $integration_type = $_POST['integration']; } $api_key = yikes_get_mc_api_key(); // setup/check our transients if (WP_DEBUG || false === ($interest_groupings = get_transient($list_id . '_interest_group'))) { // It wasn't there, so regenerate the data and save the transient $dash_position = strpos($api_key, '-'); if ($dash_position !== false) { $api_endpoint = 'https://' . substr($api_key, $dash_position + 1) . '.api.mailchimp.com/2.0/lists/interest-groupings.json'; } $interest_groupings = wp_remote_post($api_endpoint, array('body' => array('apikey' => $api_key, 'id' => $list_id, 'counts' => false), 'timeout' => 10, 'sslverify' => apply_filters('yikes-mailchimp-sslverify', true))); $interest_groupings = json_decode(wp_remote_retrieve_body($interest_groupings), true); if (isset($interest_groupings['error'])) { if (get_option('yikes-mailchimp-debug-status', '') == '1') { require_once YIKES_MC_PATH . 'includes/error_log/class-yikes-inc-easy-mailchimp-error-logging.php'; $error_logging = new Yikes_Inc_Easy_Mailchimp_Error_Logging(); $error_logging->yikes_easy_mailchimp_write_to_error_log($interest_groupings['error'], __("Get Interest Groups", 'yikes-inc-easy-mailchimp-extender'), "class.ajax.php"); } } else { // set the transient for 2 hours set_transient($list_id . '_interest_group', $interest_groupings, 2 * HOUR_IN_SECONDS); } } if (isset($interest_groupings) && !empty($interest_groupings)) { require YIKES_MC_PATH . 'admin/partials/menu/options-sections/templates/integration-interest-groups.php'; } // do not kill off execution on load, only on an ajax request if (!$load) { exit; } }
<?php $api_key = yikes_get_mc_api_key(); $dash_position = strpos($api_key, '-'); if ($dash_position !== false) { $api_endpoint = 'https://' . substr($api_key, $dash_position + 1) . '.api.mailchimp.com/2.0/lists/interest-groupings.json'; } // get the interest group data $interest_groupings = wp_remote_post($api_endpoint, array('body' => array('apikey' => $api_key, 'id' => $form_data_array['list_id']), 'timeout' => 10, 'sslverify' => apply_filters('yikes-mailchimp-sslverify', true))); $interest_groupings_body = json_decode(wp_remote_retrieve_body($interest_groupings), true); if (!is_wp_error($interest_groupings_body)) { if (isset($interest_groupings_body['error'])) { if (WP_DEBUG || get_option('yikes-mailchimp-debug-status', '') == '1') { require_once YIKES_MC_PATH . 'includes/error_log/class-yikes-inc-easy-mailchimp-error-logging.php'; $error_logging = new Yikes_Inc_Easy_Mailchimp_Error_Logging(); $error_logging->yikes_easy_mailchimp_write_to_error_log($interest_groupings_body['error'], __("Get Interest Groups", 'yikes-inc-easy-mailchimp-extender'), __("Add Interest Group to Form", 'yikes-inc-easy-mailchimp-extender')); } ?> <section class="draggable" id="error-container"> <p><span class="dashicons dashicons-no-alt"></span> <?php printf(__('Error: %s', 'yikes-inc-easy-mailchimp-extender'), $interest_groupings_body['error']); ?> </p> </section> <?php return; } // find and return the location of this merge field in the array $index = $this->findMCListIndex($form_data_array['group_id'], $interest_groupings_body, 'id'); // check for our index... if (isset($index)) {
/** * Hook to submit the data to MailChimp when * a new integration type is submitted * * @since 6.0.0 **/ public function subscribe_user_integration($email, $type, $merge_vars) { // get checkbox data $checkbox_options = get_option('optin-checkbox-init', ''); if ($type != 'registration_form') { $update = '1'; } else { $update = '0'; } // set ip address if (!isset($merge_vars['OPTIN_IP']) && isset($_SERVER['REMOTE_ADDR'])) { $merge_vars['OPTIN_IP'] = sanitize_text_field($_SERVER['REMOTE_ADDR']); } // set the optin time $merge_vars['OPTIN_TIME'] = current_time('Y-m-d H:i:s', 1); // check for interest groups $interest_groups = isset($checkbox_options[$type]['interest-groups']) ? $checkbox_options[$type]['interest-groups'] : false; // if interest groups were found, push them to the merge variable array if ($interest_groups) { $merge_vars['groupings'] = array(); foreach ($interest_groups as $interest_group_id => $interest_group_selections) { // merge variable interest groups array $merge_vars['groupings'][] = array('id' => $interest_group_id, 'groups' => $interest_group_selections); } // replace the interest groups - to avoid any errors thrown if the admin switches lists, or interest groups $merge_vars['replace_interests'] = 1; } // initialize MailChimp API $api_key = yikes_get_mc_api_key(); $dash_position = strpos($api_key, '-'); if ($dash_position !== false) { $api_endpoint = 'https://' . substr($api_key, $dash_position + 1) . '.api.mailchimp.com/2.0/lists/subscribe.json'; } $subscribe_response = wp_remote_post($api_endpoint, array('body' => apply_filters('yikes-mailchimp-checkbox-integration-subscribe-api-request', array('apikey' => yikes_get_mc_api_key(), 'id' => $checkbox_options[$type]['associated-list'], 'email' => array('email' => sanitize_email($email)), 'merge_vars' => apply_filters('yikes-mailchimp-checkbox-integration-merge-variables', $merge_vars, $type), 'double_optin' => 1, 'update_existing' => $update, 'send_welcome' => 1), $type), 'timeout' => 10, 'sslverify' => apply_filters('yikes-mailchimp-sslverify', true))); if (!Is_wp_error($subscribe_response)) { $response_body = json_decode(wp_remote_retrieve_body($subscribe_response), true); if (WP_DEBUG || isset($response_body['error'])) { if (get_option('yikes-mailchimp-debug-status', '') == '1') { require_once YIKES_MC_PATH . 'includes/error_log/class-yikes-inc-easy-mailchimp-error-logging.php'; $error_logging = new Yikes_Inc_Easy_Mailchimp_Error_Logging(); $error_logging->yikes_easy_mailchimp_write_to_error_log($response_body['error'], __("Checkbox Integration Subscribe User", 'yikes-inc-easy-mailchimp-extender'), "Checkbox Integrations"); } } } return; }
public function sendUpdateProfileEmail() { $user_email = $_POST['user_email']; $list_id = $_POST['list_id']; $api_key = yikes_get_mc_api_key(); $dash_position = strpos($api_key, '-'); $explode_key = explode('-', $api_key); $data_center = $explode_key[1]; $full_site_url = get_bloginfo('url'); // list details api call if ($dash_position !== false) { $api_endpoint = 'https://' . substr($api_key, $dash_position + 1) . '.api.mailchimp.com/2.0/lists/list.json'; } $list_details = wp_remote_post($api_endpoint, array('body' => array('apikey' => $api_key, 'filters' => array('list_id' => $list_id)), 'timeout' => 10, 'sslverify' => apply_filters('yikes-mailchimp-sslverify', true))); $list_details = json_decode(wp_remote_retrieve_body($list_details), true); if (isset($list_details['error'])) { if (WP_DEBUG || get_option('yikes-mailchimp-debug-status', '') == '1') { require_once YIKES_MC_PATH . 'includes/error_log/class-yikes-inc-easy-mailchimp-error-logging.php'; $error_logging = new Yikes_Inc_Easy_Mailchimp_Error_Logging(); $error_logging->yikes_easy_mailchimp_write_to_error_log($list_details['error'], __("Send Update Profile Email - Get Account Lists", 'yikes-inc-easy-mailchimp-extender'), "class.public_ajax.php"); } } // account details api call if ($dash_position !== false) { $api_endpoint = 'https://' . substr($api_key, $dash_position + 1) . '.api.mailchimp.com/2.0/helper/account-details.json'; } $account_details = wp_remote_post($api_endpoint, array('body' => array('apikey' => $api_key), 'timeout' => 10, 'sslverify' => apply_filters('yikes-mailchimp-sslverify', true))); $account_details = json_decode(wp_remote_retrieve_body($account_details), true); if (isset($account_details['error'])) { if (WP_DEBUG || get_option('yikes-mailchimp-debug-status', '') == '1') { require_once YIKES_MC_PATH . 'includes/error_log/class-yikes-inc-easy-mailchimp-error-logging.php'; $error_logging = new Yikes_Inc_Easy_Mailchimp_Error_Logging(); $error_logging->yikes_easy_mailchimp_write_to_error_log($account_details['error'], __("Send Update Profile Email - Get Account Details", 'yikes-inc-easy-mailchimp-extender'), "class.public_ajax.php"); } } // subscriber details api call if ($dash_position !== false) { $api_endpoint = 'https://' . substr($api_key, $dash_position + 1) . '.api.mailchimp.com/2.0/lists/member-info.json'; } $subscriber_account_details = wp_remote_post($api_endpoint, array('body' => array('apikey' => $api_key, 'id' => $list_id, 'emails' => array(array('email' => $user_email))), 'timeout' => 10, 'sslverify' => apply_filters('yikes-mailchimp-sslverify', true))); $subscriber_account_details = json_decode(wp_remote_retrieve_body($subscriber_account_details), true); if (isset($subscriber_account_details['error'])) { if (WP_DEBUG || get_option('yikes-mailchimp-debug-status', '') == '1') { require_once YIKES_MC_PATH . 'includes/error_log/class-yikes-inc-easy-mailchimp-error-logging.php'; $error_logging = new Yikes_Inc_Easy_Mailchimp_Error_Logging(); $error_logging->yikes_easy_mailchimp_write_to_error_log($subscriber_account_details['error'], __("Send Update Profile Email - Get Member Info.", 'yikes-inc-easy-mailchimp-extender'), "class.public_ajax.php"); } } // check for errors in any of the calls if (isset($list_details['error']) || isset($account_details['error']) || isset($subscriber_account_details['error'])) { $error_message = isset($list_details['error']) ? $list_details['error'] : false; if (!$error_message) { $error_message = isset($account_details['error']) ? $account_details['error'] : false; if (!$error_message) { $error_message = isset($subscriber_account_details['error']) ? $subscriber_account_details['error'] : false; if (!$error_message) { $error_message = ''; } } } $errorMessage = sprintf(__('Error sending update profile email. <strong>Error: %s</strong>. Please contact the site administrator.', 'yikes-inc-easy-mailchimp-extender'), $error_message); wp_send_json_error(array('response_text' => '<div class="yikes-easy-mc-error-message">✕ ' . $errorMessage . '</div>')); return; } // send the email! $subscriber_id = $subscriber_account_details['data'][0]['id']; $explode_url = explode('.', $account_details['contact']['url']); $update_link_href = 'http://' . $explode_url[1] . '.' . $data_center . '.list-manage1.com/profile?u=' . $account_details['user_id'] . '&id=' . $list_id . '&e=' . $subscriber_id; $subject = 'MailChimp Profile Update'; $headers = 'From: ' . $list_details['data'][0]['default_from_name'] . ' <' . $list_details['data'][0]['default_from_email'] . '>' . "\r\n"; $headers .= 'Content-type: text/html'; $email_content = '<p>Greetings,</p> <p>A request has been made to update your MailChimp account profile information. To do so please use the following link: <a href="' . $update_link_href . '" title="Update MailChimp Profile">Update MailChimp Profile Info.</a>'; $email_content .= "<p>If you did not request this update, please disregard this email.</p>"; $email_content .= '<p> </p>'; $email_content .= '<p>This email was sent from : ' . $full_site_url . '</p>'; $email_content .= '<p> </p>'; $email_content .= '<p> </p>'; $email_content .= '<p style="font-size:13px;margin-top:5em;"><em>This email was generated by the <a href="http://www.wordpress.org/plugins/yikes-inc-easy-mailchimp-extender/" target="_blank">Easy Forms for MailChimp</a> plugin, created by <a href="http://www.yikesinc.com" target="_blank">YIKES Inc.</a></em></p>'; /* Confirm that the email was sent */ if (wp_mail($user_email, apply_filters('yikes-mailchimp-update-email-subject', $subject), apply_filters('yikes-mailchimp-update-email-content', $email_content, $update_link_href), $headers)) { wp_send_json_success(array('response_text' => '<div class="yikes-easy-mc-success-message">' . sprintf(__('%s Update email successfully sent. Please check your inbox for the message.', 'yikes-inc-easy-mailchimp-extender'), '✔') . '</div>')); exit; } else { wp_send_json_error(array('response_text' => '<div class="yikes-easy-mc-error-message">' . sprintf(__('%s Email failed to send. Please contact the site administrator.', 'yikes-inc-easy-mailchimp-extender'), '✕') . '</div>')); exit; } }
</strong></p> <input autocomplete="off" <?php if ($yikes_mc_api_constant) { echo 'readonly="readonly"'; } if (strlen(yikes_get_mc_api_key()) > 0) { ?> type="password" <?php } else { ?> type="text" <?php } ?> value="<?php echo yikes_get_mc_api_key(); ?> " placeholder="<?php _e('MailChimp API Key', 'yikes-inc-easy-mailchimp-extender'); ?> " name="yikes-mc-api-key" id="yikes-mc-api-key" class="settings-page-input" /> <?php if ($yikes_mc_api_constant) { ?> <p class="description"><?php printf(__("Your MailChimp API key has been defined using the %s constant, in a PHP file.", "yikes-inc-easy-mailchimp-extender"), '<code>YIKES_MC_API_KEY</code>'); ?> </p> <?php }
function yikes_mailchimp_subscriber_count_shortcode($attributes) { // Attributes extract(shortcode_atts(array('form' => '', 'list' => ''), $attributes, 'yikes-mailchimp-subscriber-count')); /* If the user hasn't authenticated yet - bail */ if (get_option('yikes-mc-api-validation', 'invalid_api_key') != 'valid_api_key') { if (WP_DEBUG) { return '<strong>' . __("You don't appear to be connected to MailChimp.", "yikes-inc-easy-mailchimp-extender") . '</strong>'; } return; } $form = !empty($attributes['form']) ? str_replace('"', '', $attributes['form']) : false; // replace the sanitize quotes to perform a proper query $list_id = !empty($attributes['list']) ? $attributes['list'] : false; /* If no list ID was passed into the shortcode - bail */ if (!$list_id && !$form) { if (WP_DEBUG) { return '<strong>' . __('You forgot to include the list or form ID.', 'yikes-inc-easy-mailchimp-extender') . '</strong>'; } return; } /* if a form ID and a list ID were passed in, use the form ID */ if ($form || $form && $list_id) { $interface = yikes_easy_mailchimp_extender_get_form_interface(); $form_data = $interface->get_form($form); // confirm we have some results, or return an error if (!$form_data) { if (WP_DEBUG) { return __("Oh no...This form doesn't exist. Head back to the manage forms page and select a different form.", 'yikes-inc-easy-mailchimp-extender'); } return; } $list_id = sanitize_key($form_data['list_id']); // associated list id (users who fill out the form will be subscribed to this list) } // object buffer ob_start(); // submit the request the get the subscriber count try { // get the api key $api_key = yikes_get_mc_api_key(); $dash_position = strpos($api_key, '-'); if ($dash_position !== false) { $api_endpoint = 'https://' . substr($api_key, $dash_position + 1) . '.api.mailchimp.com/2.0/lists/list.json'; } // run the request $subscriber_count_response = wp_remote_post($api_endpoint, array('body' => apply_filters('yikes-mailchimp-user-subscriber-count-api-request', array('apikey' => $api_key, 'filters' => array('list_id' => $list_id)), $list_id), 'timeout' => 10, 'sslverify' => apply_filters('yikes-mailchimp-sslverify', true))); $subscriber_count_response = json_decode(wp_remote_retrieve_body($subscriber_count_response), true); if (isset($subscriber_count_response['error'])) { if (WP_DEBUG || get_option('yikes-mailchimp-debug-status', '') == '1') { require_once YIKES_MC_PATH . 'includes/error_log/class-yikes-inc-easy-mailchimp-error-logging.php'; $error_logging = new Yikes_Inc_Easy_Mailchimp_Error_Logging(); $error_logging->yikes_easy_mailchimp_write_to_error_log($subscriber_count_response['error'], __("Get Account Lists", 'yikes-inc-easy-mailchimp-extender'), "yikes-mailchimp-subscriber-count.php"); } } // if more than one list is returned, something went wrong - bail if ($subscriber_count_response['total'] != 1) { if (WP_DEBUG) { return '<strong>' . sprintf(__("It looks like this list wasn't found. Double check the list with with ID '%s' exists.", "yikes-inc-easy-mailchimp-extender"), $list_id) . '</strong>'; } return; } /* type cast the returned value as an integer */ echo (int) apply_filters('yikes-mailchimp-subscriber-count-value', $subscriber_count_response['data'][0]['stats']['member_count']); } catch (Exception $error) { echo $error->getMessage(); } return ob_get_clean(); }
/** * Create the function to output our account activity dashboard widget */ function account_activity_dashboard_widget() { // Get our list data! // Check for a transient, if not - set one up for one hour if (false === ($account_activity = get_transient('yikes-easy-mailchimp-account-activity'))) { $api_key = yikes_get_mc_api_key(); $dash_position = strpos($api_key, '-'); if ($dash_position !== false) { $api_endpoint = 'https://' . substr($api_key, $dash_position + 1) . '.api.mailchimp.com/2.0/helper/chimp-chatter.json'; } $account_activity = wp_remote_post($api_endpoint, array('body' => array('apikey' => $api_key), 'timeout' => 10, 'sslverify' => apply_filters('yikes-mailchimp-sslverify', true))); $account_activity = json_decode(wp_remote_retrieve_body($account_activity), true); if (isset($account_activity['error'])) { if (WP_DEBUG || get_option('yikes-mailchimp-debug-status', '') == '1') { require_once YIKES_MC_PATH . 'includes/error_log/class-yikes-inc-easy-mailchimp-error-logging.php'; $error_logging = new Yikes_Inc_Easy_Mailchimp_Error_Logging(); $error_logging->yikes_easy_mailchimp_write_to_error_log($account_activity['error'], __("Get Account Activity", 'yikes-inc-easy-mailchimp-extender'), "Dashboard Activity Widget"); } } else { // set our transient for one hour set_transient('yikes-easy-mailchimp-account-activity', $account_activity, 1 * HOUR_IN_SECONDS); } } if (!empty($account_activity)) { include_once YIKES_MC_PATH . 'admin/partials/dashboard-widgets/templates/account-activity-template.php'; } }
/** * Return an array of MailChimp lists associated with this account * * Used when deleting the sites MailChimp cache stored * @since 6.0.2 * @return $list_id_array - array of list id's to loop over */ public function get_mailchimp_list_ids_on_account() { $api_key = yikes_get_mc_api_key(); if (!$api_key) { // if no api key is set/site is not connected, return an empty array return array(); } $dash_position = strpos($api_key, '-'); if ($dash_position !== false) { $api_endpoint = 'https://' . substr($api_key, $dash_position + 1) . '.api.mailchimp.com/2.0/lists/list.json'; } $mailchimp_lists = wp_remote_post($api_endpoint, array('body' => array('apikey' => $api_key, 'limit' => 100), 'timeout' => 10, 'sslverify' => apply_filters('yikes-mailchimp-sslverify', true))); if (!is_wp_error($mailchimp_lists)) { $list_data = json_decode(wp_remote_retrieve_body($mailchimp_lists), true); if (isset($list_data['error'])) { if (WP_DEBUG || get_option('yikes-mailchimp-debug-status', '') == '1') { require_once YIKES_MC_PATH . 'includes/error_log/class-yikes-inc-easy-mailchimp-error-logging.php'; $error_logging = new Yikes_Inc_Easy_Mailchimp_Error_Logging(); $error_logging->yikes_easy_mailchimp_write_to_error_log($list_data['error'], __("Get List IDs", 'yikes-inc-easy-mailchimp-extender'), __("Clear API Cache", 'yikes-inc-easy-mailchimp-extender')); } } $mail_chimp_list_ids = array(); if (isset($list_data['data'])) { foreach ($list_data['data'] as $list) { $mail_chimp_list_ids[] = $list['id']; } return $mail_chimp_list_ids; } else { return array(); } } return; }