PodsUpgrade

The Pods - Custom Content Types and Fields PodsUpgrade class.

Defined (1)

The class is defined in the following location(s).

/sql/upgrade/PodsUpgrade.php  
  1. class PodsUpgrade { 
  2.  
  3. /** 
  4. * @var array 
  5. */ 
  6. public $tables = array(); 
  7.  
  8. /** 
  9. * @var array 
  10. */ 
  11. protected $progress = array(); 
  12.  
  13. /** 
  14. * @var PodsAPI 
  15. */ 
  16. protected $api = null; 
  17.  
  18. /** 
  19. * @var string 
  20. */ 
  21. protected $version = null; 
  22.  
  23. /** 
  24. */ 
  25. function __construct () { 
  26. $this->api = pods_api(); 
  27.  
  28. $this->get_tables(); 
  29. $this->get_progress(); 
  30.  
  31. /** 
  32. */ 
  33. public function install ( $_blog_id = null ) { 
  34. /** 
  35. * @var $wpdb WPDB 
  36. */ 
  37. global $wpdb; 
  38.  
  39. // Switch DB table prefixes 
  40. if ( null !== $_blog_id && $_blog_id != $wpdb->blogid ) 
  41. switch_to_blog( pods_absint( $_blog_id ) ); 
  42. else 
  43. $_blog_id = null; 
  44.  
  45. $pods_version = get_option( 'pods_version' ); 
  46.  
  47. do_action( 'pods_install', PODS_VERSION, $pods_version, $_blog_id ); 
  48.  
  49. if ( ( !pods_tableless() ) && false !== apply_filters( 'pods_install_run', null, PODS_VERSION, $pods_version, $_blog_id ) && !isset( $_GET[ 'pods_bypass_install' ] ) ) { 
  50. $sql = file_get_contents( PODS_DIR . 'sql/dump.sql' ); 
  51. $sql = apply_filters( 'pods_install_sql', $sql, PODS_VERSION, $pods_version, $_blog_id ); 
  52.  
  53. $charset_collate = 'DEFAULT CHARSET utf8'; 
  54.  
  55. if ( !empty( $wpdb->charset ) ) 
  56. $charset_collate = "DEFAULT CHARSET {$wpdb->charset}"; 
  57.  
  58. if ( !empty( $wpdb->collate ) ) 
  59. $charset_collate .= " COLLATE {$wpdb->collate}"; 
  60.  
  61. if ( 'DEFAULT CHARSET utf8' != $charset_collate ) 
  62. $sql = str_replace( 'DEFAULT CHARSET utf8', $charset_collate, $sql ); 
  63.  
  64. $sql = explode( ";\n", str_replace( array( "\r", 'wp_' ), array( "\n", $wpdb->prefix ), $sql ) ); 
  65.  
  66. for ( $i = 0, $z = count( $sql ); $i < $z; $i++ ) { 
  67. $query = trim( $sql[ $i ] ); 
  68.  
  69. if ( empty( $query ) ) 
  70. continue; 
  71.  
  72. pods_query( $query, 'Cannot setup SQL tables' ); 
  73.  
  74. do_action( 'pods_install_post', PODS_VERSION, $pods_version, $_blog_id ); 
  75.  
  76. /** 
  77. */ 
  78. public function get_tables () { 
  79. /** 
  80. * @var $wpdb WPDB 
  81. */ 
  82. global $wpdb; 
  83.  
  84. $tables = $wpdb->get_results( "SHOW TABLES LIKE '{$wpdb->prefix}pod%'", ARRAY_N ); 
  85.  
  86. if ( !empty( $tables ) ) { 
  87. foreach ( $tables as $table ) { 
  88. $this->tables[] = $table[ 0 ]; 
  89.  
  90. /** 
  91. */ 
  92. function get_progress () { 
  93. $methods = get_class_methods( $this ); 
  94.  
  95. foreach ( $methods as $method ) { 
  96. if ( 0 === strpos( $method, 'migrate_' ) ) 
  97. $this->progress[ str_replace( 'migrate_', '', $method ) ] = false; 
  98.  
  99. $progress = (array) get_option( 'pods_framework_upgrade_' . str_replace( '.', '_', $this->version ), array() ); 
  100.  
  101. if ( !empty( $progress ) ) 
  102. $this->progress = array_merge( $this->progress, $progress ); 
  103.  
  104. /** 
  105. * @param $params 
  106. * @return mixed|void 
  107. */ 
  108. public function ajax ( $params ) { 
  109. if ( !isset( $params->step ) ) 
  110. return pods_error( __( 'Invalid upgrade process.', 'pods' ) ); 
  111.  
  112. if ( !isset( $params->type ) ) 
  113. return pods_error( __( 'Invalid upgrade method.', 'pods' ) ); 
  114.  
  115. if ( !method_exists( $this, $params->step . '_' . $params->type ) ) 
  116. return pods_error( __( 'Upgrade method not found.', 'pods' ) ); 
  117.  
  118. return call_user_func( array( $this, $params->step . '_' . $params->type ), $params ); 
  119.  
  120. /** 
  121. * @param $method 
  122. * @param $v 
  123. * @param null $x 
  124. */ 
  125. public function update_progress ( $method, $v, $x = null ) { 
  126. if ( empty( $this->version ) ) 
  127. return; 
  128.  
  129. $method = str_replace( 'migrate_', '', $method ); 
  130.  
  131. if ( null !== $x ) 
  132. $this->progress[ $method ][ $x ] = (boolean) $v; 
  133. else 
  134. $this->progress[ $method ] = $v; 
  135.  
  136. update_option( 'pods_framework_upgrade_' . str_replace( '.', '_', $this->version ), $this->progress ); 
  137.  
  138. /** 
  139. * @param $method 
  140. * @param null $x 
  141. * @return bool 
  142. */ 
  143. public function check_progress ( $method, $x = null ) { 
  144. $method = str_replace( 'migrate_', '', $method ); 
  145.  
  146. if ( isset( $this->progress[ $method ] ) ) { 
  147. if ( null === $x ) 
  148. return $this->progress[ $method ]; 
  149. elseif ( isset( $this->progress[ $method ][ $x ] ) ) 
  150. return (boolean) $this->progress[ $method ][ $x ]; 
  151.  
  152. return false; 
  153.  
  154. /** 
  155. */ 
  156. public function upgraded () { 
  157. if ( empty( $this->version ) ) 
  158. return; 
  159.  
  160. $upgraded = get_option( 'pods_framework_upgraded' ); 
  161.  
  162. if ( empty( $upgraded ) || !is_array( $upgraded ) ) 
  163. $upgraded = array(); 
  164.  
  165. delete_option( 'pods_framework_upgrade_' . str_replace( '.', '_', $this->version ) ); 
  166.  
  167. if ( !in_array( $this->version, $upgraded ) ) 
  168. $upgraded[] = $this->version; 
  169.  
  170. update_option( 'pods_framework_upgraded', $upgraded ); 
  171.  
  172. /** 
  173. */ 
  174. public function cleanup () { 
  175. /** 
  176. * @var $wpdb WPDB 
  177. */ 
  178. global $wpdb; 
  179.  
  180. foreach ( $this->tables as $table ) { 
  181. if ( false !== strpos( $table, "{$wpdb->prefix}pod_" ) || "{$wpdb->prefix}pod" == $table ) 
  182. pods_query( "DROP TABLE `{$table}`", false ); 
  183.  
  184. delete_option( 'pods_roles' ); 
  185. delete_option( 'pods_version' ); 
  186. delete_option( 'pods_framework_upgrade_2_0' ); 
  187. delete_option( 'pods_framework_upgrade_2_0_sister_ids' ); 
  188. delete_option( 'pods_framework_upgraded_1_x' ); 
  189.  
  190. delete_option( 'pods_disable_file_browser' ); 
  191. delete_option( 'pods_files_require_login' ); 
  192. delete_option( 'pods_files_require_login_cap' ); 
  193. delete_option( 'pods_disable_file_upload' ); 
  194. delete_option( 'pods_upload_require_login' ); 
  195. delete_option( 'pods_upload_require_login_cap' ); 
  196.  
  197. pods_query( "DELETE FROM `@wp_postmeta` WHERE `meta_key` LIKE '_pods_1x_%'" );