PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/mf_install.php

https://github.com/leocaseiro/Magic-Fields-2
PHP | 212 lines | 160 code | 38 blank | 14 comment | 31 complexity | 4d118a7bbb5895f3428b41f4f6e257b0 MD5 | raw file
  1. <?php
  2. /**
  3. * This file content the routines for install/activate uninstall/deactivate Magic Fields
  4. */
  5. class mf_install {
  6. function install () {
  7. global $wpdb;
  8. require_once(ABSPATH.'wp-admin/includes/upgrade.php');
  9. // Get collation info
  10. $charset_collate = "";
  11. if ( ! empty($wpdb->charset) )
  12. $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
  13. if ( ! empty($wpdb->collate) )
  14. $charset_collate .= " COLLATE $wpdb->collate";
  15. //checking if the table is already installed
  16. if($wpdb->get_var( sprintf("SHOW tables LIKE '%s'",MF_TABLE_POSTTYPES) ) != MF_TABLE_POSTTYPES) {
  17. $sql = "CREATE TABLE ".MF_TABLE_POSTTYPES. " (
  18. id mediumint(9) NOT NULL AUTO_INCREMENT,
  19. type varchar(20) NOT NULL,
  20. name varchar(50) NOT NULL,
  21. description text,
  22. arguments text,
  23. active tinyint(1) DEFAULT 1,
  24. PRIMARY KEY id (id) ) $charset_collate
  25. ";
  26. dbDelta($sql);
  27. }
  28. // Table custom taxonomy
  29. if($wpdb->get_var( sprintf("SHOW tables LIKE '%s'",MF_TABLE_CUSTOM_TAXONOMY) ) != MF_TABLE_CUSTOM_TAXONOMY) {
  30. $sql = "CREATE TABLE ".MF_TABLE_CUSTOM_TAXONOMY. " (
  31. id mediumint(9) NOT NULL AUTO_INCREMENT,
  32. type varchar(20) NOT NULL,
  33. name varchar(50) NOT NULL,
  34. description text,
  35. arguments text,
  36. active tinyint(1) DEFAULT 1,
  37. PRIMARY KEY id (id) ) $charset_collate
  38. ";
  39. dbDelta($sql);
  40. }
  41. // Table custom fields
  42. if($wpdb->get_var( sprintf("SHOW tables LIKE '%s'",MF_TABLE_CUSTOM_FIELDS) ) != MF_TABLE_CUSTOM_FIELDS) {
  43. $sql = "CREATE TABLE ".MF_TABLE_CUSTOM_FIELDS. " (
  44. id int(19) NOT NULL AUTO_INCREMENT,
  45. name varchar(150) NOT NULL,
  46. label varchar(150) NOT NULL,
  47. description text,
  48. post_type varchar(120) NOT NULL,
  49. custom_group_id int(19) NOT NULL,
  50. type varchar(100) NOT NULL,
  51. requiered_field tinyint(1),
  52. display_order mediumint(9) DEFAULT 0,
  53. duplicated tinyint(1),
  54. active tinyint(1) DEFAULT 1,
  55. options text,
  56. PRIMARY KEY id (id) ) $charset_collate
  57. ";
  58. dbDelta($sql);
  59. }
  60. // Table custom groups
  61. if($wpdb->get_var( sprintf("SHOW tables LIKE '%s'",MF_TABLE_CUSTOM_GROUPS) ) != MF_TABLE_CUSTOM_GROUPS) {
  62. $sql = "CREATE TABLE ".MF_TABLE_CUSTOM_GROUPS. " (
  63. id integer NOT NULL AUTO_INCREMENT,
  64. name varchar(255) NOT NULL,
  65. label varchar(255) NOT NULL,
  66. post_type varchar(255) NOT NULL,
  67. duplicated tinyint(1) DEFAULT 0,
  68. expanded tinyint(1) DEFAULT 0,
  69. PRIMARY KEY id (id) ) $charset_collate
  70. ";
  71. dbDelta($sql);
  72. }
  73. // Table MF Post Meta
  74. if( $wpdb->get_var( sprintf("SHOW tables LIKE '%s'",MF_TABLE_POST_META) ) != MF_TABLE_POST_META ) {
  75. $sql = "CREATE TABLE ".MF_TABLE_POST_META." (
  76. meta_id INT NOT NULL,
  77. field_name VARCHAR(255) NOT NULL,
  78. field_count INT NOT NULL,
  79. group_count INT NOT NULL,
  80. post_id INT NOT NULL,
  81. PRIMARY KEY meta_id (meta_id) ) $charset_collate
  82. ";
  83. dbDelta($sql);
  84. }
  85. if (get_option(MF_DB_VERSION_KEY) == '') update_option(MF_DB_VERSION_KEY, 1);
  86. if (get_option(MF_DB_VERSION_KEY) < MF_DB_VERSION){
  87. self::upgrade();
  88. update_option(MF_DB_VERSION_KEY, MF_DB_VERSION);
  89. }
  90. }
  91. public function upgrade(){
  92. //update of DB WHERE
  93. // use MF_DB_VERSION
  94. }
  95. public function folders(){
  96. global $mf_domain;
  97. $dir_list = "";
  98. $dir_list2 = "";
  99. wp_mkdir_p(MF_FILES_DIR);
  100. wp_mkdir_p(MF_CACHE_DIR);
  101. if (!is_dir(MF_CACHE_DIR)){
  102. $dir_list2.= "<li>".MF_CACHE_DIR . "</li>";
  103. }elseif (!is_writable(MF_CACHE_DIR)){
  104. $dir_list.= "<li>".MF_CACHE_DIR . "</li>";
  105. }
  106. if (!is_dir(MF_FILES_DIR)){
  107. $dir_list2.= "<li>".MF_FILES_DIR . "</li>";
  108. }elseif (!is_writable(MF_FILES_DIR)){
  109. $dir_list.= "<li>".MF_FILES_DIR . "</li>";
  110. }
  111. if ($dir_list2 != ""){
  112. 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>";
  113. echo $dir_list2;
  114. echo "</ul></div>";
  115. }
  116. if ($dir_list != ""){
  117. 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>";
  118. echo $dir_list;
  119. echo "</ul></div>";
  120. }
  121. }
  122. //delete cache
  123. public function clear_cache(){
  124. if (is_dir(MF_CACHE_DIR)) {
  125. if ($dh = opendir(MF_CACHE_DIR)) {
  126. while (($file = readdir($dh)) !== false) {
  127. if(!is_dir($file) && !in_array($file,array('.','..','.DS_Store') ) ){
  128. @unlink(MF_CACHE_DIR.$file);
  129. }
  130. }
  131. closedir($dh);
  132. }
  133. }
  134. }
  135. public function delete_files(){
  136. if (is_dir(MF_FILES_DIR)) {
  137. if ($dh = opendir(MF_FILES_DIR)) {
  138. while (($file = readdir($dh)) !== false) {
  139. if(!is_dir($file) && !in_array($file,array('.','..','.DS_Store') ) ){
  140. @unlink(MF_FILES_DIR.$file);
  141. }
  142. }
  143. closedir($dh);
  144. }
  145. }
  146. }
  147. //unistall MF (delete thumbs, tables and settings)
  148. public function uninstall(){
  149. global $wpdb;
  150. self::clear_cache();
  151. self::delete_files();
  152. delete_option(MF_SETTINGS_KEY);
  153. //DB version
  154. delete_option(MF_DB_VERSION_KEY);
  155. $sql = "DELETE a.* FROM $wpdb->postmeta AS a, ".MF_TABLE_POST_META." AS b WHERE b.meta_id = a.meta_id";
  156. $wpdb->query($sql);
  157. $sql = "DROP TABLE " . MF_TABLE_POSTTYPES;
  158. $wpdb->query($sql);
  159. $sql = "DROP TABLE " . MF_TABLE_CUSTOM_TAXONOMY;
  160. $wpdb->query($sql);
  161. $sql = "DROP TABLE " . MF_TABLE_CUSTOM_FIELDS;
  162. $wpdb->query($sql);
  163. $sql = "DROP TABLE " . MF_TABLE_CUSTOM_GROUPS;
  164. $wpdb->query($sql);
  165. $sql = "DROP TABLE " . MF_TABLE_POST_META;
  166. $wpdb->query($sql);
  167. $current = get_option('active_plugins');
  168. $plugin = plugin_basename(MF_PATH.'/main.php');
  169. array_splice($current, array_search( $plugin, $current), 1 );
  170. do_action('deactivate_' . trim( $plugin ));
  171. update_option('active_plugins', $current);
  172. wp_redirect('options-general.php');
  173. }
  174. }