prefix}fwren_): * entries one row per submission; `response` holds the cleaned values as JSON. * entry_values one row per field value (search, unique validation, reports). * entry_meta notes, logs of actions, draft tokens, quiz score, payment refs. * logs notification / integration / payment API log. * stats daily views, starts and submissions per form (analytics). * drafts save & resume and step persistence. * * Modules add their own tables through the `fwren/install` action (payments: transactions, * order items, subscriptions, coupons). * * @package FWREN */ namespace FWREN; defined( 'ABSPATH' ) || exit; class Install { /** Run on activation. */ public static function activate() { self::create_tables(); self::add_caps(); self::protect_uploads(); update_option( 'fwren_db_version', FWREN_DB_VERSION ); // Pretty landing page URLs (/form/{slug}/). Post_Type::register(); flush_rewrite_rules(); } /** Run on deactivation. */ public static function deactivate() { wp_clear_scheduled_hook( 'fwren_daily' ); flush_rewrite_rules(); } /** Upgrade tables when the stored version is behind. */ public static function maybe_upgrade() { if ( get_option( 'fwren_db_version' ) !== FWREN_DB_VERSION ) { self::create_tables(); self::add_caps(); update_option( 'fwren_db_version', FWREN_DB_VERSION ); } } /** @return string Table name. */ public static function table( $name ) { global $wpdb; return $wpdb->prefix . 'fwren_' . $name; } /** Create or update every table. */ public static function create_tables() { global $wpdb; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; $c = $wpdb->get_charset_collate(); $entries = self::table( 'entries' ); $values = self::table( 'entry_values' ); $meta = self::table( 'entry_meta' ); $logs = self::table( 'logs' ); $stats = self::table( 'stats' ); $drafts = self::table( 'drafts' ); dbDelta( "CREATE TABLE $entries ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, form_id BIGINT UNSIGNED NOT NULL, serial INT UNSIGNED NOT NULL DEFAULT 0, status VARCHAR(20) NOT NULL DEFAULT 'unread', is_favorite TINYINT(1) NOT NULL DEFAULT 0, user_id BIGINT UNSIGNED NOT NULL DEFAULT 0, ip VARCHAR(45) NOT NULL DEFAULT '', user_agent VARCHAR(255) NOT NULL DEFAULT '', source_url VARCHAR(500) NOT NULL DEFAULT '', payment_status VARCHAR(20) NOT NULL DEFAULT '', payment_total BIGINT NOT NULL DEFAULT 0, currency VARCHAR(10) NOT NULL DEFAULT '', response LONGTEXT NULL, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, PRIMARY KEY (id), KEY form_status (form_id,status), KEY created_at (created_at) ) $c;" ); dbDelta( "CREATE TABLE $values ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, entry_id BIGINT UNSIGNED NOT NULL, form_id BIGINT UNSIGNED NOT NULL, field_name VARCHAR(100) NOT NULL, sub_field VARCHAR(100) NOT NULL DEFAULT '', value LONGTEXT NULL, PRIMARY KEY (id), KEY entry_id (entry_id), KEY form_field (form_id,field_name), KEY form_field_value (form_id,field_name,value(150)) ) $c;" ); dbDelta( "CREATE TABLE $meta ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, entry_id BIGINT UNSIGNED NOT NULL DEFAULT 0, form_id BIGINT UNSIGNED NOT NULL DEFAULT 0, meta_key VARCHAR(100) NOT NULL, meta_value LONGTEXT NULL, user_id BIGINT UNSIGNED NOT NULL DEFAULT 0, created_at DATETIME NOT NULL, PRIMARY KEY (id), KEY entry_key (entry_id,meta_key), KEY form_key (form_id,meta_key) ) $c;" ); dbDelta( "CREATE TABLE $logs ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, form_id BIGINT UNSIGNED NOT NULL DEFAULT 0, entry_id BIGINT UNSIGNED NOT NULL DEFAULT 0, component VARCHAR(50) NOT NULL DEFAULT '', status VARCHAR(20) NOT NULL DEFAULT 'info', title VARCHAR(255) NOT NULL DEFAULT '', message LONGTEXT NULL, created_at DATETIME NOT NULL, PRIMARY KEY (id), KEY form_entry (form_id,entry_id), KEY component (component) ) $c;" ); dbDelta( "CREATE TABLE $stats ( form_id BIGINT UNSIGNED NOT NULL, day DATE NOT NULL, views INT UNSIGNED NOT NULL DEFAULT 0, starts INT UNSIGNED NOT NULL DEFAULT 0, submissions INT UNSIGNED NOT NULL DEFAULT 0, PRIMARY KEY (form_id,day) ) $c;" ); dbDelta( "CREATE TABLE $drafts ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, form_id BIGINT UNSIGNED NOT NULL, hash CHAR(32) NOT NULL, user_id BIGINT UNSIGNED NOT NULL DEFAULT 0, email VARCHAR(190) NOT NULL DEFAULT '', step INT NOT NULL DEFAULT 0, data LONGTEXT NULL, ip VARCHAR(45) NOT NULL DEFAULT '', created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, PRIMARY KEY (id), UNIQUE KEY hash (hash), KEY form_user (form_id,user_id) ) $c;" ); do_action( 'fwren/install', $c ); } /** Capabilities: administrators manage everything; editors can view entries. */ public static function add_caps() { $admin = get_role( 'administrator' ); if ( $admin ) { foreach ( array( 'fwren_manage_forms', 'fwren_view_entries', 'fwren_manage_entries', 'fwren_manage_settings' ) as $cap ) { $admin->add_cap( $cap ); } } $editor = get_role( 'editor' ); if ( $editor ) { $editor->add_cap( 'fwren_view_entries' ); } } /** @return string Absolute path of the protected upload folder. */ public static function upload_dir() { $up = wp_upload_dir( null, false ); return trailingslashit( $up['basedir'] ) . 'fieldwren'; } /** @return string URL of the upload folder. */ public static function upload_url() { $up = wp_upload_dir( null, false ); return trailingslashit( $up['baseurl'] ) . 'fieldwren'; } /** Uploads are served through the plugin (signed links), never browsed directly. */ public static function protect_uploads() { $dir = self::upload_dir(); foreach ( array( $dir, $dir . '/temp', $dir . '/entries' ) as $d ) { if ( ! is_dir( $d ) ) { wp_mkdir_p( $d ); } if ( ! file_exists( $d . '/index.php' ) ) { @file_put_contents( $d . '/index.php', "\nDeny from all\n\n" ); // phpcs:ignore } if ( ! file_exists( $dir . '/temp/.htaccess' ) ) { @file_put_contents( $dir . '/temp/.htaccess', "Deny from all\n" ); // phpcs:ignore } } }