Skip to content

Commit fe33b7c

Browse files
committed
Initial creation of the admin functionality. Added config for basic configuration.
1 parent add5381 commit fe33b7c

25 files changed

+1575
-12
lines changed

plugins/wpgraphql-logging/.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,11 @@ c3.php
4848

4949
# Cache
5050
phpcs-cache.json
51+
.psalm-cache/
5152
tests/_support/
5253
tests/_output/
5354
tests/_generated/
5455
tests/_data/
5556

5657
# Playwright outputs
57-
artifacts
58+
artifacts
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
settings_page_wpgraphql-logging #poststuff .postbox .inside h2 {
2+
font-size: 1.3em;
3+
font-weight: 600;
4+
padding-left: 0;
5+
}
6+
7+
8+
.form-table td input[type="text"] {
9+
width: calc(99% - 24px);
10+
display: inline-block;
11+
}
12+
.form-table td select {
13+
width: calc(99% - 24px);
14+
display: inline-block;
15+
}
16+
17+
.wpgraphql-logging-tooltip {
18+
position: relative;
19+
vertical-align: middle;
20+
display: inline-block;
21+
margin-right: 0.25rem;
22+
}
23+
24+
.wpgraphql-logging-tooltip .dashicons {
25+
color: #787c82;
26+
vertical-align: middle;
27+
}
28+
29+
.wpgraphql-logging-tooltip .tooltip-text.description {
30+
opacity: 0;
31+
visibility: hidden;
32+
text-align: center;
33+
color: #fff;
34+
background-color: #1d2327;
35+
border-radius: 4px;
36+
position: absolute;
37+
z-index: 1;
38+
width: 180px;
39+
padding: 0.5rem;
40+
top: 50%;
41+
transform: translateY(-50%);
42+
vertical-align: middle;
43+
margin-left: 0.25rem;
44+
transition: opacity 0.12s ease;
45+
}
46+
47+
.wpgraphql-logging-tooltip .tooltip-text::after {
48+
content: "";
49+
position: absolute;
50+
top: 0;
51+
left: -10px;
52+
border-width: 6px;
53+
border-style: solid;
54+
border-color: transparent #1d2327 transparent transparent;
55+
top: 50%;
56+
transform: translateY(-50%);
57+
}
58+
59+
.wpgraphql-logging-tooltip:hover .tooltip-text,
60+
.wpgraphql-logging-tooltip:focus-within .tooltip-text {
61+
visibility: visible;
62+
opacity: 1;
63+
}
64+
65+
.wpgraphql-logging-docs ul li {
66+
list-style-type: none;
67+
margin-left: 30px;
68+
padding-bottom: 16px;
69+
}
70+
71+
.wpgraphql-logging-docs ul li:before {
72+
content: url(../../icons/doc.svg);
73+
height: 1em;
74+
margin-left: -29px;
75+
margin-top: -2px;
76+
position: absolute;
77+
width: 0.5em;
78+
}
79+
80+
81+
.wpgraphql-logging-feature-list {
82+
list-style-type: disc;
83+
font-size: 1.1em;
84+
margin-left: 30px;
85+
padding-bottom: 16px;
86+
}
Lines changed: 3 additions & 0 deletions
Loading

plugins/wpgraphql-logging/composer.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,8 @@
143143
"phpstan": [
144144
"vendor/bin/phpstan analyze --ansi --memory-limit=1G"
145145
],
146-
"php:psalm": "psalm",
147-
"php:psalm:info": "psalm --show-info=true",
148-
"php:psalm:fix": "psalm --alter",
146+
"php:psalm": "psalm --output-format=text --no-progress",
147+
"php:psalm:fix": "psalm --alter --output-format=text --no-progress",
149148
"qa": "sh bin/local/run-qa.sh",
150149
"test": [
151150
"sh bin/local/run-unit-tests.sh coverage",

plugins/wpgraphql-logging/phpcs.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@
326326

327327
<rule ref="SlevomatCodingStandard.Complexity.Cognitive">
328328
<properties>
329-
<property name="warningThreshold" value="7"/>
329+
<property name="warningThreshold" value="8"/>
330330
<property name="errorThreshold" value="10"/>
331331
</properties>
332332
</rule>

plugins/wpgraphql-logging/phpstan.neon.dist

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,7 @@ parameters:
3030
paths:
3131
- wpgraphql-logging.php
3232
- src/
33+
ignoreErrors:
34+
- identifier: empty.notAllowed
35+
-
36+
message: '#Constant WPGRAPHQL_LOGGING.* not found\.#'

plugins/wpgraphql-logging/psalm.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
findUnusedBaselineEntry="true"
99
findUnusedCode="false"
1010
phpVersion="8.1"
11+
cacheDirectory=".psalm-cache"
1112
>
1213
<projectFiles>
1314
<file name="wpgraphql-logging.php"/>

plugins/wpgraphql-logging/src/Admin/Settings/.gitkeep

Whitespace-only changes.
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WPGraphQL\Logging\Admin\Settings\Fields\Field;
6+
7+
use WPGraphQL\Logging\Admin\Settings\Fields\Settings_Field_Interface;
8+
9+
/**
10+
* Abstract Settings Field class for WPGraphQL Logging.
11+
*
12+
* This class provides a base implementation for settings fields, including rendering, sanitization, and field registration.
13+
*
14+
* @package WPGraphQL\Logging
15+
*
16+
* @since 0.0.1
17+
*/
18+
abstract class Abstract_Settings_Field implements Settings_Field_Interface {
19+
/**
20+
* Constructor.
21+
*
22+
* @param string $id The settings field ID.
23+
* @param string $tab The tab this field should be shown in.
24+
* @param string $title The settings field title.
25+
* @param string $css_class The settings field class.
26+
* @param string $description The description field to show in the tooltip.
27+
*/
28+
public function __construct(
29+
readonly string $id,
30+
readonly string $tab,
31+
readonly string $title,
32+
readonly string $css_class = '',
33+
readonly string $description = ''
34+
) {
35+
}
36+
37+
/**
38+
* Get the settings field ID.
39+
*/
40+
public function get_id(): string {
41+
return $this->id;
42+
}
43+
44+
/**
45+
* Whether the field should be rendered for a specific tab.
46+
*
47+
* @param string $tab_key The tab key.
48+
*/
49+
public function should_render_for_tab( string $tab_key ): bool {
50+
return $tab_key === $this->tab;
51+
}
52+
53+
/**
54+
* Register the settings field.
55+
*
56+
* @param string $section The section ID.
57+
* @param string $page The page URI.
58+
* @param array<string, mixed> $args The field arguments.
59+
*/
60+
public function add_settings_field( string $section, string $page, array $args ): void {
61+
/** @psalm-suppress InvalidArgument */
62+
add_settings_field(
63+
$this->get_id(),
64+
$this->title,
65+
[ $this, 'render_field_callback' ],
66+
$page,
67+
$section,
68+
array_merge(
69+
$args,
70+
[
71+
'class' => $this->css_class,
72+
'description' => $this->description,
73+
]
74+
)
75+
);
76+
}
77+
78+
/**
79+
* Callback function to render the field.
80+
*
81+
* @param array<string, mixed> $args The field arguments.
82+
*/
83+
public function render_field_callback( array $args ): void {
84+
$tab_key = (string) ( $args['tab_key'] ?? '' );
85+
$settings_key = (string) ( $args['settings_key'] ?? '' );
86+
87+
$option_value = (array) get_option( $settings_key, [] );
88+
89+
$id = $this->get_field_name( $settings_key, $tab_key, $this->get_id() );
90+
91+
printf(
92+
'<span class="wpgraphql-logging-tooltip">
93+
<span class="dashicons dashicons-editor-help"></span>
94+
<span id="%2$s-tooltip" class="tooltip-text description">%1$s</span>
95+
</span>',
96+
esc_attr( $this->description ),
97+
esc_attr( $id ),
98+
);
99+
100+
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- render_field method handles escaping internally
101+
echo $this->render_field( $option_value, $settings_key, $tab_key );
102+
}
103+
104+
/**
105+
* Generate a field name for form inputs.
106+
*
107+
* @param string $settings_key The settings key.
108+
* @param string $tab_key The tab key.
109+
* @param string $field_id The field ID.
110+
*/
111+
protected function get_field_name( string $settings_key, string $tab_key, string $field_id ): string {
112+
return "{$settings_key}[{$tab_key}][{$field_id}]";
113+
}
114+
115+
/**
116+
* Get the current field value.
117+
*
118+
* @param array<string> $option_value The option value.
119+
* @param string $tab_key The tab key.
120+
* @param mixed $default_value The default value.
121+
*/
122+
protected function get_field_value( array $option_value, string $tab_key, $default_value = '' ): mixed {
123+
if ( ! array_key_exists( $tab_key, $option_value ) ) {
124+
return $default_value;
125+
}
126+
127+
/** @var array<string, mixed> $tab_value */
128+
$tab_value = $option_value[ $tab_key ]; // @phpstan-ignore varTag.nativeType
129+
$id = $this->get_id();
130+
if ( empty( $id ) ) {
131+
return $default_value;
132+
}
133+
134+
if ( ! array_key_exists( $id, $tab_value ) ) {
135+
return $default_value;
136+
}
137+
138+
$field_value = $tab_value[ $id ];
139+
140+
if ( is_null( $field_value ) ) {
141+
return $default_value;
142+
}
143+
return $field_value;
144+
}
145+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WPGraphQL\Logging\Admin\Settings\Fields\Field;
6+
7+
/**
8+
* Checkbox Field class for WPGraphQL Logging settings.
9+
*
10+
* This class handles the rendering and sanitization of checkbox input fields in the settings form.
11+
*
12+
* @package WPGraphQL\Logging
13+
*
14+
* @since 0.0.1
15+
*/
16+
class Checkbox_Field extends Abstract_Settings_Field {
17+
/**
18+
* Render the checkbox field.
19+
*
20+
* @param array<string> $option_value The option value.
21+
* @param string $setting_key The setting key.
22+
* @param string $tab_key The tab key.
23+
*
24+
* @return string The rendered field HTML.
25+
*/
26+
public function render_field( array $option_value, string $setting_key, string $tab_key ): string {
27+
$field_name = $this->get_field_name( $setting_key, $tab_key, $this->get_id() );
28+
$field_value = $this->get_field_value( $option_value, $tab_key, false );
29+
$is_checked = is_bool( $field_value ) ? $field_value : false;
30+
31+
return sprintf(
32+
'<input type="checkbox" name="%1$s" aria-labelledby="%1$s-tooltip" value="1" %2$s class="%3$s" />',
33+
esc_attr( $field_name ),
34+
checked( 1, $is_checked, false ),
35+
sanitize_html_class( $this->css_class )
36+
);
37+
}
38+
39+
/**
40+
* Sanitize the checkbox field value.
41+
*
42+
* @param mixed $value The field value to sanitize.
43+
*
44+
* @return bool The sanitized boolean value.
45+
*/
46+
public function sanitize_field( $value ): bool {
47+
return (bool) $value;
48+
}
49+
}

0 commit comments

Comments
 (0)