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

Controller::filterSettingsCallback()   B

Complexity

Conditions 9
Paths 12

Size

Total Lines 44
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 33
c 1
b 0
f 1
dl 0
loc 44
rs 8.0555
cc 9
nc 12
nop 2
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Integrations\ProfilePress\Controllers;
4
5
use GeminiLabs\SiteReviews\Controllers\AbstractController;
6
use GeminiLabs\SiteReviews\Gatekeeper;
7
use GeminiLabs\SiteReviews\Helpers\Arr;
8
use GeminiLabs\SiteReviews\Modules\Html\Template;
9
use GeminiLabs\SiteReviews\Review;
10
11
class Controller extends AbstractController
12
{
13
    /**
14
     * @filter site-reviews/assigned_users/profile_id
15
     */
16
    public function filterProfileId(int $profileId): int
17
    {
18
        if (empty($profileId)) {
19
            global $ppress_frontend_profile_user_obj;
20
            return (int) ppress_var_obj($ppress_frontend_profile_user_obj, 'ID', 0);
0 ignored issues
show
Bug introduced by
The function ppress_var_obj was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

20
            return (int) /** @scrutinizer ignore-call */ ppress_var_obj($ppress_frontend_profile_user_obj, 'ID', 0);
Loading history...
21
        }
22
        return $profileId;
23
    }
24
25
    /**
26
     * @filter site-reviews/settings
27
     */
28
    public function filterSettings(array $settings): array
29
    {
30
        return array_merge(glsr()->config('integrations/profilepress'), $settings);
31
    }
32
33
    /**
34
     * @filter site-reviews/settings/sanitize
35
     */
36
    public function filterSettingsCallback(array $settings, array $input): array
37
    {
38
        $enabled = Arr::get($input, 'settings.integrations.profilepress.enabled');
39
        if ('yes' === $enabled && !$this->gatekeeper()->allows()) { // this renders any error notices
40
            $settings = Arr::set($settings, 'settings.integrations.profilepress.enabled', 'no');
41
        }
42
        $shortcodes = [
43
            'account_tab_reviews' => 'site_reviews',
44
            'profile_tab_form' => 'site_reviews_form',
45
            'profile_tab_reviews' => 'site_reviews',
46
            'profile_tab_summary' => 'site_reviews_summary',
47
        ];
48
        foreach ($shortcodes as $settingKey => $shortcode) {
49
            $path = "settings.integrations.profilepress.{$settingKey}";
50
            $value = Arr::get($input, $path);
51
            $pattern = get_shortcode_regex([$shortcode]);
52
            $normalizedValue = preg_replace_callback("/$pattern/", function ($match) use ($settingKey) {
53
                $atts = shortcode_parse_atts($match[3]);
54
                if (str_starts_with($settingKey, 'account')) {
55
                    $atts['author'] = 'user_id';
56
                } elseif (str_starts_with($settingKey, 'profile')) {
57
                    $atts['assigned_users'] = 'profile_id';
58
                }
59
                ksort($atts);
60
                $attributes = [];
61
                foreach ($atts as $key => $val) {
62
                    $attributes[] = sprintf('%s="%s"', $key, esc_attr($val));
63
                }
64
                $attributes = implode(' ', $attributes);
65
                return "[{$match[2]} {$attributes}]";
66
            }, $value);
67
            $settings = Arr::set($settings, $path, $normalizedValue);
68
        }
69
        foreach (['display_account_tab', 'enabled'] as $key) {
70
            $old = glsr_get_option("settings.integrations.profilepress.{$key}");
71
            $new = Arr::get($settings, "settings.integrations.profilepress.{$key}");
72
            if ($new !== $old) {
73
                // This is a simpler way to force rewrite rules to be recreated
74
                // instead of using flush_rewrite_rules().
75
                delete_option('rewrite_rules');
76
                break;
77
            }
78
        }
79
        return $settings;
80
    }
81
82
    /**
83
     * @filter site-reviews/integration/subsubsub
84
     */
85
    public function filterSubsubsub(array $subsubsub): array
86
    {
87
        $subsubsub['profilepress'] = 'ProfilePress';
88
        return $subsubsub;
89
    }
90
91
    /**
92
     * @action admin_init
93
     */
94
    public function renderNotice(): void
95
    {
96
        if (glsr_get_option('integrations.profilepress.enabled', false, 'bool')) {
97
            $this->gatekeeper()->allows(); // this renders any error notices
98
        }
99
    }
100
101
    /**
102
     * @action site-reviews/settings/profilepress
103
     */
104
    public function renderSettings(string $rows): void
105
    {
106
        glsr(Template::class)->render('integrations/profilepress/settings', [
107
            'context' => [
108
                'rows' => $rows,
109
            ],
110
        ]);
111
    }
112
113
    protected function gatekeeper(): Gatekeeper
114
    {
115
        return new Gatekeeper([
116
            'wp-user-avatar/wp-user-avatar.php' => [
117
                'minimum_version' => '4.15',
118
                'name' => 'ProfilePress',
119
                'plugin_uri' => 'https://wordpress.org/plugins/wp-user-avatar/',
120
                'untested_version' => '5.0',
121
            ],
122
        ]);
123
    }
124
}
125