Advertisement
Viruthagiri

mf_install.php

Jan 2nd, 2012
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /**
  3.  * This file content the routines for install/activate  uninstall/deactivate Magic Fields
  4.  */
  5. class mf_install {
  6.  
  7.   function install () {
  8.     global $wpdb;
  9.    
  10.     require_once(ABSPATH.'wp-admin/includes/upgrade.php');
  11.  
  12.         // Get collation info
  13.         $charset_collate = "";
  14.         if ( ! empty($wpdb->charset) )
  15.             $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
  16.         if ( ! empty($wpdb->collate) )
  17.             $charset_collate .= " COLLATE $wpdb->collate";
  18.  
  19.     //checking if the table is already installed
  20.  
  21.     if($wpdb->get_var( sprintf("SHOW tables LIKE '%s'",MF_TABLE_POSTTYPES) ) != MF_TABLE_POSTTYPES) {
  22.       $sql = "CREATE TABLE ".MF_TABLE_POSTTYPES. " (
  23.        id mediumint(9) NOT NULL AUTO_INCREMENT,
  24.        type varchar(20) NOT NULL,
  25.        name varchar(50) NOT NULL,
  26.        description text,
  27.        arguments text,
  28.        active tinyint(1) DEFAULT 1,
  29.        PRIMARY KEY id (id) ) $charset_collate
  30.      ";
  31.       dbDelta($sql);
  32.     }
  33.  
  34.     // Table custom taxonomy
  35.     if($wpdb->get_var( sprintf("SHOW tables LIKE '%s'",MF_TABLE_CUSTOM_TAXONOMY) ) != MF_TABLE_CUSTOM_TAXONOMY) {
  36.       $sql = "CREATE TABLE ".MF_TABLE_CUSTOM_TAXONOMY. " (
  37.        id mediumint(9) NOT NULL AUTO_INCREMENT,
  38.        type varchar(20) NOT NULL,
  39.        name varchar(50) NOT NULL,
  40.        description text,
  41.        arguments text,
  42.        active tinyint(1) DEFAULT 1,
  43.        PRIMARY KEY id (id) ) $charset_collate
  44.      ";
  45.       dbDelta($sql);
  46.     }
  47.    
  48.     // Table custom fields
  49.     if($wpdb->get_var( sprintf("SHOW tables LIKE '%s'",MF_TABLE_CUSTOM_FIELDS) ) != MF_TABLE_CUSTOM_FIELDS) {
  50.       $sql = "CREATE TABLE ".MF_TABLE_CUSTOM_FIELDS. " (
  51.        id int(19) NOT NULL AUTO_INCREMENT,
  52.        name varchar(150) NOT NULL,
  53.        label varchar(150) NOT NULL,
  54.        description text,
  55.        post_type varchar(120) NOT NULL,
  56.        custom_group_id int(19) NOT NULL,
  57.        type varchar(100) NOT NULL,
  58.        required_field tinyint(1),
  59.        display_order mediumint(9) DEFAULT 0,
  60.        duplicated tinyint(1),
  61.        active tinyint(1) DEFAULT 1,
  62.        options text,
  63.        PRIMARY KEY id (id) ) $charset_collate
  64.      ";
  65.       dbDelta($sql);
  66.     }
  67.  
  68.     // Table custom groups
  69.     if($wpdb->get_var( sprintf("SHOW tables LIKE '%s'",MF_TABLE_CUSTOM_GROUPS) ) != MF_TABLE_CUSTOM_GROUPS) {
  70.       $sql = "CREATE TABLE ".MF_TABLE_CUSTOM_GROUPS. " (
  71.        id integer NOT NULL AUTO_INCREMENT,
  72.        name varchar(255) NOT NULL,
  73.        label varchar(255) NOT NULL,
  74.        post_type varchar(255) NOT NULL,
  75.        duplicated tinyint(1) DEFAULT 0,
  76.        expanded tinyint(1) DEFAULT 0,
  77.        PRIMARY KEY id (id) ) $charset_collate
  78.      ";
  79.       dbDelta($sql);
  80.  
  81.     }
  82.  
  83.     // Table MF Post Meta
  84.     if( $wpdb->get_var( sprintf("SHOW tables LIKE '%s'",MF_TABLE_POST_META) ) != MF_TABLE_POST_META ) {
  85.       $sql = "CREATE TABLE ".MF_TABLE_POST_META." (
  86.        meta_id INT NOT NULL,
  87.        field_name VARCHAR(255) NOT NULL,
  88.        field_count INT NOT NULL,  
  89.        group_count  INT NOT NULL,
  90.        post_id INT NOT NULL,
  91.         PRIMARY KEY meta_id (meta_id) ) $charset_collate
  92.                 ";
  93.  
  94.       dbDelta($sql);
  95.     }
  96.    
  97.     if ( get_option( MF_DB_VERSION_KEY, FALSE ) === FALSE ) update_option(MF_DB_VERSION_KEY, MF_DB_VERSION);
  98.  
  99.     if (get_option(MF_DB_VERSION_KEY) < MF_DB_VERSION){
  100.       self::upgrade();
  101.       update_option(MF_DB_VERSION_KEY, MF_DB_VERSION);
  102.     }
  103.    
  104.    
  105.   }
  106.  
  107.   public function upgrade(){  
  108.     global $wpdb;
  109.  
  110.     $db_version = get_option(MF_DB_VERSION_KEY);
  111.  
  112.     if( $db_version < 2 ) {
  113.       $sql = "ALTER TABLE ".MF_TABLE_CUSTOM_FIELDS. " CHANGE COLUMN requiered_field required_field tinyint(1)";
  114.       $wpdb->query( $sql );
  115.     }
  116.  
  117.     update_option(MF_DB_VERSION_KEY, MF_DB_VERSION);
  118.   }
  119.  
  120.   public function folders(){
  121.     global $mf_domain;
  122.    
  123.     $dir_list = "";
  124.     $dir_list2 = "";
  125.  
  126.     wp_mkdir_p(MF_FILES_DIR);
  127.     wp_mkdir_p(MF_CACHE_DIR);
  128.    
  129.     if (!is_dir(MF_CACHE_DIR)){
  130.       $dir_list2.= "<li>".MF_CACHE_DIR . "</li>";
  131.     }elseif (!is_writable(MF_CACHE_DIR)){
  132.       $dir_list.= "<li>".MF_CACHE_DIR . "</li>";
  133.     }
  134.    
  135.     if (!is_dir(MF_FILES_DIR)){
  136.       $dir_list2.= "<li>".MF_FILES_DIR . "</li>";
  137.     }elseif (!is_writable(MF_FILES_DIR)){
  138.       $dir_list.= "<li>".MF_FILES_DIR . "</li>";
  139.     }
  140.    
  141.     if ($dir_list2 != ""){
  142.       echo "<div id='magic-fields-install-error-message' class='error'><p><strong>".__('Magic Fields is not ready yet.', $mf_domain)."</strong> ".__('must create the following folders (and must chmod 777):', $mf_domain)."</p><ul>";
  143.       echo $dir_list2;
  144.       echo "</ul></div>";
  145.     }
  146.     if ($dir_list != ""){
  147.       echo "<div id='magic-fields-install-error-message-2' class='error'><p><strong>".__('Magic Fields is not ready yet.', $mf_domain)."</strong> ".__('The following folders must be writable (usually chmod 777 is neccesary):', $mf_domain)."</p><ul>";
  148.       echo $dir_list;
  149.       echo "</ul></div>";
  150.     }
  151.  
  152.   }
  153.  
  154.   //delete cache
  155.   public function clear_cache(){
  156.     if (is_dir(MF_CACHE_DIR)) {
  157.       if ($dh = opendir(MF_CACHE_DIR)) {
  158.         while (($file = readdir($dh)) !== false) {
  159.           if(!is_dir($file) && !in_array($file,array('.','..','.DS_Store') ) ){
  160.             @unlink(MF_CACHE_DIR.$file);
  161.           }
  162.         }
  163.         closedir($dh);
  164.       }
  165.     }
  166.   }
  167.  
  168.   public function delete_files(){
  169.     if (is_dir(MF_FILES_DIR)) {
  170.       if ($dh = opendir(MF_FILES_DIR)) {
  171.         while (($file = readdir($dh)) !== false) {
  172.           if(!is_dir($file) && !in_array($file,array('.','..','.DS_Store') ) ){
  173.             @unlink(MF_FILES_DIR.$file);
  174.           }
  175.         }
  176.         closedir($dh);
  177.       }
  178.     }
  179.   }
  180.  
  181.   //unistall MF (delete thumbs, tables and settings)
  182.   public function uninstall(){
  183.     global $wpdb;
  184.  
  185.     self::clear_cache();
  186.     self::delete_files();
  187.     delete_option(MF_SETTINGS_KEY);
  188.     //DB version
  189.     delete_option(MF_DB_VERSION_KEY);
  190.  
  191.     $sql = "DELETE a.* FROM $wpdb->postmeta AS a, ".MF_TABLE_POST_META." AS b WHERE b.meta_id = a.meta_id";
  192.     $wpdb->query($sql);
  193.  
  194.     $sql = "DROP TABLE " . MF_TABLE_POSTTYPES;
  195.     $wpdb->query($sql);
  196.    
  197.     $sql = "DROP TABLE " . MF_TABLE_CUSTOM_TAXONOMY;
  198.     $wpdb->query($sql);
  199.    
  200.     $sql = "DROP TABLE " . MF_TABLE_CUSTOM_FIELDS;
  201.     $wpdb->query($sql);
  202.  
  203.     $sql = "DROP TABLE " . MF_TABLE_CUSTOM_GROUPS;
  204.     $wpdb->query($sql);
  205.  
  206.     $sql = "DROP TABLE " . MF_TABLE_POST_META;
  207.     $wpdb->query($sql);
  208.    
  209.     $current = get_option('active_plugins');
  210.     $plugin = plugin_basename(MF_PATH.'/main.php');
  211.     array_splice($current, array_search( $plugin, $current), 1 );
  212.     do_action('deactivate_' . trim( $plugin ));
  213.     update_option('active_plugins', $current);
  214.  
  215.     wp_redirect('options-general.php');
  216.   }
  217.  
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement