Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save BobbyRuby/8ba173df659521cfe631bd1ca9ee60a7 to your computer and use it in GitHub Desktop.

Select an option

Save BobbyRuby/8ba173df659521cfe631bd1ca9ee60a7 to your computer and use it in GitHub Desktop.
/**
* Produces Task Boxes Saved at the time a task was created.
*/
private function _task_create_task_meta_boxes()
{
$task_boxes = array();
if (isset($this->_post->ID)) {
$task_boxes = get_post_meta($this->_post->ID, Rvms_core_Plugin::PLUGIN_PREFIX . 'task_' . 'boxes', TRUE);
// There are task boxes
if ( ! empty($task_boxes) ) {
// Loop through the boxes
foreach ($task_boxes as $task_box) {
$task_box = class_RvmsWpCustomFunctions::convertArrayToObj($task_box);
$config = array(
'id' => $this->_prefix . 'custom_field_' . strtolower(str_replace(' ', '_', $task_box->name)), // meta box id, unique per meta box
'title' => $task_box->name . '', // meta box title
'pages' => array($this->_post_type), // post types, accept custom post types as well, default is array('post'); optional
'context' => 'normal', // where the meta box appear: normal (default), advanced, side; optional
'priority' => 'high', // order of meta box: high (default), low; optional
'fields' => array(), // list of meta fields (can be added by field arrays)
'local_images' => false, // Use summaryal or hosted images (meta box images for add/remove)
'use_with_theme' => false //change path if used with theme set to true, false for a plugin or anything else for a custom path(default false).
);
$meta_box = new class_RvmsBainternetMetaBox($config);
// There are fields
if ( ! empty( $task_box->fields ) ) {
// Loop through the fields and display
foreach ( $task_box->fields as $i => $field ) {
$field_id = preg_replace('/[^_A-Za-z0-9\-]/', '', strtolower($this->_prefix . str_replace(' ', '_', trim($field['name']))));
$field_type = $field['type'];
$field_name = $field['name'];
$field_description = $field['description'];
switch ($field_type){
case 'text':
// Task status is a status where fields should not be edited
if($this->_task_status_fields_should_be_read_only()) {
if($this->_user_manages_OR_supervises_task){
$meta_box->addText($field_id, array('std' => '', 'name' => $field_name, 'desc' => $field_description, 'style' => ''), false);
}else {
$meta_box->addText($field_id, array('std' => '', 'name' => $field_name, 'desc' => $field_description, 'style' => '', 'read_only' => TRUE), false);
}
}else{
$meta_box->addText($field_id, array('std' => '', 'name' => $field_name, 'desc' => $field_description, 'style' => ''), false);
}
break;
case 'textarea':
// Task status is a status where fields should not be edited
if($this->_task_status_fields_should_be_read_only()) {
if($this->_user_manages_OR_supervises_task) {
$meta_box->addTextarea($field_id, array('std' => '', 'name' => $field_name, 'desc' => $field_description, 'style' => ''), false);
}else{
$meta_box->addTextarea($field_id, array('std' => '', 'name' => $field_name, 'desc' => $field_description, 'style' => '', 'read_only' => TRUE), false);
}
}else {
$meta_box->addTextarea($field_id, array('std' => '', 'name' => $field_name, 'desc' => $field_description, 'style' => '', false));
}
break;
case 'file':
// Task status is a status where fields should not be edited
if($this->_task_status_fields_should_be_read_only()) {
if($this->_user_manages_OR_supervises_task){
$guid = get_post_meta($this->_post->ID, $field_id.'_file', TRUE);
$meta_box->addParagraph($field, array('value' =>
'<tr><td class="at-field">
<div class="at-label">
<label>' .$field_name ."</label>
<input type='file'
id='{$field_id}_file' name='{$field_id}_file'>
</div><div class='desc-field'>{$field_description}<br/><a href='$guid' target='_blank'>Download</a></div>
</td></tr>"));
}else {
$guid = get_post_meta($this->_post->ID, $field_id.'_file', TRUE);
$meta_box->addParagraph($field, array('value' =>
'<tr><td class="at-field">
<div class="at-label">
<label>' .$field_name ."</label>
</div><div class='desc-field'>{$field_description}<br/><a href='$guid' target='_blank'>Download</a></div>
</td></tr>"));
}
}else{
$guid = get_post_meta($this->_post->ID, $field_id.'_file', TRUE);
$meta_box->addParagraph($field, array('value' =>
'<tr><td class="at-field">
<div class="at-label">
<label>' .
$field_name .
"</label>
<input type='file'
id='{$field_id}_file' name='{$field_id}_file'>
</div><div class='desc-field'>{$field_description}<br/><a href='$guid' target='_blank'>Download</a></div>
</td></tr>"));
}
break;
case 'image':
// Task status is a status where fields should not be edited
if($this->_task_status_fields_should_be_read_only()) {
if($this->_user_manages_OR_supervises_task){
$guid = get_post_meta($this->_post->ID, $field_id.'_img', TRUE);
$meta_box->addParagraph($field, array('value' =>
'<tr><td class="at-field">
<div class="at-label">
<label>' .$field_name ."</label>
<input type='file'
id='{$field_id}_img' name='{$field_id}_img'
accept='image/png, image/jpeg'>
</div><div class='desc-field'>{$field_description}<img class='widefat' src='$guid'></div>
</td></tr>"));
}else {
$guid = get_post_meta($this->_post->ID, $field_id.'_img', TRUE);
$meta_box->addParagraph($field, array('value' =>
'<tr><td class="at-field">
<div class="at-label">
<label>' .$field_name ."</label>
</div><div class='desc-field'>{$field_description}<img class='widefat' src='$guid'></div>
</td></tr>"));
}
}else{
$guid = get_post_meta($this->_post->ID, $field_id.'_img', TRUE);
$meta_box->addParagraph($field, array('value' =>
'<tr><td class="at-field">
<div class="at-label">
<label>' .
$field_name .
"</label>
<input type='file'
id='{$field_id}_img' name='{$field_id}_img'
accept='image/png, image/jpeg'>
</div><div class='desc-field'>{$field_description}<img class='widefat' src='$guid'></div>
</td></tr>"));
}
break;
case 'select':
$field_options = $field['options'];
$field_options_rekeyed = array(); // reset options so they don't trickle to other boxes
$field_options_rekeyed['select'] = 'Select';
foreach ($field_options as $option) {
$field_options_rekeyed[str_replace(' ', '_', strtolower($option))] = $option;
}
// Task status is a status where fields should not be edited
if($this->_task_status_fields_should_be_read_only()) {
if($this->_user_manages_OR_supervises_task) {
$meta_box->addSelect($field_id, $field_options_rekeyed, array('name' => $field_name, 'desc' => $field_description, 'std' => array('select')));
} else {
// Display a readonly text field with value of this select
$meta_box->addText($field_id, array('std' => '', 'name' => $field_name, 'desc' => $field_description, 'style' => '', 'read_only' => TRUE), false);
}
}else{
$meta_box->addSelect($field_id, $field_options_rekeyed, array('name' => $field_name, 'desc' => $field_description, 'std' => array('select')));
}
break;
default:
throw new \Exception( /** @lang text */ 'Unexpected value' );
}
}
}else{
// Display paragraph stating no fields present.
$meta_box->addParagraph('no-fields', ['value' => 'No fields present.']);
}
$meta_box->finish();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment