Example #1
0
function adrotate_group($group_ids, $fallback = 0, $weight = 0, $site = 0)
{
    global $wpdb, $adrotate_config, $adrotate_debug;
    $output = $group_select = '';
    if ($group_ids) {
        $now = adrotate_now();
        $group_array = !is_array($group_ids) ? explode(",", $group_ids) : $group_ids;
        foreach ($group_array as $key => $value) {
            $group_select .= " `{$wpdb->prefix}adrotate_linkmeta`.`group` = {$value} OR";
        }
        $group_select = rtrim($group_select, " OR");
        $group = $wpdb->get_row($wpdb->prepare("SELECT * FROM `{$wpdb->prefix}adrotate_groups` WHERE `name` != '' AND `id` = %d;", $group_array[0]));
        if ($adrotate_debug['general'] == true) {
            echo "<p><strong>[DEBUG][adrotate_group] Selected group</strong><pre>";
            print_r($group);
            echo "</pre></p>";
        }
        if ($group) {
            // Get all ads in all selected groups
            $ads = $wpdb->get_results("SELECT \n\t\t\t\t\t`{$wpdb->prefix}adrotate`.`id`, \n\t\t\t\t\t`{$wpdb->prefix}adrotate`.`title`, \n\t\t\t\t\t`{$wpdb->prefix}adrotate`.`bannercode`, \n\t\t\t\t\t`{$wpdb->prefix}adrotate`.`image`, \n\t\t\t\t\t`{$wpdb->prefix}adrotate`.`responsive`, \n\t\t\t\t\t`{$wpdb->prefix}adrotate`.`tracker`, \n\t\t\t\t\t`{$wpdb->prefix}adrotate_linkmeta`.`group`\n\t\t\t\tFROM \n\t\t\t\t\t`{$wpdb->prefix}adrotate`, \n\t\t\t\t\t`{$wpdb->prefix}adrotate_linkmeta` \n\t\t\t\tWHERE \n\t\t\t\t\t({$group_select}) \n\t\t\t\t\tAND `{$wpdb->prefix}adrotate_linkmeta`.`user` = 0 \n\t\t\t\t\tAND `{$wpdb->prefix}adrotate`.`id` = `{$wpdb->prefix}adrotate_linkmeta`.`ad` \n\t\t\t\t\tAND (`{$wpdb->prefix}adrotate`.`type` = 'active' \n\t\t\t\t\t\tOR `{$wpdb->prefix}adrotate`.`type` = '2days'\n\t\t\t\t\t\tOR `{$wpdb->prefix}adrotate`.`type` = '7days')\n\t\t\t\tGROUP BY `{$wpdb->prefix}adrotate`.`id`\n\t\t\t\tORDER BY `{$wpdb->prefix}adrotate`.`id`;");
            if ($ads) {
                if ($adrotate_debug['general'] == true) {
                    echo "<p><strong>[DEBUG][adrotate_group()] All ads in group</strong><pre>";
                    print_r($ads);
                    echo "</pre></p>";
                }
                foreach ($ads as $ad) {
                    $selected[$ad->id] = $ad;
                    $selected = adrotate_filter_schedule($selected, $ad);
                }
                unset($ads);
                if ($adrotate_debug['general'] == true) {
                    echo "<p><strong>[DEBUG][adrotate_group] Reduced array based on schedule restrictions</strong><pre>";
                    print_r($selected);
                    echo "</pre></p>";
                }
                $array_count = count($selected);
                if ($array_count > 0) {
                    $before = $after = '';
                    $before = str_replace('%id%', $group_array[0], stripslashes(html_entity_decode($group->wrapper_before, ENT_QUOTES)));
                    $after = str_replace('%id%', $group_array[0], stripslashes(html_entity_decode($group->wrapper_after, ENT_QUOTES)));
                    $output .= '<div class="g g-' . $group->id . '">';
                    // Kill dynamic mode for mobile users
                    if ($adrotate_config['mobile_dynamic_mode'] == 'Y' and $group->modus == 1 and (adrotate_is_mobile() or adrotate_is_tablet())) {
                        $group->modus = 0;
                    }
                    if ($group->modus == 1) {
                        // Dynamic ads
                        $i = 1;
                        // Limit group to save resources
                        $amount = $group->adspeed >= 10000 ? 10 : 20;
                        // Randomize and trim output
                        $selected = adrotate_shuffle($selected);
                        foreach ($selected as $key => $banner) {
                            if ($i <= $amount) {
                                $image = str_replace('%folder%', $adrotate_config['banner_folder'], $banner->image);
                                $output .= '<div class="g-dyn a-' . $banner->id . ' c-' . $i . '">';
                                $output .= $before . adrotate_ad_output($banner->id, $group->id, $banner->title, $banner->bannercode, $banner->tracker, $image, $banner->responsive) . $after;
                                $output .= '</div>';
                                $i++;
                            }
                        }
                    } else {
                        if ($group->modus == 2) {
                            // Block of ads
                            $block_count = $group->gridcolumns * $group->gridrows;
                            if ($array_count < $block_count) {
                                $block_count = $array_count;
                            }
                            $columns = 1;
                            for ($i = 1; $i <= $block_count; $i++) {
                                $banner_id = array_rand($selected, 1);
                                $image = str_replace('%folder%', $adrotate_config['banner_folder'], $selected[$banner_id]->image);
                                $output .= '<div class="g-col b-' . $group->id . ' a-' . $selected[$banner_id]->id . '">';
                                $output .= $before . adrotate_ad_output($selected[$banner_id]->id, $group->id, $selected[$banner_id]->title, $selected[$banner_id]->bannercode, $selected[$banner_id]->tracker, $image, $selected[$banner_id]->responsive) . $after;
                                $output .= '</div>';
                                if ($columns == $group->gridcolumns and $i != $block_count) {
                                    $output .= '</div><div class="g g-' . $group->id . '">';
                                    $columns = 1;
                                } else {
                                    $columns++;
                                }
                                if ($adrotate_config['stats'] == 1) {
                                    adrotate_count_impression($selected[$banner_id]->id, $group->id, 0, $adrotate_config['impression_timer']);
                                }
                                unset($selected[$banner_id]);
                            }
                        } else {
                            // Default (single ad)
                            $banner_id = array_rand($selected, 1);
                            $image = str_replace('%folder%', $adrotate_config['banner_folder'], $selected[$banner_id]->image);
                            $output .= '<div class="g-single a-' . $selected[$banner_id]->id . '">';
                            $output .= $before . adrotate_ad_output($selected[$banner_id]->id, $group->id, $selected[$banner_id]->title, $selected[$banner_id]->bannercode, $selected[$banner_id]->tracker, $image, $selected[$banner_id]->responsive) . $after;
                            $output .= '</div>';
                            if ($adrotate_config['stats'] == 1) {
                                adrotate_count_impression($selected[$banner_id]->id, $group->id, 0, $adrotate_config['impression_timer']);
                            }
                        }
                    }
                    $output .= '</div>';
                    unset($selected);
                } else {
                    $output .= adrotate_error('ad_expired');
                }
            } else {
                $output .= adrotate_error('ad_unqualified');
            }
        } else {
            $output .= adrotate_error('group_not_found', array($group_array[0]));
        }
    } else {
        $output .= adrotate_error('group_no_id');
    }
    return $output;
}
function adrotate_group($group_ids, $fallback = 0, $weight = 0, $site = 0)
{
    global $wpdb, $adrotate_config, $adrotate_debug;
    $output = $group_select = $weightoverride = $mobileoverride = '';
    if ($group_ids) {
        $license = get_site_option('adrotate_activate');
        if ($site > 0 and adrotate_is_networked() and ($license['type'] == 'Network' or $license['type'] == 'Developer')) {
            $current_blog = $wpdb->blogid;
            switch_to_blog($site);
        }
        $now = adrotate_now();
        $group_array = !is_array($group_ids) ? explode(",", $group_ids) : $group_ids;
        foreach ($group_array as $key => $value) {
            $group_select .= " `{$wpdb->prefix}adrotate_linkmeta`.`group` = {$value} OR";
        }
        $group_select = rtrim($group_select, " OR");
        // Grab settings to use from first group
        $group = $wpdb->get_row($wpdb->prepare("SELECT * FROM `{$wpdb->prefix}adrotate_groups` WHERE `name` != '' AND `id` = %d;", $group_array[0]));
        if ($adrotate_debug['general'] == true) {
            echo "<p><strong>[DEBUG][adrotate_group] Group</strong><pre>";
            echo "Group ID: " . $group->id . ', ' . $group->name;
            echo "<br />Group mobile: ";
            echo $group->mobile == 1 ? "yes" : "no";
            echo "<br />User is mobile: ";
            echo adrotate_is_mobile() ? "yes" : "no";
            echo "<br />User is tablet: ";
            echo adrotate_is_tablet() ? "yes" : "no";
            echo "</pre></p>";
        }
        if ($group) {
            if ($group->mobile == 1) {
                if (!adrotate_is_mobile() and !adrotate_is_tablet()) {
                    // Desktop
                    $mobileoverride = "AND `{$wpdb->prefix}adrotate`.`desktop` = 'Y'";
                } else {
                    if (adrotate_is_mobile()) {
                        // Phones
                        $mobileoverride = "AND `{$wpdb->prefix}adrotate`.`mobile` = 'Y'";
                    } else {
                        if (adrotate_is_tablet()) {
                            // Tablets
                            $mobileoverride = "AND `{$wpdb->prefix}adrotate`.`tablet` = 'Y'";
                        }
                    }
                }
            }
            $weightoverride = $weight > 0 ? "AND `{$wpdb->prefix}adrotate`.`weight` >= {$weight} " : '';
            $fallback = $fallback == 0 ? $group->fallback : $fallback;
            // Get all ads in all selected groups
            $ads = $wpdb->get_results("SELECT \n\t\t\t\t\t`{$wpdb->prefix}adrotate`.`id`, \n\t\t\t\t\t`{$wpdb->prefix}adrotate`.`title`, \n\t\t\t\t\t`{$wpdb->prefix}adrotate`.`bannercode`, \n\t\t\t\t\t`{$wpdb->prefix}adrotate`.`image`, \n\t\t\t\t\t`{$wpdb->prefix}adrotate`.`responsive`, \n\t\t\t\t\t`{$wpdb->prefix}adrotate`.`tracker`, \n\t\t\t\t\t`{$wpdb->prefix}adrotate`.`weight`,\n\t\t\t\t\t`{$wpdb->prefix}adrotate`.`crate`, \n\t\t\t\t\t`{$wpdb->prefix}adrotate`.`irate`, \n\t\t\t\t\t`{$wpdb->prefix}adrotate`.`budget`, \n\t\t\t\t\t`{$wpdb->prefix}adrotate`.`cities`, \n\t\t\t\t\t`{$wpdb->prefix}adrotate`.`countries`,\n\t\t\t\t\t`{$wpdb->prefix}adrotate_linkmeta`.`group`\n\t\t\t\tFROM \n\t\t\t\t\t`{$wpdb->prefix}adrotate`, \n\t\t\t\t\t`{$wpdb->prefix}adrotate_linkmeta` \n\t\t\t\tWHERE \n\t\t\t\t\t({$group_select}) \n\t\t\t\t\tAND `{$wpdb->prefix}adrotate_linkmeta`.`user` = 0 \n\t\t\t\t\tAND `{$wpdb->prefix}adrotate`.`id` = `{$wpdb->prefix}adrotate_linkmeta`.`ad` \n\t\t\t\t\t{$mobileoverride}\n\t\t\t\t\t{$weightoverride}\n\t\t\t\t\tAND (`{$wpdb->prefix}adrotate`.`type` = 'active' \n\t\t\t\t\t\tOR `{$wpdb->prefix}adrotate`.`type` = '2days' \n\t\t\t\t\t\tOR `{$wpdb->prefix}adrotate`.`type` = '7days') \n\t\t\t\tGROUP BY `{$wpdb->prefix}adrotate`.`id` \n\t\t\t\tORDER BY `{$wpdb->prefix}adrotate`.`id`;");
            if ($ads) {
                if ($adrotate_debug['general'] == true) {
                    echo "<p><strong>[DEBUG][adrotate_group()] Selected ads</strong><pre>";
                    foreach ($ads as $ad) {
                        print_r($ad->id . ', ' . $ad->title . '<br>');
                    }
                    echo "</pre></p>";
                }
                foreach ($ads as $ad) {
                    $selected[$ad->id] = $ad;
                    $selected = adrotate_filter_schedule($selected, $ad);
                    if ($adrotate_config['enable_advertisers'] == 'Y' and ($ad->crate > 0 or $ad->irate > 0)) {
                        $selected = adrotate_filter_budget($selected, $ad);
                    }
                    if ($adrotate_config['enable_geo'] > 0 and $group->geo == 1) {
                        $selected = adrotate_filter_location($selected, $ad);
                    }
                }
                if ($adrotate_debug['general'] == true) {
                    echo "<p><strong>[DEBUG][adrotate_group] Reduced array based on settings</strong><pre>";
                    print_r($selected);
                    echo "</pre></p>";
                }
                $array_count = count($selected);
                if ($array_count > 0) {
                    $before = $after = '';
                    $before = str_replace('%id%', $group_array[0], stripslashes(html_entity_decode($group->wrapper_before, ENT_QUOTES)));
                    $after = str_replace('%id%', $group_array[0], stripslashes(html_entity_decode($group->wrapper_after, ENT_QUOTES)));
                    $output .= '<div class="g' . $adrotate_config['adblock_disguise'] . ' g' . $adrotate_config['adblock_disguise'] . '-' . $group->id . '">';
                    // Kill dynamic mode for mobile users
                    if ($adrotate_config['mobile_dynamic_mode'] == 'Y' and $group->modus == 1 and (adrotate_is_mobile() or adrotate_is_tablet())) {
                        $group->modus = 0;
                    }
                    if ($group->modus == 1) {
                        // Dynamic ads
                        $i = 1;
                        // Limit group to save resources
                        $amount = $group->adspeed >= 10000 ? 10 : 20;
                        // Randomize and trim output
                        $selected = adrotate_shuffle($selected);
                        foreach ($selected as $key => $banner) {
                            if ($i <= $amount) {
                                $image = str_replace('%folder%', $adrotate_config['banner_folder'], $banner->image);
                                $output .= '<div class="g' . $adrotate_config['adblock_disguise'] . '-dyn a' . $adrotate_config['adblock_disguise'] . '-' . $banner->id . ' c-' . $i . '">';
                                $output .= $before . adrotate_ad_output($banner->id, $group->id, $banner->title, $banner->bannercode, $banner->tracker, $image, $banner->responsive) . $after;
                                $output .= '</div>';
                                $i++;
                            }
                        }
                    } else {
                        if ($group->modus == 2) {
                            // Block of ads
                            $block_count = $group->gridcolumns * $group->gridrows;
                            if ($array_count < $block_count) {
                                $block_count = $array_count;
                            }
                            $columns = 1;
                            for ($i = 1; $i <= $block_count; $i++) {
                                $banner_id = adrotate_pick_weight($selected);
                                $image = str_replace('%folder%', $adrotate_config['banner_folder'], $selected[$banner_id]->image);
                                $output .= '<div class="g' . $adrotate_config['adblock_disguise'] . '-col b' . $adrotate_config['adblock_disguise'] . '-' . $group->id . ' a' . $adrotate_config['adblock_disguise'] . '-' . $selected[$banner_id]->id . '">';
                                $output .= $before . adrotate_ad_output($selected[$banner_id]->id, $group->id, $selected[$banner_id]->title, $selected[$banner_id]->bannercode, $selected[$banner_id]->tracker, $image, $selected[$banner_id]->responsive) . $after;
                                $output .= '</div>';
                                if ($columns == $group->gridcolumns and $i != $block_count) {
                                    $output .= '</div><div class="g' . $adrotate_config['adblock_disguise'] . ' g' . $adrotate_config['adblock_disguise'] . '-' . $group->id . '">';
                                    $columns = 1;
                                } else {
                                    $columns++;
                                }
                                if ($adrotate_config['stats'] == 1 and $selected[$banner_id]->tracker == "Y") {
                                    adrotate_count_impression($selected[$banner_id]->id, $group->id, $site, $adrotate_config['impression_timer']);
                                }
                                unset($selected[$banner_id]);
                            }
                        } else {
                            // Default (single ad)
                            $banner_id = adrotate_pick_weight($selected);
                            $image = str_replace('%folder%', $adrotate_config['banner_folder'], $selected[$banner_id]->image);
                            $output .= '<div class="g' . $adrotate_config['adblock_disguise'] . '-single a' . $adrotate_config['adblock_disguise'] . '-' . $selected[$banner_id]->id . '">';
                            $output .= $before . adrotate_ad_output($selected[$banner_id]->id, $group->id, $selected[$banner_id]->title, $selected[$banner_id]->bannercode, $selected[$banner_id]->tracker, $image, $selected[$banner_id]->responsive) . $after;
                            $output .= '</div>';
                            if ($adrotate_config['stats'] == 1 and $selected[$banner_id]->tracker == "Y") {
                                adrotate_count_impression($selected[$banner_id]->id, $group->id, $site, $adrotate_config['impression_timer']);
                            }
                        }
                    }
                    $output .= '</div>';
                    unset($selected);
                } else {
                    if ($site > 0 and adrotate_is_networked() and ($license['type'] == 'Network' or $license['type'] == 'Developer')) {
                        switch_to_blog($current_blog);
                    }
                    $output .= adrotate_fallback($fallback, 'expired', $site);
                }
            } else {
                if ($site > 0 and adrotate_is_networked() and ($license['type'] == 'Network' or $license['type'] == 'Developer')) {
                    switch_to_blog($current_blog);
                }
                $output .= adrotate_fallback($fallback, 'unqualified', $site);
            }
        } else {
            $output .= adrotate_error('group_not_found', array($group_array[0]));
        }
        if ($site > 0 and adrotate_is_networked() and ($license['type'] == 'Network' or $license['type'] == 'Developer')) {
            switch_to_blog($current_blog);
        }
    } else {
        $output .= adrotate_error('group_no_id');
    }
    return $output;
}