@@ -17,277 +17,277 @@ |
||
| 17 | 17 | |
| 18 | 18 | class ListTableController extends Controller |
| 19 | 19 | { |
| 20 | - /** |
|
| 21 | - * @param array $columns |
|
| 22 | - * @return array |
|
| 23 | - * @filter manage_.Application::POST_TYPE._posts_columns |
|
| 24 | - */ |
|
| 25 | - public function filterColumnsForPostType($columns) |
|
| 26 | - { |
|
| 27 | - $columns = Arr::consolidate($columns); |
|
| 28 | - $postTypeColumns = glsr()->retrieve('columns.'.glsr()->post_type, []); |
|
| 29 | - foreach ($postTypeColumns as $key => &$value) { |
|
| 30 | - if (array_key_exists($key, $columns) && empty($value)) { |
|
| 31 | - $value = $columns[$key]; |
|
| 32 | - } |
|
| 33 | - } |
|
| 34 | - return array_filter($postTypeColumns, 'strlen'); |
|
| 35 | - } |
|
| 20 | + /** |
|
| 21 | + * @param array $columns |
|
| 22 | + * @return array |
|
| 23 | + * @filter manage_.Application::POST_TYPE._posts_columns |
|
| 24 | + */ |
|
| 25 | + public function filterColumnsForPostType($columns) |
|
| 26 | + { |
|
| 27 | + $columns = Arr::consolidate($columns); |
|
| 28 | + $postTypeColumns = glsr()->retrieve('columns.'.glsr()->post_type, []); |
|
| 29 | + foreach ($postTypeColumns as $key => &$value) { |
|
| 30 | + if (array_key_exists($key, $columns) && empty($value)) { |
|
| 31 | + $value = $columns[$key]; |
|
| 32 | + } |
|
| 33 | + } |
|
| 34 | + return array_filter($postTypeColumns, 'strlen'); |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * @param string $status |
|
| 39 | - * @param WP_Post $post |
|
| 40 | - * @return string |
|
| 41 | - * @filter post_date_column_status |
|
| 42 | - */ |
|
| 43 | - public function filterDateColumnStatus($status, $post) |
|
| 44 | - { |
|
| 45 | - if (glsr()->post_type == Arr::get($post, 'post_type')) { |
|
| 46 | - $status = _x('Submitted', 'admin-text', 'site-reviews'); |
|
| 47 | - } |
|
| 48 | - return $status; |
|
| 49 | - } |
|
| 37 | + /** |
|
| 38 | + * @param string $status |
|
| 39 | + * @param WP_Post $post |
|
| 40 | + * @return string |
|
| 41 | + * @filter post_date_column_status |
|
| 42 | + */ |
|
| 43 | + public function filterDateColumnStatus($status, $post) |
|
| 44 | + { |
|
| 45 | + if (glsr()->post_type == Arr::get($post, 'post_type')) { |
|
| 46 | + $status = _x('Submitted', 'admin-text', 'site-reviews'); |
|
| 47 | + } |
|
| 48 | + return $status; |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * @param array $hidden |
|
| 53 | - * @param WP_Screen $post |
|
| 54 | - * @return array |
|
| 55 | - * @filter default_hidden_columns |
|
| 56 | - */ |
|
| 57 | - public function filterDefaultHiddenColumns($hidden, $screen) |
|
| 58 | - { |
|
| 59 | - if (Arr::get($screen, 'id') == 'edit-'.glsr()->post_type) { |
|
| 60 | - $hidden = Arr::consolidate($hidden); |
|
| 61 | - $hidden = array_unique(array_merge($hidden, [ |
|
| 62 | - 'email', 'ip_address', 'response', 'reviewer', |
|
| 63 | - ])); |
|
| 64 | - } |
|
| 65 | - return $hidden; |
|
| 66 | - } |
|
| 51 | + /** |
|
| 52 | + * @param array $hidden |
|
| 53 | + * @param WP_Screen $post |
|
| 54 | + * @return array |
|
| 55 | + * @filter default_hidden_columns |
|
| 56 | + */ |
|
| 57 | + public function filterDefaultHiddenColumns($hidden, $screen) |
|
| 58 | + { |
|
| 59 | + if (Arr::get($screen, 'id') == 'edit-'.glsr()->post_type) { |
|
| 60 | + $hidden = Arr::consolidate($hidden); |
|
| 61 | + $hidden = array_unique(array_merge($hidden, [ |
|
| 62 | + 'email', 'ip_address', 'response', 'reviewer', |
|
| 63 | + ])); |
|
| 64 | + } |
|
| 65 | + return $hidden; |
|
| 66 | + } |
|
| 67 | 67 | |
| 68 | - /** |
|
| 69 | - * @return void |
|
| 70 | - * @filter posts_clauses |
|
| 71 | - */ |
|
| 72 | - public function filterPostClauses(array $clauses, WP_Query $query) |
|
| 73 | - { |
|
| 74 | - if (!$this->hasPermission($query)) { |
|
| 75 | - return $clauses; |
|
| 76 | - } |
|
| 77 | - $table = glsr(Query::class)->table('ratings'); |
|
| 78 | - foreach ($clauses as $key => &$clause) { |
|
| 79 | - $method = Helper::buildMethodName($key, 'modifyClause'); |
|
| 80 | - if (method_exists($this, $method)) { |
|
| 81 | - $clause = call_user_func([$this, $method], $clause, $table, $query); |
|
| 82 | - } |
|
| 83 | - } |
|
| 84 | - return $clauses; |
|
| 85 | - } |
|
| 68 | + /** |
|
| 69 | + * @return void |
|
| 70 | + * @filter posts_clauses |
|
| 71 | + */ |
|
| 72 | + public function filterPostClauses(array $clauses, WP_Query $query) |
|
| 73 | + { |
|
| 74 | + if (!$this->hasPermission($query)) { |
|
| 75 | + return $clauses; |
|
| 76 | + } |
|
| 77 | + $table = glsr(Query::class)->table('ratings'); |
|
| 78 | + foreach ($clauses as $key => &$clause) { |
|
| 79 | + $method = Helper::buildMethodName($key, 'modifyClause'); |
|
| 80 | + if (method_exists($this, $method)) { |
|
| 81 | + $clause = call_user_func([$this, $method], $clause, $table, $query); |
|
| 82 | + } |
|
| 83 | + } |
|
| 84 | + return $clauses; |
|
| 85 | + } |
|
| 86 | 86 | |
| 87 | - /** |
|
| 88 | - * @param array $actions |
|
| 89 | - * @param WP_Post $post |
|
| 90 | - * @return array |
|
| 91 | - * @filter post_row_actions |
|
| 92 | - */ |
|
| 93 | - public function filterRowActions($actions, $post) |
|
| 94 | - { |
|
| 95 | - if (glsr()->post_type != Arr::get($post, 'post_type') |
|
| 96 | - || 'trash' == $post->post_status |
|
| 97 | - || !user_can(get_current_user_id(), 'edit_post', $post->ID)) { |
|
| 98 | - return $actions; |
|
| 99 | - } |
|
| 100 | - unset($actions['inline hide-if-no-js']); //Remove Quick-edit |
|
| 101 | - $rowActions = [ |
|
| 102 | - 'approve' => _x('Approve', 'admin-text', 'site-reviews'), |
|
| 103 | - 'unapprove' => _x('Unapprove', 'admin-text', 'site-reviews'), |
|
| 104 | - ]; |
|
| 105 | - $newActions = []; |
|
| 106 | - foreach ($rowActions as $key => $text) { |
|
| 107 | - $newActions[$key] = glsr(Builder::class)->a($text, [ |
|
| 108 | - 'aria-label' => esc_attr(sprintf(_x('%s this review', 'Approve the review (admin-text)', 'site-reviews'), $text)), |
|
| 109 | - 'class' => 'glsr-toggle-status', |
|
| 110 | - 'href' => wp_nonce_url( |
|
| 111 | - admin_url('post.php?post='.$post->ID.'&action='.$key.'&plugin='.glsr()->id), |
|
| 112 | - $key.'-review_'.$post->ID |
|
| 113 | - ), |
|
| 114 | - ]); |
|
| 115 | - } |
|
| 116 | - return $newActions + Arr::consolidate($actions); |
|
| 117 | - } |
|
| 87 | + /** |
|
| 88 | + * @param array $actions |
|
| 89 | + * @param WP_Post $post |
|
| 90 | + * @return array |
|
| 91 | + * @filter post_row_actions |
|
| 92 | + */ |
|
| 93 | + public function filterRowActions($actions, $post) |
|
| 94 | + { |
|
| 95 | + if (glsr()->post_type != Arr::get($post, 'post_type') |
|
| 96 | + || 'trash' == $post->post_status |
|
| 97 | + || !user_can(get_current_user_id(), 'edit_post', $post->ID)) { |
|
| 98 | + return $actions; |
|
| 99 | + } |
|
| 100 | + unset($actions['inline hide-if-no-js']); //Remove Quick-edit |
|
| 101 | + $rowActions = [ |
|
| 102 | + 'approve' => _x('Approve', 'admin-text', 'site-reviews'), |
|
| 103 | + 'unapprove' => _x('Unapprove', 'admin-text', 'site-reviews'), |
|
| 104 | + ]; |
|
| 105 | + $newActions = []; |
|
| 106 | + foreach ($rowActions as $key => $text) { |
|
| 107 | + $newActions[$key] = glsr(Builder::class)->a($text, [ |
|
| 108 | + 'aria-label' => esc_attr(sprintf(_x('%s this review', 'Approve the review (admin-text)', 'site-reviews'), $text)), |
|
| 109 | + 'class' => 'glsr-toggle-status', |
|
| 110 | + 'href' => wp_nonce_url( |
|
| 111 | + admin_url('post.php?post='.$post->ID.'&action='.$key.'&plugin='.glsr()->id), |
|
| 112 | + $key.'-review_'.$post->ID |
|
| 113 | + ), |
|
| 114 | + ]); |
|
| 115 | + } |
|
| 116 | + return $newActions + Arr::consolidate($actions); |
|
| 117 | + } |
|
| 118 | 118 | |
| 119 | - /** |
|
| 120 | - * @param array $columns |
|
| 121 | - * @return array |
|
| 122 | - * @filter manage_edit-.Application::POST_TYPE._sortable_columns |
|
| 123 | - */ |
|
| 124 | - public function filterSortableColumns($columns) |
|
| 125 | - { |
|
| 126 | - $columns = Arr::consolidate($columns); |
|
| 127 | - $postTypeColumns = glsr()->retrieve('columns.'.glsr()->post_type, []); |
|
| 128 | - unset($postTypeColumns['cb']); |
|
| 129 | - foreach ($postTypeColumns as $key => $value) { |
|
| 130 | - if (!Str::startsWith('assigned', $key) && !Str::startsWith('taxonomy', $key)) { |
|
| 131 | - $columns[$key] = $key; |
|
| 132 | - } |
|
| 133 | - } |
|
| 134 | - return $columns; |
|
| 135 | - } |
|
| 119 | + /** |
|
| 120 | + * @param array $columns |
|
| 121 | + * @return array |
|
| 122 | + * @filter manage_edit-.Application::POST_TYPE._sortable_columns |
|
| 123 | + */ |
|
| 124 | + public function filterSortableColumns($columns) |
|
| 125 | + { |
|
| 126 | + $columns = Arr::consolidate($columns); |
|
| 127 | + $postTypeColumns = glsr()->retrieve('columns.'.glsr()->post_type, []); |
|
| 128 | + unset($postTypeColumns['cb']); |
|
| 129 | + foreach ($postTypeColumns as $key => $value) { |
|
| 130 | + if (!Str::startsWith('assigned', $key) && !Str::startsWith('taxonomy', $key)) { |
|
| 131 | + $columns[$key] = $key; |
|
| 132 | + } |
|
| 133 | + } |
|
| 134 | + return $columns; |
|
| 135 | + } |
|
| 136 | 136 | |
| 137 | - /** |
|
| 138 | - * @param string $columnName |
|
| 139 | - * @param string $postType |
|
| 140 | - * @return void |
|
| 141 | - * @action bulk_edit_custom_box |
|
| 142 | - */ |
|
| 143 | - public function renderBulkEditFields($columnName, $postType) |
|
| 144 | - { |
|
| 145 | - if ('assigned_to' == $columnName && glsr()->post_type == $postType) { |
|
| 146 | - glsr()->render('partials/editor/bulk-edit-assigned-to'); |
|
| 147 | - } |
|
| 148 | - } |
|
| 137 | + /** |
|
| 138 | + * @param string $columnName |
|
| 139 | + * @param string $postType |
|
| 140 | + * @return void |
|
| 141 | + * @action bulk_edit_custom_box |
|
| 142 | + */ |
|
| 143 | + public function renderBulkEditFields($columnName, $postType) |
|
| 144 | + { |
|
| 145 | + if ('assigned_to' == $columnName && glsr()->post_type == $postType) { |
|
| 146 | + glsr()->render('partials/editor/bulk-edit-assigned-to'); |
|
| 147 | + } |
|
| 148 | + } |
|
| 149 | 149 | |
| 150 | - /** |
|
| 151 | - * @param string $postType |
|
| 152 | - * @return void |
|
| 153 | - * @action restrict_manage_posts |
|
| 154 | - */ |
|
| 155 | - public function renderColumnFilters($postType) |
|
| 156 | - { |
|
| 157 | - if (glsr()->post_type !== $postType) { |
|
| 158 | - return; |
|
| 159 | - } |
|
| 160 | - if ($filter = glsr()->runIf(ColumnFilterRating::class)) { |
|
| 161 | - echo $filter; |
|
| 162 | - } |
|
| 163 | - if ($filter = glsr()->runIf(ColumnFilterReviewType::class)) { |
|
| 164 | - echo $filter; |
|
| 165 | - } |
|
| 166 | - } |
|
| 150 | + /** |
|
| 151 | + * @param string $postType |
|
| 152 | + * @return void |
|
| 153 | + * @action restrict_manage_posts |
|
| 154 | + */ |
|
| 155 | + public function renderColumnFilters($postType) |
|
| 156 | + { |
|
| 157 | + if (glsr()->post_type !== $postType) { |
|
| 158 | + return; |
|
| 159 | + } |
|
| 160 | + if ($filter = glsr()->runIf(ColumnFilterRating::class)) { |
|
| 161 | + echo $filter; |
|
| 162 | + } |
|
| 163 | + if ($filter = glsr()->runIf(ColumnFilterReviewType::class)) { |
|
| 164 | + echo $filter; |
|
| 165 | + } |
|
| 166 | + } |
|
| 167 | 167 | |
| 168 | - /** |
|
| 169 | - * @param string $column |
|
| 170 | - * @param int $postId |
|
| 171 | - * @return void |
|
| 172 | - * @action manage_posts_custom_column |
|
| 173 | - */ |
|
| 174 | - public function renderColumnValues($column, $postId) |
|
| 175 | - { |
|
| 176 | - $review = glsr(Query::class)->review($postId); |
|
| 177 | - if (!$review->isValid()) { |
|
| 178 | - glsr(Migrate::class)->reset(); // looks like a migration is needed! |
|
| 179 | - return; |
|
| 180 | - } |
|
| 181 | - $className = Helper::buildClassName('ColumnValue'.$column, 'Controllers\ListTableColumns'); |
|
| 182 | - $value = glsr()->runIf($className, $review); |
|
| 183 | - $value = glsr()->filterString('columns/'.$column, $value, $postId); |
|
| 184 | - echo Helper::ifEmpty($value, '—'); |
|
| 185 | - } |
|
| 168 | + /** |
|
| 169 | + * @param string $column |
|
| 170 | + * @param int $postId |
|
| 171 | + * @return void |
|
| 172 | + * @action manage_posts_custom_column |
|
| 173 | + */ |
|
| 174 | + public function renderColumnValues($column, $postId) |
|
| 175 | + { |
|
| 176 | + $review = glsr(Query::class)->review($postId); |
|
| 177 | + if (!$review->isValid()) { |
|
| 178 | + glsr(Migrate::class)->reset(); // looks like a migration is needed! |
|
| 179 | + return; |
|
| 180 | + } |
|
| 181 | + $className = Helper::buildClassName('ColumnValue'.$column, 'Controllers\ListTableColumns'); |
|
| 182 | + $value = glsr()->runIf($className, $review); |
|
| 183 | + $value = glsr()->filterString('columns/'.$column, $value, $postId); |
|
| 184 | + echo Helper::ifEmpty($value, '—'); |
|
| 185 | + } |
|
| 186 | 186 | |
| 187 | - /** |
|
| 188 | - * @param int $postId |
|
| 189 | - * @return void |
|
| 190 | - * @action save_post_.Application::POST_TYPE |
|
| 191 | - */ |
|
| 192 | - public function saveBulkEditFields($postId) |
|
| 193 | - { |
|
| 194 | - if (glsr()->can('edit_posts')) { |
|
| 195 | - $review = glsr(Query::class)->review($reviewId); |
|
| 196 | - $assignedPostIds = Arr::consolidate(filter_input(INPUT_GET, 'assigned_to')); |
|
| 197 | - $assignedUserIds = Arr::consolidate(filter_input(INPUT_GET, 'user_ids')); |
|
| 198 | - glsr()->action('review/updated/post_ids', $review, $assignedPostIds); |
|
| 199 | - glsr()->action('review/updated/user_ids', $review, $assignedUserIds); |
|
| 200 | - } |
|
| 201 | - } |
|
| 187 | + /** |
|
| 188 | + * @param int $postId |
|
| 189 | + * @return void |
|
| 190 | + * @action save_post_.Application::POST_TYPE |
|
| 191 | + */ |
|
| 192 | + public function saveBulkEditFields($postId) |
|
| 193 | + { |
|
| 194 | + if (glsr()->can('edit_posts')) { |
|
| 195 | + $review = glsr(Query::class)->review($reviewId); |
|
| 196 | + $assignedPostIds = Arr::consolidate(filter_input(INPUT_GET, 'assigned_to')); |
|
| 197 | + $assignedUserIds = Arr::consolidate(filter_input(INPUT_GET, 'user_ids')); |
|
| 198 | + glsr()->action('review/updated/post_ids', $review, $assignedPostIds); |
|
| 199 | + glsr()->action('review/updated/user_ids', $review, $assignedUserIds); |
|
| 200 | + } |
|
| 201 | + } |
|
| 202 | 202 | |
| 203 | - /** |
|
| 204 | - * @return void |
|
| 205 | - * @action pre_get_posts |
|
| 206 | - */ |
|
| 207 | - public function setQueryForColumn(WP_Query $query) |
|
| 208 | - { |
|
| 209 | - if (!$this->hasPermission($query)) { |
|
| 210 | - return; |
|
| 211 | - } |
|
| 212 | - $orderby = $query->get('orderby'); |
|
| 213 | - if ('response' === $orderby) { |
|
| 214 | - $query->set('meta_key', Str::prefix('_', $orderby)); |
|
| 215 | - $query->set('orderby', 'meta_value'); |
|
| 216 | - } |
|
| 217 | - } |
|
| 203 | + /** |
|
| 204 | + * @return void |
|
| 205 | + * @action pre_get_posts |
|
| 206 | + */ |
|
| 207 | + public function setQueryForColumn(WP_Query $query) |
|
| 208 | + { |
|
| 209 | + if (!$this->hasPermission($query)) { |
|
| 210 | + return; |
|
| 211 | + } |
|
| 212 | + $orderby = $query->get('orderby'); |
|
| 213 | + if ('response' === $orderby) { |
|
| 214 | + $query->set('meta_key', Str::prefix('_', $orderby)); |
|
| 215 | + $query->set('orderby', 'meta_value'); |
|
| 216 | + } |
|
| 217 | + } |
|
| 218 | 218 | |
| 219 | - /** |
|
| 220 | - * Check if the translation string can be modified. |
|
| 221 | - * @param string $domain |
|
| 222 | - * @return bool |
|
| 223 | - */ |
|
| 224 | - protected function canModifyTranslation($domain = 'default') |
|
| 225 | - { |
|
| 226 | - $screen = glsr_current_screen(); |
|
| 227 | - return 'default' == $domain |
|
| 228 | - && 'edit' == $screen->base |
|
| 229 | - && glsr()->post_type == $screen->post_type; |
|
| 230 | - } |
|
| 219 | + /** |
|
| 220 | + * Check if the translation string can be modified. |
|
| 221 | + * @param string $domain |
|
| 222 | + * @return bool |
|
| 223 | + */ |
|
| 224 | + protected function canModifyTranslation($domain = 'default') |
|
| 225 | + { |
|
| 226 | + $screen = glsr_current_screen(); |
|
| 227 | + return 'default' == $domain |
|
| 228 | + && 'edit' == $screen->base |
|
| 229 | + && glsr()->post_type == $screen->post_type; |
|
| 230 | + } |
|
| 231 | 231 | |
| 232 | - /** |
|
| 233 | - * @return bool |
|
| 234 | - */ |
|
| 235 | - protected function hasPermission(WP_Query $query) |
|
| 236 | - { |
|
| 237 | - global $pagenow; |
|
| 238 | - return is_admin() |
|
| 239 | - && $query->is_main_query() |
|
| 240 | - && glsr()->post_type == $query->get('post_type') |
|
| 241 | - && 'edit.php' == $pagenow; |
|
| 242 | - } |
|
| 232 | + /** |
|
| 233 | + * @return bool |
|
| 234 | + */ |
|
| 235 | + protected function hasPermission(WP_Query $query) |
|
| 236 | + { |
|
| 237 | + global $pagenow; |
|
| 238 | + return is_admin() |
|
| 239 | + && $query->is_main_query() |
|
| 240 | + && glsr()->post_type == $query->get('post_type') |
|
| 241 | + && 'edit.php' == $pagenow; |
|
| 242 | + } |
|
| 243 | 243 | |
| 244 | - /** |
|
| 245 | - * @param string $join |
|
| 246 | - * @return string |
|
| 247 | - */ |
|
| 248 | - protected function modifyClauseJoin($join, $table, WP_Query $query) |
|
| 249 | - { |
|
| 250 | - global $wpdb; |
|
| 251 | - $join .= " INNER JOIN {$table} ON {$table}.review_id = {$wpdb->posts}.ID "; |
|
| 252 | - return $join; |
|
| 253 | - } |
|
| 244 | + /** |
|
| 245 | + * @param string $join |
|
| 246 | + * @return string |
|
| 247 | + */ |
|
| 248 | + protected function modifyClauseJoin($join, $table, WP_Query $query) |
|
| 249 | + { |
|
| 250 | + global $wpdb; |
|
| 251 | + $join .= " INNER JOIN {$table} ON {$table}.review_id = {$wpdb->posts}.ID "; |
|
| 252 | + return $join; |
|
| 253 | + } |
|
| 254 | 254 | |
| 255 | - /** |
|
| 256 | - * @param string $orderby |
|
| 257 | - * @return string |
|
| 258 | - */ |
|
| 259 | - protected function modifyClauseOrderby($orderby, $table, WP_Query $query) |
|
| 260 | - { |
|
| 261 | - $order = $query->get('order'); |
|
| 262 | - $orderby = $query->get('orderby'); |
|
| 263 | - $columns = [ |
|
| 264 | - 'email' => 'email', |
|
| 265 | - 'ip_address' => 'ip_address', |
|
| 266 | - 'pinned' => 'is_pinned', |
|
| 267 | - 'rating' => 'rating', |
|
| 268 | - 'review_type' => 'type', |
|
| 269 | - 'reviewer' => 'name', |
|
| 270 | - ]; |
|
| 271 | - if (array_key_exists($orderby, $columns)) { |
|
| 272 | - $column = "{$table}.{$columns[$orderby]}"; |
|
| 273 | - $orderby = "NULLIF({$column}, '') IS NULL, {$column} {$order}"; |
|
| 274 | - } |
|
| 275 | - return $orderby; |
|
| 276 | - } |
|
| 255 | + /** |
|
| 256 | + * @param string $orderby |
|
| 257 | + * @return string |
|
| 258 | + */ |
|
| 259 | + protected function modifyClauseOrderby($orderby, $table, WP_Query $query) |
|
| 260 | + { |
|
| 261 | + $order = $query->get('order'); |
|
| 262 | + $orderby = $query->get('orderby'); |
|
| 263 | + $columns = [ |
|
| 264 | + 'email' => 'email', |
|
| 265 | + 'ip_address' => 'ip_address', |
|
| 266 | + 'pinned' => 'is_pinned', |
|
| 267 | + 'rating' => 'rating', |
|
| 268 | + 'review_type' => 'type', |
|
| 269 | + 'reviewer' => 'name', |
|
| 270 | + ]; |
|
| 271 | + if (array_key_exists($orderby, $columns)) { |
|
| 272 | + $column = "{$table}.{$columns[$orderby]}"; |
|
| 273 | + $orderby = "NULLIF({$column}, '') IS NULL, {$column} {$order}"; |
|
| 274 | + } |
|
| 275 | + return $orderby; |
|
| 276 | + } |
|
| 277 | 277 | |
| 278 | - /** |
|
| 279 | - * @param string $where |
|
| 280 | - * @return string |
|
| 281 | - */ |
|
| 282 | - protected function modifyClauseWhere($where, $table, WP_Query $query) |
|
| 283 | - { |
|
| 284 | - $filters = Arr::removeEmptyValues([ |
|
| 285 | - 'rating' => filter_input(INPUT_GET, 'rating'), |
|
| 286 | - 'type' => filter_input(INPUT_GET, 'review_type'), |
|
| 287 | - ]); |
|
| 288 | - foreach ($filters as $key => $value) { |
|
| 289 | - $where .= " (AND {$table}.{$key} = '{$value}') "; |
|
| 290 | - } |
|
| 291 | - return $where; |
|
| 292 | - } |
|
| 278 | + /** |
|
| 279 | + * @param string $where |
|
| 280 | + * @return string |
|
| 281 | + */ |
|
| 282 | + protected function modifyClauseWhere($where, $table, WP_Query $query) |
|
| 283 | + { |
|
| 284 | + $filters = Arr::removeEmptyValues([ |
|
| 285 | + 'rating' => filter_input(INPUT_GET, 'rating'), |
|
| 286 | + 'type' => filter_input(INPUT_GET, 'review_type'), |
|
| 287 | + ]); |
|
| 288 | + foreach ($filters as $key => $value) { |
|
| 289 | + $where .= " (AND {$table}.{$key} = '{$value}') "; |
|
| 290 | + } |
|
| 291 | + return $where; |
|
| 292 | + } |
|
| 293 | 293 | } |
@@ -22,16 +22,16 @@ discard block |
||
| 22 | 22 | * @return array |
| 23 | 23 | * @filter manage_.Application::POST_TYPE._posts_columns |
| 24 | 24 | */ |
| 25 | - public function filterColumnsForPostType($columns) |
|
| 25 | + public function filterColumnsForPostType( $columns ) |
|
| 26 | 26 | { |
| 27 | - $columns = Arr::consolidate($columns); |
|
| 28 | - $postTypeColumns = glsr()->retrieve('columns.'.glsr()->post_type, []); |
|
| 29 | - foreach ($postTypeColumns as $key => &$value) { |
|
| 30 | - if (array_key_exists($key, $columns) && empty($value)) { |
|
| 27 | + $columns = Arr::consolidate( $columns ); |
|
| 28 | + $postTypeColumns = glsr()->retrieve( 'columns.'.glsr()->post_type, [] ); |
|
| 29 | + foreach( $postTypeColumns as $key => &$value ) { |
|
| 30 | + if( array_key_exists( $key, $columns ) && empty($value) ) { |
|
| 31 | 31 | $value = $columns[$key]; |
| 32 | 32 | } |
| 33 | 33 | } |
| 34 | - return array_filter($postTypeColumns, 'strlen'); |
|
| 34 | + return array_filter( $postTypeColumns, 'strlen' ); |
|
| 35 | 35 | } |
| 36 | 36 | |
| 37 | 37 | /** |
@@ -40,10 +40,10 @@ discard block |
||
| 40 | 40 | * @return string |
| 41 | 41 | * @filter post_date_column_status |
| 42 | 42 | */ |
| 43 | - public function filterDateColumnStatus($status, $post) |
|
| 43 | + public function filterDateColumnStatus( $status, $post ) |
|
| 44 | 44 | { |
| 45 | - if (glsr()->post_type == Arr::get($post, 'post_type')) { |
|
| 46 | - $status = _x('Submitted', 'admin-text', 'site-reviews'); |
|
| 45 | + if( glsr()->post_type == Arr::get( $post, 'post_type' ) ) { |
|
| 46 | + $status = _x( 'Submitted', 'admin-text', 'site-reviews' ); |
|
| 47 | 47 | } |
| 48 | 48 | return $status; |
| 49 | 49 | } |
@@ -54,13 +54,13 @@ discard block |
||
| 54 | 54 | * @return array |
| 55 | 55 | * @filter default_hidden_columns |
| 56 | 56 | */ |
| 57 | - public function filterDefaultHiddenColumns($hidden, $screen) |
|
| 57 | + public function filterDefaultHiddenColumns( $hidden, $screen ) |
|
| 58 | 58 | { |
| 59 | - if (Arr::get($screen, 'id') == 'edit-'.glsr()->post_type) { |
|
| 60 | - $hidden = Arr::consolidate($hidden); |
|
| 61 | - $hidden = array_unique(array_merge($hidden, [ |
|
| 59 | + if( Arr::get( $screen, 'id' ) == 'edit-'.glsr()->post_type ) { |
|
| 60 | + $hidden = Arr::consolidate( $hidden ); |
|
| 61 | + $hidden = array_unique( array_merge( $hidden, [ |
|
| 62 | 62 | 'email', 'ip_address', 'response', 'reviewer', |
| 63 | - ])); |
|
| 63 | + ] ) ); |
|
| 64 | 64 | } |
| 65 | 65 | return $hidden; |
| 66 | 66 | } |
@@ -69,16 +69,16 @@ discard block |
||
| 69 | 69 | * @return void |
| 70 | 70 | * @filter posts_clauses |
| 71 | 71 | */ |
| 72 | - public function filterPostClauses(array $clauses, WP_Query $query) |
|
| 72 | + public function filterPostClauses( array $clauses, WP_Query $query ) |
|
| 73 | 73 | { |
| 74 | - if (!$this->hasPermission($query)) { |
|
| 74 | + if( !$this->hasPermission( $query ) ) { |
|
| 75 | 75 | return $clauses; |
| 76 | 76 | } |
| 77 | - $table = glsr(Query::class)->table('ratings'); |
|
| 78 | - foreach ($clauses as $key => &$clause) { |
|
| 79 | - $method = Helper::buildMethodName($key, 'modifyClause'); |
|
| 80 | - if (method_exists($this, $method)) { |
|
| 81 | - $clause = call_user_func([$this, $method], $clause, $table, $query); |
|
| 77 | + $table = glsr( Query::class )->table( 'ratings' ); |
|
| 78 | + foreach( $clauses as $key => &$clause ) { |
|
| 79 | + $method = Helper::buildMethodName( $key, 'modifyClause' ); |
|
| 80 | + if( method_exists( $this, $method ) ) { |
|
| 81 | + $clause = call_user_func( [$this, $method], $clause, $table, $query ); |
|
| 82 | 82 | } |
| 83 | 83 | } |
| 84 | 84 | return $clauses; |
@@ -90,30 +90,30 @@ discard block |
||
| 90 | 90 | * @return array |
| 91 | 91 | * @filter post_row_actions |
| 92 | 92 | */ |
| 93 | - public function filterRowActions($actions, $post) |
|
| 93 | + public function filterRowActions( $actions, $post ) |
|
| 94 | 94 | { |
| 95 | - if (glsr()->post_type != Arr::get($post, 'post_type') |
|
| 95 | + if( glsr()->post_type != Arr::get( $post, 'post_type' ) |
|
| 96 | 96 | || 'trash' == $post->post_status |
| 97 | - || !user_can(get_current_user_id(), 'edit_post', $post->ID)) { |
|
| 97 | + || !user_can( get_current_user_id(), 'edit_post', $post->ID ) ) { |
|
| 98 | 98 | return $actions; |
| 99 | 99 | } |
| 100 | 100 | unset($actions['inline hide-if-no-js']); //Remove Quick-edit |
| 101 | 101 | $rowActions = [ |
| 102 | - 'approve' => _x('Approve', 'admin-text', 'site-reviews'), |
|
| 103 | - 'unapprove' => _x('Unapprove', 'admin-text', 'site-reviews'), |
|
| 102 | + 'approve' => _x( 'Approve', 'admin-text', 'site-reviews' ), |
|
| 103 | + 'unapprove' => _x( 'Unapprove', 'admin-text', 'site-reviews' ), |
|
| 104 | 104 | ]; |
| 105 | 105 | $newActions = []; |
| 106 | - foreach ($rowActions as $key => $text) { |
|
| 107 | - $newActions[$key] = glsr(Builder::class)->a($text, [ |
|
| 108 | - 'aria-label' => esc_attr(sprintf(_x('%s this review', 'Approve the review (admin-text)', 'site-reviews'), $text)), |
|
| 106 | + foreach( $rowActions as $key => $text ) { |
|
| 107 | + $newActions[$key] = glsr( Builder::class )->a( $text, [ |
|
| 108 | + 'aria-label' => esc_attr( sprintf( _x( '%s this review', 'Approve the review (admin-text)', 'site-reviews' ), $text ) ), |
|
| 109 | 109 | 'class' => 'glsr-toggle-status', |
| 110 | 110 | 'href' => wp_nonce_url( |
| 111 | - admin_url('post.php?post='.$post->ID.'&action='.$key.'&plugin='.glsr()->id), |
|
| 111 | + admin_url( 'post.php?post='.$post->ID.'&action='.$key.'&plugin='.glsr()->id ), |
|
| 112 | 112 | $key.'-review_'.$post->ID |
| 113 | 113 | ), |
| 114 | - ]); |
|
| 114 | + ] ); |
|
| 115 | 115 | } |
| 116 | - return $newActions + Arr::consolidate($actions); |
|
| 116 | + return $newActions + Arr::consolidate( $actions ); |
|
| 117 | 117 | } |
| 118 | 118 | |
| 119 | 119 | /** |
@@ -121,13 +121,13 @@ discard block |
||
| 121 | 121 | * @return array |
| 122 | 122 | * @filter manage_edit-.Application::POST_TYPE._sortable_columns |
| 123 | 123 | */ |
| 124 | - public function filterSortableColumns($columns) |
|
| 124 | + public function filterSortableColumns( $columns ) |
|
| 125 | 125 | { |
| 126 | - $columns = Arr::consolidate($columns); |
|
| 127 | - $postTypeColumns = glsr()->retrieve('columns.'.glsr()->post_type, []); |
|
| 126 | + $columns = Arr::consolidate( $columns ); |
|
| 127 | + $postTypeColumns = glsr()->retrieve( 'columns.'.glsr()->post_type, [] ); |
|
| 128 | 128 | unset($postTypeColumns['cb']); |
| 129 | - foreach ($postTypeColumns as $key => $value) { |
|
| 130 | - if (!Str::startsWith('assigned', $key) && !Str::startsWith('taxonomy', $key)) { |
|
| 129 | + foreach( $postTypeColumns as $key => $value ) { |
|
| 130 | + if( !Str::startsWith( 'assigned', $key ) && !Str::startsWith( 'taxonomy', $key ) ) { |
|
| 131 | 131 | $columns[$key] = $key; |
| 132 | 132 | } |
| 133 | 133 | } |
@@ -140,10 +140,10 @@ discard block |
||
| 140 | 140 | * @return void |
| 141 | 141 | * @action bulk_edit_custom_box |
| 142 | 142 | */ |
| 143 | - public function renderBulkEditFields($columnName, $postType) |
|
| 143 | + public function renderBulkEditFields( $columnName, $postType ) |
|
| 144 | 144 | { |
| 145 | - if ('assigned_to' == $columnName && glsr()->post_type == $postType) { |
|
| 146 | - glsr()->render('partials/editor/bulk-edit-assigned-to'); |
|
| 145 | + if( 'assigned_to' == $columnName && glsr()->post_type == $postType ) { |
|
| 146 | + glsr()->render( 'partials/editor/bulk-edit-assigned-to' ); |
|
| 147 | 147 | } |
| 148 | 148 | } |
| 149 | 149 | |
@@ -152,15 +152,15 @@ discard block |
||
| 152 | 152 | * @return void |
| 153 | 153 | * @action restrict_manage_posts |
| 154 | 154 | */ |
| 155 | - public function renderColumnFilters($postType) |
|
| 155 | + public function renderColumnFilters( $postType ) |
|
| 156 | 156 | { |
| 157 | - if (glsr()->post_type !== $postType) { |
|
| 157 | + if( glsr()->post_type !== $postType ) { |
|
| 158 | 158 | return; |
| 159 | 159 | } |
| 160 | - if ($filter = glsr()->runIf(ColumnFilterRating::class)) { |
|
| 160 | + if( $filter = glsr()->runIf( ColumnFilterRating::class ) ) { |
|
| 161 | 161 | echo $filter; |
| 162 | 162 | } |
| 163 | - if ($filter = glsr()->runIf(ColumnFilterReviewType::class)) { |
|
| 163 | + if( $filter = glsr()->runIf( ColumnFilterReviewType::class ) ) { |
|
| 164 | 164 | echo $filter; |
| 165 | 165 | } |
| 166 | 166 | } |
@@ -171,17 +171,17 @@ discard block |
||
| 171 | 171 | * @return void |
| 172 | 172 | * @action manage_posts_custom_column |
| 173 | 173 | */ |
| 174 | - public function renderColumnValues($column, $postId) |
|
| 174 | + public function renderColumnValues( $column, $postId ) |
|
| 175 | 175 | { |
| 176 | - $review = glsr(Query::class)->review($postId); |
|
| 177 | - if (!$review->isValid()) { |
|
| 178 | - glsr(Migrate::class)->reset(); // looks like a migration is needed! |
|
| 176 | + $review = glsr( Query::class )->review( $postId ); |
|
| 177 | + if( !$review->isValid() ) { |
|
| 178 | + glsr( Migrate::class )->reset(); // looks like a migration is needed! |
|
| 179 | 179 | return; |
| 180 | 180 | } |
| 181 | - $className = Helper::buildClassName('ColumnValue'.$column, 'Controllers\ListTableColumns'); |
|
| 182 | - $value = glsr()->runIf($className, $review); |
|
| 183 | - $value = glsr()->filterString('columns/'.$column, $value, $postId); |
|
| 184 | - echo Helper::ifEmpty($value, '—'); |
|
| 181 | + $className = Helper::buildClassName( 'ColumnValue'.$column, 'Controllers\ListTableColumns' ); |
|
| 182 | + $value = glsr()->runIf( $className, $review ); |
|
| 183 | + $value = glsr()->filterString( 'columns/'.$column, $value, $postId ); |
|
| 184 | + echo Helper::ifEmpty( $value, '—' ); |
|
| 185 | 185 | } |
| 186 | 186 | |
| 187 | 187 | /** |
@@ -189,14 +189,14 @@ discard block |
||
| 189 | 189 | * @return void |
| 190 | 190 | * @action save_post_.Application::POST_TYPE |
| 191 | 191 | */ |
| 192 | - public function saveBulkEditFields($postId) |
|
| 192 | + public function saveBulkEditFields( $postId ) |
|
| 193 | 193 | { |
| 194 | - if (glsr()->can('edit_posts')) { |
|
| 195 | - $review = glsr(Query::class)->review($reviewId); |
|
| 196 | - $assignedPostIds = Arr::consolidate(filter_input(INPUT_GET, 'assigned_to')); |
|
| 197 | - $assignedUserIds = Arr::consolidate(filter_input(INPUT_GET, 'user_ids')); |
|
| 198 | - glsr()->action('review/updated/post_ids', $review, $assignedPostIds); |
|
| 199 | - glsr()->action('review/updated/user_ids', $review, $assignedUserIds); |
|
| 194 | + if( glsr()->can( 'edit_posts' ) ) { |
|
| 195 | + $review = glsr( Query::class )->review( $reviewId ); |
|
| 196 | + $assignedPostIds = Arr::consolidate( filter_input( INPUT_GET, 'assigned_to' ) ); |
|
| 197 | + $assignedUserIds = Arr::consolidate( filter_input( INPUT_GET, 'user_ids' ) ); |
|
| 198 | + glsr()->action( 'review/updated/post_ids', $review, $assignedPostIds ); |
|
| 199 | + glsr()->action( 'review/updated/user_ids', $review, $assignedUserIds ); |
|
| 200 | 200 | } |
| 201 | 201 | } |
| 202 | 202 | |
@@ -204,15 +204,15 @@ discard block |
||
| 204 | 204 | * @return void |
| 205 | 205 | * @action pre_get_posts |
| 206 | 206 | */ |
| 207 | - public function setQueryForColumn(WP_Query $query) |
|
| 207 | + public function setQueryForColumn( WP_Query $query ) |
|
| 208 | 208 | { |
| 209 | - if (!$this->hasPermission($query)) { |
|
| 209 | + if( !$this->hasPermission( $query ) ) { |
|
| 210 | 210 | return; |
| 211 | 211 | } |
| 212 | - $orderby = $query->get('orderby'); |
|
| 213 | - if ('response' === $orderby) { |
|
| 214 | - $query->set('meta_key', Str::prefix('_', $orderby)); |
|
| 215 | - $query->set('orderby', 'meta_value'); |
|
| 212 | + $orderby = $query->get( 'orderby' ); |
|
| 213 | + if( 'response' === $orderby ) { |
|
| 214 | + $query->set( 'meta_key', Str::prefix( '_', $orderby ) ); |
|
| 215 | + $query->set( 'orderby', 'meta_value' ); |
|
| 216 | 216 | } |
| 217 | 217 | } |
| 218 | 218 | |
@@ -221,7 +221,7 @@ discard block |
||
| 221 | 221 | * @param string $domain |
| 222 | 222 | * @return bool |
| 223 | 223 | */ |
| 224 | - protected function canModifyTranslation($domain = 'default') |
|
| 224 | + protected function canModifyTranslation( $domain = 'default' ) |
|
| 225 | 225 | { |
| 226 | 226 | $screen = glsr_current_screen(); |
| 227 | 227 | return 'default' == $domain |
@@ -232,12 +232,12 @@ discard block |
||
| 232 | 232 | /** |
| 233 | 233 | * @return bool |
| 234 | 234 | */ |
| 235 | - protected function hasPermission(WP_Query $query) |
|
| 235 | + protected function hasPermission( WP_Query $query ) |
|
| 236 | 236 | { |
| 237 | 237 | global $pagenow; |
| 238 | 238 | return is_admin() |
| 239 | 239 | && $query->is_main_query() |
| 240 | - && glsr()->post_type == $query->get('post_type') |
|
| 240 | + && glsr()->post_type == $query->get( 'post_type' ) |
|
| 241 | 241 | && 'edit.php' == $pagenow; |
| 242 | 242 | } |
| 243 | 243 | |
@@ -245,7 +245,7 @@ discard block |
||
| 245 | 245 | * @param string $join |
| 246 | 246 | * @return string |
| 247 | 247 | */ |
| 248 | - protected function modifyClauseJoin($join, $table, WP_Query $query) |
|
| 248 | + protected function modifyClauseJoin( $join, $table, WP_Query $query ) |
|
| 249 | 249 | { |
| 250 | 250 | global $wpdb; |
| 251 | 251 | $join .= " INNER JOIN {$table} ON {$table}.review_id = {$wpdb->posts}.ID "; |
@@ -256,10 +256,10 @@ discard block |
||
| 256 | 256 | * @param string $orderby |
| 257 | 257 | * @return string |
| 258 | 258 | */ |
| 259 | - protected function modifyClauseOrderby($orderby, $table, WP_Query $query) |
|
| 259 | + protected function modifyClauseOrderby( $orderby, $table, WP_Query $query ) |
|
| 260 | 260 | { |
| 261 | - $order = $query->get('order'); |
|
| 262 | - $orderby = $query->get('orderby'); |
|
| 261 | + $order = $query->get( 'order' ); |
|
| 262 | + $orderby = $query->get( 'orderby' ); |
|
| 263 | 263 | $columns = [ |
| 264 | 264 | 'email' => 'email', |
| 265 | 265 | 'ip_address' => 'ip_address', |
@@ -268,7 +268,7 @@ discard block |
||
| 268 | 268 | 'review_type' => 'type', |
| 269 | 269 | 'reviewer' => 'name', |
| 270 | 270 | ]; |
| 271 | - if (array_key_exists($orderby, $columns)) { |
|
| 271 | + if( array_key_exists( $orderby, $columns ) ) { |
|
| 272 | 272 | $column = "{$table}.{$columns[$orderby]}"; |
| 273 | 273 | $orderby = "NULLIF({$column}, '') IS NULL, {$column} {$order}"; |
| 274 | 274 | } |
@@ -279,13 +279,13 @@ discard block |
||
| 279 | 279 | * @param string $where |
| 280 | 280 | * @return string |
| 281 | 281 | */ |
| 282 | - protected function modifyClauseWhere($where, $table, WP_Query $query) |
|
| 282 | + protected function modifyClauseWhere( $where, $table, WP_Query $query ) |
|
| 283 | 283 | { |
| 284 | - $filters = Arr::removeEmptyValues([ |
|
| 285 | - 'rating' => filter_input(INPUT_GET, 'rating'), |
|
| 286 | - 'type' => filter_input(INPUT_GET, 'review_type'), |
|
| 287 | - ]); |
|
| 288 | - foreach ($filters as $key => $value) { |
|
| 284 | + $filters = Arr::removeEmptyValues( [ |
|
| 285 | + 'rating' => filter_input( INPUT_GET, 'rating' ), |
|
| 286 | + 'type' => filter_input( INPUT_GET, 'review_type' ), |
|
| 287 | + ] ); |
|
| 288 | + foreach( $filters as $key => $value ) { |
|
| 289 | 289 | $where .= " (AND {$table}.{$key} = '{$value}') "; |
| 290 | 290 | } |
| 291 | 291 | return $where; |
@@ -6,44 +6,44 @@ |
||
| 6 | 6 | |
| 7 | 7 | abstract class Controller |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * @return void |
|
| 11 | - */ |
|
| 12 | - public function download($filename, $content) |
|
| 13 | - { |
|
| 14 | - if (glsr()->can('edit_others_posts')) { |
|
| 15 | - nocache_headers(); |
|
| 16 | - header('Content-Type: text/plain'); |
|
| 17 | - header('Content-Disposition: attachment; filename="'.$filename.'"'); |
|
| 18 | - echo html_entity_decode($content); |
|
| 19 | - exit; |
|
| 20 | - } |
|
| 21 | - } |
|
| 9 | + /** |
|
| 10 | + * @return void |
|
| 11 | + */ |
|
| 12 | + public function download($filename, $content) |
|
| 13 | + { |
|
| 14 | + if (glsr()->can('edit_others_posts')) { |
|
| 15 | + nocache_headers(); |
|
| 16 | + header('Content-Type: text/plain'); |
|
| 17 | + header('Content-Disposition: attachment; filename="'.$filename.'"'); |
|
| 18 | + echo html_entity_decode($content); |
|
| 19 | + exit; |
|
| 20 | + } |
|
| 21 | + } |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * @return mixed |
|
| 25 | - */ |
|
| 26 | - public function execute(CommandContract $command) |
|
| 27 | - { |
|
| 28 | - if (method_exists($command, 'handle')) { |
|
| 29 | - return $command->handle(); |
|
| 30 | - } |
|
| 31 | - } |
|
| 23 | + /** |
|
| 24 | + * @return mixed |
|
| 25 | + */ |
|
| 26 | + public function execute(CommandContract $command) |
|
| 27 | + { |
|
| 28 | + if (method_exists($command, 'handle')) { |
|
| 29 | + return $command->handle(); |
|
| 30 | + } |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * @return int |
|
| 35 | - */ |
|
| 36 | - protected function getPostId() |
|
| 37 | - { |
|
| 38 | - return intval(filter_input(INPUT_GET, 'post')); |
|
| 39 | - } |
|
| 33 | + /** |
|
| 34 | + * @return int |
|
| 35 | + */ |
|
| 36 | + protected function getPostId() |
|
| 37 | + { |
|
| 38 | + return intval(filter_input(INPUT_GET, 'post')); |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * @return bool |
|
| 43 | - */ |
|
| 44 | - protected function isReviewAdminPage() |
|
| 45 | - { |
|
| 46 | - return is_admin() |
|
| 47 | - && in_array(glsr()->post_type, [get_post_type(), filter_input(INPUT_GET, 'post_type')]); |
|
| 48 | - } |
|
| 41 | + /** |
|
| 42 | + * @return bool |
|
| 43 | + */ |
|
| 44 | + protected function isReviewAdminPage() |
|
| 45 | + { |
|
| 46 | + return is_admin() |
|
| 47 | + && in_array(glsr()->post_type, [get_post_type(), filter_input(INPUT_GET, 'post_type')]); |
|
| 48 | + } |
|
| 49 | 49 | } |
@@ -9,13 +9,13 @@ discard block |
||
| 9 | 9 | /** |
| 10 | 10 | * @return void |
| 11 | 11 | */ |
| 12 | - public function download($filename, $content) |
|
| 12 | + public function download( $filename, $content ) |
|
| 13 | 13 | { |
| 14 | - if (glsr()->can('edit_others_posts')) { |
|
| 14 | + if( glsr()->can( 'edit_others_posts' ) ) { |
|
| 15 | 15 | nocache_headers(); |
| 16 | - header('Content-Type: text/plain'); |
|
| 17 | - header('Content-Disposition: attachment; filename="'.$filename.'"'); |
|
| 18 | - echo html_entity_decode($content); |
|
| 16 | + header( 'Content-Type: text/plain' ); |
|
| 17 | + header( 'Content-Disposition: attachment; filename="'.$filename.'"' ); |
|
| 18 | + echo html_entity_decode( $content ); |
|
| 19 | 19 | exit; |
| 20 | 20 | } |
| 21 | 21 | } |
@@ -23,9 +23,9 @@ discard block |
||
| 23 | 23 | /** |
| 24 | 24 | * @return mixed |
| 25 | 25 | */ |
| 26 | - public function execute(CommandContract $command) |
|
| 26 | + public function execute( CommandContract $command ) |
|
| 27 | 27 | { |
| 28 | - if (method_exists($command, 'handle')) { |
|
| 28 | + if( method_exists( $command, 'handle' ) ) { |
|
| 29 | 29 | return $command->handle(); |
| 30 | 30 | } |
| 31 | 31 | } |
@@ -35,7 +35,7 @@ discard block |
||
| 35 | 35 | */ |
| 36 | 36 | protected function getPostId() |
| 37 | 37 | { |
| 38 | - return intval(filter_input(INPUT_GET, 'post')); |
|
| 38 | + return intval( filter_input( INPUT_GET, 'post' ) ); |
|
| 39 | 39 | } |
| 40 | 40 | |
| 41 | 41 | /** |
@@ -44,6 +44,6 @@ discard block |
||
| 44 | 44 | protected function isReviewAdminPage() |
| 45 | 45 | { |
| 46 | 46 | return is_admin() |
| 47 | - && in_array(glsr()->post_type, [get_post_type(), filter_input(INPUT_GET, 'post_type')]); |
|
| 47 | + && in_array( glsr()->post_type, [get_post_type(), filter_input( INPUT_GET, 'post_type' )] ); |
|
| 48 | 48 | } |
| 49 | 49 | } |
@@ -8,99 +8,99 @@ |
||
| 8 | 8 | |
| 9 | 9 | class BlocksController extends Controller |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * @param array $blockTypes |
|
| 13 | - * @param \WP_Post $post |
|
| 14 | - * @return array |
|
| 15 | - * @filter allowed_block_types |
|
| 16 | - */ |
|
| 17 | - public function filterAllowedBlockTypes($blockTypes, $post) |
|
| 18 | - { |
|
| 19 | - return glsr()->post_type !== get_post_type($post) |
|
| 20 | - ? $blockTypes |
|
| 21 | - : []; |
|
| 22 | - } |
|
| 11 | + /** |
|
| 12 | + * @param array $blockTypes |
|
| 13 | + * @param \WP_Post $post |
|
| 14 | + * @return array |
|
| 15 | + * @filter allowed_block_types |
|
| 16 | + */ |
|
| 17 | + public function filterAllowedBlockTypes($blockTypes, $post) |
|
| 18 | + { |
|
| 19 | + return glsr()->post_type !== get_post_type($post) |
|
| 20 | + ? $blockTypes |
|
| 21 | + : []; |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * @param array $categories |
|
| 26 | - * @return array |
|
| 27 | - * @filter block_categories |
|
| 28 | - */ |
|
| 29 | - public function filterBlockCategories($categories) |
|
| 30 | - { |
|
| 31 | - $categories = Arr::consolidate($categories); |
|
| 32 | - $categories[] = [ |
|
| 33 | - 'icon' => null, |
|
| 34 | - 'slug' => Application::ID, |
|
| 35 | - 'title' => glsr()->name, |
|
| 36 | - ]; |
|
| 37 | - return $categories; |
|
| 38 | - } |
|
| 24 | + /** |
|
| 25 | + * @param array $categories |
|
| 26 | + * @return array |
|
| 27 | + * @filter block_categories |
|
| 28 | + */ |
|
| 29 | + public function filterBlockCategories($categories) |
|
| 30 | + { |
|
| 31 | + $categories = Arr::consolidate($categories); |
|
| 32 | + $categories[] = [ |
|
| 33 | + 'icon' => null, |
|
| 34 | + 'slug' => Application::ID, |
|
| 35 | + 'title' => glsr()->name, |
|
| 36 | + ]; |
|
| 37 | + return $categories; |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * @param array $editors |
|
| 42 | - * @param string $postType |
|
| 43 | - * @return array |
|
| 44 | - * @filter classic_editor_enabled_editors_for_post_type |
|
| 45 | - * @plugin classic-editor/classic-editor.php |
|
| 46 | - */ |
|
| 47 | - public function filterEnabledEditors($editors, $postType) |
|
| 48 | - { |
|
| 49 | - return Application::POST_TYPE == $postType |
|
| 50 | - ? ['block_editor' => false, 'classic_editor' => false] |
|
| 51 | - : $editors; |
|
| 52 | - } |
|
| 40 | + /** |
|
| 41 | + * @param array $editors |
|
| 42 | + * @param string $postType |
|
| 43 | + * @return array |
|
| 44 | + * @filter classic_editor_enabled_editors_for_post_type |
|
| 45 | + * @plugin classic-editor/classic-editor.php |
|
| 46 | + */ |
|
| 47 | + public function filterEnabledEditors($editors, $postType) |
|
| 48 | + { |
|
| 49 | + return Application::POST_TYPE == $postType |
|
| 50 | + ? ['block_editor' => false, 'classic_editor' => false] |
|
| 51 | + : $editors; |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * @param bool $bool |
|
| 56 | - * @param string $postType |
|
| 57 | - * @return bool |
|
| 58 | - * @filter use_block_editor_for_post_type |
|
| 59 | - */ |
|
| 60 | - public function filterUseBlockEditor($bool, $postType) |
|
| 61 | - { |
|
| 62 | - return Application::POST_TYPE == $postType |
|
| 63 | - ? false |
|
| 64 | - : $bool; |
|
| 65 | - } |
|
| 54 | + /** |
|
| 55 | + * @param bool $bool |
|
| 56 | + * @param string $postType |
|
| 57 | + * @return bool |
|
| 58 | + * @filter use_block_editor_for_post_type |
|
| 59 | + */ |
|
| 60 | + public function filterUseBlockEditor($bool, $postType) |
|
| 61 | + { |
|
| 62 | + return Application::POST_TYPE == $postType |
|
| 63 | + ? false |
|
| 64 | + : $bool; |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - /** |
|
| 68 | - * @return void |
|
| 69 | - * @action init |
|
| 70 | - */ |
|
| 71 | - public function registerAssets() |
|
| 72 | - { |
|
| 73 | - wp_register_style( |
|
| 74 | - Application::ID.'/blocks', |
|
| 75 | - glsr()->url('assets/styles/'.Application::ID.'-blocks.css'), |
|
| 76 | - ['wp-edit-blocks'], |
|
| 77 | - glsr()->version |
|
| 78 | - ); |
|
| 79 | - wp_register_script( |
|
| 80 | - Application::ID.'/blocks', |
|
| 81 | - glsr()->url('assets/scripts/'.Application::ID.'-blocks.js'), |
|
| 82 | - ['wp-api-fetch', 'wp-blocks', 'wp-i18n', 'wp-editor', 'wp-element', Application::ID.'/admin'], |
|
| 83 | - glsr()->version |
|
| 84 | - ); |
|
| 85 | - } |
|
| 67 | + /** |
|
| 68 | + * @return void |
|
| 69 | + * @action init |
|
| 70 | + */ |
|
| 71 | + public function registerAssets() |
|
| 72 | + { |
|
| 73 | + wp_register_style( |
|
| 74 | + Application::ID.'/blocks', |
|
| 75 | + glsr()->url('assets/styles/'.Application::ID.'-blocks.css'), |
|
| 76 | + ['wp-edit-blocks'], |
|
| 77 | + glsr()->version |
|
| 78 | + ); |
|
| 79 | + wp_register_script( |
|
| 80 | + Application::ID.'/blocks', |
|
| 81 | + glsr()->url('assets/scripts/'.Application::ID.'-blocks.js'), |
|
| 82 | + ['wp-api-fetch', 'wp-blocks', 'wp-i18n', 'wp-editor', 'wp-element', Application::ID.'/admin'], |
|
| 83 | + glsr()->version |
|
| 84 | + ); |
|
| 85 | + } |
|
| 86 | 86 | |
| 87 | - /** |
|
| 88 | - * @return void |
|
| 89 | - * @action init |
|
| 90 | - */ |
|
| 91 | - public function registerBlocks() |
|
| 92 | - { |
|
| 93 | - $blocks = [ |
|
| 94 | - 'form', 'reviews', 'summary', |
|
| 95 | - ]; |
|
| 96 | - foreach ($blocks as $block) { |
|
| 97 | - $id = str_replace('_reviews', '', Application::ID.'_'.$block); |
|
| 98 | - $blockClass = Helper::buildClassName($id.'-block', 'Blocks'); |
|
| 99 | - if (!class_exists($blockClass)) { |
|
| 100 | - glsr_log()->error(sprintf('Block class missing (%s)', $blockClass)); |
|
| 101 | - continue; |
|
| 102 | - } |
|
| 103 | - glsr($blockClass)->register($block); |
|
| 104 | - } |
|
| 105 | - } |
|
| 87 | + /** |
|
| 88 | + * @return void |
|
| 89 | + * @action init |
|
| 90 | + */ |
|
| 91 | + public function registerBlocks() |
|
| 92 | + { |
|
| 93 | + $blocks = [ |
|
| 94 | + 'form', 'reviews', 'summary', |
|
| 95 | + ]; |
|
| 96 | + foreach ($blocks as $block) { |
|
| 97 | + $id = str_replace('_reviews', '', Application::ID.'_'.$block); |
|
| 98 | + $blockClass = Helper::buildClassName($id.'-block', 'Blocks'); |
|
| 99 | + if (!class_exists($blockClass)) { |
|
| 100 | + glsr_log()->error(sprintf('Block class missing (%s)', $blockClass)); |
|
| 101 | + continue; |
|
| 102 | + } |
|
| 103 | + glsr($blockClass)->register($block); |
|
| 104 | + } |
|
| 105 | + } |
|
| 106 | 106 | } |
@@ -14,9 +14,9 @@ discard block |
||
| 14 | 14 | * @return array |
| 15 | 15 | * @filter allowed_block_types |
| 16 | 16 | */ |
| 17 | - public function filterAllowedBlockTypes($blockTypes, $post) |
|
| 17 | + public function filterAllowedBlockTypes( $blockTypes, $post ) |
|
| 18 | 18 | { |
| 19 | - return glsr()->post_type !== get_post_type($post) |
|
| 19 | + return glsr()->post_type !== get_post_type( $post ) |
|
| 20 | 20 | ? $blockTypes |
| 21 | 21 | : []; |
| 22 | 22 | } |
@@ -26,9 +26,9 @@ discard block |
||
| 26 | 26 | * @return array |
| 27 | 27 | * @filter block_categories |
| 28 | 28 | */ |
| 29 | - public function filterBlockCategories($categories) |
|
| 29 | + public function filterBlockCategories( $categories ) |
|
| 30 | 30 | { |
| 31 | - $categories = Arr::consolidate($categories); |
|
| 31 | + $categories = Arr::consolidate( $categories ); |
|
| 32 | 32 | $categories[] = [ |
| 33 | 33 | 'icon' => null, |
| 34 | 34 | 'slug' => Application::ID, |
@@ -44,7 +44,7 @@ discard block |
||
| 44 | 44 | * @filter classic_editor_enabled_editors_for_post_type |
| 45 | 45 | * @plugin classic-editor/classic-editor.php |
| 46 | 46 | */ |
| 47 | - public function filterEnabledEditors($editors, $postType) |
|
| 47 | + public function filterEnabledEditors( $editors, $postType ) |
|
| 48 | 48 | { |
| 49 | 49 | return Application::POST_TYPE == $postType |
| 50 | 50 | ? ['block_editor' => false, 'classic_editor' => false] |
@@ -57,7 +57,7 @@ discard block |
||
| 57 | 57 | * @return bool |
| 58 | 58 | * @filter use_block_editor_for_post_type |
| 59 | 59 | */ |
| 60 | - public function filterUseBlockEditor($bool, $postType) |
|
| 60 | + public function filterUseBlockEditor( $bool, $postType ) |
|
| 61 | 61 | { |
| 62 | 62 | return Application::POST_TYPE == $postType |
| 63 | 63 | ? false |
@@ -72,13 +72,13 @@ discard block |
||
| 72 | 72 | { |
| 73 | 73 | wp_register_style( |
| 74 | 74 | Application::ID.'/blocks', |
| 75 | - glsr()->url('assets/styles/'.Application::ID.'-blocks.css'), |
|
| 75 | + glsr()->url( 'assets/styles/'.Application::ID.'-blocks.css' ), |
|
| 76 | 76 | ['wp-edit-blocks'], |
| 77 | 77 | glsr()->version |
| 78 | 78 | ); |
| 79 | 79 | wp_register_script( |
| 80 | 80 | Application::ID.'/blocks', |
| 81 | - glsr()->url('assets/scripts/'.Application::ID.'-blocks.js'), |
|
| 81 | + glsr()->url( 'assets/scripts/'.Application::ID.'-blocks.js' ), |
|
| 82 | 82 | ['wp-api-fetch', 'wp-blocks', 'wp-i18n', 'wp-editor', 'wp-element', Application::ID.'/admin'], |
| 83 | 83 | glsr()->version |
| 84 | 84 | ); |
@@ -93,14 +93,14 @@ discard block |
||
| 93 | 93 | $blocks = [ |
| 94 | 94 | 'form', 'reviews', 'summary', |
| 95 | 95 | ]; |
| 96 | - foreach ($blocks as $block) { |
|
| 97 | - $id = str_replace('_reviews', '', Application::ID.'_'.$block); |
|
| 98 | - $blockClass = Helper::buildClassName($id.'-block', 'Blocks'); |
|
| 99 | - if (!class_exists($blockClass)) { |
|
| 100 | - glsr_log()->error(sprintf('Block class missing (%s)', $blockClass)); |
|
| 96 | + foreach( $blocks as $block ) { |
|
| 97 | + $id = str_replace( '_reviews', '', Application::ID.'_'.$block ); |
|
| 98 | + $blockClass = Helper::buildClassName( $id.'-block', 'Blocks' ); |
|
| 99 | + if( !class_exists( $blockClass ) ) { |
|
| 100 | + glsr_log()->error( sprintf( 'Block class missing (%s)', $blockClass ) ); |
|
| 101 | 101 | continue; |
| 102 | 102 | } |
| 103 | - glsr($blockClass)->register($block); |
|
| 103 | + glsr( $blockClass )->register( $block ); |
|
| 104 | 104 | } |
| 105 | 105 | } |
| 106 | 106 | } |
@@ -21,198 +21,198 @@ |
||
| 21 | 21 | |
| 22 | 22 | class AjaxController extends Controller |
| 23 | 23 | { |
| 24 | - /** |
|
| 25 | - * @return void |
|
| 26 | - */ |
|
| 27 | - public function routerClearConsole() |
|
| 28 | - { |
|
| 29 | - glsr(AdminController::class)->routerClearConsole(); |
|
| 30 | - wp_send_json_success([ |
|
| 31 | - 'console' => glsr(Console::class)->get(), |
|
| 32 | - 'notices' => glsr(Notice::class)->get(), |
|
| 33 | - ]); |
|
| 34 | - } |
|
| 24 | + /** |
|
| 25 | + * @return void |
|
| 26 | + */ |
|
| 27 | + public function routerClearConsole() |
|
| 28 | + { |
|
| 29 | + glsr(AdminController::class)->routerClearConsole(); |
|
| 30 | + wp_send_json_success([ |
|
| 31 | + 'console' => glsr(Console::class)->get(), |
|
| 32 | + 'notices' => glsr(Notice::class)->get(), |
|
| 33 | + ]); |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * @return void |
|
| 38 | - */ |
|
| 39 | - public function routerDetectIpAddress() |
|
| 40 | - { |
|
| 41 | - glsr(AdminController::class)->routerDetectIpAddress(); |
|
| 42 | - wp_send_json_success([ |
|
| 43 | - 'notices' => glsr(Notice::class)->get(), |
|
| 44 | - ]); |
|
| 45 | - } |
|
| 36 | + /** |
|
| 37 | + * @return void |
|
| 38 | + */ |
|
| 39 | + public function routerDetectIpAddress() |
|
| 40 | + { |
|
| 41 | + glsr(AdminController::class)->routerDetectIpAddress(); |
|
| 42 | + wp_send_json_success([ |
|
| 43 | + 'notices' => glsr(Notice::class)->get(), |
|
| 44 | + ]); |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * @return void |
|
| 49 | - */ |
|
| 50 | - public function routerDismissNotice(Request $request) |
|
| 51 | - { |
|
| 52 | - glsr(NoticeController::class)->routerDismissNotice($request); |
|
| 53 | - wp_send_json_success(); |
|
| 54 | - } |
|
| 47 | + /** |
|
| 48 | + * @return void |
|
| 49 | + */ |
|
| 50 | + public function routerDismissNotice(Request $request) |
|
| 51 | + { |
|
| 52 | + glsr(NoticeController::class)->routerDismissNotice($request); |
|
| 53 | + wp_send_json_success(); |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * @return void |
|
| 58 | - */ |
|
| 59 | - public function routerMceShortcode(Request $request) |
|
| 60 | - { |
|
| 61 | - $shortcode = $request->shortcode; |
|
| 62 | - $response = false; |
|
| 63 | - if ($data = glsr()->retrieve('mce.'.$shortcode, false)) { |
|
| 64 | - if (!empty($data['errors'])) { |
|
| 65 | - $data['btn_okay'] = [esc_attr_x('Okay', 'admin-text', 'site-reviews')]; |
|
| 66 | - } |
|
| 67 | - $response = [ |
|
| 68 | - 'body' => $data['fields'], |
|
| 69 | - 'close' => $data['btn_close'], |
|
| 70 | - 'ok' => $data['btn_okay'], |
|
| 71 | - 'shortcode' => $shortcode, |
|
| 72 | - 'title' => $data['title'], |
|
| 73 | - ]; |
|
| 74 | - } |
|
| 75 | - wp_send_json_success($response); |
|
| 76 | - } |
|
| 56 | + /** |
|
| 57 | + * @return void |
|
| 58 | + */ |
|
| 59 | + public function routerMceShortcode(Request $request) |
|
| 60 | + { |
|
| 61 | + $shortcode = $request->shortcode; |
|
| 62 | + $response = false; |
|
| 63 | + if ($data = glsr()->retrieve('mce.'.$shortcode, false)) { |
|
| 64 | + if (!empty($data['errors'])) { |
|
| 65 | + $data['btn_okay'] = [esc_attr_x('Okay', 'admin-text', 'site-reviews')]; |
|
| 66 | + } |
|
| 67 | + $response = [ |
|
| 68 | + 'body' => $data['fields'], |
|
| 69 | + 'close' => $data['btn_close'], |
|
| 70 | + 'ok' => $data['btn_okay'], |
|
| 71 | + 'shortcode' => $shortcode, |
|
| 72 | + 'title' => $data['title'], |
|
| 73 | + ]; |
|
| 74 | + } |
|
| 75 | + wp_send_json_success($response); |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | - /** |
|
| 79 | - * @return void |
|
| 80 | - */ |
|
| 81 | - public function routerMigratePlugin(Request $request) |
|
| 82 | - { |
|
| 83 | - glsr(AdminController::class)->routerMigratePlugin($request); |
|
| 84 | - wp_send_json_success([ |
|
| 85 | - 'notices' => glsr(Notice::class)->get(), |
|
| 86 | - ]); |
|
| 87 | - } |
|
| 78 | + /** |
|
| 79 | + * @return void |
|
| 80 | + */ |
|
| 81 | + public function routerMigratePlugin(Request $request) |
|
| 82 | + { |
|
| 83 | + glsr(AdminController::class)->routerMigratePlugin($request); |
|
| 84 | + wp_send_json_success([ |
|
| 85 | + 'notices' => glsr(Notice::class)->get(), |
|
| 86 | + ]); |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - /** |
|
| 90 | - * @return void |
|
| 91 | - */ |
|
| 92 | - public function routerFetchConsole() |
|
| 93 | - { |
|
| 94 | - glsr(AdminController::class)->routerFetchConsole(); |
|
| 95 | - wp_send_json_success([ |
|
| 96 | - 'console' => glsr(Console::class)->get(), |
|
| 97 | - 'notices' => glsr(Notice::class)->get(), |
|
| 98 | - ]); |
|
| 99 | - } |
|
| 89 | + /** |
|
| 90 | + * @return void |
|
| 91 | + */ |
|
| 92 | + public function routerFetchConsole() |
|
| 93 | + { |
|
| 94 | + glsr(AdminController::class)->routerFetchConsole(); |
|
| 95 | + wp_send_json_success([ |
|
| 96 | + 'console' => glsr(Console::class)->get(), |
|
| 97 | + 'notices' => glsr(Notice::class)->get(), |
|
| 98 | + ]); |
|
| 99 | + } |
|
| 100 | 100 | |
| 101 | - /** |
|
| 102 | - * @return void |
|
| 103 | - */ |
|
| 104 | - public function routerFetchPagedReviews(Request $request) |
|
| 105 | - { |
|
| 106 | - $args = [ |
|
| 107 | - 'page' => $request->get('page', 0), |
|
| 108 | - 'pageUrl' => '', |
|
| 109 | - 'pagination' => 'ajax', |
|
| 110 | - 'schema' => false, |
|
| 111 | - ]; |
|
| 112 | - if (!$args['page']) { |
|
| 113 | - $urlPath = Url::path($request->url); |
|
| 114 | - $args['page'] = glsr(Helper::class)->getPageNumber($request->url); |
|
| 115 | - $args['pageUrl'] = Url::path(home_url()) === $urlPath |
|
| 116 | - ? Url::home() |
|
| 117 | - : Url::home($urlPath); |
|
| 118 | - } |
|
| 119 | - $atts = glsr(SiteReviewsShortcode::class)->normalizeAtts(Arr::consolidate($request->atts)); |
|
| 120 | - $html = glsr(SiteReviewsPartial::class)->build(wp_parse_args($args, $atts)); |
|
| 121 | - return wp_send_json_success([ |
|
| 122 | - 'pagination' => $html->getPagination(), |
|
| 123 | - 'reviews' => $html->getReviews(), |
|
| 124 | - ]); |
|
| 125 | - } |
|
| 101 | + /** |
|
| 102 | + * @return void |
|
| 103 | + */ |
|
| 104 | + public function routerFetchPagedReviews(Request $request) |
|
| 105 | + { |
|
| 106 | + $args = [ |
|
| 107 | + 'page' => $request->get('page', 0), |
|
| 108 | + 'pageUrl' => '', |
|
| 109 | + 'pagination' => 'ajax', |
|
| 110 | + 'schema' => false, |
|
| 111 | + ]; |
|
| 112 | + if (!$args['page']) { |
|
| 113 | + $urlPath = Url::path($request->url); |
|
| 114 | + $args['page'] = glsr(Helper::class)->getPageNumber($request->url); |
|
| 115 | + $args['pageUrl'] = Url::path(home_url()) === $urlPath |
|
| 116 | + ? Url::home() |
|
| 117 | + : Url::home($urlPath); |
|
| 118 | + } |
|
| 119 | + $atts = glsr(SiteReviewsShortcode::class)->normalizeAtts(Arr::consolidate($request->atts)); |
|
| 120 | + $html = glsr(SiteReviewsPartial::class)->build(wp_parse_args($args, $atts)); |
|
| 121 | + return wp_send_json_success([ |
|
| 122 | + 'pagination' => $html->getPagination(), |
|
| 123 | + 'reviews' => $html->getReviews(), |
|
| 124 | + ]); |
|
| 125 | + } |
|
| 126 | 126 | |
| 127 | - /** |
|
| 128 | - * @return void |
|
| 129 | - */ |
|
| 130 | - public function routerResetPermissions() |
|
| 131 | - { |
|
| 132 | - glsr(Role::class)->resetAll(); |
|
| 133 | - $reloadLink = glsr(Builder::class)->a([ |
|
| 134 | - 'text' => _x('reload the page', 'admin-text', 'site-reviews'), |
|
| 135 | - 'href' => 'javascript:window.location.reload(1)', |
|
| 136 | - ]); |
|
| 137 | - glsr(Notice::class)->clear()->addSuccess( |
|
| 138 | - sprintf(_x('The permissions have been reset, please %s for them to take effect.', 'admin-text', 'site-reviews'), $reloadLink) |
|
| 139 | - ); |
|
| 140 | - wp_send_json_success([ |
|
| 141 | - 'notices' => glsr(Notice::class)->get(), |
|
| 142 | - ]); |
|
| 143 | - } |
|
| 127 | + /** |
|
| 128 | + * @return void |
|
| 129 | + */ |
|
| 130 | + public function routerResetPermissions() |
|
| 131 | + { |
|
| 132 | + glsr(Role::class)->resetAll(); |
|
| 133 | + $reloadLink = glsr(Builder::class)->a([ |
|
| 134 | + 'text' => _x('reload the page', 'admin-text', 'site-reviews'), |
|
| 135 | + 'href' => 'javascript:window.location.reload(1)', |
|
| 136 | + ]); |
|
| 137 | + glsr(Notice::class)->clear()->addSuccess( |
|
| 138 | + sprintf(_x('The permissions have been reset, please %s for them to take effect.', 'admin-text', 'site-reviews'), $reloadLink) |
|
| 139 | + ); |
|
| 140 | + wp_send_json_success([ |
|
| 141 | + 'notices' => glsr(Notice::class)->get(), |
|
| 142 | + ]); |
|
| 143 | + } |
|
| 144 | 144 | |
| 145 | - /** |
|
| 146 | - * @return void |
|
| 147 | - */ |
|
| 148 | - public function routerSearchPosts(Request $request) |
|
| 149 | - { |
|
| 150 | - $results = glsr(Database::class)->searchPosts($request->search); |
|
| 151 | - wp_send_json_success([ |
|
| 152 | - 'empty' => '<div>'._x('Nothing found.', 'admin-text', 'site-reviews').'</div>', |
|
| 153 | - 'items' => $results, |
|
| 154 | - ]); |
|
| 155 | - } |
|
| 145 | + /** |
|
| 146 | + * @return void |
|
| 147 | + */ |
|
| 148 | + public function routerSearchPosts(Request $request) |
|
| 149 | + { |
|
| 150 | + $results = glsr(Database::class)->searchPosts($request->search); |
|
| 151 | + wp_send_json_success([ |
|
| 152 | + 'empty' => '<div>'._x('Nothing found.', 'admin-text', 'site-reviews').'</div>', |
|
| 153 | + 'items' => $results, |
|
| 154 | + ]); |
|
| 155 | + } |
|
| 156 | 156 | |
| 157 | - /** |
|
| 158 | - * @return void |
|
| 159 | - */ |
|
| 160 | - public function routerSearchTranslations(Request $request) |
|
| 161 | - { |
|
| 162 | - if (empty($request->exclude)) { |
|
| 163 | - $request->exclude = []; |
|
| 164 | - } |
|
| 165 | - $results = glsr(Translation::class) |
|
| 166 | - ->search($request->search) |
|
| 167 | - ->exclude() |
|
| 168 | - ->exclude($request->exclude) |
|
| 169 | - ->renderResults(); |
|
| 170 | - wp_send_json_success([ |
|
| 171 | - 'empty' => '<div>'._x('Nothing found.', 'admin-text', 'site-reviews').'</div>', |
|
| 172 | - 'items' => $results, |
|
| 173 | - ]); |
|
| 174 | - } |
|
| 157 | + /** |
|
| 158 | + * @return void |
|
| 159 | + */ |
|
| 160 | + public function routerSearchTranslations(Request $request) |
|
| 161 | + { |
|
| 162 | + if (empty($request->exclude)) { |
|
| 163 | + $request->exclude = []; |
|
| 164 | + } |
|
| 165 | + $results = glsr(Translation::class) |
|
| 166 | + ->search($request->search) |
|
| 167 | + ->exclude() |
|
| 168 | + ->exclude($request->exclude) |
|
| 169 | + ->renderResults(); |
|
| 170 | + wp_send_json_success([ |
|
| 171 | + 'empty' => '<div>'._x('Nothing found.', 'admin-text', 'site-reviews').'</div>', |
|
| 172 | + 'items' => $results, |
|
| 173 | + ]); |
|
| 174 | + } |
|
| 175 | 175 | |
| 176 | - /** |
|
| 177 | - * @return void |
|
| 178 | - */ |
|
| 179 | - public function routerSubmitReview(Request $request) |
|
| 180 | - { |
|
| 181 | - $command = new CreateReview($request->toArray()); |
|
| 182 | - $review = $this->execute($command); |
|
| 183 | - $data = [ |
|
| 184 | - 'errors' => glsr()->sessionGet($command->form_id.'errors', false), |
|
| 185 | - 'html' => (string) $review, |
|
| 186 | - 'message' => glsr()->sessionGet($command->form_id.'message', ''), |
|
| 187 | - 'recaptcha' => glsr()->sessionGet($command->form_id.'recaptcha', false), |
|
| 188 | - 'redirect' => $command->redirect(), |
|
| 189 | - 'review' => (array) $review, |
|
| 190 | - ]; |
|
| 191 | - if (false === $data['errors']) { |
|
| 192 | - glsr()->sessionClear(); |
|
| 193 | - wp_send_json_success($data); |
|
| 194 | - } |
|
| 195 | - wp_send_json_error($data); |
|
| 196 | - } |
|
| 176 | + /** |
|
| 177 | + * @return void |
|
| 178 | + */ |
|
| 179 | + public function routerSubmitReview(Request $request) |
|
| 180 | + { |
|
| 181 | + $command = new CreateReview($request->toArray()); |
|
| 182 | + $review = $this->execute($command); |
|
| 183 | + $data = [ |
|
| 184 | + 'errors' => glsr()->sessionGet($command->form_id.'errors', false), |
|
| 185 | + 'html' => (string) $review, |
|
| 186 | + 'message' => glsr()->sessionGet($command->form_id.'message', ''), |
|
| 187 | + 'recaptcha' => glsr()->sessionGet($command->form_id.'recaptcha', false), |
|
| 188 | + 'redirect' => $command->redirect(), |
|
| 189 | + 'review' => (array) $review, |
|
| 190 | + ]; |
|
| 191 | + if (false === $data['errors']) { |
|
| 192 | + glsr()->sessionClear(); |
|
| 193 | + wp_send_json_success($data); |
|
| 194 | + } |
|
| 195 | + wp_send_json_error($data); |
|
| 196 | + } |
|
| 197 | 197 | |
| 198 | - /** |
|
| 199 | - * @return void |
|
| 200 | - */ |
|
| 201 | - public function routerTogglePinned(Request $request) |
|
| 202 | - { |
|
| 203 | - wp_send_json_success([ |
|
| 204 | - 'notices' => glsr(Notice::class)->get(), |
|
| 205 | - 'pinned' => $this->execute(new TogglePinned($request->toArray())), |
|
| 206 | - ]); |
|
| 207 | - } |
|
| 198 | + /** |
|
| 199 | + * @return void |
|
| 200 | + */ |
|
| 201 | + public function routerTogglePinned(Request $request) |
|
| 202 | + { |
|
| 203 | + wp_send_json_success([ |
|
| 204 | + 'notices' => glsr(Notice::class)->get(), |
|
| 205 | + 'pinned' => $this->execute(new TogglePinned($request->toArray())), |
|
| 206 | + ]); |
|
| 207 | + } |
|
| 208 | 208 | |
| 209 | - /** |
|
| 210 | - * @return void |
|
| 211 | - */ |
|
| 212 | - public function routerToggleStatus(Request $request) |
|
| 213 | - { |
|
| 214 | - wp_send_json_success( |
|
| 215 | - $this->execute(new ToggleStatus($request->post_id, $request->status)) |
|
| 216 | - ); |
|
| 217 | - } |
|
| 209 | + /** |
|
| 210 | + * @return void |
|
| 211 | + */ |
|
| 212 | + public function routerToggleStatus(Request $request) |
|
| 213 | + { |
|
| 214 | + wp_send_json_success( |
|
| 215 | + $this->execute(new ToggleStatus($request->post_id, $request->status)) |
|
| 216 | + ); |
|
| 217 | + } |
|
| 218 | 218 | } |
@@ -26,11 +26,11 @@ discard block |
||
| 26 | 26 | */ |
| 27 | 27 | public function routerClearConsole() |
| 28 | 28 | { |
| 29 | - glsr(AdminController::class)->routerClearConsole(); |
|
| 30 | - wp_send_json_success([ |
|
| 31 | - 'console' => glsr(Console::class)->get(), |
|
| 32 | - 'notices' => glsr(Notice::class)->get(), |
|
| 33 | - ]); |
|
| 29 | + glsr( AdminController::class )->routerClearConsole(); |
|
| 30 | + wp_send_json_success( [ |
|
| 31 | + 'console' => glsr( Console::class )->get(), |
|
| 32 | + 'notices' => glsr( Notice::class )->get(), |
|
| 33 | + ] ); |
|
| 34 | 34 | } |
| 35 | 35 | |
| 36 | 36 | /** |
@@ -38,31 +38,31 @@ discard block |
||
| 38 | 38 | */ |
| 39 | 39 | public function routerDetectIpAddress() |
| 40 | 40 | { |
| 41 | - glsr(AdminController::class)->routerDetectIpAddress(); |
|
| 42 | - wp_send_json_success([ |
|
| 43 | - 'notices' => glsr(Notice::class)->get(), |
|
| 44 | - ]); |
|
| 41 | + glsr( AdminController::class )->routerDetectIpAddress(); |
|
| 42 | + wp_send_json_success( [ |
|
| 43 | + 'notices' => glsr( Notice::class )->get(), |
|
| 44 | + ] ); |
|
| 45 | 45 | } |
| 46 | 46 | |
| 47 | 47 | /** |
| 48 | 48 | * @return void |
| 49 | 49 | */ |
| 50 | - public function routerDismissNotice(Request $request) |
|
| 50 | + public function routerDismissNotice( Request $request ) |
|
| 51 | 51 | { |
| 52 | - glsr(NoticeController::class)->routerDismissNotice($request); |
|
| 52 | + glsr( NoticeController::class )->routerDismissNotice( $request ); |
|
| 53 | 53 | wp_send_json_success(); |
| 54 | 54 | } |
| 55 | 55 | |
| 56 | 56 | /** |
| 57 | 57 | * @return void |
| 58 | 58 | */ |
| 59 | - public function routerMceShortcode(Request $request) |
|
| 59 | + public function routerMceShortcode( Request $request ) |
|
| 60 | 60 | { |
| 61 | 61 | $shortcode = $request->shortcode; |
| 62 | 62 | $response = false; |
| 63 | - if ($data = glsr()->retrieve('mce.'.$shortcode, false)) { |
|
| 64 | - if (!empty($data['errors'])) { |
|
| 65 | - $data['btn_okay'] = [esc_attr_x('Okay', 'admin-text', 'site-reviews')]; |
|
| 63 | + if( $data = glsr()->retrieve( 'mce.'.$shortcode, false ) ) { |
|
| 64 | + if( !empty($data['errors']) ) { |
|
| 65 | + $data['btn_okay'] = [esc_attr_x( 'Okay', 'admin-text', 'site-reviews' )]; |
|
| 66 | 66 | } |
| 67 | 67 | $response = [ |
| 68 | 68 | 'body' => $data['fields'], |
@@ -72,18 +72,18 @@ discard block |
||
| 72 | 72 | 'title' => $data['title'], |
| 73 | 73 | ]; |
| 74 | 74 | } |
| 75 | - wp_send_json_success($response); |
|
| 75 | + wp_send_json_success( $response ); |
|
| 76 | 76 | } |
| 77 | 77 | |
| 78 | 78 | /** |
| 79 | 79 | * @return void |
| 80 | 80 | */ |
| 81 | - public function routerMigratePlugin(Request $request) |
|
| 81 | + public function routerMigratePlugin( Request $request ) |
|
| 82 | 82 | { |
| 83 | - glsr(AdminController::class)->routerMigratePlugin($request); |
|
| 84 | - wp_send_json_success([ |
|
| 85 | - 'notices' => glsr(Notice::class)->get(), |
|
| 86 | - ]); |
|
| 83 | + glsr( AdminController::class )->routerMigratePlugin( $request ); |
|
| 84 | + wp_send_json_success( [ |
|
| 85 | + 'notices' => glsr( Notice::class )->get(), |
|
| 86 | + ] ); |
|
| 87 | 87 | } |
| 88 | 88 | |
| 89 | 89 | /** |
@@ -91,37 +91,37 @@ discard block |
||
| 91 | 91 | */ |
| 92 | 92 | public function routerFetchConsole() |
| 93 | 93 | { |
| 94 | - glsr(AdminController::class)->routerFetchConsole(); |
|
| 95 | - wp_send_json_success([ |
|
| 96 | - 'console' => glsr(Console::class)->get(), |
|
| 97 | - 'notices' => glsr(Notice::class)->get(), |
|
| 98 | - ]); |
|
| 94 | + glsr( AdminController::class )->routerFetchConsole(); |
|
| 95 | + wp_send_json_success( [ |
|
| 96 | + 'console' => glsr( Console::class )->get(), |
|
| 97 | + 'notices' => glsr( Notice::class )->get(), |
|
| 98 | + ] ); |
|
| 99 | 99 | } |
| 100 | 100 | |
| 101 | 101 | /** |
| 102 | 102 | * @return void |
| 103 | 103 | */ |
| 104 | - public function routerFetchPagedReviews(Request $request) |
|
| 104 | + public function routerFetchPagedReviews( Request $request ) |
|
| 105 | 105 | { |
| 106 | 106 | $args = [ |
| 107 | - 'page' => $request->get('page', 0), |
|
| 107 | + 'page' => $request->get( 'page', 0 ), |
|
| 108 | 108 | 'pageUrl' => '', |
| 109 | 109 | 'pagination' => 'ajax', |
| 110 | 110 | 'schema' => false, |
| 111 | 111 | ]; |
| 112 | - if (!$args['page']) { |
|
| 113 | - $urlPath = Url::path($request->url); |
|
| 114 | - $args['page'] = glsr(Helper::class)->getPageNumber($request->url); |
|
| 115 | - $args['pageUrl'] = Url::path(home_url()) === $urlPath |
|
| 112 | + if( !$args['page'] ) { |
|
| 113 | + $urlPath = Url::path( $request->url ); |
|
| 114 | + $args['page'] = glsr( Helper::class )->getPageNumber( $request->url ); |
|
| 115 | + $args['pageUrl'] = Url::path( home_url() ) === $urlPath |
|
| 116 | 116 | ? Url::home() |
| 117 | - : Url::home($urlPath); |
|
| 117 | + : Url::home( $urlPath ); |
|
| 118 | 118 | } |
| 119 | - $atts = glsr(SiteReviewsShortcode::class)->normalizeAtts(Arr::consolidate($request->atts)); |
|
| 120 | - $html = glsr(SiteReviewsPartial::class)->build(wp_parse_args($args, $atts)); |
|
| 121 | - return wp_send_json_success([ |
|
| 119 | + $atts = glsr( SiteReviewsShortcode::class )->normalizeAtts( Arr::consolidate( $request->atts ) ); |
|
| 120 | + $html = glsr( SiteReviewsPartial::class )->build( wp_parse_args( $args, $atts ) ); |
|
| 121 | + return wp_send_json_success( [ |
|
| 122 | 122 | 'pagination' => $html->getPagination(), |
| 123 | 123 | 'reviews' => $html->getReviews(), |
| 124 | - ]); |
|
| 124 | + ] ); |
|
| 125 | 125 | } |
| 126 | 126 | |
| 127 | 127 | /** |
@@ -129,90 +129,90 @@ discard block |
||
| 129 | 129 | */ |
| 130 | 130 | public function routerResetPermissions() |
| 131 | 131 | { |
| 132 | - glsr(Role::class)->resetAll(); |
|
| 133 | - $reloadLink = glsr(Builder::class)->a([ |
|
| 134 | - 'text' => _x('reload the page', 'admin-text', 'site-reviews'), |
|
| 132 | + glsr( Role::class )->resetAll(); |
|
| 133 | + $reloadLink = glsr( Builder::class )->a( [ |
|
| 134 | + 'text' => _x( 'reload the page', 'admin-text', 'site-reviews' ), |
|
| 135 | 135 | 'href' => 'javascript:window.location.reload(1)', |
| 136 | - ]); |
|
| 137 | - glsr(Notice::class)->clear()->addSuccess( |
|
| 138 | - sprintf(_x('The permissions have been reset, please %s for them to take effect.', 'admin-text', 'site-reviews'), $reloadLink) |
|
| 136 | + ] ); |
|
| 137 | + glsr( Notice::class )->clear()->addSuccess( |
|
| 138 | + sprintf( _x( 'The permissions have been reset, please %s for them to take effect.', 'admin-text', 'site-reviews' ), $reloadLink ) |
|
| 139 | 139 | ); |
| 140 | - wp_send_json_success([ |
|
| 141 | - 'notices' => glsr(Notice::class)->get(), |
|
| 142 | - ]); |
|
| 140 | + wp_send_json_success( [ |
|
| 141 | + 'notices' => glsr( Notice::class )->get(), |
|
| 142 | + ] ); |
|
| 143 | 143 | } |
| 144 | 144 | |
| 145 | 145 | /** |
| 146 | 146 | * @return void |
| 147 | 147 | */ |
| 148 | - public function routerSearchPosts(Request $request) |
|
| 148 | + public function routerSearchPosts( Request $request ) |
|
| 149 | 149 | { |
| 150 | - $results = glsr(Database::class)->searchPosts($request->search); |
|
| 151 | - wp_send_json_success([ |
|
| 152 | - 'empty' => '<div>'._x('Nothing found.', 'admin-text', 'site-reviews').'</div>', |
|
| 150 | + $results = glsr( Database::class )->searchPosts( $request->search ); |
|
| 151 | + wp_send_json_success( [ |
|
| 152 | + 'empty' => '<div>'._x( 'Nothing found.', 'admin-text', 'site-reviews' ).'</div>', |
|
| 153 | 153 | 'items' => $results, |
| 154 | - ]); |
|
| 154 | + ] ); |
|
| 155 | 155 | } |
| 156 | 156 | |
| 157 | 157 | /** |
| 158 | 158 | * @return void |
| 159 | 159 | */ |
| 160 | - public function routerSearchTranslations(Request $request) |
|
| 160 | + public function routerSearchTranslations( Request $request ) |
|
| 161 | 161 | { |
| 162 | - if (empty($request->exclude)) { |
|
| 162 | + if( empty($request->exclude) ) { |
|
| 163 | 163 | $request->exclude = []; |
| 164 | 164 | } |
| 165 | - $results = glsr(Translation::class) |
|
| 166 | - ->search($request->search) |
|
| 165 | + $results = glsr( Translation::class ) |
|
| 166 | + ->search( $request->search ) |
|
| 167 | 167 | ->exclude() |
| 168 | - ->exclude($request->exclude) |
|
| 168 | + ->exclude( $request->exclude ) |
|
| 169 | 169 | ->renderResults(); |
| 170 | - wp_send_json_success([ |
|
| 171 | - 'empty' => '<div>'._x('Nothing found.', 'admin-text', 'site-reviews').'</div>', |
|
| 170 | + wp_send_json_success( [ |
|
| 171 | + 'empty' => '<div>'._x( 'Nothing found.', 'admin-text', 'site-reviews' ).'</div>', |
|
| 172 | 172 | 'items' => $results, |
| 173 | - ]); |
|
| 173 | + ] ); |
|
| 174 | 174 | } |
| 175 | 175 | |
| 176 | 176 | /** |
| 177 | 177 | * @return void |
| 178 | 178 | */ |
| 179 | - public function routerSubmitReview(Request $request) |
|
| 179 | + public function routerSubmitReview( Request $request ) |
|
| 180 | 180 | { |
| 181 | - $command = new CreateReview($request->toArray()); |
|
| 182 | - $review = $this->execute($command); |
|
| 181 | + $command = new CreateReview( $request->toArray() ); |
|
| 182 | + $review = $this->execute( $command ); |
|
| 183 | 183 | $data = [ |
| 184 | - 'errors' => glsr()->sessionGet($command->form_id.'errors', false), |
|
| 185 | - 'html' => (string) $review, |
|
| 186 | - 'message' => glsr()->sessionGet($command->form_id.'message', ''), |
|
| 187 | - 'recaptcha' => glsr()->sessionGet($command->form_id.'recaptcha', false), |
|
| 184 | + 'errors' => glsr()->sessionGet( $command->form_id.'errors', false ), |
|
| 185 | + 'html' => (string)$review, |
|
| 186 | + 'message' => glsr()->sessionGet( $command->form_id.'message', '' ), |
|
| 187 | + 'recaptcha' => glsr()->sessionGet( $command->form_id.'recaptcha', false ), |
|
| 188 | 188 | 'redirect' => $command->redirect(), |
| 189 | - 'review' => (array) $review, |
|
| 189 | + 'review' => (array)$review, |
|
| 190 | 190 | ]; |
| 191 | - if (false === $data['errors']) { |
|
| 191 | + if( false === $data['errors'] ) { |
|
| 192 | 192 | glsr()->sessionClear(); |
| 193 | - wp_send_json_success($data); |
|
| 193 | + wp_send_json_success( $data ); |
|
| 194 | 194 | } |
| 195 | - wp_send_json_error($data); |
|
| 195 | + wp_send_json_error( $data ); |
|
| 196 | 196 | } |
| 197 | 197 | |
| 198 | 198 | /** |
| 199 | 199 | * @return void |
| 200 | 200 | */ |
| 201 | - public function routerTogglePinned(Request $request) |
|
| 201 | + public function routerTogglePinned( Request $request ) |
|
| 202 | 202 | { |
| 203 | - wp_send_json_success([ |
|
| 204 | - 'notices' => glsr(Notice::class)->get(), |
|
| 205 | - 'pinned' => $this->execute(new TogglePinned($request->toArray())), |
|
| 206 | - ]); |
|
| 203 | + wp_send_json_success( [ |
|
| 204 | + 'notices' => glsr( Notice::class )->get(), |
|
| 205 | + 'pinned' => $this->execute( new TogglePinned( $request->toArray() ) ), |
|
| 206 | + ] ); |
|
| 207 | 207 | } |
| 208 | 208 | |
| 209 | 209 | /** |
| 210 | 210 | * @return void |
| 211 | 211 | */ |
| 212 | - public function routerToggleStatus(Request $request) |
|
| 212 | + public function routerToggleStatus( Request $request ) |
|
| 213 | 213 | { |
| 214 | 214 | wp_send_json_success( |
| 215 | - $this->execute(new ToggleStatus($request->post_id, $request->status)) |
|
| 215 | + $this->execute( new ToggleStatus( $request->post_id, $request->status ) ) |
|
| 216 | 216 | ); |
| 217 | 217 | } |
| 218 | 218 | } |
@@ -12,344 +12,344 @@ |
||
| 12 | 12 | |
| 13 | 13 | class Schema |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * @var array |
|
| 17 | - */ |
|
| 18 | - protected $args; |
|
| 15 | + /** |
|
| 16 | + * @var array |
|
| 17 | + */ |
|
| 18 | + protected $args; |
|
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * @var array |
|
| 22 | - */ |
|
| 23 | - protected $keyValues = []; |
|
| 20 | + /** |
|
| 21 | + * @var array |
|
| 22 | + */ |
|
| 23 | + protected $keyValues = []; |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * @var array |
|
| 27 | - */ |
|
| 28 | - protected $ratingCounts; |
|
| 25 | + /** |
|
| 26 | + * @var array |
|
| 27 | + */ |
|
| 28 | + protected $ratingCounts; |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * @var \GeminiLabs\SiteReviews\Reviews|array |
|
| 32 | - */ |
|
| 33 | - protected $reviews; |
|
| 30 | + /** |
|
| 31 | + * @var \GeminiLabs\SiteReviews\Reviews|array |
|
| 32 | + */ |
|
| 33 | + protected $reviews; |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * @return array |
|
| 37 | - */ |
|
| 38 | - public function build(array $args = [], $reviews = []) |
|
| 39 | - { |
|
| 40 | - $this->args = $args; |
|
| 41 | - $this->reviews = $reviews; |
|
| 42 | - $schema = $this->buildSummary($args); |
|
| 43 | - if (!empty($schema)) { |
|
| 44 | - $reviews = $this->buildReviews(); |
|
| 45 | - foreach ($reviews as &$review) { |
|
| 46 | - unset($review['@context']); |
|
| 47 | - unset($review['itemReviewed']); |
|
| 48 | - } |
|
| 49 | - } |
|
| 50 | - if (!empty($reviews)) { |
|
| 51 | - $schema['review'] = $reviews; |
|
| 52 | - } |
|
| 53 | - return $schema; |
|
| 54 | - } |
|
| 35 | + /** |
|
| 36 | + * @return array |
|
| 37 | + */ |
|
| 38 | + public function build(array $args = [], $reviews = []) |
|
| 39 | + { |
|
| 40 | + $this->args = $args; |
|
| 41 | + $this->reviews = $reviews; |
|
| 42 | + $schema = $this->buildSummary($args); |
|
| 43 | + if (!empty($schema)) { |
|
| 44 | + $reviews = $this->buildReviews(); |
|
| 45 | + foreach ($reviews as &$review) { |
|
| 46 | + unset($review['@context']); |
|
| 47 | + unset($review['itemReviewed']); |
|
| 48 | + } |
|
| 49 | + } |
|
| 50 | + if (!empty($reviews)) { |
|
| 51 | + $schema['review'] = $reviews; |
|
| 52 | + } |
|
| 53 | + return $schema; |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * @param array|null $args |
|
| 58 | - * @return array |
|
| 59 | - */ |
|
| 60 | - public function buildSummary($args = null, array $ratings = []) |
|
| 61 | - { |
|
| 62 | - if (is_array($args)) { |
|
| 63 | - $this->args = $args; |
|
| 64 | - } |
|
| 65 | - $buildSummary = Helper::buildMethodName($this->getSchemaOptionValue('type'), 'buildSummaryFor'); |
|
| 66 | - if ($count = array_sum($this->getRatingCounts($ratings))) { |
|
| 67 | - $schema = method_exists($this, $buildSummary) |
|
| 68 | - ? $this->$buildSummary() |
|
| 69 | - : $this->buildSummaryForCustom(); |
|
| 70 | - $schema->aggregateRating( |
|
| 71 | - $this->getSchemaType('AggregateRating') |
|
| 72 | - ->ratingValue($this->getRatingValue()) |
|
| 73 | - ->reviewCount($count) |
|
| 74 | - ->bestRating(glsr()->constant('MAX_RATING', Rating::class)) |
|
| 75 | - ->worstRating(glsr()->constant('MIN_RATING', Rating::class)) |
|
| 76 | - ); |
|
| 77 | - $schema = $schema->toArray(); |
|
| 78 | - return glsr()->filterArray('schema/'.$schema['@type'], $schema, $args); |
|
| 79 | - } |
|
| 80 | - return []; |
|
| 81 | - } |
|
| 56 | + /** |
|
| 57 | + * @param array|null $args |
|
| 58 | + * @return array |
|
| 59 | + */ |
|
| 60 | + public function buildSummary($args = null, array $ratings = []) |
|
| 61 | + { |
|
| 62 | + if (is_array($args)) { |
|
| 63 | + $this->args = $args; |
|
| 64 | + } |
|
| 65 | + $buildSummary = Helper::buildMethodName($this->getSchemaOptionValue('type'), 'buildSummaryFor'); |
|
| 66 | + if ($count = array_sum($this->getRatingCounts($ratings))) { |
|
| 67 | + $schema = method_exists($this, $buildSummary) |
|
| 68 | + ? $this->$buildSummary() |
|
| 69 | + : $this->buildSummaryForCustom(); |
|
| 70 | + $schema->aggregateRating( |
|
| 71 | + $this->getSchemaType('AggregateRating') |
|
| 72 | + ->ratingValue($this->getRatingValue()) |
|
| 73 | + ->reviewCount($count) |
|
| 74 | + ->bestRating(glsr()->constant('MAX_RATING', Rating::class)) |
|
| 75 | + ->worstRating(glsr()->constant('MIN_RATING', Rating::class)) |
|
| 76 | + ); |
|
| 77 | + $schema = $schema->toArray(); |
|
| 78 | + return glsr()->filterArray('schema/'.$schema['@type'], $schema, $args); |
|
| 79 | + } |
|
| 80 | + return []; |
|
| 81 | + } |
|
| 82 | 82 | |
| 83 | - /** |
|
| 84 | - * @return void |
|
| 85 | - */ |
|
| 86 | - public function render() |
|
| 87 | - { |
|
| 88 | - if (empty($schemas = glsr()->retrieve('schemas', []))) { |
|
| 89 | - return; |
|
| 90 | - } |
|
| 91 | - printf('<script type="application/ld+json">%s</script>', json_encode( |
|
| 92 | - glsr()->filterArray('schema/all', $schemas), |
|
| 93 | - JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES |
|
| 94 | - )); |
|
| 95 | - } |
|
| 83 | + /** |
|
| 84 | + * @return void |
|
| 85 | + */ |
|
| 86 | + public function render() |
|
| 87 | + { |
|
| 88 | + if (empty($schemas = glsr()->retrieve('schemas', []))) { |
|
| 89 | + return; |
|
| 90 | + } |
|
| 91 | + printf('<script type="application/ld+json">%s</script>', json_encode( |
|
| 92 | + glsr()->filterArray('schema/all', $schemas), |
|
| 93 | + JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES |
|
| 94 | + )); |
|
| 95 | + } |
|
| 96 | 96 | |
| 97 | - /** |
|
| 98 | - * @return void |
|
| 99 | - */ |
|
| 100 | - public function store(array $schema) |
|
| 101 | - { |
|
| 102 | - if (empty($schema)) { |
|
| 103 | - return; |
|
| 104 | - } |
|
| 105 | - $schemas = Arr::consolidate(glsr()->retrieve('schemas')); |
|
| 106 | - $schemas[] = $schema; |
|
| 107 | - $schemas = array_map('unserialize', array_unique(array_map('serialize', $schemas))); |
|
| 108 | - glsr()->store('schemas', $schemas); |
|
| 109 | - } |
|
| 97 | + /** |
|
| 98 | + * @return void |
|
| 99 | + */ |
|
| 100 | + public function store(array $schema) |
|
| 101 | + { |
|
| 102 | + if (empty($schema)) { |
|
| 103 | + return; |
|
| 104 | + } |
|
| 105 | + $schemas = Arr::consolidate(glsr()->retrieve('schemas')); |
|
| 106 | + $schemas[] = $schema; |
|
| 107 | + $schemas = array_map('unserialize', array_unique(array_map('serialize', $schemas))); |
|
| 108 | + glsr()->store('schemas', $schemas); |
|
| 109 | + } |
|
| 110 | 110 | |
| 111 | - /** |
|
| 112 | - * @param Review $review |
|
| 113 | - * @return array |
|
| 114 | - */ |
|
| 115 | - protected function buildReview($review) |
|
| 116 | - { |
|
| 117 | - $schema = $this->getSchemaType('Review') |
|
| 118 | - ->doIf(!in_array('title', $this->args['hide']), function ($schema) use ($review) { |
|
| 119 | - $schema->name($review->title); |
|
| 120 | - }) |
|
| 121 | - ->doIf(!in_array('excerpt', $this->args['hide']), function ($schema) use ($review) { |
|
| 122 | - $schema->reviewBody($review->content); |
|
| 123 | - }) |
|
| 124 | - ->datePublished((new DateTime($review->date))) |
|
| 125 | - ->author($this->getSchemaType('Person')->name($review->author)) |
|
| 126 | - ->itemReviewed($this->getSchemaType()->name($this->getSchemaOptionValue('name'))); |
|
| 127 | - if (!empty($review->rating)) { |
|
| 128 | - $schema->reviewRating( |
|
| 129 | - $this->getSchemaType('Rating') |
|
| 130 | - ->ratingValue($review->rating) |
|
| 131 | - ->bestRating(glsr()->constant('MAX_RATING', Rating::class)) |
|
| 132 | - ->worstRating(glsr()->constant('MIN_RATING', Rating::class)) |
|
| 133 | - ); |
|
| 134 | - } |
|
| 135 | - return glsr()->filterArray('schema/review', $schema->toArray(), $review, $this->args); |
|
| 136 | - } |
|
| 111 | + /** |
|
| 112 | + * @param Review $review |
|
| 113 | + * @return array |
|
| 114 | + */ |
|
| 115 | + protected function buildReview($review) |
|
| 116 | + { |
|
| 117 | + $schema = $this->getSchemaType('Review') |
|
| 118 | + ->doIf(!in_array('title', $this->args['hide']), function ($schema) use ($review) { |
|
| 119 | + $schema->name($review->title); |
|
| 120 | + }) |
|
| 121 | + ->doIf(!in_array('excerpt', $this->args['hide']), function ($schema) use ($review) { |
|
| 122 | + $schema->reviewBody($review->content); |
|
| 123 | + }) |
|
| 124 | + ->datePublished((new DateTime($review->date))) |
|
| 125 | + ->author($this->getSchemaType('Person')->name($review->author)) |
|
| 126 | + ->itemReviewed($this->getSchemaType()->name($this->getSchemaOptionValue('name'))); |
|
| 127 | + if (!empty($review->rating)) { |
|
| 128 | + $schema->reviewRating( |
|
| 129 | + $this->getSchemaType('Rating') |
|
| 130 | + ->ratingValue($review->rating) |
|
| 131 | + ->bestRating(glsr()->constant('MAX_RATING', Rating::class)) |
|
| 132 | + ->worstRating(glsr()->constant('MIN_RATING', Rating::class)) |
|
| 133 | + ); |
|
| 134 | + } |
|
| 135 | + return glsr()->filterArray('schema/review', $schema->toArray(), $review, $this->args); |
|
| 136 | + } |
|
| 137 | 137 | |
| 138 | - /** |
|
| 139 | - * @return array |
|
| 140 | - */ |
|
| 141 | - protected function buildReviews() |
|
| 142 | - { |
|
| 143 | - $reviews = []; |
|
| 144 | - foreach ($this->reviews as $review) { |
|
| 145 | - // Only include critic reviews that have been directly produced by your site, not reviews from third-party sites or syndicated reviews. |
|
| 146 | - // @see https://developers.google.com/search/docs/data-types/review |
|
| 147 | - if ('local' === $review->review_type) { |
|
| 148 | - $reviews[] = $this->buildReview($review); |
|
| 149 | - } |
|
| 150 | - } |
|
| 151 | - return $reviews; |
|
| 152 | - } |
|
| 138 | + /** |
|
| 139 | + * @return array |
|
| 140 | + */ |
|
| 141 | + protected function buildReviews() |
|
| 142 | + { |
|
| 143 | + $reviews = []; |
|
| 144 | + foreach ($this->reviews as $review) { |
|
| 145 | + // Only include critic reviews that have been directly produced by your site, not reviews from third-party sites or syndicated reviews. |
|
| 146 | + // @see https://developers.google.com/search/docs/data-types/review |
|
| 147 | + if ('local' === $review->review_type) { |
|
| 148 | + $reviews[] = $this->buildReview($review); |
|
| 149 | + } |
|
| 150 | + } |
|
| 151 | + return $reviews; |
|
| 152 | + } |
|
| 153 | 153 | |
| 154 | - /** |
|
| 155 | - * @param mixed $schema |
|
| 156 | - * @return mixed |
|
| 157 | - */ |
|
| 158 | - protected function buildSchemaValues($schema, array $values = []) |
|
| 159 | - { |
|
| 160 | - foreach ($values as $value) { |
|
| 161 | - $option = $this->getSchemaOptionValue($value); |
|
| 162 | - if (empty($option)) { |
|
| 163 | - continue; |
|
| 164 | - } |
|
| 165 | - $schema->$value($option); |
|
| 166 | - } |
|
| 167 | - return $schema; |
|
| 168 | - } |
|
| 154 | + /** |
|
| 155 | + * @param mixed $schema |
|
| 156 | + * @return mixed |
|
| 157 | + */ |
|
| 158 | + protected function buildSchemaValues($schema, array $values = []) |
|
| 159 | + { |
|
| 160 | + foreach ($values as $value) { |
|
| 161 | + $option = $this->getSchemaOptionValue($value); |
|
| 162 | + if (empty($option)) { |
|
| 163 | + continue; |
|
| 164 | + } |
|
| 165 | + $schema->$value($option); |
|
| 166 | + } |
|
| 167 | + return $schema; |
|
| 168 | + } |
|
| 169 | 169 | |
| 170 | - /** |
|
| 171 | - * @return mixed |
|
| 172 | - */ |
|
| 173 | - protected function buildSummaryForCustom() |
|
| 174 | - { |
|
| 175 | - return $this->buildSchemaValues($this->getSchemaType(), [ |
|
| 176 | - 'description', 'image', 'name', 'url', |
|
| 177 | - ]); |
|
| 178 | - } |
|
| 170 | + /** |
|
| 171 | + * @return mixed |
|
| 172 | + */ |
|
| 173 | + protected function buildSummaryForCustom() |
|
| 174 | + { |
|
| 175 | + return $this->buildSchemaValues($this->getSchemaType(), [ |
|
| 176 | + 'description', 'image', 'name', 'url', |
|
| 177 | + ]); |
|
| 178 | + } |
|
| 179 | 179 | |
| 180 | - /** |
|
| 181 | - * @return mixed |
|
| 182 | - */ |
|
| 183 | - protected function buildSummaryForLocalBusiness() |
|
| 184 | - { |
|
| 185 | - return $this->buildSchemaValues($this->buildSummaryForCustom(), [ |
|
| 186 | - 'address', 'priceRange', 'telephone', |
|
| 187 | - ]); |
|
| 188 | - } |
|
| 180 | + /** |
|
| 181 | + * @return mixed |
|
| 182 | + */ |
|
| 183 | + protected function buildSummaryForLocalBusiness() |
|
| 184 | + { |
|
| 185 | + return $this->buildSchemaValues($this->buildSummaryForCustom(), [ |
|
| 186 | + 'address', 'priceRange', 'telephone', |
|
| 187 | + ]); |
|
| 188 | + } |
|
| 189 | 189 | |
| 190 | - /** |
|
| 191 | - * @return mixed |
|
| 192 | - */ |
|
| 193 | - protected function buildSummaryForProduct() |
|
| 194 | - { |
|
| 195 | - $offerType = $this->getSchemaOption('offerType', 'AggregateOffer'); |
|
| 196 | - $offers = $this->buildSchemaValues($this->getSchemaType($offerType), [ |
|
| 197 | - 'highPrice', 'lowPrice', 'price', 'priceCurrency', |
|
| 198 | - ]); |
|
| 199 | - return $this->buildSummaryForCustom() |
|
| 200 | - ->doIf(!empty($offers->getProperties()), function ($schema) use ($offers) { |
|
| 201 | - $schema->offers($offers); |
|
| 202 | - }) |
|
| 203 | - ->setProperty('@id', $this->getSchemaOptionValue('url').'#product'); |
|
| 204 | - } |
|
| 190 | + /** |
|
| 191 | + * @return mixed |
|
| 192 | + */ |
|
| 193 | + protected function buildSummaryForProduct() |
|
| 194 | + { |
|
| 195 | + $offerType = $this->getSchemaOption('offerType', 'AggregateOffer'); |
|
| 196 | + $offers = $this->buildSchemaValues($this->getSchemaType($offerType), [ |
|
| 197 | + 'highPrice', 'lowPrice', 'price', 'priceCurrency', |
|
| 198 | + ]); |
|
| 199 | + return $this->buildSummaryForCustom() |
|
| 200 | + ->doIf(!empty($offers->getProperties()), function ($schema) use ($offers) { |
|
| 201 | + $schema->offers($offers); |
|
| 202 | + }) |
|
| 203 | + ->setProperty('@id', $this->getSchemaOptionValue('url').'#product'); |
|
| 204 | + } |
|
| 205 | 205 | |
| 206 | - /** |
|
| 207 | - * @return array |
|
| 208 | - */ |
|
| 209 | - protected function getRatingCounts(array $ratings = []) |
|
| 210 | - { |
|
| 211 | - if (!isset($this->ratingCounts)) { |
|
| 212 | - $this->ratingCounts = empty($ratings) |
|
| 213 | - ? glsr(RatingManager::class)->ratings($this->args) |
|
| 214 | - : $ratings; |
|
| 215 | - } |
|
| 216 | - return $this->ratingCounts; |
|
| 217 | - } |
|
| 206 | + /** |
|
| 207 | + * @return array |
|
| 208 | + */ |
|
| 209 | + protected function getRatingCounts(array $ratings = []) |
|
| 210 | + { |
|
| 211 | + if (!isset($this->ratingCounts)) { |
|
| 212 | + $this->ratingCounts = empty($ratings) |
|
| 213 | + ? glsr(RatingManager::class)->ratings($this->args) |
|
| 214 | + : $ratings; |
|
| 215 | + } |
|
| 216 | + return $this->ratingCounts; |
|
| 217 | + } |
|
| 218 | 218 | |
| 219 | - /** |
|
| 220 | - * @return int|float |
|
| 221 | - */ |
|
| 222 | - protected function getRatingValue() |
|
| 223 | - { |
|
| 224 | - return glsr(Rating::class)->average($this->getRatingCounts()); |
|
| 225 | - } |
|
| 219 | + /** |
|
| 220 | + * @return int|float |
|
| 221 | + */ |
|
| 222 | + protected function getRatingValue() |
|
| 223 | + { |
|
| 224 | + return glsr(Rating::class)->average($this->getRatingCounts()); |
|
| 225 | + } |
|
| 226 | 226 | |
| 227 | - /** |
|
| 228 | - * @param string $option |
|
| 229 | - * @param string $fallback |
|
| 230 | - * @return string |
|
| 231 | - */ |
|
| 232 | - protected function getSchemaOption($option, $fallback) |
|
| 233 | - { |
|
| 234 | - $option = strtolower($option); |
|
| 235 | - if ($schemaOption = trim((string) get_post_meta(intval(get_the_ID()), 'schema_'.$option, true))) { |
|
| 236 | - return $schemaOption; |
|
| 237 | - } |
|
| 238 | - $setting = glsr(OptionManager::class)->get('settings.schema.'.$option); |
|
| 239 | - if (is_array($setting)) { |
|
| 240 | - return $this->getSchemaOptionDefault($setting, $fallback); |
|
| 241 | - } |
|
| 242 | - return !empty($setting) |
|
| 243 | - ? $setting |
|
| 244 | - : $fallback; |
|
| 245 | - } |
|
| 227 | + /** |
|
| 228 | + * @param string $option |
|
| 229 | + * @param string $fallback |
|
| 230 | + * @return string |
|
| 231 | + */ |
|
| 232 | + protected function getSchemaOption($option, $fallback) |
|
| 233 | + { |
|
| 234 | + $option = strtolower($option); |
|
| 235 | + if ($schemaOption = trim((string) get_post_meta(intval(get_the_ID()), 'schema_'.$option, true))) { |
|
| 236 | + return $schemaOption; |
|
| 237 | + } |
|
| 238 | + $setting = glsr(OptionManager::class)->get('settings.schema.'.$option); |
|
| 239 | + if (is_array($setting)) { |
|
| 240 | + return $this->getSchemaOptionDefault($setting, $fallback); |
|
| 241 | + } |
|
| 242 | + return !empty($setting) |
|
| 243 | + ? $setting |
|
| 244 | + : $fallback; |
|
| 245 | + } |
|
| 246 | 246 | |
| 247 | - /** |
|
| 248 | - * @param string $fallback |
|
| 249 | - * @return string |
|
| 250 | - */ |
|
| 251 | - protected function getSchemaOptionDefault(array $setting, $fallback) |
|
| 252 | - { |
|
| 253 | - $setting = wp_parse_args($setting, [ |
|
| 254 | - 'custom' => '', |
|
| 255 | - 'default' => $fallback, |
|
| 256 | - ]); |
|
| 257 | - return 'custom' != $setting['default'] |
|
| 258 | - ? $setting['default'] |
|
| 259 | - : $setting['custom']; |
|
| 260 | - } |
|
| 247 | + /** |
|
| 248 | + * @param string $fallback |
|
| 249 | + * @return string |
|
| 250 | + */ |
|
| 251 | + protected function getSchemaOptionDefault(array $setting, $fallback) |
|
| 252 | + { |
|
| 253 | + $setting = wp_parse_args($setting, [ |
|
| 254 | + 'custom' => '', |
|
| 255 | + 'default' => $fallback, |
|
| 256 | + ]); |
|
| 257 | + return 'custom' != $setting['default'] |
|
| 258 | + ? $setting['default'] |
|
| 259 | + : $setting['custom']; |
|
| 260 | + } |
|
| 261 | 261 | |
| 262 | - /** |
|
| 263 | - * @param string $option |
|
| 264 | - * @param string $fallback |
|
| 265 | - * @return void|string |
|
| 266 | - */ |
|
| 267 | - protected function getSchemaOptionValue($option, $fallback = 'post') |
|
| 268 | - { |
|
| 269 | - if (array_key_exists($option, $this->keyValues)) { |
|
| 270 | - return $this->keyValues[$option]; |
|
| 271 | - } |
|
| 272 | - $value = $this->getSchemaOption($option, $fallback); |
|
| 273 | - if ($value != $fallback) { |
|
| 274 | - return $this->setAndGetKeyValue($option, $value); |
|
| 275 | - } |
|
| 276 | - if (!is_single() && !is_page()) { |
|
| 277 | - return; |
|
| 278 | - } |
|
| 279 | - $method = Helper::buildMethodName($option, 'getThing'); |
|
| 280 | - if (method_exists($this, $method)) { |
|
| 281 | - return $this->setAndGetKeyValue($option, $this->$method()); |
|
| 282 | - } |
|
| 283 | - } |
|
| 262 | + /** |
|
| 263 | + * @param string $option |
|
| 264 | + * @param string $fallback |
|
| 265 | + * @return void|string |
|
| 266 | + */ |
|
| 267 | + protected function getSchemaOptionValue($option, $fallback = 'post') |
|
| 268 | + { |
|
| 269 | + if (array_key_exists($option, $this->keyValues)) { |
|
| 270 | + return $this->keyValues[$option]; |
|
| 271 | + } |
|
| 272 | + $value = $this->getSchemaOption($option, $fallback); |
|
| 273 | + if ($value != $fallback) { |
|
| 274 | + return $this->setAndGetKeyValue($option, $value); |
|
| 275 | + } |
|
| 276 | + if (!is_single() && !is_page()) { |
|
| 277 | + return; |
|
| 278 | + } |
|
| 279 | + $method = Helper::buildMethodName($option, 'getThing'); |
|
| 280 | + if (method_exists($this, $method)) { |
|
| 281 | + return $this->setAndGetKeyValue($option, $this->$method()); |
|
| 282 | + } |
|
| 283 | + } |
|
| 284 | 284 | |
| 285 | - /** |
|
| 286 | - * @param string|null $type |
|
| 287 | - * @return mixed |
|
| 288 | - */ |
|
| 289 | - protected function getSchemaType($type = null) |
|
| 290 | - { |
|
| 291 | - if (!is_string($type)) { |
|
| 292 | - $type = $this->getSchemaOption('type', 'LocalBusiness'); |
|
| 293 | - } |
|
| 294 | - $className = Helper::buildClassName($type, 'Modules\Schema'); |
|
| 295 | - return class_exists($className) |
|
| 296 | - ? new $className() |
|
| 297 | - : new UnknownType($type); |
|
| 298 | - } |
|
| 285 | + /** |
|
| 286 | + * @param string|null $type |
|
| 287 | + * @return mixed |
|
| 288 | + */ |
|
| 289 | + protected function getSchemaType($type = null) |
|
| 290 | + { |
|
| 291 | + if (!is_string($type)) { |
|
| 292 | + $type = $this->getSchemaOption('type', 'LocalBusiness'); |
|
| 293 | + } |
|
| 294 | + $className = Helper::buildClassName($type, 'Modules\Schema'); |
|
| 295 | + return class_exists($className) |
|
| 296 | + ? new $className() |
|
| 297 | + : new UnknownType($type); |
|
| 298 | + } |
|
| 299 | 299 | |
| 300 | - /** |
|
| 301 | - * @return string |
|
| 302 | - */ |
|
| 303 | - protected function getThingDescription() |
|
| 304 | - { |
|
| 305 | - $post = get_post(); |
|
| 306 | - $text = Arr::get($post, 'post_excerpt'); |
|
| 307 | - if (empty($text)) { |
|
| 308 | - $text = Arr::get($post, 'post_content'); |
|
| 309 | - } |
|
| 310 | - if (function_exists('excerpt_remove_blocks')) { |
|
| 311 | - $text = excerpt_remove_blocks($text); |
|
| 312 | - } |
|
| 313 | - $text = strip_shortcodes($text); |
|
| 314 | - $text = wpautop($text); |
|
| 315 | - $text = wptexturize($text); |
|
| 316 | - $text = wp_strip_all_tags($text); |
|
| 317 | - $text = str_replace(']]>', ']]>', $text); |
|
| 318 | - return wp_trim_words($text, apply_filters('excerpt_length', 55)); |
|
| 319 | - } |
|
| 300 | + /** |
|
| 301 | + * @return string |
|
| 302 | + */ |
|
| 303 | + protected function getThingDescription() |
|
| 304 | + { |
|
| 305 | + $post = get_post(); |
|
| 306 | + $text = Arr::get($post, 'post_excerpt'); |
|
| 307 | + if (empty($text)) { |
|
| 308 | + $text = Arr::get($post, 'post_content'); |
|
| 309 | + } |
|
| 310 | + if (function_exists('excerpt_remove_blocks')) { |
|
| 311 | + $text = excerpt_remove_blocks($text); |
|
| 312 | + } |
|
| 313 | + $text = strip_shortcodes($text); |
|
| 314 | + $text = wpautop($text); |
|
| 315 | + $text = wptexturize($text); |
|
| 316 | + $text = wp_strip_all_tags($text); |
|
| 317 | + $text = str_replace(']]>', ']]>', $text); |
|
| 318 | + return wp_trim_words($text, apply_filters('excerpt_length', 55)); |
|
| 319 | + } |
|
| 320 | 320 | |
| 321 | - /** |
|
| 322 | - * @return string |
|
| 323 | - */ |
|
| 324 | - protected function getThingImage() |
|
| 325 | - { |
|
| 326 | - return (string) get_the_post_thumbnail_url(null, 'large'); |
|
| 327 | - } |
|
| 321 | + /** |
|
| 322 | + * @return string |
|
| 323 | + */ |
|
| 324 | + protected function getThingImage() |
|
| 325 | + { |
|
| 326 | + return (string) get_the_post_thumbnail_url(null, 'large'); |
|
| 327 | + } |
|
| 328 | 328 | |
| 329 | - /** |
|
| 330 | - * @return string |
|
| 331 | - */ |
|
| 332 | - protected function getThingName() |
|
| 333 | - { |
|
| 334 | - return get_the_title(); |
|
| 335 | - } |
|
| 329 | + /** |
|
| 330 | + * @return string |
|
| 331 | + */ |
|
| 332 | + protected function getThingName() |
|
| 333 | + { |
|
| 334 | + return get_the_title(); |
|
| 335 | + } |
|
| 336 | 336 | |
| 337 | - /** |
|
| 338 | - * @return string |
|
| 339 | - */ |
|
| 340 | - protected function getThingUrl() |
|
| 341 | - { |
|
| 342 | - return (string) get_the_permalink(); |
|
| 343 | - } |
|
| 337 | + /** |
|
| 338 | + * @return string |
|
| 339 | + */ |
|
| 340 | + protected function getThingUrl() |
|
| 341 | + { |
|
| 342 | + return (string) get_the_permalink(); |
|
| 343 | + } |
|
| 344 | 344 | |
| 345 | - /** |
|
| 346 | - * @param string $option |
|
| 347 | - * @param string $value |
|
| 348 | - * @return string |
|
| 349 | - */ |
|
| 350 | - protected function setAndGetKeyValue($option, $value) |
|
| 351 | - { |
|
| 352 | - $this->keyValues[$option] = $value; |
|
| 353 | - return $value; |
|
| 354 | - } |
|
| 345 | + /** |
|
| 346 | + * @param string $option |
|
| 347 | + * @param string $value |
|
| 348 | + * @return string |
|
| 349 | + */ |
|
| 350 | + protected function setAndGetKeyValue($option, $value) |
|
| 351 | + { |
|
| 352 | + $this->keyValues[$option] = $value; |
|
| 353 | + return $value; |
|
| 354 | + } |
|
| 355 | 355 | } |
@@ -35,19 +35,19 @@ discard block |
||
| 35 | 35 | /** |
| 36 | 36 | * @return array |
| 37 | 37 | */ |
| 38 | - public function build(array $args = [], $reviews = []) |
|
| 38 | + public function build( array $args = [], $reviews = [] ) |
|
| 39 | 39 | { |
| 40 | 40 | $this->args = $args; |
| 41 | 41 | $this->reviews = $reviews; |
| 42 | - $schema = $this->buildSummary($args); |
|
| 43 | - if (!empty($schema)) { |
|
| 42 | + $schema = $this->buildSummary( $args ); |
|
| 43 | + if( !empty($schema) ) { |
|
| 44 | 44 | $reviews = $this->buildReviews(); |
| 45 | - foreach ($reviews as &$review) { |
|
| 45 | + foreach( $reviews as &$review ) { |
|
| 46 | 46 | unset($review['@context']); |
| 47 | 47 | unset($review['itemReviewed']); |
| 48 | 48 | } |
| 49 | 49 | } |
| 50 | - if (!empty($reviews)) { |
|
| 50 | + if( !empty($reviews) ) { |
|
| 51 | 51 | $schema['review'] = $reviews; |
| 52 | 52 | } |
| 53 | 53 | return $schema; |
@@ -57,25 +57,25 @@ discard block |
||
| 57 | 57 | * @param array|null $args |
| 58 | 58 | * @return array |
| 59 | 59 | */ |
| 60 | - public function buildSummary($args = null, array $ratings = []) |
|
| 60 | + public function buildSummary( $args = null, array $ratings = [] ) |
|
| 61 | 61 | { |
| 62 | - if (is_array($args)) { |
|
| 62 | + if( is_array( $args ) ) { |
|
| 63 | 63 | $this->args = $args; |
| 64 | 64 | } |
| 65 | - $buildSummary = Helper::buildMethodName($this->getSchemaOptionValue('type'), 'buildSummaryFor'); |
|
| 66 | - if ($count = array_sum($this->getRatingCounts($ratings))) { |
|
| 67 | - $schema = method_exists($this, $buildSummary) |
|
| 65 | + $buildSummary = Helper::buildMethodName( $this->getSchemaOptionValue( 'type' ), 'buildSummaryFor' ); |
|
| 66 | + if( $count = array_sum( $this->getRatingCounts( $ratings ) ) ) { |
|
| 67 | + $schema = method_exists( $this, $buildSummary ) |
|
| 68 | 68 | ? $this->$buildSummary() |
| 69 | 69 | : $this->buildSummaryForCustom(); |
| 70 | 70 | $schema->aggregateRating( |
| 71 | - $this->getSchemaType('AggregateRating') |
|
| 72 | - ->ratingValue($this->getRatingValue()) |
|
| 73 | - ->reviewCount($count) |
|
| 74 | - ->bestRating(glsr()->constant('MAX_RATING', Rating::class)) |
|
| 75 | - ->worstRating(glsr()->constant('MIN_RATING', Rating::class)) |
|
| 71 | + $this->getSchemaType( 'AggregateRating' ) |
|
| 72 | + ->ratingValue( $this->getRatingValue() ) |
|
| 73 | + ->reviewCount( $count ) |
|
| 74 | + ->bestRating( glsr()->constant( 'MAX_RATING', Rating::class ) ) |
|
| 75 | + ->worstRating( glsr()->constant( 'MIN_RATING', Rating::class ) ) |
|
| 76 | 76 | ); |
| 77 | 77 | $schema = $schema->toArray(); |
| 78 | - return glsr()->filterArray('schema/'.$schema['@type'], $schema, $args); |
|
| 78 | + return glsr()->filterArray( 'schema/'.$schema['@type'], $schema, $args ); |
|
| 79 | 79 | } |
| 80 | 80 | return []; |
| 81 | 81 | } |
@@ -85,54 +85,54 @@ discard block |
||
| 85 | 85 | */ |
| 86 | 86 | public function render() |
| 87 | 87 | { |
| 88 | - if (empty($schemas = glsr()->retrieve('schemas', []))) { |
|
| 88 | + if( empty($schemas = glsr()->retrieve( 'schemas', [] )) ) { |
|
| 89 | 89 | return; |
| 90 | 90 | } |
| 91 | - printf('<script type="application/ld+json">%s</script>', json_encode( |
|
| 92 | - glsr()->filterArray('schema/all', $schemas), |
|
| 91 | + printf( '<script type="application/ld+json">%s</script>', json_encode( |
|
| 92 | + glsr()->filterArray( 'schema/all', $schemas ), |
|
| 93 | 93 | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES |
| 94 | - )); |
|
| 94 | + ) ); |
|
| 95 | 95 | } |
| 96 | 96 | |
| 97 | 97 | /** |
| 98 | 98 | * @return void |
| 99 | 99 | */ |
| 100 | - public function store(array $schema) |
|
| 100 | + public function store( array $schema ) |
|
| 101 | 101 | { |
| 102 | - if (empty($schema)) { |
|
| 102 | + if( empty($schema) ) { |
|
| 103 | 103 | return; |
| 104 | 104 | } |
| 105 | - $schemas = Arr::consolidate(glsr()->retrieve('schemas')); |
|
| 105 | + $schemas = Arr::consolidate( glsr()->retrieve( 'schemas' ) ); |
|
| 106 | 106 | $schemas[] = $schema; |
| 107 | - $schemas = array_map('unserialize', array_unique(array_map('serialize', $schemas))); |
|
| 108 | - glsr()->store('schemas', $schemas); |
|
| 107 | + $schemas = array_map( 'unserialize', array_unique( array_map( 'serialize', $schemas ) ) ); |
|
| 108 | + glsr()->store( 'schemas', $schemas ); |
|
| 109 | 109 | } |
| 110 | 110 | |
| 111 | 111 | /** |
| 112 | 112 | * @param Review $review |
| 113 | 113 | * @return array |
| 114 | 114 | */ |
| 115 | - protected function buildReview($review) |
|
| 115 | + protected function buildReview( $review ) |
|
| 116 | 116 | { |
| 117 | - $schema = $this->getSchemaType('Review') |
|
| 118 | - ->doIf(!in_array('title', $this->args['hide']), function ($schema) use ($review) { |
|
| 119 | - $schema->name($review->title); |
|
| 117 | + $schema = $this->getSchemaType( 'Review' ) |
|
| 118 | + ->doIf( !in_array( 'title', $this->args['hide'] ), function( $schema ) use ($review) { |
|
| 119 | + $schema->name( $review->title ); |
|
| 120 | 120 | }) |
| 121 | - ->doIf(!in_array('excerpt', $this->args['hide']), function ($schema) use ($review) { |
|
| 122 | - $schema->reviewBody($review->content); |
|
| 121 | + ->doIf( !in_array( 'excerpt', $this->args['hide'] ), function( $schema ) use ($review) { |
|
| 122 | + $schema->reviewBody( $review->content ); |
|
| 123 | 123 | }) |
| 124 | - ->datePublished((new DateTime($review->date))) |
|
| 125 | - ->author($this->getSchemaType('Person')->name($review->author)) |
|
| 126 | - ->itemReviewed($this->getSchemaType()->name($this->getSchemaOptionValue('name'))); |
|
| 127 | - if (!empty($review->rating)) { |
|
| 124 | + ->datePublished( (new DateTime( $review->date )) ) |
|
| 125 | + ->author( $this->getSchemaType( 'Person' )->name( $review->author ) ) |
|
| 126 | + ->itemReviewed( $this->getSchemaType()->name( $this->getSchemaOptionValue( 'name' ) ) ); |
|
| 127 | + if( !empty($review->rating) ) { |
|
| 128 | 128 | $schema->reviewRating( |
| 129 | - $this->getSchemaType('Rating') |
|
| 130 | - ->ratingValue($review->rating) |
|
| 131 | - ->bestRating(glsr()->constant('MAX_RATING', Rating::class)) |
|
| 132 | - ->worstRating(glsr()->constant('MIN_RATING', Rating::class)) |
|
| 129 | + $this->getSchemaType( 'Rating' ) |
|
| 130 | + ->ratingValue( $review->rating ) |
|
| 131 | + ->bestRating( glsr()->constant( 'MAX_RATING', Rating::class ) ) |
|
| 132 | + ->worstRating( glsr()->constant( 'MIN_RATING', Rating::class ) ) |
|
| 133 | 133 | ); |
| 134 | 134 | } |
| 135 | - return glsr()->filterArray('schema/review', $schema->toArray(), $review, $this->args); |
|
| 135 | + return glsr()->filterArray( 'schema/review', $schema->toArray(), $review, $this->args ); |
|
| 136 | 136 | } |
| 137 | 137 | |
| 138 | 138 | /** |
@@ -141,11 +141,11 @@ discard block |
||
| 141 | 141 | protected function buildReviews() |
| 142 | 142 | { |
| 143 | 143 | $reviews = []; |
| 144 | - foreach ($this->reviews as $review) { |
|
| 144 | + foreach( $this->reviews as $review ) { |
|
| 145 | 145 | // Only include critic reviews that have been directly produced by your site, not reviews from third-party sites or syndicated reviews. |
| 146 | 146 | // @see https://developers.google.com/search/docs/data-types/review |
| 147 | - if ('local' === $review->review_type) { |
|
| 148 | - $reviews[] = $this->buildReview($review); |
|
| 147 | + if( 'local' === $review->review_type ) { |
|
| 148 | + $reviews[] = $this->buildReview( $review ); |
|
| 149 | 149 | } |
| 150 | 150 | } |
| 151 | 151 | return $reviews; |
@@ -155,14 +155,14 @@ discard block |
||
| 155 | 155 | * @param mixed $schema |
| 156 | 156 | * @return mixed |
| 157 | 157 | */ |
| 158 | - protected function buildSchemaValues($schema, array $values = []) |
|
| 158 | + protected function buildSchemaValues( $schema, array $values = [] ) |
|
| 159 | 159 | { |
| 160 | - foreach ($values as $value) { |
|
| 161 | - $option = $this->getSchemaOptionValue($value); |
|
| 162 | - if (empty($option)) { |
|
| 160 | + foreach( $values as $value ) { |
|
| 161 | + $option = $this->getSchemaOptionValue( $value ); |
|
| 162 | + if( empty($option) ) { |
|
| 163 | 163 | continue; |
| 164 | 164 | } |
| 165 | - $schema->$value($option); |
|
| 165 | + $schema->$value( $option ); |
|
| 166 | 166 | } |
| 167 | 167 | return $schema; |
| 168 | 168 | } |
@@ -172,9 +172,9 @@ discard block |
||
| 172 | 172 | */ |
| 173 | 173 | protected function buildSummaryForCustom() |
| 174 | 174 | { |
| 175 | - return $this->buildSchemaValues($this->getSchemaType(), [ |
|
| 175 | + return $this->buildSchemaValues( $this->getSchemaType(), [ |
|
| 176 | 176 | 'description', 'image', 'name', 'url', |
| 177 | - ]); |
|
| 177 | + ] ); |
|
| 178 | 178 | } |
| 179 | 179 | |
| 180 | 180 | /** |
@@ -182,9 +182,9 @@ discard block |
||
| 182 | 182 | */ |
| 183 | 183 | protected function buildSummaryForLocalBusiness() |
| 184 | 184 | { |
| 185 | - return $this->buildSchemaValues($this->buildSummaryForCustom(), [ |
|
| 185 | + return $this->buildSchemaValues( $this->buildSummaryForCustom(), [ |
|
| 186 | 186 | 'address', 'priceRange', 'telephone', |
| 187 | - ]); |
|
| 187 | + ] ); |
|
| 188 | 188 | } |
| 189 | 189 | |
| 190 | 190 | /** |
@@ -192,25 +192,25 @@ discard block |
||
| 192 | 192 | */ |
| 193 | 193 | protected function buildSummaryForProduct() |
| 194 | 194 | { |
| 195 | - $offerType = $this->getSchemaOption('offerType', 'AggregateOffer'); |
|
| 196 | - $offers = $this->buildSchemaValues($this->getSchemaType($offerType), [ |
|
| 195 | + $offerType = $this->getSchemaOption( 'offerType', 'AggregateOffer' ); |
|
| 196 | + $offers = $this->buildSchemaValues( $this->getSchemaType( $offerType ), [ |
|
| 197 | 197 | 'highPrice', 'lowPrice', 'price', 'priceCurrency', |
| 198 | - ]); |
|
| 198 | + ] ); |
|
| 199 | 199 | return $this->buildSummaryForCustom() |
| 200 | - ->doIf(!empty($offers->getProperties()), function ($schema) use ($offers) { |
|
| 201 | - $schema->offers($offers); |
|
| 200 | + ->doIf( !empty($offers->getProperties()), function( $schema ) use ($offers) { |
|
| 201 | + $schema->offers( $offers ); |
|
| 202 | 202 | }) |
| 203 | - ->setProperty('@id', $this->getSchemaOptionValue('url').'#product'); |
|
| 203 | + ->setProperty( '@id', $this->getSchemaOptionValue( 'url' ).'#product' ); |
|
| 204 | 204 | } |
| 205 | 205 | |
| 206 | 206 | /** |
| 207 | 207 | * @return array |
| 208 | 208 | */ |
| 209 | - protected function getRatingCounts(array $ratings = []) |
|
| 209 | + protected function getRatingCounts( array $ratings = [] ) |
|
| 210 | 210 | { |
| 211 | - if (!isset($this->ratingCounts)) { |
|
| 211 | + if( !isset($this->ratingCounts) ) { |
|
| 212 | 212 | $this->ratingCounts = empty($ratings) |
| 213 | - ? glsr(RatingManager::class)->ratings($this->args) |
|
| 213 | + ? glsr( RatingManager::class )->ratings( $this->args ) |
|
| 214 | 214 | : $ratings; |
| 215 | 215 | } |
| 216 | 216 | return $this->ratingCounts; |
@@ -221,7 +221,7 @@ discard block |
||
| 221 | 221 | */ |
| 222 | 222 | protected function getRatingValue() |
| 223 | 223 | { |
| 224 | - return glsr(Rating::class)->average($this->getRatingCounts()); |
|
| 224 | + return glsr( Rating::class )->average( $this->getRatingCounts() ); |
|
| 225 | 225 | } |
| 226 | 226 | |
| 227 | 227 | /** |
@@ -229,15 +229,15 @@ discard block |
||
| 229 | 229 | * @param string $fallback |
| 230 | 230 | * @return string |
| 231 | 231 | */ |
| 232 | - protected function getSchemaOption($option, $fallback) |
|
| 232 | + protected function getSchemaOption( $option, $fallback ) |
|
| 233 | 233 | { |
| 234 | - $option = strtolower($option); |
|
| 235 | - if ($schemaOption = trim((string) get_post_meta(intval(get_the_ID()), 'schema_'.$option, true))) { |
|
| 234 | + $option = strtolower( $option ); |
|
| 235 | + if( $schemaOption = trim( (string)get_post_meta( intval( get_the_ID() ), 'schema_'.$option, true ) ) ) { |
|
| 236 | 236 | return $schemaOption; |
| 237 | 237 | } |
| 238 | - $setting = glsr(OptionManager::class)->get('settings.schema.'.$option); |
|
| 239 | - if (is_array($setting)) { |
|
| 240 | - return $this->getSchemaOptionDefault($setting, $fallback); |
|
| 238 | + $setting = glsr( OptionManager::class )->get( 'settings.schema.'.$option ); |
|
| 239 | + if( is_array( $setting ) ) { |
|
| 240 | + return $this->getSchemaOptionDefault( $setting, $fallback ); |
|
| 241 | 241 | } |
| 242 | 242 | return !empty($setting) |
| 243 | 243 | ? $setting |
@@ -248,12 +248,12 @@ discard block |
||
| 248 | 248 | * @param string $fallback |
| 249 | 249 | * @return string |
| 250 | 250 | */ |
| 251 | - protected function getSchemaOptionDefault(array $setting, $fallback) |
|
| 251 | + protected function getSchemaOptionDefault( array $setting, $fallback ) |
|
| 252 | 252 | { |
| 253 | - $setting = wp_parse_args($setting, [ |
|
| 253 | + $setting = wp_parse_args( $setting, [ |
|
| 254 | 254 | 'custom' => '', |
| 255 | 255 | 'default' => $fallback, |
| 256 | - ]); |
|
| 256 | + ] ); |
|
| 257 | 257 | return 'custom' != $setting['default'] |
| 258 | 258 | ? $setting['default'] |
| 259 | 259 | : $setting['custom']; |
@@ -264,21 +264,21 @@ discard block |
||
| 264 | 264 | * @param string $fallback |
| 265 | 265 | * @return void|string |
| 266 | 266 | */ |
| 267 | - protected function getSchemaOptionValue($option, $fallback = 'post') |
|
| 267 | + protected function getSchemaOptionValue( $option, $fallback = 'post' ) |
|
| 268 | 268 | { |
| 269 | - if (array_key_exists($option, $this->keyValues)) { |
|
| 269 | + if( array_key_exists( $option, $this->keyValues ) ) { |
|
| 270 | 270 | return $this->keyValues[$option]; |
| 271 | 271 | } |
| 272 | - $value = $this->getSchemaOption($option, $fallback); |
|
| 273 | - if ($value != $fallback) { |
|
| 274 | - return $this->setAndGetKeyValue($option, $value); |
|
| 272 | + $value = $this->getSchemaOption( $option, $fallback ); |
|
| 273 | + if( $value != $fallback ) { |
|
| 274 | + return $this->setAndGetKeyValue( $option, $value ); |
|
| 275 | 275 | } |
| 276 | - if (!is_single() && !is_page()) { |
|
| 276 | + if( !is_single() && !is_page() ) { |
|
| 277 | 277 | return; |
| 278 | 278 | } |
| 279 | - $method = Helper::buildMethodName($option, 'getThing'); |
|
| 280 | - if (method_exists($this, $method)) { |
|
| 281 | - return $this->setAndGetKeyValue($option, $this->$method()); |
|
| 279 | + $method = Helper::buildMethodName( $option, 'getThing' ); |
|
| 280 | + if( method_exists( $this, $method ) ) { |
|
| 281 | + return $this->setAndGetKeyValue( $option, $this->$method() ); |
|
| 282 | 282 | } |
| 283 | 283 | } |
| 284 | 284 | |
@@ -286,15 +286,15 @@ discard block |
||
| 286 | 286 | * @param string|null $type |
| 287 | 287 | * @return mixed |
| 288 | 288 | */ |
| 289 | - protected function getSchemaType($type = null) |
|
| 289 | + protected function getSchemaType( $type = null ) |
|
| 290 | 290 | { |
| 291 | - if (!is_string($type)) { |
|
| 292 | - $type = $this->getSchemaOption('type', 'LocalBusiness'); |
|
| 291 | + if( !is_string( $type ) ) { |
|
| 292 | + $type = $this->getSchemaOption( 'type', 'LocalBusiness' ); |
|
| 293 | 293 | } |
| 294 | - $className = Helper::buildClassName($type, 'Modules\Schema'); |
|
| 295 | - return class_exists($className) |
|
| 294 | + $className = Helper::buildClassName( $type, 'Modules\Schema' ); |
|
| 295 | + return class_exists( $className ) |
|
| 296 | 296 | ? new $className() |
| 297 | - : new UnknownType($type); |
|
| 297 | + : new UnknownType( $type ); |
|
| 298 | 298 | } |
| 299 | 299 | |
| 300 | 300 | /** |
@@ -303,19 +303,19 @@ discard block |
||
| 303 | 303 | protected function getThingDescription() |
| 304 | 304 | { |
| 305 | 305 | $post = get_post(); |
| 306 | - $text = Arr::get($post, 'post_excerpt'); |
|
| 307 | - if (empty($text)) { |
|
| 308 | - $text = Arr::get($post, 'post_content'); |
|
| 306 | + $text = Arr::get( $post, 'post_excerpt' ); |
|
| 307 | + if( empty($text) ) { |
|
| 308 | + $text = Arr::get( $post, 'post_content' ); |
|
| 309 | 309 | } |
| 310 | - if (function_exists('excerpt_remove_blocks')) { |
|
| 311 | - $text = excerpt_remove_blocks($text); |
|
| 310 | + if( function_exists( 'excerpt_remove_blocks' ) ) { |
|
| 311 | + $text = excerpt_remove_blocks( $text ); |
|
| 312 | 312 | } |
| 313 | - $text = strip_shortcodes($text); |
|
| 314 | - $text = wpautop($text); |
|
| 315 | - $text = wptexturize($text); |
|
| 316 | - $text = wp_strip_all_tags($text); |
|
| 317 | - $text = str_replace(']]>', ']]>', $text); |
|
| 318 | - return wp_trim_words($text, apply_filters('excerpt_length', 55)); |
|
| 313 | + $text = strip_shortcodes( $text ); |
|
| 314 | + $text = wpautop( $text ); |
|
| 315 | + $text = wptexturize( $text ); |
|
| 316 | + $text = wp_strip_all_tags( $text ); |
|
| 317 | + $text = str_replace( ']]>', ']]>', $text ); |
|
| 318 | + return wp_trim_words( $text, apply_filters( 'excerpt_length', 55 ) ); |
|
| 319 | 319 | } |
| 320 | 320 | |
| 321 | 321 | /** |
@@ -323,7 +323,7 @@ discard block |
||
| 323 | 323 | */ |
| 324 | 324 | protected function getThingImage() |
| 325 | 325 | { |
| 326 | - return (string) get_the_post_thumbnail_url(null, 'large'); |
|
| 326 | + return (string)get_the_post_thumbnail_url( null, 'large' ); |
|
| 327 | 327 | } |
| 328 | 328 | |
| 329 | 329 | /** |
@@ -339,7 +339,7 @@ discard block |
||
| 339 | 339 | */ |
| 340 | 340 | protected function getThingUrl() |
| 341 | 341 | { |
| 342 | - return (string) get_the_permalink(); |
|
| 342 | + return (string)get_the_permalink(); |
|
| 343 | 343 | } |
| 344 | 344 | |
| 345 | 345 | /** |
@@ -347,7 +347,7 @@ discard block |
||
| 347 | 347 | * @param string $value |
| 348 | 348 | * @return string |
| 349 | 349 | */ |
| 350 | - protected function setAndGetKeyValue($option, $value) |
|
| 350 | + protected function setAndGetKeyValue( $option, $value ) |
|
| 351 | 351 | { |
| 352 | 352 | $this->keyValues[$option] = $value; |
| 353 | 353 | return $value; |
@@ -21,377 +21,377 @@ |
||
| 21 | 21 | */ |
| 22 | 22 | class Console |
| 23 | 23 | { |
| 24 | - const DEBUG = 0; // Detailed debug information |
|
| 25 | - const INFO = 1; // Interesting events |
|
| 26 | - const NOTICE = 2; // Normal but significant events |
|
| 27 | - const WARNING = 4; // Exceptional occurrences that are not errors |
|
| 28 | - const ERROR = 8; // Runtime errors that do not require immediate action |
|
| 29 | - const CRITICAL = 16; // Critical conditions |
|
| 30 | - const ALERT = 32; // Action must be taken immediately |
|
| 31 | - const EMERGENCY = 64; // System is unusable |
|
| 24 | + const DEBUG = 0; // Detailed debug information |
|
| 25 | + const INFO = 1; // Interesting events |
|
| 26 | + const NOTICE = 2; // Normal but significant events |
|
| 27 | + const WARNING = 4; // Exceptional occurrences that are not errors |
|
| 28 | + const ERROR = 8; // Runtime errors that do not require immediate action |
|
| 29 | + const CRITICAL = 16; // Critical conditions |
|
| 30 | + const ALERT = 32; // Action must be taken immediately |
|
| 31 | + const EMERGENCY = 64; // System is unusable |
|
| 32 | 32 | |
| 33 | - protected $file; |
|
| 34 | - protected $log; |
|
| 35 | - protected $logOnceKey = 'glsr_log_once'; |
|
| 33 | + protected $file; |
|
| 34 | + protected $log; |
|
| 35 | + protected $logOnceKey = 'glsr_log_once'; |
|
| 36 | 36 | |
| 37 | - public function __construct() |
|
| 38 | - { |
|
| 39 | - $this->setLogFile(); |
|
| 40 | - $this->reset(); |
|
| 41 | - } |
|
| 37 | + public function __construct() |
|
| 38 | + { |
|
| 39 | + $this->setLogFile(); |
|
| 40 | + $this->reset(); |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - public function __call($method, $args) |
|
| 44 | - { |
|
| 45 | - $constant = 'static::'.strtoupper($method); |
|
| 46 | - if (defined($constant)) { |
|
| 47 | - $args = Arr::prepend($args, constant($constant)); |
|
| 48 | - return call_user_func_array([$this, 'log'], array_slice($args, 0, 3)); |
|
| 49 | - } |
|
| 50 | - throw new BadMethodCallException("Method [$method] does not exist."); |
|
| 51 | - } |
|
| 43 | + public function __call($method, $args) |
|
| 44 | + { |
|
| 45 | + $constant = 'static::'.strtoupper($method); |
|
| 46 | + if (defined($constant)) { |
|
| 47 | + $args = Arr::prepend($args, constant($constant)); |
|
| 48 | + return call_user_func_array([$this, 'log'], array_slice($args, 0, 3)); |
|
| 49 | + } |
|
| 50 | + throw new BadMethodCallException("Method [$method] does not exist."); |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * @return string |
|
| 55 | - */ |
|
| 56 | - public function __toString() |
|
| 57 | - { |
|
| 58 | - return $this->get(); |
|
| 59 | - } |
|
| 53 | + /** |
|
| 54 | + * @return string |
|
| 55 | + */ |
|
| 56 | + public function __toString() |
|
| 57 | + { |
|
| 58 | + return $this->get(); |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * @return void |
|
| 63 | - */ |
|
| 64 | - public function clear() |
|
| 65 | - { |
|
| 66 | - $this->log = ''; |
|
| 67 | - file_put_contents($this->file, $this->log); |
|
| 68 | - } |
|
| 61 | + /** |
|
| 62 | + * @return void |
|
| 63 | + */ |
|
| 64 | + public function clear() |
|
| 65 | + { |
|
| 66 | + $this->log = ''; |
|
| 67 | + file_put_contents($this->file, $this->log); |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - /** |
|
| 71 | - * @return string |
|
| 72 | - */ |
|
| 73 | - public function get() |
|
| 74 | - { |
|
| 75 | - return empty($this->log) |
|
| 76 | - ? _x('Console is empty', 'admin-text', 'site-reviews') |
|
| 77 | - : $this->log; |
|
| 78 | - } |
|
| 70 | + /** |
|
| 71 | + * @return string |
|
| 72 | + */ |
|
| 73 | + public function get() |
|
| 74 | + { |
|
| 75 | + return empty($this->log) |
|
| 76 | + ? _x('Console is empty', 'admin-text', 'site-reviews') |
|
| 77 | + : $this->log; |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - /** |
|
| 81 | - * @return int |
|
| 82 | - */ |
|
| 83 | - public function getLevel() |
|
| 84 | - { |
|
| 85 | - return glsr()->filterInt('console/level', static::INFO); |
|
| 86 | - } |
|
| 80 | + /** |
|
| 81 | + * @return int |
|
| 82 | + */ |
|
| 83 | + public function getLevel() |
|
| 84 | + { |
|
| 85 | + return glsr()->filterInt('console/level', static::INFO); |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | - /** |
|
| 89 | - * @return array |
|
| 90 | - */ |
|
| 91 | - public function getLevels() |
|
| 92 | - { |
|
| 93 | - $constants = (new ReflectionClass(__CLASS__))->getConstants(); |
|
| 94 | - return array_map('strtolower', array_flip($constants)); |
|
| 95 | - } |
|
| 88 | + /** |
|
| 89 | + * @return array |
|
| 90 | + */ |
|
| 91 | + public function getLevels() |
|
| 92 | + { |
|
| 93 | + $constants = (new ReflectionClass(__CLASS__))->getConstants(); |
|
| 94 | + return array_map('strtolower', array_flip($constants)); |
|
| 95 | + } |
|
| 96 | 96 | |
| 97 | - /** |
|
| 98 | - * @return string |
|
| 99 | - */ |
|
| 100 | - public function humanLevel() |
|
| 101 | - { |
|
| 102 | - $level = $this->getLevel(); |
|
| 103 | - return sprintf('%s (%d)', strtoupper(Arr::get($this->getLevels(), $level, 'unknown')), $level); |
|
| 104 | - } |
|
| 97 | + /** |
|
| 98 | + * @return string |
|
| 99 | + */ |
|
| 100 | + public function humanLevel() |
|
| 101 | + { |
|
| 102 | + $level = $this->getLevel(); |
|
| 103 | + return sprintf('%s (%d)', strtoupper(Arr::get($this->getLevels(), $level, 'unknown')), $level); |
|
| 104 | + } |
|
| 105 | 105 | |
| 106 | - /** |
|
| 107 | - * @param string|null $valueIfEmpty |
|
| 108 | - * @return string |
|
| 109 | - */ |
|
| 110 | - public function humanSize($valueIfEmpty = null) |
|
| 111 | - { |
|
| 112 | - $bytes = $this->size(); |
|
| 113 | - if (empty($bytes) && is_string($valueIfEmpty)) { |
|
| 114 | - return $valueIfEmpty; |
|
| 115 | - } |
|
| 116 | - $exponent = floor(log(max($bytes, 1), 1024)); |
|
| 117 | - return round($bytes / pow(1024, $exponent), 2).' '.['bytes', 'KB', 'MB', 'GB'][$exponent]; |
|
| 118 | - } |
|
| 106 | + /** |
|
| 107 | + * @param string|null $valueIfEmpty |
|
| 108 | + * @return string |
|
| 109 | + */ |
|
| 110 | + public function humanSize($valueIfEmpty = null) |
|
| 111 | + { |
|
| 112 | + $bytes = $this->size(); |
|
| 113 | + if (empty($bytes) && is_string($valueIfEmpty)) { |
|
| 114 | + return $valueIfEmpty; |
|
| 115 | + } |
|
| 116 | + $exponent = floor(log(max($bytes, 1), 1024)); |
|
| 117 | + return round($bytes / pow(1024, $exponent), 2).' '.['bytes', 'KB', 'MB', 'GB'][$exponent]; |
|
| 118 | + } |
|
| 119 | 119 | |
| 120 | - /** |
|
| 121 | - * @param int $level |
|
| 122 | - * @param mixed $message |
|
| 123 | - * @param array $context |
|
| 124 | - * @param string $backtraceLine |
|
| 125 | - * @return static |
|
| 126 | - */ |
|
| 127 | - public function log($level, $message, $context = [], $backtraceLine = '') |
|
| 128 | - { |
|
| 129 | - if (empty($backtraceLine)) { |
|
| 130 | - $backtraceLine = $this->getBacktraceLine(); |
|
| 131 | - } |
|
| 132 | - if ($this->canLogEntry($level, $backtraceLine)) { |
|
| 133 | - $levelName = Arr::get($this->getLevels(), $level); |
|
| 134 | - $context = Arr::consolidate($context); |
|
| 135 | - $backtraceLine = $this->normalizeBacktraceLine($backtraceLine); |
|
| 136 | - $message = $this->interpolate($message, $context); |
|
| 137 | - $entry = $this->buildLogEntry($levelName, $message, $backtraceLine); |
|
| 138 | - file_put_contents($this->file, $entry.PHP_EOL, FILE_APPEND | LOCK_EX); |
|
| 139 | - apply_filters('console', $message, $levelName, $backtraceLine); // Show in Blackbar plugin if installed |
|
| 140 | - $this->reset(); |
|
| 141 | - } |
|
| 142 | - return $this; |
|
| 143 | - } |
|
| 120 | + /** |
|
| 121 | + * @param int $level |
|
| 122 | + * @param mixed $message |
|
| 123 | + * @param array $context |
|
| 124 | + * @param string $backtraceLine |
|
| 125 | + * @return static |
|
| 126 | + */ |
|
| 127 | + public function log($level, $message, $context = [], $backtraceLine = '') |
|
| 128 | + { |
|
| 129 | + if (empty($backtraceLine)) { |
|
| 130 | + $backtraceLine = $this->getBacktraceLine(); |
|
| 131 | + } |
|
| 132 | + if ($this->canLogEntry($level, $backtraceLine)) { |
|
| 133 | + $levelName = Arr::get($this->getLevels(), $level); |
|
| 134 | + $context = Arr::consolidate($context); |
|
| 135 | + $backtraceLine = $this->normalizeBacktraceLine($backtraceLine); |
|
| 136 | + $message = $this->interpolate($message, $context); |
|
| 137 | + $entry = $this->buildLogEntry($levelName, $message, $backtraceLine); |
|
| 138 | + file_put_contents($this->file, $entry.PHP_EOL, FILE_APPEND | LOCK_EX); |
|
| 139 | + apply_filters('console', $message, $levelName, $backtraceLine); // Show in Blackbar plugin if installed |
|
| 140 | + $this->reset(); |
|
| 141 | + } |
|
| 142 | + return $this; |
|
| 143 | + } |
|
| 144 | 144 | |
| 145 | - /** |
|
| 146 | - * @return void |
|
| 147 | - */ |
|
| 148 | - public function logOnce() |
|
| 149 | - { |
|
| 150 | - $once = Arr::consolidate(glsr()->{$this->logOnceKey}); |
|
| 151 | - $levels = $this->getLevels(); |
|
| 152 | - foreach ($once as $entry) { |
|
| 153 | - $levelName = Arr::get($entry, 'level'); |
|
| 154 | - if (!in_array($levelName, $levels)) { |
|
| 155 | - continue; |
|
| 156 | - } |
|
| 157 | - $level = Arr::get(array_flip($levels), $levelName); |
|
| 158 | - $message = Arr::get($entry, 'message'); |
|
| 159 | - $backtraceLine = Arr::get($entry, 'backtrace'); |
|
| 160 | - $this->log($level, $message, [], $backtraceLine); |
|
| 161 | - } |
|
| 162 | - glsr()->{$this->logOnceKey} = []; |
|
| 163 | - } |
|
| 145 | + /** |
|
| 146 | + * @return void |
|
| 147 | + */ |
|
| 148 | + public function logOnce() |
|
| 149 | + { |
|
| 150 | + $once = Arr::consolidate(glsr()->{$this->logOnceKey}); |
|
| 151 | + $levels = $this->getLevels(); |
|
| 152 | + foreach ($once as $entry) { |
|
| 153 | + $levelName = Arr::get($entry, 'level'); |
|
| 154 | + if (!in_array($levelName, $levels)) { |
|
| 155 | + continue; |
|
| 156 | + } |
|
| 157 | + $level = Arr::get(array_flip($levels), $levelName); |
|
| 158 | + $message = Arr::get($entry, 'message'); |
|
| 159 | + $backtraceLine = Arr::get($entry, 'backtrace'); |
|
| 160 | + $this->log($level, $message, [], $backtraceLine); |
|
| 161 | + } |
|
| 162 | + glsr()->{$this->logOnceKey} = []; |
|
| 163 | + } |
|
| 164 | 164 | |
| 165 | - /** |
|
| 166 | - * @param string $levelName |
|
| 167 | - * @param string $handle |
|
| 168 | - * @param mixed $data |
|
| 169 | - * @return void |
|
| 170 | - */ |
|
| 171 | - public function once($levelName, $handle, $data) |
|
| 172 | - { |
|
| 173 | - $once = Arr::consolidate(glsr()->{$this->logOnceKey}); |
|
| 174 | - $filtered = array_filter($once, function ($entry) use ($levelName, $handle) { |
|
| 175 | - return Arr::get($entry, 'level') == $levelName |
|
| 176 | - && Arr::get($entry, 'handle') == $handle; |
|
| 177 | - }); |
|
| 178 | - if (!empty($filtered)) { |
|
| 179 | - return; |
|
| 180 | - } |
|
| 181 | - $once[] = [ |
|
| 182 | - 'backtrace' => $this->getBacktraceLineFromData($data), |
|
| 183 | - 'handle' => $handle, |
|
| 184 | - 'level' => $levelName, |
|
| 185 | - 'message' => '[RECURRING] '.$this->getMessageFromData($data), |
|
| 186 | - ]; |
|
| 187 | - glsr()->{$this->logOnceKey} = $once; |
|
| 188 | - } |
|
| 165 | + /** |
|
| 166 | + * @param string $levelName |
|
| 167 | + * @param string $handle |
|
| 168 | + * @param mixed $data |
|
| 169 | + * @return void |
|
| 170 | + */ |
|
| 171 | + public function once($levelName, $handle, $data) |
|
| 172 | + { |
|
| 173 | + $once = Arr::consolidate(glsr()->{$this->logOnceKey}); |
|
| 174 | + $filtered = array_filter($once, function ($entry) use ($levelName, $handle) { |
|
| 175 | + return Arr::get($entry, 'level') == $levelName |
|
| 176 | + && Arr::get($entry, 'handle') == $handle; |
|
| 177 | + }); |
|
| 178 | + if (!empty($filtered)) { |
|
| 179 | + return; |
|
| 180 | + } |
|
| 181 | + $once[] = [ |
|
| 182 | + 'backtrace' => $this->getBacktraceLineFromData($data), |
|
| 183 | + 'handle' => $handle, |
|
| 184 | + 'level' => $levelName, |
|
| 185 | + 'message' => '[RECURRING] '.$this->getMessageFromData($data), |
|
| 186 | + ]; |
|
| 187 | + glsr()->{$this->logOnceKey} = $once; |
|
| 188 | + } |
|
| 189 | 189 | |
| 190 | - /** |
|
| 191 | - * @return int |
|
| 192 | - */ |
|
| 193 | - public function size() |
|
| 194 | - { |
|
| 195 | - return file_exists($this->file) |
|
| 196 | - ? filesize($this->file) |
|
| 197 | - : 0; |
|
| 198 | - } |
|
| 190 | + /** |
|
| 191 | + * @return int |
|
| 192 | + */ |
|
| 193 | + public function size() |
|
| 194 | + { |
|
| 195 | + return file_exists($this->file) |
|
| 196 | + ? filesize($this->file) |
|
| 197 | + : 0; |
|
| 198 | + } |
|
| 199 | 199 | |
| 200 | - /** |
|
| 201 | - * @param array $backtrace |
|
| 202 | - * @param int $index |
|
| 203 | - * @return string |
|
| 204 | - */ |
|
| 205 | - protected function buildBacktraceLine($backtrace, $index) |
|
| 206 | - { |
|
| 207 | - return sprintf('%s:%s', |
|
| 208 | - Arr::get($backtrace, $index.'.file'), // realpath |
|
| 209 | - Arr::get($backtrace, $index.'.line') |
|
| 210 | - ); |
|
| 211 | - } |
|
| 200 | + /** |
|
| 201 | + * @param array $backtrace |
|
| 202 | + * @param int $index |
|
| 203 | + * @return string |
|
| 204 | + */ |
|
| 205 | + protected function buildBacktraceLine($backtrace, $index) |
|
| 206 | + { |
|
| 207 | + return sprintf('%s:%s', |
|
| 208 | + Arr::get($backtrace, $index.'.file'), // realpath |
|
| 209 | + Arr::get($backtrace, $index.'.line') |
|
| 210 | + ); |
|
| 211 | + } |
|
| 212 | 212 | |
| 213 | - /** |
|
| 214 | - * @param string $levelName |
|
| 215 | - * @param mixed $message |
|
| 216 | - * @param string $backtraceLine |
|
| 217 | - * @return string |
|
| 218 | - */ |
|
| 219 | - protected function buildLogEntry($levelName, $message, $backtraceLine = '') |
|
| 220 | - { |
|
| 221 | - return sprintf('[%s] %s [%s] %s', |
|
| 222 | - current_time('mysql'), |
|
| 223 | - strtoupper($levelName), |
|
| 224 | - $backtraceLine, |
|
| 225 | - $message |
|
| 226 | - ); |
|
| 227 | - } |
|
| 213 | + /** |
|
| 214 | + * @param string $levelName |
|
| 215 | + * @param mixed $message |
|
| 216 | + * @param string $backtraceLine |
|
| 217 | + * @return string |
|
| 218 | + */ |
|
| 219 | + protected function buildLogEntry($levelName, $message, $backtraceLine = '') |
|
| 220 | + { |
|
| 221 | + return sprintf('[%s] %s [%s] %s', |
|
| 222 | + current_time('mysql'), |
|
| 223 | + strtoupper($levelName), |
|
| 224 | + $backtraceLine, |
|
| 225 | + $message |
|
| 226 | + ); |
|
| 227 | + } |
|
| 228 | 228 | |
| 229 | - /** |
|
| 230 | - * @param int $level |
|
| 231 | - * @return bool |
|
| 232 | - */ |
|
| 233 | - protected function canLogEntry($level, $backtraceLine) |
|
| 234 | - { |
|
| 235 | - $levelExists = array_key_exists($level, $this->getLevels()); |
|
| 236 | - if (!Str::contains($backtraceLine, glsr()->path())) { |
|
| 237 | - return $levelExists; // ignore level restriction if triggered outside of the plugin |
|
| 238 | - } |
|
| 239 | - return $levelExists && $level >= $this->getLevel(); |
|
| 240 | - } |
|
| 229 | + /** |
|
| 230 | + * @param int $level |
|
| 231 | + * @return bool |
|
| 232 | + */ |
|
| 233 | + protected function canLogEntry($level, $backtraceLine) |
|
| 234 | + { |
|
| 235 | + $levelExists = array_key_exists($level, $this->getLevels()); |
|
| 236 | + if (!Str::contains($backtraceLine, glsr()->path())) { |
|
| 237 | + return $levelExists; // ignore level restriction if triggered outside of the plugin |
|
| 238 | + } |
|
| 239 | + return $levelExists && $level >= $this->getLevel(); |
|
| 240 | + } |
|
| 241 | 241 | |
| 242 | - /** |
|
| 243 | - * @return void|string |
|
| 244 | - */ |
|
| 245 | - protected function getBacktraceLine() |
|
| 246 | - { |
|
| 247 | - $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 6); |
|
| 248 | - $search = array_search('glsr_log', wp_list_pluck($backtrace, 'function')); |
|
| 249 | - if (false !== $search) { |
|
| 250 | - return $this->buildBacktraceLine($backtrace, (int) $search); |
|
| 251 | - } |
|
| 252 | - $search = array_search('log', wp_list_pluck($backtrace, 'function')); |
|
| 253 | - if (false !== $search) { |
|
| 254 | - $index = '{closure}' == Arr::get($backtrace, ($search + 2).'.function') |
|
| 255 | - ? $search + 4 |
|
| 256 | - : $search + 1; |
|
| 257 | - return $this->buildBacktraceLine($backtrace, $index); |
|
| 258 | - } |
|
| 259 | - return 'Unknown'; |
|
| 260 | - } |
|
| 242 | + /** |
|
| 243 | + * @return void|string |
|
| 244 | + */ |
|
| 245 | + protected function getBacktraceLine() |
|
| 246 | + { |
|
| 247 | + $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 6); |
|
| 248 | + $search = array_search('glsr_log', wp_list_pluck($backtrace, 'function')); |
|
| 249 | + if (false !== $search) { |
|
| 250 | + return $this->buildBacktraceLine($backtrace, (int) $search); |
|
| 251 | + } |
|
| 252 | + $search = array_search('log', wp_list_pluck($backtrace, 'function')); |
|
| 253 | + if (false !== $search) { |
|
| 254 | + $index = '{closure}' == Arr::get($backtrace, ($search + 2).'.function') |
|
| 255 | + ? $search + 4 |
|
| 256 | + : $search + 1; |
|
| 257 | + return $this->buildBacktraceLine($backtrace, $index); |
|
| 258 | + } |
|
| 259 | + return 'Unknown'; |
|
| 260 | + } |
|
| 261 | 261 | |
| 262 | - /** |
|
| 263 | - * @param mixed $data |
|
| 264 | - * @return string |
|
| 265 | - */ |
|
| 266 | - protected function getBacktraceLineFromData($data) |
|
| 267 | - { |
|
| 268 | - $backtrace = $data instanceof Throwable |
|
| 269 | - ? $data->getTrace() |
|
| 270 | - : debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1); |
|
| 271 | - return $this->buildBacktraceLine($backtrace, 0); |
|
| 272 | - } |
|
| 262 | + /** |
|
| 263 | + * @param mixed $data |
|
| 264 | + * @return string |
|
| 265 | + */ |
|
| 266 | + protected function getBacktraceLineFromData($data) |
|
| 267 | + { |
|
| 268 | + $backtrace = $data instanceof Throwable |
|
| 269 | + ? $data->getTrace() |
|
| 270 | + : debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1); |
|
| 271 | + return $this->buildBacktraceLine($backtrace, 0); |
|
| 272 | + } |
|
| 273 | 273 | |
| 274 | - /** |
|
| 275 | - * @param mixed $data |
|
| 276 | - * @return string |
|
| 277 | - */ |
|
| 278 | - protected function getMessageFromData($data) |
|
| 279 | - { |
|
| 280 | - return $data instanceof Throwable |
|
| 281 | - ? $this->normalizeThrowableMessage($data->getMessage()) |
|
| 282 | - : print_r($data, 1); |
|
| 283 | - } |
|
| 274 | + /** |
|
| 275 | + * @param mixed $data |
|
| 276 | + * @return string |
|
| 277 | + */ |
|
| 278 | + protected function getMessageFromData($data) |
|
| 279 | + { |
|
| 280 | + return $data instanceof Throwable |
|
| 281 | + ? $this->normalizeThrowableMessage($data->getMessage()) |
|
| 282 | + : print_r($data, 1); |
|
| 283 | + } |
|
| 284 | 284 | |
| 285 | - /** |
|
| 286 | - * Interpolates context values into the message placeholders. |
|
| 287 | - * @param mixed $message |
|
| 288 | - * @param array $context |
|
| 289 | - * @return string |
|
| 290 | - */ |
|
| 291 | - protected function interpolate($message, $context = []) |
|
| 292 | - { |
|
| 293 | - if ($this->isObjectOrArray($message) || !is_array($context)) { |
|
| 294 | - return print_r($message, true); |
|
| 295 | - } |
|
| 296 | - $replace = []; |
|
| 297 | - foreach ($context as $key => $value) { |
|
| 298 | - $replace['{'.$key.'}'] = $this->normalizeValue($value); |
|
| 299 | - } |
|
| 300 | - return strtr($message, $replace); |
|
| 301 | - } |
|
| 285 | + /** |
|
| 286 | + * Interpolates context values into the message placeholders. |
|
| 287 | + * @param mixed $message |
|
| 288 | + * @param array $context |
|
| 289 | + * @return string |
|
| 290 | + */ |
|
| 291 | + protected function interpolate($message, $context = []) |
|
| 292 | + { |
|
| 293 | + if ($this->isObjectOrArray($message) || !is_array($context)) { |
|
| 294 | + return print_r($message, true); |
|
| 295 | + } |
|
| 296 | + $replace = []; |
|
| 297 | + foreach ($context as $key => $value) { |
|
| 298 | + $replace['{'.$key.'}'] = $this->normalizeValue($value); |
|
| 299 | + } |
|
| 300 | + return strtr($message, $replace); |
|
| 301 | + } |
|
| 302 | 302 | |
| 303 | - /** |
|
| 304 | - * @param mixed $value |
|
| 305 | - * @return bool |
|
| 306 | - */ |
|
| 307 | - protected function isObjectOrArray($value) |
|
| 308 | - { |
|
| 309 | - return is_object($value) || is_array($value); |
|
| 310 | - } |
|
| 303 | + /** |
|
| 304 | + * @param mixed $value |
|
| 305 | + * @return bool |
|
| 306 | + */ |
|
| 307 | + protected function isObjectOrArray($value) |
|
| 308 | + { |
|
| 309 | + return is_object($value) || is_array($value); |
|
| 310 | + } |
|
| 311 | 311 | |
| 312 | - /** |
|
| 313 | - * @param string $backtraceLine |
|
| 314 | - * @return string |
|
| 315 | - */ |
|
| 316 | - protected function normalizeBacktraceLine($backtraceLine) |
|
| 317 | - { |
|
| 318 | - $search = [ |
|
| 319 | - glsr()->path('plugin/'), |
|
| 320 | - glsr()->path('plugin/', false), |
|
| 321 | - trailingslashit(glsr()->path()), |
|
| 322 | - trailingslashit(glsr()->path('', false)), |
|
| 323 | - WP_CONTENT_DIR, |
|
| 324 | - ABSPATH, |
|
| 325 | - ]; |
|
| 326 | - return str_replace(array_unique($search), '', $backtraceLine); |
|
| 327 | - } |
|
| 312 | + /** |
|
| 313 | + * @param string $backtraceLine |
|
| 314 | + * @return string |
|
| 315 | + */ |
|
| 316 | + protected function normalizeBacktraceLine($backtraceLine) |
|
| 317 | + { |
|
| 318 | + $search = [ |
|
| 319 | + glsr()->path('plugin/'), |
|
| 320 | + glsr()->path('plugin/', false), |
|
| 321 | + trailingslashit(glsr()->path()), |
|
| 322 | + trailingslashit(glsr()->path('', false)), |
|
| 323 | + WP_CONTENT_DIR, |
|
| 324 | + ABSPATH, |
|
| 325 | + ]; |
|
| 326 | + return str_replace(array_unique($search), '', $backtraceLine); |
|
| 327 | + } |
|
| 328 | 328 | |
| 329 | - /** |
|
| 330 | - * @param string $message |
|
| 331 | - * @return string |
|
| 332 | - */ |
|
| 333 | - protected function normalizeThrowableMessage($message) |
|
| 334 | - { |
|
| 335 | - $calledIn = strpos($message, ', called in'); |
|
| 336 | - return false !== $calledIn |
|
| 337 | - ? substr($message, 0, $calledIn) |
|
| 338 | - : $message; |
|
| 339 | - } |
|
| 329 | + /** |
|
| 330 | + * @param string $message |
|
| 331 | + * @return string |
|
| 332 | + */ |
|
| 333 | + protected function normalizeThrowableMessage($message) |
|
| 334 | + { |
|
| 335 | + $calledIn = strpos($message, ', called in'); |
|
| 336 | + return false !== $calledIn |
|
| 337 | + ? substr($message, 0, $calledIn) |
|
| 338 | + : $message; |
|
| 339 | + } |
|
| 340 | 340 | |
| 341 | - /** |
|
| 342 | - * @param mixed $value |
|
| 343 | - * @return string |
|
| 344 | - */ |
|
| 345 | - protected function normalizeValue($value) |
|
| 346 | - { |
|
| 347 | - if ($value instanceof DateTime) { |
|
| 348 | - $value = $value->format('Y-m-d H:i:s'); |
|
| 349 | - } elseif ($this->isObjectOrArray($value)) { |
|
| 350 | - $value = json_encode($value); |
|
| 351 | - } |
|
| 352 | - return (string) $value; |
|
| 353 | - } |
|
| 341 | + /** |
|
| 342 | + * @param mixed $value |
|
| 343 | + * @return string |
|
| 344 | + */ |
|
| 345 | + protected function normalizeValue($value) |
|
| 346 | + { |
|
| 347 | + if ($value instanceof DateTime) { |
|
| 348 | + $value = $value->format('Y-m-d H:i:s'); |
|
| 349 | + } elseif ($this->isObjectOrArray($value)) { |
|
| 350 | + $value = json_encode($value); |
|
| 351 | + } |
|
| 352 | + return (string) $value; |
|
| 353 | + } |
|
| 354 | 354 | |
| 355 | - /** |
|
| 356 | - * @return void |
|
| 357 | - */ |
|
| 358 | - protected function reset() |
|
| 359 | - { |
|
| 360 | - if ($this->size() <= pow(1024, 2) / 4) { |
|
| 361 | - return; |
|
| 362 | - } |
|
| 363 | - $this->clear(); |
|
| 364 | - file_put_contents( |
|
| 365 | - $this->file, |
|
| 366 | - $this->buildLogEntry( |
|
| 367 | - static::NOTICE, |
|
| 368 | - _x('Console was automatically cleared (256KB maximum size)', 'admin-text', 'site-reviews') |
|
| 369 | - ) |
|
| 370 | - ); |
|
| 371 | - } |
|
| 355 | + /** |
|
| 356 | + * @return void |
|
| 357 | + */ |
|
| 358 | + protected function reset() |
|
| 359 | + { |
|
| 360 | + if ($this->size() <= pow(1024, 2) / 4) { |
|
| 361 | + return; |
|
| 362 | + } |
|
| 363 | + $this->clear(); |
|
| 364 | + file_put_contents( |
|
| 365 | + $this->file, |
|
| 366 | + $this->buildLogEntry( |
|
| 367 | + static::NOTICE, |
|
| 368 | + _x('Console was automatically cleared (256KB maximum size)', 'admin-text', 'site-reviews') |
|
| 369 | + ) |
|
| 370 | + ); |
|
| 371 | + } |
|
| 372 | 372 | |
| 373 | - /** |
|
| 374 | - * @return void |
|
| 375 | - */ |
|
| 376 | - protected function setLogFile() |
|
| 377 | - { |
|
| 378 | - if (!function_exists('wp_hash')) { |
|
| 379 | - require_once ABSPATH.WPINC.'/pluggable.php'; |
|
| 380 | - } |
|
| 381 | - $uploads = wp_upload_dir(); |
|
| 382 | - $base = trailingslashit($uploads['basedir'].'/'.glsr()->id); |
|
| 383 | - $this->file = $base.'logs/'.sanitize_file_name('console-'.wp_hash(glsr()->id).'.log'); |
|
| 384 | - $files = [ |
|
| 385 | - $base.'index.php' => '<?php', |
|
| 386 | - $base.'logs/.htaccess' => 'deny from all', |
|
| 387 | - $base.'logs/index.php' => '<?php', |
|
| 388 | - $this->file => '', |
|
| 389 | - ]; |
|
| 390 | - foreach ($files as $file => $contents) { |
|
| 391 | - if (wp_mkdir_p(dirname($file)) && !file_exists($file)) { |
|
| 392 | - file_put_contents($file, $contents); |
|
| 393 | - } |
|
| 394 | - } |
|
| 395 | - $this->log = file_get_contents($this->file); |
|
| 396 | - } |
|
| 373 | + /** |
|
| 374 | + * @return void |
|
| 375 | + */ |
|
| 376 | + protected function setLogFile() |
|
| 377 | + { |
|
| 378 | + if (!function_exists('wp_hash')) { |
|
| 379 | + require_once ABSPATH.WPINC.'/pluggable.php'; |
|
| 380 | + } |
|
| 381 | + $uploads = wp_upload_dir(); |
|
| 382 | + $base = trailingslashit($uploads['basedir'].'/'.glsr()->id); |
|
| 383 | + $this->file = $base.'logs/'.sanitize_file_name('console-'.wp_hash(glsr()->id).'.log'); |
|
| 384 | + $files = [ |
|
| 385 | + $base.'index.php' => '<?php', |
|
| 386 | + $base.'logs/.htaccess' => 'deny from all', |
|
| 387 | + $base.'logs/index.php' => '<?php', |
|
| 388 | + $this->file => '', |
|
| 389 | + ]; |
|
| 390 | + foreach ($files as $file => $contents) { |
|
| 391 | + if (wp_mkdir_p(dirname($file)) && !file_exists($file)) { |
|
| 392 | + file_put_contents($file, $contents); |
|
| 393 | + } |
|
| 394 | + } |
|
| 395 | + $this->log = file_get_contents($this->file); |
|
| 396 | + } |
|
| 397 | 397 | } |
@@ -21,13 +21,13 @@ discard block |
||
| 21 | 21 | */ |
| 22 | 22 | class Console |
| 23 | 23 | { |
| 24 | - const DEBUG = 0; // Detailed debug information |
|
| 25 | - const INFO = 1; // Interesting events |
|
| 26 | - const NOTICE = 2; // Normal but significant events |
|
| 27 | - const WARNING = 4; // Exceptional occurrences that are not errors |
|
| 28 | - const ERROR = 8; // Runtime errors that do not require immediate action |
|
| 29 | - const CRITICAL = 16; // Critical conditions |
|
| 30 | - const ALERT = 32; // Action must be taken immediately |
|
| 24 | + const DEBUG = 0; // Detailed debug information |
|
| 25 | + const INFO = 1; // Interesting events |
|
| 26 | + const NOTICE = 2; // Normal but significant events |
|
| 27 | + const WARNING = 4; // Exceptional occurrences that are not errors |
|
| 28 | + const ERROR = 8; // Runtime errors that do not require immediate action |
|
| 29 | + const CRITICAL = 16; // Critical conditions |
|
| 30 | + const ALERT = 32; // Action must be taken immediately |
|
| 31 | 31 | const EMERGENCY = 64; // System is unusable |
| 32 | 32 | |
| 33 | 33 | protected $file; |
@@ -40,14 +40,14 @@ discard block |
||
| 40 | 40 | $this->reset(); |
| 41 | 41 | } |
| 42 | 42 | |
| 43 | - public function __call($method, $args) |
|
| 43 | + public function __call( $method, $args ) |
|
| 44 | 44 | { |
| 45 | - $constant = 'static::'.strtoupper($method); |
|
| 46 | - if (defined($constant)) { |
|
| 47 | - $args = Arr::prepend($args, constant($constant)); |
|
| 48 | - return call_user_func_array([$this, 'log'], array_slice($args, 0, 3)); |
|
| 45 | + $constant = 'static::'.strtoupper( $method ); |
|
| 46 | + if( defined( $constant ) ) { |
|
| 47 | + $args = Arr::prepend( $args, constant( $constant ) ); |
|
| 48 | + return call_user_func_array( [$this, 'log'], array_slice( $args, 0, 3 ) ); |
|
| 49 | 49 | } |
| 50 | - throw new BadMethodCallException("Method [$method] does not exist."); |
|
| 50 | + throw new BadMethodCallException( "Method [$method] does not exist." ); |
|
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | /** |
@@ -64,7 +64,7 @@ discard block |
||
| 64 | 64 | public function clear() |
| 65 | 65 | { |
| 66 | 66 | $this->log = ''; |
| 67 | - file_put_contents($this->file, $this->log); |
|
| 67 | + file_put_contents( $this->file, $this->log ); |
|
| 68 | 68 | } |
| 69 | 69 | |
| 70 | 70 | /** |
@@ -73,7 +73,7 @@ discard block |
||
| 73 | 73 | public function get() |
| 74 | 74 | { |
| 75 | 75 | return empty($this->log) |
| 76 | - ? _x('Console is empty', 'admin-text', 'site-reviews') |
|
| 76 | + ? _x( 'Console is empty', 'admin-text', 'site-reviews' ) |
|
| 77 | 77 | : $this->log; |
| 78 | 78 | } |
| 79 | 79 | |
@@ -82,7 +82,7 @@ discard block |
||
| 82 | 82 | */ |
| 83 | 83 | public function getLevel() |
| 84 | 84 | { |
| 85 | - return glsr()->filterInt('console/level', static::INFO); |
|
| 85 | + return glsr()->filterInt( 'console/level', static::INFO ); |
|
| 86 | 86 | } |
| 87 | 87 | |
| 88 | 88 | /** |
@@ -90,8 +90,8 @@ discard block |
||
| 90 | 90 | */ |
| 91 | 91 | public function getLevels() |
| 92 | 92 | { |
| 93 | - $constants = (new ReflectionClass(__CLASS__))->getConstants(); |
|
| 94 | - return array_map('strtolower', array_flip($constants)); |
|
| 93 | + $constants = (new ReflectionClass( __CLASS__ ))->getConstants(); |
|
| 94 | + return array_map( 'strtolower', array_flip( $constants ) ); |
|
| 95 | 95 | } |
| 96 | 96 | |
| 97 | 97 | /** |
@@ -100,21 +100,21 @@ discard block |
||
| 100 | 100 | public function humanLevel() |
| 101 | 101 | { |
| 102 | 102 | $level = $this->getLevel(); |
| 103 | - return sprintf('%s (%d)', strtoupper(Arr::get($this->getLevels(), $level, 'unknown')), $level); |
|
| 103 | + return sprintf( '%s (%d)', strtoupper( Arr::get( $this->getLevels(), $level, 'unknown' ) ), $level ); |
|
| 104 | 104 | } |
| 105 | 105 | |
| 106 | 106 | /** |
| 107 | 107 | * @param string|null $valueIfEmpty |
| 108 | 108 | * @return string |
| 109 | 109 | */ |
| 110 | - public function humanSize($valueIfEmpty = null) |
|
| 110 | + public function humanSize( $valueIfEmpty = null ) |
|
| 111 | 111 | { |
| 112 | 112 | $bytes = $this->size(); |
| 113 | - if (empty($bytes) && is_string($valueIfEmpty)) { |
|
| 113 | + if( empty($bytes) && is_string( $valueIfEmpty ) ) { |
|
| 114 | 114 | return $valueIfEmpty; |
| 115 | 115 | } |
| 116 | - $exponent = floor(log(max($bytes, 1), 1024)); |
|
| 117 | - return round($bytes / pow(1024, $exponent), 2).' '.['bytes', 'KB', 'MB', 'GB'][$exponent]; |
|
| 116 | + $exponent = floor( log( max( $bytes, 1 ), 1024 ) ); |
|
| 117 | + return round( $bytes / pow( 1024, $exponent ), 2 ).' '.['bytes', 'KB', 'MB', 'GB'][$exponent]; |
|
| 118 | 118 | } |
| 119 | 119 | |
| 120 | 120 | /** |
@@ -124,19 +124,19 @@ discard block |
||
| 124 | 124 | * @param string $backtraceLine |
| 125 | 125 | * @return static |
| 126 | 126 | */ |
| 127 | - public function log($level, $message, $context = [], $backtraceLine = '') |
|
| 127 | + public function log( $level, $message, $context = [], $backtraceLine = '' ) |
|
| 128 | 128 | { |
| 129 | - if (empty($backtraceLine)) { |
|
| 129 | + if( empty($backtraceLine) ) { |
|
| 130 | 130 | $backtraceLine = $this->getBacktraceLine(); |
| 131 | 131 | } |
| 132 | - if ($this->canLogEntry($level, $backtraceLine)) { |
|
| 133 | - $levelName = Arr::get($this->getLevels(), $level); |
|
| 134 | - $context = Arr::consolidate($context); |
|
| 135 | - $backtraceLine = $this->normalizeBacktraceLine($backtraceLine); |
|
| 136 | - $message = $this->interpolate($message, $context); |
|
| 137 | - $entry = $this->buildLogEntry($levelName, $message, $backtraceLine); |
|
| 138 | - file_put_contents($this->file, $entry.PHP_EOL, FILE_APPEND | LOCK_EX); |
|
| 139 | - apply_filters('console', $message, $levelName, $backtraceLine); // Show in Blackbar plugin if installed |
|
| 132 | + if( $this->canLogEntry( $level, $backtraceLine ) ) { |
|
| 133 | + $levelName = Arr::get( $this->getLevels(), $level ); |
|
| 134 | + $context = Arr::consolidate( $context ); |
|
| 135 | + $backtraceLine = $this->normalizeBacktraceLine( $backtraceLine ); |
|
| 136 | + $message = $this->interpolate( $message, $context ); |
|
| 137 | + $entry = $this->buildLogEntry( $levelName, $message, $backtraceLine ); |
|
| 138 | + file_put_contents( $this->file, $entry.PHP_EOL, FILE_APPEND | LOCK_EX ); |
|
| 139 | + apply_filters( 'console', $message, $levelName, $backtraceLine ); // Show in Blackbar plugin if installed |
|
| 140 | 140 | $this->reset(); |
| 141 | 141 | } |
| 142 | 142 | return $this; |
@@ -147,17 +147,17 @@ discard block |
||
| 147 | 147 | */ |
| 148 | 148 | public function logOnce() |
| 149 | 149 | { |
| 150 | - $once = Arr::consolidate(glsr()->{$this->logOnceKey}); |
|
| 150 | + $once = Arr::consolidate( glsr()->{$this->logOnceKey}); |
|
| 151 | 151 | $levels = $this->getLevels(); |
| 152 | - foreach ($once as $entry) { |
|
| 153 | - $levelName = Arr::get($entry, 'level'); |
|
| 154 | - if (!in_array($levelName, $levels)) { |
|
| 152 | + foreach( $once as $entry ) { |
|
| 153 | + $levelName = Arr::get( $entry, 'level' ); |
|
| 154 | + if( !in_array( $levelName, $levels ) ) { |
|
| 155 | 155 | continue; |
| 156 | 156 | } |
| 157 | - $level = Arr::get(array_flip($levels), $levelName); |
|
| 158 | - $message = Arr::get($entry, 'message'); |
|
| 159 | - $backtraceLine = Arr::get($entry, 'backtrace'); |
|
| 160 | - $this->log($level, $message, [], $backtraceLine); |
|
| 157 | + $level = Arr::get( array_flip( $levels ), $levelName ); |
|
| 158 | + $message = Arr::get( $entry, 'message' ); |
|
| 159 | + $backtraceLine = Arr::get( $entry, 'backtrace' ); |
|
| 160 | + $this->log( $level, $message, [], $backtraceLine ); |
|
| 161 | 161 | } |
| 162 | 162 | glsr()->{$this->logOnceKey} = []; |
| 163 | 163 | } |
@@ -168,21 +168,21 @@ discard block |
||
| 168 | 168 | * @param mixed $data |
| 169 | 169 | * @return void |
| 170 | 170 | */ |
| 171 | - public function once($levelName, $handle, $data) |
|
| 171 | + public function once( $levelName, $handle, $data ) |
|
| 172 | 172 | { |
| 173 | - $once = Arr::consolidate(glsr()->{$this->logOnceKey}); |
|
| 174 | - $filtered = array_filter($once, function ($entry) use ($levelName, $handle) { |
|
| 175 | - return Arr::get($entry, 'level') == $levelName |
|
| 176 | - && Arr::get($entry, 'handle') == $handle; |
|
| 173 | + $once = Arr::consolidate( glsr()->{$this->logOnceKey}); |
|
| 174 | + $filtered = array_filter( $once, function( $entry ) use ($levelName, $handle) { |
|
| 175 | + return Arr::get( $entry, 'level' ) == $levelName |
|
| 176 | + && Arr::get( $entry, 'handle' ) == $handle; |
|
| 177 | 177 | }); |
| 178 | - if (!empty($filtered)) { |
|
| 178 | + if( !empty($filtered) ) { |
|
| 179 | 179 | return; |
| 180 | 180 | } |
| 181 | 181 | $once[] = [ |
| 182 | - 'backtrace' => $this->getBacktraceLineFromData($data), |
|
| 182 | + 'backtrace' => $this->getBacktraceLineFromData( $data ), |
|
| 183 | 183 | 'handle' => $handle, |
| 184 | 184 | 'level' => $levelName, |
| 185 | - 'message' => '[RECURRING] '.$this->getMessageFromData($data), |
|
| 185 | + 'message' => '[RECURRING] '.$this->getMessageFromData( $data ), |
|
| 186 | 186 | ]; |
| 187 | 187 | glsr()->{$this->logOnceKey} = $once; |
| 188 | 188 | } |
@@ -192,8 +192,8 @@ discard block |
||
| 192 | 192 | */ |
| 193 | 193 | public function size() |
| 194 | 194 | { |
| 195 | - return file_exists($this->file) |
|
| 196 | - ? filesize($this->file) |
|
| 195 | + return file_exists( $this->file ) |
|
| 196 | + ? filesize( $this->file ) |
|
| 197 | 197 | : 0; |
| 198 | 198 | } |
| 199 | 199 | |
@@ -202,11 +202,11 @@ discard block |
||
| 202 | 202 | * @param int $index |
| 203 | 203 | * @return string |
| 204 | 204 | */ |
| 205 | - protected function buildBacktraceLine($backtrace, $index) |
|
| 205 | + protected function buildBacktraceLine( $backtrace, $index ) |
|
| 206 | 206 | { |
| 207 | - return sprintf('%s:%s', |
|
| 208 | - Arr::get($backtrace, $index.'.file'), // realpath |
|
| 209 | - Arr::get($backtrace, $index.'.line') |
|
| 207 | + return sprintf( '%s:%s', |
|
| 208 | + Arr::get( $backtrace, $index.'.file' ), // realpath |
|
| 209 | + Arr::get( $backtrace, $index.'.line' ) |
|
| 210 | 210 | ); |
| 211 | 211 | } |
| 212 | 212 | |
@@ -216,11 +216,11 @@ discard block |
||
| 216 | 216 | * @param string $backtraceLine |
| 217 | 217 | * @return string |
| 218 | 218 | */ |
| 219 | - protected function buildLogEntry($levelName, $message, $backtraceLine = '') |
|
| 219 | + protected function buildLogEntry( $levelName, $message, $backtraceLine = '' ) |
|
| 220 | 220 | { |
| 221 | - return sprintf('[%s] %s [%s] %s', |
|
| 222 | - current_time('mysql'), |
|
| 223 | - strtoupper($levelName), |
|
| 221 | + return sprintf( '[%s] %s [%s] %s', |
|
| 222 | + current_time( 'mysql' ), |
|
| 223 | + strtoupper( $levelName ), |
|
| 224 | 224 | $backtraceLine, |
| 225 | 225 | $message |
| 226 | 226 | ); |
@@ -230,10 +230,10 @@ discard block |
||
| 230 | 230 | * @param int $level |
| 231 | 231 | * @return bool |
| 232 | 232 | */ |
| 233 | - protected function canLogEntry($level, $backtraceLine) |
|
| 233 | + protected function canLogEntry( $level, $backtraceLine ) |
|
| 234 | 234 | { |
| 235 | - $levelExists = array_key_exists($level, $this->getLevels()); |
|
| 236 | - if (!Str::contains($backtraceLine, glsr()->path())) { |
|
| 235 | + $levelExists = array_key_exists( $level, $this->getLevels() ); |
|
| 236 | + if( !Str::contains( $backtraceLine, glsr()->path() ) ) { |
|
| 237 | 237 | return $levelExists; // ignore level restriction if triggered outside of the plugin |
| 238 | 238 | } |
| 239 | 239 | return $levelExists && $level >= $this->getLevel(); |
@@ -244,17 +244,17 @@ discard block |
||
| 244 | 244 | */ |
| 245 | 245 | protected function getBacktraceLine() |
| 246 | 246 | { |
| 247 | - $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 6); |
|
| 248 | - $search = array_search('glsr_log', wp_list_pluck($backtrace, 'function')); |
|
| 249 | - if (false !== $search) { |
|
| 250 | - return $this->buildBacktraceLine($backtrace, (int) $search); |
|
| 247 | + $backtrace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 6 ); |
|
| 248 | + $search = array_search( 'glsr_log', wp_list_pluck( $backtrace, 'function' ) ); |
|
| 249 | + if( false !== $search ) { |
|
| 250 | + return $this->buildBacktraceLine( $backtrace, (int)$search ); |
|
| 251 | 251 | } |
| 252 | - $search = array_search('log', wp_list_pluck($backtrace, 'function')); |
|
| 253 | - if (false !== $search) { |
|
| 254 | - $index = '{closure}' == Arr::get($backtrace, ($search + 2).'.function') |
|
| 252 | + $search = array_search( 'log', wp_list_pluck( $backtrace, 'function' ) ); |
|
| 253 | + if( false !== $search ) { |
|
| 254 | + $index = '{closure}' == Arr::get( $backtrace, ($search + 2).'.function' ) |
|
| 255 | 255 | ? $search + 4 |
| 256 | 256 | : $search + 1; |
| 257 | - return $this->buildBacktraceLine($backtrace, $index); |
|
| 257 | + return $this->buildBacktraceLine( $backtrace, $index ); |
|
| 258 | 258 | } |
| 259 | 259 | return 'Unknown'; |
| 260 | 260 | } |
@@ -263,23 +263,23 @@ discard block |
||
| 263 | 263 | * @param mixed $data |
| 264 | 264 | * @return string |
| 265 | 265 | */ |
| 266 | - protected function getBacktraceLineFromData($data) |
|
| 266 | + protected function getBacktraceLineFromData( $data ) |
|
| 267 | 267 | { |
| 268 | 268 | $backtrace = $data instanceof Throwable |
| 269 | 269 | ? $data->getTrace() |
| 270 | - : debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1); |
|
| 271 | - return $this->buildBacktraceLine($backtrace, 0); |
|
| 270 | + : debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 1 ); |
|
| 271 | + return $this->buildBacktraceLine( $backtrace, 0 ); |
|
| 272 | 272 | } |
| 273 | 273 | |
| 274 | 274 | /** |
| 275 | 275 | * @param mixed $data |
| 276 | 276 | * @return string |
| 277 | 277 | */ |
| 278 | - protected function getMessageFromData($data) |
|
| 278 | + protected function getMessageFromData( $data ) |
|
| 279 | 279 | { |
| 280 | 280 | return $data instanceof Throwable |
| 281 | - ? $this->normalizeThrowableMessage($data->getMessage()) |
|
| 282 | - : print_r($data, 1); |
|
| 281 | + ? $this->normalizeThrowableMessage( $data->getMessage() ) |
|
| 282 | + : print_r( $data, 1 ); |
|
| 283 | 283 | } |
| 284 | 284 | |
| 285 | 285 | /** |
@@ -288,53 +288,53 @@ discard block |
||
| 288 | 288 | * @param array $context |
| 289 | 289 | * @return string |
| 290 | 290 | */ |
| 291 | - protected function interpolate($message, $context = []) |
|
| 291 | + protected function interpolate( $message, $context = [] ) |
|
| 292 | 292 | { |
| 293 | - if ($this->isObjectOrArray($message) || !is_array($context)) { |
|
| 294 | - return print_r($message, true); |
|
| 293 | + if( $this->isObjectOrArray( $message ) || !is_array( $context ) ) { |
|
| 294 | + return print_r( $message, true ); |
|
| 295 | 295 | } |
| 296 | 296 | $replace = []; |
| 297 | - foreach ($context as $key => $value) { |
|
| 298 | - $replace['{'.$key.'}'] = $this->normalizeValue($value); |
|
| 297 | + foreach( $context as $key => $value ) { |
|
| 298 | + $replace['{'.$key.'}'] = $this->normalizeValue( $value ); |
|
| 299 | 299 | } |
| 300 | - return strtr($message, $replace); |
|
| 300 | + return strtr( $message, $replace ); |
|
| 301 | 301 | } |
| 302 | 302 | |
| 303 | 303 | /** |
| 304 | 304 | * @param mixed $value |
| 305 | 305 | * @return bool |
| 306 | 306 | */ |
| 307 | - protected function isObjectOrArray($value) |
|
| 307 | + protected function isObjectOrArray( $value ) |
|
| 308 | 308 | { |
| 309 | - return is_object($value) || is_array($value); |
|
| 309 | + return is_object( $value ) || is_array( $value ); |
|
| 310 | 310 | } |
| 311 | 311 | |
| 312 | 312 | /** |
| 313 | 313 | * @param string $backtraceLine |
| 314 | 314 | * @return string |
| 315 | 315 | */ |
| 316 | - protected function normalizeBacktraceLine($backtraceLine) |
|
| 316 | + protected function normalizeBacktraceLine( $backtraceLine ) |
|
| 317 | 317 | { |
| 318 | 318 | $search = [ |
| 319 | - glsr()->path('plugin/'), |
|
| 320 | - glsr()->path('plugin/', false), |
|
| 321 | - trailingslashit(glsr()->path()), |
|
| 322 | - trailingslashit(glsr()->path('', false)), |
|
| 319 | + glsr()->path( 'plugin/' ), |
|
| 320 | + glsr()->path( 'plugin/', false ), |
|
| 321 | + trailingslashit( glsr()->path() ), |
|
| 322 | + trailingslashit( glsr()->path( '', false ) ), |
|
| 323 | 323 | WP_CONTENT_DIR, |
| 324 | 324 | ABSPATH, |
| 325 | 325 | ]; |
| 326 | - return str_replace(array_unique($search), '', $backtraceLine); |
|
| 326 | + return str_replace( array_unique( $search ), '', $backtraceLine ); |
|
| 327 | 327 | } |
| 328 | 328 | |
| 329 | 329 | /** |
| 330 | 330 | * @param string $message |
| 331 | 331 | * @return string |
| 332 | 332 | */ |
| 333 | - protected function normalizeThrowableMessage($message) |
|
| 333 | + protected function normalizeThrowableMessage( $message ) |
|
| 334 | 334 | { |
| 335 | - $calledIn = strpos($message, ', called in'); |
|
| 335 | + $calledIn = strpos( $message, ', called in' ); |
|
| 336 | 336 | return false !== $calledIn |
| 337 | - ? substr($message, 0, $calledIn) |
|
| 337 | + ? substr( $message, 0, $calledIn ) |
|
| 338 | 338 | : $message; |
| 339 | 339 | } |
| 340 | 340 | |
@@ -342,14 +342,14 @@ discard block |
||
| 342 | 342 | * @param mixed $value |
| 343 | 343 | * @return string |
| 344 | 344 | */ |
| 345 | - protected function normalizeValue($value) |
|
| 345 | + protected function normalizeValue( $value ) |
|
| 346 | 346 | { |
| 347 | - if ($value instanceof DateTime) { |
|
| 348 | - $value = $value->format('Y-m-d H:i:s'); |
|
| 349 | - } elseif ($this->isObjectOrArray($value)) { |
|
| 350 | - $value = json_encode($value); |
|
| 347 | + if( $value instanceof DateTime ) { |
|
| 348 | + $value = $value->format( 'Y-m-d H:i:s' ); |
|
| 349 | + } elseif( $this->isObjectOrArray( $value ) ) { |
|
| 350 | + $value = json_encode( $value ); |
|
| 351 | 351 | } |
| 352 | - return (string) $value; |
|
| 352 | + return (string)$value; |
|
| 353 | 353 | } |
| 354 | 354 | |
| 355 | 355 | /** |
@@ -357,7 +357,7 @@ discard block |
||
| 357 | 357 | */ |
| 358 | 358 | protected function reset() |
| 359 | 359 | { |
| 360 | - if ($this->size() <= pow(1024, 2) / 4) { |
|
| 360 | + if( $this->size() <= pow( 1024, 2 ) / 4 ) { |
|
| 361 | 361 | return; |
| 362 | 362 | } |
| 363 | 363 | $this->clear(); |
@@ -365,7 +365,7 @@ discard block |
||
| 365 | 365 | $this->file, |
| 366 | 366 | $this->buildLogEntry( |
| 367 | 367 | static::NOTICE, |
| 368 | - _x('Console was automatically cleared (256KB maximum size)', 'admin-text', 'site-reviews') |
|
| 368 | + _x( 'Console was automatically cleared (256KB maximum size)', 'admin-text', 'site-reviews' ) |
|
| 369 | 369 | ) |
| 370 | 370 | ); |
| 371 | 371 | } |
@@ -375,23 +375,23 @@ discard block |
||
| 375 | 375 | */ |
| 376 | 376 | protected function setLogFile() |
| 377 | 377 | { |
| 378 | - if (!function_exists('wp_hash')) { |
|
| 378 | + if( !function_exists( 'wp_hash' ) ) { |
|
| 379 | 379 | require_once ABSPATH.WPINC.'/pluggable.php'; |
| 380 | 380 | } |
| 381 | 381 | $uploads = wp_upload_dir(); |
| 382 | - $base = trailingslashit($uploads['basedir'].'/'.glsr()->id); |
|
| 383 | - $this->file = $base.'logs/'.sanitize_file_name('console-'.wp_hash(glsr()->id).'.log'); |
|
| 382 | + $base = trailingslashit( $uploads['basedir'].'/'.glsr()->id ); |
|
| 383 | + $this->file = $base.'logs/'.sanitize_file_name( 'console-'.wp_hash( glsr()->id ).'.log' ); |
|
| 384 | 384 | $files = [ |
| 385 | 385 | $base.'index.php' => '<?php', |
| 386 | 386 | $base.'logs/.htaccess' => 'deny from all', |
| 387 | 387 | $base.'logs/index.php' => '<?php', |
| 388 | 388 | $this->file => '', |
| 389 | 389 | ]; |
| 390 | - foreach ($files as $file => $contents) { |
|
| 391 | - if (wp_mkdir_p(dirname($file)) && !file_exists($file)) { |
|
| 392 | - file_put_contents($file, $contents); |
|
| 390 | + foreach( $files as $file => $contents ) { |
|
| 391 | + if( wp_mkdir_p( dirname( $file ) ) && !file_exists( $file ) ) { |
|
| 392 | + file_put_contents( $file, $contents ); |
|
| 393 | 393 | } |
| 394 | 394 | } |
| 395 | - $this->log = file_get_contents($this->file); |
|
| 395 | + $this->log = file_get_contents( $this->file ); |
|
| 396 | 396 | } |
| 397 | 397 | } |
@@ -12,276 +12,276 @@ |
||
| 12 | 12 | |
| 13 | 13 | class Translation |
| 14 | 14 | { |
| 15 | - const CONTEXT_ADMIN_KEY = 'admin-text'; |
|
| 16 | - const SEARCH_THRESHOLD = 3; |
|
| 15 | + const CONTEXT_ADMIN_KEY = 'admin-text'; |
|
| 16 | + const SEARCH_THRESHOLD = 3; |
|
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * @var array |
|
| 20 | - */ |
|
| 21 | - protected $entries; |
|
| 18 | + /** |
|
| 19 | + * @var array |
|
| 20 | + */ |
|
| 21 | + protected $entries; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * @var array |
|
| 25 | - */ |
|
| 26 | - protected $results; |
|
| 23 | + /** |
|
| 24 | + * @var array |
|
| 25 | + */ |
|
| 26 | + protected $results; |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * Returns all saved custom translations with translation context. |
|
| 30 | - * @return array |
|
| 31 | - */ |
|
| 32 | - public function all() |
|
| 33 | - { |
|
| 34 | - $translations = $this->translations(); |
|
| 35 | - $entries = $this->filter($translations, $this->entries())->results(); |
|
| 36 | - array_walk($translations, function (&$entry) use ($entries) { |
|
| 37 | - $entry['desc'] = array_key_exists($entry['id'], $entries) |
|
| 38 | - ? $this->getEntryString($entries[$entry['id']], 'msgctxt') |
|
| 39 | - : ''; |
|
| 40 | - }); |
|
| 41 | - return $translations; |
|
| 42 | - } |
|
| 28 | + /** |
|
| 29 | + * Returns all saved custom translations with translation context. |
|
| 30 | + * @return array |
|
| 31 | + */ |
|
| 32 | + public function all() |
|
| 33 | + { |
|
| 34 | + $translations = $this->translations(); |
|
| 35 | + $entries = $this->filter($translations, $this->entries())->results(); |
|
| 36 | + array_walk($translations, function (&$entry) use ($entries) { |
|
| 37 | + $entry['desc'] = array_key_exists($entry['id'], $entries) |
|
| 38 | + ? $this->getEntryString($entries[$entry['id']], 'msgctxt') |
|
| 39 | + : ''; |
|
| 40 | + }); |
|
| 41 | + return $translations; |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * @return array |
|
| 46 | - */ |
|
| 47 | - public function entries() |
|
| 48 | - { |
|
| 49 | - if (!isset($this->entries)) { |
|
| 50 | - $potFile = glsr()->path(glsr()->languages.'/'.Application::ID.'.pot'); |
|
| 51 | - $entries = $this->extractEntriesFromPotFile($potFile); |
|
| 52 | - $entries = glsr()->filterArray('translation/entries', $entries); |
|
| 53 | - $this->entries = $entries; |
|
| 54 | - } |
|
| 55 | - return $this->entries; |
|
| 56 | - } |
|
| 44 | + /** |
|
| 45 | + * @return array |
|
| 46 | + */ |
|
| 47 | + public function entries() |
|
| 48 | + { |
|
| 49 | + if (!isset($this->entries)) { |
|
| 50 | + $potFile = glsr()->path(glsr()->languages.'/'.Application::ID.'.pot'); |
|
| 51 | + $entries = $this->extractEntriesFromPotFile($potFile); |
|
| 52 | + $entries = glsr()->filterArray('translation/entries', $entries); |
|
| 53 | + $this->entries = $entries; |
|
| 54 | + } |
|
| 55 | + return $this->entries; |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * @param array|null $entriesToExclude |
|
| 60 | - * @param array|null $entries |
|
| 61 | - * @return static |
|
| 62 | - */ |
|
| 63 | - public function exclude($entriesToExclude = null, $entries = null) |
|
| 64 | - { |
|
| 65 | - return $this->filter($entriesToExclude, $entries, false); |
|
| 66 | - } |
|
| 58 | + /** |
|
| 59 | + * @param array|null $entriesToExclude |
|
| 60 | + * @param array|null $entries |
|
| 61 | + * @return static |
|
| 62 | + */ |
|
| 63 | + public function exclude($entriesToExclude = null, $entries = null) |
|
| 64 | + { |
|
| 65 | + return $this->filter($entriesToExclude, $entries, false); |
|
| 66 | + } |
|
| 67 | 67 | |
| 68 | - /** |
|
| 69 | - * @param string $potFile |
|
| 70 | - * @return array |
|
| 71 | - */ |
|
| 72 | - public function extractEntriesFromPotFile($potFile, array $entries = []) |
|
| 73 | - { |
|
| 74 | - try { |
|
| 75 | - $potEntries = $this->normalize(Parser::parseFile($potFile)->getEntries()); |
|
| 76 | - foreach ($potEntries as $key => $entry) { |
|
| 77 | - if (Str::contains(static::CONTEXT_ADMIN_KEY, Arr::get($entry, 'msgctxt'))) { |
|
| 78 | - continue; |
|
| 79 | - } |
|
| 80 | - $entries[html_entity_decode($key, ENT_COMPAT, 'UTF-8')] = $entry; |
|
| 81 | - } |
|
| 82 | - } catch (Exception $e) { |
|
| 83 | - glsr_log()->error($e->getMessage()); |
|
| 84 | - } |
|
| 85 | - return $entries; |
|
| 86 | - } |
|
| 68 | + /** |
|
| 69 | + * @param string $potFile |
|
| 70 | + * @return array |
|
| 71 | + */ |
|
| 72 | + public function extractEntriesFromPotFile($potFile, array $entries = []) |
|
| 73 | + { |
|
| 74 | + try { |
|
| 75 | + $potEntries = $this->normalize(Parser::parseFile($potFile)->getEntries()); |
|
| 76 | + foreach ($potEntries as $key => $entry) { |
|
| 77 | + if (Str::contains(static::CONTEXT_ADMIN_KEY, Arr::get($entry, 'msgctxt'))) { |
|
| 78 | + continue; |
|
| 79 | + } |
|
| 80 | + $entries[html_entity_decode($key, ENT_COMPAT, 'UTF-8')] = $entry; |
|
| 81 | + } |
|
| 82 | + } catch (Exception $e) { |
|
| 83 | + glsr_log()->error($e->getMessage()); |
|
| 84 | + } |
|
| 85 | + return $entries; |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | - /** |
|
| 89 | - * @param array|null $filterWith |
|
| 90 | - * @param array|null $entries |
|
| 91 | - * @param bool $intersect |
|
| 92 | - * @return static |
|
| 93 | - */ |
|
| 94 | - public function filter($filterWith = null, $entries = null, $intersect = true) |
|
| 95 | - { |
|
| 96 | - if (!is_array($entries)) { |
|
| 97 | - $entries = $this->results; |
|
| 98 | - } |
|
| 99 | - if (!is_array($filterWith)) { |
|
| 100 | - $filterWith = $this->translations(); |
|
| 101 | - } |
|
| 102 | - $keys = array_flip(wp_list_pluck($filterWith, 'id')); |
|
| 103 | - $this->results = $intersect |
|
| 104 | - ? array_intersect_key($entries, $keys) |
|
| 105 | - : array_diff_key($entries, $keys); |
|
| 106 | - return $this; |
|
| 107 | - } |
|
| 88 | + /** |
|
| 89 | + * @param array|null $filterWith |
|
| 90 | + * @param array|null $entries |
|
| 91 | + * @param bool $intersect |
|
| 92 | + * @return static |
|
| 93 | + */ |
|
| 94 | + public function filter($filterWith = null, $entries = null, $intersect = true) |
|
| 95 | + { |
|
| 96 | + if (!is_array($entries)) { |
|
| 97 | + $entries = $this->results; |
|
| 98 | + } |
|
| 99 | + if (!is_array($filterWith)) { |
|
| 100 | + $filterWith = $this->translations(); |
|
| 101 | + } |
|
| 102 | + $keys = array_flip(wp_list_pluck($filterWith, 'id')); |
|
| 103 | + $this->results = $intersect |
|
| 104 | + ? array_intersect_key($entries, $keys) |
|
| 105 | + : array_diff_key($entries, $keys); |
|
| 106 | + return $this; |
|
| 107 | + } |
|
| 108 | 108 | |
| 109 | - /** |
|
| 110 | - * @param string $template |
|
| 111 | - * @return string |
|
| 112 | - */ |
|
| 113 | - public function render($template, array $entry) |
|
| 114 | - { |
|
| 115 | - $data = array_combine( |
|
| 116 | - array_map(function ($key) { return 'data.'.$key; }, array_keys($entry)), |
|
| 117 | - $entry |
|
| 118 | - ); |
|
| 119 | - $data['data.class'] = $data['data.error'] = ''; |
|
| 120 | - if (false === array_search($entry['s1'], wp_list_pluck($this->entries(), 'msgid'))) { |
|
| 121 | - $data['data.class'] = 'is-invalid'; |
|
| 122 | - $data['data.error'] = _x('This custom translation is no longer valid as the original text has been changed or removed.', 'admin-text', 'site-reviews'); |
|
| 123 | - } |
|
| 124 | - return glsr(Template::class)->build('partials/translations/'.$template, [ |
|
| 125 | - 'context' => array_map('esc_html', $data), |
|
| 126 | - ]); |
|
| 127 | - } |
|
| 109 | + /** |
|
| 110 | + * @param string $template |
|
| 111 | + * @return string |
|
| 112 | + */ |
|
| 113 | + public function render($template, array $entry) |
|
| 114 | + { |
|
| 115 | + $data = array_combine( |
|
| 116 | + array_map(function ($key) { return 'data.'.$key; }, array_keys($entry)), |
|
| 117 | + $entry |
|
| 118 | + ); |
|
| 119 | + $data['data.class'] = $data['data.error'] = ''; |
|
| 120 | + if (false === array_search($entry['s1'], wp_list_pluck($this->entries(), 'msgid'))) { |
|
| 121 | + $data['data.class'] = 'is-invalid'; |
|
| 122 | + $data['data.error'] = _x('This custom translation is no longer valid as the original text has been changed or removed.', 'admin-text', 'site-reviews'); |
|
| 123 | + } |
|
| 124 | + return glsr(Template::class)->build('partials/translations/'.$template, [ |
|
| 125 | + 'context' => array_map('esc_html', $data), |
|
| 126 | + ]); |
|
| 127 | + } |
|
| 128 | 128 | |
| 129 | - /** |
|
| 130 | - * Returns a rendered string of all saved custom translations with translation context. |
|
| 131 | - * @return string |
|
| 132 | - */ |
|
| 133 | - public function renderAll() |
|
| 134 | - { |
|
| 135 | - $rendered = ''; |
|
| 136 | - foreach ($this->all() as $index => $entry) { |
|
| 137 | - $entry['index'] = $index; |
|
| 138 | - $entry['prefix'] = OptionManager::databaseKey(); |
|
| 139 | - $rendered.= $this->render($entry['type'], $entry); |
|
| 140 | - } |
|
| 141 | - return $rendered; |
|
| 142 | - } |
|
| 129 | + /** |
|
| 130 | + * Returns a rendered string of all saved custom translations with translation context. |
|
| 131 | + * @return string |
|
| 132 | + */ |
|
| 133 | + public function renderAll() |
|
| 134 | + { |
|
| 135 | + $rendered = ''; |
|
| 136 | + foreach ($this->all() as $index => $entry) { |
|
| 137 | + $entry['index'] = $index; |
|
| 138 | + $entry['prefix'] = OptionManager::databaseKey(); |
|
| 139 | + $rendered.= $this->render($entry['type'], $entry); |
|
| 140 | + } |
|
| 141 | + return $rendered; |
|
| 142 | + } |
|
| 143 | 143 | |
| 144 | - /** |
|
| 145 | - * @param bool $resetAfterRender |
|
| 146 | - * @return string |
|
| 147 | - */ |
|
| 148 | - public function renderResults($resetAfterRender = true) |
|
| 149 | - { |
|
| 150 | - $rendered = ''; |
|
| 151 | - foreach ($this->results as $id => $entry) { |
|
| 152 | - $data = [ |
|
| 153 | - 'desc' => $this->getEntryString($entry, 'msgctxt'), |
|
| 154 | - 'id' => $id, |
|
| 155 | - 'p1' => $this->getEntryString($entry, 'msgid_plural'), |
|
| 156 | - 's1' => $this->getEntryString($entry, 'msgid'), |
|
| 157 | - ]; |
|
| 158 | - $text = !empty($data['p1']) |
|
| 159 | - ? sprintf('%s | %s', $data['s1'], $data['p1']) |
|
| 160 | - : $data['s1']; |
|
| 161 | - $rendered.= $this->render('result', [ |
|
| 162 | - 'entry' => json_encode($data, JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_TAG | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), |
|
| 163 | - 'text' => wp_strip_all_tags($text), |
|
| 164 | - ]); |
|
| 165 | - } |
|
| 166 | - if ($resetAfterRender) { |
|
| 167 | - $this->reset(); |
|
| 168 | - } |
|
| 169 | - return $rendered; |
|
| 170 | - } |
|
| 144 | + /** |
|
| 145 | + * @param bool $resetAfterRender |
|
| 146 | + * @return string |
|
| 147 | + */ |
|
| 148 | + public function renderResults($resetAfterRender = true) |
|
| 149 | + { |
|
| 150 | + $rendered = ''; |
|
| 151 | + foreach ($this->results as $id => $entry) { |
|
| 152 | + $data = [ |
|
| 153 | + 'desc' => $this->getEntryString($entry, 'msgctxt'), |
|
| 154 | + 'id' => $id, |
|
| 155 | + 'p1' => $this->getEntryString($entry, 'msgid_plural'), |
|
| 156 | + 's1' => $this->getEntryString($entry, 'msgid'), |
|
| 157 | + ]; |
|
| 158 | + $text = !empty($data['p1']) |
|
| 159 | + ? sprintf('%s | %s', $data['s1'], $data['p1']) |
|
| 160 | + : $data['s1']; |
|
| 161 | + $rendered.= $this->render('result', [ |
|
| 162 | + 'entry' => json_encode($data, JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_TAG | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), |
|
| 163 | + 'text' => wp_strip_all_tags($text), |
|
| 164 | + ]); |
|
| 165 | + } |
|
| 166 | + if ($resetAfterRender) { |
|
| 167 | + $this->reset(); |
|
| 168 | + } |
|
| 169 | + return $rendered; |
|
| 170 | + } |
|
| 171 | 171 | |
| 172 | - /** |
|
| 173 | - * @return void |
|
| 174 | - */ |
|
| 175 | - public function reset() |
|
| 176 | - { |
|
| 177 | - $this->results = []; |
|
| 178 | - } |
|
| 172 | + /** |
|
| 173 | + * @return void |
|
| 174 | + */ |
|
| 175 | + public function reset() |
|
| 176 | + { |
|
| 177 | + $this->results = []; |
|
| 178 | + } |
|
| 179 | 179 | |
| 180 | - /** |
|
| 181 | - * @return array |
|
| 182 | - */ |
|
| 183 | - public function results() |
|
| 184 | - { |
|
| 185 | - $results = $this->results; |
|
| 186 | - $this->reset(); |
|
| 187 | - return $results; |
|
| 188 | - } |
|
| 180 | + /** |
|
| 181 | + * @return array |
|
| 182 | + */ |
|
| 183 | + public function results() |
|
| 184 | + { |
|
| 185 | + $results = $this->results; |
|
| 186 | + $this->reset(); |
|
| 187 | + return $results; |
|
| 188 | + } |
|
| 189 | 189 | |
| 190 | - /** |
|
| 191 | - * @param string $needle |
|
| 192 | - * @return static |
|
| 193 | - */ |
|
| 194 | - public function search($needle = '') |
|
| 195 | - { |
|
| 196 | - $this->reset(); |
|
| 197 | - $needle = trim(strtolower($needle)); |
|
| 198 | - foreach ($this->entries() as $key => $entry) { |
|
| 199 | - $single = strtolower($this->getEntryString($entry, 'msgid')); |
|
| 200 | - $plural = strtolower($this->getEntryString($entry, 'msgid_plural')); |
|
| 201 | - if (strlen($needle) < static::SEARCH_THRESHOLD) { |
|
| 202 | - if (in_array($needle, [$single, $plural])) { |
|
| 203 | - $this->results[$key] = $entry; |
|
| 204 | - } |
|
| 205 | - } elseif (Str::contains(sprintf('%s %s', $single, $plural), $needle)) { |
|
| 206 | - $this->results[$key] = $entry; |
|
| 207 | - } |
|
| 208 | - } |
|
| 209 | - return $this; |
|
| 210 | - } |
|
| 190 | + /** |
|
| 191 | + * @param string $needle |
|
| 192 | + * @return static |
|
| 193 | + */ |
|
| 194 | + public function search($needle = '') |
|
| 195 | + { |
|
| 196 | + $this->reset(); |
|
| 197 | + $needle = trim(strtolower($needle)); |
|
| 198 | + foreach ($this->entries() as $key => $entry) { |
|
| 199 | + $single = strtolower($this->getEntryString($entry, 'msgid')); |
|
| 200 | + $plural = strtolower($this->getEntryString($entry, 'msgid_plural')); |
|
| 201 | + if (strlen($needle) < static::SEARCH_THRESHOLD) { |
|
| 202 | + if (in_array($needle, [$single, $plural])) { |
|
| 203 | + $this->results[$key] = $entry; |
|
| 204 | + } |
|
| 205 | + } elseif (Str::contains(sprintf('%s %s', $single, $plural), $needle)) { |
|
| 206 | + $this->results[$key] = $entry; |
|
| 207 | + } |
|
| 208 | + } |
|
| 209 | + return $this; |
|
| 210 | + } |
|
| 211 | 211 | |
| 212 | - /** |
|
| 213 | - * Store the translations to avoid unnecessary loops. |
|
| 214 | - * @return array |
|
| 215 | - */ |
|
| 216 | - public function translations() |
|
| 217 | - { |
|
| 218 | - static $translations; |
|
| 219 | - if (empty($translations)) { |
|
| 220 | - $settings = glsr(OptionManager::class)->get('settings'); |
|
| 221 | - $translations = isset($settings['strings']) |
|
| 222 | - ? $this->normalizeSettings((array) $settings['strings']) |
|
| 223 | - : []; |
|
| 224 | - } |
|
| 225 | - return $translations; |
|
| 226 | - } |
|
| 212 | + /** |
|
| 213 | + * Store the translations to avoid unnecessary loops. |
|
| 214 | + * @return array |
|
| 215 | + */ |
|
| 216 | + public function translations() |
|
| 217 | + { |
|
| 218 | + static $translations; |
|
| 219 | + if (empty($translations)) { |
|
| 220 | + $settings = glsr(OptionManager::class)->get('settings'); |
|
| 221 | + $translations = isset($settings['strings']) |
|
| 222 | + ? $this->normalizeSettings((array) $settings['strings']) |
|
| 223 | + : []; |
|
| 224 | + } |
|
| 225 | + return $translations; |
|
| 226 | + } |
|
| 227 | 227 | |
| 228 | - /** |
|
| 229 | - * @param string $key |
|
| 230 | - * @return string |
|
| 231 | - */ |
|
| 232 | - protected function getEntryString(array $entry, $key) |
|
| 233 | - { |
|
| 234 | - return isset($entry[$key]) |
|
| 235 | - ? implode('', (array) $entry[$key]) |
|
| 236 | - : ''; |
|
| 237 | - } |
|
| 228 | + /** |
|
| 229 | + * @param string $key |
|
| 230 | + * @return string |
|
| 231 | + */ |
|
| 232 | + protected function getEntryString(array $entry, $key) |
|
| 233 | + { |
|
| 234 | + return isset($entry[$key]) |
|
| 235 | + ? implode('', (array) $entry[$key]) |
|
| 236 | + : ''; |
|
| 237 | + } |
|
| 238 | 238 | |
| 239 | - /** |
|
| 240 | - * @return array |
|
| 241 | - */ |
|
| 242 | - protected function normalize(array $entries) |
|
| 243 | - { |
|
| 244 | - $keys = [ |
|
| 245 | - 'msgctxt', 'msgid', 'msgid_plural', 'msgstr', 'msgstr[0]', 'msgstr[1]', |
|
| 246 | - ]; |
|
| 247 | - array_walk($entries, function (&$entry) use ($keys) { |
|
| 248 | - foreach ($keys as $key) { |
|
| 249 | - try { |
|
| 250 | - $entry = $this->normalizeEntryString($entry, $key); |
|
| 251 | - } catch (\TypeError $error) { |
|
| 252 | - glsr_log()->once('error', 'Translation/normalize', $error); |
|
| 253 | - glsr_log()->once('debug', 'Translation/normalize', $entry); |
|
| 254 | - } |
|
| 255 | - } |
|
| 256 | - }); |
|
| 257 | - return $entries; |
|
| 258 | - } |
|
| 239 | + /** |
|
| 240 | + * @return array |
|
| 241 | + */ |
|
| 242 | + protected function normalize(array $entries) |
|
| 243 | + { |
|
| 244 | + $keys = [ |
|
| 245 | + 'msgctxt', 'msgid', 'msgid_plural', 'msgstr', 'msgstr[0]', 'msgstr[1]', |
|
| 246 | + ]; |
|
| 247 | + array_walk($entries, function (&$entry) use ($keys) { |
|
| 248 | + foreach ($keys as $key) { |
|
| 249 | + try { |
|
| 250 | + $entry = $this->normalizeEntryString($entry, $key); |
|
| 251 | + } catch (\TypeError $error) { |
|
| 252 | + glsr_log()->once('error', 'Translation/normalize', $error); |
|
| 253 | + glsr_log()->once('debug', 'Translation/normalize', $entry); |
|
| 254 | + } |
|
| 255 | + } |
|
| 256 | + }); |
|
| 257 | + return $entries; |
|
| 258 | + } |
|
| 259 | 259 | |
| 260 | - /** |
|
| 261 | - * @param string $key |
|
| 262 | - * @return array |
|
| 263 | - */ |
|
| 264 | - protected function normalizeEntryString(array $entry, $key) |
|
| 265 | - { |
|
| 266 | - if (isset($entry[$key])) { |
|
| 267 | - $entry[$key] = $this->getEntryString($entry, $key); |
|
| 268 | - } |
|
| 269 | - return $entry; |
|
| 270 | - } |
|
| 260 | + /** |
|
| 261 | + * @param string $key |
|
| 262 | + * @return array |
|
| 263 | + */ |
|
| 264 | + protected function normalizeEntryString(array $entry, $key) |
|
| 265 | + { |
|
| 266 | + if (isset($entry[$key])) { |
|
| 267 | + $entry[$key] = $this->getEntryString($entry, $key); |
|
| 268 | + } |
|
| 269 | + return $entry; |
|
| 270 | + } |
|
| 271 | 271 | |
| 272 | - /** |
|
| 273 | - * @return array |
|
| 274 | - */ |
|
| 275 | - protected function normalizeSettings(array $strings) |
|
| 276 | - { |
|
| 277 | - $defaultString = array_fill_keys(['id', 's1', 's2', 'p1', 'p2'], ''); |
|
| 278 | - $strings = array_filter($strings, 'is_array'); |
|
| 279 | - foreach ($strings as &$string) { |
|
| 280 | - $string['type'] = isset($string['p1']) ? 'plural' : 'single'; |
|
| 281 | - $string = wp_parse_args($string, $defaultString); |
|
| 282 | - } |
|
| 283 | - return array_filter($strings, function ($string) { |
|
| 284 | - return !empty($string['id']); |
|
| 285 | - }); |
|
| 286 | - } |
|
| 272 | + /** |
|
| 273 | + * @return array |
|
| 274 | + */ |
|
| 275 | + protected function normalizeSettings(array $strings) |
|
| 276 | + { |
|
| 277 | + $defaultString = array_fill_keys(['id', 's1', 's2', 'p1', 'p2'], ''); |
|
| 278 | + $strings = array_filter($strings, 'is_array'); |
|
| 279 | + foreach ($strings as &$string) { |
|
| 280 | + $string['type'] = isset($string['p1']) ? 'plural' : 'single'; |
|
| 281 | + $string = wp_parse_args($string, $defaultString); |
|
| 282 | + } |
|
| 283 | + return array_filter($strings, function ($string) { |
|
| 284 | + return !empty($string['id']); |
|
| 285 | + }); |
|
| 286 | + } |
|
| 287 | 287 | } |
@@ -32,10 +32,10 @@ discard block |
||
| 32 | 32 | public function all() |
| 33 | 33 | { |
| 34 | 34 | $translations = $this->translations(); |
| 35 | - $entries = $this->filter($translations, $this->entries())->results(); |
|
| 36 | - array_walk($translations, function (&$entry) use ($entries) { |
|
| 37 | - $entry['desc'] = array_key_exists($entry['id'], $entries) |
|
| 38 | - ? $this->getEntryString($entries[$entry['id']], 'msgctxt') |
|
| 35 | + $entries = $this->filter( $translations, $this->entries() )->results(); |
|
| 36 | + array_walk( $translations, function( &$entry ) use ($entries) { |
|
| 37 | + $entry['desc'] = array_key_exists( $entry['id'], $entries ) |
|
| 38 | + ? $this->getEntryString( $entries[$entry['id']], 'msgctxt' ) |
|
| 39 | 39 | : ''; |
| 40 | 40 | }); |
| 41 | 41 | return $translations; |
@@ -46,10 +46,10 @@ discard block |
||
| 46 | 46 | */ |
| 47 | 47 | public function entries() |
| 48 | 48 | { |
| 49 | - if (!isset($this->entries)) { |
|
| 50 | - $potFile = glsr()->path(glsr()->languages.'/'.Application::ID.'.pot'); |
|
| 51 | - $entries = $this->extractEntriesFromPotFile($potFile); |
|
| 52 | - $entries = glsr()->filterArray('translation/entries', $entries); |
|
| 49 | + if( !isset($this->entries) ) { |
|
| 50 | + $potFile = glsr()->path( glsr()->languages.'/'.Application::ID.'.pot' ); |
|
| 51 | + $entries = $this->extractEntriesFromPotFile( $potFile ); |
|
| 52 | + $entries = glsr()->filterArray( 'translation/entries', $entries ); |
|
| 53 | 53 | $this->entries = $entries; |
| 54 | 54 | } |
| 55 | 55 | return $this->entries; |
@@ -60,27 +60,27 @@ discard block |
||
| 60 | 60 | * @param array|null $entries |
| 61 | 61 | * @return static |
| 62 | 62 | */ |
| 63 | - public function exclude($entriesToExclude = null, $entries = null) |
|
| 63 | + public function exclude( $entriesToExclude = null, $entries = null ) |
|
| 64 | 64 | { |
| 65 | - return $this->filter($entriesToExclude, $entries, false); |
|
| 65 | + return $this->filter( $entriesToExclude, $entries, false ); |
|
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | /** |
| 69 | 69 | * @param string $potFile |
| 70 | 70 | * @return array |
| 71 | 71 | */ |
| 72 | - public function extractEntriesFromPotFile($potFile, array $entries = []) |
|
| 72 | + public function extractEntriesFromPotFile( $potFile, array $entries = [] ) |
|
| 73 | 73 | { |
| 74 | 74 | try { |
| 75 | - $potEntries = $this->normalize(Parser::parseFile($potFile)->getEntries()); |
|
| 76 | - foreach ($potEntries as $key => $entry) { |
|
| 77 | - if (Str::contains(static::CONTEXT_ADMIN_KEY, Arr::get($entry, 'msgctxt'))) { |
|
| 75 | + $potEntries = $this->normalize( Parser::parseFile( $potFile )->getEntries() ); |
|
| 76 | + foreach( $potEntries as $key => $entry ) { |
|
| 77 | + if( Str::contains( static::CONTEXT_ADMIN_KEY, Arr::get( $entry, 'msgctxt' ) ) ) { |
|
| 78 | 78 | continue; |
| 79 | 79 | } |
| 80 | - $entries[html_entity_decode($key, ENT_COMPAT, 'UTF-8')] = $entry; |
|
| 80 | + $entries[html_entity_decode( $key, ENT_COMPAT, 'UTF-8' )] = $entry; |
|
| 81 | 81 | } |
| 82 | - } catch (Exception $e) { |
|
| 83 | - glsr_log()->error($e->getMessage()); |
|
| 82 | + } catch( Exception $e ) { |
|
| 83 | + glsr_log()->error( $e->getMessage() ); |
|
| 84 | 84 | } |
| 85 | 85 | return $entries; |
| 86 | 86 | } |
@@ -91,18 +91,18 @@ discard block |
||
| 91 | 91 | * @param bool $intersect |
| 92 | 92 | * @return static |
| 93 | 93 | */ |
| 94 | - public function filter($filterWith = null, $entries = null, $intersect = true) |
|
| 94 | + public function filter( $filterWith = null, $entries = null, $intersect = true ) |
|
| 95 | 95 | { |
| 96 | - if (!is_array($entries)) { |
|
| 96 | + if( !is_array( $entries ) ) { |
|
| 97 | 97 | $entries = $this->results; |
| 98 | 98 | } |
| 99 | - if (!is_array($filterWith)) { |
|
| 99 | + if( !is_array( $filterWith ) ) { |
|
| 100 | 100 | $filterWith = $this->translations(); |
| 101 | 101 | } |
| 102 | - $keys = array_flip(wp_list_pluck($filterWith, 'id')); |
|
| 102 | + $keys = array_flip( wp_list_pluck( $filterWith, 'id' ) ); |
|
| 103 | 103 | $this->results = $intersect |
| 104 | - ? array_intersect_key($entries, $keys) |
|
| 105 | - : array_diff_key($entries, $keys); |
|
| 104 | + ? array_intersect_key( $entries, $keys ) |
|
| 105 | + : array_diff_key( $entries, $keys ); |
|
| 106 | 106 | return $this; |
| 107 | 107 | } |
| 108 | 108 | |
@@ -110,20 +110,20 @@ discard block |
||
| 110 | 110 | * @param string $template |
| 111 | 111 | * @return string |
| 112 | 112 | */ |
| 113 | - public function render($template, array $entry) |
|
| 113 | + public function render( $template, array $entry ) |
|
| 114 | 114 | { |
| 115 | 115 | $data = array_combine( |
| 116 | - array_map(function ($key) { return 'data.'.$key; }, array_keys($entry)), |
|
| 116 | + array_map( function( $key ) { return 'data.'.$key; }, array_keys( $entry ) ), |
|
| 117 | 117 | $entry |
| 118 | 118 | ); |
| 119 | 119 | $data['data.class'] = $data['data.error'] = ''; |
| 120 | - if (false === array_search($entry['s1'], wp_list_pluck($this->entries(), 'msgid'))) { |
|
| 120 | + if( false === array_search( $entry['s1'], wp_list_pluck( $this->entries(), 'msgid' ) ) ) { |
|
| 121 | 121 | $data['data.class'] = 'is-invalid'; |
| 122 | - $data['data.error'] = _x('This custom translation is no longer valid as the original text has been changed or removed.', 'admin-text', 'site-reviews'); |
|
| 122 | + $data['data.error'] = _x( 'This custom translation is no longer valid as the original text has been changed or removed.', 'admin-text', 'site-reviews' ); |
|
| 123 | 123 | } |
| 124 | - return glsr(Template::class)->build('partials/translations/'.$template, [ |
|
| 125 | - 'context' => array_map('esc_html', $data), |
|
| 126 | - ]); |
|
| 124 | + return glsr( Template::class )->build( 'partials/translations/'.$template, [ |
|
| 125 | + 'context' => array_map( 'esc_html', $data ), |
|
| 126 | + ] ); |
|
| 127 | 127 | } |
| 128 | 128 | |
| 129 | 129 | /** |
@@ -133,10 +133,10 @@ discard block |
||
| 133 | 133 | public function renderAll() |
| 134 | 134 | { |
| 135 | 135 | $rendered = ''; |
| 136 | - foreach ($this->all() as $index => $entry) { |
|
| 136 | + foreach( $this->all() as $index => $entry ) { |
|
| 137 | 137 | $entry['index'] = $index; |
| 138 | 138 | $entry['prefix'] = OptionManager::databaseKey(); |
| 139 | - $rendered.= $this->render($entry['type'], $entry); |
|
| 139 | + $rendered .= $this->render( $entry['type'], $entry ); |
|
| 140 | 140 | } |
| 141 | 141 | return $rendered; |
| 142 | 142 | } |
@@ -145,25 +145,25 @@ discard block |
||
| 145 | 145 | * @param bool $resetAfterRender |
| 146 | 146 | * @return string |
| 147 | 147 | */ |
| 148 | - public function renderResults($resetAfterRender = true) |
|
| 148 | + public function renderResults( $resetAfterRender = true ) |
|
| 149 | 149 | { |
| 150 | 150 | $rendered = ''; |
| 151 | - foreach ($this->results as $id => $entry) { |
|
| 151 | + foreach( $this->results as $id => $entry ) { |
|
| 152 | 152 | $data = [ |
| 153 | - 'desc' => $this->getEntryString($entry, 'msgctxt'), |
|
| 153 | + 'desc' => $this->getEntryString( $entry, 'msgctxt' ), |
|
| 154 | 154 | 'id' => $id, |
| 155 | - 'p1' => $this->getEntryString($entry, 'msgid_plural'), |
|
| 156 | - 's1' => $this->getEntryString($entry, 'msgid'), |
|
| 155 | + 'p1' => $this->getEntryString( $entry, 'msgid_plural' ), |
|
| 156 | + 's1' => $this->getEntryString( $entry, 'msgid' ), |
|
| 157 | 157 | ]; |
| 158 | 158 | $text = !empty($data['p1']) |
| 159 | - ? sprintf('%s | %s', $data['s1'], $data['p1']) |
|
| 159 | + ? sprintf( '%s | %s', $data['s1'], $data['p1'] ) |
|
| 160 | 160 | : $data['s1']; |
| 161 | - $rendered.= $this->render('result', [ |
|
| 162 | - 'entry' => json_encode($data, JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_TAG | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), |
|
| 163 | - 'text' => wp_strip_all_tags($text), |
|
| 164 | - ]); |
|
| 161 | + $rendered .= $this->render( 'result', [ |
|
| 162 | + 'entry' => json_encode( $data, JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_TAG | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ), |
|
| 163 | + 'text' => wp_strip_all_tags( $text ), |
|
| 164 | + ] ); |
|
| 165 | 165 | } |
| 166 | - if ($resetAfterRender) { |
|
| 166 | + if( $resetAfterRender ) { |
|
| 167 | 167 | $this->reset(); |
| 168 | 168 | } |
| 169 | 169 | return $rendered; |
@@ -191,18 +191,18 @@ discard block |
||
| 191 | 191 | * @param string $needle |
| 192 | 192 | * @return static |
| 193 | 193 | */ |
| 194 | - public function search($needle = '') |
|
| 194 | + public function search( $needle = '' ) |
|
| 195 | 195 | { |
| 196 | 196 | $this->reset(); |
| 197 | - $needle = trim(strtolower($needle)); |
|
| 198 | - foreach ($this->entries() as $key => $entry) { |
|
| 199 | - $single = strtolower($this->getEntryString($entry, 'msgid')); |
|
| 200 | - $plural = strtolower($this->getEntryString($entry, 'msgid_plural')); |
|
| 201 | - if (strlen($needle) < static::SEARCH_THRESHOLD) { |
|
| 202 | - if (in_array($needle, [$single, $plural])) { |
|
| 197 | + $needle = trim( strtolower( $needle ) ); |
|
| 198 | + foreach( $this->entries() as $key => $entry ) { |
|
| 199 | + $single = strtolower( $this->getEntryString( $entry, 'msgid' ) ); |
|
| 200 | + $plural = strtolower( $this->getEntryString( $entry, 'msgid_plural' ) ); |
|
| 201 | + if( strlen( $needle ) < static::SEARCH_THRESHOLD ) { |
|
| 202 | + if( in_array( $needle, [$single, $plural] ) ) { |
|
| 203 | 203 | $this->results[$key] = $entry; |
| 204 | 204 | } |
| 205 | - } elseif (Str::contains(sprintf('%s %s', $single, $plural), $needle)) { |
|
| 205 | + } elseif( Str::contains( sprintf( '%s %s', $single, $plural ), $needle ) ) { |
|
| 206 | 206 | $this->results[$key] = $entry; |
| 207 | 207 | } |
| 208 | 208 | } |
@@ -216,10 +216,10 @@ discard block |
||
| 216 | 216 | public function translations() |
| 217 | 217 | { |
| 218 | 218 | static $translations; |
| 219 | - if (empty($translations)) { |
|
| 220 | - $settings = glsr(OptionManager::class)->get('settings'); |
|
| 219 | + if( empty($translations) ) { |
|
| 220 | + $settings = glsr( OptionManager::class )->get( 'settings' ); |
|
| 221 | 221 | $translations = isset($settings['strings']) |
| 222 | - ? $this->normalizeSettings((array) $settings['strings']) |
|
| 222 | + ? $this->normalizeSettings( (array)$settings['strings'] ) |
|
| 223 | 223 | : []; |
| 224 | 224 | } |
| 225 | 225 | return $translations; |
@@ -229,28 +229,28 @@ discard block |
||
| 229 | 229 | * @param string $key |
| 230 | 230 | * @return string |
| 231 | 231 | */ |
| 232 | - protected function getEntryString(array $entry, $key) |
|
| 232 | + protected function getEntryString( array $entry, $key ) |
|
| 233 | 233 | { |
| 234 | 234 | return isset($entry[$key]) |
| 235 | - ? implode('', (array) $entry[$key]) |
|
| 235 | + ? implode( '', (array)$entry[$key] ) |
|
| 236 | 236 | : ''; |
| 237 | 237 | } |
| 238 | 238 | |
| 239 | 239 | /** |
| 240 | 240 | * @return array |
| 241 | 241 | */ |
| 242 | - protected function normalize(array $entries) |
|
| 242 | + protected function normalize( array $entries ) |
|
| 243 | 243 | { |
| 244 | 244 | $keys = [ |
| 245 | 245 | 'msgctxt', 'msgid', 'msgid_plural', 'msgstr', 'msgstr[0]', 'msgstr[1]', |
| 246 | 246 | ]; |
| 247 | - array_walk($entries, function (&$entry) use ($keys) { |
|
| 248 | - foreach ($keys as $key) { |
|
| 247 | + array_walk( $entries, function( &$entry ) use ($keys) { |
|
| 248 | + foreach( $keys as $key ) { |
|
| 249 | 249 | try { |
| 250 | - $entry = $this->normalizeEntryString($entry, $key); |
|
| 251 | - } catch (\TypeError $error) { |
|
| 252 | - glsr_log()->once('error', 'Translation/normalize', $error); |
|
| 253 | - glsr_log()->once('debug', 'Translation/normalize', $entry); |
|
| 250 | + $entry = $this->normalizeEntryString( $entry, $key ); |
|
| 251 | + } catch( \TypeError $error ) { |
|
| 252 | + glsr_log()->once( 'error', 'Translation/normalize', $error ); |
|
| 253 | + glsr_log()->once( 'debug', 'Translation/normalize', $entry ); |
|
| 254 | 254 | } |
| 255 | 255 | } |
| 256 | 256 | }); |
@@ -261,10 +261,10 @@ discard block |
||
| 261 | 261 | * @param string $key |
| 262 | 262 | * @return array |
| 263 | 263 | */ |
| 264 | - protected function normalizeEntryString(array $entry, $key) |
|
| 264 | + protected function normalizeEntryString( array $entry, $key ) |
|
| 265 | 265 | { |
| 266 | - if (isset($entry[$key])) { |
|
| 267 | - $entry[$key] = $this->getEntryString($entry, $key); |
|
| 266 | + if( isset($entry[$key]) ) { |
|
| 267 | + $entry[$key] = $this->getEntryString( $entry, $key ); |
|
| 268 | 268 | } |
| 269 | 269 | return $entry; |
| 270 | 270 | } |
@@ -272,15 +272,15 @@ discard block |
||
| 272 | 272 | /** |
| 273 | 273 | * @return array |
| 274 | 274 | */ |
| 275 | - protected function normalizeSettings(array $strings) |
|
| 275 | + protected function normalizeSettings( array $strings ) |
|
| 276 | 276 | { |
| 277 | - $defaultString = array_fill_keys(['id', 's1', 's2', 'p1', 'p2'], ''); |
|
| 278 | - $strings = array_filter($strings, 'is_array'); |
|
| 279 | - foreach ($strings as &$string) { |
|
| 277 | + $defaultString = array_fill_keys( ['id', 's1', 's2', 'p1', 'p2'], '' ); |
|
| 278 | + $strings = array_filter( $strings, 'is_array' ); |
|
| 279 | + foreach( $strings as &$string ) { |
|
| 280 | 280 | $string['type'] = isset($string['p1']) ? 'plural' : 'single'; |
| 281 | - $string = wp_parse_args($string, $defaultString); |
|
| 281 | + $string = wp_parse_args( $string, $defaultString ); |
|
| 282 | 282 | } |
| 283 | - return array_filter($strings, function ($string) { |
|
| 283 | + return array_filter( $strings, function( $string ) { |
|
| 284 | 284 | return !empty($string['id']); |
| 285 | 285 | }); |
| 286 | 286 | } |
@@ -12,67 +12,67 @@ |
||
| 12 | 12 | |
| 13 | 13 | class Pagination implements PartialContract |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * @var array |
|
| 17 | - */ |
|
| 18 | - protected $args; |
|
| 15 | + /** |
|
| 16 | + * @var array |
|
| 17 | + */ |
|
| 18 | + protected $args; |
|
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * {@inheritdoc} |
|
| 22 | - */ |
|
| 23 | - public function build(array $args = []) |
|
| 24 | - { |
|
| 25 | - $this->args = $this->normalize($args); |
|
| 26 | - if ($this->args['total'] > 1) { |
|
| 27 | - return glsr(Template::class)->build('templates/pagination', [ |
|
| 28 | - 'context' => [ |
|
| 29 | - 'links' => glsr()->filterString('paginate_links', $this->buildLinks(), $this->args), |
|
| 30 | - 'loader' => '<div class="glsr-loader"></div>', |
|
| 31 | - 'screen_reader_text' => _x('Site Reviews navigation', 'screen reader text', 'site-reviews'), |
|
| 32 | - ], |
|
| 33 | - ]); |
|
| 34 | - } |
|
| 35 | - } |
|
| 20 | + /** |
|
| 21 | + * {@inheritdoc} |
|
| 22 | + */ |
|
| 23 | + public function build(array $args = []) |
|
| 24 | + { |
|
| 25 | + $this->args = $this->normalize($args); |
|
| 26 | + if ($this->args['total'] > 1) { |
|
| 27 | + return glsr(Template::class)->build('templates/pagination', [ |
|
| 28 | + 'context' => [ |
|
| 29 | + 'links' => glsr()->filterString('paginate_links', $this->buildLinks(), $this->args), |
|
| 30 | + 'loader' => '<div class="glsr-loader"></div>', |
|
| 31 | + 'screen_reader_text' => _x('Site Reviews navigation', 'screen reader text', 'site-reviews'), |
|
| 32 | + ], |
|
| 33 | + ]); |
|
| 34 | + } |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * @return string |
|
| 39 | - */ |
|
| 40 | - protected function buildFauxLinks() |
|
| 41 | - { |
|
| 42 | - $links = (array) paginate_links(wp_parse_args(['type' => 'array'], $this->args)); |
|
| 43 | - $pattern = '/(href=["\'])([^"\']*?)(["\'])/i'; |
|
| 44 | - foreach ($links as &$link) { |
|
| 45 | - if (preg_match($pattern, $link, $matches)) { |
|
| 46 | - $page = glsr(Helper::class)->getPageNumber(Arr::get($matches, 2)); |
|
| 47 | - $replacement = sprintf('data-page="%d" href="#"', $page); |
|
| 48 | - $link = str_replace(Arr::get($matches, 0), $replacement, $link); |
|
| 49 | - } |
|
| 50 | - } |
|
| 51 | - return implode("\n", $links); |
|
| 52 | - } |
|
| 37 | + /** |
|
| 38 | + * @return string |
|
| 39 | + */ |
|
| 40 | + protected function buildFauxLinks() |
|
| 41 | + { |
|
| 42 | + $links = (array) paginate_links(wp_parse_args(['type' => 'array'], $this->args)); |
|
| 43 | + $pattern = '/(href=["\'])([^"\']*?)(["\'])/i'; |
|
| 44 | + foreach ($links as &$link) { |
|
| 45 | + if (preg_match($pattern, $link, $matches)) { |
|
| 46 | + $page = glsr(Helper::class)->getPageNumber(Arr::get($matches, 2)); |
|
| 47 | + $replacement = sprintf('data-page="%d" href="#"', $page); |
|
| 48 | + $link = str_replace(Arr::get($matches, 0), $replacement, $link); |
|
| 49 | + } |
|
| 50 | + } |
|
| 51 | + return implode("\n", $links); |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * @return string |
|
| 56 | - */ |
|
| 57 | - protected function buildLinks() |
|
| 58 | - { |
|
| 59 | - return glsr(OptionManager::class)->getBool('settings.reviews.pagination.url_parameter') |
|
| 60 | - ? paginate_links(wp_parse_args(['type' => 'plain'], $this->args)) |
|
| 61 | - : $this->buildFauxLinks(); |
|
| 62 | - } |
|
| 54 | + /** |
|
| 55 | + * @return string |
|
| 56 | + */ |
|
| 57 | + protected function buildLinks() |
|
| 58 | + { |
|
| 59 | + return glsr(OptionManager::class)->getBool('settings.reviews.pagination.url_parameter') |
|
| 60 | + ? paginate_links(wp_parse_args(['type' => 'plain'], $this->args)) |
|
| 61 | + : $this->buildFauxLinks(); |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - /** |
|
| 65 | - * @return array |
|
| 66 | - */ |
|
| 67 | - protected function normalize(array $args) |
|
| 68 | - { |
|
| 69 | - if ($baseUrl = Arr::get($args, 'baseUrl')) { |
|
| 70 | - $args['base'] = $baseUrl.'%_%'; |
|
| 71 | - } |
|
| 72 | - $args = wp_parse_args(array_filter($args), [ |
|
| 73 | - 'current' => glsr(Helper::class)->getPageNumber(), |
|
| 74 | - 'total' => 1, |
|
| 75 | - ]); |
|
| 76 | - return glsr(Style::class)->paginationArgs($args); |
|
| 77 | - } |
|
| 64 | + /** |
|
| 65 | + * @return array |
|
| 66 | + */ |
|
| 67 | + protected function normalize(array $args) |
|
| 68 | + { |
|
| 69 | + if ($baseUrl = Arr::get($args, 'baseUrl')) { |
|
| 70 | + $args['base'] = $baseUrl.'%_%'; |
|
| 71 | + } |
|
| 72 | + $args = wp_parse_args(array_filter($args), [ |
|
| 73 | + 'current' => glsr(Helper::class)->getPageNumber(), |
|
| 74 | + 'total' => 1, |
|
| 75 | + ]); |
|
| 76 | + return glsr(Style::class)->paginationArgs($args); |
|
| 77 | + } |
|
| 78 | 78 | } |
@@ -20,17 +20,17 @@ discard block |
||
| 20 | 20 | /** |
| 21 | 21 | * {@inheritdoc} |
| 22 | 22 | */ |
| 23 | - public function build(array $args = []) |
|
| 23 | + public function build( array $args = [] ) |
|
| 24 | 24 | { |
| 25 | - $this->args = $this->normalize($args); |
|
| 26 | - if ($this->args['total'] > 1) { |
|
| 27 | - return glsr(Template::class)->build('templates/pagination', [ |
|
| 25 | + $this->args = $this->normalize( $args ); |
|
| 26 | + if( $this->args['total'] > 1 ) { |
|
| 27 | + return glsr( Template::class )->build( 'templates/pagination', [ |
|
| 28 | 28 | 'context' => [ |
| 29 | - 'links' => glsr()->filterString('paginate_links', $this->buildLinks(), $this->args), |
|
| 29 | + 'links' => glsr()->filterString( 'paginate_links', $this->buildLinks(), $this->args ), |
|
| 30 | 30 | 'loader' => '<div class="glsr-loader"></div>', |
| 31 | - 'screen_reader_text' => _x('Site Reviews navigation', 'screen reader text', 'site-reviews'), |
|
| 31 | + 'screen_reader_text' => _x( 'Site Reviews navigation', 'screen reader text', 'site-reviews' ), |
|
| 32 | 32 | ], |
| 33 | - ]); |
|
| 33 | + ] ); |
|
| 34 | 34 | } |
| 35 | 35 | } |
| 36 | 36 | |
@@ -39,16 +39,16 @@ discard block |
||
| 39 | 39 | */ |
| 40 | 40 | protected function buildFauxLinks() |
| 41 | 41 | { |
| 42 | - $links = (array) paginate_links(wp_parse_args(['type' => 'array'], $this->args)); |
|
| 42 | + $links = (array)paginate_links( wp_parse_args( ['type' => 'array'], $this->args ) ); |
|
| 43 | 43 | $pattern = '/(href=["\'])([^"\']*?)(["\'])/i'; |
| 44 | - foreach ($links as &$link) { |
|
| 45 | - if (preg_match($pattern, $link, $matches)) { |
|
| 46 | - $page = glsr(Helper::class)->getPageNumber(Arr::get($matches, 2)); |
|
| 47 | - $replacement = sprintf('data-page="%d" href="#"', $page); |
|
| 48 | - $link = str_replace(Arr::get($matches, 0), $replacement, $link); |
|
| 44 | + foreach( $links as &$link ) { |
|
| 45 | + if( preg_match( $pattern, $link, $matches ) ) { |
|
| 46 | + $page = glsr( Helper::class )->getPageNumber( Arr::get( $matches, 2 ) ); |
|
| 47 | + $replacement = sprintf( 'data-page="%d" href="#"', $page ); |
|
| 48 | + $link = str_replace( Arr::get( $matches, 0 ), $replacement, $link ); |
|
| 49 | 49 | } |
| 50 | 50 | } |
| 51 | - return implode("\n", $links); |
|
| 51 | + return implode( "\n", $links ); |
|
| 52 | 52 | } |
| 53 | 53 | |
| 54 | 54 | /** |
@@ -56,23 +56,23 @@ discard block |
||
| 56 | 56 | */ |
| 57 | 57 | protected function buildLinks() |
| 58 | 58 | { |
| 59 | - return glsr(OptionManager::class)->getBool('settings.reviews.pagination.url_parameter') |
|
| 60 | - ? paginate_links(wp_parse_args(['type' => 'plain'], $this->args)) |
|
| 59 | + return glsr( OptionManager::class )->getBool( 'settings.reviews.pagination.url_parameter' ) |
|
| 60 | + ? paginate_links( wp_parse_args( ['type' => 'plain'], $this->args ) ) |
|
| 61 | 61 | : $this->buildFauxLinks(); |
| 62 | 62 | } |
| 63 | 63 | |
| 64 | 64 | /** |
| 65 | 65 | * @return array |
| 66 | 66 | */ |
| 67 | - protected function normalize(array $args) |
|
| 67 | + protected function normalize( array $args ) |
|
| 68 | 68 | { |
| 69 | - if ($baseUrl = Arr::get($args, 'baseUrl')) { |
|
| 69 | + if( $baseUrl = Arr::get( $args, 'baseUrl' ) ) { |
|
| 70 | 70 | $args['base'] = $baseUrl.'%_%'; |
| 71 | 71 | } |
| 72 | - $args = wp_parse_args(array_filter($args), [ |
|
| 73 | - 'current' => glsr(Helper::class)->getPageNumber(), |
|
| 72 | + $args = wp_parse_args( array_filter( $args ), [ |
|
| 73 | + 'current' => glsr( Helper::class )->getPageNumber(), |
|
| 74 | 74 | 'total' => 1, |
| 75 | - ]); |
|
| 76 | - return glsr(Style::class)->paginationArgs($args); |
|
| 75 | + ] ); |
|
| 76 | + return glsr( Style::class )->paginationArgs( $args ); |
|
| 77 | 77 | } |
| 78 | 78 | } |
@@ -7,78 +7,78 @@ |
||
| 7 | 7 | |
| 8 | 8 | class SummaryPercentagesTag extends SummaryTag |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * {@inheritdoc} |
|
| 12 | - */ |
|
| 13 | - protected function handle($value = null) |
|
| 14 | - { |
|
| 15 | - if (!$this->isHidden()) { |
|
| 16 | - return $this->wrap($this->percentages(), 'span'); |
|
| 17 | - } |
|
| 18 | - } |
|
| 10 | + /** |
|
| 11 | + * {@inheritdoc} |
|
| 12 | + */ |
|
| 13 | + protected function handle($value = null) |
|
| 14 | + { |
|
| 15 | + if (!$this->isHidden()) { |
|
| 16 | + return $this->wrap($this->percentages(), 'span'); |
|
| 17 | + } |
|
| 18 | + } |
|
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * @return void|string |
|
| 22 | - */ |
|
| 23 | - protected function percentages() |
|
| 24 | - { |
|
| 25 | - $percentages = preg_filter('/$/', '%', glsr(Rating::class)->percentages($this->ratings)); |
|
| 26 | - $ratingRange = range(glsr()->constant('MAX_RATING', Rating::class), 1); |
|
| 27 | - return array_reduce($ratingRange, function ($carry, $level) use ($percentages) { |
|
| 28 | - $label = $this->ratingLabel($level); |
|
| 29 | - $bar = $this->ratingBar($level, $percentages); |
|
| 30 | - $info = $this->ratingInfo($level, $percentages); |
|
| 31 | - $value = $label.$bar.$info; |
|
| 32 | - $value = glsr()->filterString('summary/wrap/bar', $value, $this->args, [ |
|
| 33 | - 'info' => wp_strip_all_tags($info, true), |
|
| 34 | - 'rating' => $level, |
|
| 35 | - ]); |
|
| 36 | - return $carry.glsr(Builder::class)->div([ |
|
| 37 | - 'class' => 'glsr-bar', |
|
| 38 | - 'text' => $value, |
|
| 39 | - ]); |
|
| 40 | - }); |
|
| 41 | - } |
|
| 20 | + /** |
|
| 21 | + * @return void|string |
|
| 22 | + */ |
|
| 23 | + protected function percentages() |
|
| 24 | + { |
|
| 25 | + $percentages = preg_filter('/$/', '%', glsr(Rating::class)->percentages($this->ratings)); |
|
| 26 | + $ratingRange = range(glsr()->constant('MAX_RATING', Rating::class), 1); |
|
| 27 | + return array_reduce($ratingRange, function ($carry, $level) use ($percentages) { |
|
| 28 | + $label = $this->ratingLabel($level); |
|
| 29 | + $bar = $this->ratingBar($level, $percentages); |
|
| 30 | + $info = $this->ratingInfo($level, $percentages); |
|
| 31 | + $value = $label.$bar.$info; |
|
| 32 | + $value = glsr()->filterString('summary/wrap/bar', $value, $this->args, [ |
|
| 33 | + 'info' => wp_strip_all_tags($info, true), |
|
| 34 | + 'rating' => $level, |
|
| 35 | + ]); |
|
| 36 | + return $carry.glsr(Builder::class)->div([ |
|
| 37 | + 'class' => 'glsr-bar', |
|
| 38 | + 'text' => $value, |
|
| 39 | + ]); |
|
| 40 | + }); |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * @param int $level |
|
| 45 | - * @return string |
|
| 46 | - */ |
|
| 47 | - protected function ratingBar($level, array $percentages) |
|
| 48 | - { |
|
| 49 | - $background = glsr(Builder::class)->span([ |
|
| 50 | - 'class' => 'glsr-bar-background-percent', |
|
| 51 | - 'style' => 'width:'.$percentages[$level], |
|
| 52 | - ]); |
|
| 53 | - return glsr(Builder::class)->span([ |
|
| 54 | - 'class' => 'glsr-bar-background', |
|
| 55 | - 'text' => $background, |
|
| 56 | - ]); |
|
| 57 | - } |
|
| 43 | + /** |
|
| 44 | + * @param int $level |
|
| 45 | + * @return string |
|
| 46 | + */ |
|
| 47 | + protected function ratingBar($level, array $percentages) |
|
| 48 | + { |
|
| 49 | + $background = glsr(Builder::class)->span([ |
|
| 50 | + 'class' => 'glsr-bar-background-percent', |
|
| 51 | + 'style' => 'width:'.$percentages[$level], |
|
| 52 | + ]); |
|
| 53 | + return glsr(Builder::class)->span([ |
|
| 54 | + 'class' => 'glsr-bar-background', |
|
| 55 | + 'text' => $background, |
|
| 56 | + ]); |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - /** |
|
| 60 | - * @param int $level |
|
| 61 | - * @return string |
|
| 62 | - */ |
|
| 63 | - protected function ratingInfo($level, array $percentages) |
|
| 64 | - { |
|
| 65 | - $count = glsr()->filterString('summary/counts', $percentages[$level], $this->ratings[$level]); |
|
| 66 | - return glsr(Builder::class)->span([ |
|
| 67 | - 'class' => 'glsr-bar-percent', |
|
| 68 | - 'text' => $count, |
|
| 69 | - ]); |
|
| 70 | - } |
|
| 59 | + /** |
|
| 60 | + * @param int $level |
|
| 61 | + * @return string |
|
| 62 | + */ |
|
| 63 | + protected function ratingInfo($level, array $percentages) |
|
| 64 | + { |
|
| 65 | + $count = glsr()->filterString('summary/counts', $percentages[$level], $this->ratings[$level]); |
|
| 66 | + return glsr(Builder::class)->span([ |
|
| 67 | + 'class' => 'glsr-bar-percent', |
|
| 68 | + 'text' => $count, |
|
| 69 | + ]); |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * @param int $level |
|
| 74 | - * @return string |
|
| 75 | - */ |
|
| 76 | - protected function ratingLabel($level) |
|
| 77 | - { |
|
| 78 | - $label = $this->args->get('labels.'.$level); |
|
| 79 | - return glsr(Builder::class)->span([ |
|
| 80 | - 'class' => 'glsr-bar-label', |
|
| 81 | - 'text' => $label, |
|
| 82 | - ]); |
|
| 83 | - } |
|
| 72 | + /** |
|
| 73 | + * @param int $level |
|
| 74 | + * @return string |
|
| 75 | + */ |
|
| 76 | + protected function ratingLabel($level) |
|
| 77 | + { |
|
| 78 | + $label = $this->args->get('labels.'.$level); |
|
| 79 | + return glsr(Builder::class)->span([ |
|
| 80 | + 'class' => 'glsr-bar-label', |
|
| 81 | + 'text' => $label, |
|
| 82 | + ]); |
|
| 83 | + } |
|
| 84 | 84 | } |
@@ -10,10 +10,10 @@ discard block |
||
| 10 | 10 | /** |
| 11 | 11 | * {@inheritdoc} |
| 12 | 12 | */ |
| 13 | - protected function handle($value = null) |
|
| 13 | + protected function handle( $value = null ) |
|
| 14 | 14 | { |
| 15 | - if (!$this->isHidden()) { |
|
| 16 | - return $this->wrap($this->percentages(), 'span'); |
|
| 15 | + if( !$this->isHidden() ) { |
|
| 16 | + return $this->wrap( $this->percentages(), 'span' ); |
|
| 17 | 17 | } |
| 18 | 18 | } |
| 19 | 19 | |
@@ -22,21 +22,21 @@ discard block |
||
| 22 | 22 | */ |
| 23 | 23 | protected function percentages() |
| 24 | 24 | { |
| 25 | - $percentages = preg_filter('/$/', '%', glsr(Rating::class)->percentages($this->ratings)); |
|
| 26 | - $ratingRange = range(glsr()->constant('MAX_RATING', Rating::class), 1); |
|
| 27 | - return array_reduce($ratingRange, function ($carry, $level) use ($percentages) { |
|
| 28 | - $label = $this->ratingLabel($level); |
|
| 29 | - $bar = $this->ratingBar($level, $percentages); |
|
| 30 | - $info = $this->ratingInfo($level, $percentages); |
|
| 25 | + $percentages = preg_filter( '/$/', '%', glsr( Rating::class )->percentages( $this->ratings ) ); |
|
| 26 | + $ratingRange = range( glsr()->constant( 'MAX_RATING', Rating::class ), 1 ); |
|
| 27 | + return array_reduce( $ratingRange, function( $carry, $level ) use ($percentages) { |
|
| 28 | + $label = $this->ratingLabel( $level ); |
|
| 29 | + $bar = $this->ratingBar( $level, $percentages ); |
|
| 30 | + $info = $this->ratingInfo( $level, $percentages ); |
|
| 31 | 31 | $value = $label.$bar.$info; |
| 32 | - $value = glsr()->filterString('summary/wrap/bar', $value, $this->args, [ |
|
| 33 | - 'info' => wp_strip_all_tags($info, true), |
|
| 32 | + $value = glsr()->filterString( 'summary/wrap/bar', $value, $this->args, [ |
|
| 33 | + 'info' => wp_strip_all_tags( $info, true ), |
|
| 34 | 34 | 'rating' => $level, |
| 35 | - ]); |
|
| 36 | - return $carry.glsr(Builder::class)->div([ |
|
| 35 | + ] ); |
|
| 36 | + return $carry.glsr( Builder::class )->div( [ |
|
| 37 | 37 | 'class' => 'glsr-bar', |
| 38 | 38 | 'text' => $value, |
| 39 | - ]); |
|
| 39 | + ] ); |
|
| 40 | 40 | }); |
| 41 | 41 | } |
| 42 | 42 | |
@@ -44,41 +44,41 @@ discard block |
||
| 44 | 44 | * @param int $level |
| 45 | 45 | * @return string |
| 46 | 46 | */ |
| 47 | - protected function ratingBar($level, array $percentages) |
|
| 47 | + protected function ratingBar( $level, array $percentages ) |
|
| 48 | 48 | { |
| 49 | - $background = glsr(Builder::class)->span([ |
|
| 49 | + $background = glsr( Builder::class )->span( [ |
|
| 50 | 50 | 'class' => 'glsr-bar-background-percent', |
| 51 | 51 | 'style' => 'width:'.$percentages[$level], |
| 52 | - ]); |
|
| 53 | - return glsr(Builder::class)->span([ |
|
| 52 | + ] ); |
|
| 53 | + return glsr( Builder::class )->span( [ |
|
| 54 | 54 | 'class' => 'glsr-bar-background', |
| 55 | 55 | 'text' => $background, |
| 56 | - ]); |
|
| 56 | + ] ); |
|
| 57 | 57 | } |
| 58 | 58 | |
| 59 | 59 | /** |
| 60 | 60 | * @param int $level |
| 61 | 61 | * @return string |
| 62 | 62 | */ |
| 63 | - protected function ratingInfo($level, array $percentages) |
|
| 63 | + protected function ratingInfo( $level, array $percentages ) |
|
| 64 | 64 | { |
| 65 | - $count = glsr()->filterString('summary/counts', $percentages[$level], $this->ratings[$level]); |
|
| 66 | - return glsr(Builder::class)->span([ |
|
| 65 | + $count = glsr()->filterString( 'summary/counts', $percentages[$level], $this->ratings[$level] ); |
|
| 66 | + return glsr( Builder::class )->span( [ |
|
| 67 | 67 | 'class' => 'glsr-bar-percent', |
| 68 | 68 | 'text' => $count, |
| 69 | - ]); |
|
| 69 | + ] ); |
|
| 70 | 70 | } |
| 71 | 71 | |
| 72 | 72 | /** |
| 73 | 73 | * @param int $level |
| 74 | 74 | * @return string |
| 75 | 75 | */ |
| 76 | - protected function ratingLabel($level) |
|
| 76 | + protected function ratingLabel( $level ) |
|
| 77 | 77 | { |
| 78 | - $label = $this->args->get('labels.'.$level); |
|
| 79 | - return glsr(Builder::class)->span([ |
|
| 78 | + $label = $this->args->get( 'labels.'.$level ); |
|
| 79 | + return glsr( Builder::class )->span( [ |
|
| 80 | 80 | 'class' => 'glsr-bar-label', |
| 81 | 81 | 'text' => $label, |
| 82 | - ]); |
|
| 82 | + ] ); |
|
| 83 | 83 | } |
| 84 | 84 | } |