Skip to content

Commit 5a33f50

Browse files
arraypressclaude
andcommitted
Hand the type questions to the kit
is_setting_on(), is_setting_enabled() and the page helpers were never about settings — they decide what a stored value means, and the same question is asked of a term's meta, a user's meta and a post's meta. This library had eleven of them and the other three had none, which is the shape of something that belongs one level down. They read the value and delegate; the kit's Value class answers. Two of them were also wrong. is_setting_field_page() called is_page(), which answers false for every post type that is not a page even though the kit's post, page and ajax types all store an id the same way — it compares the queried object now. And Page.php carried TypeCheck.php's docblock, so both files claimed to be about toggles and multi-selects. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Tk8y5eZa3CHVVSM6nVxsJw
1 parent 8f90b38 commit 5a33f50

4 files changed

Lines changed: 92 additions & 33 deletions

File tree

‎src/SettingFields.php‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,8 +1191,11 @@ public function get_field( string $field_key ): ?array {
11911191
/**
11921192
* Tell the kit which configuration this library reads.
11931193
*
1194-
* Which tab and which section of this page a field belongs to. Both are about the page's layout rather than the field, so they are read here. Without this the kit reports each one as configuration
1195-
* nothing reads, which is exactly the warning it exists to give.
1194+
* Which tab and which section of this page a field belongs to. Both are
1195+
* about the page's layout rather than the field, so they are read here.
1196+
* Without this the kit would report each of them as configuration
1197+
* nothing reads — which is the warning it exists to give, aimed at the
1198+
* wrong thing.
11961199
*
11971200
* @return void
11981201
*/

‎src/Utilities/Page.php‎

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
<?php
22
/**
3-
* Type Check Helper Functions
3+
* Page Field Helper Functions
44
*
5-
* Convenience functions for checking toggle, multi-select, and page field values.
5+
* Convenience functions for the page/post a setting points at.
66
*
77
* @package ArrayPress\RegisterSettingFields
8-
* @copyright Copyright (c) 2025, ArrayPress Limited
8+
* @copyright Copyright (c) 2026, ArrayPress Limited
99
* @license GPL2+
1010
* @since 1.0.0
1111
*/
1212

13+
use ArrayPress\FieldKit\Value;
14+
1315
/** Page Field Helpers ********************************************************/
1416

1517
if ( ! function_exists( 'get_setting_field_page_id' ) ) {
@@ -22,7 +24,7 @@
2224
* @return int Page/post ID or 0 if not set.
2325
*/
2426
function get_setting_field_page_id( string $settings_id, string $field_key ): int {
25-
return absint( get_setting_field_value( $settings_id, $field_key, 0 ) );
27+
return Value::id( get_setting_field_value( $settings_id, $field_key, 0 ) );
2628
}
2729
}
2830

@@ -34,39 +36,28 @@ function get_setting_field_page_id( string $settings_id, string $field_key ): in
3436
* @param string $field_key Field key.
3537
* @param string $fallback Fallback URL if page not set.
3638
*
37-
* @return string Page URL or fallback.
39+
* @return string Page URL, the fallback, or the site's home page.
3840
*/
3941
function get_setting_field_page_url( string $settings_id, string $field_key, string $fallback = '' ): string {
40-
$page_id = get_setting_field_page_id( $settings_id, $field_key );
41-
42-
if ( $page_id > 0 ) {
43-
$url = get_permalink( $page_id );
44-
45-
if ( $url ) {
46-
return $url;
47-
}
48-
}
49-
50-
return $fallback ?: home_url( '/' );
42+
return Value::url( get_setting_field_value( $settings_id, $field_key, 0 ), $fallback );
5143
}
5244
}
5345

5446
if ( ! function_exists( 'is_setting_field_page' ) ) {
5547
/**
5648
* Check if currently viewing the page stored in a setting field.
5749
*
50+
* Compares the queried object rather than calling is_page(): a setting
51+
* can point at any post type — the kit's `post`, `page` and `ajax` types
52+
* all store an id the same way — and is_page() answers false for every
53+
* one of them that is not a page, which this used to get wrong.
54+
*
5855
* @param string $settings_id Settings ID.
5956
* @param string $field_key Field key.
6057
*
6158
* @return bool
6259
*/
6360
function is_setting_field_page( string $settings_id, string $field_key ): bool {
64-
if ( ! is_singular() ) {
65-
return false;
66-
}
67-
68-
$page_id = get_setting_field_page_id( $settings_id, $field_key );
69-
70-
return $page_id > 0 && is_page( $page_id );
61+
return Value::is_viewing( get_setting_field_value( $settings_id, $field_key, 0 ) );
7162
}
7263
}

‎src/Utilities/TypeCheck.php‎

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,23 @@
22
/**
33
* Type Check Helper Functions
44
*
5-
* Convenience functions for checking toggle, multi-select, and page field values.
5+
* Convenience functions for asking what a stored setting means.
66
*
77
* @package ArrayPress\RegisterSettingFields
8-
* @copyright Copyright (c) 2025, ArrayPress Limited
8+
* @copyright Copyright (c) 2026, ArrayPress Limited
99
* @license GPL2+
1010
* @since 1.0.0
1111
*/
1212

13+
use ArrayPress\FieldKit\Value;
14+
15+
/*
16+
* What a value means is the kit's answer, not this library's: the same
17+
* question is asked of a term's meta, a user's meta and a post's meta, and
18+
* four copies of "is this on" would draw the line in four slightly different
19+
* places. These read the value and hand it over.
20+
*/
21+
1322
/** Toggle & Multi-Select Helpers *********************************************/
1423

1524
if ( ! function_exists( 'is_setting_on' ) ) {
@@ -22,10 +31,7 @@
2231
* @return bool
2332
*/
2433
function is_setting_on( string $settings_id, string $field_key ): bool {
25-
return filter_var(
26-
get_setting_field_value( $settings_id, $field_key, false ),
27-
FILTER_VALIDATE_BOOLEAN
28-
);
34+
return Value::is_on( get_setting_field_value( $settings_id, $field_key, false ) );
2935
}
3036
}
3137

@@ -40,8 +46,34 @@ function is_setting_on( string $settings_id, string $field_key ): bool {
4046
* @return bool
4147
*/
4248
function is_setting_enabled( string $settings_id, string $field_key, string $option ): bool {
43-
$value = get_setting_field_value( $settings_id, $field_key, [] );
49+
return Value::includes( get_setting_field_value( $settings_id, $field_key, [] ), $option );
50+
}
51+
}
52+
53+
if ( ! function_exists( 'get_setting_field_list' ) ) {
54+
/**
55+
* Get a tags or multi-select field as a list of strings.
56+
*
57+
* @param string $settings_id Settings ID.
58+
* @param string $field_key Field key.
59+
*
60+
* @return string[]
61+
*/
62+
function get_setting_field_list( string $settings_id, string $field_key ): array {
63+
return Value::list( get_setting_field_value( $settings_id, $field_key, [] ) );
64+
}
65+
}
4466

45-
return is_array( $value ) && in_array( $option, $value, true );
67+
if ( ! function_exists( 'get_setting_field_ids' ) ) {
68+
/**
69+
* Get a relational field as a list of object ids.
70+
*
71+
* @param string $settings_id Settings ID.
72+
* @param string $field_key Field key.
73+
*
74+
* @return int[]
75+
*/
76+
function get_setting_field_ids( string $settings_id, string $field_key ): array {
77+
return Value::ids( get_setting_field_value( $settings_id, $field_key, [] ) );
4678
}
4779
}

‎tests/SettingFieldsTest.php‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,4 +1041,37 @@ private function import( array $payload ): void {
10411041
unset( $redirect );
10421042
}
10431043
}
1044+
1045+
/**
1046+
* The type helpers read through the page and answer through the kit.
1047+
*
1048+
* They used to hold their own idea of what "on" means. Four libraries
1049+
* were on their way to four of those, so the answer moved to the kit and
1050+
* these became the reading half — which is the half that is about
1051+
* settings.
1052+
*/
1053+
public function test_the_type_helpers_delegate(): void {
1054+
$page = $this->page(
1055+
[
1056+
'fields' => [
1057+
'flag' => [ 'type' => 'toggle' ],
1058+
'features' => [
1059+
'type' => 'checkbox_group',
1060+
'options' => [ 'a' => 'A', 'b' => 'B' ],
1061+
],
1062+
],
1063+
]
1064+
);
1065+
1066+
update_option( $page->get_option_name(), [ 'flag' => '1', 'features' => [ 'a' ] ] );
1067+
1068+
$this->assertTrue( is_setting_on( $page->get_id(), 'flag' ) );
1069+
$this->assertTrue( is_setting_enabled( $page->get_id(), 'features', 'a' ) );
1070+
$this->assertFalse( is_setting_enabled( $page->get_id(), 'features', 'b' ) );
1071+
1072+
// The case a plain (bool) cast gets backwards.
1073+
update_option( $page->get_option_name(), [ 'flag' => '0' ] );
1074+
$this->assertFalse( is_setting_on( $page->get_id(), 'flag' ) );
1075+
}
1076+
10441077
}

0 commit comments

Comments
 (0)