/**
  * Display admin notices to the user
  *
  *
  *
  *
  */
 public function messages()
 {
     if (!isset($_GET['page']) || $_GET['page'] != 'mdjm-employees' || empty($_GET['role_action']) || empty($_GET['message'])) {
         return;
     }
     $messages = array(1 => array('updated', __('Roles deleted.', 'mobile-dj-manager')), 2 => array('updated', __('', 'mobile-dj-manager')), 3 => array('updated', __('', 'mobile-dj-manager')), 4 => array('updated', __('Permissions updated.', 'mobile-dj-manager')), 5 => array('error', __('No roles selected for deletion.', 'mobile-dj-manager')), 6 => array('error', __('Not all roles could be deleted. Do you still have users assigned?', 'mobile-dj-manager')), 7 => array('updated', __('', 'mobile-dj-manager')));
     mdjm_update_notice($messages[$_GET['message']][0], $messages[$_GET['message']][1], true);
 }
 /**
  * Display admin notices to the user
  *
  *
  *
  *
  */
 public function messages()
 {
     if (!isset($_GET['page']) || $_GET['page'] != 'mdjm-employees' || empty($_GET['user_action']) || empty($_GET['message'])) {
         return;
     }
     $messages = array(2 => array('updated', __('Employees deleted.', 'mobile-dj-manager')));
     mdjm_update_notice($messages[$_GET['message']][0], $messages[$_GET['message']][1], true);
 }
 function insert_comm($args)
 {
     global $mdjm;
     if (MDJM_DEBUG == true) {
         MDJM()->debug->log_it('Starting ' . __METHOD__, true);
     }
     if (empty($args) || !is_array($args)) {
         return mdjm_update_notice('update-nag', 'The communication was not logged');
     }
     $post_args['post_title'] = isset($args['subject']) ? wp_strip_all_tags($args['subject']) : '';
     $post_args['post_content'] = isset($args['content']) ? $args['content'] : '';
     $post_args['post_status'] = isset($args['status']) ? $args['status'] : 'ready to send';
     $post_args['post_author'] = isset($args['author']) ? $args['author'] : get_current_user_id();
     $meta_args['date_sent'] = isset($args['date_sent']) ? $args['date_sent'] : time();
     $meta_args['recipient'] = isset($args['recipient']) ? $args['recipient'] : '';
     $meta_args['source'] = isset($args['source']) ? $args['source'] : '';
     $meta_args['event'] = isset($args['event']) ? $args['event'] : '';
     if (empty($post_args['post_title'])) {
         return mdjm_update_notice('update-nag', 'The communication was not logged - no title ' . $post_args['post_title']);
     }
     if (empty($post_args['post_content'])) {
         return mdjm_update_notice('update-nag', 'The communication was not logged - no content ' . $post_args['post_content']);
     }
     if (empty($meta_args['recipient'])) {
         return mdjm_update_notice('update-nag', 'The communication was not logged - no recipient ' . $post_args['recipient']);
     }
     if (empty($meta_args['source'])) {
         return mdjm_update_notice('update-nag', 'The communication was not logged - no source ' . $post_args['source']);
     }
     $post_args['post_type'] = 'mdjm_communication';
     $post_args['ping_status'] = false;
     $post_args['comment_status'] = 'closed';
     if (MDJM_DEBUG == true) {
         MDJM()->debug->log_it('	-- Inserting COMM Post');
     }
     $comm_post_id = wp_insert_post($post_args);
     if ($comm_post_id) {
         if (MDJM_DEBUG == true) {
             MDJM()->debug->log_it('	-- COMM post created ' . $comm_post_id);
         }
         foreach ($meta_args as $meta_key => $meta_value) {
             add_post_meta($comm_post_id, '_' . $meta_key, $meta_value);
         }
     }
     // Process attachments
     if (!empty($args['attachments']) && is_array($args['attachments'])) {
         if (MDJM_DEBUG == true) {
             MDJM()->debug->log_it('	-- This email has attachments');
         }
         foreach ($args['attachments'] as $file) {
             // Make sure the file exists
             if (!file_exists($file)) {
                 if (MDJM_DEBUG == true) {
                     MDJM()->debug->log_it('	-- ERROR: Attachment not found, will not process');
                 }
                 continue;
             }
             $file_type = wp_check_filetype(basename($file), null);
             $upload_dir = wp_upload_dir();
             // Prepare an array of post data for the attachment.
             $attachment = array('guid' => $upload_dir['url'] . '/' . basename($file), 'post_mime_type' => $file_type['type'], 'post_title' => preg_replace('/\\.[^.]+$/', '', basename($file)), 'post_content' => '', 'post_status' => 'inherit');
             // Insert the attachment.
             $attach_id = wp_insert_attachment($attachment, $file, $comm_post_id);
             if ($attach_id) {
                 if (MDJM_DEBUG == true) {
                     MDJM()->debug->log_it('	-- Attachment post inserted successfully. ID ' . $attach_id);
                 }
             } else {
                 if (MDJM_DEBUG == true) {
                     MDJM()->debug->log_it('	-- Attachment post could not be inserted');
                 }
             }
             // Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
             require_once ABSPATH . 'wp-admin/includes/image.php';
             // Generate the metadata for the attachment, and update the database record.
             $attach_data = wp_generate_attachment_metadata($attach_id, $file);
             wp_update_attachment_metadata($attach_id, $attach_data);
         }
         // End foreach( $args['attachments'] as $file )
     }
     // Attachments
     return $comm_post_id;
 }
        }
        if (isset($_POST['email_subject'])) {
            if (!isset($mdjm_schedules[$_POST['slug']]['options']['email_subject']) || $mdjm_schedules[$_POST['slug']]['options']['email_subject'] != $_POST['email_subject']) {
                $mdjm_schedules[$_POST['slug']]['options']['email_subject'] = sanitize_text_field($_POST['email_subject']);
            }
        }
        if (isset($_POST['email_from'])) {
            if ($mdjm_schedules[$_POST['slug']]['options']['email_from'] != $_POST['email_from']) {
                $mdjm_schedules[$_POST['slug']]['options']['email_from'] = $_POST['email_from'];
            }
        }
        /* Update the options table */
        update_option('mdjm_schedules', $mdjm_schedules);
        $class = 'updated';
        $message = 'The <strong>' . $mdjm_schedules[$_POST['slug']]['name'] . '</strong> task has been updated successfully';
        mdjm_update_notice($class, $message);
    }
}
/*
* f_mdjm_render_scheduler
* 13/11/2014
* since 0.9.3
* Displays the scheduled tasks
*/
function f_mdjm_render_scheduler()
{
    global $mdjm_settings;
    $mdjm_schedules = get_option('mdjm_schedules');
    asort($mdjm_schedules);
    ?>
        <div class="wrap">
 /**
  * Process bulk actions if requested
  *
  * @param
  *
  * @return
  */
 public function process_bulk_actions()
 {
     $action = $this->current_action();
     if (empty($action)) {
         return;
     }
     $i = 0;
     if ('delete' === $action && !empty($_POST['client'])) {
         foreach ($_POST['client'] as $user_id) {
             if (MDJM_DEBUG == true) {
                 MDJM()->debug->log_it('Deleting client with ID ' . $user_id, true);
             }
             wp_delete_user($user_id);
             $i++;
         }
         mdjm_update_notice('updated', __($i . ' Client(s) deleted.', 'mobile-dj-manager'), true);
     }
     if ('inactive' === $action || 'active' === $action) {
         if (empty($_POST['client'])) {
             return;
         }
         foreach ($_POST['client'] as $user_id) {
             if (MDJM_DEBUG == true) {
                 MDJM()->debug->log_it('Setting client with ID ' . $user_id . ' to ' . ucfirst($this->current_action()), true);
             }
             $user = new WP_User($user_id);
             $user->set_role($action == 'inactive' ? 'inactive_client' : 'client');
             $i++;
         }
         mdjm_update_notice('updated', __($i . ' Client(s) updated.', 'mobile-dj-manager'), true);
     }
 }
 function update_field()
 {
     global $current_user;
     if ($_POST['field_type'] == 'checkbox') {
         $value = $_POST['field_value'];
     } elseif ($_POST['field_type'] == 'dropdown') {
         $value = $_POST['field_options'];
     }
     $this->fields[$_POST['field_id']] = array('label' => sanitize_text_field($_POST['field_label']), 'id' => $_POST['field_id'], 'type' => $_POST['field_type'], 'value' => !empty($value) ? $value : '', 'checked' => !empty($_POST['field_checked']) ? '1' : '0', 'display' => !empty($_POST['field_enabled']) ? '1' : '0', 'required' => !empty($_POST['field_required']) ? '1' : '0', 'desc' => !empty($_POST['field_desc']) ? sanitize_text_field($_POST['field_desc']) : '', 'default' => $this->fields[$_POST['field_id']]['default'], 'position' => $this->fields[$_POST['field_id']]['position']);
     if (update_option('mdjm_client_fields', $this->fields)) {
         mdjm_update_notice('updated', sanitize_text_field($_POST['field_label']) . ' updated successfully.');
     } else {
         mdjm_update_notice('error', __('Field could not be updated', 'mobile-dj-manager'));
     }
 }
 /**
  * Init
  *
  *
  *
  */
 public static function init()
 {
     global $wp_roles;
     // Listen for post requests
     // Update the user role
     if (isset($_POST['change_role'], $_POST['new_role'], $_POST['employees'])) {
         foreach ($_POST['employees'] as $employee) {
             mdjm_set_employee_role($employee, $_POST['new_role']);
         }
         mdjm_update_notice('updated', __('Employee roles updated.', 'mobile-dj-manager'), true);
     }
     wp_enqueue_script('jquery-validation-plugin');
     self::$all_roles = $wp_roles;
     // Filter our search by role if we need to
     self::$display_role = !empty($_GET['display_role']) ? $_GET['display_role'] : '';
     self::$orderby = !empty($_GET['orderby']) ? $_GET['orderby'] : '';
     self::$order = !empty($_GET['order']) ? $_GET['order'] : '';
     // Which tab?
     self::$active_tab = isset($_GET['tab']) ? $_GET['tab'] : 'user_roles';
     // Display the page tabs
     self::page_header();
     // Retrieve all MDJM roles
     self::$mdjm_roles = mdjm_get_roles();
     self::$total_roles = count(self::$mdjm_roles);
     // Determine the page to display
     if (self::$active_tab == 'permissions') {
         self::permissions_manager();
     } else {
         // Instantiate the user table class
         self::$mdjm_employee_table = new MDJM_Employee_Table();
         self::$mdjm_employee_table->process_bulk_actions();
         // Retrieve employee list
         self::$employees = empty($_POST['s']) ? mdjm_get_employees(self::$display_role, self::$orderby, self::$order) : self::search();
         self::$total_employees = count(mdjm_get_employees());
         self::$mdjm_employee_table->prepare_items();
         // The header for the user management page
         self::employee_page();
     }
 }
        /**
         * Display the settings page to enable admins to add/delete/edit fields
         *
         *
         *
         *
         */
        public static function custom_event_field_settings()
        {
            if (isset($_GET['message'])) {
                $messages = array('1' => array('updated', __('Field added successfully.', 'mobile-dj-manager')), '2' => array('updated', __('Field updated successfully.', 'mobile-dj-manager')), '3' => array('updated', __('Field deleted successfully.', 'mobile-dj-manager')), '4' => array('error', __('Unable to add field.', 'mobile-dj-manager')), '5' => array('error', __('Unable to update field.', 'mobile-dj-manager')), '6' => array('error', __('Unable to delete field.', 'mobile-dj-manager')));
                mdjm_update_notice($messages[$_GET['message']][0], $messages[$_GET['message']][1], true);
            }
            ?>
			<div class="mdjm-event-field-container">
			<div class="mdjm-event-field-column-left">
			
			<?php 
            /**
             * Display the Custom fields
             *
             *
             *
             */
            $field_types = array('client', 'event', 'venue');
            foreach ($field_types as $field_type) {
                ?>
				<h3><?php 
                printf(__('%s Fields', 'mobile-dj-manager'), ucfirst($field_type));
                ?>
</h3>
				<table id="mdjm_custom-<?php 
                echo $field_type;
                ?>
" class="widefat mdjm-custom-<?php 
                echo $field_type;
                ?>
-list-item" style="width:90%">
					<thead>
					<tr>
                    	<th style="width: 20px;"></th>
						<th style="width: 15%;"><?php 
                _e('Label', 'mobile-dj-manager');
                ?>
</th>
						<th style="width: 15%;"><?php 
                _e('Type', 'mobile-dj-manager');
                ?>
</th>
						<th style="width: 30%;"><?php 
                _e('Description', 'mobile-dj-manager');
                ?>
</th>
                        <th style="width: 15%;"><?php 
                _e('Options', 'mobile-dj-manager');
                ?>
</th>
						<th><?php 
                _e('Actions', 'mobile-dj-manager');
                ?>
</th>
					</tr>
					</thead>
					<tbody>
				<?php 
                $fields = mdjm_get_custom_fields($field_type);
                // Display the custom fields
                if ($fields->have_posts()) {
                    $i = 0;
                    while ($fields->have_posts()) {
                        $fields->the_post();
                        ?>
						<tr id="customfields_<?php 
                        echo $fields->post->ID;
                        ?>
" class="
							<?php 
                        echo $i == 0 ? 'alternate mdjm_sortable_row' : 'mdjm_sortable_row';
                        ?>
" data-key="<?php 
                        echo $fields->post->ID;
                        ?>
">
                            <td><span class="mdjm_draghandle"></span></td>
                            <td><?php 
                        the_title();
                        ?>
</td>
                            <td><?php 
                        echo ucfirst(get_post_meta($fields->post->ID, '_mdjm_field_type', true));
                        ?>
</td>
                            <td><?php 
                        echo $fields->post->post_content;
                        ?>
</td>
                            <td><?php 
                        self::field_icons($fields->post->ID);
                        ?>
</td>
                            <td>
                                <a href="<?php 
                        echo mdjm_get_admin_page('custom_event_fields') . '&edit_custom_field=1&id=' . $fields->post->ID;
                        ?>
"
                                class="button button-primary button-small"><?php 
                        _e('Edit', 'mobile-dj-manager');
                        ?>
</a>
                                &nbsp;&nbsp;&nbsp;<a href="<?php 
                        echo mdjm_get_admin_page('custom_event_fields') . '&delete_custom_field=1&id=' . $fields->post->ID;
                        ?>
"
                        		class="button button-secondary button-small"><?php 
                        _e('Delete', 'mobile-dj-manager');
                        ?>
</a>
                            </td>
                        </tr>
                        <?php 
                        $i++;
                        if ($i == 2) {
                            $i = 0;
                        }
                    }
                    wp_reset_postdata();
                } else {
                    ?>
					<tr>
						<td colspan="6"><?php 
                    printf(__('No Custom %s Fields Defined!', 'mobile-dj-manager'), ucfirst($field_type));
                    ?>
</td>
					</tr>
					<?php 
                }
                ?>
					</tbody>
					<tfoot>
					<tr style="font-weight: bold;">
                    	<th style="width: 20px;"></th>
						<th style="width: 15%;"><?php 
                _e('Label', 'mobile-dj-manager');
                ?>
</th>
						<th style="width: 15%;"><?php 
                _e('Type', 'mobile-dj-manager');
                ?>
</th>
						<th style="width: 25%;"><?php 
                _e('Description', 'mobile-dj-manager');
                ?>
</th>
                        <th style="width: 15%;"><?php 
                _e('Options', 'mobile-dj-manager');
                ?>
</th>
						<th><?php 
                _e('Actions', 'mobile-dj-manager');
                ?>
</th>
					</tr>
					</tfoot>
				</table>
			<?php 
            }
            // End foreach loop
            ?>
			</div>
			
			<?php 
            self::add_new_custom_field_table($field_types);
            ?>
			
			</div>
			<?php 
        }
 function delete_log($files = '')
 {
     foreach ($files as $file) {
         if (file_exists($this->files[$file][0])) {
             if (unlink($this->files[$file][0])) {
                 $this->log_it('Purged the ' . $file . ' log file', true);
                 $success[] = 'The ' . $file . ' log file was auto-purged successfully in accordance with your <a href="' . mdjm_get_admin_page('debugging') . '">Debug Settings</a>';
             } else {
                 $this->log_it('ERROR: Could not purge the ' . $file . ' log file', true);
                 $error[] = 'The ' . $file . ' log file cound not be purged';
             }
         }
     }
     if (isset($success)) {
         mdjm_update_notice('updated', implode('<br />', $success));
     }
     if (isset($error)) {
         mdjm_update_notice('error', implode('<br />', $success));
     }
 }
/**
 * Toggle the users role between active/inactive
 * This can be called via the link on the Client/DJ screen, or by a bulk action
 *
 * @param	str|arr		$users		If string, will be converted to array or User ID's to process
 *			str			$role		The role to convert the user to
 * @return
 */
function set_user_role($users, $role)
{
    // If $users is not an array, make it one
    if (!is_array($users)) {
        $users = array($users);
    }
    // Array of role names
    $role_name = array('client' => 'Active', 'inactive_client' => 'Inactive', 'dj' => 'Active', 'inactive_dj' => 'Inactive');
    $i = 0;
    // Counter
    // Loop through the $users array and adjust the roles
    foreach ($users as $user) {
        $user_id = wp_update_user(array('ID' => $user, 'role' => $role));
        if (is_wp_error($user_id)) {
            // Failed
            $user_error = true;
        } else {
            // Success
            $i++;
        }
    }
    // Define the admin notice class and content
    if (!empty($user_error) && $i == 0) {
        $class = 'error';
        $message = sprintf(__('ERROR: %s were set as %s.%sContact 
				%sMDJM Support%s with details of any errors that are displayed on your screen.', 'mobile-dj-manager'), _n($i . ' user ', ' ' . $i . ' users ', $i, 'mobile-dj-manager'), $role_name[$role], '<br />', '<a href="http://mdjm.co.uk/forums/forum/bugs/" target="_blank" title="Report this bug">', '</a>');
    } elseif (!empty($user_error) && $i < $user_count) {
        $class = 'update-nag';
        $message = sprintf(__('WARNING: Some errors occured and only %s out of %s %s were set as %s.', 'mobile-dj-manager'), $i, count($users), _n('user', 'users', $i, 'mobile-dj-manager'), $role_name[$role]);
    } else {
        $class = 'updated';
        $message = sprintf(__('%s %s successfully marked as %s.', 'mobile-dj-manager'), $i, _n('user', 'users', $i, 'mobile-dj-manager'), $role_name[$role]);
    }
    mdjm_update_notice($class, $message, true);
}