quiztech-assessment-platform/quiztech-assessment-platform.php

310 lines
No EOL
11 KiB
PHP

<?php
/**
* Plugin Name: Quiztech Assessment Platform
* Plugin URI: https://quiztech.com/
* Description: A platform for creating assessments, inviting applicants, and managing results.
* Version: 0.1.0
* Requires at least: 5.2
* Requires PHP: 7.2
* Author: Your Name / Company Name
* Author URI: https://yourwebsite.com/
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: quiztech
* Domain Path: /languages
*/
// If this file is called directly, abort.
if ( ! \defined( 'WPINC' ) ) {
die;
}
/**
* Define constants
*/
\define( 'QUIZTECH_VERSION', '0.1.0' );
\define( 'QUIZTECH_PLUGIN_DIR', \plugin_dir_path( __FILE__ ) );
\define( 'QUIZTECH_PLUGIN_URL', \plugin_dir_url( __FILE__ ) );
\define( 'QUIZTECH_PLUGIN_FILE', __FILE__ );
/**
* Load Composer autoloader.
*/
require_once __DIR__ . '/vendor/autoload.php';
/**
* Load necessary procedural files not handled by PSR-4 autoloading.
*/
require_once QUIZTECH_PLUGIN_DIR . 'src/Includes/roles.php';
require_once QUIZTECH_PLUGIN_DIR . 'src/Includes/post-types.php';
require_once QUIZTECH_PLUGIN_DIR . 'src/Includes/taxonomies.php';
require_once QUIZTECH_PLUGIN_DIR . 'src/Includes/credits.php';
require_once QUIZTECH_PLUGIN_DIR . 'src/Includes/payments.php';
// Use statements for Admin classes
use Quiztech\AssessmentPlatform\Admin\AdminListTables;
use Quiztech\AssessmentPlatform\Admin\SettingsPage;
use Quiztech\AssessmentPlatform\Admin\JobMetaboxes;
use Quiztech\AssessmentPlatform\Admin\QuestionMetaboxes;
use Quiztech\AssessmentPlatform\Admin\AssessmentMetaboxes;
use Quiztech\AssessmentPlatform\Includes\FrontendHandler;
use Quiztech\AssessmentPlatform\Includes\Ajax\AssessmentAjaxHandler;
use Quiztech\AssessmentPlatform\Includes\Ajax\AssessmentBuilderAjaxHandler;
/**
* Load plugin dependencies. (Now handled by autoloading)
*/
// require_once QUIZTECH_PLUGIN_DIR . 'includes/post-types.php'; // Autoloaded
// require_once QUIZTECH_PLUGIN_DIR . 'includes/taxonomies.php'; // Autoloaded
// require_once QUIZTECH_PLUGIN_DIR . 'includes/roles.php'; // Autoloaded
/**
* The core plugin class that is used to define internationalization,
* admin-specific hooks, and public-facing site hooks.
*/
// require plugin_dir_path( __FILE__ ) . 'includes/class-quiztech.php';
/**
* Begins execution of the plugin.
*
* Since everything within the plugin is registered via hooks,
* then kicking off the plugin from this point in the file does
* not affect the page life cycle.
*
* @since 0.1.0
*/
function run_quiztech() {
// $plugin = new Quiztech();
// $plugin->run();
}
// run_quiztech();
// Placeholder for activation/deactivation hooks
\register_activation_hook( __FILE__, __NAMESPACE__ . '\activate_quiztech' );
\register_deactivation_hook( __FILE__, __NAMESPACE__ . '\deactivate_quiztech' );
/**
* The code that runs during plugin activation.
* This action is documented in includes/class-quiztech-activator.php
*/
function activate_quiztech() {
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
$table_name = $wpdb->prefix . 'quiztech_invitations';
$sql = "CREATE TABLE {$table_name} (
id mediumint(9) NOT NULL AUTO_INCREMENT,
token varchar(32) NOT NULL,
job_id bigint(20) unsigned NOT NULL,
assessment_id bigint(20) unsigned NOT NULL,
applicant_email varchar(255) NOT NULL,
status varchar(20) NOT NULL DEFAULT 'pending',
created_timestamp datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
expiry_timestamp datetime DEFAULT NULL,
PRIMARY KEY (id),
UNIQUE KEY token (token),
KEY job_id (job_id),
KEY assessment_id (assessment_id),
KEY applicant_email (applicant_email(191)),
KEY status (status)
) {$charset_collate};";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
\dbDelta( $sql );
// Call the namespaced function for roles
\Quiztech\AssessmentPlatform\Includes\quiztech_add_roles_and_capabilities();
// Programmatically create necessary pages and assign templates
$pages_to_create = [
[
'title' => 'Quiztech Dashboard',
'slug' => 'quiztech-dashboard',
'template' => 'template-quiztech-dashboard.php',
],
[
'title' => 'Manage Jobs',
'slug' => 'manage-jobs',
'template' => 'template-manage-jobs.php',
],
[
'title' => 'Manage Assessments',
'slug' => 'manage-assessments',
'template' => 'template-manage-assessments.php',
],
[
'title' => 'Manage Questions',
'slug' => 'manage-questions',
'template' => 'template-manage-questions.php',
],
[
'title' => 'Manage Credits',
'slug' => 'manage-credits',
'template' => 'template-manage-credits.php',
],
[
'title' => 'View Results',
'slug' => 'view-results',
'template' => 'template-view-results.php',
],
];
foreach ( $pages_to_create as $page_data ) {
// Check if page already exists by slug
$existing_page = get_page_by_path( $page_data['slug'], OBJECT, 'page' );
if ( ! $existing_page ) {
$page_args = [
'post_title' => $page_data['title'],
'post_name' => $page_data['slug'],
'post_content' => '', // Content can be added later or within the template
'post_status' => 'publish',
'post_author' => 1, // Assign to admin user ID 1
'post_type' => 'page',
'page_template'=> $page_data['template'], // Assign the template
];
$new_page_id = wp_insert_post( $page_args );
// Optional: Check if page creation was successful
// if ( is_wp_error( $new_page_id ) ) {
// error_log( 'Quiztech Activation Error: Failed to create page ' . $page_data['title'] . ': ' . $new_page_id->get_error_message() );
// } elseif ( $new_page_id > 0 ) {
// // Optionally update meta if wp_insert_post didn't handle it (it should)
// // update_post_meta( $new_page_id, '_wp_page_template', $page_data['template'] );
// }
}
}
\flush_rewrite_rules(); // Ensure rewrite rules are updated for CPTs/taxonomies
}
/**
* The code that runs during plugin deactivation.
* This action is documented in includes/class-quiztech-deactivator.php
*/
function deactivate_quiztech() {
// Call the namespaced function
\Quiztech\AssessmentPlatform\Includes\quiztech_remove_roles_and_capabilities();
\flush_rewrite_rules(); // Clean up rewrite rules
}
/**
* Initialize the plugin's features.
*/
function quiztech_init() {
// Initialize frontend handler for invitation links
$frontend_handler = new \Quiztech\AssessmentPlatform\Includes\FrontendHandler();
$frontend_handler->init_hooks();
// Initialize AJAX handler for assessment interactions
\Quiztech\AssessmentPlatform\Includes\Ajax\AssessmentAjaxHandler::init();
// Initialize AJAX handler for assessment builder interactions
new \Quiztech\AssessmentPlatform\Includes\Ajax\AssessmentBuilderAjaxHandler();
// Initialize Admin-specific features
if ( is_admin() ) {
$admin_list_tables = new AdminListTables();
$admin_list_tables->register_hooks();
$settings_page = new SettingsPage();
$settings_page->register_hooks();
$job_metaboxes = new JobMetaboxes();
$job_metaboxes->register_hooks();
$question_metaboxes = new QuestionMetaboxes();
$question_metaboxes->register_hooks();
$assessment_metaboxes = new AssessmentMetaboxes();
$assessment_metaboxes->register_hooks();
}
// Future core classes (e.g., Shortcode handlers) should be instantiated here.
}
add_action( 'plugins_loaded', 'quiztech_init' );
/**
* Configure PHPMailer to use SMTP settings if enabled.
*
* @param PHPMailer\PHPMailer\PHPMailer $phpmailer The PHPMailer instance.
*/
function quiztech_configure_smtp( $phpmailer ) {
$options = get_option( 'quiztech_settings', [] );
// Check if SMTP is enabled in settings
if ( empty( $options['smtp_enabled'] ) || ! $options['smtp_enabled'] ) {
return; // SMTP not enabled, do nothing
}
// Basic validation
if ( empty( $options['smtp_host'] ) || empty( $options['smtp_port'] ) ) {
// Optionally log an error or add an admin notice here
error_log('Quiztech SMTP Error: Host or Port not configured.');
return; // Cannot configure SMTP without host and port
}
// Set mailer to use SMTP
$phpmailer->isSMTP();
// Set SMTP server details
$phpmailer->Host = $options['smtp_host'];
$phpmailer->Port = (int) $options['smtp_port'];
// Set encryption type (tls, ssl, or none)
$encryption = isset( $options['smtp_encryption'] ) ? strtolower( $options['smtp_encryption'] ) : '';
if ( in_array( $encryption, [ 'ssl', 'tls' ] ) ) {
$phpmailer->SMTPSecure = $encryption;
} else {
$phpmailer->SMTPSecure = ''; // Explicitly set to none if not ssl/tls
}
// Set authentication details if enabled
$phpmailer->SMTPAuth = ! empty( $options['smtp_auth'] ) && $options['smtp_auth'];
if ( $phpmailer->SMTPAuth ) {
if ( empty( $options['smtp_username'] ) || empty( $options['smtp_password'] ) ) {
error_log('Quiztech SMTP Error: Auth enabled but Username or Password missing.');
// Decide if we should prevent sending or let PHPMailer handle the error
// For now, we proceed and let PHPMailer fail if credentials are bad
}
$phpmailer->Username = isset( $options['smtp_username'] ) ? $options['smtp_username'] : '';
$phpmailer->Password = isset( $options['smtp_password'] ) ? $options['smtp_password'] : '';
}
// Set From address (optional, but recommended)
$from_address = isset( $options['smtp_from_address'] ) ? $options['smtp_from_address'] : '';
$from_name = isset( $options['smtp_from_name'] ) ? $options['smtp_from_name'] : get_bloginfo( 'name' );
if ( ! empty( $from_address ) && is_email( $from_address ) ) {
try {
$phpmailer->setFrom( $from_address, $from_name );
} catch ( \PHPMailer\PHPMailer\Exception $e ) {
error_log( 'Quiztech SMTP Error setting From address: ' . $e->getMessage() );
// Continue without setting From if it fails
}
}
// Optional: Add debugging
// $phpmailer->SMTPDebug = 2; // Enable verbose debug output (use 0 in production)
// $phpmailer->Debugoutput = 'error_log'; // Log debug output to PHP error log
}
add_action( 'phpmailer_init', 'quiztech_configure_smtp' );
/**
* Register REST API endpoints.
*/
function quiztech_register_rest_routes() {
register_rest_route( 'quiztech/v1', '/webhook/stripe', array(
'methods' => 'POST',
'callback' => '\Quiztech\AssessmentPlatform\Includes\quiztech_handle_payment_webhook',
'permission_callback' => '__return_true' // Allow public access - Stripe needs to reach this
) );
}
add_action( 'rest_api_init', 'quiztech_register_rest_routes' );