is_target_screen() ) {
return;
}
if ( ! $this->should_offer() ) {
return;
}
$switch_url = admin_url( 'admin-post.php' );
$dismiss_url = wp_nonce_url(
admin_url( 'admin-post.php?action=' . self::DISMISS_ACTION ),
self::DISMISS_ACTION
);
?>
';
echo esc_html__( 'Switched. Updates now come from WordPress.org. You can delete the old copy from the Plugins screen.', 'ai-workflow-automation-lite' );
echo '
';
return;
}
if ( 'error' === $status ) {
$message = get_transient( self::MSG_PREFIX . get_current_user_id() );
delete_transient( self::MSG_PREFIX . get_current_user_id() );
echo '';
echo esc_html__( 'The switch to WordPress.org could not be completed. Your current plugin is unchanged.', 'ai-workflow-automation-lite' );
if ( is_string( $message ) && '' !== $message ) {
echo ' ' . esc_html( $message );
}
echo '
';
}
}
public function handle_switch() {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'You are not allowed to do this.', 'ai-workflow-automation-lite' ), 403 );
}
check_admin_referer( self::SWITCH_ACTION );
$this->ensure_plugin_functions();
if ( $this->lite_is_installed() ) {
if ( is_plugin_active( self::LITE_BASENAME ) ) {
$this->redirect_with_status( 'already' );
}
$activated = activate_plugin( self::LITE_BASENAME );
if ( is_wp_error( $activated ) ) {
$this->redirect_with_status( 'error', $activated->get_error_message() );
}
$this->redirect_with_status( 'success' );
}
$info = $this->get_remote_info();
$download = ( $info && ! empty( $info->download_link ) ) ? $info->download_link : '';
if ( '' === $download ) {
$this->redirect_with_status( 'error', __( 'The WordPress.org download URL could not be determined.', 'ai-workflow-automation-lite' ) );
}
require_once ABSPATH . 'wp-admin/includes/file.php';
// Without direct filesystem access an install needs FTP/SSH credentials
// we cannot collect here; hand off to the core install screen instead of
// half-completing the switch.
if ( 'direct' !== get_filesystem_method() ) {
wp_safe_redirect( $this->core_install_url() );
exit;
}
$result = $this->install_and_activate( $download );
if ( is_wp_error( $result ) ) {
$this->redirect_with_status( 'error', $result->get_error_message() );
}
$this->redirect_with_status( 'success' );
}
public function handle_dismiss() {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'You are not allowed to do this.', 'ai-workflow-automation-lite' ), 403 );
}
check_admin_referer( self::DISMISS_ACTION );
update_user_meta( get_current_user_id(), self::DISMISS_META, $this->current_major() );
$target = wp_get_referer();
if ( ! $target ) {
$target = admin_url( 'plugins.php' );
}
wp_safe_redirect( $target );
exit;
}
private function install_and_activate( $download ) {
require_once ABSPATH . 'wp-admin/includes/file.php';
require_once ABSPATH . 'wp-admin/includes/misc.php';
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
if ( ! WP_Filesystem() ) {
return new WP_Error( 'fs_unavailable', __( 'The filesystem could not be initialized.', 'ai-workflow-automation-lite' ) );
}
$skin = new WP_Ajax_Upgrader_Skin();
$upgrader = new Plugin_Upgrader( $skin );
$installed = $upgrader->install( $download );
if ( is_wp_error( $installed ) ) {
return $installed;
}
if ( is_wp_error( $skin->result ) ) {
return $skin->result;
}
$skin_errors = $skin->get_errors();
if ( is_wp_error( $skin_errors ) && $skin_errors->has_errors() ) {
return $skin_errors;
}
if ( true !== $installed ) {
return new WP_Error( 'install_failed', __( 'The WordPress.org copy could not be installed.', 'ai-workflow-automation-lite' ) );
}
$activated = activate_plugin( self::LITE_BASENAME );
if ( is_wp_error( $activated ) ) {
return $activated;
}
return true;
}
private function should_offer() {
$this->ensure_plugin_functions();
if ( is_plugin_active( self::LITE_BASENAME ) ) {
return false;
}
if ( $this->is_dismissed() ) {
return false;
}
$info = $this->get_remote_info();
if ( ! $info || empty( $info->version ) ) {
return false;
}
// Downgrade protection: only offer once wp.org serves a version at least
// as new as the installed one.
return version_compare( $info->version, WP_AI_WORKFLOWS_PRO_VERSION, '>=' );
}
private function get_remote_info() {
$cached = get_transient( self::INFO_TRANSIENT );
if ( false !== $cached ) {
return $cached;
}
if ( ! function_exists( 'plugins_api' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
}
$api = plugins_api(
'plugin_information',
array(
'slug' => self::LITE_SLUG,
'fields' => array( 'sections' => false ),
)
);
$data = new stdClass();
$data->version = ( ! is_wp_error( $api ) && ! empty( $api->version ) ) ? $api->version : '';
$data->download_link = ( ! is_wp_error( $api ) && ! empty( $api->download_link ) ) ? $api->download_link : '';
$ttl = ( '' === $data->version ) ? HOUR_IN_SECONDS : 12 * HOUR_IN_SECONDS;
set_transient( self::INFO_TRANSIENT, $data, $ttl );
return $data;
}
private function is_target_screen() {
if ( ! function_exists( 'get_current_screen' ) ) {
return false;
}
$screen = get_current_screen();
if ( ! $screen ) {
return false;
}
$targets = array(
'plugins',
'plugins-network',
'toplevel_page_wp-ai-workflows',
'toplevel_page_wp-ai-workflows-tasks',
);
return in_array( $screen->id, $targets, true );
}
private function is_dismissed() {
$dismissed = (int) get_user_meta( get_current_user_id(), self::DISMISS_META, true );
return $dismissed >= $this->current_major();
}
private function current_major() {
return (int) strtok( WP_AI_WORKFLOWS_PRO_VERSION, '.' );
}
private function lite_is_installed() {
$this->ensure_plugin_functions();
$plugins = get_plugins();
return isset( $plugins[ self::LITE_BASENAME ] );
}
private function core_install_url() {
$url = self_admin_url( 'update.php?action=install-plugin&plugin=' . self::LITE_SLUG );
return wp_nonce_url( $url, 'install-plugin_' . self::LITE_SLUG );
}
private function ensure_plugin_functions() {
if ( ! function_exists( 'get_plugins' ) || ! function_exists( 'is_plugin_active' ) || ! function_exists( 'activate_plugin' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
}
private function redirect_with_status( $status, $message = '' ) {
if ( '' !== $message ) {
set_transient( self::MSG_PREFIX . get_current_user_id(), $message, MINUTE_IN_SECONDS );
}
$target = wp_get_referer();
if ( ! $target ) {
$target = admin_url( 'plugins.php' );
}
$target = add_query_arg( 'wp_ai_wporg_switch', $status, $target );
wp_safe_redirect( $target );
exit;
}
}