function ai_loop_start_hook($query)
{
    if (!$query->is_main_query()) {
        return;
    }
    if (is_feed()) {
        return;
    }
    //get post published date
    $publish_date = get_the_date('U');
    //get referer
    $http_referer = '';
    if (isset($_SERVER['HTTP_REFERER'])) {
        $http_referer = $_SERVER['HTTP_REFERER'];
    }
    $meta_value = get_post_meta(get_the_ID(), '_adinserter_block_exceptions', true);
    $selected_blocks = explode(",", $meta_value);
    $ad_code = "";
    for ($block_index = 1; $block_index <= AD_INSERTER_BLOCKS; $block_index++) {
        $obj = new ai_Block($block_index);
        $obj->load_options($block_index);
        $display_for_users = $obj->get_display_for_users();
        if ($display_for_users == AD_DISPLAY_LOGGED_IN_USERS && !is_user_logged_in()) {
            continue;
        }
        if ($display_for_users == AD_DISPLAY_NOT_LOGGED_IN_USERS && is_user_logged_in()) {
            continue;
        }
        $display_for_devices = $obj->get_display_for_devices();
        if ($display_for_devices == AD_DISPLAY_DESKTOP_DEVICES && !AI_DESKTOP) {
            continue;
        }
        if ($display_for_devices == AD_DISPLAY_MOBILE_DEVICES && !AI_MOBILE) {
            continue;
        }
        if ($display_for_devices == AD_DISPLAY_TABLET_DEVICES && !AI_TABLET) {
            continue;
        }
        if ($display_for_devices == AD_DISPLAY_PHONE_DEVICES && !AI_PHONE) {
            continue;
        }
        if ($display_for_devices == AD_DISPLAY_DESKTOP_TABLET_DEVICES && !(AI_DESKTOP || AI_TABLET)) {
            continue;
        }
        if ($display_for_devices == AD_DISPLAY_DESKTOP_PHONE_DEVICES && !(AI_DESKTOP || AI_PHONE)) {
            continue;
        }
        if ($obj->get_display_type() != AD_SELECT_BEFORE_TITLE) {
            continue;
        }
        if (is_front_page()) {
            if (!$obj->get_display_settings_home()) {
                continue;
            }
        } elseif (is_page()) {
            if (!$obj->get_display_settings_page()) {
                continue;
            }
            $enabled_on_text = $obj->get_ad_enabled_on_which_pages();
            if ($enabled_on_text == AD_ENABLED_ON_ALL_EXCEPT_ON_SELECTED) {
                if (in_array($obj->number, $selected_blocks)) {
                    continue;
                }
            } elseif ($enabled_on_text == AD_ENABLED_ONLY_ON_SELECTED) {
                if (!in_array($obj->number, $selected_blocks)) {
                    continue;
                }
            }
        } elseif (is_single()) {
            if (!$obj->get_display_settings_post()) {
                continue;
            }
            $enabled_on_text = $obj->get_ad_enabled_on_which_posts();
            if ($enabled_on_text == AD_ENABLED_ON_ALL_EXCEPT_ON_SELECTED) {
                if (in_array($obj->number, $selected_blocks)) {
                    continue;
                }
            } elseif ($enabled_on_text == AD_ENABLED_ONLY_ON_SELECTED) {
                if (!in_array($obj->number, $selected_blocks)) {
                    continue;
                }
            }
        } elseif (is_category()) {
            if (!$obj->get_display_settings_category()) {
                continue;
            }
        } elseif (is_search()) {
            if (!$obj->get_display_settings_search()) {
                continue;
            }
        } elseif (is_archive()) {
            if (!$obj->get_display_settings_archive()) {
                continue;
            }
        }
        //if empty data, continue with next
        if ($obj->get_ad_data() == AD_EMPTY_DATA) {
            continue;
        }
        if (ai_isCategoryAllowed($obj->get_ad_block_cat(), $obj->get_ad_block_cat_type()) == false) {
            continue;
        }
        if (ai_isTagAllowed($obj->get_ad_block_tag(), $obj->get_ad_block_tag_type()) == false) {
            continue;
        }
        if (ai_isUrlAllowed($obj->get_ad_url_list(), $obj->get_ad_url_list_type()) == false) {
            continue;
        }
        if (ai_isDisplayDateAllowed($obj, $publish_date) == false) {
            continue;
        }
        if (ai_isRefererAllowed($obj, $http_referer, $obj->get_ad_domain_list_type()) == false) {
            continue;
        }
        $block_class_name = get_block_class_name();
        if ($obj->get_alignment_type() == AD_ALIGNMENT_NO_WRAPPING) {
            $ad_code .= ai_getAdCode($obj);
        } else {
            $ad_code .= "<div class='" . $block_class_name . " " . $block_class_name . "-" . $block_index . "' style='" . $obj->get_alignmet_style() . "'>" . ai_getAdCode($obj) . "</div>";
        }
    }
    echo $ad_code;
}