Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
W
Wpct Plugin Abstracts
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
External wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
codeccoop
WordPress
plugins
Wpct Plugin Abstracts
Compare revisions
460e1e1e16be94f5e5edc744dc898b4883d50424 to 34b4bd64c600e8fbdc32e4600c212e34449ef23c
Compare revisions
Changes are shown as if the
source
revision was being merged into the
target
revision.
Learn more about comparing revisions.
Source
codeccoop/wp/plugins/wpct-plugin-abstracts
Select target project
No results found
34b4bd64c600e8fbdc32e4600c212e34449ef23c
Select Git revision
Branches
main
release/forms-bridge
Swap
Target
codeccoop/wp/plugins/wpct-plugin-abstracts
Select target project
codeccoop/wp/plugins/wpct-plugin-abstracts
1 result
460e1e1e16be94f5e5edc744dc898b4883d50424
Select Git revision
Branches
main
release/forms-bridge
Show changes
Only incoming changes from source
Include changes to target since source was created
Compare
Commits on Source (1)
feat: settings sanitization and menu render buffer
· 34b4bd64
Lucas García
authored
7 months ago
34b4bd64
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
class-menu.php
+7
-1
7 additions, 1 deletion
class-menu.php
class-settings.php
+70
-0
70 additions, 0 deletions
class-settings.php
with
77 additions
and
1 deletion
class-menu.php
View file @
34b4bd64
...
...
@@ -40,7 +40,7 @@ if (!class_exists('\WPCT_ABSTRACT\Menu')) :
);
}
protected
function
render_page
()
protected
function
render_page
(
$echo
=
true
)
{
$page_settings
=
$this
->
settings
->
get_settings
();
$tabs
=
array_reduce
(
$page_settings
,
function
(
$carry
,
$setting
)
{
...
...
@@ -48,6 +48,7 @@ if (!class_exists('\WPCT_ABSTRACT\Menu')) :
return
$carry
;
},
[]);
$current_tab
=
isset
(
$_GET
[
'tab'
])
?
$_GET
[
'tab'
]
:
array_key_first
(
$tabs
);
ob_start
();
?>
<div
class=
"wrap"
>
<h1>
<?=
get_admin_page_title
()
?>
</h1>
...
...
@@ -67,6 +68,11 @@ if (!class_exists('\WPCT_ABSTRACT\Menu')) :
</form>
</div>
<?php
$output
=
ob_get_clean
();
if
(
$echo
)
{
echo
$output
;
}
return
$output
;
}
public
function
get_name
()
...
...
This diff is collapsed.
Click to expand it.
class-settings.php
View file @
34b4bd64
...
...
@@ -2,6 +2,8 @@
namespace
WPCT_ABSTRACT
;
use
Error
;
if
(
!
class_exists
(
'\WPCT_ABSTRACT\Settings'
))
:
class
Undefined
...
...
@@ -60,6 +62,10 @@ if (!class_exists('\WPCT_ABSTRACT\Settings')) :
public
function
__construct
(
$group_name
)
{
$this
->
group_name
=
$group_name
;
add_filter
(
'pre_update_option'
,
function
(
$value
,
$option
,
$from
)
{
return
$this
->
sanitize_option
(
$option
,
$value
);
},
10
,
3
);
}
public
function
get_group_name
()
...
...
@@ -263,6 +269,70 @@ if (!class_exists('\WPCT_ABSTRACT\Settings')) :
{
return
$this
->
group_name
.
'_'
.
$setting
;
}
private
function
sanitize_option
(
$option
,
$value
)
{
$settings
=
$this
->
get_settings
();
if
(
in_array
(
$option
,
$settings
))
{
[
$group
,
$setting
]
=
explode
(
'_'
,
$option
);
$default
=
Settings
::
get_default
(
$group
,
$setting
);
if
(
empty
(
$value
))
{
return
$default
;
}
$schema
=
Settings
::
get_schema
(
$group
,
$setting
);
try
{
return
$this
->
sanitize_object
(
$schema
,
$value
,
$default
);
}
catch
(
Error
)
{
return
$default
;
}
}
return
$value
;
}
private
function
sanitize_object
(
$schema
,
$value
,
$default
)
{
foreach
(
$schema
as
$key
=>
$defn
)
{
if
(
empty
(
$value
[
$key
]))
{
$value
[
$key
]
=
$default
[
$key
];
}
else
{
if
(
$defn
[
'type'
]
===
'array'
)
{
$value
[
$key
]
=
$this
->
sanitize_array
(
$defn
[
'items'
],
$value
[
$key
],
$default
[
$key
]
?:
[]);
}
elseif
(
$defn
[
'type'
]
===
'object'
)
{
$value
[
$key
]
=
$this
->
sanitize_object
(
$defn
[
'properties'
],
$value
[
$key
],
$default
[
$key
]
?:
[]);
}
else
{
$value
[
$key
]
=
empty
(
$value
[
$key
])
?
$default
[
$key
]
:
$value
[
$key
];
}
}
}
foreach
(
array_keys
(
$value
)
as
$key
)
{
if
(
!
in_array
(
$key
,
array_keys
(
$schema
)))
{
unset
(
$value
[
$key
]);
};
}
return
$value
;
}
private
function
sanitize_array
(
$schema
,
$value
,
$defaults
)
{
$default
=
null
;
for
(
$i
=
0
;
$i
<
count
(
$value
);
$i
++
)
{
$default
=
count
(
$defaults
)
>
$i
?
array_shift
(
$defaults
)
:
$default
;
if
(
$schema
[
'type'
]
===
'array'
)
{
$value
[
$i
]
=
$this
->
sanitize_array
(
$schema
[
'items'
],
$value
[
$i
],
$default
?:
[]);
}
elseif
(
$schema
[
'type'
]
===
'object'
)
{
$value
[
$i
]
=
$this
->
sanitize_object
(
$schema
[
'properties'
],
$value
[
$i
],
$default
?:
[]);
}
else
{
$value
[
$i
]
=
empty
(
$value
[
$i
])
?
$default
[
0
]
:
$value
[
$i
];
}
}
return
$value
;
}
}
endif
;
...
...
This diff is collapsed.
Click to expand it.