| 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 |
* @return array |
| 31 |
*/ |
| 32 |
public function count() { |
| 33 |
global $wpdb; |
| 34 |
|
| 35 |
$terms = $this->query( |
| 36 |
[ |
| 37 |
'hide_empty' => false, |
| 38 |
] |
| 39 |
)->get_terms(); |
| 40 |
|
| 41 |
if ( ! count( $terms ) ) { |
| 42 |
return []; |
| 43 |
} |
| 44 |
|
| 45 |
$counts = []; |
| 46 |
$subqueries = []; |
| 47 |
|
| 48 |
foreach ( $terms as $term ) { |
| 49 |
$subqueries[] = $wpdb->prepare( |
| 50 |
"(SELECT COUNT(*) |
| 51 |
FROM $wpdb->postmeta |
| 52 |
WHERE meta_key = 'fl_asst_notation_label_id' |
| 53 |
AND meta_value = %d) as label_%d", |
| 54 |
$term->term_id, |
| 55 |
$term->term_id |
| 56 |
); |
| 57 |
} |
| 58 |
|
| 59 |
$results = $wpdb->get_row( 'SELECT ' . implode( ',', $subqueries ) ); |
| 60 |
$counts = []; |
| 61 |
|
| 62 |
foreach ( $results as $key => $count ) { |
| 63 |
$key = str_replace( 'label_', '', $key ); |
| 64 |
$counts[ $key ] = $count; |
| 65 |
} |
| 66 |
|
| 67 |
return $counts; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* @param string $object_type |
| 72 |
* @param int $label_id |
| 73 |
* @return array |
| 74 |
*/ |
| 75 |
function get_object_ids( $object_type, $label_id ) { |
| 76 |
global $wpdb; |
| 77 |
|
| 78 |
$results = $wpdb->get_results( |
| 79 |
$wpdb->prepare( |
| 80 |
"SELECT m1.meta_value |
| 81 |
FROM $wpdb->postmeta m1 |
| 82 |
INNER JOIN $wpdb->postmeta m2 |
| 83 |
ON m1.post_id = m2.post_id |
| 84 |
AND m1.meta_key = 'fl_asst_notation_object_id' |
| 85 |
AND m2.meta_key = 'fl_asst_notation_label_id' |
| 86 |
AND m2.meta_value = %d |
| 87 |
INNER JOIN $wpdb->postmeta m3 |
| 88 |
ON m1.post_id = m3.post_id |
| 89 |
AND m3.meta_key = 'fl_asst_notation_object_type' |
| 90 |
AND m3.meta_value = %s", |
| 91 |
$label_id, |
| 92 |
$object_type |
| 93 |
) |
| 94 |
); |
| 95 |
|
| 96 |
$ids = []; |
| 97 |
|
| 98 |
foreach ( $results as $result ) { |
| 99 |
$ids[] = $result->meta_value; |
| 100 |
} |
| 101 |
|
| 102 |
return $ids; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* @return void |
| 107 |
*/ |
| 108 |
public function save_defaults() { |
| 109 |
$did_install = get_option( static::FL_ASST_INSTALLED_LABELS, false ); |
| 110 |
$existing = $this->query( |
| 111 |
[ |
| 112 |
'hide_empty' => false, |
| 113 |
] |
| 114 |
)->get_terms(); |
| 115 |
|
| 116 |
if ( $did_install || count( $existing ) > 0 ) { |
| 117 |
return; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Note: Colors changed here need to be changed in |
| 122 |
* Color.knownColors on the frontend as well. |
| 123 |
* |
| 124 |
* TODO: Load from server config. |
| 125 |
*/ |
| 126 |
$default_labels = [ |
| 127 |
'#FF305C' => __( 'Red', 'assistant' ), |
| 128 |
'#FF9500' => __( 'Orange', 'assistant' ), |
| 129 |
'#FFD000' => __( 'Yellow', 'assistant' ), |
| 130 |
'#00D281' => __( 'Green', 'assistant' ), |
| 131 |
'#1BADF8' => __( 'Blue', 'assistant' ), |
| 132 |
]; |
| 133 |
|
| 134 |
foreach ( $default_labels as $color => $label ) { |
| 135 |
$result = wp_insert_term( |
| 136 |
$label, 'fl_asst_label', [ |
| 137 |
'slug' => sanitize_key( $label ), |
| 138 |
] |
| 139 |
); |
| 140 |
|
| 141 |
if ( ! is_wp_error( $result ) ) { |
| 142 |
update_term_meta( $result['term_id'], static::FL_ASST_NOTATION_COLOR, $color ); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
update_option( static::FL_ASST_INSTALLED_LABELS, true ); |
| 147 |
} |
| 148 |
} |
| 149 |
|