Files
homeproz/wp-content/plugins/mls-by-hansonxyz/includes/class-mls-activator.php
T
Hanson.xyz Dev acc8ac87a0 wip
2026-01-04 17:50:08 -06:00

138 lines
3.8 KiB
PHP
Executable File

<?php
/**
* Plugin activator class
*/
if (!defined('ABSPATH')) {
exit;
}
class MLS_Activator {
/**
* Run on plugin activation
*/
public static function activate() {
// Create database tables
self::create_tables();
// Set default options
self::set_defaults();
// Schedule cron events if auto-sync enabled
self::schedule_cron();
// Create upload directory
self::create_upload_dir();
// Store activation time
update_option('mls_activated_at', current_time('mysql'));
// Flush rewrite rules
flush_rewrite_rules();
}
/**
* Create database tables
*/
public static function create_tables() {
MLS_DB::create_tables();
update_option('mls_db_version', MLS_DB_VERSION);
}
/**
* Set default options
*/
private static function set_defaults() {
$defaults = array(
'api_url' => defined('MLSGRID_API_URL') ? MLSGRID_API_URL : 'https://api.mlsgrid.com/v2',
'originating_system' => 'northstar',
'auto_sync_enabled' => false,
'sync_interval' => 'hourly',
'sync_media' => true,
);
$existing = get_option(MLS_Options::OPTION_KEY, array());
$merged = wp_parse_args($existing, $defaults);
update_option(MLS_Options::OPTION_KEY, $merged);
}
/**
* Schedule cron events
*/
private static function schedule_cron() {
// Register custom cron intervals
add_filter('cron_schedules', array(__CLASS__, 'add_cron_intervals'));
// Only schedule if auto-sync is enabled
$options = get_option(MLS_Options::OPTION_KEY, array());
if (empty($options['auto_sync_enabled'])) {
return;
}
$interval = !empty($options['sync_interval']) ? $options['sync_interval'] : 'hourly';
if (!wp_next_scheduled('mls_sync_properties')) {
wp_schedule_event(time(), $interval, 'mls_sync_properties');
}
if (!wp_next_scheduled('mls_sync_media')) {
wp_schedule_event(time() + 1800, $interval, 'mls_sync_media');
}
}
/**
* Add custom cron intervals
*
* @param array $schedules Existing schedules
* @return array Modified schedules
*/
public static function add_cron_intervals($schedules) {
$schedules['every_30_minutes'] = array(
'interval' => 1800,
'display' => 'Every 30 Minutes',
);
$schedules['every_2_hours'] = array(
'interval' => 7200,
'display' => 'Every 2 Hours',
);
$schedules['every_6_hours'] = array(
'interval' => 21600,
'display' => 'Every 6 Hours',
);
$schedules['every_12_hours'] = array(
'interval' => 43200,
'display' => 'Every 12 Hours',
);
return $schedules;
}
/**
* Create upload directory for MLS media
*/
private static function create_upload_dir() {
$upload_dir = wp_upload_dir();
$mls_dir = $upload_dir['basedir'] . '/mls-listings';
if (!file_exists($mls_dir)) {
wp_mkdir_p($mls_dir);
// Create .htaccess to prevent directory listing
$htaccess = $mls_dir . '/.htaccess';
if (!file_exists($htaccess)) {
file_put_contents($htaccess, "Options -Indexes\n");
}
// Create index.php for extra protection
$index = $mls_dir . '/index.php';
if (!file_exists($index)) {
file_put_contents($index, "<?php\n// Silence is golden.\n");
}
}
}
}