1. <?php
  2. /**
  3.  * Class for Manage Admin (back-end)
  4.  *
  5.  * @package            wpBannerize
  6.  * @subpackage         wpBannerizeAdmin
  7.  * @author             =undo= <g.fazioli@undolog.com>, <g.fazioli@saidmade.com>
  8.  * @copyright          Copyright © 2008-2012 Saidmade Srl
  9.  *
  10.  */
  11.  
  12. //mod jrc 070613: my mods (throughout) to ensure plugin admin features (edit, create,delete banners, etc) are ONLY presented to users with (custom) role capability, as configured in (modified) main.h.php (define( 'kWPBannerizeUserCapabilitiy', 'manage_wp_bannerize_banners' )) AND ALSO specified as role capability for user/groups roles via wp admin, roles. 'administrator' role has this new capability added by default.
  13. //files changed: wpBannerizeAdmin.php, main.php, main.h.php
  14. //my mod mark: 'mod jrc 070613', throughout
  15.  
  16. /**
  17.  * Outputs the html inline style attribute with display.
  18.  *
  19.  * Compares the first two arguments and if not identical marks as display none
  20.  *
  21.  * @since    3.0.0
  22.  *
  23.  * @param       $display
  24.  * @param mixed $current (true) The other value to compare if not just true
  25.  * @param bool  $echo    Whether to echo or just return the string
  26.  *
  27.  * @internal param mixed $checked One of the values to compare
  28.  * @return string html attribute or empty string
  29.  */
  30. function hidden( $display, $current = true, $echo = true ) {
  31.     if ( (string)$display !== (string)$current ) {
  32.         $result = " style='display:none'";
  33.     } else {
  34.         $result = '';
  35.     }
  36.  
  37.     if ( $echo ) {
  38.         echo $result;
  39.     }
  40.  
  41.     return $result;
  42. }
  43.  
  44.  
  45. class WPBannerizeAdmin extends WPBannerizeClass {
  46.  
  47.     var $pageMain;
  48.     var $pageAddBanner;
  49.     var $pageSettings;
  50.     var $pageTools;
  51.  
  52.     /**
  53.      * Keep the default CSS sample rules
  54.      *
  55.      * @var string
  56.      */
  57.     var $cssRulesSample = '';
  58.  
  59.     function WPBannerizeAdmin( $__file__ ) {
  60.         $this->__construct( $__file__);
  61.     }
  62.  
  63.     function __construct( $__file__ ) {
  64.         // super
  65.         parent::WPBannerizeClass( $__file__ );
  66.  
  67.         // Foo string for PoEdit
  68.         $foo_publish = __( 'Publish', 'wp-bannerize' );
  69.  
  70.         //$this->init();
  71.         add_action( 'plugins_loaded', array( $this, 'init' ), 1 );
  72.     }
  73.  
  74.     /**
  75.      * Init the default plugin options and re-load from WP
  76.      *
  77.      * @since 2.2.2
  78.      */
  79.     function init() {
  80.                    
  81.         // Load localizations if available; @since 2.4.0
  82.         load_plugin_textdomain( 'wp-bannerize', false, 'wp-bannerize/localization' );
  83.  
  84.         $this->cssRulesSample = $this->cssRulesSample();
  85.         // Add version control in options
  86.         $this->options = $this->defaultOptions();
  87.         add_option( $this->options_key, $this->options );
  88.  
  89.         $this->options = get_option( $this->options_key );
  90.  
  91.        
  92.         // Add option menu in Wordpress backend
  93.         add_action( 'admin_init', array ( $this, 'plugin_init' ) );
  94.         add_action( 'admin_menu', array ( $this, 'plugin_setup' ) );
  95.  
  96.         add_filter( 'screen_layout_columns', array ( &$this, 'on_screen_layout_columns' ), 10, 2 );
  97.  
  98.         // Update version control in options
  99.         update_option( $this->options_key, $this->options );
  100.     }
  101.  
  102.     /**
  103.      * Comodity: echo saidmade WP Bannerize header
  104.      *
  105.      * @return void
  106.      */
  107.     function saidmadeHeader() {
  108.         ?>
  109.     <div class="wp_saidmade_box">
  110.         <a class="wp_saidmade_logo" href="http://www.wpxtre.me">
  111.             <?php echo $this->plugin_name ?> ver. <?php echo $this->version ?>
  112.         </a>
  113.     </div><?php
  114.     }
  115.  
  116.     function on_screen_layout_columns( $columns, $screen ) {
  117.         if ( $screen == $this->pageSettings ) {
  118.             $columns[$this->pageSettings] = 1;
  119.         } else {
  120.             if ( $screen == $this->pageTools ) {
  121.                 $columns[$this->pageTools] = 2;
  122.             }
  123.         }
  124.         return $columns;
  125.     }
  126.  
  127.     /**
  128.      * Return css rules sample
  129.      *
  130.      * @return string
  131.      */
  132.     function cssRulesSample() {
  133.         ob_start();
  134.         require_once( 'wpBannerizeCssRulesSample.css' );
  135.         $result = ob_get_contents();
  136.         ob_end_clean();
  137.         return $result;
  138.     }
  139.  
  140.     /**
  141.      * Return and setting default options values
  142.      *
  143.      * @return mixed
  144.      */
  145.     function defaultOptions() {
  146.         $this->options = array ( 'wp_bannerize_version'           => $this->version,
  147.                                  'clickCounterEnabled'            => '1',
  148.                                  'impressionsEnabled'             => '1',
  149.                                  'supportWPBannerize'             => '1',
  150.                                  'comboWindowModeFlash'           => 'Window',
  151.                                  'linkDescription'                => '0',
  152.                                  'wpBannerizeStyleDefault'        => 'default',
  153.                                  'wpBannerizeStyle'               => kWPBannerizeBannerStyleDefault,
  154.                                  'wpBannerizeStyleCustom'         => $this->cssRulesSample,
  155.                                  'wpBannerizeNoBannerHTMLMessage' => '<p>No Banner to display</p>' );
  156.         return $this->options;
  157.     }
  158.  
  159.     /**
  160.      * Reset options to default values
  161.      *
  162.      * @return void
  163.      */
  164.     function resetOptionsToDefault() {
  165.         $this->options = $this->defaultOptions();
  166.         update_option( $this->options_key, $this->options );
  167.     }
  168.  
  169.     /**
  170.      * Register style for plugin
  171.      *
  172.      * @since 2.4.9
  173.      * @return void
  174.      */
  175.     function plugin_init() {
  176.         wp_register_style( 'WPBannerizeBannerStyleAdmin', $this->uri . kWPBannerizeBannerStyleAdmin );
  177.         wp_register_style( 'wp-bannerize-jqueryui-css', $this->uri . "/css/ui-lightness/jquery-ui.custom.css" );
  178.         wp_register_style( 'fancybox-css', $this->uri . kWPBannerizeFancyBoxCSS );
  179.     }
  180.  
  181.     /**
  182.      * Execute when plugin is showing on backend
  183.      *
  184.      * @since 2.4.9
  185.      * @return void
  186.      */
  187.     function plugin_admin_scripts() {
  188.         wp_enqueue_script( 'common' );
  189.         wp_enqueue_script( 'postbox' );
  190.         wp_enqueue_script( 'wp-lists' );
  191.  
  192.         // Add wp_enqueue_script for jquery library
  193.         wp_enqueue_script( 'jquery-ui-sortable' );
  194.  
  195.         wp_enqueue_script( 'fancybox_js',
  196.             $this->uri . kWPBannerizeFancyBoxJavascript, array ( 'jquery' ), kWPBannerizeVersion, true );
  197.         wp_enqueue_script( 'WPBannerizeJavascriptAdmin', $this->uri . kWPBannerizeJavascriptAdmin, array ( 'jquery',
  198.             'media-upload',
  199.             'thickbox' ), kWPBannerizeVersion, true );
  200.         wp_enqueue_script( 'wp_bannerize_jquery_dp_js',
  201.             $this->uri . '/js/jquery-ui.min.js', array ( 'jquery' ), kWPBannerizeVersion, true );
  202.         wp_enqueue_script( 'wp_bannerize_timepicker_js',
  203.             $this->uri . '/js/jquery.timepicker.min.js', array ( 'jquery-ui-core' ), kWPBannerizeVersion, true );
  204.  
  205.         // Add main admin javascript
  206.         wp_localize_script( 'WPBannerizeJavascriptAdmin', 'wpBannerizeJavascriptLocalization', array ( 'wpBannerizeFormAction'       => kWPBannerizeFormAction,
  207.                                                                                                        'ajaxURL'                     => $this->ajaxURL,
  208.                                                                                                        'messageConfirm'              => __( 'WARINING!! Do you want delete this banner?', 'wp-bannerize' ),
  209.                                                                                                        'messageTruncateConfirm'      => __( 'WARINING!! Do you have to check `Confirm unreversible action` ', 'wp-bannerize' ),
  210.                                                                                                        'messageTruncateConfirmAgain' => __( 'WARINING!! Are you sure to erase all WP Bannerize Database Table? ', 'wp-bannerize' ),
  211.                                                                                                        'timeOnlyTitle'               => __( 'Choose Time', 'wp-bannerize' ),
  212.                                                                                                        'timeText'                    => __( 'Time', 'wp-bannerize' ),
  213.                                                                                                        'hourText'                    => __( 'Hour', 'wp-bannerize' ),
  214.                                                                                                        'minuteText'                  => __( 'Minute', 'wp-bannerize' ),
  215.                                                                                                        'secondText'                  => __( 'Seconds', 'wp-bannerize' ),
  216.                                                                                                        'currentText'                 => __( 'Now', 'wp-bannerize' ),
  217.                                                                                                        'dayNamesMin'                 => __( 'Su,Mo,Tu,We,Th,Fr,Sa', 'wp-bannerize' ),
  218.                                                                                                        'monthNames'                  => __( 'January,February,March,April,May,June,July,August,September,October,November,December', 'wp-bannerize' ),
  219.                                                                                                        'closeText'                   => __( 'Close', 'wp-bannerize' ),
  220.                                                                                                        'dateFormat'                  => __( 'mm/dd/yy', 'wp-bannerize' ) ) );
  221.     }
  222.  
  223.     /**
  224.      * Execute when plugin is showing on backend
  225.      *
  226.      * @return void
  227.      */
  228.     function plugin_admin_styles() {
  229.         wp_enqueue_style( 'fancybox-css' );
  230.         wp_enqueue_style( 'thickbox' );
  231.         wp_enqueue_style( 'WPBannerizeBannerStyleAdmin' );
  232.         wp_enqueue_style( 'wp-bannerize-jqueryui-css' );
  233.     }
  234.  
  235.     function didToolsLoadPage() {
  236.         add_meta_box( 'wp_bannerize_tools_editor', __( 'PHP Function and Shortcode Editor', 'wp-bannerize' ), array ( &$this,
  237.             'boxTools' ), $this->pageTools, 'normal', 'core' );
  238.         add_meta_box( 'wp_bannerize_tools_database', __( 'Database', 'wp-bannerize' ), array ( &$this,
  239.             'boxToolsDatabase' ), $this->pageTools, 'side', 'core' );
  240.     }
  241.  
  242.     function didSettingsLoadPage() {
  243.         add_meta_box( kWPBannerizeMetaBoxSettingsKey, __( 'Settings', 'wp-bannerize' ), array ( &$this,
  244.             'boxSettings' ), $this->pageSettings, 'normal', 'core' );
  245.     }
  246.  
  247.     function boxSettings() {
  248.         require_once( 'wpBannerizeSettings.php' );
  249.     }
  250.  
  251.     function boxTools() {
  252.         require_once( 'wpBannerizeTools.php' );
  253.     }
  254.  
  255.     function boxToolsDatabase() {
  256.         require_once( 'wpBannerizeDatabase.php' );
  257.     }
  258.  
  259.     /**
  260.      * Draw Settings Panel
  261.      */
  262.     function settings() {
  263.         global $screen_layout_columns;
  264.  
  265.         /**
  266.          * Any error flag
  267.          */
  268.         $any_error   = '';
  269.         $this->error = false;
  270.  
  271.         if ( isset( $_POST['command_action'] ) ) {
  272.             if ( $_POST['command_action'] == "updateSettings" && !isset( $_POST['tools'] ) ) {
  273.  
  274.                 $this->options['clickCounterEnabled']     = ( isset( $_POST['clickCounterEnabled'] ) ) ? '1' : '0';
  275.                 $this->options['impressionsEnabled']      = ( isset( $_POST['impressionsEnabled'] ) ) ? '1' : '0';
  276.                 $this->options['supportWPBannerize']      = ( isset( $_POST['supportWPBannerize'] ) ) ? '1' : '0';
  277.                 $this->options['comboWindowModeFlash']    = ( isset( $_POST['comboWindowModeFlash'] ) ) ? $_POST['comboWindowModeFlash'] : 'Window';
  278.                 $this->options['linkDescription']         = ( isset( $_POST['linkDescription'] ) ) ? '1' : '0';
  279.                 $this->options['wpBannerizeStyleDefault'] = ( isset( $_POST['wpBannerizeStyleDefault'] ) ) ? $_POST['wpBannerizeStyleDefault'] : 'default';
  280.  
  281.                 $this->options['wpBannerizeStyleCustom'] = ( isset( $_POST['wpBannerizeStyleCustom'] ) ) ? $_POST['wpBannerizeStyleCustom'] : $this->options['wpBannerizeStyleCustom'];
  282.  
  283.                 $this->options['wpBannerizeStyle'] = ( isset( $_POST['wpBannerizeStyle'] ) ) ? $_POST['wpBannerizeStyle'] : kWPBannerizeBannerStyleDefault;
  284.  
  285.                 $this->options['wpBannerizeNoBannerHTMLMessage'] = ( isset( $_POST['wpBannerizeNoBannerHTMLMessage'] ) ) ? $_POST['wpBannerizeNoBannerHTMLMessage'] : '';
  286.  
  287.                 update_option( $this->options_key, $this->options );
  288.  
  289.                 $any_error = __( 'Settings update succesfully!', 'wp-bannerize' );
  290.             } else {
  291.                 if ( $_POST['tools'] == "resetToDefault" ) {
  292.                     $this->resetOptionsToDefault();
  293.                     $any_error = __( 'Settings Reset to default succesfully!', 'wp-bannerize' );
  294.                 }
  295.             }
  296.         }
  297.  
  298.         ?>
  299.  
  300.     <div class="wrap">
  301.  
  302.         <?php $this->saidmadeHeader(); ?>
  303.  
  304.         <?php if ( $any_error != '' ) : ?>
  305.         <div id="message" class="<?php echo $this->error ? 'error' : 'updated' ?> fade"><p><?php echo $any_error ?></p>
  306.         </div>
  307.         <?php endif; ?>
  308.  
  309.         <div id="poststuff"
  310.              class="metabox-holder<?php echo 2 == $screen_layout_columns ? ' has-right-sidebar' : ''; ?>">
  311.             <div id="side-info-column" class="inner-sidebar">
  312.                 <?php do_meta_boxes( $this->pageSettings, 'side', "" ); ?>
  313.             </div>
  314.             <div id="post-body" class="has-sidebar">
  315.                 <div id="post-body-content" class="has-sidebar-content">
  316.                     <?php do_meta_boxes( $this->pageSettings, 'normal', "" ); ?>
  317.                 </div>
  318.             </div>
  319.             <br class="clear"/>
  320.         </div>
  321.  
  322.         <script type="text/javascript">
  323.             //<![CDATA[
  324.             jQuery( document ).ready( function () {
  325.                 // close postboxes that should be closed
  326.                 jQuery( '.if-js-closed' ).removeClass( 'if-js-closed' ).addClass( 'closed' );
  327.                 // postboxes setup
  328.                 postboxes.add_postbox_toggles( '<?php echo $this->pageSettings; ?>' );
  329.  
  330.             } );
  331.             //]]>
  332.         </script>
  333.     </div>
  334.  
  335.     <?php
  336.  
  337.     }
  338.  
  339.     /**
  340.      * Draw Tools Panel
  341.      */
  342.     function tools() {
  343.         global $screen_layout_columns;
  344.  
  345.         /**
  346.          * Any error flag
  347.          */
  348.         $any_error = '';
  349.  
  350.         if ( isset( $_POST[kWPBannerizeFormSender] ) && $_POST[kWPBannerizeFormSender] == kWPBannerizeMetaBoxToolsKey
  351.         ) {
  352.             if ( isset( $_POST[kWPBannerizeFormAction] ) && isset( $_POST['securityConfirm'] ) &&
  353.                 $_POST[kWPBannerizeFormAction] == kWPBannerizeFormActionTruncateTable
  354.             ) {
  355.                 $this->truncateTable();
  356.                 $any_error = __( 'WP Bannerize Table was erase succesfully!', 'wp-bannerize' );
  357.             }
  358.         }
  359.         ?>
  360.  
  361.     <div class="wrap">
  362.  
  363.         <?php $this->saidmadeHeader(); ?>
  364.  
  365.         <?php if ( $any_error != '' ) : ?>
  366.         <div id="message" class="<?php echo $this->error ? 'error' : 'updated' ?> fade"><p><?php echo $any_error ?></p>
  367.         </div>
  368.         <?php endif; ?>
  369.  
  370.         <div id="poststuff"
  371.              class="metabox-holder<?php echo 2 == $screen_layout_columns ? ' has-right-sidebar' : ''; ?>">
  372.             <div id="side-info-column" class="inner-sidebar">
  373.                 <?php do_meta_boxes( $this->pageTools, 'side', "" ); ?>
  374.             </div>
  375.             <div id="post-body" class="has-sidebar">
  376.                 <div id="post-body-content" class="has-sidebar-content">
  377.                     <?php do_meta_boxes( $this->pageTools, 'normal', "" ); ?>
  378.                 </div>
  379.             </div>
  380.             <br class="clear"/>
  381.         </div>
  382.  
  383.         <script type="text/javascript">
  384.             //<![CDATA[
  385.             jQuery( document ).ready( function () {
  386.                 // close postboxes that should be closed
  387.                 jQuery( '.if-js-closed' ).removeClass( 'if-js-closed' ).addClass( 'closed' );
  388.                 // postboxes setup
  389.                 postboxes.add_postbox_toggles( '<?php echo $this->pageTools; ?>' );
  390.  
  391.             } );
  392.             //]]>
  393.         </script>
  394.     </div>
  395.  
  396.     <?php
  397.  
  398.     }
  399.  
  400.  
  401.     /**
  402.      * Setup main init: add hook for backend
  403.      *
  404.      * @revision 2.4.9
  405.      */
  406.     function plugin_setup() {
  407.  
  408.         if ( function_exists( 'add_menu_page' ) ) {
  409.             $plugin_page = add_menu_page( $this->plugin_name, $this->plugin_name, kWPBannerizeUserCapabilitiy,
  410.                 $this->directory . '-mainshow', array ( &$this, 'show_banners' ),
  411.                 $this->uri . "/css/images/wp-bannerize-16x16.png" );
  412.         }
  413.         if ( function_exists( 'add_submenu_page' ) ) {
  414.  
  415.             $this->pageMain = add_submenu_page( $this->directory .
  416.                     '-mainshow', __( 'Edit', 'wp-bannerize' ), __( 'Edit', 'wp-bannerize' ), kWPBannerizeUserCapabilitiy,
  417.                 $this->directory . '-mainshow', array ( &$this, 'show_banners' ) );
  418.  
  419.             $this->pageAddBanner = add_submenu_page( $this->directory .
  420.                     '-mainshow', __( 'Add New', 'wp-bannerize' ), __( 'Add New', 'wp-bannerize' ), kWPBannerizeUserCapabilitiy,
  421.                 $this->directory . '-addnew', array ( &$this, 'add_new_banner' ) );
  422.  
  423.             $this->pageSettings = add_submenu_page( $this->directory .
  424.                     '-mainshow', __( 'Settings', 'wp-bannerize' ), __( 'Settings', 'wp-bannerize' ), kWPBannerizeUserCapabilitiy,
  425.                 $this->directory . '-settings', array ( &$this, 'settings' ) );
  426.  
  427.             $this->pageTools = add_submenu_page( $this->directory .
  428.                     '-mainshow', __( 'Tools', 'wp-bannerize' ), __( 'Tools', 'wp-bannerize' ), kWPBannerizeUserCapabilitiy,
  429.                 $this->directory . '-tools', array ( &$this, 'tools' ) );
  430.  
  431.             add_action( 'load-' . $this->pageSettings, array ( &$this, 'didSettingsLoadPage' ) );
  432.             add_action( 'load-' . $this->pageTools, array ( &$this, 'didToolsLoadPage' ) );
  433.         }
  434.  
  435.         add_action( 'admin_print_scripts-' . $plugin_page, array ( $this, 'plugin_admin_scripts' ) );
  436.         add_action( 'admin_print_scripts-' . $this->pageAddBanner, array ( $this, 'plugin_admin_scripts' ) );
  437.         add_action( 'admin_print_scripts-' . $this->pageSettings, array ( $this, 'plugin_admin_scripts' ) );
  438.         add_action( 'admin_print_scripts-' . $this->pageTools, array ( $this, 'plugin_admin_scripts' ) );
  439.  
  440.         add_action( 'admin_print_styles-' . $plugin_page, array ( $this, 'plugin_admin_styles' ) );
  441.         add_action( 'admin_print_styles-' . $this->pageAddBanner, array ( $this, 'plugin_admin_styles' ) );
  442.         add_action( 'admin_print_styles-' . $this->pageSettings, array ( $this, 'plugin_admin_styles' ) );
  443.         add_action( 'admin_print_styles-' . $this->pageTools, array ( $this, 'plugin_admin_styles' ) );
  444.  
  445.         // Add contextual Help
  446.         if ( function_exists( 'add_contextual_help' ) ) {
  447.             ob_start();
  448.             require_once( 'wpBannerizeHelp.php' );
  449.             $help = ob_get_contents();
  450.             $help = str_replace( "\t", "", $help );
  451.             $help = trim( $help );
  452.             ob_end_clean();
  453.             add_contextual_help( $plugin_page, $help );
  454.             add_contextual_help( $this->pageAddBanner, $help );
  455.             add_contextual_help( $this->pageSettings, $help );
  456.             add_contextual_help( $this->pageTools, $help );
  457.         }
  458.     }
  459.  
  460.     /**
  461.      * Add new banner Panel
  462.      *
  463.      * @return void
  464.      */
  465.     function add_new_banner() {
  466.         $any_error = '';
  467.  
  468.         if ( isset( $_POST['command_action'] ) && $_POST['command_action'] == "insert" ) {
  469.             $any_error = $this->insertBanner();
  470.         }
  471.         ?>
  472.  
  473.     <div class="wrap">
  474.         <?php $this->saidmadeHeader(); ?>
  475.  
  476.         <?php if ( $any_error != '' ) : ?>
  477.         <div id="message" class="<?php echo $this->error ? 'error' : 'updated' ?> fade"><p><?php echo $any_error ?></p>
  478.         </div>
  479.         <?php endif; ?>
  480.  
  481.         <div id="poststuff" class="metabox-holder">
  482.  
  483.             <div class="sm-padded">
  484.                 <div class="postbox">
  485.                     <h3><span><?php  _e( 'Insert a new banner', 'wp-bannerize' )?></span></h3>
  486.  
  487.                     <div class="inside">
  488.                         <form class="wpBannerizeForm" name="insert_bannerize" method="post" action=""
  489.                               enctype="multipart/form-data">
  490.                             <input type="hidden" name="command_action" value="insert"/>
  491.                             <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo kWPBannerizeMaxFileSize ?>"/>
  492.  
  493.                             <table class="form-table wp_bannerize">
  494.                                 <tr>
  495.                                     <th scope="row">
  496.                                         <label for="filename"><?php _e( 'Banner', 'wp-bannerize' )?>:</label>
  497.                                     </th>
  498.                                     <td colspan="2">
  499.                                         <fieldset>
  500.                                             <legend>
  501.                                                 <input type="radio" name="wpBannerizeBannerType" value="1"
  502.                                                        checked="checked"/> <?php _e( 'From local', 'wp-bannerize' )?>
  503.                                                 <input type="radio" name="wpBannerizeBannerType"
  504.                                                        value="2"/> <?php _e( 'By URL o Media Library', 'wp-bannerize' )?>
  505.                                                 <input type="radio" name="wpBannerizeBannerType"
  506.                                                        value="3"/> <?php _e( 'Free HTML', 'wp-bannerize' )?>
  507.                                             </legend>
  508.                                             <div class="wpBannerizeBannerType1"><input type="file" name="filename"
  509.                                                                                        id="filename"/></div>
  510.                                             <div style="display:none" class="wpBannerizeBannerType2"><input size="32"
  511.                                                                                                             type="text"
  512.                                                                                                             name="filenameFromURL"
  513.                                                                                                             id="filenameFromURL"
  514.                                                                                                             value="http://"/>
  515.                                                 <input id="wpBannerizeButtonFromMediaLibrary" type="button"
  516.                                                        class="button-secondary"
  517.                                                        value="<?php _e( 'Media Library Image', 'wp-bannerize' )?>"/>
  518.                                             </div>
  519.                                             <div style="display:none" class="wpBannerizeBannerType3"><textarea
  520.                                                 name="freeHTML" id="freeHTML" rows="2" cols="50"></textarea></div>
  521.                                         </fieldset>
  522.                                     </td>
  523.                                 </tr>
  524.  
  525.                                 <tr>
  526.                                     <th scope="row">
  527.                                         <label for="start_date"><?php _e( 'Start Date', 'wp-bannerize' )?>:</label>
  528.                                     </th>
  529.                                     <td>
  530.                                         <input class="date" type="text" name="start_date" id="start_date" size="18"/>
  531.                                         <span class="eraser" onclick="jQuery('input#start_date').val('')"></span>
  532.                                         <label for="end_date"><?php _e( 'End Date', 'wp-bannerize' )?>:</label>
  533.                                         <input class="date" type="text" name="end_date" id="end_date" size="18"/>
  534.                                         <span class="eraser" onclick="jQuery('input#end_date').val('')"></span>
  535.                                         (<?php _e( 'Leave empty to always visible', 'wp-bannerize' ) ?>)
  536.                                         <strong><?php _e( 'Server Date/time', 'wp-bannerize' ); ?>
  537.                                             : <?php echo date_i18n( $this->getPHPDateFormat() ) ?></strong>
  538.                                     </td>
  539.                                 </tr>
  540.  
  541.                                 <tr>
  542.                                     <th scope="row">
  543.                                         <label for="group"><?php _e( 'Group', 'wp-bannerize' )?>:</label>
  544.                                     </th>
  545.                                     <td>
  546.                                         <input type="text" maxlength="128" name="group" id="group"
  547.                                                value="group"/> <?php echo $this->get_combo_group() ?>
  548.                                         (<?php _e( 'Insert a key max 128 chars', 'wp-bannerize' )?>)
  549.                                     </td>
  550.                                 </tr>
  551.                                 <tr>
  552.                                     <th scope="row">
  553.                                         <label for="description"><?php _e( 'Description', 'wp-bannerize' )?>:</label>
  554.                                     </th>
  555.                                     <td>
  556.                                         <input type="text" name="description" id="description"/> <input type="checkbox"
  557.                                                                                                         name="use_description"
  558.                                                                                                         value="1"/> <?php _e( 'Use this description in output', 'wp-bannerize' ) ?>
  559.                                     </td>
  560.                                 </tr>
  561.                                 <tr>
  562.                                     <th scope="row"><label for="url"><?php _e( 'URL', 'wp-bannerize' ) ?>:</label></th>
  563.                                     <td>
  564.                                         <input type="text" name="url" id="url"/> <label
  565.                                         for="target"><?php _e( 'Target', 'wp-bannerize' )?>
  566.                                         :</label> <?php echo $this->get_target_combo() ?>
  567.                                     </td>
  568.                                 </tr>
  569.  
  570.                                 <tr>
  571.                                     <th scope="row"><label
  572.                                         for="maxImpressions"><?php _e( 'Max Impressions', 'wp-bannerize' ) ?>:</label>
  573.                                     </th>
  574.                                     <td>
  575.                                         <input type="text" name="maxImpressions" id="maxImpressions" value="0"
  576.                                                size="4"/>
  577.                                         (<?php _e( 'When Impressions are great than this value then this banner is set to hidden', 'wp-bannerize' ) ?>
  578.                                         )
  579.                                     </td>
  580.                                 </tr>
  581.  
  582.                                 <tr>
  583.                                     <th scope="row"><label
  584.                                         for="nofollow"><?php _e( 'Add “nofollow“ attribute', 'wp-bannerize' ) ?></label>
  585.                                     </th>
  586.                                     <td><input type="checkbox" name="nofollow" id="nofollow" value="1"
  587.                                                checked="checked"/></td>
  588.                                 </tr>
  589.                             </table>
  590.                             <p class="submit">
  591.                                 <input class="button-primary" type="submit"
  592.                                        value="<?php _e( 'Insert', 'wp-bannerize' )?>"/>
  593.                             </p>
  594.                         </form>
  595.  
  596.                     </div>
  597.                 </div>
  598.             </div>
  599.         </div>
  600.     </div>
  601.     <?php
  602.  
  603.     }
  604.  
  605.     /**
  606.      * Draw Options Panel
  607.      */
  608.     function show_banners() {
  609.         global $wpdb;
  610.  
  611.         if ( isset( $_POST['command_action'] ) && $_POST['command_action'] != "" ) {
  612.             switch ( $_POST['command_action'] ) {
  613.                 case "trash":
  614.                     $any_error = $this->setBannerToTrash();
  615.                     break;
  616.                 case "untrash":
  617.                     $any_error = $this->unsetBannerToTrash();
  618.                     break;
  619.                 case "delete":
  620.                     $any_error = $this->deleteBanner();
  621.                     break;
  622.                 case "update":
  623.                     $any_error = $this->updateBanner();
  624.                     break;
  625.             }
  626.         }
  627.         ?>
  628.  
  629.     <div class="wrap">
  630.  
  631.         <?php $this->saidmadeHeader(); ?>
  632.  
  633.     <p style="text-align:right;"><a class="button-primary"
  634.                                     href="?page=wp-bannerize-addnew"><?php _e( 'Add New', 'wp-bannerize' ) ?></a></p>
  635.         <?php
  636.         // Group Actions
  637.         $action = -1;
  638.         if ( isset( $_POST['groupAction'] ) && $_POST['groupAction'] != '-1' ) {
  639.             $action = $_POST['groupAction'];
  640.         } elseif ( isset( $_POST['groupAction2'] ) && $_POST['groupAction2'] != '-1' ) {
  641.             $action = $_POST['groupAction2'];
  642.         } elseif ( isset( $_GET['groupAction'] ) && $_GET['groupAction'] != '-1' ) {
  643.             $action = $_GET['groupAction'];
  644.         } elseif ( isset( $_GET['groupAction2'] ) && $_GET['groupAction2'] != '-1' ) {
  645.             $action = $_GET['groupAction2'];
  646.         }
  647.         switch ( $action ) {
  648.             case "trash-selected":
  649.                 if ( isset( $_POST['image_record'] ) ) {
  650.                     $id        = implode( ",", $_POST['image_record'] );
  651.                     $any_error = $this->setBannerToTrash( $id );
  652.                 }
  653.                 break;
  654.             case "delete-selected":
  655.                 if ( isset( $_POST['image_record'] ) ) {
  656.                     if ( is_array( $_POST['image_record'] ) ) {
  657.                         foreach ( $_POST['image_record'] as $id ) {
  658.                             $any_error = $this->deleteBanner( $id );
  659.                         }
  660.                     }
  661.                 }
  662.                 break;
  663.             case "restore-selected":
  664.                 if ( isset( $_POST['image_record'] ) ) {
  665.                     $id        = implode( ",", $_POST['image_record'] );
  666.                     $any_error = $this->unsetBannerToTrash( $id );
  667.                 }
  668.                 break;
  669.         }
  670.  
  671.         $any_error = '';
  672.         $pagenum   = isset( $_GET['pagenum'] ) ? ( ( $_GET['pagenum'] == '' ? 1 : $_GET['pagenum'] ) ) : '1';
  673.         $limit     = isset( $_REQUEST['combo_pagination_filter'] ) ? $_REQUEST['combo_pagination_filter'] : '10';
  674.         $where     = "1";
  675.         $count     = array ();
  676.  
  677.         // Build where condictions
  678.         if ( isset( $_GET['trash'] ) && $_GET['trash'] != "" ) {
  679.             $where = sprintf( "%s AND trash = '%s'", $where, $_GET['trash'] );
  680.         } else {
  681.             $where = "1 AND trash = '0'";
  682.         }
  683.  
  684.         if ( isset( $_REQUEST['combo_group_filter'] ) && $_REQUEST['combo_group_filter'] != "" ) {
  685.             $where = sprintf( "%s AND `group` = '%s'", $where, $_REQUEST['combo_group_filter'] );
  686.         }
  687.  
  688.         // All Total records
  689.         $sql          = sprintf( "SELECT COUNT(*) AS all_record FROM %s", $this->table_bannerize );
  690.         $result       = $wpdb->get_row( $sql );
  691.         $count['All'] = intval( $result->all_record );
  692.  
  693.         // Trash
  694.         $sql            = sprintf( "SELECT COUNT(*) AS trashed FROM %s WHERE trash = '1'", $this->table_bannerize );
  695.         $result         = $wpdb->get_row( $sql );
  696.         $count['Trash'] = intval( $result->trashed );
  697.  
  698.         $count['Publish'] = $count['All'] - $count['Trash'];
  699.  
  700.         // Count record with where conditions
  701.         $sql              = sprintf( "SELECT COUNT(*) AS showing FROM %s WHERE %s", $this->table_bannerize, $where );
  702.         $result           = $wpdb->get_row( $sql );
  703.         $count['showing'] = $result->showing;
  704.  
  705.         $num_pages = ceil( $count['showing'] / $limit );
  706.  
  707.         // GET query fields
  708.         $query_search = array ( 'trash'                   => isset( $_GET['trash'] ) ? $_GET['trash'] : 0,
  709.                                 'combo_group_filter'      => isset( $_REQUEST['combo_group_filter'] ) ? $_REQUEST['combo_group_filter'] : '',
  710.                                 'combo_pagination_filter' => $limit );
  711.  
  712.         $arraytolink = array_merge( array ( 'edit'    => null,
  713.                                             'pagenum' => '%#%' ), $query_search );
  714.  
  715.         $page_links = paginate_links( array ( 'base'    => add_query_arg( $arraytolink ),
  716.                                               'format'  => 'page=wp-bannerize-mainshow',
  717.                                               'total'   => $num_pages,
  718.                                               'current' => $pagenum ) );
  719.         ?>
  720.  
  721.         <?php if ( $any_error != '' ) : ?>
  722.     <div id="message" class="<?php echo $this->error ? 'error' : 'updated' ?> fade"><p><?php echo $any_error ?></p>
  723.     </div>
  724.         <?php endif; ?>
  725.  
  726.     <form name="form_show"
  727.           class="wpBannerizeForm"
  728.           method="post"
  729.           action=""
  730.           id="posts-filter"
  731.           enctype="multipart/form-data">
  732.         <input type="hidden" name="id"/>
  733.         <input type="hidden" name="action" value=""/>
  734.         <input type="hidden" name="command_action" value=""/>
  735.         <input type="hidden" name="page" value="wp-bannerize-mainshow"/>
  736.         <input type="hidden" name="status" value="<?php echo ( isset( $_GET['trash'] ) ? $_GET['trash'] : "" ) ?>"/>
  737.         <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo kWPBannerizeMaxFileSize ?>"/>
  738.  
  739.         <ul class="subsubsub">
  740.             <?php
  741.             $links        = array ();
  742.             $status_links = array ( "Publish" => "0",
  743.                                     "Trash"   => "1" );
  744.             foreach ( $status_links as $status => $value ) {
  745.                 if ( $count[$status] > 0 ) {
  746.                     $current = "";
  747.                     $addurl  = "";
  748.                     if ( ( isset( $_GET['trash'] ) && $_GET['trash'] == $value ) ||
  749.                         ( !isset( $_GET['trash'] ) && $value == "0" )
  750.                     ) {
  751.                         $current = 'class="current"';
  752.                     }
  753.                     if ( $value != "" ) {
  754.                         $addurl = "&trash=" . $value;
  755.                     }
  756.                     $links[] = sprintf( "<li><a %s href=\"?page=wp-bannerize-mainshow%s\">%s <span class=\"count\">(%s)</span></a>", $current, $addurl, __( $status, 'wp-bannerize' ), $count[$status] );
  757.                 }
  758.             }
  759.             $output = implode( '| </li>', $links ) . '</li>';
  760.             echo $output;
  761.             ?>
  762.         </ul>
  763.  
  764.         <?php if ( $count["showing"] > 0 ) : ?>
  765.  
  766.         <div class="tablenav">
  767.  
  768.             <div class="alignleft actions">
  769.                 <select name="groupAction">
  770.                     <option value="-1"><?php _e( 'Actions', 'wp-bannerize' ) ?></option>
  771.                     <?php if ( !isset( $_GET['trash'] ) || $_GET['trash'] == "0" ) : ?>
  772.                     <option value="trash-selected"><?php _e( 'Trash', 'wp-bannerize' ) ?></option>
  773.                     <?php elseif ( isset( $_GET['trash'] ) && $_GET['trash'] == "1" ) : ?>
  774.                     <option value="restore-selected"><?php _e( 'Restore', 'wp-bannerize' ) ?></option>
  775.                     <option value="delete-selected"><?php _e( 'Delete', 'wp-bannerize' ) ?></option>
  776.                     <?php endif; ?>
  777.                 </select>
  778.                 <input type="submit" class="button-secondary action" id="doaction" name="doaction"
  779.                        value="<?php _e( 'Apply', 'wp-bannerize' ) ?>"/>
  780.  
  781.                 <?php echo $this->combo_group_filter(); $this->combo_pagination_filter() ?> <input type="submit"
  782.                                                                                                    class="button-secondary action"
  783.                                                                                                    value="<?php _e( 'Filter', 'wp-bannerize' ) ?>"/>
  784.  
  785.             </div>
  786.  
  787.             <div class="tablenav-pages">
  788.                     <span class="displaying-num"><?php printf( __( "Showing %s-%s of %s", 'wp-bannerize' ), $pagenum, (
  789.                     $count['showing'] > $limit ? $limit : $count['showing'] ), $count['showing'] ) ?></span>
  790.                 <?php echo $page_links ?>
  791.             </div>
  792.             <div class="clear"></div>
  793.         </div>
  794.  
  795.         <div class="clear"></div>
  796.  
  797.         <table rel="<?php echo $pagenum . "," . $limit ?>" id="wp_bannerize_list" cellspacing="0" class="widefat">
  798.             <thead>
  799.             <tr>
  800.                 <th class="manage-column column-cb check-column" id="cb" scope="col"><input type="checkbox"/></th>
  801.                 <th class="manage-column" scope="col"></th>
  802.                 <th class="manage-column column-image" scope="col"><?php _e( 'Image', 'wp-bannerize' ) ?></th>
  803.                 <th class="manage-column column-key" scope="col"><?php _e( 'Group', 'wp-bannerize' ) ?></th>
  804.                 <th class="manage-column column-description"
  805.                     scope="col"><?php _e( 'Description', 'wp-bannerize' ) ?></th>
  806.                 <th class="manage-column column-clickcount num" scope="col">
  807.                     <div class="clickcounter" title="<?php _e( 'Click Counter', 'wp-bannerize' ) ?>"></div>
  808.                 </th>
  809.                 <th class="manage-column column-clickcount num" scope="col">
  810.                     <div class="impressions" title="<?php _e( 'Impressions', 'wp-bannerize' ) ?>"></div>
  811.                 </th>
  812.                 <th class="manage-column column-clickcount num" scope="col">
  813.                     CTR
  814.                 </th>
  815.             </tr>
  816.             </thead>
  817.  
  818.             <tfoot>
  819.             <tr>
  820.                 <th class="manage-column column-cb check-column" id="cb" scope="col"><input type="checkbox"/></th>
  821.                 <th class="manage-column" scope="col"></th>
  822.                 <th class="manage-column column-image" scope="col"><?php _e( 'Image', 'wp-bannerize' ) ?></th>
  823.                 <th class="manage-column column-key" scope="col"><?php _e( 'Group', 'wp-bannerize' ) ?></th>
  824.                 <th class="manage-column column-description"
  825.                     scope="col"><?php _e( 'Description', 'wp-bannerize' ) ?></th>
  826.                 <th class="manage-column column-clickcount num" scope="col">
  827.                     <div class="clickcounter"></div>
  828.                 </th>
  829.                 <th class="manage-column column-clickcount num" scope="col">
  830.                     <div class="impressions"></div>
  831.                 </th>
  832.                 <th class="manage-column column-clickcount num" scope="col">
  833.                     CTR
  834.                 </th>
  835.             </tr>
  836.             </tfoot>
  837.  
  838.             <tbody>
  839.                 <?php
  840.                 $alt = 0;
  841.                 $sql = sprintf( "SELECT *, IF( (`start_date` < NOW() OR `start_date` = '0000-00-00 00:00:00') AND (`end_date` > NOW() OR `end_date` = '0000-00-00 00:00:00') AND (`maximpressions` = 0 OR `impressions` < `maximpressions`), 'enabled', 'disabled' ) AS status FROM %s WHERE %s ORDER BY `sorter`, `group` ASC LIMIT %s,%s", $this->table_bannerize, $where, (
  842.                     ( $pagenum - 1 ) * $limit ), $limit );
  843.                 $row = $wpdb->get_results( $sql );
  844.                 foreach ( $row as $item ) : ?>
  845.                 <tr <?php echo ( $alt++ % 2 ) ? 'class="alternate"' : "" ?> id="item_<?php echo $item->id ?>">
  846.                     <?php $this->rowWithItem( $item ) ?>
  847.                 </tr>
  848.                     <?php endforeach;
  849.                 ?>
  850.             </tbody>
  851.         </table>
  852.  
  853.         <div class="tablenav">
  854.  
  855.             <div class="alignleft actions">
  856.                 <select name="groupAction2">
  857.                     <option value="-1"><?php _e( 'Actions', 'wp-bannerize' ) ?></option>
  858.                     <?php if ( !isset( $_GET['trash'] ) || $_GET['trash'] == "0" ) : ?>
  859.                     <option value="trash-selected"><?php _e( 'Trash', 'wp-bannerize' ) ?></option>
  860.                     <?php elseif ( isset( $_GET['trash'] ) && $_GET['trash'] == "1" ) : ?>
  861.                     <option value="restore-selected"><?php _e( 'Restore', 'wp-bannerize' ) ?></option>
  862.                     <option value="delete-selected"><?php _e( 'Delete', 'wp-bannerize' ) ?></option>
  863.                     <?php endif; ?>
  864.                 </select>
  865.                 <input type="submit" class="button-secondary action" id="doaction2" name="doaction"
  866.                        value="<?php _e( 'Apply', 'wp-bannerize' ) ?>"/>
  867.             </div>
  868.  
  869.             <div class="tablenav-pages">
  870.                     <span class="displaying-num"><?php printf( __( "Showing %s-%s of %s", 'wp-bannerize' ), $pagenum, (
  871.                     $count['showing'] > $limit ? $limit : $count['showing'] ), $count['showing'] ) ?></span>
  872.                 <?php echo $page_links ?>
  873.             </div>
  874.             <div class="clear"></div>
  875.         </div>
  876.  
  877.         <?php else : ?>
  878.         <div class="clear"></div>
  879.         <p><?php _e( 'No Banner found!', 'wp-bannerize' ) ?></p>
  880.         <?php endif; ?>
  881.     </form>
  882.  
  883.     <form name="wp_bannerize_action" id="wp_bannerize_action" class="wpBannerizeForm" method="post" action="">
  884.         <input type="hidden" name="command_action" value=""/>
  885.         <input type="hidden" name="id"/>
  886.     </form>
  887.  
  888.     </div>
  889.     <?php
  890.     }
  891.  
  892.     /**
  893.      * Retrive a single item row for a specify ID
  894.      *
  895.      * @param $id
  896.      *   Banner ID
  897.      *
  898.      * @return html
  899.      *   HTML for a row
  900.      */
  901.     function rowItemWithID( $id ) {
  902.         global $wpdb;
  903.         $sql = sprintf( "SELECT *, IF( (`start_date` < NOW() OR `start_date` = '0000-00-00 00:00:00') AND (`end_date` > NOW() OR `end_date` = '0000-00-00 00:00:00') AND (`maximpressions` = 0 OR `impressions` < `maximpressions`), 'enabled', 'disabled' ) AS status FROM `%s` WHERE id = %s", $this->table_bannerize, $id );
  904.         $row = $wpdb->get_row( $sql );
  905.         $this->rowWithItem( $row );
  906.     }
  907.  
  908.  
  909.     /**
  910.      * Compute HTML for a row
  911.      *
  912.      * @param $item
  913.      *   Result set of a query
  914.      *
  915.      * @return void
  916.      */
  917.     function rowWithItem( $item ) {
  918.         ?>
  919.     <th class="check-column" scope="row"><input type="checkbox" value="<?php echo $item->id ?>"
  920.                                                 name="image_record[]"/></th>
  921.     <th scope="row">
  922.         <div class="arrow"></div>
  923.     </th>
  924.     <td class="wp-bannerize-thumbnail">
  925.         <?php
  926.         if ( $item->banner_type == kWPBannerizeBannerTypeFromLocal || $item->banner_type == kWPBannerizeBannerTypeByURL
  927.         ) : ?>
  928.             <?php if ( $item->mime == "application/x-shockwave-flash" ) : ?>
  929.                 <a class="fancybox wp_bannerize_flash" rel="wp-bannerize-gallery-thumbnail"
  930.                    title="<?php echo $item->description ?>" href="<?php echo $item->filename ?>"></a>
  931.                 <?php else : ?>
  932.                 <a class="fancybox" rel="wp-bannerize-gallery-thumbnail"
  933.                    href="<?php echo $item->filename ?>" title="<?php echo $item->description ?>"><img
  934.                     alt="<?php echo $item->description ?>" border="0"
  935.                     src="<?php echo $item->filename ?>"/></a>
  936.                 <?php endif; ?>
  937.             <?php else : ?>
  938.             <img alt="<?php echo $item->description ?>" border="0"
  939.                  src="<?php echo $this->url . '/css/images/shellscript.png' ?>"/>
  940.             <?php endif; ?>
  941.     </td>
  942.     <td nowrap="nowrap"><?php echo $item->group ?></td>
  943.     <td width="100%">
  944.         <div class="wpBannerizeSwitch <?php echo ( $item->enabled == '1' ) ? 'on' : '' ?>"
  945.              id="wpBannerizeSwitch_<?php echo $item->id ?>">
  946.             <div></div>
  947.         </div>
  948.         <?php if ( $item->start_date != '0000-00-00 00:00:00' || $item->end_date != '0000-00-00 00:00:00' ) : ?>
  949.         <p>
  950.             <span class="start_date <?php echo $item->status ?>"><?php echo ( $this->mysql_date( $item->start_date ) ==
  951.                 '0000-00-00 00:00:00' ) ? __( 'Always', 'wp-bannerize' ) : $this->mysql_date( $item->start_date ) ?></span>
  952.             <span class="end_date <?php echo $item->status ?>"><?php echo ( $this->mysql_date( $item->end_date ) ==
  953.                 '0000-00-00 00:00:00' ) ? __( 'Always', 'wp-bannerize' ) : $this->mysql_date( $item->end_date ) ?></span>
  954.         </p>
  955.         <?php endif; ?>
  956.  
  957.         <?php if ( $item->url != '' ) : ?>
  958.         <p class="clear">
  959.             <span class="wpBannerizeURL"><a title="<?php echo $item->url ?>"
  960.                                             target="_blank"
  961.                                             href="<?php echo $item->url ?>"><?php echo $this->stringCut( $item->url ) ?></a></span>
  962.         </p>
  963.         <?php endif; ?>
  964.  
  965.         <?php if ( $item->description != '' ) : ?>
  966.         <div class="wpBannerizeDescription"><?php echo $item->description ?></div>
  967.         <?php endif; ?>
  968.  
  969.         <div class="row-actions">
  970.             <?php if ( $item->trash == "0" ) : ?>
  971.             <span class="edit">
  972.             <a href="#" class="edit_<?php echo $item->id ?>"
  973.                title="<?php _e( 'Edit', 'wp-bannerize' ) ?>"
  974.                onclick="WPBannerizeJavascript.displayEdit(<?php echo $item->id ?>)"><?php _e( 'Edit', 'wp-bannerize' ) ?></a> | </span>
  975.             <span class="trash"><a class="<?php echo $item->id ?>"
  976.                                    title="<?php _e( 'Trash', 'wp-bannerize' ) ?>"
  977.                                    href="#"><?php _e( 'Trash', 'wp-bannerize' ) ?></a> | </span>
  978.             <span class="view"><a class="fancybox submitview" rel="wp-bannerize-gallery"
  979.                                   title="<?php echo $item->description ?>"" href="<?php echo $item->filename ?>
  980.                                                                           "><?php _e( 'View', 'wp-bannerize' ) ?></a></span>
  981.             <?php else : ?>
  982.             <span class="delete"><a class="<?php echo $item->id ?>"
  983.                                     title="<?php _e( 'Delete', 'wp-bannerize' ) ?>"
  984.                                     href="#"><?php _e( 'Delete', 'wp-bannerize' ) ?></a> | </span>
  985.             <span class="restore"><a class="<?php echo $item->id ?>"
  986.                                      title="<?php _e( 'Restore', 'wp-bannerize' ) ?>"
  987.                                      href="#"><?php _e( 'Restore', 'wp-bannerize' ) ?></a></span>
  988.             <?php endif; ?>
  989.         </div>
  990.         <div id="edit_<?php echo $item->id ?>"></div>
  991.     </td>
  992.     <td class="comments column-comments">
  993.         <div class="post-com-count-wrapper">
  994.             <div class="post-com-count">
  995.                 <span><?php echo $item->clickcount ?></span>
  996.             </div>
  997.         </div>
  998.     </td>
  999.     <td class="comments column-comments">
  1000.         <div class="post-com-count-wrapper">
  1001.             <div class="post-com-count">
  1002.                 <span><?php echo $item->impressions ?></span>
  1003.             </div>
  1004.         </div>
  1005.     </td>
  1006.     <td class="comments column-comments">
  1007.         <div class="post-com-count-wrapper">
  1008.             <div class="post-com-count">
  1009.                 <span>
  1010.                     <?php
  1011.                     if ( $item->impressions > 0 ) {
  1012.                         echo intval( ( $item->clickcount / $item->impressions ) * 100 ) . '%';
  1013.                     } else {
  1014.                         echo '0%';
  1015.                     }
  1016.                     ?></span>
  1017.             </div>
  1018.         </div>
  1019.     </td>
  1020.     <?php
  1021.     }
  1022.  
  1023.     /**
  1024.      * Show hide form for inline edit in banner list
  1025.      *
  1026.      * @param  $id
  1027.      *   ID row
  1028.      *
  1029.      * @return void
  1030.      */
  1031.     function inlineEdit( $id ) {
  1032.  
  1033.         global $wpdb;
  1034.  
  1035.         $sql = sprintf( 'SELECT * FROM `%s` WHERE `id` = %s', $this->table_bannerize, $id );
  1036.         $row = $wpdb->get_row( $sql );
  1037.  
  1038.         ob_start(); ?>
  1039.     <div class="inline-edit" style="display:none">
  1040.  
  1041.         <label for="filename"><?php _e( 'Banner', 'wp-bannerize' )?>:</label>
  1042.         <fieldset>
  1043.             <legend>
  1044.                 <input type="radio" name="wpBannerizeBannerType" value="1" <?php checked( $row->banner_type, 1 ) ?>
  1045.                        checked="checked"/> <?php _e( 'From local', 'wp-bannerize' )?>
  1046.                 <input type="radio" name="wpBannerizeBannerType" <?php checked( $row->banner_type, 2 ) ?>
  1047.                        value="2"/> <?php _e( 'By URL o Media Library', 'wp-bannerize' )?>
  1048.                 <input type="radio" name="wpBannerizeBannerType"
  1049.                        value="3" <?php checked( $row->banner_type, 3 ) ?> /> <?php _e( 'Free HTML', 'wp-bannerize' )?>
  1050.             </legend>
  1051.             <div <?php hidden( $row->banner_type, 1 ) ?> class="wpBannerizeBannerType1"><input type="file"
  1052.                                                                                                name="filename"
  1053.                                                                                                id="filename"/></div>
  1054.             <div <?php hidden( $row->banner_type, 2 ) ?> class="wpBannerizeBannerType2">
  1055.                 <input value="<?php echo $row->filename ?>"
  1056.                        size="32" type="text" name="filenameFromURL"
  1057.                        id="filenameFromURL" value="http://"/>
  1058.                 <input id="wpBannerizeButtonFromMediaLibrary" type="button" class="button-secondary"
  1059.                        value="<?php _e( 'Media Library Image', 'wp-bannerize' )?>"/></div>
  1060.             <div <?php hidden( $row->banner_type, 3 ) ?> class="wpBannerizeBannerType3"><textarea name="freeHTML"
  1061.                                                                                                   id="freeHTML"
  1062.                                                                                                   rows="2"
  1063.                                                                                                   cols="50"><?php echo stripslashes( $row->free_html ) ?></textarea>
  1064.             </div>
  1065.         </fieldset>
  1066.         <br style="clear:both"/>
  1067.  
  1068.         <p>
  1069.             <label for="start_date"><?php _e( 'Start Date', 'wp-bannerize' ) ?>:</label> <input class="date"
  1070.                                                                                                 type="text"
  1071.                                                                                                 name="start_date"
  1072.                                                                                                 id="start_date"
  1073.                                                                                                 size="18"
  1074.                                                                                                 value="<?php echo ( (
  1075.                                                                                                     $row->start_date ==
  1076.                                                                                                         "" ||
  1077.                                                                                                         $row->start_date ==
  1078.                                                                                                             "0000-00-00 00:00:00" ) ? '' : $this->mysql_date( $row->start_date ) ) ?>"/>
  1079.             <span class="eraser" onclick="jQuery('input#start_date').val('')"></span>
  1080.             <label for="end_date"
  1081.                    style="float:none;display:inline;margin-left:16px"><?php _e( 'End Date', 'wp-bannerize' ) ?>:</label>
  1082.             <input class="date" type="text" name="end_date" id="end_date" size="18"
  1083.                    value="<?php echo ( ( $row->end_date == "" ||
  1084.                        $row->end_date == "0000-00-00 00:00:00" ) ? '' : $this->mysql_date( $row->end_date ) ) ?>"/>
  1085.             <span class="eraser" onclick="jQuery('input#end_date').val('')"></span>
  1086.             <strong style="color:#888"><?php _e( 'Server Date/time', 'wp-bannerize' ); ?>
  1087.                 : <?php echo date_i18n( $this->getPHPDateFormat() ) ?></strong>
  1088.         </p>
  1089.  
  1090.         <p>
  1091.             <label><?php _e( 'Group', 'wp-bannerize' ) ?>:</label> <input size="8" type="text" id="group" name="group"
  1092.                                                                           value="<?php echo $row->group ?>"/> <?php echo $this->get_combo_group() ?>
  1093.         </p>
  1094.  
  1095.         <p>
  1096.             <label><?php _e( 'Description', 'wp-bannerize' ) ?>:</label> <input size="32" type="text" name="description"
  1097.                                                                                 value="<?php echo $row->description ?>"/>
  1098.             <input <?php echo ( ( $row->use_description == '1' ) ? 'checked="checked"' : '' ) ?> type="checkbox"
  1099.                                                                                                  name="use_description"
  1100.                                                                                                  value="1"/> <?php _e( 'Use this description in output', 'wp-bannerize' ) ?>
  1101.         </p>
  1102.  
  1103.         <p>
  1104.             <label><?php _e( 'URL', 'wp-bannerize' ) ?>:</label> <input type="text" name="url" size="32"
  1105.                                                                         value="<?php echo $row->url ?>"/>
  1106.             <label style="float:none;display:inline;margin-left:16px"><?php _e( 'Target', 'wp-bannerize' ) ?>
  1107.                 :</label> <?php echo $this->get_target_combo( $row->target ) ?>
  1108.         </p>
  1109.  
  1110.         <p>
  1111.             <label for="clickcount" style="float:none;display:inline"><?php _e( 'Click Counter', 'wp-bannerize' ) ?>
  1112.                 :</label>
  1113.             <input size="4" class="number" type="text" name="clickcount" id="clickcount"
  1114.                    value="<?php echo $row->clickcount ?>"/>
  1115.             <label for="impressions"
  1116.                    style="float:none;display:inline;margin-left:16px"><?php _e( 'Impressions', 'wp-bannerize' ) ?>
  1117.                 :</label>
  1118.             <input class="number"
  1119.                    type="text"
  1120.                    name="impressions"
  1121.                    id="iImpressions"
  1122.                    value="<?php echo $row->impressions ?>"
  1123.                    size="8"/>
  1124.             <label for="maxImpressions"
  1125.                    style="float:none;display:inline;margin-left:16px"><?php _e( 'Max Impressions', 'wp-bannerize' ) ?>
  1126.                 :</label>
  1127.             <input class="number"
  1128.                    type="text"
  1129.                    name="maxImpressions"
  1130.                    id="maxImpressions"
  1131.                    value="<?php echo $row->maximpressions ?>"
  1132.                    size="4"/>
  1133.         </p>
  1134.  
  1135.         <p>
  1136.             <label for="nofollow"
  1137.                    style="float:none;display:inline"><?php _e( 'Add nofollow attribute', 'wp-bannerize' ) ?></label>
  1138.             <input <?php echo ( ( $row->nofollow == '1' ) ? 'checked="checked"' : '' ) ?> type="checkbox"
  1139.                                                                                           name="nofollow"
  1140.                                                                                           id="nofollow"
  1141.                                                                                           value="1"/>
  1142.             <label for="width" style="float:none;display:inline;margin-left:16px"><?php _e( 'Width', 'wp-bannerize' ) ?>
  1143.                 :</label>
  1144.             <input size="4" type="text" name="width" id="width" value="<?php echo $row->width ?>"/>
  1145.             <label for="height"
  1146.                    style="float:none;display:inline;margin-left:16px"><?php _e( 'Height', 'wp-bannerize' ) ?>:</label>
  1147.             <input size="4" type="text" name="height" id="height" value="<?php echo $row->height ?>"/>
  1148.         </p>
  1149.  
  1150.         <p class="submit inline-edit-save">
  1151.             <a onclick="WPBannerizeJavascript.hideInlineEdit(<?php echo $row->id ?>)"
  1152.                class="button-secondary cancel alignleft" title="<?php _e( 'Cancel', 'wp-bannerize' ) ?>" href="#"
  1153.                accesskey="c"><?php _e( 'Cancel', 'wp-bannerize' ) ?></a>
  1154.             <a onclick="WPBannerizeJavascript.update(<?php echo $row->id ?>)" class="button-primary save alignright"
  1155.                title="<?php _e( 'Update', 'wp-bannerize' ) ?>" href="#"
  1156.                accesskey="s"><?php _e( 'Update', 'wp-bannerize' ) ?></a><span style="display:none"
  1157.                                                                               class="wpBannerizeAjaxLoader"></span>
  1158.         </p>
  1159.     </div><?php
  1160.         $result = ob_get_contents();
  1161.         ob_end_clean();
  1162.         return $result;
  1163.     }
  1164.  
  1165.     /**
  1166.      * Build the select/option filter group
  1167.      *
  1168.      * @return void
  1169.      */
  1170.     function combo_group_filter() {
  1171.         global $wpdb;
  1172.         $o    = '<select name="combo_group_filter">' . '<option value="">' . __( 'All groups', 'wp-bannerize' ) .
  1173.             '</option>';
  1174.         $q    = "SELECT `group` FROM `" . $this->table_bannerize . "` GROUP BY `group` ORDER BY `group` ";
  1175.         $rows = $wpdb->get_results( $q );
  1176.         foreach ( $rows as $row ) {
  1177.             if ( isset( $_REQUEST['combo_group_filter'] ) && $_REQUEST['combo_group_filter'] == $row->group ) {
  1178.                 $sel = 'selected="selected"';
  1179.             } else {
  1180.                 $sel = "";
  1181.             }
  1182.             $o .= '<option ' . $sel . 'value="' . $row->group . '">' . $row->group . '</option>';
  1183.         }
  1184.         $o .= '</select>';
  1185.         echo $o;
  1186.     }
  1187.  
  1188.     function groupMenu( $name = "wpBannerizeGroupMenu", $selected = "", $firstItem = "" ) {
  1189.         global $wpdb;
  1190.         ob_start();
  1191.         ?>
  1192.     <select name="<?php echo $name ?>" id="<?php echo $name ?>">
  1193.         <?php
  1194.         $sql  = sprintf( "SELECT `group` FROM `%s` GROUP BY `group` ORDER BY `group` ", $this->table_bannerize );
  1195.         $rows = $wpdb->get_results( $sql );
  1196.         if ( $firstItem != '' ) : ?>
  1197.             <option value=""><?php echo $firstItem ?></option>
  1198.             <?php endif;
  1199.         foreach ( $rows as $row ) : ?>
  1200.             <option <?php selected( $row->group, $selected ) ?> value="<?php echo $row->group ?>"><?php echo $row->group ?></option>
  1201.             <?php endforeach;
  1202.         ?>
  1203.     </select>
  1204.     <?php
  1205.         $result = ob_get_contents();
  1206.         ob_end_clean();
  1207.         return $result;
  1208.     }
  1209.  
  1210.     function combo_pagination_filter() {
  1211.         $pagination = isset( $_REQUEST['combo_pagination_filter'] ) ? $_REQUEST['combo_pagination_filter'] : '';
  1212.         ?>
  1213.     <select name="combo_pagination_filter">
  1214.         <option <?php selected( $pagination, 10 ) ?> value="10">10</option>
  1215.         <option <?php selected( $pagination, 20 ) ?> value="20">20</option>
  1216.         <option <?php selected( $pagination, 30 ) ?> value="30">30</option>
  1217.     </select>
  1218.     <?php
  1219.  
  1220.     }
  1221.  
  1222.     /**
  1223.      * Show Adobe Flash Window mode combo for settings
  1224.      *
  1225.      * @param null $param
  1226.      */
  1227.     function comboWindowModeFlash( $param = null ) {
  1228.         if ( is_null( $param ) ) {
  1229.             $param = $_REQUEST['comboWindowModeFlash'];
  1230.         }
  1231.         ?>
  1232.     <select id="comboWindowModeFlash" name="comboWindowModeFlash">
  1233.         <option <?php echo ( $param == "Window" ) ? 'selected="selected"' : "" ?> value="Window">Window</option>
  1234.         <option <?php echo ( $param == "Opaque" ) ? 'selected="selected"' : "" ?> value="Opaque">Opaque</option>
  1235.         <option <?php echo ( $param == "Transparent" ) ? 'selected="selected"' : "" ?> value="Transparent">Transparent
  1236.         </option>
  1237.         <option <?php echo ( $param == "Direct" ) ? 'selected="selected"' : "" ?> value="Direct">Direct</option>
  1238.         <option <?php echo ( $param == "GPU" ) ? 'selected="selected"' : "" ?> value="GPU">GPU</option>
  1239.     </select>
  1240.     <?php
  1241.  
  1242.     }
  1243.  
  1244.     /**
  1245.      * Build combo group
  1246.      *
  1247.      * @return string
  1248.      */
  1249.     function get_combo_group() {
  1250.         global $wpdb;
  1251.         $o    = '<select id="group_filter">' . '<option value=""></option>';
  1252.         $q    = "SELECT `group` FROM `" . $this->table_bannerize . "` GROUP BY `group` ORDER BY `group` ";
  1253.         $rows = $wpdb->get_results( $q );
  1254.         foreach ( $rows as $row ) {
  1255.             $o .= '<option value="' . $row->group . '">' . $row->group . '</option>';
  1256.         }
  1257.         $o .= '</select>';
  1258.         return $o;
  1259.     }
  1260.  
  1261.     /**
  1262.      * Get Select Checked Categories
  1263.      *
  1264.      * @param null $cats
  1265.      *
  1266.      * @return string
  1267.      */
  1268.     function get_categories_checkboxes( $cats = null ) {
  1269.         if ( !is_null( $cats ) ) {
  1270.             $cat_array = explode( ",", $cats );
  1271.         }
  1272.         $res = get_categories();
  1273.         $o   = "";
  1274.         foreach ( $res as $cat ) {
  1275.             $checked = "";
  1276.             if ( !is_null( $cats ) ) {
  1277.                 if ( in_array( $cat->cat_ID, $cat_array ) ) {
  1278.                     $checked = 'checked="checked"';
  1279.                 }
  1280.             }
  1281.             $o .= '<label><input ' . $checked . ' type="checkbox" name="categories[]" id="categories[]" value="' .
  1282.                 $cat->cat_ID . '" /> ' . $cat->cat_name . '</label> ';
  1283.         }
  1284.         return $o;
  1285.     }
  1286.  
  1287.     /**
  1288.      * Build combo menu for target
  1289.      *
  1290.      * @param string $sel
  1291.      *
  1292.      * @return string
  1293.      */
  1294.     function get_target_combo( $sel = "_blank" ) {
  1295.         $o = '<select name="target" id="target">' . '<option></option>' . '<option ' .
  1296.             ( ( $sel == '_blank' ) ? 'selected="selected"' : '' ) . '>_blank</option>' . '<option ' .
  1297.             ( ( $sel == '_parent' ) ? 'selected="selected"' : '' ) . '>_parent</option>' . '<option ' .
  1298.             ( ( $sel == '_self' ) ? 'selected="selected"' : '' ) . '>_self</option>' . '<option ' .
  1299.             ( ( $sel == '_top' ) ? 'selected="selected"' : '' ) . '>_top</option>' . '</select>';
  1300.         return $o;
  1301.     }
  1302.  
  1303.     /**
  1304.      * Insert banner into the database table
  1305.      *
  1306.      * @return bool|string|void
  1307.      */
  1308.     function insertBanner() {
  1309.         $wpBannerizeBannerType = intval( $_POST['wpBannerizeBannerType'] );
  1310.         switch ( $wpBannerizeBannerType ) {
  1311.             case 1:
  1312.                 return $this->addBannerFromLocal();
  1313.                 break;
  1314.             case 2:
  1315.                 return $this->addBannerFromURL();
  1316.                 break;
  1317.             case 3:
  1318.                 return $this->addBannerWithFreeHTML();
  1319.                 break;
  1320.             default:
  1321.                 break;
  1322.         }
  1323.         return false;
  1324.     }
  1325.  
  1326.     function addBannerWithFreeHTML() {
  1327.         global $wpdb;
  1328.  
  1329.         $group           = $_POST['group'];
  1330.         $description     = $_POST['description'];
  1331.         $use_description = isset( $_POST['use_description'] ) ? $_POST['use_description'] : 0;
  1332.         $url             = $_POST['url'];
  1333.         $target          = $_POST['target'];
  1334.         $nofollow        = isset( $_POST['nofollow'] ) ? $_POST['nofollow'] : 0;
  1335.  
  1336.         $start_date = $this->mysql_date( $_POST['start_date'] );
  1337.         $end_date   = $this->mysql_date( $_POST['end_date'] );
  1338.  
  1339.         $wpdb->show_errors();
  1340.         $rows = $wpdb->insert( $this->table_bannerize, array ( 'banner_type'     => $_POST['wpBannerizeBannerType'],
  1341.                                                                'group'           => $group,
  1342.                                                                'description'     => $description,
  1343.                                                                'use_description' => $use_description,
  1344.                                                                'url'             => $url,
  1345.                                                                'target'          => $target,
  1346.                                                                'nofollow'        => $nofollow,
  1347.                                                                'start_date'      => $start_date,
  1348.                                                                'end_date'        => $end_date,
  1349.                                                                'maximpressions'  => $_POST['maxImpressions'],
  1350.                                                                'free_html'       => $_POST['freeHTML']
  1351.  
  1352.         ) );
  1353.         if ( $rows !== false ) {
  1354.             $this->error = false;
  1355.             return __( 'Banner added succesfully!', 'wp-bannerize' );
  1356.         } else {
  1357.             $this->error = true;
  1358.             return __( 'Error on insert Free HTML banner type', 'wp-bannerize' );
  1359.         }
  1360.     }
  1361.  
  1362.     function addBannerFromURL() {
  1363.         global $wpdb;
  1364.  
  1365.         $dimensions = array ( $_POST['width'], $_POST['height'] );
  1366.         $mime       = "";
  1367.  
  1368.         if ( function_exists( 'getimagesize' ) ) {
  1369.             $dimensions = @getimagesize( $_POST['filenameFromURL'] );
  1370.             if ( !isset( $dimensions ) ) {
  1371.                 $dimensions = array ( '0', '0' );
  1372.             } else {
  1373.                 $mime = $dimensions['mime'];
  1374.             }
  1375.         }
  1376.  
  1377.         $group           = $_POST['group'];
  1378.         $description     = $_POST['description'];
  1379.         $use_description = isset( $_POST['use_description'] ) ? $_POST['use_description'] : 0;
  1380.         $url             = $_POST['url'];
  1381.         $target          = $_POST['target'];
  1382.         $nofollow        = isset( $_POST['nofollow'] ) ? $_POST['nofollow'] : 0;
  1383.  
  1384.         $start_date = $this->mysql_date( $_POST['start_date'] );
  1385.         $end_date   = $this->mysql_date( $_POST['end_date'] );
  1386.  
  1387.         $wpdb->show_errors();
  1388.         $rows = $wpdb->insert( $this->table_bannerize, array(
  1389.                                                             'banner_type'     => $_POST['wpBannerizeBannerType'],
  1390.                                                             'group'           => $group,
  1391.                                                             'description'     => $description,
  1392.                                                             'use_description' => $use_description,
  1393.                                                             'url'             => $url,
  1394.                                                             'filename'        => $_POST['filenameFromURL'],
  1395.                                                             'target'          => $target,
  1396.                                                             'nofollow'        => $nofollow,
  1397.                                                             'start_date'      => $start_date,
  1398.                                                             'end_date'        => $end_date,
  1399.                                                             'maximpressions'  => $_POST['maxImpressions'],
  1400.                                                             'mime'            => $mime,
  1401.                                                             'width'           => $dimensions[0],
  1402.                                                             'height'          => $dimensions[1],
  1403.                                                             'free_html'       => ''
  1404.  
  1405.         ) );
  1406.         if ( $rows !== false ) {
  1407.             $this->error = false;
  1408.             return __( 'Banner added succesfully!', 'wp-bannerize' );
  1409.         } else {
  1410.             $this->error = false;
  1411.             return __( 'Error on insert URL banner type', 'wp-bannerize' );
  1412.         }
  1413.     }
  1414.  
  1415.     function addBannerFromLocal() {
  1416.         global $wpdb;
  1417.  
  1418.         // check post error
  1419.         if ( is_uploaded_file( $_FILES['filename']['tmp_name'] ) ) {
  1420.             //$size = floor($_FILES['filename']['size'] / (1024 * 1024));
  1421.             $mime = $_FILES['filename']['type'];
  1422.             $name = $_FILES['filename']['name'];
  1423.             $temp = $_FILES['filename']['tmp_name'];
  1424.  
  1425.             $group           = $_POST['group'];
  1426.             $description     = $_POST['description'];
  1427.             $use_description = isset( $_POST['use_description'] ) ? $_POST['use_description'] : 0;
  1428.             $url             = $_POST['url'];
  1429.             $target          = $_POST['target'];
  1430.             $nofollow        = isset( $_POST['nofollow'] ) ? $_POST['nofollow'] : 0;
  1431.             $dimensions      = array ( '0', '0' );
  1432.  
  1433.             $start_date = $this->mysql_date( $_POST['start_date'] );
  1434.             $end_date   = $this->mysql_date( $_POST['end_date'] );
  1435.  
  1436.             $uploads = wp_upload_bits( strtolower( $name ), null, '' );
  1437.  
  1438.             if ( move_uploaded_file( $temp, $uploads['file'] ) ) {
  1439.                 if ( function_exists( 'getimagesize' ) ) {
  1440.                     $dimensions = @getimagesize( $uploads['file'] );
  1441.                     if ( !isset( $dimensions ) ) {
  1442.                         $dimensions = array ( '0', '0' );
  1443.                     }
  1444.                 }
  1445.  
  1446.                 $wpdb->show_errors();
  1447.                 $rows = $wpdb->insert( $this->table_bannerize, array(
  1448.                                                                     'banner_type'     => $_POST['wpBannerizeBannerType'],
  1449.                                                                     'group'           => $group,
  1450.                                                                     'description'     => $description,
  1451.                                                                     'use_description' => $use_description,
  1452.                                                                     'url'             => $url,
  1453.                                                                     'filename'        => $uploads['url'],
  1454.                                                                     'target'          => $target,
  1455.                                                                     'nofollow'        => $nofollow,
  1456.                                                                     'mime'            => $mime,
  1457.                                                                     'realpath'        => $uploads['file'],
  1458.                                                                     'width'           => $dimensions[0],
  1459.                                                                     'height'          => $dimensions[1],
  1460.                                                                     'start_date'      => $start_date,
  1461.                                                                     'end_date'        => $end_date,
  1462.                                                                     'maximpressions'  => $_POST['maxImpressions'],
  1463.                                                                     'free_html'       => ''
  1464.  
  1465.                                                                ) );
  1466.                 if ( $rows !== false ) {
  1467.                     $this->error = false;
  1468.                     return __( 'Banner added succesfully!', 'wp-bannerize' );
  1469.                 } else {
  1470.                     $this->error = true;
  1471.                     return __( 'Error on insert local banner type', 'wp-bannerize' );
  1472.                 }
  1473.             } else {
  1474.                 $this->error = true;
  1475.                 $error       = sprintf( __( 'Error while copying [%s] [%s bytes] - [%s]', 'wp-bannerize' ), $_FILES['filename']['name'], $_FILES['filename']['size'], $_FILES['filename']['error'] );
  1476.                 return $error;
  1477.             }
  1478.         } else {
  1479.             $this->error = true;
  1480.             $error       = sprintf( __( 'No file to upload! - [%s]', 'wp-bannerize' ), $_FILES['filename']['error'] );
  1481.             return $error;
  1482.         }
  1483.     }
  1484.  
  1485.     /**
  1486.      * Set one or more banner in trash mode: trash = "1"
  1487.      *
  1488.      * @param  $id      string|array
  1489.      *
  1490.      * @return string|void
  1491.      */
  1492.     function setBannerToTrash( $id = null ) {
  1493.         global $wpdb;
  1494.         $id  = ( is_null( $id ) ) ? $_POST['id'] : $id;
  1495.         $sql = sprintf( "UPDATE `%s` SET trash = '1' WHERE id IN(%s)", $this->table_bannerize, $id );
  1496.         $wpdb->query( $sql );
  1497.         $this->error = false;
  1498.         return __( 'Banner sent to trash succesfully!', 'wp-bannerize' );
  1499.     }
  1500.  
  1501.     /**
  1502.      * Set one or more banner in publish mode: trash = "0"
  1503.      *
  1504.      * @param  $id      string|array
  1505.      *
  1506.      * @return void
  1507.      */
  1508.     function unsetBannerToTrash( $id = null ) {
  1509.         global $wpdb;
  1510.         $id  = ( is_null( $id ) ) ? $_POST['id'] : $id;
  1511.         $sql = sprintf( "UPDATE `%s` SET trash = '0' WHERE id IN(%s)", $this->table_bannerize, $id );
  1512.         $wpdb->query( $sql );
  1513.         $this->error = false;
  1514.         return __( 'Banner restore from trash succesfully!', 'wp-bannerize' );
  1515.     }
  1516.  
  1517.     /**
  1518.      * Delete (permanently) a banner from Database and filesystem. Because a banner is delete from disk, this method
  1519.      * is call from loop for delete more banner
  1520.      *
  1521.      * @param null $id
  1522.      *
  1523.      * @return string|void
  1524.      */
  1525.     function deleteBanner( $id = null ) {
  1526.         global $wpdb;
  1527.  
  1528.         $id = ( is_null( $id ) ) ? $_POST['id'] : $id;
  1529.  
  1530.         // Delete from disk only local banner.
  1531.         $wpBannerizeBannerType = $wpdb->get_var(
  1532.             "SELECT `banner_type` FROM `" . $this->table_bannerize . "` WHERE `id` = " . $id );
  1533.  
  1534.         if ( $wpBannerizeBannerType == '1' ) {
  1535.             $filename = $wpdb->get_var( "SELECT `realpath` FROM `" . $this->table_bannerize . "` WHERE `id` = " . $id );
  1536.             @unlink( $filename );
  1537.         }
  1538.  
  1539.         $q = "DELETE FROM `" . $this->table_bannerize . "` WHERE `id` = " . $id;
  1540.         $wpdb->query( $q );
  1541.  
  1542.         $this->error = false;
  1543.         return __( 'Banner delete succesfully!', 'wp-bannerize' );
  1544.     }
  1545.  
  1546.     /**
  1547.      * Update a banner data and image
  1548.      *
  1549.      * @return bool|string|void Information message
  1550.      */
  1551.     function updateBanner() {
  1552.         $wpBannerizeBannerType = intval( $_POST['wpBannerizeBannerType'] );
  1553.         switch ( $wpBannerizeBannerType ) {
  1554.             case 1:
  1555.                 return $this->updateBannerFromLocal();
  1556.                 break;
  1557.             case 2:
  1558.                 return $this->updateBannerFromURL();
  1559.                 break;
  1560.             case 3:
  1561.                 return $this->updateBannerWithFreeHTML();
  1562.                 break;
  1563.             default:
  1564.                 break;
  1565.         }
  1566.         return false;
  1567.     }
  1568.  
  1569.     function updateBannerWithFreeHTML() {
  1570.         global $wpdb;
  1571.  
  1572.         $dimensions = array ( $_POST['width'], $_POST['height'] );
  1573.         $mime       = "";
  1574.  
  1575.         $values = array ( 'banner_type'     => $_POST['wpBannerizeBannerType'],
  1576.                           'group'           => $_POST['group'],
  1577.                           'start_date'      => $this->mysql_date( $_POST['start_date'] ),
  1578.                           'end_date'        => $this->mysql_date( $_POST['end_date'] ),
  1579.                           'maximpressions'  => $_POST['maxImpressions'],
  1580.                           'impressions'     => $_POST['impressions'],
  1581.                           'description'     => $_POST['description'],
  1582.                           'url'             => $_POST['url'],
  1583.                           'target'          => $_POST['target'],
  1584.                           'use_description' => isset( $_POST['use_description'] ) ? $_POST['use_description'] : 0,
  1585.                           'nofollow'        => $_POST['nofollow'],
  1586.                           'clickcount'      => $_POST['clickcount'],
  1587.                           'width'           => $dimensions[0],
  1588.                           'height'          => $dimensions[1],
  1589.                           'filename'        => $_POST['filenameFromURL'],
  1590.                           'mime'            => $mime,
  1591.                           'free_html'       => $_POST['freeHTML'] );
  1592.         $where  = array ( 'id' => $_POST['id'] );
  1593.         $wpdb->show_errors();
  1594.         $result = $wpdb->update( $this->table_bannerize, $values, $where );
  1595.         if ( $result !== false ) {
  1596.             $this->error = false;
  1597.             return __( 'Banner update succesfully!', 'wp-bannerize' );
  1598.         } else {
  1599.             $this->error = true;
  1600.             return __( 'Error while update free HTML Banner!', 'wp-bannerize' );
  1601.         }
  1602.     }
  1603.  
  1604.     function updateBannerFromLocal() {
  1605.         global $wpdb;
  1606.  
  1607.         // Retrive image info
  1608.         $sql = sprintf( "SELECT * FROM `%s` WHERE id = %s", $this->table_bannerize, $_POST['id'] );
  1609.         $row = $wpdb->get_row( $sql );
  1610.  
  1611.         $filename   = $row->filename;
  1612.         $mime       = $row->mime;
  1613.         $realpath   = $row->realpath;
  1614.         $dimensions = array ( $_POST['width'], $_POST['height'] );
  1615.  
  1616.         if ( is_uploaded_file( $_FILES['filename']['tmp_name'] ) ) {
  1617.             //$size = floor($_FILES['filename']['size'] / (1024 * 1024));
  1618.             $mime = $_FILES['filename']['type'];
  1619.             $name = $_FILES['filename']['name'];
  1620.             $temp = $_FILES['filename']['tmp_name'];
  1621.  
  1622.             $dimensions = array ( '0', '0' );
  1623.  
  1624.             $uploads = wp_upload_bits( strtolower( $name ), null, '' );
  1625.  
  1626.             if ( move_uploaded_file( $temp, $uploads['file'] ) ) {
  1627.                 if ( function_exists( 'getimagesize' ) ) {
  1628.                     $dimensions = @getimagesize( $uploads['file'] );
  1629.                     if ( !isset( $dimensions ) ) {
  1630.                         $dimensions = array ( '0', '0' );
  1631.                     }
  1632.                 }
  1633.                 // Delete old image
  1634.                 @unlink( $realpath );
  1635.  
  1636.                 $filename = $uploads['url'];
  1637.                 $realpath = $uploads['file'];
  1638.             }
  1639.         }
  1640.         $values = array ( 'banner_type'     => $_POST['wpBannerizeBannerType'],
  1641.                           'group'           => $_POST['group'],
  1642.                           'start_date'      => $this->mysql_date( $_POST['start_date'] ),
  1643.                           'end_date'        => $this->mysql_date( $_POST['end_date'] ),
  1644.                           'maximpressions'  => $_POST['maxImpressions'],
  1645.                           'impressions'     => $_POST['impressions'],
  1646.                           'description'     => $_POST['description'],
  1647.                           'url'             => $_POST['url'],
  1648.                           'target'          => $_POST['target'],
  1649.                           'use_description' => isset( $_POST['use_description'] ) ? $_POST['use_description'] : 0,
  1650.                           'nofollow'        => $_POST['nofollow'],
  1651.                           'clickcount'      => $_POST['clickcount'],
  1652.                           'width'           => $dimensions[0],
  1653.                           'height'          => $dimensions[1],
  1654.                           'filename'        => $filename,
  1655.                           'realpath'        => $realpath,
  1656.                           'mime'            => $mime );
  1657.         $where  = array ( 'id' => $_POST['id'] );
  1658.         $wpdb->show_errors();
  1659.         $result = $wpdb->update( $this->table_bannerize, $values, $where );
  1660.         if ( $result !== false ) {
  1661.             $this->error = false;
  1662.             return __( 'Banner update succesfully!', 'wp-bannerize' );
  1663.         } else {
  1664.             $this->error = true;
  1665.             return __( 'Error while update local Banner!', 'wp-bannerize' );
  1666.         }
  1667.     }
  1668.  
  1669.     function updateBannerFromURL() {
  1670.         global $wpdb;
  1671.  
  1672.         $dimensions = array ( $_POST['width'], $_POST['height'] );
  1673.         $mime       = "";
  1674.  
  1675.         if ( function_exists( 'getimagesize' ) ) {
  1676.             $dimensions = @getimagesize( $_POST['filenameFromURL'] );
  1677.             if ( !isset( $dimensions ) ) {
  1678.                 $dimensions = array ( '0', '0' );
  1679.             } else {
  1680.                 $mime = $dimensions['mime'];
  1681.             }
  1682.             if ( !(
  1683.                 $_POST['width'] == '0' || $_POST['height'] == '0' || $_POST['width'] == '' || $_POST['height'] == '' )
  1684.             ) {
  1685.                 $dimensions[0] = $_POST['width'];
  1686.                 $dimensions[1] = $_POST['height'];
  1687.             }
  1688.         }
  1689.  
  1690.         $values = array ( 'banner_type'     => $_POST['wpBannerizeBannerType'],
  1691.                           'group'           => $_POST['group'],
  1692.                           'start_date'      => $this->mysql_date( $_POST['start_date'] ),
  1693.                           'end_date'        => $this->mysql_date( $_POST['end_date'] ),
  1694.                           'maximpressions'  => $_POST['maxImpressions'],
  1695.                           'impressions'     => $_POST['impressions'],
  1696.                           'description'     => $_POST['description'],
  1697.                           'url'             => $_POST['url'],
  1698.                           'target'          => $_POST['target'],
  1699.                           'use_description' => isset( $_POST['use_description'] ) ? $_POST['use_description'] : 0,
  1700.                           'nofollow'        => $_POST['nofollow'],
  1701.                           'clickcount'      => $_POST['clickcount'],
  1702.                           'width'           => $dimensions[0],
  1703.                           'height'          => $dimensions[1],
  1704.                           'filename'        => $_POST['filenameFromURL'],
  1705.                           'mime'            => $mime );
  1706.         $where  = array ( 'id' => $_POST['id'] );
  1707.         $wpdb->show_errors();
  1708.         $result = $wpdb->update( $this->table_bannerize, $values, $where );
  1709.         if ( $result !== false ) {
  1710.             $this->error = false;
  1711.             return __( 'Banner update succesfully!', 'wp-bannerize' );
  1712.         } else {
  1713.             $this->error = true;
  1714.             return __( 'Error while update local Banner!', 'wp-bannerize' );
  1715.         }
  1716.     }
  1717.  
  1718.     /**
  1719.      * Attach settings in Wordpress Plugins list
  1720.      *
  1721.      * @param $pluginfile
  1722.      */
  1723.     function register_plugin_settings( $pluginfile ) {
  1724.         $this->plugin_file = $pluginfile;
  1725.         add_action(
  1726.             'plugin_action_links_' . basename( dirname( $pluginfile ) ) . '/' . basename( $pluginfile ), array ( &$this,
  1727.             'plugin_settings' ), 10, 4 );
  1728.         add_filter( 'plugin_row_meta', array ( &$this, 'add_plugin_links' ), 10, 2 );
  1729.     }
  1730.  
  1731.     /**
  1732.      * Add link to Plugin list page
  1733.      *
  1734.      * @param  $links
  1735.      *
  1736.      * @return string
  1737.      */
  1738.     function plugin_settings( $links ) {
  1739.         $settings_link = '<a href="admin.php?page=wp-bannerize-mainshow">' . __( 'Settings' ) . '</a>';
  1740.         array_unshift( $links, $settings_link );
  1741.         return $links;
  1742.     }
  1743.  
  1744.     /**
  1745.      * Add links on installed plugin list
  1746.      *
  1747.      * @param $links
  1748.      * @param $file
  1749.      *
  1750.      * @return array
  1751.      */
  1752.     function add_plugin_links( $links, $file ) {
  1753.         if ( $file == plugin_basename( $this->plugin_file ) ) {
  1754.             $links[] = '<strong style="color:#fa0">' . __( 'For more info visit', 'wp-bannerize' ) .
  1755.                 ' <a href="http://blog.wpxtre.me">wpXtreme Blog</a></strong>';
  1756.         }
  1757.         return $links;
  1758.     }
  1759.  
  1760.     /**
  1761.      * Call on Plugin Activation
  1762.      *
  1763.      * @since 2.5.0
  1764.      *
  1765.      * @return void
  1766.      */
  1767.     function pluginDidActive() {
  1768.         // Table doesn't exists: create it
  1769.         $this->createTable();
  1770.  
  1771.         // Rename tabel if needed
  1772.         $this->renameTable();
  1773.        
  1774.         //mod jrc 170613: add admin role to default admin if not alreay exist
  1775.             global $wp_roles;
  1776.  
  1777.       if (!isset($wp_roles)) {
  1778.         $wp_roles = new WP_Roles();
  1779.       }
  1780.  
  1781.       $wp_roles->use_db = true;
  1782.       $administrator = $wp_roles->get_role('administrator');
  1783.  
  1784.       if (!$administrator->has_cap(kWPBannerizeUserCapabilitiy)) {
  1785.         $wp_roles->add_cap('administrator', kWPBannerizeUserCapabilitiy);
  1786.  
  1787.         //ensure we can unset role cap it safely later
  1788.                 add_option( 'scl_wp_bannerize_has_set_role_cap', kWPBannerizeUserCapabilitiy );
  1789.                
  1790.       }
  1791.         //end mod jrc 070613:
  1792.        
  1793.     }
  1794.  
  1795.     function pluginDidDeactive() {
  1796.                    
  1797.         //mod jrc 170613: add admin role to default admin if not alreay exist
  1798.             global $wp_roles;
  1799.  
  1800.       if (!isset($wp_roles)) {
  1801.         $wp_roles = new WP_Roles();
  1802.       }
  1803.  
  1804.       $wp_roles->use_db = true;
  1805.       $administrator = $wp_roles->get_role('administrator');
  1806.  
  1807.       //attempt to unset role cap safely only iff we have previously set it (ie. it is not a standard wp role cap, or something reqd/added by another plugin)
  1808.       //warning: if you choose a role cap related to another plugin (loaded later) then this is not 100% reliable - it is possble to accidentaly remove admin capability to other chosen plugin
  1809.       //hence: try and use the custom role cap 'manage_wp_bannerize_banners' - set in main.h.php via -  define( 'kWPBannerizeUserCapabilitiy', 'manage_wp_bannerize_banners' );
  1810.       $has_set_role = get_option('scl_wp_bannerize_has_set_role_cap');
  1811.  
  1812.       if (!empty($has_set_role) && $has_set_role == kWPBannerizeUserCapabilitiy && $administrator->has_cap(kWPBannerizeUserCapabilitiy)) {
  1813.         $wp_roles->remove_cap('administrator', kWPBannerizeUserCapabilitiy);
  1814.                 delete_option('scl_wp_bannerize_has_set_role_cap');
  1815.       }
  1816.         //end mod jrc 070613:                  
  1817.                    
  1818.     }
  1819.  
  1820.  
  1821.     /**
  1822.      * Check if previous database table name exists.
  1823.      *
  1824.      * @return void
  1825.      */
  1826.     function previousDatabaseTableNameExists() {
  1827.         global $wpdb;
  1828.         $sql = sprintf( "SHOW TABLES LIKE '%s'", $this->prev_table_bannerize );
  1829.         if ( $wpdb->get_var( $sql ) != $this->prev_table_bannerize ) {
  1830.             // table does not exist!
  1831.             return false;
  1832.         }
  1833.         return true;
  1834.     }
  1835.  
  1836.     /**
  1837.      * Create the WP Bannerize table. This method use Wordpress dbDelta() function for check if table exists and update
  1838.      * table if needed.
  1839.      *
  1840.      * @since 2.1.0
  1841.      *
  1842.      * @return void
  1843.      */
  1844.     function createTable() {
  1845.         if(!function_exists('dbDelta')) {
  1846.             require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
  1847.         }
  1848.         ob_start();
  1849.         require_once( 'wpBannerizeTable.sql' );
  1850.         $sql = sprintf( ob_get_contents(), ( $this->previousDatabaseTableNameExists() ) ? $this->prev_table_bannerize : $this->table_bannerize );
  1851.         @dbDelta( $sql );
  1852.         ob_end_clean();
  1853.     }
  1854.  
  1855.     /**
  1856.      * Rename previous databsse table name if needed
  1857.      *
  1858.      * @return void
  1859.      */
  1860.     function renameTable() {
  1861.         global $wpdb;
  1862.         if ( $this->previousDatabaseTableNameExists() ) {
  1863.             $sql = sprintf( "RENAME TABLE `%s` TO `%s`", $this->prev_table_bannerize, $this->table_bannerize );
  1864.             $wpdb->query( $sql );
  1865.         }
  1866.     }
  1867.  
  1868.     /**
  1869.      * Truncate WP bannerize Database Table
  1870.      *
  1871.      * @return void
  1872.      */
  1873.     function truncateTable() {
  1874.         global $wpdb;
  1875.         $sql = sprintf( "TRUNCATE TABLE `%s`", $this->table_bannerize );
  1876.         $wpdb->query( $sql );
  1877.     }
  1878.  
  1879.     /**
  1880.      * Get WP Bannerize table information. Display number of row and table size.
  1881.      *
  1882.      * @param bool $echo
  1883.      *   True (default) display output, else return an Object with table status information
  1884.      *
  1885.      * @return Object with table status information if $echo param is true
  1886.      */
  1887.     function tableInformation( $echo = true ) {
  1888.         global $wpdb;
  1889.         $sql    = sprintf( "SHOW TABLE STATUS LIKE '%s'", $this->table_bannerize );
  1890.         $result = $wpdb->get_row( $sql );
  1891.         $data   = intval( $result->Data_length );
  1892.         $index  = intval( $result->Index_length );
  1893.         $size   = round( ( $data + $index ) / 1024, 3 );
  1894.         $gain   = round( floatval( $result->Data_free ) / 1024, 2 );
  1895.  
  1896.         if ( $echo ) :  ?>
  1897.         <p><strong><?php _e( 'Numer of rows', 'wp-bannerize' ) ?>:</strong> <?php echo $result->Rows ?></p>
  1898.         <p><strong><?php _e( 'Size', 'wp-bannerize' ) ?>:</strong> <?php echo $size ?> Kb
  1899.             <?php if ( $gain > 0 ) : ?>
  1900.                 <strong style="color:#c00">(<?php echo $gain ?> Kb)</strong>
  1901.                 <?php endif; ?>
  1902.         </p>
  1903.         <?php else :
  1904.             return $result;
  1905.         endif;
  1906.     }
  1907.  
  1908.     /**
  1909.      * Cut a string
  1910.      *
  1911.      * @param        $s String to cut
  1912.      * @param int    $l Length
  1913.      * @param string $f Append string
  1914.      *
  1915.      * @return string
  1916.      */
  1917.     function stringCut( $s, $l = 32, $f = "..." ) {
  1918.         if ( strlen( $s ) > $l ) {
  1919.             return substr( $s, 0, ( $l - strlen( $f ) ) / 2 ) . $f .
  1920.                 substr( $s, -( $l - strlen( $f ) ) / 2, ( $l - strlen( $f ) ) / 2 );
  1921.         } else {
  1922.             return $s;
  1923.         }
  1924.     }
  1925.  
  1926.     /**
  1927.      * Reformatting a date
  1928.      *
  1929.      * @param $s
  1930.      *   String date
  1931.      *
  1932.      * @return string
  1933.      *   Format date or "0000-00-00 00:00:00" for default
  1934.      */
  1935.     function mysql_date( $s ) {
  1936.         $result = "0000-00-00 00:00:00";
  1937.         $f      = __( 'mm/dd/yy', 'wp-bannerize' ) . ' H:i';
  1938.         if ( $s != "" && $s != $result ) {
  1939.             if ( substr( $s, 4, 1 ) == '-' ) {
  1940.                 if ( substr( $f, 0, 1 ) == "m" ) {
  1941.                     $fa = "m/d/Y H:i";
  1942.                 } else {
  1943.                     $fa = "d/m/Y H:i";
  1944.                 }
  1945.                 $date   = date_create( $s );
  1946.                 $result = date_format( $date, $fa );
  1947.             } else {
  1948.                 $a = explode( ' ', $s );
  1949.                 $d = explode( '/', $a[0] );
  1950.                 if ( substr( $f, 0, 1 ) == 'm' ) { // mm/dd/yyyy hh:mm
  1951.                     $result = sprintf( '%s-%s-%s %s:00', $d[2], $d[0], $d[1], $a[1] );
  1952.                 } else {
  1953.                     if ( substr( $f, 0, 1 ) == 'd' ) { // dd/mm/yyyy hh:mm
  1954.                         $result = sprintf( '%s-%s-%s %s:00', $d[2], $d[1], $d[0], $a[1] );
  1955.                     }
  1956.                 }
  1957.             }
  1958.         }
  1959.         return $result;
  1960.     }
  1961.  
  1962.     /**
  1963.      * Return the localized date format in accordance with the language selected
  1964.      *
  1965.      * @return string
  1966.      *   Localized date format in accordance with the language selected
  1967.      */
  1968.     function getPHPDateFormat() {
  1969.         $f = __( 'mm/dd/yy', 'wp-bannerize' );
  1970.         if ( substr( $f, 0, 1 ) == "m" ) {
  1971.             $result = "m/d/Y H:i";
  1972.         } else {
  1973.             $result = "d/m/Y H:i";
  1974.         }
  1975.         return $result;
  1976.     }
  1977.  
  1978.     /**
  1979.      * Return HTML code (ul/li) with all Wordpress categories
  1980.      *
  1981.      * @param array $selected
  1982.      *
  1983.      * @return string
  1984.      */
  1985.     function categoriesTree( $selected = null ) {
  1986.  
  1987.         $allCategories = get_categories();
  1988.         $o             = '<ul style="margin-left:12px">';
  1989.  
  1990.         foreach ( $allCategories as $cat ) {
  1991.             if ( $cat->parent == '0' ) {
  1992.                 $o .= $this->_iterateCategory( $cat, $selected );
  1993.             }
  1994.         }
  1995.         return $o . '</ul>';
  1996.     }
  1997.  
  1998.     /**
  1999.      * Internal "iterate" recursive function. For build a tree of category
  2000.      * Parent/Child
  2001.      *
  2002.      * @param object $cat_object
  2003.      * @param array  $selected
  2004.      *
  2005.      * @return string
  2006.      */
  2007.     function _iterateCategory( $cat_object, $selected = null ) {
  2008.         $checked = "";
  2009.         if ( !is_null( $selected ) && is_array( $selected ) ) {
  2010.             $checked = ( in_array( $cat_object->cat_ID, $selected ) ) ? 'checked="checked"' : "";
  2011.         }
  2012.         $ou = '<li><label><input ' . $checked .
  2013.             ' type="checkbox" name="wpBannerizeCategoriesTree[]" class="wpBannerizeCategoriesTree" value="' .
  2014.             $cat_object->cat_ID . '" /> ' . $cat_object->cat_name . '</label>';
  2015.  
  2016.         $childs = get_categories( 'parent=' . $cat_object->cat_ID );
  2017.         foreach ( $childs as $cat ) {
  2018.             $ou .= '<ul style="margin-left:12px">' . $this->_iterateCategory( $cat, $selected ) . '</ul>';
  2019.         }
  2020.         $ou .= '</li>';
  2021.         return $ou;
  2022.     }
  2023.  
  2024. } // end of class