PluginProbe
Assistant – Every Day Productivity Apps / 0.7.0.2
Assistant – Every Day Productivity Apps v0.7.0.2
1.5.5 trunk 0.1.0 0.2.0 0.2.1 0.2.2 0.3.0 0.3.1 0.4 0.4.1 0.4.2 0.5.0 0.5.1 0.6.0 0.7.0 0.7.0.2 0.7.0.3 0.7.0.4 0.7.0.5 0.7.0.6 0.7.0.7 0.7.0.8 0.7.0.9 0.7.1 1.0 All 71 releases
assistant / backend / src / Data / Repository / LabelsRepository.php

LabelsRepository.php in Assistant – Every Day Productivity Apps 0.7.0.2, at backend/src/Data/Repository/LabelsRepository.php

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