array( type, label, description, options, ... ). * * Supported types: heading, text, url, image, textarea, plaintext, lines, * checkbox, color, select, number, checkboxes. * * @return array */ public function get_fields() { return array(); } /** * Whether the module renders its own admin tab content. * * @return bool */ public function has_custom_page() { return false; } /** * Custom tab content for modules where has_custom_page() is true. */ public function render_custom_page() {} /** * Called after the module settings were saved. */ public function on_settings_saved() {} /** * Called when the module is switched on or off on the dashboard. * * @param bool $enabled New state. */ public function on_toggle( $enabled ) {} // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found -- Overridden by child classes. /** * Whether this module has a settings tab. * * @return bool */ public function has_settings() { return $this->has_custom_page() || (bool) $this->get_fields(); } /** * Option name used to store this module's settings. * * @return string */ public function option_name() { return 'toolvane_' . $this->get_id(); } /** * Settings merged with defaults. * * @return array */ public function get_settings() { if ( null === $this->settings ) { $saved = get_option( $this->option_name(), array() ); $this->settings = wp_parse_args( is_array( $saved ) ? $saved : array(), $this->get_defaults() ); } return $this->settings; } /** * Single setting value. * * @param string $key Setting key. * @param mixed $fallback Value returned when the key does not exist. * @return mixed */ public function get( $key, $fallback = null ) { $settings = $this->get_settings(); return array_key_exists( $key, $settings ) ? $settings[ $key ] : $fallback; } /** * Clears the settings cache. */ public function flush_settings() { $this->settings = null; } /** * Sanitizes submitted settings based on the field definitions. * Hooked through register_setting(); it may run twice on the first save, so it is idempotent. * * @param mixed $input Raw input. * @return array */ public function sanitize( $input ) { $input = is_array( $input ) ? $input : array(); $defaults = $this->get_defaults(); $old = get_option( $this->option_name(), array() ); $old = is_array( $old ) ? $old : array(); $clean = array(); foreach ( $this->get_fields() as $key => $field ) { if ( 'heading' === $field['type'] ) { continue; } $value = isset( $input[ $key ] ) ? $input[ $key ] : null; $default = isset( $defaults[ $key ] ) ? $defaults[ $key ] : ''; switch ( $field['type'] ) { case 'checkbox': $clean[ $key ] = ! empty( $value ); break; case 'url': case 'image': $clean[ $key ] = esc_url_raw( trim( (string) $value ) ); break; case 'textarea': $clean[ $key ] = wp_kses_post( (string) $value ); break; case 'plaintext': $clean[ $key ] = sanitize_textarea_field( (string) $value ); break; case 'lines': $lines = preg_split( '/\r\n|\r|\n/', (string) $value ); $lines = array_filter( array_map( 'trim', array_map( 'sanitize_text_field', $lines ) ), 'strlen' ); $clean[ $key ] = implode( "\n", array_unique( $lines ) ); break; case 'color': $color = sanitize_hex_color( (string) $value ); $clean[ $key ] = $color ? $color : ''; break; case 'select': $options = isset( $field['options'] ) ? $field['options'] : array(); $clean[ $key ] = array_key_exists( (string) $value, $options ) ? (string) $value : $default; break; case 'number': $num = is_numeric( $value ) ? (int) $value : (int) $default; if ( isset( $field['min'] ) ) { $num = max( (int) $field['min'], $num ); } if ( isset( $field['max'] ) ) { $num = min( (int) $field['max'], $num ); } $clean[ $key ] = $num; break; case 'checkboxes': $options = isset( $field['options'] ) ? array_map( 'strval', array_keys( $field['options'] ) ) : array(); $values = array_map( 'sanitize_text_field', array_map( 'strval', (array) $value ) ); $clean[ $key ] = array_values( array_intersect( $values, $options ) ); break; default: $clean[ $key ] = sanitize_text_field( (string) $value ); } } $this->flush_settings(); return $clean; } /** * Splits a "lines" setting into an array. * * @param string $key Setting key. * @return string[] */ protected function get_lines( $key ) { $value = (string) $this->get( $key, '' ); if ( '' === trim( $value ) ) { return array(); } return array_values( array_filter( array_map( 'trim', explode( "\n", $value ) ), 'strlen' ) ); } /** * Role choices for "checkboxes" fields. * * @return array */ protected function role_options() { $roles = array(); foreach ( wp_roles()->get_names() as $slug => $name ) { $roles[ $slug ] = translate_user_role( $name ); } return $roles; } /** * Public post types that can be selected in settings. * * @param string[] $exclude Post types to leave out. * @return array name => label */ protected function post_type_options( array $exclude = array() ) { $exclude = array_merge( $exclude, array( 'attachment', 'wp_navigation', 'wp_template', 'wp_template_part', 'wp_global_styles', 'wp_font_family', 'wp_font_face', 'shop_order', 'shop_order_refund', 'shop_order_placehold', 'shop_coupon', 'shop_subscription', 'elementor_snippet', 'elementor_font', 'elementor_icons', 'e-landing-page-kit' ) ); $types = array(); foreach ( get_post_types( array( 'show_ui' => true ), 'objects' ) as $type ) { if ( in_array( $type->name, $exclude, true ) ) { continue; } $types[ $type->name ] = $type->labels->singular_name . ' (' . $type->name . ')'; } return $types; } /** * Whether the current user has at least one of the given roles. * * @param array $roles Role slugs. * @return bool */ protected function current_user_has_role( array $roles ) { if ( ! is_user_logged_in() || ! $roles ) { return false; } $user = wp_get_current_user(); if ( array_intersect( $roles, (array) $user->roles ) ) { return true; } return is_multisite() && is_super_admin() && in_array( 'administrator', $roles, true ); } }