@@ -81,12 +81,24 @@ public static function settings(): array
81
81
default: false ,
82
82
section: 'rollbar_wp_general ' ,
83
83
),
84
+ 'server_side_access_token ' => new Setting (
85
+ id: 'server_side_access_token ' ,
86
+ type: SettingType::Text,
87
+ default: '' ,
88
+ section: 'rollbar_wp_general ' ,
89
+ ),
84
90
'js_logging_enabled ' => new Setting (
85
91
id: 'js_logging_enabled ' ,
86
92
type: SettingType::Boolean,
87
93
default: false ,
88
94
section: 'rollbar_wp_general ' ,
89
95
),
96
+ 'client_side_access_token ' => new Setting (
97
+ id: 'client_side_access_token ' ,
98
+ type: SettingType::Text,
99
+ default: '' ,
100
+ section: 'rollbar_wp_general ' ,
101
+ ),
90
102
'environment ' => new Setting (
91
103
id: 'environment ' ,
92
104
type: SettingType::Text,
@@ -505,7 +517,7 @@ public function saveSettings(array $settings): void
505
517
*
506
518
* @return void
507
519
*/
508
- public function restoreDefaults ()
520
+ public function restoreDefaults (): void
509
521
{
510
522
$ settings = [];
511
523
@@ -549,6 +561,69 @@ public static function toBoolean(mixed $value): bool
549
561
return boolval ($ value );
550
562
}
551
563
564
+ /**
565
+ * Returns a reasonable integer from a given value.
566
+ *
567
+ * @param mixed $value The value to convert to an integer.
568
+ * @return int
569
+ */
570
+ public static function toInteger (mixed $ value ): int
571
+ {
572
+ if (is_int ($ value )) {
573
+ return $ value ;
574
+ }
575
+ if (is_string ($ value )) {
576
+ return intval ($ value );
577
+ }
578
+ if (is_bool ($ value )) {
579
+ return $ value ? 1 : 0 ;
580
+ }
581
+ if (is_numeric ($ value )) {
582
+ return intval ($ value );
583
+ }
584
+ return 0 ;
585
+ }
586
+
587
+ /**
588
+ * Returns a reasonable string from a given value.
589
+ *
590
+ * @param mixed $value The value to convert to a string.
591
+ * @return string
592
+ */
593
+ public static function toString (mixed $ value ): string
594
+ {
595
+ if (is_string ($ value )) {
596
+ return $ value ;
597
+ }
598
+ if (is_bool ($ value )) {
599
+ return $ value ? 'true ' : 'false ' ;
600
+ }
601
+ if (is_int ($ value )) {
602
+ return (string ) $ value ;
603
+ }
604
+ if (is_numeric ($ value )) {
605
+ return (string ) $ value ;
606
+ }
607
+ return '' ;
608
+ }
609
+
610
+ /**
611
+ * Returns an array of strings from a given value.
612
+ *
613
+ * @param mixed $value The value to convert to an array of strings.
614
+ * @return string[]
615
+ */
616
+ public static function toStringArray (mixed $ value ): array
617
+ {
618
+ if (is_array ($ value )) {
619
+ return array_map (fn ($ v ) => self ::toString ($ v ), $ value );
620
+ }
621
+ if (is_string ($ value )) {
622
+ return [$ value ];
623
+ }
624
+ return [];
625
+ }
626
+
552
627
/**
553
628
* Fetch settings provided in Admin -> Tools -> Rollbar
554
629
*
@@ -574,6 +649,17 @@ private function fetchSettings(): void
574
649
$ options ['client_side_access_token ' ] ?? null ,
575
650
);
576
651
652
+ $ this ->settings = self ::normalizeSettings ($ options );
653
+ }
654
+
655
+ /**
656
+ * Normalizes the settings array to ensure all required fields are set and properly formatted.
657
+ *
658
+ * @param array<string, mixed> $options
659
+ * @return array
660
+ */
661
+ public static function normalizeSettings (array $ options ): array
662
+ {
577
663
$ settings = [
578
664
'php_logging_enabled ' => (!empty ($ options ['php_logging_enabled ' ])) ? 1 : 0 ,
579
665
'js_logging_enabled ' => (!empty ($ options ['js_logging_enabled ' ])) ? 1 : 0 ,
@@ -591,20 +677,20 @@ private function fetchSettings(): void
591
677
// Filter out options that are not in the list of options.
592
678
$ options = array_intersect_key ($ options , array_flip (self ::listOptions ()));
593
679
594
- foreach (self ::listOptions () as $ option ) {
680
+ foreach (self ::settings () as $ key => $ setting ) {
595
681
// 'access_token' and 'enabled' are different in WordPress plugin
596
682
// look for 'server_side_access_token' and 'php_logging_enabled' above
597
- if (in_array ($ option , ['access_token ' , 'enabled ' ])) {
683
+ if (in_array ($ key , ['access_token ' , 'enabled ' ])) {
598
684
continue ;
599
685
}
600
686
601
- if (!isset ($ options [$ option ])) {
602
- $ value = $ this ->getDefaultOption ($ option );
603
- } else {
604
- $ value = $ options [$ option ];
687
+ $ value = $ options [$ key ] ?? ($ setting ?->default ?? null );
688
+ if ($ setting !== null ) {
689
+ $ value = $ setting ->coerceValue ($ value );
690
+ } elseif (!isset ($ options [$ key ])) {
691
+ continue ;
605
692
}
606
-
607
- $ settings [$ option ] = $ value ;
693
+ $ settings [$ key ] = $ value ;
608
694
}
609
695
610
696
/**
@@ -614,7 +700,7 @@ private function fetchSettings(): void
614
700
* @since 3.0.0
615
701
*
616
702
*/
617
- $ this -> settings = apply_filters ('rollbar_plugin_settings ' , $ settings );
703
+ return apply_filters ('rollbar_plugin_settings ' , $ settings );
618
704
}
619
705
620
706
/**
0 commit comments