Manage', __('Manage Ads', 'adwit'), 'manage_options', 'adwit', 'adwit_banner_manage_ads');
add_submenu_page('adwit', 'adwit > Setup Adwit Ads', __('Setup Adwit Ads', 'adwit'), 'manage_options', 'adwit_setup', 'adwit_banner_setup');
}
function adwit_load_scriptaculus() {
wp_enqueue_script('scriptaculous');
}
function adwit_banner_manage_ads() {
$res = adwit_find_or_create_customer ();
if ( is_array($res) && !empty($res)) {
if ($res['status']==0){
global $current_user;
get_currentuserinfo();
global $adwit_recovery;
require_once(ADWIT_BANNER_MANAGER_PATH . '/views/auth.php');
}elseif($res['status']==2){
echo "".__('You should set at least one zone','adwit')."";
adwit_banner_setup(false);
}else{
$adwit_opt = $res['data'];
$data = array();
$data['date'] = _get_date_now_adwit_format();
$data['publisher_id'] = $adwit_opt['adwit_express_publisher_id'];
echo "";
}
}else{
echo "";
}
}
function adwit_banner_setup($isnew = true) {
//Find or create customer
if ($isnew!==false){
$res = adwit_find_or_create_customer();
if ($res['status']==0) {
global $current_user;
get_currentuserinfo();
global $adwit_recovery;
require_once(ADWIT_BANNER_MANAGER_PATH . '/views/auth.php');
exit;
}
}
$options = adwit_options();
$vp_type = 'page';
if (isset($_POST['post_type']) && !empty($_POST['post_type'])){
$vp_type = $_POST['post_type'];
}
switch ($vp_type) {
case 'post':
$view_page = 'post';
$adwit_ads = _get_adwit_post_type_data($options['ads'], 'post');
break;
case 'home':
$view_page = 'home';
$adwit_ads = _get_adwit_post_type_data($options['ads'], 'home');
break;
case 'search':
$view_page = 'search';
$adwit_ads = _get_adwit_post_type_data($options['ads'], 'search');
break;
default:
$view_page = 'page';
$adwit_ads = _get_adwit_post_type_data($options['ads'], 'page');
break;
}
include(ADWIT_BANNER_MANAGER_PATH . '/views/main.php');
}
function adwit_inject_hook($content) {
if (is_home() || is_archive() || is_search() || is_page() || is_single() && !empty($content)) {
$adwit_ads = _get_adwit_ads_data();
if (empty($adwit_ads)) {
return $content;
}
$paragraphes = preg_match_all("/(
)/", $content, $result);
if (count($result[0]) > 1) {
$two_array = array_chunk($result[0], ceil(count($result[0]) / 2));
$script_top = _get_adwit_ads('top');
$two_array[0][0] = $script_top . $two_array[0][0];
$script_middle = _get_adwit_ads('middle');
$script_bottom = _get_adwit_ads('bottom');
$two_array[1][2] = $two_array[1][2];
$result = implode(array_merge($two_array[0], array($script_middle), $two_array[1]));
$content = $result . $script_bottom;
} else {
$script_top = _get_adwit_ads('top');
if (!empty($script_top)){
$content = $script_top.$content;
}
$script_bottom = _get_adwit_ads('bottom');
if (!empty($script_bottom)){
$content = $content.$script_bottom;
}
}
}
return $content;
}
function adwit_title_inject_hook($content) {
$script_head = '';
$adwit_ads = _get_adwit_ads_data();
if (empty($adwit_ads)) {
return $content;
}
if ((is_home() || is_page()) && !empty($content) ) {
$script_h = _get_adwit_ads('head');
if (!empty($script_h)) {
//$script_head = $script_h . '
';
}
}
return $script_head . $content;
}
//add_filter('the_title', 'adwit_title_inject_hook');
add_filter('the_content', 'adwit_inject_hook');
function _get_adwit_post_type_data($ads, $type='page') {
$res = array();
if (is_array($ads) && !empty($ads)) {
foreach ($ads as $key => $val) {
if (0 == strcmp($type, substr($key, 0, strlen($type)))) {
$res[$key] = $val;
}
}
}
return $res;
}
function _adwit_script_generator($key_id='', $w, $h) {
if (empty($key_id)) {
return;
}
// $key_id = '3770273667';
$ad = "\n".
"";
return $ad;
}
function _get_adwit_ads_data() {
$options = adwit_options();
$res = array();
if (is_page ()) {
$res = _get_adwit_post_type_data($options['ads'], 'page');
} elseif (is_single() || is_archive()) {
$res = _get_adwit_post_type_data($options['ads'], 'post');
} elseif (is_home()) {
$res = _get_adwit_post_type_data($options['ads'], 'home');
} elseif ( is_search()) {
$res = _get_adwit_post_type_data($options['ads'], 'search');
}
return $res;
}
function _get_adwit_ads($pos='') {
if (empty($pos)) {
return '';
}
$post_type = '';
if (is_page ()) {
$post_type = 'page_' . $pos;
} elseif (is_single() || is_archive()) {
$post_type = 'post_' . $pos;
} elseif (is_home()) {
$post_type = 'home_' . $pos;
} elseif ( is_search()) {
$post_type = 'search_' . $pos;
}
$adwit_ads = _get_adwit_ads_data();
$script_pos = '';
if ($adwit_ads[$post_type . '_left']) {
$script_pos .= '' . _adwit_script_generator($adwit_ads[$post_type . '_left']['key'], $adwit_ads[$post_type . '_left']['width'], $adwit_ads[$post_type . '_left']['height']) . '
';
}
if ($adwit_ads[$post_type . '_right']) {
$script_pos .= '' . _adwit_script_generator($adwit_ads[$post_type . '_right']['key'], $adwit_ads[$post_type . '_right']['width'], $adwit_ads[$post_type . '_right']['height']) . '
';
}
if ($adwit_ads[$post_type . '_center']) {
$script_pos .= '' . _adwit_script_generator($adwit_ads[$post_type . '_center']['key'], $adwit_ads[$post_type . '_center']['width'], $adwit_ads[$post_type . '_center']['height']) . '
';
}
return $script_pos;
}
class Widget_Adwit_Banner extends WP_Widget {
/** constructor */
function Widget_Adwit_Banner() {
require_once(ADWIT_BANNER_MANAGER_PATH . '/adwit-banner-manager-admin.php');
$widget_ops = array('classname' => 'widget_adwit_banner', 'description' => __( 'Create and display your adwit-express ads', 'adwit') );
parent::WP_Widget('adwit_banner', __('Adwit-Banner', 'adwit'), $widget_ops);
}
function widget($args, $instance) {
extract( $args );
$sizeid = trim( $instance['sizeid'] );
$key_id = trim( $instance['key'] );
$title = apply_filters('widget_title', $instance['title']);
if ( empty($title) ) $title = __( 'Adwit Banner', 'adwit' );
echo "{$before_widget}{$before_title}" . esc_html($title) ."{$after_title}";
if (!empty($key_id) && $sizeid > 0){
$bann_list = _get_bann_list();
//Add new banner info into adwit_options (wp)
$bsizes = array();
foreach($bann_list as $bk => $bv){
if ($bv['id'] == $sizeid){
$bsizes = explode('x', $bk);
break;
}
}
// $key_id = '3770273667';
echo '';
$ad = "\n".
"";
echo $ad;
echo '
';
}
}
/** @see WP_Widget::update */
function update($new_instance, $old_instance) {
if ( $new_instance['sizeid'] != ''){
$adwit_data = adwit_options();
$instance['sizeid'] = trim( strip_tags( stripslashes( $new_instance['sizeid'] ) ) );
$instance['title'] = trim( strip_tags( stripslashes( $new_instance['title'] ) ) );
$tmp_sizeid = trim( strip_tags( stripslashes( $new_instance['tmp_sizeid'] ) ) );
$wg_opt_list = $this->_adwit_widget_filter($tmp_sizeid);
$bann_list = _get_bann_list();
$url_data = array();
$url_data['page_type'] = 'widget';
$url_data['pos_v'] = $new_instance['sizeid'];
$url_data['pos_h'] = 0;
$url_data['size_id'] = $new_instance['sizeid'];
$url_data['status'] = 'on';
$url_data['publisher_id'] = $adwit_data['adwit_express_publisher_id'];
//Get zone key
$url_express_create_zone = ADWIT_HOME_URL . '/' . adwit_generate_token('campaign_ads/zone_add', $url_data, $adwit_data['adwit_express_key']);
$get_zone_key = wp_remote_fopen($url_express_create_zone);
if ( !empty($tmp_sizeid) && $tmp_sizeid > 0 && $tmp_sizeid != $new_instance['sizeid']){
$url_data['status'] = 'off';
$url_data['pos_v'] = $tmp_sizeid;
$url_data['size_id'] = $tmp_sizeid;
$url_express_update_zone = ADWIT_HOME_URL . '/' . adwit_generate_token('campaign_ads/zone_add', $url_data, $adwit_data['adwit_express_key']);
wp_remote_fopen($url_express_update_zone);
}
//Add new banner info into adwit_options (wp)
$bsizes = array();
$img_bsize = '';
foreach($bann_list as $bk => $bv){
if ($bv['id'] == $new_instance['sizeid']){
$img_bsize = $bk;
$bsizes = explode('x', $bk);
break;
}
}
$instance['page_type'] = 'widget';
$instance['pos_v'] = $new_instance['sizeid'];
$instance['pos_h'] = 0;
$instance['width'] = $bsizes[0];
$instance['height'] = $bsizes[1];
$instance['bsize'] = $img_bsize;
$instance['key'] = $get_zone_key; //zone key
$instance['img_name'] = $img_bsize. '.png';
return $instance;
}
}
function _adwit_widget_filter($tmp_sizeid = ''){
$bann_list = _get_bann_list();
/*
$wg_opt_list = adwit_widget_options();
if (!empty($tmp_sizeid)){
foreach ($wg_opt_list as $kk => $vv){
if ($tmp_sizeid == $vv['sizeid'] || empty($vv)){
unset($wg_opt_list[$kk]);
}
}
}
foreach ($bann_list as $k1 => $v1){
foreach ($wg_opt_list as $v2){
if ($v1['id'] == $v2['sizeid'] || empty($v1)){
//unset($bann_list[$k1]);
}
}
}
*/
return $bann_list;
}
/** @see WP_Widget::form */
function form($instance){
//widget_adwit_banner
$instance = wp_parse_args( (array) $instance, array('sizeid' => '', 'title'=>'') );
$sizeid = esc_attr($instance['sizeid']);
$title = esc_attr($instance['title']);
echo '
';
$bann_list = $this->_adwit_widget_filter();
echo ' '.__('Add Banner', 'adwit').'
';
}
}
add_action( 'widgets_init', 'adwit_banner_init' );
function adwit_banner_init() {
register_widget('Widget_Adwit_Banner');
}
?>