# woo-mailerlite/2.0.4/includes/shared/mailerlite-wp-functions.php

MailerLite – WooCommerce integration, version 2.0.4. 299 lines.

- Page: https://pluginprobe.com/plugins/woo-mailerlite/2.0.4/code/includes/shared/mailerlite-wp-functions.php
- Raw: https://pluginprobe.com/plugins/woo-mailerlite/2.0.4/raw/includes/shared/mailerlite-wp-functions.php
- Modified: 2023-07-25T07:19:24+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/woo-mailerlite/2.0.4/code/includes/shared/mailerlite-wp-functions.php#L10-L20`.

```php
<?php

use MailerLite\Includes\Classes\Settings\ApiSettings;
use MailerLite\Includes\Shared\Api\ApiType;
use MailerLite\Includes\Shared\Api\PlatformAPI;

/**
 * Functions inside this file are being used in different MailerLite related WordPress plugins
 */

if ( ! function_exists('mailerlite_wp_get_groups')) :
    /**
     * Get groups from API
     *
     * @param $api_key
     *
     * @return array|bool
     */
    function mailerlite_wp_get_groups()
    {

        if ( ! ApiSettings::getInstance()->wpApiKeyExists()) {
            return false;
        }

        $groups = array();

        try {
            $mailerliteClient = new PlatformAPI(MAILERLITE_WP_API_KEY);

            if ($mailerliteClient->getApiType() === ApiType::INVALID) {

                return false;
            }

            $results = $mailerliteClient->getGroups();

            if ($results) {

                if (is_array($results) && ! isset($results->error->message)) {
                    if (sizeof($results) > 0) {
                        foreach ($results as $result) {
                            $groups[] = (array)$result;
                        }
                    }
                }

                return $groups;
            }

            return false;
        } catch (Exception $e) {
            return false;
        }
    }
endif;

if ( ! function_exists('mailerlite_wp_get_subscriber_by_email')) :
    /**
     * Get subscriber from API by email
     *
     * @param $email
     *
     * @return mixed
     */
    function mailerlite_wp_get_subscriber_by_email($email)
    {

        if ( ! ApiSettings::getInstance()->wpApiKeyExists()) {
            return false;
        }

        if (empty($email)) {
            return false;
        }

        try {
            $mailerliteClient = new PlatformAPI(MAILERLITE_WP_API_KEY);

            $subscriber = $mailerliteClient->searchSubscriber($email);

            if (isset($subscriber->id)) {
                return $subscriber;
            } else {
                return false;
            }
        } catch (Exception $e) {
            return false;
        }
    }
endif;

if ( ! function_exists('mailerlite_wp_update_subscriber')) :
    /**
     * Update subscriber via API
     *
     * @param $subscriber_email
     * @param array $subscriber_data
     *
     * @return mixed
     */
    function mailerlite_wp_update_subscriber($subscriber_email, $subscriber_data = array())
    {

        if ( ! ApiSettings::getInstance()->wpApiKeyExists()) {
            return false;
        }

        if (empty($subscriber_email)) {
            return false;
        }
        try {

            $mailerliteClient = new PlatformAPI(MAILERLITE_WP_API_KEY);

            $subscriber_updated = $mailerliteClient->updateSubscriber($subscriber_email,
                $subscriber_data); // returns updated subscriber
            if (isset($subscriber_updated->id)) {
                return $subscriber_updated;
            } else {
                return false;
            }
        } catch (Exception $e) {
            return false;
        }
    }
endif;

if ( ! function_exists('mailerlite_wp_sync_ecommerce_customer')) :
    function mailerlite_wp_sync_ecommerce_customer($customer_id)
    {

        $shop = get_option('woo_ml_shop_id', false);

        if ($shop === false) {

            return false;
        }

        $mailerliteClient = new PlatformAPI(MAILERLITE_WP_API_KEY);

        if ($mailerliteClient->getApiType() == ApiType::CURRENT) {

            $customer = new WC_Customer($customer_id);

            $email = $customer->get_email();

            $fields = [
                'name'         => $customer->get_first_name(),
                'last_name'    => $customer->get_last_name(),
                'company'      => $customer->get_billing_company(),
                'city'         => $customer->get_billing_city(),
                'z_i_p'        => $customer->get_billing_postcode(),
                'state'        => $customer->get_billing_state(),
                'country'      => $customer->get_billing_country(),
                'phone'        => $customer->get_billing_phone(),
                'orders_count' => $customer->get_order_count(),
                'total_spent'  => $customer->get_total_spent(),
            ];

            $mailerliteClient->syncCustomer($shop, $customer_id, $email, $fields);
        }
    }
endif;

if ( ! function_exists('mailerlite_wp_set_double_optin')) :
    /**
     * Set MailerLite double opt in status
     *
     * @param bool $status
     *
     * @return bool
     */
    function mailerlite_wp_set_double_optin($status)
    {

        if ( ! ApiSettings::getInstance()->wpApiKeyExists()) {
            return false;
        }

        try {

            $mailerliteClient = new PlatformAPI(MAILERLITE_WP_API_KEY);

            if ($mailerliteClient->getApiType() === ApiType::CLASSIC) {

                $result = $mailerliteClient->setDoubleOptin($status);

                if (isset($result->enabled)) {
                    $double_optin = $result->enabled == true ? 'yes' : 'no';
                    update_option('double_optin', $double_optin);

                    return $result->enabled;
                } else {
                    return false;
                }
            }

            if ($mailerliteClient->getApiType() === ApiType::CURRENT) {

                $doi = $mailerliteClient->getDoubleOptin();

                if ($doi !== $status) {
                    $result = $mailerliteClient->setDoubleOptin($status);

                    $double_optin = $result == true ? 'yes' : 'no';

                    update_option('double_optin', $double_optin);

                    return $result;
                }

                return $doi;
            }
        } catch (Exception $e) {
            return false;
        }
    }
endif;

if ( ! function_exists('mailerlite_wp_create_custom_field')) :
    /**
     * Create custom field in MailerLite
     *
     * @param array $field_data
     *
     * @return bool
     */
    function mailerlite_wp_create_custom_field($field_data)
    {

        if ( ! ApiSettings::getInstance()->wpApiKeyExists()) {

            return false;
        }

        if ( ! isset($field_data['title']) || ! isset($field_data['type'])) {

            return false;
        }

        try {

            $mailerliteClient = new PlatformAPI(MAILERLITE_WP_API_KEY);

            $temp_name = false;

            if (isset($field_data['key'])) {

                $temp_name = $field_data['key'] . ' ' . $field_data['title'];
            }

            $field_added = $mailerliteClient->createField($temp_name ?: $field_data['title'], $field_data['type']);

            if (isset($field_added->id)) {

                if ($mailerliteClient->getApiType() === ApiType::CURRENT) {

                    $mailerliteClient->updateField($field_added->id, $field_data['name'] . ' ' . $field_data['title']);
                }

                return $field_added;
            } else {
                return false;
            }

        } catch (Exception $e) {
            return false;
        }
    }
endif;

if ( ! function_exists('mailerlite_wp_get_custom_fields')) :
    /**
     * Get custom fields from MailerLite
     *
     * @param array $args
     *
     * @return mixed
     */
    function mailerlite_wp_get_custom_fields($args = array())
    {

        if ( ! ApiSettings::getInstance()->wpApiKeyExists()) {
            return false;
        }

        try {

            $mailerliteClient = new PlatformAPI(MAILERLITE_WP_API_KEY);

            return $mailerliteClient->getFields();

        } catch (Exception $e) {

            return false;
        }
    }
endif;
```
