521 lines
13 KiB
PHP
521 lines
13 KiB
PHP
<?php
|
|
/**
|
|
* HSSM Main Class
|
|
*
|
|
* Main plugin class that orchestrates the Hi-School Slide Manager functionality
|
|
*
|
|
* @package Hi_School_Slide_Manager
|
|
* @since 1.0.0
|
|
*/
|
|
|
|
// Prevent direct access
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit( 'Direct access forbidden.' );
|
|
}
|
|
|
|
/**
|
|
* HSSM Main Class
|
|
*
|
|
* Core plugin functionality and initialization
|
|
*/
|
|
class HSSM_Main {
|
|
|
|
/**
|
|
* Plugin instance
|
|
*
|
|
* @since 1.0.0
|
|
* @var HSSM_Main
|
|
*/
|
|
private static $instance = null;
|
|
|
|
/**
|
|
* Slides CPT manager
|
|
*
|
|
* @since 1.0.0
|
|
* @var HSSM_Slides_CPT
|
|
*/
|
|
public $slides_cpt;
|
|
|
|
/**
|
|
* REST API manager
|
|
*
|
|
* @since 1.0.0
|
|
* @var HSSM_REST_API
|
|
*/
|
|
public $rest_api;
|
|
|
|
/**
|
|
* Frontend manager
|
|
*
|
|
* @since 1.0.0
|
|
* @var HSSM_Frontend
|
|
*/
|
|
public $frontend;
|
|
|
|
/**
|
|
* Shortcode manager
|
|
*
|
|
* @since 1.1.0
|
|
* @var HSSM_Shortcode
|
|
*/
|
|
public $shortcode;
|
|
|
|
/**
|
|
* Admin manager
|
|
*
|
|
* @since 1.0.0
|
|
* @var HSSM_Admin
|
|
*/
|
|
public $admin;
|
|
|
|
/**
|
|
* Email notifications manager
|
|
*
|
|
* @since 1.0.0
|
|
* @var HSSM_Email_Notifications
|
|
*/
|
|
public $email_notifications;
|
|
|
|
/**
|
|
* Constructor
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
private function __construct() {
|
|
// Private constructor to enforce singleton
|
|
}
|
|
|
|
/**
|
|
* Get singleton instance
|
|
*
|
|
* @since 1.0.0
|
|
* @return HSSM_Main
|
|
*/
|
|
public static function get_instance() {
|
|
if ( null === self::$instance ) {
|
|
self::$instance = new self();
|
|
}
|
|
return self::$instance;
|
|
}
|
|
|
|
/**
|
|
* Initialize the plugin
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
public function init() {
|
|
// Initialize core components
|
|
$this->init_components();
|
|
|
|
// Hook into WordPress
|
|
$this->init_hooks();
|
|
|
|
// Load text domain for translations
|
|
$this->load_textdomain();
|
|
}
|
|
|
|
/**
|
|
* Initialize plugin components
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
private function init_components() {
|
|
// Manually require class files (since they don't follow exact autoloader pattern)
|
|
require_once HSSM_PLUGIN_DIR . 'includes/class-hssm-slides-cpt.php';
|
|
require_once HSSM_PLUGIN_DIR . 'includes/class-hssm-rest-api.php';
|
|
require_once HSSM_PLUGIN_DIR . 'includes/class-hssm-admin.php';
|
|
require_once HSSM_PLUGIN_DIR . 'includes/class-hssm-frontend.php';
|
|
require_once HSSM_PLUGIN_DIR . 'includes/class-hssm-notifications.php';
|
|
require_once HSSM_PLUGIN_DIR . 'includes/class-hssm-api-client.php';
|
|
require_once HSSM_PLUGIN_DIR . 'includes/class-hssm-slider.php';
|
|
require_once HSSM_PLUGIN_DIR . 'includes/class-hssm-shortcode.php';
|
|
|
|
// Initialize slides custom post type
|
|
$this->slides_cpt = new HSSM_Slides_CPT();
|
|
|
|
// Initialize REST API endpoints
|
|
$this->rest_api = new HSSM_REST_API();
|
|
|
|
// Initialize admin functionality
|
|
$this->admin = new HSSM_Admin();
|
|
|
|
// Initialize frontend functionality
|
|
$this->frontend = new HSSM_Frontend();
|
|
|
|
// Initialize shortcode
|
|
$this->shortcode = new HSSM_Shortcode();
|
|
|
|
// Initialize email notifications
|
|
$this->email_notifications = new HSSM_Notifications();
|
|
}
|
|
|
|
/**
|
|
* Initialize WordPress hooks
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
private function init_hooks() {
|
|
$plugin_mode = get_option( 'hssm_plugin_mode', 'manager' );
|
|
|
|
// Core WordPress hooks
|
|
if ( 'manager' === $plugin_mode ) {
|
|
add_action( 'init', array( $this, 'register_post_types' ) );
|
|
add_action( 'init', array( $this, 'register_taxonomies' ) );
|
|
add_action( 'rest_api_init', array( $this, 'register_rest_routes' ) );
|
|
}
|
|
|
|
// Frontend hooks
|
|
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend_assets' ), 20 );
|
|
add_action( 'template_redirect', array( $this, 'handle_frontend_pages' ) );
|
|
|
|
// Admin hooks
|
|
add_action( 'admin_init', array( $this, 'admin_init' ) );
|
|
add_action( 'admin_menu', array( $this, 'add_admin_menus' ) );
|
|
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_assets' ) );
|
|
|
|
// AJAX hooks for authenticated users
|
|
// Note: Main AJAX handlers are in HSSM_Frontend class
|
|
add_action( 'wp_ajax_hssm_upload_image', array( $this, 'ajax_upload_image' ) );
|
|
add_action( 'wp_ajax_hssm_preview_slides', array( $this, 'ajax_preview_slides' ) );
|
|
add_action( 'wp_ajax_hssm_send_preview_email', array( $this, 'ajax_send_preview_email' ) );
|
|
|
|
// Cron hooks for scheduled slides
|
|
add_action( 'hssm_check_scheduled_slides', array( $this, 'check_scheduled_slides' ) );
|
|
}
|
|
|
|
/**
|
|
* Load plugin text domain
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
private function load_textdomain() {
|
|
load_plugin_textdomain(
|
|
'hi-school-slide-manager',
|
|
false,
|
|
dirname( HSSM_PLUGIN_BASENAME ) . '/languages'
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Register custom post types
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
public function register_post_types() {
|
|
if ( $this->slides_cpt ) {
|
|
$this->slides_cpt->register();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Register custom taxonomies
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
public function register_taxonomies() {
|
|
if ( $this->slides_cpt ) {
|
|
$this->slides_cpt->register_taxonomies();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Register REST API routes
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
public function register_rest_routes() {
|
|
if ( $this->rest_api ) {
|
|
$this->rest_api->register_routes();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Enqueue frontend assets
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
public function enqueue_frontend_assets() {
|
|
// Prevent slider assets from loading on admin-facing "frontend" pages
|
|
if ( is_page_template('page-slide-dashboard.php') || is_page_template('page-slide-preview.php') ) {
|
|
// These pages will load their own specific styles (admin-slides-dashboard.css) or rely on explicit shortcode enqueueing.
|
|
return;
|
|
}
|
|
|
|
// Enqueue slider assets globally so they are available for the shortcode on regular frontend pages
|
|
wp_enqueue_style(
|
|
'hssm-slider',
|
|
HSSM_PLUGIN_URL . 'assets/css/slider.css',
|
|
array(),
|
|
HSSM_VERSION
|
|
);
|
|
|
|
wp_enqueue_script(
|
|
'hssm-slider',
|
|
HSSM_PLUGIN_URL . 'assets/js/slider.js',
|
|
array( 'jquery' ),
|
|
HSSM_VERSION,
|
|
true
|
|
);
|
|
|
|
if ( $this->frontend ) {
|
|
$this->frontend->enqueue_assets();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Handle frontend page routing
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
public function handle_frontend_pages() {
|
|
if ( $this->frontend ) {
|
|
$this->frontend->handle_page_routing();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Initialize admin functionality
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
public function admin_init() {
|
|
if ( $this->admin ) {
|
|
$this->admin->init();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Add admin menus
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
public function add_admin_menus() {
|
|
if ( $this->admin ) {
|
|
$this->admin->add_menus();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Enqueue admin assets
|
|
*
|
|
* @since 1.0.0
|
|
* @param string $hook Current admin page hook
|
|
*/
|
|
public function enqueue_admin_assets( $hook ) {
|
|
if ( $this->admin ) {
|
|
$this->admin->enqueue_assets( $hook );
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get companion sites configuration
|
|
*
|
|
* Centralized source of truth for companion sites
|
|
*
|
|
* @since 1.0.0
|
|
* @return array List of companion sites
|
|
*/
|
|
public static function get_companion_sites() {
|
|
return array(
|
|
array(
|
|
'slug' => 'hi-school',
|
|
'name' => 'Hi-School Pharmacy',
|
|
'description' => 'Hi-School Pharmacy companion site'
|
|
),
|
|
array(
|
|
'slug' => 'one-stop',
|
|
'name' => 'One Stop Hardware',
|
|
'description' => 'One Stop Hardware companion site'
|
|
),
|
|
array(
|
|
'slug' => 'ace',
|
|
'name' => 'Ace Hardware',
|
|
'description' => 'Ace Hardware companion site'
|
|
)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Check for scheduled slides that need to be activated/deactivated
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
public function check_scheduled_slides() {
|
|
if ( $this->slides_cpt ) {
|
|
$this->slides_cpt->check_scheduled_slides();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Plugin activation
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
public static function activate() {
|
|
// Set up cron jobs
|
|
self::setup_cron_jobs();
|
|
|
|
// Flush rewrite rules
|
|
flush_rewrite_rules();
|
|
|
|
// Set default options
|
|
self::set_default_options();
|
|
|
|
// Create default taxonomy terms
|
|
self::create_default_terms();
|
|
}
|
|
|
|
/**
|
|
* Plugin deactivation
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
public static function deactivate() {
|
|
// Clear cron jobs
|
|
wp_clear_scheduled_hook( 'hssm_check_scheduled_slides' );
|
|
|
|
// Flush rewrite rules
|
|
flush_rewrite_rules();
|
|
}
|
|
|
|
/**
|
|
* Plugin uninstall
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
public static function uninstall() {
|
|
// Remove plugin options
|
|
self::remove_plugin_options();
|
|
|
|
// Remove user meta
|
|
self::remove_user_meta();
|
|
|
|
// Remove taxonomy terms and slides
|
|
self::remove_plugin_data();
|
|
}
|
|
|
|
/**
|
|
* Create default taxonomy terms for sites
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
private static function create_default_terms() {
|
|
// Load the CPT class manually during activation
|
|
require_once HSSM_PLUGIN_DIR . 'includes/class-hssm-slides-cpt.php';
|
|
|
|
// Create a temporary instance to set up default terms
|
|
$slides_cpt = new HSSM_Slides_CPT();
|
|
|
|
// Register the taxonomies first
|
|
$slides_cpt->register_taxonomies();
|
|
|
|
// Default terms are created automatically in the register_taxonomies method
|
|
}
|
|
|
|
/**
|
|
* Setup cron jobs
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
private static function setup_cron_jobs() {
|
|
if ( ! wp_next_scheduled( 'hssm_check_scheduled_slides' ) ) {
|
|
wp_schedule_event( time(), 'hourly', 'hssm_check_scheduled_slides' );
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Set default plugin options
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
private static function set_default_options() {
|
|
$default_options = array(
|
|
'hssm_plugin_mode' => 'manager',
|
|
'hssm_api_url' => '',
|
|
'hssm_client_site_slug' => '',
|
|
'hssm_notification_emails' => array( get_option( 'admin_email' ) ),
|
|
'hssm_default_slide_duration' => 30, // days
|
|
'hssm_cache_duration' => 3600, // 1 hour
|
|
'hssm_api_version' => 'v1',
|
|
'hssm_preview_page_slug' => 'slide-preview',
|
|
'hssm_manager_page_slug' => 'slide-manager',
|
|
'hssm_api_keys' => array( wp_generate_password( 32, false ) ), // Generate initial API key
|
|
);
|
|
|
|
foreach ( $default_options as $option_name => $option_value ) {
|
|
if ( ! get_option( $option_name ) ) {
|
|
add_option( $option_name, $option_value );
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Remove plugin options
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
private static function remove_plugin_options() {
|
|
$options = array(
|
|
'hssm_notification_emails',
|
|
'hssm_default_slide_duration',
|
|
'hssm_cache_duration',
|
|
'hssm_api_version',
|
|
'hssm_preview_page_slug',
|
|
'hssm_manager_page_slug',
|
|
'hssm_api_keys',
|
|
);
|
|
|
|
foreach ( $options as $option ) {
|
|
delete_option( $option );
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Remove user meta
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
private static function remove_user_meta() {
|
|
global $wpdb;
|
|
|
|
$wpdb->delete(
|
|
$wpdb->usermeta,
|
|
array( 'meta_key' => 'hssm_user_preferences' )
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Remove plugin data including slides and taxonomy terms
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
private static function remove_plugin_data() {
|
|
// Get all slide posts
|
|
$slides = get_posts( array(
|
|
'post_type' => 'hssm_slide',
|
|
'numberposts' => -1,
|
|
'post_status' => 'any'
|
|
) );
|
|
|
|
// Delete all slides
|
|
foreach ( $slides as $slide ) {
|
|
wp_delete_post( $slide->ID, true );
|
|
}
|
|
|
|
// Remove taxonomy terms
|
|
$taxonomies = array( 'hssm_site', 'hssm_client' );
|
|
foreach ( $taxonomies as $taxonomy ) {
|
|
$terms = get_terms( array(
|
|
'taxonomy' => $taxonomy,
|
|
'hide_empty' => false,
|
|
) );
|
|
|
|
if ( ! is_wp_error( $terms ) ) {
|
|
foreach ( $terms as $term ) {
|
|
wp_delete_term( $term->term_id, $taxonomy );
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|