@@ -81,12 +81,24 @@ public static function settings(): array
8181 default: false ,
8282 section: 'rollbar_wp_general ' ,
8383 ),
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+ ),
8490 'js_logging_enabled ' => new Setting (
8591 id: 'js_logging_enabled ' ,
8692 type: SettingType::Boolean,
8793 default: false ,
8894 section: 'rollbar_wp_general ' ,
8995 ),
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+ ),
90102 'environment ' => new Setting (
91103 id: 'environment ' ,
92104 type: SettingType::Text,
@@ -505,7 +517,7 @@ public function saveSettings(array $settings): void
505517 *
506518 * @return void
507519 */
508- public function restoreDefaults ()
520+ public function restoreDefaults (): void
509521 {
510522 $ settings = [];
511523
@@ -549,6 +561,69 @@ public static function toBoolean(mixed $value): bool
549561 return boolval ($ value );
550562 }
551563
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+
552627 /**
553628 * Fetch settings provided in Admin -> Tools -> Rollbar
554629 *
@@ -574,6 +649,17 @@ private function fetchSettings(): void
574649 $ options ['client_side_access_token ' ] ?? null ,
575650 );
576651
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+ {
577663 $ settings = [
578664 'php_logging_enabled ' => (!empty ($ options ['php_logging_enabled ' ])) ? 1 : 0 ,
579665 'js_logging_enabled ' => (!empty ($ options ['js_logging_enabled ' ])) ? 1 : 0 ,
@@ -591,20 +677,20 @@ private function fetchSettings(): void
591677 // Filter out options that are not in the list of options.
592678 $ options = array_intersect_key ($ options , array_flip (self ::listOptions ()));
593679
594- foreach (self ::listOptions () as $ option ) {
680+ foreach (self ::settings () as $ key => $ setting ) {
595681 // 'access_token' and 'enabled' are different in WordPress plugin
596682 // 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 ' ])) {
598684 continue ;
599685 }
600686
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 ;
605692 }
606-
607- $ settings [$ option ] = $ value ;
693+ $ settings [$ key ] = $ value ;
608694 }
609695
610696 /**
@@ -614,7 +700,7 @@ private function fetchSettings(): void
614700 * @since 3.0.0
615701 *
616702 */
617- $ this -> settings = apply_filters ('rollbar_plugin_settings ' , $ settings );
703+ return apply_filters ('rollbar_plugin_settings ' , $ settings );
618704 }
619705
620706 /**
0 commit comments