| 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 |
/** |
| 30 |
* @param string $object_type |
| 31 |
* @param int $label_id |
| 32 |
* @return array |
| 33 |
*/ |
| 34 |
function get_object_ids( $object_type, $label_id ) { |
| 35 |
global $wpdb; |
| 36 |
|
| 37 |
$sql = "SELECT m1.meta_value |
| 38 |
FROM $wpdb->postmeta m1 |
| 39 |
INNER JOIN $wpdb->postmeta m2 |
| 40 |
ON m1.post_id = m2.post_id |
| 41 |
AND m1.meta_key = 'fl_asst_notation_object_id' |
| 42 |
AND m2.meta_key = 'fl_asst_notation_label_id' |
| 43 |
AND m2.meta_value = '%d' |
| 44 |
INNER JOIN $wpdb->postmeta m3 |
| 45 |
ON m1.post_id = m3.post_id |
| 46 |
AND m3.meta_key = 'fl_asst_notation_object_type' |
| 47 |
AND m3.meta_value = '%s'"; |
| 48 |
|
| 49 |
$prepared = $wpdb->prepare( $sql, $label_id, $object_type ); |
| 50 |
$results = $wpdb->get_results( $prepared ); |
| 51 |
$ids = []; |
| 52 |
|
| 53 |
foreach ( $results as $result ) { |
| 54 |
$ids[] = $result->meta_value; |
| 55 |
} |
| 56 |
|
| 57 |
return $ids; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* @return void |
| 62 |
*/ |
| 63 |
public function save_defaults() { |
| 64 |
$did_install = get_option( static::FL_ASST_INSTALLED_LABELS, false ); |
| 65 |
$existing = $this->query( |
| 66 |
[ |
| 67 |
'hide_empty' => false, |
| 68 |
] |
| 69 |
)->get_terms(); |
| 70 |
|
| 71 |
if ( $did_install || count( $existing ) > 0 ) { |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Note: Colors changed here need to be changed in |
| 77 |
* Color.knownColors on the frontend as well. |
| 78 |
* |
| 79 |
* TODO: Load from server config. |
| 80 |
*/ |
| 81 |
$default_labels = [ |
| 82 |
'#FF305C' => __( 'Red', 'fl-assistant' ), |
| 83 |
'#FF9500' => __( 'Orange', 'fl-assistant' ), |
| 84 |
'#FFD000' => __( 'Yellow', 'fl-assistant' ), |
| 85 |
'#00D281' => __( 'Green', 'fl-assistant' ), |
| 86 |
'#1BADF8' => __( 'Blue', 'fl-assistant' ), |
| 87 |
]; |
| 88 |
|
| 89 |
foreach ( $default_labels as $color => $label ) { |
| 90 |
$result = wp_insert_term( |
| 91 |
$label, 'fl_asst_label', [ |
| 92 |
'slug' => sanitize_key( $label ), |
| 93 |
] |
| 94 |
); |
| 95 |
|
| 96 |
if ( ! is_wp_error( $result ) ) { |
| 97 |
update_term_meta( $result['term_id'], static::FL_ASST_NOTATION_COLOR, $color ); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
update_option( static::FL_ASST_INSTALLED_LABELS, true ); |
| 102 |
} |
| 103 |
} |
| 104 |
|