Plugin Directory

Changeset 459789


Ignore:
Timestamp:
11/06/2011 12:37:46 AM (14 years ago)
Author:
dan.imbrogno
Message:
  • removed activation / deactivation functions in favour of update check > install table
Location:
make-my-blog-honest
Files:
45 added
8 edited

Legend:

Unmodified
Added
Removed
  • make-my-blog-honest/trunk/index.php

    r459772 r459789  
    44Plugin URI: http://bloggingsquared.com/plugins/Word11Demo
    55Description: This plugin will teach you how to build a plug-in of your own, with Honest Eds as the example client.
    6 Version: 1.3.2
     6Version: 1.3.3
    77Author: Dan Imbrogno
    88Author URI: http://bloggingsquared.com
  • make-my-blog-honest/trunk/readme.txt

    r459699 r459789  
    55Requires at least: 3.2
    66Tested up to: 3.2.1
    7 Stable tag: 1.3.2
     7Stable tag: 1.3.3
    88
    99Learn how to build better WordPress plugins with this handy tutorial. You'll learn some good plugin techniques in this easy to follow tutorial.
     
    3232
    3333== Changelog ==
     34
     35= 1.3.3 =
     36* Improved installation functions, stopped using activation / deactivation hooks
     37
    3438= 1.3.2 =
    3539* Moved load_plugin_textdomain into init action
  • make-my-blog-honest/trunk/step10.php

    r459699 r459789  
    610610            if ($installed_ver == false) {
    611611
    612                 MakeMyBlogHonest_Activate();
     612                $this->InstallTable();
    613613
    614614                return false;
     
    648648       
    649649        }
     650       
     651        function InstallTable() {
     652       
     653            global $wpdb;
     654   
     655            $deals_table = $wpdb->prefix . self::PREFIX.'_deals';
     656           
     657            if($wpdb->get_var( 'SHOW TABLES LIKE \'' . $deals_table . '\';' ) != $deals_table)
     658            {
     659               
     660                $create_deals_table_sql = 'CREATE TABLE ' . $deals_table. ' (
     661                            `id` mediumint(9) NOT NULL AUTO_INCREMENT,
     662                            `product_name` varchar(256) NOT NULL,
     663                            `sale_price` float NOT NULL,
     664                            `enabled` tinyint(1) NOT NULL,
     665                            `expires` DATETIME NOT NULL,
     666                            UNIQUE KEY id (id)
     667                    );';
     668               
     669                $wpdb->query( $create_deals_table_sql );
     670               
     671            }
     672           
     673            add_option( self::PREFIX.'_db_version', self::DB_VERSION );
     674           
     675        }
    650676                   
    651677    }
     
    653679    $myHonestBlog = new MakeMyBlogHonest();
    654680   
    655     register_activation_hook(
    656         'makemybloghonest/makemybloghonest.php' ,
    657         'MakeMyBlogHonest_Activate'
    658     );
    659    
    660     register_deactivation_hook(
    661         'makemybloghonest/makemybloghonest.php' ,
    662         'MakeMyBlogHonest_Deactivate'
    663     );
    664    
    665     function MakeMyBlogHonest_Activate() {
    666    
    667         global $wpdb;
    668 
    669         $deals_table = $wpdb->prefix . MakeMyBlogHonest::PREFIX.'_deals';
    670 
    671         if($wpdb->get_var( 'SHOW TABLES LIKE \'' . $deals_table . '\';' ) != $deals_table)
    672         {
    673    
    674             $create_deals_table_sql = 'CREATE TABLE ' . $deals_table. ' (
    675                         `id` mediumint(9) NOT NULL AUTO_INCREMENT,
    676                         `product_name` varchar(256) NOT NULL,
    677                         `sale_price` float NOT NULL,
    678                         `enabled` tinyint(1) NOT NULL,
    679                         `expires` DATETIME NOT NULL,
    680                         UNIQUE KEY id (id)
    681                 );';
    682 
    683             $wpdb->query($create_deals_table_sql);
    684            
    685         }
    686        
    687         add_option( MakeMyBlogHonest::PREFIX.'_db_version', MakeMyBlogHonest::DB_VERSION );
    688        
    689     }
    690    
    691     function MakeMyBlogHonest_Deactivate() {
    692        
    693     }
    694    
    695681}
    696682
  • make-my-blog-honest/trunk/step11.php

    r459699 r459789  
    77  If you mess anything up you can always copy step10.php into this file. */
    88
    9 if (!class_exists('MakeMyBlogHonest')) {
    10 
    11     class MakeMyBlogHonest {
    12    
    13         const PREFIX = 'mmbh';
    14 
     9if( !class_exists( 'MakeMyBlogHonest' ) ) {
     10   
     11    class MakeMyBlogHonest
     12    {
     13       
     14        const PREFIX = "mmbh";
     15       
    1516        private $plugin_url;
    16 
     17       
    1718        const DB_VERSION = 4;
    18 
    19         var $output = array();
    20 
     19       
     20        private $output = array();
     21       
    2122        function __construct() {
    22 
     23           
    2324            $this->plugin_url = plugins_url(basename(dirname(__FILE__)));
    24 
    25             add_filter('bloginfo', array($this, 'AddEdsCheesySloganToTitle'), 1, 2);
    26 
    27             add_filter('theme_mod_header_image', array($this, 'AddEdsHorribleBanner'));
    28 
    29             add_action('wp_enqueue_scripts', array($this, 'AddEdsAnnoyingPopup'));
    30 
    31             add_action('wp_print_styles', array($this, 'AddEdsDisgustingBackground'));
    32 
    33             add_action('admin_menu', array($this, 'InsertAdminMenuLink'));
    34 
     25           
     26            add_filter( 'bloginfo', array( $this, 'AddEdsCheesySloganToTitle' ), 1, 2 );
     27       
     28            add_filter( 'theme_mod_header_image', array( $this, 'AddEdsHorribleBanner' ) );
     29           
     30            add_action( 'wp_enqueue_scripts', array( $this, 'AddEdsAnnoyingPopup' ) );
     31           
     32            add_action( 'wp_print_styles', array( $this, 'AddEdsDisgustingBackground' ) );
     33           
     34            add_action( 'admin_menu', array( $this, 'InsertAdminMenuLink' ) );
     35           
    3536            add_action( 'init', array( $this, 'LoadPluginTextDomain' ), 1 );
    3637           
    3738            add_action( 'init', array( $this, 'AddDefaultOptions' ), 2 );
    38 
    39             add_action('admin_init', array($this, 'RegisterAdminSettings'));
    40 
    41             add_action('wp_enqueue_scripts', array($this, 'AddEdsAnnoyingPopup'));
    42 
    43             add_action('wp_print_styles', array($this, 'AddEdsDisgustingBackground'));
    44 
    45             add_action('init', array($this, 'CheckUpdate'), 1);
    46 
    47             add_action('init', array($this, 'RouteActions'), 2);
    48        
     39           
     40            add_action( 'admin_init', array( $this, 'RegisterAdminSettings' ) );
     41                       
     42            add_action( 'wp_enqueue_scripts', array( $this, 'AddEdsAnnoyingPopup' ) );
     43           
     44            add_action( 'wp_print_styles', array( $this, 'AddEdsDisgustingBackground' ) );
     45           
     46            add_action( 'init', array( $this, 'CheckUpdate' ) , 1 );
     47           
     48            add_action( 'init', array($this,'RouteActions'),2);
     49           
    4950        }
    5051       
     
    6061           
    6162            add_option( self::PREFIX . '_cheesy_slogan_text',
    62                 __( 'you can\'t beat his prices!', self::PREFIX ) );
     63                    __( 'you can\'t beat his prices!', self::PREFIX ) );
    6364   
    6465            add_option( self::PREFIX . '_horrible_banner_image', '1');
     
    6970               
    7071        }
    71 
    72         function AddEdsCheesySloganToTitle($current_value, $field) {
    73 
    74             if ($field == 'name')
    75                 return $current_value . ' – ' .
    76                     get_option(self::PREFIX . '_cheesy_slogan_text');
     72       
     73        function AddEdsCheesySloganToTitle( $current_value, $field )
     74        {
     75       
     76            if( $field == 'name' ) return $current_value . ' – ' .
     77                get_option(self::PREFIX . '_cheesy_slogan_text');
     78       
     79        }
     80       
     81        function AddEdsHorribleBanner()
     82        {
     83       
     84            if( get_option( self::PREFIX . '_horrible_banner_image' ) )
     85            {
     86           
     87                return $this->plugin_url . '/images/eds-horrible-banner' .
     88                    get_option( self::PREFIX . '_horrible_banner_image' ) . '.jpg';
     89           
     90            }
     91           
     92        }
     93       
     94        function AddEdsAnnoyingPopup()
     95        {
     96
     97            wp_register_script(
     98                'eds_annoying_popup',
     99                $this->plugin_url.'/js/eds_annoying_popup-3.0.js',
     100                array( 'jquery' ),
     101                '3.0',
     102                true
     103            );
     104           
     105            wp_localize_script(
     106                'eds_annoying_popup',
     107                'eds_annoying_popup_vars',
     108                array(
     109                    'prefix'=>self::PREFIX,
     110                    'text_error'=>__('Deals are currently unavailable.', self::PREFIX),
     111                    '_wpnonce'=>wp_create_nonce('GetRandomDeal')
     112                )
     113            );
     114                   
     115            $popup_enabled = get_option( self::PREFIX .
     116                '_annoying_popup_enabled' );
     117               
     118            if( true == $popup_enabled )
     119            {
     120           
     121                wp_enqueue_script( 'eds_annoying_popup' );
     122           
     123            }
     124           
     125        }
     126       
     127        function AddEdsDisgustingBackground()
     128        {
     129       
     130            wp_register_style(
     131                'eds_disgusting_background',
     132                $this->plugin_url.'/css/eds_disgusting_background.css',
     133                false,
     134                '1.0',
     135                'screen'
     136            );
     137           
     138            $background_enabled = get_option( self::PREFIX .
     139                '_disgusting_background_enabled' );
     140               
     141            if( true == $background_enabled )
     142            {
     143               
     144                wp_enqueue_style( 'eds_disgusting_background' );
     145               
     146            }
     147           
     148        }
     149       
     150        function InsertAdminMenuLink()
     151        {
     152           
     153            $page = add_submenu_page(
     154                'options-general.php',
     155                __( 'Honesty Settings Page', self::PREFIX ),
     156                __( 'Honesty Settings', self::PREFIX ),
     157                'manage_options' ,
     158                'honesty-settings-page' ,
     159                array( $this , 'HonestSettingsPageHtml' )
     160            );
     161           
     162           
     163           
     164            $page = add_submenu_page(
     165                'options-general.php',
     166                __( 'Deal Configuration Page', self::PREFIX ),
     167                __( 'Deal Configuration', self::PREFIX ),
     168                'manage_options' ,
     169                'deal-configuration-page' ,
     170                array( $this , 'DealConfigurationPageHtml' )
     171            );
     172           
     173            add_action(
     174                'admin_print_scripts-' . $page,
     175                array( $this, 'DealConfigurationPageJs' )
     176            );
     177           
     178            add_action(
     179                'admin_print_styles-' . $page,
     180                array( $this, 'DealConfigurationPageCss' )
     181            );
     182       
     183        }
     184       
     185        function HonestSettingsPageHtml()
     186        {
     187           
     188            include( 'html/honesty-settings-page-step5.php' );
     189           
     190        }
     191       
     192        function DealConfigurationPageHtml()
     193        {
     194       
     195            include( 'html/deal-configuration-page-step8.php' );
     196       
     197        }
     198       
     199        function RegisterAdminSettings()
     200        {
     201           
     202            register_setting(
     203                self::PREFIX . 'Settings',
     204                self::PREFIX . '_cheesy_slogan_text'
     205            );
     206           
     207            register_setting(
     208                self::PREFIX . 'Settings',
     209                self::PREFIX . '_horrible_banner_image'
     210            );
     211           
     212            register_setting(
     213                self::PREFIX . 'Settings',
     214                self::PREFIX . '_disgusting_background_enabled'
     215            );
     216           
     217            register_setting(
     218                self::PREFIX . 'Settings',
     219                self::PREFIX . '_annoying_popup_enabled'
     220            );
     221           
     222        }
     223       
     224        function DealConfigurationPageJs()
     225        {
     226       
     227            wp_register_script(
     228                self::PREFIX . '_deal_configuration_page',
     229                $this->plugin_url.'/js/deal_configuration_page.js',
     230                array( 'jquery' ),
     231                '1.0',
     232                true
     233            );
     234           
     235            wp_enqueue_script( self::PREFIX . '_deal_configuration_page' );
     236       
     237        }
     238       
     239        function DealConfigurationPageCss()
     240        {
     241       
     242            wp_register_style(
     243                self::PREFIX . '_deal_configuration_page',
     244                $this->plugin_url.'/css/deal_configuration_page.css',
     245                false,
     246                '1.0',
     247                'screen'
     248            );
     249           
     250            wp_enqueue_style( self::PREFIX . '_deal_configuration_page' );
     251       
     252        }
     253       
     254        function RouteActions()
     255        {
     256           
     257            if(
     258                !isset( $_POST[ self::PREFIX . 'Action'] ) ||
     259                !isset( $_POST[ '_wpnonce'] )
     260            )
     261            {
     262                return false;
     263            }
     264           
     265            $action = $_POST[ self::PREFIX . 'Action'];
     266            $nonce = $_POST[ '_wpnonce' ];
     267           
     268            if(!wp_verify_nonce($nonce, $action ))
     269                wp_die(__('You are not authorized to perform this action.',self::PREFIX));
     270           
     271            $result = $this->DoAction( $action );
     272
     273            if( $this->IsAjax() )
     274            {
     275               
     276                $this->AjaxResponse( $action );
     277           
     278            }
     279           
     280            return $result;
     281           
     282        }
     283       
     284        function DoAction( $action )
     285        {
     286           
     287            $result = false;
     288
     289            switch( $action )
     290            {
     291                case 'AddDeal':
     292               
     293                    if(!current_user_can('manage_options') || !is_admin())
     294                        wp_die(__('You are not authorized to perform this action.',self::PREFIX));
    77295                       
    78         }
    79 
    80         function AddEdsHorribleBanner() {
    81 
    82             if (get_option(self::PREFIX . '_horrible_banner_image')) {
    83 
    84                 return $this->plugin_url . '/images/eds-horrible-banner' .
    85                     get_option(self::PREFIX . '_horrible_banner_image') . '.jpg';
    86                    
    87             }
    88            
    89         }
    90 
    91         function AddEdsAnnoyingPopup() {
    92 
    93             wp_register_script(
    94                 'eds_annoying_popup',
    95                 $this->plugin_url . '/js/eds_annoying_popup-3.0.js',
    96                 array('jquery'),
    97                 '3.0',
    98                 true
    99             );
    100 
    101             wp_localize_script(
    102                 'eds_annoying_popup',
    103                 'eds_annoying_popup_vars',
    104                 array(
    105                     'prefix' => self::PREFIX,
    106                     '_wpnonce' => wp_create_nonce('GetRandomDeal')
     296                    $product_name = ( isset( $_POST['product_name'] ) ) ?
     297                        $_POST['product_name'] : '';
     298                   
     299                    $sale_price =   ( isset( $_POST['sale_price'] ) ) ? $_POST['sale_price'] : '';
     300                   
     301                    $enabled =   ( isset( $_POST['enabled'] ) ) ? true : false;
     302                   
     303                    $expires =   ( isset( $_POST['expires'] ) ) ? $_POST['expires'] : false;
     304                   
     305                    $result = $this->AddDeal( $product_name, $sale_price, $enabled, $expires );
     306                   
     307                break;
     308               
     309                case 'SetEnabledStatus':
     310               
     311                    if(!current_user_can('manage_options') || !is_admin())
     312                        wp_die(__('You are not authorized to perform this action.',self::PREFIX));
     313                   
     314                    $id = ( isset( $_POST['id'] ) ) ? $_POST['id'] : false;
     315                   
     316                    $enabled = ( isset( $_POST['enabled'] ) ) ? $_POST['enabled'] : false;
     317
     318                    $result = $this->SetEnabledStatus( $id, $enabled );
     319                   
     320                break;
     321               
     322                case 'DeleteDeal':
     323               
     324                    if(!current_user_can('manage_options') || !is_admin())
     325                        wp_die(__('You are not authorized to perform this action.',self::PREFIX));
     326                   
     327                    $id = ( isset( $_POST['id'] ) ) ? $_POST['id'] : false;
     328                   
     329                    $result = $this->DeleteDeal( $id );
     330                   
     331                break;
     332               
     333            }
     334           
     335            return $result;
     336           
     337        }
     338       
     339        function AjaxResponse( $action )
     340        {           
     341           
     342            $data = $this->output;
     343
     344            $data['action'] = $action;
     345           
     346            if( $action == 'AddDeal' || $action == 'SetEnabledStatus' || $action == 'DeleteDeal' )
     347            {
     348               
     349                ob_start();
     350               
     351                    include( 'html/elements/deal-list.php' );
     352                   
     353                    $data['html'] = ob_get_contents();
     354                   
     355                ob_end_clean();
     356               
     357            }
     358           
     359            elseif( $action == 'GetRandomDeal' )
     360            {
     361               
     362                ob_start();
     363                   
     364                    include( 'html/elements/deal-display.php' );
     365                    $data['html'] = ob_get_contents();
     366                   
     367                ob_end_clean();
     368           
     369            }
     370           
     371            die(json_encode($data));
     372           
     373        }
     374       
     375        private function IsAjax()
     376        {
     377       
     378            if (
     379                isset( $_SERVER['HTTP_X_REQUESTED_WITH'] ) &&
     380                $_SERVER['HTTP_X_REQUESTED_WITH'] == "XMLHttpRequest"
     381            )
     382            {
     383           
     384                return true;
     385               
     386            }
     387           
     388            return false;
     389           
     390        }
     391       
     392        private function GetDeals()
     393        {
     394           
     395            global $wpdb;
     396           
     397            $sql = 'SELECT * FROM '. $wpdb->prefix . self::PREFIX . '_deals';
     398           
     399            $result = $wpdb->get_results( $sql );
     400           
     401            return $result;
     402           
     403        }
     404       
     405        private function GetRandomDeal()
     406        {
     407           
     408            global $wpdb;
     409           
     410            $sql = 'SELECT * FROM '. $wpdb->prefix . self::PREFIX .
     411                '_deals WHERE `enabled` = true AND `expires` > NOW();';
     412           
     413            $results = $wpdb->get_results( $sql );
     414           
     415            if( is_array($results) && sizeof( $results ) > 0 )
     416            {
     417               
     418                return $results[array_rand( $results, 1 )];
     419           
     420            }
     421           
     422            return false;
     423           
     424        }
     425       
     426        private function AddDeal( $product_name, $sale_price, $enabled = false, $expires = false )
     427        {
     428       
     429            global $wpdb;
     430           
     431            if( empty( $product_name ) )
     432            {
     433           
     434                $this->output['error'] = __( 'The product name cannot be empty',
     435                    self::PREFIX );
     436               
     437                return false;
     438           
     439            }
     440           
     441            if( !is_numeric( $sale_price ) )
     442            {
     443           
     444                $this->output['error'] = __( 'The sale price must be a valid number.',
     445                    self::PREFIX );
     446               
     447                return false;
     448               
     449            }
     450           
     451            if( is_array( $expires ) ) {
     452           
     453                if(
     454                    !array_key_exists('Year', $expires) ||
     455                    !array_key_exists('Month', $expires) ||
     456                    !array_key_exists('Day', $expires)
    107457                )
    108             );
    109 
    110             $popup_enabled = get_option(self::PREFIX .
    111                     '_annoying_popup_enabled');
    112 
    113             if (true == $popup_enabled) {
    114 
    115                 wp_enqueue_script('eds_annoying_popup');
    116            
    117             }
    118            
    119         }
    120 
    121         function AddEdsDisgustingBackground() {
    122 
    123             wp_register_style(
    124                 'eds_disgusting_background',
    125                 $this->plugin_url . '/css/eds_disgusting_background.css',
    126                 false,
    127                 '1.0',
    128                 'screen'
    129             );
    130 
    131             $background_enabled = get_option(self::PREFIX .
    132                     '_disgusting_background_enabled');
    133 
    134             if (true == $background_enabled) {
    135 
    136                 wp_enqueue_style('eds_disgusting_background');
    137                
    138             }
    139            
    140         }
    141 
    142         function InsertAdminMenuLink() {
    143 
    144             $page = add_submenu_page(
    145                 'options-general.php',
    146                 __('Honesty Settings Page', self::PREFIX),
    147                 __('Honesty Settings', self::PREFIX),
    148                 'manage_options',
    149                 'honesty-settings-page',
    150                 array($this, 'HonestSettingsPageHtml')
    151             );
    152 
    153 
    154 
    155             $page = add_submenu_page(
    156                 'options-general.php',
    157                 __('Deal Configuration Page', self::PREFIX),
    158                 __('Deal Configuration', self::PREFIX),
    159                 'manage_options',
    160                 'deal-configuration-page',
    161                 array($this, 'DealConfigurationPageHtml')
    162             );
    163 
    164             add_action(
    165                     'admin_print_scripts-' . $page,
    166                     array($this, 'DealConfigurationPageJs')
    167             );
    168 
    169             add_action(
    170                 'admin_print_styles-' . $page,
    171                 array($this, 'DealConfigurationPageCss')
    172             );
    173            
    174         }
    175 
    176         function HonestSettingsPageHtml() {
    177 
    178             include( 'html/honesty-settings-page-step5.php' );
    179            
    180         }
    181 
    182         function DealConfigurationPageHtml() {
    183 
    184             include( 'html/deal-configuration-page-step8.php' );
    185            
    186         }
    187 
    188         function RegisterAdminSettings() {
    189 
    190             register_setting(
    191                     self::PREFIX . 'Settings', self::PREFIX . '_cheesy_slogan_text'
    192             );
    193 
    194             register_setting(
    195                     self::PREFIX . 'Settings', self::PREFIX . '_horrible_banner_image'
    196             );
    197 
    198             register_setting(
    199                     self::PREFIX . 'Settings', self::PREFIX . '_disgusting_background_enabled'
    200             );
    201 
    202             register_setting(
    203                     self::PREFIX . 'Settings', self::PREFIX . '_annoying_popup_enabled'
    204             );
    205            
    206         }
    207 
    208         function DealConfigurationPageJs() {
    209 
    210             wp_register_script(
    211                     self::PREFIX . '_deal_configuration_page', $this->plugin_url . '/js/deal_configuration_page.js', array('jquery'), '1.0', true
    212             );
    213 
    214             wp_enqueue_script(self::PREFIX . '_deal_configuration_page');
    215            
    216         }
    217 
    218         function DealConfigurationPageCss() {
    219 
    220             wp_register_style(
    221                     self::PREFIX . '_deal_configuration_page', $this->plugin_url . '/css/deal_configuration_page.css', false, '1.0', 'screen'
    222             );
    223 
    224             wp_enqueue_style(self::PREFIX . '_deal_configuration_page');
    225            
    226         }
    227 
    228         function RouteActions() {
    229 
    230             if (
    231                 !isset($_POST[self::PREFIX . 'Action']) ||
    232                 !isset($_POST['_wpnonce'])
    233             ) {
    234            
     458                {
     459                    $this->output['error'] =
     460                        __( 'A year month and day are required for the expiration date.',
     461                            self::PREFIX );
     462               
     463                    return false;
     464                }
     465               
     466                $expires = $expires['Year'] . '-' . $expires['Month'] . '-' . $expires['Day'];
     467           
     468            }
     469           
     470           
     471            $sql = $wpdb->prepare(
     472                'INSERT INTO ' . $wpdb->prefix . self::PREFIX .
     473                    '_deals(`product_name`, `sale_price`, `enabled`, `expires`)'.
     474                    ' VALUES(%s,%s, %s, %s);',
     475                $product_name,
     476                $sale_price,
     477                $enabled,
     478                $expires
     479            );
     480       
     481            $result = $wpdb->query( $sql );
     482       
     483            if( $result !== false )
     484            {
     485           
     486                $this->output['success'] = __('Deal has been added.', self::PREFIX );
     487               
     488            }
     489            else
     490            {
     491               
     492                $this->output['error'] = __(
     493                    'There was a problem adding the deal, please try again.',
     494                    self::PREFIX );
     495               
     496            }
     497           
     498            return $result;
     499       
     500        }
     501       
     502        private function SetEnabledStatus( $id, $enabled )
     503        {
     504           
     505            global $wpdb;
     506           
     507            if( $id == false ) {
     508           
     509                $this->output['error'] = __(
     510                    'There was a problem changing the deal status, please try again.',
     511                    self::PREFIX );
     512               
    235513                return false;
    236514               
    237515            }
    238 
    239             $action = $_POST[self::PREFIX . 'Action'];
    240            
    241             $nonce = $_POST['_wpnonce'];
    242 
    243             if (!wp_verify_nonce($nonce, $action))
    244                 wp_die(__('You are not authorized to perform this action.', self::PREFIX));
    245 
    246             $result = $this->DoAction($action);
    247 
    248             if ($this->IsAjax()) {
    249 
    250                 $this->AjaxResponse($action);
    251                
    252             }
    253 
     516           
     517            $sql = $wpdb->prepare(
     518                'UPDATE ' . $wpdb->prefix . self::PREFIX .
     519                    '_deals SET `enabled` = %d WHERE `id` = %d;',
     520                $enabled,
     521                $id
     522            );
     523           
     524            $result = $wpdb->query( $sql );
     525           
     526            if($result !== false)
     527            {
     528           
     529                $this->output['success'] =  __( 'The enabled status has been updated.',
     530                    self::PREFIX );
     531               
     532            }
     533            else
     534            {
     535           
     536                $this->output['error'] = __(
     537                    'There was a problem changing the deal status, please try again.',
     538                    self::PREFIX );
     539               
     540            }
     541           
    254542            return $result;
    255543           
    256544        }
    257 
    258         function DoAction($action) {
    259 
    260             $result = false;
    261 
    262             switch ($action) {
    263            
    264                 case 'AddDeal':
    265 
    266                     if (!current_user_can('manage_options') || !is_admin())
    267                         wp_die(__('You are not authorized to perform this action.', self::PREFIX));
    268 
    269                     $product_name = ( isset($_POST['product_name']) ) ?
    270                         $_POST['product_name'] : '';
    271 
    272                     $sale_price = ( isset($_POST['sale_price']) ) ? $_POST['sale_price'] : '';
    273 
    274                     $enabled = ( isset($_POST['enabled']) ) ? true : false;
    275 
    276                     $expires = ( isset($_POST['expires']) ) ? $_POST['expires'] : false;
    277 
    278                     $result = $this->AddDeal($product_name, $sale_price, $enabled, $expires);
    279 
    280                 break;
    281 
    282                 case 'SetEnabledStatus':
    283 
    284                     if (!current_user_can('manage_options') || !is_admin())
    285                         wp_die(__('You are not authorized to perform this action.', self::PREFIX));
    286 
    287                     $id = ( isset($_POST['id']) ) ? $_POST['id'] : false;
    288 
    289                     $enabled = ( isset($_POST['enabled']) ) ? $_POST['enabled'] : false;
    290 
    291                     $result = $this->SetEnabledStatus($id, $enabled);
    292 
    293                 break;
    294 
    295                 case 'DeleteDeal':
    296 
    297                     if (!current_user_can('manage_options') || !is_admin())
    298                         wp_die(__('You are not authorized to perform this action.', self::PREFIX));
    299 
    300                     $id = ( isset($_POST['id']) ) ? $_POST['id'] : false;
    301 
    302                     $result = $this->DeleteDeal($id);
    303 
    304                 break;
    305                
    306             }
    307 
     545       
     546        private function DeleteDeal( $id = false )
     547        {
     548           
     549            global $wpdb;
     550           
     551            if( $id == false ) {
     552           
     553                $this->output['error'] = __(
     554                    'There was a problem deleting the deal, please try again.',
     555                    self::PREFIX );
     556                return false;
     557               
     558            }
     559           
     560            $sql = $wpdb->prepare(
     561                'DELETE FROM ' . $wpdb->prefix . self::PREFIX .
     562                    '_deals WHERE `id` = %d;',
     563                $id
     564            );
     565           
     566            $result = $wpdb->query( $sql );
     567           
     568            if($result !== false)
     569            {
     570           
     571                $this->output['success'] =  __( 'The deal has been deleted.',
     572                    self::PREFIX );
     573               
     574            }
     575            else
     576            {
     577           
     578                $this->output['error'] = __(
     579                    'There was a problem deleting the deal, please try again..',
     580                    self::PREFIX );
     581               
     582            }
     583           
    308584            return $result;
    309            
    310         }
    311 
    312         function AjaxResponse($action) {
    313 
    314             $data = $this->output;
    315 
    316             $data['action'] = $action;
    317 
    318             if ($action == 'AddDeal' || $action == 'SetEnabledStatus' || $action == 'DeleteDeal') {
    319 
    320                 ob_start();
    321 
    322                 include( 'html/elements/deal-list.php' );
    323 
    324                 $data['html'] = ob_get_contents();
    325 
    326                 ob_end_clean();
    327                
    328             } elseif ($action == 'GetRandomDeal') {
    329 
    330                 ob_start();
    331 
    332                 include( 'html/elements/deal-display.php' );
    333                
    334                 $data['html'] = ob_get_contents();
    335 
    336                 ob_end_clean();
    337                
    338             }
    339 
    340             die(json_encode($data));
    341            
    342         }
    343 
    344         private function IsAjax() {
    345 
    346             if (
    347                 isset($_SERVER['HTTP_X_REQUESTED_WITH']) &&
    348                 $_SERVER['HTTP_X_REQUESTED_WITH'] == "XMLHttpRequest"
    349             ) {
    350 
    351                 return true;
    352                
    353             }
    354 
    355             return false;
    356            
    357         }
    358 
    359         private function GetDeals() {
    360 
     585       
     586        }
     587       
     588        function CheckUpdate() {
     589           
    361590            global $wpdb;
    362 
    363             $sql = 'SELECT * FROM ' . $wpdb->prefix . self::PREFIX . '_deals';
    364 
    365             $result = $wpdb->get_results($sql);
    366 
    367             return $result;
    368            
    369         }
    370 
    371         private function GetRandomDeal() {
    372 
     591           
     592            $installed_ver = get_option( self::PREFIX . '_db_version' );
     593           
     594            if ($installed_ver == false) {
     595
     596                $this->InstallTable();
     597
     598                return false;
     599
     600            }
     601           
     602            if( $installed_ver == self::DB_VERSION ) return false;
     603           
     604            $deals_table = $wpdb->prefix . self::PREFIX . '_deals';
     605           
     606            if( $installed_ver < 3 )
     607            {
     608               
     609                $sql_add_enabled =  'ALTER TABLE ' . $deals_table .
     610                    ' ADD `expires` DATETIME NOT NULL AFTER `enabled`;';
     611               
     612                $wpdb->query( $sql_add_enabled );
     613               
     614            }
     615           
     616            if( $installed_ver < 4 )
     617            {
     618           
     619                $sql_edit_sale_price_type = 'ALTER TABLE ' .
     620                    $deals_table.' MODIFY `sale_price` float NOT NULL;';
     621               
     622                $wpdb->query( $sql_edit_sale_price_type );
     623               
     624                $sql_edit_enabled_type = 'ALTER TABLE ' . $deals_table .
     625                    ' MODIFY `enabled` tinyint(1) NOT NULL;';
     626               
     627                $wpdb->query( $sql_edit_enabled_type );
     628   
     629            }
     630           
     631            update_option( self::PREFIX . '_db_version', self::DB_VERSION );
     632       
     633        }
     634       
     635        function InstallTable() {
     636       
    373637            global $wpdb;
    374 
    375             $sql = 'SELECT * FROM ' . $wpdb->prefix . self::PREFIX .
    376                 '_deals WHERE `enabled` = true AND `expires` > NOW();';
    377 
    378             $results = $wpdb->get_results($sql);
    379 
    380             if (is_array($results) && sizeof($results) > 0) {
    381 
    382                 return $results[array_rand($results, 1)];
    383                
    384             }
    385 
    386             return false;
    387            
    388         }
    389 
    390         private function AddDeal($product_name, $sale_price, $enabled = false, $expires = false) {
    391 
    392             global $wpdb;
    393 
    394             if (empty($product_name)) {
    395 
    396                 $this->output['error'] = __('The product name cannot be empty', self::PREFIX);
    397 
    398                 return false;
    399                
    400             }
    401 
    402             if (!is_numeric($sale_price)) {
    403 
    404                 $this->output['error'] = __('The sale price must be a valid number.', self::PREFIX);
    405 
    406                 return false;
    407                
    408             }
    409 
    410             if (is_array($expires)) {
    411 
    412                 if (
    413                         !array_key_exists('Year', $expires) ||
    414                         !array_key_exists('Month', $expires) ||
    415                         !array_key_exists('Day', $expires)
    416                 ) {
    417                
    418                     $this->output['error'] =
    419                         __('A year month and day are required for the expiration date.', self::PREFIX);
    420 
    421                     return false;
    422                    
    423                 }
    424 
    425                 $expires = $expires['Year'] . '-' . $expires['Month'] . '-' . $expires['Day'];
    426                
    427             }
    428 
    429 
    430             $sql = $wpdb->prepare(
    431                 'INSERT INTO ' . $wpdb->prefix . self::PREFIX .
    432                 '_deals(`product_name`, `sale_price`, `enabled`, `expires`)' .
    433                 ' VALUES(%s,%s, %s, %s);', $product_name, $sale_price, $enabled, $expires
    434             );
    435 
    436             $result = $wpdb->query($sql);
    437 
    438             if ($result !== false) {
    439 
    440                 $this->output['success'] = __('Deal has been added.', self::PREFIX);
    441                
    442             } else {
    443 
    444                 $this->output['error'] = __(
    445                     'There was a problem adding the deal, please try again.', self::PREFIX);
    446                        
    447             }
    448 
    449             return $result;
    450            
    451         }
    452 
    453         private function SetEnabledStatus($id, $enabled) {
    454 
    455             global $wpdb;
    456 
    457             if ($id == false) {
    458 
    459                 $this->output['error'] = __(
    460                     'There was a problem changing the deal status, please try again.', self::PREFIX);
    461 
    462                 return false;
    463                
    464             }
    465 
    466             $sql = $wpdb->prepare(
    467                 'UPDATE ' . $wpdb->prefix . self::PREFIX .
    468                 '_deals SET `enabled` = %d WHERE `id` = %d;', $enabled, $id
    469             );
    470 
    471             $result = $wpdb->query($sql);
    472 
    473             if ($result !== false) {
    474 
    475                 $this->output['success'] = __('The enabled status has been updated.', self::PREFIX);
    476                
    477             } else {
    478 
    479                 $this->output['error'] = __(
    480                     'There was a problem changing the deal status, please try again.', self::PREFIX);
    481                        
    482             }
    483 
    484             return $result;
    485            
    486         }
    487 
    488         private function DeleteDeal($id = false) {
    489 
    490             global $wpdb;
    491 
    492             if ($id == false) {
    493 
    494                 $this->output['error'] = __(
    495                     'There was a problem deleting the deal, please try again.', self::PREFIX);
    496                    
    497                 return false;
    498                
    499             }
    500 
    501             $sql = $wpdb->prepare(
    502                 'DELETE FROM ' . $wpdb->prefix . self::PREFIX .
    503                 '_deals WHERE `id` = %d;', $id
    504             );
    505 
    506             $result = $wpdb->query($sql);
    507 
    508             if ($result !== false) {
    509 
    510                 $this->output['success'] = __('The deal has been deleted.', self::PREFIX);
    511                
    512             } else {
    513 
    514                 $this->output['error'] = __(
    515                     'There was a problem deleting the deal, please try again..', self::PREFIX);
    516                        
    517             }
    518 
    519             return $result;
    520            
    521         }
    522 
    523         function CheckUpdate() {
    524 
    525             global $wpdb;
    526 
    527             $installed_ver = get_option(self::PREFIX . '_db_version');
    528 
    529             if ($installed_ver == false) {
    530 
    531                 MakeMyBlogHonest_Activate();
    532 
    533                 return false;
    534                
    535             }
    536 
    537             if ($installed_ver == self::DB_VERSION)
    538                 return false;
    539 
    540             $deals_table = $wpdb->prefix . self::PREFIX . '_deals';
    541 
    542             if ($installed_ver < 3) {
    543 
    544                 $sql_add_enabled = 'ALTER TABLE ' . $deals_table .
    545                     ' ADD `expires` DATETIME NOT NULL AFTER `enabled`;';
    546 
    547                 $wpdb->query($sql_add_enabled);
    548                
    549             }
    550 
    551             if ($installed_ver < 4) {
    552 
    553                 $sql_edit_sale_price_type = 'ALTER TABLE ' .
    554                     $deals_table . ' MODIFY `sale_price` float NOT NULL;';
    555 
    556                 $wpdb->query($sql_edit_sale_price_type);
    557 
    558                 $sql_edit_enabled_type = 'ALTER TABLE ' . $deals_table .
    559                     ' MODIFY `enabled` tinyint(1) NOT NULL;';
    560 
    561                 $wpdb->query($sql_edit_enabled_type);
    562                
    563             }
    564 
    565             update_option(self::PREFIX . '_db_version', self::DB_VERSION);
    566            
    567         }
    568 
     638   
     639            $deals_table = $wpdb->prefix . self::PREFIX.'_deals';
     640           
     641            if($wpdb->get_var( 'SHOW TABLES LIKE \'' . $deals_table . '\';' ) != $deals_table)
     642            {
     643               
     644                $create_deals_table_sql = 'CREATE TABLE ' . $deals_table. ' (
     645                            `id` mediumint(9) NOT NULL AUTO_INCREMENT,
     646                            `product_name` varchar(256) NOT NULL,
     647                            `sale_price` float NOT NULL,
     648                            `enabled` tinyint(1) NOT NULL,
     649                            `expires` DATETIME NOT NULL,
     650                            UNIQUE KEY id (id)
     651                    );';
     652               
     653                $wpdb->query( $create_deals_table_sql );
     654               
     655            }
     656           
     657            add_option( self::PREFIX.'_db_version', self::DB_VERSION );
     658           
     659        }
     660                   
    569661    }
    570 
     662   
    571663    $myHonestBlog = new MakeMyBlogHonest();
    572 
    573     register_activation_hook(
    574         'makemybloghonest/makemybloghonest.php', 'MakeMyBlogHonest_Activate'
    575     );
    576 
    577     register_deactivation_hook(
    578         'makemybloghonest/makemybloghonest.php', 'MakeMyBlogHonest_Deactivate'
    579     );
    580 
    581     function MakeMyBlogHonest_Activate() {
    582 
    583         global $wpdb;
    584 
    585         $deals_table = $wpdb->prefix . MakeMyBlogHonest::PREFIX . '_deals';
    586 
    587         if ($wpdb->get_var('SHOW TABLES LIKE \'' . $deals_table . '\';') != $deals_table) {
    588 
    589             $create_deals_table_sql = 'CREATE TABLE ' . $deals_table . ' (
    590                 `id` mediumint(9) NOT NULL AUTO_INCREMENT,
    591                 `product_name` varchar(256) NOT NULL,
    592                 `sale_price` float NOT NULL,
    593                 `enabled` tinyint(1) NOT NULL,
    594                 `expires` DATETIME NOT NULL,
    595                 UNIQUE KEY id (id)
    596             );';
    597 
    598             $wpdb->query($create_deals_table_sql);
    599            
    600         }
    601 
    602         add_option(MakeMyBlogHonest::PREFIX . '_db_version', MakeMyBlogHonest::DB_VERSION);
    603        
    604     }
    605 
    606     function MakeMyBlogHonest_Deactivate() {
    607        
    608     }
    609 
     664   
    610665}
    611666
  • make-my-blog-honest/trunk/step6.php

    r459699 r459789  
    213213            if ($installed_ver == false) {
    214214
    215                 MakeMyBlogHonest_Activate();
     215                $this->InstallTable();
    216216
    217217                return false;
     
    258258        }
    259259       
     260        /* Runs if the plugin needs to add the table */
     261        function InstallTable() {
     262       
     263            global $wpdb;
     264   
     265            $deals_table = $wpdb->prefix . self::PREFIX.'_deals';
     266           
     267            /* Make sure we aren't going to overwrite an existing table. */
     268            if($wpdb->get_var( 'SHOW TABLES LIKE \'' . $deals_table . '\';' ) != $deals_table)
     269            {
     270               
     271                $create_deals_table_sql = 'CREATE TABLE ' . $deals_table. ' (
     272                            `id` mediumint(9) NOT NULL AUTO_INCREMENT,
     273                            `product_name` varchar(256) NOT NULL,
     274                            `sale_price` float NOT NULL,
     275                            `enabled` tinyint(1) NOT NULL,
     276                            `expires` DATETIME NOT NULL,
     277                            UNIQUE KEY id (id)
     278                    );';
     279               
     280                /* Create the table */
     281                $wpdb->query( $create_deals_table_sql );
     282               
     283            }
     284           
     285            /* Save the table version for future reference */
     286            add_option( self::PREFIX.'_db_version', self::DB_VERSION );
     287           
     288        }
     289       
    260290/* END NEW CODE */
    261291           
     
    264294    $myHonestBlog = new MakeMyBlogHonest();
    265295   
    266 /* NEW CODE */
    267    
    268     /* It's best to do activation and deactivation outside the class,
    269         because when activation occurs, the plugin object is not yet
    270         created */
    271     register_activation_hook(
    272         'makemybloghonest/makemybloghonest.php' ,
    273         'MakeMyBlogHonest_Activate'
    274     );
    275    
    276     register_deactivation_hook(
    277         'makemybloghonest/makemybloghonest.php' ,
    278         'MakeMyBlogHonest_Deactivate'
    279     );
    280    
    281     /* This runs only on activation. Does not run when plugin is upgraded */
    282     function MakeMyBlogHonest_Activate() {
    283    
    284         global $wpdb;
    285 
    286         $deals_table = $wpdb->prefix . MakeMyBlogHonest::PREFIX.'_deals';
    287        
    288         /* Make sure we aren't going to overwrite an existing table. */
    289         if($wpdb->get_var( 'SHOW TABLES LIKE \'' . $deals_table . '\';' ) != $deals_table)
    290         {
    291            
    292             $create_deals_table_sql = 'CREATE TABLE ' . $deals_table. ' (
    293                         `id` mediumint(9) NOT NULL AUTO_INCREMENT,
    294                         `product_name` varchar(256) NOT NULL,
    295                         `sale_price` float NOT NULL,
    296                         `enabled` tinyint(1) NOT NULL,
    297                         `expires` DATETIME NOT NULL,
    298                         UNIQUE KEY id (id)
    299                 );';
    300            
    301             /* Create the table */
    302             $wpdb->query( $create_deals_table_sql );
    303            
    304         }
    305        
    306         /* Save the table version for future reference */
    307         add_option( MakeMyBlogHonest::PREFIX.'_db_version', MakeMyBlogHonest::DB_VERSION );
    308        
    309     }
    310    
    311     /* It's uncommon to need to do anything on plugin deactivation.
    312         Don't delete data here the user might want to retrieve when they
    313         reactivate your plugin! */
    314     function MakeMyBlogHonest_Deactivate() {
    315        
    316     }
    317    
    318     /* To run code when your user deletes your plugin, create a file called
    319         uninstall.php in your plugins root directory. Check out the uninstall.php
    320         file to learn more. */
    321    
    322 /* END NEW CODE */
    323    
    324296}
    325297
     298/* To run code when your user deletes your plugin, create a file called
     299    uninstall.php in your plugins root directory. Check out the uninstall.php
     300    file to learn more. */
     301
    326302/* We've now added our own table to the WordPress database. BooYah! */
    327303
  • make-my-blog-honest/trunk/step7.php

    r459699 r459789  
    298298            if ($installed_ver == false) {
    299299
    300                 MakeMyBlogHonest_Activate();
     300                $this->InstallTable();
    301301
    302302                return false;
     
    336336       
    337337        }
     338       
     339        function InstallTable() {
     340       
     341            global $wpdb;
     342   
     343            $deals_table = $wpdb->prefix . self::PREFIX.'_deals';
     344           
     345            if($wpdb->get_var( 'SHOW TABLES LIKE \'' . $deals_table . '\';' ) != $deals_table)
     346            {
     347               
     348                $create_deals_table_sql = 'CREATE TABLE ' . $deals_table. ' (
     349                            `id` mediumint(9) NOT NULL AUTO_INCREMENT,
     350                            `product_name` varchar(256) NOT NULL,
     351                            `sale_price` float NOT NULL,
     352                            `enabled` tinyint(1) NOT NULL,
     353                            `expires` DATETIME NOT NULL,
     354                            UNIQUE KEY id (id)
     355                    );';
     356               
     357                $wpdb->query( $create_deals_table_sql );
     358               
     359            }
     360           
     361            add_option( self::PREFIX.'_db_version', self::DB_VERSION );
     362           
     363        }
    338364                   
    339365    }
     
    341367    $myHonestBlog = new MakeMyBlogHonest();
    342368   
    343     register_activation_hook(
    344         'makemybloghonest/makemybloghonest.php' ,
    345         'MakeMyBlogHonest_Activate'
    346     );
    347    
    348     register_deactivation_hook(
    349         'makemybloghonest/makemybloghonest.php' ,
    350         'MakeMyBlogHonest_Deactivate'
    351     );
    352    
    353     function MakeMyBlogHonest_Activate() {
    354    
    355         global $wpdb;
    356 
    357         $deals_table = $wpdb->prefix . MakeMyBlogHonest::PREFIX.'_deals';
    358 
    359         if($wpdb->get_var( 'SHOW TABLES LIKE \'' . $deals_table . '\';' ) != $deals_table)
    360         {
    361    
    362             $create_deals_table_sql = 'CREATE TABLE ' . $deals_table. ' (
    363                         `id` mediumint(9) NOT NULL AUTO_INCREMENT,
    364                         `product_name` varchar(256) NOT NULL,
    365                         `sale_price` float NOT NULL,
    366                         `enabled` tinyint(1) NOT NULL,
    367                         `expires` DATETIME NOT NULL,
    368                         UNIQUE KEY id (id)
    369                 );';
    370 
    371             $wpdb->query($create_deals_table_sql);
    372            
    373         }
    374        
    375         add_option( MakeMyBlogHonest::PREFIX.'_db_version', MakeMyBlogHonest::DB_VERSION );
    376        
    377     }
    378    
    379     function MakeMyBlogHonest_Deactivate() {
    380        
    381     }
    382    
    383369}
    384370
  • make-my-blog-honest/trunk/step8.php

    r459699 r459789  
    516516            if ($installed_ver == false) {
    517517
    518                 MakeMyBlogHonest_Activate();
     518                $this->InstallTable();
    519519
    520520                return false;
     
    554554       
    555555        }
     556       
     557        function InstallTable() {
     558       
     559            global $wpdb;
     560   
     561            $deals_table = $wpdb->prefix . self::PREFIX.'_deals';
     562           
     563            if($wpdb->get_var( 'SHOW TABLES LIKE \'' . $deals_table . '\';' ) != $deals_table)
     564            {
     565               
     566                $create_deals_table_sql = 'CREATE TABLE ' . $deals_table. ' (
     567                            `id` mediumint(9) NOT NULL AUTO_INCREMENT,
     568                            `product_name` varchar(256) NOT NULL,
     569                            `sale_price` float NOT NULL,
     570                            `enabled` tinyint(1) NOT NULL,
     571                            `expires` DATETIME NOT NULL,
     572                            UNIQUE KEY id (id)
     573                    );';
     574               
     575                $wpdb->query( $create_deals_table_sql );
     576               
     577            }
     578           
     579            add_option( self::PREFIX.'_db_version', self::DB_VERSION );
     580           
     581        }
    556582                   
    557583    }
    558584   
    559585    $myHonestBlog = new MakeMyBlogHonest();
    560    
    561     register_activation_hook(
    562         'makemybloghonest/makemybloghonest.php' ,
    563         'MakeMyBlogHonest_Activate'
    564     );
    565    
    566     register_deactivation_hook(
    567         'makemybloghonest/makemybloghonest.php' ,
    568         'MakeMyBlogHonest_Deactivate'
    569     );
    570    
    571     function MakeMyBlogHonest_Activate() {
    572    
    573         global $wpdb;
    574 
    575         $deals_table = $wpdb->prefix . MakeMyBlogHonest::PREFIX.'_deals';
    576 
    577         if($wpdb->get_var( 'SHOW TABLES LIKE \'' . $deals_table . '\';' ) != $deals_table)
    578         {
    579    
    580             $create_deals_table_sql = 'CREATE TABLE ' . $deals_table. ' (
    581                         `id` mediumint(9) NOT NULL AUTO_INCREMENT,
    582                         `product_name` varchar(256) NOT NULL,
    583                         `sale_price` float NOT NULL,
    584                         `enabled` tinyint(1) NOT NULL,
    585                         `expires` DATETIME NOT NULL,
    586                         UNIQUE KEY id (id)
    587                 );';
    588 
    589             $wpdb->query($create_deals_table_sql);
    590            
    591         }
    592        
    593         add_option( MakeMyBlogHonest::PREFIX.'_db_version', MakeMyBlogHonest::DB_VERSION );
    594        
    595     }
    596    
    597     function MakeMyBlogHonest_Deactivate() {
    598        
    599     }
    600586   
    601587}
  • make-my-blog-honest/trunk/step9.php

    r459699 r459789  
    597597            if ($installed_ver == false) {
    598598
    599                 MakeMyBlogHonest_Activate();
     599                $this->InstallTable();
    600600
    601601                return false;
     
    635635       
    636636        }
     637       
     638        function InstallTable() {
     639       
     640            global $wpdb;
     641   
     642            $deals_table = $wpdb->prefix . self::PREFIX.'_deals';
     643           
     644            if($wpdb->get_var( 'SHOW TABLES LIKE \'' . $deals_table . '\';' ) != $deals_table)
     645            {
     646               
     647                $create_deals_table_sql = 'CREATE TABLE ' . $deals_table. ' (
     648                            `id` mediumint(9) NOT NULL AUTO_INCREMENT,
     649                            `product_name` varchar(256) NOT NULL,
     650                            `sale_price` float NOT NULL,
     651                            `enabled` tinyint(1) NOT NULL,
     652                            `expires` DATETIME NOT NULL,
     653                            UNIQUE KEY id (id)
     654                    );';
     655               
     656                $wpdb->query( $create_deals_table_sql );
     657               
     658            }
     659           
     660            add_option( self::PREFIX.'_db_version', self::DB_VERSION );
     661           
     662        }
    637663                   
    638664    }
     
    640666    $myHonestBlog = new MakeMyBlogHonest();
    641667   
    642     register_activation_hook(
    643         'makemybloghonest/makemybloghonest.php' ,
    644         'MakeMyBlogHonest_Activate'
    645     );
    646    
    647     register_deactivation_hook(
    648         'makemybloghonest/makemybloghonest.php' ,
    649         'MakeMyBlogHonest_Deactivate'
    650     );
    651    
    652     function MakeMyBlogHonest_Activate() {
    653    
    654         global $wpdb;
    655 
    656         $deals_table = $wpdb->prefix . MakeMyBlogHonest::PREFIX.'_deals';
    657 
    658         if($wpdb->get_var( 'SHOW TABLES LIKE \'' . $deals_table . '\';' ) != $deals_table)
    659         {
    660    
    661             $create_deals_table_sql = 'CREATE TABLE ' . $deals_table. ' (
    662                         `id` mediumint(9) NOT NULL AUTO_INCREMENT,
    663                         `product_name` varchar(256) NOT NULL,
    664                         `sale_price` float NOT NULL,
    665                         `enabled` tinyint(1) NOT NULL,
    666                         `expires` DATETIME NOT NULL,
    667                         UNIQUE KEY id (id)
    668                 );';
    669 
    670             $wpdb->query($create_deals_table_sql);
    671            
    672         }
    673        
    674         add_option( MakeMyBlogHonest::PREFIX.'_db_version', MakeMyBlogHonest::DB_VERSION );
    675        
    676     }
    677    
    678     function MakeMyBlogHonest_Deactivate() {
    679        
    680     }
    681    
    682668}
    683669
Note: See TracChangeset for help on using the changeset viewer.