Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * @package myplugin1
- */
- /*
- Plugin Name: myplugin1
- Plugin URI: xxx
- Description: xxx
- Domain Path: /lang
- Version: 0.1
- Author: xxx
- Author URI: xxx
- License: GPLv2 or later
- */
- /*
- This program is free software; you can redistribute it and/or
- modify it under the terms of the GNU General Public License
- as published by the Free Software Foundation; either version 2
- of the License, or (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
- //No direct access
- if( !function_exists( 'add_action' ) )
- {
- header('Status: 403 Forbidden');
- header('HTTP/1.1 403 Forbidden');
- exit;
- }
- //PHP5 mandatory
- if( version_compare( PHP_VERSION, '5.0.0', '<' ) )
- {
- add_action( 'admin_notices', 'put_version_requirephp' );
- function put_version_requirephp() {
- if( current_user_can( 'manage_options' ) )
- echo '<div class="error"><p>This plugin requires at least PHP 5.</p></div>';
- }
- return;
- }
- //WP 3.3 mandatory (help screens)
- if( version_compare( $wp_version, '3.3', '<' ) )
- {
- add_action( 'admin_notices', 'put_version_requirewp' );
- function put_version_requirewp() {
- if( current_user_can( 'manage_options' ) )
- echo '<div class="error"><p>This plugin requires at least WP 3.3</p></div>';
- }
- return;
- }
- if ( ! class_exists('myplugin1') )
- {
- class myplugin1
- {
- private $adminOptionsName = 'myplugin1AdminOptions'; //admin options variables
- private $myplugin1_table_version = '1.0'; // version of the table for the plugin
- private $pluginURL;
- private $textdom = 'myplugin1';//traduction prefix
- private $table1 = 'mytable1';
- private $table2 = 'mytable2';
- private $table3 = 'mytable3';
- private $screen_id;
- // For testing plugin locales
- public function switch_locale( $locale )
- {
- $locale = 'LOCALETEST'; // => lang/myplugin1-LOCALETEST.po
- return $locale;
- }
- /** Fonction d'initialisation du plugin
- *
- * @return
- */
- public function __construct()
- {
- if ( is_admin() )
- {
- $this->pluginURL = plugin_dir_url( __FILE__ );
- $this->register_hooks();
- add_action('admin_menu', array(&$this, 'myplugin1_adminmenu'));
- // add_filter( 'plugin_locale' , array( $this , 'switch_locale' ) ); // Use in TEST mode : Quick way to fake locale
- }
- }
- private function register_hooks()
- {
- if(function_exists('register_activation_hook'))
- register_activation_hook(__FILE__, array(&$this, 'on_activate'));
- if(function_exists('register_deactivation_hook'))
- register_deactivation_hook(__FILE__, array(&$this, 'on_deactivate'));
- // if(function_exists('register_uninstall_hook'))
- // register_uninstall_hook(__FILE__, array(&$this, 'on_uninstall'));
- //=> Error : must be static call
- }
- /** On plugin install
- *
- * @return void
- */
- public function on_activate()
- {
- $this->do_activate();
- }
- /** Db & options setup
- *
- * @return
- */
- private function do_activate()
- {
- $this->getAdminOptions();
- $this->myplugin1_update_db_check();
- }
- /** On plugin on_deactivation
- *
- * @return void
- */
- public function on_deactivate()
- {
- $this->do_deactivate();
- }
- /** Do nothing
- *
- * @return
- */
- private function do_deactivate()
- {
- return;
- }
- /** Delete multiple options
- * ex : deleteOptions('is_installed', 'my_plugin_version', 'my_option');
- * @return boolean
- */
- function deleteOptions()
- {
- $args = func_get_args();
- if (count($args) > 0)
- {
- foreach ($args as $option) {
- if ( ! delete_option($option))
- return FALSE;
- }
- return TRUE;
- }
- return FALSE;
- }
- /** On plugin uninstall
- *
- * @return
- */
- public function on_uninstall()
- {
- if ( __FILE__ != WP_UNINSTALL_PLUGIN )
- return;
- $this->do_uninstall();
- }
- /** Delete db && unregister options
- *
- * @return void
- */
- private function do_uninstall()
- {
- global $wpdb;
- //delete tables
- $tables = array(
- $wpdb->prefix.$this->table1,
- $wpdb->prefix.$this->table2,
- $wpdb->prefix.$this->table3);
- foreach($tables as $table)
- {
- if($wpdb->get_var("SHOW TABLES LIKE '$table'") == $table)
- {
- $sql = "DROP TABLE `$table`";
- $wpdb->query($sql);
- }
- }
- //delete options
- delete_option($this->adminOptionsName);
- }
- /** Administration default options
- *
- * @return
- */
- private function getAdminOptions()
- {
- //OptionList
- $ntAdminOptions = array(
- 'myplugin1_table_version' => $this->myplugin1_table_version
- );
- $nt_options = get_option($this->adminOptionsName);
- if (!empty($nt_options))
- {
- foreach ($nt_options as $key => $option)
- $ntAdminOptions[$key] = $option;
- }
- update_option($this->adminOptionsName, $ntAdminOptions);
- return $ntAdminOptions;
- }
- /** Tables creation
- *
- * @return
- */
- private function table_install()
- {
- global $wpdb;
- $charset_collate = '';
- if ( ! empty($wpdb->charset) )
- $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
- if ( ! empty($wpdb->collate) )
- $charset_collate .= " COLLATE $wpdb->collate";
- $table1 = $wpdb->prefix.$this->table1;
- if($wpdb->get_var("SHOW TABLES LIKE '$table1'") != $table1)
- {
- $sql = "CREATE TABLE `$table1` (
- `id_track` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
- `id_abo_track` mediumint(8) unsigned NOT NULL,
- `mail_abo_track` varchar(255) NOT NULL,
- PRIMARY KEY (`id_track`)
- ) $charset_collate;";
- require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
- dbDelta($sql);
- }
- $table2 = $wpdb->prefix.$this->table2;
- if($wpdb->get_var("SHOW TABLES LIKE '$table2'") != $table2)
- {
- $sql = "CREATE TABLE `$table2` (
- `id_ntl` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
- `nom_ntl` varchar(255) NOT NULL,
- `id_liste_ntl` mediumint(8) unsigned NOT NULL,
- PRIMARY KEY (`id_ntl`)
- ) $charset_collate;";
- require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
- dbDelta($sql);
- }
- $table3 = $wpdb->prefix.$this->table3;
- if($wpdb->get_var("SHOW TABLES LIKE '$table3'") != $table3)
- {
- $sql = "CREATE TABLE `$table3` (
- `id_ntr` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
- `id_ntl_ntr` mediumint(8) unsigned NOT NULL COMMENT 'letter id',
- `lien_ntr` varchar(255) NOT NULL,
- `code_ntr` varchar(100) NOT NULL,
- PRIMARY KEY (`id_ntr`)
- ) $charset_collate;";
- require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
- dbDelta($sql);
- }
- // plugin versioning
- add_option("myplugin1_table_version", $this->myplugin1_table_version);
- }
- /** Plugin table update checking
- *
- * @return
- */
- private function myplugin1_update_db_check()
- {
- if ( get_option('myplugin1_table_version') != $this->myplugin1_table_version )
- {
- $this->table_install();
- }
- }
- /** Administration menu
- *
- * @return
- */
- public function mainmenu()
- {
- echo '
- <div class="wrap"><div id="icon-options-general" class="icon32"><br></div>
- <h2>Main menu</h2>
- <p>Menu content</p>
- </div>';
- }
- public function mysubmenu()
- {
- echo '
- <div class="wrap"><div id="icon-options-general" class="icon32"><br></div>
- <h2>Submenu title</h2>
- <p>Submenu content</p>
- </div>';
- $screen = get_current_screen();
- $screen->add_help_tab( array(
- 'id' => $this->screen_id,
- 'title' => __('Submenu help'),
- 'content' => __('Submenu help content'),
- ));
- }
- public function myplugin1_adminmenu()
- {
- $pluginpath = $this->pluginURL;
- if (function_exists('add_menu_page'))
- {
- $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' );
- }
- if (function_exists('add_submenu_page'))
- {
- $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') );
- }
- }
- }
- }
- $myplugin1_instance = new myplugin1();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement