Advertisement
creacom

basic wordpress class

Dec 14th, 2011
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.52 KB | None | 0 0
  1. <?php
  2. /**
  3. * @package myplugin1
  4. */
  5. /*
  6. Plugin Name: myplugin1
  7. Plugin URI: xxx
  8. Description: xxx
  9. Domain Path: /lang
  10. Version: 0.1
  11. Author: xxx
  12. Author URI: xxx
  13. License: GPLv2 or later
  14. */
  15. /*
  16. This program is free software; you can redistribute it and/or
  17. modify it under the terms of the GNU General Public License
  18. as published by the Free Software Foundation; either version 2
  19. of the License, or (at your option) any later version.
  20.  
  21. This program is distributed in the hope that it will be useful,
  22. but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  24. GNU General Public License for more details.
  25.  
  26. You should have received a copy of the GNU General Public License
  27. along with this program; if not, write to the Free Software
  28. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  29. */
  30. //No direct access
  31. if( !function_exists( 'add_action' ) )
  32. {
  33.     header('Status: 403 Forbidden');
  34.     header('HTTP/1.1 403 Forbidden');
  35.     exit;
  36. }
  37. //PHP5 mandatory
  38. if( version_compare( PHP_VERSION, '5.0.0', '<' ) )
  39. {
  40.     add_action( 'admin_notices', 'put_version_requirephp' );
  41.     function put_version_requirephp() {
  42.         if( current_user_can( 'manage_options' ) )
  43.             echo '<div class="error"><p>This plugin requires at least PHP 5.</p></div>';
  44.     }
  45.     return;
  46. }
  47. //WP 3.3 mandatory (help screens)
  48. if( version_compare( $wp_version, '3.3', '<' ) )
  49. {
  50.     add_action( 'admin_notices', 'put_version_requirewp' );
  51.     function put_version_requirewp() {
  52.         if( current_user_can( 'manage_options' ) )
  53.             echo '<div class="error"><p>This plugin requires at least WP 3.3</p></div>';
  54.     }
  55.     return;
  56. }
  57.  
  58. if ( ! class_exists('myplugin1') )
  59. {
  60.     class myplugin1
  61.     {
  62.         private $adminOptionsName = 'myplugin1AdminOptions'; //admin options variables
  63.         private $myplugin1_table_version = '1.0'; // version of the table for the plugin
  64.         private $pluginURL;
  65.         private $textdom = 'myplugin1';//traduction prefix
  66.         private $table1 = 'mytable1';
  67.         private $table2 = 'mytable2';
  68.         private $table3 = 'mytable3';
  69.         private $screen_id;
  70.        
  71.         // For testing plugin locales
  72.         public function switch_locale( $locale )
  73.         {
  74.             $locale = 'LOCALETEST'; // => lang/myplugin1-LOCALETEST.po
  75.             return $locale;
  76.         }
  77.        
  78.         /** Fonction d'initialisation du plugin
  79.         *
  80.         * @return
  81.         */
  82.         public function __construct()
  83.         {
  84.             if ( is_admin() )
  85.             {
  86.                 $this->pluginURL = plugin_dir_url( __FILE__ );
  87.                 $this->register_hooks();
  88.                 add_action('admin_menu', array(&$this, 'myplugin1_adminmenu'));
  89.                 // add_filter( 'plugin_locale' , array( $this , 'switch_locale' ) ); // Use in TEST mode : Quick way to fake locale
  90.             }
  91.         }
  92.        
  93.         private function register_hooks()
  94.         {
  95.             if(function_exists('register_activation_hook'))
  96.                 register_activation_hook(__FILE__, array(&$this, 'on_activate'));
  97.            
  98.             if(function_exists('register_deactivation_hook'))
  99.                 register_deactivation_hook(__FILE__, array(&$this, 'on_deactivate'));
  100.            
  101.             // if(function_exists('register_uninstall_hook'))
  102.                 // register_uninstall_hook(__FILE__, array(&$this, 'on_uninstall'));
  103.             //=> Error : must be static call
  104.         }
  105.        
  106.         /** On plugin install
  107.         *
  108.         * @return void
  109.         */
  110.         public function on_activate()
  111.         {
  112.             $this->do_activate();
  113.         }
  114.        
  115.         /** Db & options setup
  116.         *
  117.         * @return
  118.         */
  119.         private function do_activate()
  120.         {
  121.             $this->getAdminOptions();
  122.             $this->myplugin1_update_db_check();
  123.         }
  124.        
  125.         /** On plugin on_deactivation
  126.         *
  127.         * @return void
  128.         */
  129.         public function on_deactivate()
  130.         {
  131.             $this->do_deactivate();
  132.         }
  133.        
  134.         /** Do nothing
  135.         *
  136.         * @return
  137.         */
  138.         private function do_deactivate()
  139.         {
  140.             return;
  141.         }
  142.         /** Delete multiple options
  143.         * ex : deleteOptions('is_installed', 'my_plugin_version', 'my_option');
  144.         * @return boolean
  145.         */
  146.         function deleteOptions()
  147.         {
  148.             $args = func_get_args();
  149.             if (count($args) > 0)
  150.             {
  151.                 foreach ($args as $option) {
  152.                     if ( ! delete_option($option))
  153.                         return FALSE;
  154.                 }
  155.                 return TRUE;
  156.             }
  157.             return FALSE;
  158.         }
  159.         /** On plugin uninstall
  160.         *
  161.         * @return
  162.         */
  163.         public function on_uninstall()
  164.         {
  165.             if ( __FILE__ != WP_UNINSTALL_PLUGIN )
  166.                 return;
  167.            
  168.             $this->do_uninstall();
  169.         }
  170.        
  171.         /** Delete db && unregister options
  172.         *
  173.         * @return void
  174.         */
  175.         private function do_uninstall()
  176.         {
  177.             global $wpdb;
  178.             //delete tables
  179.             $tables = array(
  180.                 $wpdb->prefix.$this->table1,
  181.                 $wpdb->prefix.$this->table2,
  182.                 $wpdb->prefix.$this->table3);
  183.             foreach($tables as $table)
  184.             {
  185.                 if($wpdb->get_var("SHOW TABLES LIKE '$table'") == $table)
  186.                 {
  187.                     $sql = "DROP TABLE `$table`";
  188.                     $wpdb->query($sql);
  189.                 }
  190.             }
  191.             //delete options
  192.             delete_option($this->adminOptionsName);
  193.         }
  194.        
  195.         /** Administration default options
  196.         *
  197.         * @return
  198.         */
  199.         private function getAdminOptions()
  200.         {
  201.         //OptionList
  202.             $ntAdminOptions = array(
  203.                 'myplugin1_table_version' => $this->myplugin1_table_version
  204.             );
  205.             $nt_options = get_option($this->adminOptionsName);
  206.             if (!empty($nt_options))
  207.             {
  208.                 foreach ($nt_options as $key => $option)
  209.                     $ntAdminOptions[$key] = $option;
  210.             }
  211.             update_option($this->adminOptionsName, $ntAdminOptions);
  212.             return $ntAdminOptions;
  213.         }
  214.        
  215.         /** Tables creation
  216.         *
  217.         * @return
  218.         */
  219.         private function table_install()
  220.         {
  221.             global $wpdb;
  222.             $charset_collate = '';
  223.             if ( ! empty($wpdb->charset) )
  224.                 $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
  225.             if ( ! empty($wpdb->collate) )
  226.                 $charset_collate .= " COLLATE $wpdb->collate";
  227.            
  228.             $table1 = $wpdb->prefix.$this->table1;
  229.             if($wpdb->get_var("SHOW TABLES LIKE '$table1'") != $table1)
  230.             {
  231.                 $sql = "CREATE TABLE `$table1` (
  232.                 `id_track` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
  233.                 `id_abo_track` mediumint(8) unsigned NOT NULL,
  234.                 `mail_abo_track` varchar(255) NOT NULL,
  235.                 PRIMARY KEY (`id_track`)
  236.                 ) $charset_collate;";
  237.                 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
  238.                 dbDelta($sql);
  239.             }
  240.             $table2 = $wpdb->prefix.$this->table2;
  241.             if($wpdb->get_var("SHOW TABLES LIKE '$table2'") != $table2)
  242.             {
  243.                 $sql = "CREATE TABLE `$table2` (
  244.                 `id_ntl` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
  245.                 `nom_ntl` varchar(255) NOT NULL,
  246.                 `id_liste_ntl` mediumint(8) unsigned NOT NULL,
  247.                 PRIMARY KEY (`id_ntl`)
  248.                 ) $charset_collate;";
  249.                 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
  250.                 dbDelta($sql);
  251.             }
  252.             $table3 = $wpdb->prefix.$this->table3;
  253.             if($wpdb->get_var("SHOW TABLES LIKE '$table3'") != $table3)
  254.             {
  255.                 $sql = "CREATE TABLE `$table3` (
  256.                 `id_ntr` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
  257.                 `id_ntl_ntr` mediumint(8) unsigned NOT NULL COMMENT 'letter id',
  258.                 `lien_ntr` varchar(255) NOT NULL,
  259.                 `code_ntr` varchar(100) NOT NULL,
  260.                 PRIMARY KEY (`id_ntr`)
  261.                 ) $charset_collate;";
  262.                 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
  263.                 dbDelta($sql);
  264.             }
  265.             // plugin versioning
  266.             add_option("myplugin1_table_version", $this->myplugin1_table_version);
  267.         }
  268.        
  269.         /** Plugin table update checking
  270.         *
  271.         * @return
  272.         */
  273.         private function myplugin1_update_db_check()
  274.         {
  275.             if ( get_option('myplugin1_table_version') != $this->myplugin1_table_version )
  276.             {
  277.                 $this->table_install();
  278.             }
  279.         }
  280.        
  281.         /** Administration menu
  282.         *
  283.         * @return
  284.         */
  285.         public function mainmenu()
  286.         {
  287.             echo '
  288.             <div class="wrap"><div id="icon-options-general" class="icon32"><br></div>
  289.             <h2>Main menu</h2>
  290.             <p>Menu content</p>
  291.             </div>';
  292.         }
  293.        
  294.         public function mysubmenu()
  295.         {
  296.             echo '
  297.             <div class="wrap"><div id="icon-options-general" class="icon32"><br></div>
  298.             <h2>Submenu title</h2>
  299.             <p>Submenu content</p>
  300.             </div>';
  301.             $screen = get_current_screen();
  302.             $screen->add_help_tab( array(
  303.                 'id' => $this->screen_id,
  304.                 'title' => __('Submenu help'),
  305.                 'content' => __('Submenu help content'),
  306.                 ));
  307.         }
  308.        
  309.         public function myplugin1_adminmenu()
  310.         {
  311.             $pluginpath = $this->pluginURL;
  312.             if (function_exists('add_menu_page'))
  313.             {
  314.                 $menu_page = add_menu_page(__('My main menu page title', $this->textdom), __('My main menu title', $this->textdom), 'manage_options', 'mainmenu', array(&$this, 'mainmenu'), $pluginpath.'images/menuicon.gif' );
  315.             }
  316.             if (function_exists('add_submenu_page'))
  317.             {
  318.                 $submenu_page = add_submenu_page('mainmenu', __('My main submenu page title', $this->textdom), __('My main submenu title', $this->textdom), 'manage_options', 'mysubmenu', array(&$this, 'mysubmenu') );
  319.             }
  320.         }
  321.     }
  322. }
  323.  
  324. $myplugin1_instance = new myplugin1();
  325. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement