PodsUpgrade.php
337 lines
| 1 | <?php |
| 2 | |
| 3 | // Don't load directly. |
| 4 | if ( ! defined( 'ABSPATH' ) ) { |
| 5 | die( '-1' ); |
| 6 | } |
| 7 | |
| 8 | /** |
| 9 | * @package Pods\Upgrade |
| 10 | */ |
| 11 | class PodsUpgrade { |
| 12 | |
| 13 | /** |
| 14 | * @var array |
| 15 | */ |
| 16 | public $tables = []; |
| 17 | |
| 18 | /** |
| 19 | * @var array |
| 20 | */ |
| 21 | protected $progress = []; |
| 22 | |
| 23 | /** |
| 24 | * @var PodsAPI |
| 25 | */ |
| 26 | protected $api = null; |
| 27 | |
| 28 | /** |
| 29 | * @var string |
| 30 | */ |
| 31 | protected $version = null; |
| 32 | |
| 33 | /** |
| 34 | * |
| 35 | */ |
| 36 | public function __construct() { |
| 37 | $this->api = pods_api(); |
| 38 | |
| 39 | $this->get_tables(); |
| 40 | $this->get_progress(); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * @param null $_blog_id Blog ID to install. |
| 45 | */ |
| 46 | public function install( $_blog_id = null ) { |
| 47 | /** |
| 48 | * @var $wpdb WPDB |
| 49 | */ |
| 50 | global $wpdb; |
| 51 | |
| 52 | // Switch DB table prefixes |
| 53 | if ( null !== $_blog_id && $_blog_id !== $wpdb->blogid ) { |
| 54 | switch_to_blog( pods_absint( $_blog_id ) ); |
| 55 | } else { |
| 56 | $_blog_id = null; |
| 57 | } |
| 58 | |
| 59 | $pods_version = get_option( 'pods_version' ); |
| 60 | |
| 61 | /** |
| 62 | * Allow hooking in before Pods install is run. |
| 63 | * |
| 64 | * @param string $current_version The current version of Pods installed. |
| 65 | * @param string $new_version The new version of Pods being installed. |
| 66 | * @param int|null $blog_id The blog ID being installed on, or null for |
| 67 | */ |
| 68 | do_action( 'pods_install', PODS_VERSION, $pods_version, $_blog_id ); |
| 69 | |
| 70 | /** |
| 71 | * Allow filtering of whether the Pods SQL installation should be run. Return false to bypass. |
| 72 | * |
| 73 | * @param bool $run Whether the Pods SQL installation should be run. |
| 74 | * @param string $current_version The current version of Pods installed. |
| 75 | * @param string $new_version The new version of Pods being installed. |
| 76 | * @param int|null $blog_id The blog ID being installed on, or null for |
| 77 | */ |
| 78 | $run = apply_filters( 'pods_install_run', true, PODS_VERSION, $pods_version, $_blog_id ); |
| 79 | |
| 80 | if ( false !== $run && ! pods_tableless() && 0 === (int) pods_v( 'pods_bypass_install' ) ) { |
| 81 | $sql = file_get_contents( PODS_DIR . 'sql/dump.sql' ); |
| 82 | $sql = apply_filters( 'pods_install_sql', $sql, PODS_VERSION, $pods_version, $_blog_id ); |
| 83 | |
| 84 | $charset_collate = 'DEFAULT CHARSET utf8'; |
| 85 | |
| 86 | if ( ! empty( $wpdb->charset ) ) { |
| 87 | $charset_collate = "DEFAULT CHARSET {$wpdb->charset}"; |
| 88 | } |
| 89 | |
| 90 | if ( ! empty( $wpdb->collate ) ) { |
| 91 | $charset_collate .= " COLLATE {$wpdb->collate}"; |
| 92 | } |
| 93 | |
| 94 | if ( 'DEFAULT CHARSET utf8' !== $charset_collate ) { |
| 95 | $sql = str_replace( 'DEFAULT CHARSET utf8', $charset_collate, $sql ); |
| 96 | } |
| 97 | |
| 98 | $sql = explode( ";\n", str_replace( [ "\r", 'wp_' ], [ "\n", $wpdb->prefix ], $sql ) ); |
| 99 | $sql = array_map( 'trim', $sql ); |
| 100 | $sql = array_filter( $sql ); |
| 101 | |
| 102 | foreach ( $sql as $query ) { |
| 103 | pods_query( $query, 'Cannot setup SQL tables' ); |
| 104 | } |
| 105 | |
| 106 | // Auto activate component. |
| 107 | if ( ! PodsInit::$components ) { |
| 108 | if ( ! pods_light() ) { |
| 109 | PodsInit::$components = pods_components(); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | if ( PodsInit::$components ) { |
| 114 | PodsInit::$components->activate_component( 'templates' ); |
| 115 | PodsInit::$components->activate_component( 'migrate-packages' ); |
| 116 | } |
| 117 | }//end if |
| 118 | |
| 119 | /** |
| 120 | * Allow hooking in after Pods install is run. |
| 121 | * |
| 122 | * @param string $current_version The current version of Pods installed. |
| 123 | * @param string $new_version The new version of Pods being installed. |
| 124 | * @param int|null $blog_id The blog ID being installed on, or null for |
| 125 | */ |
| 126 | do_action( 'pods_install_post', PODS_VERSION, $pods_version, $_blog_id ); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Handle dbDelta for Pods tables. |
| 131 | * |
| 132 | * @since 2.8.9 |
| 133 | */ |
| 134 | public function delta_tables() { |
| 135 | global $wpdb; |
| 136 | |
| 137 | if ( pods_tableless() ) { |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | $pods_version = get_option( 'pods_version' ); |
| 142 | |
| 143 | $sql = file_get_contents( PODS_DIR . 'sql/dump.sql' ); |
| 144 | $sql = apply_filters( 'pods_install_sql', $sql, PODS_VERSION, $pods_version, get_current_blog_id() ); |
| 145 | |
| 146 | $charset_collate = 'DEFAULT CHARSET utf8'; |
| 147 | |
| 148 | if ( ! empty( $wpdb->charset ) ) { |
| 149 | $charset_collate = "DEFAULT CHARSET {$wpdb->charset}"; |
| 150 | } |
| 151 | |
| 152 | if ( ! empty( $wpdb->collate ) ) { |
| 153 | $charset_collate .= " COLLATE {$wpdb->collate}"; |
| 154 | } |
| 155 | |
| 156 | if ( 'DEFAULT CHARSET utf8' !== $charset_collate ) { |
| 157 | $sql = str_replace( 'DEFAULT CHARSET utf8', $charset_collate, $sql ); |
| 158 | } |
| 159 | |
| 160 | $sql = explode( ";\n", str_replace( [ "\r", 'wp_' ], [ "\n", $wpdb->prefix ], $sql ) ); |
| 161 | |
| 162 | // Remove empty lines and queries. |
| 163 | $sql = array_map( 'trim', $sql ); |
| 164 | $sql = array_filter( $sql ); |
| 165 | |
| 166 | // dbDelta will handle what we need. |
| 167 | $sql = str_replace( 'CREATE TABLE IF NOT EXISTS', 'CREATE TABLE', $sql ); |
| 168 | |
| 169 | if ( empty( $sql ) ) { |
| 170 | return; |
| 171 | } |
| 172 | |
| 173 | require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 174 | |
| 175 | pods_debug( $sql ); |
| 176 | |
| 177 | pods_debug( dbDelta( $sql ) ); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * |
| 182 | */ |
| 183 | public function get_tables() { |
| 184 | /** |
| 185 | * @var $wpdb WPDB |
| 186 | */ |
| 187 | global $wpdb; |
| 188 | |
| 189 | $tables = $wpdb->get_results( "SHOW TABLES LIKE '{$wpdb->prefix}pod%'", ARRAY_N ); |
| 190 | |
| 191 | if ( ! empty( $tables ) ) { |
| 192 | foreach ( $tables as $table ) { |
| 193 | $this->tables[] = $table[0]; |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * |
| 200 | */ |
| 201 | public function get_progress() { |
| 202 | $methods = get_class_methods( $this ); |
| 203 | |
| 204 | foreach ( $methods as $method ) { |
| 205 | if ( 0 === strpos( $method, 'migrate_' ) ) { |
| 206 | $this->progress[ str_replace( 'migrate_', '', $method ) ] = false; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | if ( empty( $this->version ) ) { |
| 211 | return; |
| 212 | } |
| 213 | |
| 214 | $progress = (array) get_option( 'pods_framework_upgrade_' . str_replace( '.', '_', $this->version ), [] ); |
| 215 | |
| 216 | if ( ! empty( $progress ) ) { |
| 217 | $this->progress = array_merge( $this->progress, $progress ); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * @param $params |
| 223 | * |
| 224 | * @return mixed|void |
| 225 | */ |
| 226 | public function ajax( $params ) { |
| 227 | if ( ! isset( $params->step ) ) { |
| 228 | return pods_error( __( 'Invalid upgrade process.', 'pods' ) ); |
| 229 | } |
| 230 | |
| 231 | if ( ! isset( $params->type ) ) { |
| 232 | return pods_error( __( 'Invalid upgrade method.', 'pods' ) ); |
| 233 | } |
| 234 | |
| 235 | if ( ! method_exists( $this, $params->step . '_' . $params->type ) ) { |
| 236 | return pods_error( __( 'Upgrade method not found.', 'pods' ) ); |
| 237 | } |
| 238 | |
| 239 | return call_user_func( [ $this, $params->step . '_' . $params->type ], $params ); |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * @param $method |
| 244 | * @param $v |
| 245 | * @param null $x |
| 246 | */ |
| 247 | public function update_progress( $method, $v, $x = null ) { |
| 248 | if ( empty( $this->version ) ) { |
| 249 | return; |
| 250 | } |
| 251 | |
| 252 | $method = str_replace( 'migrate_', '', $method ); |
| 253 | |
| 254 | if ( null !== $x ) { |
| 255 | $this->progress[ $method ][ $x ] = (boolean) $v; |
| 256 | } else { |
| 257 | $this->progress[ $method ] = $v; |
| 258 | } |
| 259 | |
| 260 | update_option( 'pods_framework_upgrade_' . str_replace( '.', '_', $this->version ), $this->progress ); |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * @param $method |
| 265 | * @param null $x |
| 266 | * |
| 267 | * @return bool |
| 268 | */ |
| 269 | public function check_progress( $method, $x = null ) { |
| 270 | $method = str_replace( 'migrate_', '', $method ); |
| 271 | |
| 272 | if ( isset( $this->progress[ $method ] ) ) { |
| 273 | if ( null === $x ) { |
| 274 | return $this->progress[ $method ]; |
| 275 | } elseif ( isset( $this->progress[ $method ][ $x ] ) ) { |
| 276 | return (boolean) $this->progress[ $method ][ $x ]; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | return false; |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * |
| 285 | */ |
| 286 | public function upgraded() { |
| 287 | if ( empty( $this->version ) ) { |
| 288 | return; |
| 289 | } |
| 290 | |
| 291 | $upgraded = get_option( 'pods_framework_upgraded' ); |
| 292 | |
| 293 | if ( empty( $upgraded ) || ! is_array( $upgraded ) ) { |
| 294 | $upgraded = []; |
| 295 | } |
| 296 | |
| 297 | delete_option( 'pods_framework_upgrade_' . str_replace( '.', '_', $this->version ) ); |
| 298 | |
| 299 | if ( ! in_array( $this->version, $upgraded, true ) ) { |
| 300 | $upgraded[] = $this->version; |
| 301 | } |
| 302 | |
| 303 | update_option( 'pods_framework_upgraded', $upgraded ); |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * |
| 308 | */ |
| 309 | public function cleanup() { |
| 310 | /** |
| 311 | * @var $wpdb WPDB |
| 312 | */ |
| 313 | global $wpdb; |
| 314 | |
| 315 | foreach ( $this->tables as $table ) { |
| 316 | if ( false !== strpos( $table, "{$wpdb->prefix}pod_" ) || "{$wpdb->prefix}pod" === $table ) { |
| 317 | pods_query( "DROP TABLE `{$table}`", false ); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | delete_option( 'pods_roles' ); |
| 322 | delete_option( 'pods_version' ); |
| 323 | delete_option( 'pods_framework_upgrade_2_0' ); |
| 324 | delete_option( 'pods_framework_upgrade_2_0_sister_ids' ); |
| 325 | delete_option( 'pods_framework_upgraded_1_x' ); |
| 326 | |
| 327 | delete_option( 'pods_disable_file_browser' ); |
| 328 | delete_option( 'pods_files_require_login' ); |
| 329 | delete_option( 'pods_files_require_login_cap' ); |
| 330 | delete_option( 'pods_disable_file_upload' ); |
| 331 | delete_option( 'pods_upload_require_login' ); |
| 332 | delete_option( 'pods_upload_require_login_cap' ); |
| 333 | |
| 334 | pods_query( "DELETE FROM `@wp_postmeta` WHERE `meta_key` LIKE '_pods_1x_%'" ); |
| 335 | } |
| 336 | } |
| 337 |