| 1 |
<?php |
| 2 |
/** |
| 3 |
* Surfaces the "database update required" notice to wp-kit's NoticeManager |
| 4 |
* and runs the upgrade when AdminNotice fires its admin-ajax action. |
| 5 |
* |
| 6 |
* @package Texty\Migrations |
| 7 |
* @since TEXTY_VERSION |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace Texty\Migrations; |
| 11 |
|
| 12 |
use Throwable; |
| 13 |
use WeDevs\WPKit\AdminNotification\Contracts\NoticeProviderInterface; |
| 14 |
use WeDevs\WPKit\AdminNotification\Notice; |
| 15 |
|
| 16 |
defined( 'ABSPATH' ) || exit; |
| 17 |
|
| 18 |
/** |
| 19 |
* Migration notice provider. |
| 20 |
*/ |
| 21 |
class NoticeProvider implements NoticeProviderInterface { |
| 22 |
|
| 23 |
/** |
| 24 |
* Ajax action name for the upgrade button. |
| 25 |
*/ |
| 26 |
public const AJAX_ACTION = 'texty_run_migration'; |
| 27 |
|
| 28 |
/** |
| 29 |
* Constructor — registers the admin-ajax handler that AdminNotice's |
| 30 |
* `ajax_data` body posts to. |
| 31 |
* |
| 32 |
* @since TEXTY_VERSION |
| 33 |
*/ |
| 34 |
public function __construct() { |
| 35 |
add_action( 'wp_ajax_' . self::AJAX_ACTION, [ $this, 'handle_upgrade' ] ); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Return notices to surface in the admin. |
| 40 |
* |
| 41 |
* @return array |
| 42 |
* @since TEXTY_VERSION |
| 43 |
*/ |
| 44 |
public function get_notices(): array { |
| 45 |
$migrations = texty()->migrations(); |
| 46 |
|
| 47 |
if ( ! $migrations->is_upgrade_required() ) { |
| 48 |
return []; |
| 49 |
} |
| 50 |
|
| 51 |
$registry = $migrations->get_manager()->get_registry(); |
| 52 |
|
| 53 |
$notice = new Notice( |
| 54 |
[ |
| 55 |
'key' => 'texty_migration_required', |
| 56 |
'type' => 'warning', |
| 57 |
'title' => __( 'Texty database update required', 'texty' ), |
| 58 |
'description' => sprintf( |
| 59 |
/* translators: 1: target plugin version, 2: current DB schema version */ |
| 60 |
__( 'Texty %1$s needs to update your database from version %2$s. The update is safe to re-run and usually finishes in a few seconds.', 'texty' ), |
| 61 |
esc_html( $registry->get_plugin_version() ), |
| 62 |
esc_html( $registry->get_db_installed_version() ) |
| 63 |
), |
| 64 |
'priority' => 5, |
| 65 |
'scope' => 'local', |
| 66 |
'is_dismissible' => false, |
| 67 |
'actions' => [ |
| 68 |
[ |
| 69 |
'type' => 'primary', |
| 70 |
'text' => __( 'Update database', 'texty' ), |
| 71 |
'loading_text' => __( 'Updating…', 'texty' ), |
| 72 |
'reload' => true, |
| 73 |
'ajax_data' => [ |
| 74 |
'action' => self::AJAX_ACTION, |
| 75 |
'nonce' => wp_create_nonce( self::AJAX_ACTION ), |
| 76 |
], |
| 77 |
], |
| 78 |
], |
| 79 |
] |
| 80 |
); |
| 81 |
|
| 82 |
return [ $notice ]; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Ajax handler — runs pending migrations. |
| 87 |
* |
| 88 |
* Mirrors wp-kit's MigrationRESTController guards (capability, ongoing |
| 89 |
* upgrade, required check) so both entry points stay consistent. |
| 90 |
* |
| 91 |
* @return void |
| 92 |
* @since TEXTY_VERSION |
| 93 |
*/ |
| 94 |
public function handle_upgrade(): void { |
| 95 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 96 |
wp_send_json_error( [ 'message' => __( 'You do not have permission.', 'texty' ) ], 403 ); |
| 97 |
} |
| 98 |
|
| 99 |
check_ajax_referer( self::AJAX_ACTION, 'nonce' ); |
| 100 |
|
| 101 |
$manager = texty()->migrations()->get_manager(); |
| 102 |
|
| 103 |
if ( $manager->has_ongoing_process() ) { |
| 104 |
wp_send_json_error( [ 'message' => __( 'Upgrade already in progress.', 'texty' ) ], 409 ); |
| 105 |
} |
| 106 |
|
| 107 |
if ( ! $manager->is_upgrade_required() ) { |
| 108 |
wp_send_json_success( [ 'message' => __( 'No upgrade required.', 'texty' ) ] ); |
| 109 |
} |
| 110 |
|
| 111 |
try { |
| 112 |
$manager->do_upgrade(); |
| 113 |
} catch ( Throwable $e ) { |
| 114 |
error_log( sprintf( 'Texty migration failed: %s', $e->getMessage() ) ); |
| 115 |
wp_send_json_error( [ 'message' => $e->getMessage() ], 500 ); |
| 116 |
} |
| 117 |
|
| 118 |
wp_send_json_success( [ 'message' => __( 'Database updated successfully.', 'texty' ) ] ); |
| 119 |
} |
| 120 |
} |
| 121 |
|