ass MP_Advertising { /** * The global db prefix **/ private static $dbPrefix; /** * The global tables **/ private static $table_banners_contents; private static $table_banners_contents_not_available; private static $table_banners; /** * The current banners list **/ private static $banner_zones; private static $currentBanners; private static $disabled_taxonomies=array('nav_menu','link_category','post_format','post_status','ef_usergroup','following_users'); /** * The allowed banners list **/ private static $allowedBanners = array(); private static $initialAllowedBanners = array( 'CODE_HEAD' => 'code', 'CODE_FOOT' => 'code', 'SKIN' => 'code', 'OOP' => 'code', 'LEADERBOARD' => 'code', 'MEDIUM_RECTANGLE_UP' => 'code', 'MEDIUM_RECTANGLE_UP_MOBILE' => 'code', 'MEDIUM_RECTANGLE_DW' => 'code', 'MEDIUM_RECTANGLE_DW_MOBILE' => 'code', 'BEFORE_ARTICLE' => 'code', 'INNER_ARTICLE' => 'code', 'END_ARTICLE' => 'code', 'PREMIUM_NATIVE' => 'code', 'PROMOBOX_1' => 'code', 'PROMOBOX_2' => 'code' ); /************************ * PUBLIC STATIC METHODS * ************************/ /** * Initialization method **/ public static function init() { # set db prefix and tables MP_Advertising::setDbPrefix(); MP_Advertising::setDbTables(); # set admin actions callback add_action( 'admin_menu', array( __CLASS__, 'adminMenu' ) ); add_action( 'admin_init', array( __CLASS__, 'adminInit' ) ); # set page actions add_action( 'wp', array( __CLASS__, 'extractBanners' ) ); # set ajax actions callback add_action( 'wp_ajax_Mp_Advertising.updateZoneView', array( __CLASS__, 'updateZoneView' ) ); add_action( 'wp_ajax_Mp_Advertising.updatePageView', array( __CLASS__, 'updatePageView' ) ); add_action( 'wp_ajax_Mp_Advertising.updateTaxView', array( __CLASS__, 'updateTaxView' ) ); add_action( 'wp_ajax_Mp_Advertising.updateTermViewFromTax', array( __CLASS__, 'updateTermViewFromTax' ) ); add_action( 'wp_ajax_Mp_Advertising.updatePostView', array( __CLASS__, 'updatePostView' ) ); add_action( 'wp_ajax_Mp_Advertising.updatePostPostTypeView', array( __CLASS__, 'updatePostPostTypeView' ) ); add_action( 'wp_ajax_Mp_Advertising.updatePostTaxTypeView', array( __CLASS__, 'updatePostTaxTypeView' ) ); add_action( 'wp_ajax_Mp_Advertising.updateSearchPostType', array( __CLASS__, 'updateSearchPostType' ) ); add_action( 'wp_ajax_Mp_Advertising.updateSearchPostTaxTypeView', array( __CLASS__, 'updateSearchPostTaxTypeView' ) ); add_action( 'wp_ajax_Mp_Advertising.searchGetPostsList', array( __CLASS__, 'searchGetPostsList' ) ); add_action( 'wp_ajax_Mp_Advertising.updateLinkView', array( __CLASS__, 'updateLinkView' ) ); add_action( 'wp_ajax_Mp_Advertising.deleteLinkBanners', array( __CLASS__, 'deleteLinkBanners' ) ); add_action( 'wp_ajax_Mp_Advertising.saveBannersPerZone', array( __CLASS__, 'saveBannersPerZone' ) ); add_action( 'wp_ajax_Mp_Advertising.toggleSwitchOnOffBanner', array( __CLASS__, 'toggleSwitchOnOffBanner' ) ); add_action( 'wp_ajax_Mp_Advertising.toggleSwitchOnOffNewMPAdv', array( __CLASS__, 'toggleSwitchOnOffNewMPAdv' ) ); add_action( 'wp_ajax_Mp_Advertising.saveTopicTax', array( __CLASS__, 'saveTopicTax' ) ); add_action( 'wp_ajax_Mp_Advertising.optionsPageSetBanners', array( __CLASS__, 'optionsPageSetBanners' ) ); add_action( 'wp_ajax_Mp_Advertising.optionsPageDeleteBanners', array( __CLASS__, 'optionsPageDeleteBanners' ) ); } /** * Invoked on admin_init action **/ public static function adminInit() { if( isset( $_GET['page'] ) && ( $_GET['page'] === 'mp-advertising' || $_GET['page'] === 'mp-advertising-options' ) && (current_user_can( 'manage_options' ) ) ) { # enqueue scripts wp_enqueue_script ( 'mp-advertising-javascripts', plugins_url( "data/js/mp_advertising.js" , dirname(__FILE__) ), array( 'jquery' ), MP_ADVERTISING_VERSION ); wp_enqueue_script ( 'webtoolkit-base64', plugins_url( "data/js/webtoolkit.base64.js" , dirname(__FILE__) ), array( 'jquery' ), MP_ADVERTISING_VERSION ); wp_enqueue_script ( 'multi-select', plugins_url( "data/js/chosen.jquery.min.js" , dirname(__FILE__) ), array( 'jquery' ), MP_ADVERTISING_VERSION ); # enqueue styles wp_enqueue_style ( 'mp-advertising-stylesheets', plugins_url( "data/css/mp_advertising.css" , dirname(__FILE__) ), array(), MP_ADVERTISING_VERSION, 'screen' ); } // end if( isset( $_GET['page'] ) && ( $_GET['page'] === 'mp-advertising' || $_GET['page'] === 'mp-advertising-options') ) } // end function adminInit() /** * Invoked on admin_menu action * Create admin menu Banner Management and Banner Options **/ public static function adminMenu() { add_menu_page(__( 'Banner Management', MP_ADVERTISING_LANGUAGE_PLUGIN ), __( 'Banner Management', MP_ADVERTISING_LANGUAGE_PLUGIN ), 'manage_options', 'mp-advertising', array( __CLASS__, 'adminPageOutput')); add_submenu_page('mp-advertising', __( 'Banner Management', MP_ADVERTISING_LANGUAGE_PLUGIN ), __( 'Banner Management', MP_ADVERTISING_LANGUAGE_PLUGIN ), 'manage_options', 'mp-advertising', array( __CLASS__, 'adminPageOutput') ); add_submenu_page('mp-advertising', __( 'Banner Options', MP_ADVERTISING_LANGUAGE_PLUGIN ), __( 'Banner Options', MP_ADVERTISING_LANGUAGE_PLUGIN ), 'manage_options', 'mp-advertising-options', array( __CLASS__, 'adminPageOptionsOutput') ); } // end function adminMenu() /** * Print Banner Management page if have correct permission **/ public static function adminPageOutput() { if ( !current_user_can( 'manage_options' ) ) { wp_die( __( 'You do not have permission to access this page.', MP_ADVERTISING_LANGUAGE_PLUGIN ) ); } // end if ( !current_user_can( 'manage_options' ) ) MP_Advertising::createTables(); require_once MP_ADVERTISING_DIR . '/tpl/options.form.php'; } // end function adminPageOutput() /** * Print Banner Options page if have correct permission **/ public static function adminPageOptionsOutput() { if ( !current_user_can( 'manage_options' ) ) { wp_die( __( 'You do not have permission to access this page.', MP_ADVERTISING_LANGUAGE_PLUGIN ) ); } // end if ( !current_user_can( 'manage_options' ) ) require_once MP_ADVERTISING_DIR . '/tpl/options.page.php'; } // end function adminPageOptionsOutput() /** * Updates the zone view with banner textareas **/ public static function updateZoneView() { # use global db object global $wpdb; if( !isset( $_POST['zone'] ) || !trim( $_POST['zone'] ) ) { wp_die( 'Error: ' . __( 'Incorrect parameters', MP_ADVERTISING_LANGUAGE_PLUGIN ) ); } // end if( !isset( $_POST['zone'] ) || !trim( $_POST['zone'] ) ) if( !isset( $_POST['type'] ) || !trim( $_POST['type'] ) ) { wp_die( 'Error: ' . __( 'Incorrect parameters', MP_ADVERTISING_LANGUAGE_PLUGIN ) ); } // end if( !isset( $_POST['type'] ) || !trim( $_POST['type'] ) ) $zone = trim( $_POST['zone'] ); $type = trim( $_POST['type'] ); // var_dump($zone); // var_dump($type); if( $zone === 'none' ) { wp_die( '' ); } // end if( $zone === 'none' ) # set answer $output = array(); if( $zone === 'link-add' ) { $output[] = ( '' ); } elseif( substr( $zone, 0, 7 ) === 'http://' ) { $output[] = ( '' ); } else { $output[] = '

' . __( 'Banners availables', MP_ADVERTISING_LANGUAGE_PLUGIN ) . '

'; } // end if( $zone === 'link-add' ) $banners = $wpdb->get_results(" SELECT * FROM " . MP_Advertising::$table_banners_contents . "," . MP_Advertising::$table_banners . " WHERE " . MP_Advertising::$table_banners_contents . ".cont_banner_label=" . MP_Advertising::$table_banners . ".banner_label AND cont_site_zone = '{$zone}' "); $banner_contents = array(); foreach( $banners as $banner ) { $banner_contents[ $banner->cont_banner_label ] = stripslashes( $banner->cont_banner_content ); } // end foreach( $banners as $banner ) // check blocked not available // GLOBAL $not_available_global = $wpdb->get_results(" SELECT cont_banner_label FROM " . MP_Advertising::$table_banners_contents_not_available . ", " . MP_Advertising::$table_banners . " WHERE " . MP_Advertising::$table_banners_contents_not_available . ".cont_banner_label=" . MP_Advertising::$table_banners . ".banner_label AND cont_site_zone = 'global' "); $not_available_global_banners = array(); foreach($not_available_global as $na) { $not_available_global_banners[] = $na->cont_banner_label; } // specific ALL parent type $not_available_specific_type_banners=array(); if($type=="type" || $type=="page" || $type=='cat' || $type=='tag' || $type=="tax") { $not_available_specific_type = $wpdb->get_results(" SELECT cont_banner_label FROM " . MP_Advertising::$table_banners_contents_not_available . ", " . MP_Advertising::$table_banners . " WHERE " . MP_Advertising::$table_banners_contents_not_available . ".cont_banner_label=" . MP_Advertising::$table_banners . ".banner_label AND cont_site_zone = '{$type}-0' "); foreach($not_available_specific_type as $na) { $not_available_specific_type_banners[] = $na->cont_banner_label; } } $not_available_specific_type_term_banners=array(); $not_available_specific_type_term_banners_tax=array(); // specific ALL sub-parent type // terms (from taxonomy) and specific post (from post type) if( ($type=="tax" || $type=="type") && isset($_POST['opt']) && !empty($_POST['opt']) && $zone!=$_POST["opt"]."-0") { $not_available_specific_type_term = $wpdb->get_results(" SELECT cont_banner_label FROM " . MP_Advertising::$table_banners_contents_not_available . ", " . MP_Advertising::$table_banners . " WHERE " . MP_Advertising::$table_banners_contents_not_available . ".cont_banner_label=" . MP_Advertising::$table_banners . ".banner_label AND cont_site_zone = '{$_POST["opt"]}-0' "); foreach($not_available_specific_type_term as $na) { $not_available_specific_type_term_banners[] = $na->cont_banner_label; } // var_dump($not_available_specific_type_term_banners); if(strpos($zone, 'type-tax') !== false){ $args = array( 'public' => true ); $_types = get_post_types( $args ); foreach($_types as $_post_type){ $not_available_specific_type_term = $wpdb->get_results(" SELECT cont_banner_label FROM " . MP_Advertising::$table_banners_contents_not_available . ", " . MP_Advertising::$table_banners . " WHERE " . MP_Advertising::$table_banners_contents_not_available . ".cont_banner_label=" . MP_Advertising::$table_banners . ".banner_label AND cont_site_zone = 'type-{$_post_type}-0' "); foreach($not_available_specific_type_term as $na) { $not_available_specific_type_term_banners_tax[$_post_type] = $na->cont_banner_label; } } } // var_dump($not_available_specific_type_term_banners_tax); } // specific zone $not_available = $wpdb->get_results(" SELECT cont_banner_label FROM " . MP_Advertising::$table_banners_contents_not_available . ", " . MP_Advertising::$table_banners . " WHERE " . MP_Advertising::$table_banners_contents_not_available . ".cont_banner_label=" . MP_Advertising::$table_banners . ".banner_label AND cont_site_zone = '{$zone}' "); $not_available_banners = array(); foreach($not_available as $na) { $not_available_banners[] = $na->cont_banner_label; } //get allowed banners MP_Advertising::allowedBannersFunction(); # loop results foreach( MP_Advertising::$allowedBanners as $banner_label=>$banner_type ) { $row_template = MP_Advertising::getBannerRowTemplate( 'zone' ); $checked_switch_on_off = "checked"; $class_switch_on_off = null; $disabled_textarea = null; $label_switch_on_off = null; # disabled from global zone if(in_array($banner_label,$not_available_global_banners) && $zone!='global') { $checked_switch_on_off = null; $class_switch_on_off = "not_available_parent"; $disabled_textarea = "disabled"; $label_switch_on_off = __( 'disabled from "global" zone', MP_ADVERTISING_LANGUAGE_PLUGIN ); } # disabled from ALL parent zone if(!empty($not_available_specific_type_banners) && in_array($banner_label,$not_available_specific_type_banners) && $zone!=$type.'-0') { $checked_switch_on_off = null; $class_switch_on_off = "not_available_parent"; $disabled_textarea = "disabled"; $real_zone_name = null; if ( "type" == $type ) { $real_zone_name=__( 'Post', MP_ADVERTISING_LANGUAGE_PLUGIN ); } elseif( 'cat' == $type ) { $real_zone_name=__( 'Categories', MP_ADVERTISING_LANGUAGE_PLUGIN ); } elseif( 'tag' == $type ) { $real_zone_name=__( 'Tags', MP_ADVERTISING_LANGUAGE_PLUGIN ); } elseif( 'tax' == $type ) { $real_zone_name=__( 'Taxonomies', MP_ADVERTISING_LANGUAGE_PLUGIN ); } elseif( 'page' == $type ) { $real_zone_name=__( 'Pages', MP_ADVERTISING_LANGUAGE_PLUGIN ); } $label_switch_on_off = (empty($label_switch_on_off)) ? sprintf(__( 'disabled from "%s" option ALL', MP_ADVERTISING_LANGUAGE_PLUGIN ),strtolower($real_zone_name)) : $label_switch_on_off." ".sprintf(__( 'and from "%s" option ALL', MP_ADVERTISING_LANGUAGE_PLUGIN ),strtolower($real_zone_name)); } # disabled from ALL (sub)parent zone if ( in_array($banner_label,$not_available_specific_type_term_banners) ) { $checked_switch_on_off = null; $class_switch_on_off = "not_available_parent"; $disabled_textarea = "disabled"; if ( 'tax' == $type || (strpos($zone, 'type-tax') !== false) ) { $label_switch_on_off = (empty($label_switch_on_off)) ? __( 'disabled from the selected term option ALL', MP_ADVERTISING_LANGUAGE_PLUGIN ) : $label_switch_on_off." ". __( 'and from the selected term option ALL', MP_ADVERTISING_LANGUAGE_PLUGIN ); } else { $label_switch_on_off = (empty($label_switch_on_off)) ? __( 'disabled from the selected post_type option ALL', MP_ADVERTISING_LANGUAGE_PLUGIN ) : $label_switch_on_off." ". __( 'and from the selected post_type option ALL', MP_ADVERTISING_LANGUAGE_PLUGIN ); } } if ( in_array($banner_label,$not_available_specific_type_term_banners_tax) ) { $label_switch_on_off = (empty($label_switch_on_off)) ? __( 'Disabled posts type: ', MP_ADVERTISING_LANGUAGE_PLUGIN ) : $label_switch_on_off." ". __( 'and disabled posts type: ', MP_ADVERTISING_LANGUAGE_PLUGIN ); foreach($not_available_specific_type_term_banners_tax as $k=>$val){ if($banner_label==$val){ $label_switch_on_off.="".$k.""; } } } # zone disabled if ( in_array($banner_label,$not_available_banners) ) { $checked_switch_on_off = null; $disabled_textarea = 'disabled'; $label_switch_on_off = (empty($label_switch_on_off)) ? null : $label_switch_on_off.' '. __( 'and from itself', MP_ADVERTISING_LANGUAGE_PLUGIN ); } #output switch $switch_on_off = '
'.$label_switch_on_off.'
'; $output[] = sprintf( $row_template, strtoupper($banner_label), $switch_on_off, $banner_label, $disabled_textarea, ( isset( $banner_contents[ $banner_label ] ) ) ? $banner_contents[ $banner_label ] : '' ); } // end foreach( $banners as $banner ) # return string wp_die( implode( '', $output ) ); } // end function updateZoneView() /************************ * PAGES * ************************/ /** * Updates the page view with pages select **/ public static function updatePageView() { # use global db object global $wpdb; // get ids of pages "edited" $ids = array(); $pageids = $wpdb->get_results (" ( SELECT DISTINCT( cont_site_zone ) FROM " . MP_Advertising::$table_banners_contents . " WHERE cont_site_zone LIKE 'page-%' ) UNION ( SELECT DISTINCT( cont_site_zone ) FROM " . MP_Advertising::$table_banners_contents_not_available . " WHERE cont_site_zone LIKE 'page-%' ) "); $pageidsHome = $wpdb->get_results (" SELECT DISTINCT( cont_site_zone ) FROM " . MP_Advertising::$table_banners_contents . " WHERE cont_site_zone LIKE 'home'"); #loops pageids foreach( $pageids as $id ) { $ids[] = intval( str_replace( 'page-', '', $id->cont_site_zone ) ); } // end foreach( $pageids as $id ) // query $args = array( "posts_per_page" => -1, "s" => $_POST['search'], "orderby" => "title", "post_type" => array("page"), "post_status" => array('publish') ); $pages = get_posts($args); // max 15 elements min 5 elements in select $count_ = (count($pages)>=15) ? 15 : count($pages)+1; $count_ = ($count_<5) ? 5 : $count_; # set answer $output = array(); $output[] ='

' . __( 'Select a page (* page edited)', MP_ADVERTISING_LANGUAGE_PLUGIN ) . '

'; $output[] = ''; # return string wp_die( implode( '', $output ) ); } // end function updatePageView() /************************ * TAXONOMIES * ************************/ /** * Updates the tassonomies view with tassonimia select * The first view clicking on the button "taxonomies" **/ public static function updateTaxView() { # use global db object global $wpdb; // get ids of taxonomies "edited" $ids = array(); $taxids = $wpdb->get_results (" ( SELECT DISTINCT( cont_site_zone ) FROM " . MP_Advertising::$table_banners_contents . " WHERE cont_site_zone LIKE 'tax-%' ) UNION ( SELECT DISTINCT( cont_site_zone ) FROM " . MP_Advertising::$table_banners_contents_not_available . " WHERE cont_site_zone LIKE 'tax-%' ) "); # loop taxids foreach( $taxids as $id ) { $ids[] = str_replace( 'tax-', '', $id->cont_site_zone ) ; } // end foreach( $taxids as $id ) // get taxonomies $taxonomies = get_taxonomies(); $disabled_taxonomies=array('nav_menu','link_category','post_format','post_status','ef_usergroup','following_users'); # set answer $output = array(); $output[] ='

' . __( 'Select a taxonomy', MP_ADVERTISING_LANGUAGE_PLUGIN ) . '

'; $output[] = ''; # return string wp_die( implode( '', $output ) ); } // end function updateTaxView() /** * Updates the terms view with taxonomy select * view clicking a taxonomy name **/ public static function updateTermViewFromTax() { # use global db object global $wpdb; // get taxonomy id $tax = str_replace( 'tax-', "", $_POST['zone']); $tax = substr($_POST['zone'], 4); $type_tax="term"; if($tax=='category'){ $type_tax='cat'; } else if($tax=='post_tag'){ $type_tax='tag'; } // get ids of terms "edited" $ids = array(); $arg="( SELECT DISTINCT( cont_site_zone ) FROM " . MP_Advertising::$table_banners_contents . " WHERE cont_site_zone LIKE 'tax-".$tax."-term-%' ) UNION ( SELECT DISTINCT( cont_site_zone ) FROM " . MP_Advertising::$table_banners_contents_not_available . " WHERE cont_site_zone LIKE 'tax-".$tax."-term-%' )"; if($type_tax=='cat'){ $arg="( SELECT DISTINCT( cont_site_zone ) FROM " . MP_Advertising::$table_banners_contents . " WHERE cont_site_zone LIKE 'cat-%' ) UNION ( SELECT DISTINCT( cont_site_zone ) FROM " . MP_Advertising::$table_banners_contents_not_available . " WHERE cont_site_zone LIKE 'cat-%' )"; } else if($type_tax=='tag'){ $arg="( SELECT DISTINCT( cont_site_zone ) FROM " . MP_Advertising::$table_banners_contents . " WHERE cont_site_zone LIKE 'tag-%' ) UNION ( SELECT DISTINCT( cont_site_zone ) FROM " . MP_Advertising::$table_banners_contents_not_available . " WHERE cont_site_zone LIKE 'tag-%' )"; } $taxids = $wpdb->get_results ($arg); # loop taxids foreach( $taxids as $id ) { if($type_tax=='term'){ $ids[] = intval( str_replace( 'tax-'.$tax.'-term-', '', $id->cont_site_zone ) ); } else if($type_tax=='cat'){ $ids[] = intval( str_replace( 'cat-', '', $id->cont_site_zone ) ); } else if($type_tax=='tag'){ $ids[] = intval( str_replace( 'tag-', '', $id->cont_site_zone ) ); } } // end foreach( $catids as $id ) // get terms $terms = get_terms( $tax, array('hide_empty'=>0) ); // max 15 elements min 5 elements in select $count_ = (count($terms)>=15) ? 15 : count($terms)+1; $count_ = ($count_<5) ? 5 : $count_; # set answer $output = array(); $output[] ='

' . __( 'Select a term of this taxonomy (* term edited)', MP_ADVERTISING_LANGUAGE_PLUGIN ) . '

'; $output[] = ''; # return string wp_die( implode( '', $output ) ); } // end function updateTaxView() /************************ * POST TYPES * ************************/ /** * Updates the post view with post select * The first view clicking on the button "POST" **/ public static function updatePostView() { # use global db object global $wpdb; $ids = array(); $typesids = $wpdb->get_results(" ( SELECT DISTINCT( cont_site_zone ) FROM " . MP_Advertising::$table_banners_contents . " WHERE cont_site_zone LIKE 'type-%' ) UNION ( SELECT DISTINCT( cont_site_zone ) FROM " . MP_Advertising::$table_banners_contents_not_available . " WHERE cont_site_zone LIKE 'type-%' ) "); # loop typesids foreach ( $typesids as $id ) { $id = str_replace( 'type-', '', $id->cont_site_zone ) ; $id = preg_replace('/-[0-9]+/', '', $id); $ids[] = $id ; } // end foreach( $catids as $id ) $output = array(); $output[] ='

' . __( 'Select a post', MP_ADVERTISING_LANGUAGE_PLUGIN ) . '

'; $output[] = ''; # return string wp_die( implode( '', $output ) ); } // end function updatePostView() /** * Updates the post_types view with post types select * The view clicking on the button "BY POST TYPES" **/ public static function updatePostPostTypeView() { # use global db object global $wpdb; // get ids of post types "edited" $ids = array(); $typesids = $wpdb->get_results(" ( SELECT DISTINCT( cont_site_zone ) FROM " . MP_Advertising::$table_banners_contents . " WHERE cont_site_zone LIKE 'type-%' ) UNION ( SELECT DISTINCT( cont_site_zone ) FROM " . MP_Advertising::$table_banners_contents_not_available . " WHERE cont_site_zone LIKE 'type-%' ) "); # loop typesids foreach ( $typesids as $id ) { $id = str_replace( 'type-', '', $id->cont_site_zone ) ; $id = preg_replace('/-[0-9]+/', '', $id); $ids[] = $id ; } // end foreach( $catids as $id ) # get types $args = array( 'public' => true ); $types = get_post_types( $args ); // max 15 elements min 5 elements in select $count_ = (count($types)>=15) ? 15 : count($types)-1; $count_ = ($count_<5) ? 5 : $count_; # set answer $output = array(); $output[] ='

' . __( 'Select a post type (* posts types edited)', MP_ADVERTISING_LANGUAGE_PLUGIN ) . '

'; $output[] = ''; # return string wp_die( implode( '', $output ) ); } // end function updatePostPostTypeView() /** * Updates the post-ID view with post ID select * The view clicking on the button "BY POST ID" **/ public static function updatePostPostIDView($post_type) { # use global db object global $wpdb; $post_type = str_replace( 'type-', "", $post_type); $ids =$post__in= array(); $postsIDS = $wpdb->get_results (" ( SELECT DISTINCT( cont_site_zone ) FROM " . MP_Advertising::$table_banners_contents . " WHERE cont_site_zone LIKE 'type-".$post_type."-%' ) UNION ( SELECT DISTINCT( cont_site_zone ) FROM " . MP_Advertising::$table_banners_contents_not_available . " WHERE cont_site_zone LIKE 'type-".$post_type."-%' ) "); # loop posts foreach ( $postsIDS as $id ) { $my_id = str_replace( 'type-'.$post_type."-", '', $id->cont_site_zone );; $ids[] = $my_id; if(is_numeric($my_id) && (int)$my_id!=0){ $post__in[]=(int)$my_id; } } // end foreach( $postsIDS as $id ) $posts=array(); if(!empty($post__in)){ $args = array( "posts_per_page" => -1, "orderby" => "title", 'post__in' => $post__in, "post_type" => array($post_type), "post_status" => array('any') ); $posts = get_posts($args); } // max 15 elements min 5 elements in select $count_ = (count($posts)>=15) ? 15 : count($postsIDS)+1; $count_ = ($count_<5) ? 5 : $count_; # set answer $output = array(); $output[] ='

'.__('All post customized',MP_ADVERTISING_LANGUAGE_PLUGIN).'

'; $output[] = ''; # return string return ( implode( '', $output ) ); } // end function updatePostPostIDView() /** * Updates the tax type view with taxs select * The view clicking on the button "BY TAXONOMY" **/ public static function updatePostTaxTypeView() { # use global db object global $wpdb; // get ids of taxonomies "edited" $ids = array(); $taxids = $wpdb->get_results (" ( SELECT DISTINCT( cont_site_zone ) FROM " . MP_Advertising::$table_banners_contents . " WHERE cont_site_zone LIKE 'type-tax-%' ) UNION ( SELECT DISTINCT( cont_site_zone ) FROM " . MP_Advertising::$table_banners_contents_not_available . " WHERE cont_site_zone LIKE 'type-tax-%' ) "); # loop taxids foreach( $taxids as $id ) { $ids[] = str_replace( 'type-tax-', '', $id->cont_site_zone ) ; } // end foreach( $taxids as $id ) // get taxonomies $taxonomies = get_taxonomies(); # set answer $output = array(); $output[] ='

' . __( 'Select a taxonomy', MP_ADVERTISING_LANGUAGE_PLUGIN ) . '

'; $output[] = ''; # return string wp_die( implode( '', $output ) ); } // end function updatePostTaxTypeView() /** * Updates the post_types view * Print an input with the scope to search posts with the post type previously selected. * The result will be input in the select#select-post **/ public static function updateSearchPostType() { # use global db object global $wpdb; # get post type selected $type_post = str_replace( 'type-', "", $_POST['zone']); # set answer $output = array(); $output[] = '
'; $output[] = '
'; $output[] = '
'; $output[] = MP_Advertising::updatePostPostIDView($_POST['zone']); $output[] = '
'; # return string wp_die( implode( '', $output ) ); } // end function updateSearchPostType() /** * Updates the post tax view * Print an input with the scope to search posts with the post type previously selected. * The result will be input in the select#select-post **/ public static function updateSearchPostTaxTypeView() { # use global db object global $wpdb; // get taxonomy id $tax = str_replace( 'type-tax-', "", $_POST['zone']); $tax = substr($_POST['zone'], 9); $type_tax="term"; if($tax=='category'){ $type_tax='cat'; } else if($tax=='post_tag'){ $type_tax='tag'; } // get ids of terms "edited" $ids = array(); $taxids = $wpdb->get_results(" ( SELECT DISTINCT( cont_site_zone ) FROM " . MP_Advertising::$table_banners_contents . " WHERE cont_site_zone LIKE 'type-tax-".$tax."-term-%' ) UNION ( SELECT DISTINCT( cont_site_zone ) FROM " . MP_Advertising::$table_banners_contents_not_available . " WHERE cont_site_zone LIKE 'type-tax-".$tax."-term-%' )" ); # loop taxids type-tax-TAX-term- foreach( $taxids as $id ) { $ids[] = intval( str_replace( 'type-tax-'.$tax.'-term-', '', $id->cont_site_zone ) ); } // end foreach( $catids as $id ) // get terms $terms = get_terms( $tax, array('hide_empty'=>0) ); // max 15 elements min 5 elements in select $count_ = (count($terms)>=15) ? 15 : count($terms)+1; $count_ = ($count_<5) ? 5 : $count_; # set answer $output = array(); $output[] ='

' . __( 'Select a term of this taxonomy (* term edited)', MP_ADVERTISING_LANGUAGE_PLUGIN ) . '

'; $zone=str_replace('type-','',$_POST['zone']); $output[] = ''; # return string wp_die( implode( '', $output ) ); } // end function updateSearchPostTaxType() /** * Updates list (select#select-post) * send in post method a variable called "search" **/ public static function searchGetPostsList() { # use global db object global $wpdb; # get post type selected $post_type = str_replace( 'type-', "", $_POST['zone']); // get ids of post ( that have this post_types ) "edited" $ids = array(); $postsIDS = $wpdb->get_results (" ( SELECT DISTINCT( cont_site_zone ) FROM " . MP_Advertising::$table_banners_contents . " WHERE cont_site_zone LIKE 'type-".$post_type."-%' ) UNION ( SELECT DISTINCT( cont_site_zone ) FROM " . MP_Advertising::$table_banners_contents_not_available . " WHERE cont_site_zone LIKE 'type-".$post_type."-%' ) "); # loop posts foreach ( $postsIDS as $id ) { $ids[] = str_replace( 'type-'.$post_type."-", '', $id->cont_site_zone ) ; } // end foreach( $postsIDS as $id ) // max 15 elements min 5 elements in select $count_ = (count($postsIDS)>=15) ? 15 : count($postsIDS)+1; $count_ = ($count_<5) ? 5 : $count_; # query get posts $args = array( "posts_per_page" => -1, "s" => $_POST['search'], "orderby" => "title", "post_type" => array($post_type), "post_status" => array('any') ); $posts = get_posts($args); if(is_numeric($_POST['search'])){ $post_numeric= get_post((int)$_POST['search']); } # set answer $output = array(); $output[] ='

' . sprintf( __( 'Select a %s', MP_ADVERTISING_LANGUAGE_PLUGIN ), $post_type) . '

'; $output[] = ''; # return string wp_die( implode( '', $output ) ); } // end function searchGetPostsList() /************************ * LINKS * ************************/ /** * Updates the page view with the setted pages list * The first view clicking on the button "links" **/ public static function updateLinkView() { # use global db object global $wpdb; $row = MP_Advertising::getBannerRowTemplate( 'url' ); # gets already setted pages $pages = $wpdb->get_results(" SELECT DISTINCT( cont_site_zone ) FROM " . MP_Advertising::$table_banners_contents . " WHERE cont_banner_content != '' AND cont_banner_url_based = '1' "); # set answer $output = array(); $output[] = ''; # loop pages $output[] = ''; # return string wp_die( implode( '', $output ) ); } // end function updateLinkView() /** * Delete links banners according to posted data **/ public static function deleteLinkBanners() { # use global db object global $wpdb; # check params if( !isset( $_POST['zone'] ) || !trim( $_POST['zone'] ) ) { wp_die( 'Error: ' . __( 'Incorrect parameters', MP_ADVERTISING_LANGUAGE_PLUGIN ) ); } // end if( !isset( $_POST['zone'] ) || !trim( $_POST['zone'] ) ) $zone = trim( $_POST['zone'] ); # deletes db info $wpdb->query(" DELETE FROM " . MP_Advertising::$table_banners_contents . " WHERE cont_site_zone = '" . esc_sql( $zone ) . "' AND cont_banner_url_based = '1' "); wp_die( 'ok' ); } // end function deleteLinkBanners() /************************** * SAVE BANNERS PER ZONE * **************************/ /** * Set banners according to posted data **/ public static function saveBannersPerZone() { # use global db object global $wpdb; # check params if ( !isset( $_POST['zone'] ) || !trim( $_POST['zone'] ) ) { wp_die( 'Error: ' . __( 'Incorrect parameters', MP_ADVERTISING_LANGUAGE_PLUGIN ) ); } // end if( !isset( $_POST['zone'] ) || !trim( $_POST['zone'] ) ) if ( !isset( $_POST['codes'] ) || !trim( $_POST['codes'] ) ) { wp_die( 'Error: ' . __( 'Incorrect parameters', MP_ADVERTISING_LANGUAGE_PLUGIN ) ); } // end if( !isset( $_POST['codes'] ) || !trim( $_POST['codes'] ) ) $zone = trim( $_POST['zone'] ); $codes = explode( '<[{SEP}]>', utf8_decode( trim( base64_decode( $_POST['codes'] ) ) ) ); $url_based = substr( $zone, 0, 7 ) === 'http://' ? '1' : '0'; # delete previous entries // $wpdb->query("DELETE FROM " . MP_Advertising::$table_banners_contents . " WHERE cont_site_zone = '" . esc_sql( $zone ) . "'"); # loop codes foreach ( $codes as $code ) { # extract values $info = explode( '<{[SEP]}>', $code ); # if there's not content $trimmed=trim( $info[1] ); if( empty($trimmed) ) { $wpdb->query("DELETE FROM " . MP_Advertising::$table_banners_contents . " WHERE `cont_site_zone` = '" . esc_sql( $zone ) . "' AND `cont_banner_label` = '" . esc_sql( $info[0] ) . "'"); } else { # if there's content $num_rows=$wpdb->get_var("SELECT count(*) FROM " . MP_Advertising::$table_banners_contents . " WHERE `cont_site_zone` = '" . esc_sql( $zone ) . "' AND `cont_banner_label` = '" . esc_sql( $info[0] ) . "' LIMIT 1"); if((int)$num_rows>1 || (int)$num_rows==0) { $wpdb->query("DELETE FROM " . MP_Advertising::$table_banners_contents . " WHERE `cont_site_zone` = '" . esc_sql( $zone ) . "' AND `cont_banner_label` = '" . esc_sql( $info[0] ) . "'"); $res=$wpdb->query(" REPLACE INTO " . MP_Advertising::$table_banners_contents . " (`cont_site_zone`, `cont_banner_label`, `cont_banner_url_based`, `cont_banner_content`) VALUES ( '" . esc_sql( $zone ) . "', '" . esc_sql( $info[0] ) . "', '{$url_based}', '" . esc_sql( str_replace( array( '<[EAMP]>', '<[PLUS]>' ), array( '&', '+' ), $info[1] ) ) . "' ) "); } else{ //only one $res=$wpdb->query(" UPDATE " . MP_Advertising::$table_banners_contents . " SET `cont_site_zone`='" . esc_sql( $zone ) . "',`cont_banner_label`='" . esc_sql( $info[0] ) . "',`cont_banner_url_based`='{$url_based}',`cont_banner_content`='" . esc_sql( str_replace( array( '<[EAMP]>', '<[PLUS]>' ), array( '&', '+' ), $info[1] ) ) . "' WHERE `cont_site_zone` = '" . esc_sql( $zone ) . "' AND `cont_banner_label` = '" . esc_sql( $info[0] ) . "'"); } //die; # writes db info } // end if( trim( $info[1] ) !== '' ) } // end foreach( $codes as $code ) # return result wp_die( 'ok' ); } // end function saveBannersPerZone() /*************************** * DISABLE/ENABLE BANNERS * ***************************/ /** * Enable or disable banners **/ public static function toggleSwitchOnOffBanner() { # use global db object global $wpdb; # check params if ( !isset( $_POST['zone'] ) || !trim( $_POST['zone'] ) ) { wp_die( 'Error: ' . __( 'Incorrect parameters', MP_ADVERTISING_LANGUAGE_PLUGIN ) ); } // end if( !isset( $_POST['zone'] ) || !trim( $_POST['zone'] ) ) if ( !isset( $_POST['banner'] ) || !trim( $_POST['banner'] ) ) { wp_die( 'Error: ' . __( 'Incorrect parameters', MP_ADVERTISING_LANGUAGE_PLUGIN ) ); } // end if( !isset( $_POST['banner'] ) || !trim( $_POST['banner'] ) ) if ( !isset( $_POST['checked'] ) || !is_numeric( $_POST['checked'] )) { wp_die( 'Error: ' . __( 'Incorrect parameters', MP_ADVERTISING_LANGUAGE_PLUGIN ) ); } // end if( !isset( $_POST['checked'] ) || !trim( $_POST['checked'] ) ) $zone = trim( $_POST['zone'] ); $banner = trim( $_POST['banner'] ); $checked = trim( $_POST['checked'] ); if ( (bool)$checked ) { # deletes db info $wpdb->query(" DELETE FROM " . MP_Advertising::$table_banners_contents_not_available . " WHERE cont_site_zone = '" . esc_sql( $zone ) . "' AND cont_banner_label = '".esc_sql( $banner )."' "); } else { $wpdb->query(" DELETE FROM " . MP_Advertising::$table_banners_contents_not_available . " WHERE cont_site_zone = '" . esc_sql( $zone ) . "' AND cont_banner_label = '".esc_sql( $banner )."' "); # add db info $wpdb->query(" REPLACE INTO `" . MP_Advertising::$table_banners_contents_not_available . "` (`cont_site_zone`, `cont_banner_label`) VALUES ('".esc_sql( $zone )."','".esc_sql( $banner )."') "); } wp_die( 'ok' ); } // end function toggleSwitchOnOffBanner() /** * Enable or disable new MP ADV **/ public static function toggleSwitchOnOffNewMPAdv() { # use global db object global $wpdb; # check params if ( !isset( $_POST['checked'] ) || !is_numeric( $_POST['checked'] )) { wp_die( 'Error: ' . __( 'Incorrect parameters', MP_ADVERTISING_LANGUAGE_PLUGIN ) ); } // end if( !isset( $_POST['checked'] ) || !trim( $_POST['checked'] ) ) $checked = trim( $_POST['checked'] ); update_option('mp_advertising_v_1_7',$checked); wp_die( 'ok' ); } // end function toggleSwitchOnOffNewMPAdv() /** * save topic tax **/ public static function saveTopicTax() { # use global db object global $wpdb; update_option('mp_advertising_topic_tax',$_POST['tax']); wp_die( 'ok' ); } // end function saveTopicTax() /************************** * OPTIONS PAGE * **************************/ /** * delete banners **/ public static function optionsPageDeleteBanners() { # use global db object global $wpdb; if( !isset( $_POST['banner'] ) || !trim( $_POST['banner'] ) ) { wp_die( 'Error: ' . __( 'Incorrect parameters', MP_ADVERTISING_LANGUAGE_PLUGIN ) ); } // end if( !isset( $_POST['banner'] ) || !trim( $_POST['banner'] ) ) $banner = trim( $_POST['banner'] ); $wpdb->query(" DELETE FROM " . MP_Advertising::$table_banners . " WHERE banner_label = '".esc_sql( $banner )."' "); wp_die( 'ok' ); } // end function optionsPageDeleteBanners() /** * Set new banners **/ public static function optionsPageSetBanners() { # use global db object global $wpdb; # check params if ( !isset( $_POST['input_banner_label'] ) || !trim( $_POST['input_banner_label'] ) ) { wp_die( 'Error: ' . __( 'Incorrect parameters', MP_ADVERTISING_LANGUAGE_PLUGIN ) ); } // end if( !isset( $_POST['input_banner_label'] ) || !trim( $_POST['input_banner_label'] ) ) if ( !isset( $_POST['input_banner_type'] ) || !trim( $_POST['input_banner_type'] )) { wp_die( 'Error: ' . __( 'Incorrect parameters', MP_ADVERTISING_LANGUAGE_PLUGIN ) ); } // end if( !isset( $_POST['input_banner_type'] ) || !trim( $_POST['input_banner_type'] ) ) $input_banner_label = trim( $_POST['input_banner_label'] ); $input_banner_desc = trim( $_POST['input_banner_desc'] ); $input_banner_type = trim( $_POST['input_banner_type'] ); # add db info $wpdb->query(" REPLACE INTO `" . MP_Advertising::$table_banners . "` (`banner_label`, `banner_desc`, `banner_type`) VALUES ('".esc_sql( $input_banner_label )."','".esc_sql( $input_banner_desc )."','".esc_sql( $input_banner_type )."') "); wp_die( 'ok' ); } // end function optionsPageSetBanners() /** * Outputs a banner content * * @param name, string, optional, The banner name * @param before, string, optional, An optional text to prepend to banner content * @param after, string, optional, An optional text to append to banner content * @param echo, boolean, optional, True to echo result * @param disabled_before_after, boolean, optional, True to print only the banner content **/ public static function output( $name = '', $before = '', $after = '', $echo = true, $disabled_before_after=false ) { # use global db object global $wpdb; $result = false; #reverse array order $banners_available_reversed = !empty(MP_Advertising::$banner_zones) ? array_reverse(MP_Advertising::$banner_zones) : null; if(!empty($banners_available_reversed) && false!==$banners_available_reversed){ foreach ( $banners_available_reversed as $zone ) { if( isset( MP_Advertising::$currentBanners[ $name ][ $zone ]) ) { $result = MP_Advertising::$currentBanners[ $name ][ $zone ]; break; } } } if( $before . $after === '' && false !== $result ) { $type = MP_Advertising::$allowedBanners[ $name ]; $before = "
\n\t
"; $after = "\t
\n
"; } // end if( $before . $after === '' && $result !== '' ) if(is_user_logged_in()){ $before="\n\n".$before; $after=$after."\n\n"; } if (!$disabled_before_after && false !== $result && !empty($result) ) { $result = $before . stripslashes($result) . $after; } if ( $echo ) { echo $result; } else { return $result; } // end if( $echo ) } // end function output() /** * set Allowed banners **/ public static function allowedBannersFunction() { # use global db object global $wpdb; # get banners $banners = $wpdb->get_results("SELECT * FROM " . MP_Advertising::$table_banners." ORDER BY banner_label"); foreach ( $banners as $banner ) { MP_Advertising::$allowedBanners[$banner->banner_label]=$banner->banner_type; } } /** * Extract current banners * Only in frontend **/ public static function extractBanners() { if ( is_admin() ) { return true; } # check for banners array if ( is_array( MP_Advertising::$banner_zones ) || isset( $_GET['no_banners'] ) ) { return true; } # use global db object global $wpdb; $queried_object = get_queried_object(); $post_id=@get_the_ID(); MP_Advertising::allowedBannersFunction(); # create banners array MP_Advertising::$banner_zones = array(); # global MP_Advertising::$banner_zones[] = 'global'; # if is home if ( is_home() || is_front_page() ) { MP_Advertising::$banner_zones[] = 'home'; } // end if( is_home() || is_front_page() ) # if is page and not front_page if (!is_front_page() && (is_page() || is_404()) ) { MP_Advertising::$banner_zones[] = 'page-0'; if(is_page()) { MP_Advertising::$banner_zones[] = 'page-'.$post_id; } } // end if( is_page() || is_404() ) # if is category if ( is_category() ) { MP_Advertising::$banner_zones[] = 'cat-0'; MP_Advertising::$banner_zones[] = 'cat-' . $queried_object->term_id; } // end if( is_category()) # if is tag if ( is_tag() ) { MP_Advertising::$banner_zones[] = 'tag-0'; MP_Advertising::$banner_zones[] = 'tag-' . $queried_object->term_id; } // end if( is_category()) # if is tax if ( is_tax() ) { MP_Advertising::$banner_zones[] = 'tax-0'; MP_Advertising::$banner_zones[] = 'tax-' . $queried_object->taxonomy."-term-0"; MP_Advertising::$banner_zones[] = 'tax-' . $queried_object->taxonomy.'-term-'.$queried_object->term_id; } // end if( is_tax() ) # if is post type archive if ( is_post_type_archive() ) { MP_Advertising::$banner_zones[] = 'type-0'; MP_Advertising::$banner_zones[] = 'type-'.get_post_type().'-0'; } // end if( is_post_type_archive() ) # if is single $post_id=apply_filters('mp_advertising_filter_post_id', $post_id); if ( is_numeric($post_id) && (is_single() || ($post_id!==false && !empty($post_id)))) { $type = get_post_type($post_id); $categories = get_the_category($post_id); $taxonomies = get_taxonomies(); $start_taxonomies=array("speciale","category","post_tag"); foreach($taxonomies as $k=>$tax){ if(in_array($k,MP_Advertising::$disabled_taxonomies) || in_array($tax,MP_Advertising::$disabled_taxonomies)){ unset($taxonomies[$k]); } } $mp_advertising_topic_tax=get_option('mp_advertising_topic_tax'); if(empty($mp_advertising_topic_tax) || false==$mp_advertising_topic_tax){ $mp_advertising_topic_tax=$start_taxonomies; } $mp_advertising_v_1_7=(bool)get_option('mp_advertising_v_1_7'); if(!$mp_advertising_v_1_7){ # if it have a category (page) if ( !empty($categories) ) { MP_Advertising::$banner_zones[] = 'cat-0'; } foreach ( $categories as $category ) { if ( $category->category_parent==0 ) { MP_Advertising::$banner_zones[] = 'cat-' . $category->cat_ID; } // end if( intval( $category->parent ) === 0 ) } // end foreach( $categories as $category ) # if it have a tax (page) $tax_initial=false; foreach ( $taxonomies as $taxonomy ) { $term_initial=false; if($taxonomy!='post_tag' && $taxonomy!='category'){ $tts = wp_get_post_terms($post_id, $taxonomy); if ( !empty($tts) && false !== $tts ) { foreach ($tts as $tt) { if(isset($tt->term_id)){ if ( !$tax_initial ) { MP_Advertising::$banner_zones[] = 'tax-0'; $tax_initial=true; } if ( !$term_initial ) { MP_Advertising::$banner_zones[] = 'tax-' . $taxonomy ."-term-0"; $term_initial=true; } MP_Advertising::$banner_zones[] = 'tax-' .$taxonomy.'-term-'.$tt->term_id; } } } } } // end foreach( $taxonomies as $taxonomy ) } MP_Advertising::$banner_zones[] = 'type-0'; MP_Advertising::$banner_zones[] = 'type-' . $type. '-0'; # if it have a type other tags foreach($taxonomies as $k=>$tax){ if(in_array($k,$mp_advertising_topic_tax) || in_array($tax,$mp_advertising_topic_tax)){ unset($taxonomies[$k]); } } foreach ( $taxonomies as $taxonomy ) { $term_initial=false; $tts = wp_get_post_terms($post_id, $taxonomy); if ( !empty($tts) && false !== $tts ) { foreach ($tts as $tt) { if(isset($tt->term_id)){ if ( !$term_initial ) { MP_Advertising::$banner_zones[] = 'type-tax-' . $taxonomy ."-term-0"; $term_initial=true; } MP_Advertising::$banner_zones[] = 'type-tax-' .$taxonomy.'-term-'.$tt->term_id; } } } } // end foreach( $taxonomies as $taxonomy ) # if it have a type tax speciale,post_tag,category foreach ( array_reverse($mp_advertising_topic_tax) as $taxonomy ) { $term_initial=false; $tts = wp_get_post_terms($post_id, $taxonomy); if ( !empty($tts) && false !== $tts ) { foreach ($tts as $tt) { if(isset($tt->term_id)){ if ( !$term_initial ) { MP_Advertising::$banner_zones[] = 'type-tax-' . $taxonomy ."-term-0"; $term_initial=true; } MP_Advertising::$banner_zones[] = 'type-tax-' .$taxonomy.'-term-'.$tt->term_id; } } } } // end foreach( $taxonomies as $taxonomy ) MP_Advertising::$banner_zones[] = 'type-' . $type. '-'.$post_id; } // end if( is_single() ) #current page url MP_Advertising::$banner_zones[] = MP_Advertising::getCurrentPageUrl(); if(isset($_GET['check_banner']) && $_GET['check_banner']==1){ var_dump(MP_Advertising::$banner_zones);die; } // create site_zone condition to use in future queries $_site_zones=null; $total_banner_zones = count(MP_Advertising::$banner_zones); if ( is_array(MP_Advertising::$banner_zones) && $total_banner_zones>0 ) { $i=0; foreach ( MP_Advertising::$banner_zones as $cont_banner_label ) { $i++; $_site_zones.="`cont_site_zone` LIKE '" . esc_sql( $cont_banner_label ) . "'"; if ( $i<$total_banner_zones ) { $_site_zones.=" OR "; } } } // get banners not available $not_availables = $wpdb->get_results(" SELECT * FROM `" . MP_Advertising::$table_banners_contents_not_available . "`, " . MP_Advertising::$table_banners . " WHERE " . MP_Advertising::$table_banners_contents_not_available . ".cont_banner_label=" . MP_Advertising::$table_banners . ".banner_label AND (".$_site_zones.") GROUP BY `cont_banner_label`"); // delete banners not available from allowerdBanners lists foreach ( $not_availables as $not_available ) { unset(MP_Advertising::$allowedBanners[$not_available->cont_banner_label]); } // extract all "allowed" banners not empty $extract_banners = $wpdb->get_results(" SELECT * FROM `" . MP_Advertising::$table_banners_contents . "`, " . MP_Advertising::$table_banners . " WHERE " . MP_Advertising::$table_banners_contents . ".cont_banner_label=" . MP_Advertising::$table_banners . ".banner_label AND `cont_banner_content` != '' AND (".$_site_zones." ) AND `cont_banner_label` IN('" . implode( "','", array_keys( MP_Advertising::$allowedBanners ) ) . "') "); //set currentBanners foreach ( $extract_banners as $extract_banner ) { MP_Advertising::$currentBanners[ strtoupper( $extract_banner->cont_banner_label ) ][$extract_banner->cont_site_zone] = $extract_banner->cont_banner_content; } } // end function extractBanners() /************************* * PRIVATE STATIC METHODS * *************************/ /** * Install plugin if required **/ private static function createTables() { # use global db object # check if tables exists global $wpdb; if ( $wpdb->get_var("SHOW TABLES LIKE '" . MP_Advertising::$table_banners . "'") != MP_Advertising::$table_banners || $wpdb->get_var("SHOW TABLES LIKE '" . MP_Advertising::$table_banners_contents_not_available . "'") != MP_Advertising::$table_banners_contents_not_available || $wpdb->get_var("SHOW TABLES LIKE '" . MP_Advertising::$table_banners_contents . "'") != MP_Advertising::$table_banners_contents) { $wpdb->query(" CREATE TABLE IF NOT EXISTS `" . MP_Advertising::$table_banners_contents_not_available . "` ( `cont_site_zone` text NOT NULL default '', `cont_banner_label` varchar(64) NOT NULL default '' ) ENGINE=MyISAM DEFAULT CHARSET=utf8; "); $wpdb->query(" CREATE TABLE IF NOT EXISTS `" . MP_Advertising::$table_banners_contents . "` ( `cont_site_zone` text NOT NULL default '', `cont_banner_label` varchar(64) NOT NULL default '', `cont_banner_url_based` enum('0','1') NOT NULL default '0', `cont_banner_content` text NULL ) ENGINE=MyISAM DEFAULT CHARSET=utf8; "); $wpdb->query(" CREATE TABLE IF NOT EXISTS `" . MP_Advertising::$table_banners . "` ( `banner_label` varchar(64) NOT NULL default '', `banner_desc` varchar(255) NOT NULL default '', `banner_type` varchar(64) NOT NULL default 'banner', PRIMARY KEY (`banner_label`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; "); $values = null; $i = 0; $total_banner = count(MP_Advertising::$initialAllowedBanners); foreach ( MP_Advertising::$initialAllowedBanners as $k=>$banner ) { $desc=str_replace('_',' ',strtolower($k)); $values.="('".$k."', '".$desc."', '".$banner."')"; $i++; if ( $total_banner>$i ) { $values.=","; } } $wpdb->query(" REPLACE INTO `" . MP_Advertising::$table_banners . "` (`banner_label`, `banner_desc`, `banner_type`) VALUES ".$values." ;"); } } // end function createTables() /** * Set the db prefix **/ private static function setDbPrefix() { # use global db object global $wpdb; // MP_Advertising::$dbPrefix = preg_replace( '#_[0-9]+_#', '_', $wpdb->prefix ); MP_Advertising::$dbPrefix = $wpdb->prefix; } // end function setDbPrefix() /** * Set the db tables **/ private static function setDbTables() { MP_Advertising::$table_banners_contents=(defined("MP_ADVERTISING_TABLE_BANNERS_CONTENTS")) ? MP_Advertising::$dbPrefix.MP_ADVERTISING_TABLE_BANNERS_CONTENTS : MP_Advertising::$dbPrefix . "advertising_banners_contents"; MP_Advertising::$table_banners_contents_not_available=(defined("MP_ADVERTISING_TABLE_BANNERS_CONTENTS")) ? MP_Advertising::$dbPrefix.MP_ADVERTISING_TABLE_BANNERS_CONTENTS_NOT_AVAILABLE : MP_Advertising::$dbPrefix . "advertising_banners_contents_not_available"; MP_Advertising::$table_banners=(defined("MP_ADVERTISING_TABLE_BANNERS")) ? MP_Advertising::$dbPrefix.MP_ADVERTISING_TABLE_BANNERS : MP_Advertising::$dbPrefix . "advertising_banners"; } // end function setDbTables() /** * Returns the current page url **/ private static function getCurrentPageUrl() { // if( is_single() ) { // return get_permalink(); // } return implode('', array ( $_SERVER['SERVER_PORT'] === 443 ? 'https' : 'http', '://', $_SERVER['SERVER_NAME'], !in_array( $_SERVER['SERVER_PORT'], array( 80, 443 ) ) ? ':' . $_SERVER['SERVER_PORT'] : '', $_SERVER['REQUEST_URI'] ) ); } // end function getCurrentPageUrl() /** * Returns the banner row template * * @param type, string, required, The banner row type * * @return string **/ private static function getBannerRowTemplate( $type ) { $row_template = ''; switch( $type ) { case 'zone': $row_template = ' ' ; break; case 'url': $row_template = ' ' ; break; } // end switch( $type ) return $row_template; } // end function getBannerRowTemplate() } // end class MP_Advertising