Tables
1 year ago
CartLineItemDetailsMigrationService.php
1 day ago
GeneralMigration.php
1 year ago
MigrationsServiceProvider.php
1 month ago
ProductPageMigrationService.php
1 day ago
RewriteRulesMigrationService.php
1 year ago
Table.php
4 years ago
ThemeMigrationService.php
5 months ago
UpdateMigrationServiceProvider.php
2 months ago
UserMetaMigrationsService.php
4 years ago
VersionMigration.php
1 day ago
WebhookMigrationsService.php
1 year ago
VersionMigration.php
167 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Database; |
| 4 | |
| 5 | /** |
| 6 | * A migration that will run each time the version of the plugin changes. |
| 7 | */ |
| 8 | abstract class VersionMigration { |
| 9 | /** |
| 10 | * The key for the migration. |
| 11 | * |
| 12 | * @var string |
| 13 | */ |
| 14 | protected $migration_key = 'surecart_migration_version'; |
| 15 | |
| 16 | /** |
| 17 | * Set from run() when a post update fails; complete() then defers marking |
| 18 | * so the next admin_init retries. |
| 19 | * |
| 20 | * @var bool |
| 21 | */ |
| 22 | protected $failed = false; |
| 23 | |
| 24 | /** |
| 25 | * Failed runs allowed before giving up — otherwise a permanently failing |
| 26 | * save would retry on every admin_init forever. |
| 27 | * |
| 28 | * @var int |
| 29 | */ |
| 30 | const MAX_FAILED_ATTEMPTS = 5; |
| 31 | |
| 32 | /** |
| 33 | * Run on init. |
| 34 | * |
| 35 | * @return void |
| 36 | */ |
| 37 | public function bootstrap() { |
| 38 | add_action( 'admin_init', [ $this, 'maybeRun' ] ); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Get the current version of the plugin. |
| 43 | * |
| 44 | * @return string |
| 45 | */ |
| 46 | public function currentVersion() { |
| 47 | return \SureCart::plugin()->version(); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Maybe let's run the migration. |
| 52 | * |
| 53 | * @return void |
| 54 | */ |
| 55 | public function maybeRun() { |
| 56 | if ( ! $this->shouldMigrate() ) { |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | // run the migration. |
| 61 | $this->run(); |
| 62 | |
| 63 | // update the migration complete on admin_init complete, after all migrations have run. |
| 64 | add_action( 'admin_init', [ $this, 'complete' ], 999999 ); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Should we run this migration? |
| 69 | * |
| 70 | * @return boolean |
| 71 | */ |
| 72 | public function shouldMigrate(): bool { |
| 73 | // check if we already have done this migration. |
| 74 | return version_compare( $this->currentVersion(), $this->getLastMigrationVersion(), '!=' ); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Run the migration |
| 79 | * |
| 80 | * @return void |
| 81 | */ |
| 82 | protected function run() { |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Save migrated post content without kses mangling it. |
| 87 | * |
| 88 | * Migrations re-save content that already passed a privileged save plus |
| 89 | * plugin-generated block markup, so kses must not run — admin_init also |
| 90 | * fires on logged-out admin-ajax requests, where kses would strip <style> |
| 91 | * and <script> tags. wp_update_post() also expects slashed data, so we |
| 92 | * slash to keep backslashes (e.g. CSS "\f101", block attribute JSON) intact. |
| 93 | * |
| 94 | * Callers must only pass content derived from what is already stored plus |
| 95 | * plugin-generated markup — never fresh user, customer, or webhook input, |
| 96 | * which would inherit the kses bypass. |
| 97 | * |
| 98 | * @param int $post_id The post to update. |
| 99 | * @param string $content The migrated post content. |
| 100 | * @return int|\WP_Error The post ID on success, WP_Error on failure. |
| 101 | */ |
| 102 | protected function updatePostContent( int $post_id, string $content ) { |
| 103 | // remember the actual prior state — kses_init() on restore would recompute |
| 104 | // from capability and undo a kses another plugin forced on. |
| 105 | $had_kses = false !== has_filter( 'content_save_pre', 'wp_filter_post_kses' ); |
| 106 | |
| 107 | // disables kses process-wide, so scope it per save (don't hoist around |
| 108 | // callers' loops) — the toggle is cheap, the narrow window is the point. |
| 109 | kses_remove_filters(); |
| 110 | |
| 111 | try { |
| 112 | $result = wp_update_post( |
| 113 | wp_slash( |
| 114 | array( |
| 115 | 'ID' => $post_id, |
| 116 | 'post_content' => $content, |
| 117 | ) |
| 118 | ), |
| 119 | true |
| 120 | ); |
| 121 | } finally { |
| 122 | // a throwing save_post hook must not leave kses disabled for the rest of the request. |
| 123 | if ( $had_kses ) { |
| 124 | kses_init_filters(); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | return $result; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Store the current plugin version when complete. |
| 133 | * |
| 134 | * A failed run defers completion so the next admin_init retries; after |
| 135 | * MAX_FAILED_ATTEMPTS we mark complete anyway and stop. The counter resets |
| 136 | * on completion, so the next version bump gets a fresh retry budget. |
| 137 | * |
| 138 | * @return void |
| 139 | */ |
| 140 | public function complete() { |
| 141 | $attempts_key = $this->migration_key . '_failed_attempts'; |
| 142 | |
| 143 | if ( $this->failed ) { |
| 144 | $attempts = (int) get_option( $attempts_key, 0 ) + 1; |
| 145 | |
| 146 | if ( $attempts < self::MAX_FAILED_ATTEMPTS ) { |
| 147 | update_option( $attempts_key, $attempts, false ); |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | error_log( 'SureCart: ' . $this->migration_key . ' migration gave up after ' . $attempts . ' failed attempts — some content may still be on the old markup.' ); |
| 152 | } |
| 153 | |
| 154 | delete_option( $attempts_key ); |
| 155 | update_option( $this->migration_key, $this->currentVersion(), false ); // don't autoload since we are only doing this on the admin. |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Get the last version there was a migration. |
| 160 | * |
| 161 | * @return string |
| 162 | */ |
| 163 | public function getLastMigrationVersion() { |
| 164 | return get_option( $this->migration_key, '0.0.0' ); |
| 165 | } |
| 166 | } |
| 167 |