PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 6.2.2
MainWP Dashboard: Self-hosted WordPress Management for Agencies v6.2.2
6.2.2 6.2.1 6.2 6.1.8 6.1.7 6.1.6 6.1.5 6.1.4 6.1.3 6.1.2 6.1.1 6.1 6.0.12 6.0.11 4.6.0.1 5.0 5.0.1 5.0.2 5.0.3 5.0.3.1 5.0.3.2 5.1 5.1.1 5.2 5.2.1 All 155 releases
← All changes | includes/rest-api/controller/version2/class-mainwp-rest-settings-controller.php +101 -50 6.1.8 → 6.2.2 View file →
@@ -531,9 +531,9 @@
531 531 if ( array_key_exists( $key, $body ) ) {
532 532 $val = $body[ $key ];
533 533 $val = $this->cast_field_value( $val, $cast );
534 534 if ( 'mainwp_widgets' === $key ) {
535 - $show_widgets = get_user_option( 'mainwp_settings_show_widgets', array() );
535 + $show_widgets = get_user_option( 'mainwp_settings_show_widgets' );
536 536 $val = array_replace( $show_widgets, (array) $val );
537 537 }
538 538 update_user_option( $user_id, $option_name, $val, true );
539 539 }
@@ -583,8 +583,9 @@
583 583 do_action( 'mainwp_debug_log', 'Update general settings error: ' . $e->getMessage() );
584 584 return new WP_Error(
585 585 'rest_invalid_param',
586 586 /* translators: %s: Error message */ sprintf( __( 'Update general settings error: %s', 'mainwp' ), $e->getMessage() ),
587 + array( 'status' => 500 )
587 588 );
588 589 }
589 590
590 591 return rest_ensure_response(
@@ -684,8 +685,9 @@
684 685 do_action( 'mainwp_debug_log', 'Update advanced settings error: ' . $e->getMessage() );
685 686 return new WP_Error(
686 687 'rest_invalid_param',
687 688 /* translators: %s: Error message */ sprintf( __( 'Update advanced settings error: %s', 'mainwp' ), $e->getMessage() ),
689 + array( 'status' => 500 )
688 690 );
689 691 }
690 692
691 693 return rest_ensure_response(
@@ -881,14 +883,14 @@
881 883
882 884 // Get mail type.
883 885 $setting = $request->get_param( 'mail_type' );
884 886 if ( empty( $setting ) ) {
885 - return new WP_Error( 'invalid_mail_type', __( 'Mail type is not exist.', 'mainwp' ) );
887 + return new WP_Error( 'invalid_mail_type', __( 'Mail type is not exist.', 'mainwp' ), array( 'status' => 400 ) );
886 888 }
887 889
888 890 $type = $setting['type'] ?? '';
889 891 if ( empty( $type ) ) {
890 - return new WP_Error( 'invalid_mail_type', __( 'Invalid email type.', 'mainwp' ) );
892 + return new WP_Error( 'invalid_mail_type', __( 'Invalid email type.', 'mainwp' ), array( 'status' => 400 ) );
891 893 }
892 894
893 895 // Get email settings.
894 896 $emails_settings = get_option( 'mainwp_settings_notification_emails' );
@@ -1060,9 +1062,9 @@
1060 1062 $custom_product_types = $this->safe_json_decode( $all_opts['custom_product_types'] ?? '' );
1061 1063
1062 1064 // Check if product type duplicated.
1063 1065 if ( isset( $custom_product_types[ $slug ] ) ) {
1064 - return new WP_Error( 'duplicate_title', __( 'Duplicate product type. Please choose another one.', 'mainwp' ) );
1066 + return new WP_Error( 'duplicate_title', __( 'Duplicate product type. Please choose another one.', 'mainwp' ), array( 'status' => 400 ) );
1065 1067 }
1066 1068 // Map product type.
1067 1069 $custom_product_types[ $slug ] = $title;
1068 1070 $all_opts['custom_product_types'] = wp_json_encode( $custom_product_types );
@@ -1118,9 +1120,9 @@
1118 1120 }
1119 1121
1120 1122 $slug = $request->get_param( 'slug' );
1121 1123 if ( empty( $slug ) ) {
1122 - return new WP_Error( 'invalid_slug', __( 'Invalid product type slug.', 'mainwp' ) );
1124 + return new WP_Error( 'invalid_slug', __( 'Invalid product type slug.', 'mainwp' ), array( 'status' => 400 ) );
1123 1125 }
1124 1126
1125 1127 // Default Product Type.
1126 1128 $default_product_types = Cost_Tracker_Admin::get_default_product_types();
@@ -1131,9 +1133,9 @@
1131 1133 $icon = ! empty( $body['icon'] ) ? $body['icon'] : 'deficon:folder open';
1132 1134
1133 1135 // Cannot change title if it's a default product type.
1134 1136 if ( array_key_exists( $slug, $default_product_types ) && $title !== $default_product_types[ $slug ] ) {
1135 - return new WP_Error( 'invalid_title', __( 'Cannot change title of default product type.', 'mainwp' ) );
1137 + return new WP_Error( 'invalid_title', __( 'Cannot change title of default product type.', 'mainwp' ), array( 'status' => 400 ) );
1136 1138 }
1137 1139
1138 1140 // Map product type.
1139 1141 $all_opts = Cost_Tracker_Utility::get_instance()->get_all_options();
@@ -1181,9 +1183,9 @@
1181 1183 public function delete_cost_tracker_product_type( $request ) { // phpcs:ignore -- NOSONAR - complex.
1182 1184 $slug = $request->get_param( 'slug' );
1183 1185
1184 1186 if ( empty( $slug ) ) {
1185 - return new WP_Error( 'invalid_slug', __( 'Invalid product type slug.', 'mainwp' ) );
1187 + return new WP_Error( 'invalid_slug', __( 'Invalid product type slug.', 'mainwp' ), array( 'status' => 400 ) );
1186 1188 }
1187 1189
1188 1190 // Default Product Type.
1189 1191 $default_product_types = Cost_Tracker_Admin::get_default_product_types();
@@ -1188,9 +1190,9 @@
1188 1190 // Default Product Type.
1189 1191 $default_product_types = Cost_Tracker_Admin::get_default_product_types();
1190 1192 // Cannot delete a default product type.
1191 1193 if ( array_key_exists( $slug, $default_product_types ) ) {
1192 - return new WP_Error( 'invalid_title', __( 'Cannot delete of default product type.', 'mainwp' ) );
1194 + return new WP_Error( 'invalid_title', __( 'Cannot delete of default product type.', 'mainwp' ), array( 'status' => 400 ) );
1193 1195 }
1194 1196
1195 1197 // Get all options.
1196 1198 $all_opts = Cost_Tracker_Utility::get_instance()->get_all_options();
@@ -1199,9 +1201,9 @@
1199 1201 $product_types_icons = $this->safe_json_decode( $all_opts['product_types_icons'] ?? '' );
1200 1202
1201 1203 // Check if product type exists.
1202 1204 if ( ! isset( $custom_product_types[ $slug ] ) ) {
1203 - return new WP_Error( 'invalid_slug', __( 'Product type does not exist.', 'mainwp' ) );
1205 + return new WP_Error( 'invalid_slug', __( 'Product type does not exist.', 'mainwp' ), array( 'status' => 404 ) );
1204 1206 }
1205 1207
1206 1208 // Delete product type.
1207 1209 unset( $custom_product_types[ $slug ] );
@@ -1249,9 +1251,9 @@
1249 1251 // Get payment methods.
1250 1252 $payment_methods = $this->safe_json_decode( $all_opts['custom_payment_methods'] ?? '' );
1251 1253
1252 1254 if ( isset( $payment_methods[ $slug ] ) ) {
1253 - return new WP_Error( 'duplicate_title', __( 'Duplicate payment method. Please choose another one.', 'mainwp' ) );
1255 + return new WP_Error( 'duplicate_title', __( 'Duplicate payment method. Please choose another one.', 'mainwp' ), array( 'status' => 400 ) );
1254 1256 }
1255 1257
1256 1258 // Save payment method.
1257 1259 $payment_methods[ $slug ] = $title;
@@ -1300,9 +1302,9 @@
1300 1302 $all_opts = Cost_Tracker_Utility::get_instance()->get_all_options();
1301 1303 // Get payment methods.
1302 1304 $payment_methods = $this->safe_json_decode( $all_opts['custom_payment_methods'] ?? '' );
1303 1305 if ( ! isset( $payment_methods[ $slug ] ) ) {
1304 - return new WP_Error( 'invalid_slug', __( 'Invalid payment method slug.', 'mainwp' ) );
1306 + return new WP_Error( 'invalid_slug', __( 'Invalid payment method slug.', 'mainwp' ), array( 'status' => 404 ) );
1305 1307 }
1306 1308
1307 1309 $payment_methods[ $slug ] = $body['title'];
1308 1310 $all_opts['custom_payment_methods'] = wp_json_encode( $payment_methods );
@@ -1336,9 +1338,9 @@
1336 1338 $all_opts = Cost_Tracker_Utility::get_instance()->get_all_options();
1337 1339 // Get payment methods.
1338 1340 $payment_methods = $this->safe_json_decode( $all_opts['custom_payment_methods'] ?? '' );
1339 1341 if ( ! isset( $payment_methods[ $slug ] ) ) {
1340 - return new WP_Error( 'invalid_slug', __( 'Invalid payment method slug.', 'mainwp' ) );
1342 + return new WP_Error( 'invalid_slug', __( 'Invalid payment method slug.', 'mainwp' ), array( 'status' => 404 ) );
1341 1343 }
1342 1344
1343 1345 unset( $payment_methods[ $slug ] );
1344 1346 $all_opts['custom_payment_methods'] = wp_json_encode( $payment_methods );
@@ -1494,9 +1496,9 @@
1494 1496
1495 1497 // Get API slug data.
1496 1498 $api = $request->get_param( 'api_slug' );
1497 1499 if ( empty( $api ) ) {
1498 - return new WP_Error( 'invalid_api_slug', __( 'API backup not exists.', 'mainwp' ) );
1500 + return new WP_Error( 'invalid_api_slug', __( 'API backup not exists.', 'mainwp' ), array( 'status' => 400 ) );
1499 1501 }
1500 1502
1501 1503 $updated = array();
1502 1504 // Update option if present.
@@ -1686,9 +1688,10 @@
1686 1688 $destroy_id = $request->get_param( 'destroy_id' ); // Get id from url.
1687 1689 if ( empty( $destroy_id ) ) {
1688 1690 return new WP_Error(
1689 1691 'missing_destroy_id',
1690 - __( 'Destroy ID is required.', 'mainwp' )
1692 + __( 'Destroy ID is required.', 'mainwp' ),
1693 + array( 'status' => 400 )
1691 1694 );
1692 1695 }
1693 1696
1694 1697 $destroy_data = get_transient( "mainwp_tool_destroy_job_{$destroy_id}" );
@@ -1694,9 +1697,10 @@
1694 1697 $destroy_data = get_transient( "mainwp_tool_destroy_job_{$destroy_id}" );
1695 1698 if ( ! $destroy_data ) {
1696 1699 return new WP_Error(
1697 1700 'destroy_session_job_not_found',
1698 - __( 'Destroy session job not found.', 'mainwp' )
1701 + __( 'Destroy session job not found.', 'mainwp' ),
1702 + array( 'status' => 404 )
1699 1703 );
1700 1704 }
1701 1705
1702 1706 return rest_ensure_response(
@@ -1843,9 +1847,10 @@
1843 1847
1844 1848 if ( empty( $renew_id ) ) {
1845 1849 return new WP_Error(
1846 1850 'missing_renew_id',
1847 - __( 'Renew ID is required.', 'mainwp' )
1851 + __( 'Renew ID is required.', 'mainwp' ),
1852 + array( 'status' => 400 )
1848 1853 );
1849 1854 }
1850 1855
1851 1856 // Get disconnect data.
@@ -1853,9 +1858,10 @@
1853 1858
1854 1859 if ( ! $renew_data ) {
1855 1860 return new WP_Error(
1856 1861 'renew_connection_job_not_found',
1857 - __( 'Renew connection job not found.', 'mainwp' )
1862 + __( 'Renew connection job not found.', 'mainwp' ),
1863 + array( 'status' => 404 )
1858 1864 );
1859 1865 }
1860 1866
1861 1867 return rest_ensure_response(
@@ -2090,9 +2096,10 @@
2090 2096
2091 2097 if ( empty( $disconnect_id ) ) {
2092 2098 return new WP_Error(
2093 2099 'missing_disconnect_id',
2094 - __( 'Disconnect ID is required.', 'mainwp' )
2100 + __( 'Disconnect ID is required.', 'mainwp' ),
2101 + array( 'status' => 400 )
2095 2102 );
2096 2103 }
2097 2104
2098 2105 // Get disconnect data.
@@ -2100,9 +2107,10 @@
2100 2107
2101 2108 if ( ! $disconnect_data ) {
2102 2109 return new WP_Error(
2103 2110 'disconnect_job_not_found',
2104 - __( 'Job not found.', 'mainwp' )
2111 + __( 'Job not found.', 'mainwp' ),
2112 + array( 'status' => 404 )
2105 2113 );
2106 2114 }
2107 2115
2108 2116 return rest_ensure_response(
@@ -2285,9 +2293,10 @@
2285 2293 ),
2286 2294 'plugin_automatic_daily_update' => array(
2287 2295 'required' => false,
2288 2296 'description' => __( 'Plugin automatic daily update.', 'mainwp' ),
2289 - 'type' => 'boolean',
2297 + 'type' => 'integer',
2298 + 'enum' => $val_bool,
2290 2299 'sanitize_callback' => $this->make_enum_sanitizer( $val_bool ),
2291 2300 'validate_callback' => $this->make_enum_validator( $val_bool ),
2292 2301 ),
2293 2302 'theme_automatic_daily_update' => array(
@@ -2292,9 +2301,10 @@
2292 2301 ),
2293 2302 'theme_automatic_daily_update' => array(
2294 2303 'required' => false,
2295 2304 'description' => __( 'Theme automatic daily update.', 'mainwp' ),
2296 - 'type' => 'boolean',
2305 + 'type' => 'integer',
2306 + 'enum' => $val_bool,
2297 2307 'sanitize_callback' => $this->make_enum_sanitizer( $val_bool ),
2298 2308 'validate_callback' => $this->make_enum_validator( $val_bool ),
2299 2309 ),
2300 2310 'trans_automatic_daily_update' => array(
@@ -2716,8 +2726,9 @@
2716 2726 if ( ! is_email( $value ) ) {
2717 2727 return new WP_Error(
2718 2728 'invalid_email',
2719 2729 /* translators: %s: Email address */ sprintf( __( 'Invalid email address: %s.', 'mainwp' ), $value ),
2730 + array( 'status' => 400 )
2720 2731 );
2721 2732 }
2722 2733 return true;
2723 2734 },
@@ -3037,13 +3048,13 @@
3037 3048 * Sanitize API slug.
3038 3049 *
3039 3050 * @param string $value API slug.
3040 3051 *
3041 - * @return string
3052 + * @return array|WP_Error The API backup definition the slug names, with the slug added to it, or WP_Error for a slug with no definition.
3042 3053 */
3043 3054 public function sanitize_api_slug( $value ) {
3044 3055 if ( empty( $value ) ) {
3045 - return new WP_Error( 'invalid_slug', __( 'Invalid API slug.', 'mainwp' ) );
3056 + return new WP_Error( 'invalid_slug', __( 'Invalid API slug.', 'mainwp' ), array( 'status' => 400 ) );
3046 3057 }
3047 3058
3048 3059 // Sanitize field.
3049 3060 $value = $this->sanitize_field( $value );
@@ -3048,9 +3059,9 @@
3048 3059 // Sanitize field.
3049 3060 $value = $this->sanitize_field( $value );
3050 3061 $api_backups = $this->get_api_backup_definitions(); // Get API backup definitions.
3051 3062 if ( ! array_key_exists( $value, $api_backups ) ) {
3052 - return new WP_Error( 'invalid_slug', __( 'API slug not exists.', 'mainwp' ) );
3063 + return new WP_Error( 'invalid_slug', __( 'API slug not exists.', 'mainwp' ), array( 'status' => 400 ) );
3053 3064 }
3054 3065 $api_backups[ $value ]['slug'] = $value; // Add slug to API backup definition.
3055 3066 return $api_backups[ $value ];
3056 3067 }
@@ -3064,9 +3075,9 @@
3064 3075 * @return bool|WP_Error
3065 3076 */
3066 3077 public function validate_api_slug( $value, $request ) {
3067 3078 if ( empty( $value ) ) {
3068 - return new WP_Error( 'invalid_slug', __( 'Invalid API slug.', 'mainwp' ) );
3079 + return new WP_Error( 'invalid_slug', __( 'Invalid API slug.', 'mainwp' ), array( 'status' => 400 ) );
3069 3080 }
3070 3081
3071 3082 $value = $this->sanitize_field( $value );
3072 3083
@@ -3072,9 +3083,9 @@
3072 3083
3073 3084 // Check if API slug exists.
3074 3085 $api_backups = $this->get_api_backup_definitions();
3075 3086 if ( ! array_key_exists( $value, $api_backups ) ) {
3076 - return new WP_Error( 'invalid_slug', __( 'API slug not exists.', 'mainwp' ) );
3087 + return new WP_Error( 'invalid_slug', __( 'API slug not exists.', 'mainwp' ), array( 'status' => 400 ) );
3077 3088 }
3078 3089
3079 3090 return true;
3080 3091 }
@@ -3096,9 +3107,9 @@
3096 3107
3097 3108 $value = $this->sanitize_field( $value ); // Sanitize field.
3098 3109 $symbol = Cost_Tracker_Utility::get_currency_symbol( $value ); // Get currency symbol.
3099 3110 if ( empty( $symbol ) ) {
3100 - return new WP_Error( 'invalid_currency', __( 'Invalid currency.', 'mainwp' ) );
3111 + return new WP_Error( 'invalid_currency', __( 'Invalid currency.', 'mainwp' ), array( 'status' => 400 ) );
3101 3112 }
3102 3113 return true;
3103 3114 }
3104 3115 /**
@@ -3115,9 +3126,9 @@
3115 3126 }
3116 3127
3117 3128 $value = $this->sanitize_field( $value );
3118 3129 if ( ! preg_match( '/^#([A-Fa-f0-9]{6})$/', $value ) ) {
3119 - return new WP_Error( 'invalid_color', __( 'Invalid color. Color must be in the format "#RRGGBB".', 'mainwp' ) );
3130 + return new WP_Error( 'invalid_color', __( 'Invalid color. Color must be in the format "#RRGGBB".', 'mainwp' ), array( 'status' => 400 ) );
3120 3131 }
3121 3132 return true;
3122 3133 }
3123 3134
@@ -3137,9 +3148,10 @@
3137 3148 $value = $this->sanitize_field( $value );
3138 3149 if ( ! preg_match( '/^[a-z0-9_\-]+:[a-z0-9_\-\s]+$/i', $value ) ) {
3139 3150 return new WP_Error(
3140 3151 'invalid_icon',
3141 - __( 'Invalid icon. Icon must be in the format "icon_type:icon_name".', 'mainwp' )
3152 + __( 'Invalid icon. Icon must be in the format "icon_type:icon_name".', 'mainwp' ),
3153 + array( 'status' => 400 )
3142 3154 );
3143 3155 }
3144 3156 return true;
3145 3157 }
@@ -3156,9 +3168,9 @@
3156 3168 * @return bool|WP_Error
3157 3169 */
3158 3170 public function validate_product_type_slug( $value, $request ) {
3159 3171 if ( empty( $value ) ) {
3160 - return new WP_Error( 'invalid_slug', __( 'Invalid product type slug.', 'mainwp' ) );
3172 + return new WP_Error( 'invalid_slug', __( 'Invalid product type slug.', 'mainwp' ), array( 'status' => 400 ) );
3161 3173 }
3162 3174
3163 3175 $value = $this->sanitize_field( $value );
3164 3176 $all_opts = Cost_Tracker_Utility::get_instance()->get_all_options();
@@ -3169,9 +3181,9 @@
3169 3181 $product_types = array_merge( $product_types, Cost_Tracker_Admin::get_default_product_types() );
3170 3182 }
3171 3183
3172 3184 if ( ! isset( $product_types[ $value ] ) ) {
3173 - return new WP_Error( 'invalid_slug', __( 'Product type does not exist.', 'mainwp' ) );
3185 + return new WP_Error( 'invalid_slug', __( 'Product type does not exist.', 'mainwp' ), array( 'status' => 400 ) );
3174 3186 }
3175 3187
3176 3188 return true;
3177 3189 }
@@ -3187,9 +3199,9 @@
3187 3199 * @return bool|WP_Error
3188 3200 */
3189 3201 public function validate_payment_method_slug( $value, $request ) { // phpcs:ignore -- NOSONAR - long method.
3190 3202 if ( empty( $value ) ) {
3191 - return new WP_Error( 'invalid_slug', __( 'Invalid payment method slug.', 'mainwp' ) );
3203 + return new WP_Error( 'invalid_slug', __( 'Invalid payment method slug.', 'mainwp' ), array( 'status' => 400 ) );
3192 3204 }
3193 3205
3194 3206 $value = $this->sanitize_field( $value );
3195 3207 $all_opts = Cost_Tracker_Utility::get_instance()->get_all_options();
@@ -3194,9 +3206,9 @@
3194 3206 $value = $this->sanitize_field( $value );
3195 3207 $all_opts = Cost_Tracker_Utility::get_instance()->get_all_options();
3196 3208 $payment_methods = $this->safe_json_decode( $all_opts['custom_payment_methods'] ?? '' );
3197 3209 if ( ! isset( $payment_methods[ $value ] ) ) {
3198 - return new WP_Error( 'invalid_slug', __( 'Payment method does not exist.', 'mainwp' ) );
3210 + return new WP_Error( 'invalid_slug', __( 'Payment method does not exist.', 'mainwp' ), array( 'status' => 400 ) );
3199 3211 }
3200 3212
3201 3213 return true;
3202 3214 }
@@ -3211,9 +3223,9 @@
3211 3223 * @return string|WP_Error
3212 3224 */
3213 3225 public function sanitize_email_type( $value ) { // phpcs:ignore -- NOSONAR - long method.
3214 3226 if ( empty( $value ) ) {
3215 - return new WP_Error( 'invalid_mail_type', __( 'Invalid email type.', 'mainwp' ) );
3227 + return new WP_Error( 'invalid_mail_type', __( 'Invalid email type.', 'mainwp' ), array( 'status' => 400 ) );
3216 3228 }
3217 3229
3218 3230 // Sanitize email type.
3219 3231 $value = $this->sanitize_field( $value );
@@ -3220,9 +3232,9 @@
3220 3232
3221 3233 // Get default email settings.
3222 3234 $email_setting = MainWP_Notification_Settings::get_default_emails_fields( $value, '', true );
3223 3235 if ( empty( $email_setting ) ) {
3224 - return new WP_Error( 'invalid_mail_type', __( 'Invalid email type.', 'mainwp' ) );
3236 + return new WP_Error( 'invalid_mail_type', __( 'Invalid email type.', 'mainwp' ), array( 'status' => 400 ) );
3225 3237 }
3226 3238
3227 3239 return array_merge( $email_setting, array( 'type' => $value ) );
3228 3240 }
@@ -3238,9 +3250,9 @@
3238 3250 * @return bool|WP_Error
3239 3251 */
3240 3252 public function validate_email_type( $value, $request ) { // phpcs:ignore -- NOSONAR - long method.
3241 3253 if ( empty( $value ) ) {
3242 - return new WP_Error( 'invalid_mail_type', __( 'Invalid email type.', 'mainwp' ) );
3254 + return new WP_Error( 'invalid_mail_type', __( 'Invalid email type.', 'mainwp' ), array( 'status' => 400 ) );
3243 3255 }
3244 3256
3245 3257 $value = $this->sanitize_field( $value );
3246 3258 // Get notification email types.
@@ -3246,9 +3258,9 @@
3246 3258 // Get notification email types.
3247 3259 $notification_emails = MainWP_Notification_Settings::get_notification_types();
3248 3260
3249 3261 if ( ! array_key_exists( $value, $notification_emails ) ) {
3250 - return new WP_Error( 'invalid_mail_type', __( 'Email type does not exist.', 'mainwp' ) );
3262 + return new WP_Error( 'invalid_mail_type', __( 'Email type does not exist.', 'mainwp' ), array( 'status' => 400 ) );
3251 3263 }
3252 3264
3253 3265 return true;
3254 3266 }
@@ -3267,9 +3279,9 @@
3267 3279 }
3268 3280
3269 3281 $int_value = (int) $this->sanitize_field( $value );
3270 3282 if ( $int_value < 1 || $int_value > 12 ) {
3271 - return new WP_Error( 'invalid_frequency_daily_update', __( 'Invalid frequency daily update. Use 1-12.', 'mainwp' ) );
3283 + return new WP_Error( 'invalid_frequency_daily_update', __( 'Invalid frequency daily update. Use 1-12.', 'mainwp' ), array( 'status' => 400 ) );
3272 3284 }
3273 3285 return true;
3274 3286 }
3275 3287
@@ -3291,8 +3303,9 @@
3291 3303 if ( preg_match( '/\d/', $value ) ) {
3292 3304 return new WP_Error(
3293 3305 'invalid_date_format_numeric',
3294 3306 __( 'Invalid date format: should not contain digits. Use format characters like F j, Y.', 'mainwp' ),
3307 + array( 'status' => 400 )
3295 3308 );
3296 3309 }
3297 3310
3298 3311 // Allows only valid PHP date() format characters and some basic separators.
@@ -3299,8 +3312,9 @@
3299 3312 if ( ! preg_match( '/^[dDjlNSwzWFmMntLoYyaABgGhHisueIOPTZcrU\-\.,:\/\s\\\\]+$/', $value ) ) {
3300 3313 return new WP_Error(
3301 3314 'invalid_date_format',
3302 3315 __( 'Invalid date format. Use valid PHP date format characters like F j, Y.', 'mainwp' ),
3316 + array( 'status' => 400 )
3303 3317 );
3304 3318 }
3305 3319
3306 3320 // Check rendering works.
@@ -3308,8 +3322,9 @@
3308 3322 if ( false === $date || '' === $date ) {
3309 3323 return new WP_Error(
3310 3324 'invalid_date_format_render',
3311 3325 __( 'Invalid or unsupported date format.', 'mainwp' ),
3326 + array( 'status' => 400 )
3312 3327 );
3313 3328 }
3314 3329
3315 3330 return true;
@@ -3332,8 +3347,9 @@
3332 3347 if ( preg_match( '/\d/', $value ) ) {
3333 3348 return new WP_Error(
3334 3349 'invalid_time_format_numeric',
3335 3350 __( 'Invalid time format: should not contain digits. Use format characters like g:i a.', 'mainwp' ),
3351 + array( 'status' => 400 )
3336 3352 );
3337 3353 }
3338 3354
3339 3355 // Allows only valid PHP date() format characters and some basic separators.
@@ -3340,8 +3356,9 @@
3340 3356 if ( ! preg_match( '/^[gGhHisAa\-\.,:\/\s\\\\]+$/', $value ) ) {
3341 3357 return new WP_Error(
3342 3358 'invalid_time_format',
3343 3359 __( 'Invalid time format. Use valid PHP time format characters like g:i a or H:i.', 'mainwp' ),
3360 + array( 'status' => 400 )
3344 3361 );
3345 3362 }
3346 3363
3347 3364 // Check rendering works.
@@ -3349,8 +3366,9 @@
3349 3366 if ( false === $time || '' === $time ) {
3350 3367 return new WP_Error(
3351 3368 'invalid_time_format_render',
3352 3369 __( 'Invalid or unsupported time format.', 'mainwp' ),
3370 + array( 'status' => 400 )
3353 3371 );
3354 3372 }
3355 3373
3356 3374 return true;
@@ -3388,8 +3406,9 @@
3388 3406 } catch ( Exception $e ) {
3389 3407 return new WP_Error(
3390 3408 'invalid_timezone_string',
3391 3409 __( 'Invalid timezone. Use a valid format like "UTC+0" or its equivalent name, e.g. "Europe/London".', 'mainwp' ),
3410 + array( 'status' => 400 )
3392 3411 );
3393 3412 }
3394 3413
3395 3414 // Convert to UTC offset.
@@ -3404,8 +3423,9 @@
3404 3423
3405 3424 return new WP_Error(
3406 3425 'invalid_timezone_string',
3407 3426 __( 'Invalid timezone. Use a valid format like "UTC+0" or its equivalent name, e.g. "Europe/London".', 'mainwp' ),
3427 + array( 'status' => 400 )
3408 3428 );
3409 3429 }
3410 3430
3411 3431 /**
@@ -3438,8 +3458,9 @@
3438 3458
3439 3459 return new WP_Error(
3440 3460 'invalid_timezone_string',
3441 3461 __( 'Invalid timezone. Use a valid format like UTC+0 or its equivalent name, e.g. "Europe/London".', 'mainwp' ),
3462 + array( 'status' => 400 )
3442 3463 );
3443 3464 }
3444 3465
3445 3466 /**
@@ -3452,13 +3473,14 @@
3452 3473 public function sanitize_mainwp_widgets( $value ) { // phpcs:ignore -- NOSONAR - complex.
3453 3474 if ( ! is_array( $value ) ) {
3454 3475 return new WP_Error(
3455 3476 'invalid_mainwp_widgets_type',
3456 - __( 'mainwp_widgets must be an object.', 'mainwp' )
3477 + __( 'mainwp_widgets must be an object.', 'mainwp' ),
3478 + array( 'status' => 400 )
3457 3479 );
3458 3480 }
3459 3481
3460 - $existing = get_user_option( 'mainwp_settings_show_widgets', array() );
3482 + $existing = get_user_option( 'mainwp_settings_show_widgets' );
3461 3483 if ( ! is_array( $existing ) ) {
3462 3484 $existing = array();
3463 3485 }
3464 3486
@@ -3490,8 +3512,9 @@
3490 3512 if ( ! is_array( $value ) ) {
3491 3513 return new WP_Error(
3492 3514 'invalid_mainwp_widgets_type',
3493 3515 __( 'mainwp_widgets must be an object.', 'mainwp' ),
3516 + array( 'status' => 400 )
3494 3517 );
3495 3518 }
3496 3519
3497 3520 // Sanitize field data.
@@ -3496,9 +3519,9 @@
3496 3519
3497 3520 // Sanitize field data.
3498 3521 $values = array_map( 'sanitize_text_field', wp_unslash( $value ) );
3499 3522
3500 - $widgets = get_user_option( 'mainwp_settings_show_widgets', array() );
3523 + $widgets = get_user_option( 'mainwp_settings_show_widgets' );
3501 3524 $allowed = array_keys( $widgets );
3502 3525 $keys = array_keys( $values );
3503 3526
3504 3527 $invalid_keys = array_values( array_diff( $keys, $allowed ) );
@@ -3511,8 +3534,9 @@
3511 3534 __( 'Invalid widget keys: %1$s. Allowed keys: %2$s.', 'mainwp' ),
3512 3535 esc_html( implode( ', ', $invalid_keys ) ),
3513 3536 esc_html( implode( ', ', $allowed ) )
3514 3537 ),
3538 + array( 'status' => 400 )
3515 3539 );
3516 3540 }
3517 3541
3518 3542 foreach ( $values as $k => $v ) {
@@ -3519,8 +3543,9 @@
3519 3543 if ( is_array( $v ) || is_object( $v ) ) {
3520 3544 return new WP_Error(
3521 3545 'invalid_mainwp_widgets_value_type',
3522 3546 /* translators: %s: Widget key name */ sprintf( __( 'Value for "%s" must be 0 or 1.', 'mainwp' ), esc_html( $k ) ),
3547 + array( 'status' => 400 )
3523 3548 );
3524 3549 }
3525 3550 $int = (int) $v;
3526 3551 if ( 0 !== $int && 1 !== $int ) {
@@ -3526,8 +3551,9 @@
3526 3551 if ( 0 !== $int && 1 !== $int ) {
3527 3552 return new WP_Error(
3528 3553 'invalid_mainwp_widgets_value',
3529 3554 /* translators: %s: Widget key name */ sprintf( __( 'Invalid value for "%s". Allowed: 0 or 1.', 'mainwp' ), esc_html( $k ) ),
3555 + array( 'status' => 400 )
3530 3556 );
3531 3557 }
3532 3558 }
3533 3559
@@ -3548,9 +3574,10 @@
3548 3574
3549 3575 if ( ! is_array( $value ) ) {
3550 3576 return new WP_Error(
3551 3577 'invalid_status_codes_type',
3552 - __( 'Status codes must be an array.', 'mainwp' )
3578 + __( 'Status codes must be an array.', 'mainwp' ),
3579 + array( 'status' => 400 )
3553 3580 );
3554 3581 }
3555 3582
3556 3583 // Parse ID list to ensure all values are integers.
@@ -3618,9 +3645,10 @@
3618 3645 public function sanitize_sync_data( $value ) { // phpcs:ignore -- NOSONAR - complex.
3619 3646 if ( ! is_array( $value ) ) {
3620 3647 return new WP_Error(
3621 3648 'invalid_sync_data_type',
3622 - __( 'Sync data must be an object.', 'mainwp' )
3649 + __( 'Sync data must be an object.', 'mainwp' ),
3650 + array( 'status' => 400 )
3623 3651 );
3624 3652 }
3625 3653
3626 3654 $values = array_map( 'sanitize_text_field', wp_unslash( $value ) );
@@ -3655,9 +3683,10 @@
3655 3683
3656 3684 if ( ! is_array( $value ) ) {
3657 3685 return new WP_Error(
3658 3686 'invalid_sync_data_type',
3659 - __( 'Sync data must be an object.', 'mainwp' )
3687 + __( 'Sync data must be an object.', 'mainwp' ),
3688 + array( 'status' => 400 )
3660 3689 );
3661 3690 }
3662 3691 // Sanitize field data.
3663 3692 $values = array_map( 'sanitize_text_field', wp_unslash( $value ) );
@@ -3676,8 +3705,9 @@
3676 3705 __( 'Invalid sync data keys: %1$s. Allowed keys: %2$s.', 'mainwp' ),
3677 3706 esc_html( implode( ', ', $invalid_keys ) ),
3678 3707 esc_html( implode( ', ', $allowed ) )
3679 3708 ),
3709 + array( 'status' => 400 )
3680 3710 );
3681 3711 }
3682 3712
3683 3713 foreach ( $values as $k => $v ) {
@@ -3684,8 +3714,9 @@
3684 3714 if ( is_array( $v ) || is_object( $v ) ) {
3685 3715 return new WP_Error(
3686 3716 'invalid_sync_data_type',
3687 3717 /* translators: %s: Sync data key name */ sprintf( __( 'Value for "%s" must be 0 or 1.', 'mainwp' ), esc_html( $k ) ),
3718 + array( 'status' => 400 )
3688 3719 );
3689 3720 }
3690 3721 $int = (int) $v;
3691 3722 if ( 0 !== $int && 1 !== $int ) {
@@ -3691,8 +3722,9 @@
3691 3722 if ( 0 !== $int && 1 !== $int ) {
3692 3723 return new WP_Error(
3693 3724 'invalid_sync_data_type',
3694 3725 /* translators: %s: Sync data key name */ sprintf( __( 'Invalid value for "%s". Allowed: 0 or 1.', 'mainwp' ), esc_html( $k ) ),
3726 + array( 'status' => 400 )
3695 3727 );
3696 3728 }
3697 3729 }
3698 3730
@@ -3715,8 +3747,9 @@
3715 3747 if ( ! preg_match( '/^(?:[01]?\d|2[0-3]):[0-5]\d$/', $value ) ) {
3716 3748 return new WP_Error(
3717 3749 'invalid_time_update',
3718 3750 __( 'Invalid time. Use whole hours in 24h format like 0:00, 9:00, 23:00, etc...', 'mainwp' ),
3751 + array( 'status' => 400 )
3719 3752 );
3720 3753 }
3721 3754
3722 3755 return $value;
@@ -3742,8 +3775,9 @@
3742 3775
3743 3776 return new WP_Error(
3744 3777 'invalid_time_update',
3745 3778 __( 'Invalid time. Use whole hours in 24h format like 0:00, 9:00, 23:00, etc...', 'mainwp' ),
3779 + array( 'status' => 400 )
3746 3780 );
3747 3781 }
3748 3782
3749 3783 /**
@@ -3775,15 +3809,17 @@
3775 3809 if ( 'module-api-backups' === $primary_backup_value && ( null !== $backup_feature && '0' !== (string) $backup_feature && 0 !== (int) $backup_feature ) ) {
3776 3810 // When primary backup is module-api-backups, legacy feature must be 0.
3777 3811 return new WP_Error(
3778 3812 'invalid_backup_config',
3779 - __( 'When mainwp_primary_backup is "module-api-backups", mainwp_enable_legacy_backup_feature must be disabled (0).', 'mainwp' )
3813 + __( 'When mainwp_primary_backup is "module-api-backups", mainwp_enable_legacy_backup_feature must be disabled (0).', 'mainwp' ),
3814 + array( 'status' => 400 )
3780 3815 );
3781 3816 } elseif ( '' === $primary_backup_value && ( null !== $backup_feature && '1' !== (string) $backup_feature && 1 !== (int) $backup_feature ) ) {
3782 3817 // When primary backup is not module-api-backups, legacy feature must be 1.
3783 3818 return new WP_Error(
3784 3819 'invalid_backup_config',
3785 - __( 'When mainwp_primary_backup is not "module-api-backups", mainwp_enable_legacy_backup_feature must be enabled (1).', 'mainwp' )
3820 + __( 'When mainwp_primary_backup is not "module-api-backups", mainwp_enable_legacy_backup_feature must be enabled (1).', 'mainwp' ),
3821 + array( 'status' => 400 )
3786 3822 );
3787 3823 }
3788 3824 }
3789 3825
@@ -3821,8 +3857,9 @@
3821 3857 // Return error.
3822 3858 return new WP_Error(
3823 3859 'empty_body',
3824 3860 __( 'Request body is empty.', 'mainwp' ),
3861 + array( 'status' => 400 )
3825 3862 );
3826 3863 }
3827 3864
3828 3865 /**
@@ -3836,8 +3873,9 @@
3836 3873 if ( 'application/json' !== $content_type ) {
3837 3874 return new WP_Error(
3838 3875 'invalid_content_type',
3839 3876 __( 'Invalid content type. Expected application/json.', 'mainwp' ),
3877 + array( 'status' => 400 )
3840 3878 );
3841 3879 }
3842 3880 return true;
3843 3881 }
@@ -3871,9 +3909,10 @@
3871 3909 $websites = MainWP_DB::instance()->get_sites();
3872 3910 if ( empty( $websites ) ) {
3873 3911 return new WP_Error(
3874 3912 'no_website_found',
3875 - __( 'No website found.', 'mainwp' )
3913 + __( 'No website found.', 'mainwp' ),
3914 + array( 'status' => 404 )
3876 3915 );
3877 3916 }
3878 3917 return $websites;
3879 3918 }
@@ -3914,8 +3953,9 @@
3914 3953 if ( empty( $user_id ) ) {
3915 3954 return new WP_Error(
3916 3955 'invalid_user',
3917 3956 __( 'User not authenticated.', 'mainwp' ),
3957 + array( 'status' => 401 )
3918 3958 );
3919 3959 }
3920 3960 return $user_id;
3921 3961 }
@@ -3967,9 +4007,9 @@
3967 4007 MainWP_Utility::update_option( $option_name, $val );
3968 4008 return true;
3969 4009 }
3970 4010 // translators: %s: Missing key.
3971 - return new WP_Error( 'missing_key', sprintf( __( 'Missing key %s.', 'mainwp' ), esc_html( $key ) ), );
4011 + return new WP_Error( 'missing_key', sprintf( __( 'Missing key %s.', 'mainwp' ), esc_html( $key ) ), array( 'status' => 400 ) );
3972 4012 }
3973 4013
3974 4014 /**
3975 4015 * Format date.
@@ -4116,10 +4156,14 @@
4116 4156 */
4117 4157 protected function get_general_settings_data() {
4118 4158 // Default Setting.
4119 4159 $default_setting = MainWP_Settings_Indicator::get_defaults_value();
4120 - $show_widgets = get_user_option( 'mainwp_settings_show_widgets', array() );
4160 + $show_widgets = get_user_option( 'mainwp_settings_show_widgets' );
4121 4161
4162 + if ( ! is_array( $show_widgets ) ) {
4163 + $show_widgets = array();
4164 + }
4165 +
4122 4166 // Map settings.
4123 4167 $gmt_offset = (float) get_option( 'gmt_offset', 0 );
4124 4168 $tz_sign = $gmt_offset >= 0 ? '+' : '-';
4125 4169 $tz_value = (string) abs( $gmt_offset );
@@ -4152,8 +4196,15 @@
4152 4196 );
4153 4197 }
4154 4198
4155 4199 $enable_legacy_backup = get_option( 'mainwp_enableLegacyBackupFeature' );
4200 +
4201 + // get_user_option()'s second argument is a user id, not a default, and the defaults array keys this on the option name rather than the response field name.
4202 + $sidebar_position = get_user_option( 'mainwp_sidebarPosition' );
4203 + if ( false === $sidebar_position ) {
4204 + $sidebar_position = $default_setting['mainwp_sidebarPosition'];
4205 + }
4206 +
4156 4207 return array(
4157 4208 // General settings.
4158 4209 'time_daily_update' => get_option( 'mainwp_timeDailyUpdate', $default_setting['mainwp_timeDailyUpdate'] ),
4159 4210 'frequency_daily_update' => (int) get_option( 'mainwp_frequencyDailyUpdate', $default_setting['mainwp_frequencyDailyUpdate'] ),
@@ -4159,9 +4210,9 @@
4159 4210 'frequency_daily_update' => (int) get_option( 'mainwp_frequencyDailyUpdate', $default_setting['mainwp_frequencyDailyUpdate'] ),
4160 4211 'date_format' => get_option( 'date_format', $default_setting['date_format'] ),
4161 4212 'time_format' => get_option( 'time_format', $default_setting['time_format'] ),
4162 4213 'timezone_string' => $tz_string,
4163 - 'sidebar_position' => get_user_option( ' mainwp_sidebarPosition', $default_setting['sidebar_position'] ),
4214 + 'sidebar_position' => (int) $sidebar_position,
4164 4215 'hide_update_everything' => get_option( 'mainwp_hide_update_everything', $default_setting['mainwp_hide_update_everything'] ),
4165 4216 'mainwp_widgets' => $show_widgets,
4166 4217 'plugin_automatic_daily_update' => (int) get_option( 'mainwp_pluginAutomaticDailyUpdate', $default_setting['mainwp_pluginAutomaticDailyUpdate'] ),
4167 4218 'theme_automatic_daily_update' => (int) get_option( 'mainwp_themeAutomaticDailyUpdate', $default_setting['mainwp_themeAutomaticDailyUpdate'] ),