Passed
Push — master ( 5b55dd...2362db )
by Joel
06:46
created

outputDoNotRedirectAdminSection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 2
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace RedirectionIO\Client\Wordpress;
4
5
/**
6
 * WordPress Core Functions Overrider
7
 * WordPress Version at the time of writing: 4.8.3.
8
 *
9
 * This class is used to clone and modify core wordpress functions to fit our needs
10
 * without changing initial ones for complete isolation
11
 */
12
class WPCoreFunctionsOverrider
13
{
14
    /**
15
     * See source function here: wp-admin/includes/templates.php | line 1308.
16
     *
17
     * @param mixed $page
18
     */
19
    public function doSettingsSections($page)
20
    {
21
        global $wp_settings_sections, $wp_settings_fields;
22
23
        if (!isset($wp_settings_sections[$page])) {
24
            return;
25
        }
26
27
        $sections = (array) $wp_settings_sections[$page];
28
29
        // remove doNotRedirectAdmin section from sections array to use it later
30
        $doNotRedirectAdminSection = $sections['redirectionio-section-do-not-redirect-admin'];
31
        unset($sections['redirectionio-section-do-not-redirect-admin']);
32
33
        echo '<div id="rio_connections">';
34
35
        $nb = count($sections);
36
        foreach ($sections as $section) {
37
            if ($section['title']) {
38
                if (1 === $nb) {
39
                    echo '<h2>' . $section['title'] . '</h2>';
40
                } else {
41
                    echo '<h2>' . $section['title'] . ' <span class="dashicons dashicons-trash rio_connections_remove" onclick="rioRemoveConnection(event)"></span></h2>';
42
                }
43
            }
44
45
            $connection = RedirectionIOSettingsPage::getConnectionFromSection($page, $section['id']);
46
47
            if (RedirectionIOSettingsPage::isWorkingConnection($connection)) {
48
                echo '
49
                    <div class="rio_connection_status rio_connection_working">
50
                        <span class="dashicons dashicons-yes"></span>' . __('working', 'redirectionio') .
51
                    '</div>
52
                ';
53
            } else {
54
                echo '
55
                    <div class="rio_connection_status rio_connection_not_working">
56
                    <span class="dashicons dashicons-no-alt"></span>' . __('not working', 'redirectionio') .
57
                    '</div>
58
                ';
59
            }
60
61
            if ($section['callback']) {
62
                call_user_func($section['callback'], $section);
63
            }
64
65
            if (!isset($wp_settings_fields) || !isset($wp_settings_fields[$page]) || !isset($wp_settings_fields[$page][$section['id']])) {
66
                continue;
67
            }
68
            echo '<table class="form-table">';
69
            $this->doSettingsFields($page, $section['id']);
70
            echo '</table>';
71
        }
72
73
        echo '
74
            <button id="rio_connections_add" class="button" onclick="rioAddConnection(event)">' .
75
            __('Add') .
76
            '</button>
77
            </div>
78
        ';
79
80
        $this->outputDoNotRedirectAdminSection($page, $doNotRedirectAdminSection);
81
    }
82
83
    /**
84
     * See source function here: wp-admin/includes/templates.php | line 1343.
85
     *
86
     * @param mixed $page
87
     * @param mixed $section
88
     */
89
    private function doSettingsFields($page, $section)
90
    {
91
        global $wp_settings_fields;
92
93
        if (!isset($wp_settings_fields[$page][$section])) {
94
            return;
95
        }
96
97
        foreach ((array) $wp_settings_fields[$page][$section] as $field) {
98
            $class = '';
99
100
            if (!empty($field['args']['class'])) {
101
                $class = ' class="' . esc_attr($field['args']['class']) . '"';
102
            }
103
104
            echo "<tr{$class}>";
105
106
            if (!empty($field['args']['label_for'])) {
107
                echo '<th scope="row"><label for="' . esc_attr($field['args']['label_for']) . '">' . $field['title'] . '</label></th>';
108
            } else {
109
                echo '<th scope="row">' . $field['title'] . '</th>';
110
            }
111
112
            echo '<td>';
113
            call_user_func($field['callback'], $field['args']);
114
            echo '</td>';
115
            echo '</tr>';
116
        }
117
    }
118
119
    /**
120
     * @param mixed $page
121
     * @param mixed $section
122
     */
123
    private function outputDoNotRedirectAdminSection($page, $section)
124
    {
125
        if ($section['title']) {
126
            echo '<h2>' . $section['title'] . '</h2>';
127
        }
128
129
        echo '<p>' . __('This option let you ignore eventual redirection rules set on admin area pages.', 'redirectionio') . '</p>';
130
        echo '<p>' . sprintf(__("
131
            %sExample :%s if by mistake you add a redirection rule from %s/wp-login.php%s to %s/foo%s,
132
            you'll not be able to connect to your admin area anymore.
133
        ", 'redirectionio'), '<b>', '</b>', '<code>', '</code>', '<code>', '</code>') . '</p>';
134
        echo '<p>' . __('To prevent this, we recommend you to always leave this option enabled.', 'redirectionio') . '</p>';
135
136
        echo '<table class="form-table">';
137
        $this->doSettingsFields($page, $section['id']);
138
        echo '</table>';
139
    }
140
}
141