Test Failed
Push — develop ( 6a684d...5c54e5 )
by Paul
08:51
created

DirectoryController   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 168
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 24
eloc 80
c 1
b 0
f 1
dl 0
loc 168
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A registerProfileRatingShortcode() 0 19 5
A insertPreviewCss() 0 12 3
A filterAvailableShortcodes() 0 8 1
A filterMetaBoxSettings() 0 20 4
A registerProfileBuilderField() 0 10 3
A filterInlineStyles() 0 5 1
A filterMemberDirectoryTheme() 0 10 3
A filterMemberDirectoryArgs() 0 32 4
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Integrations\ProfilePress\Controllers;
4
5
use GeminiLabs\SiteReviews\Commands\EnqueuePublicAssets;
6
use GeminiLabs\SiteReviews\Controllers\AbstractController;
7
use GeminiLabs\SiteReviews\Database\CountManager;
8
use GeminiLabs\SiteReviews\Helper;
9
use GeminiLabs\SiteReviews\Helpers\Arr;
10
use GeminiLabs\SiteReviews\Integrations\ProfilePress\RatingField;
11
use GeminiLabs\SiteReviews\Modules\Html\Builder;
12
use ProfilePress\Core\Admin\SettingsPages\DragDropBuilder\DragDropBuilder;
0 ignored issues
show
Bug introduced by
The type ProfilePress\Core\Admin\...Builder\DragDropBuilder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use ProfilePress\Core\Classes\FormRepository;
0 ignored issues
show
Bug introduced by
The type ProfilePress\Core\Classes\FormRepository was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
15
class DirectoryController extends AbstractController
16
{
17
    /**
18
     * @param array $shortcodes
19
     *
20
     * @filter ppress_user_profile_available_shortcodes
21
     */
22
    public function filterAvailableShortcodes($shortcodes): array
23
    {
24
        $shortcodes = Arr::consolidate($shortcodes);
25
        $shortcodes['profile-rating'] = [
26
            'description' => esc_html_x('Rating of user', 'admin-text', 'site-reviews'),
27
            'shortcode' => 'profile-rating',
28
        ];
29
        return $shortcodes;
30
    }
31
32
    /**
33
     * @filter site-reviews/enqueue/public/inline-styles
34
     */
35
    public function filterInlineStyles(string $css): string
36
    {
37
        $css .= '.pp-member-directory .pp-member-rating {display:flex;justify-content:center;}';
38
        $css .= '.pp-member-directory .pp-member-rating * {display:inline-flex;}';
39
        return $css;
40
    }
41
42
    /**
43
     * @param array $args
44
     *
45
     * @filter ppress_member_directory_wp_user_args
46
     */
47
    public function filterMemberDirectoryArgs($args): array
48
    {
49
        $args = Arr::consolidate($args);
50
        $sortby = $args['meta_key'] ?? '';
51
        if (!in_array($sortby, [RatingField::LOW_RATED, RatingField::HIGH_RATED])) {
52
            return $args;
53
        }
54
        $order = RatingField::HIGH_RATED === $sortby ? 'DESC' : 'ASC';
55
        $sortKey = 'bayesian' === glsr_get_option('integrations.profilepress.directory_sorting')
56
            ? CountManager::META_RANKING
57
            : CountManager::META_AVERAGE;
58
        $args['meta_query'] ??= [];
59
        $args['meta_query'][] = [
60
            'relation' => 'OR',
61
            [
62
                'compare' => 'NOT EXISTS',
63
                'key' => $sortKey,
64
                'type' => 'NUMERIC',
65
            ],
66
            '_rating_key' => [
67
                'compare' => 'EXISTS',
68
                'key' => $sortKey,
69
                'type' => 'NUMERIC',
70
            ],
71
        ];
72
        $args['orderby'] = [
73
            '_rating_key' => $order,
74
            'user_registered' => 'DESC',
75
        ];
76
        unset($args['meta_key']);
77
        unset($args['order']);
78
        return $args;
79
    }
80
81
    /**
82
     * @param string $className
83
     * @param string $formTheme
84
     * @param string $formType
85
     *
86
     * @filter ppress_register_dnd_form_class
87
     */
88
    public function filterMemberDirectoryTheme($className, $formTheme, $formType): string
89
    {
90
        if ('MemberDirectory' !== $formType) {
91
            return (string) $className;
92
        }
93
        $override = Helper::buildClassName((string) $formTheme, 'Integrations\ProfilePress\MemberDirectory');
94
        if (!class_exists($override)) {
95
            return (string) $className;
96
        }
97
        return $override;
98
    }
99
100
    /**
101
     * @param array $settings
102
     *
103
     * @filter ppress_form_builder_meta_box_settings
104
     */
105
    public function filterMetaBoxSettings($settings): array
106
    {
107
        $settings = Arr::consolidate($settings);
108
        $sorting = $settings['ppress_md_sorting'] ?? [];
109
        if (empty($sorting)) {
110
            return $settings;
111
        }
112
        foreach (['ppress_md_sort_default', 'ppress_md_sort_method_fields'] as $id) {
113
            $index = array_search($id, array_column($sorting, 'id'));
114
            if (false === $index) {
115
                continue;
116
            }
117
            $group = _x('Review Fields', 'admin-text', 'site-reviews');
118
            $sorting[$index]['options'][$group] = [
119
                RatingField::HIGH_RATED => _x('Highest rated first', 'admin-text', 'site-reviews'),
120
                RatingField::LOW_RATED => _x('Lowest rated first', 'admin-text', 'site-reviews'),
121
            ];
122
            $settings['ppress_md_sorting'] = $sorting;
123
        }
124
        return $settings;
125
    }
126
127
    /**
128
     * @action wp_ajax_pp-builder-preview:5
129
     */
130
    public function insertPreviewCss(): void
131
    {
132
        check_ajax_referer('ppress-admin-nonce');
133
        if (!current_user_can('manage_options')) {
134
            return;
135
        }
136
        if ('pp-builder-preview' !== filter_input(INPUT_POST, 'action')) {
137
            return;
138
        }
139
        $css = file_get_contents(glsr()->path('assets/styles/minimal.css'));
140
        $css .= glsr(EnqueuePublicAssets::class)->inlineStyles();
141
        $_POST['builder_css'] = $_POST['builder_css'].$css;
142
    }
143
144
    /**
145
     * @action admin_init
146
     */
147
    public function registerProfileBuilderField(): void
148
    {
149
        if (!DragDropBuilder::get_instance()->is_drag_drop_page()) {
150
            return;
151
        }
152
        $formType = sanitize_text_field(filter_input(INPUT_GET, 'form-type'));
153
        if (!in_array($formType, [FormRepository::USER_PROFILE_TYPE, FormRepository::MEMBERS_DIRECTORY_TYPE])) {
154
            return;
155
        }
156
        new RatingField();
157
    }
158
159
    /**
160
     * @param \WP_User $user
161
     *
162
     * @action ppress_register_profile_shortcode
163
     */
164
    public function registerProfileRatingShortcode($user): void
165
    {
166
        if (!is_a($user, 'WP_User')) {
167
            return;
168
        }
169
        add_shortcode('profile-rating', function () use ($user) {
170
            $html = '';
171
            if (!glsr_get_option('integrations.profilepress.enabled', false, 'bool')) {
172
                return $html;
173
            }
174
            $total = glsr(CountManager::class)->usersReviews($user->ID);
175
            if (0 < $total || glsr_get_option('integrations.profilepress.directory_display_empty', false, 'bool')) {
176
                $rating = glsr(CountManager::class)->usersAverage($user->ID);
177
                $html = glsr(Builder::class)->div([
178
                    'class' => 'pp-member-rating',
179
                    'text' => glsr_star_rating($rating, $total),
180
                ]);
181
            }
182
            return $html;
183
        });
184
    }
185
}
186