v-wordpress-plugin-updater/v-wp-updater/v-wp-updater.php
2026-04-06 02:45:39 -04:00

209 lines
5 KiB
PHP

<?php
/**
* Plugin Name: V WordPress Plugin Updater
* Plugin URI: https://github.com/djav1985/v-wordpress-plugin-updater
* Description: Automated plugin and theme updater for centralized update management.
* Version: 2.0.0
* Author: Vontainment
* Author URI: https://vontainment.com
* License: MIT
* Text Domain: v-wp-updater
*
* Project: UpdateAPI
* Link: https://vontainment.com
*
* @package VWPU
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// Load Composer autoloader.
require_once __DIR__ . '/vendor/autoload.php';
use VWPU\Services\PluginUpdater;
use VWPU\Services\ThemeUpdater;
use VWPU\Helpers\Logger;
use VWPU\Helpers\Options;
// Determine execution context flags up front so conditional bootstrapping can
// avoid loading admin-specific functionality on frontend requests.
$is_admin_context = is_admin();
/**
* Runs on plugin activation.
*
* Calls the main installation function to handle all setup tasks.
*
* @since 1.0.0
* @return void
*/
function vwpu_activate(): void {
if ( ! current_user_can( 'activate_plugins' ) ) {
return;
}
try {
include_once plugin_dir_path( __FILE__ ) . 'install.php';
vwpu_install();
} catch ( Exception $e ) {
Logger::error( 'Activation error', array( 'exception' => $e->getMessage() ) );
}
}
register_activation_hook( __FILE__, 'vwpu_activate' );
/**
* Runs on plugin deactivation.
*
* Clears all scheduled events without deleting stored options so settings persist.
*
* @since 2.0.0
* @return void
*/
function vwpu_deactivate(): void {
if ( ! current_user_can( 'activate_plugins' ) ) {
return;
}
try {
$uninstall_file = plugin_dir_path( __FILE__ ) . 'uninstall.php';
if ( ! file_exists( $uninstall_file ) ) {
Logger::error(
'Deactivation cleanup include missing',
array(
'event' => 'deactivation_cleanup',
'file' => $uninstall_file,
'action' => 'skip_cleanup',
)
);
return;
}
include_once $uninstall_file;
if ( function_exists( 'vwpu_clear_plugin_update_schedule' ) ) {
vwpu_clear_plugin_update_schedule();
} else {
Logger::error(
'Plugin schedule cleanup helper unavailable',
array(
'event' => 'deactivation_cleanup',
'function' => 'vwpu_clear_plugin_update_schedule',
'action' => 'continue',
)
);
}
if ( function_exists( 'vwpu_clear_theme_update_schedule' ) ) {
vwpu_clear_theme_update_schedule();
} else {
Logger::error(
'Theme schedule cleanup helper unavailable',
array(
'event' => 'deactivation_cleanup',
'function' => 'vwpu_clear_theme_update_schedule',
'action' => 'continue',
)
);
}
} catch ( Exception $e ) {
Logger::error( 'Deactivation error', array( 'exception' => $e->getMessage() ) );
}
}
register_deactivation_hook( __FILE__, 'vwpu_deactivate' );
/**
* Runs on plugin uninstall.
*
* Calls the main uninstallation function to handle all cleanup tasks.
*
* @since 1.0.0
* @return void
*/
function vwpu_uninstall(): void {
if ( ! current_user_can( 'activate_plugins' ) ) {
return;
}
try {
$uninstall_file = plugin_dir_path( __FILE__ ) . 'uninstall.php';
if ( ! file_exists( $uninstall_file ) ) {
Logger::error(
'Uninstall cleanup include missing',
array(
'event' => 'uninstall_cleanup',
'file' => $uninstall_file,
'action' => 'skip_cleanup',
)
);
return;
}
include_once $uninstall_file;
if ( function_exists( 'vwpu_uninstall_cleanup' ) ) {
vwpu_uninstall_cleanup();
} else {
Logger::error(
'Uninstall cleanup helper unavailable',
array(
'event' => 'uninstall_cleanup',
'function' => 'vwpu_uninstall_cleanup',
'action' => 'continue',
)
);
}
} catch ( Exception $e ) {
Logger::error( 'Uninstall error', array( 'exception' => $e->getMessage() ) );
}
}
register_uninstall_hook( __FILE__, 'vwpu_uninstall' );
/**
* Sets up dashboard widgets and styles.
*
* Adds custom dashboard widgets and styles for the WordPress admin dashboard.
*
* @since 1.0.0
* @return void
*/
function vwpu_dashboard_setup(): void {
$widgets_dir = plugin_dir_path( __FILE__ ) . 'widgets/';
if ( file_exists( $widgets_dir . 'settings.php' ) ) {
require_once $widgets_dir . 'settings.php';
wp_add_dashboard_widget(
'vwpu_settings_widget',
__( 'V WordPress Updater Settings', 'v-wp-updater' ),
'vwpu_widget_settings_display'
);
}
}
if ( $is_admin_context ) {
add_action( 'wp_dashboard_setup', 'vwpu_dashboard_setup' );
}
/**
* Run theme updater if enabled.
*/
function vwpu_run_theme_updater() {
if ( ! Options::is_true( 'update_themes' ) ) {
return;
}
$theme_updater = new ThemeUpdater();
$theme_updater->run_updates();
}
add_action( 'vwpu_theme_updater_check_updates', 'vwpu_run_theme_updater' );
/**
* Run plugin updater if enabled.
*/
function vwpu_run_plugin_updater() {
if ( ! Options::is_true( 'update_plugins' ) ) {
return;
}
$plugin_updater = new PluginUpdater();
$plugin_updater->run_updates();
}
add_action( 'vwpu_plugin_updater_check_updates', 'vwpu_run_plugin_updater' );