Skip to content

Commit f2f9e65

Browse files
committed
feat: Various features and tweaks
1 parent 66cfd0a commit f2f9e65

13 files changed

+802
-205
lines changed

phpcs.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
<config name="testVersion" value="8.0-"/>
55

66
<rule ref="Oblak"/>
7+
<rule ref="Oblak-WooCommerce"/>
78
<rule ref="Oblak-Advanced"/>
9+
<rule ref="Oblak-Slevomat"/>
10+
811

912
<rule ref="Squiz.Commenting.FunctionComment.IncorrectTypeHint">
1013
<severity>0</severity>

src/Admin/Custom_Field.php

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
<?php //phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
2+
/**
3+
* Custom_Field class file.
4+
*
5+
* @package eXtended WooCommerce
6+
* @subpackage Admin
7+
*/
8+
9+
namespace XWC\Admin;
10+
11+
use XWP\Helper\Traits\Singleton_Ex;
12+
13+
/**
14+
* Base class for custom fields
15+
*/
16+
abstract class Custom_Field {
17+
use Singleton_Ex;
18+
19+
/**
20+
* Did output flag
21+
*
22+
* @var bool
23+
*/
24+
protected bool $did_output = false;
25+
26+
/**
27+
* Constructor
28+
*/
29+
private function __construct() {
30+
\add_filter( $this->get_hook(), array( $this, 'output_field' ), 99, $this->get_args() );
31+
\add_filter( $this->get_hook(), array( $this, 'set_output' ), 100, 0 );
32+
\add_filter( 'woocommerce_admin_settings_sanitize_option', array( $this, 'sanitize_option' ), 10, 3 );
33+
\add_filter( 'admin_footer', array( $this, 'output_css' ), 99, 0 );
34+
\add_filter( 'admin_footer', array( $this, 'output_js' ), 99, 0 );
35+
}
36+
37+
/**
38+
* Get hook definitions.
39+
*
40+
* @return string
41+
*/
42+
private function get_hook(): string {
43+
$hook = 'admin' === $this->get_type()
44+
? 'woocommerce_generate_%s_html'
45+
: 'woocommerce_admin_field_%s';
46+
47+
return \sprintf( $hook, $this->get_name() );
48+
}
49+
50+
/**
51+
* Get hook arguments
52+
*
53+
* @return int
54+
*/
55+
private function get_args(): int {
56+
return 'admin' === $this->get_type()
57+
? 4
58+
: 1;
59+
}
60+
61+
/**
62+
* Get field type
63+
*
64+
* @return string
65+
*/
66+
abstract protected function get_name(): string;
67+
68+
/**
69+
* Get field type
70+
*
71+
* @return 'admin'|'settings'
72+
*/
73+
abstract protected function get_type(): string;
74+
75+
/**
76+
* Get field css
77+
*
78+
* @return string
79+
*/
80+
protected function get_css(): string {
81+
return '';
82+
}
83+
84+
/**
85+
* Get field js
86+
*
87+
* @return string
88+
*/
89+
protected function get_js(): string {
90+
return '';
91+
}
92+
93+
/**
94+
* Set did output flag
95+
*
96+
* @return void
97+
*/
98+
public function set_output(): void {
99+
$this->did_output = true;
100+
}
101+
102+
/**
103+
* Adds custom styles
104+
*/
105+
public function output_css() {
106+
if ( ! $this->did_output || ! $this->get_css() ) {
107+
return;
108+
}
109+
110+
//phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped, Generic.Strings.UnnecessaryStringConcat
111+
\printf(
112+
'<' . '%1$s id="%3$s-field-css">%2$s</%1$s' . '>',
113+
'style',
114+
$this->get_css(),
115+
$this->get_name(),
116+
);
117+
//phpcs:enable WordPress.Security.EscapeOutput.OutputNotEscaped, Generic.Strings.UnnecessaryStringConcat
118+
}
119+
120+
/**
121+
* Adds custom scripts
122+
*/
123+
public function output_js() {
124+
if ( ! $this->did_output || ! $this->get_js() ) {
125+
return;
126+
}
127+
128+
//phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped, Generic.Strings.UnnecessaryStringConcat
129+
\printf(
130+
'<' . '%1$s id="%3$s-field-js">%2$s</%1$s' . '>',
131+
'script',
132+
$this->get_js(),
133+
$this->get_name(),
134+
);
135+
//phpcs:enable WordPress.Security.EscapeOutput.OutputNotEscaped, Generic.Strings.UnnecessaryStringConcat
136+
}
137+
138+
/**
139+
* Sanitize custom field callback wrapper
140+
*
141+
* @param mixed $value The value to sanitize.
142+
* @param array<string,mixed> $option The field option.
143+
* @param mixed $raw_value The raw value.
144+
* @return mixed
145+
*/
146+
public function sanitize_option( $value, $option, $raw_value ) {
147+
if ( $this->get_name() !== $option['type'] || ( $option['nosanitize'] ?? false ) ) {
148+
return $value;
149+
}
150+
151+
return $this->sanitize( $value, $option, $raw_value );
152+
}
153+
154+
/**
155+
* Sanitize the field value
156+
*
157+
* @param mixed $value The value to sanitize.
158+
* @param array<string,mixed> $option The field option.
159+
* @param mixed $raw_value The raw value.
160+
* @return mixed
161+
*/
162+
protected function sanitize( mixed $value, array $option, mixed $raw_value ): mixed {
163+
return $value;
164+
}
165+
}

0 commit comments

Comments
 (0)