@@ -7,249 +7,249 @@ |
||
| 7 | 7 | |
| 8 | 8 | trait QuerySql |
| 9 | 9 | { |
| 10 | - public $args; |
|
| 11 | - public $db; |
|
| 12 | - |
|
| 13 | - public function escFieldsForInsert(array $fields) |
|
| 14 | - { |
|
| 15 | - return sprintf('(`%s`)', implode('`,`', $fields)); |
|
| 16 | - } |
|
| 17 | - |
|
| 18 | - public function escValuesForInsert(array $values) |
|
| 19 | - { |
|
| 20 | - $values = array_map('esc_sql', $values); |
|
| 21 | - return sprintf("('%s')", implode("','", array_values($values))); |
|
| 22 | - } |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * @param string $clause |
|
| 26 | - * @return array |
|
| 27 | - */ |
|
| 28 | - public function sqlClauses(array $values, $clause) |
|
| 29 | - { |
|
| 30 | - $prefix = Str::restrictTo('and, join', $clause); |
|
| 31 | - foreach (array_keys($this->args) as $key) { |
|
| 32 | - $method = Helper::buildMethodName($key, 'clause-'.$prefix); |
|
| 33 | - if (method_exists($this, $method)) { |
|
| 34 | - $values[] = call_user_func([$this, $method]); |
|
| 35 | - } |
|
| 36 | - } |
|
| 37 | - return $values; |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * @return string |
|
| 42 | - */ |
|
| 43 | - public function sqlFrom() |
|
| 44 | - { |
|
| 45 | - $from = "FROM {$this->table('ratings')} r"; |
|
| 46 | - $from = glsr()->filterString('query/sql/from', $from, $this); |
|
| 47 | - return $from; |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * @return string |
|
| 52 | - */ |
|
| 53 | - public function sqlGroupBy() |
|
| 54 | - { |
|
| 55 | - $groupBy = 'GROUP BY p.ID'; |
|
| 56 | - return glsr()->filterString('query/sql/group-by', $groupBy, $this); |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * @return string |
|
| 61 | - */ |
|
| 62 | - public function sqlJoin() |
|
| 63 | - { |
|
| 64 | - $join = [ |
|
| 65 | - "INNER JOIN {$this->db->posts} AS p ON r.review_id = p.ID", |
|
| 66 | - ]; |
|
| 67 | - $join = glsr()->filterArray('query/sql/join', $join, $this); |
|
| 68 | - return implode(' ', $join); |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * @return string |
|
| 73 | - */ |
|
| 74 | - public function sqlJoinClauses() |
|
| 75 | - { |
|
| 76 | - $join = $this->sqlClauses([], 'join'); |
|
| 77 | - $join = glsr()->filterArray('query/sql/join-clauses', $join, $this); |
|
| 78 | - return trim($this->sqlJoin().' '.implode(' ', $join)); |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * @return string |
|
| 83 | - */ |
|
| 84 | - public function sqlJoinPivots() |
|
| 85 | - { |
|
| 86 | - $join = [ |
|
| 87 | - "LEFT JOIN {$this->table('assigned_posts')} apt on r.ID = apt.rating_id", |
|
| 88 | - "LEFT JOIN {$this->table('assigned_terms')} att on r.ID = att.rating_id", |
|
| 89 | - "LEFT JOIN {$this->table('assigned_users')} aut on r.ID = aut.rating_id", |
|
| 90 | - ]; |
|
| 91 | - $join = glsr()->filterArray('query/sql/join-pivots', $join, $this); |
|
| 92 | - return implode(' ', $join); |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * @return string |
|
| 97 | - */ |
|
| 98 | - public function sqlLimit() |
|
| 99 | - { |
|
| 100 | - $limit = $this->args['per_page'] > 0 |
|
| 101 | - ? $this->db->prepare('LIMIT %d', $this->args['per_page']) |
|
| 102 | - : ''; |
|
| 103 | - return glsr()->filterString('query/sql/limit', $limit, $this); |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * @return string |
|
| 108 | - */ |
|
| 109 | - public function sqlOffset() |
|
| 110 | - { |
|
| 111 | - $offsetBy = (($this->args['page'] - 1) * $this->args['per_page']) + $this->args['offset']; |
|
| 112 | - $offset = ($offsetBy > 0) |
|
| 113 | - ? $this->db->prepare('OFFSET %d', $offsetBy) |
|
| 114 | - : ''; |
|
| 115 | - return glsr()->filterString('query/sql/offset', $offset, $this); |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * @return string |
|
| 120 | - */ |
|
| 121 | - public function sqlOrderBy() |
|
| 122 | - { |
|
| 123 | - $values = [ |
|
| 124 | - 'none' => '', |
|
| 125 | - 'rand' => 'ORDER BY RAND()', |
|
| 126 | - 'relevance' => '', |
|
| 127 | - ]; |
|
| 128 | - $order = $this->args['order']; |
|
| 129 | - $orderby = $this->args['orderby']; |
|
| 130 | - if (Str::startsWith('p.', $orderby)) { |
|
| 131 | - $orderBy = "ORDER BY r.is_pinned {$order}, {$orderby} {$order}"; |
|
| 132 | - } elseif (array_key_exists($orderby, $values)) { |
|
| 133 | - $orderBy = $orderby; |
|
| 134 | - } else { |
|
| 135 | - $orderBy = ''; |
|
| 136 | - } |
|
| 137 | - return glsr()->filterString('query/sql/order-by', $orderBy, $this); |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * @return string |
|
| 142 | - */ |
|
| 143 | - public function sqlSelect() |
|
| 144 | - { |
|
| 145 | - $select = [ |
|
| 146 | - 'r.*', |
|
| 147 | - 'p.post_author as author_id', |
|
| 148 | - 'p.post_date as date', |
|
| 149 | - 'p.post_content as content', |
|
| 150 | - 'p.post_title as title', |
|
| 151 | - 'p.post_status as status', |
|
| 152 | - 'GROUP_CONCAT(DISTINCT apt.post_id) as post_ids', |
|
| 153 | - 'GROUP_CONCAT(DISTINCT att.term_id) as term_ids', |
|
| 154 | - 'GROUP_CONCAT(DISTINCT aut.user_id) as user_ids', |
|
| 155 | - ]; |
|
| 156 | - $select = glsr()->filterArray('query/sql/select', $select, $this); |
|
| 157 | - $select = implode(', ', $select); |
|
| 158 | - return "SELECT {$select}"; |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * @return string |
|
| 163 | - */ |
|
| 164 | - public function sqlWhere() |
|
| 165 | - { |
|
| 166 | - $where = [ |
|
| 167 | - $this->db->prepare('AND p.post_type = %s', glsr()->post_type), |
|
| 168 | - "AND p.post_status = 'publish'", |
|
| 169 | - ]; |
|
| 170 | - $where = $this->sqlClauses($where, 'and'); |
|
| 171 | - $where = glsr()->filterArray('query/sql/where', $where, $this); |
|
| 172 | - $where = implode(' ', $where); |
|
| 173 | - return "WHERE 1=1 {$where}"; |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - /** |
|
| 177 | - * @return string |
|
| 178 | - */ |
|
| 179 | - public function table($table) |
|
| 180 | - { |
|
| 181 | - return glsr(SqlSchema::class)->table($table); |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - /** |
|
| 185 | - * This takes care of assigned_to, category, and user. |
|
| 186 | - * @return string |
|
| 187 | - */ |
|
| 188 | - protected function clauseAndAssignedTo() |
|
| 189 | - { |
|
| 190 | - $clauses = []; |
|
| 191 | - if ($postIds = $this->args['assigned_to']) { |
|
| 192 | - $clauses[] = $this->db->prepare('(apt.post_id IN (%s) AND apt.is_published = 1)', implode(',', $postIds)); |
|
| 193 | - } |
|
| 194 | - if ($termIds = $this->args['category']) { |
|
| 195 | - $clauses[] = $this->db->prepare('(att.term_id IN (%s))', implode(',', $termIds)); |
|
| 196 | - } |
|
| 197 | - if ($userIds = $this->args['user']) { |
|
| 198 | - $clauses[] = $this->db->prepare('(aut.user_id IN (%s))', implode(',', $userIds)); |
|
| 199 | - } |
|
| 200 | - if ($clauses = implode(' OR ', $clauses)) { |
|
| 201 | - return "AND ($clauses)"; |
|
| 202 | - } |
|
| 203 | - return ''; |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - /** |
|
| 207 | - * @return string |
|
| 208 | - */ |
|
| 209 | - protected function clauseAndRating() |
|
| 210 | - { |
|
| 211 | - return $this->args['rating'] |
|
| 212 | - ? $this->db->prepare('AND r.rating > %d', --$this->args['rating']) |
|
| 213 | - : ''; |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - /** |
|
| 217 | - * @return string |
|
| 218 | - */ |
|
| 219 | - protected function clauseAndType() |
|
| 220 | - { |
|
| 221 | - return $this->args['type'] |
|
| 222 | - ? $this->db->prepare('AND r.type = %s', $this->args['type']) |
|
| 223 | - : ''; |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - /** |
|
| 227 | - * @return string |
|
| 228 | - */ |
|
| 229 | - protected function clauseJoinAssignedTo() |
|
| 230 | - { |
|
| 231 | - return !empty($this->args['assigned_to']) |
|
| 232 | - ? "INNER JOIN {$this->table('assigned_posts')} AS apt ON r.ID = apt.rating_id" |
|
| 233 | - : ''; |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - /** |
|
| 237 | - * @return string |
|
| 238 | - */ |
|
| 239 | - protected function clauseJoinCategory() |
|
| 240 | - { |
|
| 241 | - return !empty($this->args['category']) |
|
| 242 | - ? "INNER JOIN {$this->table('assigned_terms')} AS att ON r.ID = att.rating_id" |
|
| 243 | - : ''; |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - /** |
|
| 247 | - * @return string |
|
| 248 | - */ |
|
| 249 | - protected function clauseJoinUser() |
|
| 250 | - { |
|
| 251 | - return !empty($this->args['user']) |
|
| 252 | - ? "INNER JOIN {$this->table('assigned_users')} AS aut ON r.ID = aut.rating_id" |
|
| 253 | - : ''; |
|
| 254 | - } |
|
| 10 | + public $args; |
|
| 11 | + public $db; |
|
| 12 | + |
|
| 13 | + public function escFieldsForInsert(array $fields) |
|
| 14 | + { |
|
| 15 | + return sprintf('(`%s`)', implode('`,`', $fields)); |
|
| 16 | + } |
|
| 17 | + |
|
| 18 | + public function escValuesForInsert(array $values) |
|
| 19 | + { |
|
| 20 | + $values = array_map('esc_sql', $values); |
|
| 21 | + return sprintf("('%s')", implode("','", array_values($values))); |
|
| 22 | + } |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * @param string $clause |
|
| 26 | + * @return array |
|
| 27 | + */ |
|
| 28 | + public function sqlClauses(array $values, $clause) |
|
| 29 | + { |
|
| 30 | + $prefix = Str::restrictTo('and, join', $clause); |
|
| 31 | + foreach (array_keys($this->args) as $key) { |
|
| 32 | + $method = Helper::buildMethodName($key, 'clause-'.$prefix); |
|
| 33 | + if (method_exists($this, $method)) { |
|
| 34 | + $values[] = call_user_func([$this, $method]); |
|
| 35 | + } |
|
| 36 | + } |
|
| 37 | + return $values; |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * @return string |
|
| 42 | + */ |
|
| 43 | + public function sqlFrom() |
|
| 44 | + { |
|
| 45 | + $from = "FROM {$this->table('ratings')} r"; |
|
| 46 | + $from = glsr()->filterString('query/sql/from', $from, $this); |
|
| 47 | + return $from; |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * @return string |
|
| 52 | + */ |
|
| 53 | + public function sqlGroupBy() |
|
| 54 | + { |
|
| 55 | + $groupBy = 'GROUP BY p.ID'; |
|
| 56 | + return glsr()->filterString('query/sql/group-by', $groupBy, $this); |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * @return string |
|
| 61 | + */ |
|
| 62 | + public function sqlJoin() |
|
| 63 | + { |
|
| 64 | + $join = [ |
|
| 65 | + "INNER JOIN {$this->db->posts} AS p ON r.review_id = p.ID", |
|
| 66 | + ]; |
|
| 67 | + $join = glsr()->filterArray('query/sql/join', $join, $this); |
|
| 68 | + return implode(' ', $join); |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * @return string |
|
| 73 | + */ |
|
| 74 | + public function sqlJoinClauses() |
|
| 75 | + { |
|
| 76 | + $join = $this->sqlClauses([], 'join'); |
|
| 77 | + $join = glsr()->filterArray('query/sql/join-clauses', $join, $this); |
|
| 78 | + return trim($this->sqlJoin().' '.implode(' ', $join)); |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * @return string |
|
| 83 | + */ |
|
| 84 | + public function sqlJoinPivots() |
|
| 85 | + { |
|
| 86 | + $join = [ |
|
| 87 | + "LEFT JOIN {$this->table('assigned_posts')} apt on r.ID = apt.rating_id", |
|
| 88 | + "LEFT JOIN {$this->table('assigned_terms')} att on r.ID = att.rating_id", |
|
| 89 | + "LEFT JOIN {$this->table('assigned_users')} aut on r.ID = aut.rating_id", |
|
| 90 | + ]; |
|
| 91 | + $join = glsr()->filterArray('query/sql/join-pivots', $join, $this); |
|
| 92 | + return implode(' ', $join); |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * @return string |
|
| 97 | + */ |
|
| 98 | + public function sqlLimit() |
|
| 99 | + { |
|
| 100 | + $limit = $this->args['per_page'] > 0 |
|
| 101 | + ? $this->db->prepare('LIMIT %d', $this->args['per_page']) |
|
| 102 | + : ''; |
|
| 103 | + return glsr()->filterString('query/sql/limit', $limit, $this); |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * @return string |
|
| 108 | + */ |
|
| 109 | + public function sqlOffset() |
|
| 110 | + { |
|
| 111 | + $offsetBy = (($this->args['page'] - 1) * $this->args['per_page']) + $this->args['offset']; |
|
| 112 | + $offset = ($offsetBy > 0) |
|
| 113 | + ? $this->db->prepare('OFFSET %d', $offsetBy) |
|
| 114 | + : ''; |
|
| 115 | + return glsr()->filterString('query/sql/offset', $offset, $this); |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * @return string |
|
| 120 | + */ |
|
| 121 | + public function sqlOrderBy() |
|
| 122 | + { |
|
| 123 | + $values = [ |
|
| 124 | + 'none' => '', |
|
| 125 | + 'rand' => 'ORDER BY RAND()', |
|
| 126 | + 'relevance' => '', |
|
| 127 | + ]; |
|
| 128 | + $order = $this->args['order']; |
|
| 129 | + $orderby = $this->args['orderby']; |
|
| 130 | + if (Str::startsWith('p.', $orderby)) { |
|
| 131 | + $orderBy = "ORDER BY r.is_pinned {$order}, {$orderby} {$order}"; |
|
| 132 | + } elseif (array_key_exists($orderby, $values)) { |
|
| 133 | + $orderBy = $orderby; |
|
| 134 | + } else { |
|
| 135 | + $orderBy = ''; |
|
| 136 | + } |
|
| 137 | + return glsr()->filterString('query/sql/order-by', $orderBy, $this); |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * @return string |
|
| 142 | + */ |
|
| 143 | + public function sqlSelect() |
|
| 144 | + { |
|
| 145 | + $select = [ |
|
| 146 | + 'r.*', |
|
| 147 | + 'p.post_author as author_id', |
|
| 148 | + 'p.post_date as date', |
|
| 149 | + 'p.post_content as content', |
|
| 150 | + 'p.post_title as title', |
|
| 151 | + 'p.post_status as status', |
|
| 152 | + 'GROUP_CONCAT(DISTINCT apt.post_id) as post_ids', |
|
| 153 | + 'GROUP_CONCAT(DISTINCT att.term_id) as term_ids', |
|
| 154 | + 'GROUP_CONCAT(DISTINCT aut.user_id) as user_ids', |
|
| 155 | + ]; |
|
| 156 | + $select = glsr()->filterArray('query/sql/select', $select, $this); |
|
| 157 | + $select = implode(', ', $select); |
|
| 158 | + return "SELECT {$select}"; |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * @return string |
|
| 163 | + */ |
|
| 164 | + public function sqlWhere() |
|
| 165 | + { |
|
| 166 | + $where = [ |
|
| 167 | + $this->db->prepare('AND p.post_type = %s', glsr()->post_type), |
|
| 168 | + "AND p.post_status = 'publish'", |
|
| 169 | + ]; |
|
| 170 | + $where = $this->sqlClauses($where, 'and'); |
|
| 171 | + $where = glsr()->filterArray('query/sql/where', $where, $this); |
|
| 172 | + $where = implode(' ', $where); |
|
| 173 | + return "WHERE 1=1 {$where}"; |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + /** |
|
| 177 | + * @return string |
|
| 178 | + */ |
|
| 179 | + public function table($table) |
|
| 180 | + { |
|
| 181 | + return glsr(SqlSchema::class)->table($table); |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + /** |
|
| 185 | + * This takes care of assigned_to, category, and user. |
|
| 186 | + * @return string |
|
| 187 | + */ |
|
| 188 | + protected function clauseAndAssignedTo() |
|
| 189 | + { |
|
| 190 | + $clauses = []; |
|
| 191 | + if ($postIds = $this->args['assigned_to']) { |
|
| 192 | + $clauses[] = $this->db->prepare('(apt.post_id IN (%s) AND apt.is_published = 1)', implode(',', $postIds)); |
|
| 193 | + } |
|
| 194 | + if ($termIds = $this->args['category']) { |
|
| 195 | + $clauses[] = $this->db->prepare('(att.term_id IN (%s))', implode(',', $termIds)); |
|
| 196 | + } |
|
| 197 | + if ($userIds = $this->args['user']) { |
|
| 198 | + $clauses[] = $this->db->prepare('(aut.user_id IN (%s))', implode(',', $userIds)); |
|
| 199 | + } |
|
| 200 | + if ($clauses = implode(' OR ', $clauses)) { |
|
| 201 | + return "AND ($clauses)"; |
|
| 202 | + } |
|
| 203 | + return ''; |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + /** |
|
| 207 | + * @return string |
|
| 208 | + */ |
|
| 209 | + protected function clauseAndRating() |
|
| 210 | + { |
|
| 211 | + return $this->args['rating'] |
|
| 212 | + ? $this->db->prepare('AND r.rating > %d', --$this->args['rating']) |
|
| 213 | + : ''; |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + /** |
|
| 217 | + * @return string |
|
| 218 | + */ |
|
| 219 | + protected function clauseAndType() |
|
| 220 | + { |
|
| 221 | + return $this->args['type'] |
|
| 222 | + ? $this->db->prepare('AND r.type = %s', $this->args['type']) |
|
| 223 | + : ''; |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + /** |
|
| 227 | + * @return string |
|
| 228 | + */ |
|
| 229 | + protected function clauseJoinAssignedTo() |
|
| 230 | + { |
|
| 231 | + return !empty($this->args['assigned_to']) |
|
| 232 | + ? "INNER JOIN {$this->table('assigned_posts')} AS apt ON r.ID = apt.rating_id" |
|
| 233 | + : ''; |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + /** |
|
| 237 | + * @return string |
|
| 238 | + */ |
|
| 239 | + protected function clauseJoinCategory() |
|
| 240 | + { |
|
| 241 | + return !empty($this->args['category']) |
|
| 242 | + ? "INNER JOIN {$this->table('assigned_terms')} AS att ON r.ID = att.rating_id" |
|
| 243 | + : ''; |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + /** |
|
| 247 | + * @return string |
|
| 248 | + */ |
|
| 249 | + protected function clauseJoinUser() |
|
| 250 | + { |
|
| 251 | + return !empty($this->args['user']) |
|
| 252 | + ? "INNER JOIN {$this->table('assigned_users')} AS aut ON r.ID = aut.rating_id" |
|
| 253 | + : ''; |
|
| 254 | + } |
|
| 255 | 255 | } |
@@ -10,28 +10,28 @@ discard block |
||
| 10 | 10 | public $args; |
| 11 | 11 | public $db; |
| 12 | 12 | |
| 13 | - public function escFieldsForInsert(array $fields) |
|
| 13 | + public function escFieldsForInsert( array $fields ) |
|
| 14 | 14 | { |
| 15 | - return sprintf('(`%s`)', implode('`,`', $fields)); |
|
| 15 | + return sprintf( '(`%s`)', implode( '`,`', $fields ) ); |
|
| 16 | 16 | } |
| 17 | 17 | |
| 18 | - public function escValuesForInsert(array $values) |
|
| 18 | + public function escValuesForInsert( array $values ) |
|
| 19 | 19 | { |
| 20 | - $values = array_map('esc_sql', $values); |
|
| 21 | - return sprintf("('%s')", implode("','", array_values($values))); |
|
| 20 | + $values = array_map( 'esc_sql', $values ); |
|
| 21 | + return sprintf( "('%s')", implode( "','", array_values( $values ) ) ); |
|
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 24 | /** |
| 25 | 25 | * @param string $clause |
| 26 | 26 | * @return array |
| 27 | 27 | */ |
| 28 | - public function sqlClauses(array $values, $clause) |
|
| 28 | + public function sqlClauses( array $values, $clause ) |
|
| 29 | 29 | { |
| 30 | - $prefix = Str::restrictTo('and, join', $clause); |
|
| 31 | - foreach (array_keys($this->args) as $key) { |
|
| 32 | - $method = Helper::buildMethodName($key, 'clause-'.$prefix); |
|
| 33 | - if (method_exists($this, $method)) { |
|
| 34 | - $values[] = call_user_func([$this, $method]); |
|
| 30 | + $prefix = Str::restrictTo( 'and, join', $clause ); |
|
| 31 | + foreach( array_keys( $this->args ) as $key ) { |
|
| 32 | + $method = Helper::buildMethodName( $key, 'clause-'.$prefix ); |
|
| 33 | + if( method_exists( $this, $method ) ) { |
|
| 34 | + $values[] = call_user_func( [$this, $method] ); |
|
| 35 | 35 | } |
| 36 | 36 | } |
| 37 | 37 | return $values; |
@@ -42,8 +42,8 @@ discard block |
||
| 42 | 42 | */ |
| 43 | 43 | public function sqlFrom() |
| 44 | 44 | { |
| 45 | - $from = "FROM {$this->table('ratings')} r"; |
|
| 46 | - $from = glsr()->filterString('query/sql/from', $from, $this); |
|
| 45 | + $from = "FROM {$this->table( 'ratings' )} r"; |
|
| 46 | + $from = glsr()->filterString( 'query/sql/from', $from, $this ); |
|
| 47 | 47 | return $from; |
| 48 | 48 | } |
| 49 | 49 | |
@@ -53,7 +53,7 @@ discard block |
||
| 53 | 53 | public function sqlGroupBy() |
| 54 | 54 | { |
| 55 | 55 | $groupBy = 'GROUP BY p.ID'; |
| 56 | - return glsr()->filterString('query/sql/group-by', $groupBy, $this); |
|
| 56 | + return glsr()->filterString( 'query/sql/group-by', $groupBy, $this ); |
|
| 57 | 57 | } |
| 58 | 58 | |
| 59 | 59 | /** |
@@ -64,8 +64,8 @@ discard block |
||
| 64 | 64 | $join = [ |
| 65 | 65 | "INNER JOIN {$this->db->posts} AS p ON r.review_id = p.ID", |
| 66 | 66 | ]; |
| 67 | - $join = glsr()->filterArray('query/sql/join', $join, $this); |
|
| 68 | - return implode(' ', $join); |
|
| 67 | + $join = glsr()->filterArray( 'query/sql/join', $join, $this ); |
|
| 68 | + return implode( ' ', $join ); |
|
| 69 | 69 | } |
| 70 | 70 | |
| 71 | 71 | /** |
@@ -73,9 +73,9 @@ discard block |
||
| 73 | 73 | */ |
| 74 | 74 | public function sqlJoinClauses() |
| 75 | 75 | { |
| 76 | - $join = $this->sqlClauses([], 'join'); |
|
| 77 | - $join = glsr()->filterArray('query/sql/join-clauses', $join, $this); |
|
| 78 | - return trim($this->sqlJoin().' '.implode(' ', $join)); |
|
| 76 | + $join = $this->sqlClauses( [], 'join' ); |
|
| 77 | + $join = glsr()->filterArray( 'query/sql/join-clauses', $join, $this ); |
|
| 78 | + return trim( $this->sqlJoin().' '.implode( ' ', $join ) ); |
|
| 79 | 79 | } |
| 80 | 80 | |
| 81 | 81 | /** |
@@ -84,12 +84,12 @@ discard block |
||
| 84 | 84 | public function sqlJoinPivots() |
| 85 | 85 | { |
| 86 | 86 | $join = [ |
| 87 | - "LEFT JOIN {$this->table('assigned_posts')} apt on r.ID = apt.rating_id", |
|
| 88 | - "LEFT JOIN {$this->table('assigned_terms')} att on r.ID = att.rating_id", |
|
| 89 | - "LEFT JOIN {$this->table('assigned_users')} aut on r.ID = aut.rating_id", |
|
| 87 | + "LEFT JOIN {$this->table( 'assigned_posts' )} apt on r.ID = apt.rating_id", |
|
| 88 | + "LEFT JOIN {$this->table( 'assigned_terms' )} att on r.ID = att.rating_id", |
|
| 89 | + "LEFT JOIN {$this->table( 'assigned_users' )} aut on r.ID = aut.rating_id", |
|
| 90 | 90 | ]; |
| 91 | - $join = glsr()->filterArray('query/sql/join-pivots', $join, $this); |
|
| 92 | - return implode(' ', $join); |
|
| 91 | + $join = glsr()->filterArray( 'query/sql/join-pivots', $join, $this ); |
|
| 92 | + return implode( ' ', $join ); |
|
| 93 | 93 | } |
| 94 | 94 | |
| 95 | 95 | /** |
@@ -98,9 +98,9 @@ discard block |
||
| 98 | 98 | public function sqlLimit() |
| 99 | 99 | { |
| 100 | 100 | $limit = $this->args['per_page'] > 0 |
| 101 | - ? $this->db->prepare('LIMIT %d', $this->args['per_page']) |
|
| 101 | + ? $this->db->prepare( 'LIMIT %d', $this->args['per_page'] ) |
|
| 102 | 102 | : ''; |
| 103 | - return glsr()->filterString('query/sql/limit', $limit, $this); |
|
| 103 | + return glsr()->filterString( 'query/sql/limit', $limit, $this ); |
|
| 104 | 104 | } |
| 105 | 105 | |
| 106 | 106 | /** |
@@ -110,9 +110,9 @@ discard block |
||
| 110 | 110 | { |
| 111 | 111 | $offsetBy = (($this->args['page'] - 1) * $this->args['per_page']) + $this->args['offset']; |
| 112 | 112 | $offset = ($offsetBy > 0) |
| 113 | - ? $this->db->prepare('OFFSET %d', $offsetBy) |
|
| 113 | + ? $this->db->prepare( 'OFFSET %d', $offsetBy ) |
|
| 114 | 114 | : ''; |
| 115 | - return glsr()->filterString('query/sql/offset', $offset, $this); |
|
| 115 | + return glsr()->filterString( 'query/sql/offset', $offset, $this ); |
|
| 116 | 116 | } |
| 117 | 117 | |
| 118 | 118 | /** |
@@ -127,14 +127,14 @@ discard block |
||
| 127 | 127 | ]; |
| 128 | 128 | $order = $this->args['order']; |
| 129 | 129 | $orderby = $this->args['orderby']; |
| 130 | - if (Str::startsWith('p.', $orderby)) { |
|
| 130 | + if( Str::startsWith( 'p.', $orderby ) ) { |
|
| 131 | 131 | $orderBy = "ORDER BY r.is_pinned {$order}, {$orderby} {$order}"; |
| 132 | - } elseif (array_key_exists($orderby, $values)) { |
|
| 132 | + } elseif( array_key_exists( $orderby, $values ) ) { |
|
| 133 | 133 | $orderBy = $orderby; |
| 134 | 134 | } else { |
| 135 | 135 | $orderBy = ''; |
| 136 | 136 | } |
| 137 | - return glsr()->filterString('query/sql/order-by', $orderBy, $this); |
|
| 137 | + return glsr()->filterString( 'query/sql/order-by', $orderBy, $this ); |
|
| 138 | 138 | } |
| 139 | 139 | |
| 140 | 140 | /** |
@@ -153,8 +153,8 @@ discard block |
||
| 153 | 153 | 'GROUP_CONCAT(DISTINCT att.term_id) as term_ids', |
| 154 | 154 | 'GROUP_CONCAT(DISTINCT aut.user_id) as user_ids', |
| 155 | 155 | ]; |
| 156 | - $select = glsr()->filterArray('query/sql/select', $select, $this); |
|
| 157 | - $select = implode(', ', $select); |
|
| 156 | + $select = glsr()->filterArray( 'query/sql/select', $select, $this ); |
|
| 157 | + $select = implode( ', ', $select ); |
|
| 158 | 158 | return "SELECT {$select}"; |
| 159 | 159 | } |
| 160 | 160 | |
@@ -164,21 +164,21 @@ discard block |
||
| 164 | 164 | public function sqlWhere() |
| 165 | 165 | { |
| 166 | 166 | $where = [ |
| 167 | - $this->db->prepare('AND p.post_type = %s', glsr()->post_type), |
|
| 167 | + $this->db->prepare( 'AND p.post_type = %s', glsr()->post_type ), |
|
| 168 | 168 | "AND p.post_status = 'publish'", |
| 169 | 169 | ]; |
| 170 | - $where = $this->sqlClauses($where, 'and'); |
|
| 171 | - $where = glsr()->filterArray('query/sql/where', $where, $this); |
|
| 172 | - $where = implode(' ', $where); |
|
| 170 | + $where = $this->sqlClauses( $where, 'and' ); |
|
| 171 | + $where = glsr()->filterArray( 'query/sql/where', $where, $this ); |
|
| 172 | + $where = implode( ' ', $where ); |
|
| 173 | 173 | return "WHERE 1=1 {$where}"; |
| 174 | 174 | } |
| 175 | 175 | |
| 176 | 176 | /** |
| 177 | 177 | * @return string |
| 178 | 178 | */ |
| 179 | - public function table($table) |
|
| 179 | + public function table( $table ) |
|
| 180 | 180 | { |
| 181 | - return glsr(SqlSchema::class)->table($table); |
|
| 181 | + return glsr( SqlSchema::class )->table( $table ); |
|
| 182 | 182 | } |
| 183 | 183 | |
| 184 | 184 | /** |
@@ -188,16 +188,16 @@ discard block |
||
| 188 | 188 | protected function clauseAndAssignedTo() |
| 189 | 189 | { |
| 190 | 190 | $clauses = []; |
| 191 | - if ($postIds = $this->args['assigned_to']) { |
|
| 192 | - $clauses[] = $this->db->prepare('(apt.post_id IN (%s) AND apt.is_published = 1)', implode(',', $postIds)); |
|
| 191 | + if( $postIds = $this->args['assigned_to'] ) { |
|
| 192 | + $clauses[] = $this->db->prepare( '(apt.post_id IN (%s) AND apt.is_published = 1)', implode( ',', $postIds ) ); |
|
| 193 | 193 | } |
| 194 | - if ($termIds = $this->args['category']) { |
|
| 195 | - $clauses[] = $this->db->prepare('(att.term_id IN (%s))', implode(',', $termIds)); |
|
| 194 | + if( $termIds = $this->args['category'] ) { |
|
| 195 | + $clauses[] = $this->db->prepare( '(att.term_id IN (%s))', implode( ',', $termIds ) ); |
|
| 196 | 196 | } |
| 197 | - if ($userIds = $this->args['user']) { |
|
| 198 | - $clauses[] = $this->db->prepare('(aut.user_id IN (%s))', implode(',', $userIds)); |
|
| 197 | + if( $userIds = $this->args['user'] ) { |
|
| 198 | + $clauses[] = $this->db->prepare( '(aut.user_id IN (%s))', implode( ',', $userIds ) ); |
|
| 199 | 199 | } |
| 200 | - if ($clauses = implode(' OR ', $clauses)) { |
|
| 200 | + if( $clauses = implode( ' OR ', $clauses ) ) { |
|
| 201 | 201 | return "AND ($clauses)"; |
| 202 | 202 | } |
| 203 | 203 | return ''; |
@@ -209,7 +209,7 @@ discard block |
||
| 209 | 209 | protected function clauseAndRating() |
| 210 | 210 | { |
| 211 | 211 | return $this->args['rating'] |
| 212 | - ? $this->db->prepare('AND r.rating > %d', --$this->args['rating']) |
|
| 212 | + ? $this->db->prepare( 'AND r.rating > %d', --$this->args['rating'] ) |
|
| 213 | 213 | : ''; |
| 214 | 214 | } |
| 215 | 215 | |
@@ -219,7 +219,7 @@ discard block |
||
| 219 | 219 | protected function clauseAndType() |
| 220 | 220 | { |
| 221 | 221 | return $this->args['type'] |
| 222 | - ? $this->db->prepare('AND r.type = %s', $this->args['type']) |
|
| 222 | + ? $this->db->prepare( 'AND r.type = %s', $this->args['type'] ) |
|
| 223 | 223 | : ''; |
| 224 | 224 | } |
| 225 | 225 | |
@@ -229,7 +229,7 @@ discard block |
||
| 229 | 229 | protected function clauseJoinAssignedTo() |
| 230 | 230 | { |
| 231 | 231 | return !empty($this->args['assigned_to']) |
| 232 | - ? "INNER JOIN {$this->table('assigned_posts')} AS apt ON r.ID = apt.rating_id" |
|
| 232 | + ? "INNER JOIN {$this->table( 'assigned_posts' )} AS apt ON r.ID = apt.rating_id" |
|
| 233 | 233 | : ''; |
| 234 | 234 | } |
| 235 | 235 | |
@@ -239,7 +239,7 @@ discard block |
||
| 239 | 239 | protected function clauseJoinCategory() |
| 240 | 240 | { |
| 241 | 241 | return !empty($this->args['category']) |
| 242 | - ? "INNER JOIN {$this->table('assigned_terms')} AS att ON r.ID = att.rating_id" |
|
| 242 | + ? "INNER JOIN {$this->table( 'assigned_terms' )} AS att ON r.ID = att.rating_id" |
|
| 243 | 243 | : ''; |
| 244 | 244 | } |
| 245 | 245 | |
@@ -249,7 +249,7 @@ discard block |
||
| 249 | 249 | protected function clauseJoinUser() |
| 250 | 250 | { |
| 251 | 251 | return !empty($this->args['user']) |
| 252 | - ? "INNER JOIN {$this->table('assigned_users')} AS aut ON r.ID = aut.rating_id" |
|
| 252 | + ? "INNER JOIN {$this->table( 'assigned_users' )} AS aut ON r.ID = aut.rating_id" |
|
| 253 | 253 | : ''; |
| 254 | 254 | } |
| 255 | 255 | } |
@@ -42,7 +42,7 @@ discard block |
||
| 42 | 42 | */ |
| 43 | 43 | public function sqlFrom() |
| 44 | 44 | { |
| 45 | - $from = "FROM {$this->table('ratings')} r"; |
|
| 45 | + $from = "from {$this->table('ratings')} r"; |
|
| 46 | 46 | $from = glsr()->filterString('query/sql/from', $from, $this); |
| 47 | 47 | return $from; |
| 48 | 48 | } |
@@ -155,7 +155,7 @@ discard block |
||
| 155 | 155 | ]; |
| 156 | 156 | $select = glsr()->filterArray('query/sql/select', $select, $this); |
| 157 | 157 | $select = implode(', ', $select); |
| 158 | - return "SELECT {$select}"; |
|
| 158 | + return "select {$select}"; |
|
| 159 | 159 | } |
| 160 | 160 | |
| 161 | 161 | /** |
@@ -10,47 +10,47 @@ discard block |
||
| 10 | 10 | |
| 11 | 11 | class SqlQueries |
| 12 | 12 | { |
| 13 | - protected $db; |
|
| 14 | - protected $postType; |
|
| 13 | + protected $db; |
|
| 14 | + protected $postType; |
|
| 15 | 15 | |
| 16 | - public function __construct() |
|
| 17 | - { |
|
| 18 | - global $wpdb; |
|
| 19 | - $this->db = $wpdb; |
|
| 20 | - $this->postType = Application::POST_TYPE; |
|
| 21 | - } |
|
| 16 | + public function __construct() |
|
| 17 | + { |
|
| 18 | + global $wpdb; |
|
| 19 | + $this->db = $wpdb; |
|
| 20 | + $this->postType = Application::POST_TYPE; |
|
| 21 | + } |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * @return bool |
|
| 25 | - */ |
|
| 26 | - public function deletePostCountMetaKeys() |
|
| 27 | - { |
|
| 28 | - return $this->db->query(" |
|
| 23 | + /** |
|
| 24 | + * @return bool |
|
| 25 | + */ |
|
| 26 | + public function deletePostCountMetaKeys() |
|
| 27 | + { |
|
| 28 | + return $this->db->query(" |
|
| 29 | 29 | DELETE |
| 30 | 30 | FROM {$this->db->postmeta} |
| 31 | 31 | WHERE meta_key LIKE '_glsr_%' |
| 32 | 32 | "); |
| 33 | - } |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * @return bool |
|
| 37 | - */ |
|
| 38 | - public function deleteTermCountMetaKeys() |
|
| 39 | - { |
|
| 40 | - return $this->db->query(" |
|
| 35 | + /** |
|
| 36 | + * @return bool |
|
| 37 | + */ |
|
| 38 | + public function deleteTermCountMetaKeys() |
|
| 39 | + { |
|
| 40 | + return $this->db->query(" |
|
| 41 | 41 | DELETE |
| 42 | 42 | FROM {$this->db->termmeta} |
| 43 | 43 | WHERE meta_key LIKE '_glsr_%' |
| 44 | 44 | "); |
| 45 | - } |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * @param string $metaReviewId |
|
| 49 | - * @return int |
|
| 50 | - */ |
|
| 51 | - public function getPostIdFromReviewId($metaReviewId) |
|
| 52 | - { |
|
| 53 | - $postId = $this->db->get_var(" |
|
| 47 | + /** |
|
| 48 | + * @param string $metaReviewId |
|
| 49 | + * @return int |
|
| 50 | + */ |
|
| 51 | + public function getPostIdFromReviewId($metaReviewId) |
|
| 52 | + { |
|
| 53 | + $postId = $this->db->get_var(" |
|
| 54 | 54 | SELECT p.ID |
| 55 | 55 | FROM {$this->db->posts} AS p |
| 56 | 56 | INNER JOIN {$this->db->postmeta} AS m ON p.ID = m.post_id |
@@ -58,18 +58,18 @@ discard block |
||
| 58 | 58 | AND m.meta_key = '_review_id' |
| 59 | 59 | AND m.meta_value = '{$metaReviewId}' |
| 60 | 60 | "); |
| 61 | - return intval($postId); |
|
| 62 | - } |
|
| 61 | + return intval($postId); |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - /** |
|
| 65 | - * @return array |
|
| 66 | - */ |
|
| 67 | - public function getRatings(array $args) |
|
| 68 | - { |
|
| 69 | - // get types |
|
| 70 | - // get for each type |
|
| 71 | - $table = glsr(SqlSchema::class)->table('ratings'); |
|
| 72 | - return (array) $this->db->get_results(" |
|
| 64 | + /** |
|
| 65 | + * @return array |
|
| 66 | + */ |
|
| 67 | + public function getRatings(array $args) |
|
| 68 | + { |
|
| 69 | + // get types |
|
| 70 | + // get for each type |
|
| 71 | + $table = glsr(SqlSchema::class)->table('ratings'); |
|
| 72 | + return (array) $this->db->get_results(" |
|
| 73 | 73 | SELECT r.rating AS rating, COUNT(r.rating) AS count |
| 74 | 74 | FROM {$table} AS r |
| 75 | 75 | {$this->getInnerJoinForRatings($args)} |
@@ -77,16 +77,16 @@ discard block |
||
| 77 | 77 | {$this->getAndForRatings($args)} |
| 78 | 78 | GROUP BY rating |
| 79 | 79 | "); |
| 80 | - } |
|
| 80 | + } |
|
| 81 | 81 | |
| 82 | - /** |
|
| 83 | - * @param int $lastPostId |
|
| 84 | - * @param int $limit |
|
| 85 | - * @return array |
|
| 86 | - */ |
|
| 87 | - public function getReviewCounts(array $args, $lastPostId = 0, $limit = 500) |
|
| 88 | - { |
|
| 89 | - return (array) $this->db->get_results(" |
|
| 82 | + /** |
|
| 83 | + * @param int $lastPostId |
|
| 84 | + * @param int $limit |
|
| 85 | + * @return array |
|
| 86 | + */ |
|
| 87 | + public function getReviewCounts(array $args, $lastPostId = 0, $limit = 500) |
|
| 88 | + { |
|
| 89 | + return (array) $this->db->get_results(" |
|
| 90 | 90 | SELECT DISTINCT p.ID, m1.meta_value AS rating, m2.meta_value AS type |
| 91 | 91 | FROM {$this->db->posts} AS p |
| 92 | 92 | INNER JOIN {$this->db->postmeta} AS m1 ON p.ID = m1.post_id |
@@ -101,17 +101,17 @@ discard block |
||
| 101 | 101 | ORDER By p.ID ASC |
| 102 | 102 | LIMIT {$limit} |
| 103 | 103 | "); |
| 104 | - } |
|
| 104 | + } |
|
| 105 | 105 | |
| 106 | - /** |
|
| 107 | - * @todo remove this? |
|
| 108 | - * @param string $metaKey |
|
| 109 | - * @return array |
|
| 110 | - */ |
|
| 111 | - public function getReviewCountsFor($metaKey) |
|
| 112 | - { |
|
| 113 | - $metaKey = Str::prefix('_', $metaKey); |
|
| 114 | - return (array) $this->db->get_results(" |
|
| 106 | + /** |
|
| 107 | + * @todo remove this? |
|
| 108 | + * @param string $metaKey |
|
| 109 | + * @return array |
|
| 110 | + */ |
|
| 111 | + public function getReviewCountsFor($metaKey) |
|
| 112 | + { |
|
| 113 | + $metaKey = Str::prefix('_', $metaKey); |
|
| 114 | + return (array) $this->db->get_results(" |
|
| 115 | 115 | SELECT DISTINCT m.meta_value AS name, COUNT(*) num_posts |
| 116 | 116 | FROM {$this->db->posts} AS p |
| 117 | 117 | INNER JOIN {$this->db->postmeta} AS m ON p.ID = m.post_id |
@@ -119,16 +119,16 @@ discard block |
||
| 119 | 119 | AND m.meta_key = '{$metaKey}' |
| 120 | 120 | GROUP BY name |
| 121 | 121 | "); |
| 122 | - } |
|
| 122 | + } |
|
| 123 | 123 | |
| 124 | - /** |
|
| 125 | - * @todo remove this? |
|
| 126 | - * @param string $reviewType |
|
| 127 | - * @return array |
|
| 128 | - */ |
|
| 129 | - public function getReviewIdsByType($reviewType) |
|
| 130 | - { |
|
| 131 | - $results = $this->db->get_col(" |
|
| 124 | + /** |
|
| 125 | + * @todo remove this? |
|
| 126 | + * @param string $reviewType |
|
| 127 | + * @return array |
|
| 128 | + */ |
|
| 129 | + public function getReviewIdsByType($reviewType) |
|
| 130 | + { |
|
| 131 | + $results = $this->db->get_col(" |
|
| 132 | 132 | SELECT DISTINCT m1.meta_value AS review_id |
| 133 | 133 | FROM {$this->db->posts} AS p |
| 134 | 134 | INNER JOIN {$this->db->postmeta} AS m1 ON p.ID = m1.post_id |
@@ -138,20 +138,20 @@ discard block |
||
| 138 | 138 | AND m2.meta_key = '_review_type' |
| 139 | 139 | AND m2.meta_value = '{$reviewType}' |
| 140 | 140 | "); |
| 141 | - return array_keys(array_flip($results)); |
|
| 142 | - } |
|
| 141 | + return array_keys(array_flip($results)); |
|
| 142 | + } |
|
| 143 | 143 | |
| 144 | - /** |
|
| 145 | - * @param int $greaterThanId |
|
| 146 | - * @param int $limit |
|
| 147 | - * @return array |
|
| 148 | - */ |
|
| 149 | - public function getReviewRatingsFromIds(array $postIds, $greaterThanId = 0, $limit = 100) |
|
| 150 | - { |
|
| 151 | - sort($postIds); |
|
| 152 | - $postIds = array_slice($postIds, intval(array_search($greaterThanId, $postIds)), $limit); |
|
| 153 | - $postIds = implode(',', $postIds); |
|
| 154 | - return (array) $this->db->get_results(" |
|
| 144 | + /** |
|
| 145 | + * @param int $greaterThanId |
|
| 146 | + * @param int $limit |
|
| 147 | + * @return array |
|
| 148 | + */ |
|
| 149 | + public function getReviewRatingsFromIds(array $postIds, $greaterThanId = 0, $limit = 100) |
|
| 150 | + { |
|
| 151 | + sort($postIds); |
|
| 152 | + $postIds = array_slice($postIds, intval(array_search($greaterThanId, $postIds)), $limit); |
|
| 153 | + $postIds = implode(',', $postIds); |
|
| 154 | + return (array) $this->db->get_results(" |
|
| 155 | 155 | SELECT p.ID, m.meta_value AS rating |
| 156 | 156 | FROM {$this->db->posts} AS p |
| 157 | 157 | INNER JOIN {$this->db->postmeta} AS m ON p.ID = m.post_id |
@@ -164,20 +164,20 @@ discard block |
||
| 164 | 164 | ORDER By p.ID ASC |
| 165 | 165 | LIMIT {$limit} |
| 166 | 166 | "); |
| 167 | - } |
|
| 167 | + } |
|
| 168 | 168 | |
| 169 | - /** |
|
| 170 | - * @param string $key |
|
| 171 | - * @param string $status |
|
| 172 | - * @return array |
|
| 173 | - */ |
|
| 174 | - public function getReviewsMeta($key, $status = 'publish') |
|
| 175 | - { |
|
| 176 | - $postStatusQuery = 'all' != $status && !empty($status) |
|
| 177 | - ? "AND p.post_status = '{$status}'" |
|
| 178 | - : ''; |
|
| 179 | - $key = Str::prefix('_', $key); |
|
| 180 | - $values = $this->db->get_col(" |
|
| 169 | + /** |
|
| 170 | + * @param string $key |
|
| 171 | + * @param string $status |
|
| 172 | + * @return array |
|
| 173 | + */ |
|
| 174 | + public function getReviewsMeta($key, $status = 'publish') |
|
| 175 | + { |
|
| 176 | + $postStatusQuery = 'all' != $status && !empty($status) |
|
| 177 | + ? "AND p.post_status = '{$status}'" |
|
| 178 | + : ''; |
|
| 179 | + $key = Str::prefix('_', $key); |
|
| 180 | + $values = $this->db->get_col(" |
|
| 181 | 181 | SELECT DISTINCT m.meta_value |
| 182 | 182 | FROM {$this->db->postmeta} m |
| 183 | 183 | LEFT JOIN {$this->db->posts} p ON p.ID = m.post_id |
@@ -188,87 +188,87 @@ discard block |
||
| 188 | 188 | GROUP BY p.ID -- remove duplicate meta_value entries |
| 189 | 189 | ORDER BY m.meta_id ASC -- sort by oldest meta_value |
| 190 | 190 | "); |
| 191 | - sort($values); |
|
| 192 | - return $values; |
|
| 193 | - } |
|
| 191 | + sort($values); |
|
| 192 | + return $values; |
|
| 193 | + } |
|
| 194 | 194 | |
| 195 | - /** |
|
| 196 | - * @param string $and |
|
| 197 | - * @return string |
|
| 198 | - */ |
|
| 199 | - protected function getAndForCounts(array $args, $and = '') |
|
| 200 | - { |
|
| 201 | - $postIds = implode(',', array_filter(Arr::get($args, 'post_ids', []))); |
|
| 202 | - $termIds = implode(',', array_filter(Arr::get($args, 'term_ids', []))); |
|
| 203 | - if (!empty($args['type'])) { |
|
| 204 | - $and .= "AND m2.meta_value = '{$args['type']}' "; |
|
| 205 | - } |
|
| 206 | - if ($postIds) { |
|
| 207 | - $and .= "AND m3.meta_key = '_assigned_to' AND m3.meta_value IN ({$postIds}) "; |
|
| 208 | - } |
|
| 209 | - if ($termIds) { |
|
| 210 | - $and .= "AND tr.term_taxonomy_id IN ({$termIds}) "; |
|
| 211 | - } |
|
| 212 | - return glsr()->filter('query/and-for-counts', $and); |
|
| 213 | - } |
|
| 195 | + /** |
|
| 196 | + * @param string $and |
|
| 197 | + * @return string |
|
| 198 | + */ |
|
| 199 | + protected function getAndForCounts(array $args, $and = '') |
|
| 200 | + { |
|
| 201 | + $postIds = implode(',', array_filter(Arr::get($args, 'post_ids', []))); |
|
| 202 | + $termIds = implode(',', array_filter(Arr::get($args, 'term_ids', []))); |
|
| 203 | + if (!empty($args['type'])) { |
|
| 204 | + $and .= "AND m2.meta_value = '{$args['type']}' "; |
|
| 205 | + } |
|
| 206 | + if ($postIds) { |
|
| 207 | + $and .= "AND m3.meta_key = '_assigned_to' AND m3.meta_value IN ({$postIds}) "; |
|
| 208 | + } |
|
| 209 | + if ($termIds) { |
|
| 210 | + $and .= "AND tr.term_taxonomy_id IN ({$termIds}) "; |
|
| 211 | + } |
|
| 212 | + return glsr()->filter('query/and-for-counts', $and); |
|
| 213 | + } |
|
| 214 | 214 | |
| 215 | - /** |
|
| 216 | - * @param string $and |
|
| 217 | - * @return string |
|
| 218 | - */ |
|
| 219 | - protected function getAndForRatings(array $args, $and = '') |
|
| 220 | - { |
|
| 221 | - $assignedQueries = []; |
|
| 222 | - if ($postIds = Arr::consolidate(Arr::get($args, 'post_ids', []))) { |
|
| 223 | - $postIds = implode(',', array_filter($postIds)); |
|
| 224 | - $assignedQueries[] = "(ap.post_id IN ({$postIds}) AND ap.is_published = 1)"; |
|
| 225 | - } |
|
| 226 | - if ($termIds = Arr::consolidate(Arr::get($args, 'term_ids', []))) { |
|
| 227 | - $termIds = implode(',', array_filter($termIds)); |
|
| 228 | - $assignedQueries[] = "at.term_id IN ({$termIds})"; |
|
| 229 | - } |
|
| 230 | - if ($rating = Helper::castToInt(Arr::get($args, 'rating'))) { |
|
| 231 | - ++$rating; |
|
| 232 | - $and .= "AND r.rating < {$rating} "; |
|
| 233 | - } |
|
| 234 | - if ($type = Arr::get($args, 'type')) { |
|
| 235 | - $and .= "AND r.type = '{$type}' "; |
|
| 236 | - } |
|
| 237 | - if ($assignedQuery = implode(' OR ', $assignedQueries)) { |
|
| 238 | - $and .= "AND ($assignedQuery) "; |
|
| 239 | - } |
|
| 240 | - return glsr()->filter('query/and-for-ratings', $and); |
|
| 241 | - } |
|
| 215 | + /** |
|
| 216 | + * @param string $and |
|
| 217 | + * @return string |
|
| 218 | + */ |
|
| 219 | + protected function getAndForRatings(array $args, $and = '') |
|
| 220 | + { |
|
| 221 | + $assignedQueries = []; |
|
| 222 | + if ($postIds = Arr::consolidate(Arr::get($args, 'post_ids', []))) { |
|
| 223 | + $postIds = implode(',', array_filter($postIds)); |
|
| 224 | + $assignedQueries[] = "(ap.post_id IN ({$postIds}) AND ap.is_published = 1)"; |
|
| 225 | + } |
|
| 226 | + if ($termIds = Arr::consolidate(Arr::get($args, 'term_ids', []))) { |
|
| 227 | + $termIds = implode(',', array_filter($termIds)); |
|
| 228 | + $assignedQueries[] = "at.term_id IN ({$termIds})"; |
|
| 229 | + } |
|
| 230 | + if ($rating = Helper::castToInt(Arr::get($args, 'rating'))) { |
|
| 231 | + ++$rating; |
|
| 232 | + $and .= "AND r.rating < {$rating} "; |
|
| 233 | + } |
|
| 234 | + if ($type = Arr::get($args, 'type')) { |
|
| 235 | + $and .= "AND r.type = '{$type}' "; |
|
| 236 | + } |
|
| 237 | + if ($assignedQuery = implode(' OR ', $assignedQueries)) { |
|
| 238 | + $and .= "AND ($assignedQuery) "; |
|
| 239 | + } |
|
| 240 | + return glsr()->filter('query/and-for-ratings', $and); |
|
| 241 | + } |
|
| 242 | 242 | |
| 243 | - /** |
|
| 244 | - * @param string $innerJoin |
|
| 245 | - * @return string |
|
| 246 | - */ |
|
| 247 | - protected function getInnerJoinForCounts(array $args, $innerJoin = '') |
|
| 248 | - { |
|
| 249 | - if (!empty(Arr::get($args, 'post_ids'))) { |
|
| 250 | - $innerJoin .= "INNER JOIN {$this->db->postmeta} AS m3 ON p.ID = m3.post_id "; |
|
| 251 | - } |
|
| 252 | - if (!empty(Arr::get($args, 'term_ids'))) { |
|
| 253 | - $innerJoin .= "INNER JOIN {$this->db->term_relationships} AS tr ON p.ID = tr.object_id "; |
|
| 254 | - } |
|
| 255 | - return glsr()->filter('query/inner-join-for-counts', $innerJoin); |
|
| 256 | - } |
|
| 243 | + /** |
|
| 244 | + * @param string $innerJoin |
|
| 245 | + * @return string |
|
| 246 | + */ |
|
| 247 | + protected function getInnerJoinForCounts(array $args, $innerJoin = '') |
|
| 248 | + { |
|
| 249 | + if (!empty(Arr::get($args, 'post_ids'))) { |
|
| 250 | + $innerJoin .= "INNER JOIN {$this->db->postmeta} AS m3 ON p.ID = m3.post_id "; |
|
| 251 | + } |
|
| 252 | + if (!empty(Arr::get($args, 'term_ids'))) { |
|
| 253 | + $innerJoin .= "INNER JOIN {$this->db->term_relationships} AS tr ON p.ID = tr.object_id "; |
|
| 254 | + } |
|
| 255 | + return glsr()->filter('query/inner-join-for-counts', $innerJoin); |
|
| 256 | + } |
|
| 257 | 257 | |
| 258 | - /** |
|
| 259 | - * @param string $innerJoin |
|
| 260 | - * @return string |
|
| 261 | - */ |
|
| 262 | - protected function getInnerJoinForRatings(array $args, $innerJoin = '') |
|
| 263 | - { |
|
| 264 | - if (Arr::consolidate(Arr::get($args, 'post_ids', []))) { |
|
| 265 | - $table = glsr(SqlSchema::class)->table('assigned_posts'); |
|
| 266 | - $innerJoin .= "INNER JOIN {$table} AS ap ON r.ID = ap.rating_id "; |
|
| 267 | - } |
|
| 268 | - if (Arr::consolidate(Arr::get($args, 'term_ids', []))) { |
|
| 269 | - $table = glsr(SqlSchema::class)->table('assigned_terms'); |
|
| 270 | - $innerJoin .= "INNER JOIN {$table} AS at ON r.ID = at.rating_id "; |
|
| 271 | - } |
|
| 272 | - return glsr()->filter('query/inner-join-for-ratings', $innerJoin); |
|
| 273 | - } |
|
| 258 | + /** |
|
| 259 | + * @param string $innerJoin |
|
| 260 | + * @return string |
|
| 261 | + */ |
|
| 262 | + protected function getInnerJoinForRatings(array $args, $innerJoin = '') |
|
| 263 | + { |
|
| 264 | + if (Arr::consolidate(Arr::get($args, 'post_ids', []))) { |
|
| 265 | + $table = glsr(SqlSchema::class)->table('assigned_posts'); |
|
| 266 | + $innerJoin .= "INNER JOIN {$table} AS ap ON r.ID = ap.rating_id "; |
|
| 267 | + } |
|
| 268 | + if (Arr::consolidate(Arr::get($args, 'term_ids', []))) { |
|
| 269 | + $table = glsr(SqlSchema::class)->table('assigned_terms'); |
|
| 270 | + $innerJoin .= "INNER JOIN {$table} AS at ON r.ID = at.rating_id "; |
|
| 271 | + } |
|
| 272 | + return glsr()->filter('query/inner-join-for-ratings', $innerJoin); |
|
| 273 | + } |
|
| 274 | 274 | } |
@@ -25,11 +25,11 @@ discard block |
||
| 25 | 25 | */ |
| 26 | 26 | public function deletePostCountMetaKeys() |
| 27 | 27 | { |
| 28 | - return $this->db->query(" |
|
| 28 | + return $this->db->query( " |
|
| 29 | 29 | DELETE |
| 30 | 30 | FROM {$this->db->postmeta} |
| 31 | 31 | WHERE meta_key LIKE '_glsr_%' |
| 32 | - "); |
|
| 32 | + " ); |
|
| 33 | 33 | } |
| 34 | 34 | |
| 35 | 35 | /** |
@@ -37,46 +37,46 @@ discard block |
||
| 37 | 37 | */ |
| 38 | 38 | public function deleteTermCountMetaKeys() |
| 39 | 39 | { |
| 40 | - return $this->db->query(" |
|
| 40 | + return $this->db->query( " |
|
| 41 | 41 | DELETE |
| 42 | 42 | FROM {$this->db->termmeta} |
| 43 | 43 | WHERE meta_key LIKE '_glsr_%' |
| 44 | - "); |
|
| 44 | + " ); |
|
| 45 | 45 | } |
| 46 | 46 | |
| 47 | 47 | /** |
| 48 | 48 | * @param string $metaReviewId |
| 49 | 49 | * @return int |
| 50 | 50 | */ |
| 51 | - public function getPostIdFromReviewId($metaReviewId) |
|
| 51 | + public function getPostIdFromReviewId( $metaReviewId ) |
|
| 52 | 52 | { |
| 53 | - $postId = $this->db->get_var(" |
|
| 53 | + $postId = $this->db->get_var( " |
|
| 54 | 54 | SELECT p.ID |
| 55 | 55 | FROM {$this->db->posts} AS p |
| 56 | 56 | INNER JOIN {$this->db->postmeta} AS m ON p.ID = m.post_id |
| 57 | 57 | WHERE p.post_type = '{$this->postType}' |
| 58 | 58 | AND m.meta_key = '_review_id' |
| 59 | 59 | AND m.meta_value = '{$metaReviewId}' |
| 60 | - "); |
|
| 61 | - return intval($postId); |
|
| 60 | + " ); |
|
| 61 | + return intval( $postId ); |
|
| 62 | 62 | } |
| 63 | 63 | |
| 64 | 64 | /** |
| 65 | 65 | * @return array |
| 66 | 66 | */ |
| 67 | - public function getRatings(array $args) |
|
| 67 | + public function getRatings( array $args ) |
|
| 68 | 68 | { |
| 69 | 69 | // get types |
| 70 | 70 | // get for each type |
| 71 | - $table = glsr(SqlSchema::class)->table('ratings'); |
|
| 72 | - return (array) $this->db->get_results(" |
|
| 71 | + $table = glsr( SqlSchema::class )->table( 'ratings' ); |
|
| 72 | + return (array)$this->db->get_results( " |
|
| 73 | 73 | SELECT r.rating AS rating, COUNT(r.rating) AS count |
| 74 | 74 | FROM {$table} AS r |
| 75 | - {$this->getInnerJoinForRatings($args)} |
|
| 75 | + {$this->getInnerJoinForRatings( $args )} |
|
| 76 | 76 | WHERE r.is_approved = 1 |
| 77 | - {$this->getAndForRatings($args)} |
|
| 77 | + {$this->getAndForRatings( $args )} |
|
| 78 | 78 | GROUP BY rating |
| 79 | - "); |
|
| 79 | + " ); |
|
| 80 | 80 | } |
| 81 | 81 | |
| 82 | 82 | /** |
@@ -84,23 +84,23 @@ discard block |
||
| 84 | 84 | * @param int $limit |
| 85 | 85 | * @return array |
| 86 | 86 | */ |
| 87 | - public function getReviewCounts(array $args, $lastPostId = 0, $limit = 500) |
|
| 87 | + public function getReviewCounts( array $args, $lastPostId = 0, $limit = 500 ) |
|
| 88 | 88 | { |
| 89 | - return (array) $this->db->get_results(" |
|
| 89 | + return (array)$this->db->get_results( " |
|
| 90 | 90 | SELECT DISTINCT p.ID, m1.meta_value AS rating, m2.meta_value AS type |
| 91 | 91 | FROM {$this->db->posts} AS p |
| 92 | 92 | INNER JOIN {$this->db->postmeta} AS m1 ON p.ID = m1.post_id |
| 93 | 93 | INNER JOIN {$this->db->postmeta} AS m2 ON p.ID = m2.post_id |
| 94 | - {$this->getInnerJoinForCounts($args)} |
|
| 94 | + {$this->getInnerJoinForCounts( $args )} |
|
| 95 | 95 | WHERE p.ID > {$lastPostId} |
| 96 | 96 | AND p.post_status = 'publish' |
| 97 | 97 | AND p.post_type = '{$this->postType}' |
| 98 | 98 | AND m1.meta_key = '_rating' |
| 99 | 99 | AND m2.meta_key = '_review_type' |
| 100 | - {$this->getAndForCounts($args)} |
|
| 100 | + {$this->getAndForCounts( $args )} |
|
| 101 | 101 | ORDER By p.ID ASC |
| 102 | 102 | LIMIT {$limit} |
| 103 | - "); |
|
| 103 | + " ); |
|
| 104 | 104 | } |
| 105 | 105 | |
| 106 | 106 | /** |
@@ -108,17 +108,17 @@ discard block |
||
| 108 | 108 | * @param string $metaKey |
| 109 | 109 | * @return array |
| 110 | 110 | */ |
| 111 | - public function getReviewCountsFor($metaKey) |
|
| 111 | + public function getReviewCountsFor( $metaKey ) |
|
| 112 | 112 | { |
| 113 | - $metaKey = Str::prefix('_', $metaKey); |
|
| 114 | - return (array) $this->db->get_results(" |
|
| 113 | + $metaKey = Str::prefix( '_', $metaKey ); |
|
| 114 | + return (array)$this->db->get_results( " |
|
| 115 | 115 | SELECT DISTINCT m.meta_value AS name, COUNT(*) num_posts |
| 116 | 116 | FROM {$this->db->posts} AS p |
| 117 | 117 | INNER JOIN {$this->db->postmeta} AS m ON p.ID = m.post_id |
| 118 | 118 | WHERE p.post_type = '{$this->postType}' |
| 119 | 119 | AND m.meta_key = '{$metaKey}' |
| 120 | 120 | GROUP BY name |
| 121 | - "); |
|
| 121 | + " ); |
|
| 122 | 122 | } |
| 123 | 123 | |
| 124 | 124 | /** |
@@ -126,9 +126,9 @@ discard block |
||
| 126 | 126 | * @param string $reviewType |
| 127 | 127 | * @return array |
| 128 | 128 | */ |
| 129 | - public function getReviewIdsByType($reviewType) |
|
| 129 | + public function getReviewIdsByType( $reviewType ) |
|
| 130 | 130 | { |
| 131 | - $results = $this->db->get_col(" |
|
| 131 | + $results = $this->db->get_col( " |
|
| 132 | 132 | SELECT DISTINCT m1.meta_value AS review_id |
| 133 | 133 | FROM {$this->db->posts} AS p |
| 134 | 134 | INNER JOIN {$this->db->postmeta} AS m1 ON p.ID = m1.post_id |
@@ -137,8 +137,8 @@ discard block |
||
| 137 | 137 | AND m1.meta_key = '_review_id' |
| 138 | 138 | AND m2.meta_key = '_review_type' |
| 139 | 139 | AND m2.meta_value = '{$reviewType}' |
| 140 | - "); |
|
| 141 | - return array_keys(array_flip($results)); |
|
| 140 | + " ); |
|
| 141 | + return array_keys( array_flip( $results ) ); |
|
| 142 | 142 | } |
| 143 | 143 | |
| 144 | 144 | /** |
@@ -146,12 +146,12 @@ discard block |
||
| 146 | 146 | * @param int $limit |
| 147 | 147 | * @return array |
| 148 | 148 | */ |
| 149 | - public function getReviewRatingsFromIds(array $postIds, $greaterThanId = 0, $limit = 100) |
|
| 149 | + public function getReviewRatingsFromIds( array $postIds, $greaterThanId = 0, $limit = 100 ) |
|
| 150 | 150 | { |
| 151 | - sort($postIds); |
|
| 152 | - $postIds = array_slice($postIds, intval(array_search($greaterThanId, $postIds)), $limit); |
|
| 153 | - $postIds = implode(',', $postIds); |
|
| 154 | - return (array) $this->db->get_results(" |
|
| 151 | + sort( $postIds ); |
|
| 152 | + $postIds = array_slice( $postIds, intval( array_search( $greaterThanId, $postIds ) ), $limit ); |
|
| 153 | + $postIds = implode( ',', $postIds ); |
|
| 154 | + return (array)$this->db->get_results( " |
|
| 155 | 155 | SELECT p.ID, m.meta_value AS rating |
| 156 | 156 | FROM {$this->db->posts} AS p |
| 157 | 157 | INNER JOIN {$this->db->postmeta} AS m ON p.ID = m.post_id |
@@ -163,7 +163,7 @@ discard block |
||
| 163 | 163 | GROUP BY p.ID |
| 164 | 164 | ORDER By p.ID ASC |
| 165 | 165 | LIMIT {$limit} |
| 166 | - "); |
|
| 166 | + " ); |
|
| 167 | 167 | } |
| 168 | 168 | |
| 169 | 169 | /** |
@@ -171,13 +171,13 @@ discard block |
||
| 171 | 171 | * @param string $status |
| 172 | 172 | * @return array |
| 173 | 173 | */ |
| 174 | - public function getReviewsMeta($key, $status = 'publish') |
|
| 174 | + public function getReviewsMeta( $key, $status = 'publish' ) |
|
| 175 | 175 | { |
| 176 | 176 | $postStatusQuery = 'all' != $status && !empty($status) |
| 177 | 177 | ? "AND p.post_status = '{$status}'" |
| 178 | 178 | : ''; |
| 179 | - $key = Str::prefix('_', $key); |
|
| 180 | - $values = $this->db->get_col(" |
|
| 179 | + $key = Str::prefix( '_', $key ); |
|
| 180 | + $values = $this->db->get_col( " |
|
| 181 | 181 | SELECT DISTINCT m.meta_value |
| 182 | 182 | FROM {$this->db->postmeta} m |
| 183 | 183 | LEFT JOIN {$this->db->posts} p ON p.ID = m.post_id |
@@ -187,8 +187,8 @@ discard block |
||
| 187 | 187 | $postStatusQuery |
| 188 | 188 | GROUP BY p.ID -- remove duplicate meta_value entries |
| 189 | 189 | ORDER BY m.meta_id ASC -- sort by oldest meta_value |
| 190 | - "); |
|
| 191 | - sort($values); |
|
| 190 | + " ); |
|
| 191 | + sort( $values ); |
|
| 192 | 192 | return $values; |
| 193 | 193 | } |
| 194 | 194 | |
@@ -196,79 +196,79 @@ discard block |
||
| 196 | 196 | * @param string $and |
| 197 | 197 | * @return string |
| 198 | 198 | */ |
| 199 | - protected function getAndForCounts(array $args, $and = '') |
|
| 199 | + protected function getAndForCounts( array $args, $and = '' ) |
|
| 200 | 200 | { |
| 201 | - $postIds = implode(',', array_filter(Arr::get($args, 'post_ids', []))); |
|
| 202 | - $termIds = implode(',', array_filter(Arr::get($args, 'term_ids', []))); |
|
| 203 | - if (!empty($args['type'])) { |
|
| 201 | + $postIds = implode( ',', array_filter( Arr::get( $args, 'post_ids', [] ) ) ); |
|
| 202 | + $termIds = implode( ',', array_filter( Arr::get( $args, 'term_ids', [] ) ) ); |
|
| 203 | + if( !empty($args['type']) ) { |
|
| 204 | 204 | $and .= "AND m2.meta_value = '{$args['type']}' "; |
| 205 | 205 | } |
| 206 | - if ($postIds) { |
|
| 206 | + if( $postIds ) { |
|
| 207 | 207 | $and .= "AND m3.meta_key = '_assigned_to' AND m3.meta_value IN ({$postIds}) "; |
| 208 | 208 | } |
| 209 | - if ($termIds) { |
|
| 209 | + if( $termIds ) { |
|
| 210 | 210 | $and .= "AND tr.term_taxonomy_id IN ({$termIds}) "; |
| 211 | 211 | } |
| 212 | - return glsr()->filter('query/and-for-counts', $and); |
|
| 212 | + return glsr()->filter( 'query/and-for-counts', $and ); |
|
| 213 | 213 | } |
| 214 | 214 | |
| 215 | 215 | /** |
| 216 | 216 | * @param string $and |
| 217 | 217 | * @return string |
| 218 | 218 | */ |
| 219 | - protected function getAndForRatings(array $args, $and = '') |
|
| 219 | + protected function getAndForRatings( array $args, $and = '' ) |
|
| 220 | 220 | { |
| 221 | 221 | $assignedQueries = []; |
| 222 | - if ($postIds = Arr::consolidate(Arr::get($args, 'post_ids', []))) { |
|
| 223 | - $postIds = implode(',', array_filter($postIds)); |
|
| 222 | + if( $postIds = Arr::consolidate( Arr::get( $args, 'post_ids', [] ) ) ) { |
|
| 223 | + $postIds = implode( ',', array_filter( $postIds ) ); |
|
| 224 | 224 | $assignedQueries[] = "(ap.post_id IN ({$postIds}) AND ap.is_published = 1)"; |
| 225 | 225 | } |
| 226 | - if ($termIds = Arr::consolidate(Arr::get($args, 'term_ids', []))) { |
|
| 227 | - $termIds = implode(',', array_filter($termIds)); |
|
| 226 | + if( $termIds = Arr::consolidate( Arr::get( $args, 'term_ids', [] ) ) ) { |
|
| 227 | + $termIds = implode( ',', array_filter( $termIds ) ); |
|
| 228 | 228 | $assignedQueries[] = "at.term_id IN ({$termIds})"; |
| 229 | 229 | } |
| 230 | - if ($rating = Helper::castToInt(Arr::get($args, 'rating'))) { |
|
| 230 | + if( $rating = Helper::castToInt( Arr::get( $args, 'rating' ) ) ) { |
|
| 231 | 231 | ++$rating; |
| 232 | 232 | $and .= "AND r.rating < {$rating} "; |
| 233 | 233 | } |
| 234 | - if ($type = Arr::get($args, 'type')) { |
|
| 234 | + if( $type = Arr::get( $args, 'type' ) ) { |
|
| 235 | 235 | $and .= "AND r.type = '{$type}' "; |
| 236 | 236 | } |
| 237 | - if ($assignedQuery = implode(' OR ', $assignedQueries)) { |
|
| 237 | + if( $assignedQuery = implode( ' OR ', $assignedQueries ) ) { |
|
| 238 | 238 | $and .= "AND ($assignedQuery) "; |
| 239 | 239 | } |
| 240 | - return glsr()->filter('query/and-for-ratings', $and); |
|
| 240 | + return glsr()->filter( 'query/and-for-ratings', $and ); |
|
| 241 | 241 | } |
| 242 | 242 | |
| 243 | 243 | /** |
| 244 | 244 | * @param string $innerJoin |
| 245 | 245 | * @return string |
| 246 | 246 | */ |
| 247 | - protected function getInnerJoinForCounts(array $args, $innerJoin = '') |
|
| 247 | + protected function getInnerJoinForCounts( array $args, $innerJoin = '' ) |
|
| 248 | 248 | { |
| 249 | - if (!empty(Arr::get($args, 'post_ids'))) { |
|
| 249 | + if( !empty(Arr::get( $args, 'post_ids' )) ) { |
|
| 250 | 250 | $innerJoin .= "INNER JOIN {$this->db->postmeta} AS m3 ON p.ID = m3.post_id "; |
| 251 | 251 | } |
| 252 | - if (!empty(Arr::get($args, 'term_ids'))) { |
|
| 252 | + if( !empty(Arr::get( $args, 'term_ids' )) ) { |
|
| 253 | 253 | $innerJoin .= "INNER JOIN {$this->db->term_relationships} AS tr ON p.ID = tr.object_id "; |
| 254 | 254 | } |
| 255 | - return glsr()->filter('query/inner-join-for-counts', $innerJoin); |
|
| 255 | + return glsr()->filter( 'query/inner-join-for-counts', $innerJoin ); |
|
| 256 | 256 | } |
| 257 | 257 | |
| 258 | 258 | /** |
| 259 | 259 | * @param string $innerJoin |
| 260 | 260 | * @return string |
| 261 | 261 | */ |
| 262 | - protected function getInnerJoinForRatings(array $args, $innerJoin = '') |
|
| 262 | + protected function getInnerJoinForRatings( array $args, $innerJoin = '' ) |
|
| 263 | 263 | { |
| 264 | - if (Arr::consolidate(Arr::get($args, 'post_ids', []))) { |
|
| 265 | - $table = glsr(SqlSchema::class)->table('assigned_posts'); |
|
| 264 | + if( Arr::consolidate( Arr::get( $args, 'post_ids', [] ) ) ) { |
|
| 265 | + $table = glsr( SqlSchema::class )->table( 'assigned_posts' ); |
|
| 266 | 266 | $innerJoin .= "INNER JOIN {$table} AS ap ON r.ID = ap.rating_id "; |
| 267 | 267 | } |
| 268 | - if (Arr::consolidate(Arr::get($args, 'term_ids', []))) { |
|
| 269 | - $table = glsr(SqlSchema::class)->table('assigned_terms'); |
|
| 268 | + if( Arr::consolidate( Arr::get( $args, 'term_ids', [] ) ) ) { |
|
| 269 | + $table = glsr( SqlSchema::class )->table( 'assigned_terms' ); |
|
| 270 | 270 | $innerJoin .= "INNER JOIN {$table} AS at ON r.ID = at.rating_id "; |
| 271 | 271 | } |
| 272 | - return glsr()->filter('query/inner-join-for-ratings', $innerJoin); |
|
| 272 | + return glsr()->filter( 'query/inner-join-for-ratings', $innerJoin ); |
|
| 273 | 273 | } |
| 274 | 274 | } |
@@ -9,93 +9,93 @@ |
||
| 9 | 9 | |
| 10 | 10 | class TermCountsManager |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * @var CountsManager |
|
| 14 | - */ |
|
| 15 | - protected $manager; |
|
| 12 | + /** |
|
| 13 | + * @var CountsManager |
|
| 14 | + */ |
|
| 15 | + protected $manager; |
|
| 16 | 16 | |
| 17 | - public function __construct() |
|
| 18 | - { |
|
| 19 | - $this->manager = glsr(CountsManager::class); |
|
| 20 | - } |
|
| 17 | + public function __construct() |
|
| 18 | + { |
|
| 19 | + $this->manager = glsr(CountsManager::class); |
|
| 20 | + } |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * @param int $termTaxonomyId |
|
| 24 | - * @return array |
|
| 25 | - */ |
|
| 26 | - public function build($termTaxonomyId) |
|
| 27 | - { |
|
| 28 | - return $this->manager->buildCounts([ |
|
| 29 | - 'term_ids' => [$termTaxonomyId], |
|
| 30 | - ]); |
|
| 31 | - } |
|
| 22 | + /** |
|
| 23 | + * @param int $termTaxonomyId |
|
| 24 | + * @return array |
|
| 25 | + */ |
|
| 26 | + public function build($termTaxonomyId) |
|
| 27 | + { |
|
| 28 | + return $this->manager->buildCounts([ |
|
| 29 | + 'term_ids' => [$termTaxonomyId], |
|
| 30 | + ]); |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * @return void |
|
| 35 | - */ |
|
| 36 | - public function decrease(Review $review) |
|
| 37 | - { |
|
| 38 | - foreach ($review->term_ids as $termId) { |
|
| 39 | - if (empty($counts = $this->get($termId))) { |
|
| 40 | - continue; |
|
| 41 | - } |
|
| 42 | - $this->update($termId, |
|
| 43 | - $this->manager->decreaseRating($counts, $review->review_type, $review->rating) |
|
| 44 | - ); |
|
| 45 | - } |
|
| 46 | - } |
|
| 33 | + /** |
|
| 34 | + * @return void |
|
| 35 | + */ |
|
| 36 | + public function decrease(Review $review) |
|
| 37 | + { |
|
| 38 | + foreach ($review->term_ids as $termId) { |
|
| 39 | + if (empty($counts = $this->get($termId))) { |
|
| 40 | + continue; |
|
| 41 | + } |
|
| 42 | + $this->update($termId, |
|
| 43 | + $this->manager->decreaseRating($counts, $review->review_type, $review->rating) |
|
| 44 | + ); |
|
| 45 | + } |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * @param int $termId |
|
| 50 | - * @return array |
|
| 51 | - */ |
|
| 52 | - public function get($termId) |
|
| 53 | - { |
|
| 54 | - return array_filter((array) get_term_meta($termId, CountsManager::META_COUNT, true)); |
|
| 55 | - } |
|
| 48 | + /** |
|
| 49 | + * @param int $termId |
|
| 50 | + * @return array |
|
| 51 | + */ |
|
| 52 | + public function get($termId) |
|
| 53 | + { |
|
| 54 | + return array_filter((array) get_term_meta($termId, CountsManager::META_COUNT, true)); |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * @return void |
|
| 59 | - */ |
|
| 60 | - public function increase(Review $review) |
|
| 61 | - { |
|
| 62 | - $terms = glsr(ReviewManager::class)->normalizeTerms(implode(',', $review->term_ids)); |
|
| 63 | - foreach ($terms as $term) { |
|
| 64 | - $counts = $this->get($term['term_id']); |
|
| 65 | - $counts = empty($counts) |
|
| 66 | - ? $this->build($term['term_taxonomy_id']) |
|
| 67 | - : $this->manager->increaseRating($counts, $review->review_type, $review->rating); |
|
| 68 | - $this->update($term['term_id'], $counts); |
|
| 69 | - } |
|
| 70 | - } |
|
| 57 | + /** |
|
| 58 | + * @return void |
|
| 59 | + */ |
|
| 60 | + public function increase(Review $review) |
|
| 61 | + { |
|
| 62 | + $terms = glsr(ReviewManager::class)->normalizeTerms(implode(',', $review->term_ids)); |
|
| 63 | + foreach ($terms as $term) { |
|
| 64 | + $counts = $this->get($term['term_id']); |
|
| 65 | + $counts = empty($counts) |
|
| 66 | + ? $this->build($term['term_taxonomy_id']) |
|
| 67 | + : $this->manager->increaseRating($counts, $review->review_type, $review->rating); |
|
| 68 | + $this->update($term['term_id'], $counts); |
|
| 69 | + } |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * @param int $termId |
|
| 74 | - * @return void |
|
| 75 | - */ |
|
| 76 | - public function update($termId, array $reviewCounts) |
|
| 77 | - { |
|
| 78 | - $term = get_term($termId, Application::TAXONOMY); |
|
| 79 | - if (!isset($term->term_id)) { |
|
| 80 | - return; |
|
| 81 | - } |
|
| 82 | - $ratingCounts = $this->manager->flatten($reviewCounts); |
|
| 83 | - update_term_meta($termId, CountsManager::META_COUNT, $reviewCounts); |
|
| 84 | - update_term_meta($termId, CountsManager::META_AVERAGE, glsr(Rating::class)->average($ratingCounts)); |
|
| 85 | - update_term_meta($termId, CountsManager::META_RANKING, glsr(Rating::class)->ranking($ratingCounts)); |
|
| 86 | - } |
|
| 72 | + /** |
|
| 73 | + * @param int $termId |
|
| 74 | + * @return void |
|
| 75 | + */ |
|
| 76 | + public function update($termId, array $reviewCounts) |
|
| 77 | + { |
|
| 78 | + $term = get_term($termId, Application::TAXONOMY); |
|
| 79 | + if (!isset($term->term_id)) { |
|
| 80 | + return; |
|
| 81 | + } |
|
| 82 | + $ratingCounts = $this->manager->flatten($reviewCounts); |
|
| 83 | + update_term_meta($termId, CountsManager::META_COUNT, $reviewCounts); |
|
| 84 | + update_term_meta($termId, CountsManager::META_AVERAGE, glsr(Rating::class)->average($ratingCounts)); |
|
| 85 | + update_term_meta($termId, CountsManager::META_RANKING, glsr(Rating::class)->ranking($ratingCounts)); |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | - /** |
|
| 89 | - * @return void |
|
| 90 | - */ |
|
| 91 | - public function updateAll() |
|
| 92 | - { |
|
| 93 | - // glsr(SqlQueries::class)->deleteTermCountMetaKeys(); // @todo test this with SiteGround for race conditions |
|
| 94 | - $terms = glsr(Database::class)->getTerms([ |
|
| 95 | - 'fields' => 'all', |
|
| 96 | - ]); |
|
| 97 | - foreach ($terms as $term) { |
|
| 98 | - $this->update($term->term_id, $this->build($term->term_taxonomy_id)); |
|
| 99 | - } |
|
| 100 | - } |
|
| 88 | + /** |
|
| 89 | + * @return void |
|
| 90 | + */ |
|
| 91 | + public function updateAll() |
|
| 92 | + { |
|
| 93 | + // glsr(SqlQueries::class)->deleteTermCountMetaKeys(); // @todo test this with SiteGround for race conditions |
|
| 94 | + $terms = glsr(Database::class)->getTerms([ |
|
| 95 | + 'fields' => 'all', |
|
| 96 | + ]); |
|
| 97 | + foreach ($terms as $term) { |
|
| 98 | + $this->update($term->term_id, $this->build($term->term_taxonomy_id)); |
|
| 99 | + } |
|
| 100 | + } |
|
| 101 | 101 | } |
@@ -16,31 +16,31 @@ discard block |
||
| 16 | 16 | |
| 17 | 17 | public function __construct() |
| 18 | 18 | { |
| 19 | - $this->manager = glsr(CountsManager::class); |
|
| 19 | + $this->manager = glsr( CountsManager::class ); |
|
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | /** |
| 23 | 23 | * @param int $termTaxonomyId |
| 24 | 24 | * @return array |
| 25 | 25 | */ |
| 26 | - public function build($termTaxonomyId) |
|
| 26 | + public function build( $termTaxonomyId ) |
|
| 27 | 27 | { |
| 28 | - return $this->manager->buildCounts([ |
|
| 28 | + return $this->manager->buildCounts( [ |
|
| 29 | 29 | 'term_ids' => [$termTaxonomyId], |
| 30 | - ]); |
|
| 30 | + ] ); |
|
| 31 | 31 | } |
| 32 | 32 | |
| 33 | 33 | /** |
| 34 | 34 | * @return void |
| 35 | 35 | */ |
| 36 | - public function decrease(Review $review) |
|
| 36 | + public function decrease( Review $review ) |
|
| 37 | 37 | { |
| 38 | - foreach ($review->term_ids as $termId) { |
|
| 39 | - if (empty($counts = $this->get($termId))) { |
|
| 38 | + foreach( $review->term_ids as $termId ) { |
|
| 39 | + if( empty($counts = $this->get( $termId )) ) { |
|
| 40 | 40 | continue; |
| 41 | 41 | } |
| 42 | - $this->update($termId, |
|
| 43 | - $this->manager->decreaseRating($counts, $review->review_type, $review->rating) |
|
| 42 | + $this->update( $termId, |
|
| 43 | + $this->manager->decreaseRating( $counts, $review->review_type, $review->rating ) |
|
| 44 | 44 | ); |
| 45 | 45 | } |
| 46 | 46 | } |
@@ -49,23 +49,23 @@ discard block |
||
| 49 | 49 | * @param int $termId |
| 50 | 50 | * @return array |
| 51 | 51 | */ |
| 52 | - public function get($termId) |
|
| 52 | + public function get( $termId ) |
|
| 53 | 53 | { |
| 54 | - return array_filter((array) get_term_meta($termId, CountsManager::META_COUNT, true)); |
|
| 54 | + return array_filter( (array)get_term_meta( $termId, CountsManager::META_COUNT, true ) ); |
|
| 55 | 55 | } |
| 56 | 56 | |
| 57 | 57 | /** |
| 58 | 58 | * @return void |
| 59 | 59 | */ |
| 60 | - public function increase(Review $review) |
|
| 60 | + public function increase( Review $review ) |
|
| 61 | 61 | { |
| 62 | - $terms = glsr(ReviewManager::class)->normalizeTerms(implode(',', $review->term_ids)); |
|
| 63 | - foreach ($terms as $term) { |
|
| 64 | - $counts = $this->get($term['term_id']); |
|
| 62 | + $terms = glsr( ReviewManager::class )->normalizeTerms( implode( ',', $review->term_ids ) ); |
|
| 63 | + foreach( $terms as $term ) { |
|
| 64 | + $counts = $this->get( $term['term_id'] ); |
|
| 65 | 65 | $counts = empty($counts) |
| 66 | - ? $this->build($term['term_taxonomy_id']) |
|
| 67 | - : $this->manager->increaseRating($counts, $review->review_type, $review->rating); |
|
| 68 | - $this->update($term['term_id'], $counts); |
|
| 66 | + ? $this->build( $term['term_taxonomy_id'] ) |
|
| 67 | + : $this->manager->increaseRating( $counts, $review->review_type, $review->rating ); |
|
| 68 | + $this->update( $term['term_id'], $counts ); |
|
| 69 | 69 | } |
| 70 | 70 | } |
| 71 | 71 | |
@@ -73,16 +73,16 @@ discard block |
||
| 73 | 73 | * @param int $termId |
| 74 | 74 | * @return void |
| 75 | 75 | */ |
| 76 | - public function update($termId, array $reviewCounts) |
|
| 76 | + public function update( $termId, array $reviewCounts ) |
|
| 77 | 77 | { |
| 78 | - $term = get_term($termId, Application::TAXONOMY); |
|
| 79 | - if (!isset($term->term_id)) { |
|
| 78 | + $term = get_term( $termId, Application::TAXONOMY ); |
|
| 79 | + if( !isset($term->term_id) ) { |
|
| 80 | 80 | return; |
| 81 | 81 | } |
| 82 | - $ratingCounts = $this->manager->flatten($reviewCounts); |
|
| 83 | - update_term_meta($termId, CountsManager::META_COUNT, $reviewCounts); |
|
| 84 | - update_term_meta($termId, CountsManager::META_AVERAGE, glsr(Rating::class)->average($ratingCounts)); |
|
| 85 | - update_term_meta($termId, CountsManager::META_RANKING, glsr(Rating::class)->ranking($ratingCounts)); |
|
| 82 | + $ratingCounts = $this->manager->flatten( $reviewCounts ); |
|
| 83 | + update_term_meta( $termId, CountsManager::META_COUNT, $reviewCounts ); |
|
| 84 | + update_term_meta( $termId, CountsManager::META_AVERAGE, glsr( Rating::class )->average( $ratingCounts ) ); |
|
| 85 | + update_term_meta( $termId, CountsManager::META_RANKING, glsr( Rating::class )->ranking( $ratingCounts ) ); |
|
| 86 | 86 | } |
| 87 | 87 | |
| 88 | 88 | /** |
@@ -91,11 +91,11 @@ discard block |
||
| 91 | 91 | public function updateAll() |
| 92 | 92 | { |
| 93 | 93 | // glsr(SqlQueries::class)->deleteTermCountMetaKeys(); // @todo test this with SiteGround for race conditions |
| 94 | - $terms = glsr(Database::class)->getTerms([ |
|
| 94 | + $terms = glsr( Database::class )->getTerms( [ |
|
| 95 | 95 | 'fields' => 'all', |
| 96 | - ]); |
|
| 97 | - foreach ($terms as $term) { |
|
| 98 | - $this->update($term->term_id, $this->build($term->term_taxonomy_id)); |
|
| 96 | + ] ); |
|
| 97 | + foreach( $terms as $term ) { |
|
| 98 | + $this->update( $term->term_id, $this->build( $term->term_taxonomy_id ) ); |
|
| 99 | 99 | } |
| 100 | 100 | } |
| 101 | 101 | } |
@@ -7,85 +7,85 @@ |
||
| 7 | 7 | |
| 8 | 8 | class PostCountsManager |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * @var CountsManager |
|
| 12 | - */ |
|
| 13 | - protected $manager; |
|
| 10 | + /** |
|
| 11 | + * @var CountsManager |
|
| 12 | + */ |
|
| 13 | + protected $manager; |
|
| 14 | 14 | |
| 15 | - public function __construct() |
|
| 16 | - { |
|
| 17 | - $this->manager = glsr(CountsManager::class); |
|
| 18 | - } |
|
| 15 | + public function __construct() |
|
| 16 | + { |
|
| 17 | + $this->manager = glsr(CountsManager::class); |
|
| 18 | + } |
|
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * @param int $postId |
|
| 22 | - * @return array |
|
| 23 | - */ |
|
| 24 | - public function build($postId) |
|
| 25 | - { |
|
| 26 | - return $this->manager->buildCounts([ |
|
| 27 | - 'post_ids' => [$postId], |
|
| 28 | - ]); |
|
| 29 | - } |
|
| 20 | + /** |
|
| 21 | + * @param int $postId |
|
| 22 | + * @return array |
|
| 23 | + */ |
|
| 24 | + public function build($postId) |
|
| 25 | + { |
|
| 26 | + return $this->manager->buildCounts([ |
|
| 27 | + 'post_ids' => [$postId], |
|
| 28 | + ]); |
|
| 29 | + } |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * @return void |
|
| 33 | - */ |
|
| 34 | - public function decrease(Review $review) |
|
| 35 | - { |
|
| 36 | - if (empty($counts = $this->get($review->assigned_to))) { |
|
| 37 | - return; |
|
| 38 | - } |
|
| 39 | - $this->update($review->assigned_to, |
|
| 40 | - $this->manager->decreaseRating($counts, $review->review_type, $review->rating) |
|
| 41 | - ); |
|
| 42 | - } |
|
| 31 | + /** |
|
| 32 | + * @return void |
|
| 33 | + */ |
|
| 34 | + public function decrease(Review $review) |
|
| 35 | + { |
|
| 36 | + if (empty($counts = $this->get($review->assigned_to))) { |
|
| 37 | + return; |
|
| 38 | + } |
|
| 39 | + $this->update($review->assigned_to, |
|
| 40 | + $this->manager->decreaseRating($counts, $review->review_type, $review->rating) |
|
| 41 | + ); |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * @param int $postId |
|
| 46 | - * @return array |
|
| 47 | - */ |
|
| 48 | - public function get($postId) |
|
| 49 | - { |
|
| 50 | - return array_filter((array) get_post_meta($postId, CountsManager::META_COUNT, true)); |
|
| 51 | - } |
|
| 44 | + /** |
|
| 45 | + * @param int $postId |
|
| 46 | + * @return array |
|
| 47 | + */ |
|
| 48 | + public function get($postId) |
|
| 49 | + { |
|
| 50 | + return array_filter((array) get_post_meta($postId, CountsManager::META_COUNT, true)); |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * @return void |
|
| 55 | - */ |
|
| 56 | - public function increase(Review $review) |
|
| 57 | - { |
|
| 58 | - if (!get_post($review->assigned_to) instanceof \WP_Post) { |
|
| 59 | - return; |
|
| 60 | - } |
|
| 61 | - $counts = $this->get($review->assigned_to); |
|
| 62 | - $counts = empty($counts) |
|
| 63 | - ? $this->build($review->assigned_to) |
|
| 64 | - : $this->manager->increaseRating($counts, $review->review_type, $review->rating); |
|
| 65 | - $this->update($review->assigned_to, $counts); |
|
| 66 | - } |
|
| 53 | + /** |
|
| 54 | + * @return void |
|
| 55 | + */ |
|
| 56 | + public function increase(Review $review) |
|
| 57 | + { |
|
| 58 | + if (!get_post($review->assigned_to) instanceof \WP_Post) { |
|
| 59 | + return; |
|
| 60 | + } |
|
| 61 | + $counts = $this->get($review->assigned_to); |
|
| 62 | + $counts = empty($counts) |
|
| 63 | + ? $this->build($review->assigned_to) |
|
| 64 | + : $this->manager->increaseRating($counts, $review->review_type, $review->rating); |
|
| 65 | + $this->update($review->assigned_to, $counts); |
|
| 66 | + } |
|
| 67 | 67 | |
| 68 | - /** |
|
| 69 | - * @param int $postId |
|
| 70 | - * @return void |
|
| 71 | - */ |
|
| 72 | - public function update($postId, array $reviewCounts) |
|
| 73 | - { |
|
| 74 | - $ratingCounts = $this->manager->flatten($reviewCounts); |
|
| 75 | - update_post_meta($postId, CountsManager::META_COUNT, $reviewCounts); |
|
| 76 | - update_post_meta($postId, CountsManager::META_AVERAGE, glsr(Rating::class)->average($ratingCounts)); |
|
| 77 | - update_post_meta($postId, CountsManager::META_RANKING, glsr(Rating::class)->ranking($ratingCounts)); |
|
| 78 | - } |
|
| 68 | + /** |
|
| 69 | + * @param int $postId |
|
| 70 | + * @return void |
|
| 71 | + */ |
|
| 72 | + public function update($postId, array $reviewCounts) |
|
| 73 | + { |
|
| 74 | + $ratingCounts = $this->manager->flatten($reviewCounts); |
|
| 75 | + update_post_meta($postId, CountsManager::META_COUNT, $reviewCounts); |
|
| 76 | + update_post_meta($postId, CountsManager::META_AVERAGE, glsr(Rating::class)->average($ratingCounts)); |
|
| 77 | + update_post_meta($postId, CountsManager::META_RANKING, glsr(Rating::class)->ranking($ratingCounts)); |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - /** |
|
| 81 | - * @return void |
|
| 82 | - */ |
|
| 83 | - public function updateAll() |
|
| 84 | - { |
|
| 85 | - glsr(SqlQueries::class)->deletePostCountMetaKeys(); // @todo test this with SiteGround for race conditions |
|
| 86 | - $postIds = glsr(SqlQueries::class)->getReviewsMeta('assigned_to'); |
|
| 87 | - foreach ($postIds as $postId) { |
|
| 88 | - $this->update($postId, $this->build($postId)); |
|
| 89 | - } |
|
| 90 | - } |
|
| 80 | + /** |
|
| 81 | + * @return void |
|
| 82 | + */ |
|
| 83 | + public function updateAll() |
|
| 84 | + { |
|
| 85 | + glsr(SqlQueries::class)->deletePostCountMetaKeys(); // @todo test this with SiteGround for race conditions |
|
| 86 | + $postIds = glsr(SqlQueries::class)->getReviewsMeta('assigned_to'); |
|
| 87 | + foreach ($postIds as $postId) { |
|
| 88 | + $this->update($postId, $this->build($postId)); |
|
| 89 | + } |
|
| 90 | + } |
|
| 91 | 91 | } |
@@ -14,30 +14,30 @@ discard block |
||
| 14 | 14 | |
| 15 | 15 | public function __construct() |
| 16 | 16 | { |
| 17 | - $this->manager = glsr(CountsManager::class); |
|
| 17 | + $this->manager = glsr( CountsManager::class ); |
|
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | /** |
| 21 | 21 | * @param int $postId |
| 22 | 22 | * @return array |
| 23 | 23 | */ |
| 24 | - public function build($postId) |
|
| 24 | + public function build( $postId ) |
|
| 25 | 25 | { |
| 26 | - return $this->manager->buildCounts([ |
|
| 26 | + return $this->manager->buildCounts( [ |
|
| 27 | 27 | 'post_ids' => [$postId], |
| 28 | - ]); |
|
| 28 | + ] ); |
|
| 29 | 29 | } |
| 30 | 30 | |
| 31 | 31 | /** |
| 32 | 32 | * @return void |
| 33 | 33 | */ |
| 34 | - public function decrease(Review $review) |
|
| 34 | + public function decrease( Review $review ) |
|
| 35 | 35 | { |
| 36 | - if (empty($counts = $this->get($review->assigned_to))) { |
|
| 36 | + if( empty($counts = $this->get( $review->assigned_to )) ) { |
|
| 37 | 37 | return; |
| 38 | 38 | } |
| 39 | - $this->update($review->assigned_to, |
|
| 40 | - $this->manager->decreaseRating($counts, $review->review_type, $review->rating) |
|
| 39 | + $this->update( $review->assigned_to, |
|
| 40 | + $this->manager->decreaseRating( $counts, $review->review_type, $review->rating ) |
|
| 41 | 41 | ); |
| 42 | 42 | } |
| 43 | 43 | |
@@ -45,36 +45,36 @@ discard block |
||
| 45 | 45 | * @param int $postId |
| 46 | 46 | * @return array |
| 47 | 47 | */ |
| 48 | - public function get($postId) |
|
| 48 | + public function get( $postId ) |
|
| 49 | 49 | { |
| 50 | - return array_filter((array) get_post_meta($postId, CountsManager::META_COUNT, true)); |
|
| 50 | + return array_filter( (array)get_post_meta( $postId, CountsManager::META_COUNT, true ) ); |
|
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | /** |
| 54 | 54 | * @return void |
| 55 | 55 | */ |
| 56 | - public function increase(Review $review) |
|
| 56 | + public function increase( Review $review ) |
|
| 57 | 57 | { |
| 58 | - if (!get_post($review->assigned_to) instanceof \WP_Post) { |
|
| 58 | + if( !get_post( $review->assigned_to ) instanceof \WP_Post ) { |
|
| 59 | 59 | return; |
| 60 | 60 | } |
| 61 | - $counts = $this->get($review->assigned_to); |
|
| 61 | + $counts = $this->get( $review->assigned_to ); |
|
| 62 | 62 | $counts = empty($counts) |
| 63 | - ? $this->build($review->assigned_to) |
|
| 64 | - : $this->manager->increaseRating($counts, $review->review_type, $review->rating); |
|
| 65 | - $this->update($review->assigned_to, $counts); |
|
| 63 | + ? $this->build( $review->assigned_to ) |
|
| 64 | + : $this->manager->increaseRating( $counts, $review->review_type, $review->rating ); |
|
| 65 | + $this->update( $review->assigned_to, $counts ); |
|
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | /** |
| 69 | 69 | * @param int $postId |
| 70 | 70 | * @return void |
| 71 | 71 | */ |
| 72 | - public function update($postId, array $reviewCounts) |
|
| 72 | + public function update( $postId, array $reviewCounts ) |
|
| 73 | 73 | { |
| 74 | - $ratingCounts = $this->manager->flatten($reviewCounts); |
|
| 75 | - update_post_meta($postId, CountsManager::META_COUNT, $reviewCounts); |
|
| 76 | - update_post_meta($postId, CountsManager::META_AVERAGE, glsr(Rating::class)->average($ratingCounts)); |
|
| 77 | - update_post_meta($postId, CountsManager::META_RANKING, glsr(Rating::class)->ranking($ratingCounts)); |
|
| 74 | + $ratingCounts = $this->manager->flatten( $reviewCounts ); |
|
| 75 | + update_post_meta( $postId, CountsManager::META_COUNT, $reviewCounts ); |
|
| 76 | + update_post_meta( $postId, CountsManager::META_AVERAGE, glsr( Rating::class )->average( $ratingCounts ) ); |
|
| 77 | + update_post_meta( $postId, CountsManager::META_RANKING, glsr( Rating::class )->ranking( $ratingCounts ) ); |
|
| 78 | 78 | } |
| 79 | 79 | |
| 80 | 80 | /** |
@@ -82,10 +82,10 @@ discard block |
||
| 82 | 82 | */ |
| 83 | 83 | public function updateAll() |
| 84 | 84 | { |
| 85 | - glsr(SqlQueries::class)->deletePostCountMetaKeys(); // @todo test this with SiteGround for race conditions |
|
| 86 | - $postIds = glsr(SqlQueries::class)->getReviewsMeta('assigned_to'); |
|
| 87 | - foreach ($postIds as $postId) { |
|
| 88 | - $this->update($postId, $this->build($postId)); |
|
| 85 | + glsr( SqlQueries::class )->deletePostCountMetaKeys(); // @todo test this with SiteGround for race conditions |
|
| 86 | + $postIds = glsr( SqlQueries::class )->getReviewsMeta( 'assigned_to' ); |
|
| 87 | + foreach( $postIds as $postId ) { |
|
| 88 | + $this->update( $postId, $this->build( $postId ) ); |
|
| 89 | 89 | } |
| 90 | 90 | } |
| 91 | 91 | } |
@@ -6,60 +6,60 @@ |
||
| 6 | 6 | |
| 7 | 7 | class DefaultsManager |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * @return array |
|
| 11 | - */ |
|
| 12 | - public function defaults() |
|
| 13 | - { |
|
| 14 | - $settings = $this->settings(); |
|
| 15 | - $defaults = (array) array_combine(array_keys($settings), wp_list_pluck($settings, 'default')); |
|
| 16 | - return wp_parse_args($defaults, [ |
|
| 17 | - 'version' => '', |
|
| 18 | - 'version_upgraded_from' => '', |
|
| 19 | - ]); |
|
| 20 | - } |
|
| 9 | + /** |
|
| 10 | + * @return array |
|
| 11 | + */ |
|
| 12 | + public function defaults() |
|
| 13 | + { |
|
| 14 | + $settings = $this->settings(); |
|
| 15 | + $defaults = (array) array_combine(array_keys($settings), wp_list_pluck($settings, 'default')); |
|
| 16 | + return wp_parse_args($defaults, [ |
|
| 17 | + 'version' => '', |
|
| 18 | + 'version_upgraded_from' => '', |
|
| 19 | + ]); |
|
| 20 | + } |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * @return array |
|
| 24 | - */ |
|
| 25 | - public function get() |
|
| 26 | - { |
|
| 27 | - return Arr::convertFromDotNotation($this->defaults()); |
|
| 28 | - } |
|
| 22 | + /** |
|
| 23 | + * @return array |
|
| 24 | + */ |
|
| 25 | + public function get() |
|
| 26 | + { |
|
| 27 | + return Arr::convertFromDotNotation($this->defaults()); |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * @return array |
|
| 32 | - */ |
|
| 33 | - public function set() |
|
| 34 | - { |
|
| 35 | - $settings = glsr(OptionManager::class)->all(); |
|
| 36 | - $currentSettings = Arr::removeEmptyValues($settings); |
|
| 37 | - $defaultSettings = array_replace_recursive($this->get(), $currentSettings); |
|
| 38 | - $updatedSettings = array_replace_recursive($settings, $defaultSettings); |
|
| 39 | - update_option(OptionManager::databaseKey(), $updatedSettings); |
|
| 40 | - return $defaultSettings; |
|
| 41 | - } |
|
| 30 | + /** |
|
| 31 | + * @return array |
|
| 32 | + */ |
|
| 33 | + public function set() |
|
| 34 | + { |
|
| 35 | + $settings = glsr(OptionManager::class)->all(); |
|
| 36 | + $currentSettings = Arr::removeEmptyValues($settings); |
|
| 37 | + $defaultSettings = array_replace_recursive($this->get(), $currentSettings); |
|
| 38 | + $updatedSettings = array_replace_recursive($settings, $defaultSettings); |
|
| 39 | + update_option(OptionManager::databaseKey(), $updatedSettings); |
|
| 40 | + return $defaultSettings; |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * @return array |
|
| 45 | - */ |
|
| 46 | - public function settings() |
|
| 47 | - { |
|
| 48 | - $settings = glsr()->filterArray('addon/settings', glsr()->config('settings')); |
|
| 49 | - return $this->normalize($settings); |
|
| 50 | - } |
|
| 43 | + /** |
|
| 44 | + * @return array |
|
| 45 | + */ |
|
| 46 | + public function settings() |
|
| 47 | + { |
|
| 48 | + $settings = glsr()->filterArray('addon/settings', glsr()->config('settings')); |
|
| 49 | + return $this->normalize($settings); |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * @return array |
|
| 54 | - */ |
|
| 55 | - protected function normalize(array $settings) |
|
| 56 | - { |
|
| 57 | - array_walk($settings, function (&$setting) { |
|
| 58 | - if (isset($setting['default'])) { |
|
| 59 | - return; |
|
| 60 | - } |
|
| 61 | - $setting['default'] = ''; |
|
| 62 | - }); |
|
| 63 | - return $settings; |
|
| 64 | - } |
|
| 52 | + /** |
|
| 53 | + * @return array |
|
| 54 | + */ |
|
| 55 | + protected function normalize(array $settings) |
|
| 56 | + { |
|
| 57 | + array_walk($settings, function (&$setting) { |
|
| 58 | + if (isset($setting['default'])) { |
|
| 59 | + return; |
|
| 60 | + } |
|
| 61 | + $setting['default'] = ''; |
|
| 62 | + }); |
|
| 63 | + return $settings; |
|
| 64 | + } |
|
| 65 | 65 | } |
@@ -12,11 +12,11 @@ discard block |
||
| 12 | 12 | public function defaults() |
| 13 | 13 | { |
| 14 | 14 | $settings = $this->settings(); |
| 15 | - $defaults = (array) array_combine(array_keys($settings), wp_list_pluck($settings, 'default')); |
|
| 16 | - return wp_parse_args($defaults, [ |
|
| 15 | + $defaults = (array)array_combine( array_keys( $settings ), wp_list_pluck( $settings, 'default' ) ); |
|
| 16 | + return wp_parse_args( $defaults, [ |
|
| 17 | 17 | 'version' => '', |
| 18 | 18 | 'version_upgraded_from' => '', |
| 19 | - ]); |
|
| 19 | + ] ); |
|
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | /** |
@@ -24,7 +24,7 @@ discard block |
||
| 24 | 24 | */ |
| 25 | 25 | public function get() |
| 26 | 26 | { |
| 27 | - return Arr::convertFromDotNotation($this->defaults()); |
|
| 27 | + return Arr::convertFromDotNotation( $this->defaults() ); |
|
| 28 | 28 | } |
| 29 | 29 | |
| 30 | 30 | /** |
@@ -32,11 +32,11 @@ discard block |
||
| 32 | 32 | */ |
| 33 | 33 | public function set() |
| 34 | 34 | { |
| 35 | - $settings = glsr(OptionManager::class)->all(); |
|
| 36 | - $currentSettings = Arr::removeEmptyValues($settings); |
|
| 37 | - $defaultSettings = array_replace_recursive($this->get(), $currentSettings); |
|
| 38 | - $updatedSettings = array_replace_recursive($settings, $defaultSettings); |
|
| 39 | - update_option(OptionManager::databaseKey(), $updatedSettings); |
|
| 35 | + $settings = glsr( OptionManager::class )->all(); |
|
| 36 | + $currentSettings = Arr::removeEmptyValues( $settings ); |
|
| 37 | + $defaultSettings = array_replace_recursive( $this->get(), $currentSettings ); |
|
| 38 | + $updatedSettings = array_replace_recursive( $settings, $defaultSettings ); |
|
| 39 | + update_option( OptionManager::databaseKey(), $updatedSettings ); |
|
| 40 | 40 | return $defaultSettings; |
| 41 | 41 | } |
| 42 | 42 | |
@@ -45,17 +45,17 @@ discard block |
||
| 45 | 45 | */ |
| 46 | 46 | public function settings() |
| 47 | 47 | { |
| 48 | - $settings = glsr()->filterArray('addon/settings', glsr()->config('settings')); |
|
| 49 | - return $this->normalize($settings); |
|
| 48 | + $settings = glsr()->filterArray( 'addon/settings', glsr()->config( 'settings' ) ); |
|
| 49 | + return $this->normalize( $settings ); |
|
| 50 | 50 | } |
| 51 | 51 | |
| 52 | 52 | /** |
| 53 | 53 | * @return array |
| 54 | 54 | */ |
| 55 | - protected function normalize(array $settings) |
|
| 55 | + protected function normalize( array $settings ) |
|
| 56 | 56 | { |
| 57 | - array_walk($settings, function (&$setting) { |
|
| 58 | - if (isset($setting['default'])) { |
|
| 57 | + array_walk( $settings, function( &$setting ) { |
|
| 58 | + if( isset($setting['default']) ) { |
|
| 59 | 59 | return; |
| 60 | 60 | } |
| 61 | 61 | $setting['default'] = ''; |
@@ -10,73 +10,73 @@ |
||
| 10 | 10 | |
| 11 | 11 | class NormalizeQueryArgs extends Arguments |
| 12 | 12 | { |
| 13 | - public $assigned_to; |
|
| 14 | - public $category; |
|
| 15 | - public $offset; |
|
| 16 | - public $order; |
|
| 17 | - public $orderby; |
|
| 18 | - public $page; |
|
| 19 | - public $per_page; |
|
| 20 | - public $post__in; |
|
| 21 | - public $post__not_in; |
|
| 22 | - public $rating; |
|
| 23 | - public $type; |
|
| 24 | - public $user; |
|
| 13 | + public $assigned_to; |
|
| 14 | + public $category; |
|
| 15 | + public $offset; |
|
| 16 | + public $order; |
|
| 17 | + public $orderby; |
|
| 18 | + public $page; |
|
| 19 | + public $per_page; |
|
| 20 | + public $post__in; |
|
| 21 | + public $post__not_in; |
|
| 22 | + public $rating; |
|
| 23 | + public $type; |
|
| 24 | + public $user; |
|
| 25 | 25 | |
| 26 | - public function __construct(array $args = []) |
|
| 27 | - { |
|
| 28 | - $args = glsr(ReviewsDefaults::class)->merge($args); |
|
| 29 | - $args['assigned_to'] = Arr::uniqueInt(Arr::consolidate($args['assigned_to'])); |
|
| 30 | - $args['category'] = $this->normalizeTermIds($args['category']); |
|
| 31 | - $args['offset'] = absint(filter_var($args['offset'], FILTER_SANITIZE_NUMBER_INT)); |
|
| 32 | - $args['order'] = Str::restrictTo('ASC,DESC,', sanitize_key($args['order']), 'DESC'); // include an empty value |
|
| 33 | - $args['orderby'] = $this->normalizeOrderBy($args['orderby']); |
|
| 34 | - $args['page'] = absint($args['page']); |
|
| 35 | - $args['per_page'] = absint($args['per_page']); // "0" and "-1" = all |
|
| 36 | - $args['post__in'] = Arr::uniqueInt(Arr::consolidate($args['post__in'])); |
|
| 37 | - $args['post__not_in'] = Arr::uniqueInt(Arr::consolidate($args['post__not_in'])); |
|
| 38 | - $args['rating'] = absint(filter_var($args['rating'], FILTER_SANITIZE_NUMBER_INT)); |
|
| 39 | - $args['type'] = sanitize_key($args['type']); |
|
| 40 | - $args['user'] = $this->normalizeUserIds(Arr::consolidate($args['user'])); |
|
| 41 | - parent::__construct($args); |
|
| 42 | - } |
|
| 26 | + public function __construct(array $args = []) |
|
| 27 | + { |
|
| 28 | + $args = glsr(ReviewsDefaults::class)->merge($args); |
|
| 29 | + $args['assigned_to'] = Arr::uniqueInt(Arr::consolidate($args['assigned_to'])); |
|
| 30 | + $args['category'] = $this->normalizeTermIds($args['category']); |
|
| 31 | + $args['offset'] = absint(filter_var($args['offset'], FILTER_SANITIZE_NUMBER_INT)); |
|
| 32 | + $args['order'] = Str::restrictTo('ASC,DESC,', sanitize_key($args['order']), 'DESC'); // include an empty value |
|
| 33 | + $args['orderby'] = $this->normalizeOrderBy($args['orderby']); |
|
| 34 | + $args['page'] = absint($args['page']); |
|
| 35 | + $args['per_page'] = absint($args['per_page']); // "0" and "-1" = all |
|
| 36 | + $args['post__in'] = Arr::uniqueInt(Arr::consolidate($args['post__in'])); |
|
| 37 | + $args['post__not_in'] = Arr::uniqueInt(Arr::consolidate($args['post__not_in'])); |
|
| 38 | + $args['rating'] = absint(filter_var($args['rating'], FILTER_SANITIZE_NUMBER_INT)); |
|
| 39 | + $args['type'] = sanitize_key($args['type']); |
|
| 40 | + $args['user'] = $this->normalizeUserIds(Arr::consolidate($args['user'])); |
|
| 41 | + parent::__construct($args); |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * @return string |
|
| 46 | - */ |
|
| 47 | - protected function normalizeOrderBy($orderBy) |
|
| 48 | - { |
|
| 49 | - $orderBy = Str::restrictTo('author,comment_count,date,ID,menu_order,none,rand,relevance', $orderBy, 'date'); |
|
| 50 | - if (in_array($orderBy, ['comment_count', 'ID', 'menu_order'])) { |
|
| 51 | - return Str::prefix('p.', $orderBy); |
|
| 52 | - } |
|
| 53 | - if (in_array($orderBy, ['author', 'date'])) { |
|
| 54 | - return Str::prefix('p.post_', $orderBy); |
|
| 55 | - } |
|
| 56 | - return $orderBy; |
|
| 57 | - } |
|
| 44 | + /** |
|
| 45 | + * @return string |
|
| 46 | + */ |
|
| 47 | + protected function normalizeOrderBy($orderBy) |
|
| 48 | + { |
|
| 49 | + $orderBy = Str::restrictTo('author,comment_count,date,ID,menu_order,none,rand,relevance', $orderBy, 'date'); |
|
| 50 | + if (in_array($orderBy, ['comment_count', 'ID', 'menu_order'])) { |
|
| 51 | + return Str::prefix('p.', $orderBy); |
|
| 52 | + } |
|
| 53 | + if (in_array($orderBy, ['author', 'date'])) { |
|
| 54 | + return Str::prefix('p.post_', $orderBy); |
|
| 55 | + } |
|
| 56 | + return $orderBy; |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - /** |
|
| 60 | - * @param array[]|string $termIds |
|
| 61 | - * @return array |
|
| 62 | - */ |
|
| 63 | - public function normalizeTermIds($termIds) |
|
| 64 | - { |
|
| 65 | - return glsr(ReviewManager::class)->normalizeTermIds($termIds); |
|
| 66 | - } |
|
| 59 | + /** |
|
| 60 | + * @param array[]|string $termIds |
|
| 61 | + * @return array |
|
| 62 | + */ |
|
| 63 | + public function normalizeTermIds($termIds) |
|
| 64 | + { |
|
| 65 | + return glsr(ReviewManager::class)->normalizeTermIds($termIds); |
|
| 66 | + } |
|
| 67 | 67 | |
| 68 | - /** |
|
| 69 | - * @return array |
|
| 70 | - */ |
|
| 71 | - protected function normalizeUserIds(array $users) |
|
| 72 | - { |
|
| 73 | - $userIds = []; |
|
| 74 | - foreach ($users as $userId) { |
|
| 75 | - if (!is_numeric($userId)) { |
|
| 76 | - $userId = Helper::castToInt(username_exists($userId)); |
|
| 77 | - } |
|
| 78 | - $userIds[] = $userId; |
|
| 79 | - } |
|
| 80 | - return Arr::uniqueInt($userIds); |
|
| 81 | - } |
|
| 68 | + /** |
|
| 69 | + * @return array |
|
| 70 | + */ |
|
| 71 | + protected function normalizeUserIds(array $users) |
|
| 72 | + { |
|
| 73 | + $userIds = []; |
|
| 74 | + foreach ($users as $userId) { |
|
| 75 | + if (!is_numeric($userId)) { |
|
| 76 | + $userId = Helper::castToInt(username_exists($userId)); |
|
| 77 | + } |
|
| 78 | + $userIds[] = $userId; |
|
| 79 | + } |
|
| 80 | + return Arr::uniqueInt($userIds); |
|
| 81 | + } |
|
| 82 | 82 | } |
@@ -23,35 +23,35 @@ discard block |
||
| 23 | 23 | public $type; |
| 24 | 24 | public $user; |
| 25 | 25 | |
| 26 | - public function __construct(array $args = []) |
|
| 26 | + public function __construct( array $args = [] ) |
|
| 27 | 27 | { |
| 28 | - $args = glsr(ReviewsDefaults::class)->merge($args); |
|
| 29 | - $args['assigned_to'] = Arr::uniqueInt(Arr::consolidate($args['assigned_to'])); |
|
| 30 | - $args['category'] = $this->normalizeTermIds($args['category']); |
|
| 31 | - $args['offset'] = absint(filter_var($args['offset'], FILTER_SANITIZE_NUMBER_INT)); |
|
| 32 | - $args['order'] = Str::restrictTo('ASC,DESC,', sanitize_key($args['order']), 'DESC'); // include an empty value |
|
| 33 | - $args['orderby'] = $this->normalizeOrderBy($args['orderby']); |
|
| 34 | - $args['page'] = absint($args['page']); |
|
| 35 | - $args['per_page'] = absint($args['per_page']); // "0" and "-1" = all |
|
| 36 | - $args['post__in'] = Arr::uniqueInt(Arr::consolidate($args['post__in'])); |
|
| 37 | - $args['post__not_in'] = Arr::uniqueInt(Arr::consolidate($args['post__not_in'])); |
|
| 38 | - $args['rating'] = absint(filter_var($args['rating'], FILTER_SANITIZE_NUMBER_INT)); |
|
| 39 | - $args['type'] = sanitize_key($args['type']); |
|
| 40 | - $args['user'] = $this->normalizeUserIds(Arr::consolidate($args['user'])); |
|
| 41 | - parent::__construct($args); |
|
| 28 | + $args = glsr( ReviewsDefaults::class )->merge( $args ); |
|
| 29 | + $args['assigned_to'] = Arr::uniqueInt( Arr::consolidate( $args['assigned_to'] ) ); |
|
| 30 | + $args['category'] = $this->normalizeTermIds( $args['category'] ); |
|
| 31 | + $args['offset'] = absint( filter_var( $args['offset'], FILTER_SANITIZE_NUMBER_INT ) ); |
|
| 32 | + $args['order'] = Str::restrictTo( 'ASC,DESC,', sanitize_key( $args['order'] ), 'DESC' ); // include an empty value |
|
| 33 | + $args['orderby'] = $this->normalizeOrderBy( $args['orderby'] ); |
|
| 34 | + $args['page'] = absint( $args['page'] ); |
|
| 35 | + $args['per_page'] = absint( $args['per_page'] ); // "0" and "-1" = all |
|
| 36 | + $args['post__in'] = Arr::uniqueInt( Arr::consolidate( $args['post__in'] ) ); |
|
| 37 | + $args['post__not_in'] = Arr::uniqueInt( Arr::consolidate( $args['post__not_in'] ) ); |
|
| 38 | + $args['rating'] = absint( filter_var( $args['rating'], FILTER_SANITIZE_NUMBER_INT ) ); |
|
| 39 | + $args['type'] = sanitize_key( $args['type'] ); |
|
| 40 | + $args['user'] = $this->normalizeUserIds( Arr::consolidate( $args['user'] ) ); |
|
| 41 | + parent::__construct( $args ); |
|
| 42 | 42 | } |
| 43 | 43 | |
| 44 | 44 | /** |
| 45 | 45 | * @return string |
| 46 | 46 | */ |
| 47 | - protected function normalizeOrderBy($orderBy) |
|
| 47 | + protected function normalizeOrderBy( $orderBy ) |
|
| 48 | 48 | { |
| 49 | - $orderBy = Str::restrictTo('author,comment_count,date,ID,menu_order,none,rand,relevance', $orderBy, 'date'); |
|
| 50 | - if (in_array($orderBy, ['comment_count', 'ID', 'menu_order'])) { |
|
| 51 | - return Str::prefix('p.', $orderBy); |
|
| 49 | + $orderBy = Str::restrictTo( 'author,comment_count,date,ID,menu_order,none,rand,relevance', $orderBy, 'date' ); |
|
| 50 | + if( in_array( $orderBy, ['comment_count', 'ID', 'menu_order'] ) ) { |
|
| 51 | + return Str::prefix( 'p.', $orderBy ); |
|
| 52 | 52 | } |
| 53 | - if (in_array($orderBy, ['author', 'date'])) { |
|
| 54 | - return Str::prefix('p.post_', $orderBy); |
|
| 53 | + if( in_array( $orderBy, ['author', 'date'] ) ) { |
|
| 54 | + return Str::prefix( 'p.post_', $orderBy ); |
|
| 55 | 55 | } |
| 56 | 56 | return $orderBy; |
| 57 | 57 | } |
@@ -60,23 +60,23 @@ discard block |
||
| 60 | 60 | * @param array[]|string $termIds |
| 61 | 61 | * @return array |
| 62 | 62 | */ |
| 63 | - public function normalizeTermIds($termIds) |
|
| 63 | + public function normalizeTermIds( $termIds ) |
|
| 64 | 64 | { |
| 65 | - return glsr(ReviewManager::class)->normalizeTermIds($termIds); |
|
| 65 | + return glsr( ReviewManager::class )->normalizeTermIds( $termIds ); |
|
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | /** |
| 69 | 69 | * @return array |
| 70 | 70 | */ |
| 71 | - protected function normalizeUserIds(array $users) |
|
| 71 | + protected function normalizeUserIds( array $users ) |
|
| 72 | 72 | { |
| 73 | 73 | $userIds = []; |
| 74 | - foreach ($users as $userId) { |
|
| 75 | - if (!is_numeric($userId)) { |
|
| 76 | - $userId = Helper::castToInt(username_exists($userId)); |
|
| 74 | + foreach( $users as $userId ) { |
|
| 75 | + if( !is_numeric( $userId ) ) { |
|
| 76 | + $userId = Helper::castToInt( username_exists( $userId ) ); |
|
| 77 | 77 | } |
| 78 | 78 | $userIds[] = $userId; |
| 79 | 79 | } |
| 80 | - return Arr::uniqueInt($userIds); |
|
| 80 | + return Arr::uniqueInt( $userIds ); |
|
| 81 | 81 | } |
| 82 | 82 | } |
@@ -12,164 +12,164 @@ |
||
| 12 | 12 | |
| 13 | 13 | class QueryBuilder |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * Build a WP_Query meta_query/tax_query. |
|
| 17 | - * @return array |
|
| 18 | - */ |
|
| 19 | - public function buildQuery(array $keys = [], array $values = []) |
|
| 20 | - { |
|
| 21 | - $queries = []; |
|
| 22 | - foreach ($keys as $key) { |
|
| 23 | - if (!array_key_exists($key, $values)) { |
|
| 24 | - continue; |
|
| 25 | - } |
|
| 26 | - $methodName = Helper::buildMethodName($key, __FUNCTION__); |
|
| 27 | - if (!method_exists($this, $methodName)) { |
|
| 28 | - continue; |
|
| 29 | - } |
|
| 30 | - $query = call_user_func([$this, $methodName], $values[$key]); |
|
| 31 | - if (is_array($query)) { |
|
| 32 | - $queries[] = $query; |
|
| 33 | - } |
|
| 34 | - } |
|
| 35 | - return $queries; |
|
| 36 | - } |
|
| 15 | + /** |
|
| 16 | + * Build a WP_Query meta_query/tax_query. |
|
| 17 | + * @return array |
|
| 18 | + */ |
|
| 19 | + public function buildQuery(array $keys = [], array $values = []) |
|
| 20 | + { |
|
| 21 | + $queries = []; |
|
| 22 | + foreach ($keys as $key) { |
|
| 23 | + if (!array_key_exists($key, $values)) { |
|
| 24 | + continue; |
|
| 25 | + } |
|
| 26 | + $methodName = Helper::buildMethodName($key, __FUNCTION__); |
|
| 27 | + if (!method_exists($this, $methodName)) { |
|
| 28 | + continue; |
|
| 29 | + } |
|
| 30 | + $query = call_user_func([$this, $methodName], $values[$key]); |
|
| 31 | + if (is_array($query)) { |
|
| 32 | + $queries[] = $query; |
|
| 33 | + } |
|
| 34 | + } |
|
| 35 | + return $queries; |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * @return string |
|
| 40 | - */ |
|
| 41 | - public function buildSqlLines(array $values, array $conditions) |
|
| 42 | - { |
|
| 43 | - $string = ''; |
|
| 44 | - $values = array_filter($values); |
|
| 45 | - foreach ($conditions as $key => $value) { |
|
| 46 | - if (!isset($values[$key])) { |
|
| 47 | - continue; |
|
| 48 | - } |
|
| 49 | - $values[$key] = implode(',', (array) $values[$key]); |
|
| 50 | - $string.= Str::contains($value, '%s') |
|
| 51 | - ? sprintf($value, strval($values[$key])) |
|
| 52 | - : $value; |
|
| 53 | - } |
|
| 54 | - return $string; |
|
| 55 | - } |
|
| 38 | + /** |
|
| 39 | + * @return string |
|
| 40 | + */ |
|
| 41 | + public function buildSqlLines(array $values, array $conditions) |
|
| 42 | + { |
|
| 43 | + $string = ''; |
|
| 44 | + $values = array_filter($values); |
|
| 45 | + foreach ($conditions as $key => $value) { |
|
| 46 | + if (!isset($values[$key])) { |
|
| 47 | + continue; |
|
| 48 | + } |
|
| 49 | + $values[$key] = implode(',', (array) $values[$key]); |
|
| 50 | + $string.= Str::contains($value, '%s') |
|
| 51 | + ? sprintf($value, strval($values[$key])) |
|
| 52 | + : $value; |
|
| 53 | + } |
|
| 54 | + return $string; |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * Build a SQL 'OR' string from an array. |
|
| 59 | - * @param string|array $values |
|
| 60 | - * @param string $sprintfFormat |
|
| 61 | - * @return string |
|
| 62 | - */ |
|
| 63 | - public function buildSqlOr($values, $sprintfFormat) |
|
| 64 | - { |
|
| 65 | - if (!is_array($values)) { |
|
| 66 | - $values = explode(',', $values); |
|
| 67 | - } |
|
| 68 | - $values = array_filter(array_map('trim', (array) $values)); |
|
| 69 | - $values = array_map(function ($value) use ($sprintfFormat) { |
|
| 70 | - return sprintf($sprintfFormat, $value); |
|
| 71 | - }, $values); |
|
| 72 | - return implode(' OR ', $values); |
|
| 73 | - } |
|
| 57 | + /** |
|
| 58 | + * Build a SQL 'OR' string from an array. |
|
| 59 | + * @param string|array $values |
|
| 60 | + * @param string $sprintfFormat |
|
| 61 | + * @return string |
|
| 62 | + */ |
|
| 63 | + public function buildSqlOr($values, $sprintfFormat) |
|
| 64 | + { |
|
| 65 | + if (!is_array($values)) { |
|
| 66 | + $values = explode(',', $values); |
|
| 67 | + } |
|
| 68 | + $values = array_filter(array_map('trim', (array) $values)); |
|
| 69 | + $values = array_map(function ($value) use ($sprintfFormat) { |
|
| 70 | + return sprintf($sprintfFormat, $value); |
|
| 71 | + }, $values); |
|
| 72 | + return implode(' OR ', $values); |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - /** |
|
| 76 | - * Get the current page number from the global query. |
|
| 77 | - * @param bool $isEnabled |
|
| 78 | - * @return int |
|
| 79 | - */ |
|
| 80 | - public function getPaged($isEnabled = true) |
|
| 81 | - { |
|
| 82 | - return $isEnabled |
|
| 83 | - ? max(1, intval(filter_input(INPUT_GET, glsr()->constant('PAGED_QUERY_VAR')))) |
|
| 84 | - : 1; |
|
| 85 | - } |
|
| 75 | + /** |
|
| 76 | + * Get the current page number from the global query. |
|
| 77 | + * @param bool $isEnabled |
|
| 78 | + * @return int |
|
| 79 | + */ |
|
| 80 | + public function getPaged($isEnabled = true) |
|
| 81 | + { |
|
| 82 | + return $isEnabled |
|
| 83 | + ? max(1, intval(filter_input(INPUT_GET, glsr()->constant('PAGED_QUERY_VAR')))) |
|
| 84 | + : 1; |
|
| 85 | + } |
|
| 86 | 86 | |
| 87 | - /** |
|
| 88 | - * @param string $value |
|
| 89 | - * @return void|array |
|
| 90 | - */ |
|
| 91 | - protected function buildQueryAssignedTo($value) |
|
| 92 | - { |
|
| 93 | - if (!empty($value)) { |
|
| 94 | - $postIds = Arr::convertFromString($value, 'is_numeric'); |
|
| 95 | - return [ |
|
| 96 | - 'compare' => 'IN', |
|
| 97 | - 'key' => '_assigned_to', |
|
| 98 | - 'value' => glsr(Multilingual::class)->getPostIds($postIds), |
|
| 99 | - ]; |
|
| 100 | - } |
|
| 101 | - } |
|
| 87 | + /** |
|
| 88 | + * @param string $value |
|
| 89 | + * @return void|array |
|
| 90 | + */ |
|
| 91 | + protected function buildQueryAssignedTo($value) |
|
| 92 | + { |
|
| 93 | + if (!empty($value)) { |
|
| 94 | + $postIds = Arr::convertFromString($value, 'is_numeric'); |
|
| 95 | + return [ |
|
| 96 | + 'compare' => 'IN', |
|
| 97 | + 'key' => '_assigned_to', |
|
| 98 | + 'value' => glsr(Multilingual::class)->getPostIds($postIds), |
|
| 99 | + ]; |
|
| 100 | + } |
|
| 101 | + } |
|
| 102 | 102 | |
| 103 | - /** |
|
| 104 | - * @param array $value |
|
| 105 | - * @return void|array |
|
| 106 | - */ |
|
| 107 | - protected function buildQueryCategory($value) |
|
| 108 | - { |
|
| 109 | - if (!empty($value)) { |
|
| 110 | - return [ |
|
| 111 | - 'field' => 'term_id', |
|
| 112 | - 'taxonomy' => Application::TAXONOMY, |
|
| 113 | - 'terms' => $value, |
|
| 114 | - ]; |
|
| 115 | - } |
|
| 116 | - } |
|
| 103 | + /** |
|
| 104 | + * @param array $value |
|
| 105 | + * @return void|array |
|
| 106 | + */ |
|
| 107 | + protected function buildQueryCategory($value) |
|
| 108 | + { |
|
| 109 | + if (!empty($value)) { |
|
| 110 | + return [ |
|
| 111 | + 'field' => 'term_id', |
|
| 112 | + 'taxonomy' => Application::TAXONOMY, |
|
| 113 | + 'terms' => $value, |
|
| 114 | + ]; |
|
| 115 | + } |
|
| 116 | + } |
|
| 117 | 117 | |
| 118 | - /** |
|
| 119 | - * @param string $value |
|
| 120 | - * @return void|array |
|
| 121 | - */ |
|
| 122 | - protected function buildQueryEmail($value) |
|
| 123 | - { |
|
| 124 | - if (!empty($value)) { |
|
| 125 | - return [ |
|
| 126 | - 'key' => '_email', |
|
| 127 | - 'value' => $value, |
|
| 128 | - ]; |
|
| 129 | - } |
|
| 130 | - } |
|
| 118 | + /** |
|
| 119 | + * @param string $value |
|
| 120 | + * @return void|array |
|
| 121 | + */ |
|
| 122 | + protected function buildQueryEmail($value) |
|
| 123 | + { |
|
| 124 | + if (!empty($value)) { |
|
| 125 | + return [ |
|
| 126 | + 'key' => '_email', |
|
| 127 | + 'value' => $value, |
|
| 128 | + ]; |
|
| 129 | + } |
|
| 130 | + } |
|
| 131 | 131 | |
| 132 | - /** |
|
| 133 | - * @param string $value |
|
| 134 | - * @return void|array |
|
| 135 | - */ |
|
| 136 | - protected function buildQueryIpAddress($value) |
|
| 137 | - { |
|
| 138 | - if (!empty($value)) { |
|
| 139 | - return [ |
|
| 140 | - 'key' => '_ip_address', |
|
| 141 | - 'value' => $value, |
|
| 142 | - ]; |
|
| 143 | - } |
|
| 144 | - } |
|
| 132 | + /** |
|
| 133 | + * @param string $value |
|
| 134 | + * @return void|array |
|
| 135 | + */ |
|
| 136 | + protected function buildQueryIpAddress($value) |
|
| 137 | + { |
|
| 138 | + if (!empty($value)) { |
|
| 139 | + return [ |
|
| 140 | + 'key' => '_ip_address', |
|
| 141 | + 'value' => $value, |
|
| 142 | + ]; |
|
| 143 | + } |
|
| 144 | + } |
|
| 145 | 145 | |
| 146 | - /** |
|
| 147 | - * @param string $value |
|
| 148 | - * @return void|array |
|
| 149 | - */ |
|
| 150 | - protected function buildQueryRating($value) |
|
| 151 | - { |
|
| 152 | - if (is_numeric($value) |
|
| 153 | - && in_array(intval($value), range(1, glsr()->constant('MAX_RATING', Rating::class)))) { |
|
| 154 | - return [ |
|
| 155 | - 'compare' => '>=', |
|
| 156 | - 'key' => '_rating', |
|
| 157 | - 'value' => $value, |
|
| 158 | - ]; |
|
| 159 | - } |
|
| 160 | - } |
|
| 146 | + /** |
|
| 147 | + * @param string $value |
|
| 148 | + * @return void|array |
|
| 149 | + */ |
|
| 150 | + protected function buildQueryRating($value) |
|
| 151 | + { |
|
| 152 | + if (is_numeric($value) |
|
| 153 | + && in_array(intval($value), range(1, glsr()->constant('MAX_RATING', Rating::class)))) { |
|
| 154 | + return [ |
|
| 155 | + 'compare' => '>=', |
|
| 156 | + 'key' => '_rating', |
|
| 157 | + 'value' => $value, |
|
| 158 | + ]; |
|
| 159 | + } |
|
| 160 | + } |
|
| 161 | 161 | |
| 162 | - /** |
|
| 163 | - * @param string $value |
|
| 164 | - * @return void|array |
|
| 165 | - */ |
|
| 166 | - protected function buildQueryType($value) |
|
| 167 | - { |
|
| 168 | - if (!in_array($value, ['', 'all'])) { |
|
| 169 | - return [ |
|
| 170 | - 'key' => '_review_type', |
|
| 171 | - 'value' => $value, |
|
| 172 | - ]; |
|
| 173 | - } |
|
| 174 | - } |
|
| 162 | + /** |
|
| 163 | + * @param string $value |
|
| 164 | + * @return void|array |
|
| 165 | + */ |
|
| 166 | + protected function buildQueryType($value) |
|
| 167 | + { |
|
| 168 | + if (!in_array($value, ['', 'all'])) { |
|
| 169 | + return [ |
|
| 170 | + 'key' => '_review_type', |
|
| 171 | + 'value' => $value, |
|
| 172 | + ]; |
|
| 173 | + } |
|
| 174 | + } |
|
| 175 | 175 | } |
@@ -16,19 +16,19 @@ discard block |
||
| 16 | 16 | * Build a WP_Query meta_query/tax_query. |
| 17 | 17 | * @return array |
| 18 | 18 | */ |
| 19 | - public function buildQuery(array $keys = [], array $values = []) |
|
| 19 | + public function buildQuery( array $keys = [], array $values = [] ) |
|
| 20 | 20 | { |
| 21 | 21 | $queries = []; |
| 22 | - foreach ($keys as $key) { |
|
| 23 | - if (!array_key_exists($key, $values)) { |
|
| 22 | + foreach( $keys as $key ) { |
|
| 23 | + if( !array_key_exists( $key, $values ) ) { |
|
| 24 | 24 | continue; |
| 25 | 25 | } |
| 26 | - $methodName = Helper::buildMethodName($key, __FUNCTION__); |
|
| 27 | - if (!method_exists($this, $methodName)) { |
|
| 26 | + $methodName = Helper::buildMethodName( $key, __FUNCTION__ ); |
|
| 27 | + if( !method_exists( $this, $methodName ) ) { |
|
| 28 | 28 | continue; |
| 29 | 29 | } |
| 30 | - $query = call_user_func([$this, $methodName], $values[$key]); |
|
| 31 | - if (is_array($query)) { |
|
| 30 | + $query = call_user_func( [$this, $methodName], $values[$key] ); |
|
| 31 | + if( is_array( $query ) ) { |
|
| 32 | 32 | $queries[] = $query; |
| 33 | 33 | } |
| 34 | 34 | } |
@@ -38,17 +38,17 @@ discard block |
||
| 38 | 38 | /** |
| 39 | 39 | * @return string |
| 40 | 40 | */ |
| 41 | - public function buildSqlLines(array $values, array $conditions) |
|
| 41 | + public function buildSqlLines( array $values, array $conditions ) |
|
| 42 | 42 | { |
| 43 | 43 | $string = ''; |
| 44 | - $values = array_filter($values); |
|
| 45 | - foreach ($conditions as $key => $value) { |
|
| 46 | - if (!isset($values[$key])) { |
|
| 44 | + $values = array_filter( $values ); |
|
| 45 | + foreach( $conditions as $key => $value ) { |
|
| 46 | + if( !isset($values[$key]) ) { |
|
| 47 | 47 | continue; |
| 48 | 48 | } |
| 49 | - $values[$key] = implode(',', (array) $values[$key]); |
|
| 50 | - $string.= Str::contains($value, '%s') |
|
| 51 | - ? sprintf($value, strval($values[$key])) |
|
| 49 | + $values[$key] = implode( ',', (array)$values[$key] ); |
|
| 50 | + $string .= Str::contains( $value, '%s' ) |
|
| 51 | + ? sprintf( $value, strval( $values[$key] ) ) |
|
| 52 | 52 | : $value; |
| 53 | 53 | } |
| 54 | 54 | return $string; |
@@ -60,16 +60,16 @@ discard block |
||
| 60 | 60 | * @param string $sprintfFormat |
| 61 | 61 | * @return string |
| 62 | 62 | */ |
| 63 | - public function buildSqlOr($values, $sprintfFormat) |
|
| 63 | + public function buildSqlOr( $values, $sprintfFormat ) |
|
| 64 | 64 | { |
| 65 | - if (!is_array($values)) { |
|
| 66 | - $values = explode(',', $values); |
|
| 65 | + if( !is_array( $values ) ) { |
|
| 66 | + $values = explode( ',', $values ); |
|
| 67 | 67 | } |
| 68 | - $values = array_filter(array_map('trim', (array) $values)); |
|
| 69 | - $values = array_map(function ($value) use ($sprintfFormat) { |
|
| 70 | - return sprintf($sprintfFormat, $value); |
|
| 71 | - }, $values); |
|
| 72 | - return implode(' OR ', $values); |
|
| 68 | + $values = array_filter( array_map( 'trim', (array)$values ) ); |
|
| 69 | + $values = array_map( function( $value ) use ($sprintfFormat) { |
|
| 70 | + return sprintf( $sprintfFormat, $value ); |
|
| 71 | + }, $values ); |
|
| 72 | + return implode( ' OR ', $values ); |
|
| 73 | 73 | } |
| 74 | 74 | |
| 75 | 75 | /** |
@@ -77,10 +77,10 @@ discard block |
||
| 77 | 77 | * @param bool $isEnabled |
| 78 | 78 | * @return int |
| 79 | 79 | */ |
| 80 | - public function getPaged($isEnabled = true) |
|
| 80 | + public function getPaged( $isEnabled = true ) |
|
| 81 | 81 | { |
| 82 | 82 | return $isEnabled |
| 83 | - ? max(1, intval(filter_input(INPUT_GET, glsr()->constant('PAGED_QUERY_VAR')))) |
|
| 83 | + ? max( 1, intval( filter_input( INPUT_GET, glsr()->constant( 'PAGED_QUERY_VAR' ) ) ) ) |
|
| 84 | 84 | : 1; |
| 85 | 85 | } |
| 86 | 86 | |
@@ -88,14 +88,14 @@ discard block |
||
| 88 | 88 | * @param string $value |
| 89 | 89 | * @return void|array |
| 90 | 90 | */ |
| 91 | - protected function buildQueryAssignedTo($value) |
|
| 91 | + protected function buildQueryAssignedTo( $value ) |
|
| 92 | 92 | { |
| 93 | - if (!empty($value)) { |
|
| 94 | - $postIds = Arr::convertFromString($value, 'is_numeric'); |
|
| 93 | + if( !empty($value) ) { |
|
| 94 | + $postIds = Arr::convertFromString( $value, 'is_numeric' ); |
|
| 95 | 95 | return [ |
| 96 | 96 | 'compare' => 'IN', |
| 97 | 97 | 'key' => '_assigned_to', |
| 98 | - 'value' => glsr(Multilingual::class)->getPostIds($postIds), |
|
| 98 | + 'value' => glsr( Multilingual::class )->getPostIds( $postIds ), |
|
| 99 | 99 | ]; |
| 100 | 100 | } |
| 101 | 101 | } |
@@ -104,9 +104,9 @@ discard block |
||
| 104 | 104 | * @param array $value |
| 105 | 105 | * @return void|array |
| 106 | 106 | */ |
| 107 | - protected function buildQueryCategory($value) |
|
| 107 | + protected function buildQueryCategory( $value ) |
|
| 108 | 108 | { |
| 109 | - if (!empty($value)) { |
|
| 109 | + if( !empty($value) ) { |
|
| 110 | 110 | return [ |
| 111 | 111 | 'field' => 'term_id', |
| 112 | 112 | 'taxonomy' => Application::TAXONOMY, |
@@ -119,9 +119,9 @@ discard block |
||
| 119 | 119 | * @param string $value |
| 120 | 120 | * @return void|array |
| 121 | 121 | */ |
| 122 | - protected function buildQueryEmail($value) |
|
| 122 | + protected function buildQueryEmail( $value ) |
|
| 123 | 123 | { |
| 124 | - if (!empty($value)) { |
|
| 124 | + if( !empty($value) ) { |
|
| 125 | 125 | return [ |
| 126 | 126 | 'key' => '_email', |
| 127 | 127 | 'value' => $value, |
@@ -133,9 +133,9 @@ discard block |
||
| 133 | 133 | * @param string $value |
| 134 | 134 | * @return void|array |
| 135 | 135 | */ |
| 136 | - protected function buildQueryIpAddress($value) |
|
| 136 | + protected function buildQueryIpAddress( $value ) |
|
| 137 | 137 | { |
| 138 | - if (!empty($value)) { |
|
| 138 | + if( !empty($value) ) { |
|
| 139 | 139 | return [ |
| 140 | 140 | 'key' => '_ip_address', |
| 141 | 141 | 'value' => $value, |
@@ -147,10 +147,10 @@ discard block |
||
| 147 | 147 | * @param string $value |
| 148 | 148 | * @return void|array |
| 149 | 149 | */ |
| 150 | - protected function buildQueryRating($value) |
|
| 150 | + protected function buildQueryRating( $value ) |
|
| 151 | 151 | { |
| 152 | - if (is_numeric($value) |
|
| 153 | - && in_array(intval($value), range(1, glsr()->constant('MAX_RATING', Rating::class)))) { |
|
| 152 | + if( is_numeric( $value ) |
|
| 153 | + && in_array( intval( $value ), range( 1, glsr()->constant( 'MAX_RATING', Rating::class ) ) ) ) { |
|
| 154 | 154 | return [ |
| 155 | 155 | 'compare' => '>=', |
| 156 | 156 | 'key' => '_rating', |
@@ -163,9 +163,9 @@ discard block |
||
| 163 | 163 | * @param string $value |
| 164 | 164 | * @return void|array |
| 165 | 165 | */ |
| 166 | - protected function buildQueryType($value) |
|
| 166 | + protected function buildQueryType( $value ) |
|
| 167 | 167 | { |
| 168 | - if (!in_array($value, ['', 'all'])) { |
|
| 168 | + if( !in_array( $value, ['', 'all'] ) ) { |
|
| 169 | 169 | return [ |
| 170 | 170 | 'key' => '_review_type', |
| 171 | 171 | 'value' => $value, |
@@ -8,26 +8,26 @@ |
||
| 8 | 8 | |
| 9 | 9 | class RatingManager |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * @param bool $flatten |
|
| 13 | - * @return array |
|
| 14 | - */ |
|
| 15 | - public function ratings(array $args = []) |
|
| 16 | - { |
|
| 17 | - $args = wp_parse_args($args, [ |
|
| 18 | - 'max' => glsr()->constant('MAX_RATING', Rating::class), |
|
| 19 | - 'min' => glsr()->constant('MIN_RATING', Rating::class), |
|
| 20 | - ]); |
|
| 21 | - $ratings = []; |
|
| 22 | - $results = glsr(Query::class)->ratings($args); |
|
| 23 | - array_walk_recursive($results, function ($rating, $index) use (&$ratings) { |
|
| 24 | - $ratings[$index] = $rating + intval(Arr::get($ratings, $index, 0)); |
|
| 25 | - }); |
|
| 26 | - foreach ($ratings as $index => &$rating) { |
|
| 27 | - if (!Helper::inRange($index, $args['min'], $args['max'])) { |
|
| 28 | - $rating = 0; |
|
| 29 | - } |
|
| 30 | - } |
|
| 31 | - return $ratings; |
|
| 32 | - } |
|
| 11 | + /** |
|
| 12 | + * @param bool $flatten |
|
| 13 | + * @return array |
|
| 14 | + */ |
|
| 15 | + public function ratings(array $args = []) |
|
| 16 | + { |
|
| 17 | + $args = wp_parse_args($args, [ |
|
| 18 | + 'max' => glsr()->constant('MAX_RATING', Rating::class), |
|
| 19 | + 'min' => glsr()->constant('MIN_RATING', Rating::class), |
|
| 20 | + ]); |
|
| 21 | + $ratings = []; |
|
| 22 | + $results = glsr(Query::class)->ratings($args); |
|
| 23 | + array_walk_recursive($results, function ($rating, $index) use (&$ratings) { |
|
| 24 | + $ratings[$index] = $rating + intval(Arr::get($ratings, $index, 0)); |
|
| 25 | + }); |
|
| 26 | + foreach ($ratings as $index => &$rating) { |
|
| 27 | + if (!Helper::inRange($index, $args['min'], $args['max'])) { |
|
| 28 | + $rating = 0; |
|
| 29 | + } |
|
| 30 | + } |
|
| 31 | + return $ratings; |
|
| 32 | + } |
|
| 33 | 33 | } |
@@ -12,19 +12,19 @@ |
||
| 12 | 12 | * @param bool $flatten |
| 13 | 13 | * @return array |
| 14 | 14 | */ |
| 15 | - public function ratings(array $args = []) |
|
| 15 | + public function ratings( array $args = [] ) |
|
| 16 | 16 | { |
| 17 | - $args = wp_parse_args($args, [ |
|
| 18 | - 'max' => glsr()->constant('MAX_RATING', Rating::class), |
|
| 19 | - 'min' => glsr()->constant('MIN_RATING', Rating::class), |
|
| 20 | - ]); |
|
| 17 | + $args = wp_parse_args( $args, [ |
|
| 18 | + 'max' => glsr()->constant( 'MAX_RATING', Rating::class ), |
|
| 19 | + 'min' => glsr()->constant( 'MIN_RATING', Rating::class ), |
|
| 20 | + ] ); |
|
| 21 | 21 | $ratings = []; |
| 22 | - $results = glsr(Query::class)->ratings($args); |
|
| 23 | - array_walk_recursive($results, function ($rating, $index) use (&$ratings) { |
|
| 24 | - $ratings[$index] = $rating + intval(Arr::get($ratings, $index, 0)); |
|
| 22 | + $results = glsr( Query::class )->ratings( $args ); |
|
| 23 | + array_walk_recursive( $results, function( $rating, $index ) use (&$ratings) { |
|
| 24 | + $ratings[$index] = $rating + intval( Arr::get( $ratings, $index, 0 ) ); |
|
| 25 | 25 | }); |
| 26 | - foreach ($ratings as $index => &$rating) { |
|
| 27 | - if (!Helper::inRange($index, $args['min'], $args['max'])) { |
|
| 26 | + foreach( $ratings as $index => &$rating ) { |
|
| 27 | + if( !Helper::inRange( $index, $args['min'], $args['max'] ) ) { |
|
| 28 | 28 | $rating = 0; |
| 29 | 29 | } |
| 30 | 30 | } |
@@ -9,46 +9,46 @@ discard block |
||
| 9 | 9 | |
| 10 | 10 | class Query |
| 11 | 11 | { |
| 12 | - use QuerySql; |
|
| 12 | + use QuerySql; |
|
| 13 | 13 | |
| 14 | - public $args; |
|
| 15 | - public $db; |
|
| 14 | + public $args; |
|
| 15 | + public $db; |
|
| 16 | 16 | |
| 17 | - public function __construct() |
|
| 18 | - { |
|
| 19 | - global $wpdb; |
|
| 20 | - $this->db = $wpdb; |
|
| 21 | - } |
|
| 17 | + public function __construct() |
|
| 18 | + { |
|
| 19 | + global $wpdb; |
|
| 20 | + $this->db = $wpdb; |
|
| 21 | + } |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * @return array |
|
| 25 | - */ |
|
| 26 | - public function ratings(array $args) |
|
| 27 | - { |
|
| 28 | - $this->setArgs($args); |
|
| 29 | - $join = implode(' ', $this->sqlClauses([], 'join')); |
|
| 30 | - $and = implode(' ', $this->sqlClauses([], 'and')); |
|
| 31 | - $results = $this->db->get_results(" |
|
| 23 | + /** |
|
| 24 | + * @return array |
|
| 25 | + */ |
|
| 26 | + public function ratings(array $args) |
|
| 27 | + { |
|
| 28 | + $this->setArgs($args); |
|
| 29 | + $join = implode(' ', $this->sqlClauses([], 'join')); |
|
| 30 | + $and = implode(' ', $this->sqlClauses([], 'and')); |
|
| 31 | + $results = $this->db->get_results(" |
|
| 32 | 32 | SELECT r.rating, r.type, COUNT(r.rating) AS count |
| 33 | 33 | FROM {$this->table('ratings')} AS r {$join} |
| 34 | 34 | WHERE r.is_approved = 1 {$and} |
| 35 | 35 | GROUP BY r.type, r.rating |
| 36 | 36 | ", ARRAY_A); |
| 37 | - return $this->normalizeRatings($results); |
|
| 38 | - } |
|
| 37 | + return $this->normalizeRatings($results); |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * @todo make sure we delete the cached review when modifying it |
|
| 42 | - * @param int $postId |
|
| 43 | - * @return Review |
|
| 44 | - */ |
|
| 45 | - public function review($postId) |
|
| 46 | - { |
|
| 47 | - $reviewId = Helper::castToInt($postId); |
|
| 48 | - if (($review = glsr(Cache::class)->get($reviewId, 'reviews')) instanceof Review) { |
|
| 49 | - return $review; |
|
| 50 | - } |
|
| 51 | - $result = $this->db->get_row(" |
|
| 40 | + /** |
|
| 41 | + * @todo make sure we delete the cached review when modifying it |
|
| 42 | + * @param int $postId |
|
| 43 | + * @return Review |
|
| 44 | + */ |
|
| 45 | + public function review($postId) |
|
| 46 | + { |
|
| 47 | + $reviewId = Helper::castToInt($postId); |
|
| 48 | + if (($review = glsr(Cache::class)->get($reviewId, 'reviews')) instanceof Review) { |
|
| 49 | + return $review; |
|
| 50 | + } |
|
| 51 | + $result = $this->db->get_row(" |
|
| 52 | 52 | {$this->sqlSelect()} |
| 53 | 53 | {$this->sqlFrom()} |
| 54 | 54 | {$this->sqlJoin()} |
@@ -56,23 +56,23 @@ discard block |
||
| 56 | 56 | WHERE r.review_id = {$reviewId} |
| 57 | 57 | GROUP BY r.ID |
| 58 | 58 | "); |
| 59 | - $review = new Review($result); |
|
| 60 | - if (!empty($result)) { |
|
| 61 | - glsr(Cache::class)->store($reviewId, 'reviews', $review); |
|
| 62 | - } |
|
| 63 | - return $review; |
|
| 64 | - } |
|
| 59 | + $review = new Review($result); |
|
| 60 | + if (!empty($result)) { |
|
| 61 | + glsr(Cache::class)->store($reviewId, 'reviews', $review); |
|
| 62 | + } |
|
| 63 | + return $review; |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - /** |
|
| 67 | - * @return array |
|
| 68 | - */ |
|
| 69 | - public function reviews(array $args = [], array $postIds = []) |
|
| 70 | - { |
|
| 71 | - $this->setArgs($args); |
|
| 72 | - if (!empty($postIds)) { |
|
| 73 | - $postIds = implode(',', Arr::uniqueInt($postIds)); |
|
| 74 | - } else { |
|
| 75 | - $postIds = "SELECT ids.* FROM ( |
|
| 66 | + /** |
|
| 67 | + * @return array |
|
| 68 | + */ |
|
| 69 | + public function reviews(array $args = [], array $postIds = []) |
|
| 70 | + { |
|
| 71 | + $this->setArgs($args); |
|
| 72 | + if (!empty($postIds)) { |
|
| 73 | + $postIds = implode(',', Arr::uniqueInt($postIds)); |
|
| 74 | + } else { |
|
| 75 | + $postIds = "SELECT ids.* FROM ( |
|
| 76 | 76 | SELECT r.review_id |
| 77 | 77 | {$this->sqlFrom()} |
| 78 | 78 | {$this->sqlJoinClauses()} |
@@ -81,8 +81,8 @@ discard block |
||
| 81 | 81 | {$this->sqlLimit()} |
| 82 | 82 | {$this->sqlOffset()} |
| 83 | 83 | ) as ids"; |
| 84 | - } |
|
| 85 | - $results = $this->db->get_results(" |
|
| 84 | + } |
|
| 85 | + $results = $this->db->get_results(" |
|
| 86 | 86 | {$this->sqlSelect()} |
| 87 | 87 | {$this->sqlFrom()} |
| 88 | 88 | {$this->sqlJoin()} |
@@ -90,65 +90,65 @@ discard block |
||
| 90 | 90 | WHERE r.review_id in ({$postIds}) |
| 91 | 91 | GROUP BY r.ID |
| 92 | 92 | "); |
| 93 | - foreach ($results as &$result) { |
|
| 94 | - $result = new Review($result); |
|
| 95 | - glsr(Cache::class)->store($result->ID, 'reviews', $result); |
|
| 96 | - } |
|
| 97 | - return $results; |
|
| 98 | - } |
|
| 93 | + foreach ($results as &$result) { |
|
| 94 | + $result = new Review($result); |
|
| 95 | + glsr(Cache::class)->store($result->ID, 'reviews', $result); |
|
| 96 | + } |
|
| 97 | + return $results; |
|
| 98 | + } |
|
| 99 | 99 | |
| 100 | - /** |
|
| 101 | - * @return int |
|
| 102 | - */ |
|
| 103 | - public function totalReviews(array $args = [], array $reviews = []) |
|
| 104 | - { |
|
| 105 | - $this->setArgs($args); |
|
| 106 | - if (empty($this->sqlLimit()) && !empty($reviews)) { |
|
| 107 | - return count($reviews); |
|
| 108 | - } |
|
| 109 | - return (int) $this->db->get_var(" |
|
| 100 | + /** |
|
| 101 | + * @return int |
|
| 102 | + */ |
|
| 103 | + public function totalReviews(array $args = [], array $reviews = []) |
|
| 104 | + { |
|
| 105 | + $this->setArgs($args); |
|
| 106 | + if (empty($this->sqlLimit()) && !empty($reviews)) { |
|
| 107 | + return count($reviews); |
|
| 108 | + } |
|
| 109 | + return (int) $this->db->get_var(" |
|
| 110 | 110 | SELECT COUNT(*) |
| 111 | 111 | {$this->sqlFrom()} |
| 112 | 112 | {$this->sqlJoin()} |
| 113 | 113 | {$this->sqlWhere()} |
| 114 | 114 | "); |
| 115 | - } |
|
| 115 | + } |
|
| 116 | 116 | |
| 117 | - /** |
|
| 118 | - * @param int $postId |
|
| 119 | - * @return bool |
|
| 120 | - */ |
|
| 121 | - public function hasRevisions($postId) |
|
| 122 | - { |
|
| 123 | - $revisions = (int) $this->db->get_var(" |
|
| 117 | + /** |
|
| 118 | + * @param int $postId |
|
| 119 | + * @return bool |
|
| 120 | + */ |
|
| 121 | + public function hasRevisions($postId) |
|
| 122 | + { |
|
| 123 | + $revisions = (int) $this->db->get_var(" |
|
| 124 | 124 | SELECT COUNT(*) |
| 125 | 125 | FROM {$this->db->posts} |
| 126 | 126 | WHERE post_type = 'revision' AND post_parent = {$postId} |
| 127 | 127 | "); |
| 128 | - return $revisions > 0; |
|
| 129 | - } |
|
| 128 | + return $revisions > 0; |
|
| 129 | + } |
|
| 130 | 130 | |
| 131 | - /** |
|
| 132 | - * @return array |
|
| 133 | - */ |
|
| 134 | - protected function normalizeRatings(array $ratings = []) |
|
| 135 | - { |
|
| 136 | - $normalized = []; |
|
| 137 | - foreach ($ratings as $result) { |
|
| 138 | - $type = $result['type']; |
|
| 139 | - if (!array_key_exists($type, $normalized)) { |
|
| 140 | - $normalized[$type] = glsr(Rating::class)->emptyArray(); |
|
| 141 | - } |
|
| 142 | - $normalized[$type] = Arr::set($normalized[$type], $result['rating'], $result['count']); |
|
| 143 | - } |
|
| 144 | - return $normalized; |
|
| 145 | - } |
|
| 131 | + /** |
|
| 132 | + * @return array |
|
| 133 | + */ |
|
| 134 | + protected function normalizeRatings(array $ratings = []) |
|
| 135 | + { |
|
| 136 | + $normalized = []; |
|
| 137 | + foreach ($ratings as $result) { |
|
| 138 | + $type = $result['type']; |
|
| 139 | + if (!array_key_exists($type, $normalized)) { |
|
| 140 | + $normalized[$type] = glsr(Rating::class)->emptyArray(); |
|
| 141 | + } |
|
| 142 | + $normalized[$type] = Arr::set($normalized[$type], $result['rating'], $result['count']); |
|
| 143 | + } |
|
| 144 | + return $normalized; |
|
| 145 | + } |
|
| 146 | 146 | |
| 147 | - /** |
|
| 148 | - * @return void |
|
| 149 | - */ |
|
| 150 | - public function setArgs(array $args = []) |
|
| 151 | - { |
|
| 152 | - $this->args = (new NormalizeQueryArgs($args))->toArray(); |
|
| 153 | - } |
|
| 147 | + /** |
|
| 148 | + * @return void |
|
| 149 | + */ |
|
| 150 | + public function setArgs(array $args = []) |
|
| 151 | + { |
|
| 152 | + $this->args = (new NormalizeQueryArgs($args))->toArray(); |
|
| 153 | + } |
|
| 154 | 154 | } |
@@ -23,18 +23,18 @@ discard block |
||
| 23 | 23 | /** |
| 24 | 24 | * @return array |
| 25 | 25 | */ |
| 26 | - public function ratings(array $args) |
|
| 26 | + public function ratings( array $args ) |
|
| 27 | 27 | { |
| 28 | - $this->setArgs($args); |
|
| 29 | - $join = implode(' ', $this->sqlClauses([], 'join')); |
|
| 30 | - $and = implode(' ', $this->sqlClauses([], 'and')); |
|
| 31 | - $results = $this->db->get_results(" |
|
| 28 | + $this->setArgs( $args ); |
|
| 29 | + $join = implode( ' ', $this->sqlClauses( [], 'join' ) ); |
|
| 30 | + $and = implode( ' ', $this->sqlClauses( [], 'and' ) ); |
|
| 31 | + $results = $this->db->get_results( " |
|
| 32 | 32 | SELECT r.rating, r.type, COUNT(r.rating) AS count |
| 33 | - FROM {$this->table('ratings')} AS r {$join} |
|
| 33 | + FROM {$this->table( 'ratings' )} AS r {$join} |
|
| 34 | 34 | WHERE r.is_approved = 1 {$and} |
| 35 | 35 | GROUP BY r.type, r.rating |
| 36 | - ", ARRAY_A); |
|
| 37 | - return $this->normalizeRatings($results); |
|
| 36 | + ", ARRAY_A ); |
|
| 37 | + return $this->normalizeRatings( $results ); |
|
| 38 | 38 | } |
| 39 | 39 | |
| 40 | 40 | /** |
@@ -42,23 +42,23 @@ discard block |
||
| 42 | 42 | * @param int $postId |
| 43 | 43 | * @return Review |
| 44 | 44 | */ |
| 45 | - public function review($postId) |
|
| 45 | + public function review( $postId ) |
|
| 46 | 46 | { |
| 47 | - $reviewId = Helper::castToInt($postId); |
|
| 48 | - if (($review = glsr(Cache::class)->get($reviewId, 'reviews')) instanceof Review) { |
|
| 47 | + $reviewId = Helper::castToInt( $postId ); |
|
| 48 | + if( ($review = glsr( Cache::class )->get( $reviewId, 'reviews' )) instanceof Review ) { |
|
| 49 | 49 | return $review; |
| 50 | 50 | } |
| 51 | - $result = $this->db->get_row(" |
|
| 51 | + $result = $this->db->get_row( " |
|
| 52 | 52 | {$this->sqlSelect()} |
| 53 | 53 | {$this->sqlFrom()} |
| 54 | 54 | {$this->sqlJoin()} |
| 55 | 55 | {$this->sqlJoinPivots()} |
| 56 | 56 | WHERE r.review_id = {$reviewId} |
| 57 | 57 | GROUP BY r.ID |
| 58 | - "); |
|
| 59 | - $review = new Review($result); |
|
| 60 | - if (!empty($result)) { |
|
| 61 | - glsr(Cache::class)->store($reviewId, 'reviews', $review); |
|
| 58 | + " ); |
|
| 59 | + $review = new Review( $result ); |
|
| 60 | + if( !empty($result) ) { |
|
| 61 | + glsr( Cache::class )->store( $reviewId, 'reviews', $review ); |
|
| 62 | 62 | } |
| 63 | 63 | return $review; |
| 64 | 64 | } |
@@ -66,11 +66,11 @@ discard block |
||
| 66 | 66 | /** |
| 67 | 67 | * @return array |
| 68 | 68 | */ |
| 69 | - public function reviews(array $args = [], array $postIds = []) |
|
| 69 | + public function reviews( array $args = [], array $postIds = [] ) |
|
| 70 | 70 | { |
| 71 | - $this->setArgs($args); |
|
| 72 | - if (!empty($postIds)) { |
|
| 73 | - $postIds = implode(',', Arr::uniqueInt($postIds)); |
|
| 71 | + $this->setArgs( $args ); |
|
| 72 | + if( !empty($postIds) ) { |
|
| 73 | + $postIds = implode( ',', Arr::uniqueInt( $postIds ) ); |
|
| 74 | 74 | } else { |
| 75 | 75 | $postIds = "SELECT ids.* FROM ( |
| 76 | 76 | SELECT r.review_id |
@@ -82,17 +82,17 @@ discard block |
||
| 82 | 82 | {$this->sqlOffset()} |
| 83 | 83 | ) as ids"; |
| 84 | 84 | } |
| 85 | - $results = $this->db->get_results(" |
|
| 85 | + $results = $this->db->get_results( " |
|
| 86 | 86 | {$this->sqlSelect()} |
| 87 | 87 | {$this->sqlFrom()} |
| 88 | 88 | {$this->sqlJoin()} |
| 89 | 89 | {$this->sqlJoinPivots()} |
| 90 | 90 | WHERE r.review_id in ({$postIds}) |
| 91 | 91 | GROUP BY r.ID |
| 92 | - "); |
|
| 93 | - foreach ($results as &$result) { |
|
| 94 | - $result = new Review($result); |
|
| 95 | - glsr(Cache::class)->store($result->ID, 'reviews', $result); |
|
| 92 | + " ); |
|
| 93 | + foreach( $results as &$result ) { |
|
| 94 | + $result = new Review( $result ); |
|
| 95 | + glsr( Cache::class )->store( $result->ID, 'reviews', $result ); |
|
| 96 | 96 | } |
| 97 | 97 | return $results; |
| 98 | 98 | } |
@@ -100,46 +100,46 @@ discard block |
||
| 100 | 100 | /** |
| 101 | 101 | * @return int |
| 102 | 102 | */ |
| 103 | - public function totalReviews(array $args = [], array $reviews = []) |
|
| 103 | + public function totalReviews( array $args = [], array $reviews = [] ) |
|
| 104 | 104 | { |
| 105 | - $this->setArgs($args); |
|
| 106 | - if (empty($this->sqlLimit()) && !empty($reviews)) { |
|
| 107 | - return count($reviews); |
|
| 105 | + $this->setArgs( $args ); |
|
| 106 | + if( empty($this->sqlLimit()) && !empty($reviews) ) { |
|
| 107 | + return count( $reviews ); |
|
| 108 | 108 | } |
| 109 | - return (int) $this->db->get_var(" |
|
| 109 | + return (int)$this->db->get_var( " |
|
| 110 | 110 | SELECT COUNT(*) |
| 111 | 111 | {$this->sqlFrom()} |
| 112 | 112 | {$this->sqlJoin()} |
| 113 | 113 | {$this->sqlWhere()} |
| 114 | - "); |
|
| 114 | + " ); |
|
| 115 | 115 | } |
| 116 | 116 | |
| 117 | 117 | /** |
| 118 | 118 | * @param int $postId |
| 119 | 119 | * @return bool |
| 120 | 120 | */ |
| 121 | - public function hasRevisions($postId) |
|
| 121 | + public function hasRevisions( $postId ) |
|
| 122 | 122 | { |
| 123 | - $revisions = (int) $this->db->get_var(" |
|
| 123 | + $revisions = (int)$this->db->get_var( " |
|
| 124 | 124 | SELECT COUNT(*) |
| 125 | 125 | FROM {$this->db->posts} |
| 126 | 126 | WHERE post_type = 'revision' AND post_parent = {$postId} |
| 127 | - "); |
|
| 127 | + " ); |
|
| 128 | 128 | return $revisions > 0; |
| 129 | 129 | } |
| 130 | 130 | |
| 131 | 131 | /** |
| 132 | 132 | * @return array |
| 133 | 133 | */ |
| 134 | - protected function normalizeRatings(array $ratings = []) |
|
| 134 | + protected function normalizeRatings( array $ratings = [] ) |
|
| 135 | 135 | { |
| 136 | 136 | $normalized = []; |
| 137 | - foreach ($ratings as $result) { |
|
| 137 | + foreach( $ratings as $result ) { |
|
| 138 | 138 | $type = $result['type']; |
| 139 | - if (!array_key_exists($type, $normalized)) { |
|
| 140 | - $normalized[$type] = glsr(Rating::class)->emptyArray(); |
|
| 139 | + if( !array_key_exists( $type, $normalized ) ) { |
|
| 140 | + $normalized[$type] = glsr( Rating::class )->emptyArray(); |
|
| 141 | 141 | } |
| 142 | - $normalized[$type] = Arr::set($normalized[$type], $result['rating'], $result['count']); |
|
| 142 | + $normalized[$type] = Arr::set( $normalized[$type], $result['rating'], $result['count'] ); |
|
| 143 | 143 | } |
| 144 | 144 | return $normalized; |
| 145 | 145 | } |
@@ -147,8 +147,8 @@ discard block |
||
| 147 | 147 | /** |
| 148 | 148 | * @return void |
| 149 | 149 | */ |
| 150 | - public function setArgs(array $args = []) |
|
| 150 | + public function setArgs( array $args = [] ) |
|
| 151 | 151 | { |
| 152 | - $this->args = (new NormalizeQueryArgs($args))->toArray(); |
|
| 152 | + $this->args = (new NormalizeQueryArgs( $args ))->toArray(); |
|
| 153 | 153 | } |
| 154 | 154 | } |
@@ -71,7 +71,8 @@ |
||
| 71 | 71 | $this->setArgs($args); |
| 72 | 72 | if (!empty($postIds)) { |
| 73 | 73 | $postIds = implode(',', Arr::uniqueInt($postIds)); |
| 74 | - } else { |
|
| 74 | + } |
|
| 75 | + else { |
|
| 75 | 76 | $postIds = "SELECT ids.* FROM ( |
| 76 | 77 | SELECT r.review_id |
| 77 | 78 | {$this->sqlFrom()} |