| 1 |
<?php |
| 2 |
|
| 3 |
namespace FL\Assistant\Data\Repository; |
| 4 |
|
| 5 |
|
| 6 |
class LabelsRepository extends TermsRepository { |
| 7 |
|
| 8 |
/** |
| 9 |
* Storage keys. |
| 10 |
*/ |
| 11 |
const FL_ASST_INSTALLED_LABELS = 'fl_asst_installed_labels'; |
| 12 |
const FL_ASST_NOTATION_COLOR = 'fl_asst_notation_color'; |
| 13 |
|
| 14 |
/** |
| 15 |
* @param array $args |
| 16 |
* |
| 17 |
* @return \WP_Query |
| 18 |
*/ |
| 19 |
public function query( array $args = [] ) { |
| 20 |
$args = array_merge( |
| 21 |
$args, [ |
| 22 |
'taxonomy' => 'fl_asst_label', |
| 23 |
] |
| 24 |
); |
| 25 |
|
| 26 |
return parent::query( $args ); |
| 27 |
} |
| 28 |
|
| 29 |
public function save_defaults() { |
| 30 |
$did_install = get_option( static::FL_ASST_INSTALLED_LABELS, false ); |
| 31 |
$existing = $this->query( |
| 32 |
[ |
| 33 |
'hide_empty' => false, |
| 34 |
] |
| 35 |
)->get_terms(); |
| 36 |
|
| 37 |
if ( $did_install || count( $existing ) > 0 ) { |
| 38 |
return; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Note: Colors changed here need to be changed in |
| 43 |
* Color.knownColors on the frontend as well. |
| 44 |
* |
| 45 |
* TODO: Load from server config. |
| 46 |
*/ |
| 47 |
$default_labels = [ |
| 48 |
'#FF305C' => __( 'Red', 'fl-assistant' ), |
| 49 |
'#FF9500' => __( 'Orange', 'fl-assistant' ), |
| 50 |
'#FFD000' => __( 'Yellow', 'fl-assistant' ), |
| 51 |
'#00D281' => __( 'Green', 'fl-assistant' ), |
| 52 |
'#1BADF8' => __( 'Blue', 'fl-assistant' ), |
| 53 |
]; |
| 54 |
|
| 55 |
foreach ( $default_labels as $color => $label ) { |
| 56 |
$result = wp_insert_term( |
| 57 |
$label, 'fl_asst_label', [ |
| 58 |
'slug' => sanitize_key( $label ), |
| 59 |
] |
| 60 |
); |
| 61 |
|
| 62 |
if ( ! is_wp_error( $result ) ) { |
| 63 |
update_term_meta( $result['term_id'], static::FL_ASST_NOTATION_COLOR, $color ); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
update_option( static::FL_ASST_INSTALLED_LABELS, true ); |
| 68 |
} |
| 69 |
} |
| 70 |
|