Skip to content

Instantly share code, notes, and snippets.

@broskees

broskees/plugin_init.php Secret

Created Nov 27, 2017
Embed
What would you like to do?
Timesheet Plugin Init
<?php
global $timesheetPluginInit;
$timesheetPluginInit = new TimesheetPluginInit();
class TimesheetPluginInit {
//Calls all init functions
function timesheet_init_plugin() {
$this->timesheet_create_dbs();
$this->timesheet_create_roles();
$this->timesheet_admin_bar_removal();
$this->timesheet_add_plugin_caps();
}
//create database tables
function timesheet_create_dbs() {
//get database includes
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
// Create Dates database
$table_name = $wpdb->prefix . 'timesheets_dates';
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
mon_date varchar(15) NOT NULL,
tue_date varchar(15) NOT NULL,
wed_date varchar(15) NOT NULL,
thu_date varchar(15) NOT NULL,
fri_date varchar(15) NOT NULL,
sat_date varchar(15) NOT NULL,
sun_date varchar(15) NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
// Create Hours Database
$table_name = $wpdb->prefix . 'timesheets_hours';
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
mon_hours varchar(15) NOT NULL,
tue_hours varchar(15) NOT NULL,
wed_hours varchar(15) NOT NULL,
thu_hours varchar(15) NOT NULL,
fri_hours varchar(15) NOT NULL,
sat_hours varchar(15) NOT NULL,
sun_hours varchar(15) NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
// Create Break Hours Database
$table_name = $wpdb->prefix . 'timesheets_break_hours';
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
mon_break_hours varchar(15) NOT NULL,
tue_break_hours varchar(15) NOT NULL,
wed_break_hours varchar(15) NOT NULL,
thu_break_hours varchar(15) NOT NULL,
fri_break_hours varchar(15) NOT NULL,
sat_break_hours varchar(15) NOT NULL,
sun_break_hours varchar(15) NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
// Create Monday Labor Codes Database
$table_name = $wpdb->prefix . 'timesheets_mon_labor_codes';
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
demo boolean NOT NULL,
process_piping boolean NOT NULL,
insulation boolean NOT NULL,
plumbing boolean NOT NULL,
hvac boolean NOT NULL,
shop boolean NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
// Create Tuesday Labor Codes Database
$table_name = $wpdb->prefix . 'timesheets_tue_labor_codes';
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
demo boolean NOT NULL,
process_piping boolean NOT NULL,
insulation boolean NOT NULL,
plumbing boolean NOT NULL,
hvac boolean NOT NULL,
shop boolean NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
// Create Wednesday Labor Codes Database
$table_name = $wpdb->prefix . 'timesheets_wed_labor_codes';
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
demo boolean NOT NULL,
process_piping boolean NOT NULL,
insulation boolean NOT NULL,
plumbing boolean NOT NULL,
hvac boolean NOT NULL,
shop boolean NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
// Create Thursday Labor Codes Database
$table_name = $wpdb->prefix . 'timesheets_thu_labor_codes';
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
demo boolean NOT NULL,
process_piping boolean NOT NULL,
insulation boolean NOT NULL,
plumbing boolean NOT NULL,
hvac boolean NOT NULL,
shop boolean NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
// Create Friday Labor Codes Database
$table_name = $wpdb->prefix . 'timesheets_fri_labor_codes';
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
demo boolean NOT NULL,
process_piping boolean NOT NULL,
insulation boolean NOT NULL,
plumbing boolean NOT NULL,
hvac boolean NOT NULL,
shop boolean NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
// Create Saturday Labor Codes Database
$table_name = $wpdb->prefix . 'timesheets_sat_labor_codes';
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
demo boolean NOT NULL,
process_piping boolean NOT NULL,
insulation boolean NOT NULL,
plumbing boolean NOT NULL,
hvac boolean NOT NULL,
shop boolean NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
// Create Sunday Labor Codes Database
$table_name = $wpdb->prefix . 'timesheets_sun_labor_codes';
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
demo boolean NOT NULL,
process_piping boolean NOT NULL,
insulation boolean NOT NULL,
plumbing boolean NOT NULL,
hvac boolean NOT NULL,
shop boolean NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
// Create Job Numbers Database
$table_name = $wpdb->prefix . 'timesheets_job_numbers';
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
mon_job_numbers varchar(255) NOT NULL,
tue_job_numbers varchar(255) NOT NULL,
wed_job_numbers varchar(255) NOT NULL,
thu_job_numbers varchar(255) NOT NULL,
fri_job_numbers varchar(255) NOT NULL,
sat_job_numbers varchar(255) NOT NULL,
sun_job_numbers varchar(255) NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
// Create Labor Descriptions Database
$table_name = $wpdb->prefix . 'timesheets_labor_descriptions';
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
mon_labor_descriptions varchar(255) NOT NULL,
tue_labor_descriptions varchar(255) NOT NULL,
wed_labor_descriptions varchar(255) NOT NULL,
thu_labor_descriptions varchar(255) NOT NULL,
fri_labor_descriptions varchar(255) NOT NULL,
sat_labor_descriptions varchar(255) NOT NULL,
sun_labor_descriptions varchar(255) NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
// Create Other Database
$table_name = $wpdb->prefix . 'timesheets_other';
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
remarks varchar(255) NOT NULL,
miles decimal NOT NULL,
mpg decimal NOT NULL,
other_expenses varchar(255) NOT NULL,
user_id decimal NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
}
//creates roles
function timesheet_create_roles(){
$result = remove_role('timesheet_manage');
$result = remove_role('lovett_timesheet_manager');
$result = add_role(
'lovett_timesheet_manager',
__( 'Lovett Timesheet Manager' ),
array(
'switch_themes' => false, // false explicitly denies
'edit_themes' => false,
'edit_theme_options' => false,
'install_themes' => false,
'activate_plugins' => false,
'edit_plugins' => false,
'install_plugins' => false,
'manage_options' => false,
'moderate_comments' => false,
'manage_categories' => false,
'manage_links' => false,
'upload_files' => false,
'import' => false,
'unfiltered_html' => false,
'edit_others_posts' => false,
'edit_published_posts' => false,
'publish_posts' => false,
'edit_pages' => false,
'edit_posts' => false,
'delete_posts' => false,
'list_users' => true, // true allows this capability
'remove_users' => true,
'promote_users' => false,
'delete_users' => true,
'create_users' => true,
'edit_users' => true,
'read' => true,
'manage_timesheets' => true
)
);
}
//kills admin bar
function timesheet_admin_bar_removal(){
if (!current_user_can('administrator') && !is_admin()) {
show_admin_bar(false);
}
}
//adds admin plugin capabilities
function timesheet_add_plugin_caps(){
// gets the author role
$role = get_role( 'administrator' );
// This only works, because it accesses the class instance.
$role->add_cap( 'manage_timesheets' );
}
}
register_activation_hook( __FILE__, array( 'PluginInit', 'timesheet_init_plugin' ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment