Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
fusewp/core / src / Functions / functions.php
Size: Mime:
<?php

use FuseWP\Core\Core;
use FuseWP\Core\Integrations\AbstractIntegration;
use FuseWP\Core\Sync\Sources\AbstractSyncSource;

/**
 * Check if an admin settings page is FuseWP'
 *
 * @return bool
 */
function fusewp_is_admin_page()
{
    $fwp_admin_pages_slug = array(
        FUSEWP_SETTINGS_SETTINGS_SLUG,
        FUSEWP_SYNC_SETTINGS_SLUG
    );

    return (isset($_GET['page']) && in_array($_GET['page'], $fwp_admin_pages_slug));
}


/**
 * @param $bucket
 * @param $key
 * @param bool $default
 * @param bool $empty
 *
 * @return bool|mixed
 */
function fusewpVar($bucket, $key, $default = false, $empty = false)
{
    if ($empty) {
        return ! empty($bucket[$key]) ? $bucket[$key] : $default;
    }

    return isset($bucket[$key]) ? $bucket[$key] : $default;
}

function fusewpVarObj($bucket, $key, $default = false, $empty = false)
{
    if ($empty) {
        return ! empty($bucket->$key) ? $bucket->$key : $default;
    }

    return isset($bucket->$key) ? $bucket->$key : $default;
}

function fusewpVarPOST($key, $default = false, $empty = false)
{
    if ($empty) {
        return ! empty($_POST[$key]) ? $_POST[$key] : $default;
    }

    return isset($_POST[$key]) ? $_POST[$key] : $default;
}

function fusewpVarGET($key, $default = false, $empty = false)
{
    if ($empty) {
        return ! empty($_GET[$key]) ? $_GET[$key] : $default;
    }

    return isset($_GET[$key]) ? $_GET[$key] : $default;
}

function fusewp_sanitize_key($key)
{
    return str_replace('-', '_', sanitize_key($key));
}

/**
 * @param $integration_id
 *
 * @return AbstractIntegration[]|AbstractIntegration|false
 */
function fusewp_get_registered_integrations($integration_id = '')
{
    $integrations = apply_filters('fusewp_registered_integrations', []);

    if ( ! empty($integration_id)) {
        return fusewpVar($integrations, $integration_id);
    }

    return $integrations;
}

/**
 * Get list of integrations with Sync support.
 *
 * @param string $integration_id
 *
 * @return AbstractIntegration[]|AbstractIntegration|false
 */
function fusewp_get_registered_sync_integrations($integration_id = '')
{
    $bucket = array_filter(fusewp_get_registered_integrations(), function (AbstractIntegration $integration) {
        return in_array(AbstractIntegration::SYNC_SUPPORT, $integration::features_support());
    });

    if ( ! empty($integration_id)) {
        return fusewpVar($bucket, $integration_id);
    }

    return $bucket;
}

/**
 * @param string $source_id
 *
 * @return AbstractSyncSource[]|AbstractSyncSource|false
 */
function fusewp_get_registered_sync_sources($source_id = '')
{
    $sources = apply_filters('fusewp_registered_sync_sources', []);

    if ( ! empty($source_id)) {
        return fusewpVar($sources, $source_id);
    }

    return $sources;
}

function fusewp_clean($var, $callback = 'sanitize_text_field')
{
    if (is_array($var)) {
        return array_map('fusewp_clean', $var);
    } else {
        return is_scalar($var) ? call_user_func($callback, $var) : $var;
    }
}

/**
 * @param $cache_key
 * @param $callback
 *
 * @return bool|mixed
 * @todo add support for using transient for cache
 */
function fusewp_cache_transform($cache_key, $callback)
{
    if (is_customize_preview()) return $callback();

    static $cache_transform_bucket = [];

    $result = fusewpVar($cache_transform_bucket, $cache_key, false);

    if ( ! $result) {

        $result = $callback();

        $cache_transform_bucket[$cache_key] = $result;
    }

    return $result;
}

function fusewp_content_http_redirect($myURL)
{
    ?>
    <script type="text/javascript">
        window.location.href = "<?php echo $myURL;?>"
    </script>
    <meta http-equiv="refresh" content="0; url=<?php echo $myURL; ?>">
    Please wait while you are redirected...or
    <a href="<?php echo $myURL; ?>">Click Here</a> if you do not want to wait.
    <?php
}

/**
 * @param string $integration_id
 * @param string $error
 *
 * @return void
 */
function fusewp_log_error($integration_id, $error)
{
    global $wpdb;

    $wpdb->insert(
        Core::sync_log_db_table(),
        [
            'error_message' => $error,
            'integration'   => $integration_id,
            'date'          => current_time('mysql', true),
        ],
        [
            '%s',
            '%s',
            '%s'
        ]
    );
}

function fusewp_delete_error_log($log_id)
{
    global $wpdb;

    return $wpdb->delete(
        Core::sync_log_db_table(),
        ['id' => $log_id],
        ['%d']
    );
}

/**
 * @param $template
 * @param $vars
 * @param $return
 *
 * @return string|void
 */
function fusewp_render_view($template, $vars = [], $return = false)
{
    $parentDir = dirname(__FILE__, 2) . '/Admin/SettingsPage/views/';

    $path = $parentDir . $template . '.php';

    extract($vars);
    ob_start();
    require apply_filters('fusewp_render_view', $path, $vars, $template, $parentDir);
    $output = apply_filters('fusewp_render_view_output', ob_get_clean(), $template, $vars, $parentDir);
    if ($return) {
        return $output;
    }
    echo $output;
}

/**
 * @param $source
 *
 * @return bool
 */
function fusewp_sync_rule_source_exists($source)
{
    global $wpdb;

    $table = Core::sync_rule_db_table();

    $result = $wpdb->get_var(
        $wpdb->prepare("SELECT source FROM $table WHERE source = %s", sanitize_text_field($source))
    );

    return ! is_null($result);
}

function fusewp_sync_get_real_source_id($source_id)
{
    if (strpos($source_id, '|') !== false) {
        $source_id = fusewpVar(explode('|', $source_id), 0);
    }

    return $source_id;
}

function fusewp_sync_get_source_item_id($source_id)
{
    if (strpos($source_id, '|') !== false) {
        return fusewpVar(explode('|', $source_id), 1);
    }

    return false;
}

/**
 * @param int $rule_id
 *
 * @return mixed
 */
function fusewp_sync_get_rule($rule_id)
{
    global $wpdb;

    $table = Core::sync_rule_db_table();

    return $wpdb->get_row(
        $wpdb->prepare("SELECT * FROM {$table} WHERE id = %d", absint($rule_id))
    );
}

/**
 * @param int $rule_id
 *
 * @return int|false
 */
function fusewp_sync_delete_rule($rule_id)
{
    global $wpdb;

    return $wpdb->delete(
        Core::sync_rule_db_table(),
        ['id' => $rule_id],
        ['%d']
    );
}

/**
 * @param string $source
 *
 * @return mixed|null
 */
function fusewp_sync_get_rule_by_source($source, $status = 'any')
{
    global $wpdb;

    $table = Core::sync_rule_db_table();

    $sql = "SELECT * FROM {$table} WHERE source = %s";

    $replacement = [sanitize_text_field($source)];

    if ($status !== 'any' && in_array($status, ['active', 'disabled'])) {
        $sql           .= ' AND status = %s';
        $replacement[] = $status;
    }

    return $wpdb->get_row($wpdb->prepare($sql, $replacement), ARRAY_A);
}

/**
 * @param mixed $postedData
 *
 * @return int|WP_Error|false
 */
function fusewp_add_sync_rule_settings($postedData)
{
    if (fusewp_sync_rule_source_exists($postedData['fusewp_sync_source'])) {

        return new \WP_Error(
            'fusewp_sync_rule_exist',
            esc_html__('Sync rule for the selected source already exist.', 'fusewp')
        );
    }

    global $wpdb;

    $insert = $wpdb->insert(
        Core::sync_rule_db_table(),
        [
            'source'       => sanitize_text_field(fusewpVar($postedData, 'fusewp_sync_source', '')),
            'destinations' => wp_json_encode(fusewp_clean(fusewpVar($postedData, 'fusewp_sync_destinations', ''))),
            'status'       => sanitize_text_field($postedData['sync_status'])
        ],
        [
            '%s',
            '%s',
            '%s'
        ]
    );

    return ! $insert ? false : $wpdb->insert_id;
}

/**
 * @param int $rule_id
 * @param mixed $postedData
 *
 * @return false|int
 */
function fusewp_update_sync_rule_settings($rule_id, $postedData)
{
    global $wpdb;

    $result = $wpdb->update(
        Core::sync_rule_db_table(),
        [
            'source'       => sanitize_text_field(fusewpVar($postedData, 'fusewp_sync_source', '')),
            'destinations' => wp_json_encode(fusewp_clean(fusewpVar($postedData, 'fusewp_sync_destinations', ''))),
            'status'       => sanitize_text_field($postedData['sync_status'])
        ],
        ['id' => intval($rule_id)],
        [
            '%s',
            '%s',
            '%s'
        ],
        ['%d']
    );

    return $result !== false ? $rule_id : $result;
}

/**
 * @param $rule_id
 * @param $status
 *
 * @return bool|int
 */
function fusewp_sync_update_rule_status($rule_id, $status)
{
    global $wpdb;

    if (in_array($status, ['active', 'disabled'])) {

        return $wpdb->update(
            Core::sync_rule_db_table(),
            ['status' => sanitize_text_field($status)],
            ['id' => $rule_id],
            ['%s'],
            ['%d']
        );
    }

    return false;
}

function fusewp_get_ip_address()
{
    $user_ip = '127.0.0.1';

    $keys = array(
        'HTTP_CLIENT_IP',
        'HTTP_X_FORWARDED_FOR',
        'HTTP_X_FORWARDED',
        'HTTP_X_CLUSTER_CLIENT_IP',
        'HTTP_FORWARDED_FOR',
        'HTTP_FORWARDED',
        'REMOTE_ADDR',
    );

    foreach ($keys as $key) {
        // Bail if the key doesn't exists.
        if ( ! isset($_SERVER[$key])) {
            continue;
        }

        if ($key == 'HTTP_X_FORWARDED_FOR' && ! empty($_SERVER[$key])) {
            //to check ip is pass from proxy
            // can include more than 1 ip, first is the public one
            $_SERVER[$key] = explode(',', $_SERVER[$key]);
            $_SERVER[$key] = $_SERVER[$key][0];
        }

        // Bail if the IP is not valid.
        if ( ! filter_var(wp_unslash(trim($_SERVER[$key])), FILTER_VALIDATE_IP)) {
            continue;
        }

        $user_ip = str_replace('::1', '127.0.0.1', $_SERVER[$key]);
    }

    return apply_filters('fusewp_ip_address', $user_ip);
}

function fusewp_is_boolean($maybe_bool)
{
    if (is_bool($maybe_bool)) return true;

    if (is_string($maybe_bool)) {
        $maybe_bool = strtolower($maybe_bool);

        $valid_boolean_values = array(
            'false',
            'true',
            '0',
            '1',
        );

        return in_array($maybe_bool, $valid_boolean_values, true);
    }

    if (is_int($maybe_bool)) return in_array($maybe_bool, array(0, 1), true);

    return false;
}

function fusewp_is_valid_data($value)
{
    return fusewp_is_boolean($value) || is_int($value) || ! empty($value);
}

/**
 * Check if HTTP status code is successful.
 *
 * @param int $code
 *
 * @return bool
 */
function fusewp_is_http_code_success($code)
{
    $code = intval($code);

    return $code >= 200 && $code <= 299;
}

function fusewp_selected($selected, $current = true, $display = true)
{
    if (is_array($selected)) {
        $result = in_array($current, $selected) ? ' selected="selected"' : '';
    } else {
        $result = selected($selected, $current, false);
    }

    if ( ! $display) return $result;

    echo $result;
}

function fusewp_is_premium()
{
    return class_exists('\FuseWP\Libsodium\Libsodium') &&
           defined('FUSEWP_DETACH_LIBSODIUM');
}