| 1 |
<?php |
| 2 |
/** |
| 3 |
* The abstract migration class for ThemeIsle SDK. |
| 4 |
* |
| 5 |
* @package ThemeIsleSDK |
| 6 |
* @subpackage Modules |
| 7 |
* @copyright Copyright (c) 2024, Themeisle |
| 8 |
* @license http://opensource.org/licenses/gpl-3.0.php GNU Public License |
| 9 |
* @since 3.3.50 |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace ThemeisleSDK\Modules; |
| 13 |
|
| 14 |
// Exit if accessed directly. |
| 15 |
if ( ! defined( 'ABSPATH' ) ) { |
| 16 |
exit; |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Abstract base class for SDK migrations. |
| 21 |
* |
| 22 |
* Migration files should return an anonymous class instance extending this class: |
| 23 |
* |
| 24 |
* return new class extends \ThemeisleSDK\Modules\Abstract_Migration { |
| 25 |
* public function up() { ... } |
| 26 |
* }; |
| 27 |
*/ |
| 28 |
abstract class Abstract_Migration { |
| 29 |
/** |
| 30 |
* WordPress database object. |
| 31 |
* |
| 32 |
* @var \wpdb |
| 33 |
*/ |
| 34 |
protected $wpdb; |
| 35 |
|
| 36 |
/** |
| 37 |
* WordPress table prefix. |
| 38 |
* |
| 39 |
* @var string |
| 40 |
*/ |
| 41 |
protected $prefix; |
| 42 |
|
| 43 |
/** |
| 44 |
* WordPress charset and collation string. |
| 45 |
* |
| 46 |
* @var string |
| 47 |
*/ |
| 48 |
protected $charset_collate; |
| 49 |
|
| 50 |
/** |
| 51 |
* Constructor. Populates database helpers. |
| 52 |
*/ |
| 53 |
public function __construct() { |
| 54 |
global $wpdb; |
| 55 |
$this->wpdb = $wpdb; |
| 56 |
$this->prefix = $wpdb->prefix; |
| 57 |
$this->charset_collate = $wpdb->get_charset_collate(); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Run the migration. |
| 62 |
*/ |
| 63 |
abstract public function up(); |
| 64 |
|
| 65 |
/** |
| 66 |
* Reverse the migration. |
| 67 |
* |
| 68 |
* Override in concrete migrations to undo what up() did. Called by |
| 69 |
* Migrator::rollback() — never invoked automatically. |
| 70 |
* |
| 71 |
* @return void |
| 72 |
*/ |
| 73 |
public function down() { |
| 74 |
// No-op by default. Override to implement rollback logic. |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Determine whether this migration should run. |
| 79 |
* |
| 80 |
* Override to add a custom idempotency check beyond name-based tracking. |
| 81 |
* Return false to skip the migration without recording it. |
| 82 |
* |
| 83 |
* @return bool |
| 84 |
*/ |
| 85 |
public function should_run() { |
| 86 |
return true; |
| 87 |
} |
| 88 |
} |
| 89 |
|