<?php

/**
 * Implements hook_ctools_plugin_directory().
 *
 * @param $owner
 * @param $plugin_type
 *
 * @return string
 */
function mkbh_ads_ctools_plugin_directory($owner, $plugin_type)
{
    if ($owner == 'ctools' && in_array($plugin_type, ['content_types'])) {
        return 'ctools/plugins/' . $plugin_type;
    }

    return NULL;
}

/**
 * Helper function to render DFP Ad Tag.
 *
 * @param $ad_unit
 *
 * @return string
 */
function mkbh_ads_dfp_tag($ad_unit)
{
    if (mkbh_ads_is_hide_ads_for_user()) {
        return '';
    }

    $build = [];

    $tag = dfp_tag_load($ad_unit);
    if (!isset($tag->disabled) || $tag->disabled == FALSE) {
        $build = dfp_tag($ad_unit);
    }

    return render($build);
}

/**
 * Check user plan
 *
 * @return bool
 */
function mkbh_ads_is_hide_ads_for_user()
{
    global $user;

    if ($user->uid > 0) {
        $plansToHideAdsFor = \Illuminate\Support\Arr::except(
            array_flip(
                mkbh_general_stripe_plan_ids()
            ),
            MKBH_DONATE_PERSONAL_BASIS_PLAN_ID
        );

        return database()
                ->table('mkbh_stripe_subscriptions')
                ->where('user_id', '=', $user->uid)
                ->whereIn('plan_id', array_keys($plansToHideAdsFor))
                ->count() > 0;
    }

    return false;
}

/**
 * Implements hook_preprocess_html
 *
 * @param $vars
 */
function mkbh_ads_preprocess_html(&$vars)
{
    if (mkbh_ads_is_hide_ads_for_user()) {
        $vars['classes_array'][] = 'hide-ads-from-user';
    }
}
