| 1 |
<?php |
| 2 |
/** |
| 3 |
* UpStream_Options_Tasks |
| 4 |
* |
| 5 |
* @package UpStream |
| 6 |
*/ |
| 7 |
|
| 8 |
// Exit if accessed directly. |
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
if ( ! class_exists( 'UpStream_Options_Tasks' ) ) : |
| 14 |
|
| 15 |
/** |
| 16 |
* CMB2 Theme Options |
| 17 |
* |
| 18 |
* @version 0.1.0 |
| 19 |
*/ |
| 20 |
class UpStream_Options_Tasks { |
| 21 |
|
| 22 |
|
| 23 |
/** |
| 24 |
* Array of metaboxes/fields |
| 25 |
* |
| 26 |
* @var array |
| 27 |
*/ |
| 28 |
public $id = 'upstream_tasks'; |
| 29 |
|
| 30 |
/** |
| 31 |
* Page title |
| 32 |
* |
| 33 |
* @var string |
| 34 |
*/ |
| 35 |
protected $title = ''; |
| 36 |
|
| 37 |
/** |
| 38 |
* Menu Title |
| 39 |
* |
| 40 |
* @var string |
| 41 |
*/ |
| 42 |
protected $menu_title = ''; |
| 43 |
|
| 44 |
/** |
| 45 |
* Menu Title |
| 46 |
* |
| 47 |
* @var string |
| 48 |
*/ |
| 49 |
protected $description = ''; |
| 50 |
|
| 51 |
/** |
| 52 |
* Holds an instance of the object |
| 53 |
* |
| 54 |
* @var Myprefix_Admin |
| 55 |
**/ |
| 56 |
public static $instance = null; |
| 57 |
|
| 58 |
/** |
| 59 |
* Constructor |
| 60 |
* |
| 61 |
* @since 0.1.0 |
| 62 |
*/ |
| 63 |
public function __construct() { |
| 64 |
// Set our title. |
| 65 |
$this->title = upstream_task_label_plural(); |
| 66 |
$this->menu_title = $this->title; |
| 67 |
// $this->description = sprintf( __( '%s Description', 'upstream' ), upstream_task_label() ); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Returns the running object |
| 72 |
* |
| 73 |
* @return Myprefix_Admin |
| 74 |
**/ |
| 75 |
public static function get_instance() { |
| 76 |
if ( is_null( self::$instance ) ) { |
| 77 |
self::$instance = new self(); |
| 78 |
} |
| 79 |
|
| 80 |
return self::$instance; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
/** |
| 85 |
* Add the options metabox to the array of metaboxes |
| 86 |
* |
| 87 |
* @since 0.1.0 |
| 88 |
*/ |
| 89 |
public function options() { |
| 90 |
$options = apply_filters( |
| 91 |
$this->id . '_option_fields', |
| 92 |
array( |
| 93 |
'id' => $this->id, // upstream_tasks. |
| 94 |
'title' => $this->title, |
| 95 |
'menu_title' => $this->menu_title, |
| 96 |
'desc' => $this->description, |
| 97 |
'show_on' => array( |
| 98 |
'key' => 'options-page', |
| 99 |
'value' => array( $this->id ), |
| 100 |
), |
| 101 |
'show_names' => true, |
| 102 |
'fields' => array( |
| 103 |
array( |
| 104 |
'name' => __( 'Statuses', 'upstream' ), |
| 105 |
'id' => 'status_title', |
| 106 |
'type' => 'title', |
| 107 |
'desc' => sprintf( |
| 108 |
// translators: %1$s: upstream_task_label_plural. |
| 109 |
// translators: %2$s: upstream_task_label. |
| 110 |
// translators: %3$s: upstream_task_label. |
| 111 |
__( |
| 112 |
'The statuses and colors that can be used for the status of %1$s.<br>These will become available in the %2$s Status dropdown within each %3$s', |
| 113 |
'upstream' |
| 114 |
), |
| 115 |
upstream_task_label_plural(), |
| 116 |
upstream_task_label(), |
| 117 |
upstream_task_label() |
| 118 |
), |
| 119 |
), |
| 120 |
array( |
| 121 |
'id' => 'statuses', |
| 122 |
'type' => 'group', |
| 123 |
'name' => '', |
| 124 |
'description' => '', |
| 125 |
'options' => array( |
| 126 |
'group_title' => __( 'Status {#}', 'upstream' ), |
| 127 |
'add_button' => __( 'Add Status', 'upstream' ), |
| 128 |
'remove_button' => __( 'Remove Entry', 'upstream' ), |
| 129 |
'sortable' => true, // beta. |
| 130 |
), |
| 131 |
'sanitization_cb' => array( 'UpStream_Admin', 'on_before_save' ), |
| 132 |
'fields' => array( |
| 133 |
array( |
| 134 |
'name' => __( 'Hidden', 'upstream' ), |
| 135 |
'id' => 'id', |
| 136 |
'type' => 'hidden', |
| 137 |
), |
| 138 |
array( |
| 139 |
'name' => __( 'Status Color', 'upstream' ), |
| 140 |
'id' => 'color', |
| 141 |
'type' => 'colorpicker', |
| 142 |
'attributes' => array( |
| 143 |
'data-colorpicker' => json_encode( |
| 144 |
array( |
| 145 |
// Iris Options set here as values in the 'data-colorpicker' array. |
| 146 |
'palettes' => upstream_colorpicker_default_colors(), |
| 147 |
'width' => 300, |
| 148 |
) |
| 149 |
), |
| 150 |
), |
| 151 |
), |
| 152 |
array( |
| 153 |
'name' => __( 'Status Name', 'upstream' ), |
| 154 |
'id' => 'name', |
| 155 |
'type' => 'text', |
| 156 |
), |
| 157 |
array( |
| 158 |
'name' => __( 'Type of Status', 'upstream' ), |
| 159 |
'id' => 'type', |
| 160 |
'type' => 'radio', |
| 161 |
'default' => 'open', |
| 162 |
'desc' => __( |
| 163 |
"A Status Name such as 'In Progress' or 'Overdue' would be considered Open.", |
| 164 |
'upstream' |
| 165 |
) . '<br>' . __( |
| 166 |
"A Status Name such as 'Complete' or 'Cancelled' would be considered Closed.", |
| 167 |
'upstream' |
| 168 |
), |
| 169 |
'options' => array( |
| 170 |
'open' => __( 'Open', 'upstream' ), |
| 171 |
'closed' => __( 'Closed', 'upstream' ), |
| 172 |
), |
| 173 |
), |
| 174 |
array( |
| 175 |
'name' => __( 'Percentage', 'upstream' ), |
| 176 |
'id' => 'percent', |
| 177 |
'type' => 'select', |
| 178 |
'options' => array( |
| 179 |
'0' => __( '0%' ), |
| 180 |
'5' => __( '5%' ), |
| 181 |
'10' => __( '10%' ), |
| 182 |
'15' => __( '15%' ), |
| 183 |
'20' => __( '20%' ), |
| 184 |
'25' => __( '25%' ), |
| 185 |
'30' => __( '30%' ), |
| 186 |
'35' => __( '35%' ), |
| 187 |
'40' => __( '40%' ), |
| 188 |
'45' => __( '45%' ), |
| 189 |
'50' => __( '50%' ), |
| 190 |
'55' => __( '55%' ), |
| 191 |
'60' => __( '60%' ), |
| 192 |
'65' => __( '65%' ), |
| 193 |
'70' => __( '70%' ), |
| 194 |
'75' => __( '75%' ), |
| 195 |
'80' => __( '80%' ), |
| 196 |
'85' => __( '85%' ), |
| 197 |
'90' => __( '90%' ), |
| 198 |
'95' => __( '95%' ), |
| 199 |
'100' => __( '100%' ), |
| 200 |
), |
| 201 |
), |
| 202 |
), |
| 203 |
), |
| 204 |
), |
| 205 |
) |
| 206 |
); |
| 207 |
|
| 208 |
return $options; |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Create ids for all existent tasks statuses. |
| 213 |
* |
| 214 |
* @since 1.17.0 |
| 215 |
* @static |
| 216 |
*/ |
| 217 |
public static function create_tasks_statuses_ids() { |
| 218 |
$continue = ! (bool) get_option( 'upstream:created_tasks_args_ids' ); |
| 219 |
if ( ! $continue ) { |
| 220 |
return; |
| 221 |
} |
| 222 |
|
| 223 |
$tasks = get_option( 'upstream_tasks' ); |
| 224 |
if ( isset( $tasks['statuses'] ) ) { |
| 225 |
$tasks['statuses'] = UpStream_Admin::create_missing_ids_in_set( $tasks['statuses'] ); |
| 226 |
|
| 227 |
update_option( 'upstream_tasks', $tasks ); |
| 228 |
|
| 229 |
$tasks = $tasks['statuses']; |
| 230 |
|
| 231 |
// Update existent Tasks status across all Projects. |
| 232 |
global $wpdb; |
| 233 |
|
| 234 |
$metas = $wpdb->get_results( |
| 235 |
sprintf( |
| 236 |
'SELECT `post_id`, `meta_value` |
| 237 |
FROM `%s` |
| 238 |
WHERE `meta_key` = "_upstream_project_tasks"', |
| 239 |
$wpdb->prefix . 'postmeta' |
| 240 |
) |
| 241 |
); |
| 242 |
|
| 243 |
if ( count( $metas ) > 0 ) { |
| 244 |
$get_task_status_id_by_title = function ( $needle ) use ( &$tasks ) { |
| 245 |
foreach ( $tasks as $task ) { |
| 246 |
if ( $needle === $task['name'] ) { |
| 247 |
return $task['id']; |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
return false; |
| 252 |
}; |
| 253 |
|
| 254 |
$replace_task_status_with_its_id = function ( $task ) use ( &$get_task_status_id_by_title ) { |
| 255 |
if ( isset( $task['status'] ) ) { |
| 256 |
$task_id = $get_task_status_id_by_title( $task['status'] ); |
| 257 |
if ( false !== $task_id ) { |
| 258 |
$task['status'] = $task_id; |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
return $task; |
| 263 |
}; |
| 264 |
|
| 265 |
foreach ( $metas as $meta ) { |
| 266 |
if ( empty( $meta->meta_value ) ) { |
| 267 |
continue; |
| 268 |
} |
| 269 |
|
| 270 |
$project_id = (int) $meta->post_id; |
| 271 |
|
| 272 |
$data = array_filter( maybe_unserialize( (string) $meta->meta_value ) ); |
| 273 |
$data = array_map( $replace_task_status_with_its_id, $data ); |
| 274 |
|
| 275 |
update_post_meta( $project_id, '_upstream_project_tasks', $data ); |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
update_option( 'upstream:created_tasks_args_ids', 1 ); |
| 280 |
} |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
endif; |
| 286 |
|